00001
00002
00003
00004 #ifndef CPU_H_
00005 #define CPU_H_
00006
00009
00010 #ifdef _MSC_VER
00011 #pragma warning( disable : 4127 )
00012 #endif
00013
00015 #ifndef INT16_MAX
00016 #define INT16_MAX 32767
00017 #endif
00018 #ifndef INT16_MIN
00019 #define INT16_MIN (-INT16_MAX - 1)
00020 #endif
00021
00022 #if SHRT_MAX == INT16_MAX
00023 typedef short int int16;
00024 #elif INT_MAX == INT16_MAX
00025 typedef int int16;
00026 #else
00027 #error failed to define int16, please report this to gary@pa.uky.edu
00028 #endif
00029
00030 #ifndef UINT16_MAX
00031 #define UINT16_MAX 65535
00032 #endif
00033
00034 #if USHRT_MAX == UINT16_MAX
00035 typedef unsigned short int uint16;
00036 #elif UINT_MAX == UINT16_MAX
00037 typedef unsigned int uint16;
00038 #else
00039 #error failed to define uint16, please report this to gary@pa.uky.edu
00040 #endif
00041
00042 #ifndef INT32_MAX
00043 #define INT32_MAX 2147483647L
00044 #endif
00045 #ifndef INT32_MIN
00046 #define INT32_MIN (-INT32_MAX - 1)
00047 #endif
00048
00049 #if INT_MAX == INT32_MAX
00050 typedef int int32;
00051 #elif LONG_MAX == INT32_MAX
00052 typedef long int int32;
00053 #else
00054 #error failed to define int32, please report this to gary@pa.uky.edu
00055 #endif
00056
00057 #ifndef UINT32_MAX
00058 #define UINT32_MAX 4294967295UL
00059 #endif
00060
00061 #if UINT_MAX == UINT32_MAX
00062 typedef unsigned int uint32;
00063 #elif ULONG_MAX == UINT32_MAX
00064 typedef unsigned long int uint32;
00065 #else
00066 #error failed to define uint32, please report this to gary@pa.uky.edu
00067 #endif
00068
00069 #if LONG_MAX > INT32_MAX
00070
00071
00072
00073
00074
00075
00076
00077 #undef INT64_MAX
00078 #define INT64_MAX 9223372036854775807L
00079
00080 #undef INT64_MIN
00081 #define INT64_MIN (-INT64_MAX - 1L)
00082
00083 #if LONG_MAX == INT64_MAX
00084 #define HAVE_INT64 1
00085 typedef long int int64;
00086 #endif
00087
00088 #endif
00089
00090 #if ULONG_MAX > UINT32_MAX
00091
00092 #undef UINT64_MAX
00093 #define UINT64_MAX 18446744073709551615UL
00094
00095 #if ULONG_MAX == UINT64_MAX
00096 #define HAVE_UINT64 1
00097 typedef unsigned long int uint64;
00098 #endif
00099
00100 #endif
00101
00123 #ifdef cray
00124 #ifndef __cray
00125 #define __cray 1
00126 #endif
00127 #endif
00128
00130 #ifdef __x86_64
00131 #ifndef __amd64
00132 #define __amd64 1
00133 #endif
00134 #endif
00135
00136 #if defined(_ARCH_PPC) || defined(__POWERPC__) || defined(__powerpc__) || defined(PPC)
00137 #ifndef __ppc__
00138 #define __ppc__ 1
00139 #endif
00140 #endif
00141
00147 #if defined(unix) || defined(__unix__)
00148 #ifndef __unix
00149 #define __unix 1
00150 #endif
00151 #endif
00152
00154 #ifdef __ECC
00155 #ifndef __ICC
00156 #define __ICC __ECC
00157 #endif
00158 #endif
00159
00161 #undef __GNUC_EXCL__
00162 #if defined(__GNUC__) && ! ( defined(__ICC) || defined(__PATHSCALE__) || defined(__OPENCC__) || defined(__clang__) )
00163 #define __GNUC_EXCL__ 1
00164 #endif
00165
00166
00167 #ifndef HAVE_FUNC
00168 #undef __func__
00169 #define __func__ DEBUG_ENTRY.name()
00170 #endif
00171
00172
00175
00176 const realnum BIGFLOAT = numeric_limits<realnum>::max()/realnum(100.f);
00178 const realnum SMALLFLOAT = numeric_limits<realnum>::min()*realnum(100.f);
00179
00181 const double BIGDOUBLE = DBL_MAX/100.;
00182 const double SMALLDOUBLE = DBL_MIN*100.;
00183
00184 const int STDLEN = 32;
00185
00193 typedef enum { AS_DATA_ONLY_TRY, AS_DATA_LOCAL_TRY, AS_LOCAL_DATA_TRY, AS_LOCAL_ONLY_TRY,
00194 AS_DATA_ONLY, AS_DATA_OPTIONAL, AS_DATA_LOCAL, AS_LOCAL_DATA, AS_LOCAL_ONLY } access_scheme;
00195
00196
00197
00198 const ios_base::openmode mode_r = ios_base::in;
00199 const ios_base::openmode mode_w = ios_base::out | ios_base::trunc;
00200 const ios_base::openmode mode_a = ios_base::out | ios_base::app;
00201 const ios_base::openmode mode_rp = ios_base::in | ios_base::out;
00202 const ios_base::openmode mode_wp = ios_base::in | ios_base::out | ios_base::trunc;
00203 const ios_base::openmode mode_ap = ios_base::in | ios_base::out | ios_base::app;
00204
00205 const ios_base::openmode mode_rb = mode_r | ios_base::binary;
00206 const ios_base::openmode mode_wb = mode_w | ios_base::binary;
00207 const ios_base::openmode mode_ab = mode_a | ios_base::binary;
00208 const ios_base::openmode mode_rpb = mode_rp | ios_base::binary;
00209 const ios_base::openmode mode_wpb = mode_wp | ios_base::binary;
00210 const ios_base::openmode mode_apb = mode_ap | ios_base::binary;
00211
00212 FILE* open_data( const char* fname, const char* mode, access_scheme scheme=AS_DATA_ONLY );
00213 void open_data( fstream& stream, const char* fname, ios_base::openmode mode, access_scheme scheme=AS_DATA_ONLY );
00214
00215
00216
00217 class t_cpu_i
00218 {
00221 union
00222 {
00223 char c[4];
00224 int32 i;
00225 } endian;
00226
00227 sys_float test_float;
00228 double test_double;
00229
00230 int32 Float_SNaN_Value;
00231 # ifdef HAVE_INT64
00232 int64 Double_SNaN_Value;
00233 # else
00234 int32 Double_SNaN_Value[2];
00235 # endif
00236
00237 # ifdef __unix
00238 struct sigaction p_action;
00239 struct sigaction p_default;
00240 # endif
00241
00243 bool p_lgAssertAbort;
00244
00246 long n_avail_CPU;
00248 bool p_lgMPI;
00253 bool p_lgMPISingleRankMode;
00255 long n_rank;
00257 char HostName[STDLEN];
00259 vector<string> chSearchPath;
00261 char p_chDirSeparator;
00262 int nFileDone;
00263
00264 void enable_traps() const;
00265 static void signal_handler(int sig);
00266
00267 vector<string> p_exit_status;
00268
00269 void getPathList( const char* fname, vector<string>& PathList, access_scheme scheme ) const;
00270 public:
00271 t_cpu_i();
00272
00273 bool big_endian() const { return ( endian.i == 0x12345678 ); }
00274 bool little_endian() const { return ( endian.i == 0x78563412 ); }
00275
00276 sys_float min_float() const { return test_float; }
00277 double min_double() const { return test_double; }
00278
00279 # ifdef __unix
00280 const struct sigaction* action() const { return &p_action; }
00281 const struct sigaction* deflt() const { return &p_default; }
00282 # endif
00283
00284 void set_signal_handlers();
00285
00286 void setAssertAbort(bool val)
00287 {
00288 p_lgAssertAbort = val;
00289 #ifdef CATCH_SIGNAL
00290 # ifdef __unix
00291 if( val )
00292 sigaction( SIGABRT, deflt(), NULL );
00293 else
00294 sigaction( SIGABRT, action(), NULL );
00295 # endif
00296 # ifdef _MSC_VER
00297 if( val )
00298 signal( SIGABRT, SIG_DFL );
00299 else
00300 signal( SIGABRT, &signal_handler );
00301 # endif
00302 #endif
00303 }
00304 bool lgAssertAbort() const { return p_lgAssertAbort; }
00305
00306 void set_nCPU(long n) { n_avail_CPU = n; }
00307 long nCPU() const { return n_avail_CPU; }
00308 bool lgMPI() const { return p_lgMPI; }
00309 void set_MPISingleRankMode( bool mode ) { p_lgMPISingleRankMode = mode; }
00310 bool lgMPISingleRankMode() const { return p_lgMPISingleRankMode; }
00311 void set_nRANK(long n) { n_rank = n; }
00312 long nRANK() const { return n_rank; }
00313 bool lgMaster() const { return ( n_rank == 0 ); }
00314 bool lgMPI_talk() const { return lgMaster() || lgMPISingleRankMode(); }
00315 const char *host_name() const { return HostName; }
00316 void printDataPath() const;
00317 char chDirSeparator() const { return p_chDirSeparator; }
00318 bool firstOpen() const { return ( nFileDone == 0 ); }
00319 const string& chExitStatus(exit_type s) const { return p_exit_status[s]; }
00320
00321 friend FILE* open_data( const char* fname, const char* mode, access_scheme scheme );
00322 friend void open_data( fstream& stream, const char* fname, ios_base::openmode mode, access_scheme scheme );
00323
00324 friend void set_NaN(sys_float &x);
00325 friend void set_NaN(sys_float x[], long n);
00326 friend void set_NaN(double &x);
00327 friend void set_NaN(double x[], long n);
00328 };
00329 class t_cpu
00330 {
00331 static t_cpu_i *m_i;
00332 public:
00333 t_cpu_i &i()
00334 {
00335 return *m_i;
00336 }
00337 t_cpu();
00338 ~t_cpu();
00339 };
00340
00341 static t_cpu cpu;
00342
00343
00344
00345
00346
00347
00348
00349
00351 void set_NaN(sys_float &x);
00352 void set_NaN(sys_float x[],
00353 long n);
00354 void set_NaN(double &x);
00355 void set_NaN(double x[],
00356 long n);
00357
00359 bool MyIsnan(const sys_float &x);
00360 bool MyIsnan(const double &x);
00361
00362
00363
00364 #ifdef _MSC_VER
00365 #define NORETURN __declspec(noreturn)
00366 #elif defined(__GNUC__) || ( defined(__INTEL_COMPILER) && defined(__linux) )
00367 #define NORETURN __attribute__ ((noreturn))
00368 #else
00369 #define NORETURN
00370 #endif
00371
00372 #define RESTRICT
00373 #define UNLIKELY(x) (x)
00374 #ifdef __GNUC__
00375 #if (__GNUC__ >= 3)
00376 #undef UNLIKELY
00377 #define UNLIKELY(x) __builtin_expect((x),0)
00378 #endif
00379 #undef RESTRICT
00380 #define RESTRICT __restrict
00381 #define UNUSED __attribute__ ((unused))
00382 #else
00383 #define UNUSED
00384 #endif
00385
00386
00387
00388
00389
00390 #define DO_EXPAND(VAL) VAL ## 1
00391 #define EXPAND(VAL) DO_EXPAND(VAL)
00392
00393
00394
00395
00396 #if defined __INTEL_COMPILER
00397 # define __COMP "icc"
00398 # define __COMP_VER __INTEL_COMPILER
00399
00400
00401
00402 #elif defined __PATHSCALE__
00403 # define __COMP "pathCC"
00404 # define __COMP_VER __PATHCC__ * 100 + __PATHCC_MINOR__ * 10 + __PATHCC_PATCHLEVEL__
00405
00406
00407
00408 #elif defined __OPENCC__
00409 # define __COMP "Open64"
00410 # if EXPAND(__OPENCC_PATCHLEVEL__) == 1
00411 # define __COMP_VER __OPENCC__ * 100 + __OPENCC_MINOR__ * 10
00412 # else
00413 # define __COMP_VER __OPENCC__ * 100 + __OPENCC_MINOR__ * 10 + __OPENCC_PATCHLEVEL__
00414 # endif
00415
00416
00417
00418 #elif defined __clang__
00419 # define __COMP "clang++"
00420 # define __COMP_VER __clang_major__ * 100 + __clang_minor__ * 10 + __clang_patchlevel__
00421
00422
00423 #elif defined __GNUC__
00424 # define __COMP "g++"
00425 # if defined(__GNUC_PATCHLEVEL__)
00426 # define __COMP_VER (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
00427 # else
00428 # define __COMP_VER (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
00429 # endif
00430
00431 #elif defined __PGI
00432 # define __COMP "Portland Group"
00433 # if defined(__PGIC__)
00434 # define __COMP_VER (__PGIC__ * 100 + __PGIC_MINOR__ * 10 + __PGIC_PATCHLEVEL__)
00435 # else
00436 # define __COMP_VER 0
00437 # endif
00438
00439
00440
00441 #elif defined(__sgi) && defined(_COMPILER_VERSION)
00442 # define __COMP "MIPSpro"
00443 # define __COMP_VER _COMPILER_VERSION
00444
00445
00446 #elif defined __HP_aCC
00447 # define __COMP "HP aCC"
00448 # define __COMP_VER __HP_aCC
00449
00450
00451 #elif defined __DECC
00452 # define __COMP "DEC CC"
00453 # define __COMP_VER __DECC_VER
00454
00455
00456 #elif defined _MSC_VER
00457 # define __COMP "vs"
00458 # define __COMP_VER _MSC_VER
00459
00460
00461 #elif defined __SUNPRO_CC
00462 # define __COMP "Solaris Studio"
00463 # define __COMP_VER __SUNPRO_CC
00464
00465
00466 #else
00467 # define __COMP "unknown"
00468 # define __COMP_VER 0
00469 #endif
00470
00471
00472
00473 #if defined __linux
00474 # if defined __i386
00475 # define __OS "Linux (IA32)"
00476 # elif defined __amd64
00477 # define __OS "Linux (AMD64)"
00478 # elif defined __ia64
00479 # define __OS "Linux (IA64)"
00480 # elif defined __ppc__
00481 # define __OS "Linux (PowerPC)"
00482 # else
00483 # define __OS "Linux (other)"
00484 # endif
00485
00486
00487 #elif defined macintosh
00488 # define __OS "Mac OS 9"
00489
00490
00491 #elif defined __MACOSX__
00492 # define __OS "Mac OS X"
00493
00494
00495 #elif defined __APPLE__
00496 # define __OS "Apple MacOS"
00497
00498
00499 #elif defined hpux
00500 # define __OS "HP-UX"
00501
00502
00503 #elif defined __sun
00504 # define __OS "Solaris"
00505
00506
00507 #elif defined _AIX
00508 # define __OS "AIX"
00509
00510
00511 #elif defined ultrix
00512 # define __OS "Ultrix"
00513
00514
00515 #elif defined __FreeBSD__
00516 # define __OS "FreeBSD"
00517
00518 #elif defined __NetBSD__
00519 # define __OS "NetBSD"
00520
00521 #elif defined __OpenBSD__
00522 # define __OS "OpenBSD"
00523
00524
00525
00526 #elif defined _WIN64
00527 # define __OS "Win64"
00528
00529
00530 #elif defined _WIN32
00531 # define __OS "Win32"
00532
00533
00534 #elif defined __CYGWIN__
00535 # define __OS "Cygwin"
00536
00537
00538 #elif defined __sgi
00539 # define __OS "IRIX"
00540
00541
00542 #else
00543 # define __OS "unknown"
00544 #endif
00545
00546
00547 #ifndef MM
00548
00549 #if defined(__GNUC_EXCL__) && ((__GNUC__ == 2 && __GNUC_MINOR__ == 96) || (__GNUC__ == 3 && __GNUC_MINOR__ == 4))
00550 #error "This g++ version cannot compile Cloudy and must not be used! Please update g++ to a functional version. See http://wiki.nublado.org/wiki/CompilingCloudy for more details."
00551 #endif
00552
00553 #if __INTEL_COMPILER >= 1200 && __INTEL_COMPILER < 1300
00554 #error "This icc version cannot compile Cloudy and must not be used! Please use a functional version of icc, or g++. See http://wiki.nublado.org/wiki/CompilingCloudy for more details."
00555 #endif
00556 #endif
00557
00558 #ifdef _MSC_VER
00559 #pragma warning( default : 4127 )
00560 #endif
00561
00562 #endif