e3190f60daeec1dac000703ed7f3be4bae7fde88
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / tlib / compatibility.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23 #if defined( __MINGW32__) || defined (WIN32)
24 // Simulate some Unix fonctions on Windows
25
26 #include <windows.h>
27 #include "math.h"
28
29 #if defined(_MBCS) || __MINGW32__
30 bool chdir(const char* path)
31 {
32 return !SetCurrentDirectory(path);
33 }
34
35 int mkdir(const char* path, unsigned int attribute)
36 {
37 return CreateDirectory(path,NULL);
38 }
39
40 char* getcwd(char* str, unsigned int size)
41 {
42 GetCurrentDirectory(size, str);
43 return str;
44 }
45 void getFaustPathname(char* str, unsigned int size)
46 {
47 GetModuleFileName(NULL, str, size);
48 }
49 #else
50 bool chdir(const char* path)
51 {
52 wchar_t wstr[2048];
53 mbstowcs(wstr,path,2048);
54 return !SetCurrentDirectory(wstr);
55 }
56
57 int mkdir(const char* path, unsigned int attribute)
58 {
59 wchar_t wstr[2048];
60 mbstowcs(wstr,path,2048);
61 return CreateDirectory(wstr,NULL);
62 }
63
64 char* getcwd(char* str, unsigned int size)
65 {
66 wchar_t wstr[2048];
67 GetCurrentDirectory(2048, wstr);
68 wcstombs(str,wstr,size);
69 return str;
70 }
71
72 void getFaustPathname(char* str, unsigned int size)
73 {
74 wchar_t wstr[2048];
75 GetModuleFileName(NULL, wstr, 2048);
76 wcstombs(str,wstr,size);
77 }
78
79 #endif
80
81 int isatty(int file)
82 {
83 return 0;
84 }
85
86 #if !defined(__MINGW32__)
87
88 typedef union
89 {
90 double value;
91 struct
92 {
93 unsigned int lsw;
94 unsigned int msw;
95 } parts;
96 } ieee_double_shape_type;
97
98
99 #define EXTRACT_WORDS(ix0,ix1,d) \
100 do { \
101 ieee_double_shape_type ew_u; \
102 ew_u.value = (d); \
103 (ix0) = ew_u.parts.msw; \
104 (ix1) = ew_u.parts.lsw; \
105 } while (0)
106
107 /* Get the more significant 32 bit int from a double. */
108
109 #define GET_HIGH_WORD(i,d) \
110 do { \
111 ieee_double_shape_type gh_u; \
112 gh_u.value = (d); \
113 (i) = gh_u.parts.msw; \
114 } while (0)
115
116 /* Get the less significant 32 bit int from a double. */
117
118 #define GET_LOW_WORD(i,d) \
119 do { \
120 ieee_double_shape_type gl_u; \
121 gl_u.value = (d); \
122 (i) = gl_u.parts.lsw; \
123 } while (0)
124
125 #define SET_HIGH_WORD(d,v) \
126 do { \
127 ieee_double_shape_type sh_u; \
128 sh_u.value = (d); \
129 sh_u.parts.msw = (v); \
130 (d) = sh_u.value; \
131 } while (0)
132
133 double remainder(double x, double p)
134 {
135 int hx,hp;
136 unsigned int sx,lx,lp;
137 double p_half;
138
139 EXTRACT_WORDS(hx,lx,x);
140 EXTRACT_WORDS(hp,lp,p);
141 sx = hx&0x80000000;
142 hp &= 0x7fffffff;
143 hx &= 0x7fffffff;
144
145 /* purge off exception values */
146 if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */
147 if((hx>=0x7ff00000)|| /* x not finite */
148 ((hp>=0x7ff00000)&& /* p is NaN */
149 (((hp-0x7ff00000)|lp)!=0)))
150 return (x*p)/(x*p);
151
152
153 static const double zero = 0.0;
154 if (hp<=0x7fdfffff) x = fmod(x,p+p); /* now x < 2p */
155 if (((hx-hp)|(lx-lp))==0) return zero*x;
156 x = fabs(x);
157 p = fabs(p);
158 if (hp<0x00200000) {
159 if(x+x>p) {
160 x-=p;
161 if(x+x>=p) x -= p;
162 }
163 } else {
164 p_half = 0.5*p;
165 if(x>p_half) {
166 x-=p;
167 if(x>=p_half) x -= p;
168 }
169 }
170 GET_HIGH_WORD(hx,x);
171 SET_HIGH_WORD(x,hx^sx);
172 return x;
173 }
174 #endif
175
176 #else // Linux
177
178 #include <stdlib.h>
179 #include <string.h>
180 void getFaustPathname(char* str, unsigned int size)
181 {
182 char* path = getenv("_");
183 if (path) {
184 strncpy(str, path, size);
185 } else {
186 // prevent the case of _ undefined
187 strncpy(str, "/usr/local/bin/faust", size);
188 }
189 }
190
191 #endif
192
193