arm_cmplx_dot_prod_f32.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_dot_prod_f32.c
  4. * Description: Floating-point complex dot product
  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 cmplx_dot_prod Complex Dot Product
  34. Computes the dot product of two complex vectors.
  35. The vectors are multiplied element-by-element and then summed.
  36. The <code>pSrcA</code> points to the first complex input vector and
  37. <code>pSrcB</code> points to the second complex input vector.
  38. <code>numSamples</code> specifies the number of complex samples
  39. and the data in each array is stored in an interleaved fashion
  40. (real, imag, real, imag, ...).
  41. Each array has a total of <code>2*numSamples</code> values.
  42. The underlying algorithm is used:
  43. <pre>
  44. realResult = 0;
  45. imagResult = 0;
  46. for (n = 0; n < numSamples; n++) {
  47. realResult += pSrcA[(2*n)+0] * pSrcB[(2*n)+0] - pSrcA[(2*n)+1] * pSrcB[(2*n)+1];
  48. imagResult += pSrcA[(2*n)+0] * pSrcB[(2*n)+1] + pSrcA[(2*n)+1] * pSrcB[(2*n)+0];
  49. }
  50. </pre>
  51. There are separate functions for floating-point, Q15, and Q31 data types.
  52. */
  53. /**
  54. @addtogroup cmplx_dot_prod
  55. @{
  56. */
  57. /**
  58. @brief Floating-point complex dot product.
  59. @param[in] pSrcA points to the first input vector
  60. @param[in] pSrcB points to the second input vector
  61. @param[in] numSamples number of samples in each vector
  62. @param[out] realResult real part of the result returned here
  63. @param[out] imagResult imaginary part of the result returned here
  64. @return none
  65. */
  66. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  67. void arm_cmplx_dot_prod_f32(
  68. const float32_t * pSrcA,
  69. const float32_t * pSrcB,
  70. uint32_t numSamples,
  71. float32_t * realResult,
  72. float32_t * imagResult)
  73. {
  74. uint32_t blockSize = numSamples * CMPLX_DIM; /* loop counters */
  75. uint32_t blkCnt;
  76. float32_t real_sum, imag_sum;
  77. f32x4_t vecSrcA, vecSrcB;
  78. f32x4_t vec_acc = vdupq_n_f32(0.0f);
  79. float32_t a0,b0,c0,d0;
  80. /* Compute 2 complex samples at a time */
  81. blkCnt = blockSize >> 2U;
  82. while (blkCnt > 0U)
  83. {
  84. vecSrcA = vld1q(pSrcA);
  85. vecSrcB = vld1q(pSrcB);
  86. vec_acc = vcmlaq(vec_acc, vecSrcA, vecSrcB);
  87. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  88. /*
  89. * Decrement the blkCnt loop counter
  90. * Advance vector source and destination pointers
  91. */
  92. pSrcA += 4;
  93. pSrcB += 4;
  94. blkCnt--;
  95. }
  96. real_sum = vgetq_lane(vec_acc, 0) + vgetq_lane(vec_acc, 2);
  97. imag_sum = vgetq_lane(vec_acc, 1) + vgetq_lane(vec_acc, 3);
  98. /* Tail */
  99. blkCnt = (blockSize & 3) >> 1;
  100. while (blkCnt > 0U)
  101. {
  102. a0 = *pSrcA++;
  103. b0 = *pSrcA++;
  104. c0 = *pSrcB++;
  105. d0 = *pSrcB++;
  106. real_sum += a0 * c0;
  107. imag_sum += a0 * d0;
  108. real_sum -= b0 * d0;
  109. imag_sum += b0 * c0;
  110. /* Decrement loop counter */
  111. blkCnt--;
  112. }
  113. /*
  114. * Store the real and imaginary results in the destination buffers
  115. */
  116. *realResult = real_sum;
  117. *imagResult = imag_sum;
  118. }
  119. #else
  120. void arm_cmplx_dot_prod_f32(
  121. const float32_t * pSrcA,
  122. const float32_t * pSrcB,
  123. uint32_t numSamples,
  124. float32_t * realResult,
  125. float32_t * imagResult)
  126. {
  127. uint32_t blkCnt; /* Loop counter */
  128. float32_t real_sum = 0.0f, imag_sum = 0.0f; /* Temporary result variables */
  129. float32_t a0,b0,c0,d0;
  130. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  131. float32x4x2_t vec1,vec2,vec3,vec4;
  132. float32x4_t accR,accI;
  133. float32x2_t accum = vdup_n_f32(0);
  134. accR = vdupq_n_f32(0.0f);
  135. accI = vdupq_n_f32(0.0f);
  136. /* Loop unrolling: Compute 8 outputs at a time */
  137. blkCnt = numSamples >> 3U;
  138. while (blkCnt > 0U)
  139. {
  140. /* C = (A[0]+jA[1])*(B[0]+jB[1]) + ... */
  141. /* Calculate dot product and then store the result in a temporary buffer. */
  142. vec1 = vld2q_f32(pSrcA);
  143. vec2 = vld2q_f32(pSrcB);
  144. /* Increment pointers */
  145. pSrcA += 8;
  146. pSrcB += 8;
  147. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  148. accR = vmlaq_f32(accR,vec1.val[0],vec2.val[0]);
  149. accR = vmlsq_f32(accR,vec1.val[1],vec2.val[1]);
  150. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  151. accI = vmlaq_f32(accI,vec1.val[1],vec2.val[0]);
  152. accI = vmlaq_f32(accI,vec1.val[0],vec2.val[1]);
  153. vec3 = vld2q_f32(pSrcA);
  154. vec4 = vld2q_f32(pSrcB);
  155. /* Increment pointers */
  156. pSrcA += 8;
  157. pSrcB += 8;
  158. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  159. accR = vmlaq_f32(accR,vec3.val[0],vec4.val[0]);
  160. accR = vmlsq_f32(accR,vec3.val[1],vec4.val[1]);
  161. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  162. accI = vmlaq_f32(accI,vec3.val[1],vec4.val[0]);
  163. accI = vmlaq_f32(accI,vec3.val[0],vec4.val[1]);
  164. /* Decrement the loop counter */
  165. blkCnt--;
  166. }
  167. accum = vpadd_f32(vget_low_f32(accR), vget_high_f32(accR));
  168. real_sum += vget_lane_f32(accum, 0) + vget_lane_f32(accum, 1);
  169. accum = vpadd_f32(vget_low_f32(accI), vget_high_f32(accI));
  170. imag_sum += vget_lane_f32(accum, 0) + vget_lane_f32(accum, 1);
  171. /* Tail */
  172. blkCnt = numSamples & 0x7;
  173. #else
  174. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  175. /* Loop unrolling: Compute 4 outputs at a time */
  176. blkCnt = numSamples >> 2U;
  177. while (blkCnt > 0U)
  178. {
  179. a0 = *pSrcA++;
  180. b0 = *pSrcA++;
  181. c0 = *pSrcB++;
  182. d0 = *pSrcB++;
  183. real_sum += a0 * c0;
  184. imag_sum += a0 * d0;
  185. real_sum -= b0 * d0;
  186. imag_sum += b0 * c0;
  187. a0 = *pSrcA++;
  188. b0 = *pSrcA++;
  189. c0 = *pSrcB++;
  190. d0 = *pSrcB++;
  191. real_sum += a0 * c0;
  192. imag_sum += a0 * d0;
  193. real_sum -= b0 * d0;
  194. imag_sum += b0 * c0;
  195. a0 = *pSrcA++;
  196. b0 = *pSrcA++;
  197. c0 = *pSrcB++;
  198. d0 = *pSrcB++;
  199. real_sum += a0 * c0;
  200. imag_sum += a0 * d0;
  201. real_sum -= b0 * d0;
  202. imag_sum += b0 * c0;
  203. a0 = *pSrcA++;
  204. b0 = *pSrcA++;
  205. c0 = *pSrcB++;
  206. d0 = *pSrcB++;
  207. real_sum += a0 * c0;
  208. imag_sum += a0 * d0;
  209. real_sum -= b0 * d0;
  210. imag_sum += b0 * c0;
  211. /* Decrement loop counter */
  212. blkCnt--;
  213. }
  214. /* Loop unrolling: Compute remaining outputs */
  215. blkCnt = numSamples % 0x4U;
  216. #else
  217. /* Initialize blkCnt with number of samples */
  218. blkCnt = numSamples;
  219. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  220. #endif /* #if defined(ARM_MATH_NEON) */
  221. while (blkCnt > 0U)
  222. {
  223. a0 = *pSrcA++;
  224. b0 = *pSrcA++;
  225. c0 = *pSrcB++;
  226. d0 = *pSrcB++;
  227. real_sum += a0 * c0;
  228. imag_sum += a0 * d0;
  229. real_sum -= b0 * d0;
  230. imag_sum += b0 * c0;
  231. /* Decrement loop counter */
  232. blkCnt--;
  233. }
  234. /* Store real and imaginary result in destination buffer. */
  235. *realResult = real_sum;
  236. *imagResult = imag_sum;
  237. }
  238. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  239. /**
  240. @} end of cmplx_dot_prod group
  241. */