arm_mat_trans_q15.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_mat_trans_q15.c
  9. *
  10. * Description: Q15 matrix transpose.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupMatrix
  43. */
  44. /**
  45. * @addtogroup MatrixTrans
  46. * @{
  47. */
  48. /*
  49. * @brief Q15 matrix transpose.
  50. * @param[in] *pSrc points to the input matrix
  51. * @param[out] *pDst points to the output matrix
  52. * @return The function returns either <code>ARM_MATH_SIZE_MISMATCH</code>
  53. * or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  54. */
  55. arm_status arm_mat_trans_q15(
  56. const arm_matrix_instance_q15 * pSrc,
  57. arm_matrix_instance_q15 * pDst)
  58. {
  59. q15_t *pSrcA = pSrc->pData; /* input data matrix pointer */
  60. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  61. uint16_t nRows = pSrc->numRows; /* number of nRows */
  62. uint16_t nColumns = pSrc->numCols; /* number of nColumns */
  63. uint16_t col, row = nRows, i = 0u; /* row and column loop counters */
  64. arm_status status; /* status of matrix transpose */
  65. #ifndef ARM_MATH_CM0_FAMILY
  66. /* Run the below code for Cortex-M4 and Cortex-M3 */
  67. #ifndef UNALIGNED_SUPPORT_DISABLE
  68. q31_t in; /* variable to hold temporary output */
  69. #else
  70. q15_t in;
  71. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  72. #ifdef ARM_MATH_MATRIX_CHECK
  73. /* Check for matrix mismatch condition */
  74. if((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  75. {
  76. /* Set status as ARM_MATH_SIZE_MISMATCH */
  77. status = ARM_MATH_SIZE_MISMATCH;
  78. }
  79. else
  80. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  81. {
  82. /* Matrix transpose by exchanging the rows with columns */
  83. /* row loop */
  84. do
  85. {
  86. /* Apply loop unrolling and exchange the columns with row elements */
  87. col = nColumns >> 2u;
  88. /* The pointer pOut is set to starting address of the column being processed */
  89. pOut = pDst->pData + i;
  90. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  91. ** a second loop below computes the remaining 1 to 3 samples. */
  92. while(col > 0u)
  93. {
  94. #ifndef UNALIGNED_SUPPORT_DISABLE
  95. /* Read two elements from the row */
  96. in = *__SIMD32(pSrcA)++;
  97. /* Unpack and store one element in the destination */
  98. #ifndef ARM_MATH_BIG_ENDIAN
  99. *pOut = (q15_t) in;
  100. #else
  101. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  102. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  103. /* Update the pointer pOut to point to the next row of the transposed matrix */
  104. pOut += nRows;
  105. /* Unpack and store the second element in the destination */
  106. #ifndef ARM_MATH_BIG_ENDIAN
  107. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  108. #else
  109. *pOut = (q15_t) in;
  110. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  111. /* Update the pointer pOut to point to the next row of the transposed matrix */
  112. pOut += nRows;
  113. /* Read two elements from the row */
  114. #ifndef ARM_MATH_BIG_ENDIAN
  115. in = *__SIMD32(pSrcA)++;
  116. #else
  117. in = *__SIMD32(pSrcA)++;
  118. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  119. /* Unpack and store one element in the destination */
  120. #ifndef ARM_MATH_BIG_ENDIAN
  121. *pOut = (q15_t) in;
  122. #else
  123. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  124. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  125. /* Update the pointer pOut to point to the next row of the transposed matrix */
  126. pOut += nRows;
  127. /* Unpack and store the second element in the destination */
  128. #ifndef ARM_MATH_BIG_ENDIAN
  129. *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16);
  130. #else
  131. *pOut = (q15_t) in;
  132. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  133. #else
  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. /* Update the pointer px to point to the next row of the transposed matrix */
  145. pOut += nRows;
  146. /* Read one element from the row */
  147. in = *pSrcA++;
  148. /* Store one element in the destination */
  149. *pOut = in;
  150. /* Update the pointer px to point to the next row of the transposed matrix */
  151. pOut += nRows;
  152. /* Read one element from the row */
  153. in = *pSrcA++;
  154. /* Store one element in the destination */
  155. *pOut = in;
  156. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  157. /* Update the pointer pOut to point to the next row of the transposed matrix */
  158. pOut += nRows;
  159. /* Decrement the column loop counter */
  160. col--;
  161. }
  162. /* Perform matrix transpose for last 3 samples here. */
  163. col = nColumns % 0x4u;
  164. #else
  165. /* Run the below code for Cortex-M0 */
  166. #ifdef ARM_MATH_MATRIX_CHECK
  167. /* Check for matrix mismatch condition */
  168. if((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows))
  169. {
  170. /* Set status as ARM_MATH_SIZE_MISMATCH */
  171. status = ARM_MATH_SIZE_MISMATCH;
  172. }
  173. else
  174. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  175. {
  176. /* Matrix transpose by exchanging the rows with columns */
  177. /* row loop */
  178. do
  179. {
  180. /* The pointer pOut is set to starting address of the column being processed */
  181. pOut = pDst->pData + i;
  182. /* Initialize column loop counter */
  183. col = nColumns;
  184. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  185. while(col > 0u)
  186. {
  187. /* Read and store the input element in the destination */
  188. *pOut = *pSrcA++;
  189. /* Update the pointer pOut to point to the next row of the transposed matrix */
  190. pOut += nRows;
  191. /* Decrement the column loop counter */
  192. col--;
  193. }
  194. i++;
  195. /* Decrement the row loop counter */
  196. row--;
  197. } while(row > 0u);
  198. /* set status as ARM_MATH_SUCCESS */
  199. status = ARM_MATH_SUCCESS;
  200. }
  201. /* Return to application */
  202. return (status);
  203. }
  204. /**
  205. * @} end of MatrixTrans group
  206. */