arm_cmplx_dot_prod_f32.c 9.2 KB

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