arm_mat_mult_q15.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_q15.c
  4. * Description: Q15 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. @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)
  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, colCnt; /* 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. colCnt = numColsA;
  333. q15_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec;
  334. q15_t *pInA0 = pInA;
  335. q15_t *pInA1 = pInA0 + numColsA;
  336. q15_t *pInA2 = pInA1 + numColsA;
  337. q15_t *pInA3 = pInA2 + numColsA;
  338. q63_t acc0, acc1, acc2, acc3;
  339. acc0 = 0LL;
  340. acc1 = 0LL;
  341. acc2 = 0LL;
  342. acc3 = 0LL;
  343. pSrcA0Vec = (q15_t const *) pInA0;
  344. pSrcA1Vec = (q15_t const *) pInA1;
  345. pSrcA2Vec = (q15_t const *) pInA2;
  346. pSrcA3Vec = (q15_t const *) pInA3;
  347. vecOffs = vecColBOffs;
  348. blkCnt = (numColsA) >> 3;
  349. while (blkCnt > 0U)
  350. {
  351. q15x8_t vecB, vecA;
  352. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  353. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  354. vecA = vld1q(pSrcA0Vec); pSrcA0Vec += 8;
  355. acc0 = vmlaldavaq(acc0, vecA, vecB);
  356. vecA = vld1q(pSrcA1Vec); pSrcA1Vec += 8;
  357. acc1 = vmlaldavaq(acc1, vecA, vecB);
  358. vecA = vld1q(pSrcA2Vec); pSrcA2Vec += 8;
  359. acc2 = vmlaldavaq(acc2, vecA, vecB);
  360. vecA = vld1q(pSrcA3Vec); pSrcA3Vec += 8;
  361. acc3 = vmlaldavaq(acc3, vecA, vecB);
  362. blkCnt--;
  363. }
  364. /*
  365. * tail
  366. */
  367. blkCnt = numColsA & 7;
  368. if (blkCnt > 0U)
  369. {
  370. mve_pred16_t p0 = vctp16q(blkCnt);
  371. q15x8_t vecB, vecA;
  372. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  373. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  374. vecA = vld1q(pSrcA0Vec);
  375. acc0 = vmlaldavaq_p(acc0, vecA, vecB, p0);
  376. vecA = vld1q(pSrcA1Vec);
  377. acc1 = vmlaldavaq_p(acc1, vecA, vecB, p0);
  378. vecA = vld1q(pSrcA2Vec);
  379. acc2 = vmlaldavaq_p(acc2, vecA, vecB, p0);
  380. vecA = vld1q(pSrcA3Vec);
  381. acc3 = vmlaldavaq_p(acc3, vecA, vecB, p0);
  382. }
  383. px[0] = (q15_t)MVE_ASRL_SAT16(acc0, 15);
  384. px[1 * numColsB] = (q15_t)MVE_ASRL_SAT16(acc1, 15);
  385. px[2 * numColsB] = (q15_t)MVE_ASRL_SAT16(acc2, 15);
  386. px[3 * numColsB] = (q15_t)MVE_ASRL_SAT16(acc3, 15);
  387. px++;
  388. /*
  389. * Decrement the column loop counter
  390. */
  391. col--;
  392. /*
  393. * Update the pointer pInB to point to the starting address of the next column
  394. */
  395. pInB = pSrcB->pData + (numColsB - col);
  396. }
  397. /*
  398. * Update the pointer pInA to point to the starting address of the next row
  399. */
  400. pInA += (numColsA * 4);
  401. /*
  402. * Decrement the row loop counter
  403. */
  404. rowCnt --;
  405. }
  406. rowCnt = row & 3;
  407. while (rowCnt > 0U)
  408. {
  409. /*
  410. * Output pointer is set to starting address of the row being processed
  411. */
  412. px = pOut + i;
  413. i = i + numColsB;
  414. /*
  415. * For every row wise process, the column loop counter is to be initiated
  416. */
  417. col = numColsB;
  418. /*
  419. * For every row wise process, the pInB pointer is set
  420. * to the starting address of the pSrcB data
  421. */
  422. pInB = pSrcB->pData;
  423. /*
  424. * column loop
  425. */
  426. while (col > 0U)
  427. {
  428. /*
  429. * generate 4 columns elements
  430. */
  431. /*
  432. * Matrix A columns number of MAC operations are to be performed
  433. */
  434. colCnt = numColsA;
  435. q15_t const *pSrcA0Vec;
  436. q15_t *pInA0 = pInA;
  437. q63_t acc0;
  438. acc0 = 0LL;
  439. pSrcA0Vec = (q15_t const *) pInA0;
  440. vecOffs = vecColBOffs;
  441. blkCnt = (numColsA) >> 3;
  442. while (blkCnt > 0U)
  443. {
  444. q15x8_t vecB, vecA;
  445. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  446. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  447. vecA = vld1q(pSrcA0Vec);
  448. pSrcA0Vec += 8;
  449. acc0 = vmlaldavaq(acc0, vecA, vecB);
  450. blkCnt--;
  451. }
  452. /*
  453. * tail
  454. */
  455. blkCnt = numColsA & 7;
  456. if (blkCnt > 0U)
  457. {
  458. mve_pred16_t p0 = vctp16q(blkCnt);
  459. q15x8_t vecB, vecA;
  460. vecB = vldrhq_gather_shifted_offset((int16_t const *)pInB, vecOffs);
  461. vecOffs = vecOffs + (uint16_t) (numColsB * 8);
  462. vecA = vld1q(pSrcA0Vec);
  463. acc0 = vmlaldavaq_p(acc0, vecA, vecB, p0);
  464. }
  465. px[0] = (q15_t)MVE_ASRL_SAT16(acc0, 15);
  466. px++;
  467. /*
  468. * Decrement the column loop counter
  469. */
  470. col--;
  471. /*
  472. * Update the pointer pInB to point to the starting address of the next column
  473. */
  474. pInB = pSrcB->pData + (numColsB - col);
  475. }
  476. /*
  477. * Update the pointer pInA to point to the starting address of the next row
  478. */
  479. pInA += (numColsA );
  480. rowCnt--;
  481. }
  482. /* Set status as ARM_MATH_SUCCESS */
  483. status = ARM_MATH_SUCCESS;
  484. }
  485. /* Return to application */
  486. return (status);
  487. }
  488. #else
  489. arm_status arm_mat_mult_q15(
  490. const arm_matrix_instance_q15 * pSrcA,
  491. const arm_matrix_instance_q15 * pSrcB,
  492. arm_matrix_instance_q15 * pDst,
  493. q15_t * pState)
  494. {
  495. q63_t sum; /* Accumulator */
  496. #if defined (ARM_MATH_DSP) /* != CM0 */
  497. q15_t *pSrcBT = pState; /* Input data matrix pointer for transpose */
  498. q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */
  499. q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */
  500. q15_t *px; /* Temporary output data matrix pointer */
  501. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  502. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  503. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  504. uint16_t numRowsB = pSrcB->numRows; /* Number of rows of input matrix A */
  505. uint32_t col, i = 0U, row = numRowsB, colCnt; /* Loop counters */
  506. arm_status status; /* Status of matrix multiplication */
  507. q31_t in; /* Temporary variable to hold the input value */
  508. q31_t inA1, inB1, inA2, inB2;
  509. #ifdef ARM_MATH_MATRIX_CHECK
  510. /* Check for matrix mismatch condition */
  511. if ((pSrcA->numCols != pSrcB->numRows) ||
  512. (pSrcA->numRows != pDst->numRows) ||
  513. (pSrcB->numCols != pDst->numCols) )
  514. {
  515. /* Set status as ARM_MATH_SIZE_MISMATCH */
  516. status = ARM_MATH_SIZE_MISMATCH;
  517. }
  518. else
  519. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  520. {
  521. /* Matrix transpose */
  522. do
  523. {
  524. /* The pointer px is set to starting address of column being processed */
  525. px = pSrcBT + i;
  526. /* Apply loop unrolling and exchange columns with row elements */
  527. col = numColsB >> 2U;
  528. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  529. ** a second loop below computes the remaining 1 to 3 samples. */
  530. while (col > 0U)
  531. {
  532. /* Read two elements from row */
  533. in = read_q15x2_ia ((q15_t **) &pInB);
  534. /* Unpack and store one element in destination */
  535. #ifndef ARM_MATH_BIG_ENDIAN
  536. *px = (q15_t) in;
  537. #else
  538. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  539. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  540. /* Update pointer px to point to next row of transposed matrix */
  541. px += numRowsB;
  542. /* Unpack and store second element in destination */
  543. #ifndef ARM_MATH_BIG_ENDIAN
  544. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  545. #else
  546. *px = (q15_t) in;
  547. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  548. /* Update pointer px to point to next row of transposed matrix */
  549. px += numRowsB;
  550. /* Read two elements from row */
  551. in = read_q15x2_ia ((q15_t **) &pInB);
  552. /* Unpack and store one element in destination */
  553. #ifndef ARM_MATH_BIG_ENDIAN
  554. *px = (q15_t) in;
  555. #else
  556. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  557. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  558. px += numRowsB;
  559. #ifndef ARM_MATH_BIG_ENDIAN
  560. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  561. #else
  562. *px = (q15_t) in;
  563. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  564. px += numRowsB;
  565. /* Decrement column loop counter */
  566. col--;
  567. }
  568. /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
  569. ** No loop unrolling is used. */
  570. col = numColsB % 0x4U;
  571. while (col > 0U)
  572. {
  573. /* Read and store input element in destination */
  574. *px = *pInB++;
  575. /* Update pointer px to point to next row of transposed matrix */
  576. px += numRowsB;
  577. /* Decrement column loop counter */
  578. col--;
  579. }
  580. i++;
  581. /* Decrement row loop counter */
  582. row--;
  583. } while (row > 0U);
  584. /* Reset variables for usage in following multiplication process */
  585. row = numRowsA;
  586. i = 0U;
  587. px = pDst->pData;
  588. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  589. /* row loop */
  590. do
  591. {
  592. /* For every row wise process, column loop counter is to be initiated */
  593. col = numColsB;
  594. /* For every row wise process, pIn2 pointer is set to starting address of transposed pSrcB data */
  595. pInB = pSrcBT;
  596. /* column loop */
  597. do
  598. {
  599. /* Set variable sum, that acts as accumulator, to zero */
  600. sum = 0;
  601. /* Initiate pointer pInA to point to starting address of column being processed */
  602. pInA = pSrcA->pData + i;
  603. /* Apply loop unrolling and compute 2 MACs simultaneously. */
  604. colCnt = numColsA >> 2U;
  605. /* matrix multiplication */
  606. while (colCnt > 0U)
  607. {
  608. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  609. /* read real and imag values from pSrcA and pSrcB buffer */
  610. inA1 = read_q15x2_ia ((q15_t **) &pInA);
  611. inB1 = read_q15x2_ia ((q15_t **) &pInB);
  612. inA2 = read_q15x2_ia ((q15_t **) &pInA);
  613. inB2 = read_q15x2_ia ((q15_t **) &pInB);
  614. /* Multiply and Accumlates */
  615. sum = __SMLALD(inA1, inB1, sum);
  616. sum = __SMLALD(inA2, inB2, sum);
  617. /* Decrement loop counter */
  618. colCnt--;
  619. }
  620. /* process remaining column samples */
  621. colCnt = numColsA % 0x4U;
  622. while (colCnt > 0U)
  623. {
  624. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  625. sum += *pInA++ * *pInB++;
  626. /* Decrement loop counter */
  627. colCnt--;
  628. }
  629. /* Saturate and store result in destination buffer */
  630. *px = (q15_t) (__SSAT((sum >> 15), 16));
  631. px++;
  632. /* Decrement column loop counter */
  633. col--;
  634. } while (col > 0U);
  635. i = i + numColsA;
  636. /* Decrement row loop counter */
  637. row--;
  638. } while (row > 0U);
  639. #else /* #if defined (ARM_MATH_DSP) */
  640. q15_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  641. q15_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  642. q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */
  643. q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */
  644. q15_t *pOut = pDst->pData; /* Output data matrix pointer */
  645. q15_t *px; /* Temporary output data matrix pointer */
  646. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  647. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  648. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  649. uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  650. arm_status status; /* Status of matrix multiplication */
  651. (void)pState;
  652. #ifdef ARM_MATH_MATRIX_CHECK
  653. /* Check for matrix mismatch condition */
  654. if ((pSrcA->numCols != pSrcB->numRows) ||
  655. (pSrcA->numRows != pDst->numRows) ||
  656. (pSrcB->numCols != pDst->numCols) )
  657. {
  658. /* Set status as ARM_MATH_SIZE_MISMATCH */
  659. status = ARM_MATH_SIZE_MISMATCH;
  660. }
  661. else
  662. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  663. {
  664. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  665. /* row loop */
  666. do
  667. {
  668. /* Output pointer is set to starting address of the row being processed */
  669. px = pOut + i;
  670. /* For every row wise process, column loop counter is to be initiated */
  671. col = numColsB;
  672. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  673. pIn2 = pSrcB->pData;
  674. /* column loop */
  675. do
  676. {
  677. /* Set the variable sum, that acts as accumulator, to zero */
  678. sum = 0;
  679. /* Initiate pointer pIn1 to point to starting address of pSrcA */
  680. pIn1 = pInA;
  681. /* Matrix A columns number of MAC operations are to be performed */
  682. colCnt = numColsA;
  683. /* matrix multiplication */
  684. while (colCnt > 0U)
  685. {
  686. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  687. /* Perform multiply-accumulates */
  688. sum += (q31_t) * pIn1++ * *pIn2;
  689. pIn2 += numColsB;
  690. /* Decrement loop counter */
  691. colCnt--;
  692. }
  693. /* Convert result from 34.30 to 1.15 format and store saturated value in destination buffer */
  694. /* Saturate and store result in destination buffer */
  695. *px++ = (q15_t) __SSAT((sum >> 15), 16);
  696. /* Decrement column loop counter */
  697. col--;
  698. /* Update pointer pIn2 to point to starting address of next column */
  699. pIn2 = pInB + (numColsB - col);
  700. } while (col > 0U);
  701. /* Update pointer pSrcA to point to starting address of next row */
  702. i = i + numColsB;
  703. pInA = pInA + numColsA;
  704. /* Decrement row loop counter */
  705. row--;
  706. } while (row > 0U);
  707. #endif /* #if defined (ARM_MATH_DSP) */
  708. /* Set status as ARM_MATH_SUCCESS */
  709. status = ARM_MATH_SUCCESS;
  710. }
  711. /* Return to application */
  712. return (status);
  713. }
  714. #endif /* defined(ARM_MATH_MVEI) */
  715. /**
  716. @} end of MatrixMult group
  717. */