arm_dot_prod_f16.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_f16.c
  4. * Description: Floating-point dot product
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/basic_math_functions_f16.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_MVE_FLOAT16) && !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. #if defined(ARM_FLOAT16_SUPPORTED)
  100. void arm_dot_prod_f16(
  101. const float16_t * pSrcA,
  102. const float16_t * pSrcB,
  103. uint32_t blockSize,
  104. float16_t * result)
  105. {
  106. uint32_t blkCnt; /* Loop counter */
  107. _Float16 sum = 0.0f; /* Temporary return variable */
  108. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  109. /* Loop unrolling: Compute 4 outputs at a time */
  110. blkCnt = blockSize >> 2U;
  111. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  112. ** a second loop below computes the remaining 1 to 3 samples. */
  113. while (blkCnt > 0U)
  114. {
  115. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  116. /* Calculate dot product and store result in a temporary buffer. */
  117. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  118. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  119. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  120. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  121. /* Decrement loop counter */
  122. blkCnt--;
  123. }
  124. /* Loop unrolling: Compute remaining outputs */
  125. blkCnt = blockSize % 0x4U;
  126. #else
  127. /* Initialize blkCnt with number of samples */
  128. blkCnt = blockSize;
  129. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  130. while (blkCnt > 0U)
  131. {
  132. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  133. /* Calculate dot product and store result in a temporary buffer. */
  134. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  135. /* Decrement loop counter */
  136. blkCnt--;
  137. }
  138. /* Store result in destination buffer */
  139. *result = sum;
  140. }
  141. #endif
  142. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  143. /**
  144. @} end of BasicDotProd group
  145. */