1 /******************************************************************************
2 * @file arm_math_utils.h
3 * @brief Public header file for CMSIS DSP Library
4 * @version V1.9.0
5 * @date 20. July 2020
6 ******************************************************************************/
7 /*
8 * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the License); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25 #ifndef _ARM_MATH_UTILS_H_
26
27 #define _ARM_MATH_UTILS_H_
28
29 #include "arm_math_types.h"
30 #include <limits.h>
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #endif
36
37 /**
38 * @brief Macros required for reciprocal calculation in Normalized LMS
39 */
40
41 #define INDEX_MASK 0x0000003F
42
43 #ifndef MIN
44 #define MIN(x,y) ((x) < (y) ? (x) : (y))
45 #endif
46
47 #ifndef MAX
48 #define MAX(x,y) ((x) > (y) ? (x) : (y))
49 #endif
50
51 #ifndef ARM_SQ
52 #define ARM_SQ(x) ((x) * (x))
53 #endif
54
55 #ifndef ARM_ROUND_UP
56 #define ARM_ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
57 #endif
58
59
60 /**
61 * @brief Function to Calculates 1/in (reciprocal) value of Q31 Data type.
62 It should not be used with negative values.
63 */
arm_recip_q31(q31_t in,q31_t * dst,const q31_t * pRecipTable)64 __STATIC_FORCEINLINE uint32_t arm_recip_q31(
65 q31_t in,
66 q31_t * dst,
67 const q31_t * pRecipTable)
68 {
69 q31_t out;
70 uint32_t tempVal;
71 uint32_t index, i;
72 uint32_t signBits;
73
74 if (in > 0)
75 {
76 signBits = ((uint32_t) (__CLZ( (uint32_t)in) - 1));
77 }
78 else
79 {
80 signBits = ((uint32_t) (__CLZ((uint32_t)(-in)) - 1));
81 }
82
83 /* Convert input sample to 1.31 format */
84 in = (in << signBits);
85
86 /* calculation of index for initial approximated Val */
87 index = (uint32_t)(in >> 24);
88 index = (index & INDEX_MASK);
89
90 /* 1.31 with exp 1 */
91 out = pRecipTable[index];
92
93 /* calculation of reciprocal value */
94 /* running approximation for two iterations */
95 for (i = 0U; i < 2U; i++)
96 {
97 tempVal = (uint32_t) (((q63_t) in * out) >> 31);
98 tempVal = 0x7FFFFFFFu - tempVal;
99 /* 1.31 with exp 1 */
100 /* out = (q31_t) (((q63_t) out * tempVal) >> 30); */
101 out = clip_q63_to_q31(((q63_t) out * tempVal) >> 30);
102 }
103
104 /* write output */
105 *dst = out;
106
107 /* return num of signbits of out = 1/in value */
108 return (signBits + 1U);
109 }
110
111
112 /**
113 * @brief Function to Calculates 1/in (reciprocal) value of Q15 Data type.
114 It should not be used with negative values.
115 */
arm_recip_q15(q15_t in,q15_t * dst,const q15_t * pRecipTable)116 __STATIC_FORCEINLINE uint32_t arm_recip_q15(
117 q15_t in,
118 q15_t * dst,
119 const q15_t * pRecipTable)
120 {
121 q15_t out = 0;
122 int32_t tempVal = 0;
123 uint32_t index = 0, i = 0;
124 uint32_t signBits = 0;
125
126 if (in > 0)
127 {
128 signBits = ((uint32_t)(__CLZ( (uint32_t)in) - 17));
129 }
130 else
131 {
132 signBits = ((uint32_t)(__CLZ((uint32_t)(-in)) - 17));
133 }
134
135 /* Convert input sample to 1.15 format */
136 in = (q15_t)(in << signBits);
137
138 /* calculation of index for initial approximated Val */
139 index = (uint32_t)(in >> 8);
140 index = (index & INDEX_MASK);
141
142 /* 1.15 with exp 1 */
143 out = pRecipTable[index];
144
145 /* calculation of reciprocal value */
146 /* running approximation for two iterations */
147 for (i = 0U; i < 2U; i++)
148 {
149 tempVal = (((q31_t) in * out) >> 15);
150 tempVal = 0x7FFF - tempVal;
151 /* 1.15 with exp 1 */
152 out = (q15_t) (((q31_t) out * tempVal) >> 14);
153 /* out = clip_q31_to_q15(((q31_t) out * tempVal) >> 14); */
154 }
155
156 /* write output */
157 *dst = out;
158
159 /* return num of signbits of out = 1/in value */
160 return (signBits + 1);
161 }
162
163
164 /**
165 * @brief 64-bit to 32-bit unsigned normalization
166 * @param[in] in is input unsigned long long value
167 * @param[out] normalized is the 32-bit normalized value
168 * @param[out] norm is norm scale
169 */
arm_norm_64_to_32u(uint64_t in,int32_t * normalized,int32_t * norm)170 __STATIC_INLINE void arm_norm_64_to_32u(uint64_t in, int32_t * normalized, int32_t *norm)
171 {
172 int32_t n1;
173 int32_t hi = (int32_t) (in >> 32);
174 int32_t lo = (int32_t) ((in << 32) >> 32);
175
176 n1 = __CLZ((uint32_t)hi) - 32;
177 if (!n1)
178 {
179 /*
180 * input fits in 32-bit
181 */
182 n1 = __CLZ((uint32_t)lo);
183 if (!n1)
184 {
185 /*
186 * MSB set, need to scale down by 1
187 */
188 *norm = -1;
189 *normalized = (((uint32_t) lo) >> 1);
190 } else
191 {
192 if (n1 == 32)
193 {
194 /*
195 * input is zero
196 */
197 *norm = 0;
198 *normalized = 0;
199 } else
200 {
201 /*
202 * 32-bit normalization
203 */
204 *norm = n1 - 1;
205 *normalized = lo << *norm;
206 }
207 }
208 } else
209 {
210 /*
211 * input fits in 64-bit
212 */
213 n1 = 1 - n1;
214 *norm = -n1;
215 /*
216 * 64 bit normalization
217 */
218 *normalized = (int32_t)(((uint32_t)lo) >> n1) | (hi << (32 - n1));
219 }
220 }
221
arm_div_int64_to_int32(int64_t num,int32_t den)222 __STATIC_INLINE int32_t arm_div_int64_to_int32(int64_t num, int32_t den)
223 {
224 int32_t result;
225 uint64_t absNum;
226 int32_t normalized;
227 int32_t norm;
228
229 /*
230 * if sum fits in 32bits
231 * avoid costly 64-bit division
232 */
233 if (num == (int64_t)LONG_MIN)
234 {
235 absNum = LONG_MAX;
236 }
237 else
238 {
239 absNum = (uint64_t) (num > 0 ? num : -num);
240 }
241 arm_norm_64_to_32u(absNum, &normalized, &norm);
242 if (norm > 0)
243 /*
244 * 32-bit division
245 */
246 result = (int32_t) num / den;
247 else
248 /*
249 * 64-bit division
250 */
251 result = (int32_t) (num / den);
252
253 return result;
254 }
255
256 #undef INDEX_MASK
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif /*ifndef _ARM_MATH_UTILS_H_ */
263