arm_cmplx_mag_squared_f16.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_squared_f16.c
  4. * Description: Floating-point complex magnitude squared
  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_squared Complex Magnitude Squared
  35. Computes the magnitude squared 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] = 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_squared
  53. @{
  54. */
  55. /**
  56. @brief Floating-point complex magnitude squared.
  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. void arm_cmplx_mag_squared_f16(
  64. const float16_t * pSrc,
  65. float16_t * pDst,
  66. uint32_t numSamples)
  67. {
  68. int32_t blockSize = numSamples; /* loop counters */
  69. f16x8x2_t vecSrc;
  70. f16x8_t sum;
  71. /* Compute 4 complex samples at a time */
  72. while (blockSize > 0)
  73. {
  74. mve_pred16_t p = vctp16q(blockSize);
  75. vecSrc = vld2q(pSrc);
  76. sum = vmulq_m(vuninitializedq_f16(),vecSrc.val[0], vecSrc.val[0],p);
  77. sum = vfmaq_m(sum, vecSrc.val[1], vecSrc.val[1],p);
  78. vstrhq_p_f16(pDst, sum,p);
  79. pSrc += 16;
  80. pDst += 8;
  81. /*
  82. * Decrement the blockSize loop counter
  83. */
  84. blockSize-= 8;
  85. }
  86. }
  87. #else
  88. void arm_cmplx_mag_squared_f16(
  89. const float16_t * pSrc,
  90. float16_t * pDst,
  91. uint32_t numSamples)
  92. {
  93. uint32_t blkCnt; /* Loop counter */
  94. _Float16 real, imag; /* Temporary input variables */
  95. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  96. /* Loop unrolling: Compute 4 outputs at a time */
  97. blkCnt = numSamples >> 2U;
  98. while (blkCnt > 0U)
  99. {
  100. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  101. real = *pSrc++;
  102. imag = *pSrc++;
  103. *pDst++ = (real * real) + (imag * imag);
  104. real = *pSrc++;
  105. imag = *pSrc++;
  106. *pDst++ = (real * real) + (imag * imag);
  107. real = *pSrc++;
  108. imag = *pSrc++;
  109. *pDst++ = (real * real) + (imag * imag);
  110. real = *pSrc++;
  111. imag = *pSrc++;
  112. *pDst++ = (real * real) + (imag * imag);
  113. /* Decrement loop counter */
  114. blkCnt--;
  115. }
  116. /* Loop unrolling: Compute remaining outputs */
  117. blkCnt = numSamples % 0x4U;
  118. #else
  119. /* Initialize blkCnt with number of samples */
  120. blkCnt = numSamples;
  121. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  122. while (blkCnt > 0U)
  123. {
  124. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  125. real = *pSrc++;
  126. imag = *pSrc++;
  127. /* store result in destination buffer. */
  128. *pDst++ = (real * real) + (imag * imag);
  129. /* Decrement loop counter */
  130. blkCnt--;
  131. }
  132. }
  133. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  134. /**
  135. @} end of cmplx_mag_squared group
  136. */
  137. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */