arm_cmplx_mag_q31.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_q31.c
  4. * Description: Q31 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.h"
  29. /**
  30. @ingroup groupCmplxMath
  31. */
  32. /**
  33. @addtogroup cmplx_mag
  34. @{
  35. */
  36. /**
  37. @brief Q31 complex magnitude.
  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 2.30 format.
  43. Input down scaling is not required.
  44. */
  45. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  46. #include "arm_helium_utils.h"
  47. void arm_cmplx_mag_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 sum;
  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. sum = vqaddq(vmulhq(vecSrc.val[0], vecSrc.val[0]),
  64. vmulhq(vecSrc.val[1], vecSrc.val[1]));
  65. sum = vshrq(sum, 1);
  66. /*
  67. This function is using a table. There are compilations flags to avoid
  68. including this table (and in this case, arm_cmplx_maq_q31 must not
  69. be built and linked.)
  70. */
  71. sum = FAST_VSQRT_Q31(sum);
  72. vst1q(pDst, sum);
  73. /*
  74. * Decrement the blockSize loop counter
  75. */
  76. blkCnt--;
  77. pSrc += 8;
  78. pDst += 4;
  79. }
  80. /*
  81. * tail
  82. */
  83. blkCnt = blockSize & 3;
  84. while (blkCnt > 0U)
  85. {
  86. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  87. real = *pSrc++;
  88. imag = *pSrc++;
  89. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  90. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  91. /* store result in 2.30 format in destination buffer. */
  92. arm_sqrt_q31(acc0 + acc1, pDst++);
  93. /* Decrement loop counter */
  94. blkCnt--;
  95. }
  96. }
  97. #else
  98. void arm_cmplx_mag_q31(
  99. const q31_t * pSrc,
  100. q31_t * pDst,
  101. uint32_t numSamples)
  102. {
  103. uint32_t blkCnt; /* Loop counter */
  104. q31_t real, imag; /* Temporary input variables */
  105. q31_t acc0, acc1; /* Accumulators */
  106. #if defined (ARM_MATH_LOOPUNROLL)
  107. /* Loop unrolling: Compute 4 outputs at a time */
  108. blkCnt = numSamples >> 2U;
  109. while (blkCnt > 0U)
  110. {
  111. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  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. /* store result in 2.30 format in destination buffer. */
  117. arm_sqrt_q31(acc0 + acc1, pDst++);
  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. arm_sqrt_q31(acc0 + acc1, pDst++);
  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. arm_sqrt_q31(acc0 + acc1, pDst++);
  128. real = *pSrc++;
  129. imag = *pSrc++;
  130. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  131. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  132. arm_sqrt_q31(acc0 + acc1, pDst++);
  133. /* Decrement loop counter */
  134. blkCnt--;
  135. }
  136. /* Loop unrolling: Compute remaining outputs */
  137. blkCnt = numSamples % 0x4U;
  138. #else
  139. /* Initialize blkCnt with number of samples */
  140. blkCnt = numSamples;
  141. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  142. while (blkCnt > 0U)
  143. {
  144. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  145. real = *pSrc++;
  146. imag = *pSrc++;
  147. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  148. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  149. /* store result in 2.30 format in destination buffer. */
  150. arm_sqrt_q31(acc0 + acc1, pDst++);
  151. /* Decrement loop counter */
  152. blkCnt--;
  153. }
  154. }
  155. #endif /* defined(ARM_MATH_MVEI) */
  156. /**
  157. @} end of cmplx_mag group
  158. */