arm_cmplx_mult_real_f32.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_real_f32.c
  4. * Description: Floating-point complex by real 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 CmplxByRealMult Complex-by-Real Multiplication
  34. Multiplies a complex vector by a real 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 while the real array has a total of <code>numSamples</code>
  40. real values.
  41. The underlying algorithm is used:
  42. <pre>
  43. for (n = 0; n < numSamples; n++) {
  44. pCmplxDst[(2*n)+0] = pSrcCmplx[(2*n)+0] * pSrcReal[n];
  45. pCmplxDst[(2*n)+1] = pSrcCmplx[(2*n)+1] * pSrcReal[n];
  46. }
  47. </pre>
  48. There are separate functions for floating-point, Q15, and Q31 data types.
  49. */
  50. /**
  51. @addtogroup CmplxByRealMult
  52. @{
  53. */
  54. /**
  55. @brief Floating-point complex-by-real multiplication.
  56. @param[in] pSrcCmplx points to complex input vector
  57. @param[in] pSrcReal points to real input vector
  58. @param[out] pCmplxDst points to complex output vector
  59. @param[in] numSamples number of samples in each vector
  60. */
  61. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  62. void arm_cmplx_mult_real_f32(
  63. const float32_t * pSrcCmplx,
  64. const float32_t * pSrcReal,
  65. float32_t * pCmplxDst,
  66. uint32_t numSamples)
  67. {
  68. static const uint32_t stride_cmplx_x_real_32[4] = { 0, 0, 1, 1 };
  69. uint32_t blockSizeC = numSamples * CMPLX_DIM; /* loop counters */
  70. uint32_t blkCnt;
  71. f32x4_t rVec;
  72. f32x4_t cmplxVec;
  73. f32x4_t dstVec;
  74. uint32x4_t strideVec;
  75. float32_t in;
  76. /* stride vector for pairs of real generation */
  77. strideVec = vld1q(stride_cmplx_x_real_32);
  78. /* Compute 4 complex outputs at a time */
  79. blkCnt = blockSizeC >> 2;
  80. while (blkCnt > 0U)
  81. {
  82. cmplxVec = vld1q(pSrcCmplx);
  83. rVec = vldrwq_gather_shifted_offset_f32(pSrcReal, strideVec);
  84. dstVec = vmulq(cmplxVec, rVec);
  85. vst1q(pCmplxDst, dstVec);
  86. pSrcReal += 2;
  87. pSrcCmplx += 4;
  88. pCmplxDst += 4;
  89. blkCnt--;
  90. }
  91. blkCnt = (blockSizeC & 3) >> 1;
  92. while (blkCnt > 0U)
  93. {
  94. /* C[2 * i ] = A[2 * i ] * B[i]. */
  95. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  96. in = *pSrcReal++;
  97. /* store result in destination buffer. */
  98. *pCmplxDst++ = *pSrcCmplx++ * in;
  99. *pCmplxDst++ = *pSrcCmplx++ * in;
  100. /* Decrement loop counter */
  101. blkCnt--;
  102. }
  103. }
  104. #else
  105. void arm_cmplx_mult_real_f32(
  106. const float32_t * pSrcCmplx,
  107. const float32_t * pSrcReal,
  108. float32_t * pCmplxDst,
  109. uint32_t numSamples)
  110. {
  111. uint32_t blkCnt; /* Loop counter */
  112. float32_t in; /* Temporary variable */
  113. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  114. float32x4_t r;
  115. float32x4x2_t ab,outCplx;
  116. /* Compute 4 outputs at a time */
  117. blkCnt = numSamples >> 2U;
  118. while (blkCnt > 0U)
  119. {
  120. ab = vld2q_f32(pSrcCmplx); // load & separate real/imag pSrcA (de-interleave 2)
  121. r = vld1q_f32(pSrcReal); // load & separate real/imag pSrcB
  122. /* Increment pointers */
  123. pSrcCmplx += 8;
  124. pSrcReal += 4;
  125. outCplx.val[0] = vmulq_f32(ab.val[0], r);
  126. outCplx.val[1] = vmulq_f32(ab.val[1], r);
  127. vst2q_f32(pCmplxDst, outCplx);
  128. pCmplxDst += 8;
  129. blkCnt--;
  130. }
  131. /* Tail */
  132. blkCnt = numSamples & 3;
  133. #else
  134. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  135. /* Loop unrolling: Compute 4 outputs at a time */
  136. blkCnt = numSamples >> 2U;
  137. while (blkCnt > 0U)
  138. {
  139. /* C[2 * i ] = A[2 * i ] * B[i]. */
  140. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  141. in = *pSrcReal++;
  142. /* store result in destination buffer. */
  143. *pCmplxDst++ = *pSrcCmplx++ * in;
  144. *pCmplxDst++ = *pSrcCmplx++ * in;
  145. in = *pSrcReal++;
  146. *pCmplxDst++ = *pSrcCmplx++ * in;
  147. *pCmplxDst++ = *pSrcCmplx++ * in;
  148. in = *pSrcReal++;
  149. *pCmplxDst++ = *pSrcCmplx++ * in;
  150. *pCmplxDst++ = *pSrcCmplx++ * in;
  151. in = *pSrcReal++;
  152. *pCmplxDst++ = *pSrcCmplx++* in;
  153. *pCmplxDst++ = *pSrcCmplx++ * in;
  154. /* Decrement loop counter */
  155. blkCnt--;
  156. }
  157. /* Loop unrolling: Compute remaining outputs */
  158. blkCnt = numSamples % 0x4U;
  159. #else
  160. /* Initialize blkCnt with number of samples */
  161. blkCnt = numSamples;
  162. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  163. #endif /* #if defined(ARM_MATH_NEON) */
  164. while (blkCnt > 0U)
  165. {
  166. /* C[2 * i ] = A[2 * i ] * B[i]. */
  167. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  168. in = *pSrcReal++;
  169. /* store result in destination buffer. */
  170. *pCmplxDst++ = *pSrcCmplx++ * in;
  171. *pCmplxDst++ = *pSrcCmplx++ * in;
  172. /* Decrement loop counter */
  173. blkCnt--;
  174. }
  175. }
  176. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  177. /**
  178. @} end of CmplxByRealMult group
  179. */