arm_cmplx_mag_q15.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_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. @par Scaling and Overflow Behavior
  42. The function implements 1.15 by 1.15 multiplications and finally output is converted into 2.14 format.
  43. */
  44. /* Sqrt q31 is used otherwise accuracy is not good enough
  45. for small values and for some applications it is
  46. an issue.
  47. */
  48. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. #include "arm_helium_utils.h"
  50. void arm_cmplx_mag_q15(
  51. const q15_t * pSrc,
  52. q15_t * pDst,
  53. uint32_t numSamples)
  54. {
  55. int32_t blockSize = numSamples; /* loop counters */
  56. uint32_t blkCnt; /* loop counters */
  57. q15x8x2_t vecSrc;
  58. q31x4_t prod0;
  59. q31x4_t prod1;
  60. q31_t in;
  61. q31_t acc0;
  62. q31x4_t acc0V;
  63. q31x4_t acc1V;
  64. q31_t res;
  65. q15x8_t resV;
  66. blkCnt = blockSize >> 3;
  67. while (blkCnt > 0U)
  68. {
  69. vecSrc = vld2q(pSrc);
  70. pSrc += 16;
  71. acc0V = vdupq_n_s32(0);
  72. acc1V = vdupq_n_s32(0);
  73. prod0 = vmullbq_int_s16(vecSrc.val[0], vecSrc.val[0]);
  74. acc0V = vqaddq_s32(acc0V,prod0);
  75. prod0 = vmullbq_int_s16(vecSrc.val[1], vecSrc.val[1]);
  76. acc0V = vqaddq_s32(acc0V,prod0);
  77. prod1 = vmulltq_int_s16(vecSrc.val[0], vecSrc.val[0]);
  78. acc1V = vqaddq_s32(acc1V,prod1);
  79. prod1 = vmulltq_int_s16(vecSrc.val[1], vecSrc.val[1]);
  80. acc1V = vqaddq_s32(acc1V,prod1);
  81. acc0V = vshrq(acc0V, 1);
  82. acc1V = vshrq(acc1V, 1);
  83. acc0V = FAST_VSQRT_Q31(acc0V);
  84. acc1V = FAST_VSQRT_Q31(acc1V);
  85. resV = vdupq_n_s16(0);
  86. resV = vqshrnbq_n_s32(resV,acc0V,16);
  87. resV = vqshrntq_n_s32(resV,acc1V,16);
  88. vst1q(pDst, resV);
  89. pDst += 8;
  90. /*
  91. * Decrement the blockSize loop counter
  92. */
  93. blkCnt--;
  94. }
  95. /*
  96. * tail
  97. */
  98. blkCnt = blockSize & 7;
  99. while (blkCnt > 0U)
  100. {
  101. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  102. in = read_q15x2_ia ((q15_t **) &pSrc);
  103. acc0 = __SMUAD(in, in);
  104. /* store result in 2.14 format in destination buffer. */
  105. arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
  106. *pDst++ = res >> 16;
  107. /* Decrement loop counter */
  108. blkCnt--;
  109. }
  110. }
  111. #else
  112. void arm_cmplx_mag_q15(
  113. const q15_t * pSrc,
  114. q15_t * pDst,
  115. uint32_t numSamples)
  116. {
  117. q31_t res; /* temporary result */
  118. uint32_t blkCnt; /* Loop counter */
  119. #if defined (ARM_MATH_DSP)
  120. q31_t in;
  121. q31_t acc0; /* Accumulators */
  122. #else
  123. q15_t real, imag; /* Temporary input variables */
  124. q31_t acc0, acc1; /* Accumulators */
  125. #endif
  126. #if defined (ARM_MATH_LOOPUNROLL)
  127. /* Loop unrolling: Compute 4 outputs at a time */
  128. blkCnt = numSamples >> 2U;
  129. while (blkCnt > 0U)
  130. {
  131. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  132. #if defined (ARM_MATH_DSP)
  133. in = read_q15x2_ia (&pSrc);
  134. acc0 = __SMUAD(in, in);
  135. /* store result in 2.14 format in destination buffer. */
  136. arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
  137. *pDst++ = res >> 16;
  138. in = read_q15x2_ia (&pSrc);
  139. acc0 = __SMUAD(in, in);
  140. arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
  141. *pDst++ = res >> 16;
  142. in = read_q15x2_ia (&pSrc);
  143. acc0 = __SMUAD(in, in);
  144. arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
  145. *pDst++ = res >> 16;
  146. in = read_q15x2_ia (&pSrc);
  147. acc0 = __SMUAD(in, in);
  148. arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
  149. *pDst++ = res >> 16;
  150. #else
  151. real = *pSrc++;
  152. imag = *pSrc++;
  153. acc0 = ((q31_t) real * real);
  154. acc1 = ((q31_t) imag * imag);
  155. /* store result in 2.14 format in destination buffer. */
  156. arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
  157. *pDst++ = res >> 16;
  158. real = *pSrc++;
  159. imag = *pSrc++;
  160. acc0 = ((q31_t) real * real);
  161. acc1 = ((q31_t) imag * imag);
  162. arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
  163. *pDst++ = res >> 16;
  164. real = *pSrc++;
  165. imag = *pSrc++;
  166. acc0 = ((q31_t) real * real);
  167. acc1 = ((q31_t) imag * imag);
  168. arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
  169. *pDst++ = res >> 16;
  170. real = *pSrc++;
  171. imag = *pSrc++;
  172. acc0 = ((q31_t) real * real);
  173. acc1 = ((q31_t) imag * imag);
  174. arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
  175. *pDst++ = res >> 16;
  176. #endif /* #if defined (ARM_MATH_DSP) */
  177. /* Decrement loop counter */
  178. blkCnt--;
  179. }
  180. /* Loop unrolling: Compute remaining outputs */
  181. blkCnt = numSamples % 0x4U;
  182. #else
  183. /* Initialize blkCnt with number of samples */
  184. blkCnt = numSamples;
  185. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  186. while (blkCnt > 0U)
  187. {
  188. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  189. #if defined (ARM_MATH_DSP)
  190. in = read_q15x2_ia (&pSrc);
  191. acc0 = __SMUAD(in, in);
  192. /* store result in 2.14 format in destination buffer. */
  193. arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
  194. *pDst++ = res >> 16;
  195. #else
  196. real = *pSrc++;
  197. imag = *pSrc++;
  198. acc0 = ((q31_t) real * real);
  199. acc1 = ((q31_t) imag * imag);
  200. /* store result in 2.14 format in destination buffer. */
  201. arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
  202. *pDst++ = res >> 16;
  203. #endif
  204. /* Decrement loop counter */
  205. blkCnt--;
  206. }
  207. }
  208. #endif /* defined(ARM_MATH_MVEI) */
  209. /**
  210. @} end of cmplx_mag group
  211. */