arm_mat_trans_q15.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "dsp/matrix_functions.h"
  29. /**
  30. @ingroup groupMatrix
  31. */
  32. /**
  33. @addtogroup MatrixTrans
  34. @{
  35. */
  36. /**
  37. @brief Q15 matrix transpose.
  38. @param[in] pSrc points to input matrix
  39. @param[out] pDst points to output matrix
  40. @return execution status
  41. - \ref ARM_MATH_SUCCESS : Operation successful
  42. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  43. */
  44. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  45. #include "arm_helium_utils.h"
  46. arm_status arm_mat_trans_q15(
  47. const arm_matrix_instance_q15 * pSrc,
  48. arm_matrix_instance_q15 * pDst)
  49. {
  50. arm_status status; /* status of matrix transpose */
  51. #ifdef ARM_MATH_MATRIX_CHECK
  52. /* Check for matrix mismatch condition */
  53. if ((pSrc->numRows != pDst->numCols) ||
  54. (pSrc->numCols != pDst->numRows) )
  55. {
  56. /* Set status as ARM_MATH_SIZE_MISMATCH */
  57. status = ARM_MATH_SIZE_MISMATCH;
  58. }
  59. else
  60. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  61. {
  62. if (pDst->numRows == pDst->numCols)
  63. {
  64. if (pDst->numCols == 1)
  65. {
  66. pDst->pData[0] = pSrc->pData[0];
  67. return(ARM_MATH_SUCCESS);
  68. }
  69. if (pDst->numCols == 2)
  70. return arm_mat_trans_16bit_2x2((uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
  71. if (pDst->numCols == 3)
  72. return arm_mat_trans_16bit_3x3_mve((uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
  73. if (pDst->numCols == 4)
  74. return arm_mat_trans_16bit_4x4_mve((uint16_t *)pSrc->pData, (uint16_t *)pDst->pData);
  75. }
  76. arm_mat_trans_16bit_generic(pSrc->numRows, pSrc->numCols, (uint16_t *)pSrc->pData, (uint16_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. arm_status arm_mat_trans_q15(
  85. const arm_matrix_instance_q15 * pSrc,
  86. arm_matrix_instance_q15 * pDst)
  87. {
  88. q15_t *pIn = pSrc->pData; /* input data matrix pointer */
  89. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  90. uint16_t nRows = pSrc->numRows; /* number of rows */
  91. uint16_t nCols = pSrc->numCols; /* number of columns */
  92. uint32_t col, row = nRows, i = 0U; /* Loop counters */
  93. arm_status status; /* status of matrix transpose */
  94. #if defined (ARM_MATH_LOOPUNROLL)
  95. q31_t in; /* variable to hold temporary output */
  96. #endif
  97. #ifdef ARM_MATH_MATRIX_CHECK
  98. /* Check for matrix mismatch condition */
  99. if ((pSrc->numRows != pDst->numCols) ||
  100. (pSrc->numCols != pDst->numRows) )
  101. {
  102. /* Set status as ARM_MATH_SIZE_MISMATCH */
  103. status = ARM_MATH_SIZE_MISMATCH;
  104. }
  105. else
  106. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  107. {
  108. /* Matrix transpose by exchanging the rows with columns */
  109. /* row loop */
  110. do
  111. {
  112. /* Pointer pOut is set to starting address of column being processed */
  113. pOut = pDst->pData + i;
  114. #if defined (ARM_MATH_LOOPUNROLL)
  115. /* Loop unrolling: Compute 4 outputs at a time */
  116. col = nCols >> 2U;
  117. while (col > 0U) /* column loop */
  118. {
  119. /* Read two elements from row */
  120. in = read_q15x2_ia (&pIn);
  121. /* Unpack and store one element in destination */
  122. #ifndef ARM_MATH_BIG_ENDIAN
  123. *pOut = (q15_t) in;
  124. #else
  125. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  126. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  127. /* Update pointer pOut to point to next row of transposed matrix */
  128. pOut += nRows;
  129. /* Unpack and store second element in destination */
  130. #ifndef ARM_MATH_BIG_ENDIAN
  131. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  132. #else
  133. *pOut = (q15_t) in;
  134. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  135. /* Update pointer pOut to point to next row of transposed matrix */
  136. pOut += nRows;
  137. /* Read two elements from row */
  138. in = read_q15x2_ia (&pIn);
  139. /* Unpack and store one element in destination */
  140. #ifndef ARM_MATH_BIG_ENDIAN
  141. *pOut = (q15_t) in;
  142. #else
  143. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  144. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  145. /* Update pointer pOut to point to next row of transposed matrix */
  146. pOut += nRows;
  147. /* Unpack and store second element in destination */
  148. #ifndef ARM_MATH_BIG_ENDIAN
  149. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  150. #else
  151. *pOut = (q15_t) in;
  152. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  153. /* Update pointer pOut to point to next row of transposed matrix */
  154. pOut += nRows;
  155. /* Decrement column loop counter */
  156. col--;
  157. }
  158. /* Loop unrolling: Compute remaining outputs */
  159. col = nCols % 0x4U;
  160. #else
  161. /* Initialize col with number of samples */
  162. col = nCols;
  163. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  164. while (col > 0U)
  165. {
  166. /* Read and store input element in destination */
  167. *pOut = *pIn++;
  168. /* Update pointer pOut to point to next row of transposed matrix */
  169. pOut += nRows;
  170. /* Decrement column loop counter */
  171. col--;
  172. }
  173. i++;
  174. /* Decrement row loop counter */
  175. row--;
  176. } while (row > 0U); /* row loop end */
  177. /* Set status as ARM_MATH_SUCCESS */
  178. status = ARM_MATH_SUCCESS;
  179. }
  180. /* Return to application */
  181. return (status);
  182. }
  183. #endif /* defined(ARM_MATH_MVEI) */
  184. /**
  185. @} end of MatrixTrans group
  186. */