12bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian/*********************************************************************** 22bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianCopyright (c) 2006-2011, Skype Limited. All rights reserved. 32bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianRedistribution and use in source and binary forms, with or without 42bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianmodification, are permitted provided that the following conditions 52bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianare met: 62bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian- Redistributions of source code must retain the above copyright notice, 72bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianthis list of conditions and the following disclaimer. 82bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian- Redistributions in binary form must reproduce the above copyright 92bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramaniannotice, this list of conditions and the following disclaimer in the 102bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramaniandocumentation and/or other materials provided with the distribution. 112bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian- Neither the name of Internet Society, IETF or IETF Trust, nor the 122bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramaniannames of specific contributors, may be used to endorse or promote 132bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianproducts derived from this software without specific prior written 142bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianpermission. 152bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 162bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 172bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 182bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 192bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 202bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 212bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 222bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 232bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 242bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 252bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianPOSSIBILITY OF SUCH DAMAGE. 262bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian***********************************************************************/ 272bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 282bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#ifdef HAVE_CONFIG_H 292bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#include "config.h" 302bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#endif 312bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 322bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#include "SigProc_FIX.h" 332bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 342bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian/* Compute number of bits to right shift the sum of squares of a vector */ 352bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian/* of int16s to make it fit in an int32 */ 362bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianvoid silk_sum_sqr_shift( 372bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian opus_int32 *energy, /* O Energy of x, after shifting to the right */ 382bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian opus_int *shift, /* O Number of bits right shift applied to energy */ 392bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian const opus_int16 *x, /* I Input vector */ 402bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian opus_int len /* I Length of input vector */ 412bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian) 422bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian{ 432bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian opus_int i, shft; 440c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim opus_uint32 nrg_tmp; 450c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim opus_int32 nrg; 462bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 470c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim /* Do a first run with the maximum shift we could have. */ 480c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim shft = 31-silk_CLZ32(len); 490c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim /* Let's be conservative with rounding and start with nrg=len. */ 500c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg = len; 510c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim for( i = 0; i < len - 1; i += 2 ) { 520c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); 530c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg_tmp = silk_SMLABB_ovflw( nrg_tmp, x[ i + 1 ], x[ i + 1 ] ); 540c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); 552bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian } 560c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim if( i < len ) { 570c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim /* One sample left to process */ 580c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); 590c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); 600c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim } 610c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim silk_assert( nrg >= 0 ); 620c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim /* Make sure the result will fit in a 32-bit signed integer with two bits 630c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim of headroom. */ 640c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim shft = silk_max_32(0, shft+3 - silk_CLZ32(nrg)); 650c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg = 0; 660c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim for( i = 0 ; i < len - 1; i += 2 ) { 672bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); 682bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian nrg_tmp = silk_SMLABB_ovflw( nrg_tmp, x[ i + 1 ], x[ i + 1 ] ); 690c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); 702bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian } 710c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim if( i < len ) { 722bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian /* One sample left to process */ 732bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); 742bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); 752bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian } 762bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 770c2090c324e4f2ba2a8621c8b083559bab74c7c5Felicia Lim silk_assert( nrg >= 0 ); 782bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 792bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian /* Output arguments */ 802bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian *shift = shft; 812bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian *energy = nrg; 822bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian} 832bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian 84