arm_mat_cmplx_mult_q31.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_cmplx_mult_q31.c
  4. * Description: Floating-point matrix multiplication
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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. @addtogroup CmplxMatrixMult
  34. @{
  35. */
  36. /**
  37. @brief Q31 Complex matrix multiplication.
  38. @param[in] pSrcA points to first input complex matrix structure
  39. @param[in] pSrcB points to second input complex matrix structure
  40. @param[out] pDst points to output complex matrix structure
  41. @return execution status
  42. - \ref ARM_MATH_SUCCESS : Operation successful
  43. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  44. @par Scaling and Overflow Behavior
  45. The function is implemented using an internal 64-bit accumulator.
  46. The accumulator has a 2.62 format and maintains full precision of the intermediate
  47. multiplication results but provides only a single guard bit. There is no saturation
  48. on intermediate additions. Thus, if the accumulator overflows it wraps around and
  49. distorts the result. The input signals should be scaled down to avoid intermediate
  50. overflows. The input is thus scaled down by log2(numColsA) bits
  51. to avoid overflows, as a total of numColsA additions are performed internally.
  52. The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  53. */
  54. arm_status arm_mat_cmplx_mult_q31(
  55. const arm_matrix_instance_q31 * pSrcA,
  56. const arm_matrix_instance_q31 * pSrcB,
  57. arm_matrix_instance_q31 * pDst)
  58. {
  59. q31_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  60. q31_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  61. q31_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  62. q31_t *pOut = pDst->pData; /* Output data matrix pointer */
  63. q31_t *px; /* Temporary output data matrix pointer */
  64. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  65. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  66. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  67. q63_t sumReal, sumImag; /* Accumulator */
  68. q31_t a1, b1, c1, d1;
  69. uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  70. arm_status status; /* status of matrix multiplication */
  71. #if defined (ARM_MATH_LOOPUNROLL)
  72. q31_t a0, b0, c0, d0;
  73. #endif
  74. #ifdef ARM_MATH_MATRIX_CHECK
  75. /* Check for matrix mismatch condition */
  76. if ((pSrcA->numCols != pSrcB->numRows) ||
  77. (pSrcA->numRows != pDst->numRows) ||
  78. (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. sumReal = 0.0;
  103. sumImag = 0.0;
  104. /* Initiate pointer pIn1 to point to starting address of column being processed */
  105. pIn1 = pInA;
  106. #if defined (ARM_MATH_LOOPUNROLL)
  107. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  108. colCnt = numColsA >> 2U;
  109. /* matrix multiplication */
  110. while (colCnt > 0U)
  111. {
  112. /* Reading real part of complex matrix A */
  113. a0 = *pIn1;
  114. /* Reading real part of complex matrix B */
  115. c0 = *pIn2;
  116. /* Reading imaginary part of complex matrix A */
  117. b0 = *(pIn1 + 1U);
  118. /* Reading imaginary part of complex matrix B */
  119. d0 = *(pIn2 + 1U);
  120. /* Multiply and Accumlates */
  121. sumReal += (q63_t) a0 * c0;
  122. sumImag += (q63_t) b0 * c0;
  123. /* update pointers */
  124. pIn1 += 2U;
  125. pIn2 += 2 * numColsB;
  126. /* Multiply and Accumlates */
  127. sumReal -= (q63_t) b0 * d0;
  128. sumImag += (q63_t) a0 * d0;
  129. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  130. /* read real and imag values from pSrcA and pSrcB buffer */
  131. a1 = *(pIn1 );
  132. c1 = *(pIn2 );
  133. b1 = *(pIn1 + 1U);
  134. d1 = *(pIn2 + 1U);
  135. /* Multiply and Accumlates */
  136. sumReal += (q63_t) a1 * c1;
  137. sumImag += (q63_t) b1 * c1;
  138. /* update pointers */
  139. pIn1 += 2U;
  140. pIn2 += 2 * numColsB;
  141. /* Multiply and Accumlates */
  142. sumReal -= (q63_t) b1 * d1;
  143. sumImag += (q63_t) a1 * d1;
  144. a0 = *(pIn1 );
  145. c0 = *(pIn2 );
  146. b0 = *(pIn1 + 1U);
  147. d0 = *(pIn2 + 1U);
  148. /* Multiply and Accumlates */
  149. sumReal += (q63_t) a0 * c0;
  150. sumImag += (q63_t) b0 * c0;
  151. /* update pointers */
  152. pIn1 += 2U;
  153. pIn2 += 2 * numColsB;
  154. /* Multiply and Accumlates */
  155. sumReal -= (q63_t) b0 * d0;
  156. sumImag += (q63_t) a0 * d0;
  157. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  158. a1 = *(pIn1 );
  159. c1 = *(pIn2 );
  160. b1 = *(pIn1 + 1U);
  161. d1 = *(pIn2 + 1U);
  162. /* Multiply and Accumlates */
  163. sumReal += (q63_t) a1 * c1;
  164. sumImag += (q63_t) b1 * c1;
  165. /* update pointers */
  166. pIn1 += 2U;
  167. pIn2 += 2 * numColsB;
  168. /* Multiply and Accumlates */
  169. sumReal -= (q63_t) b1 * d1;
  170. sumImag += (q63_t) a1 * d1;
  171. /* Decrement 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. #else
  178. /* Initialize blkCnt with number of samples */
  179. colCnt = numColsA;
  180. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  181. while (colCnt > 0U)
  182. {
  183. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  184. a1 = *(pIn1 );
  185. c1 = *(pIn2 );
  186. b1 = *(pIn1 + 1U);
  187. d1 = *(pIn2 + 1U);
  188. /* Multiply and Accumlates */
  189. sumReal += (q63_t) a1 * c1;
  190. sumImag += (q63_t) b1 * c1;
  191. /* update pointers */
  192. pIn1 += 2U;
  193. pIn2 += 2 * numColsB;
  194. /* Multiply and Accumlates */
  195. sumReal -= (q63_t) b1 * d1;
  196. sumImag += (q63_t) a1 * d1;
  197. /* Decrement loop counter */
  198. colCnt--;
  199. }
  200. /* Store result in destination buffer */
  201. *px++ = (q31_t) clip_q63_to_q31(sumReal >> 31);
  202. *px++ = (q31_t) clip_q63_to_q31(sumImag >> 31);
  203. /* Update pointer pIn2 to point to starting address of next column */
  204. j++;
  205. pIn2 = pSrcB->pData + 2U * j;
  206. /* Decrement column loop counter */
  207. col--;
  208. } while (col > 0U);
  209. /* Update pointer pInA to point to starting address of next row */
  210. i = i + numColsB;
  211. pInA = pInA + 2 * numColsA;
  212. /* Decrement row loop counter */
  213. row--;
  214. } while (row > 0U);
  215. /* Set status as ARM_MATH_SUCCESS */
  216. status = ARM_MATH_SUCCESS;
  217. }
  218. /* Return to application */
  219. return (status);
  220. }
  221. /**
  222. @} end of MatrixMult group
  223. */