arm_mat_mult_q15.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_q15.c
  4. * Description: Q15 matrix multiplication
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 The function returns either
  43. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  44. *
  45. * @details
  46. * <b>Scaling and Overflow Behavior:</b>
  47. *
  48. * \par
  49. * The function is implemented using a 64-bit internal accumulator. The inputs to the
  50. * multiplications are in 1.15 format and multiplications yield a 2.30 result.
  51. * The 2.30 intermediate
  52. * results are accumulated in a 64-bit accumulator in 34.30 format. This approach
  53. * provides 33 guard bits and there is no risk of overflow. The 34.30 result is then
  54. * truncated to 34.15 format by discarding the low 15 bits and then saturated to
  55. * 1.15 format.
  56. *
  57. * \par
  58. * Refer to <code>arm_mat_mult_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
  59. *
  60. */
  61. arm_status arm_mat_mult_q15(
  62. const arm_matrix_instance_q15 * pSrcA,
  63. const arm_matrix_instance_q15 * pSrcB,
  64. arm_matrix_instance_q15 * pDst,
  65. q15_t * pState)
  66. {
  67. q63_t sum; /* accumulator */
  68. #if defined (ARM_MATH_DSP)
  69. /* Run the below code for Cortex-M4 and Cortex-M3 */
  70. q15_t *pSrcBT = pState; /* input data matrix pointer for transpose */
  71. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
  72. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
  73. q15_t *px; /* Temporary output data matrix pointer */
  74. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  75. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  76. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  77. uint16_t numRowsB = pSrcB->numRows; /* number of rows of input matrix A */
  78. uint16_t col, i = 0U, row = numRowsB, colCnt; /* loop counters */
  79. arm_status status; /* status of matrix multiplication */
  80. #ifndef UNALIGNED_SUPPORT_DISABLE
  81. q31_t in; /* Temporary variable to hold the input value */
  82. q31_t pSourceA1, pSourceB1, pSourceA2, pSourceB2;
  83. #else
  84. q15_t in; /* Temporary variable to hold the input value */
  85. q15_t inA1, inB1, inA2, inB2;
  86. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  87. #ifdef ARM_MATH_MATRIX_CHECK
  88. /* Check for matrix mismatch condition */
  89. if ((pSrcA->numCols != pSrcB->numRows) ||
  90. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  91. {
  92. /* Set status as ARM_MATH_SIZE_MISMATCH */
  93. status = ARM_MATH_SIZE_MISMATCH;
  94. }
  95. else
  96. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  97. {
  98. /* Matrix transpose */
  99. do
  100. {
  101. /* Apply loop unrolling and exchange the columns with row elements */
  102. col = numColsB >> 2;
  103. /* The pointer px is set to starting address of the column being processed */
  104. px = pSrcBT + i;
  105. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  106. ** a second loop below computes the remaining 1 to 3 samples. */
  107. while (col > 0U)
  108. {
  109. #ifndef UNALIGNED_SUPPORT_DISABLE
  110. /* Read two elements from the row */
  111. in = *__SIMD32(pInB)++;
  112. /* Unpack and store one element in the destination */
  113. #ifndef ARM_MATH_BIG_ENDIAN
  114. *px = (q15_t) in;
  115. #else
  116. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  117. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  118. /* Update the pointer px to point to the next row of the transposed matrix */
  119. px += numRowsB;
  120. /* Unpack and store the second element in the destination */
  121. #ifndef ARM_MATH_BIG_ENDIAN
  122. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  123. #else
  124. *px = (q15_t) in;
  125. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  126. /* Update the pointer px to point to the next row of the transposed matrix */
  127. px += numRowsB;
  128. /* Read two elements from the row */
  129. in = *__SIMD32(pInB)++;
  130. /* Unpack and store one element in the destination */
  131. #ifndef ARM_MATH_BIG_ENDIAN
  132. *px = (q15_t) in;
  133. #else
  134. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  135. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  136. /* Update the pointer px to point to the next row of the transposed matrix */
  137. px += numRowsB;
  138. /* Unpack and store the second element in the destination */
  139. #ifndef ARM_MATH_BIG_ENDIAN
  140. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  141. #else
  142. *px = (q15_t) in;
  143. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  144. /* Update the pointer px to point to the next row of the transposed matrix */
  145. px += numRowsB;
  146. #else
  147. /* Read one element from the row */
  148. in = *pInB++;
  149. /* Store one element in the destination */
  150. *px = in;
  151. /* Update the pointer px to point to the next row of the transposed matrix */
  152. px += numRowsB;
  153. /* Read one element from the row */
  154. in = *pInB++;
  155. /* Store one element in the destination */
  156. *px = in;
  157. /* Update the pointer px to point to the next row of the transposed matrix */
  158. px += numRowsB;
  159. /* Read one element from the row */
  160. in = *pInB++;
  161. /* Store one element in the destination */
  162. *px = in;
  163. /* Update the pointer px to point to the next row of the transposed matrix */
  164. px += numRowsB;
  165. /* Read one element from the row */
  166. in = *pInB++;
  167. /* Store one element in the destination */
  168. *px = in;
  169. /* Update the pointer px to point to the next row of the transposed matrix */
  170. px += numRowsB;
  171. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  172. /* Decrement the column loop counter */
  173. col--;
  174. }
  175. /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
  176. ** No loop unrolling is used. */
  177. col = numColsB % 0x4U;
  178. while (col > 0U)
  179. {
  180. /* Read and store the input element in the destination */
  181. *px = *pInB++;
  182. /* Update the pointer px to point to the next row of the transposed matrix */
  183. px += numRowsB;
  184. /* Decrement the column loop counter */
  185. col--;
  186. }
  187. i++;
  188. /* Decrement the row loop counter */
  189. row--;
  190. } while (row > 0U);
  191. /* Reset the variables for the usage in the following multiplication process */
  192. row = numRowsA;
  193. i = 0U;
  194. px = pDst->pData;
  195. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  196. /* row loop */
  197. do
  198. {
  199. /* For every row wise process, the column loop counter is to be initiated */
  200. col = numColsB;
  201. /* For every row wise process, the pIn2 pointer is set
  202. ** to the starting address of the transposed pSrcB data */
  203. pInB = pSrcBT;
  204. /* column loop */
  205. do
  206. {
  207. /* Set the variable sum, that acts as accumulator, to zero */
  208. sum = 0;
  209. /* Apply loop unrolling and compute 2 MACs simultaneously. */
  210. colCnt = numColsA >> 2;
  211. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  212. pInA = pSrcA->pData + i;
  213. /* matrix multiplication */
  214. while (colCnt > 0U)
  215. {
  216. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  217. #ifndef UNALIGNED_SUPPORT_DISABLE
  218. /* read real and imag values from pSrcA and pSrcB buffer */
  219. pSourceA1 = *__SIMD32(pInA)++;
  220. pSourceB1 = *__SIMD32(pInB)++;
  221. pSourceA2 = *__SIMD32(pInA)++;
  222. pSourceB2 = *__SIMD32(pInB)++;
  223. /* Multiply and Accumlates */
  224. sum = __SMLALD(pSourceA1, pSourceB1, sum);
  225. sum = __SMLALD(pSourceA2, pSourceB2, sum);
  226. #else
  227. /* read real and imag values from pSrcA and pSrcB buffer */
  228. inA1 = *pInA++;
  229. inB1 = *pInB++;
  230. inA2 = *pInA++;
  231. /* Multiply and Accumlates */
  232. sum += inA1 * inB1;
  233. inB2 = *pInB++;
  234. inA1 = *pInA++;
  235. inB1 = *pInB++;
  236. /* Multiply and Accumlates */
  237. sum += inA2 * inB2;
  238. inA2 = *pInA++;
  239. inB2 = *pInB++;
  240. /* Multiply and Accumlates */
  241. sum += inA1 * inB1;
  242. sum += inA2 * inB2;
  243. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  244. /* Decrement the loop counter */
  245. colCnt--;
  246. }
  247. /* process remaining column samples */
  248. colCnt = numColsA & 3U;
  249. while (colCnt > 0U)
  250. {
  251. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  252. sum += *pInA++ * *pInB++;
  253. /* Decrement the loop counter */
  254. colCnt--;
  255. }
  256. /* Saturate and store the result in the destination buffer */
  257. *px = (q15_t) (__SSAT((sum >> 15), 16));
  258. px++;
  259. /* Decrement the column loop counter */
  260. col--;
  261. } while (col > 0U);
  262. i = i + numColsA;
  263. /* Decrement the row loop counter */
  264. row--;
  265. } while (row > 0U);
  266. #else
  267. /* Run the below code for Cortex-M0 */
  268. q15_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  269. q15_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  270. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
  271. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
  272. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  273. q15_t *px; /* Temporary output data matrix pointer */
  274. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  275. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  276. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  277. uint16_t col, i = 0U, row = numRowsA, colCnt; /* loop counters */
  278. arm_status status; /* status of matrix multiplication */
  279. #ifdef ARM_MATH_MATRIX_CHECK
  280. /* Check for matrix mismatch condition */
  281. if ((pSrcA->numCols != pSrcB->numRows) ||
  282. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  283. {
  284. /* Set status as ARM_MATH_SIZE_MISMATCH */
  285. status = ARM_MATH_SIZE_MISMATCH;
  286. }
  287. else
  288. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  289. {
  290. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  291. /* row loop */
  292. do
  293. {
  294. /* Output pointer is set to starting address of the row being processed */
  295. px = pOut + i;
  296. /* For every row wise process, the column loop counter is to be initiated */
  297. col = numColsB;
  298. /* For every row wise process, the pIn2 pointer is set
  299. ** to the starting address of the pSrcB data */
  300. pIn2 = pSrcB->pData;
  301. /* column loop */
  302. do
  303. {
  304. /* Set the variable sum, that acts as accumulator, to zero */
  305. sum = 0;
  306. /* Initiate the pointer pIn1 to point to the starting address of pSrcA */
  307. pIn1 = pInA;
  308. /* Matrix A columns number of MAC operations are to be performed */
  309. colCnt = numColsA;
  310. /* matrix multiplication */
  311. while (colCnt > 0U)
  312. {
  313. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  314. /* Perform the multiply-accumulates */
  315. sum += (q31_t) * pIn1++ * *pIn2;
  316. pIn2 += numColsB;
  317. /* Decrement the loop counter */
  318. colCnt--;
  319. }
  320. /* Convert the result from 34.30 to 1.15 format and store the saturated value in destination buffer */
  321. /* Saturate and store the result in the destination buffer */
  322. *px++ = (q15_t) __SSAT((sum >> 15), 16);
  323. /* Decrement the column loop counter */
  324. col--;
  325. /* Update the pointer pIn2 to point to the starting address of the next column */
  326. pIn2 = pInB + (numColsB - col);
  327. } while (col > 0U);
  328. /* Update the pointer pSrcA to point to the starting address of the next row */
  329. i = i + numColsB;
  330. pInA = pInA + numColsA;
  331. /* Decrement the row loop counter */
  332. row--;
  333. } while (row > 0U);
  334. #endif /* #if defined (ARM_MATH_DSP) */
  335. /* set status as ARM_MATH_SUCCESS */
  336. status = ARM_MATH_SUCCESS;
  337. }
  338. /* Return to application */
  339. return (status);
  340. }
  341. /**
  342. * @} end of MatrixMult group
  343. */