1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_q31_to_float.c
4  * Description:  Converts the elements of the Q31 vector to floating-point vector
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13  *
14  * SPDX-License-Identifier: Apache-2.0
15  *
16  * Licensed under the Apache License, Version 2.0 (the License); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #include "dsp/support_functions.h"
30 
31 /**
32   @ingroup groupSupport
33  */
34 
35 /**
36  * @defgroup q31_to_x  Convert 32-bit fixed point value
37  */
38 
39 /**
40   @addtogroup q31_to_x
41   @{
42  */
43 
44 /**
45   @brief         Converts the elements of the Q31 vector to floating-point vector.
46   @param[in]     pSrc       points to the Q31 input vector
47   @param[out]    pDst       points to the floating-point output vector
48   @param[in]     blockSize  number of samples in each vector
49 
50   @par           Details
51                    The equation used for the conversion process is:
52   <pre>
53       pDst[n] = (float32_t) pSrc[n] / 2147483648;   0 <= n < blockSize.
54   </pre>
55  */
56 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_q31_to_float(const q31_t * pSrc,float32_t * pDst,uint32_t blockSize)57 ARM_DSP_ATTRIBUTE void arm_q31_to_float(
58   const q31_t * pSrc,
59         float32_t * pDst,
60         uint32_t blockSize)
61 {
62     uint32_t  blkCnt;           /* loop counters */
63     q31x4_t vecDst;
64     q31_t const *pSrcVec;
65 
66     pSrcVec = (q31_t const *) pSrc;
67     blkCnt = blockSize >> 2;
68     while (blkCnt > 0U)
69     {
70         /* C = (float32_t) A / 2147483648 */
71         /* convert from q31 to float and then store the results in the destination buffer */
72         vecDst = vld1q(pSrcVec);
73         pSrcVec += 4;
74         vstrwq(pDst, vcvtq_n_f32_s32(vecDst, 31));
75         pDst += 4;
76         /*
77          * Decrement the blockSize loop counter
78          */
79         blkCnt--;
80     }
81     /*
82      * tail
83      * (will be merged thru tail predication)
84      */
85     blkCnt = blockSize & 3;
86     while (blkCnt > 0U)
87     {
88       /* C = (float32_t) A / 2147483648 */
89 
90       /* Convert from q31 to float and store result in destination buffer */
91       *pDst++ = ((float32_t) *pSrcVec++ / 2147483648.0f);
92 
93       /* Decrement loop counter */
94       blkCnt--;
95     }
96 }
97 
98 #else
99 #if defined(ARM_MATH_NEON_EXPERIMENTAL)
arm_q31_to_float(const q31_t * pSrc,float32_t * pDst,uint32_t blockSize)100 ARM_DSP_ATTRIBUTE void arm_q31_to_float(
101   const q31_t * pSrc,
102         float32_t * pDst,
103         uint32_t blockSize)
104 {
105   const q31_t *pIn = pSrc;                             /* Src pointer */
106   uint32_t blkCnt;                               /* loop counter */
107 
108   int32x4_t inV;
109   float32x4_t outV;
110 
111   blkCnt = blockSize >> 2U;
112 
113   /* Compute 4 outputs at a time.
114    ** a second loop below computes the remaining 1 to 3 samples. */
115   while (blkCnt > 0U)
116   {
117     /* C = (float32_t) A / 2147483648 */
118     /* Convert from q31 to float and then store the results in the destination buffer */
119     inV = vld1q_s32(pIn);
120     pIn += 4;
121 
122     outV = vcvtq_n_f32_s32(inV,31);
123 
124     vst1q_f32(pDst, outV);
125     pDst += 4;
126 
127     /* Decrement the loop counter */
128     blkCnt--;
129   }
130 
131   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
132    ** No loop unrolling is used. */
133   blkCnt = blockSize & 3;
134 
135 
136   while (blkCnt > 0U)
137   {
138     /* C = (float32_t) A / 2147483648 */
139     /* Convert from q31 to float and then store the results in the destination buffer */
140     *pDst++ = ((float32_t) * pIn++ / 2147483648.0f);
141 
142     /* Decrement the loop counter */
143     blkCnt--;
144   }
145 }
146 #else
arm_q31_to_float(const q31_t * pSrc,float32_t * pDst,uint32_t blockSize)147 ARM_DSP_ATTRIBUTE void arm_q31_to_float(
148   const q31_t * pSrc,
149   float32_t * pDst,
150   uint32_t blockSize)
151 {
152   const q31_t *pIn = pSrc;                             /* Src pointer */
153   uint32_t blkCnt;                               /* loop counter */
154 
155 #if defined (ARM_MATH_LOOPUNROLL)
156 
157   /* Loop unrolling */
158   blkCnt = blockSize >> 2U;
159 
160   while (blkCnt > 0U)
161   {
162     /* C = (float32_t) A / 2147483648 */
163 
164     /* Convert from q31 to float and store result in destination buffer */
165     *pDst++ = ((float32_t) *pIn++ / 2147483648.0f);
166     *pDst++ = ((float32_t) *pIn++ / 2147483648.0f);
167     *pDst++ = ((float32_t) *pIn++ / 2147483648.0f);
168     *pDst++ = ((float32_t) *pIn++ / 2147483648.0f);
169 
170     /* Decrement loop counter */
171     blkCnt--;
172   }
173 
174   /* Loop unrolling: Compute remaining outputs */
175   blkCnt = blockSize % 0x4U;
176 
177 #else
178 
179   /* Initialize blkCnt with number of samples */
180   blkCnt = blockSize;
181 
182 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
183 
184   while (blkCnt > 0U)
185   {
186     /* C = (float32_t) A / 2147483648 */
187 
188     /* Convert from q31 to float and store result in destination buffer */
189     *pDst++ = ((float32_t) *pIn++ / 2147483648.0f);
190 
191     /* Decrement loop counter */
192     blkCnt--;
193   }
194 
195 }
196 #endif /* #if defined(ARM_MATH_NEON) */
197 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
198 
199 /**
200   @} end of q31_to_x group
201  */
202