arm_dot_prod_q15.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_q15.c
  4. * Description: Q15 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.h"
  29. /**
  30. @ingroup groupMath
  31. */
  32. /**
  33. @addtogroup BasicDotProd
  34. @{
  35. */
  36. /**
  37. @brief Dot product of Q15 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. @return none
  43. @par Scaling and Overflow Behavior
  44. The intermediate multiplications are in 1.15 x 1.15 = 2.30 format and these
  45. results are added to a 64-bit accumulator in 34.30 format.
  46. Nonsaturating additions are used and given that there are 33 guard bits in the accumulator
  47. there is no risk of overflow.
  48. The return result is in 34.30 format.
  49. */
  50. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  51. #include "arm_helium_utils.h"
  52. void arm_dot_prod_q15(
  53. const q15_t * pSrcA,
  54. const q15_t * pSrcB,
  55. uint32_t blockSize,
  56. q63_t * result)
  57. {
  58. uint32_t blkCnt; /* loop counters */
  59. q15x8_t vecA;
  60. q15x8_t vecB;
  61. q63_t sum = 0LL;
  62. /* Compute 8 outputs at a time */
  63. blkCnt = blockSize >> 3;
  64. while (blkCnt > 0U)
  65. {
  66. /*
  67. * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
  68. * Calculate dot product and then store the result in a temporary buffer.
  69. */
  70. vecA = vld1q(pSrcA);
  71. vecB = vld1q(pSrcB);
  72. sum = vmlaldavaq(sum, vecA, vecB);
  73. /*
  74. * Decrement the blockSize loop counter
  75. */
  76. blkCnt--;
  77. /*
  78. * advance vector source and destination pointers
  79. */
  80. pSrcA += 8;
  81. pSrcB += 8;
  82. }
  83. /*
  84. * tail
  85. */
  86. blkCnt = blockSize & 7;
  87. if (blkCnt > 0U)
  88. {
  89. mve_pred16_t p0 = vctp16q(blkCnt);
  90. vecA = vld1q(pSrcA);
  91. vecB = vld1q(pSrcB);
  92. sum = vmlaldavaq_p(sum, vecA, vecB, p0);
  93. }
  94. *result = sum;
  95. }
  96. #else
  97. void arm_dot_prod_q15(
  98. const q15_t * pSrcA,
  99. const q15_t * pSrcB,
  100. uint32_t blockSize,
  101. q63_t * result)
  102. {
  103. uint32_t blkCnt; /* Loop counter */
  104. q63_t sum = 0; /* Temporary return variable */
  105. #if defined (ARM_MATH_LOOPUNROLL)
  106. /* Loop unrolling: Compute 4 outputs at a time */
  107. blkCnt = blockSize >> 2U;
  108. while (blkCnt > 0U)
  109. {
  110. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  111. #if defined (ARM_MATH_DSP)
  112. /* Calculate dot product and store result in a temporary buffer. */
  113. sum = __SMLALD(read_q15x2_ia (&pSrcA), read_q15x2_ia (&pSrcB), sum);
  114. sum = __SMLALD(read_q15x2_ia (&pSrcA), read_q15x2_ia (&pSrcB), sum);
  115. #else
  116. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  117. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  118. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  119. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  120. #endif
  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. //#if defined (ARM_MATH_DSP)
  135. // sum = __SMLALD(*pSrcA++, *pSrcB++, sum);
  136. //#else
  137. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  138. //#endif
  139. /* Decrement loop counter */
  140. blkCnt--;
  141. }
  142. /* Store result in destination buffer in 34.30 format */
  143. *result = sum;
  144. }
  145. #endif /* defined(ARM_MATH_MVEI) */
  146. /**
  147. @} end of BasicDotProd group
  148. */