arm_cmplx_dot_prod_f16.c 6.6 KB

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