arm_cmplx_mag_f16.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_f16.c
  4. * Description: Floating-point complex magnitude
  5. *
  6. *
  7. * Target Processor: Cortex-M cores
  8. * -------------------------------------------------------------------- */
  9. /*
  10. * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #include "dsp/complex_math_functions_f16.h"
  27. #if defined(ARM_FLOAT16_SUPPORTED)
  28. /**
  29. @ingroup groupCmplxMath
  30. */
  31. /**
  32. @defgroup cmplx_mag Complex Magnitude
  33. Computes the magnitude of the elements of a complex data vector.
  34. The <code>pSrc</code> points to the source data and
  35. <code>pDst</code> points to the where the result should be written.
  36. <code>numSamples</code> specifies the number of complex samples
  37. in the input array and the data is stored in an interleaved fashion
  38. (real, imag, real, imag, ...).
  39. The input array has a total of <code>2*numSamples</code> values;
  40. the output array has a total of <code>numSamples</code> values.
  41. The underlying algorithm is used:
  42. <pre>
  43. for (n = 0; n < numSamples; n++) {
  44. pDst[n] = sqrt(pSrc[(2*n)+0]^2 + pSrc[(2*n)+1]^2);
  45. }
  46. </pre>
  47. There are separate functions for floating-point, Q15, and Q31 data types.
  48. */
  49. /**
  50. @addtogroup cmplx_mag
  51. @{
  52. */
  53. /**
  54. @brief Floating-point complex magnitude.
  55. @param[in] pSrc points to input vector
  56. @param[out] pDst points to output vector
  57. @param[in] numSamples number of samples in each vector
  58. @return none
  59. */
  60. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  61. #include "arm_helium_utils.h"
  62. void arm_cmplx_mag_f16(
  63. const float16_t * pSrc,
  64. float16_t * pDst,
  65. uint32_t numSamples)
  66. {
  67. int32_t blockSize = numSamples; /* loop counters */
  68. uint32_t blkCnt; /* loop counters */
  69. f16x8x2_t vecSrc;
  70. f16x8_t sum;
  71. /* Compute 4 complex samples at a time */
  72. blkCnt = blockSize >> 3;
  73. while (blkCnt > 0U)
  74. {
  75. q15x8_t newtonStartVec;
  76. f16x8_t sumHalf, invSqrt;
  77. vecSrc = vld2q(pSrc);
  78. pSrc += 16;
  79. sum = vmulq(vecSrc.val[0], vecSrc.val[0]);
  80. sum = vfmaq(sum, vecSrc.val[1], vecSrc.val[1]);
  81. /*
  82. * inlined Fast SQRT using inverse SQRT newton-raphson method
  83. */
  84. /* compute initial value */
  85. newtonStartVec = vdupq_n_s16(INVSQRT_MAGIC_F16) - vshrq((q15x8_t) sum, 1);
  86. sumHalf = sum * 0.5f;
  87. /*
  88. * compute 3 x iterations
  89. *
  90. * The more iterations, the more accuracy.
  91. * If you need to trade a bit of accuracy for more performance,
  92. * you can comment out the 3rd use of the macro.
  93. */
  94. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, (f16x8_t) newtonStartVec);
  95. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, invSqrt);
  96. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, invSqrt);
  97. /*
  98. * set negative values to 0
  99. */
  100. invSqrt = vdupq_m(invSqrt, (float16_t)0.0f, vcmpltq(invSqrt, (float16_t)0.0f));
  101. /*
  102. * sqrt(x) = x * invSqrt(x)
  103. */
  104. sum = vmulq(sum, invSqrt);
  105. vstrhq_f16(pDst, sum);
  106. pDst += 8;
  107. /*
  108. * Decrement the blockSize loop counter
  109. */
  110. blkCnt--;
  111. }
  112. /*
  113. * tail
  114. */
  115. blkCnt = blockSize & 7;
  116. if (blkCnt > 0U)
  117. {
  118. mve_pred16_t p0 = vctp16q(blkCnt);
  119. q15x8_t newtonStartVec;
  120. f16x8_t sumHalf, invSqrt;
  121. vecSrc = vld2q((float16_t const *)pSrc);
  122. sum = vmulq(vecSrc.val[0], vecSrc.val[0]);
  123. sum = vfmaq(sum, vecSrc.val[1], vecSrc.val[1]);
  124. /*
  125. * inlined Fast SQRT using inverse SQRT newton-raphson method
  126. */
  127. /* compute initial value */
  128. newtonStartVec = vdupq_n_s16(INVSQRT_MAGIC_F16) - vshrq((q15x8_t) sum, 1);
  129. sumHalf = vmulq(sum, (float16_t)0.5);
  130. /*
  131. * compute 2 x iterations
  132. */
  133. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, (f16x8_t) newtonStartVec);
  134. INVSQRT_NEWTON_MVE_F16(invSqrt, sumHalf, invSqrt);
  135. /*
  136. * set negative values to 0
  137. */
  138. invSqrt = vdupq_m(invSqrt, (float16_t)0.0, vcmpltq(invSqrt, (float16_t)0.0));
  139. /*
  140. * sqrt(x) = x * invSqrt(x)
  141. */
  142. sum = vmulq(sum, invSqrt);
  143. vstrhq_p_f16(pDst, sum, p0);
  144. }
  145. }
  146. #else
  147. void arm_cmplx_mag_f16(
  148. const float16_t * pSrc,
  149. float16_t * pDst,
  150. uint32_t numSamples)
  151. {
  152. uint32_t blkCnt; /* loop counter */
  153. _Float16 real, imag; /* Temporary variables to hold input values */
  154. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  155. /* Loop unrolling: Compute 4 outputs at a time */
  156. blkCnt = numSamples >> 2U;
  157. while (blkCnt > 0U)
  158. {
  159. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  160. real = *pSrc++;
  161. imag = *pSrc++;
  162. /* store result in destination buffer. */
  163. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  164. real = *pSrc++;
  165. imag = *pSrc++;
  166. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  167. real = *pSrc++;
  168. imag = *pSrc++;
  169. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  170. real = *pSrc++;
  171. imag = *pSrc++;
  172. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  173. /* Decrement loop counter */
  174. blkCnt--;
  175. }
  176. /* Loop unrolling: Compute remaining outputs */
  177. blkCnt = numSamples % 0x4U;
  178. #else
  179. /* Initialize blkCnt with number of samples */
  180. blkCnt = numSamples;
  181. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  182. while (blkCnt > 0U)
  183. {
  184. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  185. real = *pSrc++;
  186. imag = *pSrc++;
  187. /* store result in destination buffer. */
  188. arm_sqrt_f16((real * real) + (imag * imag), pDst++);
  189. /* Decrement loop counter */
  190. blkCnt--;
  191. }
  192. }
  193. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  194. /**
  195. @} end of cmplx_mag group
  196. */
  197. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */