arm_mat_mult_fast_q31.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_fast_q31.c
  4. * Description: Q31 matrix multiplication (fast variant)
  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 Q31 matrix multiplication (fast variant) for Cortex-M3 and Cortex-M4
  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. * @return The function returns either
  42. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  43. *
  44. * @details
  45. * <b>Scaling and Overflow Behavior:</b>
  46. *
  47. * \par
  48. * The difference between the function arm_mat_mult_q31() and this fast variant is that
  49. * the fast variant use a 32-bit rather than a 64-bit accumulator.
  50. * The result of each 1.31 x 1.31 multiplication is truncated to
  51. * 2.30 format. These intermediate results are accumulated in a 32-bit register in 2.30
  52. * format. Finally, the accumulator is saturated and converted to a 1.31 result.
  53. *
  54. * \par
  55. * The fast version has the same overflow behavior as the standard version but provides
  56. * less precision since it discards the low 32 bits of each multiplication result.
  57. * In order to avoid overflows completely the input signals must be scaled down.
  58. * Scale down one of the input matrices by log2(numColsA) bits to
  59. * avoid overflows, as a total of numColsA additions are computed internally for each
  60. * output element.
  61. *
  62. * \par
  63. * See <code>arm_mat_mult_q31()</code> for a slower implementation of this function
  64. * which uses 64-bit accumulation to provide higher precision.
  65. */
  66. arm_status arm_mat_mult_fast_q31(
  67. const arm_matrix_instance_q31 * pSrcA,
  68. const arm_matrix_instance_q31 * pSrcB,
  69. arm_matrix_instance_q31 * pDst)
  70. {
  71. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  72. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  73. q31_t *px; /* Temporary output data matrix pointer */
  74. q31_t sum; /* Accumulator */
  75. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  76. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  77. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  78. uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  79. arm_status status; /* status of matrix multiplication */
  80. q31_t inA1, inB1;
  81. #if defined (ARM_MATH_DSP)
  82. q31_t sum2, sum3, sum4;
  83. q31_t inA2, inB2;
  84. q31_t *pInA2;
  85. q31_t *px2;
  86. #endif
  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. px = pDst->pData;
  99. #if defined (ARM_MATH_DSP)
  100. row = row >> 1;
  101. px2 = px + numColsB;
  102. #endif
  103. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  104. /* row loop */
  105. while (row > 0U)
  106. {
  107. /* For every row wise process, the column loop counter is to be initiated */
  108. col = numColsB;
  109. /* For every row wise process, the pIn2 pointer is set
  110. ** to the starting address of the pSrcB data */
  111. pInB = pSrcB->pData;
  112. j = 0U;
  113. #if defined (ARM_MATH_DSP)
  114. col = col >> 1;
  115. #endif
  116. /* column loop */
  117. while (col > 0U)
  118. {
  119. /* Set the variable sum, that acts as accumulator, to zero */
  120. sum = 0;
  121. /* Initiate data pointers */
  122. pInA = pSrcA->pData + i;
  123. pInB = pSrcB->pData + j;
  124. #if defined (ARM_MATH_DSP)
  125. sum2 = 0;
  126. sum3 = 0;
  127. sum4 = 0;
  128. pInA2 = pInA + numColsA;
  129. colCnt = numColsA;
  130. #else
  131. colCnt = numColsA >> 2;
  132. #endif
  133. /* matrix multiplication */
  134. while (colCnt > 0U)
  135. {
  136. #if defined (ARM_MATH_DSP)
  137. inA1 = *pInA++;
  138. inB1 = pInB[0];
  139. inA2 = *pInA2++;
  140. inB2 = pInB[1];
  141. pInB += numColsB;
  142. sum = __SMMLA(inA1, inB1, sum);
  143. sum2 = __SMMLA(inA1, inB2, sum2);
  144. sum3 = __SMMLA(inA2, inB1, sum3);
  145. sum4 = __SMMLA(inA2, inB2, sum4);
  146. #else
  147. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  148. /* Perform the multiply-accumulates */
  149. inB1 = *pInB;
  150. pInB += numColsB;
  151. inA1 = pInA[0];
  152. sum = __SMMLA(inA1, inB1, sum);
  153. inB1 = *pInB;
  154. pInB += numColsB;
  155. inA1 = pInA[1];
  156. sum = __SMMLA(inA1, inB1, sum);
  157. inB1 = *pInB;
  158. pInB += numColsB;
  159. inA1 = pInA[2];
  160. sum = __SMMLA(inA1, inB1, sum);
  161. inB1 = *pInB;
  162. pInB += numColsB;
  163. inA1 = pInA[3];
  164. sum = __SMMLA(inA1, inB1, sum);
  165. pInA += 4U;
  166. #endif
  167. /* Decrement the loop counter */
  168. colCnt--;
  169. }
  170. #ifdef ARM_MATH_CM0_FAMILY
  171. /* If the columns of pSrcA is not a multiple of 4, compute any remaining output samples here. */
  172. colCnt = numColsA % 0x4U;
  173. while (colCnt > 0U)
  174. {
  175. sum = __SMMLA(*pInA++, *pInB, sum);
  176. pInB += numColsB;
  177. colCnt--;
  178. }
  179. j++;
  180. #endif
  181. /* Convert the result from 2.30 to 1.31 format and store in destination buffer */
  182. *px++ = sum << 1;
  183. #if defined (ARM_MATH_DSP)
  184. *px++ = sum2 << 1;
  185. *px2++ = sum3 << 1;
  186. *px2++ = sum4 << 1;
  187. j += 2;
  188. #endif
  189. /* Decrement the column loop counter */
  190. col--;
  191. }
  192. i = i + numColsA;
  193. #if defined (ARM_MATH_DSP)
  194. i = i + numColsA;
  195. px = px2 + (numColsB & 1U);
  196. px2 = px + numColsB;
  197. #endif
  198. /* Decrement the row loop counter */
  199. row--;
  200. }
  201. /* Compute any remaining odd row/column below */
  202. #if defined (ARM_MATH_DSP)
  203. /* Compute remaining output column */
  204. if (numColsB & 1U) {
  205. /* Avoid redundant computation of last element */
  206. row = numRowsA & (~0x1);
  207. /* Point to remaining unfilled column in output matrix */
  208. px = pDst->pData+numColsB-1;
  209. pInA = pSrcA->pData;
  210. /* row loop */
  211. while (row > 0)
  212. {
  213. /* point to last column in matrix B */
  214. pInB = pSrcB->pData + numColsB-1;
  215. /* Set the variable sum, that acts as accumulator, to zero */
  216. sum = 0;
  217. /* Compute 4 columns at once */
  218. colCnt = numColsA >> 2;
  219. /* matrix multiplication */
  220. while (colCnt > 0U)
  221. {
  222. inA1 = *pInA++;
  223. inA2 = *pInA++;
  224. inB1 = *pInB;
  225. pInB += numColsB;
  226. inB2 = *pInB;
  227. pInB += numColsB;
  228. sum = __SMMLA(inA1, inB1, sum);
  229. sum = __SMMLA(inA2, inB2, sum);
  230. inA1 = *pInA++;
  231. inA2 = *pInA++;
  232. inB1 = *pInB;
  233. pInB += numColsB;
  234. inB2 = *pInB;
  235. pInB += numColsB;
  236. sum = __SMMLA(inA1, inB1, sum);
  237. sum = __SMMLA(inA2, inB2, sum);
  238. /* Decrement the loop counter */
  239. colCnt--;
  240. }
  241. colCnt = numColsA & 3U;
  242. while (colCnt > 0U) {
  243. sum = __SMMLA(*pInA++, *pInB, sum);
  244. pInB += numColsB;
  245. colCnt--;
  246. }
  247. /* Convert the result from 2.30 to 1.31 format and store in destination buffer */
  248. *px = sum << 1;
  249. px += numColsB;
  250. /* Decrement the row loop counter */
  251. row--;
  252. }
  253. }
  254. /* Compute remaining output row */
  255. if (numRowsA & 1U) {
  256. /* point to last row in output matrix */
  257. px = pDst->pData+(numColsB)*(numRowsA-1);
  258. col = numColsB;
  259. i = 0U;
  260. /* col loop */
  261. while (col > 0)
  262. {
  263. /* point to last row in matrix A */
  264. pInA = pSrcA->pData + (numRowsA-1)*numColsA;
  265. pInB = pSrcB->pData + i;
  266. /* Set the variable sum, that acts as accumulator, to zero */
  267. sum = 0;
  268. /* Compute 4 columns at once */
  269. colCnt = numColsA >> 2;
  270. /* matrix multiplication */
  271. while (colCnt > 0U)
  272. {
  273. inA1 = *pInA++;
  274. inA2 = *pInA++;
  275. inB1 = *pInB;
  276. pInB += numColsB;
  277. inB2 = *pInB;
  278. pInB += numColsB;
  279. sum = __SMMLA(inA1, inB1, sum);
  280. sum = __SMMLA(inA2, inB2, sum);
  281. inA1 = *pInA++;
  282. inA2 = *pInA++;
  283. inB1 = *pInB;
  284. pInB += numColsB;
  285. inB2 = *pInB;
  286. pInB += numColsB;
  287. sum = __SMMLA(inA1, inB1, sum);
  288. sum = __SMMLA(inA2, inB2, sum);
  289. /* Decrement the loop counter */
  290. colCnt--;
  291. }
  292. colCnt = numColsA & 3U;
  293. while (colCnt > 0U) {
  294. sum = __SMMLA(*pInA++, *pInB, sum);
  295. pInB += numColsB;
  296. colCnt--;
  297. }
  298. /* Saturate and store the result in the destination buffer */
  299. *px++ = sum << 1;
  300. i++;
  301. /* Decrement the col loop counter */
  302. col--;
  303. }
  304. }
  305. #endif /* #if defined (ARM_MATH_DSP) */
  306. /* set status as ARM_MATH_SUCCESS */
  307. status = ARM_MATH_SUCCESS;
  308. }
  309. /* Return to application */
  310. return (status);
  311. }
  312. /**
  313. * @} end of MatrixMult group
  314. */