1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_trans_q15.c
4 * Description: Q15 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 @addtogroup MatrixTrans
37 @{
38 */
39
40 /**
41 @brief Q15 matrix transpose.
42 @param[in] pSrc points to input matrix
43 @param[out] pDst points to output matrix
44 @return execution status
45 - \ref ARM_MATH_SUCCESS : Operation successful
46 - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
47 */
48
49 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
50
51 #include "arm_helium_utils.h"
52
53
54
arm_mat_trans_q15(const arm_matrix_instance_q15 * pSrc,arm_matrix_instance_q15 * pDst)55 arm_status arm_mat_trans_q15(
56 const arm_matrix_instance_q15 * pSrc,
57 arm_matrix_instance_q15 * pDst)
58 {
59 arm_status status; /* status of matrix transpose */
60
61 #ifdef ARM_MATH_MATRIX_CHECK
62
63 /* Check for matrix mismatch condition */
64 if ((pSrc->numRows != pDst->numCols) ||
65 (pSrc->numCols != pDst->numRows) )
66 {
67 /* Set status as ARM_MATH_SIZE_MISMATCH */
68 status = ARM_MATH_SIZE_MISMATCH;
69 }
70 else
71
72 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
73
74 {
75 if (pDst->numRows == pDst->numCols)
76 {
77 if (pDst->numCols == 1)
78 {
79 pDst->pData[0] = pSrc->pData[0];
80 return(ARM_MATH_SUCCESS);
81 }
82 if (pDst->numCols == 2)
83 return arm_mat_trans_16bit_2x2((uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
84 if (pDst->numCols == 3)
85 return arm_mat_trans_16bit_3x3_mve((uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
86 if (pDst->numCols == 4)
87 return arm_mat_trans_16bit_4x4_mve((uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
88 }
89
90 arm_mat_trans_16bit_generic(pSrc->numRows, pSrc->numCols, (uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
91 /* Set status as ARM_MATH_SUCCESS */
92 status = ARM_MATH_SUCCESS;
93 }
94
95 /* Return to application */
96 return (status);
97 }
98 #else
arm_mat_trans_q15(const arm_matrix_instance_q15 * pSrc,arm_matrix_instance_q15 * pDst)99 arm_status arm_mat_trans_q15(
100 const arm_matrix_instance_q15 * pSrc,
101 arm_matrix_instance_q15 * pDst)
102 {
103 q15_t *pIn = pSrc->pData; /* input data matrix pointer */
104 q15_t *pOut = pDst->pData; /* output data matrix pointer */
105 uint16_t nRows = pSrc->numRows; /* number of rows */
106 uint16_t nCols = pSrc->numCols; /* number of columns */
107 uint32_t col, row = nRows, i = 0U; /* Loop counters */
108 arm_status status; /* status of matrix transpose */
109
110 #if defined (ARM_MATH_LOOPUNROLL)
111 q31_t in; /* variable to hold temporary output */
112 #endif
113
114 #ifdef ARM_MATH_MATRIX_CHECK
115
116 /* Check for matrix mismatch condition */
117 if ((pSrc->numRows != pDst->numCols) ||
118 (pSrc->numCols != pDst->numRows) )
119 {
120 /* Set status as ARM_MATH_SIZE_MISMATCH */
121 status = ARM_MATH_SIZE_MISMATCH;
122 }
123 else
124
125 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
126
127 {
128 /* Matrix transpose by exchanging the rows with columns */
129 /* row loop */
130 do
131 {
132 /* Pointer pOut is set to starting address of column being processed */
133 pOut = pDst->pData + i;
134
135 #if defined (ARM_MATH_LOOPUNROLL)
136
137 /* Loop unrolling: Compute 4 outputs at a time */
138 col = nCols >> 2U;
139
140 while (col > 0U) /* column loop */
141 {
142 /* Read two elements from row */
143 in = read_q15x2_ia ((q15_t **) &pIn);
144
145 /* Unpack and store one element in destination */
146 #ifndef ARM_MATH_BIG_ENDIAN
147 *pOut = (q15_t) in;
148 #else
149 *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
150 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
151
152 /* Update pointer pOut to point to next row of transposed matrix */
153 pOut += nRows;
154
155 /* Unpack and store second element in destination */
156 #ifndef ARM_MATH_BIG_ENDIAN
157 *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
158 #else
159 *pOut = (q15_t) in;
160 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
161
162 /* Update pointer pOut to point to next row of transposed matrix */
163 pOut += nRows;
164
165 /* Read two elements from row */
166 in = read_q15x2_ia ((q15_t **) &pIn);
167
168 /* Unpack and store one element in destination */
169 #ifndef ARM_MATH_BIG_ENDIAN
170 *pOut = (q15_t) in;
171 #else
172 *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
173
174 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
175
176 /* Update pointer pOut to point to next row of transposed matrix */
177 pOut += nRows;
178
179 /* Unpack and store second element in destination */
180 #ifndef ARM_MATH_BIG_ENDIAN
181 *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
182 #else
183 *pOut = (q15_t) in;
184 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
185
186 /* Update pointer pOut to point to next row of transposed matrix */
187 pOut += nRows;
188
189 /* Decrement column loop counter */
190 col--;
191 }
192
193 /* Loop unrolling: Compute remaining outputs */
194 col = nCols % 0x4U;
195
196 #else
197
198 /* Initialize col with number of samples */
199 col = nCols;
200
201 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
202
203 while (col > 0U)
204 {
205 /* Read and store input element in destination */
206 *pOut = *pIn++;
207
208 /* Update pointer pOut to point to next row of transposed matrix */
209 pOut += nRows;
210
211 /* Decrement column loop counter */
212 col--;
213 }
214
215 i++;
216
217 /* Decrement row loop counter */
218 row--;
219
220 } while (row > 0U); /* row loop end */
221
222 /* Set status as ARM_MATH_SUCCESS */
223 status = ARM_MATH_SUCCESS;
224 }
225
226 /* Return to application */
227 return (status);
228 }
229 #endif /* defined(ARM_MATH_MVEI) */
230
231 /**
232 @} end of MatrixTrans group
233 */
234