Chris@1: #ifndef _OS_H Chris@1: #define _OS_H Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: #ifdef jail to whip a few platforms into the UNIX ideal. Chris@1: last mod: $Id: os.h 16227 2009-07-08 06:58:46Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #ifdef HAVE_CONFIG_H Chris@1: #include "config.h" Chris@1: #endif Chris@1: Chris@1: #include Chris@1: #include Chris@1: Chris@1: #include "misc.h" Chris@1: Chris@1: #ifndef _V_IFDEFJAIL_H_ Chris@1: # define _V_IFDEFJAIL_H_ Chris@1: Chris@1: # ifdef __GNUC__ Chris@1: # define STIN static __inline__ Chris@1: # elif _WIN32 Chris@1: # define STIN static __inline Chris@1: # else Chris@1: # define STIN static Chris@1: # endif Chris@1: Chris@1: #ifdef DJGPP Chris@1: # define rint(x) (floor((x)+0.5f)) Chris@1: #endif Chris@1: Chris@1: #ifndef M_PI Chris@1: # define M_PI (3.1415926536f) Chris@1: #endif Chris@1: Chris@1: #if defined(_WIN32) && !defined(__SYMBIAN32__) Chris@1: # include Chris@1: # define rint(x) (floor((x)+0.5f)) Chris@1: # define NO_FLOAT_MATH_LIB Chris@1: # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b)) Chris@1: #endif Chris@1: Chris@1: #if defined(__SYMBIAN32__) && defined(__WINS__) Chris@1: void *_alloca(size_t size); Chris@1: # define alloca _alloca Chris@1: #endif Chris@1: Chris@1: #ifndef FAST_HYPOT Chris@1: # define FAST_HYPOT hypot Chris@1: #endif Chris@1: Chris@1: #endif Chris@1: Chris@1: #ifdef HAVE_ALLOCA_H Chris@1: # include Chris@1: #endif Chris@1: Chris@1: #ifdef USE_MEMORY_H Chris@1: # include Chris@1: #endif Chris@1: Chris@1: #ifndef min Chris@1: # define min(x,y) ((x)>(y)?(y):(x)) Chris@1: #endif Chris@1: Chris@1: #ifndef max Chris@1: # define max(x,y) ((x)<(y)?(y):(x)) Chris@1: #endif Chris@1: Chris@1: Chris@1: /* Special i386 GCC implementation */ Chris@1: #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__) Chris@1: # define VORBIS_FPU_CONTROL Chris@1: /* both GCC and MSVC are kinda stupid about rounding/casting to int. Chris@1: Because of encapsulation constraints (GCC can't see inside the asm Chris@1: block and so we end up doing stupid things like a store/load that Chris@1: is collectively a noop), we do it this way */ Chris@1: Chris@1: /* we must set up the fpu before this works!! */ Chris@1: Chris@1: typedef ogg_int16_t vorbis_fpu_control; Chris@1: Chris@1: static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){ Chris@1: ogg_int16_t ret; Chris@1: ogg_int16_t temp; Chris@1: __asm__ __volatile__("fnstcw %0\n\t" Chris@1: "movw %0,%%dx\n\t" Chris@1: "andw $62463,%%dx\n\t" Chris@1: "movw %%dx,%1\n\t" Chris@1: "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx"); Chris@1: *fpu=ret; Chris@1: } Chris@1: Chris@1: static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){ Chris@1: __asm__ __volatile__("fldcw %0":: "m"(fpu)); Chris@1: } Chris@1: Chris@1: /* assumes the FPU is in round mode! */ Chris@1: static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise, Chris@1: we get extra fst/fld to Chris@1: truncate precision */ Chris@1: int i; Chris@1: __asm__("fistl %0": "=m"(i) : "t"(f)); Chris@1: return(i); Chris@1: } Chris@1: #endif /* Special i386 GCC implementation */ Chris@1: Chris@1: Chris@1: /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the Chris@1: * 64 bit compiler */ Chris@1: #if defined(_MSC_VER) && !defined(_WIN64) && !defined(_WIN32_WCE) Chris@1: # define VORBIS_FPU_CONTROL Chris@1: Chris@1: typedef ogg_int16_t vorbis_fpu_control; Chris@1: Chris@1: static __inline int vorbis_ftoi(double f){ Chris@1: int i; Chris@1: __asm{ Chris@1: fld f Chris@1: fistp i Chris@1: } Chris@1: return i; Chris@1: } Chris@1: Chris@1: static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){ Chris@1: } Chris@1: Chris@1: static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){ Chris@1: } Chris@1: Chris@1: #endif /* Special MSVC 32 bit implementation */ Chris@1: Chris@1: Chris@1: /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be Chris@1: done safely because all x86_64 CPUs supports SSE2. */ Chris@1: #if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__)) Chris@1: # define VORBIS_FPU_CONTROL Chris@1: Chris@1: typedef ogg_int16_t vorbis_fpu_control; Chris@1: Chris@1: #include Chris@1: static __inline int vorbis_ftoi(double f){ Chris@1: return _mm_cvtsd_si32(_mm_load_sd(&f)); Chris@1: } Chris@1: Chris@1: static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){ Chris@1: } Chris@1: Chris@1: static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){ Chris@1: } Chris@1: Chris@1: #endif /* Special MSVC x64 implementation */ Chris@1: Chris@1: Chris@1: /* If no special implementation was found for the current compiler / platform, Chris@1: use the default implementation here: */ Chris@1: #ifndef VORBIS_FPU_CONTROL Chris@1: Chris@1: typedef int vorbis_fpu_control; Chris@1: Chris@1: static int vorbis_ftoi(double f){ Chris@1: /* Note: MSVC and GCC (at least on some systems) round towards zero, thus, Chris@1: the floor() call is required to ensure correct roudning of Chris@1: negative numbers */ Chris@1: return (int)floor(f+.5); Chris@1: } Chris@1: Chris@1: /* We don't have special code for this compiler/arch, so do it the slow way */ Chris@1: # define vorbis_fpu_setround(vorbis_fpu_control) {} Chris@1: # define vorbis_fpu_restore(vorbis_fpu_control) {} Chris@1: Chris@1: #endif /* default implementation */ Chris@1: Chris@1: #endif /* _OS_H */