arm_mat_mult_q31.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_q31.c
  9. *
  10. * Description: Q31 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 Q31 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. * @return The function returns either
  54. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  55. *
  56. * @details
  57. * <b>Scaling and Overflow Behavior:</b>
  58. *
  59. * \par
  60. * The function is implemented using an internal 64-bit accumulator.
  61. * The accumulator has a 2.62 format and maintains full precision of the intermediate
  62. * multiplication results but provides only a single guard bit. There is no saturation
  63. * on intermediate additions. Thus, if the accumulator overflows it wraps around and
  64. * distorts the result. The input signals should be scaled down to avoid intermediate
  65. * overflows. The input is thus scaled down by log2(numColsA) bits
  66. * to avoid overflows, as a total of numColsA additions are performed internally.
  67. * The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  68. *
  69. * \par
  70. * See <code>arm_mat_mult_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
  71. *
  72. */
  73. arm_status arm_mat_mult_q31(
  74. const arm_matrix_instance_q31 * pSrcA,
  75. const arm_matrix_instance_q31 * pSrcB,
  76. arm_matrix_instance_q31 * pDst)
  77. {
  78. q31_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  79. q31_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  80. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  81. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  82. q31_t *px; /* Temporary output data matrix pointer */
  83. q63_t sum; /* Accumulator */
  84. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  85. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  86. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  87. #ifndef ARM_MATH_CM0_FAMILY
  88. /* Run the below code for Cortex-M4 and Cortex-M3 */
  89. uint16_t col, i = 0u, j, row = numRowsA, colCnt; /* loop counters */
  90. arm_status status; /* status of matrix multiplication */
  91. q31_t a0, a1, a2, a3, b0, b1, b2, b3;
  92. #ifdef ARM_MATH_MATRIX_CHECK
  93. /* Check for matrix mismatch condition */
  94. if((pSrcA->numCols != pSrcB->numRows) ||
  95. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  96. {
  97. /* Set status as ARM_MATH_SIZE_MISMATCH */
  98. status = ARM_MATH_SIZE_MISMATCH;
  99. }
  100. else
  101. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  102. {
  103. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  104. /* row loop */
  105. do
  106. {
  107. /* Output pointer is set to starting address of the row being processed */
  108. px = pOut + i;
  109. /* For every row wise process, the column loop counter is to be initiated */
  110. col = numColsB;
  111. /* For every row wise process, the pIn2 pointer is set
  112. ** to the starting address of the pSrcB data */
  113. pIn2 = pSrcB->pData;
  114. j = 0u;
  115. /* column loop */
  116. do
  117. {
  118. /* Set the variable sum, that acts as accumulator, to zero */
  119. sum = 0;
  120. /* Initiate the pointer pIn1 to point to the starting address of pInA */
  121. pIn1 = pInA;
  122. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  123. colCnt = numColsA >> 2;
  124. /* matrix multiplication */
  125. while(colCnt > 0u)
  126. {
  127. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  128. /* Perform the multiply-accumulates */
  129. b0 = *pIn2;
  130. pIn2 += numColsB;
  131. a0 = *pIn1++;
  132. a1 = *pIn1++;
  133. b1 = *pIn2;
  134. pIn2 += numColsB;
  135. b2 = *pIn2;
  136. pIn2 += numColsB;
  137. sum += (q63_t) a0 *b0;
  138. sum += (q63_t) a1 *b1;
  139. a2 = *pIn1++;
  140. a3 = *pIn1++;
  141. b3 = *pIn2;
  142. pIn2 += numColsB;
  143. sum += (q63_t) a2 *b2;
  144. sum += (q63_t) a3 *b3;
  145. /* Decrement the loop counter */
  146. colCnt--;
  147. }
  148. /* If the columns of pSrcA is not a multiple of 4, compute any remaining output samples here.
  149. ** No loop unrolling is used. */
  150. colCnt = numColsA % 0x4u;
  151. while(colCnt > 0u)
  152. {
  153. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  154. /* Perform the multiply-accumulates */
  155. sum += (q63_t) * pIn1++ * *pIn2;
  156. pIn2 += numColsB;
  157. /* Decrement the loop counter */
  158. colCnt--;
  159. }
  160. /* Convert the result from 2.62 to 1.31 format and store in destination buffer */
  161. *px++ = (q31_t) (sum >> 31);
  162. /* Update the pointer pIn2 to point to the starting address of the next column */
  163. j++;
  164. pIn2 = (pSrcB->pData) + j;
  165. /* Decrement the column loop counter */
  166. col--;
  167. } while(col > 0u);
  168. #else
  169. /* Run the below code for Cortex-M0 */
  170. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  171. uint16_t col, i = 0u, row = numRowsA, colCnt; /* loop counters */
  172. arm_status status; /* status of matrix multiplication */
  173. #ifdef ARM_MATH_MATRIX_CHECK
  174. /* Check for matrix mismatch condition */
  175. if((pSrcA->numCols != pSrcB->numRows) ||
  176. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  177. {
  178. /* Set status as ARM_MATH_SIZE_MISMATCH */
  179. status = ARM_MATH_SIZE_MISMATCH;
  180. }
  181. else
  182. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  183. {
  184. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  185. /* row loop */
  186. do
  187. {
  188. /* Output pointer is set to starting address of the row being processed */
  189. px = pOut + i;
  190. /* For every row wise process, the column loop counter is to be initiated */
  191. col = numColsB;
  192. /* For every row wise process, the pIn2 pointer is set
  193. ** to the starting address of the pSrcB data */
  194. pIn2 = pSrcB->pData;
  195. /* column loop */
  196. do
  197. {
  198. /* Set the variable sum, that acts as accumulator, to zero */
  199. sum = 0;
  200. /* Initiate the pointer pIn1 to point to the starting address of pInA */
  201. pIn1 = pInA;
  202. /* Matrix A columns number of MAC operations are to be performed */
  203. colCnt = numColsA;
  204. /* matrix multiplication */
  205. while(colCnt > 0u)
  206. {
  207. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  208. /* Perform the multiply-accumulates */
  209. sum += (q63_t) * pIn1++ * *pIn2;
  210. pIn2 += numColsB;
  211. /* Decrement the loop counter */
  212. colCnt--;
  213. }
  214. /* Convert the result from 2.62 to 1.31 format and store in destination buffer */
  215. *px++ = (q31_t) clip_q63_to_q31(sum >> 31);
  216. /* Decrement the column loop counter */
  217. col--;
  218. /* Update the pointer pIn2 to point to the starting address of the next column */
  219. pIn2 = pInB + (numColsB - col);
  220. } while(col > 0u);
  221. #endif
  222. /* Update the pointer pInA to point to the starting address of the next row */
  223. i = i + numColsB;
  224. pInA = pInA + numColsA;
  225. /* Decrement the row loop counter */
  226. row--;
  227. } while(row > 0u);
  228. /* set status as ARM_MATH_SUCCESS */
  229. status = ARM_MATH_SUCCESS;
  230. }
  231. /* Return to application */
  232. return (status);
  233. }
  234. /**
  235. * @} end of MatrixMult group
  236. */