arm_mat_mult_f64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_f64.c
  4. * Description: Floating-point matrix multiplication
  5. *
  6. * $Date: 10 August 2022
  7. * $Revision: V1.9.1
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/matrix_functions.h"
  29. #if defined(ARM_MATH_NEON) && defined(__aarch64__)
  30. #define GROUPOFROWS 8
  31. #endif
  32. /**
  33. * @ingroup groupMatrix
  34. */
  35. /**
  36. * @defgroup MatrixMult Matrix Multiplication
  37. *
  38. * Multiplies two matrices.
  39. *
  40. * \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices"
  41. * Matrix multiplication is only defined if the number of columns of the
  42. * first matrix equals the number of rows of the second matrix.
  43. * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
  44. * in an <code>M x P</code> matrix.
  45. * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
  46. * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
  47. * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
  48. */
  49. /**
  50. * @addtogroup MatrixMult
  51. * @{
  52. */
  53. /**
  54. * @brief Floating-point matrix multiplication.
  55. * @param[in] *pSrcA points to the first input matrix structure
  56. * @param[in] *pSrcB points to the second input matrix structure
  57. * @param[out] *pDst points to output matrix structure
  58. * @return The function returns either
  59. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  60. */
  61. #if defined(ARM_MATH_NEON) && defined(__aarch64__)
  62. arm_status arm_mat_mult_f64(
  63. const arm_matrix_instance_f64 * pSrcA,
  64. const arm_matrix_instance_f64 * pSrcB,
  65. arm_matrix_instance_f64 * pDst)
  66. {
  67. float64_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  68. float64_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  69. float64_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  70. float64_t *pOut = pDst->pData; /* output data matrix pointer */
  71. float64_t *px; /* Temporary output data matrix pointer */
  72. float64_t sum; /* Accumulator */
  73. uint32_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  74. uint32_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  75. uint32_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  76. uint32_t col, i = 0U, j, row = numRowsA, rowCnt, colCnt; /* loop counters */
  77. arm_status status; /* status of matrix multiplication */
  78. float64x2_t a0V, a1V, a2V, a3V, a4V, a5V, a6V, a7V;
  79. float64x2_t acc0,acc1,acc2,acc3,acc4,acc5,acc6,acc7,temp;
  80. float64_t *pIn1B = pSrcA->pData;
  81. float64_t *pIn1C = pSrcA->pData;
  82. float64_t *pIn1D = pSrcA->pData;
  83. float64_t *pIn1E = pSrcA->pData;
  84. float64_t *pIn1F = pSrcA->pData;
  85. float64_t *pIn1G = pSrcA->pData;
  86. float64_t *pIn1H = pSrcA->pData;
  87. float64_t *pxB,*pxC, *pxD, *pxE, *pxF, *pxG, *pxH; /* Temporary output data matrix pointer */
  88. float64_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.0;
  125. sum1 = 0.0;
  126. sum2 = 0.0;
  127. sum3 = 0.0;
  128. sum4 = 0.0;
  129. sum5 = 0.0;
  130. sum6 = 0.0;
  131. sum7 = 0.0;
  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_f64(0.0);
  142. acc1 = vdupq_n_f64(0.0);
  143. acc2 = vdupq_n_f64(0.0);
  144. acc3 = vdupq_n_f64(0.0);
  145. acc4 = vdupq_n_f64(0.0);
  146. acc5 = vdupq_n_f64(0.0);
  147. acc6 = vdupq_n_f64(0.0);
  148. acc7 = vdupq_n_f64(0.0);
  149. /* Compute 2 MACs simultaneously. */
  150. colCnt = numColsA >> 1U;
  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_f64(pIn1);
  156. a1V = vld1q_f64(pIn1B);
  157. a2V = vld1q_f64(pIn1C);
  158. a3V = vld1q_f64(pIn1D);
  159. a4V = vld1q_f64(pIn1E);
  160. a5V = vld1q_f64(pIn1F);
  161. a6V = vld1q_f64(pIn1G);
  162. a7V = vld1q_f64(pIn1H);
  163. pIn1 += 2;
  164. pIn1B += 2;
  165. pIn1C += 2;
  166. pIn1D += 2;
  167. pIn1E += 2;
  168. pIn1F += 2;
  169. pIn1G += 2;
  170. pIn1H += 2;
  171. temp = vsetq_lane_f64(*pIn2,temp,0);
  172. pIn2 += numColsB;
  173. temp = vsetq_lane_f64(*pIn2,temp,1);
  174. pIn2 += numColsB;
  175. acc0 = vmlaq_f64(acc0,a0V,temp);
  176. acc1 = vmlaq_f64(acc1,a1V,temp);
  177. acc2 = vmlaq_f64(acc2,a2V,temp);
  178. acc3 = vmlaq_f64(acc3,a3V,temp);
  179. acc4 = vmlaq_f64(acc4,a4V,temp);
  180. acc5 = vmlaq_f64(acc5,a5V,temp);
  181. acc6 = vmlaq_f64(acc6,a6V,temp);
  182. acc7 = vmlaq_f64(acc7,a7V,temp);
  183. /* Decrement the loop count */
  184. colCnt--;
  185. }
  186. sum0 += vaddvq_f64(acc0);
  187. sum1 += vaddvq_f64(acc1);
  188. sum2 += vaddvq_f64(acc2);
  189. sum3 += vaddvq_f64(acc3);
  190. sum4 += vaddvq_f64(acc4);
  191. sum5 += vaddvq_f64(acc5);
  192. sum6 += vaddvq_f64(acc6);
  193. sum7 += vaddvq_f64(acc7);
  194. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  195. ** No loop unrolling is used. */
  196. colCnt = numColsA & 1;
  197. while (colCnt > 0U)
  198. {
  199. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  200. sum0 += *pIn1++ * (*pIn2);
  201. sum1 += *pIn1B++ * (*pIn2);
  202. sum2 += *pIn1C++ * (*pIn2);
  203. sum3 += *pIn1D++ * (*pIn2);
  204. sum4 += *pIn1E++ * (*pIn2);
  205. sum5 += *pIn1F++ * (*pIn2);
  206. sum6 += *pIn1G++ * (*pIn2);
  207. sum7 += *pIn1H++ * (*pIn2);
  208. pIn2 += numColsB;
  209. /* Decrement the loop counter */
  210. colCnt--;
  211. }
  212. /* Store the result in the destination buffer */
  213. *px++ = sum0;
  214. *pxB++ = sum1;
  215. *pxC++ = sum2;
  216. *pxD++ = sum3;
  217. *pxE++ = sum4;
  218. *pxF++ = sum5;
  219. *pxG++ = sum6;
  220. *pxH++ = sum7;
  221. /* Update the pointer pIn2 to point to the starting address of the next column */
  222. j++;
  223. pIn2 = pSrcB->pData + j;
  224. /* Decrement the column loop counter */
  225. col--;
  226. } while (col > 0U);
  227. /* Update the pointer pInA to point to the starting address of the next row */
  228. i = i + numColsB;
  229. pInA = pInA + GROUPOFROWS*numColsA;
  230. /* Decrement the row loop counter */
  231. rowCnt--;
  232. }
  233. /*
  234. i was the index of a group of rows computed by previous loop.
  235. Now i is the index of a row since below code is computing row per row
  236. and no more group of row per group of rows.
  237. */
  238. i = GROUPOFROWS*i;
  239. rowCnt = row & 7;
  240. while(rowCnt > 0)
  241. {
  242. /* Output pointer is set to starting address of the row being processed */
  243. px = pOut + i;
  244. /* For every row wise process, the column loop counter is to be initiated */
  245. col = numColsB;
  246. /* For every row wise process, the pIn2 pointer is set
  247. ** to the starting address of the pSrcB data */
  248. pIn2 = pSrcB->pData;
  249. j = 0U;
  250. /* Column loop */
  251. do
  252. {
  253. /* Set the variable sum, that acts as accumulator, to zero */
  254. sum = 0.0;
  255. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  256. pIn1 = pInA;
  257. acc0 = vdupq_n_f64(0.0);
  258. /* Compute 4 MACs simultaneously. */
  259. colCnt = numColsA >> 1U;
  260. /* Matrix multiplication */
  261. while (colCnt > 0U)
  262. {
  263. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  264. a0V = vld1q_f64(pIn1); // load & separate real/imag pSrcA (de-interleave 2)
  265. pIn1 += 2;
  266. temp = vsetq_lane_f64(*pIn2,temp,0);
  267. pIn2 += numColsB;
  268. temp = vsetq_lane_f64(*pIn2,temp,1);
  269. pIn2 += numColsB;
  270. acc0 = vmlaq_f64(acc0,a0V,temp);
  271. /* Decrement the loop count */
  272. colCnt--;
  273. }
  274. //accum = vpadd_f32(vget_low_f32(acc0), vget_high_f32(acc0));
  275. sum += vaddvq_f64(acc0);
  276. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  277. ** No loop unrolling is used. */
  278. colCnt = numColsA % 0x2U;
  279. while (colCnt > 0U)
  280. {
  281. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  282. sum += *pIn1++ * (*pIn2);
  283. pIn2 += numColsB;
  284. /* Decrement the loop counter */
  285. colCnt--;
  286. }
  287. /* Store the result in the destination buffer */
  288. *px++ = sum;
  289. /* Update the pointer pIn2 to point to the starting address of the next column */
  290. j++;
  291. pIn2 = pSrcB->pData + j;
  292. /* Decrement the column loop counter */
  293. col--;
  294. } while (col > 0U);
  295. /* Update the pointer pInA to point to the starting address of the next row */
  296. i = i + numColsB;
  297. pInA = pInA + numColsA;
  298. /* Decrement the row loop counter */
  299. rowCnt--;
  300. }
  301. /* Set status as ARM_MATH_SUCCESS */
  302. status = ARM_MATH_SUCCESS;
  303. }
  304. /* Return to application */
  305. return (status);
  306. }
  307. #else
  308. arm_status arm_mat_mult_f64(
  309. const arm_matrix_instance_f64 * pSrcA,
  310. const arm_matrix_instance_f64 * pSrcB,
  311. arm_matrix_instance_f64 * pDst)
  312. {
  313. float64_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  314. float64_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  315. float64_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  316. float64_t *pInB = pSrcB->pData; /* Input data matrix pointer B */
  317. float64_t *pOut = pDst->pData; /* Output data matrix pointer */
  318. float64_t *px; /* Temporary output data matrix pointer */
  319. float64_t sum; /* Accumulator */
  320. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  321. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  322. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  323. uint64_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  324. arm_status status; /* Status of matrix multiplication */
  325. #ifdef ARM_MATH_MATRIX_CHECK
  326. /* Check for matrix mismatch condition */
  327. if ((pSrcA->numCols != pSrcB->numRows) ||
  328. (pSrcA->numRows != pDst->numRows) ||
  329. (pSrcB->numCols != pDst->numCols) )
  330. {
  331. /* Set status as ARM_MATH_SIZE_MISMATCH */
  332. status = ARM_MATH_SIZE_MISMATCH;
  333. }
  334. else
  335. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  336. {
  337. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  338. /* row loop */
  339. do
  340. {
  341. /* Output pointer is set to starting address of row being processed */
  342. px = pOut + i;
  343. /* For every row wise process, column loop counter is to be initiated */
  344. col = numColsB;
  345. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  346. pIn2 = pSrcB->pData;
  347. /* column loop */
  348. do
  349. {
  350. /* Set the variable sum, that acts as accumulator, to zero */
  351. sum = 0.0;
  352. /* Initialize pointer pIn1 to point to starting address of column being processed */
  353. pIn1 = pInA;
  354. #if defined (ARM_MATH_LOOPUNROLL)
  355. /* Loop unrolling: Compute 4 MACs at a time. */
  356. colCnt = numColsA >> 2U;
  357. /* matrix multiplication */
  358. while (colCnt > 0U)
  359. {
  360. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  361. /* Perform the multiply-accumulates */
  362. sum += *pIn1++ * *pIn2;
  363. pIn2 += numColsB;
  364. sum += *pIn1++ * *pIn2;
  365. pIn2 += numColsB;
  366. sum += *pIn1++ * *pIn2;
  367. pIn2 += numColsB;
  368. sum += *pIn1++ * *pIn2;
  369. pIn2 += numColsB;
  370. /* Decrement loop counter */
  371. colCnt--;
  372. }
  373. /* Loop unrolling: Compute remaining MACs */
  374. colCnt = numColsA % 0x4U;
  375. #else
  376. /* Initialize cntCnt with number of columns */
  377. colCnt = numColsA;
  378. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  379. while (colCnt > 0U)
  380. {
  381. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  382. /* Perform the multiply-accumulates */
  383. sum += *pIn1++ * *pIn2;
  384. pIn2 += numColsB;
  385. /* Decrement loop counter */
  386. colCnt--;
  387. }
  388. /* Store result in destination buffer */
  389. *px++ = sum;
  390. /* Decrement column loop counter */
  391. col--;
  392. /* Update pointer pIn2 to point to starting address of next column */
  393. pIn2 = pInB + (numColsB - col);
  394. } while (col > 0U);
  395. /* Update pointer pInA to point to starting address of next row */
  396. i = i + numColsB;
  397. pInA = pInA + numColsA;
  398. /* Decrement row loop counter */
  399. row--;
  400. } while (row > 0U);
  401. /* Set status as ARM_MATH_SUCCESS */
  402. status = ARM_MATH_SUCCESS;
  403. }
  404. /* Return to application */
  405. return (status);
  406. }
  407. #endif
  408. /**
  409. * @} end of MatrixMult group
  410. */