Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1 | #ifndef Py_PYMATH_H |
| 2 | #define Py_PYMATH_H |
| 3 | |
| 4 | #include "pyconfig.h" /* include for defines */ |
| 5 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 6 | /************************************************************************** |
| 7 | Symbols and macros to supply platform-independent interfaces to mathematical |
| 8 | functions and constants |
| 9 | **************************************************************************/ |
| 10 | |
Mark Dickinson | f371859 | 2009-12-21 15:27:41 +0000 | [diff] [blame] | 11 | /* Python provides implementations for copysign, round and hypot in |
| 12 | * Python/pymath.c just in case your math library doesn't provide the |
| 13 | * functions. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 14 | * |
| 15 | *Note: PC/pyconfig.h defines copysign as _copysign |
| 16 | */ |
| 17 | #ifndef HAVE_COPYSIGN |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 18 | extern double copysign(double, double); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
Mark Dickinson | f253786 | 2009-04-18 13:58:18 +0000 | [diff] [blame] | 21 | #ifndef HAVE_ROUND |
| 22 | extern double round(double); |
| 23 | #endif |
| 24 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 25 | #ifndef HAVE_HYPOT |
| 26 | extern double hypot(double, double); |
| 27 | #endif |
| 28 | |
| 29 | /* extra declarations */ |
| 30 | #ifndef _MSC_VER |
| 31 | #ifndef __STDC__ |
| 32 | extern double fmod (double, double); |
| 33 | extern double frexp (double, int *); |
| 34 | extern double ldexp (double, int); |
| 35 | extern double modf (double, double *); |
| 36 | extern double pow(double, double); |
| 37 | #endif /* __STDC__ */ |
| 38 | #endif /* _MSC_VER */ |
| 39 | |
| 40 | #ifdef _OSF_SOURCE |
| 41 | /* OSF1 5.1 doesn't make these available with XOPEN_SOURCE_EXTENDED defined */ |
| 42 | extern int finite(double); |
| 43 | extern double copysign(double, double); |
| 44 | #endif |
| 45 | |
| 46 | /* High precision defintion of pi and e (Euler) |
| 47 | * The values are taken from libc6's math.h. |
| 48 | */ |
| 49 | #ifndef Py_MATH_PIl |
| 50 | #define Py_MATH_PIl 3.1415926535897932384626433832795029L |
| 51 | #endif |
| 52 | #ifndef Py_MATH_PI |
| 53 | #define Py_MATH_PI 3.14159265358979323846 |
| 54 | #endif |
| 55 | |
| 56 | #ifndef Py_MATH_El |
| 57 | #define Py_MATH_El 2.7182818284590452353602874713526625L |
| 58 | #endif |
| 59 | |
| 60 | #ifndef Py_MATH_E |
| 61 | #define Py_MATH_E 2.7182818284590452354 |
| 62 | #endif |
| 63 | |
Mark Dickinson | 87ec085 | 2009-02-09 17:15:59 +0000 | [diff] [blame] | 64 | /* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU |
| 65 | register and into a 64-bit memory location, rounding from extended |
| 66 | precision to double precision in the process. On other platforms it does |
| 67 | nothing. */ |
| 68 | |
| 69 | /* we take double rounding as evidence of x87 usage */ |
| 70 | #ifndef Py_FORCE_DOUBLE |
| 71 | # ifdef X87_DOUBLE_ROUNDING |
| 72 | PyAPI_FUNC(double) _Py_force_double(double); |
| 73 | # define Py_FORCE_DOUBLE(X) (_Py_force_double(X)) |
| 74 | # else |
| 75 | # define Py_FORCE_DOUBLE(X) (X) |
| 76 | # endif |
| 77 | #endif |
| 78 | |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 79 | #ifdef HAVE_GCC_ASM_FOR_X87 |
| 80 | PyAPI_FUNC(unsigned short) _Py_get_387controlword(void); |
| 81 | PyAPI_FUNC(void) _Py_set_387controlword(unsigned short); |
| 82 | #endif |
| 83 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 84 | /* Py_IS_NAN(X) |
| 85 | * Return 1 if float or double arg is a NaN, else 0. |
| 86 | * Caution: |
| 87 | * X is evaluated more than once. |
| 88 | * This may not work on all platforms. Each platform has *some* |
| 89 | * way to spell this, though -- override in pyconfig.h if you have |
| 90 | * a platform where it doesn't work. |
| 91 | * Note: PC/pyconfig.h defines Py_IS_NAN as _isnan |
| 92 | */ |
| 93 | #ifndef Py_IS_NAN |
Mark Dickinson | 6cb2bdd | 2009-01-04 17:02:56 +0000 | [diff] [blame] | 94 | #if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1 |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 95 | #define Py_IS_NAN(X) isnan(X) |
| 96 | #else |
| 97 | #define Py_IS_NAN(X) ((X) != (X)) |
| 98 | #endif |
| 99 | #endif |
| 100 | |
| 101 | /* Py_IS_INFINITY(X) |
| 102 | * Return 1 if float or double arg is an infinity, else 0. |
| 103 | * Caution: |
| 104 | * X is evaluated more than once. |
| 105 | * This implementation may set the underflow flag if |X| is very small; |
| 106 | * it really can't be implemented correctly (& easily) before C99. |
| 107 | * Override in pyconfig.h if you have a better spelling on your platform. |
Mark Dickinson | 87ec085 | 2009-02-09 17:15:59 +0000 | [diff] [blame] | 108 | * Py_FORCE_DOUBLE is used to avoid getting false negatives from a |
| 109 | * non-infinite value v sitting in an 80-bit x87 register such that |
| 110 | * v becomes infinite when spilled from the register to 64-bit memory. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 111 | * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf |
| 112 | */ |
| 113 | #ifndef Py_IS_INFINITY |
Mark Dickinson | 87ec085 | 2009-02-09 17:15:59 +0000 | [diff] [blame] | 114 | # if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1 |
| 115 | # define Py_IS_INFINITY(X) isinf(X) |
| 116 | # else |
| 117 | # define Py_IS_INFINITY(X) ((X) && \ |
| 118 | (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X))) |
| 119 | # endif |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | /* Py_IS_FINITE(X) |
| 123 | * Return 1 if float or double arg is neither infinite nor NAN, else 0. |
| 124 | * Some compilers (e.g. VisualStudio) have intrisics for this, so a special |
| 125 | * macro for this particular test is useful |
| 126 | * Note: PC/pyconfig.h defines Py_IS_FINITE as _finite |
| 127 | */ |
| 128 | #ifndef Py_IS_FINITE |
Mark Dickinson | 52144f5 | 2009-01-05 17:08:27 +0000 | [diff] [blame] | 129 | #if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1 |
| 130 | #define Py_IS_FINITE(X) isfinite(X) |
| 131 | #elif defined HAVE_FINITE |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 132 | #define Py_IS_FINITE(X) finite(X) |
| 133 | #else |
| 134 | #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) |
| 135 | #endif |
| 136 | #endif |
| 137 | |
| 138 | /* HUGE_VAL is supposed to expand to a positive double infinity. Python |
| 139 | * uses Py_HUGE_VAL instead because some platforms are broken in this |
| 140 | * respect. We used to embed code in pyport.h to try to worm around that, |
| 141 | * but different platforms are broken in conflicting ways. If you're on |
| 142 | * a platform where HUGE_VAL is defined incorrectly, fiddle your Python |
| 143 | * config to #define Py_HUGE_VAL to something that works on your platform. |
| 144 | */ |
| 145 | #ifndef Py_HUGE_VAL |
| 146 | #define Py_HUGE_VAL HUGE_VAL |
| 147 | #endif |
| 148 | |
| 149 | /* Py_NAN |
| 150 | * A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or |
| 151 | * INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform |
| 152 | * doesn't support NaNs. |
| 153 | */ |
| 154 | #if !defined(Py_NAN) && !defined(Py_NO_NAN) |
| 155 | #define Py_NAN (Py_HUGE_VAL * 0.) |
| 156 | #endif |
| 157 | |
| 158 | /* Py_OVERFLOWED(X) |
| 159 | * Return 1 iff a libm function overflowed. Set errno to 0 before calling |
| 160 | * a libm function, and invoke this macro after, passing the function |
| 161 | * result. |
| 162 | * Caution: |
| 163 | * This isn't reliable. C99 no longer requires libm to set errno under |
| 164 | * any exceptional condition, but does require +- HUGE_VAL return |
| 165 | * values on overflow. A 754 box *probably* maps HUGE_VAL to a |
| 166 | * double infinity, and we're cool if that's so, unless the input |
| 167 | * was an infinity and an infinity is the expected result. A C89 |
| 168 | * system sets errno to ERANGE, so we check for that too. We're |
| 169 | * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or |
| 170 | * if the returned result is a NaN, or if a C89 box returns HUGE_VAL |
| 171 | * in non-overflow cases. |
| 172 | * X is evaluated more than once. |
| 173 | * Some platforms have better way to spell this, so expect some #ifdef'ery. |
| 174 | * |
| 175 | * OpenBSD uses 'isinf()' because a compiler bug on that platform causes |
| 176 | * the longer macro version to be mis-compiled. This isn't optimal, and |
| 177 | * should be removed once a newer compiler is available on that platform. |
| 178 | * The system that had the failure was running OpenBSD 3.2 on Intel, with |
| 179 | * gcc 2.95.3. |
| 180 | * |
| 181 | * According to Tim's checkin, the FreeBSD systems use isinf() to work |
| 182 | * around a FPE bug on that platform. |
| 183 | */ |
| 184 | #if defined(__FreeBSD__) || defined(__OpenBSD__) |
| 185 | #define Py_OVERFLOWED(X) isinf(X) |
| 186 | #else |
| 187 | #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \ |
| 188 | (X) == Py_HUGE_VAL || \ |
| 189 | (X) == -Py_HUGE_VAL)) |
| 190 | #endif |
| 191 | |
| 192 | #endif /* Py_PYMATH_H */ |