arm_mat_cmplx_mult_f32.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_cmplx_mult_f32.c
  4. * Description: Floating-point matrix multiplication
  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. * @defgroup CmplxMatrixMult Complex Matrix Multiplication
  34. *
  35. * Complex Matrix multiplication is only defined if the number of columns of the
  36. * first matrix equals the number of rows of the second matrix.
  37. * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
  38. * in an <code>M x P</code> matrix.
  39. * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
  40. * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
  41. * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
  42. */
  43. /**
  44. * @addtogroup CmplxMatrixMult
  45. * @{
  46. */
  47. /**
  48. * @brief Floating-point Complex matrix multiplication.
  49. * @param[in] *pSrcA points to the first input complex matrix structure
  50. * @param[in] *pSrcB points to the second input complex matrix structure
  51. * @param[out] *pDst points to output complex matrix structure
  52. * @return The function returns either
  53. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  54. */
  55. arm_status arm_mat_cmplx_mult_f32(
  56. const arm_matrix_instance_f32 * pSrcA,
  57. const arm_matrix_instance_f32 * pSrcB,
  58. arm_matrix_instance_f32 * pDst)
  59. {
  60. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  61. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  62. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  63. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  64. float32_t *px; /* Temporary output data matrix pointer */
  65. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  66. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  67. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  68. float32_t sumReal1, sumImag1; /* accumulator */
  69. float32_t a0, b0, c0, d0;
  70. float32_t a1, b1, c1, d1;
  71. float32_t sumReal2, sumImag2; /* accumulator */
  72. /* Run the below code for Cortex-M4 and Cortex-M3 */
  73. uint16_t col, i = 0U, j, 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) || (pSrcB->numCols != pDst->numCols))
  79. {
  80. /* Set status as ARM_MATH_SIZE_MISMATCH */
  81. status = ARM_MATH_SIZE_MISMATCH;
  82. }
  83. else
  84. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  85. {
  86. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  87. /* row loop */
  88. do
  89. {
  90. /* Output pointer is set to starting address of the row being processed */
  91. px = pOut + 2 * i;
  92. /* For every row wise process, the column loop counter is to be initiated */
  93. col = numColsB;
  94. /* For every row wise process, the pIn2 pointer is set
  95. ** to the starting address of the pSrcB data */
  96. pIn2 = pSrcB->pData;
  97. j = 0U;
  98. /* column loop */
  99. do
  100. {
  101. /* Set the variable sum, that acts as accumulator, to zero */
  102. sumReal1 = 0.0f;
  103. sumImag1 = 0.0f;
  104. sumReal2 = 0.0f;
  105. sumImag2 = 0.0f;
  106. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  107. pIn1 = pInA;
  108. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  109. colCnt = numColsA >> 2;
  110. /* matrix multiplication */
  111. while (colCnt > 0U)
  112. {
  113. /* Reading real part of complex matrix A */
  114. a0 = *pIn1;
  115. /* Reading real part of complex matrix B */
  116. c0 = *pIn2;
  117. /* Reading imaginary part of complex matrix A */
  118. b0 = *(pIn1 + 1U);
  119. /* Reading imaginary part of complex matrix B */
  120. d0 = *(pIn2 + 1U);
  121. sumReal1 += a0 * c0;
  122. sumImag1 += b0 * c0;
  123. pIn1 += 2U;
  124. pIn2 += 2 * numColsB;
  125. sumReal2 -= b0 * d0;
  126. sumImag2 += a0 * d0;
  127. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  128. a1 = *pIn1;
  129. c1 = *pIn2;
  130. b1 = *(pIn1 + 1U);
  131. d1 = *(pIn2 + 1U);
  132. sumReal1 += a1 * c1;
  133. sumImag1 += b1 * c1;
  134. pIn1 += 2U;
  135. pIn2 += 2 * numColsB;
  136. sumReal2 -= b1 * d1;
  137. sumImag2 += a1 * d1;
  138. a0 = *pIn1;
  139. c0 = *pIn2;
  140. b0 = *(pIn1 + 1U);
  141. d0 = *(pIn2 + 1U);
  142. sumReal1 += a0 * c0;
  143. sumImag1 += b0 * c0;
  144. pIn1 += 2U;
  145. pIn2 += 2 * numColsB;
  146. sumReal2 -= b0 * d0;
  147. sumImag2 += a0 * d0;
  148. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  149. a1 = *pIn1;
  150. c1 = *pIn2;
  151. b1 = *(pIn1 + 1U);
  152. d1 = *(pIn2 + 1U);
  153. sumReal1 += a1 * c1;
  154. sumImag1 += b1 * c1;
  155. pIn1 += 2U;
  156. pIn2 += 2 * numColsB;
  157. sumReal2 -= b1 * d1;
  158. sumImag2 += a1 * d1;
  159. /* Decrement the loop count */
  160. colCnt--;
  161. }
  162. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  163. ** No loop unrolling is used. */
  164. colCnt = numColsA % 0x4U;
  165. while (colCnt > 0U)
  166. {
  167. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  168. a1 = *pIn1;
  169. c1 = *pIn2;
  170. b1 = *(pIn1 + 1U);
  171. d1 = *(pIn2 + 1U);
  172. sumReal1 += a1 * c1;
  173. sumImag1 += b1 * c1;
  174. pIn1 += 2U;
  175. pIn2 += 2 * numColsB;
  176. sumReal2 -= b1 * d1;
  177. sumImag2 += a1 * d1;
  178. /* Decrement the loop counter */
  179. colCnt--;
  180. }
  181. sumReal1 += sumReal2;
  182. sumImag1 += sumImag2;
  183. /* Store the result in the destination buffer */
  184. *px++ = sumReal1;
  185. *px++ = sumImag1;
  186. /* Update the pointer pIn2 to point to the starting address of the next column */
  187. j++;
  188. pIn2 = pSrcB->pData + 2U * j;
  189. /* Decrement the column loop counter */
  190. col--;
  191. } while (col > 0U);
  192. /* Update the pointer pInA to point to the starting address of the next row */
  193. i = i + numColsB;
  194. pInA = pInA + 2 * numColsA;
  195. /* Decrement the row loop counter */
  196. row--;
  197. } while (row > 0U);
  198. /* Set status as ARM_MATH_SUCCESS */
  199. status = ARM_MATH_SUCCESS;
  200. }
  201. /* Return to application */
  202. return (status);
  203. }
  204. /**
  205. * @} end of MatrixMult group
  206. */