arm_cmplx_mag_f16.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_f16.c
  4. * Description: Floating-point complex magnitude
  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/complex_math_functions_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupCmplxMath
  32. */
  33. /**
  34. @addtogroup cmplx_mag
  35. @{
  36. */
  37. /**
  38. @brief Floating-point complex magnitude.
  39. @param[in] pSrc points to input vector
  40. @param[out] pDst points to output vector
  41. @param[in] numSamples number of samples in each vector
  42. */
  43. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. void arm_cmplx_mag_f16(
  46. const float16_t * pSrc,
  47. float16_t * pDst,
  48. uint32_t numSamples)
  49. {
  50. int32_t blockSize = numSamples; /* loop counters */
  51. uint32_t blkCnt; /* loop counters */
  52. f16x8x2_t vecSrc;
  53. f16x8_t sum;
  54. /* Compute 4 complex samples at a time */
  55. blkCnt = blockSize >> 3;
  56. while (blkCnt > 0U)
  57. {
  58. q15x8_t newtonStartVec;
  59. f16x8_t sumHalf, invSqrt;
  60. vecSrc = vld2q(pSrc);
  61. pSrc += 16;
  62. sum = vmulq(vecSrc.val[0], vecSrc.val[0]);
  63. sum = vfmaq(sum, vecSrc.val[1], vecSrc.val[1]);
  64. /*
  65. * inlined Fast SQRT using inverse SQRT newton-raphson method
  66. */
  67. /* compute initial value */
  68. newtonStartVec = vdupq_n_s16(INVSQRT_MAGIC_F16) - vshrq((q15x8_t) sum, 1);
  69. sumHalf = sum * 0.5f;
  70. /*
  71. * compute 3 x iterations
  72. *
  73. * The more iterations, the more accuracy.
  74. * If you need to trade a bit of accuracy for more performance,
  75. * you can comment out the 3rd use of the macro.
  76. */
  77. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, (f16x8_t) newtonStartVec);
  78. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, invSqrt);
  79. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, invSqrt);
  80. /*
  81. * set negative values to 0
  82. */
  83. invSqrt = vdupq_m(invSqrt, (float16_t)0.0f, vcmpltq(invSqrt, (float16_t)0.0f));
  84. /*
  85. * sqrt(x) = x * invSqrt(x)
  86. */
  87. sum = vmulq(sum, invSqrt);
  88. vstrhq_f16(pDst, sum);
  89. pDst += 8;
  90. /*
  91. * Decrement the blockSize loop counter
  92. */
  93. blkCnt--;
  94. }
  95. /*
  96. * tail
  97. */
  98. blkCnt = blockSize & 7;
  99. if (blkCnt > 0U)
  100. {
  101. mve_pred16_t p0 = vctp16q(blkCnt);
  102. q15x8_t newtonStartVec;
  103. f16x8_t sumHalf, invSqrt;
  104. vecSrc = vld2q((float16_t const *)pSrc);
  105. sum = vmulq(vecSrc.val[0], vecSrc.val[0]);
  106. sum = vfmaq(sum, vecSrc.val[1], vecSrc.val[1]);
  107. /*
  108. * inlined Fast SQRT using inverse SQRT newton-raphson method
  109. */
  110. /* compute initial value */
  111. newtonStartVec = vdupq_n_s16(INVSQRT_MAGIC_F16) - vshrq((q15x8_t) sum, 1);
  112. sumHalf = vmulq(sum, (float16_t)0.5);
  113. /*
  114. * compute 2 x iterations
  115. */
  116. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, (f16x8_t) newtonStartVec);
  117. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, invSqrt);
  118. /*
  119. * set negative values to 0
  120. */
  121. invSqrt = vdupq_m(invSqrt, (float16_t)0.0, vcmpltq(invSqrt, (float16_t)0.0));
  122. /*
  123. * sqrt(x) = x * invSqrt(x)
  124. */
  125. sum = vmulq(sum, invSqrt);
  126. vstrhq_p_f16(pDst, sum, p0);
  127. }
  128. }
  129. #else
  130. void arm_cmplx_mag_f16(
  131. const float16_t * pSrc,
  132. float16_t * pDst,
  133. uint32_t numSamples)
  134. {
  135. uint32_t blkCnt; /* loop counter */
  136. _Float16 real, imag; /* Temporary variables to hold input values */
  137. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  138. /* Loop unrolling: Compute 4 outputs at a time */
  139. blkCnt = numSamples >> 2U;
  140. while (blkCnt > 0U)
  141. {
  142. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  143. real = *pSrc++;
  144. imag = *pSrc++;
  145. /* store result in destination buffer. */
  146. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  147. real = *pSrc++;
  148. imag = *pSrc++;
  149. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  150. real = *pSrc++;
  151. imag = *pSrc++;
  152. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  153. real = *pSrc++;
  154. imag = *pSrc++;
  155. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  156. /* Decrement loop counter */
  157. blkCnt--;
  158. }
  159. /* Loop unrolling: Compute remaining outputs */
  160. blkCnt = numSamples % 0x4U;
  161. #else
  162. /* Initialize blkCnt with number of samples */
  163. blkCnt = numSamples;
  164. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  165. while (blkCnt > 0U)
  166. {
  167. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  168. real = *pSrc++;
  169. imag = *pSrc++;
  170. /* store result in destination buffer. */
  171. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  172. /* Decrement loop counter */
  173. blkCnt--;
  174. }
  175. }
  176. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  177. /**
  178. @} end of cmplx_mag group
  179. */
  180. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */