arm_dot_prod_f16.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_f16.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_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  54. #include "arm_helium_utils.h"
  55. void arm_dot_prod_f16(
  56. const float16_t * pSrcA,
  57. const float16_t * pSrcB,
  58. uint32_t blockSize,
  59. float16_t * result)
  60. {
  61. f16x8_t vecA, vecB;
  62. f16x8_t vecSum;
  63. uint32_t blkCnt;
  64. float16_t sum = 0.0f;
  65. vecSum = vdupq_n_f16(0.0f);
  66. /* Compute 4 outputs at a time */
  67. blkCnt = blockSize >> 3U;
  68. while (blkCnt > 0U)
  69. {
  70. /*
  71. * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
  72. * Calculate dot product and then store the result in a temporary buffer.
  73. * and advance vector source and destination pointers
  74. */
  75. vecA = vld1q(pSrcA);
  76. pSrcA += 8;
  77. vecB = vld1q(pSrcB);
  78. pSrcB += 8;
  79. vecSum = vfmaq(vecSum, vecA, vecB);
  80. /*
  81. * Decrement the blockSize loop counter
  82. */
  83. blkCnt --;
  84. }
  85. blkCnt = blockSize & 7;
  86. if (blkCnt > 0U)
  87. {
  88. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  89. mve_pred16_t p0 = vctp16q(blkCnt);
  90. vecA = vld1q(pSrcA);
  91. vecB = vld1q(pSrcB);
  92. vecSum = vfmaq_m(vecSum, vecA, vecB, p0);
  93. }
  94. sum = vecAddAcrossF16Mve(vecSum);
  95. /* Store result in destination buffer */
  96. *result = sum;
  97. }
  98. #else
  99. void arm_dot_prod_f16(
  100. const float16_t * pSrcA,
  101. const float16_t * pSrcB,
  102. uint32_t blockSize,
  103. float16_t * result)
  104. {
  105. uint32_t blkCnt; /* Loop counter */
  106. float16_t sum = 0.0f; /* Temporary return variable */
  107. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  108. /* Loop unrolling: Compute 4 outputs at a time */
  109. blkCnt = blockSize >> 2U;
  110. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  111. ** a second loop below computes the remaining 1 to 3 samples. */
  112. while (blkCnt > 0U)
  113. {
  114. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  115. /* Calculate dot product and store result in a temporary buffer. */
  116. sum += (*pSrcA++) * (*pSrcB++);
  117. sum += (*pSrcA++) * (*pSrcB++);
  118. sum += (*pSrcA++) * (*pSrcB++);
  119. sum += (*pSrcA++) * (*pSrcB++);
  120. /* Decrement loop counter */
  121. blkCnt--;
  122. }
  123. /* Loop unrolling: Compute remaining outputs */
  124. blkCnt = blockSize % 0x4U;
  125. #else
  126. /* Initialize blkCnt with number of samples */
  127. blkCnt = blockSize;
  128. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  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. /* Decrement loop counter */
  135. blkCnt--;
  136. }
  137. /* Store result in destination buffer */
  138. *result = sum;
  139. }
  140. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  141. /**
  142. @} end of BasicDotProd group
  143. */