FAUST compiler  0.9.9.6b8
compatibility.cpp
Go to the documentation of this file.
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 #include "compatibility.hh"
23 #include "math.h"
24 
25 #if defined(__MINGW32__) || defined (WIN32)
26  // Simulate some Unix fonctions on Windows
27 
28  #ifndef __MINGW32__
29  /* missing on Windows : see http://bugs.mysql.com/bug.php?id=15936 */
30  double rint(double nr)
31  {
32  double f = floor(nr);
33  double c = ceil(nr);
34  return (((c -nr) >= (nr - f)) ? f : c);
35  }
36  #endif
37 
38  #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
39  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
40  #else
41  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
42  #endif
43 
44  int gettimeofday(struct timeval *tv, struct timezone *tz)
45  {
46  FILETIME ft;
47  unsigned __int64 tmpres = 0;
48  static int tzflag;
49 
50  if (NULL != tv)
51  {
52  GetSystemTimeAsFileTime(&ft);
53 
54  tmpres |= ft.dwHighDateTime;
55  tmpres <<= 32;
56  tmpres |= ft.dwLowDateTime;
57 
58  /*converting file time to unix epoch*/
59  tmpres -= DELTA_EPOCH_IN_MICROSECS;
60  tmpres /= 10; /*convert into microseconds*/
61  tv->tv_sec = (long)(tmpres / 1000000UL);
62  tv->tv_usec = (long)(tmpres % 1000000UL);
63  }
64 
65  if (NULL != tz)
66  {
67  if (!tzflag)
68  {
69  _tzset();
70  tzflag++;
71  }
72  tz->tz_minuteswest = _timezone / 60;
73  tz->tz_dsttime = _daylight;
74  }
75  return 0;
76  }
77 
78 #if defined(_MBCS) || __MINGW32__
79  bool chdir(const char* path)
80  {
81  return !SetCurrentDirectory(path);
82  }
83 
84  int mkdir(const char* path, unsigned int attribute)
85  {
86  return CreateDirectory(path,NULL);
87  }
88 
89  char* getcwd(char* str, unsigned int size)
90  {
91  GetCurrentDirectory(size, str);
92  return str;
93  }
94  void getFaustPathname(char* str, unsigned int size)
95  {
96  GetModuleFileName(NULL, str, size);
97  }
98 #else
99  bool chdir(const char* path)
100  {
101  wchar_t wstr[2048];
102  mbstowcs(wstr,path,2048);
103  return !SetCurrentDirectory(wstr);
104  }
105 
106  int mkdir(const char* path, unsigned int attribute)
107  {
108  wchar_t wstr[2048];
109  mbstowcs(wstr,path,2048);
110  return CreateDirectory(wstr,NULL);
111  }
112 
113  char* getcwd(char* str, unsigned int size)
114  {
115  wchar_t wstr[2048];
116  GetCurrentDirectory(2048, wstr);
117  wcstombs(str,wstr,size);
118  return str;
119  }
120 
121  void getFaustPathname(char* str, unsigned int size)
122  {
123  wchar_t wstr[2048];
124  GetModuleFileName(NULL, wstr, 2048);
125  wcstombs(str,wstr,size);
126  }
127 
128 #endif
129 
130 #if !defined(__MINGW32__)
131 
132  typedef union
133  {
134  double value;
135  struct
136  {
137  unsigned int lsw;
138  unsigned int msw;
139  } parts;
140  } ieee_double_shape_type;
141 
142 
143 #define EXTRACT_WORDS(ix0,ix1,d) \
144  do { \
145  ieee_double_shape_type ew_u; \
146  ew_u.value = (d); \
147  (ix0) = ew_u.parts.msw; \
148  (ix1) = ew_u.parts.lsw; \
149  } while (0)
150 
151  /* Get the more significant 32 bit int from a double. */
152 
153 #define GET_HIGH_WORD(i,d) \
154  do { \
155  ieee_double_shape_type gh_u; \
156  gh_u.value = (d); \
157  (i) = gh_u.parts.msw; \
158  } while (0)
159 
160  /* Get the less significant 32 bit int from a double. */
161 
162 #define GET_LOW_WORD(i,d) \
163  do { \
164  ieee_double_shape_type gl_u; \
165  gl_u.value = (d); \
166  (i) = gl_u.parts.lsw; \
167  } while (0)
168 
169 #define SET_HIGH_WORD(d,v) \
170  do { \
171  ieee_double_shape_type sh_u; \
172  sh_u.value = (d); \
173  sh_u.parts.msw = (v); \
174  (d) = sh_u.value; \
175  } while (0)
176 
177  double remainder(double x, double p)
178  {
179  int hx,hp;
180  unsigned int sx,lx,lp;
181  double p_half;
182 
183  EXTRACT_WORDS(hx,lx,x);
184  EXTRACT_WORDS(hp,lp,p);
185  sx = hx&0x80000000;
186  hp &= 0x7fffffff;
187  hx &= 0x7fffffff;
188 
189  /* purge off exception values */
190  if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */
191  if((hx>=0x7ff00000)|| /* x not finite */
192  ((hp>=0x7ff00000)&& /* p is NaN */
193  (((hp-0x7ff00000)|lp)!=0)))
194  return (x*p)/(x*p);
195 
196 
197  static const double zero = 0.0;
198  if (hp<=0x7fdfffff) x = fmod(x,p+p); /* now x < 2p */
199  if (((hx-hp)|(lx-lp))==0) return zero*x;
200  x = fabs(x);
201  p = fabs(p);
202  if (hp<0x00200000) {
203  if(x+x>p) {
204  x-=p;
205  if(x+x>=p) x -= p;
206  }
207  } else {
208  p_half = 0.5*p;
209  if(x>p_half) {
210  x-=p;
211  if(x>=p_half) x -= p;
212  }
213  }
214  GET_HIGH_WORD(hx,x);
215  SET_HIGH_WORD(x,hx^sx);
216  return x;
217  }
218 #endif
219 
220 #else // Linux
221 
222  #include <stdlib.h>
223  #include <string.h>
224  void getFaustPathname(char* str, unsigned int size)
225  {
226  char* path = getenv("_");
227  if (path) {
228  strncpy(str, path, size);
229  } else {
230  // prevent the case of _ undefined
231  strncpy(str, "/usr/local/bin/faust", size);
232  }
233  }
234 
235 #endif
236 
237 
void getFaustPathname(char *str, unsigned int size)