arm_vlog_q15.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_vlog_q15
  4. * Description: Q15 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_Q15_ACCURACY 15
  30. /* Bit to represent the normalization factor
  31. It is Ceiling[Log2[LOG_Q15_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_Q15_ACCURACY
  35. which would give an integer part of -15.
  36. */
  37. #define LOG_Q15_INTEGER_PART 4
  38. /* 2.0 in q14 */
  39. #define LOQ_Q15_THRESHOLD (1u << LOG_Q15_ACCURACY)
  40. /* HALF */
  41. #define LOQ_Q15_Q16_HALF LOQ_Q15_THRESHOLD
  42. #define LOQ_Q15_Q14_HALF (LOQ_Q15_Q16_HALF >> 2)
  43. /* 1.0 / Log2[Exp[1]] in q15 */
  44. #define LOG_Q15_INVLOG2EXP 0x58b9u
  45. /* Clay Turner algorithm */
  46. static uint16_t arm_scalar_log_q15(uint16_t src)
  47. {
  48. int i;
  49. int16_t c = __CLZ(src)-16;
  50. int16_t normalization=0;
  51. /* 0.5 in q11 */
  52. uint16_t inc = LOQ_Q15_Q16_HALF >> (LOG_Q15_INTEGER_PART + 1);
  53. /* Will compute y = log2(x) for 1 <= x < 2.0 */
  54. uint16_t x;
  55. /* q11 */
  56. uint16_t y=0;
  57. /* q11 */
  58. int16_t tmp;
  59. /* Normalize and convert to q14 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 q11 instead of q16
  71. because we know 0 <= y < 1.0 but
  72. we want a result allowing to do a
  73. product on int16 rather than having to go
  74. through int32
  75. */
  76. for(i = 0; i < LOG_Q15_ACCURACY ; i++)
  77. {
  78. x = (((int32_t)x*x)) >> (LOG_Q15_ACCURACY - 1);
  79. if (x >= LOQ_Q15_THRESHOLD)
  80. {
  81. y += inc ;
  82. x = x >> 1;
  83. }
  84. inc = inc >> 1;
  85. }
  86. /*
  87. Convert the Log2 to Log and apply normalization.
  88. We compute (y - normalisation) * (1 / Log2[e]).
  89. */
  90. /* q11 */
  91. //tmp = y - ((int32_t)normalization << (LOG_Q15_ACCURACY + 1));
  92. tmp = (int16_t)y - (normalization << (LOG_Q15_ACCURACY - LOG_Q15_INTEGER_PART));
  93. /* q4.11 */
  94. y = ((int32_t)tmp * LOG_Q15_INVLOG2EXP) >> 15;
  95. return(y);
  96. }
  97. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  98. q15x8_t vlogq_q15(q15x8_t src)
  99. {
  100. int i;
  101. int16x8_t c = vclzq_s16(src);
  102. int16x8_t normalization = c;
  103. /* 0.5 in q11 */
  104. uint16_t inc = LOQ_Q15_Q16_HALF >> (LOG_Q15_INTEGER_PART + 1);
  105. /* Will compute y = log2(x) for 1 <= x < 2.0 */
  106. uint16x8_t x;
  107. /* q11 */
  108. uint16x8_t y = vdupq_n_u16(0);
  109. /* q11 */
  110. int16x8_t vtmp;
  111. mve_pred16_t p;
  112. /* Normalize and convert to q14 format */
  113. vtmp = vsubq_n_s16(c,1);
  114. x = vshlq_u16((uint16x8_t)src,vtmp);
  115. /* Compute the Log2. Result is in q11 instead of q16
  116. because we know 0 <= y < 1.0 but
  117. we want a result allowing to do a
  118. product on int16 rather than having to go
  119. through int32
  120. */
  121. for(i = 0; i < LOG_Q15_ACCURACY ; i++)
  122. {
  123. x = vmulhq_u16(x,x);
  124. x = vshlq_n_u16(x,2);
  125. p = vcmphiq_u16(x,vdupq_n_u16(LOQ_Q15_THRESHOLD));
  126. y = vaddq_m_n_u16(y, y,inc,p);
  127. x = vshrq_m_n_u16(x,x,1,p);
  128. inc = inc >> 1;
  129. }
  130. /*
  131. Convert the Log2 to Log and apply normalization.
  132. We compute (y - normalisation) * (1 / Log2[e]).
  133. */
  134. /* q11 */
  135. // tmp = (int16_t)y - (normalization << (LOG_Q15_ACCURACY - LOG_Q15_INTEGER_PART));
  136. vtmp = vshlq_n_s16(normalization,LOG_Q15_ACCURACY - LOG_Q15_INTEGER_PART);
  137. vtmp = vsubq_s16((int16x8_t)y,vtmp);
  138. /* q4.11 */
  139. // y = ((int32_t)tmp * LOG_Q15_INVLOG2EXP) >> 15;
  140. vtmp = vqdmulhq_n_s16(vtmp,LOG_Q15_INVLOG2EXP);
  141. return(vtmp);
  142. }
  143. #endif
  144. /**
  145. @ingroup groupFastMath
  146. */
  147. /**
  148. @addtogroup vlog
  149. @{
  150. */
  151. /**
  152. @brief q15 vector of log values.
  153. @param[in] pSrc points to the input vector in q15
  154. @param[out] pDst points to the output vector in q4.11
  155. @param[in] blockSize number of samples in each vector
  156. @return none
  157. */
  158. void arm_vlog_q15(
  159. const q15_t * pSrc,
  160. q15_t * pDst,
  161. uint32_t blockSize)
  162. {
  163. uint32_t blkCnt; /* loop counters */
  164. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  165. q15x8_t src;
  166. q15x8_t dst;
  167. blkCnt = blockSize >> 3;
  168. while (blkCnt > 0U)
  169. {
  170. src = vld1q(pSrc);
  171. dst = vlogq_q15(src);
  172. vst1q(pDst, dst);
  173. pSrc += 8;
  174. pDst += 8;
  175. /* Decrement loop counter */
  176. blkCnt--;
  177. }
  178. blkCnt = blockSize & 7;
  179. #else
  180. blkCnt = blockSize;
  181. #endif
  182. while (blkCnt > 0U)
  183. {
  184. *pDst++ = arm_scalar_log_q15(*pSrc++);
  185. /* Decrement loop counter */
  186. blkCnt--;
  187. }
  188. }
  189. /**
  190. @} end of vlog group
  191. */