arm_mat_mult_f64.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_mult_f64.c
  4. * Description: Floating-point matrix multiplication
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/matrix_functions.h"
  29. /**
  30. * @ingroup groupMatrix
  31. */
  32. /**
  33. * @defgroup MatrixMult Matrix Multiplication
  34. *
  35. * Multiplies two matrices.
  36. *
  37. * \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices"
  38. * Matrix multiplication is only defined if the number of columns of the
  39. * first matrix equals the number of rows of the second matrix.
  40. * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
  41. * in an <code>M x P</code> matrix.
  42. * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
  43. * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
  44. * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
  45. */
  46. /**
  47. * @addtogroup MatrixMult
  48. * @{
  49. */
  50. /**
  51. * @brief Floating-point matrix multiplication.
  52. * @param[in] *pSrcA points to the first input matrix structure
  53. * @param[in] *pSrcB points to the second input matrix structure
  54. * @param[out] *pDst points to output matrix structure
  55. * @return The function returns either
  56. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  57. */
  58. arm_status arm_mat_mult_f64(
  59. const arm_matrix_instance_f64 * pSrcA,
  60. const arm_matrix_instance_f64 * pSrcB,
  61. arm_matrix_instance_f64 * pDst)
  62. {
  63. float64_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  64. float64_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  65. float64_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  66. float64_t *pInB = pSrcB->pData; /* Input data matrix pointer B */
  67. float64_t *pOut = pDst->pData; /* Output data matrix pointer */
  68. float64_t *px; /* Temporary output data matrix pointer */
  69. float64_t sum; /* Accumulator */
  70. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  71. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  72. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  73. uint64_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */
  74. arm_status status; /* Status of matrix multiplication */
  75. #ifdef ARM_MATH_MATRIX_CHECK
  76. /* Check for matrix mismatch condition */
  77. if ((pSrcA->numCols != pSrcB->numRows) ||
  78. (pSrcA->numRows != pDst->numRows) ||
  79. (pSrcB->numCols != pDst->numCols) )
  80. {
  81. /* Set status as ARM_MATH_SIZE_MISMATCH */
  82. status = ARM_MATH_SIZE_MISMATCH;
  83. }
  84. else
  85. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  86. {
  87. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  88. /* row loop */
  89. do
  90. {
  91. /* Output pointer is set to starting address of row being processed */
  92. px = pOut + i;
  93. /* For every row wise process, column loop counter is to be initiated */
  94. col = numColsB;
  95. /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */
  96. pIn2 = pSrcB->pData;
  97. /* column loop */
  98. do
  99. {
  100. /* Set the variable sum, that acts as accumulator, to zero */
  101. sum = 0.0;
  102. /* Initialize pointer pIn1 to point to starting address of column being processed */
  103. pIn1 = pInA;
  104. #if defined (ARM_MATH_LOOPUNROLL)
  105. /* Loop unrolling: Compute 4 MACs at a time. */
  106. colCnt = numColsA >> 2U;
  107. /* matrix multiplication */
  108. while (colCnt > 0U)
  109. {
  110. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  111. /* Perform the multiply-accumulates */
  112. sum += *pIn1++ * *pIn2;
  113. pIn2 += numColsB;
  114. sum += *pIn1++ * *pIn2;
  115. pIn2 += numColsB;
  116. sum += *pIn1++ * *pIn2;
  117. pIn2 += numColsB;
  118. sum += *pIn1++ * *pIn2;
  119. pIn2 += numColsB;
  120. /* Decrement loop counter */
  121. colCnt--;
  122. }
  123. /* Loop unrolling: Compute remaining MACs */
  124. colCnt = numColsA % 0x4U;
  125. #else
  126. /* Initialize cntCnt with number of columns */
  127. colCnt = numColsA;
  128. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  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. sum += *pIn1++ * *pIn2;
  134. pIn2 += numColsB;
  135. /* Decrement loop counter */
  136. colCnt--;
  137. }
  138. /* Store result in destination buffer */
  139. *px++ = sum;
  140. /* Decrement column loop counter */
  141. col--;
  142. /* Update pointer pIn2 to point to starting address of next column */
  143. pIn2 = pInB + (numColsB - col);
  144. } while (col > 0U);
  145. /* Update pointer pInA to point to starting address of next row */
  146. i = i + numColsB;
  147. pInA = pInA + numColsA;
  148. /* Decrement row loop counter */
  149. row--;
  150. } while (row > 0U);
  151. /* Set status as ARM_MATH_SUCCESS */
  152. status = ARM_MATH_SUCCESS;
  153. }
  154. /* Return to application */
  155. return (status);
  156. }
  157. /**
  158. * @} end of MatrixMult group
  159. */