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_q15.c |
||
4 | * Description: Converts the elements of the floating-point vector to Q15 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 | * @addtogroup float_to_x |
||
37 | * @{ |
||
38 | */ |
||
39 | |||
40 | /** |
||
41 | * @brief Converts the elements of the floating-point vector to Q15 vector. |
||
42 | * @param[in] *pSrc points to the floating-point input vector |
||
43 | * @param[out] *pDst points to the Q15 output vector |
||
44 | * @param[in] blockSize length of the input vector |
||
45 | * @return none. |
||
46 | * |
||
47 | * \par Description: |
||
48 | * \par |
||
49 | * The equation used for the conversion process is: |
||
50 | * <pre> |
||
51 | * pDst[n] = (q15_t)(pSrc[n] * 32768); 0 <= n < blockSize. |
||
52 | * </pre> |
||
53 | * \par Scaling and Overflow Behavior: |
||
54 | * \par |
||
55 | * The function uses saturating arithmetic. |
||
56 | * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated. |
||
57 | * \note |
||
58 | * In order to apply rounding, the library should be rebuilt with the ROUNDING macro |
||
59 | * defined in the preprocessor section of project options. |
||
60 | * |
||
61 | */ |
||
62 | |||
63 | |||
64 | void arm_float_to_q15( |
||
65 | float32_t * pSrc, |
||
66 | q15_t * pDst, |
||
67 | uint32_t blockSize) |
||
68 | { |
||
69 | float32_t *pIn = pSrc; /* Src pointer */ |
||
70 | uint32_t blkCnt; /* loop counter */ |
||
71 | |||
72 | #ifdef ARM_MATH_ROUNDING |
||
73 | |||
74 | float32_t in; |
||
75 | |||
76 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
77 | |||
78 | #if defined (ARM_MATH_DSP) |
||
79 | |||
80 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
||
81 | |||
82 | /*loop Unrolling */ |
||
83 | blkCnt = blockSize >> 2U; |
||
84 | |||
85 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
||
86 | ** a second loop below computes the remaining 1 to 3 samples. */ |
||
87 | while (blkCnt > 0U) |
||
88 | { |
||
89 | |||
90 | #ifdef ARM_MATH_ROUNDING |
||
91 | /* C = A * 32768 */ |
||
92 | /* convert from float to q15 and then store the results in the destination buffer */ |
||
93 | in = *pIn++; |
||
94 | in = (in * 32768.0f); |
||
95 | in += in > 0.0f ? 0.5f : -0.5f; |
||
96 | *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16)); |
||
97 | |||
98 | in = *pIn++; |
||
99 | in = (in * 32768.0f); |
||
100 | in += in > 0.0f ? 0.5f : -0.5f; |
||
101 | *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16)); |
||
102 | |||
103 | in = *pIn++; |
||
104 | in = (in * 32768.0f); |
||
105 | in += in > 0.0f ? 0.5f : -0.5f; |
||
106 | *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16)); |
||
107 | |||
108 | in = *pIn++; |
||
109 | in = (in * 32768.0f); |
||
110 | in += in > 0.0f ? 0.5f : -0.5f; |
||
111 | *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16)); |
||
112 | |||
113 | #else |
||
114 | |||
115 | /* C = A * 32768 */ |
||
116 | /* convert from float to q15 and then store the results in the destination buffer */ |
||
117 | *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16); |
||
118 | *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16); |
||
119 | *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16); |
||
120 | *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16); |
||
121 | |||
122 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
123 | |||
124 | /* Decrement the loop counter */ |
||
125 | blkCnt--; |
||
126 | } |
||
127 | |||
128 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
||
129 | ** No loop unrolling is used. */ |
||
130 | blkCnt = blockSize % 0x4U; |
||
131 | |||
132 | while (blkCnt > 0U) |
||
133 | { |
||
134 | |||
135 | #ifdef ARM_MATH_ROUNDING |
||
136 | /* C = A * 32768 */ |
||
137 | /* convert from float to q15 and then store the results in the destination buffer */ |
||
138 | in = *pIn++; |
||
139 | in = (in * 32768.0f); |
||
140 | in += in > 0.0f ? 0.5f : -0.5f; |
||
141 | *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16)); |
||
142 | |||
143 | #else |
||
144 | |||
145 | /* C = A * 32768 */ |
||
146 | /* convert from float to q15 and then store the results in the destination buffer */ |
||
147 | *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16); |
||
148 | |||
149 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
150 | |||
151 | /* Decrement the loop counter */ |
||
152 | blkCnt--; |
||
153 | } |
||
154 | |||
155 | |||
156 | #else |
||
157 | |||
158 | /* Run the below code for Cortex-M0 */ |
||
159 | |||
160 | /* Loop over blockSize number of values */ |
||
161 | blkCnt = blockSize; |
||
162 | |||
163 | while (blkCnt > 0U) |
||
164 | { |
||
165 | |||
166 | #ifdef ARM_MATH_ROUNDING |
||
167 | /* C = A * 32768 */ |
||
168 | /* convert from float to q15 and then store the results in the destination buffer */ |
||
169 | in = *pIn++; |
||
170 | in = (in * 32768.0f); |
||
171 | in += in > 0 ? 0.5f : -0.5f; |
||
172 | *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16)); |
||
173 | |||
174 | #else |
||
175 | |||
176 | /* C = A * 32768 */ |
||
177 | /* convert from float to q15 and then store the results in the destination buffer */ |
||
178 | *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16); |
||
179 | |||
180 | #endif /* #ifdef ARM_MATH_ROUNDING */ |
||
181 | |||
182 | /* Decrement the loop counter */ |
||
183 | blkCnt--; |
||
184 | } |
||
185 | |||
186 | #endif /* #if defined (ARM_MATH_DSP) */ |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @} end of float_to_x group |
||
192 | */ |