solve_LS_FIX.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main_FIX.h"
  31. #include "stack_alloc.h"
  32. #include "tuning_parameters.h"
  33. /*****************************/
  34. /* Internal function headers */
  35. /*****************************/
  36. typedef struct {
  37. opus_int32 Q36_part;
  38. opus_int32 Q48_part;
  39. } inv_D_t;
  40. /* Factorize square matrix A into LDL form */
  41. static OPUS_INLINE void silk_LDL_factorize_FIX(
  42. opus_int32 *A, /* I/O Pointer to Symetric Square Matrix */
  43. opus_int M, /* I Size of Matrix */
  44. opus_int32 *L_Q16, /* I/O Pointer to Square Upper triangular Matrix */
  45. inv_D_t *inv_D /* I/O Pointer to vector holding inverted diagonal elements of D */
  46. );
  47. /* Solve Lx = b, when L is lower triangular and has ones on the diagonal */
  48. static OPUS_INLINE void silk_LS_SolveFirst_FIX(
  49. const opus_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */
  50. opus_int M, /* I Dim of Matrix equation */
  51. const opus_int32 *b, /* I b Vector */
  52. opus_int32 *x_Q16 /* O x Vector */
  53. );
  54. /* Solve L^t*x = b, where L is lower triangular with ones on the diagonal */
  55. static OPUS_INLINE void silk_LS_SolveLast_FIX(
  56. const opus_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */
  57. const opus_int M, /* I Dim of Matrix equation */
  58. const opus_int32 *b, /* I b Vector */
  59. opus_int32 *x_Q16 /* O x Vector */
  60. );
  61. static OPUS_INLINE void silk_LS_divide_Q16_FIX(
  62. opus_int32 T[], /* I/O Numenator vector */
  63. inv_D_t *inv_D, /* I 1 / D vector */
  64. opus_int M /* I dimension */
  65. );
  66. /* Solves Ax = b, assuming A is symmetric */
  67. void silk_solve_LDL_FIX(
  68. opus_int32 *A, /* I Pointer to symetric square matrix A */
  69. opus_int M, /* I Size of matrix */
  70. const opus_int32 *b, /* I Pointer to b vector */
  71. opus_int32 *x_Q16 /* O Pointer to x solution vector */
  72. )
  73. {
  74. VARDECL( opus_int32, L_Q16 );
  75. opus_int32 Y[ MAX_MATRIX_SIZE ];
  76. inv_D_t inv_D[ MAX_MATRIX_SIZE ];
  77. SAVE_STACK;
  78. silk_assert( M <= MAX_MATRIX_SIZE );
  79. ALLOC( L_Q16, M * M, opus_int32 );
  80. /***************************************************
  81. Factorize A by LDL such that A = L*D*L',
  82. where L is lower triangular with ones on diagonal
  83. ****************************************************/
  84. silk_LDL_factorize_FIX( A, M, L_Q16, inv_D );
  85. /****************************************************
  86. * substitute D*L'*x = Y. ie:
  87. L*D*L'*x = b => L*Y = b <=> Y = inv(L)*b
  88. ******************************************************/
  89. silk_LS_SolveFirst_FIX( L_Q16, M, b, Y );
  90. /****************************************************
  91. D*L'*x = Y <=> L'*x = inv(D)*Y, because D is
  92. diagonal just multiply with 1/d_i
  93. ****************************************************/
  94. silk_LS_divide_Q16_FIX( Y, inv_D, M );
  95. /****************************************************
  96. x = inv(L') * inv(D) * Y
  97. *****************************************************/
  98. silk_LS_SolveLast_FIX( L_Q16, M, Y, x_Q16 );
  99. RESTORE_STACK;
  100. }
  101. static OPUS_INLINE void silk_LDL_factorize_FIX(
  102. opus_int32 *A, /* I/O Pointer to Symetric Square Matrix */
  103. opus_int M, /* I Size of Matrix */
  104. opus_int32 *L_Q16, /* I/O Pointer to Square Upper triangular Matrix */
  105. inv_D_t *inv_D /* I/O Pointer to vector holding inverted diagonal elements of D */
  106. )
  107. {
  108. opus_int i, j, k, status, loop_count;
  109. const opus_int32 *ptr1, *ptr2;
  110. opus_int32 diag_min_value, tmp_32, err;
  111. opus_int32 v_Q0[ MAX_MATRIX_SIZE ], D_Q0[ MAX_MATRIX_SIZE ];
  112. opus_int32 one_div_diag_Q36, one_div_diag_Q40, one_div_diag_Q48;
  113. silk_assert( M <= MAX_MATRIX_SIZE );
  114. status = 1;
  115. diag_min_value = silk_max_32( silk_SMMUL( silk_ADD_SAT32( A[ 0 ], A[ silk_SMULBB( M, M ) - 1 ] ), SILK_FIX_CONST( FIND_LTP_COND_FAC, 31 ) ), 1 << 9 );
  116. for( loop_count = 0; loop_count < M && status == 1; loop_count++ ) {
  117. status = 0;
  118. for( j = 0; j < M; j++ ) {
  119. ptr1 = matrix_adr( L_Q16, j, 0, M );
  120. tmp_32 = 0;
  121. for( i = 0; i < j; i++ ) {
  122. v_Q0[ i ] = silk_SMULWW( D_Q0[ i ], ptr1[ i ] ); /* Q0 */
  123. tmp_32 = silk_SMLAWW( tmp_32, v_Q0[ i ], ptr1[ i ] ); /* Q0 */
  124. }
  125. tmp_32 = silk_SUB32( matrix_ptr( A, j, j, M ), tmp_32 );
  126. if( tmp_32 < diag_min_value ) {
  127. tmp_32 = silk_SUB32( silk_SMULBB( loop_count + 1, diag_min_value ), tmp_32 );
  128. /* Matrix not positive semi-definite, or ill conditioned */
  129. for( i = 0; i < M; i++ ) {
  130. matrix_ptr( A, i, i, M ) = silk_ADD32( matrix_ptr( A, i, i, M ), tmp_32 );
  131. }
  132. status = 1;
  133. break;
  134. }
  135. D_Q0[ j ] = tmp_32; /* always < max(Correlation) */
  136. /* two-step division */
  137. one_div_diag_Q36 = silk_INVERSE32_varQ( tmp_32, 36 ); /* Q36 */
  138. one_div_diag_Q40 = silk_LSHIFT( one_div_diag_Q36, 4 ); /* Q40 */
  139. err = silk_SUB32( (opus_int32)1 << 24, silk_SMULWW( tmp_32, one_div_diag_Q40 ) ); /* Q24 */
  140. one_div_diag_Q48 = silk_SMULWW( err, one_div_diag_Q40 ); /* Q48 */
  141. /* Save 1/Ds */
  142. inv_D[ j ].Q36_part = one_div_diag_Q36;
  143. inv_D[ j ].Q48_part = one_div_diag_Q48;
  144. matrix_ptr( L_Q16, j, j, M ) = 65536; /* 1.0 in Q16 */
  145. ptr1 = matrix_adr( A, j, 0, M );
  146. ptr2 = matrix_adr( L_Q16, j + 1, 0, M );
  147. for( i = j + 1; i < M; i++ ) {
  148. tmp_32 = 0;
  149. for( k = 0; k < j; k++ ) {
  150. tmp_32 = silk_SMLAWW( tmp_32, v_Q0[ k ], ptr2[ k ] ); /* Q0 */
  151. }
  152. tmp_32 = silk_SUB32( ptr1[ i ], tmp_32 ); /* always < max(Correlation) */
  153. /* tmp_32 / D_Q0[j] : Divide to Q16 */
  154. matrix_ptr( L_Q16, i, j, M ) = silk_ADD32( silk_SMMUL( tmp_32, one_div_diag_Q48 ),
  155. silk_RSHIFT( silk_SMULWW( tmp_32, one_div_diag_Q36 ), 4 ) );
  156. /* go to next column */
  157. ptr2 += M;
  158. }
  159. }
  160. }
  161. silk_assert( status == 0 );
  162. }
  163. static OPUS_INLINE void silk_LS_divide_Q16_FIX(
  164. opus_int32 T[], /* I/O Numenator vector */
  165. inv_D_t *inv_D, /* I 1 / D vector */
  166. opus_int M /* I dimension */
  167. )
  168. {
  169. opus_int i;
  170. opus_int32 tmp_32;
  171. opus_int32 one_div_diag_Q36, one_div_diag_Q48;
  172. for( i = 0; i < M; i++ ) {
  173. one_div_diag_Q36 = inv_D[ i ].Q36_part;
  174. one_div_diag_Q48 = inv_D[ i ].Q48_part;
  175. tmp_32 = T[ i ];
  176. T[ i ] = silk_ADD32( silk_SMMUL( tmp_32, one_div_diag_Q48 ), silk_RSHIFT( silk_SMULWW( tmp_32, one_div_diag_Q36 ), 4 ) );
  177. }
  178. }
  179. /* Solve Lx = b, when L is lower triangular and has ones on the diagonal */
  180. static OPUS_INLINE void silk_LS_SolveFirst_FIX(
  181. const opus_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */
  182. opus_int M, /* I Dim of Matrix equation */
  183. const opus_int32 *b, /* I b Vector */
  184. opus_int32 *x_Q16 /* O x Vector */
  185. )
  186. {
  187. opus_int i, j;
  188. const opus_int32 *ptr32;
  189. opus_int32 tmp_32;
  190. for( i = 0; i < M; i++ ) {
  191. ptr32 = matrix_adr( L_Q16, i, 0, M );
  192. tmp_32 = 0;
  193. for( j = 0; j < i; j++ ) {
  194. tmp_32 = silk_SMLAWW( tmp_32, ptr32[ j ], x_Q16[ j ] );
  195. }
  196. x_Q16[ i ] = silk_SUB32( b[ i ], tmp_32 );
  197. }
  198. }
  199. /* Solve L^t*x = b, where L is lower triangular with ones on the diagonal */
  200. static OPUS_INLINE void silk_LS_SolveLast_FIX(
  201. const opus_int32 *L_Q16, /* I Pointer to Lower Triangular Matrix */
  202. const opus_int M, /* I Dim of Matrix equation */
  203. const opus_int32 *b, /* I b Vector */
  204. opus_int32 *x_Q16 /* O x Vector */
  205. )
  206. {
  207. opus_int i, j;
  208. const opus_int32 *ptr32;
  209. opus_int32 tmp_32;
  210. for( i = M - 1; i >= 0; i-- ) {
  211. ptr32 = matrix_adr( L_Q16, 0, i, M );
  212. tmp_32 = 0;
  213. for( j = M - 1; j > i; j-- ) {
  214. tmp_32 = silk_SMLAWW( tmp_32, ptr32[ silk_SMULBB( j, M ) ], x_Q16[ j ] );
  215. }
  216. x_Q16[ i ] = silk_SUB32( b[ i ], tmp_32 );
  217. }
  218. }