arm_dot_prod_q7.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_q7.c
  4. * Description: Q7 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 Q7 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.7 x 1.7 = 2.14 format and these
  45. results are added to an accumulator in 18.14 format.
  46. Nonsaturating additions are used and there is no danger of wrap around as long as
  47. the vectors are less than 2^18 elements long.
  48. The return result is in 18.14 format.
  49. */
  50. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  51. #include "arm_helium_utils.h"
  52. void arm_dot_prod_q7(
  53. const q7_t * pSrcA,
  54. const q7_t * pSrcB,
  55. uint32_t blockSize,
  56. q31_t * result)
  57. {
  58. uint32_t blkCnt; /* loop counters */
  59. q7x16_t vecA;
  60. q7x16_t vecB;
  61. q31_t sum = 0;
  62. /* Compute 16 outputs at a time */
  63. blkCnt = blockSize >> 4;
  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 = vmladavaq(sum, vecA, vecB);
  73. /*
  74. * Decrement the blockSize loop counter
  75. */
  76. blkCnt--;
  77. /*
  78. * advance vector source and destination pointers
  79. */
  80. pSrcA += 16;
  81. pSrcB += 16;
  82. }
  83. /*
  84. * tail
  85. */
  86. blkCnt = blockSize & 0xF;
  87. if (blkCnt > 0U)
  88. {
  89. mve_pred16_t p0 = vctp8q(blkCnt);
  90. vecA = vld1q(pSrcA);
  91. vecB = vld1q(pSrcB);
  92. sum = vmladavaq_p(sum, vecA, vecB, p0);
  93. }
  94. *result = sum;
  95. }
  96. #else
  97. void arm_dot_prod_q7(
  98. const q7_t * pSrcA,
  99. const q7_t * pSrcB,
  100. uint32_t blockSize,
  101. q31_t * result)
  102. {
  103. uint32_t blkCnt; /* Loop counter */
  104. q31_t sum = 0; /* Temporary return variable */
  105. #if defined (ARM_MATH_LOOPUNROLL)
  106. #if defined (ARM_MATH_DSP)
  107. q31_t input1, input2; /* Temporary variables */
  108. q31_t inA1, inA2, inB1, inB2; /* Temporary variables */
  109. #endif
  110. /* Loop unrolling: Compute 4 outputs at a time */
  111. blkCnt = blockSize >> 2U;
  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. #if defined (ARM_MATH_DSP)
  116. /* read 4 samples at a time from sourceA */
  117. input1 = read_q7x4_ia (&pSrcA);
  118. /* read 4 samples at a time from sourceB */
  119. input2 = read_q7x4_ia (&pSrcB);
  120. /* extract two q7_t samples to q15_t samples */
  121. inA1 = __SXTB16(__ROR(input1, 8));
  122. /* extract reminaing two samples */
  123. inA2 = __SXTB16(input1);
  124. /* extract two q7_t samples to q15_t samples */
  125. inB1 = __SXTB16(__ROR(input2, 8));
  126. /* extract reminaing two samples */
  127. inB2 = __SXTB16(input2);
  128. /* multiply and accumulate two samples at a time */
  129. sum = __SMLAD(inA1, inB1, sum);
  130. sum = __SMLAD(inA2, inB2, sum);
  131. #else
  132. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  133. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  134. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  135. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  136. #endif
  137. /* Decrement loop counter */
  138. blkCnt--;
  139. }
  140. /* Loop unrolling: Compute remaining outputs */
  141. blkCnt = blockSize % 0x4U;
  142. #else
  143. /* Initialize blkCnt with number of samples */
  144. blkCnt = blockSize;
  145. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  146. while (blkCnt > 0U)
  147. {
  148. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  149. /* Calculate dot product and store result in a temporary buffer. */
  150. //#if defined (ARM_MATH_DSP)
  151. // sum = __SMLAD(*pSrcA++, *pSrcB++, sum);
  152. //#else
  153. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  154. //#endif
  155. /* Decrement loop counter */
  156. blkCnt--;
  157. }
  158. /* Store result in destination buffer in 18.14 format */
  159. *result = sum;
  160. }
  161. #endif /* defined(ARM_MATH_MVEI) */
  162. /**
  163. @} end of BasicDotProd group
  164. */