arm_vlog_f16.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_vlog_f16.c
  4. * Description: Fast vectorized log
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.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_f16.h"
  29. #include "dsp/support_functions_f16.h"
  30. #if defined(ARM_FLOAT16_SUPPORTED)
  31. /* Degree of the polynomial approximation */
  32. #define NB_DEG_LOGF16 3
  33. /*
  34. Related to the Log2 of the number of approximations.
  35. For instance, with 3 there are 1 + 2^3 polynomials
  36. */
  37. #define NB_DIV_LOGF16 3
  38. /* Length of the LUT table */
  39. #define NB_LUT_LOGF16 (NB_DEG_LOGF16+1)*(1 + (1<<NB_DIV_LOGF16))
  40. /*
  41. LUT of polynomial approximations.
  42. Could be generated with:
  43. ClearAll[lut, coefs, nb, deg];
  44. nb = 3;
  45. deg = 3;
  46. lut = Table[
  47. MiniMaxApproximation[
  48. Log[x/2^nb + i], {x, {10^-6, 1.0/2^nb}, deg, 0},
  49. MaxIterations -> 1000][[2, 1]], {i, 1, 2, (1.0/2^nb)}];
  50. coefs = Chop@Flatten[CoefficientList[lut, x]];
  51. */
  52. static float16_t lut_logf16[NB_LUT_LOGF16]={
  53. 0,0.125,-0.00781197,0.00063974,0.117783,
  54. 0.111111,-0.00617212,0.000447935,0.223144,
  55. 0.1,-0.00499952,0.000327193,0.318454,0.0909091,
  56. -0.00413191,0.000246234,0.405465,0.0833333,
  57. -0.00347199,0.000189928,0.485508,0.0769231,
  58. -0.00295841,0.00014956,0.559616,0.0714286,
  59. -0.0025509,0.000119868,0.628609,0.0666667,
  60. -0.00222213,0.0000975436,0.693147,
  61. 0.0625,-0.00195305,0.0000804357};
  62. float16_t logf16_scalar(float16_t x)
  63. {
  64. int16_t i = arm_typecast_s16_f16(x);
  65. int32_t vecExpUnBiased = (i >> 10) - 15;
  66. i = i - (vecExpUnBiased << 10);
  67. float16_t vecTmpFlt1 = arm_typecast_f16_s16(i);
  68. float16_t *lut;
  69. int n;
  70. float16_t tmp,v;
  71. tmp = ((_Float16)vecTmpFlt1 - 1.0f16) * (1 << NB_DIV_LOGF16);
  72. n = (int)floor((double)tmp);
  73. v = (_Float16)tmp - (_Float16)n;
  74. lut = lut_logf16 + n * (1+NB_DEG_LOGF16);
  75. float16_t res = lut[NB_DEG_LOGF16-1];
  76. for(int j=NB_DEG_LOGF16-2; j >=0 ; j--)
  77. {
  78. res = (_Float16)lut[j] + (_Float16)v * (_Float16)res;
  79. }
  80. res = (_Float16)res + 0.693147f16 * (_Float16)vecExpUnBiased;
  81. return(res);
  82. }
  83. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  84. #include "arm_common_tables.h"
  85. #include "arm_vec_math_f16.h"
  86. float16x8_t vlogq_lut_f16(float16x8_t vecIn)
  87. {
  88. int16x8_t i = vreinterpretq_s16_f16(vecIn);
  89. int16x8_t vecExpUnBiased = vsubq_n_s16(vshrq_n_s16(i,10), 15);
  90. i = vsubq_s16(i,vshlq_n_s16(vecExpUnBiased,10));
  91. float16x8_t vecTmpFlt1 = vreinterpretq_f16_s16(i);
  92. float16x8_t lutV;
  93. int16x8_t n;
  94. int16x8_t offset;
  95. float16x8_t tmp,v,res;
  96. tmp = vmulq_n_f16(vsubq_n_f16(vecTmpFlt1,1.0f16),(_Float16)(1 << NB_DIV_LOGF16));
  97. n = vcvtq_s16_f16(tmp);
  98. v = vsubq_f16(tmp,vcvtq_f16_s16(n));
  99. offset = vmulq_n_s16(n,(1+NB_DEG_LOGF16));
  100. offset = vaddq_n_s16(offset,NB_DEG_LOGF16-1);
  101. res = vldrhq_gather_shifted_offset_f16(lut_logf16,(uint16x8_t)offset);
  102. offset = vsubq_n_s16(offset,1);
  103. for(int j=NB_DEG_LOGF16-2; j >=0 ; j--)
  104. {
  105. lutV = vldrhq_gather_shifted_offset_f16(lut_logf16,(uint16x8_t)offset);
  106. res = vfmaq_f16(lutV,v,res);
  107. offset = vsubq_n_s16(offset,1);
  108. }
  109. res = vfmaq_n_f16(res,vcvtq_f16_s16(vecExpUnBiased),0.693147f16);
  110. return(res);
  111. }
  112. #endif
  113. /**
  114. @ingroup groupFastMath
  115. */
  116. /**
  117. @addtogroup vlog
  118. @{
  119. */
  120. /**
  121. @brief Floating-point vector of log values.
  122. @param[in] pSrc points to the input vector
  123. @param[out] pDst points to the output vector
  124. @param[in] blockSize number of samples in each vector
  125. @return none
  126. */
  127. void arm_vlog_f16(
  128. const float16_t * pSrc,
  129. float16_t * pDst,
  130. uint32_t blockSize)
  131. {
  132. uint32_t blkCnt;
  133. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  134. f16x8_t src;
  135. f16x8_t dst;
  136. blkCnt = blockSize >> 3;
  137. while (blkCnt > 0U)
  138. {
  139. src = vld1q(pSrc);
  140. dst = vlogq_lut_f16(src);
  141. vst1q(pDst, dst);
  142. pSrc += 8;
  143. pDst += 8;
  144. /* Decrement loop counter */
  145. blkCnt--;
  146. }
  147. blkCnt = blockSize & 7;
  148. #else
  149. blkCnt = blockSize;
  150. #endif
  151. while (blkCnt > 0U)
  152. {
  153. /* C = log(A) */
  154. /* Calculate log and store result in destination buffer. */
  155. *pDst++ = logf16_scalar(*pSrc++);
  156. /* Decrement loop counter */
  157. blkCnt--;
  158. }
  159. }
  160. /**
  161. @} end of vlog group
  162. */
  163. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */