arm_mat_mult_q15.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_q15.c
  4. * Description: Q15 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 Q15 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. @param[in] pState points to the array for storing intermediate results (Unused)
  42. @return execution status
  43. - \ref ARM_MATH_SUCCESS : Operation successful
  44. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  45. @par Scaling and Overflow Behavior
  46. The function is implemented using an internal 64-bit accumulator. The inputs to the
  47. multiplications are in 1.15 format and multiplications yield a 2.30 result.
  48. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  49. This approach provides 33 guard bits and there is no risk of overflow.
  50. The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits
  51. and then saturated to 1.15 format.
  52. @par
  53. Refer to \ref arm_mat_mult_fast_q15() for a faster but less precise version of this function.
  54. */
  55. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  56. #define MVE_ASRL_SAT16(acc, shift) ((sqrshrl_sat48(acc, -(32-shift)) >> 32) & 0xffffffff)
  57. #define MATRIX_DIM2 2
  58. #define MATRIX_DIM3 3
  59. #define MATRIX_DIM4 4
  60. __STATIC_INLINE arm_status arm_mat_mult_q15_2x2_mve(
  61. const arm_matrix_instance_q15 * pSrcA,
  62. const arm_matrix_instance_q15 * pSrcB,
  63. arm_matrix_instance_q15 * pDst)
  64. {
  65. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  66. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  67. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  68. uint16x8_t vecColBOffs;
  69. q15_t *pInA0 = pInA;
  70. q15_t *pInA1 = pInA0 + MATRIX_DIM2;
  71. q63_t acc0, acc1;
  72. q15x8_t vecB, vecA0, vecA1;
  73. mve_pred16_t p0 = vctp16q(MATRIX_DIM2);
  74. vecColBOffs = vidupq_u16((uint32_t)0, 2); /* MATRIX_DIM2 */
  75. pInB = pSrcB->pData;
  76. vecB = vldrhq_gather_shifted_offset_z_s16((q15_t const *)pInB, vecColBOffs, p0);
  77. vecA0 = vldrhq_s16(pInA0);
  78. vecA1 = vldrhq_s16(pInA1);
  79. acc0 = vmlaldavq(vecA0, vecB);
  80. acc1 = vmlaldavq(vecA1, vecB);
  81. acc0 = asrl(acc0, 15);
  82. acc1 = asrl(acc1, 15);
  83. pOut[0 * MATRIX_DIM2] = (q15_t) __SSAT(acc0, 16);
  84. pOut[1 * MATRIX_DIM2] = (q15_t) __SSAT(acc1, 16);
  85. pOut++;
  86. /* move to next B column */
  87. pInB = pInB + 1;
  88. vecB = vldrhq_gather_shifted_offset_z_s16(pInB, vecColBOffs, p0);
  89. acc0 = vmlaldavq(vecA0, vecB);
  90. acc1 = vmlaldavq(vecA1, vecB);
  91. acc0 = asrl(acc0, 15);
  92. acc1 = asrl(acc1, 15);
  93. pOut[0 * MATRIX_DIM2] = (q15_t) __SSAT(acc0, 16);
  94. pOut[1 * MATRIX_DIM2] = (q15_t) __SSAT(acc1, 16);
  95. /*
  96. * Return to application
  97. */
  98. return (ARM_MATH_SUCCESS);
  99. }
  100. __STATIC_INLINE arm_status arm_mat_mult_q15_3x3_mve(
  101. const arm_matrix_instance_q15 * pSrcA,
  102. const arm_matrix_instance_q15 * pSrcB,
  103. arm_matrix_instance_q15 * pDst)
  104. {
  105. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  106. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  107. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  108. uint16x8_t vecColBOffs;
  109. q15_t *pInA0 = pInA;
  110. q15_t *pInA1 = pInA0 + MATRIX_DIM3;
  111. q15_t *pInA2 = pInA1 + MATRIX_DIM3;
  112. q63_t acc0, acc1, acc2;
  113. q15x8_t vecB, vecA0, vecA1, vecA2;
  114. mve_pred16_t p0 = vctp16q(MATRIX_DIM3);
  115. vecColBOffs = vidupq_u16((uint32_t)0, 1);
  116. vecColBOffs = vecColBOffs * MATRIX_DIM3;
  117. pInB = pSrcB->pData;
  118. vecB = vldrhq_gather_shifted_offset_z_s16((q15_t const *)pInB, vecColBOffs, p0);
  119. vecA0 = vldrhq_s16(pInA0);
  120. vecA1 = vldrhq_s16(pInA1);
  121. vecA2 = vldrhq_s16(pInA2);
  122. acc0 = vmlaldavq(vecA0, vecB);
  123. acc1 = vmlaldavq(vecA1, vecB);
  124. acc2 = vmlaldavq(vecA2, vecB);
  125. acc0 = asrl(acc0, 15);
  126. acc1 = asrl(acc1, 15);
  127. acc2 = asrl(acc2, 15);
  128. pOut[0 * MATRIX_DIM3] = (q15_t) __SSAT(acc0, 16);
  129. pOut[1 * MATRIX_DIM3] = (q15_t) __SSAT(acc1, 16);
  130. pOut[2 * MATRIX_DIM3] = (q15_t) __SSAT(acc2, 16);
  131. pOut++;
  132. /* move to next B column */
  133. pInB = pInB + 1;
  134. vecB = vldrhq_gather_shifted_offset_z_s16(pInB, vecColBOffs, p0);
  135. acc0 = vmlaldavq(vecA0, vecB);
  136. acc1 = vmlaldavq(vecA1, vecB);
  137. acc2 = vmlaldavq(vecA2, vecB);
  138. acc0 = asrl(acc0, 15);
  139. acc1 = asrl(acc1, 15);
  140. acc2 = asrl(acc2, 15);
  141. pOut[0 * MATRIX_DIM3] = (q15_t) __SSAT(acc0, 16);
  142. pOut[1 * MATRIX_DIM3] = (q15_t) __SSAT(acc1, 16);
  143. pOut[2 * MATRIX_DIM3] = (q15_t) __SSAT(acc2, 16);
  144. pOut++;
  145. /* move to next B column */
  146. pInB = pInB + 1;
  147. vecB = vldrhq_gather_shifted_offset_z_s16(pInB, vecColBOffs, p0);
  148. acc0 = vmlaldavq(vecA0, vecB);
  149. acc1 = vmlaldavq(vecA1, vecB);
  150. acc2 = vmlaldavq(vecA2, vecB);
  151. acc0 = asrl(acc0, 15);
  152. acc1 = asrl(acc1, 15);
  153. acc2 = asrl(acc2, 15);
  154. pOut[0 * MATRIX_DIM3] = (q15_t) __SSAT(acc0, 16);
  155. pOut[1 * MATRIX_DIM3] = (q15_t) __SSAT(acc1, 16);
  156. pOut[2 * MATRIX_DIM3] = (q15_t) __SSAT(acc2, 16);
  157. /*
  158. * Return to application
  159. */
  160. return (ARM_MATH_SUCCESS);
  161. }
  162. __STATIC_INLINE arm_status arm_mat_mult_q15_4x4_mve(
  163. const arm_matrix_instance_q15 * pSrcA,
  164. const arm_matrix_instance_q15 * pSrcB,
  165. arm_matrix_instance_q15 * pDst)
  166. {
  167. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  168. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  169. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  170. uint16x8_t vecColBOffs;
  171. q15_t *pInA0 = pInA;
  172. q15_t *pInA1 = pInA0 + MATRIX_DIM4;
  173. q15_t *pInA2 = pInA1 + MATRIX_DIM4;
  174. q15_t *pInA3 = pInA2 + MATRIX_DIM4;
  175. q63_t acc0, acc1, acc2, acc3;
  176. q15x8_t vecB, vecA0, vecA1, vecA2, vecA3;
  177. mve_pred16_t p0 = vctp16q(MATRIX_DIM4);
  178. vecColBOffs = vidupq_u16((uint32_t)0, 4);
  179. pInB = pSrcB->pData;
  180. vecB = vldrhq_gather_shifted_offset_z_s16((q15_t const *)pInB, vecColBOffs, p0);
  181. vecA0 = vldrhq_s16(pInA0);
  182. vecA1 = vldrhq_s16(pInA1);
  183. vecA2 = vldrhq_s16(pInA2);
  184. vecA3 = vldrhq_s16(pInA3);
  185. acc0 = vmlaldavq(vecA0, vecB);
  186. acc1 = vmlaldavq(vecA1, vecB);
  187. acc2 = vmlaldavq(vecA2, vecB);
  188. acc3 = vmlaldavq(vecA3, vecB);
  189. acc0 = asrl(acc0, 15);
  190. acc1 = asrl(acc1, 15);
  191. acc2 = asrl(acc2, 15);
  192. acc3 = asrl(acc3, 15);
  193. pOut[0 * MATRIX_DIM4] = (q15_t) __SSAT(acc0, 16);
  194. pOut[1 * MATRIX_DIM4] = (q15_t) __SSAT(acc1, 16);
  195. pOut[2 * MATRIX_DIM4] = (q15_t) __SSAT(acc2, 16);
  196. pOut[3 * MATRIX_DIM4] = (q15_t) __SSAT(acc3, 16);
  197. pOut++;
  198. /* move to next B column */
  199. pInB = pInB + 1;
  200. vecB = vldrhq_gather_shifted_offset_z_s16(pInB, vecColBOffs, p0);
  201. acc0 = vmlaldavq(vecA0, vecB);
  202. acc1 = vmlaldavq(vecA1, vecB);
  203. acc2 = vmlaldavq(vecA2, vecB);
  204. acc3 = vmlaldavq(vecA3, vecB);
  205. acc0 = asrl(acc0, 15);
  206. acc1 = asrl(acc1, 15);
  207. acc2 = asrl(acc2, 15);
  208. acc3 = asrl(acc3, 15);
  209. pOut[0 * MATRIX_DIM4] = (q15_t) __SSAT(acc0, 16);
  210. pOut[1 * MATRIX_DIM4] = (q15_t) __SSAT(acc1, 16);
  211. pOut[2 * MATRIX_DIM4] = (q15_t) __SSAT(acc2, 16);
  212. pOut[3 * MATRIX_DIM4] = (q15_t) __SSAT(acc3, 16);
  213. pOut++;
  214. /* move to next B column */
  215. pInB = pInB + 1;
  216. vecB = vldrhq_gather_shifted_offset_z_s16(pInB, vecColBOffs, p0);
  217. acc0 = vmlaldavq(vecA0, vecB);
  218. acc1 = vmlaldavq(vecA1, vecB);
  219. acc2 = vmlaldavq(vecA2, vecB);
  220. acc3 = vmlaldavq(vecA3, vecB);
  221. acc0 = asrl(acc0, 15);
  222. acc1 = asrl(acc1, 15);
  223. acc2 = asrl(acc2, 15);
  224. acc3 = asrl(acc3, 15);
  225. pOut[0 * MATRIX_DIM4] = (q15_t) __SSAT(acc0, 16);
  226. pOut[1 * MATRIX_DIM4] = (q15_t) __SSAT(acc1, 16);
  227. pOut[2 * MATRIX_DIM4] = (q15_t) __SSAT(acc2, 16);
  228. pOut[3 * MATRIX_DIM4] = (q15_t) __SSAT(acc3, 16);
  229. pOut++;
  230. /* move to next B column */
  231. pInB = pInB + 1;
  232. vecB = vldrhq_gather_shifted_offset_z_s16(pInB, vecColBOffs, p0);
  233. acc0 = vmlaldavq(vecA0, vecB);
  234. acc1 = vmlaldavq(vecA1, vecB);
  235. acc2 = vmlaldavq(vecA2, vecB);
  236. acc3 = vmlaldavq(vecA3, vecB);
  237. acc0 = asrl(acc0, 15);
  238. acc1 = asrl(acc1, 15);
  239. acc2 = asrl(acc2, 15);
  240. acc3 = asrl(acc3, 15);
  241. pOut[0 * MATRIX_DIM4] = (q15_t) __SSAT(acc0, 16);
  242. pOut[1 * MATRIX_DIM4] = (q15_t) __SSAT(acc1, 16);
  243. pOut[2 * MATRIX_DIM4] = (q15_t) __SSAT(acc2, 16);
  244. pOut[3 * MATRIX_DIM4] = (q15_t) __SSAT(acc3, 16);
  245. /*
  246. * Return to application
  247. */
  248. return (ARM_MATH_SUCCESS);
  249. }
  250. arm_status arm_mat_mult_q15(
  251. const arm_matrix_instance_q15 * pSrcA,
  252. const arm_matrix_instance_q15 * pSrcB,
  253. arm_matrix_instance_q15 * pDst,
  254. q15_t * pState)
  255. {
  256. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  257. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  258. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  259. q15_t *px; /* Temporary output data matrix pointer */
  260. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  261. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  262. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  263. uint16_t col, i = 0U, row = numRowsA; /* loop counters */
  264. uint16x8_t vecOffs, vecColBOffs;
  265. uint32_t blkCnt,rowCnt; /* loop counters */
  266. arm_status status; /* Status of matrix multiplication */
  267. (void)pState;
  268. #ifdef ARM_MATH_MATRIX_CHECK
  269. /* Check for matrix mismatch condition */
  270. if ((pSrcA->numCols != pSrcB->numRows) ||
  271. (pSrcA->numRows != pDst->numRows) ||
  272. (pSrcB->numCols != pDst->numCols) )
  273. {
  274. /* Set status as ARM_MATH_SIZE_MISMATCH */
  275. status = ARM_MATH_SIZE_MISMATCH;
  276. }
  277. else
  278. #endif
  279. {
  280. /* small squared matrix specialized routines */
  281. if(numRowsA == numColsB && numColsB == numColsA) {
  282. if (numRowsA == 1)
  283. {
  284. q63_t sum;
  285. sum = pInA[0] * pInB[0];
  286. pOut[0] = (q15_t) __SSAT((sum >> 15), 16);
  287. return (ARM_MATH_SUCCESS);
  288. }
  289. else if(numRowsA == 2)
  290. return arm_mat_mult_q15_2x2_mve(pSrcA, pSrcB, pDst);
  291. else if(numRowsA == 3)
  292. return arm_mat_mult_q15_3x3_mve(pSrcA, pSrcB, pDst);
  293. else if (numRowsA == 4)
  294. return arm_mat_mult_q15_4x4_mve(pSrcA, pSrcB, pDst);
  295. }
  296. vecColBOffs = vidupq_u16((uint32_t)0, 1);
  297. vecColBOffs = vecColBOffs * (uint16_t) (numColsB);
  298. /*
  299. * The following loop performs the dot-product of each row in pSrcA with each column in pSrcB
  300. */
  301. /*
  302. * row loop
  303. */
  304. rowCnt = row >> 2;
  305. while (rowCnt > 0U)
  306. {
  307. /*
  308. * Output pointer is set to starting address of the row being processed
  309. */
  310. px = pOut + i;
  311. i = i + 4 * numColsB;
  312. /*
  313. * For every row wise process, the column loop counter is to be initiated
  314. */
  315. col = numColsB;
  316. /*
  317. * For every row wise process, the pInB pointer is set
  318. * to the starting address of the pSrcB data
  319. */
  320. pInB = pSrcB->pData;
  321. /*
  322. * column loop
  323. */
  324. while (col > 0U)
  325. {
  326. /*
  327. * generate 4 columns elements
  328. */
  329. /*
  330. * Matrix A columns number of MAC operations are to be performed
  331. */
  332. q15_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec;
  333. q15_t *pInA0 = pInA;
  334. q15_t *pInA1 = pInA0 + numColsA;
  335. q15_t *pInA2 = pInA1 + numColsA;
  336. q15_t *pInA3 = pInA2 + numColsA;
  337. q63_t acc0, acc1, acc2, acc3;
  338. acc0 = 0LL;
  339. acc1 = 0LL;
  340. acc2 = 0LL;
  341. acc3 = 0LL;
  342. pSrcA0Vec = (q15_t const *) pInA0;
  343. pSrcA1Vec = (q15_t const *) pInA1;
  344. pSrcA2Vec = (q15_t const *) pInA2;
  345. pSrcA3Vec = (q15_t const *) pInA3;
  346. vecOffs = vecColBOffs;
  347. blkCnt = (numColsA) >> 3;
  348. while (blkCnt > 0U)
  349. {
  350. q15x8_t vecB, vecA;
  351. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  352. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  353. vecA = vld1q(pSrcA0Vec); pSrcA0Vec += 8;
  354. acc0 = vmlaldavaq(acc0, vecA, vecB);
  355. vecA = vld1q(pSrcA1Vec); pSrcA1Vec += 8;
  356. acc1 = vmlaldavaq(acc1, vecA, vecB);
  357. vecA = vld1q(pSrcA2Vec); pSrcA2Vec += 8;
  358. acc2 = vmlaldavaq(acc2, vecA, vecB);
  359. vecA = vld1q(pSrcA3Vec); pSrcA3Vec += 8;
  360. acc3 = vmlaldavaq(acc3, vecA, vecB);
  361. blkCnt--;
  362. }
  363. /*
  364. * tail
  365. */
  366. blkCnt = numColsA & 7;
  367. if (blkCnt > 0U)
  368. {
  369. mve_pred16_t p0 = vctp16q(blkCnt);
  370. q15x8_t vecB, vecA;
  371. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  372. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  373. vecA = vld1q(pSrcA0Vec);
  374. acc0 = vmlaldavaq_p(acc0, vecA, vecB, p0);
  375. vecA = vld1q(pSrcA1Vec);
  376. acc1 = vmlaldavaq_p(acc1, vecA, vecB, p0);
  377. vecA = vld1q(pSrcA2Vec);
  378. acc2 = vmlaldavaq_p(acc2, vecA, vecB, p0);
  379. vecA = vld1q(pSrcA3Vec);
  380. acc3 = vmlaldavaq_p(acc3, vecA, vecB, p0);
  381. }
  382. px[0] = (q15_t)MVE_ASRL_SAT16(acc0, 15);
  383. px[1 * numColsB] = (q15_t)MVE_ASRL_SAT16(acc1, 15);
  384. px[2 * numColsB] = (q15_t)MVE_ASRL_SAT16(acc2, 15);
  385. px[3 * numColsB] = (q15_t)MVE_ASRL_SAT16(acc3, 15);
  386. px++;
  387. /*
  388. * Decrement the column loop counter
  389. */
  390. col--;
  391. /*
  392. * Update the pointer pInB to point to the starting address of the next column
  393. */
  394. pInB = pSrcB->pData + (numColsB - col);
  395. }
  396. /*
  397. * Update the pointer pInA to point to the starting address of the next row
  398. */
  399. pInA += (numColsA * 4);
  400. /*
  401. * Decrement the row loop counter
  402. */
  403. rowCnt --;
  404. }
  405. rowCnt = row & 3;
  406. while (rowCnt > 0U)
  407. {
  408. /*
  409. * Output pointer is set to starting address of the row being processed
  410. */
  411. px = pOut + i;
  412. i = i + numColsB;
  413. /*
  414. * For every row wise process, the column loop counter is to be initiated
  415. */
  416. col = numColsB;
  417. /*
  418. * For every row wise process, the pInB pointer is set
  419. * to the starting address of the pSrcB data
  420. */
  421. pInB = pSrcB->pData;
  422. /*
  423. * column loop
  424. */
  425. while (col > 0U)
  426. {
  427. /*
  428. * generate 4 columns elements
  429. */
  430. /*
  431. * Matrix A columns number of MAC operations are to be performed
  432. */
  433. q15_t const *pSrcA0Vec;
  434. q15_t *pInA0 = pInA;
  435. q63_t acc0;
  436. acc0 = 0LL;
  437. pSrcA0Vec = (q15_t const *) pInA0;
  438. vecOffs = vecColBOffs;
  439. blkCnt = (numColsA) >> 3;
  440. while (blkCnt > 0U)
  441. {
  442. q15x8_t vecB, vecA;
  443. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  444. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  445. vecA = vld1q(pSrcA0Vec);
  446. pSrcA0Vec += 8;
  447. acc0 = vmlaldavaq(acc0, vecA, vecB);
  448. blkCnt--;
  449. }
  450. /*
  451. * tail
  452. */
  453. blkCnt = numColsA & 7;
  454. if (blkCnt > 0U)
  455. {
  456. mve_pred16_t p0 = vctp16q(blkCnt);
  457. q15x8_t vecB, vecA;
  458. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  459. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  460. vecA = vld1q(pSrcA0Vec);
  461. acc0 = vmlaldavaq_p(acc0, vecA, vecB, p0);
  462. }
  463. px[0] = (q15_t)MVE_ASRL_SAT16(acc0, 15);
  464. px++;
  465. /*
  466. * Decrement the column loop counter
  467. */
  468. col--;
  469. /*
  470. * Update the pointer pInB to point to the starting address of the next column
  471. */
  472. pInB = pSrcB->pData + (numColsB - col);
  473. }
  474. /*
  475. * Update the pointer pInA to point to the starting address of the next row
  476. */
  477. pInA += (numColsA );
  478. rowCnt--;
  479. }
  480. /* Set status as ARM_MATH_SUCCESS */
  481. status = ARM_MATH_SUCCESS;
  482. }
  483. /* Return to application */
  484. return (status);
  485. }
  486. #else
  487. arm_status arm_mat_mult_q15(
  488. const arm_matrix_instance_q15 * pSrcA,
  489. const arm_matrix_instance_q15 * pSrcB,
  490. arm_matrix_instance_q15 * pDst,
  491. q15_t * pState)
  492. {
  493. q63_t sum; /* Accumulator */
  494. #if defined (ARM_MATH_DSP) /* != CM0 */
  495. q15_t *pSrcBT = pState; /* Input data matrix pointer for transpose */
  496. q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */
  497. q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */
  498. q15_t *px; /* Temporary output data matrix pointer */
  499. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  500. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  501. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  502. uint16_t numRowsB = pSrcB->numRows; /* Number of rows of input matrix B */
  503. uint32_t col, i = 0U, row = numRowsB, colCnt; /* Loop counters */
  504. arm_status status; /* Status of matrix multiplication */
  505. q31_t in; /* Temporary variable to hold the input value */
  506. q31_t inA1, inB1, inA2, inB2;
  507. #ifdef ARM_MATH_MATRIX_CHECK
  508. /* Check for matrix mismatch condition */
  509. if ((pSrcA->numCols != pSrcB->numRows) ||
  510. (pSrcA->numRows != pDst->numRows) ||
  511. (pSrcB->numCols != pDst->numCols) )
  512. {
  513. /* Set status as ARM_MATH_SIZE_MISMATCH */
  514. status = ARM_MATH_SIZE_MISMATCH;
  515. }
  516. else
  517. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  518. {
  519. /* Matrix transpose */
  520. do
  521. {
  522. /* The pointer px is set to starting address of column being processed */
  523. px = pSrcBT + i;
  524. /* Apply loop unrolling and exchange columns with row elements */
  525. col = numColsB >> 2U;
  526. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  527. ** a second loop below computes the remaining 1 to 3 samples. */
  528. while (col > 0U)
  529. {
  530. /* Read two elements from row */
  531. in = read_q15x2_ia ((q15_t **) &pInB);
  532. /* Unpack and store one element in destination */
  533. #ifndef ARM_MATH_BIG_ENDIAN
  534. *px = (q15_t) in;
  535. #else
  536. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  537. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  538. /* Update pointer px to point to next row of transposed matrix */
  539. px += numRowsB;
  540. /* Unpack and store second element in destination */
  541. #ifndef ARM_MATH_BIG_ENDIAN
  542. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  543. #else
  544. *px = (q15_t) in;
  545. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  546. /* Update pointer px to point to next row of transposed matrix */
  547. px += numRowsB;
  548. /* Read two elements from row */
  549. in = read_q15x2_ia ((q15_t **) &pInB);
  550. /* Unpack and store one element in destination */
  551. #ifndef ARM_MATH_BIG_ENDIAN
  552. *px = (q15_t) in;
  553. #else
  554. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  555. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  556. px += numRowsB;
  557. #ifndef ARM_MATH_BIG_ENDIAN
  558. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  559. #else
  560. *px = (q15_t) in;
  561. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  562. px += numRowsB;
  563. /* Decrement column loop counter */
  564. col--;
  565. }
  566. /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
  567. ** No loop unrolling is used. */
  568. col = numColsB % 0x4U;
  569. while (col > 0U)
  570. {
  571. /* Read and store input element in destination */
  572. *px = *pInB++;
  573. /* Update pointer px to point to next row of transposed matrix */
  574. px += numRowsB;
  575. /* Decrement column loop counter */
  576. col--;
  577. }
  578. i++;
  579. /* Decrement row loop counter */
  580. row--;
  581. } while (row > 0U);
  582. /* Reset variables for usage in following multiplication process */
  583. row = numRowsA;
  584. i = 0U;
  585. px = pDst->pData;
  586. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  587. /* row loop */
  588. do
  589. {
  590. /* For every row wise process, column loop counter is to be initiated */
  591. col = numColsB;
  592. /* For every row wise process, pIn2 pointer is set to starting address of transposed pSrcB data */
  593. pInB = pSrcBT;
  594. /* column loop */
  595. do
  596. {
  597. /* Set variable sum, that acts as accumulator, to zero */
  598. sum = 0;
  599. /* Initiate pointer pInA to point to starting address of column being processed */
  600. pInA = pSrcA->pData + i;
  601. /* Apply loop unrolling and compute 2 MACs simultaneously. */
  602. colCnt = numColsA >> 2U;
  603. /* matrix multiplication */
  604. while (colCnt > 0U)
  605. {
  606. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  607. /* read real and imag values from pSrcA and pSrcB buffer */
  608. inA1 = read_q15x2_ia ((q15_t **) &pInA);
  609. inB1 = read_q15x2_ia ((q15_t **) &pInB);
  610. inA2 = read_q15x2_ia ((q15_t **) &pInA);
  611. inB2 = read_q15x2_ia ((q15_t **) &pInB);
  612. /* Multiply and Accumulates */
  613. sum = __SMLALD(inA1, inB1, sum);
  614. sum = __SMLALD(inA2, inB2, sum);
  615. /* Decrement loop counter */
  616. colCnt--;
  617. }
  618. /* process remaining column samples */
  619. colCnt = numColsA % 0x4U;
  620. while (colCnt > 0U)
  621. {
  622. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  623. sum += *pInA++ * *pInB++;
  624. /* Decrement loop counter */
  625. colCnt--;
  626. }
  627. /* Saturate and store result in destination buffer */
  628. *px = (q15_t) (__SSAT((sum >> 15), 16));
  629. px++;
  630. /* Decrement column loop counter */
  631. col--;
  632. } while (col > 0U);
  633. i = i + numColsA;
  634. /* Decrement row loop counter */
  635. row--;
  636. } while (row > 0U);
  637. #else /* #if defined (ARM_MATH_DSP) */
  638. q15_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  639. q15_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  640. q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */
  641. q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */
  642. q15_t *pOut = pDst->pData; /* Output data matrix pointer */
  643. q15_t *px; /* Temporary output data matrix pointer */
  644. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  645. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  646. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  647. uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  648. arm_status status; /* Status of matrix multiplication */
  649. (void)pState;
  650. #ifdef ARM_MATH_MATRIX_CHECK
  651. /* Check for matrix mismatch condition */
  652. if ((pSrcA->numCols != pSrcB->numRows) ||
  653. (pSrcA->numRows != pDst->numRows) ||
  654. (pSrcB->numCols != pDst->numCols) )
  655. {
  656. /* Set status as ARM_MATH_SIZE_MISMATCH */
  657. status = ARM_MATH_SIZE_MISMATCH;
  658. }
  659. else
  660. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  661. {
  662. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  663. /* row loop */
  664. do
  665. {
  666. /* Output pointer is set to starting address of the row being processed */
  667. px = pOut + i;
  668. /* For every row wise process, column loop counter is to be initiated */
  669. col = numColsB;
  670. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  671. pIn2 = pSrcB->pData;
  672. /* column loop */
  673. do
  674. {
  675. /* Set the variable sum, that acts as accumulator, to zero */
  676. sum = 0;
  677. /* Initiate pointer pIn1 to point to starting address of pSrcA */
  678. pIn1 = pInA;
  679. /* Matrix A columns number of MAC operations are to be performed */
  680. colCnt = numColsA;
  681. /* matrix multiplication */
  682. while (colCnt > 0U)
  683. {
  684. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  685. /* Perform multiply-accumulates */
  686. sum += (q31_t) * pIn1++ * *pIn2;
  687. pIn2 += numColsB;
  688. /* Decrement loop counter */
  689. colCnt--;
  690. }
  691. /* Convert result from 34.30 to 1.15 format and store saturated value in destination buffer */
  692. /* Saturate and store result in destination buffer */
  693. *px++ = (q15_t) __SSAT((sum >> 15), 16);
  694. /* Decrement column loop counter */
  695. col--;
  696. /* Update pointer pIn2 to point to starting address of next column */
  697. pIn2 = pInB + (numColsB - col);
  698. } while (col > 0U);
  699. /* Update pointer pSrcA to point to starting address of next row */
  700. i = i + numColsB;
  701. pInA = pInA + numColsA;
  702. /* Decrement row loop counter */
  703. row--;
  704. } while (row > 0U);
  705. #endif /* #if defined (ARM_MATH_DSP) */
  706. /* Set status as ARM_MATH_SUCCESS */
  707. status = ARM_MATH_SUCCESS;
  708. }
  709. /* Return to application */
  710. return (status);
  711. }
  712. #endif /* defined(ARM_MATH_MVEI) */
  713. /**
  714. @} end of MatrixMult group
  715. */