arm_cmplx_dot_prod_f32.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_dot_prod_f32.c
  4. * Description: Floating-point complex dot product
  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 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. */
  65. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  66. void arm_cmplx_dot_prod_f32(
  67. const float32_t * pSrcA,
  68. const float32_t * pSrcB,
  69. uint32_t numSamples,
  70. float32_t * realResult,
  71. float32_t * imagResult)
  72. {
  73. int32_t blkCnt;
  74. float32_t real_sum, imag_sum;
  75. f32x4_t vecSrcA, vecSrcB;
  76. f32x4_t vec_acc = vdupq_n_f32(0.0f);
  77. f32x4_t vecSrcC, vecSrcD;
  78. blkCnt = numSamples >> 2;
  79. blkCnt -= 1;
  80. if (blkCnt > 0) {
  81. /* should give more freedom to generate stall free code */
  82. vecSrcA = vld1q(pSrcA);
  83. vecSrcB = vld1q(pSrcB);
  84. pSrcA += 4;
  85. pSrcB += 4;
  86. while (blkCnt > 0) {
  87. vec_acc = vcmlaq(vec_acc, vecSrcA, vecSrcB);
  88. vecSrcC = vld1q(pSrcA);
  89. pSrcA += 4;
  90. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  91. vecSrcD = vld1q(pSrcB);
  92. pSrcB += 4;
  93. vec_acc = vcmlaq(vec_acc, vecSrcC, vecSrcD);
  94. vecSrcA = vld1q(pSrcA);
  95. pSrcA += 4;
  96. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  97. vecSrcB = vld1q(pSrcB);
  98. pSrcB += 4;
  99. /*
  100. * Decrement the blockSize loop counter
  101. */
  102. blkCnt--;
  103. }
  104. /* process last elements out of the loop avoid the armclang breaking the SW pipeline */
  105. vec_acc = vcmlaq(vec_acc, vecSrcA, vecSrcB);
  106. vecSrcC = vld1q(pSrcA);
  107. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  108. vecSrcD = vld1q(pSrcB);
  109. vec_acc = vcmlaq(vec_acc, vecSrcC, vecSrcD);
  110. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  111. /*
  112. * tail
  113. */
  114. blkCnt = CMPLX_DIM * (numSamples & 3);
  115. while (blkCnt > 0) {
  116. mve_pred16_t p = vctp32q(blkCnt);
  117. pSrcA += 4;
  118. pSrcB += 4;
  119. vecSrcA = vldrwq_z_f32(pSrcA, p);
  120. vecSrcB = vldrwq_z_f32(pSrcB, p);
  121. vec_acc = vcmlaq_m(vec_acc, vecSrcA, vecSrcB, p);
  122. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  123. blkCnt -= 4;
  124. }
  125. } else {
  126. /* small vector */
  127. blkCnt = numSamples * CMPLX_DIM;
  128. vec_acc = vdupq_n_f32(0.0f);
  129. do {
  130. mve_pred16_t p = vctp32q(blkCnt);
  131. vecSrcA = vldrwq_z_f32(pSrcA, p);
  132. vecSrcB = vldrwq_z_f32(pSrcB, p);
  133. vec_acc = vcmlaq_m(vec_acc, vecSrcA, vecSrcB, p);
  134. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  135. /*
  136. * Decrement the blkCnt loop counter
  137. * Advance vector source and destination pointers
  138. */
  139. pSrcA += 4;
  140. pSrcB += 4;
  141. blkCnt -= 4;
  142. }
  143. while (blkCnt > 0);
  144. }
  145. real_sum = vgetq_lane(vec_acc, 0) + vgetq_lane(vec_acc, 2);
  146. imag_sum = vgetq_lane(vec_acc, 1) + vgetq_lane(vec_acc, 3);
  147. /*
  148. * Store the real and imaginary results in the destination buffers
  149. */
  150. *realResult = real_sum;
  151. *imagResult = imag_sum;
  152. }
  153. #else
  154. void arm_cmplx_dot_prod_f32(
  155. const float32_t * pSrcA,
  156. const float32_t * pSrcB,
  157. uint32_t numSamples,
  158. float32_t * realResult,
  159. float32_t * imagResult)
  160. {
  161. uint32_t blkCnt; /* Loop counter */
  162. float32_t real_sum = 0.0f, imag_sum = 0.0f; /* Temporary result variables */
  163. float32_t a0,b0,c0,d0;
  164. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  165. float32x4x2_t vec1,vec2,vec3,vec4;
  166. float32x4_t accR,accI;
  167. float32x2_t accum = vdup_n_f32(0);
  168. accR = vdupq_n_f32(0.0f);
  169. accI = vdupq_n_f32(0.0f);
  170. /* Loop unrolling: Compute 8 outputs at a time */
  171. blkCnt = numSamples >> 3U;
  172. while (blkCnt > 0U)
  173. {
  174. /* C = (A[0]+jA[1])*(B[0]+jB[1]) + ... */
  175. /* Calculate dot product and then store the result in a temporary buffer. */
  176. vec1 = vld2q_f32(pSrcA);
  177. vec2 = vld2q_f32(pSrcB);
  178. /* Increment pointers */
  179. pSrcA += 8;
  180. pSrcB += 8;
  181. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  182. accR = vmlaq_f32(accR,vec1.val[0],vec2.val[0]);
  183. accR = vmlsq_f32(accR,vec1.val[1],vec2.val[1]);
  184. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  185. accI = vmlaq_f32(accI,vec1.val[1],vec2.val[0]);
  186. accI = vmlaq_f32(accI,vec1.val[0],vec2.val[1]);
  187. vec3 = vld2q_f32(pSrcA);
  188. vec4 = vld2q_f32(pSrcB);
  189. /* Increment pointers */
  190. pSrcA += 8;
  191. pSrcB += 8;
  192. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  193. accR = vmlaq_f32(accR,vec3.val[0],vec4.val[0]);
  194. accR = vmlsq_f32(accR,vec3.val[1],vec4.val[1]);
  195. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  196. accI = vmlaq_f32(accI,vec3.val[1],vec4.val[0]);
  197. accI = vmlaq_f32(accI,vec3.val[0],vec4.val[1]);
  198. /* Decrement the loop counter */
  199. blkCnt--;
  200. }
  201. accum = vpadd_f32(vget_low_f32(accR), vget_high_f32(accR));
  202. real_sum += vget_lane_f32(accum, 0) + vget_lane_f32(accum, 1);
  203. accum = vpadd_f32(vget_low_f32(accI), vget_high_f32(accI));
  204. imag_sum += vget_lane_f32(accum, 0) + vget_lane_f32(accum, 1);
  205. /* Tail */
  206. blkCnt = numSamples & 0x7;
  207. #else
  208. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  209. /* Loop unrolling: Compute 4 outputs at a time */
  210. blkCnt = numSamples >> 2U;
  211. while (blkCnt > 0U)
  212. {
  213. a0 = *pSrcA++;
  214. b0 = *pSrcA++;
  215. c0 = *pSrcB++;
  216. d0 = *pSrcB++;
  217. real_sum += a0 * c0;
  218. imag_sum += a0 * d0;
  219. real_sum -= b0 * d0;
  220. imag_sum += b0 * c0;
  221. a0 = *pSrcA++;
  222. b0 = *pSrcA++;
  223. c0 = *pSrcB++;
  224. d0 = *pSrcB++;
  225. real_sum += a0 * c0;
  226. imag_sum += a0 * d0;
  227. real_sum -= b0 * d0;
  228. imag_sum += b0 * c0;
  229. a0 = *pSrcA++;
  230. b0 = *pSrcA++;
  231. c0 = *pSrcB++;
  232. d0 = *pSrcB++;
  233. real_sum += a0 * c0;
  234. imag_sum += a0 * d0;
  235. real_sum -= b0 * d0;
  236. imag_sum += b0 * c0;
  237. a0 = *pSrcA++;
  238. b0 = *pSrcA++;
  239. c0 = *pSrcB++;
  240. d0 = *pSrcB++;
  241. real_sum += a0 * c0;
  242. imag_sum += a0 * d0;
  243. real_sum -= b0 * d0;
  244. imag_sum += b0 * c0;
  245. /* Decrement loop counter */
  246. blkCnt--;
  247. }
  248. /* Loop unrolling: Compute remaining outputs */
  249. blkCnt = numSamples % 0x4U;
  250. #else
  251. /* Initialize blkCnt with number of samples */
  252. blkCnt = numSamples;
  253. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  254. #endif /* #if defined(ARM_MATH_NEON) */
  255. while (blkCnt > 0U)
  256. {
  257. a0 = *pSrcA++;
  258. b0 = *pSrcA++;
  259. c0 = *pSrcB++;
  260. d0 = *pSrcB++;
  261. real_sum += a0 * c0;
  262. imag_sum += a0 * d0;
  263. real_sum -= b0 * d0;
  264. imag_sum += b0 * c0;
  265. /* Decrement loop counter */
  266. blkCnt--;
  267. }
  268. /* Store real and imaginary result in destination buffer. */
  269. *realResult = real_sum;
  270. *imagResult = imag_sum;
  271. }
  272. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  273. /**
  274. @} end of cmplx_dot_prod group
  275. */