arm_dot_prod_f32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_f32.c
  4. * Description: Floating-point 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 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. @return none
  52. */
  53. #if defined (ARM_MATH_HELIUM)
  54. #include "arm_mve.h"
  55. #include "arm_helium_utils.h"
  56. void arm_dot_prod_f32(
  57. const float32_t * pSrcA,
  58. const float32_t * pSrcB,
  59. uint32_t blockSize,
  60. float32_t * result)
  61. {
  62. float32x4_t vecA, vecB;
  63. float32x4_t vecSum;
  64. vecSum = vdupq_n_f32(0.0);
  65. do {
  66. /*
  67. * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
  68. * Calculate dot product and then store the result in a temporary buffer.
  69. */
  70. mve_pred16_t p = vctp32q(blockSize);
  71. vecA = vldrwq_z_f32(pSrcA, p);
  72. vecB = vldrwq_z_f32(pSrcB, p);
  73. vecSum = vfmaq_m(vecSum, vecA, vecB, p);
  74. /*
  75. * Decrement the blockSize loop counter
  76. * Advance vector source and destination pointers
  77. */
  78. post_incr_vec_size(pSrcA);
  79. post_incr_vec_size(pSrcB);
  80. blockSize -= VEC_LANES_F32;
  81. }
  82. while ((int32_t) blockSize > 0);
  83. *result = vecAddAcrossF32Mve(vecSum);
  84. }
  85. #else
  86. void arm_dot_prod_f32(
  87. const float32_t * pSrcA,
  88. const float32_t * pSrcB,
  89. uint32_t blockSize,
  90. float32_t * result)
  91. {
  92. uint32_t blkCnt; /* Loop counter */
  93. float32_t sum = 0.0f; /* Temporary return variable */
  94. #if defined(ARM_MATH_NEON)
  95. float32x4_t vec1;
  96. float32x4_t vec2;
  97. float32x4_t res;
  98. float32x4_t accum = vdupq_n_f32(0);
  99. /* Compute 4 outputs at a time */
  100. blkCnt = blockSize >> 2U;
  101. vec1 = vld1q_f32(pSrcA);
  102. vec2 = vld1q_f32(pSrcB);
  103. while (blkCnt > 0U)
  104. {
  105. /* C = A[0]*B[0] + A[1]*B[1] + A[2]*B[2] + ... + A[blockSize-1]*B[blockSize-1] */
  106. /* Calculate dot product and then store the result in a temporary buffer. */
  107. accum = vmlaq_f32(accum, vec1, vec2);
  108. /* Increment pointers */
  109. pSrcA += 4;
  110. pSrcB += 4;
  111. vec1 = vld1q_f32(pSrcA);
  112. vec2 = vld1q_f32(pSrcB);
  113. /* Decrement the loop counter */
  114. blkCnt--;
  115. }
  116. #if __aarch64__
  117. sum = vpadds_f32(vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)));
  118. #else
  119. sum = (vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)))[0] + (vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)))[1];
  120. #endif
  121. /* Tail */
  122. blkCnt = blockSize & 0x3;
  123. #else
  124. #if defined (ARM_MATH_LOOPUNROLL)
  125. /* Loop unrolling: Compute 4 outputs at a time */
  126. blkCnt = blockSize >> 2U;
  127. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  128. ** a second loop below computes the remaining 1 to 3 samples. */
  129. while (blkCnt > 0U)
  130. {
  131. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  132. /* Calculate dot product and store result in a temporary buffer. */
  133. sum += (*pSrcA++) * (*pSrcB++);
  134. sum += (*pSrcA++) * (*pSrcB++);
  135. sum += (*pSrcA++) * (*pSrcB++);
  136. sum += (*pSrcA++) * (*pSrcB++);
  137. /* Decrement loop counter */
  138. blkCnt--;
  139. }
  140. /* Loop unrolling: Compute remaining outputs */
  141. blkCnt = blockSize % 0x4U;
  142. #else
  143. /* Initialize blkCnt with number of samples */
  144. blkCnt = blockSize;
  145. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  146. #endif /* #if defined(ARM_MATH_NEON) */
  147. while (blkCnt > 0U)
  148. {
  149. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  150. /* Calculate dot product and store result in a temporary buffer. */
  151. sum += (*pSrcA++) * (*pSrcB++);
  152. /* Decrement loop counter */
  153. blkCnt--;
  154. }
  155. /* Store result in destination buffer */
  156. *result = sum;
  157. }
  158. #endif /* ARM_MATH_HELIUM */
  159. /**
  160. @} end of BasicDotProd group
  161. */