FAUST compiler  0.9.9.6b8
compatibility.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021  
00022 
00023 #if defined( __MINGW32__) || defined (WIN32)
00024     // Simulate some Unix fonctions on Windows
00025 
00026     #include <windows.h>
00027     #include "math.h"
00028 
00029 #if defined(_MBCS) || __MINGW32__
00030     bool chdir(const char* path)
00031     {
00032         return !SetCurrentDirectory(path);
00033     }
00034 
00035     int mkdir(const char* path, unsigned int attribute)
00036     {
00037         return CreateDirectory(path,NULL);
00038     }
00039 
00040     char* getcwd(char* str, unsigned int size)
00041     {
00042         GetCurrentDirectory(size, str);
00043         return str;
00044     }
00045     void getFaustPathname(char* str, unsigned int size)
00046     {
00047         GetModuleFileName(NULL, str, size);
00048     }
00049 #else
00050     bool chdir(const char* path)
00051     {
00052         wchar_t wstr[2048];
00053         mbstowcs(wstr,path,2048);
00054         return !SetCurrentDirectory(wstr);
00055     }
00056 
00057     int mkdir(const char* path, unsigned int attribute)
00058     {
00059         wchar_t wstr[2048];
00060         mbstowcs(wstr,path,2048);
00061         return CreateDirectory(wstr,NULL);
00062     }
00063 
00064     char* getcwd(char* str, unsigned int size)
00065     {
00066         wchar_t wstr[2048];
00067         GetCurrentDirectory(2048, wstr);
00068         wcstombs(str,wstr,size);
00069         return str;
00070     }
00071 
00072     void getFaustPathname(char* str, unsigned int size)
00073     {
00074         wchar_t wstr[2048];
00075         GetModuleFileName(NULL, wstr, 2048);
00076         wcstombs(str,wstr,size);
00077     }
00078 
00079 #endif
00080 
00081     int isatty(int file)
00082     {
00083         return 0;
00084     }
00085 
00086 #if !defined(__MINGW32__)
00087 
00088     typedef union
00089     {
00090         double value;
00091         struct
00092         {
00093             unsigned int lsw;
00094             unsigned int msw;
00095         } parts;
00096     } ieee_double_shape_type;
00097 
00098 
00099 #define EXTRACT_WORDS(ix0,ix1,d)                \
00100     do {                                \
00101     ieee_double_shape_type ew_u;                    \
00102     ew_u.value = (d);                       \
00103     (ix0) = ew_u.parts.msw;                 \
00104     (ix1) = ew_u.parts.lsw;                 \
00105     } while (0)
00106 
00107     /* Get the more significant 32 bit int from a double.  */
00108 
00109 #define GET_HIGH_WORD(i,d)                  \
00110     do {                                \
00111     ieee_double_shape_type gh_u;                    \
00112     gh_u.value = (d);                       \
00113     (i) = gh_u.parts.msw;                       \
00114     } while (0)
00115 
00116     /* Get the less significant 32 bit int from a double.  */
00117 
00118 #define GET_LOW_WORD(i,d)                   \
00119     do {                                \
00120     ieee_double_shape_type gl_u;                    \
00121     gl_u.value = (d);                       \
00122     (i) = gl_u.parts.lsw;                       \
00123     } while (0)
00124 
00125 #define SET_HIGH_WORD(d,v)                  \
00126     do {                                \
00127     ieee_double_shape_type sh_u;                    \
00128     sh_u.value = (d);                       \
00129     sh_u.parts.msw = (v);                       \
00130     (d) = sh_u.value;                       \
00131     } while (0)
00132 
00133     double remainder(double x, double p)
00134     {
00135         int hx,hp;
00136         unsigned int sx,lx,lp;
00137         double p_half;
00138 
00139         EXTRACT_WORDS(hx,lx,x);
00140         EXTRACT_WORDS(hp,lp,p);
00141         sx = hx&0x80000000;
00142         hp &= 0x7fffffff;
00143         hx &= 0x7fffffff;
00144 
00145         /* purge off exception values */
00146         if((hp|lp)==0) return (x*p)/(x*p);  /* p = 0 */
00147         if((hx>=0x7ff00000)||           /* x not finite */
00148             ((hp>=0x7ff00000)&&         /* p is NaN */
00149             (((hp-0x7ff00000)|lp)!=0)))
00150             return (x*p)/(x*p);
00151 
00152 
00153         static const double zero = 0.0;
00154         if (hp<=0x7fdfffff) x = fmod(x,p+p);    /* now x < 2p */
00155         if (((hx-hp)|(lx-lp))==0) return zero*x;
00156         x  = fabs(x);
00157         p  = fabs(p);
00158         if (hp<0x00200000) {
00159             if(x+x>p) {
00160                 x-=p;
00161                 if(x+x>=p) x -= p;
00162             }
00163         } else {
00164             p_half = 0.5*p;
00165             if(x>p_half) {
00166                 x-=p;
00167                 if(x>=p_half) x -= p;
00168             }
00169         }
00170         GET_HIGH_WORD(hx,x);
00171         SET_HIGH_WORD(x,hx^sx);
00172         return x;
00173     }
00174 #endif
00175 
00176 #else // Linux
00177 
00178     #include <stdlib.h>
00179     #include <string.h>
00180     void getFaustPathname(char* str, unsigned int size)
00181     {
00182         char* path = getenv("_");
00183         if (path) {
00184             strncpy(str, path, size);
00185         } else {
00186             // prevent the case of _ undefined
00187             strncpy(str, "/usr/local/bin/faust", size);
00188         } 
00189     }
00190 
00191 #endif
00192 
00193