arm_cmplx_mult_cmplx_f32.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_cmplx_f32.c
  4. * Description: Floating-point 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. @defgroup CmplxByCmplxMult Complex-by-Complex Multiplication
  34. Multiplies a complex vector by another complex vector and generates a complex result.
  35. The data in the complex arrays is stored in an interleaved fashion
  36. (real, imag, real, imag, ...).
  37. The parameter <code>numSamples</code> represents the number of complex
  38. samples processed. The complex arrays have a total of <code>2*numSamples</code>
  39. real values.
  40. The underlying algorithm is used:
  41. <pre>
  42. for (n = 0; n < numSamples; n++) {
  43. pDst[(2*n)+0] = pSrcA[(2*n)+0] * pSrcB[(2*n)+0] - pSrcA[(2*n)+1] * pSrcB[(2*n)+1];
  44. pDst[(2*n)+1] = pSrcA[(2*n)+0] * pSrcB[(2*n)+1] + pSrcA[(2*n)+1] * pSrcB[(2*n)+0];
  45. }
  46. </pre>
  47. There are separate functions for floating-point, Q15, and Q31 data types.
  48. */
  49. /**
  50. @addtogroup CmplxByCmplxMult
  51. @{
  52. */
  53. /**
  54. @brief Floating-point complex-by-complex multiplication.
  55. @param[in] pSrcA points to first input vector
  56. @param[in] pSrcB points to second input vector
  57. @param[out] pDst points to output vector
  58. @param[in] numSamples number of samples in each vector
  59. */
  60. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  61. void arm_cmplx_mult_cmplx_f32(
  62. const float32_t * pSrcA,
  63. const float32_t * pSrcB,
  64. float32_t * pDst,
  65. uint32_t numSamples)
  66. {
  67. int32_t blkCnt;
  68. f32x4_t vecSrcA, vecSrcB;
  69. f32x4_t vecSrcC, vecSrcD;
  70. f32x4_t vec_acc;
  71. blkCnt = numSamples >> 2;
  72. blkCnt -= 1;
  73. if (blkCnt > 0) {
  74. /* should give more freedom to generate stall free code */
  75. vecSrcA = vld1q(pSrcA);
  76. vecSrcB = vld1q(pSrcB);
  77. pSrcA += 4;
  78. pSrcB += 4;
  79. while (blkCnt > 0) {
  80. vec_acc = vcmulq(vecSrcA, vecSrcB);
  81. vecSrcC = vld1q(pSrcA);
  82. pSrcA += 4;
  83. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  84. vecSrcD = vld1q(pSrcB);
  85. pSrcB += 4;
  86. vst1q(pDst, vec_acc);
  87. pDst += 4;
  88. vec_acc = vcmulq(vecSrcC, vecSrcD);
  89. vecSrcA = vld1q(pSrcA);
  90. pSrcA += 4;
  91. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  92. vecSrcB = vld1q(pSrcB);
  93. pSrcB += 4;
  94. vst1q(pDst, vec_acc);
  95. pDst += 4;
  96. /*
  97. * Decrement the blockSize loop counter
  98. */
  99. blkCnt--;
  100. }
  101. /* process last elements out of the loop avoid the armclang breaking the SW pipeline */
  102. vec_acc = vcmulq(vecSrcA, vecSrcB);
  103. vecSrcC = vld1q(pSrcA);
  104. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  105. vecSrcD = vld1q(pSrcB);
  106. vst1q(pDst, vec_acc);
  107. pDst += 4;
  108. vec_acc = vcmulq(vecSrcC, vecSrcD);
  109. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  110. vst1q(pDst, vec_acc);
  111. pDst += 4;
  112. /*
  113. * tail
  114. */
  115. blkCnt = CMPLX_DIM * (numSamples & 3);
  116. while (blkCnt > 0) {
  117. mve_pred16_t p = vctp32q(blkCnt);
  118. pSrcA += 4;
  119. pSrcB += 4;
  120. vecSrcA = vldrwq_z_f32(pSrcA, p);
  121. vecSrcB = vldrwq_z_f32(pSrcB, p);
  122. vec_acc = vcmulq_m(vuninitializedq_f32(),vecSrcA, vecSrcB, p);
  123. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  124. vstrwq_p_f32(pDst, vec_acc, p);
  125. pDst += 4;
  126. blkCnt -= 4;
  127. }
  128. } else {
  129. /* small vector */
  130. blkCnt = numSamples * CMPLX_DIM;
  131. vec_acc = vdupq_n_f32(0.0f);
  132. do {
  133. mve_pred16_t p = vctp32q(blkCnt);
  134. vecSrcA = vldrwq_z_f32(pSrcA, p);
  135. vecSrcB = vldrwq_z_f32(pSrcB, p);
  136. vec_acc = vcmulq_m(vuninitializedq_f32(),vecSrcA, vecSrcB, p);
  137. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  138. vstrwq_p_f32(pDst, vec_acc, p);
  139. pDst += 4;
  140. /*
  141. * Decrement the blkCnt loop counter
  142. * Advance vector source and destination pointers
  143. */
  144. pSrcA += 4;
  145. pSrcB += 4;
  146. blkCnt -= 4;
  147. }
  148. while (blkCnt > 0);
  149. }
  150. }
  151. #else
  152. void arm_cmplx_mult_cmplx_f32(
  153. const float32_t * pSrcA,
  154. const float32_t * pSrcB,
  155. float32_t * pDst,
  156. uint32_t numSamples)
  157. {
  158. uint32_t blkCnt; /* Loop counter */
  159. float32_t a, b, c, d; /* Temporary variables to store real and imaginary values */
  160. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  161. float32x4x2_t va, vb;
  162. float32x4x2_t outCplx;
  163. /* Compute 4 outputs at a time */
  164. blkCnt = numSamples >> 2U;
  165. while (blkCnt > 0U)
  166. {
  167. va = vld2q_f32(pSrcA); // load & separate real/imag pSrcA (de-interleave 2)
  168. vb = vld2q_f32(pSrcB); // load & separate real/imag pSrcB
  169. /* Increment pointers */
  170. pSrcA += 8;
  171. pSrcB += 8;
  172. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  173. outCplx.val[0] = vmulq_f32(va.val[0], vb.val[0]);
  174. outCplx.val[0] = vmlsq_f32(outCplx.val[0], va.val[1], vb.val[1]);
  175. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  176. outCplx.val[1] = vmulq_f32(va.val[0], vb.val[1]);
  177. outCplx.val[1] = vmlaq_f32(outCplx.val[1], va.val[1], vb.val[0]);
  178. vst2q_f32(pDst, outCplx);
  179. /* Increment pointer */
  180. pDst += 8;
  181. /* Decrement the loop counter */
  182. blkCnt--;
  183. }
  184. /* Tail */
  185. blkCnt = numSamples & 3;
  186. #else
  187. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  188. /* Loop unrolling: Compute 4 outputs at a time */
  189. blkCnt = numSamples >> 2U;
  190. while (blkCnt > 0U)
  191. {
  192. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  193. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  194. a = *pSrcA++;
  195. b = *pSrcA++;
  196. c = *pSrcB++;
  197. d = *pSrcB++;
  198. /* store result in destination buffer. */
  199. *pDst++ = (a * c) - (b * d);
  200. *pDst++ = (a * d) + (b * c);
  201. a = *pSrcA++;
  202. b = *pSrcA++;
  203. c = *pSrcB++;
  204. d = *pSrcB++;
  205. *pDst++ = (a * c) - (b * d);
  206. *pDst++ = (a * d) + (b * c);
  207. a = *pSrcA++;
  208. b = *pSrcA++;
  209. c = *pSrcB++;
  210. d = *pSrcB++;
  211. *pDst++ = (a * c) - (b * d);
  212. *pDst++ = (a * d) + (b * c);
  213. a = *pSrcA++;
  214. b = *pSrcA++;
  215. c = *pSrcB++;
  216. d = *pSrcB++;
  217. *pDst++ = (a * c) - (b * d);
  218. *pDst++ = (a * d) + (b * c);
  219. /* Decrement loop counter */
  220. blkCnt--;
  221. }
  222. /* Loop unrolling: Compute remaining outputs */
  223. blkCnt = numSamples % 0x4U;
  224. #else
  225. /* Initialize blkCnt with number of samples */
  226. blkCnt = numSamples;
  227. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  228. #endif /* #if defined(ARM_MATH_NEON) */
  229. while (blkCnt > 0U)
  230. {
  231. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  232. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  233. a = *pSrcA++;
  234. b = *pSrcA++;
  235. c = *pSrcB++;
  236. d = *pSrcB++;
  237. /* store result in destination buffer. */
  238. *pDst++ = (a * c) - (b * d);
  239. *pDst++ = (a * d) + (b * c);
  240. /* Decrement loop counter */
  241. blkCnt--;
  242. }
  243. }
  244. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  245. /**
  246. @} end of CmplxByCmplxMult group
  247. */