arm_mat_mult_f32.c 16 KB

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