annotate src/opus-1.3/celt/mathops.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 /* Copyright (c) 2002-2008 Jean-Marc Valin
Chris@69 2 Copyright (c) 2007-2008 CSIRO
Chris@69 3 Copyright (c) 2007-2009 Xiph.Org Foundation
Chris@69 4 Written by Jean-Marc Valin */
Chris@69 5 /**
Chris@69 6 @file mathops.h
Chris@69 7 @brief Various math functions
Chris@69 8 */
Chris@69 9 /*
Chris@69 10 Redistribution and use in source and binary forms, with or without
Chris@69 11 modification, are permitted provided that the following conditions
Chris@69 12 are met:
Chris@69 13
Chris@69 14 - Redistributions of source code must retain the above copyright
Chris@69 15 notice, this list of conditions and the following disclaimer.
Chris@69 16
Chris@69 17 - Redistributions in binary form must reproduce the above copyright
Chris@69 18 notice, this list of conditions and the following disclaimer in the
Chris@69 19 documentation and/or other materials provided with the distribution.
Chris@69 20
Chris@69 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@69 22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@69 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@69 24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
Chris@69 25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@69 26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@69 27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Chris@69 28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Chris@69 29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Chris@69 30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@69 31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@69 32 */
Chris@69 33
Chris@69 34 #ifndef MATHOPS_H
Chris@69 35 #define MATHOPS_H
Chris@69 36
Chris@69 37 #include "arch.h"
Chris@69 38 #include "entcode.h"
Chris@69 39 #include "os_support.h"
Chris@69 40
Chris@69 41 #define PI 3.141592653f
Chris@69 42
Chris@69 43 /* Multiplies two 16-bit fractional values. Bit-exactness of this macro is important */
Chris@69 44 #define FRAC_MUL16(a,b) ((16384+((opus_int32)(opus_int16)(a)*(opus_int16)(b)))>>15)
Chris@69 45
Chris@69 46 unsigned isqrt32(opus_uint32 _val);
Chris@69 47
Chris@69 48 /* CELT doesn't need it for fixed-point, by analysis.c does. */
Chris@69 49 #if !defined(FIXED_POINT) || defined(ANALYSIS_C)
Chris@69 50 #define cA 0.43157974f
Chris@69 51 #define cB 0.67848403f
Chris@69 52 #define cC 0.08595542f
Chris@69 53 #define cE ((float)PI/2)
Chris@69 54 static OPUS_INLINE float fast_atan2f(float y, float x) {
Chris@69 55 float x2, y2;
Chris@69 56 x2 = x*x;
Chris@69 57 y2 = y*y;
Chris@69 58 /* For very small values, we don't care about the answer, so
Chris@69 59 we can just return 0. */
Chris@69 60 if (x2 + y2 < 1e-18f)
Chris@69 61 {
Chris@69 62 return 0;
Chris@69 63 }
Chris@69 64 if(x2<y2){
Chris@69 65 float den = (y2 + cB*x2) * (y2 + cC*x2);
Chris@69 66 return -x*y*(y2 + cA*x2) / den + (y<0 ? -cE : cE);
Chris@69 67 }else{
Chris@69 68 float den = (x2 + cB*y2) * (x2 + cC*y2);
Chris@69 69 return x*y*(x2 + cA*y2) / den + (y<0 ? -cE : cE) - (x*y<0 ? -cE : cE);
Chris@69 70 }
Chris@69 71 }
Chris@69 72 #undef cA
Chris@69 73 #undef cB
Chris@69 74 #undef cC
Chris@69 75 #undef cE
Chris@69 76 #endif
Chris@69 77
Chris@69 78
Chris@69 79 #ifndef OVERRIDE_CELT_MAXABS16
Chris@69 80 static OPUS_INLINE opus_val32 celt_maxabs16(const opus_val16 *x, int len)
Chris@69 81 {
Chris@69 82 int i;
Chris@69 83 opus_val16 maxval = 0;
Chris@69 84 opus_val16 minval = 0;
Chris@69 85 for (i=0;i<len;i++)
Chris@69 86 {
Chris@69 87 maxval = MAX16(maxval, x[i]);
Chris@69 88 minval = MIN16(minval, x[i]);
Chris@69 89 }
Chris@69 90 return MAX32(EXTEND32(maxval),-EXTEND32(minval));
Chris@69 91 }
Chris@69 92 #endif
Chris@69 93
Chris@69 94 #ifndef OVERRIDE_CELT_MAXABS32
Chris@69 95 #ifdef FIXED_POINT
Chris@69 96 static OPUS_INLINE opus_val32 celt_maxabs32(const opus_val32 *x, int len)
Chris@69 97 {
Chris@69 98 int i;
Chris@69 99 opus_val32 maxval = 0;
Chris@69 100 opus_val32 minval = 0;
Chris@69 101 for (i=0;i<len;i++)
Chris@69 102 {
Chris@69 103 maxval = MAX32(maxval, x[i]);
Chris@69 104 minval = MIN32(minval, x[i]);
Chris@69 105 }
Chris@69 106 return MAX32(maxval, -minval);
Chris@69 107 }
Chris@69 108 #else
Chris@69 109 #define celt_maxabs32(x,len) celt_maxabs16(x,len)
Chris@69 110 #endif
Chris@69 111 #endif
Chris@69 112
Chris@69 113
Chris@69 114 #ifndef FIXED_POINT
Chris@69 115
Chris@69 116 #define celt_sqrt(x) ((float)sqrt(x))
Chris@69 117 #define celt_rsqrt(x) (1.f/celt_sqrt(x))
Chris@69 118 #define celt_rsqrt_norm(x) (celt_rsqrt(x))
Chris@69 119 #define celt_cos_norm(x) ((float)cos((.5f*PI)*(x)))
Chris@69 120 #define celt_rcp(x) (1.f/(x))
Chris@69 121 #define celt_div(a,b) ((a)/(b))
Chris@69 122 #define frac_div32(a,b) ((float)(a)/(b))
Chris@69 123
Chris@69 124 #ifdef FLOAT_APPROX
Chris@69 125
Chris@69 126 /* Note: This assumes radix-2 floating point with the exponent at bits 23..30 and an offset of 127
Chris@69 127 denorm, +/- inf and NaN are *not* handled */
Chris@69 128
Chris@69 129 /** Base-2 log approximation (log2(x)). */
Chris@69 130 static OPUS_INLINE float celt_log2(float x)
Chris@69 131 {
Chris@69 132 int integer;
Chris@69 133 float frac;
Chris@69 134 union {
Chris@69 135 float f;
Chris@69 136 opus_uint32 i;
Chris@69 137 } in;
Chris@69 138 in.f = x;
Chris@69 139 integer = (in.i>>23)-127;
Chris@69 140 in.i -= integer<<23;
Chris@69 141 frac = in.f - 1.5f;
Chris@69 142 frac = -0.41445418f + frac*(0.95909232f
Chris@69 143 + frac*(-0.33951290f + frac*0.16541097f));
Chris@69 144 return 1+integer+frac;
Chris@69 145 }
Chris@69 146
Chris@69 147 /** Base-2 exponential approximation (2^x). */
Chris@69 148 static OPUS_INLINE float celt_exp2(float x)
Chris@69 149 {
Chris@69 150 int integer;
Chris@69 151 float frac;
Chris@69 152 union {
Chris@69 153 float f;
Chris@69 154 opus_uint32 i;
Chris@69 155 } res;
Chris@69 156 integer = floor(x);
Chris@69 157 if (integer < -50)
Chris@69 158 return 0;
Chris@69 159 frac = x-integer;
Chris@69 160 /* K0 = 1, K1 = log(2), K2 = 3-4*log(2), K3 = 3*log(2) - 2 */
Chris@69 161 res.f = 0.99992522f + frac * (0.69583354f
Chris@69 162 + frac * (0.22606716f + 0.078024523f*frac));
Chris@69 163 res.i = (res.i + (integer<<23)) & 0x7fffffff;
Chris@69 164 return res.f;
Chris@69 165 }
Chris@69 166
Chris@69 167 #else
Chris@69 168 #define celt_log2(x) ((float)(1.442695040888963387*log(x)))
Chris@69 169 #define celt_exp2(x) ((float)exp(0.6931471805599453094*(x)))
Chris@69 170 #endif
Chris@69 171
Chris@69 172 #endif
Chris@69 173
Chris@69 174 #ifdef FIXED_POINT
Chris@69 175
Chris@69 176 #include "os_support.h"
Chris@69 177
Chris@69 178 #ifndef OVERRIDE_CELT_ILOG2
Chris@69 179 /** Integer log in base2. Undefined for zero and negative numbers */
Chris@69 180 static OPUS_INLINE opus_int16 celt_ilog2(opus_int32 x)
Chris@69 181 {
Chris@69 182 celt_sig_assert(x>0);
Chris@69 183 return EC_ILOG(x)-1;
Chris@69 184 }
Chris@69 185 #endif
Chris@69 186
Chris@69 187
Chris@69 188 /** Integer log in base2. Defined for zero, but not for negative numbers */
Chris@69 189 static OPUS_INLINE opus_int16 celt_zlog2(opus_val32 x)
Chris@69 190 {
Chris@69 191 return x <= 0 ? 0 : celt_ilog2(x);
Chris@69 192 }
Chris@69 193
Chris@69 194 opus_val16 celt_rsqrt_norm(opus_val32 x);
Chris@69 195
Chris@69 196 opus_val32 celt_sqrt(opus_val32 x);
Chris@69 197
Chris@69 198 opus_val16 celt_cos_norm(opus_val32 x);
Chris@69 199
Chris@69 200 /** Base-2 logarithm approximation (log2(x)). (Q14 input, Q10 output) */
Chris@69 201 static OPUS_INLINE opus_val16 celt_log2(opus_val32 x)
Chris@69 202 {
Chris@69 203 int i;
Chris@69 204 opus_val16 n, frac;
Chris@69 205 /* -0.41509302963303146, 0.9609890551383969, -0.31836011537636605,
Chris@69 206 0.15530808010959576, -0.08556153059057618 */
Chris@69 207 static const opus_val16 C[5] = {-6801+(1<<(13-DB_SHIFT)), 15746, -5217, 2545, -1401};
Chris@69 208 if (x==0)
Chris@69 209 return -32767;
Chris@69 210 i = celt_ilog2(x);
Chris@69 211 n = VSHR32(x,i-15)-32768-16384;
Chris@69 212 frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2], MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, C[4]))))))));
Chris@69 213 return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT);
Chris@69 214 }
Chris@69 215
Chris@69 216 /*
Chris@69 217 K0 = 1
Chris@69 218 K1 = log(2)
Chris@69 219 K2 = 3-4*log(2)
Chris@69 220 K3 = 3*log(2) - 2
Chris@69 221 */
Chris@69 222 #define D0 16383
Chris@69 223 #define D1 22804
Chris@69 224 #define D2 14819
Chris@69 225 #define D3 10204
Chris@69 226
Chris@69 227 static OPUS_INLINE opus_val32 celt_exp2_frac(opus_val16 x)
Chris@69 228 {
Chris@69 229 opus_val16 frac;
Chris@69 230 frac = SHL16(x, 4);
Chris@69 231 return ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 , MULT16_16_Q15(D3,frac))))));
Chris@69 232 }
Chris@69 233 /** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */
Chris@69 234 static OPUS_INLINE opus_val32 celt_exp2(opus_val16 x)
Chris@69 235 {
Chris@69 236 int integer;
Chris@69 237 opus_val16 frac;
Chris@69 238 integer = SHR16(x,10);
Chris@69 239 if (integer>14)
Chris@69 240 return 0x7f000000;
Chris@69 241 else if (integer < -15)
Chris@69 242 return 0;
Chris@69 243 frac = celt_exp2_frac(x-SHL16(integer,10));
Chris@69 244 return VSHR32(EXTEND32(frac), -integer-2);
Chris@69 245 }
Chris@69 246
Chris@69 247 opus_val32 celt_rcp(opus_val32 x);
Chris@69 248
Chris@69 249 #define celt_div(a,b) MULT32_32_Q31((opus_val32)(a),celt_rcp(b))
Chris@69 250
Chris@69 251 opus_val32 frac_div32(opus_val32 a, opus_val32 b);
Chris@69 252
Chris@69 253 #define M1 32767
Chris@69 254 #define M2 -21
Chris@69 255 #define M3 -11943
Chris@69 256 #define M4 4936
Chris@69 257
Chris@69 258 /* Atan approximation using a 4th order polynomial. Input is in Q15 format
Chris@69 259 and normalized by pi/4. Output is in Q15 format */
Chris@69 260 static OPUS_INLINE opus_val16 celt_atan01(opus_val16 x)
Chris@69 261 {
Chris@69 262 return MULT16_16_P15(x, ADD32(M1, MULT16_16_P15(x, ADD32(M2, MULT16_16_P15(x, ADD32(M3, MULT16_16_P15(M4, x)))))));
Chris@69 263 }
Chris@69 264
Chris@69 265 #undef M1
Chris@69 266 #undef M2
Chris@69 267 #undef M3
Chris@69 268 #undef M4
Chris@69 269
Chris@69 270 /* atan2() approximation valid for positive input values */
Chris@69 271 static OPUS_INLINE opus_val16 celt_atan2p(opus_val16 y, opus_val16 x)
Chris@69 272 {
Chris@69 273 if (y < x)
Chris@69 274 {
Chris@69 275 opus_val32 arg;
Chris@69 276 arg = celt_div(SHL32(EXTEND32(y),15),x);
Chris@69 277 if (arg >= 32767)
Chris@69 278 arg = 32767;
Chris@69 279 return SHR16(celt_atan01(EXTRACT16(arg)),1);
Chris@69 280 } else {
Chris@69 281 opus_val32 arg;
Chris@69 282 arg = celt_div(SHL32(EXTEND32(x),15),y);
Chris@69 283 if (arg >= 32767)
Chris@69 284 arg = 32767;
Chris@69 285 return 25736-SHR16(celt_atan01(EXTRACT16(arg)),1);
Chris@69 286 }
Chris@69 287 }
Chris@69 288
Chris@69 289 #endif /* FIXED_POINT */
Chris@69 290 #endif /* MATHOPS_H */