arm_mat_vec_mult_q7.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_vec_mult_q7.c
  4. * Description: Q7 matrix and vector multiplication
  5. *
  6. * $Date: 23 April 2021
  7. *
  8. * $Revision: V1.9.0
  9. *
  10. * Target Processor: Cortex-M and Cortex-A cores
  11. * -------------------------------------------------------------------- */
  12. /*
  13. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  14. *
  15. * SPDX-License-Identifier: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the License); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. */
  29. #include "dsp/matrix_functions.h"
  30. /**
  31. * @ingroup groupMatrix
  32. */
  33. /**
  34. * @addtogroup MatrixVectMult
  35. * @{
  36. */
  37. /**
  38. * @brief Q7 matrix and vector multiplication.
  39. * @param[in] *pSrcMat points to the input matrix structure
  40. * @param[in] *pVec points to the input vector
  41. * @param[out] *pDst points to the output vector
  42. */
  43. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. void arm_mat_vec_mult_q7(
  46. const arm_matrix_instance_q7 * pSrcMat,
  47. const q7_t *pSrcVec,
  48. q7_t *pDstVec)
  49. {
  50. const q7_t *pMatSrc = pSrcMat->pData;
  51. const q7_t *pMat0, *pMat1;
  52. uint32_t numRows = pSrcMat->numRows;
  53. uint32_t numCols = pSrcMat->numCols;
  54. q7_t *px;
  55. int32_t row;
  56. uint16_t blkCnt; /* loop counters */
  57. row = numRows;
  58. px = pDstVec;
  59. /*
  60. * compute 4x64-bit accumulators per loop
  61. */
  62. while (row >= 4)
  63. {
  64. q7_t const *pMat0Vec, *pMat1Vec, *pMat2Vec, *pMat3Vec, *pVec;
  65. const q7_t *pMat2, *pMat3;
  66. q7_t const *pSrcVecPtr = pSrcVec;
  67. q31_t acc0, acc1, acc2, acc3;
  68. q7x16_t vecMatA0, vecMatA1, vecMatA2, vecMatA3, vecIn;
  69. pVec = pSrcVec;
  70. /*
  71. * Initialize the pointer pIn1 to point to the starting address of the column being processed
  72. */
  73. pMat0 = pMatSrc;
  74. pMat1 = pMat0 + numCols;
  75. pMat2 = pMat1 + numCols;
  76. pMat3 = pMat2 + numCols;
  77. acc0 = 0L;
  78. acc1 = 0L;
  79. acc2 = 0L;
  80. acc3 = 0L;
  81. pMat0Vec = pMat0;
  82. pMat1Vec = pMat1;
  83. pMat2Vec = pMat2;
  84. pMat3Vec = pMat3;
  85. pVec = pSrcVecPtr;
  86. blkCnt = numCols >> 4;
  87. while (blkCnt > 0U)
  88. {
  89. vecMatA0 = vld1q(pMat0Vec);
  90. pMat0Vec += 16;
  91. vecMatA1 = vld1q(pMat1Vec);
  92. pMat1Vec += 16;
  93. vecMatA2 = vld1q(pMat2Vec);
  94. pMat2Vec += 16;
  95. vecMatA3 = vld1q(pMat3Vec);
  96. pMat3Vec += 16;
  97. vecIn = vld1q(pVec);
  98. pVec += 16;
  99. acc0 = vmladavaq(acc0, vecIn, vecMatA0);
  100. acc1 = vmladavaq(acc1, vecIn, vecMatA1);
  101. acc2 = vmladavaq(acc2, vecIn, vecMatA2);
  102. acc3 = vmladavaq(acc3, vecIn, vecMatA3);
  103. blkCnt--;
  104. }
  105. /*
  106. * tail
  107. * (will be merged thru tail predication)
  108. */
  109. blkCnt = numCols & 0xF;
  110. if (blkCnt > 0U)
  111. {
  112. mve_pred16_t p0 = vctp8q(blkCnt);
  113. vecMatA0 = vld1q(pMat0Vec);
  114. vecMatA1 = vld1q(pMat1Vec);
  115. vecMatA2 = vld1q(pMat2Vec);
  116. vecMatA3 = vld1q(pMat3Vec);
  117. vecIn = vldrbq_z_s8(pVec, p0);
  118. acc0 = vmladavaq(acc0, vecIn, vecMatA0);
  119. acc1 = vmladavaq(acc1, vecIn, vecMatA1);
  120. acc2 = vmladavaq(acc2, vecIn, vecMatA2);
  121. acc3 = vmladavaq(acc3, vecIn, vecMatA3);
  122. }
  123. *px++ = __SSAT(acc0 >> 7, 8);
  124. *px++ = __SSAT(acc1 >> 7, 8);
  125. *px++ = __SSAT(acc2 >> 7, 8);
  126. *px++ = __SSAT(acc3 >> 7, 8);
  127. pMatSrc += numCols * 4;
  128. /*
  129. * Decrement the row loop counter
  130. */
  131. row -= 4;
  132. }
  133. /*
  134. * process any remaining rows pair
  135. */
  136. if (row >= 2)
  137. {
  138. q7_t const *pMat0Vec, *pMat1Vec, *pVec;
  139. q7_t const *pSrcVecPtr = pSrcVec;
  140. q31_t acc0, acc1;
  141. q7x16_t vecMatA0, vecMatA1, vecIn;
  142. /*
  143. * For every row wise process, the pInVec pointer is set
  144. * to the starting address of the vector
  145. */
  146. pVec = pSrcVec;
  147. /*
  148. * Initialize the pointer pIn1 to point to the starting address of the column being processed
  149. */
  150. pMat0 = pMatSrc;
  151. pMat1 = pMat0 + numCols;
  152. acc0 = 0;
  153. acc1 = 0;
  154. pMat0Vec = pMat0;
  155. pMat1Vec = pMat1;
  156. pVec = pSrcVecPtr;
  157. blkCnt = numCols >> 4;
  158. while (blkCnt > 0U)
  159. {
  160. vecMatA0 = vld1q(pMat0Vec);
  161. pMat0Vec += 16;
  162. vecMatA1 = vld1q(pMat1Vec);
  163. pMat1Vec += 16;
  164. vecIn = vld1q(pVec);
  165. pVec += 16;
  166. acc0 = vmladavaq(acc0, vecIn, vecMatA0);
  167. acc1 = vmladavaq(acc1, vecIn, vecMatA1);
  168. blkCnt--;
  169. }
  170. /*
  171. * tail
  172. * (will be merged thru tail predication)
  173. */
  174. blkCnt = numCols & 0xF;
  175. if (blkCnt > 0U)
  176. {
  177. mve_pred16_t p0 = vctp8q(blkCnt);
  178. vecMatA0 = vld1q(pMat0Vec);
  179. vecMatA1 = vld1q(pMat1Vec);
  180. vecIn = vldrbq_z_s8(pVec, p0);
  181. acc0 = vmladavaq(acc0, vecIn, vecMatA0);
  182. acc1 = vmladavaq(acc1, vecIn, vecMatA1);
  183. }
  184. *px++ = __SSAT(acc0 >> 7, 8);
  185. *px++ = __SSAT(acc1 >> 7, 8);
  186. pMatSrc += numCols * 2;
  187. /*
  188. * Decrement the row loop counter
  189. */
  190. row -= 2;
  191. }
  192. if (row >= 1)
  193. {
  194. q7_t const *pMat0Vec, *pVec;
  195. q7_t const *pSrcVecPtr = pSrcVec;
  196. q31_t acc0;
  197. q7x16_t vecMatA0, vecIn;
  198. /*
  199. * For every row wise process, the pInVec pointer is set
  200. * to the starting address of the vector
  201. */
  202. pVec = pSrcVec;
  203. /*
  204. * Initialize the pointer pIn1 to point to the starting address of the column being processed
  205. */
  206. pMat0 = pMatSrc;
  207. acc0 = 0LL;
  208. pMat0Vec = pMat0;
  209. pVec = pSrcVecPtr;
  210. blkCnt = numCols >> 4;
  211. while (blkCnt > 0U)
  212. {
  213. vecMatA0 = vld1q(pMat0Vec);
  214. pMat0Vec += 16;
  215. vecIn = vld1q(pVec);
  216. pVec += 16;
  217. acc0 = vmladavaq(acc0, vecIn, vecMatA0);
  218. blkCnt--;
  219. }
  220. /*
  221. * tail
  222. * (will be merged thru tail predication)
  223. */
  224. blkCnt = numCols & 0xF;
  225. if (blkCnt > 0U)
  226. {
  227. mve_pred16_t p0 = vctp8q(blkCnt);
  228. vecMatA0 = vld1q(pMat0Vec);
  229. vecIn = vldrbq_z_s8(pVec, p0);
  230. acc0 = vmladavaq(acc0, vecIn, vecMatA0);
  231. }
  232. *px++ = __SSAT(acc0 >> 7, 8);
  233. }
  234. }
  235. #else
  236. void arm_mat_vec_mult_q7(const arm_matrix_instance_q7 *pSrcMat, const q7_t *pVec, q7_t *pDst)
  237. {
  238. uint32_t numRows = pSrcMat->numRows;
  239. uint32_t numCols = pSrcMat->numCols;
  240. const q7_t *pSrcA = pSrcMat->pData;
  241. const q7_t *pInA1; /* input data matrix pointer of Q7 type */
  242. const q7_t *pInA2; /* input data matrix pointer of Q7 type */
  243. const q7_t *pInA3; /* input data matrix pointer of Q7 type */
  244. const q7_t *pInA4; /* input data matrix pointer of Q7 type */
  245. const q7_t *pInVec; /* input data vector pointer of Q7 type */
  246. q7_t *px; /* output data pointer */
  247. uint32_t i, row, colCnt; /* loop counters */
  248. q31_t matData, matData2, vecData, vecData2;
  249. /* Process 4 rows at a time */
  250. row = numRows >> 2;
  251. i = 0u;
  252. px = pDst;
  253. /* The following loop performs the dot-product of each row in pSrcA with the vector */
  254. while (row > 0) {
  255. /* Initialize accumulators */
  256. q31_t sum1 = 0;
  257. q31_t sum2 = 0;
  258. q31_t sum3 = 0;
  259. q31_t sum4 = 0;
  260. /* For every row wise process, the pInVec pointer is set
  261. ** to the starting address of the vector */
  262. pInVec = pVec;
  263. /* Loop unrolling: process 4 columns per iteration */
  264. colCnt = numCols >> 2;
  265. /* Initialize row pointers so we can track 4 rows at once */
  266. pInA1 = pSrcA + i;
  267. pInA2 = pInA1 + numCols;
  268. pInA3 = pInA2 + numCols;
  269. pInA4 = pInA3 + numCols;
  270. // Inner loop: matrix-vector multiplication
  271. while (colCnt > 0u) {
  272. // Read 4 values from vector
  273. vecData = read_q7x4_ia (&pInVec);
  274. vecData2 = __SXTB16(__ROR(vecData, 8));
  275. vecData = __SXTB16(vecData);
  276. // Read 16 values from the matrix - 4 values from each of 4 rows, and do multiply accumulate
  277. matData = read_q7x4_ia (&pInA1);
  278. matData2 = __SXTB16(__ROR(matData, 8));
  279. matData = __SXTB16(matData);
  280. sum1 = __SMLAD(matData, vecData, sum1);
  281. sum1 = __SMLAD(matData2, vecData2, sum1);
  282. matData = read_q7x4_ia (&pInA2);
  283. matData2 = __SXTB16(__ROR(matData, 8));
  284. matData = __SXTB16(matData);
  285. sum2 = __SMLAD(matData, vecData, sum2);
  286. sum2 = __SMLAD(matData2, vecData2, sum2);
  287. matData = read_q7x4_ia (&pInA3);
  288. matData2 = __SXTB16(__ROR(matData, 8));
  289. matData = __SXTB16(matData);
  290. sum3 = __SMLAD(matData, vecData, sum3);
  291. sum3 = __SMLAD(matData2, vecData2, sum3);
  292. matData = read_q7x4_ia (&pInA4);
  293. matData2 = __SXTB16(__ROR(matData, 8));
  294. matData = __SXTB16(matData);
  295. sum4 = __SMLAD(matData, vecData, sum4);
  296. sum4 = __SMLAD(matData2, vecData2, sum4);
  297. // Decrement the loop counter
  298. colCnt--;
  299. }
  300. /* process any remaining columns */
  301. colCnt = numCols & 3u;
  302. while (colCnt > 0) {
  303. vecData = *pInVec++;
  304. sum1 += *pInA1++ * vecData;
  305. sum2 += *pInA2++ * vecData;
  306. sum3 += *pInA3++ * vecData;
  307. sum4 += *pInA4++ * vecData;
  308. colCnt--;
  309. }
  310. /* Saturate and store the result in the destination buffer */
  311. *px++ = (q7_t)(__SSAT((sum1 >> 7), 8));
  312. *px++ = (q7_t)(__SSAT((sum2 >> 7), 8));
  313. *px++ = (q7_t)(__SSAT((sum3 >> 7), 8));
  314. *px++ = (q7_t)(__SSAT((sum4 >> 7), 8));
  315. i = i + numCols * 4;
  316. /* Decrement the row loop counter */
  317. row--;
  318. }
  319. /* process any remaining rows */
  320. row = numRows & 3u;
  321. while (row > 0) {
  322. q31_t sum = 0;
  323. pInVec = pVec;
  324. pInA1 = pSrcA + i;
  325. // loop unrolling - process 4 elements at a time
  326. colCnt = numCols >> 2;
  327. while (colCnt > 0) {
  328. vecData = read_q7x4_ia (&pInVec);
  329. vecData2 = __SXTB16(__ROR(vecData, 8));
  330. vecData = __SXTB16(vecData);
  331. matData = read_q7x4_ia (&pInA1);
  332. matData2 = __SXTB16(__ROR(matData, 8));
  333. matData = __SXTB16(matData);
  334. sum = __SMLAD(matData, vecData, sum);
  335. sum = __SMLAD(matData2, vecData2, sum);
  336. colCnt--;
  337. }
  338. // process remainder of row
  339. colCnt = numCols & 3u;
  340. while (colCnt > 0) {
  341. sum += *pInA1++ * *pInVec++;
  342. colCnt--;
  343. }
  344. *px++ = (q7_t)(__SSAT((sum >> 7), 8));
  345. i = i + numCols;
  346. row--;
  347. }
  348. }
  349. #endif /* defined(ARM_MATH_MVEI) */
  350. /**
  351. * @} end of MatrixMult group
  352. */