arm_cmplx_mult_cmplx_q15.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_cmplx_q15.c
  4. * Description: Q15 complex-by-complex multiplication
  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 CmplxByCmplxMult
  34. @{
  35. */
  36. /**
  37. @brief Q15 complex-by-complex multiplication.
  38. @param[in] pSrcA points to first input vector
  39. @param[in] pSrcB points to second input vector
  40. @param[out] pDst points to output vector
  41. @param[in] numSamples number of samples in each vector
  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_mult_cmplx_q15(
  47. const q15_t * pSrcA,
  48. const q15_t * pSrcB,
  49. q15_t * pDst,
  50. uint32_t numSamples)
  51. {
  52. int32_t blkCnt;
  53. q15x8_t vecSrcA, vecSrcB;
  54. q15x8_t vecSrcC, vecSrcD;
  55. q15x8_t vecDst;
  56. blkCnt = (numSamples >> 3);
  57. blkCnt -= 1;
  58. if (blkCnt > 0)
  59. {
  60. /* should give more freedom to generate stall free code */
  61. vecSrcA = vld1q(pSrcA);
  62. vecSrcB = vld1q(pSrcB);
  63. pSrcA += 8;
  64. pSrcB += 8;
  65. while (blkCnt > 0)
  66. {
  67. /* C[2 * i] = A[2 * i] * B[2 * i] - A[2 * i + 1] * B[2 * i + 1]. */
  68. vecDst = vqdmlsdhq(vuninitializedq_s16(), vecSrcA, vecSrcB);
  69. vecSrcC = vld1q(pSrcA);
  70. pSrcA += 8;
  71. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i]. */
  72. vecDst = vqdmladhxq(vecDst, vecSrcA, vecSrcB);
  73. vecSrcD = vld1q(pSrcB);
  74. pSrcB += 8;
  75. vstrhq_s16(pDst, vshrq(vecDst, 2));
  76. pDst += 8;
  77. vecDst = vqdmlsdhq(vuninitializedq_s16(), vecSrcC, vecSrcD);
  78. vecSrcA = vld1q(pSrcA);
  79. pSrcA += 8;
  80. vecDst = vqdmladhxq(vecDst, vecSrcC, vecSrcD);
  81. vecSrcB = vld1q(pSrcB);
  82. pSrcB += 8;
  83. vstrhq_s16(pDst, vshrq(vecDst, 2));
  84. pDst += 8;
  85. /*
  86. * Decrement the blockSize loop counter
  87. */
  88. blkCnt--;
  89. }
  90. /* process last elements out of the loop avoid the armclang breaking the SW pipeline */
  91. vecDst = vqdmlsdhq(vuninitializedq_s16(), vecSrcA, vecSrcB);
  92. vecSrcC = vld1q(pSrcA);
  93. vecDst = vqdmladhxq(vecDst, vecSrcA, vecSrcB);
  94. vecSrcD = vld1q(pSrcB);
  95. vstrhq_s16(pDst, vshrq(vecDst, 2));
  96. pDst += 8;
  97. vecDst = vqdmlsdhq(vuninitializedq_s16(), vecSrcC, vecSrcD);
  98. vecDst = vqdmladhxq(vecDst, vecSrcC, vecSrcD);
  99. vstrhq_s16(pDst, vshrq(vecDst, 2));
  100. pDst += 8;
  101. /*
  102. * tail
  103. */
  104. blkCnt = CMPLX_DIM * (numSamples & 7);
  105. do
  106. {
  107. mve_pred16_t p = vctp16q(blkCnt);
  108. pSrcA += 8;
  109. pSrcB += 8;
  110. vecSrcA = vldrhq_z_s16(pSrcA, p);
  111. vecSrcB = vldrhq_z_s16(pSrcB, p);
  112. vecDst = vqdmlsdhq_m(vuninitializedq_s16(), vecSrcA, vecSrcB, p);
  113. vecDst = vqdmladhxq_m(vecDst, vecSrcA, vecSrcB, p);
  114. vecDst = vshrq_m(vuninitializedq_s16(), vecDst, 2, p);
  115. vstrhq_p_s16(pDst, vecDst, p);
  116. pDst += 8;
  117. blkCnt -= 8;
  118. }
  119. while ((int32_t) blkCnt > 0);
  120. }
  121. else
  122. {
  123. blkCnt = numSamples * CMPLX_DIM;
  124. while (blkCnt > 0) {
  125. mve_pred16_t p = vctp16q(blkCnt);
  126. vecSrcA = vldrhq_z_s16(pSrcA, p);
  127. vecSrcB = vldrhq_z_s16(pSrcB, p);
  128. vecDst = vqdmlsdhq_m(vuninitializedq_s16(), vecSrcA, vecSrcB, p);
  129. vecDst = vqdmladhxq_m(vecDst, vecSrcA, vecSrcB, p);
  130. vecDst = vshrq_m(vuninitializedq_s16(), vecDst, 2, p);
  131. vstrhq_p_s16(pDst, vecDst, p);
  132. pDst += 8;
  133. pSrcA += 8;
  134. pSrcB += 8;
  135. blkCnt -= 8;
  136. }
  137. }
  138. }
  139. #else
  140. void arm_cmplx_mult_cmplx_q15(
  141. const q15_t * pSrcA,
  142. const q15_t * pSrcB,
  143. q15_t * pDst,
  144. uint32_t numSamples)
  145. {
  146. uint32_t blkCnt; /* Loop counter */
  147. q15_t a, b, c, d; /* Temporary variables */
  148. #if defined (ARM_MATH_LOOPUNROLL)
  149. /* Loop unrolling: Compute 4 outputs at a time */
  150. blkCnt = numSamples >> 2U;
  151. while (blkCnt > 0U)
  152. {
  153. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  154. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  155. a = *pSrcA++;
  156. b = *pSrcA++;
  157. c = *pSrcB++;
  158. d = *pSrcB++;
  159. /* store result in 3.13 format in destination buffer. */
  160. *pDst++ = (q15_t) ( (((q31_t) a * c) >> 17) - (((q31_t) b * d) >> 17) );
  161. *pDst++ = (q15_t) ( (((q31_t) a * d) >> 17) + (((q31_t) b * c) >> 17) );
  162. a = *pSrcA++;
  163. b = *pSrcA++;
  164. c = *pSrcB++;
  165. d = *pSrcB++;
  166. *pDst++ = (q15_t) ( (((q31_t) a * c) >> 17) - (((q31_t) b * d) >> 17) );
  167. *pDst++ = (q15_t) ( (((q31_t) a * d) >> 17) + (((q31_t) b * c) >> 17) );
  168. a = *pSrcA++;
  169. b = *pSrcA++;
  170. c = *pSrcB++;
  171. d = *pSrcB++;
  172. *pDst++ = (q15_t) ( (((q31_t) a * c) >> 17) - (((q31_t) b * d) >> 17) );
  173. *pDst++ = (q15_t) ( (((q31_t) a * d) >> 17) + (((q31_t) b * c) >> 17) );
  174. a = *pSrcA++;
  175. b = *pSrcA++;
  176. c = *pSrcB++;
  177. d = *pSrcB++;
  178. *pDst++ = (q15_t) ( (((q31_t) a * c) >> 17) - (((q31_t) b * d) >> 17) );
  179. *pDst++ = (q15_t) ( (((q31_t) a * d) >> 17) + (((q31_t) b * c) >> 17) );
  180. /* Decrement loop counter */
  181. blkCnt--;
  182. }
  183. /* Loop unrolling: Compute remaining outputs */
  184. blkCnt = numSamples % 0x4U;
  185. #else
  186. /* Initialize blkCnt with number of samples */
  187. blkCnt = numSamples;
  188. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  189. while (blkCnt > 0U)
  190. {
  191. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  192. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  193. a = *pSrcA++;
  194. b = *pSrcA++;
  195. c = *pSrcB++;
  196. d = *pSrcB++;
  197. /* store result in 3.13 format in destination buffer. */
  198. *pDst++ = (q15_t) ( (((q31_t) a * c) >> 17) - (((q31_t) b * d) >> 17) );
  199. *pDst++ = (q15_t) ( (((q31_t) a * d) >> 17) + (((q31_t) b * c) >> 17) );
  200. /* Decrement loop counter */
  201. blkCnt--;
  202. }
  203. }
  204. #endif /* defined(ARM_MATH_MVEI) */
  205. /**
  206. @} end of CmplxByCmplxMult group
  207. */