arm_mat_mult_q31.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_q31.c
  4. * Description: Q31 matrix multiplication
  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 groupMatrix
  31. */
  32. /**
  33. @addtogroup MatrixMult
  34. @{
  35. */
  36. /**
  37. @brief Q31 matrix multiplication.
  38. @param[in] pSrcA points to the first input matrix structure
  39. @param[in] pSrcB points to the second input matrix structure
  40. @param[out] pDst points to output matrix structure
  41. @return execution status
  42. - \ref ARM_MATH_SUCCESS : Operation successful
  43. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  44. @par Scaling and Overflow Behavior
  45. The function is implemented using an internal 64-bit accumulator.
  46. The accumulator has a 2.62 format and maintains full precision of the intermediate
  47. multiplication results but provides only a single guard bit. There is no saturation
  48. on intermediate additions. Thus, if the accumulator overflows it wraps around and
  49. distorts the result. The input signals should be scaled down to avoid intermediate
  50. overflows. The input is thus scaled down by log2(numColsA) bits
  51. to avoid overflows, as a total of numColsA additions are performed internally.
  52. The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  53. @remark
  54. Refer to \ref arm_mat_mult_fast_q31() for a faster but less precise implementation of this function.
  55. */
  56. arm_status arm_mat_mult_q31(
  57. const arm_matrix_instance_q31 * pSrcA,
  58. const arm_matrix_instance_q31 * pSrcB,
  59. arm_matrix_instance_q31 * pDst)
  60. {
  61. q31_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  62. q31_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  63. q31_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  64. q31_t *pInB = pSrcB->pData; /* Input data matrix pointer B */
  65. q31_t *pOut = pDst->pData; /* Output data matrix pointer */
  66. q31_t *px; /* Temporary output data matrix pointer */
  67. q63_t sum; /* Accumulator */
  68. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  69. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  70. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  71. uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  72. arm_status status; /* Status of matrix multiplication */
  73. #ifdef ARM_MATH_MATRIX_CHECK
  74. /* Check for matrix mismatch condition */
  75. if ((pSrcA->numCols != pSrcB->numRows) ||
  76. (pSrcA->numRows != pDst->numRows) ||
  77. (pSrcB->numCols != pDst->numCols) )
  78. {
  79. /* Set status as ARM_MATH_SIZE_MISMATCH */
  80. status = ARM_MATH_SIZE_MISMATCH;
  81. }
  82. else
  83. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  84. {
  85. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  86. /* row loop */
  87. do
  88. {
  89. /* Output pointer is set to starting address of row being processed */
  90. px = pOut + i;
  91. /* For every row wise process, column loop counter is to be initiated */
  92. col = numColsB;
  93. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  94. pIn2 = pSrcB->pData;
  95. /* column loop */
  96. do
  97. {
  98. /* Set the variable sum, that acts as accumulator, to zero */
  99. sum = 0;
  100. /* Initialize pointer pIn1 to point to starting address of column being processed */
  101. pIn1 = pInA;
  102. #if defined (ARM_MATH_LOOPUNROLL)
  103. /* Loop unrolling: Compute 4 MACs at a time. */
  104. colCnt = numColsA >> 2U;
  105. /* matrix multiplication */
  106. while (colCnt > 0U)
  107. {
  108. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  109. /* Perform the multiply-accumulates */
  110. sum += (q63_t) *pIn1++ * *pIn2;
  111. pIn2 += numColsB;
  112. sum += (q63_t) *pIn1++ * *pIn2;
  113. pIn2 += numColsB;
  114. sum += (q63_t) *pIn1++ * *pIn2;
  115. pIn2 += numColsB;
  116. sum += (q63_t) *pIn1++ * *pIn2;
  117. pIn2 += numColsB;
  118. /* Decrement loop counter */
  119. colCnt--;
  120. }
  121. /* Loop unrolling: Compute remaining MACs */
  122. colCnt = numColsA % 0x4U;
  123. #else
  124. /* Initialize cntCnt with number of columns */
  125. colCnt = numColsA;
  126. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  127. while (colCnt > 0U)
  128. {
  129. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  130. /* Perform the multiply-accumulates */
  131. sum += (q63_t) *pIn1++ * *pIn2;
  132. pIn2 += numColsB;
  133. /* Decrement loop counter */
  134. colCnt--;
  135. }
  136. /* Convert result from 2.62 to 1.31 format and store in destination buffer */
  137. *px++ = (q31_t) (sum >> 31);
  138. /* Decrement column loop counter */
  139. col--;
  140. /* Update pointer pIn2 to point to starting address of next column */
  141. pIn2 = pInB + (numColsB - col);
  142. } while (col > 0U);
  143. /* Update pointer pInA to point to starting address of next row */
  144. i = i + numColsB;
  145. pInA = pInA + numColsA;
  146. /* Decrement row loop counter */
  147. row--;
  148. } while (row > 0U);
  149. /* Set status as ARM_MATH_SUCCESS */
  150. status = ARM_MATH_SUCCESS;
  151. }
  152. /* Return to application */
  153. return (status);
  154. }
  155. /**
  156. @} end of MatrixMult group
  157. */