arm_dot_prod_q7.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_q7.c
  4. * Description: Q7 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. @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. void arm_dot_prod_q7(
  51. const q7_t * pSrcA,
  52. const q7_t * pSrcB,
  53. uint32_t blockSize,
  54. q31_t * result)
  55. {
  56. uint32_t blkCnt; /* Loop counter */
  57. q31_t sum = 0; /* Temporary return variable */
  58. #if defined (ARM_MATH_LOOPUNROLL)
  59. #if defined (ARM_MATH_DSP)
  60. q31_t input1, input2; /* Temporary variables */
  61. q31_t inA1, inA2, inB1, inB2; /* Temporary variables */
  62. #endif
  63. /* Loop unrolling: Compute 4 outputs at a time */
  64. blkCnt = blockSize >> 2U;
  65. while (blkCnt > 0U)
  66. {
  67. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  68. #if defined (ARM_MATH_DSP)
  69. /* read 4 samples at a time from sourceA */
  70. input1 = read_q7x4_ia ((q7_t **) &pSrcA);
  71. /* read 4 samples at a time from sourceB */
  72. input2 = read_q7x4_ia ((q7_t **) &pSrcB);
  73. /* extract two q7_t samples to q15_t samples */
  74. inA1 = __SXTB16(__ROR(input1, 8));
  75. /* extract reminaing two samples */
  76. inA2 = __SXTB16(input1);
  77. /* extract two q7_t samples to q15_t samples */
  78. inB1 = __SXTB16(__ROR(input2, 8));
  79. /* extract reminaing two samples */
  80. inB2 = __SXTB16(input2);
  81. /* multiply and accumulate two samples at a time */
  82. sum = __SMLAD(inA1, inB1, sum);
  83. sum = __SMLAD(inA2, inB2, sum);
  84. #else
  85. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  86. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  87. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  88. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  89. #endif
  90. /* Decrement loop counter */
  91. blkCnt--;
  92. }
  93. /* Loop unrolling: Compute remaining outputs */
  94. blkCnt = blockSize % 0x4U;
  95. #else
  96. /* Initialize blkCnt with number of samples */
  97. blkCnt = blockSize;
  98. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  99. while (blkCnt > 0U)
  100. {
  101. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  102. /* Calculate dot product and store result in a temporary buffer. */
  103. //#if defined (ARM_MATH_DSP)
  104. // sum = __SMLAD(*pSrcA++, *pSrcB++, sum);
  105. //#else
  106. sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
  107. //#endif
  108. /* Decrement loop counter */
  109. blkCnt--;
  110. }
  111. /* Store result in destination buffer in 18.14 format */
  112. *result = sum;
  113. }
  114. /**
  115. @} end of BasicDotProd group
  116. */