arm_dot_prod_f32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. void arm_dot_prod_f32(
  54. const float32_t * pSrcA,
  55. const float32_t * pSrcB,
  56. uint32_t blockSize,
  57. float32_t * result)
  58. {
  59. uint32_t blkCnt; /* Loop counter */
  60. float32_t sum = 0.0f; /* Temporary return variable */
  61. #if defined(ARM_MATH_NEON)
  62. float32x4_t vec1;
  63. float32x4_t vec2;
  64. float32x4_t res;
  65. float32x4_t accum = vdupq_n_f32(0);
  66. /* Compute 4 outputs at a time */
  67. blkCnt = blockSize >> 2U;
  68. vec1 = vld1q_f32(pSrcA);
  69. vec2 = vld1q_f32(pSrcB);
  70. while (blkCnt > 0U)
  71. {
  72. /* C = A[0]*B[0] + A[1]*B[1] + A[2]*B[2] + ... + A[blockSize-1]*B[blockSize-1] */
  73. /* Calculate dot product and then store the result in a temporary buffer. */
  74. accum = vmlaq_f32(accum, vec1, vec2);
  75. /* Increment pointers */
  76. pSrcA += 4;
  77. pSrcB += 4;
  78. vec1 = vld1q_f32(pSrcA);
  79. vec2 = vld1q_f32(pSrcB);
  80. /* Decrement the loop counter */
  81. blkCnt--;
  82. }
  83. #if __aarch64__
  84. sum = vpadds_f32(vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)));
  85. #else
  86. sum = (vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)))[0] + (vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)))[1];
  87. #endif
  88. /* Tail */
  89. blkCnt = blockSize & 0x3;
  90. #else
  91. #if defined (ARM_MATH_LOOPUNROLL)
  92. /* Loop unrolling: Compute 4 outputs at a time */
  93. blkCnt = blockSize >> 2U;
  94. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  95. ** a second loop below computes the remaining 1 to 3 samples. */
  96. while (blkCnt > 0U)
  97. {
  98. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  99. /* Calculate dot product and store result in a temporary buffer. */
  100. sum += (*pSrcA++) * (*pSrcB++);
  101. sum += (*pSrcA++) * (*pSrcB++);
  102. sum += (*pSrcA++) * (*pSrcB++);
  103. sum += (*pSrcA++) * (*pSrcB++);
  104. /* Decrement loop counter */
  105. blkCnt--;
  106. }
  107. /* Loop unrolling: Compute remaining outputs */
  108. blkCnt = blockSize % 0x4U;
  109. #else
  110. /* Initialize blkCnt with number of samples */
  111. blkCnt = blockSize;
  112. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  113. #endif /* #if defined(ARM_MATH_NEON) */
  114. while (blkCnt > 0U)
  115. {
  116. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  117. /* Calculate dot product and store result in a temporary buffer. */
  118. sum += (*pSrcA++) * (*pSrcB++);
  119. /* Decrement loop counter */
  120. blkCnt--;
  121. }
  122. /* Store result in destination buffer */
  123. *result = sum;
  124. }
  125. /**
  126. @} end of BasicDotProd group
  127. */