arm_mat_trans_f32.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_trans_f32.c
  4. * Description: Floating-point matrix transpose
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  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. @defgroup MatrixTrans Matrix Transpose
  34. Tranposes a matrix.
  35. Transposing an <code>M x N</code> matrix flips it around the center diagonal and results in an <code>N x M</code> matrix.
  36. \image html MatrixTranspose.gif "Transpose of a 3 x 3 matrix"
  37. */
  38. /**
  39. @addtogroup MatrixTrans
  40. @{
  41. */
  42. /**
  43. @brief Floating-point matrix transpose.
  44. @param[in] pSrc points to input matrix
  45. @param[out] pDst points to output matrix
  46. @return execution status
  47. - \ref ARM_MATH_SUCCESS : Operation successful
  48. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  49. */
  50. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  51. #include "arm_helium_utils.h"
  52. arm_status arm_mat_trans_f32(
  53. const arm_matrix_instance_f32 * pSrc,
  54. arm_matrix_instance_f32 * pDst)
  55. {
  56. arm_status status; /* status of matrix transpose */
  57. #ifdef ARM_MATH_MATRIX_CHECK
  58. /* Check for matrix mismatch condition */
  59. if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  60. {
  61. /* Set status as ARM_MATH_SIZE_MISMATCH */
  62. status = ARM_MATH_SIZE_MISMATCH;
  63. }
  64. else
  65. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  66. {
  67. if (pDst->numRows == pDst->numCols)
  68. {
  69. if (pDst->numCols == 2)
  70. return arm_mat_trans_32bit_2x2_mve((uint32_t *)pSrc->pData, (uint32_t *)pDst->pData);
  71. if (pDst->numCols == 3)
  72. return arm_mat_trans_32bit_3x3_mve((uint32_t *)pSrc->pData, (uint32_t *)pDst->pData);
  73. if (pDst->numCols == 4)
  74. return arm_mat_trans_32bit_4x4_mve((uint32_t *)pSrc->pData, (uint32_t *)pDst->pData);
  75. }
  76. arm_mat_trans_32bit_generic_mve(pSrc->numRows, pSrc->numCols, (uint32_t *)pSrc->pData, (uint32_t *)pDst->pData);
  77. /* Set status as ARM_MATH_SUCCESS */
  78. status = ARM_MATH_SUCCESS;
  79. }
  80. /* Return to application */
  81. return (status);
  82. }
  83. #else
  84. #if defined(ARM_MATH_NEON)
  85. arm_status arm_mat_trans_f32(
  86. const arm_matrix_instance_f32 * pSrc,
  87. arm_matrix_instance_f32 * pDst)
  88. {
  89. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  90. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  91. float32_t *px; /* Temporary output data matrix pointer */
  92. uint16_t nRows = pSrc->numRows; /* number of rows */
  93. uint16_t nColumns = pSrc->numCols; /* number of columns */
  94. uint16_t blkCnt, rowCnt, i = 0U, row = nRows; /* loop counters */
  95. arm_status status; /* status of matrix transpose */
  96. #ifdef ARM_MATH_MATRIX_CHECK
  97. /* Check for matrix mismatch condition */
  98. if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  99. {
  100. /* Set status as ARM_MATH_SIZE_MISMATCH */
  101. status = ARM_MATH_SIZE_MISMATCH;
  102. }
  103. else
  104. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  105. {
  106. /* Matrix transpose by exchanging the rows with columns */
  107. /* Row loop */
  108. rowCnt = row >> 2;
  109. while (rowCnt > 0U)
  110. {
  111. float32x4_t row0V,row1V,row2V,row3V;
  112. float32x4x2_t ra0,ra1,rb0,rb1;
  113. blkCnt = nColumns >> 2;
  114. /* The pointer px is set to starting address of the column being processed */
  115. px = pOut + i;
  116. /* Compute 4 outputs at a time.
  117. ** a second loop below computes the remaining 1 to 3 samples. */
  118. while (blkCnt > 0U) /* Column loop */
  119. {
  120. row0V = vld1q_f32(pIn);
  121. row1V = vld1q_f32(pIn + 1 * nColumns);
  122. row2V = vld1q_f32(pIn + 2 * nColumns);
  123. row3V = vld1q_f32(pIn + 3 * nColumns);
  124. pIn += 4;
  125. ra0 = vzipq_f32(row0V,row2V);
  126. ra1 = vzipq_f32(row1V,row3V);
  127. rb0 = vzipq_f32(ra0.val[0],ra1.val[0]);
  128. rb1 = vzipq_f32(ra0.val[1],ra1.val[1]);
  129. vst1q_f32(px,rb0.val[0]);
  130. px += nRows;
  131. vst1q_f32(px,rb0.val[1]);
  132. px += nRows;
  133. vst1q_f32(px,rb1.val[0]);
  134. px += nRows;
  135. vst1q_f32(px,rb1.val[1]);
  136. px += nRows;
  137. /* Decrement the column loop counter */
  138. blkCnt--;
  139. }
  140. /* Perform matrix transpose for last 3 samples here. */
  141. blkCnt = nColumns % 0x4U;
  142. while (blkCnt > 0U)
  143. {
  144. /* Read and store the input element in the destination */
  145. *px++ = *pIn;
  146. *px++ = *(pIn + 1 * nColumns);
  147. *px++ = *(pIn + 2 * nColumns);
  148. *px++ = *(pIn + 3 * nColumns);
  149. px += (nRows - 4);
  150. pIn++;
  151. /* Decrement the column loop counter */
  152. blkCnt--;
  153. }
  154. i += 4;
  155. pIn += 3 * nColumns;
  156. /* Decrement the row loop counter */
  157. rowCnt--;
  158. } /* Row loop end */
  159. rowCnt = row & 3;
  160. while (rowCnt > 0U)
  161. {
  162. blkCnt = nColumns ;
  163. /* The pointer px is set to starting address of the column being processed */
  164. px = pOut + i;
  165. while (blkCnt > 0U)
  166. {
  167. /* Read and store the input element in the destination */
  168. *px = *pIn++;
  169. /* Update the pointer px to point to the next row of the transposed matrix */
  170. px += nRows;
  171. /* Decrement the column loop counter */
  172. blkCnt--;
  173. }
  174. i++;
  175. rowCnt -- ;
  176. }
  177. /* Set status as ARM_MATH_SUCCESS */
  178. status = ARM_MATH_SUCCESS;
  179. }
  180. /* Return to application */
  181. return (status);
  182. }
  183. #else
  184. arm_status arm_mat_trans_f32(
  185. const arm_matrix_instance_f32 * pSrc,
  186. arm_matrix_instance_f32 * pDst)
  187. {
  188. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  189. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  190. float32_t *px; /* Temporary output data matrix pointer */
  191. uint16_t nRows = pSrc->numRows; /* number of rows */
  192. uint16_t nCols = pSrc->numCols; /* number of columns */
  193. uint32_t col, row = nRows, i = 0U; /* Loop counters */
  194. arm_status status; /* status of matrix transpose */
  195. #ifdef ARM_MATH_MATRIX_CHECK
  196. /* Check for matrix mismatch condition */
  197. if ((pSrc->numRows != pDst->numCols) ||
  198. (pSrc->numCols != pDst->numRows) )
  199. {
  200. /* Set status as ARM_MATH_SIZE_MISMATCH */
  201. status = ARM_MATH_SIZE_MISMATCH;
  202. }
  203. else
  204. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  205. {
  206. /* Matrix transpose by exchanging the rows with columns */
  207. /* row loop */
  208. do
  209. {
  210. /* Pointer px is set to starting address of column being processed */
  211. px = pOut + i;
  212. #if defined (ARM_MATH_LOOPUNROLL)
  213. /* Loop unrolling: Compute 4 outputs at a time */
  214. col = nCols >> 2U;
  215. while (col > 0U) /* column loop */
  216. {
  217. /* Read and store input element in destination */
  218. *px = *pIn++;
  219. /* Update pointer px to point to next row of transposed matrix */
  220. px += nRows;
  221. *px = *pIn++;
  222. px += nRows;
  223. *px = *pIn++;
  224. px += nRows;
  225. *px = *pIn++;
  226. px += nRows;
  227. /* Decrement column loop counter */
  228. col--;
  229. }
  230. /* Loop unrolling: Compute remaining outputs */
  231. col = nCols % 0x4U;
  232. #else
  233. /* Initialize col with number of samples */
  234. col = nCols;
  235. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  236. while (col > 0U)
  237. {
  238. /* Read and store input element in destination */
  239. *px = *pIn++;
  240. /* Update pointer px to point to next row of transposed matrix */
  241. px += nRows;
  242. /* Decrement column loop counter */
  243. col--;
  244. }
  245. i++;
  246. /* Decrement row loop counter */
  247. row--;
  248. } while (row > 0U); /* row loop end */
  249. /* Set status as ARM_MATH_SUCCESS */
  250. status = ARM_MATH_SUCCESS;
  251. }
  252. /* Return to application */
  253. return (status);
  254. }
  255. #endif /* #if defined(ARM_MATH_NEON) */
  256. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  257. /**
  258. * @} end of MatrixTrans group
  259. */