fast_math_functions.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /******************************************************************************
  2. * @file fast_math_functions.h
  3. * @brief Public header file for CMSIS DSP Library
  4. * @version V1.9.0
  5. * @date 23 April 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 exp 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_vexp_f32(
  130. const float32_t * pSrc,
  131. float32_t * pDst,
  132. uint32_t blockSize);
  133. /**
  134. * @defgroup SQRT Square Root
  135. *
  136. * Computes the square root of a number.
  137. * There are separate functions for Q15, Q31, and floating-point data types.
  138. * The square root function is computed using the Newton-Raphson algorithm.
  139. * This is an iterative algorithm of the form:
  140. * <pre>
  141. * x1 = x0 - f(x0)/f'(x0)
  142. * </pre>
  143. * where <code>x1</code> is the current estimate,
  144. * <code>x0</code> is the previous estimate, and
  145. * <code>f'(x0)</code> is the derivative of <code>f()</code> evaluated at <code>x0</code>.
  146. * For the square root function, the algorithm reduces to:
  147. * <pre>
  148. * x0 = in/2 [initial guess]
  149. * x1 = 1/2 * ( x0 + in / x0) [each iteration]
  150. * </pre>
  151. */
  152. /**
  153. * @addtogroup SQRT
  154. * @{
  155. */
  156. /**
  157. @brief Floating-point square root function.
  158. @param[in] in input value
  159. @param[out] pOut square root of input value
  160. @return execution status
  161. - \ref ARM_MATH_SUCCESS : input value is positive
  162. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  163. */
  164. __STATIC_FORCEINLINE arm_status arm_sqrt_f32(
  165. float32_t in,
  166. float32_t * pOut)
  167. {
  168. if (in >= 0.0f)
  169. {
  170. #if defined ( __CC_ARM )
  171. #if defined __TARGET_FPU_VFP
  172. *pOut = __sqrtf(in);
  173. #else
  174. *pOut = sqrtf(in);
  175. #endif
  176. #elif defined ( __ICCARM__ )
  177. #if defined __ARMVFP__
  178. __ASM("VSQRT.F32 %0,%1" : "=t"(*pOut) : "t"(in));
  179. #else
  180. *pOut = sqrtf(in);
  181. #endif
  182. #else
  183. *pOut = sqrtf(in);
  184. #endif
  185. return (ARM_MATH_SUCCESS);
  186. }
  187. else
  188. {
  189. *pOut = 0.0f;
  190. return (ARM_MATH_ARGUMENT_ERROR);
  191. }
  192. }
  193. /**
  194. @brief Q31 square root function.
  195. @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF
  196. @param[out] pOut points to square root of input value
  197. @return execution status
  198. - \ref ARM_MATH_SUCCESS : input value is positive
  199. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  200. */
  201. arm_status arm_sqrt_q31(
  202. q31_t in,
  203. q31_t * pOut);
  204. /**
  205. @brief Q15 square root function.
  206. @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF
  207. @param[out] pOut points to square root of input value
  208. @return execution status
  209. - \ref ARM_MATH_SUCCESS : input value is positive
  210. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  211. */
  212. arm_status arm_sqrt_q15(
  213. q15_t in,
  214. q15_t * pOut);
  215. /**
  216. * @brief Vector Floating-point square root function.
  217. * @param[in] pIn input vector.
  218. * @param[out] pOut vector of square roots of input elements.
  219. * @param[in] len length of input vector.
  220. * @return The function returns ARM_MATH_SUCCESS if input value is positive value or ARM_MATH_ARGUMENT_ERROR if
  221. * <code>in</code> is negative value and returns zero output for negative values.
  222. */
  223. void arm_vsqrt_f32(
  224. float32_t * pIn,
  225. float32_t * pOut,
  226. uint16_t len);
  227. void arm_vsqrt_q31(
  228. q31_t * pIn,
  229. q31_t * pOut,
  230. uint16_t len);
  231. void arm_vsqrt_q15(
  232. q15_t * pIn,
  233. q15_t * pOut,
  234. uint16_t len);
  235. /**
  236. * @} end of SQRT group
  237. */
  238. /**
  239. @brief Fixed point division
  240. @param[in] numerator Numerator
  241. @param[in] denominator Denominator
  242. @param[out] quotient Quotient value normalized between -1.0 and 1.0
  243. @param[out] shift Shift left value to get the unnormalized quotient
  244. @return error status
  245. When dividing by 0, an error ARM_MATH_NANINF is returned. And the quotient is forced
  246. to the saturated negative or positive value.
  247. */
  248. arm_status arm_divide_q15(q15_t numerator,
  249. q15_t denominator,
  250. q15_t *quotient,
  251. int16_t *shift);
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif /* ifndef _FAST_MATH_FUNCTIONS_H_ */