fast_math_functions.h 10 KB

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