arm_cmplx_mag_f16.c 6.6 KB

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