| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #ifndef _CDDEFINES_H_ |
|---|
| 5 | #define _CDDEFINES_H_ |
|---|
| 6 | |
|---|
| 7 | #ifdef _MSC_VER |
|---|
| 8 | |
|---|
| 9 | # pragma warning( disable : 4127 ) |
|---|
| 10 | |
|---|
| 11 | # ifndef WIN32_LEAN_AND_MEAN |
|---|
| 12 | # define WIN32_LEAN_AND_MEAN |
|---|
| 13 | # endif |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <math.h> |
|---|
| 27 | #include <assert.h> |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include <float.h> |
|---|
| 30 | #include <limits> |
|---|
| 31 | #include <limits.h> |
|---|
| 32 | #include <time.h> |
|---|
| 33 | #include <signal.h> |
|---|
| 34 | #include <string> |
|---|
| 35 | #include <sstream> |
|---|
| 36 | #include <vector> |
|---|
| 37 | #include <valarray> |
|---|
| 38 | #include <complex> |
|---|
| 39 | #include <map> |
|---|
| 40 | #include <memory> |
|---|
| 41 | #include <stdexcept> |
|---|
| 42 | #include <algorithm> |
|---|
| 43 | #include <fstream> |
|---|
| 44 | #include <bitset> |
|---|
| 45 | #ifdef DMALLOC |
|---|
| 46 | #include <dmalloc.h> |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | using namespace std; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | #ifndef EXTERN |
|---|
| 60 | #define EXTERN extern |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | #undef STATIC |
|---|
| 64 | #ifdef USE_GPROF |
|---|
| 65 | #define STATIC |
|---|
| 66 | #else |
|---|
| 67 | #define STATIC static |
|---|
| 68 | #endif |
|---|
| 69 | |
|---|
| 70 | #ifdef FLT_IS_DBL |
|---|
| 71 | typedef double realnum; |
|---|
| 72 | #else |
|---|
| 73 | typedef float realnum; |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | template<bool> struct StaticAssertFailed; |
|---|
| 78 | template<> struct StaticAssertFailed<true> {}; |
|---|
| 79 | #define STATIC_ASSERT(x) ((void)StaticAssertFailed< (x) == true >()) |
|---|
| 80 | |
|---|
| 81 | typedef float sys_float; |
|---|
| 82 | |
|---|
| 83 | #define float PLEASE_USE_REALNUM_NOT_FLOAT |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | #include "cpu.h" |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | template<typename T> class Singleton |
|---|
| 113 | { |
|---|
| 114 | public: |
|---|
| 115 | static T& Inst() |
|---|
| 116 | { |
|---|
| 117 | static T instance; |
|---|
| 118 | return instance; |
|---|
| 119 | } |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | EXTERN FILE *ioQQQ; |
|---|
| 133 | |
|---|
| 134 | EXTERN FILE *ioStdin; |
|---|
| 135 | |
|---|
| 136 | extern FILE *ioMAP; |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | EXTERN FILE* ioPrnErr; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | EXTERN bool lgAbort; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | EXTERN bool lgTestCodeCalled; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | EXTERN bool lgTestCodeEnabled; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | EXTERN bool lgPrnErr; |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | EXTERN long int nzone; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | EXTERN double fnzone; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | EXTERN long int iteration; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | EXTERN long int nSimThisCoreload; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | extern const double ZeroNum; |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | const int FILENAME_PATH_LENGTH = 200; |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | const int FILENAME_PATH_LENGTH_2 = FILENAME_PATH_LENGTH*2; |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | const int INPUT_LINE_LENGTH = 200; |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | const int LIMELM = 30; |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | const int NISO = 2; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | const int NHYDRO_MAX_LEVEL = 401; |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | const double MAX_DENSITY = 1.e21; |
|---|
| 213 | |
|---|
| 214 | #if 0 |
|---|
| 215 | |
|---|
| 216 | const double TEMP_STOP_DEFAULT = 4000.; |
|---|
| 217 | |
|---|
| 218 | const double TEMP_LIMIT_HIGH = 1e10; |
|---|
| 219 | |
|---|
| 220 | const double TEMP_LIMIT_LOW = 2.8; |
|---|
| 221 | #endif |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | const double DEPTH_OFFSET = 1.e-30; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | enum collider { |
|---|
| 228 | ipELECTRON, |
|---|
| 229 | ipPROTON, |
|---|
| 230 | ipHE_PLUS, |
|---|
| 231 | ipALPHA, |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | ipNCOLLIDER |
|---|
| 237 | }; |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | const int ipRecEsc = 2; |
|---|
| 242 | |
|---|
| 243 | const int ipRecNetEsc = 1; |
|---|
| 244 | |
|---|
| 245 | const int ipRecRad = 0; |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | const int ipPRD = 1; |
|---|
| 253 | |
|---|
| 254 | const int ipCRD = -1; |
|---|
| 255 | |
|---|
| 256 | const int ipCRDW = 2; |
|---|
| 257 | |
|---|
| 258 | const int ipLY_A = -2; |
|---|
| 259 | |
|---|
| 260 | const int ipDEST_K2 = 1; |
|---|
| 261 | |
|---|
| 262 | const int ipDEST_INCOM = 2; |
|---|
| 263 | |
|---|
| 264 | const int ipDEST_SIMPL = 3; |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | const int ipH1s = 0; |
|---|
| 269 | const int ipH2s = 1; |
|---|
| 270 | const int ipH2p = 2; |
|---|
| 271 | const int ipH3s = 3; |
|---|
| 272 | const int ipH3p = 4; |
|---|
| 273 | const int ipH3d = 5; |
|---|
| 274 | const int ipH4s = 6; |
|---|
| 275 | const int ipH4p = 7; |
|---|
| 276 | const int ipH4d = 8; |
|---|
| 277 | const int ipH4f = 9; |
|---|
| 278 | const int ipH5s = 10; |
|---|
| 279 | const int ipH5p = 11; |
|---|
| 280 | const int ipH5d = 12; |
|---|
| 281 | const int ipH5f = 13; |
|---|
| 282 | const int ipH5g = 14; |
|---|
| 283 | const int ipH6s = 15; |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | const int ipHe1s1S = 0; |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | const int ipHe2s3S = 1; |
|---|
| 292 | const int ipHe2s1S = 2; |
|---|
| 293 | const int ipHe2p3P0 = 3; |
|---|
| 294 | const int ipHe2p3P1 = 4; |
|---|
| 295 | const int ipHe2p3P2 = 5; |
|---|
| 296 | const int ipHe2p1P = 6; |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | const int ipHe3s3S = 7; |
|---|
| 300 | const int ipHe3s1S = 8; |
|---|
| 301 | const int ipHe3p3P = 9; |
|---|
| 302 | const int ipHe3d3D = 10; |
|---|
| 303 | const int ipHe3d1D = 11; |
|---|
| 304 | const int ipHe3p1P = 12; |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | const int ipHe4s3S = 13; |
|---|
| 308 | const int ipHe4s1S = 14; |
|---|
| 309 | const int ipHe4p3P = 15; |
|---|
| 310 | const int ipHe4d3D = 16; |
|---|
| 311 | const int ipHe4d1D = 17; |
|---|
| 312 | const int ipHe4f3F = 18; |
|---|
| 313 | const int ipHe4f1F = 19; |
|---|
| 314 | const int ipHe4p1P = 20; |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | const int ipH_LIKE = 0; |
|---|
| 320 | const int ipHE_LIKE = 1; |
|---|
| 321 | const int ipLI_LIKE = 2; |
|---|
| 322 | const int ipBE_LIKE = 3; |
|---|
| 323 | const int ipB_LIKE = 4; |
|---|
| 324 | const int ipC_LIKE = 5; |
|---|
| 325 | const int ipN_LIKE = 6; |
|---|
| 326 | const int ipO_LIKE = 7; |
|---|
| 327 | const int ipF_LIKE = 8; |
|---|
| 328 | const int ipNE_LIKE = 9; |
|---|
| 329 | const int ipNA_LIKE = 10; |
|---|
| 330 | const int ipMG_LIKE = 11; |
|---|
| 331 | const int ipAL_LIKE = 12; |
|---|
| 332 | const int ipSI_LIKE = 13; |
|---|
| 333 | const int ipP_LIKE = 14; |
|---|
| 334 | const int ipS_LIKE = 15; |
|---|
| 335 | const int ipCL_LIKE = 16; |
|---|
| 336 | const int ipAR_LIKE = 17; |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | const int ipHYDROGEN = 0; |
|---|
| 340 | const int ipHELIUM = 1; |
|---|
| 341 | const int ipLITHIUM = 2; |
|---|
| 342 | const int ipBERYLLIUM = 3; |
|---|
| 343 | const int ipBORON = 4; |
|---|
| 344 | const int ipCARBON = 5; |
|---|
| 345 | const int ipNITROGEN = 6; |
|---|
| 346 | const int ipOXYGEN = 7; |
|---|
| 347 | const int ipFLUORINE = 8; |
|---|
| 348 | const int ipNEON = 9; |
|---|
| 349 | const int ipSODIUM = 10; |
|---|
| 350 | const int ipMAGNESIUM = 11; |
|---|
| 351 | const int ipALUMINIUM = 12; |
|---|
| 352 | const int ipSILICON = 13; |
|---|
| 353 | const int ipPHOSPHORUS = 14; |
|---|
| 354 | const int ipSULPHUR = 15; |
|---|
| 355 | const int ipCHLORINE = 16; |
|---|
| 356 | const int ipARGON = 17; |
|---|
| 357 | const int ipPOTASSIUM = 18; |
|---|
| 358 | const int ipCALCIUM = 19; |
|---|
| 359 | const int ipSCANDIUM = 20; |
|---|
| 360 | const int ipTITANIUM = 21; |
|---|
| 361 | const int ipVANADIUM = 22; |
|---|
| 362 | const int ipCHROMIUM = 23; |
|---|
| 363 | const int ipMANGANESE = 24; |
|---|
| 364 | const int ipIRON = 25; |
|---|
| 365 | const int ipCOBALT = 26; |
|---|
| 366 | const int ipNICKEL = 27; |
|---|
| 367 | const int ipCOPPER = 28; |
|---|
| 368 | const int ipZINC = 29; |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | double fudge(long int ipnt); |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | void broken(void); |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | void fixit(void); |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | void CodeReview(void); |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | void TestCode(void); |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | void *MyMalloc(size_t size, const char *file, int line); |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | void *MyCalloc(size_t num, size_t size); |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | void *MyRealloc(void *p, size_t size); |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | void MyAssert(const char *file, int line); |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | NORETURN void cdExit(int iexit); |
|---|
| 430 | |
|---|
| 431 | class cloudy_exit |
|---|
| 432 | { |
|---|
| 433 | const char* p_routine; |
|---|
| 434 | const char* p_file; |
|---|
| 435 | long p_line; |
|---|
| 436 | int p_exit; |
|---|
| 437 | public: |
|---|
| 438 | cloudy_exit(const char* routine, const char* file, long line, int exit_code) |
|---|
| 439 | { |
|---|
| 440 | p_routine = routine; |
|---|
| 441 | p_file = file; |
|---|
| 442 | p_line = line; |
|---|
| 443 | p_exit = exit_code; |
|---|
| 444 | } |
|---|
| 445 | virtual ~cloudy_exit() throw() |
|---|
| 446 | { |
|---|
| 447 | p_routine = NULL; |
|---|
| 448 | p_file = NULL; |
|---|
| 449 | } |
|---|
| 450 | const char* routine() const throw() |
|---|
| 451 | { |
|---|
| 452 | return p_routine; |
|---|
| 453 | } |
|---|
| 454 | const char* file() const throw() |
|---|
| 455 | { |
|---|
| 456 | return p_file; |
|---|
| 457 | } |
|---|
| 458 | long line() const |
|---|
| 459 | { |
|---|
| 460 | return p_line; |
|---|
| 461 | } |
|---|
| 462 | int exit_status() const |
|---|
| 463 | { |
|---|
| 464 | return p_exit; |
|---|
| 465 | } |
|---|
| 466 | }; |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | #define cdEXIT( FAIL ) throw cloudy_exit( __func__, __FILE__, __LINE__, FAIL ) |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | #define puts( STR ) Using_puts_before_cdEXIT_is_no_longer_needed |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | void ShowMe(void); |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | NORETURN void TotalInsanity(void); |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | NORETURN void BadRead(void); |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | NORETURN void BadOpen(void); |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | NORETURN void NoNumb(char *chCard); |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | int dbg_printf(int debug, const char *fmt, ...); |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | int dprintf(FILE *fp, const char *format, ...); |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | char *read_whole_line( char *chLine , int nChar , FILE *ioIN ); |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | #ifndef NDEBUG |
|---|
| 520 | # define DEBUG |
|---|
| 521 | #else |
|---|
| 522 | # undef DEBUG |
|---|
| 523 | #endif |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | #if defined(malloc) |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | # define MALLOC(exp) (malloc(exp)) |
|---|
| 530 | #else |
|---|
| 531 | |
|---|
| 532 | # define MALLOC(exp) (MyMalloc(exp,__FILE__, __LINE__)) |
|---|
| 533 | #endif |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | #if defined(calloc) |
|---|
| 537 | |
|---|
| 538 | # define CALLOC calloc |
|---|
| 539 | #else |
|---|
| 540 | |
|---|
| 541 | # define CALLOC MyCalloc |
|---|
| 542 | #endif |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | #if defined(realloc) |
|---|
| 546 | |
|---|
| 547 | # define REALLOC realloc |
|---|
| 548 | #else |
|---|
| 549 | |
|---|
| 550 | # define REALLOC MyRealloc |
|---|
| 551 | #endif |
|---|
| 552 | |
|---|
| 553 | class bad_signal |
|---|
| 554 | { |
|---|
| 555 | int p_sig; |
|---|
| 556 | public: |
|---|
| 557 | explicit bad_signal(int sig) |
|---|
| 558 | { |
|---|
| 559 | p_sig = sig; |
|---|
| 560 | } |
|---|
| 561 | virtual ~bad_signal() throw() {} |
|---|
| 562 | int sig() const throw() |
|---|
| 563 | { |
|---|
| 564 | return p_sig; |
|---|
| 565 | } |
|---|
| 566 | }; |
|---|
| 567 | |
|---|
| 568 | class bad_assert |
|---|
| 569 | { |
|---|
| 570 | const char* p_file; |
|---|
| 571 | long p_line; |
|---|
| 572 | public: |
|---|
| 573 | bad_assert(const char* file, long line) |
|---|
| 574 | { |
|---|
| 575 | p_file = file; |
|---|
| 576 | p_line = line; |
|---|
| 577 | } |
|---|
| 578 | virtual ~bad_assert() throw() |
|---|
| 579 | { |
|---|
| 580 | p_file = NULL; |
|---|
| 581 | } |
|---|
| 582 | const char* file() const throw() |
|---|
| 583 | { |
|---|
| 584 | return p_file; |
|---|
| 585 | } |
|---|
| 586 | long line() const throw() |
|---|
| 587 | { |
|---|
| 588 | return p_line; |
|---|
| 589 | } |
|---|
| 590 | }; |
|---|
| 591 | |
|---|
| 592 | class bad_mpi { |
|---|
| 593 | long p_failMode; |
|---|
| 594 | public: |
|---|
| 595 | bad_mpi(long failMode) : p_failMode(failMode) {} |
|---|
| 596 | long failMode() const { return p_failMode; } |
|---|
| 597 | }; |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | #undef ASSERT |
|---|
| 606 | #ifndef OLD_ASSERT |
|---|
| 607 | |
|---|
| 608 | # ifdef NDEBUG |
|---|
| 609 | # define ASSERT(exp) ((void)0) |
|---|
| 610 | # else |
|---|
| 611 | # define ASSERT(exp) \ |
|---|
| 612 | do { \ |
|---|
| 613 | if (!(exp)) \ |
|---|
| 614 | { \ |
|---|
| 615 | if( cpu.lgAssertAbort() ) \ |
|---|
| 616 | abort(); \ |
|---|
| 617 | else \ |
|---|
| 618 | throw bad_assert(__FILE__,__LINE__); \ |
|---|
| 619 | } \ |
|---|
| 620 | } while( 0 ) |
|---|
| 621 | # endif |
|---|
| 622 | #else |
|---|
| 623 | |
|---|
| 624 | # ifdef NDEBUG |
|---|
| 625 | # define ASSERT(exp) ((void)0) |
|---|
| 626 | # else |
|---|
| 627 | # define ASSERT(exp) \ |
|---|
| 628 | do { \ |
|---|
| 629 | if (!(exp)) \ |
|---|
| 630 | MyAssert(__FILE__, __LINE__); \ |
|---|
| 631 | } while( 0 ) |
|---|
| 632 | # endif |
|---|
| 633 | #endif |
|---|
| 634 | |
|---|
| 635 | NORETURN inline void OUT_OF_RANGE(const char* str) |
|---|
| 636 | { |
|---|
| 637 | if( cpu.lgAssertAbort() ) |
|---|
| 638 | abort(); |
|---|
| 639 | else |
|---|
| 640 | throw out_of_range( str ); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | |
|---|
| 646 | #undef isnan |
|---|
| 647 | #define isnan MyIsnan |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | class t_debug : public Singleton<t_debug> |
|---|
| 652 | { |
|---|
| 653 | friend class Singleton<t_debug>; |
|---|
| 654 | FILE *p_fp; |
|---|
| 655 | int p_callLevel; |
|---|
| 656 | protected: |
|---|
| 657 | t_debug() |
|---|
| 658 | { |
|---|
| 659 | p_fp = stderr; |
|---|
| 660 | p_callLevel = 0; |
|---|
| 661 | } |
|---|
| 662 | public: |
|---|
| 663 | void enter(const char *name) |
|---|
| 664 | { |
|---|
| 665 | ++p_callLevel; |
|---|
| 666 | fprintf(p_fp,"%*c%s\n",p_callLevel,'>',name); |
|---|
| 667 | } |
|---|
| 668 | void leave(const char *name) |
|---|
| 669 | { |
|---|
| 670 | fprintf(p_fp,"%*c%s\n",p_callLevel,'<',name); |
|---|
| 671 | --p_callLevel; |
|---|
| 672 | } |
|---|
| 673 | }; |
|---|
| 674 | |
|---|
| 675 | template<bool lgTrace> |
|---|
| 676 | class debugtrace |
|---|
| 677 | { |
|---|
| 678 | const char *p_name; |
|---|
| 679 | public: |
|---|
| 680 | explicit debugtrace(const char *funcname) |
|---|
| 681 | { |
|---|
| 682 | p_name = funcname; |
|---|
| 683 | # ifdef _MSC_VER |
|---|
| 684 | |
|---|
| 685 | # pragma warning( disable : 4127 ) |
|---|
| 686 | # endif |
|---|
| 687 | if( lgTrace ) |
|---|
| 688 | t_debug::Inst().enter(p_name); |
|---|
| 689 | } |
|---|
| 690 | ~debugtrace() |
|---|
| 691 | { |
|---|
| 692 | # ifdef _MSC_VER |
|---|
| 693 | |
|---|
| 694 | # pragma warning( disable : 4127 ) |
|---|
| 695 | # endif |
|---|
| 696 | if( lgTrace ) |
|---|
| 697 | t_debug::Inst().leave(p_name); |
|---|
| 698 | p_name = NULL; |
|---|
| 699 | } |
|---|
| 700 | const char* name() const |
|---|
| 701 | { |
|---|
| 702 | return p_name; |
|---|
| 703 | } |
|---|
| 704 | }; |
|---|
| 705 | |
|---|
| 706 | #ifdef DEBUG_FUN |
|---|
| 707 | #define DEBUG_ENTRY( funcname ) debugtrace<true> DEBUG_ENTRY( funcname ) |
|---|
| 708 | #else |
|---|
| 709 | #ifdef __GNUC__ |
|---|
| 710 | #define DEBUG_ENTRY( funcname ) ((void)0) |
|---|
| 711 | #else |
|---|
| 712 | #define DEBUG_ENTRY( funcname ) debugtrace<false> DEBUG_ENTRY( funcname ) |
|---|
| 713 | #endif |
|---|
| 714 | #endif |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | inline char TorF( bool l ) { return l ? 'T' : 'F'; } |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | |
|---|
| 721 | inline bool is_odd( int j ) { return (j&1) == 1; } |
|---|
| 722 | inline bool is_odd( long j ) { return (j&1L) == 1L; } |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | inline long nint( double x ) { return static_cast<long>( (x < 0.) ? x-0.5 : x+0.5 ); } |
|---|
| 727 | |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | inline long min( int a, long b ) { long c = a; return ( (c < b) ? c : b ); } |
|---|
| 731 | inline long min( long a, int b ) { long c = b; return ( (a < c) ? a : c ); } |
|---|
| 732 | inline double min( sys_float a, double b ) { double c = a; return ( (c < b) ? c : b ); } |
|---|
| 733 | inline double min( double a, sys_float b ) { double c = b; return ( (a < c) ? a : c ); } |
|---|
| 734 | |
|---|
| 735 | #undef MIN2 |
|---|
| 736 | |
|---|
| 737 | #define MIN2 min |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | #undef MIN3 |
|---|
| 741 | |
|---|
| 742 | #define MIN3(a,b,c) (min(min(a,b),c)) |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | #undef MIN4 |
|---|
| 746 | |
|---|
| 747 | #define MIN4(a,b,c,d) (min(min(a,b),min(c,d))) |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | inline long max( int a, long b ) { long c = a; return ( (c > b) ? c : b ); } |
|---|
| 752 | inline long max( long a, int b ) { long c = b; return ( (a > c) ? a : c ); } |
|---|
| 753 | inline double max( sys_float a, double b ) { double c = a; return ( (c > b) ? c : b ); } |
|---|
| 754 | inline double max( double a, sys_float b ) { double c = b; return ( (a > c) ? a : c ); } |
|---|
| 755 | |
|---|
| 756 | #undef MAX2 |
|---|
| 757 | |
|---|
| 758 | #define MAX2 max |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | #undef MAX3 |
|---|
| 762 | |
|---|
| 763 | #define MAX3(a,b,c) (max(max(a,b),c)) |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | #undef MAX4 |
|---|
| 767 | |
|---|
| 768 | #define MAX4(a,b,c,d) (max(max(a,b),max(c,d))) |
|---|
| 769 | |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | template<class T> |
|---|
| 776 | inline T sign( T x, T y ) |
|---|
| 777 | { |
|---|
| 778 | return ( y < T() ) ? -abs(x) : abs(x); |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | template<class T> |
|---|
| 784 | inline int sign3( T x ) { return ( x < T() ) ? -1 : ( ( x > T() ) ? 1 : 0 ); } |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | inline bool fp_equal( sys_float x, sys_float y, int n=3 ) |
|---|
| 789 | { |
|---|
| 790 | #ifdef _MSC_VER |
|---|
| 791 | |
|---|
| 792 | # pragma warning( disable : 4127 ) |
|---|
| 793 | |
|---|
| 794 | # ifndef WIN32_LEAN_AND_MEAN |
|---|
| 795 | # define WIN32_LEAN_AND_MEAN |
|---|
| 796 | # endif |
|---|
| 797 | #endif |
|---|
| 798 | ASSERT( n >= 1 ); |
|---|
| 799 | |
|---|
| 800 | if( isnan(x) || isnan(y) ) |
|---|
| 801 | return false; |
|---|
| 802 | int sx = sign3(x); |
|---|
| 803 | int sy = sign3(y); |
|---|
| 804 | |
|---|
| 805 | if( sx == 0 && sy == 0 ) |
|---|
| 806 | return true; |
|---|
| 807 | |
|---|
| 808 | if( sx*sy != 1 ) |
|---|
| 809 | return false; |
|---|
| 810 | x = abs(x); |
|---|
| 811 | y = abs(y); |
|---|
| 812 | return ( 1.f - min(x,y)/max(x,y) < ((sys_float)n+0.1f)*FLT_EPSILON ); |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | inline bool fp_equal( double x, double y, int n=3 ) |
|---|
| 816 | { |
|---|
| 817 | ASSERT( n >= 1 ); |
|---|
| 818 | |
|---|
| 819 | if( isnan(x) || isnan(y) ) |
|---|
| 820 | return false; |
|---|
| 821 | int sx = sign3(x); |
|---|
| 822 | int sy = sign3(y); |
|---|
| 823 | |
|---|
| 824 | if( sx == 0 && sy == 0 ) |
|---|
| 825 | return true; |
|---|
| 826 | |
|---|
| 827 | if( sx*sy != 1 ) |
|---|
| 828 | return false; |
|---|
| 829 | x = abs(x); |
|---|
| 830 | y = abs(y); |
|---|
| 831 | return ( 1. - min(x,y)/max(x,y) < ((double)n+0.1)*DBL_EPSILON ); |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | inline bool fp_equal_tol( sys_float x, sys_float y, sys_float tol ) |
|---|
| 835 | { |
|---|
| 836 | ASSERT( tol > 0.f ); |
|---|
| 837 | |
|---|
| 838 | if( isnan(x) || isnan(y) ) |
|---|
| 839 | return false; |
|---|
| 840 | |
|---|
| 841 | ASSERT( tol >= FLT_EPSILON*max(abs(x),abs(y)) ); |
|---|
| 842 | return ( abs( x-y ) <= tol ); |
|---|
| 843 | } |
|---|
| 844 | |
|---|
| 845 | inline bool fp_equal_tol( double x, double y, double tol ) |
|---|
| 846 | { |
|---|
| 847 | ASSERT( tol > 0. ); |
|---|
| 848 | |
|---|
| 849 | if( isnan(x) || isnan(y) ) |
|---|
| 850 | return false; |
|---|
| 851 | |
|---|
| 852 | ASSERT( tol >= DBL_EPSILON*max(abs(x),abs(y)) ); |
|---|
| 853 | return ( abs( x-y ) <= tol ); |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | inline bool fp_bound( sys_float lo, sys_float x, sys_float hi, int n=3 ) |
|---|
| 858 | { |
|---|
| 859 | ASSERT( n >= 1 ); |
|---|
| 860 | |
|---|
| 861 | if( isnan(x) || isnan(lo) || isnan(hi) ) |
|---|
| 862 | return false; |
|---|
| 863 | if( fp_equal(lo,hi,n) ) |
|---|
| 864 | return fp_equal(0.5f*(lo+hi),x,n); |
|---|
| 865 | if( ((hi-x)/(hi-lo))*((x-lo)/(hi-lo)) < -((sys_float)n+0.1f)*FLT_EPSILON ) |
|---|
| 866 | return false; |
|---|
| 867 | return true; |
|---|
| 868 | } |
|---|
| 869 | inline bool fp_bound( double lo, double x, double hi, int n=3 ) |
|---|
| 870 | { |
|---|
| 871 | ASSERT( n >= 1 ); |
|---|
| 872 | |
|---|
| 873 | if( isnan(x) || isnan(lo) || isnan(hi) ) |
|---|
| 874 | return false; |
|---|
| 875 | if( fp_equal(lo,hi,n) ) |
|---|
| 876 | return fp_equal(0.5*(lo+hi),x,n); |
|---|
| 877 | if( ((hi-x)/(hi-lo))*((x-lo)/(hi-lo)) < -((double)n+0.1)*DBL_EPSILON ) |
|---|
| 878 | return false; |
|---|
| 879 | return true; |
|---|
| 880 | } |
|---|
| 881 | inline bool fp_bound_tol( sys_float lo, sys_float x, sys_float hi, sys_float tol ) |
|---|
| 882 | { |
|---|
| 883 | ASSERT( tol > 0.f ); |
|---|
| 884 | |
|---|
| 885 | if( isnan(x) || isnan(lo) || isnan(hi) ) |
|---|
| 886 | return false; |
|---|
| 887 | if( fp_equal_tol(lo,hi,tol) ) |
|---|
| 888 | return fp_equal_tol(0.5f*(lo+hi),x,tol); |
|---|
| 889 | if( ((hi-x)/(hi-lo))*((x-lo)/(hi-lo)) < -tol ) |
|---|
| 890 | return false; |
|---|
| 891 | return true; |
|---|
| 892 | } |
|---|
| 893 | inline bool fp_bound_tol( double lo, double x, double hi, double tol ) |
|---|
| 894 | { |
|---|
| 895 | ASSERT( tol > 0. ); |
|---|
| 896 | |
|---|
| 897 | if( isnan(x) || isnan(lo) || isnan(hi) ) |
|---|
| 898 | return false; |
|---|
| 899 | if( fp_equal_tol(lo,hi,tol) ) |
|---|
| 900 | return |
|---|