arm_cmplx_mult_cmplx_f32.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 "arm_math.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. @return none
  60. */
  61. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  62. void arm_cmplx_mult_cmplx_f32(
  63. const float32_t * pSrcA,
  64. const float32_t * pSrcB,
  65. float32_t * pDst,
  66. uint32_t numSamples)
  67. {
  68. uint32_t blkCnt; /* loop counters */
  69. uint32_t blockSize = numSamples; /* loop counters */
  70. float32_t a, b, c, d; /* Temporary variables to store real and imaginary values */
  71. f32x4x2_t vecA;
  72. f32x4x2_t vecB;
  73. f32x4x2_t vecDst;
  74. /* Compute 4 complex outputs at a time */
  75. blkCnt = blockSize >> 2;
  76. while (blkCnt > 0U)
  77. {
  78. vecA = vld2q(pSrcA); // load & separate real/imag pSrcA (de-interleave 2)
  79. vecB = vld2q(pSrcB); // load & separate real/imag pSrcB
  80. pSrcA += 8;
  81. pSrcB += 8;
  82. /* C[2 * i] = A[2 * i] * B[2 * i] - A[2 * i + 1] * B[2 * i + 1]. */
  83. vecDst.val[0] = vmulq(vecA.val[0], vecB.val[0]);
  84. vecDst.val[0] = vfmsq(vecDst.val[0],vecA.val[1], vecB.val[1]);
  85. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i]. */
  86. vecDst.val[1] = vmulq(vecA.val[0], vecB.val[1]);
  87. vecDst.val[1] = vfmaq(vecDst.val[1], vecA.val[1], vecB.val[0]);
  88. vst2q(pDst, vecDst);
  89. pDst += 8;
  90. blkCnt--;
  91. }
  92. /* Tail */
  93. blkCnt = blockSize & 3;
  94. while (blkCnt > 0U)
  95. {
  96. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  97. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  98. a = *pSrcA++;
  99. b = *pSrcA++;
  100. c = *pSrcB++;
  101. d = *pSrcB++;
  102. /* store result in destination buffer. */
  103. *pDst++ = (a * c) - (b * d);
  104. *pDst++ = (a * d) + (b * c);
  105. /* Decrement loop counter */
  106. blkCnt--;
  107. }
  108. }
  109. #else
  110. void arm_cmplx_mult_cmplx_f32(
  111. const float32_t * pSrcA,
  112. const float32_t * pSrcB,
  113. float32_t * pDst,
  114. uint32_t numSamples)
  115. {
  116. uint32_t blkCnt; /* Loop counter */
  117. float32_t a, b, c, d; /* Temporary variables to store real and imaginary values */
  118. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  119. float32x4x2_t va, vb;
  120. float32x4x2_t outCplx;
  121. /* Compute 4 outputs at a time */
  122. blkCnt = numSamples >> 2U;
  123. while (blkCnt > 0U)
  124. {
  125. va = vld2q_f32(pSrcA); // load & separate real/imag pSrcA (de-interleave 2)
  126. vb = vld2q_f32(pSrcB); // load & separate real/imag pSrcB
  127. /* Increment pointers */
  128. pSrcA += 8;
  129. pSrcB += 8;
  130. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  131. outCplx.val[0] = vmulq_f32(va.val[0], vb.val[0]);
  132. outCplx.val[0] = vmlsq_f32(outCplx.val[0], va.val[1], vb.val[1]);
  133. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  134. outCplx.val[1] = vmulq_f32(va.val[0], vb.val[1]);
  135. outCplx.val[1] = vmlaq_f32(outCplx.val[1], va.val[1], vb.val[0]);
  136. vst2q_f32(pDst, outCplx);
  137. /* Increment pointer */
  138. pDst += 8;
  139. /* Decrement the loop counter */
  140. blkCnt--;
  141. }
  142. /* Tail */
  143. blkCnt = numSamples & 3;
  144. #else
  145. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  146. /* Loop unrolling: Compute 4 outputs at a time */
  147. blkCnt = numSamples >> 2U;
  148. while (blkCnt > 0U)
  149. {
  150. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  151. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  152. a = *pSrcA++;
  153. b = *pSrcA++;
  154. c = *pSrcB++;
  155. d = *pSrcB++;
  156. /* store result in destination buffer. */
  157. *pDst++ = (a * c) - (b * d);
  158. *pDst++ = (a * d) + (b * c);
  159. a = *pSrcA++;
  160. b = *pSrcA++;
  161. c = *pSrcB++;
  162. d = *pSrcB++;
  163. *pDst++ = (a * c) - (b * d);
  164. *pDst++ = (a * d) + (b * c);
  165. a = *pSrcA++;
  166. b = *pSrcA++;
  167. c = *pSrcB++;
  168. d = *pSrcB++;
  169. *pDst++ = (a * c) - (b * d);
  170. *pDst++ = (a * d) + (b * c);
  171. a = *pSrcA++;
  172. b = *pSrcA++;
  173. c = *pSrcB++;
  174. d = *pSrcB++;
  175. *pDst++ = (a * c) - (b * d);
  176. *pDst++ = (a * d) + (b * c);
  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. #endif /* #if defined(ARM_MATH_NEON) */
  187. while (blkCnt > 0U)
  188. {
  189. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  190. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  191. a = *pSrcA++;
  192. b = *pSrcA++;
  193. c = *pSrcB++;
  194. d = *pSrcB++;
  195. /* store result in destination buffer. */
  196. *pDst++ = (a * c) - (b * d);
  197. *pDst++ = (a * d) + (b * c);
  198. /* Decrement loop counter */
  199. blkCnt--;
  200. }
  201. }
  202. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  203. /**
  204. @} end of CmplxByCmplxMult group
  205. */