annotate src/opus-1.3/silk/Inlines.h @ 69:7aeed7906520

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