arm_dot_prod_f16.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. @addtogroup BasicDotProd
  34. @{
  35. */
  36. /**
  37. @brief Dot product of floating-point vectors.
  38. @param[in] pSrcA points to the first input vector.
  39. @param[in] pSrcB points to the second input vector.
  40. @param[in] blockSize number of samples in each vector.
  41. @param[out] result output result returned here.
  42. */
  43. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. void arm_dot_prod_f16(
  46. const float16_t * pSrcA,
  47. const float16_t * pSrcB,
  48. uint32_t blockSize,
  49. float16_t * result)
  50. {
  51. f16x8_t vecA, vecB;
  52. f16x8_t vecSum;
  53. uint32_t blkCnt;
  54. float16_t sum = 0.0f;
  55. vecSum = vdupq_n_f16(0.0f);
  56. /* Compute 4 outputs at a time */
  57. blkCnt = blockSize >> 3U;
  58. while (blkCnt > 0U)
  59. {
  60. /*
  61. * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
  62. * Calculate dot product and then store the result in a temporary buffer.
  63. * and advance vector source and destination pointers
  64. */
  65. vecA = vld1q(pSrcA);
  66. pSrcA += 8;
  67. vecB = vld1q(pSrcB);
  68. pSrcB += 8;
  69. vecSum = vfmaq(vecSum, vecA, vecB);
  70. /*
  71. * Decrement the blockSize loop counter
  72. */
  73. blkCnt --;
  74. }
  75. blkCnt = blockSize & 7;
  76. if (blkCnt > 0U)
  77. {
  78. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  79. mve_pred16_t p0 = vctp16q(blkCnt);
  80. vecA = vld1q(pSrcA);
  81. vecB = vld1q(pSrcB);
  82. vecSum = vfmaq_m(vecSum, vecA, vecB, p0);
  83. }
  84. sum = vecAddAcrossF16Mve(vecSum);
  85. /* Store result in destination buffer */
  86. *result = sum;
  87. }
  88. #else
  89. #if defined(ARM_FLOAT16_SUPPORTED)
  90. void arm_dot_prod_f16(
  91. const float16_t * pSrcA,
  92. const float16_t * pSrcB,
  93. uint32_t blockSize,
  94. float16_t * result)
  95. {
  96. uint32_t blkCnt; /* Loop counter */
  97. _Float16 sum = 0.0f; /* Temporary return variable */
  98. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  99. /* Loop unrolling: Compute 4 outputs at a time */
  100. blkCnt = blockSize >> 2U;
  101. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  102. ** a second loop below computes the remaining 1 to 3 samples. */
  103. while (blkCnt > 0U)
  104. {
  105. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  106. /* Calculate dot product and store result in a temporary buffer. */
  107. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  108. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  109. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  110. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  111. /* Decrement loop counter */
  112. blkCnt--;
  113. }
  114. /* Loop unrolling: Compute remaining outputs */
  115. blkCnt = blockSize % 0x4U;
  116. #else
  117. /* Initialize blkCnt with number of samples */
  118. blkCnt = blockSize;
  119. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  120. while (blkCnt > 0U)
  121. {
  122. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  123. /* Calculate dot product and store result in a temporary buffer. */
  124. sum += (_Float16)(*pSrcA++) * (_Float16)(*pSrcB++);
  125. /* Decrement loop counter */
  126. blkCnt--;
  127. }
  128. /* Store result in destination buffer */
  129. *result = sum;
  130. }
  131. #endif
  132. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  133. /**
  134. @} end of BasicDotProd group
  135. */