arm_vlog_q31.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_vlog_q31
  4. * Description: Q31 vector log
  5. *
  6. * $Date: 19 July 2021
  7. * $Revision: V1.10.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/fast_math_functions.h"
  29. #define LOG_Q31_ACCURACY 31
  30. /* Bit to represent the normalization factor
  31. It is Ceiling[Log2[LOG_Q31_ACCURACY]] of the previous value.
  32. The Log2 algorithm is assuming that the value x is
  33. 1 <= x < 2.
  34. But input value could be as small a 2^-LOG_Q31_ACCURACY
  35. which would give an integer part of -31.
  36. */
  37. #define LOG_Q31_INTEGER_PART 5
  38. /* 2.0 in Q30 */
  39. #define LOQ_Q31_THRESHOLD (1u << LOG_Q31_ACCURACY)
  40. /* HALF */
  41. #define LOQ_Q31_Q32_HALF LOQ_Q31_THRESHOLD
  42. #define LOQ_Q31_Q30_HALF (LOQ_Q31_Q32_HALF >> 2)
  43. /* 1.0 / Log2[Exp[1]] in Q31 */
  44. #define LOG_Q31_INVLOG2EXP 0x58b90bfbuL
  45. /* Clay Turner algorithm */
  46. static uint32_t arm_scalar_log_q31(uint32_t src)
  47. {
  48. int32_t i;
  49. int32_t c = __CLZ(src);
  50. int32_t normalization=0;
  51. /* 0.5 in q26 */
  52. uint32_t inc = LOQ_Q31_Q32_HALF >> (LOG_Q31_INTEGER_PART + 1);
  53. /* Will compute y = log2(x) for 1 <= x < 2.0 */
  54. uint32_t x;
  55. /* q26 */
  56. uint32_t y=0;
  57. /* q26 */
  58. int32_t tmp;
  59. /* Normalize and convert to q30 format */
  60. x = src;
  61. if ((c-1) < 0)
  62. {
  63. x = x >> (1-c);
  64. }
  65. else
  66. {
  67. x = x << (c-1);
  68. }
  69. normalization = c;
  70. /* Compute the Log2. Result is in q26
  71. because we know 0 <= y < 1.0 but
  72. do not want to use q32 to allow
  73. following computation with less instructions.
  74. */
  75. for(i = 0; i < LOG_Q31_ACCURACY ; i++)
  76. {
  77. x = ((int64_t)x*x) >> (LOG_Q31_ACCURACY - 1);
  78. if (x >= LOQ_Q31_THRESHOLD)
  79. {
  80. y += inc ;
  81. x = x >> 1;
  82. }
  83. inc = inc >> 1;
  84. }
  85. /*
  86. Convert the Log2 to Log and apply normalization.
  87. We compute (y - normalisation) * (1 / Log2[e]).
  88. */
  89. /* q26 */
  90. tmp = (int32_t)y - (normalization << (LOG_Q31_ACCURACY - LOG_Q31_INTEGER_PART));
  91. /* q5.26 */
  92. y = ((int64_t)tmp * LOG_Q31_INVLOG2EXP) >> 31;
  93. return(y);
  94. }
  95. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  96. q31x4_t vlogq_q31(q31x4_t src)
  97. {
  98. int32_t i;
  99. int32x4_t c = vclzq_s32(src);
  100. int32x4_t normalization = c;
  101. /* 0.5 in q11 */
  102. uint32_t inc = LOQ_Q31_Q32_HALF >> (LOG_Q31_INTEGER_PART + 1);
  103. /* Will compute y = log2(x) for 1 <= x < 2.0 */
  104. uint32x4_t x;
  105. /* q11 */
  106. uint32x4_t y = vdupq_n_u32(0);
  107. /* q11 */
  108. int32x4_t vtmp;
  109. mve_pred16_t p;
  110. /* Normalize and convert to q14 format */
  111. vtmp = vsubq_n_s32(c,1);
  112. x = vshlq_u32((uint32x4_t)src,vtmp);
  113. /* Compute the Log2. Result is in Q26
  114. because we know 0 <= y < 1.0 but
  115. do not want to use Q32 to allow
  116. following computation with less instructions.
  117. */
  118. for(i = 0; i < LOG_Q31_ACCURACY ; i++)
  119. {
  120. x = vmulhq_u32(x,x);
  121. x = vshlq_n_u32(x,2);
  122. p = vcmphiq_u32(x,vdupq_n_u32(LOQ_Q31_THRESHOLD));
  123. y = vaddq_m_n_u32(y, y,inc,p);
  124. x = vshrq_m_n_u32(x,x,1,p);
  125. inc = inc >> 1;
  126. }
  127. /*
  128. Convert the Log2 to Log and apply normalization.
  129. We compute (y - normalisation) * (1 / Log2[e]).
  130. */
  131. /* q11 */
  132. // tmp = (int16_t)y - (normalization << (LOG_Q15_ACCURACY - LOG_Q15_INTEGER_PART));
  133. vtmp = vshlq_n_s32(normalization,LOG_Q31_ACCURACY - LOG_Q31_INTEGER_PART);
  134. vtmp = vsubq_s32((int32x4_t)y,vtmp);
  135. /* q4.11 */
  136. // y = ((int32_t)tmp * LOG_Q15_INVLOG2EXP) >> 15;
  137. vtmp = vqdmulhq_n_s32(vtmp,LOG_Q31_INVLOG2EXP);
  138. return(vtmp);
  139. }
  140. #endif
  141. /**
  142. @ingroup groupFastMath
  143. */
  144. /**
  145. @addtogroup vlog
  146. @{
  147. */
  148. /**
  149. @brief q31 vector of log values.
  150. @param[in] pSrc points to the input vector in q31
  151. @param[out] pDst points to the output vector q5.26
  152. @param[in] blockSize number of samples in each vector
  153. @return none
  154. */
  155. void arm_vlog_q31(
  156. const q31_t * pSrc,
  157. q31_t * pDst,
  158. uint32_t blockSize)
  159. {
  160. uint32_t blkCnt; /* loop counters */
  161. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  162. q31x4_t src;
  163. q31x4_t dst;
  164. blkCnt = blockSize >> 2;
  165. while (blkCnt > 0U)
  166. {
  167. src = vld1q(pSrc);
  168. dst = vlogq_q31(src);
  169. vst1q(pDst, dst);
  170. pSrc += 4;
  171. pDst += 4;
  172. /* Decrement loop counter */
  173. blkCnt--;
  174. }
  175. blkCnt = blockSize & 3;
  176. #else
  177. blkCnt = blockSize;
  178. #endif
  179. while (blkCnt > 0U)
  180. {
  181. *pDst++=arm_scalar_log_q31(*pSrc++);
  182. blkCnt--;
  183. }
  184. }
  185. /**
  186. @} end of vlog group
  187. */