fast_math_functions.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /******************************************************************************
  2. * @file fast_math_functions.h
  3. * @brief Public header file for CMSIS DSP Library
  4. * @version V1.9.0
  5. * @date 20. July 2020
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #ifndef _FAST_MATH_FUNCTIONS_H_
  25. #define _FAST_MATH_FUNCTIONS_H_
  26. #include "arm_math_types.h"
  27. #include "arm_math_memory.h"
  28. #include "dsp/none.h"
  29. #include "dsp/utils.h"
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif
  34. /**
  35. * @brief Macros required for SINE and COSINE Fast math approximations
  36. */
  37. #define FAST_MATH_TABLE_SIZE 512
  38. #define FAST_MATH_Q31_SHIFT (32 - 10)
  39. #define FAST_MATH_Q15_SHIFT (16 - 10)
  40. #ifndef PI
  41. #define PI 3.14159265358979f
  42. #endif
  43. /**
  44. * @defgroup groupFastMath Fast Math Functions
  45. * This set of functions provides a fast approximation to sine, cosine, and square root.
  46. * As compared to most of the other functions in the CMSIS math library, the fast math functions
  47. * operate on individual values and not arrays.
  48. * There are separate functions for Q15, Q31, and floating-point data.
  49. *
  50. */
  51. /**
  52. * @ingroup groupFastMath
  53. */
  54. /**
  55. @addtogroup sin
  56. @{
  57. */
  58. /**
  59. * @brief Fast approximation to the trigonometric sine function for floating-point data.
  60. * @param[in] x input value in radians.
  61. * @return sin(x).
  62. */
  63. float32_t arm_sin_f32(
  64. float32_t x);
  65. /**
  66. * @brief Fast approximation to the trigonometric sine function for Q31 data.
  67. * @param[in] x Scaled input value in radians.
  68. * @return sin(x).
  69. */
  70. q31_t arm_sin_q31(
  71. q31_t x);
  72. /**
  73. * @brief Fast approximation to the trigonometric sine function for Q15 data.
  74. * @param[in] x Scaled input value in radians.
  75. * @return sin(x).
  76. */
  77. q15_t arm_sin_q15(
  78. q15_t x);
  79. /**
  80. @} end of sin group
  81. */
  82. /**
  83. @addtogroup cos
  84. @{
  85. */
  86. /**
  87. * @brief Fast approximation to the trigonometric cosine function for floating-point data.
  88. * @param[in] x input value in radians.
  89. * @return cos(x).
  90. */
  91. float32_t arm_cos_f32(
  92. float32_t x);
  93. /**
  94. * @brief Fast approximation to the trigonometric cosine function for Q31 data.
  95. * @param[in] x Scaled input value in radians.
  96. * @return cos(x).
  97. */
  98. q31_t arm_cos_q31(
  99. q31_t x);
  100. /**
  101. * @brief Fast approximation to the trigonometric cosine function for Q15 data.
  102. * @param[in] x Scaled input value in radians.
  103. * @return cos(x).
  104. */
  105. q15_t arm_cos_q15(
  106. q15_t x);
  107. /**
  108. @} end of cos group
  109. */
  110. /**
  111. @brief Floating-point vector of log values.
  112. @param[in] pSrc points to the input vector
  113. @param[out] pDst points to the output vector
  114. @param[in] blockSize number of samples in each vector
  115. @return none
  116. */
  117. void arm_vlog_f32(
  118. const float32_t * pSrc,
  119. float32_t * pDst,
  120. uint32_t blockSize);
  121. /**
  122. @brief Floating-point vector of exp values.
  123. @param[in] pSrc points to the input vector
  124. @param[out] pDst points to the output vector
  125. @param[in] blockSize number of samples in each vector
  126. @return none
  127. */
  128. void arm_vexp_f32(
  129. const float32_t * pSrc,
  130. float32_t * pDst,
  131. uint32_t blockSize);
  132. /**
  133. * @defgroup SQRT Square Root
  134. *
  135. * Computes the square root of a number.
  136. * There are separate functions for Q15, Q31, and floating-point data types.
  137. * The square root function is computed using the Newton-Raphson algorithm.
  138. * This is an iterative algorithm of the form:
  139. * <pre>
  140. * x1 = x0 - f(x0)/f'(x0)
  141. * </pre>
  142. * where <code>x1</code> is the current estimate,
  143. * <code>x0</code> is the previous estimate, and
  144. * <code>f'(x0)</code> is the derivative of <code>f()</code> evaluated at <code>x0</code>.
  145. * For the square root function, the algorithm reduces to:
  146. * <pre>
  147. * x0 = in/2 [initial guess]
  148. * x1 = 1/2 * ( x0 + in / x0) [each iteration]
  149. * </pre>
  150. */
  151. /**
  152. * @addtogroup SQRT
  153. * @{
  154. */
  155. /**
  156. @brief Floating-point square root function.
  157. @param[in] in input value
  158. @param[out] pOut square root of input value
  159. @return execution status
  160. - \ref ARM_MATH_SUCCESS : input value is positive
  161. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  162. */
  163. __STATIC_FORCEINLINE arm_status arm_sqrt_f32(
  164. float32_t in,
  165. float32_t * pOut)
  166. {
  167. if (in >= 0.0f)
  168. {
  169. #if defined ( __CC_ARM )
  170. #if defined __TARGET_FPU_VFP
  171. *pOut = __sqrtf(in);
  172. #else
  173. *pOut = sqrtf(in);
  174. #endif
  175. #elif defined ( __ICCARM__ )
  176. #if defined __ARMVFP__
  177. __ASM("VSQRT.F32 %0,%1" : "=t"(*pOut) : "t"(in));
  178. #else
  179. *pOut = sqrtf(in);
  180. #endif
  181. #else
  182. *pOut = sqrtf(in);
  183. #endif
  184. return (ARM_MATH_SUCCESS);
  185. }
  186. else
  187. {
  188. *pOut = 0.0f;
  189. return (ARM_MATH_ARGUMENT_ERROR);
  190. }
  191. }
  192. /**
  193. @brief Q31 square root function.
  194. @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF
  195. @param[out] pOut points to square root of input value
  196. @return execution status
  197. - \ref ARM_MATH_SUCCESS : input value is positive
  198. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  199. */
  200. arm_status arm_sqrt_q31(
  201. q31_t in,
  202. q31_t * pOut);
  203. /**
  204. @brief Q15 square root function.
  205. @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF
  206. @param[out] pOut points to square root of input value
  207. @return execution status
  208. - \ref ARM_MATH_SUCCESS : input value is positive
  209. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  210. */
  211. arm_status arm_sqrt_q15(
  212. q15_t in,
  213. q15_t * pOut);
  214. /**
  215. * @brief Vector Floating-point square root function.
  216. * @param[in] pIn input vector.
  217. * @param[out] pOut vector of square roots of input elements.
  218. * @param[in] len length of input vector.
  219. * @return The function returns ARM_MATH_SUCCESS if input value is positive value or ARM_MATH_ARGUMENT_ERROR if
  220. * <code>in</code> is negative value and returns zero output for negative values.
  221. */
  222. void arm_vsqrt_f32(
  223. float32_t * pIn,
  224. float32_t * pOut,
  225. uint16_t len);
  226. void arm_vsqrt_q31(
  227. q31_t * pIn,
  228. q31_t * pOut,
  229. uint16_t len);
  230. void arm_vsqrt_q15(
  231. q15_t * pIn,
  232. q15_t * pOut,
  233. uint16_t len);
  234. /**
  235. * @} end of SQRT group
  236. */
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif /* ifndef _FAST_MATH_FUNCTIONS_H_ */