Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /*********************************************************************** |
michael@0 | 2 | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
michael@0 | 3 | Redistribution and use in source and binary forms, with or without |
michael@0 | 4 | modification, are permitted provided that the following conditions |
michael@0 | 5 | are met: |
michael@0 | 6 | - Redistributions of source code must retain the above copyright notice, |
michael@0 | 7 | this list of conditions and the following disclaimer. |
michael@0 | 8 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 10 | documentation and/or other materials provided with the distribution. |
michael@0 | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
michael@0 | 12 | names of specific contributors, may be used to endorse or promote |
michael@0 | 13 | products derived from this software without specific prior written |
michael@0 | 14 | permission. |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
michael@0 | 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
michael@0 | 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
michael@0 | 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
michael@0 | 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
michael@0 | 22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
michael@0 | 23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
michael@0 | 25 | POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | ***********************************************************************/ |
michael@0 | 27 | |
michael@0 | 28 | /*! \file silk_Inlines.h |
michael@0 | 29 | * \brief silk_Inlines.h defines OPUS_INLINE signal processing functions. |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #ifndef SILK_FIX_INLINES_H |
michael@0 | 33 | #define SILK_FIX_INLINES_H |
michael@0 | 34 | |
michael@0 | 35 | #ifdef __cplusplus |
michael@0 | 36 | extern "C" |
michael@0 | 37 | { |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | /* count leading zeros of opus_int64 */ |
michael@0 | 41 | static OPUS_INLINE opus_int32 silk_CLZ64( opus_int64 in ) |
michael@0 | 42 | { |
michael@0 | 43 | opus_int32 in_upper; |
michael@0 | 44 | |
michael@0 | 45 | in_upper = (opus_int32)silk_RSHIFT64(in, 32); |
michael@0 | 46 | if (in_upper == 0) { |
michael@0 | 47 | /* Search in the lower 32 bits */ |
michael@0 | 48 | return 32 + silk_CLZ32( (opus_int32) in ); |
michael@0 | 49 | } else { |
michael@0 | 50 | /* Search in the upper 32 bits */ |
michael@0 | 51 | return silk_CLZ32( in_upper ); |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | /* get number of leading zeros and fractional part (the bits right after the leading one */ |
michael@0 | 56 | static OPUS_INLINE void silk_CLZ_FRAC( |
michael@0 | 57 | opus_int32 in, /* I input */ |
michael@0 | 58 | opus_int32 *lz, /* O number of leading zeros */ |
michael@0 | 59 | opus_int32 *frac_Q7 /* O the 7 bits right after the leading one */ |
michael@0 | 60 | ) |
michael@0 | 61 | { |
michael@0 | 62 | opus_int32 lzeros = silk_CLZ32(in); |
michael@0 | 63 | |
michael@0 | 64 | * lz = lzeros; |
michael@0 | 65 | * frac_Q7 = silk_ROR32(in, 24 - lzeros) & 0x7f; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | /* Approximation of square root */ |
michael@0 | 69 | /* Accuracy: < +/- 10% for output values > 15 */ |
michael@0 | 70 | /* < +/- 2.5% for output values > 120 */ |
michael@0 | 71 | static OPUS_INLINE opus_int32 silk_SQRT_APPROX( opus_int32 x ) |
michael@0 | 72 | { |
michael@0 | 73 | opus_int32 y, lz, frac_Q7; |
michael@0 | 74 | |
michael@0 | 75 | if( x <= 0 ) { |
michael@0 | 76 | return 0; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | silk_CLZ_FRAC(x, &lz, &frac_Q7); |
michael@0 | 80 | |
michael@0 | 81 | if( lz & 1 ) { |
michael@0 | 82 | y = 32768; |
michael@0 | 83 | } else { |
michael@0 | 84 | y = 46214; /* 46214 = sqrt(2) * 32768 */ |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /* get scaling right */ |
michael@0 | 88 | y >>= silk_RSHIFT(lz, 1); |
michael@0 | 89 | |
michael@0 | 90 | /* increment using fractional part of input */ |
michael@0 | 91 | y = silk_SMLAWB(y, y, silk_SMULBB(213, frac_Q7)); |
michael@0 | 92 | |
michael@0 | 93 | return y; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | /* Divide two int32 values and return result as int32 in a given Q-domain */ |
michael@0 | 97 | static OPUS_INLINE opus_int32 silk_DIV32_varQ( /* O returns a good approximation of "(a32 << Qres) / b32" */ |
michael@0 | 98 | const opus_int32 a32, /* I numerator (Q0) */ |
michael@0 | 99 | const opus_int32 b32, /* I denominator (Q0) */ |
michael@0 | 100 | const opus_int Qres /* I Q-domain of result (>= 0) */ |
michael@0 | 101 | ) |
michael@0 | 102 | { |
michael@0 | 103 | opus_int a_headrm, b_headrm, lshift; |
michael@0 | 104 | opus_int32 b32_inv, a32_nrm, b32_nrm, result; |
michael@0 | 105 | |
michael@0 | 106 | silk_assert( b32 != 0 ); |
michael@0 | 107 | silk_assert( Qres >= 0 ); |
michael@0 | 108 | |
michael@0 | 109 | /* Compute number of bits head room and normalize inputs */ |
michael@0 | 110 | a_headrm = silk_CLZ32( silk_abs(a32) ) - 1; |
michael@0 | 111 | a32_nrm = silk_LSHIFT(a32, a_headrm); /* Q: a_headrm */ |
michael@0 | 112 | b_headrm = silk_CLZ32( silk_abs(b32) ) - 1; |
michael@0 | 113 | b32_nrm = silk_LSHIFT(b32, b_headrm); /* Q: b_headrm */ |
michael@0 | 114 | |
michael@0 | 115 | /* Inverse of b32, with 14 bits of precision */ |
michael@0 | 116 | b32_inv = silk_DIV32_16( silk_int32_MAX >> 2, silk_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ |
michael@0 | 117 | |
michael@0 | 118 | /* First approximation */ |
michael@0 | 119 | result = silk_SMULWB(a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ |
michael@0 | 120 | |
michael@0 | 121 | /* Compute residual by subtracting product of denominator and first approximation */ |
michael@0 | 122 | /* It's OK to overflow because the final value of a32_nrm should always be small */ |
michael@0 | 123 | a32_nrm = silk_SUB32_ovflw(a32_nrm, silk_LSHIFT_ovflw( silk_SMMUL(b32_nrm, result), 3 )); /* Q: a_headrm */ |
michael@0 | 124 | |
michael@0 | 125 | /* Refinement */ |
michael@0 | 126 | result = silk_SMLAWB(result, a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */ |
michael@0 | 127 | |
michael@0 | 128 | /* Convert to Qres domain */ |
michael@0 | 129 | lshift = 29 + a_headrm - b_headrm - Qres; |
michael@0 | 130 | if( lshift < 0 ) { |
michael@0 | 131 | return silk_LSHIFT_SAT32(result, -lshift); |
michael@0 | 132 | } else { |
michael@0 | 133 | if( lshift < 32){ |
michael@0 | 134 | return silk_RSHIFT(result, lshift); |
michael@0 | 135 | } else { |
michael@0 | 136 | /* Avoid undefined result */ |
michael@0 | 137 | return 0; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /* Invert int32 value and return result as int32 in a given Q-domain */ |
michael@0 | 143 | static OPUS_INLINE opus_int32 silk_INVERSE32_varQ( /* O returns a good approximation of "(1 << Qres) / b32" */ |
michael@0 | 144 | const opus_int32 b32, /* I denominator (Q0) */ |
michael@0 | 145 | const opus_int Qres /* I Q-domain of result (> 0) */ |
michael@0 | 146 | ) |
michael@0 | 147 | { |
michael@0 | 148 | opus_int b_headrm, lshift; |
michael@0 | 149 | opus_int32 b32_inv, b32_nrm, err_Q32, result; |
michael@0 | 150 | |
michael@0 | 151 | silk_assert( b32 != 0 ); |
michael@0 | 152 | silk_assert( Qres > 0 ); |
michael@0 | 153 | |
michael@0 | 154 | /* Compute number of bits head room and normalize input */ |
michael@0 | 155 | b_headrm = silk_CLZ32( silk_abs(b32) ) - 1; |
michael@0 | 156 | b32_nrm = silk_LSHIFT(b32, b_headrm); /* Q: b_headrm */ |
michael@0 | 157 | |
michael@0 | 158 | /* Inverse of b32, with 14 bits of precision */ |
michael@0 | 159 | b32_inv = silk_DIV32_16( silk_int32_MAX >> 2, silk_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */ |
michael@0 | 160 | |
michael@0 | 161 | /* First approximation */ |
michael@0 | 162 | result = silk_LSHIFT(b32_inv, 16); /* Q: 61 - b_headrm */ |
michael@0 | 163 | |
michael@0 | 164 | /* Compute residual by subtracting product of denominator and first approximation from one */ |
michael@0 | 165 | err_Q32 = silk_LSHIFT( ((opus_int32)1<<29) - silk_SMULWB(b32_nrm, b32_inv), 3 ); /* Q32 */ |
michael@0 | 166 | |
michael@0 | 167 | /* Refinement */ |
michael@0 | 168 | result = silk_SMLAWW(result, err_Q32, b32_inv); /* Q: 61 - b_headrm */ |
michael@0 | 169 | |
michael@0 | 170 | /* Convert to Qres domain */ |
michael@0 | 171 | lshift = 61 - b_headrm - Qres; |
michael@0 | 172 | if( lshift <= 0 ) { |
michael@0 | 173 | return silk_LSHIFT_SAT32(result, -lshift); |
michael@0 | 174 | } else { |
michael@0 | 175 | if( lshift < 32){ |
michael@0 | 176 | return silk_RSHIFT(result, lshift); |
michael@0 | 177 | }else{ |
michael@0 | 178 | /* Avoid undefined result */ |
michael@0 | 179 | return 0; |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | #ifdef __cplusplus |
michael@0 | 185 | } |
michael@0 | 186 | #endif |
michael@0 | 187 | |
michael@0 | 188 | #endif /* SILK_FIX_INLINES_H */ |