Home | History | Annotate | Download | only in celt
      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 #ifdef ENABLE_ASSERTIONS
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #ifdef __GNUC__
     64 __attribute__((noreturn))
     65 #endif
     66 static OPUS_INLINE void _celt_fatal(const char *str, const char *file, int line)
     67 {
     68    fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
     69    abort();
     70 }
     71 #define celt_assert(cond) {if (!(cond)) {celt_fatal("assertion failed: " #cond);}}
     72 #define celt_assert2(cond, message) {if (!(cond)) {celt_fatal("assertion failed: " #cond "\n" message);}}
     73 #else
     74 #define celt_assert(cond)
     75 #define celt_assert2(cond, message)
     76 #endif
     77 
     78 #define IMUL32(a,b) ((a)*(b))
     79 
     80 #define MIN16(a,b) ((a) < (b) ? (a) : (b))   /**< Minimum 16-bit value.   */
     81 #define MAX16(a,b) ((a) > (b) ? (a) : (b))   /**< Maximum 16-bit value.   */
     82 #define MIN32(a,b) ((a) < (b) ? (a) : (b))   /**< Minimum 32-bit value.   */
     83 #define MAX32(a,b) ((a) > (b) ? (a) : (b))   /**< Maximum 32-bit value.   */
     84 #define IMIN(a,b) ((a) < (b) ? (a) : (b))   /**< Minimum int value.   */
     85 #define IMAX(a,b) ((a) > (b) ? (a) : (b))   /**< Maximum int value.   */
     86 #define UADD32(a,b) ((a)+(b))
     87 #define USUB32(a,b) ((a)-(b))
     88 
     89 /* Set this if opus_int64 is a native type of the CPU. */
     90 /* Assume that all LP64 architectures have fast 64-bit types; also x86_64
     91    (which can be ILP32 for x32) and Win64 (which is LLP64). */
     92 #if defined(__x86_64__) || defined(__LP64__) || defined(_WIN64)
     93 #define OPUS_FAST_INT64 1
     94 #else
     95 #define OPUS_FAST_INT64 0
     96 #endif
     97 
     98 #define PRINT_MIPS(file)
     99 
    100 #ifdef FIXED_POINT
    101 
    102 typedef opus_int16 opus_val16;
    103 typedef opus_int32 opus_val32;
    104 typedef opus_int64 opus_val64;
    105 
    106 typedef opus_val32 celt_sig;
    107 typedef opus_val16 celt_norm;
    108 typedef opus_val32 celt_ener;
    109 
    110 #define Q15ONE 32767
    111 
    112 #define SIG_SHIFT 12
    113 /* Safe saturation value for 32-bit signals. Should be less than
    114    2^31*(1-0.85) to avoid blowing up on DC at deemphasis.*/
    115 #define SIG_SAT (300000000)
    116 
    117 #define NORM_SCALING 16384
    118 
    119 #define DB_SHIFT 10
    120 
    121 #define EPSILON 1
    122 #define VERY_SMALL 0
    123 #define VERY_LARGE16 ((opus_val16)32767)
    124 #define Q15_ONE ((opus_val16)32767)
    125 
    126 #define SCALEIN(a)      (a)
    127 #define SCALEOUT(a)     (a)
    128 
    129 #define ABS16(x) ((x) < 0 ? (-(x)) : (x))
    130 #define ABS32(x) ((x) < 0 ? (-(x)) : (x))
    131 
    132 static OPUS_INLINE opus_int16 SAT16(opus_int32 x) {
    133    return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
    134 }
    135 
    136 #ifdef FIXED_DEBUG
    137 #include "fixed_debug.h"
    138 #else
    139 
    140 #include "fixed_generic.h"
    141 
    142 #ifdef OPUS_ARM_PRESUME_AARCH64_NEON_INTR
    143 #include "arm/fixed_arm64.h"
    144 #elif OPUS_ARM_INLINE_EDSP
    145 #include "arm/fixed_armv5e.h"
    146 #elif defined (OPUS_ARM_INLINE_ASM)
    147 #include "arm/fixed_armv4.h"
    148 #elif defined (BFIN_ASM)
    149 #include "fixed_bfin.h"
    150 #elif defined (TI_C5X_ASM)
    151 #include "fixed_c5x.h"
    152 #elif defined (TI_C6X_ASM)
    153 #include "fixed_c6x.h"
    154 #endif
    155 
    156 #endif
    157 
    158 #else /* FIXED_POINT */
    159 
    160 typedef float opus_val16;
    161 typedef float opus_val32;
    162 typedef float opus_val64;
    163 
    164 typedef float celt_sig;
    165 typedef float celt_norm;
    166 typedef float celt_ener;
    167 
    168 #ifdef FLOAT_APPROX
    169 /* This code should reliably detect NaN/inf even when -ffast-math is used.
    170    Assumes IEEE 754 format. */
    171 static OPUS_INLINE int celt_isnan(float x)
    172 {
    173    union {float f; opus_uint32 i;} in;
    174    in.f = x;
    175    return ((in.i>>23)&0xFF)==0xFF && (in.i&0x007FFFFF)!=0;
    176 }
    177 #else
    178 #ifdef __FAST_MATH__
    179 #error Cannot build libopus with -ffast-math unless FLOAT_APPROX is defined. This could result in crashes on extreme (e.g. NaN) input
    180 #endif
    181 #define celt_isnan(x) ((x)!=(x))
    182 #endif
    183 
    184 #define Q15ONE 1.0f
    185 
    186 #define NORM_SCALING 1.f
    187 
    188 #define EPSILON 1e-15f
    189 #define VERY_SMALL 1e-30f
    190 #define VERY_LARGE16 1e15f
    191 #define Q15_ONE ((opus_val16)1.f)
    192 
    193 /* This appears to be the same speed as C99's fabsf() but it's more portable. */
    194 #define ABS16(x) ((float)fabs(x))
    195 #define ABS32(x) ((float)fabs(x))
    196 
    197 #define QCONST16(x,bits) (x)
    198 #define QCONST32(x,bits) (x)
    199 
    200 #define NEG16(x) (-(x))
    201 #define NEG32(x) (-(x))
    202 #define NEG32_ovflw(x) (-(x))
    203 #define EXTRACT16(x) (x)
    204 #define EXTEND32(x) (x)
    205 #define SHR16(a,shift) (a)
    206 #define SHL16(a,shift) (a)
    207 #define SHR32(a,shift) (a)
    208 #define SHL32(a,shift) (a)
    209 #define PSHR32(a,shift) (a)
    210 #define VSHR32(a,shift) (a)
    211 
    212 #define PSHR(a,shift)   (a)
    213 #define SHR(a,shift)    (a)
    214 #define SHL(a,shift)    (a)
    215 #define SATURATE(x,a)   (x)
    216 #define SATURATE16(x)   (x)
    217 
    218 #define ROUND16(a,shift)  (a)
    219 #define SROUND16(a,shift) (a)
    220 #define HALF16(x)       (.5f*(x))
    221 #define HALF32(x)       (.5f*(x))
    222 
    223 #define ADD16(a,b) ((a)+(b))
    224 #define SUB16(a,b) ((a)-(b))
    225 #define ADD32(a,b) ((a)+(b))
    226 #define SUB32(a,b) ((a)-(b))
    227 #define ADD32_ovflw(a,b) ((a)+(b))
    228 #define SUB32_ovflw(a,b) ((a)-(b))
    229 #define MULT16_16_16(a,b)     ((a)*(b))
    230 #define MULT16_16(a,b)     ((opus_val32)(a)*(opus_val32)(b))
    231 #define MAC16_16(c,a,b)     ((c)+(opus_val32)(a)*(opus_val32)(b))
    232 
    233 #define MULT16_32_Q15(a,b)     ((a)*(b))
    234 #define MULT16_32_Q16(a,b)     ((a)*(b))
    235 
    236 #define MULT32_32_Q31(a,b)     ((a)*(b))
    237 
    238 #define MAC16_32_Q15(c,a,b)     ((c)+(a)*(b))
    239 #define MAC16_32_Q16(c,a,b)     ((c)+(a)*(b))
    240 
    241 #define MULT16_16_Q11_32(a,b)     ((a)*(b))
    242 #define MULT16_16_Q11(a,b)     ((a)*(b))
    243 #define MULT16_16_Q13(a,b)     ((a)*(b))
    244 #define MULT16_16_Q14(a,b)     ((a)*(b))
    245 #define MULT16_16_Q15(a,b)     ((a)*(b))
    246 #define MULT16_16_P15(a,b)     ((a)*(b))
    247 #define MULT16_16_P13(a,b)     ((a)*(b))
    248 #define MULT16_16_P14(a,b)     ((a)*(b))
    249 #define MULT16_32_P16(a,b)     ((a)*(b))
    250 
    251 #define DIV32_16(a,b)     (((opus_val32)(a))/(opus_val16)(b))
    252 #define DIV32(a,b)     (((opus_val32)(a))/(opus_val32)(b))
    253 
    254 #define SCALEIN(a)      ((a)*CELT_SIG_SCALE)
    255 #define SCALEOUT(a)     ((a)*(1/CELT_SIG_SCALE))
    256 
    257 #define SIG2WORD16(x) (x)
    258 
    259 #endif /* !FIXED_POINT */
    260 
    261 #ifndef GLOBAL_STACK_SIZE
    262 #ifdef FIXED_POINT
    263 #define GLOBAL_STACK_SIZE 120000
    264 #else
    265 #define GLOBAL_STACK_SIZE 120000
    266 #endif
    267 #endif
    268 
    269 #endif /* ARCH_H */
    270