arm_dot_prod_q15.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dot_prod_q15.c
  4. * Description: Q15 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 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. void arm_dot_prod_q15(
  51. const q15_t * pSrcA,
  52. const q15_t * pSrcB,
  53. uint32_t blockSize,
  54. q63_t * result)
  55. {
  56. uint32_t blkCnt; /* Loop counter */
  57. q63_t sum = 0; /* Temporary return variable */
  58. #if defined (ARM_MATH_LOOPUNROLL)
  59. /* Loop unrolling: Compute 4 outputs at a time */
  60. blkCnt = blockSize >> 2U;
  61. while (blkCnt > 0U)
  62. {
  63. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  64. #if defined (ARM_MATH_DSP)
  65. /* Calculate dot product and store result in a temporary buffer. */
  66. sum = __SMLALD(read_q15x2_ia ((q15_t **) &pSrcA), read_q15x2_ia ((q15_t **) &pSrcB), sum);
  67. sum = __SMLALD(read_q15x2_ia ((q15_t **) &pSrcA), read_q15x2_ia ((q15_t **) &pSrcB), sum);
  68. #else
  69. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  70. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  71. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  72. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  73. #endif
  74. /* Decrement loop counter */
  75. blkCnt--;
  76. }
  77. /* Loop unrolling: Compute remaining outputs */
  78. blkCnt = blockSize % 0x4U;
  79. #else
  80. /* Initialize blkCnt with number of samples */
  81. blkCnt = blockSize;
  82. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  83. while (blkCnt > 0U)
  84. {
  85. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  86. /* Calculate dot product and store result in a temporary buffer. */
  87. //#if defined (ARM_MATH_DSP)
  88. // sum = __SMLALD(*pSrcA++, *pSrcB++, sum);
  89. //#else
  90. sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
  91. //#endif
  92. /* Decrement loop counter */
  93. blkCnt--;
  94. }
  95. /* Store result in destination buffer in 34.30 format */
  96. *result = sum;
  97. }
  98. /**
  99. @} end of BasicDotProd group
  100. */