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