fast_math_functions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #ifndef _FAST_MATH_FUNCTIONS_H_
  26. #define _FAST_MATH_FUNCTIONS_H_
  27. #include "arm_math_types.h"
  28. #include "arm_math_memory.h"
  29. #include "dsp/none.h"
  30. #include "dsp/utils.h"
  31. #include "dsp/basic_math_functions.h"
  32. #include <math.h>
  33. #ifdef __cplusplus
  34. extern "C"
  35. {
  36. #endif
  37. /**
  38. * @brief Macros required for SINE and COSINE Fast math approximations
  39. */
  40. #define FAST_MATH_TABLE_SIZE 512
  41. #define FAST_MATH_Q31_SHIFT (32 - 10)
  42. #define FAST_MATH_Q15_SHIFT (16 - 10)
  43. #ifndef PI
  44. #define PI 3.14159265358979f
  45. #endif
  46. #ifndef PI_F64
  47. #define PI_F64 3.14159265358979323846
  48. #endif
  49. /**
  50. * @defgroup groupFastMath Fast Math Functions
  51. * This set of functions provides a fast approximation to sine, cosine, and square root.
  52. * As compared to most of the other functions in the CMSIS math library, the fast math functions
  53. * operate on individual values and not arrays.
  54. * There are separate functions for Q15, Q31, and floating-point data.
  55. *
  56. */
  57. /**
  58. * @brief Fast approximation to the trigonometric sine function for floating-point data.
  59. * @param[in] x input value in radians.
  60. * @return sin(x).
  61. */
  62. float32_t arm_sin_f32(
  63. float32_t x);
  64. /**
  65. * @brief Fast approximation to the trigonometric sine function for Q31 data.
  66. * @param[in] x Scaled input value in radians.
  67. * @return sin(x).
  68. */
  69. q31_t arm_sin_q31(
  70. q31_t x);
  71. /**
  72. * @brief Fast approximation to the trigonometric sine function for Q15 data.
  73. * @param[in] x Scaled input value in radians.
  74. * @return sin(x).
  75. */
  76. q15_t arm_sin_q15(
  77. q15_t x);
  78. /**
  79. * @brief Fast approximation to the trigonometric cosine function for floating-point data.
  80. * @param[in] x input value in radians.
  81. * @return cos(x).
  82. */
  83. float32_t arm_cos_f32(
  84. float32_t x);
  85. /**
  86. * @brief Fast approximation to the trigonometric cosine function for Q31 data.
  87. * @param[in] x Scaled input value in radians.
  88. * @return cos(x).
  89. */
  90. q31_t arm_cos_q31(
  91. q31_t x);
  92. /**
  93. * @brief Fast approximation to the trigonometric cosine function for Q15 data.
  94. * @param[in] x Scaled input value in radians.
  95. * @return cos(x).
  96. */
  97. q15_t arm_cos_q15(
  98. q15_t x);
  99. /**
  100. @brief Floating-point vector of log values.
  101. @param[in] pSrc points to the input vector
  102. @param[out] pDst points to the output vector
  103. @param[in] blockSize number of samples in each vector
  104. */
  105. void arm_vlog_f32(
  106. const float32_t * pSrc,
  107. float32_t * pDst,
  108. uint32_t blockSize);
  109. /**
  110. @brief Floating-point vector of log values.
  111. @param[in] pSrc points to the input vector
  112. @param[out] pDst points to the output vector
  113. @param[in] blockSize number of samples in each vector
  114. */
  115. void arm_vlog_f64(
  116. const float64_t * pSrc,
  117. float64_t * pDst,
  118. uint32_t blockSize);
  119. /**
  120. * @brief q31 vector of log values.
  121. * @param[in] pSrc points to the input vector in q31
  122. * @param[out] pDst points to the output vector in q5.26
  123. * @param[in] blockSize number of samples in each vector
  124. */
  125. void arm_vlog_q31(const q31_t * pSrc,
  126. q31_t * pDst,
  127. uint32_t blockSize);
  128. /**
  129. * @brief q15 vector of log values.
  130. * @param[in] pSrc points to the input vector in q15
  131. * @param[out] pDst points to the output vector in q4.11
  132. * @param[in] blockSize number of samples in each vector
  133. */
  134. void arm_vlog_q15(const q15_t * pSrc,
  135. q15_t * pDst,
  136. uint32_t blockSize);
  137. /**
  138. @brief Floating-point vector of exp values.
  139. @param[in] pSrc points to the input vector
  140. @param[out] pDst points to the output vector
  141. @param[in] blockSize number of samples in each vector
  142. */
  143. void arm_vexp_f32(
  144. const float32_t * pSrc,
  145. float32_t * pDst,
  146. uint32_t blockSize);
  147. /**
  148. @brief Floating-point vector of exp values.
  149. @param[in] pSrc points to the input vector
  150. @param[out] pDst points to the output vector
  151. @param[in] blockSize number of samples in each vector
  152. */
  153. void arm_vexp_f64(
  154. const float64_t * pSrc,
  155. float64_t * pDst,
  156. uint32_t blockSize);
  157. /**
  158. * @defgroup SQRT Square Root
  159. *
  160. * Computes the square root of a number.
  161. * There are separate functions for Q15, Q31, and floating-point data types.
  162. * The square root function is computed using the Newton-Raphson algorithm.
  163. * This is an iterative algorithm of the form:
  164. * <pre>
  165. * x1 = x0 - f(x0)/f'(x0)
  166. * </pre>
  167. * where <code>x1</code> is the current estimate,
  168. * <code>x0</code> is the previous estimate, and
  169. * <code>f'(x0)</code> is the derivative of <code>f()</code> evaluated at <code>x0</code>.
  170. * For the square root function, the algorithm reduces to:
  171. * <pre>
  172. * x0 = in/2 [initial guess]
  173. * x1 = 1/2 * ( x0 + in / x0) [each iteration]
  174. * </pre>
  175. */
  176. /**
  177. * @addtogroup SQRT
  178. * @{
  179. */
  180. /**
  181. @brief Floating-point square root function.
  182. @param[in] in input value
  183. @param[out] pOut square root of input value
  184. @return execution status
  185. - \ref ARM_MATH_SUCCESS : input value is positive
  186. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  187. */
  188. __STATIC_FORCEINLINE arm_status arm_sqrt_f32(
  189. const float32_t in,
  190. float32_t * pOut)
  191. {
  192. if (in >= 0.0f)
  193. {
  194. #if defined ( __CC_ARM )
  195. #if defined __TARGET_FPU_VFP
  196. *pOut = __sqrtf(in);
  197. #else
  198. *pOut = sqrtf(in);
  199. #endif
  200. #elif defined ( __ICCARM__ )
  201. #if defined __ARMVFP__
  202. __ASM("VSQRT.F32 %0,%1" : "=t"(*pOut) : "t"(in));
  203. #else
  204. *pOut = sqrtf(in);
  205. #endif
  206. #elif defined ( __ARMCC_VERSION ) && ( __ARMCC_VERSION >= 6010050 )
  207. *pOut = _sqrtf(in);
  208. #elif defined(__GNUC_PYTHON__)
  209. *pOut = sqrtf(in);
  210. #elif defined ( __GNUC__ )
  211. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  212. __ASM("VSQRT.F32 %0,%1" : "=t"(*pOut) : "t"(in));
  213. #else
  214. *pOut = sqrtf(in);
  215. #endif
  216. #else
  217. *pOut = sqrtf(in);
  218. #endif
  219. return (ARM_MATH_SUCCESS);
  220. }
  221. else
  222. {
  223. *pOut = 0.0f;
  224. return (ARM_MATH_ARGUMENT_ERROR);
  225. }
  226. }
  227. /**
  228. @brief Q31 square root function.
  229. @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF
  230. @param[out] pOut points to square root of input value
  231. @return execution status
  232. - \ref ARM_MATH_SUCCESS : input value is positive
  233. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  234. */
  235. arm_status arm_sqrt_q31(
  236. q31_t in,
  237. q31_t * pOut);
  238. /**
  239. @brief Q15 square root function.
  240. @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF
  241. @param[out] pOut points to square root of input value
  242. @return execution status
  243. - \ref ARM_MATH_SUCCESS : input value is positive
  244. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  245. */
  246. arm_status arm_sqrt_q15(
  247. q15_t in,
  248. q15_t * pOut);
  249. /**
  250. * @} end of SQRT group
  251. */
  252. /**
  253. @brief Fixed point division
  254. @param[in] numerator Numerator
  255. @param[in] denominator Denominator
  256. @param[out] quotient Quotient value normalized between -1.0 and 1.0
  257. @param[out] shift Shift left value to get the unnormalized quotient
  258. @return error status
  259. When dividing by 0, an error ARM_MATH_NANINF is returned. And the quotient is forced
  260. to the saturated negative or positive value.
  261. */
  262. arm_status arm_divide_q15(q15_t numerator,
  263. q15_t denominator,
  264. q15_t *quotient,
  265. int16_t *shift);
  266. /**
  267. @brief Fixed point division
  268. @param[in] numerator Numerator
  269. @param[in] denominator Denominator
  270. @param[out] quotient Quotient value normalized between -1.0 and 1.0
  271. @param[out] shift Shift left value to get the unnormalized quotient
  272. @return error status
  273. When dividing by 0, an error ARM_MATH_NANINF is returned. And the quotient is forced
  274. to the saturated negative or positive value.
  275. */
  276. arm_status arm_divide_q31(q31_t numerator,
  277. q31_t denominator,
  278. q31_t *quotient,
  279. int16_t *shift);
  280. /**
  281. @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
  282. @param[in] y y coordinate
  283. @param[in] x x coordinate
  284. @param[out] result Result
  285. @return error status.
  286. */
  287. arm_status arm_atan2_f32(float32_t y,float32_t x,float32_t *result);
  288. /**
  289. @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
  290. @param[in] y y coordinate
  291. @param[in] x x coordinate
  292. @param[out] result Result in Q2.29
  293. @return error status.
  294. */
  295. arm_status arm_atan2_q31(q31_t y,q31_t x,q31_t *result);
  296. /**
  297. @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
  298. @param[in] y y coordinate
  299. @param[in] x x coordinate
  300. @param[out] result Result in Q2.13
  301. @return error status.
  302. */
  303. arm_status arm_atan2_q15(q15_t y,q15_t x,q15_t *result);
  304. #ifdef __cplusplus
  305. }
  306. #endif
  307. #endif /* ifndef _FAST_MATH_FUNCTIONS_H_ */