arm_mat_trans_f32.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_trans_f32.c
  4. * Description: Floating-point matrix transpose
  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. /**
  29. * @defgroup MatrixTrans Matrix Transpose
  30. *
  31. * Tranposes a matrix.
  32. * Transposing an <code>M x N</code> matrix flips it around the center diagonal and results in an <code>N x M</code> matrix.
  33. * \image html MatrixTranspose.gif "Transpose of a 3 x 3 matrix"
  34. */
  35. #include "arm_math.h"
  36. /**
  37. * @ingroup groupMatrix
  38. */
  39. /**
  40. * @addtogroup MatrixTrans
  41. * @{
  42. */
  43. /**
  44. * @brief Floating-point matrix transpose.
  45. * @param[in] *pSrc points to the input matrix
  46. * @param[out] *pDst points to the output matrix
  47. * @return The function returns either <code>ARM_MATH_SIZE_MISMATCH</code>
  48. * or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  49. */
  50. arm_status arm_mat_trans_f32(
  51. const arm_matrix_instance_f32 * pSrc,
  52. arm_matrix_instance_f32 * pDst)
  53. {
  54. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  55. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  56. float32_t *px; /* Temporary output data matrix pointer */
  57. uint16_t nRows = pSrc->numRows; /* number of rows */
  58. uint16_t nColumns = pSrc->numCols; /* number of columns */
  59. #if defined (ARM_MATH_DSP)
  60. /* Run the below code for Cortex-M4 and Cortex-M3 */
  61. uint16_t blkCnt, i = 0U, row = nRows; /* loop counters */
  62. arm_status status; /* status of matrix transpose */
  63. #ifdef ARM_MATH_MATRIX_CHECK
  64. /* Check for matrix mismatch condition */
  65. if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  66. {
  67. /* Set status as ARM_MATH_SIZE_MISMATCH */
  68. status = ARM_MATH_SIZE_MISMATCH;
  69. }
  70. else
  71. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  72. {
  73. /* Matrix transpose by exchanging the rows with columns */
  74. /* row loop */
  75. do
  76. {
  77. /* Loop Unrolling */
  78. blkCnt = nColumns >> 2;
  79. /* The pointer px is set to starting address of the column being processed */
  80. px = pOut + i;
  81. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  82. ** a second loop below computes the remaining 1 to 3 samples. */
  83. while (blkCnt > 0U) /* column loop */
  84. {
  85. /* Read and store the input element in the destination */
  86. *px = *pIn++;
  87. /* Update the pointer px to point to the next row of the transposed matrix */
  88. px += nRows;
  89. /* Read and store the input element in the destination */
  90. *px = *pIn++;
  91. /* Update the pointer px to point to the next row of the transposed matrix */
  92. px += nRows;
  93. /* Read and store the input element in the destination */
  94. *px = *pIn++;
  95. /* Update the pointer px to point to the next row of the transposed matrix */
  96. px += nRows;
  97. /* Read and store the input element in the destination */
  98. *px = *pIn++;
  99. /* Update the pointer px to point to the next row of the transposed matrix */
  100. px += nRows;
  101. /* Decrement the column loop counter */
  102. blkCnt--;
  103. }
  104. /* Perform matrix transpose for last 3 samples here. */
  105. blkCnt = nColumns % 0x4U;
  106. while (blkCnt > 0U)
  107. {
  108. /* Read and store the input element in the destination */
  109. *px = *pIn++;
  110. /* Update the pointer px to point to the next row of the transposed matrix */
  111. px += nRows;
  112. /* Decrement the column loop counter */
  113. blkCnt--;
  114. }
  115. #else
  116. /* Run the below code for Cortex-M0 */
  117. uint16_t col, i = 0U, row = nRows; /* loop counters */
  118. arm_status status; /* status of matrix transpose */
  119. #ifdef ARM_MATH_MATRIX_CHECK
  120. /* Check for matrix mismatch condition */
  121. if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  122. {
  123. /* Set status as ARM_MATH_SIZE_MISMATCH */
  124. status = ARM_MATH_SIZE_MISMATCH;
  125. }
  126. else
  127. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  128. {
  129. /* Matrix transpose by exchanging the rows with columns */
  130. /* row loop */
  131. do
  132. {
  133. /* The pointer px is set to starting address of the column being processed */
  134. px = pOut + i;
  135. /* Initialize column loop counter */
  136. col = nColumns;
  137. while (col > 0U)
  138. {
  139. /* Read and store the input element in the destination */
  140. *px = *pIn++;
  141. /* Update the pointer px to point to the next row of the transposed matrix */
  142. px += nRows;
  143. /* Decrement the column loop counter */
  144. col--;
  145. }
  146. #endif /* #if defined (ARM_MATH_DSP) */
  147. i++;
  148. /* Decrement the row loop counter */
  149. row--;
  150. } while (row > 0U); /* row loop end */
  151. /* Set status as ARM_MATH_SUCCESS */
  152. status = ARM_MATH_SUCCESS;
  153. }
  154. /* Return to application */
  155. return (status);
  156. }
  157. /**
  158. * @} end of MatrixTrans group
  159. */