Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "main_FIX.h" Chris@69: Chris@69: /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ Chris@69: opus_int32 silk_residual_energy16_covar_FIX( Chris@69: const opus_int16 *c, /* I Prediction vector */ Chris@69: const opus_int32 *wXX, /* I Correlation matrix */ Chris@69: const opus_int32 *wXx, /* I Correlation vector */ Chris@69: opus_int32 wxx, /* I Signal energy */ Chris@69: opus_int D, /* I Dimension */ Chris@69: opus_int cQ /* I Q value for c vector 0 - 15 */ Chris@69: ) Chris@69: { Chris@69: opus_int i, j, lshifts, Qxtra; Chris@69: opus_int32 c_max, w_max, tmp, tmp2, nrg; Chris@69: opus_int cn[ MAX_MATRIX_SIZE ]; Chris@69: const opus_int32 *pRow; Chris@69: Chris@69: /* Safety checks */ Chris@69: celt_assert( D >= 0 ); Chris@69: celt_assert( D <= 16 ); Chris@69: celt_assert( cQ > 0 ); Chris@69: celt_assert( cQ < 16 ); Chris@69: Chris@69: lshifts = 16 - cQ; Chris@69: Qxtra = lshifts; Chris@69: Chris@69: c_max = 0; Chris@69: for( i = 0; i < D; i++ ) { Chris@69: c_max = silk_max_32( c_max, silk_abs( (opus_int32)c[ i ] ) ); Chris@69: } Chris@69: Qxtra = silk_min_int( Qxtra, silk_CLZ32( c_max ) - 17 ); Chris@69: Chris@69: w_max = silk_max_32( wXX[ 0 ], wXX[ D * D - 1 ] ); Chris@69: Qxtra = silk_min_int( Qxtra, silk_CLZ32( silk_MUL( D, silk_RSHIFT( silk_SMULWB( w_max, c_max ), 4 ) ) ) - 5 ); Chris@69: Qxtra = silk_max_int( Qxtra, 0 ); Chris@69: for( i = 0; i < D; i++ ) { Chris@69: cn[ i ] = silk_LSHIFT( ( opus_int )c[ i ], Qxtra ); Chris@69: silk_assert( silk_abs(cn[i]) <= ( silk_int16_MAX + 1 ) ); /* Check that silk_SMLAWB can be used */ Chris@69: } Chris@69: lshifts -= Qxtra; Chris@69: Chris@69: /* Compute wxx - 2 * wXx * c */ Chris@69: tmp = 0; Chris@69: for( i = 0; i < D; i++ ) { Chris@69: tmp = silk_SMLAWB( tmp, wXx[ i ], cn[ i ] ); Chris@69: } Chris@69: nrg = silk_RSHIFT( wxx, 1 + lshifts ) - tmp; /* Q: -lshifts - 1 */ Chris@69: Chris@69: /* Add c' * wXX * c, assuming wXX is symmetric */ Chris@69: tmp2 = 0; Chris@69: for( i = 0; i < D; i++ ) { Chris@69: tmp = 0; Chris@69: pRow = &wXX[ i * D ]; Chris@69: for( j = i + 1; j < D; j++ ) { Chris@69: tmp = silk_SMLAWB( tmp, pRow[ j ], cn[ j ] ); Chris@69: } Chris@69: tmp = silk_SMLAWB( tmp, silk_RSHIFT( pRow[ i ], 1 ), cn[ i ] ); Chris@69: tmp2 = silk_SMLAWB( tmp2, tmp, cn[ i ] ); Chris@69: } Chris@69: nrg = silk_ADD_LSHIFT32( nrg, tmp2, lshifts ); /* Q: -lshifts - 1 */ Chris@69: Chris@69: /* Keep one bit free always, because we add them for LSF interpolation */ Chris@69: if( nrg < 1 ) { Chris@69: nrg = 1; Chris@69: } else if( nrg > silk_RSHIFT( silk_int32_MAX, lshifts + 2 ) ) { Chris@69: nrg = silk_int32_MAX >> 1; Chris@69: } else { Chris@69: nrg = silk_LSHIFT( nrg, lshifts + 1 ); /* Q0 */ Chris@69: } Chris@69: return nrg; Chris@69: Chris@69: }