1 /* ----------------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_float_to_q31.c
10 * Description: Converts the elements of the floating-point vector to Q31 vector.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * ---------------------------------------------------------------------------- */
33 * @ingroup groupSupport
37 * @defgroup float_to_x Convert 32-bit floating point value
41 * @addtogroup float_to_x
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
54 * The equation used for the conversion process is:
57 * pDst[n] = (q31_t)(pSrc[n] * 2147483648); 0 <= n < blockSize.
59 * <b>Scaling and Overflow Behavior:</b>
61 * The function uses saturating arithmetic.
62 * Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] will be saturated.
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.
69 void arm_float_to_q31(
74 float32_t *pIn = pSrc; /* Src pointer */
75 uint32_t blkCnt; /* loop counter */
77 #ifdef ARM_MATH_ROUNDING
81 #endif /* #ifdef ARM_MATH_ROUNDING */
85 /* Run the below code for Cortex-M4 and Cortex-M3 */
88 blkCnt = blockSize >> 2u;
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. */
95 #ifdef ARM_MATH_ROUNDING
98 /* convert from float to Q31 and then store the results in the destination buffer */
100 in = (in * 2147483648.0f);
101 in += in > 0 ? 0.5 : -0.5;
102 *pDst++ = clip_q63_to_q31((q63_t) (in));
105 in = (in * 2147483648.0f);
106 in += in > 0 ? 0.5 : -0.5;
107 *pDst++ = clip_q63_to_q31((q63_t) (in));
110 in = (in * 2147483648.0f);
111 in += in > 0 ? 0.5 : -0.5;
112 *pDst++ = clip_q63_to_q31((q63_t) (in));
115 in = (in * 2147483648.0f);
116 in += in > 0 ? 0.5 : -0.5;
117 *pDst++ = clip_q63_to_q31((q63_t) (in));
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));
128 #endif /* #ifdef ARM_MATH_ROUNDING */
130 /* Decrement the loop counter */
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;
141 #ifdef ARM_MATH_ROUNDING
143 /* C = A * 2147483648 */
144 /* convert from float to Q31 and then store the results in the destination buffer */
146 in = (in * 2147483648.0f);
147 in += in > 0 ? 0.5 : -0.5;
148 *pDst++ = clip_q63_to_q31((q63_t) (in));
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));
156 #endif /* #ifdef ARM_MATH_ROUNDING */
158 /* Decrement the loop counter */
165 /* Run the below code for Cortex-M0 */
167 /* Loop over blockSize number of values */
173 #ifdef ARM_MATH_ROUNDING
175 /* C = A * 2147483648 */
176 /* convert from float to Q31 and then store the results in the destination buffer */
178 in = (in * 2147483648.0f);
179 in += in > 0 ? 0.5f : -0.5f;
180 *pDst++ = clip_q63_to_q31((q63_t) (in));
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));
188 #endif /* #ifdef ARM_MATH_ROUNDING */
190 /* Decrement the loop counter */
194 #endif /* #ifndef ARM_MATH_CM0 */
199 * @} end of float_to_x group