arm_cmplx_mag_squared_q31.c 5.0 KB

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