arm_cmplx_mag_q15.c 6.6 KB

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