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