arm_cmplx_dot_prod_f16.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_dot_prod_f16.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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupCmplxMath
  32. */
  33. /**
  34. @defgroup cmplx_dot_prod Complex Dot Product
  35. Computes the dot product of two complex vectors.
  36. The vectors are multiplied element-by-element and then summed.
  37. The <code>pSrcA</code> points to the first complex input vector and
  38. <code>pSrcB</code> points to the second complex input vector.
  39. <code>numSamples</code> specifies the number of complex samples
  40. and the data in each array is stored in an interleaved fashion
  41. (real, imag, real, imag, ...).
  42. Each array has a total of <code>2*numSamples</code> values.
  43. The underlying algorithm is used:
  44. <pre>
  45. realResult = 0;
  46. imagResult = 0;
  47. for (n = 0; n < numSamples; n++) {
  48. realResult += pSrcA[(2*n)+0] * pSrcB[(2*n)+0] - pSrcA[(2*n)+1] * pSrcB[(2*n)+1];
  49. imagResult += pSrcA[(2*n)+0] * pSrcB[(2*n)+1] + pSrcA[(2*n)+1] * pSrcB[(2*n)+0];
  50. }
  51. </pre>
  52. There are separate functions for floating-point, Q15, and Q31 data types.
  53. */
  54. /**
  55. @addtogroup cmplx_dot_prod
  56. @{
  57. */
  58. /**
  59. @brief Floating-point complex dot product.
  60. @param[in] pSrcA points to the first input vector
  61. @param[in] pSrcB points to the second input vector
  62. @param[in] numSamples number of samples in each vector
  63. @param[out] realResult real part of the result returned here
  64. @param[out] imagResult imaginary part of the result returned here
  65. @return none
  66. */
  67. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  68. #include "arm_helium_utils.h"
  69. void arm_cmplx_dot_prod_f16(
  70. const float16_t * pSrcA,
  71. const float16_t * pSrcB,
  72. uint32_t numSamples,
  73. float16_t * realResult,
  74. float16_t * imagResult)
  75. {
  76. int32_t blkCnt;
  77. float16_t real_sum, imag_sum;
  78. f16x8_t vecSrcA, vecSrcB;
  79. f16x8_t vec_acc = vdupq_n_f16(0.0f16);
  80. f16x8_t vecSrcC, vecSrcD;
  81. blkCnt = (numSamples >> 3);
  82. blkCnt -= 1;
  83. if (blkCnt > 0) {
  84. /* should give more freedom to generate stall free code */
  85. vecSrcA = vld1q( pSrcA);
  86. vecSrcB = vld1q( pSrcB);
  87. pSrcA += 8;
  88. pSrcB += 8;
  89. while (blkCnt > 0) {
  90. vec_acc = vcmlaq(vec_acc, vecSrcA, vecSrcB);
  91. vecSrcC = vld1q(pSrcA);
  92. pSrcA += 8;
  93. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  94. vecSrcD = vld1q(pSrcB);
  95. pSrcB += 8;
  96. vec_acc = vcmlaq(vec_acc, vecSrcC, vecSrcD);
  97. vecSrcA = vld1q(pSrcA);
  98. pSrcA += 8;
  99. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  100. vecSrcB = vld1q(pSrcB);
  101. pSrcB += 8;
  102. /*
  103. * Decrement the blockSize loop counter
  104. */
  105. blkCnt--;
  106. }
  107. /* process last elements out of the loop avoid the armclang breaking the SW pipeline */
  108. vec_acc = vcmlaq(vec_acc, vecSrcA, vecSrcB);
  109. vecSrcC = vld1q(pSrcA);
  110. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  111. vecSrcD = vld1q(pSrcB);
  112. vec_acc = vcmlaq(vec_acc, vecSrcC, vecSrcD);
  113. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  114. /*
  115. * tail
  116. */
  117. blkCnt = CMPLX_DIM * (numSamples & 7);
  118. while (blkCnt > 0) {
  119. mve_pred16_t p = vctp16q(blkCnt);
  120. pSrcA += 8;
  121. pSrcB += 8;
  122. vecSrcA = vldrhq_z_f16(pSrcA, p);
  123. vecSrcB = vldrhq_z_f16(pSrcB, p);
  124. vec_acc = vcmlaq_m(vec_acc, vecSrcA, vecSrcB, p);
  125. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  126. blkCnt -= 8;
  127. }
  128. } else {
  129. /* small vector */
  130. blkCnt = numSamples * CMPLX_DIM;
  131. vec_acc = vdupq_n_f16(0.0f16);
  132. do {
  133. mve_pred16_t p = vctp16q(blkCnt);
  134. vecSrcA = vldrhq_z_f16(pSrcA, p);
  135. vecSrcB = vldrhq_z_f16(pSrcB, p);
  136. vec_acc = vcmlaq_m(vec_acc, vecSrcA, vecSrcB, p);
  137. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  138. /*
  139. * Decrement the blkCnt loop counter
  140. * Advance vector source and destination pointers
  141. */
  142. pSrcA += 8;
  143. pSrcB += 8;
  144. blkCnt -= 8;
  145. }
  146. while (blkCnt > 0);
  147. }
  148. /* Sum the partial parts */
  149. mve_cmplx_sum_intra_r_i_f16(vec_acc, real_sum, imag_sum);
  150. /*
  151. * Store the real and imaginary results in the destination buffers
  152. */
  153. *realResult = real_sum;
  154. *imagResult = imag_sum;
  155. }
  156. #else
  157. void arm_cmplx_dot_prod_f16(
  158. const float16_t * pSrcA,
  159. const float16_t * pSrcB,
  160. uint32_t numSamples,
  161. float16_t * realResult,
  162. float16_t * imagResult)
  163. {
  164. uint32_t blkCnt; /* Loop counter */
  165. _Float16 real_sum = 0.0f, imag_sum = 0.0f; /* Temporary result variables */
  166. _Float16 a0,b0,c0,d0;
  167. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  168. /* Loop unrolling: Compute 4 outputs at a time */
  169. blkCnt = numSamples >> 2U;
  170. while (blkCnt > 0U)
  171. {
  172. a0 = *pSrcA++;
  173. b0 = *pSrcA++;
  174. c0 = *pSrcB++;
  175. d0 = *pSrcB++;
  176. real_sum += a0 * c0;
  177. imag_sum += a0 * d0;
  178. real_sum -= b0 * d0;
  179. imag_sum += b0 * c0;
  180. a0 = *pSrcA++;
  181. b0 = *pSrcA++;
  182. c0 = *pSrcB++;
  183. d0 = *pSrcB++;
  184. real_sum += a0 * c0;
  185. imag_sum += a0 * d0;
  186. real_sum -= b0 * d0;
  187. imag_sum += b0 * c0;
  188. a0 = *pSrcA++;
  189. b0 = *pSrcA++;
  190. c0 = *pSrcB++;
  191. d0 = *pSrcB++;
  192. real_sum += a0 * c0;
  193. imag_sum += a0 * d0;
  194. real_sum -= b0 * d0;
  195. imag_sum += b0 * c0;
  196. a0 = *pSrcA++;
  197. b0 = *pSrcA++;
  198. c0 = *pSrcB++;
  199. d0 = *pSrcB++;
  200. real_sum += a0 * c0;
  201. imag_sum += a0 * d0;
  202. real_sum -= b0 * d0;
  203. imag_sum += b0 * c0;
  204. /* Decrement loop counter */
  205. blkCnt--;
  206. }
  207. /* Loop unrolling: Compute remaining outputs */
  208. blkCnt = numSamples % 0x4U;
  209. #else
  210. /* Initialize blkCnt with number of samples */
  211. blkCnt = numSamples;
  212. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  213. while (blkCnt > 0U)
  214. {
  215. a0 = *pSrcA++;
  216. b0 = *pSrcA++;
  217. c0 = *pSrcB++;
  218. d0 = *pSrcB++;
  219. real_sum += a0 * c0;
  220. imag_sum += a0 * d0;
  221. real_sum -= b0 * d0;
  222. imag_sum += b0 * c0;
  223. /* Decrement loop counter */
  224. blkCnt--;
  225. }
  226. /* Store real and imaginary result in destination buffer. */
  227. *realResult = real_sum;
  228. *imagResult = imag_sum;
  229. }
  230. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  231. /**
  232. @} end of cmplx_dot_prod group
  233. */
  234. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */