utils.h 6.1 KB

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