arm_mat_mult_f32.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_mat_mult_f32.c
  9. *
  10. * Description: Floating-point matrix multiplication.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupMatrix
  43. */
  44. /**
  45. * @defgroup MatrixMult Matrix Multiplication
  46. *
  47. * Multiplies two matrices.
  48. *
  49. * \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices"
  50. * Matrix multiplication is only defined if the number of columns of the
  51. * first matrix equals the number of rows of the second matrix.
  52. * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
  53. * in an <code>M x P</code> matrix.
  54. * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
  55. * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
  56. * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
  57. */
  58. /**
  59. * @addtogroup MatrixMult
  60. * @{
  61. */
  62. /**
  63. * @brief Floating-point matrix multiplication.
  64. * @param[in] *pSrcA points to the first input matrix structure
  65. * @param[in] *pSrcB points to the second input matrix structure
  66. * @param[out] *pDst points to output matrix structure
  67. * @return The function returns either
  68. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  69. */
  70. arm_status arm_mat_mult_f32(
  71. const arm_matrix_instance_f32 * pSrcA,
  72. const arm_matrix_instance_f32 * pSrcB,
  73. arm_matrix_instance_f32 * pDst)
  74. {
  75. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  76. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  77. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  78. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  79. float32_t *px; /* Temporary output data matrix pointer */
  80. float32_t sum; /* Accumulator */
  81. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  82. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  83. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  84. #ifndef ARM_MATH_CM0_FAMILY
  85. /* Run the below code for Cortex-M4 and Cortex-M3 */
  86. float32_t in1, in2, in3, in4;
  87. uint16_t col, i = 0u, j, row = numRowsA, colCnt; /* loop counters */
  88. arm_status status; /* status of matrix multiplication */
  89. #ifdef ARM_MATH_MATRIX_CHECK
  90. /* Check for matrix mismatch condition */
  91. if((pSrcA->numCols != pSrcB->numRows) ||
  92. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  93. {
  94. /* Set status as ARM_MATH_SIZE_MISMATCH */
  95. status = ARM_MATH_SIZE_MISMATCH;
  96. }
  97. else
  98. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  99. {
  100. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  101. /* row loop */
  102. do
  103. {
  104. /* Output pointer is set to starting address of the row being processed */
  105. px = pOut + i;
  106. /* For every row wise process, the column loop counter is to be initiated */
  107. col = numColsB;
  108. /* For every row wise process, the pIn2 pointer is set
  109. ** to the starting address of the pSrcB data */
  110. pIn2 = pSrcB->pData;
  111. j = 0u;
  112. /* column loop */
  113. do
  114. {
  115. /* Set the variable sum, that acts as accumulator, to zero */
  116. sum = 0.0f;
  117. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  118. pIn1 = pInA;
  119. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  120. colCnt = numColsA >> 2u;
  121. /* matrix multiplication */
  122. while(colCnt > 0u)
  123. {
  124. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  125. in3 = *pIn2;
  126. pIn2 += numColsB;
  127. in1 = pIn1[0];
  128. in2 = pIn1[1];
  129. sum += in1 * in3;
  130. in4 = *pIn2;
  131. pIn2 += numColsB;
  132. sum += in2 * in4;
  133. in3 = *pIn2;
  134. pIn2 += numColsB;
  135. in1 = pIn1[2];
  136. in2 = pIn1[3];
  137. sum += in1 * in3;
  138. in4 = *pIn2;
  139. pIn2 += numColsB;
  140. sum += in2 * in4;
  141. pIn1 += 4u;
  142. /* Decrement the loop count */
  143. colCnt--;
  144. }
  145. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  146. ** No loop unrolling is used. */
  147. colCnt = numColsA % 0x4u;
  148. while(colCnt > 0u)
  149. {
  150. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  151. sum += *pIn1++ * (*pIn2);
  152. pIn2 += numColsB;
  153. /* Decrement the loop counter */
  154. colCnt--;
  155. }
  156. /* Store the result in the destination buffer */
  157. *px++ = sum;
  158. /* Update the pointer pIn2 to point to the starting address of the next column */
  159. j++;
  160. pIn2 = pSrcB->pData + j;
  161. /* Decrement the column loop counter */
  162. col--;
  163. } while(col > 0u);
  164. #else
  165. /* Run the below code for Cortex-M0 */
  166. float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  167. uint16_t col, i = 0u, row = numRowsA, colCnt; /* loop counters */
  168. arm_status status; /* status of matrix multiplication */
  169. #ifdef ARM_MATH_MATRIX_CHECK
  170. /* Check for matrix mismatch condition */
  171. if((pSrcA->numCols != pSrcB->numRows) ||
  172. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  173. {
  174. /* Set status as ARM_MATH_SIZE_MISMATCH */
  175. status = ARM_MATH_SIZE_MISMATCH;
  176. }
  177. else
  178. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  179. {
  180. /* The following loop performs the dot-product of each row in pInA with each column in pInB */
  181. /* row loop */
  182. do
  183. {
  184. /* Output pointer is set to starting address of the row being processed */
  185. px = pOut + i;
  186. /* For every row wise process, the column loop counter is to be initiated */
  187. col = numColsB;
  188. /* For every row wise process, the pIn2 pointer is set
  189. ** to the starting address of the pSrcB data */
  190. pIn2 = pSrcB->pData;
  191. /* column loop */
  192. do
  193. {
  194. /* Set the variable sum, that acts as accumulator, to zero */
  195. sum = 0.0f;
  196. /* Initialize the pointer pIn1 to point to the starting address of the row being processed */
  197. pIn1 = pInA;
  198. /* Matrix A columns number of MAC operations are to be performed */
  199. colCnt = numColsA;
  200. while(colCnt > 0u)
  201. {
  202. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  203. sum += *pIn1++ * (*pIn2);
  204. pIn2 += numColsB;
  205. /* Decrement the loop counter */
  206. colCnt--;
  207. }
  208. /* Store the result in the destination buffer */
  209. *px++ = sum;
  210. /* Decrement the column loop counter */
  211. col--;
  212. /* Update the pointer pIn2 to point to the starting address of the next column */
  213. pIn2 = pInB + (numColsB - col);
  214. } while(col > 0u);
  215. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  216. /* Update the pointer pInA to point to the starting address of the next row */
  217. i = i + numColsB;
  218. pInA = pInA + numColsA;
  219. /* Decrement the row loop counter */
  220. row--;
  221. } while(row > 0u);
  222. /* Set status as ARM_MATH_SUCCESS */
  223. status = ARM_MATH_SUCCESS;
  224. }
  225. /* Return to application */
  226. return (status);
  227. }
  228. /**
  229. * @} end of MatrixMult group
  230. */