arm_mat_cmplx_mult_f32.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_cmplx_mult_f32.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. @defgroup CmplxMatrixMult Complex Matrix Multiplication
  34. Complex Matrix multiplication is only defined if the number of columns of the
  35. first matrix equals the number of rows of the second matrix.
  36. Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
  37. in an <code>M x P</code> matrix.
  38. @par
  39. When matrix size checking is enabled, the functions check:
  40. - that the inner dimensions of <code>pSrcA</code> and <code>pSrcB</code> are equal;
  41. - that the size of the output 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 first input complex matrix structure
  50. @param[in] pSrcB points to second input complex matrix structure
  51. @param[out] pDst points to output complex matrix structure
  52. @return execution status
  53. - \ref ARM_MATH_SUCCESS : Operation successful
  54. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  55. */
  56. #if defined(ARM_MATH_NEON)
  57. arm_status arm_mat_cmplx_mult_f32(
  58. const arm_matrix_instance_f32 * pSrcA,
  59. const arm_matrix_instance_f32 * pSrcB,
  60. arm_matrix_instance_f32 * pDst)
  61. {
  62. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  63. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  64. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  65. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  66. float32_t *px; /* Temporary output data matrix pointer */
  67. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  68. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  69. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  70. float32_t sumReal1, sumImag1; /* accumulator */
  71. float32_t a0, b0, c0, d0;
  72. float32_t a1, a1B,b1, b1B, c1, d1;
  73. float32_t sumReal2, sumImag2; /* accumulator */
  74. float32x4x2_t a0V, a1V;
  75. float32x4_t accR0,accI0, accR1,accI1,tempR, tempI;
  76. float32x2_t accum = vdup_n_f32(0);
  77. float32_t *pIn1B = pSrcA->pData;
  78. uint16_t col, i = 0U, j, rowCnt, row = numRowsA, colCnt; /* loop counters */
  79. arm_status status; /* status of matrix multiplication */
  80. float32_t sumReal1B, sumImag1B;
  81. float32_t sumReal2B, sumImag2B;
  82. float32_t *pxB;
  83. #ifdef ARM_MATH_MATRIX_CHECK
  84. /* Check for matrix mismatch condition */
  85. if ((pSrcA->numCols != pSrcB->numRows) ||
  86. (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  87. {
  88. /* Set status as ARM_MATH_SIZE_MISMATCH */
  89. status = ARM_MATH_SIZE_MISMATCH;
  90. }
  91. else
  92. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  93. {
  94. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  95. rowCnt = row >> 1;
  96. /* Row loop */
  97. while (rowCnt > 0U)
  98. {
  99. /* Output pointer is set to starting address of the row being processed */
  100. px = pOut + 2 * i;
  101. pxB = px + 2 * numColsB;
  102. /* For every row wise process, the column loop counter is to be initiated */
  103. col = numColsB;
  104. /* For every row wise process, the pIn2 pointer is set
  105. ** to the starting address of the pSrcB data */
  106. pIn2 = pSrcB->pData;
  107. j = 0U;
  108. /* Column loop */
  109. while (col > 0U)
  110. {
  111. /* Set the variable sum, that acts as accumulator, to zero */
  112. sumReal1 = 0.0f;
  113. sumImag1 = 0.0f;
  114. sumReal1B = 0.0f;
  115. sumImag1B = 0.0f;
  116. sumReal2 = 0.0f;
  117. sumImag2 = 0.0f;
  118. sumReal2B = 0.0f;
  119. sumImag2B = 0.0f;
  120. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  121. pIn1 = pInA;
  122. pIn1B = pIn1 + 2*numColsA;
  123. accR0 = vdupq_n_f32(0.0);
  124. accI0 = vdupq_n_f32(0.0);
  125. accR1 = vdupq_n_f32(0.0);
  126. accI1 = vdupq_n_f32(0.0);
  127. /* Compute 4 MACs simultaneously. */
  128. colCnt = numColsA >> 2;
  129. /* Matrix multiplication */
  130. while (colCnt > 0U)
  131. {
  132. /* Reading real part of complex matrix A */
  133. a0V = vld2q_f32(pIn1); // load & separate real/imag pSrcA (de-interleave 2)
  134. a1V = vld2q_f32(pIn1B); // load & separate real/imag pSrcA (de-interleave 2)
  135. pIn1 += 8;
  136. pIn1B += 8;
  137. tempR[0] = *pIn2;
  138. tempI[0] = *(pIn2 + 1U);
  139. pIn2 += 2 * numColsB;
  140. tempR[1] = *pIn2;
  141. tempI[1] = *(pIn2 + 1U);
  142. pIn2 += 2 * numColsB;
  143. tempR[2] = *pIn2;
  144. tempI[2] = *(pIn2 + 1U);
  145. pIn2 += 2 * numColsB;
  146. tempR[3] = *pIn2;
  147. tempI[3] = *(pIn2 + 1U);
  148. pIn2 += 2 * numColsB;
  149. accR0 = vmlaq_f32(accR0,a0V.val[0],tempR);
  150. accR0 = vmlsq_f32(accR0,a0V.val[1],tempI);
  151. accI0 = vmlaq_f32(accI0,a0V.val[1],tempR);
  152. accI0 = vmlaq_f32(accI0,a0V.val[0],tempI);
  153. accR1 = vmlaq_f32(accR1,a1V.val[0],tempR);
  154. accR1 = vmlsq_f32(accR1,a1V.val[1],tempI);
  155. accI1 = vmlaq_f32(accI1,a1V.val[1],tempR);
  156. accI1 = vmlaq_f32(accI1,a1V.val[0],tempI);
  157. /* Decrement the loop count */
  158. colCnt--;
  159. }
  160. accum = vpadd_f32(vget_low_f32(accR0), vget_high_f32(accR0));
  161. sumReal1 += accum[0] + accum[1];
  162. accum = vpadd_f32(vget_low_f32(accI0), vget_high_f32(accI0));
  163. sumImag1 += accum[0] + accum[1];
  164. accum = vpadd_f32(vget_low_f32(accR1), vget_high_f32(accR1));
  165. sumReal1B += accum[0] + accum[1];
  166. accum = vpadd_f32(vget_low_f32(accI1), vget_high_f32(accI1));
  167. sumImag1B += accum[0] + accum[1];
  168. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  169. ** No loop unrolling is used. */
  170. colCnt = numColsA & 3;
  171. while (colCnt > 0U)
  172. {
  173. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  174. a1 = *pIn1;
  175. a1B = *pIn1B;
  176. c1 = *pIn2;
  177. b1 = *(pIn1 + 1U);
  178. b1B = *(pIn1B + 1U);
  179. d1 = *(pIn2 + 1U);
  180. sumReal1 += a1 * c1;
  181. sumImag1 += b1 * c1;
  182. sumReal1B += a1B * c1;
  183. sumImag1B += b1B * c1;
  184. pIn1 += 2U;
  185. pIn1B += 2U;
  186. pIn2 += 2 * numColsB;
  187. sumReal2 -= b1 * d1;
  188. sumImag2 += a1 * d1;
  189. sumReal2B -= b1B * d1;
  190. sumImag2B += a1B * d1;
  191. /* Decrement the loop counter */
  192. colCnt--;
  193. }
  194. sumReal1 += sumReal2;
  195. sumImag1 += sumImag2;
  196. sumReal1B += sumReal2B;
  197. sumImag1B += sumImag2B;
  198. /* Store the result in the destination buffer */
  199. *px++ = sumReal1;
  200. *px++ = sumImag1;
  201. *pxB++ = sumReal1B;
  202. *pxB++ = sumImag1B;
  203. /* Update the pointer pIn2 to point to the starting address of the next column */
  204. j++;
  205. pIn2 = pSrcB->pData + 2U * j;
  206. /* Decrement the column loop counter */
  207. col--;
  208. }
  209. /* Update the pointer pInA to point to the starting address of the next 2 row */
  210. i = i + 2*numColsB;
  211. pInA = pInA + 4 * numColsA;
  212. /* Decrement the row loop counter */
  213. rowCnt--;
  214. }
  215. rowCnt = row & 1;
  216. while (rowCnt > 0U)
  217. {
  218. /* Output pointer is set to starting address of the row being processed */
  219. px = pOut + 2 * i;
  220. /* For every row wise process, the column loop counter is to be initiated */
  221. col = numColsB;
  222. /* For every row wise process, the pIn2 pointer is set
  223. ** to the starting address of the pSrcB data */
  224. pIn2 = pSrcB->pData;
  225. j = 0U;
  226. /* Column loop */
  227. while (col > 0U)
  228. {
  229. /* Set the variable sum, that acts as accumulator, to zero */
  230. sumReal1 = 0.0f;
  231. sumImag1 = 0.0f;
  232. sumReal2 = 0.0f;
  233. sumImag2 = 0.0f;
  234. /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
  235. pIn1 = pInA;
  236. accR0 = vdupq_n_f32(0.0);
  237. accI0 = vdupq_n_f32(0.0);
  238. /* Compute 4 MACs simultaneously. */
  239. colCnt = numColsA >> 2;
  240. /* Matrix multiplication */
  241. while (colCnt > 0U)
  242. {
  243. /* Reading real part of complex matrix A */
  244. a0V = vld2q_f32(pIn1); // load & separate real/imag pSrcA (de-interleave 2)
  245. pIn1 += 8;
  246. tempR[0] = *pIn2;
  247. tempI[0] = *(pIn2 + 1U);
  248. pIn2 += 2 * numColsB;
  249. tempR[1] = *pIn2;
  250. tempI[1] = *(pIn2 + 1U);
  251. pIn2 += 2 * numColsB;
  252. tempR[2] = *pIn2;
  253. tempI[2] = *(pIn2 + 1U);
  254. pIn2 += 2 * numColsB;
  255. tempR[3] = *pIn2;
  256. tempI[3] = *(pIn2 + 1U);
  257. pIn2 += 2 * numColsB;
  258. accR0 = vmlaq_f32(accR0,a0V.val[0],tempR);
  259. accR0 = vmlsq_f32(accR0,a0V.val[1],tempI);
  260. accI0 = vmlaq_f32(accI0,a0V.val[1],tempR);
  261. accI0 = vmlaq_f32(accI0,a0V.val[0],tempI);
  262. /* Decrement the loop count */
  263. colCnt--;
  264. }
  265. accum = vpadd_f32(vget_low_f32(accR0), vget_high_f32(accR0));
  266. sumReal1 += accum[0] + accum[1];
  267. accum = vpadd_f32(vget_low_f32(accI0), vget_high_f32(accI0));
  268. sumImag1 += accum[0] + accum[1];
  269. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  270. ** No loop unrolling is used. */
  271. colCnt = numColsA & 3;
  272. while (colCnt > 0U)
  273. {
  274. /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */
  275. a1 = *pIn1;
  276. c1 = *pIn2;
  277. b1 = *(pIn1 + 1U);
  278. d1 = *(pIn2 + 1U);
  279. sumReal1 += a1 * c1;
  280. sumImag1 += b1 * c1;
  281. pIn1 += 2U;
  282. pIn2 += 2 * numColsB;
  283. sumReal2 -= b1 * d1;
  284. sumImag2 += a1 * d1;
  285. /* Decrement the loop counter */
  286. colCnt--;
  287. }
  288. sumReal1 += sumReal2;
  289. sumImag1 += sumImag2;
  290. /* Store the result in the destination buffer */
  291. *px++ = sumReal1;
  292. *px++ = sumImag1;
  293. /* Update the pointer pIn2 to point to the starting address of the next column */
  294. j++;
  295. pIn2 = pSrcB->pData + 2U * j;
  296. /* Decrement the column loop counter */
  297. col--;
  298. }
  299. /* Update the pointer pInA to point to the starting address of the next row */
  300. i = i + numColsB;
  301. pInA = pInA + 2 * numColsA;
  302. /* Decrement the row loop counter */
  303. rowCnt--;
  304. }
  305. /* Set status as ARM_MATH_SUCCESS */
  306. status = ARM_MATH_SUCCESS;
  307. }
  308. /* Return to application */
  309. return (status);
  310. }
  311. #else
  312. arm_status arm_mat_cmplx_mult_f32(
  313. const arm_matrix_instance_f32 * pSrcA,
  314. const arm_matrix_instance_f32 * pSrcB,
  315. arm_matrix_instance_f32 * pDst)
  316. {
  317. float32_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  318. float32_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  319. float32_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  320. float32_t *pOut = pDst->pData; /* Output data matrix pointer */
  321. float32_t *px; /* Temporary output data matrix pointer */
  322. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  323. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  324. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  325. float32_t sumReal, sumImag; /* Accumulator */
  326. float32_t a1, b1, c1, d1;
  327. uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  328. arm_status status; /* status of matrix multiplication */
  329. #if defined (ARM_MATH_LOOPUNROLL)
  330. float32_t a0, b0, c0, d0;
  331. #endif
  332. #ifdef ARM_MATH_MATRIX_CHECK
  333. /* Check for matrix mismatch condition */
  334. if ((pSrcA->numCols != pSrcB->numRows) ||
  335. (pSrcA->numRows != pDst->numRows) ||
  336. (pSrcB->numCols != pDst->numCols) )
  337. {
  338. /* Set status as ARM_MATH_SIZE_MISMATCH */
  339. status = ARM_MATH_SIZE_MISMATCH;
  340. }
  341. else
  342. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  343. {
  344. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  345. /* row loop */
  346. do
  347. {
  348. /* Output pointer is set to starting address of the row being processed */
  349. px = pOut + 2 * i;
  350. /* For every row wise process, the column loop counter is to be initiated */
  351. col = numColsB;
  352. /* For every row wise process, the pIn2 pointer is set
  353. ** to the starting address of the pSrcB data */
  354. pIn2 = pSrcB->pData;
  355. j = 0U;
  356. /* column loop */
  357. do
  358. {
  359. /* Set the variable sum, that acts as accumulator, to zero */
  360. sumReal = 0.0f;
  361. sumImag = 0.0f;
  362. /* Initiate pointer pIn1 to point to starting address of column being processed */
  363. pIn1 = pInA;
  364. #if defined (ARM_MATH_LOOPUNROLL)
  365. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  366. colCnt = numColsA >> 2U;
  367. /* matrix multiplication */
  368. while (colCnt > 0U)
  369. {
  370. /* Reading real part of complex matrix A */
  371. a0 = *pIn1;
  372. /* Reading real part of complex matrix B */
  373. c0 = *pIn2;
  374. /* Reading imaginary part of complex matrix A */
  375. b0 = *(pIn1 + 1U);
  376. /* Reading imaginary part of complex matrix B */
  377. d0 = *(pIn2 + 1U);
  378. /* Multiply and Accumlates */
  379. sumReal += a0 * c0;
  380. sumImag += b0 * c0;
  381. /* update pointers */
  382. pIn1 += 2U;
  383. pIn2 += 2 * numColsB;
  384. /* Multiply and Accumlates */
  385. sumReal -= b0 * d0;
  386. sumImag += a0 * d0;
  387. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  388. /* read real and imag values from pSrcA and pSrcB buffer */
  389. a1 = *(pIn1 );
  390. c1 = *(pIn2 );
  391. b1 = *(pIn1 + 1U);
  392. d1 = *(pIn2 + 1U);
  393. /* Multiply and Accumlates */
  394. sumReal += a1 * c1;
  395. sumImag += b1 * c1;
  396. /* update pointers */
  397. pIn1 += 2U;
  398. pIn2 += 2 * numColsB;
  399. /* Multiply and Accumlates */
  400. sumReal -= b1 * d1;
  401. sumImag += a1 * d1;
  402. a0 = *(pIn1 );
  403. c0 = *(pIn2 );
  404. b0 = *(pIn1 + 1U);
  405. d0 = *(pIn2 + 1U);
  406. /* Multiply and Accumlates */
  407. sumReal += a0 * c0;
  408. sumImag += b0 * c0;
  409. /* update pointers */
  410. pIn1 += 2U;
  411. pIn2 += 2 * numColsB;
  412. /* Multiply and Accumlates */
  413. sumReal -= b0 * d0;
  414. sumImag += a0 * d0;
  415. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  416. a1 = *(pIn1 );
  417. c1 = *(pIn2 );
  418. b1 = *(pIn1 + 1U);
  419. d1 = *(pIn2 + 1U);
  420. /* Multiply and Accumlates */
  421. sumReal += a1 * c1;
  422. sumImag += b1 * c1;
  423. /* update pointers */
  424. pIn1 += 2U;
  425. pIn2 += 2 * numColsB;
  426. /* Multiply and Accumlates */
  427. sumReal -= b1 * d1;
  428. sumImag += a1 * d1;
  429. /* Decrement loop count */
  430. colCnt--;
  431. }
  432. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  433. ** No loop unrolling is used. */
  434. colCnt = numColsA % 0x4U;
  435. #else
  436. /* Initialize blkCnt with number of samples */
  437. colCnt = numColsA;
  438. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  439. while (colCnt > 0U)
  440. {
  441. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  442. a1 = *(pIn1 );
  443. c1 = *(pIn2 );
  444. b1 = *(pIn1 + 1U);
  445. d1 = *(pIn2 + 1U);
  446. /* Multiply and Accumlates */
  447. sumReal += a1 * c1;
  448. sumImag += b1 * c1;
  449. /* update pointers */
  450. pIn1 += 2U;
  451. pIn2 += 2 * numColsB;
  452. /* Multiply and Accumlates */
  453. sumReal -= b1 * d1;
  454. sumImag += a1 * d1;
  455. /* Decrement loop counter */
  456. colCnt--;
  457. }
  458. /* Store result in destination buffer */
  459. *px++ = sumReal;
  460. *px++ = sumImag;
  461. /* Update pointer pIn2 to point to starting address of next column */
  462. j++;
  463. pIn2 = pSrcB->pData + 2U * j;
  464. /* Decrement column loop counter */
  465. col--;
  466. } while (col > 0U);
  467. /* Update pointer pInA to point to starting address of next row */
  468. i = i + numColsB;
  469. pInA = pInA + 2 * numColsA;
  470. /* Decrement row loop counter */
  471. row--;
  472. } while (row > 0U);
  473. /* Set status as ARM_MATH_SUCCESS */
  474. status = ARM_MATH_SUCCESS;
  475. }
  476. /* Return to application */
  477. return (status);
  478. }
  479. #endif /* #if defined(ARM_MATH_NEON) */
  480. /**
  481. @} end of MatrixMult group
  482. */