arm_mat_mult_f32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_f32.c
  4. * Description: Floating-point 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. * @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. #if defined(ARM_MATH_NEON)
  59. #define GROUPOFROWS 8
  60. arm_status arm_mat_mult_f32(
  61. const arm_matrix_instance_f32 * pSrcA,
  62. const arm_matrix_instance_f32 * pSrcB,
  63. arm_matrix_instance_f32 * pDst)
  64. {
  65. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  66. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  67. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  68. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  69. float32_t *px; /* Temporary output data matrix pointer */
  70. float32_t sum; /* Accumulator */
  71. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  72. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  73. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  74. uint16_t col, i = 0U, j, row = numRowsA, rowCnt, colCnt; /* loop counters */
  75. arm_status status; /* status of matrix multiplication */
  76. float32x4_t a0V, a1V, a2V, a3V, a4V, a5V, a6V, a7V;
  77. float32x4_t acc0,acc1,acc2,acc3,acc4,acc5,acc6,acc7,temp;
  78. float32x2_t accum = vdup_n_f32(0);
  79. float32_t *pIn1B = pSrcA->pData;
  80. float32_t *pIn1C = pSrcA->pData;
  81. float32_t *pIn1D = pSrcA->pData;
  82. float32_t *pIn1E = pSrcA->pData;
  83. float32_t *pIn1F = pSrcA->pData;
  84. float32_t *pIn1G = pSrcA->pData;
  85. float32_t *pIn1H = pSrcA->pData;
  86. float32_t *pxB,*pxC, *pxD, *pxE, *pxF, *pxG, *pxH; /* Temporary output data matrix pointer */
  87. float32_t sum0,sum1, sum2,sum3, sum4, sum5 , sum6, sum7;
  88. #ifdef ARM_MATH_MATRIX_CHECK
  89. /* Check for matrix mismatch condition */
  90. if ((pSrcA->numCols != pSrcB->numRows) ||
  91. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  92. {
  93. /* Set status as ARM_MATH_SIZE_MISMATCH */
  94. status = ARM_MATH_SIZE_MISMATCH;
  95. }
  96. else
  97. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  98. {
  99. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  100. /* Row loop */
  101. rowCnt = row >> 3;
  102. while(rowCnt > 0)
  103. {
  104. /* Output pointer is set to starting address of the row being processed */
  105. px = pOut + GROUPOFROWS*i;
  106. pxB = px + numColsB;
  107. pxC = px + 2*numColsB;
  108. pxD = px + 3*numColsB;
  109. pxE = px + 4*numColsB;
  110. pxF = px + 5*numColsB;
  111. pxG = px + 6*numColsB;
  112. pxH = px + 7*numColsB;
  113. /* For every row wise process, the column loop counter is to be initiated */
  114. col = numColsB;
  115. /* For every row wise process, the pIn2 pointer is set
  116. ** to the starting address of the pSrcB data */
  117. pIn2 = pSrcB->pData;
  118. j = 0U;
  119. /* Column loop */
  120. do
  121. {
  122. /* Set the variable sum, that acts as accumulator, to zero */
  123. sum0 = 0.0f;
  124. sum1 = 0.0f;
  125. sum2 = 0.0f;
  126. sum3 = 0.0f;
  127. sum4 = 0.0f;
  128. sum5 = 0.0f;
  129. sum6 = 0.0f;
  130. sum7 = 0.0f;
  131. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  132. pIn1 = pInA;
  133. pIn1B = pIn1 + numColsA;
  134. pIn1C = pIn1 + 2*numColsA;
  135. pIn1D = pIn1 + 3*numColsA;
  136. pIn1E = pIn1 + 4*numColsA;
  137. pIn1F = pIn1 + 5*numColsA;
  138. pIn1G = pIn1 + 6*numColsA;
  139. pIn1H = pIn1 + 7*numColsA;
  140. acc0 = vdupq_n_f32(0.0);
  141. acc1 = vdupq_n_f32(0.0);
  142. acc2 = vdupq_n_f32(0.0);
  143. acc3 = vdupq_n_f32(0.0);
  144. acc4 = vdupq_n_f32(0.0);
  145. acc5 = vdupq_n_f32(0.0);
  146. acc6 = vdupq_n_f32(0.0);
  147. acc7 = vdupq_n_f32(0.0);
  148. /* Compute 4 MACs simultaneously. */
  149. colCnt = numColsA >> 2U;
  150. /* Matrix multiplication */
  151. while (colCnt > 0U)
  152. {
  153. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  154. a0V = vld1q_f32(pIn1);
  155. a1V = vld1q_f32(pIn1B);
  156. a2V = vld1q_f32(pIn1C);
  157. a3V = vld1q_f32(pIn1D);
  158. a4V = vld1q_f32(pIn1E);
  159. a5V = vld1q_f32(pIn1F);
  160. a6V = vld1q_f32(pIn1G);
  161. a7V = vld1q_f32(pIn1H);
  162. pIn1 += 4;
  163. pIn1B += 4;
  164. pIn1C += 4;
  165. pIn1D += 4;
  166. pIn1E += 4;
  167. pIn1F += 4;
  168. pIn1G += 4;
  169. pIn1H += 4;
  170. temp[0] = *pIn2;
  171. pIn2 += numColsB;
  172. temp[1] = *pIn2;
  173. pIn2 += numColsB;
  174. temp[2] = *pIn2;
  175. pIn2 += numColsB;
  176. temp[3] = *pIn2;
  177. pIn2 += numColsB;
  178. acc0 = vmlaq_f32(acc0,a0V,temp);
  179. acc1 = vmlaq_f32(acc1,a1V,temp);
  180. acc2 = vmlaq_f32(acc2,a2V,temp);
  181. acc3 = vmlaq_f32(acc3,a3V,temp);
  182. acc4 = vmlaq_f32(acc4,a4V,temp);
  183. acc5 = vmlaq_f32(acc5,a5V,temp);
  184. acc6 = vmlaq_f32(acc6,a6V,temp);
  185. acc7 = vmlaq_f32(acc7,a7V,temp);
  186. /* Decrement the loop count */
  187. colCnt--;
  188. }
  189. accum = vpadd_f32(vget_low_f32(acc0), vget_high_f32(acc0));
  190. sum0 += accum[0] + accum[1];
  191. accum = vpadd_f32(vget_low_f32(acc1), vget_high_f32(acc1));
  192. sum1 += accum[0] + accum[1];
  193. accum = vpadd_f32(vget_low_f32(acc2), vget_high_f32(acc2));
  194. sum2 += accum[0] + accum[1];
  195. accum = vpadd_f32(vget_low_f32(acc3), vget_high_f32(acc3));
  196. sum3 += accum[0] + accum[1];
  197. accum = vpadd_f32(vget_low_f32(acc4), vget_high_f32(acc4));
  198. sum4 += accum[0] + accum[1];
  199. accum = vpadd_f32(vget_low_f32(acc5), vget_high_f32(acc5));
  200. sum5 += accum[0] + accum[1];
  201. accum = vpadd_f32(vget_low_f32(acc6), vget_high_f32(acc6));
  202. sum6 += accum[0] + accum[1];
  203. accum = vpadd_f32(vget_low_f32(acc7), vget_high_f32(acc7));
  204. sum7 += accum[0] + accum[1];
  205. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  206. ** No loop unrolling is used. */
  207. colCnt = numColsA & 3;
  208. while (colCnt > 0U)
  209. {
  210. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  211. sum0 += *pIn1++ * (*pIn2);
  212. sum1 += *pIn1B++ * (*pIn2);
  213. sum2 += *pIn1C++ * (*pIn2);
  214. sum3 += *pIn1D++ * (*pIn2);
  215. sum4 += *pIn1E++ * (*pIn2);
  216. sum5 += *pIn1F++ * (*pIn2);
  217. sum6 += *pIn1G++ * (*pIn2);
  218. sum7 += *pIn1H++ * (*pIn2);
  219. pIn2 += numColsB;
  220. /* Decrement the loop counter */
  221. colCnt--;
  222. }
  223. /* Store the result in the destination buffer */
  224. *px++ = sum0;
  225. *pxB++ = sum1;
  226. *pxC++ = sum2;
  227. *pxD++ = sum3;
  228. *pxE++ = sum4;
  229. *pxF++ = sum5;
  230. *pxG++ = sum6;
  231. *pxH++ = sum7;
  232. /* Update the pointer pIn2 to point to the starting address of the next column */
  233. j++;
  234. pIn2 = pSrcB->pData + j;
  235. /* Decrement the column loop counter */
  236. col--;
  237. } while (col > 0U);
  238. /* Update the pointer pInA to point to the starting address of the next row */
  239. i = i + numColsB;
  240. pInA = pInA + GROUPOFROWS*numColsA;
  241. /* Decrement the row loop counter */
  242. rowCnt--;
  243. }
  244. /*
  245. i was the index of a group of rows computed by previous loop.
  246. Now i is the index of a row since below code is computing row per row
  247. and no more group of row per group of rows.
  248. */
  249. i = GROUPOFROWS*i;
  250. rowCnt = row & 7;
  251. while(rowCnt > 0)
  252. {
  253. /* Output pointer is set to starting address of the row being processed */
  254. px = pOut + i;
  255. /* For every row wise process, the column loop counter is to be initiated */
  256. col = numColsB;
  257. /* For every row wise process, the pIn2 pointer is set
  258. ** to the starting address of the pSrcB data */
  259. pIn2 = pSrcB->pData;
  260. j = 0U;
  261. /* Column loop */
  262. do
  263. {
  264. /* Set the variable sum, that acts as accumulator, to zero */
  265. sum = 0.0f;
  266. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  267. pIn1 = pInA;
  268. acc0 = vdupq_n_f32(0.0);
  269. /* Compute 4 MACs simultaneously. */
  270. colCnt = numColsA >> 2U;
  271. /* Matrix multiplication */
  272. while (colCnt > 0U)
  273. {
  274. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  275. a0V = vld1q_f32(pIn1); // load & separate real/imag pSrcA (de-interleave 2)
  276. pIn1 += 4;
  277. temp[0] = *pIn2;
  278. pIn2 += numColsB;
  279. temp[1] = *pIn2;
  280. pIn2 += numColsB;
  281. temp[2] = *pIn2;
  282. pIn2 += numColsB;
  283. temp[3] = *pIn2;
  284. pIn2 += numColsB;
  285. acc0 = vmlaq_f32(acc0,a0V,temp);
  286. /* Decrement the loop count */
  287. colCnt--;
  288. }
  289. accum = vpadd_f32(vget_low_f32(acc0), vget_high_f32(acc0));
  290. sum += accum[0] + accum[1];
  291. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  292. ** No loop unrolling is used. */
  293. colCnt = numColsA % 0x4U;
  294. while (colCnt > 0U)
  295. {
  296. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  297. sum += *pIn1++ * (*pIn2);
  298. pIn2 += numColsB;
  299. /* Decrement the loop counter */
  300. colCnt--;
  301. }
  302. /* Store the result in the destination buffer */
  303. *px++ = sum;
  304. /* Update the pointer pIn2 to point to the starting address of the next column */
  305. j++;
  306. pIn2 = pSrcB->pData + j;
  307. /* Decrement the column loop counter */
  308. col--;
  309. } while (col > 0U);
  310. /* Update the pointer pInA to point to the starting address of the next row */
  311. i = i + numColsB;
  312. pInA = pInA + numColsA;
  313. /* Decrement the row loop counter */
  314. rowCnt--;
  315. }
  316. /* Set status as ARM_MATH_SUCCESS */
  317. status = ARM_MATH_SUCCESS;
  318. }
  319. /* Return to application */
  320. return (status);
  321. }
  322. #else
  323. arm_status arm_mat_mult_f32(
  324. const arm_matrix_instance_f32 * pSrcA,
  325. const arm_matrix_instance_f32 * pSrcB,
  326. arm_matrix_instance_f32 * pDst)
  327. {
  328. float32_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  329. float32_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  330. float32_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  331. float32_t *pInB = pSrcB->pData; /* Input data matrix pointer B */
  332. float32_t *pOut = pDst->pData; /* Output data matrix pointer */
  333. float32_t *px; /* Temporary output data matrix pointer */
  334. float32_t sum; /* Accumulator */
  335. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  336. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  337. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  338. uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  339. arm_status status; /* Status of matrix multiplication */
  340. #ifdef ARM_MATH_MATRIX_CHECK
  341. /* Check for matrix mismatch condition */
  342. if ((pSrcA->numCols != pSrcB->numRows) ||
  343. (pSrcA->numRows != pDst->numRows) ||
  344. (pSrcB->numCols != pDst->numCols) )
  345. {
  346. /* Set status as ARM_MATH_SIZE_MISMATCH */
  347. status = ARM_MATH_SIZE_MISMATCH;
  348. }
  349. else
  350. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  351. {
  352. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  353. /* row loop */
  354. do
  355. {
  356. /* Output pointer is set to starting address of row being processed */
  357. px = pOut + i;
  358. /* For every row wise process, column loop counter is to be initiated */
  359. col = numColsB;
  360. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  361. pIn2 = pSrcB->pData;
  362. /* column loop */
  363. do
  364. {
  365. /* Set the variable sum, that acts as accumulator, to zero */
  366. sum = 0.0f;
  367. /* Initialize pointer pIn1 to point to starting address of column being processed */
  368. pIn1 = pInA;
  369. #if defined (ARM_MATH_LOOPUNROLL)
  370. /* Loop unrolling: Compute 4 MACs at a time. */
  371. colCnt = numColsA >> 2U;
  372. /* matrix multiplication */
  373. while (colCnt > 0U)
  374. {
  375. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  376. /* Perform the multiply-accumulates */
  377. sum += *pIn1++ * *pIn2;
  378. pIn2 += numColsB;
  379. sum += *pIn1++ * *pIn2;
  380. pIn2 += numColsB;
  381. sum += *pIn1++ * *pIn2;
  382. pIn2 += numColsB;
  383. sum += *pIn1++ * *pIn2;
  384. pIn2 += numColsB;
  385. /* Decrement loop counter */
  386. colCnt--;
  387. }
  388. /* Loop unrolling: Compute remaining MACs */
  389. colCnt = numColsA % 0x4U;
  390. #else
  391. /* Initialize cntCnt with number of columns */
  392. colCnt = numColsA;
  393. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  394. while (colCnt > 0U)
  395. {
  396. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  397. /* Perform the multiply-accumulates */
  398. sum += *pIn1++ * *pIn2;
  399. pIn2 += numColsB;
  400. /* Decrement loop counter */
  401. colCnt--;
  402. }
  403. /* Store result in destination buffer */
  404. *px++ = sum;
  405. /* Decrement column loop counter */
  406. col--;
  407. /* Update pointer pIn2 to point to starting address of next column */
  408. pIn2 = pInB + (numColsB - col);
  409. } while (col > 0U);
  410. /* Update pointer pInA to point to starting address of next row */
  411. i = i + numColsB;
  412. pInA = pInA + numColsA;
  413. /* Decrement row loop counter */
  414. row--;
  415. } while (row > 0U);
  416. /* Set status as ARM_MATH_SUCCESS */
  417. status = ARM_MATH_SUCCESS;
  418. }
  419. /* Return to application */
  420. return (status);
  421. }
  422. #endif /* #if defined(ARM_MATH_NEON) */
  423. /**
  424. * @} end of MatrixMult group
  425. */