arm_mat_cmplx_mult_f16.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_cmplx_mult_f16.c
  4. * Description: Floating-point matrix multiplication
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/matrix_functions_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupMatrix
  32. */
  33. /**
  34. @addtogroup CmplxMatrixMult
  35. @{
  36. */
  37. /**
  38. @brief Floating-point Complex matrix multiplication.
  39. @param[in] pSrcA points to first input complex matrix structure
  40. @param[in] pSrcB points to second input complex matrix structure
  41. @param[out] pDst points to output complex matrix structure
  42. @return execution status
  43. - \ref ARM_MATH_SUCCESS : Operation successful
  44. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  45. */
  46. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) && defined(__CMSIS_GCC_H)
  47. #pragma GCC warning "Scalar version of arm_mat_cmplx_mult_f16 built. Helium version has build issues with gcc."
  48. #endif
  49. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) && !defined(__CMSIS_GCC_H)
  50. #include "arm_helium_utils.h"
  51. #define DONTCARE 0 /* inactive lane content */
  52. __STATIC_FORCEINLINE arm_status arm_mat_cmplx_mult_f16_2x2_mve(
  53. const arm_matrix_instance_f16 * pSrcA,
  54. const arm_matrix_instance_f16 * pSrcB,
  55. arm_matrix_instance_f16 * pDst)
  56. {
  57. const uint16_t MATRIX_DIM = 2;
  58. float16_t const *pInB = pSrcB->pData; /* input data matrix pointer B */
  59. float16_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  60. float16_t *pOut = pDst->pData; /* output data matrix pointer */
  61. uint16x8_t vecColBOffs0,vecColAOffs0,vecColAOffs1;
  62. float16_t *pInA0 = pInA;
  63. f16x8_t acc0, acc1;
  64. f16x8_t vecB, vecA0, vecA1;
  65. f16x8_t vecTmp;
  66. uint16_t tmp;
  67. static const uint16_t offsetB0[8] = { 0, 1,
  68. MATRIX_DIM * CMPLX_DIM, MATRIX_DIM * CMPLX_DIM + 1,
  69. 2, 3,
  70. MATRIX_DIM * CMPLX_DIM + 2 , MATRIX_DIM * CMPLX_DIM + 3,
  71. };
  72. vecColBOffs0 = vldrhq_u16((uint16_t const *) offsetB0);
  73. tmp = 0;
  74. vecColAOffs0 = viwdupq_u16(tmp, 4, 1);
  75. tmp = (CMPLX_DIM * MATRIX_DIM);
  76. vecColAOffs1 = vecColAOffs0 + (uint16_t)(CMPLX_DIM * MATRIX_DIM);
  77. pInB = (float16_t const *)pSrcB->pData;
  78. vecA0 = vldrhq_gather_shifted_offset_f16(pInA0, vecColAOffs0);
  79. vecA1 = vldrhq_gather_shifted_offset_f16(pInA0, vecColAOffs1);
  80. vecB = vldrhq_gather_shifted_offset(pInB, vecColBOffs0);
  81. acc0 = vcmulq(vecA0, vecB);
  82. acc0 = vcmlaq_rot90(acc0, vecA0, vecB);
  83. acc1 = vcmulq(vecA1, vecB);
  84. acc1 = vcmlaq_rot90(acc1, vecA1, vecB);
  85. /*
  86. * Compute
  87. * re0+re1 | im0+im1 | re0+re1 | im0+im1
  88. * re2+re3 | im2+im3 | re2+re3 | im2+im3
  89. */
  90. vecTmp = (f16x8_t) vrev64q_s32((int32x4_t) acc0);
  91. vecTmp = vaddq(vecTmp, acc0);
  92. *(float32_t *)(&pOut[0 * CMPLX_DIM * MATRIX_DIM]) = ((f32x4_t)vecTmp)[0];
  93. *(float32_t *)(&pOut[0 * CMPLX_DIM * MATRIX_DIM + CMPLX_DIM]) = ((f32x4_t)vecTmp)[2];
  94. vecTmp = (f16x8_t) vrev64q_s32((int32x4_t) acc1);
  95. vecTmp = vaddq(vecTmp, acc1);
  96. *(float32_t *)(&pOut[1 * CMPLX_DIM * MATRIX_DIM]) = ((f32x4_t)vecTmp)[0];
  97. *(float32_t *)(&pOut[1 * CMPLX_DIM * MATRIX_DIM + CMPLX_DIM]) = ((f32x4_t)vecTmp)[2];
  98. /*
  99. * Return to application
  100. */
  101. return (ARM_MATH_SUCCESS);
  102. }
  103. __STATIC_FORCEINLINE arm_status arm_mat_cmplx_mult_f16_3x3_mve(
  104. const arm_matrix_instance_f16 * pSrcA,
  105. const arm_matrix_instance_f16 * pSrcB,
  106. arm_matrix_instance_f16 * pDst)
  107. {
  108. const uint16_t MATRIX_DIM = 3;
  109. float16_t const *pInB = pSrcB->pData; /* input data matrix pointer B */
  110. float16_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  111. float16_t *pOut = pDst->pData; /* output data matrix pointer */
  112. uint16x8_t vecColBOffs0;
  113. float16_t *pInA0 = pInA;
  114. float16_t *pInA1 = pInA0 + CMPLX_DIM * MATRIX_DIM;
  115. float16_t *pInA2 = pInA1 + CMPLX_DIM * MATRIX_DIM;
  116. f16x8_t acc0, acc1, acc2;
  117. f16x8_t vecB, vecA0, vecA1, vecA2;
  118. static const uint16_t offsetB0[8] = { 0, 1,
  119. MATRIX_DIM * CMPLX_DIM, MATRIX_DIM * CMPLX_DIM + 1,
  120. 2 * MATRIX_DIM * CMPLX_DIM, 2 * MATRIX_DIM * CMPLX_DIM + 1,
  121. DONTCARE, DONTCARE
  122. };
  123. /* enable predication to disable upper half complex vector element */
  124. mve_pred16_t p0 = vctp16q(MATRIX_DIM * CMPLX_DIM);
  125. vecColBOffs0 = vldrhq_u16((uint16_t const *) offsetB0);
  126. pInB = (float16_t const *)pSrcB->pData;
  127. vecA0 = vldrhq_f16(pInA0);
  128. vecA1 = vldrhq_f16(pInA1);
  129. vecA2 = vldrhq_f16(pInA2);
  130. vecB = vldrhq_gather_shifted_offset_z(pInB, vecColBOffs0, p0);
  131. acc0 = vcmulq(vecA0, vecB);
  132. acc0 = vcmlaq_rot90(acc0, vecA0, vecB);
  133. acc1 = vcmulq(vecA1, vecB);
  134. acc1 = vcmlaq_rot90(acc1, vecA1, vecB);
  135. acc2 = vcmulq(vecA2, vecB);
  136. acc2 = vcmlaq_rot90(acc2, vecA2, vecB);
  137. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  138. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  139. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  140. pOut += CMPLX_DIM;
  141. /*
  142. * move to next B column
  143. */
  144. pInB = pInB + CMPLX_DIM;
  145. vecB = vldrhq_gather_shifted_offset_z(pInB, vecColBOffs0, p0);
  146. acc0 = vcmulq(vecA0, vecB);
  147. acc0 = vcmlaq_rot90(acc0, vecA0, vecB);
  148. acc1 = vcmulq(vecA1, vecB);
  149. acc1 = vcmlaq_rot90(acc1, vecA1, vecB);
  150. acc2 = vcmulq(vecA2, vecB);
  151. acc2 = vcmlaq_rot90(acc2, vecA2, vecB);
  152. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  153. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  154. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  155. pOut += CMPLX_DIM;
  156. /*
  157. * move to next B column
  158. */
  159. pInB = pInB + CMPLX_DIM;
  160. vecB = vldrhq_gather_shifted_offset_z(pInB, vecColBOffs0, p0);
  161. acc0 = vcmulq(vecA0, vecB);
  162. acc0 = vcmlaq_rot90(acc0, vecA0, vecB);
  163. acc1 = vcmulq(vecA1, vecB);
  164. acc1 = vcmlaq_rot90(acc1, vecA1, vecB);
  165. acc2 = vcmulq(vecA2, vecB);
  166. acc2 = vcmlaq_rot90(acc2, vecA2, vecB);
  167. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  168. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  169. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  170. /*
  171. * Return to application
  172. */
  173. return (ARM_MATH_SUCCESS);
  174. }
  175. __STATIC_FORCEINLINE arm_status arm_mat_cmplx_mult_f16_4x4_mve(
  176. const arm_matrix_instance_f16 * pSrcA,
  177. const arm_matrix_instance_f16 * pSrcB,
  178. arm_matrix_instance_f16 * pDst)
  179. {
  180. const uint16_t MATRIX_DIM = 4;
  181. float16_t const *pInB = pSrcB->pData; /* input data matrix pointer B */
  182. float16_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  183. float16_t *pOut = pDst->pData; /* output data matrix pointer */
  184. uint16x8_t vecColBOffs0;
  185. float16_t *pInA0 = pInA;
  186. float16_t *pInA1 = pInA0 + CMPLX_DIM * MATRIX_DIM;
  187. float16_t *pInA2 = pInA1 + CMPLX_DIM * MATRIX_DIM;
  188. float16_t *pInA3 = pInA2 + CMPLX_DIM * MATRIX_DIM;
  189. f16x8_t acc0, acc1, acc2, acc3;
  190. f16x8_t vecB, vecA;
  191. static const uint16_t offsetB0[8] = { 0, 1,
  192. MATRIX_DIM * CMPLX_DIM, MATRIX_DIM * CMPLX_DIM + 1,
  193. 2 * MATRIX_DIM * CMPLX_DIM, 2 * MATRIX_DIM * CMPLX_DIM + 1,
  194. 3 * MATRIX_DIM * CMPLX_DIM, 3 * MATRIX_DIM * CMPLX_DIM + 1
  195. };
  196. vecColBOffs0 = vldrhq_u16((uint16_t const *) offsetB0);
  197. pInB = (float16_t const *)pSrcB->pData;
  198. vecB = vldrhq_gather_shifted_offset(pInB, vecColBOffs0);
  199. vecA = vldrhq_f16(pInA0);
  200. acc0 = vcmulq(vecA, vecB);
  201. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  202. vecA = vldrhq_f16(pInA1);
  203. acc1 = vcmulq(vecA, vecB);
  204. acc1 = vcmlaq_rot90(acc1, vecA, vecB);
  205. vecA = vldrhq_f16(pInA2);
  206. acc2 = vcmulq(vecA, vecB);
  207. acc2 = vcmlaq_rot90(acc2, vecA, vecB);
  208. vecA = vldrhq_f16(pInA3);
  209. acc3 = vcmulq(vecA, vecB);
  210. acc3 = vcmlaq_rot90(acc3, vecA, vecB);
  211. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  212. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  213. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  214. mve_cmplx_sum_intra_vec_f16(acc3, &pOut[3 * CMPLX_DIM * MATRIX_DIM]);
  215. pOut += CMPLX_DIM;
  216. /*
  217. * move to next B column
  218. */
  219. pInB = pInB + CMPLX_DIM;
  220. vecB = vldrhq_gather_shifted_offset(pInB, vecColBOffs0);
  221. vecA = vldrhq_f16(pInA0);
  222. acc0 = vcmulq(vecA, vecB);
  223. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  224. vecA = vldrhq_f16(pInA1);
  225. acc1 = vcmulq(vecA, vecB);
  226. acc1 = vcmlaq_rot90(acc1, vecA, vecB);
  227. vecA = vldrhq_f16(pInA2);
  228. acc2 = vcmulq(vecA, vecB);
  229. acc2 = vcmlaq_rot90(acc2, vecA, vecB);
  230. vecA = vldrhq_f16(pInA3);
  231. acc3 = vcmulq(vecA, vecB);
  232. acc3 = vcmlaq_rot90(acc3, vecA, vecB);
  233. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  234. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  235. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  236. mve_cmplx_sum_intra_vec_f16(acc3, &pOut[3 * CMPLX_DIM * MATRIX_DIM]);
  237. pOut += CMPLX_DIM;
  238. /*
  239. * move to next B column
  240. */
  241. pInB = pInB + CMPLX_DIM;
  242. vecB = vldrhq_gather_shifted_offset(pInB, vecColBOffs0);
  243. vecA = vldrhq_f16(pInA0);
  244. acc0 = vcmulq(vecA, vecB);
  245. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  246. vecA = vldrhq_f16(pInA1);
  247. acc1 = vcmulq(vecA, vecB);
  248. acc1 = vcmlaq_rot90(acc1, vecA, vecB);
  249. vecA = vldrhq_f16(pInA2);
  250. acc2 = vcmulq(vecA, vecB);
  251. acc2 = vcmlaq_rot90(acc2, vecA, vecB);
  252. vecA = vldrhq_f16(pInA3);
  253. acc3 = vcmulq(vecA, vecB);
  254. acc3 = vcmlaq_rot90(acc3, vecA, vecB);
  255. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  256. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  257. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  258. mve_cmplx_sum_intra_vec_f16(acc3, &pOut[3 * CMPLX_DIM * MATRIX_DIM]);
  259. pOut += CMPLX_DIM;
  260. /*
  261. * move to next B column
  262. */
  263. pInB = pInB + CMPLX_DIM;
  264. vecB = vldrhq_gather_shifted_offset(pInB, vecColBOffs0);
  265. vecA = vldrhq_f16(pInA0);
  266. acc0 = vcmulq(vecA, vecB);
  267. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  268. vecA = vldrhq_f16(pInA1);
  269. acc1 = vcmulq(vecA, vecB);
  270. acc1 = vcmlaq_rot90(acc1, vecA, vecB);
  271. vecA = vldrhq_f16(pInA2);
  272. acc2 = vcmulq(vecA, vecB);
  273. acc2 = vcmlaq_rot90(acc2, vecA, vecB);
  274. vecA = vldrhq_f16(pInA3);
  275. acc3 = vcmulq(vecA, vecB);
  276. acc3 = vcmlaq_rot90(acc3, vecA, vecB);
  277. mve_cmplx_sum_intra_vec_f16(acc0, &pOut[0 * CMPLX_DIM * MATRIX_DIM]);
  278. mve_cmplx_sum_intra_vec_f16(acc1, &pOut[1 * CMPLX_DIM * MATRIX_DIM]);
  279. mve_cmplx_sum_intra_vec_f16(acc2, &pOut[2 * CMPLX_DIM * MATRIX_DIM]);
  280. mve_cmplx_sum_intra_vec_f16(acc3, &pOut[3 * CMPLX_DIM * MATRIX_DIM]);
  281. /*
  282. * Return to application
  283. */
  284. return (ARM_MATH_SUCCESS);
  285. }
  286. arm_status arm_mat_cmplx_mult_f16(
  287. const arm_matrix_instance_f16 * pSrcA,
  288. const arm_matrix_instance_f16 * pSrcB,
  289. arm_matrix_instance_f16 * pDst)
  290. {
  291. float16_t const *pInB = (float16_t const *) pSrcB->pData; /* input data matrix pointer B */
  292. float16_t const *pInA = (float16_t const *) pSrcA->pData; /* input data matrix pointer A */
  293. float16_t *pOut = pDst->pData; /* output data matrix pointer */
  294. float16_t *px; /* Temporary output data matrix pointer */
  295. uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
  296. uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
  297. uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
  298. uint16_t col, i = 0U, row = numRowsA; /* loop counters */
  299. arm_status status; /* status of matrix multiplication */
  300. uint16x8_t vecOffs, vecColBOffs;
  301. uint32_t blkCnt,rowCnt; /* loop counters */
  302. #ifdef ARM_MATH_MATRIX_CHECK
  303. /* Check for matrix mismatch condition */
  304. if ((pSrcA->numCols != pSrcB->numRows) ||
  305. (pSrcA->numRows != pDst->numRows) ||
  306. (pSrcB->numCols != pDst->numCols) )
  307. {
  308. /* Set status as ARM_MATH_SIZE_MISMATCH */
  309. status = ARM_MATH_SIZE_MISMATCH;
  310. }
  311. else
  312. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  313. {
  314. /*
  315. * small squared matrix specialized routines
  316. */
  317. if (numRowsA == numColsB && numColsB == numColsA)
  318. {
  319. if (numRowsA == 1)
  320. {
  321. pOut[0] = pInA[0] * pInB[0] - pInA[1] * pInB[1];
  322. pOut[1] = pInA[0] * pInB[1] + pInA[1] * pInB[0];
  323. return (ARM_MATH_SUCCESS);
  324. }
  325. else if (numRowsA == 2)
  326. return arm_mat_cmplx_mult_f16_2x2_mve(pSrcA, pSrcB, pDst);
  327. else if (numRowsA == 3)
  328. return arm_mat_cmplx_mult_f16_3x3_mve(pSrcA, pSrcB, pDst);
  329. else if (numRowsA == 4)
  330. return arm_mat_cmplx_mult_f16_4x4_mve(pSrcA, pSrcB, pDst);
  331. }
  332. vecColBOffs[0] = 0;
  333. vecColBOffs[1] = 1;
  334. vecColBOffs[2] = numColsB * CMPLX_DIM;
  335. vecColBOffs[3] = (numColsB * CMPLX_DIM) + 1;
  336. vecColBOffs[4] = 2*numColsB * CMPLX_DIM;
  337. vecColBOffs[5] = 2*(numColsB * CMPLX_DIM) + 1;
  338. vecColBOffs[6] = 3*numColsB * CMPLX_DIM;
  339. vecColBOffs[7] = 3*(numColsB * CMPLX_DIM) + 1;
  340. /*
  341. * The following loop performs the dot-product of each row in pSrcA with each column in pSrcB
  342. */
  343. /*
  344. * row loop
  345. */
  346. rowCnt = row >> 2;
  347. while (rowCnt > 0u)
  348. {
  349. /*
  350. * Output pointer is set to starting address of the row being processed
  351. */
  352. px = pOut + i * CMPLX_DIM;
  353. i = i + 4 * numColsB;
  354. /*
  355. * For every row wise process, the column loop counter is to be initiated
  356. */
  357. col = numColsB;
  358. /*
  359. * For every row wise process, the pInB pointer is set
  360. * to the starting address of the pSrcB data
  361. */
  362. pInB = (float16_t const *) pSrcB->pData;
  363. /*
  364. * column loop
  365. */
  366. while (col > 0u)
  367. {
  368. /*
  369. * generate 4 columns elements
  370. */
  371. /*
  372. * Matrix A columns number of MAC operations are to be performed
  373. */
  374. float16_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec;
  375. float16_t const *pInA0 = pInA;
  376. float16_t const *pInA1 = pInA0 + numColsA * CMPLX_DIM;
  377. float16_t const *pInA2 = pInA1 + numColsA * CMPLX_DIM;
  378. float16_t const *pInA3 = pInA2 + numColsA * CMPLX_DIM;
  379. f16x8_t acc0, acc1, acc2, acc3;
  380. acc0 = vdupq_n_f16(0.0f16);
  381. acc1 = vdupq_n_f16(0.0f16);
  382. acc2 = vdupq_n_f16(0.0f16);
  383. acc3 = vdupq_n_f16(0.0f16);
  384. pSrcA0Vec = (float16_t const *) pInA0;
  385. pSrcA1Vec = (float16_t const *) pInA1;
  386. pSrcA2Vec = (float16_t const *) pInA2;
  387. pSrcA3Vec = (float16_t const *) pInA3;
  388. vecOffs = vecColBOffs;
  389. /*
  390. * process 1 x 4 block output
  391. */
  392. blkCnt = (numColsA * CMPLX_DIM) >> 3;
  393. while (blkCnt > 0U)
  394. {
  395. f16x8_t vecB, vecA;
  396. vecB = vldrhq_gather_shifted_offset_f16(pInB, vecOffs);
  397. /*
  398. * move Matrix B read offsets, 4 rows down
  399. */
  400. vecOffs = vaddq_n_u16(vecOffs , (uint16_t) (numColsB * 4 * CMPLX_DIM));
  401. vecA = vld1q(pSrcA0Vec); pSrcA0Vec += 8;
  402. acc0 = vcmlaq(acc0, vecA, vecB);
  403. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  404. vecA = vld1q(pSrcA1Vec); pSrcA1Vec += 8;
  405. acc1 = vcmlaq(acc1, vecA, vecB);
  406. acc1 = vcmlaq_rot90(acc1, vecA, vecB);
  407. vecA = vld1q(pSrcA2Vec); pSrcA2Vec += 8;
  408. acc2 = vcmlaq(acc2, vecA, vecB);
  409. acc2 = vcmlaq_rot90(acc2, vecA, vecB);
  410. vecA = vld1q(pSrcA3Vec); pSrcA3Vec += 8;
  411. acc3 = vcmlaq(acc3, vecA, vecB);
  412. acc3 = vcmlaq_rot90(acc3, vecA, vecB);
  413. blkCnt--;
  414. }
  415. /*
  416. * Unsupported addressing mode compiler crash
  417. */
  418. /*
  419. * tail
  420. * (will be merged thru tail predication)
  421. */
  422. blkCnt = (numColsA * CMPLX_DIM) & 7;
  423. if (blkCnt > 0U)
  424. {
  425. mve_pred16_t p0 = vctp16q(blkCnt);
  426. f16x8_t vecB, vecA;
  427. vecB = vldrhq_gather_shifted_offset_z_f16(pInB, vecOffs, p0);
  428. /*
  429. * move Matrix B read offsets, 4 rows down
  430. */
  431. vecOffs = vaddq_n_u16(vecOffs, (uint16_t) (numColsB * 4 * CMPLX_DIM));
  432. vecA = vld1q(pSrcA0Vec);
  433. acc0 = vcmlaq(acc0, vecA, vecB);
  434. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  435. vecA = vld1q(pSrcA1Vec);
  436. acc1 = vcmlaq(acc1, vecA, vecB);
  437. acc1 = vcmlaq_rot90(acc1, vecA, vecB);
  438. vecA = vld1q(pSrcA2Vec);
  439. acc2 = vcmlaq(acc2, vecA, vecB);
  440. acc2 = vcmlaq_rot90(acc2, vecA, vecB);
  441. vecA = vld1q(pSrcA3Vec);
  442. acc3 = vcmlaq(acc3, vecA, vecB);
  443. acc3 = vcmlaq_rot90(acc3, vecA, vecB);
  444. }
  445. mve_cmplx_sum_intra_vec_f16(acc0, &px[0 * CMPLX_DIM * numColsB + 0]);
  446. mve_cmplx_sum_intra_vec_f16(acc1, &px[1 * CMPLX_DIM * numColsB + 0]);
  447. mve_cmplx_sum_intra_vec_f16(acc2, &px[2 * CMPLX_DIM * numColsB + 0]);
  448. mve_cmplx_sum_intra_vec_f16(acc3, &px[3 * CMPLX_DIM * numColsB + 0]);
  449. px += CMPLX_DIM;
  450. /*
  451. * Decrement the column loop counter
  452. */
  453. col--;
  454. /*
  455. * Update the pointer pInB to point to the starting address of the next column
  456. */
  457. pInB = (float16_t const *) pSrcB->pData + (numColsB - col) * CMPLX_DIM;
  458. }
  459. /*
  460. * Update the pointer pInA to point to the starting address of the next row
  461. */
  462. pInA += (numColsA * 4) * CMPLX_DIM;
  463. /*
  464. * Decrement the row loop counter
  465. */
  466. rowCnt --;
  467. }
  468. rowCnt = row & 3;
  469. while (rowCnt > 0u)
  470. {
  471. /*
  472. * Output pointer is set to starting address of the row being processed
  473. */
  474. px = pOut + i * CMPLX_DIM;
  475. i = i + numColsB;
  476. /*
  477. * For every row wise process, the column loop counter is to be initiated
  478. */
  479. col = numColsB;
  480. /*
  481. * For every row wise process, the pInB pointer is set
  482. * to the starting address of the pSrcB data
  483. */
  484. pInB = (float16_t const *) pSrcB->pData;
  485. /*
  486. * column loop
  487. */
  488. while (col > 0u)
  489. {
  490. /*
  491. * generate 4 columns elements
  492. */
  493. /*
  494. * Matrix A columns number of MAC operations are to be performed
  495. */
  496. float16_t const *pSrcA0Vec;
  497. float16_t const *pInA0 = pInA;
  498. f16x8_t acc0;
  499. acc0 = vdupq_n_f16(0.0f16);
  500. pSrcA0Vec = (float16_t const *) pInA0;
  501. vecOffs = vecColBOffs;
  502. /*
  503. * process 1 x 4 block output
  504. */
  505. blkCnt = (numColsA * CMPLX_DIM) >> 3;
  506. while (blkCnt > 0U)
  507. {
  508. f16x8_t vecB, vecA;
  509. vecB = vldrhq_gather_shifted_offset(pInB, vecOffs);
  510. /*
  511. * move Matrix B read offsets, 4 rows down
  512. */
  513. vecOffs = vaddq_n_u16(vecOffs, (uint16_t) (4*numColsB * CMPLX_DIM));
  514. vecA = vld1q(pSrcA0Vec);
  515. pSrcA0Vec += 8;
  516. acc0 = vcmlaq(acc0, vecA, vecB);
  517. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  518. blkCnt--;
  519. }
  520. /*
  521. * tail
  522. */
  523. blkCnt = (numColsA * CMPLX_DIM) & 7;
  524. if (blkCnt > 0U)
  525. {
  526. mve_pred16_t p0 = vctp16q(blkCnt);
  527. f16x8_t vecB, vecA;
  528. vecB = vldrhq_gather_shifted_offset_z(pInB, vecOffs, p0);
  529. vecA = vld1q(pSrcA0Vec);
  530. acc0 = vcmlaq(acc0, vecA, vecB);
  531. acc0 = vcmlaq_rot90(acc0, vecA, vecB);
  532. }
  533. mve_cmplx_sum_intra_vec_f16(acc0, &px[0]);
  534. px += CMPLX_DIM;
  535. /*
  536. * Decrement the column loop counter
  537. */
  538. col--;
  539. /*
  540. * Update the pointer pInB to point to the starting address of the next column
  541. */
  542. pInB = (float16_t const *) pSrcB->pData + (numColsB - col) * CMPLX_DIM;
  543. }
  544. /*
  545. * Update the pointer pInA to point to the starting address of the next row
  546. */
  547. pInA += numColsA * CMPLX_DIM;
  548. rowCnt--;
  549. }
  550. /*
  551. * set status as ARM_MATH_SUCCESS
  552. */
  553. status = ARM_MATH_SUCCESS;
  554. }
  555. /*
  556. * Return to application
  557. */
  558. return (status);
  559. }
  560. #else
  561. arm_status arm_mat_cmplx_mult_f16(
  562. const arm_matrix_instance_f16 * pSrcA,
  563. const arm_matrix_instance_f16 * pSrcB,
  564. arm_matrix_instance_f16 * pDst)
  565. {
  566. float16_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */
  567. float16_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */
  568. float16_t *pInA = pSrcA->pData; /* Input data matrix pointer A */
  569. float16_t *pOut = pDst->pData; /* Output data matrix pointer */
  570. float16_t *px; /* Temporary output data matrix pointer */
  571. uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */
  572. uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */
  573. uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */
  574. _Float16 sumReal, sumImag; /* Accumulator */
  575. _Float16 a1, b1, c1, d1;
  576. uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
  577. arm_status status; /* status of matrix multiplication */
  578. #if defined (ARM_MATH_LOOPUNROLL)
  579. _Float16 a0, b0, c0, d0;
  580. #endif
  581. #ifdef ARM_MATH_MATRIX_CHECK
  582. /* Check for matrix mismatch condition */
  583. if ((pSrcA->numCols != pSrcB->numRows) ||
  584. (pSrcA->numRows != pDst->numRows) ||
  585. (pSrcB->numCols != pDst->numCols) )
  586. {
  587. /* Set status as ARM_MATH_SIZE_MISMATCH */
  588. status = ARM_MATH_SIZE_MISMATCH;
  589. }
  590. else
  591. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  592. {
  593. /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  594. /* row loop */
  595. do
  596. {
  597. /* Output pointer is set to starting address of the row being processed */
  598. px = pOut + 2 * i;
  599. /* For every row wise process, the column loop counter is to be initiated */
  600. col = numColsB;
  601. /* For every row wise process, the pIn2 pointer is set
  602. ** to the starting address of the pSrcB data */
  603. pIn2 = pSrcB->pData;
  604. j = 0U;
  605. /* column loop */
  606. do
  607. {
  608. /* Set the variable sum, that acts as accumulator, to zero */
  609. sumReal = 0.0f16;
  610. sumImag = 0.0f16;
  611. /* Initiate pointer pIn1 to point to starting address of column being processed */
  612. pIn1 = pInA;
  613. #if defined (ARM_MATH_LOOPUNROLL)
  614. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  615. colCnt = numColsA >> 2U;
  616. /* matrix multiplication */
  617. while (colCnt > 0U)
  618. {
  619. /* Reading real part of complex matrix A */
  620. a0 = *pIn1;
  621. /* Reading real part of complex matrix B */
  622. c0 = *pIn2;
  623. /* Reading imaginary part of complex matrix A */
  624. b0 = *(pIn1 + 1U);
  625. /* Reading imaginary part of complex matrix B */
  626. d0 = *(pIn2 + 1U);
  627. /* Multiply and Accumlates */
  628. sumReal += a0 * c0;
  629. sumImag += b0 * c0;
  630. /* update pointers */
  631. pIn1 += 2U;
  632. pIn2 += 2 * numColsB;
  633. /* Multiply and Accumlates */
  634. sumReal -= b0 * d0;
  635. sumImag += a0 * d0;
  636. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  637. /* read real and imag values from pSrcA and pSrcB buffer */
  638. a1 = *(pIn1 );
  639. c1 = *(pIn2 );
  640. b1 = *(pIn1 + 1U);
  641. d1 = *(pIn2 + 1U);
  642. /* Multiply and Accumlates */
  643. sumReal += a1 * c1;
  644. sumImag += b1 * c1;
  645. /* update pointers */
  646. pIn1 += 2U;
  647. pIn2 += 2 * numColsB;
  648. /* Multiply and Accumlates */
  649. sumReal -= b1 * d1;
  650. sumImag += a1 * d1;
  651. a0 = *(pIn1 );
  652. c0 = *(pIn2 );
  653. b0 = *(pIn1 + 1U);
  654. d0 = *(pIn2 + 1U);
  655. /* Multiply and Accumlates */
  656. sumReal += a0 * c0;
  657. sumImag += b0 * c0;
  658. /* update pointers */
  659. pIn1 += 2U;
  660. pIn2 += 2 * numColsB;
  661. /* Multiply and Accumlates */
  662. sumReal -= b0 * d0;
  663. sumImag += a0 * d0;
  664. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  665. a1 = *(pIn1 );
  666. c1 = *(pIn2 );
  667. b1 = *(pIn1 + 1U);
  668. d1 = *(pIn2 + 1U);
  669. /* Multiply and Accumlates */
  670. sumReal += a1 * c1;
  671. sumImag += b1 * c1;
  672. /* update pointers */
  673. pIn1 += 2U;
  674. pIn2 += 2 * numColsB;
  675. /* Multiply and Accumlates */
  676. sumReal -= b1 * d1;
  677. sumImag += a1 * d1;
  678. /* Decrement loop count */
  679. colCnt--;
  680. }
  681. /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
  682. ** No loop unrolling is used. */
  683. colCnt = numColsA % 0x4U;
  684. #else
  685. /* Initialize blkCnt with number of samples */
  686. colCnt = numColsA;
  687. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  688. while (colCnt > 0U)
  689. {
  690. /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */
  691. a1 = *(pIn1 );
  692. c1 = *(pIn2 );
  693. b1 = *(pIn1 + 1U);
  694. d1 = *(pIn2 + 1U);
  695. /* Multiply and Accumlates */
  696. sumReal += a1 * c1;
  697. sumImag += b1 * c1;
  698. /* update pointers */
  699. pIn1 += 2U;
  700. pIn2 += 2 * numColsB;
  701. /* Multiply and Accumlates */
  702. sumReal -= b1 * d1;
  703. sumImag += a1 * d1;
  704. /* Decrement loop counter */
  705. colCnt--;
  706. }
  707. /* Store result in destination buffer */
  708. *px++ = sumReal;
  709. *px++ = sumImag;
  710. /* Update pointer pIn2 to point to starting address of next column */
  711. j++;
  712. pIn2 = pSrcB->pData + 2U * j;
  713. /* Decrement column loop counter */
  714. col--;
  715. } while (col > 0U);
  716. /* Update pointer pInA to point to starting address of next row */
  717. i = i + numColsB;
  718. pInA = pInA + 2 * numColsA;
  719. /* Decrement row loop counter */
  720. row--;
  721. } while (row > 0U);
  722. /* Set status as ARM_MATH_SUCCESS */
  723. status = ARM_MATH_SUCCESS;
  724. }
  725. /* Return to application */
  726. return (status);
  727. }
  728. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  729. /**
  730. @} end of MatrixMult group
  731. */
  732. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */