arm_mat_trans_q15.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_trans_q15.c
  4. * Description: Q15 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. #include "arm_math.h"
  29. /**
  30. * @ingroup groupMatrix
  31. */
  32. /**
  33. * @addtogroup MatrixTrans
  34. * @{
  35. */
  36. /*
  37. * @brief Q15 matrix transpose.
  38. * @param[in] *pSrc points to the input matrix
  39. * @param[out] *pDst points to the output matrix
  40. * @return The function returns either <code>ARM_MATH_SIZE_MISMATCH</code>
  41. * or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  42. */
  43. arm_status arm_mat_trans_q15(
  44. const arm_matrix_instance_q15 * pSrc,
  45. arm_matrix_instance_q15 * pDst)
  46. {
  47. q15_t *pSrcA = pSrc->pData; /* input data matrix pointer */
  48. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  49. uint16_t nRows = pSrc->numRows; /* number of nRows */
  50. uint16_t nColumns = pSrc->numCols; /* number of nColumns */
  51. uint16_t col, row = nRows, i = 0U; /* row and column loop counters */
  52. arm_status status; /* status of matrix transpose */
  53. #if defined (ARM_MATH_DSP)
  54. /* Run the below code for Cortex-M4 and Cortex-M3 */
  55. #ifndef UNALIGNED_SUPPORT_DISABLE
  56. q31_t in; /* variable to hold temporary output */
  57. #else
  58. q15_t in;
  59. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  60. #ifdef ARM_MATH_MATRIX_CHECK
  61. /* Check for matrix mismatch condition */
  62. if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  63. {
  64. /* Set status as ARM_MATH_SIZE_MISMATCH */
  65. status = ARM_MATH_SIZE_MISMATCH;
  66. }
  67. else
  68. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  69. {
  70. /* Matrix transpose by exchanging the rows with columns */
  71. /* row loop */
  72. do
  73. {
  74. /* Apply loop unrolling and exchange the columns with row elements */
  75. col = nColumns >> 2U;
  76. /* The pointer pOut is set to starting address of the column being processed */
  77. pOut = pDst->pData + i;
  78. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  79. ** a second loop below computes the remaining 1 to 3 samples. */
  80. while (col > 0U)
  81. {
  82. #ifndef UNALIGNED_SUPPORT_DISABLE
  83. /* Read two elements from the row */
  84. in = *__SIMD32(pSrcA)++;
  85. /* Unpack and store one element in the destination */
  86. #ifndef ARM_MATH_BIG_ENDIAN
  87. *pOut = (q15_t) in;
  88. #else
  89. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  90. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  91. /* Update the pointer pOut to point to the next row of the transposed matrix */
  92. pOut += nRows;
  93. /* Unpack and store the second element in the destination */
  94. #ifndef ARM_MATH_BIG_ENDIAN
  95. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  96. #else
  97. *pOut = (q15_t) in;
  98. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  99. /* Update the pointer pOut to point to the next row of the transposed matrix */
  100. pOut += nRows;
  101. /* Read two elements from the row */
  102. #ifndef ARM_MATH_BIG_ENDIAN
  103. in = *__SIMD32(pSrcA)++;
  104. #else
  105. in = *__SIMD32(pSrcA)++;
  106. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  107. /* Unpack and store one element in the destination */
  108. #ifndef ARM_MATH_BIG_ENDIAN
  109. *pOut = (q15_t) in;
  110. #else
  111. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  112. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  113. /* Update the pointer pOut to point to the next row of the transposed matrix */
  114. pOut += nRows;
  115. /* Unpack and store the second element in the destination */
  116. #ifndef ARM_MATH_BIG_ENDIAN
  117. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  118. #else
  119. *pOut = (q15_t) in;
  120. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  121. #else
  122. /* Read one element from the row */
  123. in = *pSrcA++;
  124. /* Store one element in the destination */
  125. *pOut = in;
  126. /* Update the pointer px to point to the next row of the transposed matrix */
  127. pOut += nRows;
  128. /* Read one element from the row */
  129. in = *pSrcA++;
  130. /* Store one element in the destination */
  131. *pOut = in;
  132. /* Update the pointer px to point to the next row of the transposed matrix */
  133. pOut += nRows;
  134. /* Read one element from the row */
  135. in = *pSrcA++;
  136. /* Store one element in the destination */
  137. *pOut = in;
  138. /* Update the pointer px to point to the next row of the transposed matrix */
  139. pOut += nRows;
  140. /* Read one element from the row */
  141. in = *pSrcA++;
  142. /* Store one element in the destination */
  143. *pOut = in;
  144. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  145. /* Update the pointer pOut to point to the next row of the transposed matrix */
  146. pOut += nRows;
  147. /* Decrement the column loop counter */
  148. col--;
  149. }
  150. /* Perform matrix transpose for last 3 samples here. */
  151. col = nColumns % 0x4U;
  152. #else
  153. /* Run the below code for Cortex-M0 */
  154. #ifdef ARM_MATH_MATRIX_CHECK
  155. /* Check for matrix mismatch condition */
  156. if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  157. {
  158. /* Set status as ARM_MATH_SIZE_MISMATCH */
  159. status = ARM_MATH_SIZE_MISMATCH;
  160. }
  161. else
  162. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  163. {
  164. /* Matrix transpose by exchanging the rows with columns */
  165. /* row loop */
  166. do
  167. {
  168. /* The pointer pOut is set to starting address of the column being processed */
  169. pOut = pDst->pData + i;
  170. /* Initialize column loop counter */
  171. col = nColumns;
  172. #endif /* #if defined (ARM_MATH_DSP) */
  173. while (col > 0U)
  174. {
  175. /* Read and store the input element in the destination */
  176. *pOut = *pSrcA++;
  177. /* Update the pointer pOut to point to the next row of the transposed matrix */
  178. pOut += nRows;
  179. /* Decrement the column loop counter */
  180. col--;
  181. }
  182. i++;
  183. /* Decrement the row loop counter */
  184. row--;
  185. } while (row > 0U);
  186. /* set status as ARM_MATH_SUCCESS */
  187. status = ARM_MATH_SUCCESS;
  188. }
  189. /* Return to application */
  190. return (status);
  191. }
  192. /**
  193. * @} end of MatrixTrans group
  194. */