| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////// Put x87 FPU in double-precision mode ////////////////// | ||
| 2 | // taken from: | ||
| 3 | // https://gist.github.com/rversteegen/9865ad0dfa734981d3f41ab968d7cf58 | ||
| 4 | // | ||
| 5 | // For cross-platform portability, force x87 floating-point calculations to be | ||
| 6 | // done with intermediate results stored in double precision (53 bit mantissa) | ||
| 7 | // instead of extended double precision (64 bit mantissa) registers. We change | ||
| 8 | // the x87 control register to accomplish this. But it only affects the | ||
| 9 | // mantissa, not the exponent, so does not remove all inconsistencies. | ||
| 10 | // | ||
| 11 | // See http://yosefk.com/blog/consistency-how-to-defeat-the-purpose-of-ieee-floating-point.html | ||
| 12 | // and http://christian-seiler.de/projekte/fpmath/ | ||
| 13 | |||
| 14 | #ifndef GEO_I387_H | ||
| 15 | #define GEO_I387_H | ||
| 16 | |||
| 17 | // Indirectly include features.h (for glibc detection), which might not exist | ||
| 18 | #include <limits.h> | ||
| 19 | |||
| 20 | // Check for x86 or amd64, for Visual C++, GCC | ||
| 21 | #if defined(_M_IX86) || defined(_M_AMD64) || defined(__i386__) || defined(__x86_64__) | ||
| 22 | |||
| 23 | #if defined(_MSC_VER) || defined(_WIN32) | ||
| 24 | // Windows, either Visual C++ or MinGW. Note, Windows defaults to double-precision, | ||
| 25 | // but MinGW switches on extended precision | ||
| 26 | |||
| 27 | #include <float.h> | ||
| 28 | |||
| 29 | // Unfortunately at least some MinGW versions (4.8.1) ship with a copy of | ||
| 30 | // gcc with a float.h which shadows the MinGW float.h header | ||
| 31 | #ifndef _PC_53 | ||
| 32 | #define _PC_53 0x00010000 | ||
| 33 | #define _MCW_PC 0x00030000 | ||
| 34 | _CRTIMP unsigned int __cdecl __MINGW_NOTHROW _controlfp (unsigned int unNew, unsigned int unMask); | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #define GEO_387 | ||
| 38 | inline void geo_disable_387_extended_precision() { | ||
| 39 | _controlfp(_PC_53, _MCW_PC); | ||
| 40 | } | ||
| 41 | |||
| 42 | #elif defined(__gnu_linux__) || defined(__GNU_LIBRARY__) || defined(__GLIBC__) | ||
| 43 | // For glibc | ||
| 44 | |||
| 45 | #include <fpu_control.h> | ||
| 46 | |||
| 47 | #define GEO_387 | ||
| 48 | inline void geo_disable_387_extended_precision() { | ||
| 49 | fpu_control_t cw; | ||
| 50 | 520 | _FPU_GETCW(cw); | |
| 51 | 520 | cw = (fpu_control_t)(cw & ~_FPU_EXTENDED) | _FPU_DOUBLE; | |
| 52 | 520 | _FPU_SETCW(cw); | |
| 53 | } | ||
| 54 | |||
| 55 | #endif | ||
| 56 | #endif | ||
| 57 | |||
| 58 | |||
| 59 | #endif // GEO_387_H | ||
| 60 |