arm_mat_mult_fast_q31.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_q31.c
  9. *
  10. * Description: Q31 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 Q31 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. * @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 difference between the function arm_mat_mult_q31() and this fast variant is that
  61. * the fast variant use a 32-bit rather than a 64-bit accumulator.
  62. * The result of each 1.31 x 1.31 multiplication is truncated to
  63. * 2.30 format. These intermediate results are accumulated in a 32-bit register in 2.30
  64. * format. Finally, the accumulator is saturated and converted to a 1.31 result.
  65. *
  66. * \par
  67. * The fast version has the same overflow behavior as the standard version but provides
  68. * less precision since it discards the low 32 bits of each multiplication result.
  69. * In order to avoid overflows completely the input signals must be scaled down.
  70. * Scale down one of the input matrices by log2(numColsA) bits to
  71. * avoid overflows, as a total of numColsA additions are computed internally for each
  72. * output element.
  73. *
  74. * \par
  75. * See <code>arm_mat_mult_q31()</code> for a slower implementation of this function
  76. * which uses 64-bit accumulation to provide higher precision.
  77. */
  78. arm_status arm_mat_mult_fast_q31(
  79. const arm_matrix_instance_q31 * pSrcA,
  80. const arm_matrix_instance_q31 * pSrcB,
  81. arm_matrix_instance_q31 * pDst)
  82. {
  83. q31_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  84. q31_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  85. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  86. // q31_t *pSrcB = pSrcB->pData; /* input data matrix pointer B */
  87. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  88. q31_t *px; /* Temporary output data matrix pointer */
  89. q31_t sum; /* Accumulator */
  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 col, i = 0u, j, row = numRowsA, colCnt; /* loop counters */
  94. arm_status status; /* status of matrix multiplication */
  95. q31_t inA1, inA2, inA3, inA4, inB1, inB2, inB3, inB4;
  96. #ifdef ARM_MATH_MATRIX_CHECK
  97. /* Check for matrix mismatch condition */
  98. if((pSrcA->numCols != pSrcB->numRows) ||
  99. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  100. {
  101. /* Set status as ARM_MATH_SIZE_MISMATCH */
  102. status = ARM_MATH_SIZE_MISMATCH;
  103. }
  104. else
  105. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  106. {
  107. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  108. /* row loop */
  109. do
  110. {
  111. /* Output pointer is set to starting address of the row being processed */
  112. px = pOut + i;
  113. /* For every row wise process, the column loop counter is to be initiated */
  114. col = numColsB;
  115. /* For every row wise process, the pIn2 pointer is set
  116. ** to the starting address of the pSrcB data */
  117. pIn2 = pSrcB->pData;
  118. j = 0u;
  119. /* column loop */
  120. do
  121. {
  122. /* Set the variable sum, that acts as accumulator, to zero */
  123. sum = 0;
  124. /* Initiate the pointer pIn1 to point to the starting address of pInA */
  125. pIn1 = pInA;
  126. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  127. colCnt = numColsA >> 2;
  128. /* matrix multiplication */
  129. while(colCnt > 0u)
  130. {
  131. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  132. /* Perform the multiply-accumulates */
  133. inB1 = *pIn2;
  134. pIn2 += numColsB;
  135. inA1 = pIn1[0];
  136. inA2 = pIn1[1];
  137. inB2 = *pIn2;
  138. pIn2 += numColsB;
  139. inB3 = *pIn2;
  140. pIn2 += numColsB;
  141. sum = (q31_t) ((((q63_t) sum << 32) + ((q63_t) inA1 * inB1)) >> 32);
  142. sum = (q31_t) ((((q63_t) sum << 32) + ((q63_t) inA2 * inB2)) >> 32);
  143. inA3 = pIn1[2];
  144. inA4 = pIn1[3];
  145. inB4 = *pIn2;
  146. pIn2 += numColsB;
  147. sum = (q31_t) ((((q63_t) sum << 32) + ((q63_t) inA3 * inB3)) >> 32);
  148. sum = (q31_t) ((((q63_t) sum << 32) + ((q63_t) inA4 * inB4)) >> 32);
  149. pIn1 += 4u;
  150. /* Decrement the loop counter */
  151. colCnt--;
  152. }
  153. /* If the columns of pSrcA is not a multiple of 4, compute any remaining output samples here.
  154. ** No loop unrolling is used. */
  155. colCnt = numColsA % 0x4u;
  156. while(colCnt > 0u)
  157. {
  158. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  159. /* Perform the multiply-accumulates */
  160. sum = (q31_t) ((((q63_t) sum << 32) +
  161. ((q63_t) * pIn1++ * (*pIn2))) >> 32);
  162. pIn2 += numColsB;
  163. /* Decrement the loop counter */
  164. colCnt--;
  165. }
  166. /* Convert the result from 2.30 to 1.31 format and store in destination buffer */
  167. *px++ = sum << 1;
  168. /* Update the pointer pIn2 to point to the starting address of the next column */
  169. j++;
  170. pIn2 = pSrcB->pData + j;
  171. /* Decrement the column loop counter */
  172. col--;
  173. } while(col > 0u);
  174. /* Update the pointer pInA to point to the starting address of the next row */
  175. i = i + numColsB;
  176. pInA = pInA + numColsA;
  177. /* Decrement the row loop counter */
  178. row--;
  179. } while(row > 0u);
  180. /* set status as ARM_MATH_SUCCESS */
  181. status = ARM_MATH_SUCCESS;
  182. }
  183. /* Return to application */
  184. return (status);
  185. }
  186. /**
  187. * @} end of MatrixMult group
  188. */