arm_mat_cmplx_mult_q31.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_cmplx_mult_q31.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. * @addtogroup CmplxMatrixMult
  34. * @{
  35. */
  36. /**
  37. * @brief Q31 Complex matrix multiplication
  38. * @param[in] *pSrcA points to the first input complex matrix structure
  39. * @param[in] *pSrcB points to the second input complex matrix structure
  40. * @param[out] *pDst points to output complex matrix structure
  41. * @return The function returns either
  42. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  43. *
  44. * @details
  45. * <b>Scaling and Overflow Behavior:</b>
  46. *
  47. * \par
  48. * The function is implemented using an internal 64-bit accumulator.
  49. * The accumulator has a 2.62 format and maintains full precision of the intermediate
  50. * multiplication results but provides only a single guard bit. There is no saturation
  51. * on intermediate additions. Thus, if the accumulator overflows it wraps around and
  52. * distorts the result. The input signals should be scaled down to avoid intermediate
  53. * overflows. The input is thus scaled down by log2(numColsA) bits
  54. * to avoid overflows, as a total of numColsA additions are performed internally.
  55. * The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  56. *
  57. *
  58. */
  59. arm_status arm_mat_cmplx_mult_q31(
  60. const arm_matrix_instance_q31 * pSrcA,
  61. const arm_matrix_instance_q31 * pSrcB,
  62. arm_matrix_instance_q31 * pDst)
  63. {
  64. q31_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  65. q31_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  66. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  67. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  68. q31_t *px; /* Temporary output data matrix pointer */
  69. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  70. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  71. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  72. q63_t sumReal1, sumImag1; /* accumulator */
  73. q31_t a0, b0, c0, d0;
  74. q31_t a1, b1, c1, d1;
  75. /* Run the below code for Cortex-M4 and Cortex-M3 */
  76. uint16_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  77. arm_status status; /* status of matrix multiplication */
  78. #ifdef ARM_MATH_MATRIX_CHECK
  79. /* Check for matrix mismatch condition */
  80. if ((pSrcA->numCols != pSrcB->numRows) ||
  81. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  82. {
  83. /* Set status as ARM_MATH_SIZE_MISMATCH */
  84. status = ARM_MATH_SIZE_MISMATCH;
  85. }
  86. else
  87. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  88. {
  89. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  90. /* row loop */
  91. do
  92. {
  93. /* Output pointer is set to starting address of the row being processed */
  94. px = pOut + 2 * i;
  95. /* For every row wise process, the column loop counter is to be initiated */
  96. col = numColsB;
  97. /* For every row wise process, the pIn2 pointer is set
  98. ** to the starting address of the pSrcB data */
  99. pIn2 = pSrcB->pData;
  100. j = 0U;
  101. /* column loop */
  102. do
  103. {
  104. /* Set the variable sum, that acts as accumulator, to zero */
  105. sumReal1 = 0.0;
  106. sumImag1 = 0.0;
  107. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  108. pIn1 = pInA;
  109. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  110. colCnt = numColsA >> 2;
  111. /* matrix multiplication */
  112. while (colCnt > 0U)
  113. {
  114. /* Reading real part of complex matrix A */
  115. a0 = *pIn1;
  116. /* Reading real part of complex matrix B */
  117. c0 = *pIn2;
  118. /* Reading imaginary part of complex matrix A */
  119. b0 = *(pIn1 + 1U);
  120. /* Reading imaginary part of complex matrix B */
  121. d0 = *(pIn2 + 1U);
  122. /* Multiply and Accumlates */
  123. sumReal1 += (q63_t) a0 *c0;
  124. sumImag1 += (q63_t) b0 *c0;
  125. /* update pointers */
  126. pIn1 += 2U;
  127. pIn2 += 2 * numColsB;
  128. /* Multiply and Accumlates */
  129. sumReal1 -= (q63_t) b0 *d0;
  130. sumImag1 += (q63_t) a0 *d0;
  131. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  132. /* read real and imag values from pSrcA and pSrcB buffer */
  133. a1 = *pIn1;
  134. c1 = *pIn2;
  135. b1 = *(pIn1 + 1U);
  136. d1 = *(pIn2 + 1U);
  137. /* Multiply and Accumlates */
  138. sumReal1 += (q63_t) a1 *c1;
  139. sumImag1 += (q63_t) b1 *c1;
  140. /* update pointers */
  141. pIn1 += 2U;
  142. pIn2 += 2 * numColsB;
  143. /* Multiply and Accumlates */
  144. sumReal1 -= (q63_t) b1 *d1;
  145. sumImag1 += (q63_t) a1 *d1;
  146. a0 = *pIn1;
  147. c0 = *pIn2;
  148. b0 = *(pIn1 + 1U);
  149. d0 = *(pIn2 + 1U);
  150. /* Multiply and Accumlates */
  151. sumReal1 += (q63_t) a0 *c0;
  152. sumImag1 += (q63_t) b0 *c0;
  153. /* update pointers */
  154. pIn1 += 2U;
  155. pIn2 += 2 * numColsB;
  156. /* Multiply and Accumlates */
  157. sumReal1 -= (q63_t) b0 *d0;
  158. sumImag1 += (q63_t) a0 *d0;
  159. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  160. a1 = *pIn1;
  161. c1 = *pIn2;
  162. b1 = *(pIn1 + 1U);
  163. d1 = *(pIn2 + 1U);
  164. /* Multiply and Accumlates */
  165. sumReal1 += (q63_t) a1 *c1;
  166. sumImag1 += (q63_t) b1 *c1;
  167. /* update pointers */
  168. pIn1 += 2U;
  169. pIn2 += 2 * numColsB;
  170. /* Multiply and Accumlates */
  171. sumReal1 -= (q63_t) b1 *d1;
  172. sumImag1 += (q63_t) a1 *d1;
  173. /* Decrement the loop count */
  174. colCnt--;
  175. }
  176. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  177. ** No loop unrolling is used. */
  178. colCnt = numColsA % 0x4U;
  179. while (colCnt > 0U)
  180. {
  181. /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  182. a1 = *pIn1;
  183. c1 = *pIn2;
  184. b1 = *(pIn1 + 1U);
  185. d1 = *(pIn2 + 1U);
  186. /* Multiply and Accumlates */
  187. sumReal1 += (q63_t) a1 *c1;
  188. sumImag1 += (q63_t) b1 *c1;
  189. /* update pointers */
  190. pIn1 += 2U;
  191. pIn2 += 2 * numColsB;
  192. /* Multiply and Accumlates */
  193. sumReal1 -= (q63_t) b1 *d1;
  194. sumImag1 += (q63_t) a1 *d1;
  195. /* Decrement the loop counter */
  196. colCnt--;
  197. }
  198. /* Store the result in the destination buffer */
  199. *px++ = (q31_t) clip_q63_to_q31(sumReal1 >> 31);
  200. *px++ = (q31_t) clip_q63_to_q31(sumImag1 >> 31);
  201. /* Update the pointer pIn2 to point to the starting address of the next column */
  202. j++;
  203. pIn2 = pSrcB->pData + 2U * j;
  204. /* Decrement the column loop counter */
  205. col--;
  206. } while (col > 0U);
  207. /* Update the pointer pInA to point to the starting address of the next row */
  208. i = i + numColsB;
  209. pInA = pInA + 2 * numColsA;
  210. /* Decrement the row loop counter */
  211. row--;
  212. } while (row > 0U);
  213. /* Set status as ARM_MATH_SUCCESS */
  214. status = ARM_MATH_SUCCESS;
  215. }
  216. /* Return to application */
  217. return (status);
  218. }
  219. /**
  220. * @} end of MatrixMult group
  221. */