arm_mat_mult_q31.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_q31.c
  4. * Description: Q31 matrix multiplication
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  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. /**
  30. @ingroup groupMatrix
  31. */
  32. /**
  33. @addtogroup MatrixMult
  34. @{
  35. */
  36. /**
  37. @brief Q31 matrix multiplication.
  38. @param[in] pSrcA points to the first input matrix structure
  39. @param[in] pSrcB points to the second input matrix structure
  40. @param[out] pDst points to output matrix structure
  41. @return execution status
  42. - \ref ARM_MATH_SUCCESS : Operation successful
  43. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  44. @par Scaling and Overflow Behavior
  45. The function is implemented using an internal 64-bit accumulator.
  46. The accumulator has a 2.62 format and maintains full precision of the intermediate
  47. multiplication results but provides only a single guard bit. There is no saturation
  48. on intermediate additions. Thus, if the accumulator overflows it wraps around and
  49. distorts the result. The input signals should be scaled down to avoid intermediate
  50. overflows. The input is thus scaled down by log2(numColsA) bits
  51. to avoid overflows, as a total of numColsA additions are performed internally.
  52. The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  53. @remark
  54. Refer to \ref arm_mat_mult_fast_q31() for a faster but less precise implementation of this function.
  55. */
  56. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  57. #define MATRIX_DIM2 2
  58. #define MATRIX_DIM3 3
  59. #define MATRIX_DIM4 4
  60. __STATIC_INLINE arm_status arm_mat_mult_q31_2x2_mve(
  61. const arm_matrix_instance_q31 * pSrcA,
  62. const arm_matrix_instance_q31 * pSrcB,
  63. arm_matrix_instance_q31 * pDst)
  64. {
  65. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  66. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  67. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  68. uint32x4_t vecColBOffs;
  69. q31_t *pInA0 = pInA;
  70. q31_t *pInA1 = pInA0 + MATRIX_DIM2;
  71. q63_t acc0, acc1;
  72. q31x4_t vecB, vecA0, vecA1;
  73. /* enable predication to disable half of vector elements */
  74. mve_pred16_t p0 = vctp32q(MATRIX_DIM2);
  75. vecColBOffs = vidupq_u32((uint32_t)0, 1);
  76. vecColBOffs = vecColBOffs * MATRIX_DIM2;
  77. pInB = pSrcB->pData;
  78. /* load 1st B column (partial load) */
  79. vecB = vldrwq_gather_shifted_offset_z_s32(pInB, vecColBOffs, p0);
  80. /* load A rows */
  81. vecA0 = vldrwq_s32(pInA0);
  82. vecA1 = vldrwq_s32(pInA1);
  83. acc0 = vrmlaldavhq(vecA0, vecB);
  84. acc1 = vrmlaldavhq(vecA1, vecB);
  85. acc0 = asrl(acc0, 23);
  86. acc1 = asrl(acc1, 23);
  87. pOut[0 * MATRIX_DIM2] = (q31_t) acc0;
  88. pOut[1 * MATRIX_DIM2] = (q31_t) acc1;
  89. pOut++;
  90. /* move to next B column */
  91. pInB = pInB + 1;
  92. vecB = vldrwq_gather_shifted_offset_z_s32(pInB, vecColBOffs, p0);
  93. acc0 = vrmlaldavhq(vecA0, vecB);
  94. acc1 = vrmlaldavhq(vecA1, vecB);
  95. acc0 = asrl(acc0, 23);
  96. acc1 = asrl(acc1, 23);
  97. pOut[0 * MATRIX_DIM2] = (q31_t) acc0;
  98. pOut[1 * MATRIX_DIM2] = (q31_t) acc1;
  99. /*
  100. * Return to application
  101. */
  102. return (ARM_MATH_SUCCESS);
  103. }
  104. __STATIC_INLINE arm_status arm_mat_mult_q31_3x3_mve(
  105. const arm_matrix_instance_q31 * pSrcA,
  106. const arm_matrix_instance_q31 * pSrcB,
  107. arm_matrix_instance_q31 * pDst)
  108. {
  109. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  110. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  111. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  112. uint32x4_t vecColBOffs;
  113. q31_t *pInA0 = pInA;
  114. q31_t *pInA1 = pInA0 + MATRIX_DIM3;
  115. q31_t *pInA2 = pInA1 + MATRIX_DIM3;
  116. q63_t acc0, acc1, acc2;
  117. q31x4_t vecB, vecA;
  118. /* enable predication to disable last (4th) vector element */
  119. mve_pred16_t p0 = vctp32q(MATRIX_DIM3);
  120. vecColBOffs = vidupq_u32((uint32_t)0, 1);
  121. vecColBOffs = vecColBOffs * MATRIX_DIM3;
  122. pInB = pSrcB->pData;
  123. vecB = vldrwq_gather_shifted_offset_z_s32(pInB, vecColBOffs, p0);
  124. vecA = vldrwq_s32(pInA0);
  125. acc0 = vrmlaldavhq(vecA, vecB);
  126. vecA = vldrwq_s32(pInA1);
  127. acc1 = vrmlaldavhq(vecA, vecB);
  128. vecA = vldrwq_s32(pInA2);
  129. acc2 = vrmlaldavhq(vecA, vecB);
  130. acc0 = asrl(acc0, 23);
  131. acc1 = asrl(acc1, 23);
  132. acc2 = asrl(acc2, 23);
  133. pOut[0 * MATRIX_DIM3] = (q31_t) acc0;
  134. pOut[1 * MATRIX_DIM3] = (q31_t) acc1;
  135. pOut[2 * MATRIX_DIM3] = (q31_t) acc2;
  136. pOut++;
  137. /* move to next B column */
  138. pInB = pInB + 1;
  139. vecB = vldrwq_gather_shifted_offset_z_s32(pInB, vecColBOffs, p0);
  140. vecA = vldrwq_s32(pInA0);
  141. acc0 = vrmlaldavhq(vecA, vecB);
  142. vecA = vldrwq_s32(pInA1);
  143. acc1 = vrmlaldavhq(vecA, vecB);
  144. vecA = vldrwq_s32(pInA2);
  145. acc2 = vrmlaldavhq(vecA, vecB);
  146. acc0 = asrl(acc0, 23);
  147. acc1 = asrl(acc1, 23);
  148. acc2 = asrl(acc2, 23);
  149. pOut[0 * MATRIX_DIM3] = (q31_t) acc0;
  150. pOut[1 * MATRIX_DIM3] = (q31_t) acc1;
  151. pOut[2 * MATRIX_DIM3] = (q31_t) acc2;
  152. pOut++;
  153. /* move to next B column */
  154. pInB = pInB + 1;
  155. vecB = vldrwq_gather_shifted_offset_z_s32(pInB, vecColBOffs, p0);
  156. vecA = vldrwq_s32(pInA0);
  157. acc0 = vrmlaldavhq(vecA, vecB);
  158. vecA = vldrwq_s32(pInA1);
  159. acc1 = vrmlaldavhq(vecA, vecB);
  160. vecA = vldrwq_s32(pInA2);
  161. acc2 = vrmlaldavhq(vecA, vecB);
  162. acc0 = asrl(acc0, 23);
  163. acc1 = asrl(acc1, 23);
  164. acc2 = asrl(acc2, 23);
  165. pOut[0 * MATRIX_DIM3] = (q31_t) acc0;
  166. pOut[1 * MATRIX_DIM3] = (q31_t) acc1;
  167. pOut[2 * MATRIX_DIM3] = (q31_t) acc2;
  168. /*
  169. * Return to application
  170. */
  171. return (ARM_MATH_SUCCESS);
  172. }
  173. __STATIC_INLINE arm_status arm_mat_mult_q31_4x4_mve(
  174. const arm_matrix_instance_q31 * pSrcA,
  175. const arm_matrix_instance_q31 * pSrcB,
  176. arm_matrix_instance_q31 * pDst)
  177. {
  178. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  179. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  180. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  181. uint32x4_t vecColBOffs;
  182. q31_t *pInA0 = pInA;
  183. q31_t *pInA1 = pInA0 + MATRIX_DIM4;
  184. q31_t *pInA2 = pInA1 + MATRIX_DIM4;
  185. q31_t *pInA3 = pInA2 + MATRIX_DIM4;
  186. q63_t acc0, acc1, acc2, acc3;
  187. q31x4_t vecB, vecA;
  188. vecColBOffs = vidupq_u32((uint32_t)0, 4);
  189. pInB = pSrcB->pData;
  190. vecB = vldrwq_gather_shifted_offset_s32(pInB, vecColBOffs);
  191. vecA = vldrwq_s32(pInA0);
  192. acc0 = vrmlaldavhq(vecA, vecB);
  193. vecA = vldrwq_s32(pInA1);
  194. acc1 = vrmlaldavhq(vecA, vecB);
  195. vecA = vldrwq_s32(pInA2);
  196. acc2 = vrmlaldavhq(vecA, vecB);
  197. vecA = vldrwq_s32(pInA3);
  198. acc3 = vrmlaldavhq(vecA, vecB);
  199. acc0 = asrl(acc0, 23);
  200. acc1 = asrl(acc1, 23);
  201. acc2 = asrl(acc2, 23);
  202. acc3 = asrl(acc3, 23);
  203. pOut[0 * MATRIX_DIM4] = (q31_t) acc0;
  204. pOut[1 * MATRIX_DIM4] = (q31_t) acc1;
  205. pOut[2 * MATRIX_DIM4] = (q31_t) acc2;
  206. pOut[3 * MATRIX_DIM4] = (q31_t) acc3;
  207. pOut++;
  208. /* move to next B column */
  209. pInB = pInB + 1;
  210. vecB = vldrwq_gather_shifted_offset_s32(pInB, vecColBOffs);
  211. vecA = vldrwq_s32(pInA0);
  212. acc0 = vrmlaldavhq(vecA, vecB);
  213. vecA = vldrwq_s32(pInA1);
  214. acc1 = vrmlaldavhq(vecA, vecB);
  215. vecA = vldrwq_s32(pInA2);
  216. acc2 = vrmlaldavhq(vecA, vecB);
  217. vecA = vldrwq_s32(pInA3);
  218. acc3 = vrmlaldavhq(vecA, vecB);
  219. acc0 = asrl(acc0, 23);
  220. acc1 = asrl(acc1, 23);
  221. acc2 = asrl(acc2, 23);
  222. acc3 = asrl(acc3, 23);
  223. pOut[0 * MATRIX_DIM4] = (q31_t) acc0;
  224. pOut[1 * MATRIX_DIM4] = (q31_t) acc1;
  225. pOut[2 * MATRIX_DIM4] = (q31_t) acc2;
  226. pOut[3 * MATRIX_DIM4] = (q31_t) acc3;
  227. pOut++;
  228. /* move to next B column */
  229. pInB = pInB + 1;
  230. vecB = vldrwq_gather_shifted_offset_s32(pInB, vecColBOffs);
  231. vecA = vldrwq_s32(pInA0);
  232. acc0 = vrmlaldavhq(vecA, vecB);
  233. vecA = vldrwq_s32(pInA1);
  234. acc1 = vrmlaldavhq(vecA, vecB);
  235. vecA = vldrwq_s32(pInA2);
  236. acc2 = vrmlaldavhq(vecA, vecB);
  237. vecA = vldrwq_s32(pInA3);
  238. acc3 = vrmlaldavhq(vecA, vecB);
  239. acc0 = asrl(acc0, 23);
  240. acc1 = asrl(acc1, 23);
  241. acc2 = asrl(acc2, 23);
  242. acc3 = asrl(acc3, 23);
  243. pOut[0 * MATRIX_DIM4] = (q31_t) acc0;
  244. pOut[1 * MATRIX_DIM4] = (q31_t) acc1;
  245. pOut[2 * MATRIX_DIM4] = (q31_t) acc2;
  246. pOut[3 * MATRIX_DIM4] = (q31_t) acc3;
  247. pOut++;
  248. /* move to next B column */
  249. pInB = pInB + 1;
  250. vecB = vldrwq_gather_shifted_offset_s32(pInB, vecColBOffs);
  251. vecA = vldrwq_s32(pInA0);
  252. acc0 = vrmlaldavhq(vecA, vecB);
  253. vecA = vldrwq_s32(pInA1);
  254. acc1 = vrmlaldavhq(vecA, vecB);
  255. vecA = vldrwq_s32(pInA2);
  256. acc2 = vrmlaldavhq(vecA, vecB);
  257. vecA = vldrwq_s32(pInA3);
  258. acc3 = vrmlaldavhq(vecA, vecB);
  259. acc0 = asrl(acc0, 23);
  260. acc1 = asrl(acc1, 23);
  261. acc2 = asrl(acc2, 23);
  262. acc3 = asrl(acc3, 23);
  263. pOut[0 * MATRIX_DIM4] = (q31_t) acc0;
  264. pOut[1 * MATRIX_DIM4] = (q31_t) acc1;
  265. pOut[2 * MATRIX_DIM4] = (q31_t) acc2;
  266. pOut[3 * MATRIX_DIM4] = (q31_t) acc3;
  267. /*
  268. * Return to application
  269. */
  270. return (ARM_MATH_SUCCESS);
  271. }
  272. arm_status arm_mat_mult_q31(
  273. const arm_matrix_instance_q31 * pSrcA,
  274. const arm_matrix_instance_q31 * pSrcB,
  275. arm_matrix_instance_q31 * pDst)
  276. {
  277. q31_t const *pInB = (q31_t const *)pSrcB->pData; /* input data matrix pointer B */
  278. q31_t const *pInA = (q31_t const *)pSrcA->pData; /* input data matrix pointer A */
  279. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  280. q31_t *px; /* Temporary output data matrix pointer */
  281. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  282. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  283. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  284. uint16_t col, i = 0U, row = numRowsA; /* loop counters */
  285. arm_status status; /* status of matrix multiplication */
  286. uint32x4_t vecOffs, vecColBOffs;
  287. uint32_t blkCnt, rowCnt; /* loop counters */
  288. #ifdef ARM_MATH_MATRIX_CHECK
  289. /* Check for matrix mismatch condition */
  290. if ((pSrcA->numCols != pSrcB->numRows) ||
  291. (pSrcA->numRows != pDst->numRows) ||
  292. (pSrcB->numCols != pDst->numCols) )
  293. {
  294. /* Set status as ARM_MATH_SIZE_MISMATCH */
  295. status = ARM_MATH_SIZE_MISMATCH;
  296. }
  297. else
  298. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  299. {
  300. /* small squared matrix specialized routines */
  301. if(numRowsA == numColsB && numColsB == numColsA) {
  302. if (numRowsA == 1)
  303. {
  304. q63_t sum = (q63_t) *pInA * *pInB;
  305. pOut[0] = (q31_t)(sum >> 31);
  306. return (ARM_MATH_SUCCESS);
  307. }
  308. else if(numRowsA == 2)
  309. return arm_mat_mult_q31_2x2_mve(pSrcA, pSrcB, pDst);
  310. else if(numRowsA == 3)
  311. return arm_mat_mult_q31_3x3_mve(pSrcA, pSrcB, pDst);
  312. else if (numRowsA == 4)
  313. return arm_mat_mult_q31_4x4_mve(pSrcA, pSrcB, pDst);
  314. }
  315. vecColBOffs = vidupq_u32((uint32_t)0, 1);
  316. vecColBOffs = vecColBOffs * (uint32_t) (numColsB);
  317. /*
  318. * The following loop performs the dot-product of each row in pSrcA with each column in pSrcB
  319. */
  320. /*
  321. * row loop
  322. */
  323. rowCnt = row >> 2;
  324. while (rowCnt > 0U)
  325. {
  326. /*
  327. * Output pointer is set to starting address of the row being processed
  328. */
  329. px = pOut + i;
  330. i = i + 4 * numColsB;
  331. /*
  332. * For every row wise process, the column loop counter is to be initiated
  333. */
  334. col = numColsB;
  335. /*
  336. * For every row wise process, the pInB pointer is set
  337. * to the starting address of the pSrcB data
  338. */
  339. pInB = (q31_t const *)pSrcB->pData;
  340. /*
  341. * column loop
  342. */
  343. while (col > 0U)
  344. {
  345. /*
  346. * generate 4 columns elements
  347. */
  348. /*
  349. * Matrix A columns number of MAC operations are to be performed
  350. */
  351. q31_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec;
  352. q31_t const *pInA0 = pInA;
  353. q31_t const *pInA1 = pInA0 + numColsA;
  354. q31_t const *pInA2 = pInA1 + numColsA;
  355. q31_t const *pInA3 = pInA2 + numColsA;
  356. q63_t acc0, acc1, acc2, acc3;
  357. acc0 = 0LL;
  358. acc1 = 0LL;
  359. acc2 = 0LL;
  360. acc3 = 0LL;
  361. pSrcA0Vec = (q31_t const *) pInA0;
  362. pSrcA1Vec = (q31_t const *) pInA1;
  363. pSrcA2Vec = (q31_t const *) pInA2;
  364. pSrcA3Vec = (q31_t const *) pInA3;
  365. vecOffs = vecColBOffs;
  366. /* process 1 x 4 block output */
  367. blkCnt = numColsA >> 2;
  368. while (blkCnt > 0U)
  369. {
  370. q31x4_t vecB, vecA;
  371. vecB = vldrwq_gather_shifted_offset(pInB, vecOffs);
  372. /* move Matrix B read offsets, 4 rows down */
  373. vecOffs = vecOffs + (uint32_t) (numColsB * 4);
  374. vecA = vld1q(pSrcA0Vec); pSrcA0Vec += 4;
  375. acc0 = vrmlaldavhaq(acc0, vecA, vecB);
  376. vecA = vld1q(pSrcA1Vec); pSrcA1Vec += 4;
  377. acc1 = vrmlaldavhaq(acc1, vecA, vecB);
  378. vecA = vld1q(pSrcA2Vec); pSrcA2Vec += 4;
  379. acc2 = vrmlaldavhaq(acc2, vecA, vecB);
  380. vecA = vld1q(pSrcA3Vec); pSrcA3Vec += 4;
  381. acc3 = vrmlaldavhaq(acc3, vecA, vecB);
  382. blkCnt--;
  383. }
  384. /*
  385. * tail
  386. * (will be merged thru tail predication)
  387. */
  388. blkCnt = numColsA & 3;
  389. if (blkCnt > 0U)
  390. {
  391. mve_pred16_t p0 = vctp32q(blkCnt);
  392. q31x4_t vecB, vecA;
  393. vecB = vldrwq_gather_shifted_offset_z(pInB, vecOffs, p0);
  394. //vecOffs = vecOffs + (uint32_t) (numColsB * 4);
  395. vecA = vld1q(pSrcA0Vec); pSrcA0Vec += 4;
  396. acc0 = vrmlaldavhaq(acc0, vecA, vecB);
  397. vecA = vld1q(pSrcA1Vec); pSrcA1Vec += 4;
  398. acc1 = vrmlaldavhaq(acc1, vecA, vecB);
  399. vecA = vld1q(pSrcA2Vec); pSrcA2Vec += 4;
  400. acc2 = vrmlaldavhaq(acc2, vecA, vecB);
  401. vecA = vld1q(pSrcA3Vec); pSrcA3Vec += 4;
  402. acc3 = vrmlaldavhaq(acc3, vecA, vecB);
  403. }
  404. acc0 = asrl(acc0, 23);
  405. acc1 = asrl(acc1, 23);
  406. acc2 = asrl(acc2, 23);
  407. acc3 = asrl(acc3, 23);
  408. px[0] = (q31_t) acc0;
  409. px[1 * numColsB] = (q31_t) acc1;
  410. px[2 * numColsB] = (q31_t) acc2;
  411. px[3 * numColsB] = (q31_t) acc3;
  412. px++;
  413. /*
  414. * Decrement the column loop counter
  415. */
  416. col--;
  417. /*
  418. * Update the pointer pInB to point to the starting address of the next column
  419. */
  420. pInB = (q31_t const *)pSrcB->pData + (numColsB - col);
  421. }
  422. /*
  423. * Update the pointer pInA to point to the starting address of the next row
  424. */
  425. pInA += (numColsA * 4);
  426. /*
  427. * Decrement the row loop counter
  428. */
  429. rowCnt --;
  430. }
  431. rowCnt = row & 3;
  432. while (rowCnt > 0U)
  433. {
  434. /*
  435. * Output pointer is set to starting address of the row being processed
  436. */
  437. px = pOut + i;
  438. i = i + numColsB;
  439. /*
  440. * For every row wise process, the column loop counter is to be initiated
  441. */
  442. col = numColsB;
  443. /*
  444. * For every row wise process, the pInB pointer is set
  445. * to the starting address of the pSrcB data
  446. */
  447. pInB = (q31_t const *)pSrcB->pData;
  448. /*
  449. * column loop
  450. */
  451. while (col > 0U)
  452. {
  453. /*
  454. * generate 4 columns elements
  455. */
  456. /*
  457. * Matrix A columns number of MAC operations are to be performed
  458. */
  459. q31_t const *pSrcA0Vec;
  460. q31_t const *pInA0 = pInA;
  461. q63_t acc0;
  462. acc0 = 0LL;
  463. pSrcA0Vec = (q31_t const *) pInA0;
  464. vecOffs = vecColBOffs;
  465. /* process 1 x 4 block output */
  466. blkCnt = numColsA >> 2;
  467. while (blkCnt > 0U)
  468. {
  469. q31x4_t vecB, vecA;
  470. vecB = vldrwq_gather_shifted_offset(pInB, vecOffs);
  471. /* move Matrix B read offsets, 4 rows down */
  472. vecOffs = vecOffs + (uint32_t) (numColsB * 4);
  473. vecA = vld1q(pSrcA0Vec); pSrcA0Vec += 4;
  474. acc0 = vrmlaldavhaq(acc0, vecA, vecB);
  475. blkCnt--;
  476. }
  477. /*
  478. * tail
  479. * (will be merged thru tail predication)
  480. */
  481. blkCnt = numColsA & 3;
  482. if (blkCnt > 0U)
  483. {
  484. mve_pred16_t p0 = vctp32q(blkCnt);
  485. q31x4_t vecB, vecA;
  486. vecB = vldrwq_gather_shifted_offset_z(pInB, vecOffs, p0);
  487. //vecOffs = vecOffs + (uint32_t) (numColsB * 4);
  488. vecA = vld1q(pSrcA0Vec);
  489. pSrcA0Vec += 4;
  490. acc0 = vrmlaldavhaq(acc0, vecA, vecB);
  491. }
  492. acc0 = asrl(acc0, 23);
  493. px[0] = (q31_t) acc0;
  494. px++;
  495. /*
  496. * Decrement the column loop counter
  497. */
  498. col--;
  499. /*
  500. * Update the pointer pInB to point to the starting address of the next column
  501. */
  502. pInB = (q31_t const *)pSrcB->pData + (numColsB - col);
  503. }
  504. /*
  505. * Update the pointer pInA to point to the starting address of the next row
  506. */
  507. pInA += numColsA;
  508. /*
  509. * Decrement the row loop counter
  510. */
  511. rowCnt--;
  512. }
  513. /*
  514. * set status as ARM_MATH_SUCCESS
  515. */
  516. status = ARM_MATH_SUCCESS;
  517. }
  518. /* Return to application */
  519. return (status);
  520. }
  521. #else
  522. arm_status arm_mat_mult_q31(
  523. const arm_matrix_instance_q31 * pSrcA,
  524. const arm_matrix_instance_q31 * pSrcB,
  525. arm_matrix_instance_q31 * pDst)
  526. {
  527. q31_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  528. q31_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  529. q31_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  530. q31_t *pInB = pSrcB->pData; /* Input data matrix pointer B */
  531. q31_t *pOut = pDst->pData; /* Output data matrix pointer */
  532. q31_t *px; /* Temporary output data matrix pointer */
  533. q63_t sum; /* Accumulator */
  534. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  535. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  536. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  537. uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  538. arm_status status; /* Status of matrix multiplication */
  539. #ifdef ARM_MATH_MATRIX_CHECK
  540. /* Check for matrix mismatch condition */
  541. if ((pSrcA->numCols != pSrcB->numRows) ||
  542. (pSrcA->numRows != pDst->numRows) ||
  543. (pSrcB->numCols != pDst->numCols) )
  544. {
  545. /* Set status as ARM_MATH_SIZE_MISMATCH */
  546. status = ARM_MATH_SIZE_MISMATCH;
  547. }
  548. else
  549. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  550. {
  551. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  552. /* row loop */
  553. do
  554. {
  555. /* Output pointer is set to starting address of row being processed */
  556. px = pOut + i;
  557. /* For every row wise process, column loop counter is to be initiated */
  558. col = numColsB;
  559. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  560. pIn2 = pSrcB->pData;
  561. /* column loop */
  562. do
  563. {
  564. /* Set the variable sum, that acts as accumulator, to zero */
  565. sum = 0;
  566. /* Initialize pointer pIn1 to point to starting address of column being processed */
  567. pIn1 = pInA;
  568. #if defined (ARM_MATH_LOOPUNROLL)
  569. /* Loop unrolling: Compute 4 MACs at a time. */
  570. colCnt = numColsA >> 2U;
  571. /* matrix multiplication */
  572. while (colCnt > 0U)
  573. {
  574. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  575. /* Perform the multiply-accumulates */
  576. sum += (q63_t) *pIn1++ * *pIn2;
  577. pIn2 += numColsB;
  578. sum += (q63_t) *pIn1++ * *pIn2;
  579. pIn2 += numColsB;
  580. sum += (q63_t) *pIn1++ * *pIn2;
  581. pIn2 += numColsB;
  582. sum += (q63_t) *pIn1++ * *pIn2;
  583. pIn2 += numColsB;
  584. /* Decrement loop counter */
  585. colCnt--;
  586. }
  587. /* Loop unrolling: Compute remaining MACs */
  588. colCnt = numColsA % 0x4U;
  589. #else
  590. /* Initialize cntCnt with number of columns */
  591. colCnt = numColsA;
  592. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  593. while (colCnt > 0U)
  594. {
  595. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  596. /* Perform the multiply-accumulates */
  597. sum += (q63_t) *pIn1++ * *pIn2;
  598. pIn2 += numColsB;
  599. /* Decrement loop counter */
  600. colCnt--;
  601. }
  602. /* Convert result from 2.62 to 1.31 format and store in destination buffer */
  603. *px++ = (q31_t) (sum >> 31);
  604. /* Decrement column loop counter */
  605. col--;
  606. /* Update pointer pIn2 to point to starting address of next column */
  607. pIn2 = pInB + (numColsB - col);
  608. } while (col > 0U);
  609. /* Update pointer pInA to point to starting address of next row */
  610. i = i + numColsB;
  611. pInA = pInA + numColsA;
  612. /* Decrement row loop counter */
  613. row--;
  614. } while (row > 0U);
  615. /* Set status as ARM_MATH_SUCCESS */
  616. status = ARM_MATH_SUCCESS;
  617. }
  618. /* Return to application */
  619. return (status);
  620. }
  621. #endif /* defined(ARM_MATH_MVEI) */
  622. /**
  623. @} end of MatrixMult group
  624. */