arm_mat_solve_lower_triangular_f16.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_solve_lower_triangular_f16.c
  4. * Description: Solve linear system LT X = A with LT lower 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 LT . X = A where LT is a lower triangular matrix
  39. * @param[in] lt The lower triangular matrix
  40. * @param[in] a The matrix a
  41. * @param[out] dst The solution X of LT . 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_lower_triangular_f16(
  47. const arm_matrix_instance_f16 * lt,
  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 ((lt->numRows != lt->numCols) ||
  55. (lt->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. /* a1 b1 c1 x1 = a1
  64. b2 c2 x2 a2
  65. c3 x3 a3
  66. x3 = a3 / c3
  67. x2 = (a2 - c2 x3) / b2
  68. */
  69. int i,j,k,n,cols;
  70. n = dst->numRows;
  71. cols = dst->numCols;
  72. float16_t *pX = dst->pData;
  73. float16_t *pLT = lt->pData;
  74. float16_t *pA = a->pData;
  75. float16_t *lt_row;
  76. float16_t *a_col;
  77. _Float16 invLT;
  78. f16x8_t vecA;
  79. f16x8_t vecX;
  80. for(i=0; i < n ; i++)
  81. {
  82. for(j=0; j+7 < cols; j += 8)
  83. {
  84. vecA = vld1q_f16(&pA[i * cols + j]);
  85. for(k=0; k < i; k++)
  86. {
  87. vecX = vld1q_f16(&pX[cols*k+j]);
  88. vecA = vfmsq(vecA,vdupq_n_f16(pLT[n*i + k]),vecX);
  89. }
  90. if ((_Float16)pLT[n*i + i]==0.0f16)
  91. {
  92. return(ARM_MATH_SINGULAR);
  93. }
  94. invLT = 1.0f16 / (_Float16)pLT[n*i + i];
  95. vecA = vmulq(vecA,vdupq_n_f16(invLT));
  96. vst1q(&pX[i*cols+j],vecA);
  97. }
  98. for(; j < cols; j ++)
  99. {
  100. a_col = &pA[j];
  101. lt_row = &pLT[n*i];
  102. _Float16 tmp=a_col[i * cols];
  103. for(k=0; k < i; k++)
  104. {
  105. tmp -= (_Float16)lt_row[k] * (_Float16)pX[cols*k+j];
  106. }
  107. if ((_Float16)lt_row[i]==0.0f16)
  108. {
  109. return(ARM_MATH_SINGULAR);
  110. }
  111. tmp = tmp / (_Float16)lt_row[i];
  112. pX[i*cols+j] = tmp;
  113. }
  114. }
  115. status = ARM_MATH_SUCCESS;
  116. }
  117. /* Return to application */
  118. return (status);
  119. }
  120. #else
  121. arm_status arm_mat_solve_lower_triangular_f16(
  122. const arm_matrix_instance_f16 * lt,
  123. const arm_matrix_instance_f16 * a,
  124. arm_matrix_instance_f16 * dst)
  125. {
  126. arm_status status; /* status of matrix inverse */
  127. #ifdef ARM_MATH_MATRIX_CHECK
  128. /* Check for matrix mismatch condition */
  129. if ((lt->numRows != lt->numCols) ||
  130. (lt->numRows != a->numRows) )
  131. {
  132. /* Set status as ARM_MATH_SIZE_MISMATCH */
  133. status = ARM_MATH_SIZE_MISMATCH;
  134. }
  135. else
  136. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  137. {
  138. /* a1 b1 c1 x1 = a1
  139. b2 c2 x2 a2
  140. c3 x3 a3
  141. x3 = a3 / c3
  142. x2 = (a2 - c2 x3) / b2
  143. */
  144. int i,j,k,n,cols;
  145. n = dst->numRows;
  146. cols = dst->numCols;
  147. float16_t *pX = dst->pData;
  148. float16_t *pLT = lt->pData;
  149. float16_t *pA = a->pData;
  150. float16_t *lt_row;
  151. float16_t *a_col;
  152. for(j=0; j < cols; j ++)
  153. {
  154. a_col = &pA[j];
  155. for(i=0; i < n ; i++)
  156. {
  157. lt_row = &pLT[n*i];
  158. float16_t tmp=a_col[i * cols];
  159. for(k=0; k < i; k++)
  160. {
  161. tmp -= (_Float16)lt_row[k] * (_Float16)pX[cols*k+j];
  162. }
  163. if ((_Float16)lt_row[i]==0.0f16)
  164. {
  165. return(ARM_MATH_SINGULAR);
  166. }
  167. tmp = (_Float16)tmp / (_Float16)lt_row[i];
  168. pX[i*cols+j] = tmp;
  169. }
  170. }
  171. status = ARM_MATH_SUCCESS;
  172. }
  173. /* Return to application */
  174. return (status);
  175. }
  176. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  177. /**
  178. @} end of MatrixInv group
  179. */
  180. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */