arm_mat_mult_q31.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_q31.c
  4. * Description: Q31 matrix multiplication
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 The function returns either
  42. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  43. *
  44. * @details
  45. * <b>Scaling and Overflow Behavior:</b>
  46. *
  47. * \par
  48. * The function is implemented using an internal 64-bit accumulator.
  49. * The accumulator has a 2.62 format and maintains full precision of the intermediate
  50. * multiplication results but provides only a single guard bit. There is no saturation
  51. * on intermediate additions. Thus, if the accumulator overflows it wraps around and
  52. * distorts the result. The input signals should be scaled down to avoid intermediate
  53. * overflows. The input is thus scaled down by log2(numColsA) bits
  54. * to avoid overflows, as a total of numColsA additions are performed internally.
  55. * The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  56. *
  57. * \par
  58. * See <code>arm_mat_mult_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
  59. *
  60. */
  61. arm_status arm_mat_mult_q31(
  62. const arm_matrix_instance_q31 * pSrcA,
  63. const arm_matrix_instance_q31 * pSrcB,
  64. arm_matrix_instance_q31 * pDst)
  65. {
  66. q31_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  67. q31_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  68. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  69. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  70. q31_t *px; /* Temporary output data matrix pointer */
  71. q63_t sum; /* Accumulator */
  72. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  73. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  74. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  75. #if defined (ARM_MATH_DSP)
  76. /* Run the below code for Cortex-M4 and Cortex-M3 */
  77. uint16_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  78. arm_status status; /* status of matrix multiplication */
  79. q31_t a0, a1, a2, a3, b0, b1, b2, b3;
  80. #ifdef ARM_MATH_MATRIX_CHECK
  81. /* Check for matrix mismatch condition */
  82. if ((pSrcA->numCols != pSrcB->numRows) ||
  83. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  84. {
  85. /* Set status as ARM_MATH_SIZE_MISMATCH */
  86. status = ARM_MATH_SIZE_MISMATCH;
  87. }
  88. else
  89. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  90. {
  91. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  92. /* row loop */
  93. do
  94. {
  95. /* Output pointer is set to starting address of the row being processed */
  96. px = pOut + i;
  97. /* For every row wise process, the column loop counter is to be initiated */
  98. col = numColsB;
  99. /* For every row wise process, the pIn2 pointer is set
  100. ** to the starting address of the pSrcB data */
  101. pIn2 = pSrcB->pData;
  102. j = 0U;
  103. /* column loop */
  104. do
  105. {
  106. /* Set the variable sum, that acts as accumulator, to zero */
  107. sum = 0;
  108. /* Initiate the pointer pIn1 to point to the starting address of pInA */
  109. pIn1 = pInA;
  110. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  111. colCnt = numColsA >> 2;
  112. /* matrix multiplication */
  113. while (colCnt > 0U)
  114. {
  115. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  116. /* Perform the multiply-accumulates */
  117. b0 = *pIn2;
  118. pIn2 += numColsB;
  119. a0 = *pIn1++;
  120. a1 = *pIn1++;
  121. b1 = *pIn2;
  122. pIn2 += numColsB;
  123. b2 = *pIn2;
  124. pIn2 += numColsB;
  125. sum += (q63_t) a0 *b0;
  126. sum += (q63_t) a1 *b1;
  127. a2 = *pIn1++;
  128. a3 = *pIn1++;
  129. b3 = *pIn2;
  130. pIn2 += numColsB;
  131. sum += (q63_t) a2 *b2;
  132. sum += (q63_t) a3 *b3;
  133. /* Decrement the loop counter */
  134. colCnt--;
  135. }
  136. /* If the columns of pSrcA is not a multiple of 4, compute any remaining output samples here.
  137. ** No loop unrolling is used. */
  138. colCnt = numColsA % 0x4U;
  139. while (colCnt > 0U)
  140. {
  141. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  142. /* Perform the multiply-accumulates */
  143. sum += (q63_t) * pIn1++ * *pIn2;
  144. pIn2 += numColsB;
  145. /* Decrement the loop counter */
  146. colCnt--;
  147. }
  148. /* Convert the result from 2.62 to 1.31 format and store in destination buffer */
  149. *px++ = (q31_t) (sum >> 31);
  150. /* Update the pointer pIn2 to point to the starting address of the next column */
  151. j++;
  152. pIn2 = (pSrcB->pData) + j;
  153. /* Decrement the column loop counter */
  154. col--;
  155. } while (col > 0U);
  156. #else
  157. /* Run the below code for Cortex-M0 */
  158. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  159. uint16_t col, i = 0U, row = numRowsA, colCnt; /* loop counters */
  160. arm_status status; /* status of matrix multiplication */
  161. #ifdef ARM_MATH_MATRIX_CHECK
  162. /* Check for matrix mismatch condition */
  163. if ((pSrcA->numCols != pSrcB->numRows) ||
  164. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  165. {
  166. /* Set status as ARM_MATH_SIZE_MISMATCH */
  167. status = ARM_MATH_SIZE_MISMATCH;
  168. }
  169. else
  170. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  171. {
  172. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  173. /* row loop */
  174. do
  175. {
  176. /* Output pointer is set to starting address of the row being processed */
  177. px = pOut + i;
  178. /* For every row wise process, the column loop counter is to be initiated */
  179. col = numColsB;
  180. /* For every row wise process, the pIn2 pointer is set
  181. ** to the starting address of the pSrcB data */
  182. pIn2 = pSrcB->pData;
  183. /* column loop */
  184. do
  185. {
  186. /* Set the variable sum, that acts as accumulator, to zero */
  187. sum = 0;
  188. /* Initiate the pointer pIn1 to point to the starting address of pInA */
  189. pIn1 = pInA;
  190. /* Matrix A columns number of MAC operations are to be performed */
  191. colCnt = numColsA;
  192. /* matrix multiplication */
  193. while (colCnt > 0U)
  194. {
  195. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  196. /* Perform the multiply-accumulates */
  197. sum += (q63_t) * pIn1++ * *pIn2;
  198. pIn2 += numColsB;
  199. /* Decrement the loop counter */
  200. colCnt--;
  201. }
  202. /* Convert the result from 2.62 to 1.31 format and store in destination buffer */
  203. *px++ = (q31_t) clip_q63_to_q31(sum >> 31);
  204. /* Decrement the column loop counter */
  205. col--;
  206. /* Update the pointer pIn2 to point to the starting address of the next column */
  207. pIn2 = pInB + (numColsB - col);
  208. } while (col > 0U);
  209. #endif
  210. /* Update the pointer pInA to point to the starting address of the next row */
  211. i = i + numColsB;
  212. pInA = pInA + numColsA;
  213. /* Decrement the row loop counter */
  214. row--;
  215. } while (row > 0U);
  216. /* set status as ARM_MATH_SUCCESS */
  217. status = ARM_MATH_SUCCESS;
  218. }
  219. /* Return to application */
  220. return (status);
  221. }
  222. /**
  223. * @} end of MatrixMult group
  224. */