arm_mat_solve_upper_triangular_f16.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_solve_upper_triangular_f16.c
  4. * Description: Solve linear system UT X = A with UT upper triangular matrix
  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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupMatrix
  32. */
  33. /**
  34. @addtogroup MatrixInv
  35. @{
  36. */
  37. /**
  38. * @brief Solve UT . X = A where UT is an upper triangular matrix
  39. * @param[in] ut The upper triangular matrix
  40. * @param[in] a The matrix a
  41. * @param[out] dst The solution X of UT . X = A
  42. * @return The function returns ARM_MATH_SINGULAR, if the system can't be solved.
  43. */
  44. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  45. #include "arm_helium_utils.h"
  46. arm_status arm_mat_solve_upper_triangular_f16(
  47. const arm_matrix_instance_f16 * ut,
  48. const arm_matrix_instance_f16 * a,
  49. arm_matrix_instance_f16 * dst)
  50. {
  51. arm_status status; /* status of matrix inverse */
  52. #ifdef ARM_MATH_MATRIX_CHECK
  53. /* Check for matrix mismatch condition */
  54. if ((ut->numRows != ut->numCols) ||
  55. (ut->numRows != a->numRows) )
  56. {
  57. /* Set status as ARM_MATH_SIZE_MISMATCH */
  58. status = ARM_MATH_SIZE_MISMATCH;
  59. }
  60. else
  61. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  62. {
  63. int i,j,k,n,cols;
  64. n = dst->numRows;
  65. cols = dst->numCols;
  66. float16_t *pX = dst->pData;
  67. float16_t *pUT = ut->pData;
  68. float16_t *pA = a->pData;
  69. float16_t *ut_row;
  70. float16_t *a_col;
  71. _Float16 invUT;
  72. f16x8_t vecA;
  73. f16x8_t vecX;
  74. for(i=n-1; i >= 0 ; i--)
  75. {
  76. for(j=0; j+7 < cols; j +=8)
  77. {
  78. vecA = vld1q_f16(&pA[i * cols + j]);
  79. for(k=n-1; k > i; k--)
  80. {
  81. vecX = vld1q_f16(&pX[cols*k+j]);
  82. vecA = vfmsq(vecA,vdupq_n_f16(pUT[n*i + k]),vecX);
  83. }
  84. if ((_Float16)pUT[n*i + i]==0.0f16)
  85. {
  86. return(ARM_MATH_SINGULAR);
  87. }
  88. invUT = 1.0f16 / (_Float16)pUT[n*i + i];
  89. vecA = vmulq(vecA,vdupq_n_f16(invUT));
  90. vst1q(&pX[i*cols+j],vecA);
  91. }
  92. for(; j < cols; j ++)
  93. {
  94. a_col = &pA[j];
  95. ut_row = &pUT[n*i];
  96. _Float16 tmp=a_col[i * cols];
  97. for(k=n-1; k > i; k--)
  98. {
  99. tmp -= (_Float16)ut_row[k] * (_Float16)pX[cols*k+j];
  100. }
  101. if ((_Float16)ut_row[i]==0.0f16)
  102. {
  103. return(ARM_MATH_SINGULAR);
  104. }
  105. tmp = tmp / (_Float16)ut_row[i];
  106. pX[i*cols+j] = tmp;
  107. }
  108. }
  109. status = ARM_MATH_SUCCESS;
  110. }
  111. /* Return to application */
  112. return (status);
  113. }
  114. #else
  115. arm_status arm_mat_solve_upper_triangular_f16(
  116. const arm_matrix_instance_f16 * ut,
  117. const arm_matrix_instance_f16 * a,
  118. arm_matrix_instance_f16 * dst)
  119. {
  120. arm_status status; /* status of matrix inverse */
  121. #ifdef ARM_MATH_MATRIX_CHECK
  122. /* Check for matrix mismatch condition */
  123. if ((ut->numRows != ut->numCols) ||
  124. (ut->numRows != a->numRows) )
  125. {
  126. /* Set status as ARM_MATH_SIZE_MISMATCH */
  127. status = ARM_MATH_SIZE_MISMATCH;
  128. }
  129. else
  130. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  131. {
  132. int i,j,k,n,cols;
  133. n = dst->numRows;
  134. cols = dst->numCols;
  135. float16_t *pX = dst->pData;
  136. float16_t *pUT = ut->pData;
  137. float16_t *pA = a->pData;
  138. float16_t *ut_row;
  139. float16_t *a_col;
  140. for(j=0; j < cols; j ++)
  141. {
  142. a_col = &pA[j];
  143. for(i=n-1; i >= 0 ; i--)
  144. {
  145. ut_row = &pUT[n*i];
  146. float16_t tmp=a_col[i * cols];
  147. for(k=n-1; k > i; k--)
  148. {
  149. tmp -= (_Float16)ut_row[k] * (_Float16)pX[cols*k+j];
  150. }
  151. if ((_Float16)ut_row[i]==0.0f16)
  152. {
  153. return(ARM_MATH_SINGULAR);
  154. }
  155. tmp = (_Float16)tmp / (_Float16)ut_row[i];
  156. pX[i*cols+j] = tmp;
  157. }
  158. }
  159. status = ARM_MATH_SUCCESS;
  160. }
  161. /* Return to application */
  162. return (status);
  163. }
  164. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  165. /**
  166. @} end of MatrixInv group
  167. */
  168. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */