testing
[gnucap-felix.git] / src / md.h
blob7b7228092c6fdeb894736ddf134577138e733b6b
1 /* -*- C++ -*-
2 * Copyright (C) 2001 Albert Davis
3 * Author: Albert Davis <aldavis@gnu.org>
5 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *------------------------------------------------------------------
22 * Machine dependent, configuration, and standard includes
24 #ifndef MD_H_INCLUDED
25 #define MD_H_INCLUDED
26 /*--------------------------------------------------------------------------*/
27 // float type to use for currents, voltages & stuff
28 // typedef double double; // has to be double
29 //typedef long double hp_float_t;
30 #define HAVE_DOUBLE_TYPES
31 typedef double voltage_t;
32 typedef double current_t;
33 typedef double charge_t;
34 typedef double conductance_t;
35 typedef double hp_float_t;
36 #define HAVE_UINT_T
37 typedef unsigned int uint_t;
38 /*--------------------------------------------------------------------------*/
39 // some functions are not const in upstream yet.
40 #define GCUF_CONST const
41 /*--------------------------------------------------------------------------*/
42 // FIXME!!
43 typedef double fun_t;
44 inline double to_fun_t(double x){return x;}
45 /*--------------------------------------------------------------------------*/
46 /* std collection of includes */
47 // system
48 #include <new>
49 #include <cstdarg>
50 #include <cassert>
51 #include <cfloat>
52 #include <cmath>
53 #include <climits>
54 #include <limits>
55 #include <cstdio>
56 #include <cerrno>
57 #include <csetjmp>
58 #include <csignal>
59 #include <cstring>
60 #include <iostream>
61 // types
62 #include <complex>
63 #include <string>
64 // containers
65 #include <list>
66 #include <vector>
67 #include <queue>
68 #include <map>
69 #include <valarray>
70 // algorithms
71 #include <typeinfo>
72 #include <algorithm>
73 /* usual but non-standard (POSIX??) collection of includes */
74 #include <unistd.h> /* chdir, access, getcwd */
75 #include <fcntl.h> /* old style unix files */
76 /*--------------------------------------------------------------------------*/
77 /* constants related to memory size, word size, etc */
78 enum {
79 BUFLEN = 256,
80 BIGBUFLEN = 4096
82 /*--------------------------------------------------------------------------*/
83 /* user interface preferences */
84 #define I_PROMPT "gnucap> "
85 #define CKT_PROMPT ">"
86 #define ANTI_COMMENT "*>"
87 #define DEFAULT_LANGUAGE "acs"
88 /*--------------------------------------------------------------------------*/
89 #if defined(__WIN32__)
90 #define ENDDIR "/\\"
91 #define PATHSEP ';'
92 #define SYSTEMSTARTFILE "gnucap.rc"
93 #define SYSTEMSTARTPATH OS::getenv("PATH")
94 #define CWDSTARTFILE "gnucap.rc"
95 #define USERSTARTFILE "gnucap.rc"
96 #define USERSTARTPATH OS::getenv("HOME")
97 #define STEPFILE "/tmp/SXXXXXX"
98 #define SHELL OS::getenv("COMSPEC")
99 /*--------------------------------------------------------------------------*/
100 #else
101 #define ENDDIR "/"
102 #define PATHSEP ':'
103 #define SYSTEMSTARTFILE "gnucap.rc"
104 #define SYSTEMSTARTPATH "/etc"
105 #define CWDSTARTFILE "gnucap.rc"
106 #define USERSTARTFILE ".gnucaprc"
107 #define USERSTARTPATH OS::getenv("HOME")
108 #define STEPFILE "/tmp/SXXXXXX"
109 #define SHELL OS::getenv("SHELL")
110 #endif
111 /*--------------------------------------------------------------------------*/
113 // for pointer hashing.
114 #define PRIME 2001
115 /*--------------------------------------------------------------------------*/
116 /* machine and compiler patches */
117 #if defined(__MINGW32__)
118 #define SIGSETJMP_IS_BROKEN
119 #define MS_DLL
120 #endif
121 /*--------------------------------------------------------------------------*/
122 /* some convenient names */
123 typedef std::complex<hp_float_t> hCOMPLEX;
124 typedef std::pair<hp_float_t,hp_float_t> hDPAIR;
125 #ifdef COMPLEX
126 # error COMPLEX mess
127 #endif
128 typedef std::complex<double> COMPLEX;
129 typedef std::pair<double,double> DPAIR;
130 const double inf = std::numeric_limits<float>::infinity( );
131 /*--------------------------------------------------------------------------*/
132 // dynamic cast kluge.
133 // Strictly, this should always be dynamic_cast, but if it has already
134 // been checked, don't bother checking again, hence static_cast.
135 // It works and is faster.
136 #if defined(NDEBUG)
137 #define prechecked_cast static_cast
138 #else
139 #define prechecked_cast dynamic_cast
140 #endif
143 template <class T, class S>
144 inline T asserted_cast(S s){
145 T x = prechecked_cast<T>(s);
146 assert(x);
147 return x;
150 /*--------------------------------------------------------------------------*/
151 /* portability hacks */
153 #if !defined(MS_DLL)
154 // The usual way for POSIX compliant systems
155 #include <dlfcn.h>
156 #define INTERFACE
157 #else
158 // Microsoft DLL hacks -- thanks to Holger Vogt and Cesar Strauss for the info
159 // Make the MS DLL functions look like the posix ones.
160 #include <windows.h>
161 #undef min
162 #undef max
163 #undef INTERFACE
164 #ifdef MAKE_DLL
165 #define INTERFACE __declspec(dllimport)
166 #else
167 #define INTERFACE __declspec(dllexport)
168 #endif
170 inline void* dlopen(const char* f, int)
172 trace0("Loadlibrary wrapper");
173 return LoadLibrary(const_cast<char*>(f));
176 inline void dlclose(void* h)
178 FreeLibrary((HINSTANCE)h);
181 inline char* dlerror()
183 static LPVOID lpMsgBuf = NULL;
184 // free the error message buffer
185 if (lpMsgBuf) {
186 LocalFree(lpMsgBuf);
188 // get the error code
189 DWORD dw = GetLastError();
190 // get the corresponding error message
191 FormatMessage(
192 FORMAT_MESSAGE_ALLOCATE_BUFFER |
193 FORMAT_MESSAGE_FROM_SYSTEM |
194 FORMAT_MESSAGE_IGNORE_INSERTS,
195 NULL,
197 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
198 (LPTSTR) &lpMsgBuf,
199 0, NULL);
200 return (char*)lpMsgBuf;
202 #define RTLD_LAZY 0x00001 /* Lazy function call binding. */
203 #define RTLD_NOW 0x00002 /* Immediate function call binding. */
204 #define RTLD_BINDING_MASK 0x3 /* Mask of binding time value. */
205 #define RTLD_NOLOAD 0x00004 /* Do not load the object. */
206 #define RTLD_DEEPBIND 0x00008 /* Use deep binding. */
207 #define RTLD_GLOBAL 0x00100
208 #define RTLD_LOCAL 0
209 #define RTLD_NODELETE 0x01000
210 #endif
212 #if defined(SIGSETJMP_IS_BROKEN)
213 #undef sigjmp_buf
214 #undef siglongjmp
215 #undef sigsetjmp
216 #define sigjmp_buf jmp_buf
217 #define siglongjmp(a,b) longjmp(a,b)
218 #define sigsetjmp(a,b) setjmp(a)
219 #endif
221 #if !defined(SIGNALARGS)
222 #define SIGNALARGS int
223 #endif
224 /*--------------------------------------------------------------------------*/
225 /* temporary hacks */
226 enum RUN_MODE {
227 rPRE_MAIN, /* it hasn't got to main yet */
228 rPRESET, /* do set up commands now, but not simulation */
229 /* store parameters, so bare invocation of a */
230 /* simulation command will do it this way. */
231 rINTERACTIVE, /* run the commands, interactively */
232 rSCRIPT, /* execute now, as a command, then restore mode */
233 rBATCH, /* execute now, as a command, then exit */
234 rPIPE // unbuffered, like script.
236 class INTERFACE ENV {
237 public:
238 static RUN_MODE run_mode; // variations on handling of dot commands
239 static int error; // error return code
241 /*--------------------------------------------------------------------------*/
242 /* my standard collection of includes */
243 #include "io_trace.h"
244 #include "io_error.h"
245 /*--------------------------------------------------------------------------*/
247 using std::string;
248 using std::ostream;
249 /*--------------------------------------------------------------------------*/
250 inline double fmin(double x, double y, double z){
251 return ( fmin(fmin(x,y),z));
253 inline double fmax(double x, double y, double z){
254 return ( fmax(fmax(x,y),z));
256 #define HAVE_IS_NUMBER
257 inline bool is_number(long double x){
258 return (( x != inf ) && (x != -inf ) && (x == x)) ;
260 inline bool is_number(double x){
261 return (( x != inf ) && (x != -inf ) && (x == x)) ;
263 inline bool is_almost(double x, double y){
264 return ( fabs(x-y) / ( fmax(fabs(x),fabs(y))+1e-20) < 1e-10 || fabs(x-y)<1e-12);
266 inline double square(double x){return x*x;}
267 inline long double square(long double x){return x*x;}
268 /*--------------------------------------------------------------------------*/
269 #endif
270 // vim:ts=8:sw=2:noet: