arm_dot_prod_f32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_LOOPUNROLL)
  62. /* Loop unrolling: Compute 4 outputs at a time */
  63. blkCnt = blockSize >> 2U;
  64. while (blkCnt > 0U)
  65. {
  66. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  67. /* Calculate dot product and store result in a temporary buffer. */
  68. sum += (*pSrcA++) * (*pSrcB++);
  69. sum += (*pSrcA++) * (*pSrcB++);
  70. sum += (*pSrcA++) * (*pSrcB++);
  71. sum += (*pSrcA++) * (*pSrcB++);
  72. /* Decrement loop counter */
  73. blkCnt--;
  74. }
  75. /* Loop unrolling: Compute remaining outputs */
  76. blkCnt = blockSize % 0x4U;
  77. #else
  78. /* Initialize blkCnt with number of samples */
  79. blkCnt = blockSize;
  80. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  81. while (blkCnt > 0U)
  82. {
  83. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  84. /* Calculate dot product and store result in a temporary buffer. */
  85. sum += (*pSrcA++) * (*pSrcB++);
  86. /* Decrement loop counter */
  87. blkCnt--;
  88. }
  89. /* Store result in destination buffer */
  90. *result = sum;
  91. }
  92. /**
  93. @} end of BasicDotProd group
  94. */