arm_dot_prod_f32.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_f32.c
  4. * Description: Floating-point dot product
  5. *
  6. * $Date: 05 October 2021
  7. * $Revision: V1.9.1
  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/basic_math_functions.h"
  29. /**
  30. @ingroup groupMath
  31. */
  32. /**
  33. @defgroup BasicDotProd Vector Dot Product
  34. Computes the dot product of two vectors.
  35. The vectors are multiplied element-by-element and then summed.
  36. <pre>
  37. sum = pSrcA[0]*pSrcB[0] + pSrcA[1]*pSrcB[1] + ... + pSrcA[blockSize-1]*pSrcB[blockSize-1]
  38. </pre>
  39. There are separate functions for floating-point, Q7, Q15, and Q31 data types.
  40. */
  41. /**
  42. @addtogroup BasicDotProd
  43. @{
  44. */
  45. /**
  46. @brief Dot product of floating-point vectors.
  47. @param[in] pSrcA points to the first input vector.
  48. @param[in] pSrcB points to the second input vector.
  49. @param[in] blockSize number of samples in each vector.
  50. @param[out] result output result returned here.
  51. */
  52. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  53. #include "arm_helium_utils.h"
  54. void arm_dot_prod_f32(
  55. const float32_t * pSrcA,
  56. const float32_t * pSrcB,
  57. uint32_t blockSize,
  58. float32_t * result)
  59. {
  60. f32x4_t vecA, vecB;
  61. f32x4_t vecSum;
  62. uint32_t blkCnt;
  63. float32_t sum = 0.0f;
  64. vecSum = vdupq_n_f32(0.0f);
  65. /* Compute 4 outputs at a time */
  66. blkCnt = blockSize >> 2U;
  67. while (blkCnt > 0U)
  68. {
  69. /*
  70. * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
  71. * Calculate dot product and then store the result in a temporary buffer.
  72. * and advance vector source and destination pointers
  73. */
  74. vecA = vld1q(pSrcA);
  75. pSrcA += 4;
  76. vecB = vld1q(pSrcB);
  77. pSrcB += 4;
  78. vecSum = vfmaq(vecSum, vecA, vecB);
  79. /*
  80. * Decrement the blockSize loop counter
  81. */
  82. blkCnt --;
  83. }
  84. blkCnt = blockSize & 3;
  85. if (blkCnt > 0U)
  86. {
  87. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  88. mve_pred16_t p0 = vctp32q(blkCnt);
  89. vecA = vld1q(pSrcA);
  90. vecB = vld1q(pSrcB);
  91. vecSum = vfmaq_m(vecSum, vecA, vecB, p0);
  92. }
  93. sum = vecAddAcrossF32Mve(vecSum);
  94. /* Store result in destination buffer */
  95. *result = sum;
  96. }
  97. #else
  98. void arm_dot_prod_f32(
  99. const float32_t * pSrcA,
  100. const float32_t * pSrcB,
  101. uint32_t blockSize,
  102. float32_t * result)
  103. {
  104. uint32_t blkCnt; /* Loop counter */
  105. float32_t sum = 0.0f; /* Temporary return variable */
  106. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  107. f32x4_t vec1;
  108. f32x4_t vec2;
  109. f32x4_t accum = vdupq_n_f32(0);
  110. #if !defined(__aarch64__)
  111. f32x2_t tmp = vdup_n_f32(0);
  112. #endif
  113. /* Compute 4 outputs at a time */
  114. blkCnt = blockSize >> 2U;
  115. vec1 = vld1q_f32(pSrcA);
  116. vec2 = vld1q_f32(pSrcB);
  117. while (blkCnt > 0U)
  118. {
  119. /* C = A[0]*B[0] + A[1]*B[1] + A[2]*B[2] + ... + A[blockSize-1]*B[blockSize-1] */
  120. /* Calculate dot product and then store the result in a temporary buffer. */
  121. accum = vmlaq_f32(accum, vec1, vec2);
  122. /* Increment pointers */
  123. pSrcA += 4;
  124. pSrcB += 4;
  125. vec1 = vld1q_f32(pSrcA);
  126. vec2 = vld1q_f32(pSrcB);
  127. /* Decrement the loop counter */
  128. blkCnt--;
  129. }
  130. #if defined(__aarch64__)
  131. sum = vpadds_f32(vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)));
  132. #else
  133. tmp = vpadd_f32(vget_low_f32(accum), vget_high_f32(accum));
  134. sum = vget_lane_f32(tmp, 0) + vget_lane_f32(tmp, 1);
  135. #endif
  136. /* Tail */
  137. blkCnt = blockSize & 0x3;
  138. #else
  139. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  140. /* Loop unrolling: Compute 4 outputs at a time */
  141. blkCnt = blockSize >> 2U;
  142. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  143. ** a second loop below computes the remaining 1 to 3 samples. */
  144. while (blkCnt > 0U)
  145. {
  146. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  147. /* Calculate dot product and store result in a temporary buffer. */
  148. sum += (*pSrcA++) * (*pSrcB++);
  149. sum += (*pSrcA++) * (*pSrcB++);
  150. sum += (*pSrcA++) * (*pSrcB++);
  151. sum += (*pSrcA++) * (*pSrcB++);
  152. /* Decrement loop counter */
  153. blkCnt--;
  154. }
  155. /* Loop unrolling: Compute remaining outputs */
  156. blkCnt = blockSize % 0x4U;
  157. #else
  158. /* Initialize blkCnt with number of samples */
  159. blkCnt = blockSize;
  160. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  161. #endif /* #if defined(ARM_MATH_NEON) */
  162. while (blkCnt > 0U)
  163. {
  164. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  165. /* Calculate dot product and store result in a temporary buffer. */
  166. sum += (*pSrcA++) * (*pSrcB++);
  167. /* Decrement loop counter */
  168. blkCnt--;
  169. }
  170. /* Store result in destination buffer */
  171. *result = sum;
  172. }
  173. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  174. /**
  175. @} end of BasicDotProd group
  176. */