arm_mat_mult_fast_q15.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 19. March 2015
  5. * $Revision: V.1.4.5
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_mat_mult_fast_q15.c
  9. *
  10. * Description: Q15 matrix multiplication (fast variant)
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3
  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 (fast variant) for Cortex-M3 and Cortex-M4
  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
  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 difference between the function arm_mat_mult_q15() and this fast variant is that
  62. * the fast variant use a 32-bit rather than a 64-bit accumulator.
  63. * The result of each 1.15 x 1.15 multiplication is truncated to
  64. * 2.30 format. These intermediate results are accumulated in a 32-bit register in 2.30
  65. * format. Finally, the accumulator is saturated and converted to a 1.15 result.
  66. *
  67. * \par
  68. * The fast version has the same overflow behavior as the standard version but provides
  69. * less precision since it discards the low 16 bits of each multiplication result.
  70. * In order to avoid overflows completely the input signals must be scaled down.
  71. * Scale down one of the input matrices by log2(numColsA) bits to
  72. * avoid overflows, as a total of numColsA additions are computed internally for each
  73. * output element.
  74. *
  75. * \par
  76. * See <code>arm_mat_mult_q15()</code> for a slower implementation of this function
  77. * which uses 64-bit accumulation to provide higher precision.
  78. */
  79. arm_status arm_mat_mult_fast_q15(
  80. const arm_matrix_instance_q15 * pSrcA,
  81. const arm_matrix_instance_q15 * pSrcB,
  82. arm_matrix_instance_q15 * pDst,
  83. q15_t * pState)
  84. {
  85. q31_t sum; /* accumulator */
  86. q15_t *pSrcBT = pState; /* input data matrix pointer for transpose */
  87. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
  88. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
  89. q15_t *px; /* Temporary output data matrix pointer */
  90. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  91. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  92. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  93. uint16_t numRowsB = pSrcB->numRows; /* number of rows of input matrix A */
  94. uint16_t col, i = 0u, row = numRowsB, colCnt; /* loop counters */
  95. arm_status status; /* status of matrix multiplication */
  96. #ifndef UNALIGNED_SUPPORT_DISABLE
  97. q31_t in; /* Temporary variable to hold the input value */
  98. q31_t inA1, inA2, inB1, inB2;
  99. #else
  100. q15_t in; /* Temporary variable to hold the input value */
  101. q15_t inA1, inA2, inB1, inB2;
  102. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  103. #ifdef ARM_MATH_MATRIX_CHECK
  104. /* Check for matrix mismatch condition */
  105. if((pSrcA->numCols != pSrcB->numRows) ||
  106. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  107. {
  108. /* Set status as ARM_MATH_SIZE_MISMATCH */
  109. status = ARM_MATH_SIZE_MISMATCH;
  110. }
  111. else
  112. #endif
  113. {
  114. /* Matrix transpose */
  115. do
  116. {
  117. /* Apply loop unrolling and exchange the columns with row elements */
  118. col = numColsB >> 2;
  119. /* The pointer px is set to starting address of the column being processed */
  120. px = pSrcBT + i;
  121. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  122. ** a second loop below computes the remaining 1 to 3 samples. */
  123. while(col > 0u)
  124. {
  125. #ifndef UNALIGNED_SUPPORT_DISABLE
  126. /* Read two elements from the row */
  127. in = *__SIMD32(pInB)++;
  128. /* Unpack and store one element in the destination */
  129. #ifndef ARM_MATH_BIG_ENDIAN
  130. *px = (q15_t) in;
  131. #else
  132. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  133. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  134. /* Update the pointer px to point to the next row of the transposed matrix */
  135. px += numRowsB;
  136. /* Unpack and store the second element in the destination */
  137. #ifndef ARM_MATH_BIG_ENDIAN
  138. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  139. #else
  140. *px = (q15_t) in;
  141. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  142. /* Update the pointer px to point to the next row of the transposed matrix */
  143. px += numRowsB;
  144. /* Read two elements from the row */
  145. in = *__SIMD32(pInB)++;
  146. /* Unpack and store one element in the destination */
  147. #ifndef ARM_MATH_BIG_ENDIAN
  148. *px = (q15_t) in;
  149. #else
  150. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  151. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  152. /* Update the pointer px to point to the next row of the transposed matrix */
  153. px += numRowsB;
  154. /* Unpack and store the second element in the destination */
  155. #ifndef ARM_MATH_BIG_ENDIAN
  156. *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  157. #else
  158. *px = (q15_t) in;
  159. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  160. #else
  161. /* Read one element from the row */
  162. in = *pInB++;
  163. /* Store one element in the destination */
  164. *px = in;
  165. /* Update the pointer px to point to the next row of the transposed matrix */
  166. px += numRowsB;
  167. /* Read one element from the row */
  168. in = *pInB++;
  169. /* Store one element in the destination */
  170. *px = in;
  171. /* Update the pointer px to point to the next row of the transposed matrix */
  172. px += numRowsB;
  173. /* Read one element from the row */
  174. in = *pInB++;
  175. /* Store one element in the destination */
  176. *px = in;
  177. /* Update the pointer px to point to the next row of the transposed matrix */
  178. px += numRowsB;
  179. /* Read one element from the row */
  180. in = *pInB++;
  181. /* Store one element in the destination */
  182. *px = in;
  183. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  184. /* Update the pointer px to point to the next row of the transposed matrix */
  185. px += numRowsB;
  186. /* Decrement the column loop counter */
  187. col--;
  188. }
  189. /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
  190. ** No loop unrolling is used. */
  191. col = numColsB % 0x4u;
  192. while(col > 0u)
  193. {
  194. /* Read and store the input element in the destination */
  195. *px = *pInB++;
  196. /* Update the pointer px to point to the next row of the transposed matrix */
  197. px += numRowsB;
  198. /* Decrement the column loop counter */
  199. col--;
  200. }
  201. i++;
  202. /* Decrement the row loop counter */
  203. row--;
  204. } while(row > 0u);
  205. /* Reset the variables for the usage in the following multiplication process */
  206. row = numRowsA;
  207. i = 0u;
  208. px = pDst->pData;
  209. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  210. /* row loop */
  211. do
  212. {
  213. /* For every row wise process, the column loop counter is to be initiated */
  214. col = numColsB;
  215. /* For every row wise process, the pIn2 pointer is set
  216. ** to the starting address of the transposed pSrcB data */
  217. pInB = pSrcBT;
  218. /* column loop */
  219. do
  220. {
  221. /* Set the variable sum, that acts as accumulator, to zero */
  222. sum = 0;
  223. /* Apply loop unrolling and compute 2 MACs simultaneously. */
  224. colCnt = numColsA >> 2;
  225. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  226. pInA = pSrcA->pData + i;
  227. /* matrix multiplication */
  228. while(colCnt > 0u)
  229. {
  230. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  231. #ifndef UNALIGNED_SUPPORT_DISABLE
  232. inA1 = *__SIMD32(pInA)++;
  233. inB1 = *__SIMD32(pInB)++;
  234. inA2 = *__SIMD32(pInA)++;
  235. inB2 = *__SIMD32(pInB)++;
  236. sum = __SMLAD(inA1, inB1, sum);
  237. sum = __SMLAD(inA2, inB2, sum);
  238. #else
  239. inA1 = *pInA++;
  240. inB1 = *pInB++;
  241. inA2 = *pInA++;
  242. sum += inA1 * inB1;
  243. inB2 = *pInB++;
  244. inA1 = *pInA++;
  245. inB1 = *pInB++;
  246. sum += inA2 * inB2;
  247. inA2 = *pInA++;
  248. inB2 = *pInB++;
  249. sum += inA1 * inB1;
  250. sum += inA2 * inB2;
  251. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  252. /* Decrement the loop counter */
  253. colCnt--;
  254. }
  255. /* process odd column samples */
  256. colCnt = numColsA % 0x4u;
  257. while(colCnt > 0u)
  258. {
  259. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  260. sum += (q31_t) (*pInA++) * (*pInB++);
  261. colCnt--;
  262. }
  263. /* Saturate and store the result in the destination buffer */
  264. *px = (q15_t) (sum >> 15);
  265. px++;
  266. /* Decrement the column loop counter */
  267. col--;
  268. } while(col > 0u);
  269. i = i + numColsA;
  270. /* Decrement the row loop counter */
  271. row--;
  272. } while(row > 0u);
  273. /* set status as ARM_MATH_SUCCESS */
  274. status = ARM_MATH_SUCCESS;
  275. }
  276. /* Return to application */
  277. return (status);
  278. }
  279. /**
  280. * @} end of MatrixMult group
  281. */