fast_math_functions.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /******************************************************************************
  2. * @file fast_math_functions.h
  3. * @brief Public header file for NMSIS DSP Library
  4. * @version V1.10.0
  5. * @date 08 July 2021
  6. * Target Processor: RISC-V Cores
  7. ******************************************************************************/
  8. /*
  9. * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  10. * Copyright (c) 2019 Nuclei Limited. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef FAST_MATH_FUNCTIONS_H_
  27. #define FAST_MATH_FUNCTIONS_H_
  28. #include "riscv_math_types.h"
  29. #include "riscv_math_memory.h"
  30. #include "dsp/none.h"
  31. #include "dsp/utils.h"
  32. #include "dsp/basic_math_functions.h"
  33. #include <math.h>
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #endif
  38. /**
  39. * @brief Macros required for SINE and COSINE Fast math approximations
  40. */
  41. #define FAST_MATH_TABLE_SIZE 512
  42. #define FAST_MATH_Q31_SHIFT (32 - 10)
  43. #define FAST_MATH_Q15_SHIFT (16 - 10)
  44. #ifndef PI
  45. #define PI 3.14159265358979f
  46. #endif
  47. #ifndef PI_F64
  48. #define PI_F64 3.14159265358979323846
  49. #endif
  50. /**
  51. * @defgroup groupFastMath Fast Math Functions
  52. * This set of functions provides a fast approximation to sine, cosine, and square root.
  53. * As compared to most of the other functions in the NMSIS math library, the fast math functions
  54. * operate on individual values and not arrays.
  55. * There are separate functions for Q15, Q31, and floating-point data.
  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 riscv_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 riscv_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 riscv_sin_q15(
  78. q15_t x);
  79. /**
  80. * @brief Fast approximation to the trigonometric cosine function for floating-point data.
  81. * @param[in] x input value in radians.
  82. * @return cos(x).
  83. */
  84. float32_t riscv_cos_f32(
  85. float32_t x);
  86. /**
  87. * @brief Fast approximation to the trigonometric cosine function for Q31 data.
  88. * @param[in] x Scaled input value in radians.
  89. * @return cos(x).
  90. */
  91. q31_t riscv_cos_q31(
  92. q31_t x);
  93. /**
  94. * @brief Fast approximation to the trigonometric cosine function for Q15 data.
  95. * @param[in] x Scaled input value in radians.
  96. * @return cos(x).
  97. */
  98. q15_t riscv_cos_q15(
  99. q15_t x);
  100. /**
  101. @brief Floating-point vector of log values.
  102. @param[in] pSrc points to the input vector
  103. @param[out] pDst points to the output vector
  104. @param[in] blockSize number of samples in each vector
  105. */
  106. void riscv_vlog_f32(
  107. const float32_t * pSrc,
  108. float32_t * pDst,
  109. uint32_t blockSize);
  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. */
  116. void riscv_vlog_f64(
  117. const float64_t * pSrc,
  118. float64_t * pDst,
  119. uint32_t blockSize);
  120. /**
  121. * @brief q31 vector of log values.
  122. * @param[in] pSrc points to the input vector in q31
  123. * @param[out] pDst points to the output vector in q5.26
  124. * @param[in] blockSize number of samples in each vector
  125. */
  126. void riscv_vlog_q31(const q31_t * pSrc,
  127. q31_t * pDst,
  128. uint32_t blockSize);
  129. /**
  130. * @brief q15 vector of log values.
  131. * @param[in] pSrc points to the input vector in q15
  132. * @param[out] pDst points to the output vector in q4.11
  133. * @param[in] blockSize number of samples in each vector
  134. */
  135. void riscv_vlog_q15(const q15_t * pSrc,
  136. q15_t * pDst,
  137. uint32_t blockSize);
  138. /**
  139. @brief Floating-point vector of exp values.
  140. @param[in] pSrc points to the input vector
  141. @param[out] pDst points to the output vector
  142. @param[in] blockSize number of samples in each vector
  143. */
  144. void riscv_vexp_f32(
  145. const float32_t * pSrc,
  146. float32_t * pDst,
  147. uint32_t blockSize);
  148. /**
  149. @brief Floating-point vector of exp values.
  150. @param[in] pSrc points to the input vector
  151. @param[out] pDst points to the output vector
  152. @param[in] blockSize number of samples in each vector
  153. */
  154. void riscv_vexp_f64(
  155. const float64_t * pSrc,
  156. float64_t * pDst,
  157. uint32_t blockSize);
  158. /**
  159. * @defgroup SQRT Square Root
  160. *
  161. * Computes the square root of a number.
  162. * There are separate functions for Q15, Q31, and floating-point data types.
  163. * The square root function is computed using the Newton-Raphson algorithm.
  164. * This is an iterative algorithm of the form:
  165. * <pre>
  166. * x1 = x0 - f(x0)/f'(x0)
  167. * </pre>
  168. * where <code>x1</code> is the current estimate,
  169. * <code>x0</code> is the previous estimate, and
  170. * <code>f'(x0)</code> is the derivative of <code>f()</code> evaluated at <code>x0</code>.
  171. * For the square root function, the algorithm reduces to:
  172. * <pre>
  173. * x0 = in/2 [initial guess]
  174. * x1 = 1/2 * ( x0 + in / x0) [each iteration]
  175. * </pre>
  176. */
  177. /**
  178. * @addtogroup SQRT
  179. * @{
  180. */
  181. /**
  182. @brief Floating-point square root function.
  183. @param[in] in input value
  184. @param[out] pOut square root of input value
  185. @return execution status
  186. - \ref RISCV_MATH_SUCCESS : input value is positive
  187. - \ref RISCV_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  188. */
  189. __STATIC_FORCEINLINE riscv_status riscv_sqrt_f32(
  190. const float32_t in,
  191. float32_t * pOut)
  192. {
  193. if (in >= 0.0f)
  194. {
  195. *pOut = sqrtf(in);
  196. return (RISCV_MATH_SUCCESS);
  197. }
  198. else
  199. {
  200. *pOut = 0.0f;
  201. return (RISCV_MATH_ARGUMENT_ERROR);
  202. }
  203. }
  204. /**
  205. @brief Q31 square root function.
  206. @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF
  207. @param[out] pOut points to square root of input value
  208. @return execution status
  209. - \ref RISCV_MATH_SUCCESS : input value is positive
  210. - \ref RISCV_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  211. */
  212. riscv_status riscv_sqrt_q31(
  213. q31_t in,
  214. q31_t * pOut);
  215. /**
  216. @brief Q15 square root function.
  217. @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF
  218. @param[out] pOut points to square root of input value
  219. @return execution status
  220. - \ref RISCV_MATH_SUCCESS : input value is positive
  221. - \ref RISCV_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  222. */
  223. riscv_status riscv_sqrt_q15(
  224. q15_t in,
  225. q15_t * pOut);
  226. /**
  227. * @} end of SQRT group
  228. */
  229. /**
  230. @brief Fixed point division
  231. @param[in] numerator Numerator
  232. @param[in] denominator Denominator
  233. @param[out] quotient Quotient value normalized between -1.0 and 1.0
  234. @param[out] shift Shift left value to get the unnormalized quotient
  235. @return error status
  236. When dividing by 0, an error RISCV_MATH_NANINF is returned. And the quotient is forced
  237. to the saturated negative or positive value.
  238. */
  239. riscv_status riscv_divide_q15(q15_t numerator,
  240. q15_t denominator,
  241. q15_t *quotient,
  242. int16_t *shift);
  243. /**
  244. @brief Fixed point division
  245. @param[in] numerator Numerator
  246. @param[in] denominator Denominator
  247. @param[out] quotient Quotient value normalized between -1.0 and 1.0
  248. @param[out] shift Shift left value to get the unnormalized quotient
  249. @return error status
  250. When dividing by 0, an error RISCV_MATH_NANINF is returned. And the quotient is forced
  251. to the saturated negative or positive value.
  252. */
  253. riscv_status riscv_divide_q31(q31_t numerator,
  254. q31_t denominator,
  255. q31_t *quotient,
  256. int16_t *shift);
  257. /**
  258. @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
  259. @param[in] y y coordinate
  260. @param[in] x x coordinate
  261. @param[out] result Result
  262. @return error status.
  263. */
  264. riscv_status riscv_atan2_f32(float32_t y,float32_t x,float32_t *result);
  265. /**
  266. @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
  267. @param[in] y y coordinate
  268. @param[in] x x coordinate
  269. @param[out] result Result in Q2.29
  270. @return error status.
  271. */
  272. riscv_status riscv_atan2_q31(q31_t y,q31_t x,q31_t *result);
  273. /**
  274. @brief Arc tangent in radian of y/x using sign of x and y to determine right quadrant.
  275. @param[in] y y coordinate
  276. @param[in] x x coordinate
  277. @param[out] result Result in Q2.13
  278. @return error status.
  279. */
  280. riscv_status riscv_atan2_q15(q15_t y,q15_t x,q15_t *result);
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif /* ifndef _FAST_MATH_FUNCTIONS_H_ */