1 /******************************************************************************
2 * @file fast_math_functions.h
3 * @brief Public header file for CMSIS DSP Library
4 * @version V1.10.0
5 * @date 08 July 2021
6 * Target Processor: Cortex-M and Cortex-A cores
7 ******************************************************************************/
8 /*
9 * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the License); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 */
25
26
27 #ifndef _FAST_MATH_FUNCTIONS_H_
28 #define _FAST_MATH_FUNCTIONS_H_
29
30 #include "arm_math_types.h"
31 #include "arm_math_memory.h"
32
33 #include "dsp/none.h"
34 #include "dsp/utils.h"
35
36 #include "dsp/basic_math_functions.h"
37
38 #include <math.h>
39
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44
45 /**
46 * @brief Macros required for SINE and COSINE Fast math approximations
47 */
48
49 #define FAST_MATH_TABLE_SIZE 512
50 #define FAST_MATH_Q31_SHIFT (32 - 10)
51 #define FAST_MATH_Q15_SHIFT (16 - 10)
52
53 #ifndef PI
54 #define PI 3.14159265358979f
55 #endif
56
57 #ifndef PI_F64
58 #define PI_F64 3.14159265358979323846
59 #endif
60
61
62
63 /**
64 * @defgroup groupFastMath Fast Math Functions
65 * This set of functions provides a fast approximation to sine, cosine, and square root.
66 * As compared to most of the other functions in the CMSIS math library, the fast math functions
67 * operate on individual values and not arrays.
68 * There are separate functions for Q15, Q31, and floating-point data.
69 *
70 */
71
72
73 /**
74 * @brief Fast approximation to the trigonometric sine function for floating-point data.
75 * @param[in] x input value in radians.
76 * @return sin(x).
77 */
78 float32_t arm_sin_f32(
79 float32_t x);
80
81
82 /**
83 * @brief Fast approximation to the trigonometric sine function for Q31 data.
84 * @param[in] x Scaled input value in radians.
85 * @return sin(x).
86 */
87 q31_t arm_sin_q31(
88 q31_t x);
89
90 /**
91 * @brief Fast approximation to the trigonometric sine function for Q15 data.
92 * @param[in] x Scaled input value in radians.
93 * @return sin(x).
94 */
95 q15_t arm_sin_q15(
96 q15_t x);
97
98
99 /**
100 * @brief Fast approximation to the trigonometric cosine function for floating-point data.
101 * @param[in] x input value in radians.
102 * @return cos(x).
103 */
104 float32_t arm_cos_f32(
105 float32_t x);
106
107
108 /**
109 * @brief Fast approximation to the trigonometric cosine function for Q31 data.
110 * @param[in] x Scaled input value in radians.
111 * @return cos(x).
112 */
113 q31_t arm_cos_q31(
114 q31_t x);
115
116
117 /**
118 * @brief Fast approximation to the trigonometric cosine function for Q15 data.
119 * @param[in] x Scaled input value in radians.
120 * @return cos(x).
121 */
122 q15_t arm_cos_q15(
123 q15_t x);
124
125
126 /**
127 @brief Floating-point vector of log values.
128 @param[in] pSrc points to the input vector
129 @param[out] pDst points to the output vector
130 @param[in] blockSize number of samples in each vector
131 @return none
132 */
133 void arm_vlog_f32(
134 const float32_t * pSrc,
135 float32_t * pDst,
136 uint32_t blockSize);
137
138
139
140 /**
141 @brief Floating-point vector of log values.
142 @param[in] pSrc points to the input vector
143 @param[out] pDst points to the output vector
144 @param[in] blockSize number of samples in each vector
145 @return none
146 */
147 void arm_vlog_f64(
148 const float64_t * pSrc,
149 float64_t * pDst,
150 uint32_t blockSize);
151
152
153
154 /**
155 * @brief q31 vector of log values.
156 * @param[in] pSrc points to the input vector in q31
157 * @param[out] pDst points to the output vector in q5.26
158 * @param[in] blockSize number of samples in each vector
159 * @return none
160 */
161 void arm_vlog_q31(const q31_t * pSrc,
162 q31_t * pDst,
163 uint32_t blockSize);
164
165 /**
166 * @brief q15 vector of log values.
167 * @param[in] pSrc points to the input vector in q15
168 * @param[out] pDst points to the output vector in q4.11
169 * @param[in] blockSize number of samples in each vector
170 * @return none
171 */
172 void arm_vlog_q15(const q15_t * pSrc,
173 q15_t * pDst,
174 uint32_t blockSize);
175
176
177
178 /**
179 @brief Floating-point vector of exp values.
180 @param[in] pSrc points to the input vector
181 @param[out] pDst points to the output vector
182 @param[in] blockSize number of samples in each vector
183 @return none
184 */
185 void arm_vexp_f32(
186 const float32_t * pSrc,
187 float32_t * pDst,
188 uint32_t blockSize);
189
190
191
192 /**
193 @brief Floating-point vector of exp values.
194 @param[in] pSrc points to the input vector
195 @param[out] pDst points to the output vector
196 @param[in] blockSize number of samples in each vector
197 @return none
198 */
199 void arm_vexp_f64(
200 const float64_t * pSrc,
201 float64_t * pDst,
202 uint32_t blockSize);
203
204
205
206 /**
207 * @defgroup SQRT Square Root
208 *
209 * Computes the square root of a number.
210 * There are separate functions for Q15, Q31, and floating-point data types.
211 * The square root function is computed using the Newton-Raphson algorithm.
212 * This is an iterative algorithm of the form:
213 * <pre>
214 * x1 = x0 - f(x0)/f'(x0)
215 * </pre>
216 * where <code>x1</code> is the current estimate,
217 * <code>x0</code> is the previous estimate, and
218 * <code>f'(x0)</code> is the derivative of <code>f()</code> evaluated at <code>x0</code>.
219 * For the square root function, the algorithm reduces to:
220 * <pre>
221 * x0 = in/2 [initial guess]
222 * x1 = 1/2 * ( x0 + in / x0) [each iteration]
223 * </pre>
224 */
225
226
227 /**
228 * @addtogroup SQRT
229 * @{
230 */
231
232 /**
233 @brief Floating-point square root function.
234 @param[in] in input value
235 @param[out] pOut square root of input value
236 @return execution status
237 - \ref ARM_MATH_SUCCESS : input value is positive
238 - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
239 */
arm_sqrt_f32(const float32_t in,float32_t * pOut)240 __STATIC_FORCEINLINE arm_status arm_sqrt_f32(
241 const float32_t in,
242 float32_t * pOut)
243 {
244 if (in >= 0.0f)
245 {
246 #if defined ( __CC_ARM )
247 #if defined __TARGET_FPU_VFP
248 *pOut = __sqrtf(in);
249 #else
250 *pOut = sqrtf(in);
251 #endif
252
253 #elif defined ( __ICCARM__ )
254 #if defined __ARMVFP__
255 __ASM("VSQRT.F32 %0,%1" : "=t"(*pOut) : "t"(in));
256 #else
257 *pOut = sqrtf(in);
258 #endif
259
260 #elif defined ( __ARMCC_VERSION ) && ( __ARMCC_VERSION >= 6010050 )
261 *pOut = _sqrtf(in);
262 #elif defined(__GNUC_PYTHON__)
263 *pOut = sqrtf(in);
264 #elif defined ( __GNUC__ )
265 #if defined (__VFP_FP__) && !defined(__SOFTFP__)
266 __ASM("VSQRT.F32 %0,%1" : "=t"(*pOut) : "t"(in));
267 #else
268 *pOut = sqrtf(in);
269 #endif
270 #else
271 *pOut = sqrtf(in);
272 #endif
273
274 return (ARM_MATH_SUCCESS);
275 }
276 else
277 {
278 *pOut = 0.0f;
279 return (ARM_MATH_ARGUMENT_ERROR);
280 }
281 }
282
283
284 /**
285 @brief Q31 square root function.
286 @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF
287 @param[out] pOut points to square root of input value
288 @return execution status
289 - \ref ARM_MATH_SUCCESS : input value is positive
290 - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
291 */
292 arm_status arm_sqrt_q31(
293 q31_t in,
294 q31_t * pOut);
295
296
297 /**
298 @brief Q15 square root function.
299 @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF
300 @param[out] pOut points to square root of input value
301 @return execution status
302 - \ref ARM_MATH_SUCCESS : input value is positive
303 - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
304 */
305 arm_status arm_sqrt_q15(
306 q15_t in,
307 q15_t * pOut);
308
309
310
311 /**
312 * @} end of SQRT group
313 */
314
315 /**
316 @brief Fixed point division
317 @param[in] numerator Numerator
318 @param[in] denominator Denominator
319 @param[out] quotient Quotient value normalized between -1.0 and 1.0
320 @param[out] shift Shift left value to get the unnormalized quotient
321 @return error status
322
323 When dividing by 0, an error ARM_MATH_NANINF is returned. And the quotient is forced
324 to the saturated negative or positive value.
325 */
326
327 arm_status arm_divide_q15(q15_t numerator,
328 q15_t denominator,
329 q15_t *quotient,
330 int16_t *shift);
331
332 /**
333 @brief Fixed point division
334 @param[in] numerator Numerator
335 @param[in] denominator Denominator
336 @param[out] quotient Quotient value normalized between -1.0 and 1.0
337 @param[out] shift Shift left value to get the unnormalized quotient
338 @return error status
339
340 When dividing by 0, an error ARM_MATH_NANINF is returned. And the quotient is forced
341 to the saturated negative or positive value.
342 */
343
344 arm_status arm_divide_q31(q31_t numerator,
345 q31_t denominator,
346 q31_t *quotient,
347 int16_t *shift);
348
349
350
351 /**
352 @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
353 @param[in] y y coordinate
354 @param[in] x x coordinate
355 @param[out] result Result
356 @return error status.
357 */
358 arm_status arm_atan2_f32(float32_t y,float32_t x,float32_t *result);
359
360
361 /**
362 @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
363 @param[in] y y coordinate
364 @param[in] x x coordinate
365 @param[out] result Result in Q2.29
366 @return error status.
367 */
368 arm_status arm_atan2_q31(q31_t y,q31_t x,q31_t *result);
369
370 /**
371 @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
372 @param[in] y y coordinate
373 @param[in] x x coordinate
374 @param[out] result Result in Q2.13
375 @return error status.
376 */
377 arm_status arm_atan2_q15(q15_t y,q15_t x,q15_t *result);
378
379 #ifdef __cplusplus
380 }
381 #endif
382
383 #endif /* ifndef _FAST_MATH_FUNCTIONS_H_ */
384