utils.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /******************************************************************************
  2. * @file riscv_math_utils.h
  3. * @brief Public header file for NMSIS 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. * Copyright (c) 2019 Nuclei Limited. 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 RISCV_MATH_UTILS_H_
  26. #define RISCV_MATH_UTILS_H_
  27. #include "riscv_math_types.h"
  28. #include <limits.h>
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. /**
  34. * @brief Macros required for reciprocal calculation in Normalized LMS
  35. */
  36. #define INDEX_MASK 0x0000003F
  37. #ifndef MIN
  38. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  39. #endif
  40. #ifndef MAX
  41. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  42. #endif
  43. #ifndef RISCV_SQ
  44. #define RISCV_SQ(x) ((x) * (x))
  45. #endif
  46. #ifndef RISCV_ROUND_UP
  47. #define RISCV_ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
  48. #endif
  49. /**
  50. * @brief Function to Calculates 1/in (reciprocal) value of Q31 Data type.
  51. It should not be used with negative values.
  52. */
  53. __STATIC_FORCEINLINE uint32_t riscv_recip_q31(
  54. q31_t in,
  55. q31_t * dst,
  56. const q31_t * pRecipTable)
  57. {
  58. q31_t out;
  59. uint32_t tempVal;
  60. uint32_t index, i;
  61. uint32_t signBits;
  62. if (in > 0)
  63. {
  64. signBits = ((uint32_t) (__CLZ( (uint32_t)in) - 1));
  65. }
  66. else
  67. {
  68. signBits = ((uint32_t) (__CLZ((uint32_t)(-in)) - 1));
  69. }
  70. /* Convert input sample to 1.31 format */
  71. in = (in << signBits);
  72. /* calculation of index for initial approximated Val */
  73. index = (uint32_t)(in >> 24);
  74. index = (index & INDEX_MASK);
  75. /* 1.31 with exp 1 */
  76. out = pRecipTable[index];
  77. /* calculation of reciprocal value */
  78. /* running approximation for two iterations */
  79. for (i = 0U; i < 2U; i++)
  80. {
  81. tempVal = (uint32_t) (((q63_t) in * out) >> 31);
  82. tempVal = 0x7FFFFFFFu - tempVal;
  83. /* 1.31 with exp 1 */
  84. /* out = (q31_t) (((q63_t) out * tempVal) >> 30); */
  85. out = clip_q63_to_q31(((q63_t) out * tempVal) >> 30);
  86. }
  87. /* write output */
  88. *dst = out;
  89. /* return num of signbits of out = 1/in value */
  90. return (signBits + 1U);
  91. }
  92. /**
  93. * @brief Function to Calculates 1/in (reciprocal) value of Q15 Data type.
  94. It should not be used with negative values.
  95. */
  96. __STATIC_FORCEINLINE uint32_t riscv_recip_q15(
  97. q15_t in,
  98. q15_t * dst,
  99. const q15_t * pRecipTable)
  100. {
  101. q15_t out = 0;
  102. int32_t tempVal = 0;
  103. uint32_t index = 0, i = 0;
  104. uint32_t signBits = 0;
  105. if (in > 0)
  106. {
  107. signBits = ((uint32_t)(__CLZ( (uint32_t)in) - 17));
  108. }
  109. else
  110. {
  111. signBits = ((uint32_t)(__CLZ((uint32_t)(-in)) - 17));
  112. }
  113. /* Convert input sample to 1.15 format */
  114. in = (q15_t)(in << signBits);
  115. /* calculation of index for initial approximated Val */
  116. index = (uint32_t)(in >> 8);
  117. index = (index & INDEX_MASK);
  118. /* 1.15 with exp 1 */
  119. out = pRecipTable[index];
  120. /* calculation of reciprocal value */
  121. /* running approximation for two iterations */
  122. for (i = 0U; i < 2U; i++)
  123. {
  124. tempVal = (((q31_t) in * out) >> 15);
  125. tempVal = 0x7FFF - tempVal;
  126. /* 1.15 with exp 1 */
  127. out = (q15_t) (((q31_t) out * tempVal) >> 14);
  128. /* out = clip_q31_to_q15(((q31_t) out * tempVal) >> 14); */
  129. }
  130. /* write output */
  131. *dst = out;
  132. /* return num of signbits of out = 1/in value */
  133. return (signBits + 1);
  134. }
  135. /**
  136. * @brief 64-bit to 32-bit unsigned normalization
  137. * @param[in] in is input unsigned long long value
  138. * @param[out] normalized is the 32-bit normalized value
  139. * @param[out] norm is norm scale
  140. */
  141. __STATIC_INLINE void riscv_norm_64_to_32u(uint64_t in, int32_t * normalized, int32_t *norm)
  142. {
  143. int32_t n1;
  144. int32_t hi = (int32_t) (in >> 32);
  145. int32_t lo = (int32_t) ((in << 32) >> 32);
  146. n1 = __CLZ((uint32_t)hi) - 32;
  147. if (!n1)
  148. {
  149. /*
  150. * input fits in 32-bit
  151. */
  152. n1 = __CLZ((uint32_t)lo);
  153. if (!n1)
  154. {
  155. /*
  156. * MSB set, need to scale down by 1
  157. */
  158. *norm = -1;
  159. *normalized = (((uint32_t) lo) >> 1);
  160. } else
  161. {
  162. if (n1 == 32)
  163. {
  164. /*
  165. * input is zero
  166. */
  167. *norm = 0;
  168. *normalized = 0;
  169. } else
  170. {
  171. /*
  172. * 32-bit normalization
  173. */
  174. *norm = n1 - 1;
  175. *normalized = lo << *norm;
  176. }
  177. }
  178. } else
  179. {
  180. /*
  181. * input fits in 64-bit
  182. */
  183. n1 = 1 - n1;
  184. *norm = -n1;
  185. /*
  186. * 64 bit normalization
  187. */
  188. *normalized = (int32_t)(((uint32_t)lo) >> n1) | (hi << (32 - n1));
  189. }
  190. }
  191. __STATIC_INLINE int32_t riscv_div_int64_to_int32(int64_t num, int32_t den)
  192. {
  193. int32_t result;
  194. uint64_t absNum;
  195. int32_t normalized;
  196. int32_t norm;
  197. /*
  198. * if sum fits in 32bits
  199. * avoid costly 64-bit division
  200. */
  201. if (num == (int64_t)LONG_MIN)
  202. {
  203. absNum = LONG_MAX;
  204. }
  205. else
  206. {
  207. absNum = (uint64_t) (num > 0 ? num : -num);
  208. }
  209. riscv_norm_64_to_32u(absNum, &normalized, &norm);
  210. if (norm > 0)
  211. /*
  212. * 32-bit division
  213. */
  214. result = (int32_t) num / den;
  215. else
  216. /*
  217. * 64-bit division
  218. */
  219. result = (int32_t) (num / den);
  220. return result;
  221. }
  222. #undef INDEX_MASK
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif /*ifndef _RISCV_MATH_UTILS_H_ */