1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_float_to_q31.c
4 * Description: Converts the elements of the floating-point vector to Q31 vector
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
32 * @ingroup groupSupport
36 * @defgroup float_to_x Convert 32-bit floating point value
40 * @addtogroup float_to_x
45 * @brief Converts the elements of the floating-point vector to Q31 vector.
46 * @param[in] *pSrc points to the floating-point input vector
47 * @param[out] *pDst points to the Q31 output vector
48 * @param[in] blockSize length of the input vector
53 * The equation used for the conversion process is:
56 * pDst[n] = (q31_t)(pSrc[n] * 2147483648); 0 <= n < blockSize.
58 * <b>Scaling and Overflow Behavior:</b>
60 * The function uses saturating arithmetic.
61 * Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] will be saturated.
63 * \note In order to apply rounding, the library should be rebuilt with the ROUNDING macro
64 * defined in the preprocessor section of project options.
68 void arm_float_to_q31(
73 float32_t
*pIn
= pSrc
; /* Src pointer */
74 uint32_t blkCnt
; /* loop counter */
76 #ifdef ARM_MATH_ROUNDING
80 #endif /* #ifdef ARM_MATH_ROUNDING */
82 #if defined (ARM_MATH_DSP)
84 /* Run the below code for Cortex-M4 and Cortex-M3 */
87 blkCnt
= blockSize
>> 2U;
89 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
90 ** a second loop below computes the remaining 1 to 3 samples. */
94 #ifdef ARM_MATH_ROUNDING
97 /* convert from float to Q31 and then store the results in the destination buffer */
99 in
= (in
* 2147483648.0f
);
100 in
+= in
> 0.0f
? 0.5f
: -0.5f
;
101 *pDst
++ = clip_q63_to_q31((q63_t
) (in
));
104 in
= (in
* 2147483648.0f
);
105 in
+= in
> 0.0f
? 0.5f
: -0.5f
;
106 *pDst
++ = clip_q63_to_q31((q63_t
) (in
));
109 in
= (in
* 2147483648.0f
);
110 in
+= in
> 0.0f
? 0.5f
: -0.5f
;
111 *pDst
++ = clip_q63_to_q31((q63_t
) (in
));
114 in
= (in
* 2147483648.0f
);
115 in
+= in
> 0.0f
? 0.5f
: -0.5f
;
116 *pDst
++ = clip_q63_to_q31((q63_t
) (in
));
120 /* C = A * 2147483648 */
121 /* convert from float to Q31 and then store the results in the destination buffer */
122 *pDst
++ = clip_q63_to_q31((q63_t
) (*pIn
++ * 2147483648.0f
));
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
));
127 #endif /* #ifdef ARM_MATH_ROUNDING */
129 /* Decrement the loop counter */
133 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
134 ** No loop unrolling is used. */
135 blkCnt
= blockSize
% 0x4U
;
140 #ifdef ARM_MATH_ROUNDING
142 /* C = A * 2147483648 */
143 /* convert from float to Q31 and then store the results in the destination buffer */
145 in
= (in
* 2147483648.0f
);
146 in
+= in
> 0.0f
? 0.5f
: -0.5f
;
147 *pDst
++ = clip_q63_to_q31((q63_t
) (in
));
151 /* C = A * 2147483648 */
152 /* convert from float to Q31 and then store the results in the destination buffer */
153 *pDst
++ = clip_q63_to_q31((q63_t
) (*pIn
++ * 2147483648.0f
));
155 #endif /* #ifdef ARM_MATH_ROUNDING */
157 /* Decrement the loop counter */
164 /* Run the below code for Cortex-M0 */
166 /* Loop over blockSize number of values */
172 #ifdef ARM_MATH_ROUNDING
174 /* C = A * 2147483648 */
175 /* convert from float to Q31 and then store the results in the destination buffer */
177 in
= (in
* 2147483648.0f
);
178 in
+= in
> 0 ? 0.5f
: -0.5f
;
179 *pDst
++ = clip_q63_to_q31((q63_t
) (in
));
183 /* C = A * 2147483648 */
184 /* convert from float to Q31 and then store the results in the destination buffer */
185 *pDst
++ = clip_q63_to_q31((q63_t
) (*pIn
++ * 2147483648.0f
));
187 #endif /* #ifdef ARM_MATH_ROUNDING */
189 /* Decrement the loop counter */
193 #endif /* #if defined (ARM_MATH_DSP) */
198 * @} end of float_to_x group