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#ifndef _V_IFDEFJAIL_H_
30#  define _V_IFDEFJAIL_H_
31
32#  ifdef __GNUC__
33#    define STIN static __inline__
34#  elif _WIN32
35#    define STIN static __inline
36#  else
37#    define STIN static
38#  endif
39
40#ifdef DJGPP
41#  define rint(x)   (floor((x)+0.5f))
42#endif
43
44#ifndef M_PI
45#  define M_PI (3.1415926536f)
46#endif
47
48#if defined(_WIN32) && !defined(__SYMBIAN32__)
49#  include <malloc.h>
50#  define rint(x)   (floor((x)+0.5f))
51#  define NO_FLOAT_MATH_LIB
52#  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
53#endif
54
55#if defined(__SYMBIAN32__) && defined(__WINS__)
56void *_alloca(size_t size);
57#  define alloca _alloca
58#endif
59
60#ifndef FAST_HYPOT
61#  define FAST_HYPOT hypot
62#endif
63
64#endif
65
66#ifdef HAVE_ALLOCA_H
67#  include <alloca.h>
68#endif
69
70#ifdef USE_MEMORY_H
71#  include <memory.h>
72#endif
73
74#ifndef min
75#  define min(x,y)  ((x)>(y)?(y):(x))
76#endif
77
78#ifndef max
79#  define max(x,y)  ((x)<(y)?(y):(x))
80#endif
81
82
83/* Special i386 GCC implementation */
84#if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
85#  define VORBIS_FPU_CONTROL
86/* both GCC and MSVC are kinda stupid about rounding/casting to int.
87   Because of encapsulation constraints (GCC can't see inside the asm
88   block and so we end up doing stupid things like a store/load that
89   is collectively a noop), we do it this way */
90
91/* we must set up the fpu before this works!! */
92
93typedef ogg_int16_t vorbis_fpu_control;
94
95static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
96  ogg_int16_t ret;
97  ogg_int16_t temp;
98  __asm__ __volatile__("fnstcw %0\n\t"
99          "movw %0,%%dx\n\t"
100          "andw $62463,%%dx\n\t"
101          "movw %%dx,%1\n\t"
102          "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
103  *fpu=ret;
104}
105
106static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
107  __asm__ __volatile__("fldcw %0":: "m"(fpu));
108}
109
110/* assumes the FPU is in round mode! */
111static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
112                                             we get extra fst/fld to
113                                             truncate precision */
114  int i;
115  __asm__("fistl %0": "=m"(i) : "t"(f));
116  return(i);
117}
118#endif /* Special i386 GCC implementation */
119
120
121/* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
122 * 64 bit compiler */
123#if defined(_MSC_VER) && !defined(_WIN64) && !defined(_WIN32_WCE)
124#  define VORBIS_FPU_CONTROL
125
126typedef ogg_int16_t vorbis_fpu_control;
127
128static __inline int vorbis_ftoi(double f){
129        int i;
130        __asm{
131                fld f
132                fistp i
133        }
134        return i;
135}
136
137static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
138}
139
140static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
141}
142
143#endif /* Special MSVC 32 bit implementation */
144
145
146/* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
147   done safely because all x86_64 CPUs supports SSE2. */
148#if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
149#  define VORBIS_FPU_CONTROL
150
151typedef ogg_int16_t vorbis_fpu_control;
152
153#include <emmintrin.h>
154static __inline int vorbis_ftoi(double f){
155        return _mm_cvtsd_si32(_mm_load_sd(&f));
156}
157
158static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
159}
160
161static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
162}
163
164#endif /* Special MSVC x64 implementation */
165
166
167/* If no special implementation was found for the current compiler / platform,
168   use the default implementation here: */
169#ifndef VORBIS_FPU_CONTROL
170
171typedef int vorbis_fpu_control;
172
173static int vorbis_ftoi(double f){
174        /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
175           the floor() call is required to ensure correct roudning of
176           negative numbers */
177        return (int)floor(f+.5);
178}
179
180/* We don't have special code for this compiler/arch, so do it the slow way */
181#  define vorbis_fpu_setround(vorbis_fpu_control) {}
182#  define vorbis_fpu_restore(vorbis_fpu_control) {}
183
184#endif /* default implementation */
185
186#endif /* _OS_H */
187