arm_cmplx_mag_q31.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. @return none
  42. @par Scaling and Overflow Behavior
  43. The function implements 1.31 by 1.31 multiplications and finally output is converted into 2.30 format.
  44. Input down scaling is not required.
  45. */
  46. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. #include "arm_helium_utils.h"
  48. void arm_cmplx_mag_q31(
  49. const q31_t * pSrc,
  50. q31_t * pDst,
  51. uint32_t numSamples)
  52. {
  53. int32_t blockSize = numSamples; /* loop counters */
  54. uint32_t blkCnt; /* loop counters */
  55. q31x4x2_t vecSrc;
  56. q31x4_t sum;
  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. sum = vqaddq(vmulhq(vecSrc.val[0], vecSrc.val[0]),
  65. vmulhq(vecSrc.val[1], vecSrc.val[1]));
  66. sum = vshrq(sum, 1);
  67. /*
  68. This function is using a table. There are compilations flags to avoid
  69. including this table (and in this case, arm_cmplx_maq_q31 must not
  70. be built and linked.)
  71. */
  72. sum = FAST_VSQRT_Q31(sum);
  73. vst1q(pDst, sum);
  74. /*
  75. * Decrement the blockSize loop counter
  76. */
  77. blkCnt--;
  78. pSrc += 8;
  79. pDst += 4;
  80. }
  81. /*
  82. * tail
  83. */
  84. blkCnt = blockSize & 3;
  85. while (blkCnt > 0U)
  86. {
  87. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  88. real = *pSrc++;
  89. imag = *pSrc++;
  90. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  91. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  92. /* store result in 2.30 format in destination buffer. */
  93. arm_sqrt_q31(acc0 + acc1, pDst++);
  94. /* Decrement loop counter */
  95. blkCnt--;
  96. }
  97. }
  98. #else
  99. void arm_cmplx_mag_q31(
  100. const q31_t * pSrc,
  101. q31_t * pDst,
  102. uint32_t numSamples)
  103. {
  104. uint32_t blkCnt; /* Loop counter */
  105. q31_t real, imag; /* Temporary input variables */
  106. q31_t acc0, acc1; /* Accumulators */
  107. #if defined (ARM_MATH_LOOPUNROLL)
  108. /* Loop unrolling: Compute 4 outputs at a time */
  109. blkCnt = numSamples >> 2U;
  110. while (blkCnt > 0U)
  111. {
  112. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  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. /* store result in 2.30 format in destination buffer. */
  118. arm_sqrt_q31(acc0 + acc1, pDst++);
  119. real = *pSrc++;
  120. imag = *pSrc++;
  121. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  122. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  123. arm_sqrt_q31(acc0 + acc1, pDst++);
  124. real = *pSrc++;
  125. imag = *pSrc++;
  126. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  127. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  128. arm_sqrt_q31(acc0 + acc1, pDst++);
  129. real = *pSrc++;
  130. imag = *pSrc++;
  131. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  132. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  133. arm_sqrt_q31(acc0 + acc1, pDst++);
  134. /* Decrement loop counter */
  135. blkCnt--;
  136. }
  137. /* Loop unrolling: Compute remaining outputs */
  138. blkCnt = numSamples % 0x4U;
  139. #else
  140. /* Initialize blkCnt with number of samples */
  141. blkCnt = numSamples;
  142. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  143. while (blkCnt > 0U)
  144. {
  145. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  146. real = *pSrc++;
  147. imag = *pSrc++;
  148. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  149. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  150. /* store result in 2.30 format in destination buffer. */
  151. arm_sqrt_q31(acc0 + acc1, pDst++);
  152. /* Decrement loop counter */
  153. blkCnt--;
  154. }
  155. }
  156. #endif /* defined(ARM_MATH_MVEI) */
  157. /**
  158. @} end of cmplx_mag group
  159. */