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