arm_cmplx_mag_q15.c 5.5 KB

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