arch.h (12504B)
1 /* Copyright (c) 2003-2008 Jean-Marc Valin 2 Copyright (c) 2007-2008 CSIRO 3 Copyright (c) 2007-2009 Xiph.Org Foundation 4 Written by Jean-Marc Valin */ 5 /** 6 @file arch.h 7 @brief Various architecture definitions for CELT 8 */ 9 /* 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 - Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 - Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in the 19 documentation and/or other materials provided with the distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #ifndef ARCH_H 35 #define ARCH_H 36 37 #include "opus_types.h" 38 #include "opus_defines.h" 39 40 # if !defined(__GNUC_PREREQ) 41 # if defined(__GNUC__)&&defined(__GNUC_MINOR__) 42 # define __GNUC_PREREQ(_maj,_min) \ 43 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min)) 44 # else 45 # define __GNUC_PREREQ(_maj,_min) 0 46 # endif 47 # endif 48 49 #if OPUS_GNUC_PREREQ(3, 0) 50 #define opus_likely(x) (__builtin_expect(!!(x), 1)) 51 #define opus_unlikely(x) (__builtin_expect(!!(x), 0)) 52 #else 53 #define opus_likely(x) (!!(x)) 54 #define opus_unlikely(x) (!!(x)) 55 #endif 56 57 #define CELT_SIG_SCALE 32768.f 58 59 #define CELT_FATAL(str) celt_fatal(str, __FILE__, __LINE__) 60 61 #if defined(ENABLE_ASSERTIONS) || defined(ENABLE_HARDENING) 62 #ifdef __GNUC__ 63 __attribute__((noreturn)) 64 #endif 65 void celt_fatal(const char *str, const char *file, int line); 66 67 #if defined(CELT_C) && !defined(OVERRIDE_celt_fatal) 68 #include <stdio.h> 69 #include <stdlib.h> 70 #ifdef __GNUC__ 71 __attribute__((noreturn)) 72 #endif 73 void celt_fatal(const char *str, const char *file, int line) 74 { 75 fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str); 76 #if defined(_MSC_VER) 77 _set_abort_behavior( 0, _WRITE_ABORT_MSG); 78 #endif 79 abort(); 80 } 81 #endif 82 83 #define celt_assert(cond) {if (!(cond)) {CELT_FATAL("assertion failed: " #cond);}} 84 #define celt_assert2(cond, message) {if (!(cond)) {CELT_FATAL("assertion failed: " #cond "\n" message);}} 85 #define MUST_SUCCEED(call) celt_assert((call) == OPUS_OK) 86 #else 87 #define celt_assert(cond) ((void)(cond)) 88 #define celt_assert2(cond, message) ((void)(cond)) 89 #define MUST_SUCCEED(call) do {if((call) != OPUS_OK) {RESTORE_STACK; return OPUS_INTERNAL_ERROR;} } while (0) 90 #endif 91 92 #if defined(ENABLE_ASSERTIONS) 93 #define celt_sig_assert(cond) {if (!(cond)) {CELT_FATAL("signal assertion failed: " #cond);}} 94 #else 95 #define celt_sig_assert(cond) ((void)(cond)) 96 #endif 97 98 #define IMUL32(a,b) ((a)*(b)) 99 100 #define MIN16(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 16-bit value. */ 101 #define MAX16(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 16-bit value. */ 102 #define MIN32(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 32-bit value. */ 103 #define MAX32(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 32-bit value. */ 104 #define IMIN(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum int value. */ 105 #define IMAX(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum int value. */ 106 #define FMIN(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum float value. */ 107 #define FMAX(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum float value. */ 108 #define UADD32(a,b) ((a)+(b)) 109 #define USUB32(a,b) ((a)-(b)) 110 #define MAXG(a,b) MAX32(a, b) 111 #define MING(a,b) MIN32(a, b) 112 113 /* Throughout the code, we use the following scaling for signals: 114 FLOAT: used for float API, normalized to +/-1. 115 INT16: used for 16-bit API, normalized to +/- 32768 116 RES: internal Opus resolution, defined as +/-1. in float builds, or either 16-bit or 24-bit int for fixed-point builds 117 SIG: internal CELT resolution: defined as +/- 32768. in float builds, or Q27 in fixed-point builds (int16 shifted by 12) 118 */ 119 120 121 /* Set this if opus_int64 is a native type of the CPU. */ 122 /* Assume that all LP64 architectures have fast 64-bit types; also x86_64 123 (which can be ILP32 for x32) and Win64 (which is LLP64). */ 124 #if defined(__x86_64__) || defined(__LP64__) || defined(_WIN64) || defined (__mips) 125 #define OPUS_FAST_INT64 1 126 #else 127 #define OPUS_FAST_INT64 0 128 #endif 129 130 #ifdef FIXED_POINT 131 #define ARG_FIXED(arg) , arg 132 #else 133 #define ARG_FIXED(arg) 134 #endif 135 136 #define PRINT_MIPS(file) 137 138 #ifdef FIXED_POINT 139 140 typedef opus_int16 opus_val16; 141 typedef opus_int32 opus_val32; 142 typedef opus_int64 opus_val64; 143 144 typedef opus_val32 celt_sig; 145 typedef opus_val32 celt_norm; 146 typedef opus_val32 celt_ener; 147 typedef opus_val32 celt_glog; 148 149 #ifdef ENABLE_RES24 150 typedef opus_val32 opus_res; 151 #define RES_SHIFT 8 152 #define SIG2RES(a) PSHR32(a, SIG_SHIFT-RES_SHIFT) 153 #define RES2INT16(a) SAT16(PSHR32(a, RES_SHIFT)) 154 #define RES2INT24(a) (a) 155 #define RES2FLOAT(a) ((1.f/32768.f/256.f)*(a)) 156 #define INT16TORES(a) SHL32(EXTEND32(a), RES_SHIFT) 157 #define INT24TORES(a) (a) 158 #define ADD_RES(a, b) ADD32(a, b) 159 #define FLOAT2RES(a) FLOAT2INT24(a) 160 #define RES2SIG(a) SHL32((a), SIG_SHIFT-RES_SHIFT) 161 #define MULT16_RES_Q15(a,b) MULT16_32_Q15(a,b) 162 #define MAX_ENCODING_DEPTH 24 163 #else 164 typedef opus_val16 opus_res; 165 #define RES_SHIFT 0 166 #define SIG2RES(a) SIG2WORD16(a) 167 #define RES2INT16(a) (a) 168 #define RES2INT24(a) SHL32(EXTEND32(a), 8) 169 #define RES2FLOAT(a) ((1.f/32768.f)*(a)) 170 #define INT16TORES(a) (a) 171 #define INT24TORES(a) SAT16(PSHR32(a, 8)) 172 #define ADD_RES(a, b) SAT16(ADD32((a), (b))); 173 #define FLOAT2RES(a) FLOAT2INT16(a) 174 #define RES2SIG(a) SHL32(EXTEND32(a), SIG_SHIFT) 175 #define MULT16_RES_Q15(a,b) MULT16_16_Q15(a,b) 176 #define MAX_ENCODING_DEPTH 16 177 #endif 178 179 #define RES2VAL16(a) RES2INT16(a) 180 #define INT16TOSIG(a) SHL32(EXTEND32(a), SIG_SHIFT) 181 #define INT24TOSIG(a) SHL32(a, SIG_SHIFT-8) 182 183 #define NORM_SHIFT 24 184 #ifdef ENABLE_QEXT 185 typedef opus_val32 celt_coef; 186 #define COEF_ONE Q31ONE 187 #define MULT_COEF_32(a, b) MULT32_32_P31(a,b) 188 #define MAC_COEF_32_ARM(c, a, b) ADD32((c), MULT32_32_Q32(a,b)) 189 #define MULT_COEF(a, b) MULT32_32_Q31(a,b) 190 #define MULT_COEF_TAPS(a, b) SHL32(MULT16_16(a,b), 1) 191 #define COEF2VAL16(x) EXTRACT16(SHR32(x, 16)) 192 #else 193 typedef opus_val16 celt_coef; 194 #define COEF_ONE Q15ONE 195 #define MULT_COEF_32(a, b) MULT16_32_Q15(a,b) 196 #define MAC_COEF_32_ARM(a, b, c) MAC16_32_Q16(a,b,c) 197 #define MULT_COEF(a, b) MULT16_16_Q15(a,b) 198 #define MULT_COEF_TAPS(a, b) MULT16_16_P15(a,b) 199 #define COEF2VAL16(x) (x) 200 #endif 201 202 #define celt_isnan(x) 0 203 204 #define Q15ONE 32767 205 #define Q31ONE 2147483647 206 207 #define SIG_SHIFT 12 208 /* Safe saturation value for 32-bit signals. We need to make sure that we can 209 add two sig values and that the first stages of the MDCT don't cause an overflow. 210 The most constraining is the ARM_ASM comb filter where we shift left by one 211 and then add two values. Because of that, we use 2^29-1. SIG_SAT must be large 212 enough to fit a full-scale high-freq tone through the prefilter and comb filter, 213 meaning 1.85*1.75*2^(15+SIG_SHIFT) = 434529895. 214 so the limit should be about 2^31*sqrt(.5). */ 215 #define SIG_SAT (536870911) 216 217 #define NORM_SCALING (1<<NORM_SHIFT) 218 219 #define DB_SHIFT 24 220 221 #define EPSILON 1 222 #define VERY_SMALL 0 223 #define VERY_LARGE16 ((opus_val16)32767) 224 #define Q15_ONE ((opus_val16)32767) 225 226 227 #define ABS16(x) ((x) < 0 ? (-(x)) : (x)) 228 #define ABS32(x) ((x) < 0 ? (-(x)) : (x)) 229 230 static OPUS_INLINE opus_int16 SAT16(opus_int32 x) { 231 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x; 232 } 233 234 #ifdef FIXED_DEBUG 235 #include "fixed_debug.h" 236 #else 237 238 #include "fixed_generic.h" 239 240 #ifdef OPUS_ARM_PRESUME_AARCH64_NEON_INTR 241 #include "arm/fixed_arm64.h" 242 #elif defined (OPUS_ARM_INLINE_EDSP) 243 #include "arm/fixed_armv5e.h" 244 #elif defined (OPUS_ARM_INLINE_ASM) 245 #include "arm/fixed_armv4.h" 246 #elif defined (BFIN_ASM) 247 #include "fixed_bfin.h" 248 #elif defined (TI_C5X_ASM) 249 #include "fixed_c5x.h" 250 #elif defined (TI_C6X_ASM) 251 #include "fixed_c6x.h" 252 #endif 253 254 #endif 255 256 #else /* FIXED_POINT */ 257 258 typedef float opus_val16; 259 typedef float opus_val32; 260 typedef float opus_val64; 261 262 typedef float celt_sig; 263 typedef float celt_norm; 264 typedef float celt_ener; 265 typedef float celt_glog; 266 267 typedef float opus_res; 268 typedef float celt_coef; 269 270 #ifdef FLOAT_APPROX 271 /* This code should reliably detect NaN/inf even when -ffast-math is used. 272 Assumes IEEE 754 format. */ 273 static OPUS_INLINE int celt_isnan(float x) 274 { 275 union {float f; opus_uint32 i;} in; 276 in.f = x; 277 return ((in.i>>23)&0xFF)==0xFF && (in.i&0x007FFFFF)!=0; 278 } 279 #else 280 #ifdef __FAST_MATH__ 281 #error Cannot build libopus with -ffast-math unless FLOAT_APPROX is defined. This could result in crashes on extreme (e.g. NaN) input 282 #endif 283 #define celt_isnan(x) ((x)!=(x)) 284 #endif 285 286 #define Q15ONE 1.0f 287 #define Q31ONE 1.0f 288 #define COEF_ONE 1.0f 289 #define COEF2VAL16(x) (x) 290 291 #define NORM_SCALING 1.f 292 293 #define EPSILON 1e-15f 294 #define VERY_SMALL 1e-30f 295 #define VERY_LARGE16 1e15f 296 #define Q15_ONE ((opus_val16)1.f) 297 298 /* This appears to be the same speed as C99's fabsf() but it's more portable. */ 299 #define ABS16(x) ((float)fabs(x)) 300 #define ABS32(x) ((float)fabs(x)) 301 302 #define QCONST16(x,bits) (x) 303 #define QCONST32(x,bits) (x) 304 #define GCONST(x) (x) 305 306 #define NEG16(x) (-(x)) 307 #define NEG32(x) (-(x)) 308 #define NEG32_ovflw(x) (-(x)) 309 #define EXTRACT16(x) (x) 310 #define EXTEND32(x) (x) 311 #define SHR16(a,shift) (a) 312 #define SHL16(a,shift) (a) 313 #define SHR32(a,shift) (a) 314 #define SHL32(a,shift) (a) 315 #define PSHR32(a,shift) (a) 316 #define VSHR32(a,shift) (a) 317 318 #define SHR64(a,shift) (a) 319 320 #define PSHR(a,shift) (a) 321 #define SHR(a,shift) (a) 322 #define SHL(a,shift) (a) 323 #define SATURATE(x,a) (x) 324 #define SATURATE16(x) (x) 325 326 #define ROUND16(a,shift) (a) 327 #define SROUND16(a,shift) (a) 328 #define HALF16(x) (.5f*(x)) 329 #define HALF32(x) (.5f*(x)) 330 331 #define ADD16(a,b) ((a)+(b)) 332 #define SUB16(a,b) ((a)-(b)) 333 #define ADD32(a,b) ((a)+(b)) 334 #define SUB32(a,b) ((a)-(b)) 335 #define ADD32_ovflw(a,b) ((a)+(b)) 336 #define SUB32_ovflw(a,b) ((a)-(b)) 337 #define SHL32_ovflw(a,shift) (a) 338 #define PSHR32_ovflw(a,shift) (a) 339 340 #define MULT16_16_16(a,b) ((a)*(b)) 341 #define MULT16_16(a,b) ((opus_val32)(a)*(opus_val32)(b)) 342 #define MAC16_16(c,a,b) ((c)+(opus_val32)(a)*(opus_val32)(b)) 343 344 #define MULT16_32_Q15(a,b) ((a)*(b)) 345 #define MULT16_32_Q16(a,b) ((a)*(b)) 346 347 #define MULT32_32_Q16(a,b) ((a)*(b)) 348 #define MULT32_32_Q31(a,b) ((a)*(b)) 349 #define MULT32_32_P31(a,b) ((a)*(b)) 350 351 #define MAC16_32_Q15(c,a,b) ((c)+(a)*(b)) 352 #define MAC16_32_Q16(c,a,b) ((c)+(a)*(b)) 353 #define MAC_COEF_32_ARM(c,a,b) ((c)+(a)*(b)) 354 355 #define MULT16_16_Q11_32(a,b) ((a)*(b)) 356 #define MULT16_16_Q11(a,b) ((a)*(b)) 357 #define MULT16_16_Q13(a,b) ((a)*(b)) 358 #define MULT16_16_Q14(a,b) ((a)*(b)) 359 #define MULT16_16_Q15(a,b) ((a)*(b)) 360 #define MULT16_16_P15(a,b) ((a)*(b)) 361 #define MULT16_16_P13(a,b) ((a)*(b)) 362 #define MULT16_16_P14(a,b) ((a)*(b)) 363 #define MULT16_32_P16(a,b) ((a)*(b)) 364 365 #define MULT_COEF_32(a, b) ((a)*(b)) 366 #define MULT_COEF(a, b) ((a)*(b)) 367 #define MULT_COEF_TAPS(a, b) ((a)*(b)) 368 369 #define DIV32_16(a,b) (((opus_val32)(a))/(opus_val16)(b)) 370 #define DIV32(a,b) (((opus_val32)(a))/(opus_val32)(b)) 371 372 #define SIG2RES(a) ((1/CELT_SIG_SCALE)*(a)) 373 #define RES2INT16(a) FLOAT2INT16(a) 374 #define RES2INT24(a) float2int(32768.f*256.f*(a)) 375 #define RES2FLOAT(a) (a) 376 #define INT16TORES(a) ((a)*(1/CELT_SIG_SCALE)) 377 #define INT24TORES(a) ((1.f/32768.f/256.f)*(a)) 378 #define ADD_RES(a, b) ADD32(a, b) 379 #define FLOAT2RES(a) (a) 380 #define RES2SIG(a) (CELT_SIG_SCALE*(a)) 381 #define MULT16_RES_Q15(a,b) MULT16_16_Q15(a,b) 382 383 #define RES2VAL16(a) (a) 384 #define FLOAT2SIG(a) ((a)*CELT_SIG_SCALE) 385 #define INT16TOSIG(a) ((float)(a)) 386 #define INT24TOSIG(a) ((float)(a)*(1.f/256.f)) 387 #define MAX_ENCODING_DEPTH 24 388 389 #endif /* !FIXED_POINT */ 390 391 #ifndef GLOBAL_STACK_SIZE 392 #ifdef FIXED_POINT 393 #define GLOBAL_STACK_SIZE 120000 394 #else 395 #define GLOBAL_STACK_SIZE 120000 396 #endif 397 #endif 398 399 #endif /* ARCH_H */