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: /*! \file silk_Inlines.h Chris@69: * \brief silk_Inlines.h defines OPUS_INLINE signal processing functions. Chris@69: */ Chris@69: Chris@69: #ifndef SILK_FIX_INLINES_H Chris@69: #define SILK_FIX_INLINES_H Chris@69: Chris@69: #ifdef __cplusplus Chris@69: extern "C" Chris@69: { Chris@69: #endif Chris@69: Chris@69: /* count leading zeros of opus_int64 */ Chris@69: static OPUS_INLINE opus_int32 silk_CLZ64( opus_int64 in ) Chris@69: { Chris@69: opus_int32 in_upper; Chris@69: Chris@69: in_upper = (opus_int32)silk_RSHIFT64(in, 32); Chris@69: if (in_upper == 0) { Chris@69: /* Search in the lower 32 bits */ Chris@69: return 32 + silk_CLZ32( (opus_int32) in ); Chris@69: } else { Chris@69: /* Search in the upper 32 bits */ Chris@69: return silk_CLZ32( in_upper ); Chris@69: } Chris@69: } Chris@69: Chris@69: /* get number of leading zeros and fractional part (the bits right after the leading one */ Chris@69: static OPUS_INLINE void silk_CLZ_FRAC( Chris@69: opus_int32 in, /* I input */ Chris@69: opus_int32 *lz, /* O number of leading zeros */ Chris@69: opus_int32 *frac_Q7 /* O the 7 bits right after the leading one */ Chris@69: ) Chris@69: { Chris@69: opus_int32 lzeros = silk_CLZ32(in); Chris@69: Chris@69: * lz = lzeros; Chris@69: * frac_Q7 = silk_ROR32(in, 24 - lzeros) & 0x7f; Chris@69: } Chris@69: Chris@69: /* Approximation of square root */ Chris@69: /* Accuracy: < +/- 10% for output values > 15 */ Chris@69: /* < +/- 2.5% for output values > 120 */ Chris@69: static OPUS_INLINE opus_int32 silk_SQRT_APPROX( opus_int32 x ) Chris@69: { Chris@69: opus_int32 y, lz, frac_Q7; Chris@69: Chris@69: if( x <= 0 ) { Chris@69: return 0; Chris@69: } Chris@69: Chris@69: silk_CLZ_FRAC(x, &lz, &frac_Q7); Chris@69: Chris@69: if( lz & 1 ) { Chris@69: y = 32768; Chris@69: } else { Chris@69: y = 46214; /* 46214 = sqrt(2) * 32768 */ Chris@69: } Chris@69: Chris@69: /* get scaling right */ Chris@69: y >>= silk_RSHIFT(lz, 1); Chris@69: Chris@69: /* increment using fractional part of input */ Chris@69: y = silk_SMLAWB(y, y, silk_SMULBB(213, frac_Q7)); Chris@69: Chris@69: return y; Chris@69: } Chris@69: Chris@69: /* Divide two int32 values and return result as int32 in a given Q-domain */ Chris@69: static OPUS_INLINE opus_int32 silk_DIV32_varQ( /* O returns a good approximation of "(a32 << Qres) / b32" */ Chris@69: const opus_int32 a32, /* I numerator (Q0) */ Chris@69: const opus_int32 b32, /* I denominator (Q0) */ Chris@69: const opus_int Qres /* I Q-domain of result (>= 0) */ Chris@69: ) Chris@69: { Chris@69: opus_int a_headrm, b_headrm, lshift; Chris@69: opus_int32 b32_inv, a32_nrm, b32_nrm, result; Chris@69: Chris@69: silk_assert( b32 != 0 ); Chris@69: silk_assert( Qres >= 0 ); Chris@69: Chris@69: /* Compute number of bits head room and normalize inputs */ Chris@69: a_headrm = silk_CLZ32( silk_abs(a32) ) - 1; Chris@69: a32_nrm = silk_LSHIFT(a32, a_headrm); /* Q: a_headrm */ Chris@69: b_headrm = silk_CLZ32( silk_abs(b32) ) - 1; Chris@69: b32_nrm = silk_LSHIFT(b32, b_headrm); /* Q: b_headrm */ Chris@69: Chris@69: /* Inverse of b32, with 14 bits of precision */ Chris@69: b32_inv = silk_DIV32_16( silk_int32_MAX >> 2, silk_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ Chris@69: Chris@69: /* First approximation */ Chris@69: result = silk_SMULWB(a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ Chris@69: Chris@69: /* Compute residual by subtracting product of denominator and first approximation */ Chris@69: /* It's OK to overflow because the final value of a32_nrm should always be small */ Chris@69: a32_nrm = silk_SUB32_ovflw(a32_nrm, silk_LSHIFT_ovflw( silk_SMMUL(b32_nrm, result), 3 )); /* Q: a_headrm */ Chris@69: Chris@69: /* Refinement */ Chris@69: result = silk_SMLAWB(result, a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ Chris@69: Chris@69: /* Convert to Qres domain */ Chris@69: lshift = 29 + a_headrm - b_headrm - Qres; Chris@69: if( lshift < 0 ) { Chris@69: return silk_LSHIFT_SAT32(result, -lshift); Chris@69: } else { Chris@69: if( lshift < 32){ Chris@69: return silk_RSHIFT(result, lshift); Chris@69: } else { Chris@69: /* Avoid undefined result */ Chris@69: return 0; Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: /* Invert int32 value and return result as int32 in a given Q-domain */ Chris@69: static OPUS_INLINE opus_int32 silk_INVERSE32_varQ( /* O returns a good approximation of "(1 << Qres) / b32" */ Chris@69: const opus_int32 b32, /* I denominator (Q0) */ Chris@69: const opus_int Qres /* I Q-domain of result (> 0) */ Chris@69: ) Chris@69: { Chris@69: opus_int b_headrm, lshift; Chris@69: opus_int32 b32_inv, b32_nrm, err_Q32, result; Chris@69: Chris@69: silk_assert( b32 != 0 ); Chris@69: silk_assert( Qres > 0 ); Chris@69: Chris@69: /* Compute number of bits head room and normalize input */ Chris@69: b_headrm = silk_CLZ32( silk_abs(b32) ) - 1; Chris@69: b32_nrm = silk_LSHIFT(b32, b_headrm); /* Q: b_headrm */ Chris@69: Chris@69: /* Inverse of b32, with 14 bits of precision */ Chris@69: b32_inv = silk_DIV32_16( silk_int32_MAX >> 2, silk_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ Chris@69: Chris@69: /* First approximation */ Chris@69: result = silk_LSHIFT(b32_inv, 16); /* Q: 61 - b_headrm */ Chris@69: Chris@69: /* Compute residual by subtracting product of denominator and first approximation from one */ Chris@69: err_Q32 = silk_LSHIFT( ((opus_int32)1<<29) - silk_SMULWB(b32_nrm, b32_inv), 3 ); /* Q32 */ Chris@69: Chris@69: /* Refinement */ Chris@69: result = silk_SMLAWW(result, err_Q32, b32_inv); /* Q: 61 - b_headrm */ Chris@69: Chris@69: /* Convert to Qres domain */ Chris@69: lshift = 61 - b_headrm - Qres; Chris@69: if( lshift <= 0 ) { Chris@69: return silk_LSHIFT_SAT32(result, -lshift); Chris@69: } else { Chris@69: if( lshift < 32){ Chris@69: return silk_RSHIFT(result, lshift); Chris@69: }else{ Chris@69: /* Avoid undefined result */ Chris@69: return 0; Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: #ifdef __cplusplus Chris@69: } Chris@69: #endif Chris@69: Chris@69: #endif /* SILK_FIX_INLINES_H */