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