arm_mat_cmplx_mult_q15.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mat_mult_q15.c
  4. * Description: Q15 complex 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 Q15 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. @param[in] pScratch points to an array for storing intermediate results
  42. @return execution status
  43. - \ref ARM_MATH_SUCCESS : Operation successful
  44. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  45. @par Conditions for optimum performance
  46. Input, output and state buffers should be aligned by 32-bit
  47. @par Scaling and Overflow Behavior
  48. The function is implemented using an internal 64-bit accumulator. The inputs to the
  49. multiplications are in 1.15 format and multiplications yield a 2.30 result.
  50. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  51. This approach provides 33 guard bits and there is no risk of overflow. The 34.30 result is then
  52. truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format.
  53. */
  54. arm_status arm_mat_cmplx_mult_q15(
  55. const arm_matrix_instance_q15 * pSrcA,
  56. const arm_matrix_instance_q15 * pSrcB,
  57. arm_matrix_instance_q15 * pDst,
  58. q15_t * pScratch)
  59. {
  60. q15_t *pSrcBT = pScratch; /* input data matrix pointer for transpose */
  61. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
  62. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
  63. q15_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. uint16_t numRowsB = pSrcB->numRows; /* number of rows of input matrix A */
  68. q63_t sumReal, sumImag; /* accumulator */
  69. uint32_t col, i = 0U, row = numRowsB, colCnt; /* Loop counters */
  70. arm_status status; /* Status of matrix multiplication */
  71. #if defined (ARM_MATH_DSP)
  72. q31_t prod1, prod2;
  73. q31_t pSourceA, pSourceB;
  74. #else
  75. q15_t a, b, c, d;
  76. #endif /* #if defined (ARM_MATH_DSP) */
  77. #ifdef ARM_MATH_MATRIX_CHECK
  78. /* Check for matrix mismatch condition */
  79. if ((pSrcA->numCols != pSrcB->numRows) ||
  80. (pSrcA->numRows != pDst->numRows) ||
  81. (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. /* Matrix transpose */
  90. do
  91. {
  92. /* The pointer px is set to starting address of column being processed */
  93. px = pSrcBT + i;
  94. #if defined (ARM_MATH_LOOPUNROLL)
  95. /* Apply loop unrolling and exchange the columns with row elements */
  96. col = numColsB >> 2;
  97. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  98. a second loop below computes the remaining 1 to 3 samples. */
  99. while (col > 0U)
  100. {
  101. /* Read two elements from row */
  102. write_q15x2 (px, read_q15x2_ia (&pInB));
  103. /* Update pointer px to point to next row of transposed matrix */
  104. px += numRowsB * 2;
  105. /* Read two elements from row */
  106. write_q15x2 (px, read_q15x2_ia (&pInB));
  107. /* Update pointer px to point to next row of transposed matrix */
  108. px += numRowsB * 2;
  109. /* Read two elements from row */
  110. write_q15x2 (px, read_q15x2_ia (&pInB));
  111. /* Update pointer px to point to next row of transposed matrix */
  112. px += numRowsB * 2;
  113. /* Read two elements from row */
  114. write_q15x2 (px, read_q15x2_ia (&pInB));
  115. /* Update pointer px to point to next row of transposed matrix */
  116. px += numRowsB * 2;
  117. /* Decrement column loop counter */
  118. col--;
  119. }
  120. /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
  121. ** No loop unrolling is used. */
  122. col = numColsB % 0x4U;
  123. #else
  124. /* Initialize blkCnt with number of samples */
  125. col = numColsB;
  126. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  127. while (col > 0U)
  128. {
  129. /* Read two elements from row */
  130. write_q15x2 (px, read_q15x2_ia (&pInB));
  131. /* Update pointer px to point to next row of transposed matrix */
  132. px += numRowsB * 2;
  133. /* Decrement column loop counter */
  134. col--;
  135. }
  136. i = i + 2U;
  137. /* Decrement row loop counter */
  138. row--;
  139. } while (row > 0U);
  140. /* Reset variables for usage in following multiplication process */
  141. row = numRowsA;
  142. i = 0U;
  143. px = pDst->pData;
  144. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  145. /* row loop */
  146. do
  147. {
  148. /* For every row wise process, column loop counter is to be initiated */
  149. col = numColsB;
  150. /* For every row wise process, pIn2 pointer is set to starting address of transposed pSrcB data */
  151. pInB = pSrcBT;
  152. /* column loop */
  153. do
  154. {
  155. /* Set variable sum, that acts as accumulator, to zero */
  156. sumReal = 0;
  157. sumImag = 0;
  158. /* Initiate pointer pInA to point to starting address of column being processed */
  159. pInA = pSrcA->pData + i * 2;
  160. /* Apply loop unrolling and compute 2 MACs simultaneously. */
  161. colCnt = numColsA >> 1U;
  162. /* matrix multiplication */
  163. while (colCnt > 0U)
  164. {
  165. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  166. #if defined (ARM_MATH_DSP)
  167. /* read real and imag values from pSrcA and pSrcB buffer */
  168. pSourceA = read_q15x2_ia ((q15_t **) &pInA);
  169. pSourceB = read_q15x2_ia ((q15_t **) &pInB);
  170. /* Multiply and Accumlates */
  171. #ifdef ARM_MATH_BIG_ENDIAN
  172. prod1 = -__SMUSD(pSourceA, pSourceB);
  173. #else
  174. prod1 = __SMUSD(pSourceA, pSourceB);
  175. #endif
  176. prod2 = __SMUADX(pSourceA, pSourceB);
  177. sumReal += (q63_t) prod1;
  178. sumImag += (q63_t) prod2;
  179. /* read real and imag values from pSrcA and pSrcB buffer */
  180. pSourceA = read_q15x2_ia ((q15_t **) &pInA);
  181. pSourceB = read_q15x2_ia ((q15_t **) &pInB);
  182. /* Multiply and Accumlates */
  183. #ifdef ARM_MATH_BIG_ENDIAN
  184. prod1 = -__SMUSD(pSourceA, pSourceB);
  185. #else
  186. prod1 = __SMUSD(pSourceA, pSourceB);
  187. #endif
  188. prod2 = __SMUADX(pSourceA, pSourceB);
  189. sumReal += (q63_t) prod1;
  190. sumImag += (q63_t) prod2;
  191. #else /* #if defined (ARM_MATH_DSP) */
  192. /* read real and imag values from pSrcA buffer */
  193. a = *pInA;
  194. b = *(pInA + 1U);
  195. /* read real and imag values from pSrcB buffer */
  196. c = *pInB;
  197. d = *(pInB + 1U);
  198. /* Multiply and Accumlates */
  199. sumReal += (q31_t) a *c;
  200. sumImag += (q31_t) a *d;
  201. sumReal -= (q31_t) b *d;
  202. sumImag += (q31_t) b *c;
  203. /* read next real and imag values from pSrcA buffer */
  204. a = *(pInA + 2U);
  205. b = *(pInA + 3U);
  206. /* read next real and imag values from pSrcB buffer */
  207. c = *(pInB + 2U);
  208. d = *(pInB + 3U);
  209. /* update pointer */
  210. pInA += 4U;
  211. /* Multiply and Accumlates */
  212. sumReal += (q31_t) a * c;
  213. sumImag += (q31_t) a * d;
  214. sumReal -= (q31_t) b * d;
  215. sumImag += (q31_t) b * c;
  216. /* update pointer */
  217. pInB += 4U;
  218. #endif /* #if defined (ARM_MATH_DSP) */
  219. /* Decrement loop counter */
  220. colCnt--;
  221. }
  222. /* process odd column samples */
  223. if ((numColsA & 0x1U) > 0U)
  224. {
  225. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  226. #if defined (ARM_MATH_DSP)
  227. /* read real and imag values from pSrcA and pSrcB buffer */
  228. pSourceA = read_q15x2_ia ((q15_t **) &pInA);
  229. pSourceB = read_q15x2_ia ((q15_t **) &pInB);
  230. /* Multiply and Accumlates */
  231. #ifdef ARM_MATH_BIG_ENDIAN
  232. prod1 = -__SMUSD(pSourceA, pSourceB);
  233. #else
  234. prod1 = __SMUSD(pSourceA, pSourceB);
  235. #endif
  236. prod2 = __SMUADX(pSourceA, pSourceB);
  237. sumReal += (q63_t) prod1;
  238. sumImag += (q63_t) prod2;
  239. #else /* #if defined (ARM_MATH_DSP) */
  240. /* read real and imag values from pSrcA and pSrcB buffer */
  241. a = *pInA++;
  242. b = *pInA++;
  243. c = *pInB++;
  244. d = *pInB++;
  245. /* Multiply and Accumlates */
  246. sumReal += (q31_t) a * c;
  247. sumImag += (q31_t) a * d;
  248. sumReal -= (q31_t) b * d;
  249. sumImag += (q31_t) b * c;
  250. #endif /* #if defined (ARM_MATH_DSP) */
  251. }
  252. /* Saturate and store result in destination buffer */
  253. *px++ = (q15_t) (__SSAT(sumReal >> 15, 16));
  254. *px++ = (q15_t) (__SSAT(sumImag >> 15, 16));
  255. /* Decrement column loop counter */
  256. col--;
  257. } while (col > 0U);
  258. i = i + numColsA;
  259. /* Decrement row loop counter */
  260. row--;
  261. } while (row > 0U);
  262. /* Set status as ARM_MATH_SUCCESS */
  263. status = ARM_MATH_SUCCESS;
  264. }
  265. /* Return to application */
  266. return (status);
  267. }
  268. /**
  269. @} end of MatrixMult group
  270. */