All sample rates/modes with fresh training now in CVS.
[platform/upstream/libvorbis.git] / lib / os.h
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-2002             *
11  * by the XIPHOPHORUS Company 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,v 1.32 2002/07/11 06:40:49 xiphmont Exp $
17
18  ********************************************************************/
19
20 #include <math.h>
21 #include <ogg/os_types.h>
22
23 #include "misc.h"
24
25 #ifndef _V_IFDEFJAIL_H_
26 #  define _V_IFDEFJAIL_H_
27
28 #  ifdef __GNUC__
29 #    define STIN static __inline__
30 #  elif _WIN32
31 #    define STIN static __inline
32 #else
33 #  define STIN static
34 #endif
35
36 #ifndef M_PI
37 #  define M_PI (3.1415926536f)
38 #endif
39
40 #ifdef _WIN32
41 #  include <malloc.h>
42 #  define rint(x)   (floor((x)+0.5f)) 
43 #  define NO_FLOAT_MATH_LIB
44 #  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
45 #endif
46
47 #ifndef FAST_HYPOT
48 #  define FAST_HYPOT hypot
49 #endif
50
51 #endif
52
53 #ifdef HAVE_ALLOCA_H
54 #  include <alloca.h>
55 #endif
56
57 #ifdef USE_MEMORY_H
58 #  include <memory.h>
59 #endif
60
61 #ifndef min
62 #  define min(x,y)  ((x)>(y)?(y):(x))
63 #endif
64
65 #ifndef max
66 #  define max(x,y)  ((x)<(y)?(y):(x))
67 #endif
68
69 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
70 #  define VORBIS_FPU_CONTROL
71 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
72    Because of encapsulation constraints (GCC can't see inside the asm
73    block and so we end up doing stupid things like a store/load that
74    is collectively a noop), we do it this way */
75
76 /* we must set up the fpu before this works!! */
77
78 typedef ogg_int16_t vorbis_fpu_control;
79
80 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
81   ogg_int16_t ret;
82   ogg_int16_t temp;
83   __asm__ __volatile__("fnstcw %0\n\t"
84           "movw %0,%%dx\n\t"
85           "orw $62463,%%dx\n\t"
86           "movw %%dx,%1\n\t"
87           "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
88   *fpu=ret;
89 }
90
91 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
92   __asm__ __volatile__("fldcw %0":: "m"(fpu));
93 }
94
95 /* assumes the FPU is in round mode! */
96 static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
97                                              we get extra fst/fld to
98                                              truncate precision */
99   int i;
100   __asm__("fistl %0": "=m"(i) : "t"(f));
101   return(i);
102 }
103 #endif
104
105
106 #if defined(_WIN32) && !defined(__GNUC__) && !defined(__BORLANDC__)
107 #  define VORBIS_FPU_CONTROL
108
109 typedef ogg_int16_t vorbis_fpu_control;
110
111 static __inline int vorbis_ftoi(double f){
112         int i;
113         __asm{
114                 fld f
115                 fistp i
116         }
117         return i;
118 }
119
120 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
121 }
122
123 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
124 }
125
126 #endif
127
128
129 #ifndef VORBIS_FPU_CONTROL
130
131 typedef int vorbis_fpu_control;
132
133 static int vorbis_ftoi(double f){
134   return (int)(f+.5);
135 }
136
137 /* We don't have special code for this compiler/arch, so do it the slow way */
138 #  define vorbis_fpu_setround(vorbis_fpu_control) {}
139 #  define vorbis_fpu_restore(vorbis_fpu_control) {}
140
141 #endif
142
143 #endif /* _OS_H */