arm_cmplx_mag_squared_q15.c 5.5 KB

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