Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
56 | mjames | 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 |
||
5 | * |
||
6 | * $Date: 27. January 2017 |
||
7 | * $Revision: V.1.5.1 |
||
8 | * |
||
9 | * Target Processor: Cortex-M cores |
||
10 | * -------------------------------------------------------------------- */ |
||
11 | /* |
||
12 | * Copyright (C) 2010-2017 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 "arm_math.h" |
||
30 | |||
31 | /** |
||
32 | * @ingroup groupSupport |
||
33 | */ |
||
34 | |||
35 | /** |
||
36 | * @defgroup float_to_x Convert 32-bit floating point value |
||
37 | */ |
||
38 | |||
39 | /** |
||
40 | * @addtogroup float_to_x |
||
41 | * @{ |
||
42 | */ |
||
43 | |||
44 | /** |
||
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 |
||
49 | * @return none. |
||
50 | * |
||
51 | *\par Description: |
||
52 | * \par |
||
53 | * The equation used for the conversion process is: |
||
54 | * |
||
55 | * <pre> |
||
56 | * pDst[n] = (q31_t)(pSrc[n] * 2147483648); 0 <= n < blockSize. |
||
57 | * </pre> |
||
58 | * <b>Scaling and Overflow Behavior:</b> |
||
59 | * \par |
||
60 | * The function uses saturating arithmetic. |
||
61 | * Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] will be saturated. |
||
62 | * |
||
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. |
||
65 | */ |
||
66 | |||
67 | |||
68 | void arm_float_to_q31( |
||
69 | float32_t * pSrc, |
||
70 | q31_t * pDst, |
||
71 | uint32_t blockSize) |
||
72 | { |
||
73 | float32_t *pIn = pSrc; /* Src pointer */ |
||
74 | uint32_t blkCnt; /* loop counter */ |
||
75 | |||
76 | #ifdef ARM_MATH_ROUNDING |
||
77 | |||
78 | float32_t in; |
||
79 | |||
80 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
81 | |||
82 | #if defined (ARM_MATH_DSP) |
||
83 | |||
84 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
||
85 | |||
86 | /*loop Unrolling */ |
||
87 | blkCnt = blockSize >> 2U; |
||
88 | |||
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. */ |
||
91 | while (blkCnt > 0U) |
||
92 | { |
||
93 | |||
94 | #ifdef ARM_MATH_ROUNDING |
||
95 | |||
96 | /* C = A * 32768 */ |
||
97 | /* convert from float to Q31 and then store the results in the destination buffer */ |
||
98 | in = *pIn++; |
||
99 | in = (in * 2147483648.0f); |
||
100 | in += in > 0.0f ? 0.5f : -0.5f; |
||
101 | *pDst++ = clip_q63_to_q31((q63_t) (in)); |
||
102 | |||
103 | in = *pIn++; |
||
104 | in = (in * 2147483648.0f); |
||
105 | in += in > 0.0f ? 0.5f : -0.5f; |
||
106 | *pDst++ = clip_q63_to_q31((q63_t) (in)); |
||
107 | |||
108 | in = *pIn++; |
||
109 | in = (in * 2147483648.0f); |
||
110 | in += in > 0.0f ? 0.5f : -0.5f; |
||
111 | *pDst++ = clip_q63_to_q31((q63_t) (in)); |
||
112 | |||
113 | in = *pIn++; |
||
114 | in = (in * 2147483648.0f); |
||
115 | in += in > 0.0f ? 0.5f : -0.5f; |
||
116 | *pDst++ = clip_q63_to_q31((q63_t) (in)); |
||
117 | |||
118 | #else |
||
119 | |||
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)); |
||
126 | |||
127 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
128 | |||
129 | /* Decrement the loop counter */ |
||
130 | blkCnt--; |
||
131 | } |
||
132 | |||
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; |
||
136 | |||
137 | while (blkCnt > 0U) |
||
138 | { |
||
139 | |||
140 | #ifdef ARM_MATH_ROUNDING |
||
141 | |||
142 | /* C = A * 2147483648 */ |
||
143 | /* convert from float to Q31 and then store the results in the destination buffer */ |
||
144 | in = *pIn++; |
||
145 | in = (in * 2147483648.0f); |
||
146 | in += in > 0.0f ? 0.5f : -0.5f; |
||
147 | *pDst++ = clip_q63_to_q31((q63_t) (in)); |
||
148 | |||
149 | #else |
||
150 | |||
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)); |
||
154 | |||
155 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
156 | |||
157 | /* Decrement the loop counter */ |
||
158 | blkCnt--; |
||
159 | } |
||
160 | |||
161 | |||
162 | #else |
||
163 | |||
164 | /* Run the below code for Cortex-M0 */ |
||
165 | |||
166 | /* Loop over blockSize number of values */ |
||
167 | blkCnt = blockSize; |
||
168 | |||
169 | while (blkCnt > 0U) |
||
170 | { |
||
171 | |||
172 | #ifdef ARM_MATH_ROUNDING |
||
173 | |||
174 | /* C = A * 2147483648 */ |
||
175 | /* convert from float to Q31 and then store the results in the destination buffer */ |
||
176 | in = *pIn++; |
||
177 | in = (in * 2147483648.0f); |
||
178 | in += in > 0 ? 0.5f : -0.5f; |
||
179 | *pDst++ = clip_q63_to_q31((q63_t) (in)); |
||
180 | |||
181 | #else |
||
182 | |||
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)); |
||
186 | |||
187 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
188 | |||
189 | /* Decrement the loop counter */ |
||
190 | blkCnt--; |
||
191 | } |
||
192 | |||
193 | #endif /* #if defined (ARM_MATH_DSP) */ |
||
194 | |||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @} end of float_to_x group |
||
199 | */ |