arm_cmplx_mag_fast_q15.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_fast_q15.c
  4. * Description: Q15 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 Q15 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.15 by 1.15 multiplications and finally output is converted into 2.14 format.
  44. Fast functions are less accurate. This function will tend to clamp to 0
  45. the too small values. So sqrt(x*x) = x will not always be true.
  46. */
  47. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  48. #include "arm_helium_utils.h"
  49. void arm_cmplx_mag_fast_q15(
  50. const q15_t * pSrc,
  51. q15_t * pDst,
  52. uint32_t numSamples)
  53. {
  54. int32_t blockSize = numSamples; /* loop counters */
  55. uint32_t blkCnt; /* loop counters */
  56. q15x8x2_t vecSrc;
  57. q15x8_t sum;
  58. q31_t in;
  59. q31_t acc0;
  60. blkCnt = blockSize >> 3;
  61. while (blkCnt > 0U)
  62. {
  63. vecSrc = vld2q(pSrc);
  64. pSrc += 16;
  65. sum = vqaddq(vmulhq(vecSrc.val[0], vecSrc.val[0]),
  66. vmulhq(vecSrc.val[1], vecSrc.val[1]));
  67. sum = vshrq(sum, 1);
  68. sum = FAST_VSQRT_Q15(sum);
  69. vst1q(pDst, sum);
  70. pDst += 8;
  71. /*
  72. * Decrement the blockSize loop counter
  73. */
  74. blkCnt--;
  75. }
  76. /*
  77. * tail
  78. */
  79. blkCnt = blockSize & 7;
  80. while (blkCnt > 0U)
  81. {
  82. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  83. in = read_q15x2_ia ((q15_t **) &pSrc);
  84. acc0 = __SMUAD(in, in);
  85. /* store result in 2.14 format in destination buffer. */
  86. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  87. /* Decrement loop counter */
  88. blkCnt--;
  89. }
  90. }
  91. #else
  92. void arm_cmplx_mag_fast_q15(
  93. const q15_t * pSrc,
  94. q15_t * pDst,
  95. uint32_t numSamples)
  96. {
  97. uint32_t blkCnt; /* Loop counter */
  98. #if defined (ARM_MATH_DSP)
  99. q31_t in;
  100. q31_t acc0; /* Accumulators */
  101. #else
  102. q15_t real, imag; /* Temporary input variables */
  103. q31_t acc0, acc1; /* Accumulators */
  104. #endif
  105. #if defined (ARM_MATH_LOOPUNROLL)
  106. /* Loop unrolling: Compute 4 outputs at a time */
  107. blkCnt = numSamples >> 2U;
  108. while (blkCnt > 0U)
  109. {
  110. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  111. #if defined (ARM_MATH_DSP)
  112. in = read_q15x2_ia ((q15_t **) &pSrc);
  113. acc0 = __SMUAD(in, in);
  114. /* store result in 2.14 format in destination buffer. */
  115. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  116. in = read_q15x2_ia ((q15_t **) &pSrc);
  117. acc0 = __SMUAD(in, in);
  118. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  119. in = read_q15x2_ia ((q15_t **) &pSrc);
  120. acc0 = __SMUAD(in, in);
  121. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  122. in = read_q15x2_ia ((q15_t **) &pSrc);
  123. acc0 = __SMUAD(in, in);
  124. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  125. #else
  126. real = *pSrc++;
  127. imag = *pSrc++;
  128. acc0 = ((q31_t) real * real);
  129. acc1 = ((q31_t) imag * imag);
  130. /* store result in 2.14 format in destination buffer. */
  131. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  132. real = *pSrc++;
  133. imag = *pSrc++;
  134. acc0 = ((q31_t) real * real);
  135. acc1 = ((q31_t) imag * imag);
  136. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  137. real = *pSrc++;
  138. imag = *pSrc++;
  139. acc0 = ((q31_t) real * real);
  140. acc1 = ((q31_t) imag * imag);
  141. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  142. real = *pSrc++;
  143. imag = *pSrc++;
  144. acc0 = ((q31_t) real * real);
  145. acc1 = ((q31_t) imag * imag);
  146. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  147. #endif /* #if defined (ARM_MATH_DSP) */
  148. /* Decrement loop counter */
  149. blkCnt--;
  150. }
  151. /* Loop unrolling: Compute remaining outputs */
  152. blkCnt = numSamples % 0x4U;
  153. #else
  154. /* Initialize blkCnt with number of samples */
  155. blkCnt = numSamples;
  156. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  157. while (blkCnt > 0U)
  158. {
  159. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  160. #if defined (ARM_MATH_DSP)
  161. in = read_q15x2_ia ((q15_t **) &pSrc);
  162. acc0 = __SMUAD(in, in);
  163. /* store result in 2.14 format in destination buffer. */
  164. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  165. #else
  166. real = *pSrc++;
  167. imag = *pSrc++;
  168. acc0 = ((q31_t) real * real);
  169. acc1 = ((q31_t) imag * imag);
  170. /* store result in 2.14 format in destination buffer. */
  171. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  172. #endif
  173. /* Decrement loop counter */
  174. blkCnt--;
  175. }
  176. }
  177. #endif /* defined(ARM_MATH_MVEI) */
  178. /**
  179. @} end of cmplx_mag group
  180. */