arm_dot_prod_f32.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. float32x2_t accum = vdup_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. res = vmulq_f32(vec1, vec2);
  75. accum = vadd_f32(accum, vpadd_f32(vget_low_f32(res), vget_high_f32(res)));
  76. /* Increment pointers */
  77. pSrcA += 4;
  78. pSrcB += 4;
  79. vec1 = vld1q_f32(pSrcA);
  80. vec2 = vld1q_f32(pSrcB);
  81. /* Decrement the loop counter */
  82. blkCnt--;
  83. }
  84. sum += accum[0] + accum[1];
  85. /* Tail */
  86. blkCnt = blockSize & 0x3;
  87. #else
  88. #if defined (ARM_MATH_LOOPUNROLL)
  89. /* Loop unrolling: Compute 4 outputs at a time */
  90. blkCnt = blockSize >> 2U;
  91. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  92. ** a second loop below computes the remaining 1 to 3 samples. */
  93. while (blkCnt > 0U)
  94. {
  95. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  96. /* Calculate dot product and store result in a temporary buffer. */
  97. sum += (*pSrcA++) * (*pSrcB++);
  98. sum += (*pSrcA++) * (*pSrcB++);
  99. sum += (*pSrcA++) * (*pSrcB++);
  100. sum += (*pSrcA++) * (*pSrcB++);
  101. /* Decrement loop counter */
  102. blkCnt--;
  103. }
  104. /* Loop unrolling: Compute remaining outputs */
  105. blkCnt = blockSize % 0x4U;
  106. #else
  107. /* Initialize blkCnt with number of samples */
  108. blkCnt = blockSize;
  109. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  110. #endif /* #if defined(ARM_MATH_NEON) */
  111. while (blkCnt > 0U)
  112. {
  113. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  114. /* Calculate dot product and store result in a temporary buffer. */
  115. sum += (*pSrcA++) * (*pSrcB++);
  116. /* Decrement loop counter */
  117. blkCnt--;
  118. }
  119. /* Store result in destination buffer */
  120. *result = sum;
  121. }
  122. /**
  123. @} end of BasicDotProd group
  124. */