PageRenderTime 40ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/media/libvorbis/lib/os.h

http://github.com/zpao/v8monkey
C Header | 190 lines | 111 code | 44 blank | 35 comment | 9 complexity | 930c98bc30f1b2507f98c22cf923f153 MD5 | raw file
  1#ifndef _OS_H
  2#define _OS_H
  3/********************************************************************
  4 *                                                                  *
  5 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
  6 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
  7 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  8 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  9 *                                                                  *
 10 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
 11 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 12 *                                                                  *
 13 ********************************************************************
 14
 15 function: #ifdef jail to whip a few platforms into the UNIX ideal.
 16 last mod: $Id: os.h 16227 2009-07-08 06:58:46Z xiphmont $
 17
 18 ********************************************************************/
 19
 20#ifdef HAVE_CONFIG_H
 21#include "config.h"
 22#endif
 23
 24#include <math.h>
 25#include <ogg/os_types.h>
 26
 27#include "misc.h"
 28
 29#ifdef SOLARIS
 30#define HAVE_ALLOCA_H
 31#endif
 32
 33#ifndef _V_IFDEFJAIL_H_
 34#  define _V_IFDEFJAIL_H_
 35
 36#  ifdef __GNUC__
 37#    define STIN static __inline__
 38#  elif _WIN32
 39#    define STIN static __inline
 40#  else
 41#    define STIN static
 42#  endif
 43
 44#ifdef DJGPP
 45#  define rint(x)   (floor((x)+0.5f))
 46#endif
 47
 48#ifndef M_PI
 49#  define M_PI (3.1415926536f)
 50#endif
 51
 52#if defined(_WIN32) && !defined(__SYMBIAN32__)
 53#  include <malloc.h>
 54#  define rint(x)   (floor((x)+0.5f))
 55#  define NO_FLOAT_MATH_LIB
 56#  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
 57#endif
 58
 59#if defined(__SYMBIAN32__) && defined(__WINS__)
 60void *_alloca(size_t size);
 61#  define alloca _alloca
 62#endif
 63
 64#ifndef FAST_HYPOT
 65#  define FAST_HYPOT hypot
 66#endif
 67
 68#endif
 69
 70#ifdef HAVE_ALLOCA_H
 71#  include <alloca.h>
 72#endif
 73
 74#ifdef USE_MEMORY_H
 75#  include <memory.h>
 76#endif
 77
 78#ifndef min
 79#  define min(x,y)  ((x)>(y)?(y):(x))
 80#endif
 81
 82#ifndef max
 83#  define max(x,y)  ((x)<(y)?(y):(x))
 84#endif
 85
 86
 87/* Special i386 GCC implementation */
 88#if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
 89#  define VORBIS_FPU_CONTROL
 90/* both GCC and MSVC are kinda stupid about rounding/casting to int.
 91   Because of encapsulation constraints (GCC can't see inside the asm
 92   block and so we end up doing stupid things like a store/load that
 93   is collectively a noop), we do it this way */
 94
 95/* we must set up the fpu before this works!! */
 96
 97typedef ogg_int16_t vorbis_fpu_control;
 98
 99static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
100  ogg_int16_t ret;
101  ogg_int16_t temp;
102  __asm__ __volatile__("fnstcw %0\n\t"
103          "movw %0,%%dx\n\t"
104          "andw $62463,%%dx\n\t"
105          "movw %%dx,%1\n\t"
106          "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
107  *fpu=ret;
108}
109
110static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
111  __asm__ __volatile__("fldcw %0":: "m"(fpu));
112}
113
114/* assumes the FPU is in round mode! */
115static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
116                                             we get extra fst/fld to
117                                             truncate precision */
118  int i;
119  __asm__("fistl %0": "=m"(i) : "t"(f));
120  return(i);
121}
122#endif /* Special i386 GCC implementation */
123
124
125/* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
126 * 64 bit compiler */
127#if defined(_MSC_VER) && !defined(_WIN64) && !defined(_WIN32_WCE)
128#  define VORBIS_FPU_CONTROL
129
130typedef ogg_int16_t vorbis_fpu_control;
131
132static __inline int vorbis_ftoi(double f){
133        int i;
134        __asm{
135                fld f
136                fistp i
137        }
138        return i;
139}
140
141static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
142}
143
144static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
145}
146
147#endif /* Special MSVC 32 bit implementation */
148
149
150/* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
151   done safely because all x86_64 CPUs supports SSE2. */
152#if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
153#  define VORBIS_FPU_CONTROL
154
155typedef ogg_int16_t vorbis_fpu_control;
156
157#include <emmintrin.h>
158static __inline int vorbis_ftoi(double f){
159        return _mm_cvtsd_si32(_mm_load_sd(&f));
160}
161
162static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
163}
164
165static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
166}
167
168#endif /* Special MSVC x64 implementation */
169
170
171/* If no special implementation was found for the current compiler / platform,
172   use the default implementation here: */
173#ifndef VORBIS_FPU_CONTROL
174
175typedef int vorbis_fpu_control;
176
177static int vorbis_ftoi(double f){
178        /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
179           the floor() call is required to ensure correct roudning of
180           negative numbers */
181        return (int)floor(f+.5);
182}
183
184/* We don't have special code for this compiler/arch, so do it the slow way */
185#  define vorbis_fpu_setround(vorbis_fpu_control) {}
186#  define vorbis_fpu_restore(vorbis_fpu_control) {}
187
188#endif /* default implementation */
189
190#endif /* _OS_H */