jpayne@69: #ifndef Py_PYMATH_H jpayne@69: #define Py_PYMATH_H jpayne@69: jpayne@69: #include "pyconfig.h" /* include for defines */ jpayne@69: jpayne@69: /************************************************************************** jpayne@69: Symbols and macros to supply platform-independent interfaces to mathematical jpayne@69: functions and constants jpayne@69: **************************************************************************/ jpayne@69: jpayne@69: /* Python provides implementations for copysign, round and hypot in jpayne@69: * Python/pymath.c just in case your math library doesn't provide the jpayne@69: * functions. jpayne@69: * jpayne@69: *Note: PC/pyconfig.h defines copysign as _copysign jpayne@69: */ jpayne@69: #ifndef HAVE_COPYSIGN jpayne@69: extern double copysign(double, double); jpayne@69: #endif jpayne@69: jpayne@69: #ifndef HAVE_ROUND jpayne@69: extern double round(double); jpayne@69: #endif jpayne@69: jpayne@69: #ifndef HAVE_HYPOT jpayne@69: extern double hypot(double, double); jpayne@69: #endif jpayne@69: jpayne@69: /* extra declarations */ jpayne@69: #ifndef _MSC_VER jpayne@69: #ifndef __STDC__ jpayne@69: extern double fmod (double, double); jpayne@69: extern double frexp (double, int *); jpayne@69: extern double ldexp (double, int); jpayne@69: extern double modf (double, double *); jpayne@69: extern double pow(double, double); jpayne@69: #endif /* __STDC__ */ jpayne@69: #endif /* _MSC_VER */ jpayne@69: jpayne@69: /* High precision definition of pi and e (Euler) jpayne@69: * The values are taken from libc6's math.h. jpayne@69: */ jpayne@69: #ifndef Py_MATH_PIl jpayne@69: #define Py_MATH_PIl 3.1415926535897932384626433832795029L jpayne@69: #endif jpayne@69: #ifndef Py_MATH_PI jpayne@69: #define Py_MATH_PI 3.14159265358979323846 jpayne@69: #endif jpayne@69: jpayne@69: #ifndef Py_MATH_El jpayne@69: #define Py_MATH_El 2.7182818284590452353602874713526625L jpayne@69: #endif jpayne@69: jpayne@69: #ifndef Py_MATH_E jpayne@69: #define Py_MATH_E 2.7182818284590452354 jpayne@69: #endif jpayne@69: jpayne@69: /* Tau (2pi) to 40 digits, taken from tauday.com/tau-digits. */ jpayne@69: #ifndef Py_MATH_TAU jpayne@69: #define Py_MATH_TAU 6.2831853071795864769252867665590057683943L jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: /* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU jpayne@69: register and into a 64-bit memory location, rounding from extended jpayne@69: precision to double precision in the process. On other platforms it does jpayne@69: nothing. */ jpayne@69: jpayne@69: /* we take double rounding as evidence of x87 usage */ jpayne@69: #ifndef Py_LIMITED_API jpayne@69: #ifndef Py_FORCE_DOUBLE jpayne@69: # ifdef X87_DOUBLE_ROUNDING jpayne@69: PyAPI_FUNC(double) _Py_force_double(double); jpayne@69: # define Py_FORCE_DOUBLE(X) (_Py_force_double(X)) jpayne@69: # else jpayne@69: # define Py_FORCE_DOUBLE(X) (X) jpayne@69: # endif jpayne@69: #endif jpayne@69: #endif jpayne@69: jpayne@69: #ifndef Py_LIMITED_API jpayne@69: #ifdef HAVE_GCC_ASM_FOR_X87 jpayne@69: PyAPI_FUNC(unsigned short) _Py_get_387controlword(void); jpayne@69: PyAPI_FUNC(void) _Py_set_387controlword(unsigned short); jpayne@69: #endif jpayne@69: #endif jpayne@69: jpayne@69: /* Py_IS_NAN(X) jpayne@69: * Return 1 if float or double arg is a NaN, else 0. jpayne@69: * Caution: jpayne@69: * X is evaluated more than once. jpayne@69: * This may not work on all platforms. Each platform has *some* jpayne@69: * way to spell this, though -- override in pyconfig.h if you have jpayne@69: * a platform where it doesn't work. jpayne@69: * Note: PC/pyconfig.h defines Py_IS_NAN as _isnan jpayne@69: */ jpayne@69: #ifndef Py_IS_NAN jpayne@69: #if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1 jpayne@69: #define Py_IS_NAN(X) isnan(X) jpayne@69: #else jpayne@69: #define Py_IS_NAN(X) ((X) != (X)) jpayne@69: #endif jpayne@69: #endif jpayne@69: jpayne@69: /* Py_IS_INFINITY(X) jpayne@69: * Return 1 if float or double arg is an infinity, else 0. jpayne@69: * Caution: jpayne@69: * X is evaluated more than once. jpayne@69: * This implementation may set the underflow flag if |X| is very small; jpayne@69: * it really can't be implemented correctly (& easily) before C99. jpayne@69: * Override in pyconfig.h if you have a better spelling on your platform. jpayne@69: * Py_FORCE_DOUBLE is used to avoid getting false negatives from a jpayne@69: * non-infinite value v sitting in an 80-bit x87 register such that jpayne@69: * v becomes infinite when spilled from the register to 64-bit memory. jpayne@69: * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf jpayne@69: */ jpayne@69: #ifndef Py_IS_INFINITY jpayne@69: # if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1 jpayne@69: # define Py_IS_INFINITY(X) isinf(X) jpayne@69: # else jpayne@69: # define Py_IS_INFINITY(X) ((X) && \ jpayne@69: (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X))) jpayne@69: # endif jpayne@69: #endif jpayne@69: jpayne@69: /* Py_IS_FINITE(X) jpayne@69: * Return 1 if float or double arg is neither infinite nor NAN, else 0. jpayne@69: * Some compilers (e.g. VisualStudio) have intrisics for this, so a special jpayne@69: * macro for this particular test is useful jpayne@69: * Note: PC/pyconfig.h defines Py_IS_FINITE as _finite jpayne@69: */ jpayne@69: #ifndef Py_IS_FINITE jpayne@69: #if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1 jpayne@69: #define Py_IS_FINITE(X) isfinite(X) jpayne@69: #elif defined HAVE_FINITE jpayne@69: #define Py_IS_FINITE(X) finite(X) jpayne@69: #else jpayne@69: #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) jpayne@69: #endif jpayne@69: #endif jpayne@69: jpayne@69: /* HUGE_VAL is supposed to expand to a positive double infinity. Python jpayne@69: * uses Py_HUGE_VAL instead because some platforms are broken in this jpayne@69: * respect. We used to embed code in pyport.h to try to worm around that, jpayne@69: * but different platforms are broken in conflicting ways. If you're on jpayne@69: * a platform where HUGE_VAL is defined incorrectly, fiddle your Python jpayne@69: * config to #define Py_HUGE_VAL to something that works on your platform. jpayne@69: */ jpayne@69: #ifndef Py_HUGE_VAL jpayne@69: #define Py_HUGE_VAL HUGE_VAL jpayne@69: #endif jpayne@69: jpayne@69: /* Py_NAN jpayne@69: * A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or jpayne@69: * INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform jpayne@69: * doesn't support NaNs. jpayne@69: */ jpayne@69: #if !defined(Py_NAN) && !defined(Py_NO_NAN) jpayne@69: #if !defined(__INTEL_COMPILER) jpayne@69: #define Py_NAN (Py_HUGE_VAL * 0.) jpayne@69: #else /* __INTEL_COMPILER */ jpayne@69: #if defined(ICC_NAN_STRICT) jpayne@69: #pragma float_control(push) jpayne@69: #pragma float_control(precise, on) jpayne@69: #pragma float_control(except, on) jpayne@69: #if defined(_MSC_VER) jpayne@69: __declspec(noinline) jpayne@69: #else /* Linux */ jpayne@69: __attribute__((noinline)) jpayne@69: #endif /* _MSC_VER */ jpayne@69: static double __icc_nan() jpayne@69: { jpayne@69: return sqrt(-1.0); jpayne@69: } jpayne@69: #pragma float_control (pop) jpayne@69: #define Py_NAN __icc_nan() jpayne@69: #else /* ICC_NAN_RELAXED as default for Intel Compiler */ jpayne@69: static const union { unsigned char buf[8]; double __icc_nan; } __nan_store = {0,0,0,0,0,0,0xf8,0x7f}; jpayne@69: #define Py_NAN (__nan_store.__icc_nan) jpayne@69: #endif /* ICC_NAN_STRICT */ jpayne@69: #endif /* __INTEL_COMPILER */ jpayne@69: #endif jpayne@69: jpayne@69: /* Py_OVERFLOWED(X) jpayne@69: * Return 1 iff a libm function overflowed. Set errno to 0 before calling jpayne@69: * a libm function, and invoke this macro after, passing the function jpayne@69: * result. jpayne@69: * Caution: jpayne@69: * This isn't reliable. C99 no longer requires libm to set errno under jpayne@69: * any exceptional condition, but does require +- HUGE_VAL return jpayne@69: * values on overflow. A 754 box *probably* maps HUGE_VAL to a jpayne@69: * double infinity, and we're cool if that's so, unless the input jpayne@69: * was an infinity and an infinity is the expected result. A C89 jpayne@69: * system sets errno to ERANGE, so we check for that too. We're jpayne@69: * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or jpayne@69: * if the returned result is a NaN, or if a C89 box returns HUGE_VAL jpayne@69: * in non-overflow cases. jpayne@69: * X is evaluated more than once. jpayne@69: * Some platforms have better way to spell this, so expect some #ifdef'ery. jpayne@69: * jpayne@69: * OpenBSD uses 'isinf()' because a compiler bug on that platform causes jpayne@69: * the longer macro version to be mis-compiled. This isn't optimal, and jpayne@69: * should be removed once a newer compiler is available on that platform. jpayne@69: * The system that had the failure was running OpenBSD 3.2 on Intel, with jpayne@69: * gcc 2.95.3. jpayne@69: * jpayne@69: * According to Tim's checkin, the FreeBSD systems use isinf() to work jpayne@69: * around a FPE bug on that platform. jpayne@69: */ jpayne@69: #if defined(__FreeBSD__) || defined(__OpenBSD__) jpayne@69: #define Py_OVERFLOWED(X) isinf(X) jpayne@69: #else jpayne@69: #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \ jpayne@69: (X) == Py_HUGE_VAL || \ jpayne@69: (X) == -Py_HUGE_VAL)) jpayne@69: #endif jpayne@69: jpayne@69: /* Return whether integral type *type* is signed or not. */ jpayne@69: #define _Py_IntegralTypeSigned(type) ((type)(-1) < 0) jpayne@69: /* Return the maximum value of integral type *type*. */ jpayne@69: #define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0) jpayne@69: /* Return the minimum value of integral type *type*. */ jpayne@69: #define _Py_IntegralTypeMin(type) ((_Py_IntegralTypeSigned(type)) ? -_Py_IntegralTypeMax(type) - 1 : 0) jpayne@69: /* Check whether *v* is in the range of integral type *type*. This is most jpayne@69: * useful if *v* is floating-point, since demoting a floating-point *v* to an jpayne@69: * integral type that cannot represent *v*'s integral part is undefined jpayne@69: * behavior. */ jpayne@69: #define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type)) jpayne@69: jpayne@69: #endif /* Py_PYMATH_H */