Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / SupportFunctions / arm_float_to_q31.c
1 /* ----------------------------------------------------------------------------   
2 * Copyright (C) 2010 ARM Limited. All rights reserved.   
3 *   
4 * $Date:        15. July 2011  
5 * $Revision:    V1.0.10  
6 *   
7 * Project:          CMSIS DSP Library   
8 * Title:                arm_float_to_q31.c   
9 *   
10 * Description:  Converts the elements of the floating-point vector to Q31 vector.   
11 *   
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *  
14 * Version 1.0.10 2011/7/15 
15 *    Big Endian support added and Merged M0 and M3/M4 Source code.  
16 *   
17 * Version 1.0.3 2010/11/29  
18 *    Re-organized the CMSIS folders and updated documentation.   
19 *    
20 * Version 1.0.2 2010/11/11   
21 *    Documentation updated.    
22 *   
23 * Version 1.0.1 2010/10/05    
24 *    Production release and review comments incorporated.   
25 *   
26 * Version 1.0.0 2010/09/20    
27 *    Production release and review comments incorporated.   
28 * ---------------------------------------------------------------------------- */
29
30 #include "arm_math.h"
31
32 /**   
33  * @ingroup groupSupport   
34  */
35
36 /**   
37  * @defgroup float_to_x  Convert 32-bit floating point value   
38  */
39
40 /**   
41  * @addtogroup float_to_x   
42  * @{   
43  */
44
45 /**   
46  * @brief Converts the elements of the floating-point vector to Q31 vector.   
47  * @param[in]       *pSrc points to the floating-point input vector   
48  * @param[out]      *pDst points to the Q31 output vector  
49  * @param[in]       blockSize length of the input vector   
50  * @return none.   
51  *   
52  *\par Description:   
53  * \par  
54  * The equation used for the conversion process is:   
55  *  
56  * <pre>   
57  *      pDst[n] = (q31_t)(pSrc[n] * 2147483648);   0 <= n < blockSize.   
58  * </pre>   
59  * <b>Scaling and Overflow Behavior:</b>   
60  * \par   
61  * The function uses saturating arithmetic.   
62  * Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] will be saturated.   
63  *  
64  * \note In order to apply rounding, the library should be rebuilt with the ROUNDING macro    
65  * defined in the preprocessor section of project options.    
66  */
67
68
69 void arm_float_to_q31(
70   float32_t * pSrc,
71   q31_t * pDst,
72   uint32_t blockSize)
73 {
74   float32_t *pIn = pSrc;                         /* Src pointer */
75   uint32_t blkCnt;                               /* loop counter */
76
77 #ifdef ARM_MATH_ROUNDING
78
79   float32_t in;
80
81 #endif /*      #ifdef ARM_MATH_ROUNDING        */
82
83 #ifndef ARM_MATH_CM0
84
85   /* Run the below code for Cortex-M4 and Cortex-M3 */
86
87   /*loop Unrolling */
88   blkCnt = blockSize >> 2u;
89
90   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
91    ** a second loop below computes the remaining 1 to 3 samples. */
92   while(blkCnt > 0u)
93   {
94
95 #ifdef ARM_MATH_ROUNDING
96
97     /* C = A * 32768 */
98     /* convert from float to Q31 and then store the results in the destination buffer */
99     in = *pIn++;
100     in = (in * 2147483648.0f);
101     in += in > 0 ? 0.5 : -0.5;
102     *pDst++ = clip_q63_to_q31((q63_t) (in));
103
104     in = *pIn++;
105     in = (in * 2147483648.0f);
106     in += in > 0 ? 0.5 : -0.5;
107     *pDst++ = clip_q63_to_q31((q63_t) (in));
108
109     in = *pIn++;
110     in = (in * 2147483648.0f);
111     in += in > 0 ? 0.5 : -0.5;
112     *pDst++ = clip_q63_to_q31((q63_t) (in));
113
114     in = *pIn++;
115     in = (in * 2147483648.0f);
116     in += in > 0 ? 0.5 : -0.5;
117     *pDst++ = clip_q63_to_q31((q63_t) (in));
118
119 #else
120
121     /* C = A * 2147483648 */
122     /* convert from float to Q31 and then store the results in the destination buffer */
123     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
124     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
125     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
126     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
127
128 #endif /*      #ifdef ARM_MATH_ROUNDING        */
129
130     /* Decrement the loop counter */
131     blkCnt--;
132   }
133
134   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
135    ** No loop unrolling is used. */
136   blkCnt = blockSize % 0x4u;
137
138   while(blkCnt > 0u)
139   {
140
141 #ifdef ARM_MATH_ROUNDING
142
143     /* C = A * 2147483648 */
144     /* convert from float to Q31 and then store the results in the destination buffer */
145     in = *pIn++;
146     in = (in * 2147483648.0f);
147     in += in > 0 ? 0.5 : -0.5;
148     *pDst++ = clip_q63_to_q31((q63_t) (in));
149
150 #else
151
152     /* C = A * 2147483648 */
153     /* convert from float to Q31 and then store the results in the destination buffer */
154     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
155
156 #endif /*      #ifdef ARM_MATH_ROUNDING        */
157
158     /* Decrement the loop counter */
159     blkCnt--;
160   }
161
162
163 #else
164
165   /* Run the below code for Cortex-M0 */
166
167   /* Loop over blockSize number of values */
168   blkCnt = blockSize;
169
170   while(blkCnt > 0u)
171   {
172
173 #ifdef ARM_MATH_ROUNDING
174
175     /* C = A * 2147483648 */
176     /* convert from float to Q31 and then store the results in the destination buffer */
177     in = *pIn++;
178     in = (in * 2147483648.0f);
179     in += in > 0 ? 0.5f : -0.5f;
180     *pDst++ = clip_q63_to_q31((q63_t) (in));
181
182 #else
183
184     /* C = A * 2147483648 */
185     /* convert from float to Q31 and then store the results in the destination buffer */
186     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
187
188 #endif /*      #ifdef ARM_MATH_ROUNDING        */
189
190     /* Decrement the loop counter */
191     blkCnt--;
192   }
193
194 #endif /* #ifndef ARM_MATH_CM0 */
195
196 }
197
198 /**   
199  * @} end of float_to_x group   
200  */