arm_mat_cmplx_mult_f32.c 8.8 KB

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