arm_cmplx_mag_squared_q31.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_squared_q31.c
  4. * Description: Q31 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.h"
  29. /**
  30. @ingroup groupCmplxMath
  31. */
  32. /**
  33. @addtogroup cmplx_mag_squared
  34. @{
  35. */
  36. /**
  37. @brief Q31 complex magnitude squared.
  38. @param[in] pSrc points to input vector
  39. @param[out] pDst points to output vector
  40. @param[in] numSamples number of samples in each vector
  41. @par Scaling and Overflow Behavior
  42. The function implements 1.31 by 1.31 multiplications and finally output is converted into 3.29 format.
  43. Input down scaling is not required.
  44. */
  45. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  46. void arm_cmplx_mag_squared_q31(
  47. const q31_t * pSrc,
  48. q31_t * pDst,
  49. uint32_t numSamples)
  50. {
  51. int32_t blockSize = numSamples; /* loop counters */
  52. uint32_t blkCnt; /* loop counters */
  53. q31x4x2_t vecSrc;
  54. q31x4_t vReal, vImag;
  55. q31x4_t vMagSq;
  56. q31_t real, imag; /* Temporary input variables */
  57. q31_t acc0, acc1; /* Accumulators */
  58. /* Compute 4 complex samples at a time */
  59. blkCnt = blockSize >> 2;
  60. while (blkCnt > 0U)
  61. {
  62. vecSrc = vld2q(pSrc);
  63. vReal = vmulhq(vecSrc.val[0], vecSrc.val[0]);
  64. vImag = vmulhq(vecSrc.val[1], vecSrc.val[1]);
  65. vMagSq = vqaddq(vReal, vImag);
  66. vMagSq = vshrq(vMagSq, 1);
  67. vst1q(pDst, vMagSq);
  68. pSrc += 8;
  69. pDst += 4;
  70. /*
  71. * Decrement the blkCnt loop counter
  72. * Advance vector source and destination pointers
  73. */
  74. blkCnt --;
  75. }
  76. /* Tail */
  77. blkCnt = blockSize & 3;
  78. while (blkCnt > 0U)
  79. {
  80. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  81. real = *pSrc++;
  82. imag = *pSrc++;
  83. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  84. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  85. /* store result in 3.29 format in destination buffer. */
  86. *pDst++ = acc0 + acc1;
  87. /* Decrement loop counter */
  88. blkCnt--;
  89. }
  90. }
  91. #else
  92. void arm_cmplx_mag_squared_q31(
  93. const q31_t * pSrc,
  94. q31_t * pDst,
  95. uint32_t numSamples)
  96. {
  97. uint32_t blkCnt; /* Loop counter */
  98. q31_t real, imag; /* Temporary input variables */
  99. q31_t acc0, acc1; /* Accumulators */
  100. #if defined (ARM_MATH_LOOPUNROLL)
  101. /* Loop unrolling: Compute 4 outputs at a time */
  102. blkCnt = numSamples >> 2U;
  103. while (blkCnt > 0U)
  104. {
  105. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  106. real = *pSrc++;
  107. imag = *pSrc++;
  108. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  109. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  110. /* store the result in 3.29 format in the destination buffer. */
  111. *pDst++ = acc0 + acc1;
  112. real = *pSrc++;
  113. imag = *pSrc++;
  114. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  115. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  116. *pDst++ = acc0 + acc1;
  117. real = *pSrc++;
  118. imag = *pSrc++;
  119. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  120. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  121. *pDst++ = acc0 + acc1;
  122. real = *pSrc++;
  123. imag = *pSrc++;
  124. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  125. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  126. *pDst++ = acc0 + acc1;
  127. /* Decrement loop counter */
  128. blkCnt--;
  129. }
  130. /* Loop unrolling: Compute remaining outputs */
  131. blkCnt = numSamples % 0x4U;
  132. #else
  133. /* Initialize blkCnt with number of samples */
  134. blkCnt = numSamples;
  135. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  136. while (blkCnt > 0U)
  137. {
  138. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  139. real = *pSrc++;
  140. imag = *pSrc++;
  141. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  142. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  143. /* store result in 3.29 format in destination buffer. */
  144. *pDst++ = acc0 + acc1;
  145. /* Decrement loop counter */
  146. blkCnt--;
  147. }
  148. }
  149. #endif /* defined(ARM_MATH_MVEI) */
  150. /**
  151. @} end of cmplx_mag_squared group
  152. */