fast_math_functions.h 9.1 KB

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