os.h 3.79 KB
Newer Older
jon's avatar
jon committed
1
2
#ifndef _OS_H
#define _OS_H
Monty's avatar
 
Monty committed
3
4
/********************************************************************
 *                                                                  *
Monty's avatar
   
Monty committed
5
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
 
Monty committed
6
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
Monty's avatar
   
Monty committed
7
8
 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
Monty's avatar
 
Monty committed
9
 *                                                                  *
Monty's avatar
   
Monty committed
10
11
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
 * by Monty <monty@xiph.org> and the XIPHOPHORUS Company            *
Monty's avatar
 
Monty committed
12
13
14
15
 * http://www.xiph.org/                                             *
 *                                                                  *
 ********************************************************************

Monty's avatar
   
Monty committed
16
 function: #ifdef jail to whip a few platforms into the UNIX ideal.
17
 last mod: $Id: os.h,v 1.20 2001/01/30 09:40:11 msmith Exp $
Monty's avatar
 
Monty committed
18
19
20

 ********************************************************************/

Monty's avatar
   
Monty committed
21
#include <math.h>
Monty's avatar
   
Monty committed
22
23
#include <ogg/os_types.h>

Monty's avatar
 
Monty committed
24
25
26
27
#ifndef _V_IFDEFJAIL_H_
#define _V_IFDEFJAIL_H_

#ifndef M_PI
Monty's avatar
   
Monty committed
28
#define M_PI (3.1415926536f)
Monty's avatar
 
Monty committed
29
30
#endif

Monty's avatar
   
Monty committed
31
#ifndef __GNUC__
Monty's avatar
 
Monty committed
32
#ifdef _WIN32
Monty's avatar
   
Monty committed
33
#  include <malloc.h>
Monty's avatar
   
Monty committed
34
#  define rint(x)   (floor((x)+0.5f)) 
Monty's avatar
 
Monty committed
35
#endif
Monty's avatar
   
Monty committed
36
37
38
39

#define STIN static
#else
#define STIN static inline
Monty's avatar
   
Monty committed
40
41
42
43
44
45
46
47
#define sqrt sqrtf
#define log logf
#define exp expf
#define pow powf
#define acos acosf
#define atan atanf
#define frexp frexpf
#define rint rintf
Monty's avatar
 
Monty committed
48
49
#endif

Monty's avatar
   
Monty committed
50

jon's avatar
jon committed
51
52
#ifdef _WIN32
#  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
Monty's avatar
   
Monty committed
53
#else /* if not _WIN32 */
jon's avatar
jon committed
54
55
56
#  define FAST_HYPOT hypot
#endif

Monty's avatar
 
Monty committed
57
58
#endif

Monty's avatar
   
Monty committed
59
#ifdef HAVE_ALLOCA_H
Monty's avatar
   
Monty committed
60
61
62
63
64
65
66
#include <alloca.h>
#endif

#ifdef USE_MEMORY_H
#include <memory.h>
#endif

67
68
69
70
71
72
73
74
#ifndef min
#  define min(x,y)  ((x)>(y)?(y):(x))
#endif

#ifndef max
#  define max(x,y)  ((x)<(y)?(y):(x))
#endif

Monty's avatar
   
Monty committed
75
76
77

#if defined(__i386__) && defined(__GNUC__)

Jack Moffitt's avatar
Jack Moffitt committed
78
79
#ifndef __BEOS__

Monty's avatar
   
Monty committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* both GCC and MSVC are kinda stupid about rounding/casting to int.
   Because of encapsulation constraints (GCC can't see inside the asm
   block and so we end up doing stupid things like a store/load that
   is collectively a noop), we do it this way */

/* we must set up the fpu before this works!! */

typedef ogg_int16_t vorbis_fpu_control;

static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  ogg_int16_t ret;
  ogg_int16_t temp;
  __asm__ __volatile__("fnstcw %0\n\t"
	  "movw %0,%%dx\n\t"
	  "orw $62463,%%dx\n\t"
	  "movw %%dx,%1\n\t"
	  "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  *fpu=ret;
}

static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  __asm__ __volatile__("fldcw %0":: "m"(fpu));
}

/* assumes the FPU is in round mode! */
static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
                                             we get extra fst/fld to
                                             truncate precision */
  int i;
  __asm__("fistl %0": "=m"(i) : "t"(f));
  return(i);
}

Jack Moffitt's avatar
Jack Moffitt committed
113
114
#else
/* this is for beos */
Jack Moffitt's avatar
Jack Moffitt committed
115
116

typedef int vorbis_fpu_control;
Jack Moffitt's avatar
Jack Moffitt committed
117
118
119
120
121
122
123
124
125
126
static int vorbis_ftoi(double f){
  return (int)(f+.5);
}

/* We don't have special code for this compiler/arch, so do it the slow way */
#define vorbis_fpu_setround(vorbis_fpu_control) {}
#define vorbis_fpu_restore(vorbis_fpu_control) {}

#endif

Monty's avatar
   
Monty committed
127
128
129
130
131
#else


typedef int vorbis_fpu_control;

132
133
#ifdef _MSC_VER 
/* MSVC++ */
Jack Moffitt's avatar
Jack Moffitt committed
134
135
136
137
138
139
140
141
142
static __inline int vorbis_ftoi(double f){
	int i;
	__asm{
		fld f
		fistp i
	}
	return i;
}

143
144
145
146
147
148
static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
}

static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
}
#else 
Jack Moffitt's avatar
Jack Moffitt committed
149
150
151
152
153

static int vorbis_ftoi(double f){
  return (int)(f+.5);
}

Michael Smith's avatar
   
Michael Smith committed
154
155
156
/* We don't have special code for this compiler/arch, so do it the slow way */
#define vorbis_fpu_setround(vorbis_fpu_control) {}
#define vorbis_fpu_restore(vorbis_fpu_control) {}
Monty's avatar
   
Monty committed
157

158
159
#endif

Monty's avatar
   
Monty committed
160
161
#endif

Monty's avatar
   
Monty committed
162
#endif /* _OS_H */