arm_mat_mult_q15.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_mat_mult_q15.c
  9. *
  10. * Description: Q15 matrix multiplication.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupMatrix
  43. */
  44. /**
  45. * @addtogroup MatrixMult
  46. * @{
  47. */
  48. /**
  49. * @brief Q15 matrix multiplication
  50. * @param[in] *pSrcA points to the first input matrix structure
  51. * @param[in] *pSrcB points to the second input matrix structure
  52. * @param[out] *pDst points to output matrix structure
  53. * @param[in] *pState points to the array for storing intermediate results (Unused)
  54. * @return The function returns either
  55. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  56. *
  57. * @details
  58. * <b>Scaling and Overflow Behavior:</b>
  59. *
  60. * \par
  61. * The function is implemented using a 64-bit internal accumulator. The inputs to the
  62. * multiplications are in 1.15 format and multiplications yield a 2.30 result.
  63. * The 2.30 intermediate
  64. * results are accumulated in a 64-bit accumulator in 34.30 format. This approach
  65. * provides 33 guard bits and there is no risk of overflow. The 34.30 result is then
  66. * truncated to 34.15 format by discarding the low 15 bits and then saturated to
  67. * 1.15 format.
  68. *
  69. * \par
  70. * 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.
  71. *
  72. */
  73. arm_status arm_mat_mult_q15(
  74. const arm_matrix_instance_q15 * pSrcA,
  75. const arm_matrix_instance_q15 * pSrcB,
  76. arm_matrix_instance_q15 * pDst,
  77. q15_t * pState CMSIS_UNUSED)
  78. {
  79. q63_t sum; /* accumulator */
  80. #ifndef ARM_MATH_CM0_FAMILY
  81. /* Run the below code for Cortex-M4 and Cortex-M3 */
  82. q15_t *pSrcBT = pState; /* input data matrix pointer for transpose */
  83. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
  84. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
  85. q15_t *px; /* Temporary output data matrix pointer */
  86. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  87. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  88. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  89. uint16_t numRowsB = pSrcB->numRows; /* number of rows of input matrix A */
  90. uint16_t col, i = 0u, row = numRowsB, colCnt; /* loop counters */
  91. arm_status status; /* status of matrix multiplication */
  92. #ifndef UNALIGNED_SUPPORT_DISABLE
  93. q31_t in; /* Temporary variable to hold the input value */
  94. q31_t pSourceA1, pSourceB1, pSourceA2, pSourceB2;
  95. #else
  96. q15_t in; /* Temporary variable to hold the input value */
  97. q15_t inA1, inB1, inA2, inB2;
  98. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  99. #ifdef ARM_MATH_MATRIX_CHECK
  100. /* Check for matrix mismatch condition */
  101. if((pSrcA->numCols != pSrcB->numRows) ||
  102. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  103. {
  104. /* Set status as ARM_MATH_SIZE_MISMATCH */
  105. status = ARM_MATH_SIZE_MISMATCH;
  106. }
  107. else
  108. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  109. {
  110. /* Matrix transpose */
  111. do
  112. {
  113. /* Apply loop unrolling and exchange the columns with row elements */
  114. col = numColsB >> 2;
  115. /* The pointer px is set to starting address of the column being processed */
  116. px = pSrcBT + i;
  117. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  118. ** a second loop below computes the remaining 1 to 3 samples. */
  119. while(col > 0u)
  120. {
  121. #ifndef UNALIGNED_SUPPORT_DISABLE
  122. /* Read two elements from the row */
  123. in = *__SIMD32(pInB)++;
  124. /* Unpack and store one element in the destination */
  125. #ifndef ARM_MATH_BIG_ENDIAN
  126. *px = (q15_t) in;
  127. #else
  128. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  129. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  130. /* Update the pointer px to point to the next row of the transposed matrix */
  131. px += numRowsB;
  132. /* Unpack and store the second element in the destination */
  133. #ifndef ARM_MATH_BIG_ENDIAN
  134. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  135. #else
  136. *px = (q15_t) in;
  137. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  138. /* Update the pointer px to point to the next row of the transposed matrix */
  139. px += numRowsB;
  140. /* Read two elements from the row */
  141. in = *__SIMD32(pInB)++;
  142. /* Unpack and store one element in the destination */
  143. #ifndef ARM_MATH_BIG_ENDIAN
  144. *px = (q15_t) in;
  145. #else
  146. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  147. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  148. /* Update the pointer px to point to the next row of the transposed matrix */
  149. px += numRowsB;
  150. /* Unpack and store the second element in the destination */
  151. #ifndef ARM_MATH_BIG_ENDIAN
  152. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  153. #else
  154. *px = (q15_t) in;
  155. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  156. /* Update the pointer px to point to the next row of the transposed matrix */
  157. px += numRowsB;
  158. #else
  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. /* Read one element from the row */
  172. in = *pInB++;
  173. /* Store one element in the destination */
  174. *px = in;
  175. /* Update the pointer px to point to the next row of the transposed matrix */
  176. px += numRowsB;
  177. /* Read one element from the row */
  178. in = *pInB++;
  179. /* Store one element in the destination */
  180. *px = in;
  181. /* Update the pointer px to point to the next row of the transposed matrix */
  182. px += numRowsB;
  183. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  184. /* Decrement the column loop counter */
  185. col--;
  186. }
  187. /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
  188. ** No loop unrolling is used. */
  189. col = numColsB % 0x4u;
  190. while(col > 0u)
  191. {
  192. /* Read and store the input element in the destination */
  193. *px = *pInB++;
  194. /* Update the pointer px to point to the next row of the transposed matrix */
  195. px += numRowsB;
  196. /* Decrement the column loop counter */
  197. col--;
  198. }
  199. i++;
  200. /* Decrement the row loop counter */
  201. row--;
  202. } while(row > 0u);
  203. /* Reset the variables for the usage in the following multiplication process */
  204. row = numRowsA;
  205. i = 0u;
  206. px = pDst->pData;
  207. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  208. /* row loop */
  209. do
  210. {
  211. /* For every row wise process, the column loop counter is to be initiated */
  212. col = numColsB;
  213. /* For every row wise process, the pIn2 pointer is set
  214. ** to the starting address of the transposed pSrcB data */
  215. pInB = pSrcBT;
  216. /* column loop */
  217. do
  218. {
  219. /* Set the variable sum, that acts as accumulator, to zero */
  220. sum = 0;
  221. /* Apply loop unrolling and compute 2 MACs simultaneously. */
  222. colCnt = numColsA >> 2;
  223. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  224. pInA = pSrcA->pData + i;
  225. /* matrix multiplication */
  226. while(colCnt > 0u)
  227. {
  228. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  229. #ifndef UNALIGNED_SUPPORT_DISABLE
  230. /* read real and imag values from pSrcA and pSrcB buffer */
  231. pSourceA1 = *__SIMD32(pInA)++;
  232. pSourceB1 = *__SIMD32(pInB)++;
  233. pSourceA2 = *__SIMD32(pInA)++;
  234. pSourceB2 = *__SIMD32(pInB)++;
  235. /* Multiply and Accumlates */
  236. sum = __SMLALD(pSourceA1, pSourceB1, sum);
  237. sum = __SMLALD(pSourceA2, pSourceB2, sum);
  238. #else
  239. /* read real and imag values from pSrcA and pSrcB buffer */
  240. inA1 = *pInA++;
  241. inB1 = *pInB++;
  242. inA2 = *pInA++;
  243. /* Multiply and Accumlates */
  244. sum += inA1 * inB1;
  245. inB2 = *pInB++;
  246. inA1 = *pInA++;
  247. inB1 = *pInB++;
  248. /* Multiply and Accumlates */
  249. sum += inA2 * inB2;
  250. inA2 = *pInA++;
  251. inB2 = *pInB++;
  252. /* Multiply and Accumlates */
  253. sum += inA1 * inB1;
  254. sum += inA2 * inB2;
  255. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  256. /* Decrement the loop counter */
  257. colCnt--;
  258. }
  259. /* process remaining column samples */
  260. colCnt = numColsA & 3u;
  261. while(colCnt > 0u)
  262. {
  263. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  264. sum += *pInA++ * *pInB++;
  265. /* Decrement the loop counter */
  266. colCnt--;
  267. }
  268. /* Saturate and store the result in the destination buffer */
  269. *px = (q15_t) (__SSAT((sum >> 15), 16));
  270. px++;
  271. /* Decrement the column loop counter */
  272. col--;
  273. } while(col > 0u);
  274. i = i + numColsA;
  275. /* Decrement the row loop counter */
  276. row--;
  277. } while(row > 0u);
  278. #else
  279. /* Run the below code for Cortex-M0 */
  280. q15_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  281. q15_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  282. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
  283. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
  284. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  285. q15_t *px; /* Temporary output data matrix pointer */
  286. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  287. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  288. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  289. uint16_t col, i = 0u, row = numRowsA, colCnt; /* loop counters */
  290. arm_status status; /* status of matrix multiplication */
  291. #ifdef ARM_MATH_MATRIX_CHECK
  292. /* Check for matrix mismatch condition */
  293. if((pSrcA->numCols != pSrcB->numRows) ||
  294. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  295. {
  296. /* Set status as ARM_MATH_SIZE_MISMATCH */
  297. status = ARM_MATH_SIZE_MISMATCH;
  298. }
  299. else
  300. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  301. {
  302. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  303. /* row loop */
  304. do
  305. {
  306. /* Output pointer is set to starting address of the row being processed */
  307. px = pOut + i;
  308. /* For every row wise process, the column loop counter is to be initiated */
  309. col = numColsB;
  310. /* For every row wise process, the pIn2 pointer is set
  311. ** to the starting address of the pSrcB data */
  312. pIn2 = pSrcB->pData;
  313. /* column loop */
  314. do
  315. {
  316. /* Set the variable sum, that acts as accumulator, to zero */
  317. sum = 0;
  318. /* Initiate the pointer pIn1 to point to the starting address of pSrcA */
  319. pIn1 = pInA;
  320. /* Matrix A columns number of MAC operations are to be performed */
  321. colCnt = numColsA;
  322. /* matrix multiplication */
  323. while(colCnt > 0u)
  324. {
  325. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  326. /* Perform the multiply-accumulates */
  327. sum += (q31_t) * pIn1++ * *pIn2;
  328. pIn2 += numColsB;
  329. /* Decrement the loop counter */
  330. colCnt--;
  331. }
  332. /* Convert the result from 34.30 to 1.15 format and store the saturated value in destination buffer */
  333. /* Saturate and store the result in the destination buffer */
  334. *px++ = (q15_t) __SSAT((sum >> 15), 16);
  335. /* Decrement the column loop counter */
  336. col--;
  337. /* Update the pointer pIn2 to point to the starting address of the next column */
  338. pIn2 = pInB + (numColsB - col);
  339. } while(col > 0u);
  340. /* Update the pointer pSrcA to point to the starting address of the next row */
  341. i = i + numColsB;
  342. pInA = pInA + numColsA;
  343. /* Decrement the row loop counter */
  344. row--;
  345. } while(row > 0u);
  346. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  347. /* set status as ARM_MATH_SUCCESS */
  348. status = ARM_MATH_SUCCESS;
  349. }
  350. /* Return to application */
  351. return (status);
  352. }
  353. /**
  354. * @} end of MatrixMult group
  355. */