arm_mat_mult_f32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_f32.c
  4. * Description: Floating-point 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. * @defgroup MatrixMult Matrix Multiplication
  34. *
  35. * Multiplies two matrices.
  36. *
  37. * \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices"
  38. * Matrix multiplication is only defined if the number of columns of the
  39. * first matrix equals the number of rows of the second matrix.
  40. * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
  41. * in an <code>M x P</code> matrix.
  42. * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
  43. * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
  44. * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
  45. */
  46. /**
  47. * @addtogroup MatrixMult
  48. * @{
  49. */
  50. /**
  51. * @brief Floating-point matrix multiplication.
  52. * @param[in] *pSrcA points to the first input matrix structure
  53. * @param[in] *pSrcB points to the second input matrix structure
  54. * @param[out] *pDst points to output matrix structure
  55. * @return The function returns either
  56. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  57. */
  58. arm_status arm_mat_mult_f32(
  59. const arm_matrix_instance_f32 * pSrcA,
  60. const arm_matrix_instance_f32 * pSrcB,
  61. arm_matrix_instance_f32 * pDst)
  62. {
  63. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  64. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  65. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  66. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  67. float32_t *px; /* Temporary output data matrix pointer */
  68. float32_t sum; /* Accumulator */
  69. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  70. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  71. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  72. #if defined (ARM_MATH_DSP)
  73. /* Run the below code for Cortex-M4 and Cortex-M3 */
  74. float32_t in1, in2, in3, in4;
  75. uint16_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  76. arm_status status; /* status of matrix multiplication */
  77. #ifdef ARM_MATH_MATRIX_CHECK
  78. /* Check for matrix mismatch condition */
  79. if ((pSrcA->numCols != pSrcB->numRows) ||
  80. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  81. {
  82. /* Set status as ARM_MATH_SIZE_MISMATCH */
  83. status = ARM_MATH_SIZE_MISMATCH;
  84. }
  85. else
  86. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  87. {
  88. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  89. /* row loop */
  90. do
  91. {
  92. /* Output pointer is set to starting address of the row being processed */
  93. px = pOut + i;
  94. /* For every row wise process, the column loop counter is to be initiated */
  95. col = numColsB;
  96. /* For every row wise process, the pIn2 pointer is set
  97. ** to the starting address of the pSrcB data */
  98. pIn2 = pSrcB->pData;
  99. j = 0U;
  100. /* column loop */
  101. do
  102. {
  103. /* Set the variable sum, that acts as accumulator, to zero */
  104. sum = 0.0f;
  105. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  106. pIn1 = pInA;
  107. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  108. colCnt = numColsA >> 2U;
  109. /* matrix multiplication */
  110. while (colCnt > 0U)
  111. {
  112. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  113. in3 = *pIn2;
  114. pIn2 += numColsB;
  115. in1 = pIn1[0];
  116. in2 = pIn1[1];
  117. sum += in1 * in3;
  118. in4 = *pIn2;
  119. pIn2 += numColsB;
  120. sum += in2 * in4;
  121. in3 = *pIn2;
  122. pIn2 += numColsB;
  123. in1 = pIn1[2];
  124. in2 = pIn1[3];
  125. sum += in1 * in3;
  126. in4 = *pIn2;
  127. pIn2 += numColsB;
  128. sum += in2 * in4;
  129. pIn1 += 4U;
  130. /* Decrement the loop count */
  131. colCnt--;
  132. }
  133. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  134. ** No loop unrolling is used. */
  135. colCnt = numColsA % 0x4U;
  136. while (colCnt > 0U)
  137. {
  138. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  139. sum += *pIn1++ * (*pIn2);
  140. pIn2 += numColsB;
  141. /* Decrement the loop counter */
  142. colCnt--;
  143. }
  144. /* Store the result in the destination buffer */
  145. *px++ = sum;
  146. /* Update the pointer pIn2 to point to the starting address of the next column */
  147. j++;
  148. pIn2 = pSrcB->pData + j;
  149. /* Decrement the column loop counter */
  150. col--;
  151. } while (col > 0U);
  152. #else
  153. /* Run the below code for Cortex-M0 */
  154. float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  155. uint16_t col, i = 0U, row = numRowsA, colCnt; /* loop counters */
  156. arm_status status; /* status of matrix multiplication */
  157. #ifdef ARM_MATH_MATRIX_CHECK
  158. /* Check for matrix mismatch condition */
  159. if ((pSrcA->numCols != pSrcB->numRows) ||
  160. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  161. {
  162. /* Set status as ARM_MATH_SIZE_MISMATCH */
  163. status = ARM_MATH_SIZE_MISMATCH;
  164. }
  165. else
  166. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  167. {
  168. /* The following loop performs the dot-product of each row in pInA with each column in pInB */
  169. /* row loop */
  170. do
  171. {
  172. /* Output pointer is set to starting address of the row being processed */
  173. px = pOut + i;
  174. /* For every row wise process, the column loop counter is to be initiated */
  175. col = numColsB;
  176. /* For every row wise process, the pIn2 pointer is set
  177. ** to the starting address of the pSrcB data */
  178. pIn2 = pSrcB->pData;
  179. /* column loop */
  180. do
  181. {
  182. /* Set the variable sum, that acts as accumulator, to zero */
  183. sum = 0.0f;
  184. /* Initialize the pointer pIn1 to point to the starting address of the row being processed */
  185. pIn1 = pInA;
  186. /* Matrix A columns number of MAC operations are to be performed */
  187. colCnt = numColsA;
  188. while (colCnt > 0U)
  189. {
  190. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  191. sum += *pIn1++ * (*pIn2);
  192. pIn2 += numColsB;
  193. /* Decrement the loop counter */
  194. colCnt--;
  195. }
  196. /* Store the result in the destination buffer */
  197. *px++ = sum;
  198. /* Decrement the column loop counter */
  199. col--;
  200. /* Update the pointer pIn2 to point to the starting address of the next column */
  201. pIn2 = pInB + (numColsB - col);
  202. } while (col > 0U);
  203. #endif /* #if defined (ARM_MATH_DSP) */
  204. /* Update the pointer pInA to point to the starting address of the next row */
  205. i = i + numColsB;
  206. pInA = pInA + numColsA;
  207. /* Decrement the row loop counter */
  208. row--;
  209. } while (row > 0U);
  210. /* Set status as ARM_MATH_SUCCESS */
  211. status = ARM_MATH_SUCCESS;
  212. }
  213. /* Return to application */
  214. return (status);
  215. }
  216. /**
  217. * @} end of MatrixMult group
  218. */