residual_energy16_FIX.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */
  32. opus_int32 silk_residual_energy16_covar_FIX(
  33. const opus_int16 *c, /* I Prediction vector */
  34. const opus_int32 *wXX, /* I Correlation matrix */
  35. const opus_int32 *wXx, /* I Correlation vector */
  36. opus_int32 wxx, /* I Signal energy */
  37. opus_int D, /* I Dimension */
  38. opus_int cQ /* I Q value for c vector 0 - 15 */
  39. )
  40. {
  41. opus_int i, j, lshifts, Qxtra;
  42. opus_int32 c_max, w_max, tmp, tmp2, nrg;
  43. opus_int cn[ MAX_MATRIX_SIZE ];
  44. const opus_int32 *pRow;
  45. /* Safety checks */
  46. silk_assert( D >= 0 );
  47. silk_assert( D <= 16 );
  48. silk_assert( cQ > 0 );
  49. silk_assert( cQ < 16 );
  50. lshifts = 16 - cQ;
  51. Qxtra = lshifts;
  52. c_max = 0;
  53. for( i = 0; i < D; i++ ) {
  54. c_max = silk_max_32( c_max, silk_abs( (opus_int32)c[ i ] ) );
  55. }
  56. Qxtra = silk_min_int( Qxtra, silk_CLZ32( c_max ) - 17 );
  57. w_max = silk_max_32( wXX[ 0 ], wXX[ D * D - 1 ] );
  58. Qxtra = silk_min_int( Qxtra, silk_CLZ32( silk_MUL( D, silk_RSHIFT( silk_SMULWB( w_max, c_max ), 4 ) ) ) - 5 );
  59. Qxtra = silk_max_int( Qxtra, 0 );
  60. for( i = 0; i < D; i++ ) {
  61. cn[ i ] = silk_LSHIFT( ( opus_int )c[ i ], Qxtra );
  62. silk_assert( silk_abs(cn[i]) <= ( silk_int16_MAX + 1 ) ); /* Check that silk_SMLAWB can be used */
  63. }
  64. lshifts -= Qxtra;
  65. /* Compute wxx - 2 * wXx * c */
  66. tmp = 0;
  67. for( i = 0; i < D; i++ ) {
  68. tmp = silk_SMLAWB( tmp, wXx[ i ], cn[ i ] );
  69. }
  70. nrg = silk_RSHIFT( wxx, 1 + lshifts ) - tmp; /* Q: -lshifts - 1 */
  71. /* Add c' * wXX * c, assuming wXX is symmetric */
  72. tmp2 = 0;
  73. for( i = 0; i < D; i++ ) {
  74. tmp = 0;
  75. pRow = &wXX[ i * D ];
  76. for( j = i + 1; j < D; j++ ) {
  77. tmp = silk_SMLAWB( tmp, pRow[ j ], cn[ j ] );
  78. }
  79. tmp = silk_SMLAWB( tmp, silk_RSHIFT( pRow[ i ], 1 ), cn[ i ] );
  80. tmp2 = silk_SMLAWB( tmp2, tmp, cn[ i ] );
  81. }
  82. nrg = silk_ADD_LSHIFT32( nrg, tmp2, lshifts ); /* Q: -lshifts - 1 */
  83. /* Keep one bit free always, because we add them for LSF interpolation */
  84. if( nrg < 1 ) {
  85. nrg = 1;
  86. } else if( nrg > silk_RSHIFT( silk_int32_MAX, lshifts + 2 ) ) {
  87. nrg = silk_int32_MAX >> 1;
  88. } else {
  89. nrg = silk_LSHIFT( nrg, lshifts + 1 ); /* Q0 */
  90. }
  91. return nrg;
  92. }