arm_cmplx_dot_prod_f32.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. 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. uint32_t blkCnt; /* Loop counter */
  74. float32_t real_sum = 0.0f, imag_sum = 0.0f; /* Temporary result variables */
  75. float32_t a0,b0,c0,d0;
  76. #if defined(ARM_MATH_NEON)
  77. float32x4x2_t vec1,vec2,vec3,vec4;
  78. float32x4_t accR,accI;
  79. float32x2_t accum = vdup_n_f32(0);
  80. accR = vdupq_n_f32(0.0);
  81. accI = vdupq_n_f32(0.0);
  82. /* Loop unrolling: Compute 8 outputs at a time */
  83. blkCnt = numSamples >> 3U;
  84. while (blkCnt > 0U)
  85. {
  86. /* C = (A[0]+jA[1])*(B[0]+jB[1]) + ... */
  87. /* Calculate dot product and then store the result in a temporary buffer. */
  88. vec1 = vld2q_f32(pSrcA);
  89. vec2 = vld2q_f32(pSrcB);
  90. /* Increment pointers */
  91. pSrcA += 8;
  92. pSrcB += 8;
  93. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  94. accR = vmlaq_f32(accR,vec1.val[0],vec2.val[0]);
  95. accR = vmlsq_f32(accR,vec1.val[1],vec2.val[1]);
  96. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  97. accI = vmlaq_f32(accI,vec1.val[1],vec2.val[0]);
  98. accI = vmlaq_f32(accI,vec1.val[0],vec2.val[1]);
  99. vec3 = vld2q_f32(pSrcA);
  100. vec4 = vld2q_f32(pSrcB);
  101. /* Increment pointers */
  102. pSrcA += 8;
  103. pSrcB += 8;
  104. /* Re{C} = Re{A}*Re{B} - Im{A}*Im{B} */
  105. accR = vmlaq_f32(accR,vec3.val[0],vec4.val[0]);
  106. accR = vmlsq_f32(accR,vec3.val[1],vec4.val[1]);
  107. /* Im{C} = Re{A}*Im{B} + Im{A}*Re{B} */
  108. accI = vmlaq_f32(accI,vec3.val[1],vec4.val[0]);
  109. accI = vmlaq_f32(accI,vec3.val[0],vec4.val[1]);
  110. /* Decrement the loop counter */
  111. blkCnt--;
  112. }
  113. accum = vpadd_f32(vget_low_f32(accR), vget_high_f32(accR));
  114. real_sum += accum[0] + accum[1];
  115. accum = vpadd_f32(vget_low_f32(accI), vget_high_f32(accI));
  116. imag_sum += accum[0] + accum[1];
  117. /* Tail */
  118. blkCnt = numSamples & 0x7;
  119. #else
  120. #if defined (ARM_MATH_LOOPUNROLL)
  121. /* Loop unrolling: Compute 4 outputs at a time */
  122. blkCnt = numSamples >> 2U;
  123. while (blkCnt > 0U)
  124. {
  125. a0 = *pSrcA++;
  126. b0 = *pSrcA++;
  127. c0 = *pSrcB++;
  128. d0 = *pSrcB++;
  129. real_sum += a0 * c0;
  130. imag_sum += a0 * d0;
  131. real_sum -= b0 * d0;
  132. imag_sum += b0 * c0;
  133. a0 = *pSrcA++;
  134. b0 = *pSrcA++;
  135. c0 = *pSrcB++;
  136. d0 = *pSrcB++;
  137. real_sum += a0 * c0;
  138. imag_sum += a0 * d0;
  139. real_sum -= b0 * d0;
  140. imag_sum += b0 * c0;
  141. a0 = *pSrcA++;
  142. b0 = *pSrcA++;
  143. c0 = *pSrcB++;
  144. d0 = *pSrcB++;
  145. real_sum += a0 * c0;
  146. imag_sum += a0 * d0;
  147. real_sum -= b0 * d0;
  148. imag_sum += b0 * c0;
  149. a0 = *pSrcA++;
  150. b0 = *pSrcA++;
  151. c0 = *pSrcB++;
  152. d0 = *pSrcB++;
  153. real_sum += a0 * c0;
  154. imag_sum += a0 * d0;
  155. real_sum -= b0 * d0;
  156. imag_sum += b0 * c0;
  157. /* Decrement loop counter */
  158. blkCnt--;
  159. }
  160. /* Loop unrolling: Compute remaining outputs */
  161. blkCnt = numSamples % 0x4U;
  162. #else
  163. /* Initialize blkCnt with number of samples */
  164. blkCnt = numSamples;
  165. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  166. #endif /* #if defined(ARM_MATH_NEON) */
  167. while (blkCnt > 0U)
  168. {
  169. a0 = *pSrcA++;
  170. b0 = *pSrcA++;
  171. c0 = *pSrcB++;
  172. d0 = *pSrcB++;
  173. real_sum += a0 * c0;
  174. imag_sum += a0 * d0;
  175. real_sum -= b0 * d0;
  176. imag_sum += b0 * c0;
  177. /* Decrement loop counter */
  178. blkCnt--;
  179. }
  180. /* Store real and imaginary result in destination buffer. */
  181. *realResult = real_sum;
  182. *imagResult = imag_sum;
  183. }
  184. /**
  185. @} end of cmplx_dot_prod group
  186. */