jpayne@69
|
1 #ifndef Py_PYMATH_H
|
jpayne@69
|
2 #define Py_PYMATH_H
|
jpayne@69
|
3
|
jpayne@69
|
4 #include "pyconfig.h" /* include for defines */
|
jpayne@69
|
5
|
jpayne@69
|
6 /**************************************************************************
|
jpayne@69
|
7 Symbols and macros to supply platform-independent interfaces to mathematical
|
jpayne@69
|
8 functions and constants
|
jpayne@69
|
9 **************************************************************************/
|
jpayne@69
|
10
|
jpayne@69
|
11 /* Python provides implementations for copysign, round and hypot in
|
jpayne@69
|
12 * Python/pymath.c just in case your math library doesn't provide the
|
jpayne@69
|
13 * functions.
|
jpayne@69
|
14 *
|
jpayne@69
|
15 *Note: PC/pyconfig.h defines copysign as _copysign
|
jpayne@69
|
16 */
|
jpayne@69
|
17 #ifndef HAVE_COPYSIGN
|
jpayne@69
|
18 extern double copysign(double, double);
|
jpayne@69
|
19 #endif
|
jpayne@69
|
20
|
jpayne@69
|
21 #ifndef HAVE_ROUND
|
jpayne@69
|
22 extern double round(double);
|
jpayne@69
|
23 #endif
|
jpayne@69
|
24
|
jpayne@69
|
25 #ifndef HAVE_HYPOT
|
jpayne@69
|
26 extern double hypot(double, double);
|
jpayne@69
|
27 #endif
|
jpayne@69
|
28
|
jpayne@69
|
29 /* extra declarations */
|
jpayne@69
|
30 #ifndef _MSC_VER
|
jpayne@69
|
31 #ifndef __STDC__
|
jpayne@69
|
32 extern double fmod (double, double);
|
jpayne@69
|
33 extern double frexp (double, int *);
|
jpayne@69
|
34 extern double ldexp (double, int);
|
jpayne@69
|
35 extern double modf (double, double *);
|
jpayne@69
|
36 extern double pow(double, double);
|
jpayne@69
|
37 #endif /* __STDC__ */
|
jpayne@69
|
38 #endif /* _MSC_VER */
|
jpayne@69
|
39
|
jpayne@69
|
40 /* High precision definition of pi and e (Euler)
|
jpayne@69
|
41 * The values are taken from libc6's math.h.
|
jpayne@69
|
42 */
|
jpayne@69
|
43 #ifndef Py_MATH_PIl
|
jpayne@69
|
44 #define Py_MATH_PIl 3.1415926535897932384626433832795029L
|
jpayne@69
|
45 #endif
|
jpayne@69
|
46 #ifndef Py_MATH_PI
|
jpayne@69
|
47 #define Py_MATH_PI 3.14159265358979323846
|
jpayne@69
|
48 #endif
|
jpayne@69
|
49
|
jpayne@69
|
50 #ifndef Py_MATH_El
|
jpayne@69
|
51 #define Py_MATH_El 2.7182818284590452353602874713526625L
|
jpayne@69
|
52 #endif
|
jpayne@69
|
53
|
jpayne@69
|
54 #ifndef Py_MATH_E
|
jpayne@69
|
55 #define Py_MATH_E 2.7182818284590452354
|
jpayne@69
|
56 #endif
|
jpayne@69
|
57
|
jpayne@69
|
58 /* Tau (2pi) to 40 digits, taken from tauday.com/tau-digits. */
|
jpayne@69
|
59 #ifndef Py_MATH_TAU
|
jpayne@69
|
60 #define Py_MATH_TAU 6.2831853071795864769252867665590057683943L
|
jpayne@69
|
61 #endif
|
jpayne@69
|
62
|
jpayne@69
|
63
|
jpayne@69
|
64 /* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU
|
jpayne@69
|
65 register and into a 64-bit memory location, rounding from extended
|
jpayne@69
|
66 precision to double precision in the process. On other platforms it does
|
jpayne@69
|
67 nothing. */
|
jpayne@69
|
68
|
jpayne@69
|
69 /* we take double rounding as evidence of x87 usage */
|
jpayne@69
|
70 #ifndef Py_LIMITED_API
|
jpayne@69
|
71 #ifndef Py_FORCE_DOUBLE
|
jpayne@69
|
72 # ifdef X87_DOUBLE_ROUNDING
|
jpayne@69
|
73 PyAPI_FUNC(double) _Py_force_double(double);
|
jpayne@69
|
74 # define Py_FORCE_DOUBLE(X) (_Py_force_double(X))
|
jpayne@69
|
75 # else
|
jpayne@69
|
76 # define Py_FORCE_DOUBLE(X) (X)
|
jpayne@69
|
77 # endif
|
jpayne@69
|
78 #endif
|
jpayne@69
|
79 #endif
|
jpayne@69
|
80
|
jpayne@69
|
81 #ifndef Py_LIMITED_API
|
jpayne@69
|
82 #ifdef HAVE_GCC_ASM_FOR_X87
|
jpayne@69
|
83 PyAPI_FUNC(unsigned short) _Py_get_387controlword(void);
|
jpayne@69
|
84 PyAPI_FUNC(void) _Py_set_387controlword(unsigned short);
|
jpayne@69
|
85 #endif
|
jpayne@69
|
86 #endif
|
jpayne@69
|
87
|
jpayne@69
|
88 /* Py_IS_NAN(X)
|
jpayne@69
|
89 * Return 1 if float or double arg is a NaN, else 0.
|
jpayne@69
|
90 * Caution:
|
jpayne@69
|
91 * X is evaluated more than once.
|
jpayne@69
|
92 * This may not work on all platforms. Each platform has *some*
|
jpayne@69
|
93 * way to spell this, though -- override in pyconfig.h if you have
|
jpayne@69
|
94 * a platform where it doesn't work.
|
jpayne@69
|
95 * Note: PC/pyconfig.h defines Py_IS_NAN as _isnan
|
jpayne@69
|
96 */
|
jpayne@69
|
97 #ifndef Py_IS_NAN
|
jpayne@69
|
98 #if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1
|
jpayne@69
|
99 #define Py_IS_NAN(X) isnan(X)
|
jpayne@69
|
100 #else
|
jpayne@69
|
101 #define Py_IS_NAN(X) ((X) != (X))
|
jpayne@69
|
102 #endif
|
jpayne@69
|
103 #endif
|
jpayne@69
|
104
|
jpayne@69
|
105 /* Py_IS_INFINITY(X)
|
jpayne@69
|
106 * Return 1 if float or double arg is an infinity, else 0.
|
jpayne@69
|
107 * Caution:
|
jpayne@69
|
108 * X is evaluated more than once.
|
jpayne@69
|
109 * This implementation may set the underflow flag if |X| is very small;
|
jpayne@69
|
110 * it really can't be implemented correctly (& easily) before C99.
|
jpayne@69
|
111 * Override in pyconfig.h if you have a better spelling on your platform.
|
jpayne@69
|
112 * Py_FORCE_DOUBLE is used to avoid getting false negatives from a
|
jpayne@69
|
113 * non-infinite value v sitting in an 80-bit x87 register such that
|
jpayne@69
|
114 * v becomes infinite when spilled from the register to 64-bit memory.
|
jpayne@69
|
115 * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf
|
jpayne@69
|
116 */
|
jpayne@69
|
117 #ifndef Py_IS_INFINITY
|
jpayne@69
|
118 # if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1
|
jpayne@69
|
119 # define Py_IS_INFINITY(X) isinf(X)
|
jpayne@69
|
120 # else
|
jpayne@69
|
121 # define Py_IS_INFINITY(X) ((X) && \
|
jpayne@69
|
122 (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X)))
|
jpayne@69
|
123 # endif
|
jpayne@69
|
124 #endif
|
jpayne@69
|
125
|
jpayne@69
|
126 /* Py_IS_FINITE(X)
|
jpayne@69
|
127 * Return 1 if float or double arg is neither infinite nor NAN, else 0.
|
jpayne@69
|
128 * Some compilers (e.g. VisualStudio) have intrisics for this, so a special
|
jpayne@69
|
129 * macro for this particular test is useful
|
jpayne@69
|
130 * Note: PC/pyconfig.h defines Py_IS_FINITE as _finite
|
jpayne@69
|
131 */
|
jpayne@69
|
132 #ifndef Py_IS_FINITE
|
jpayne@69
|
133 #if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1
|
jpayne@69
|
134 #define Py_IS_FINITE(X) isfinite(X)
|
jpayne@69
|
135 #elif defined HAVE_FINITE
|
jpayne@69
|
136 #define Py_IS_FINITE(X) finite(X)
|
jpayne@69
|
137 #else
|
jpayne@69
|
138 #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
|
jpayne@69
|
139 #endif
|
jpayne@69
|
140 #endif
|
jpayne@69
|
141
|
jpayne@69
|
142 /* HUGE_VAL is supposed to expand to a positive double infinity. Python
|
jpayne@69
|
143 * uses Py_HUGE_VAL instead because some platforms are broken in this
|
jpayne@69
|
144 * respect. We used to embed code in pyport.h to try to worm around that,
|
jpayne@69
|
145 * but different platforms are broken in conflicting ways. If you're on
|
jpayne@69
|
146 * a platform where HUGE_VAL is defined incorrectly, fiddle your Python
|
jpayne@69
|
147 * config to #define Py_HUGE_VAL to something that works on your platform.
|
jpayne@69
|
148 */
|
jpayne@69
|
149 #ifndef Py_HUGE_VAL
|
jpayne@69
|
150 #define Py_HUGE_VAL HUGE_VAL
|
jpayne@69
|
151 #endif
|
jpayne@69
|
152
|
jpayne@69
|
153 /* Py_NAN
|
jpayne@69
|
154 * A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or
|
jpayne@69
|
155 * INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform
|
jpayne@69
|
156 * doesn't support NaNs.
|
jpayne@69
|
157 */
|
jpayne@69
|
158 #if !defined(Py_NAN) && !defined(Py_NO_NAN)
|
jpayne@69
|
159 #if !defined(__INTEL_COMPILER)
|
jpayne@69
|
160 #define Py_NAN (Py_HUGE_VAL * 0.)
|
jpayne@69
|
161 #else /* __INTEL_COMPILER */
|
jpayne@69
|
162 #if defined(ICC_NAN_STRICT)
|
jpayne@69
|
163 #pragma float_control(push)
|
jpayne@69
|
164 #pragma float_control(precise, on)
|
jpayne@69
|
165 #pragma float_control(except, on)
|
jpayne@69
|
166 #if defined(_MSC_VER)
|
jpayne@69
|
167 __declspec(noinline)
|
jpayne@69
|
168 #else /* Linux */
|
jpayne@69
|
169 __attribute__((noinline))
|
jpayne@69
|
170 #endif /* _MSC_VER */
|
jpayne@69
|
171 static double __icc_nan()
|
jpayne@69
|
172 {
|
jpayne@69
|
173 return sqrt(-1.0);
|
jpayne@69
|
174 }
|
jpayne@69
|
175 #pragma float_control (pop)
|
jpayne@69
|
176 #define Py_NAN __icc_nan()
|
jpayne@69
|
177 #else /* ICC_NAN_RELAXED as default for Intel Compiler */
|
jpayne@69
|
178 static const union { unsigned char buf[8]; double __icc_nan; } __nan_store = {0,0,0,0,0,0,0xf8,0x7f};
|
jpayne@69
|
179 #define Py_NAN (__nan_store.__icc_nan)
|
jpayne@69
|
180 #endif /* ICC_NAN_STRICT */
|
jpayne@69
|
181 #endif /* __INTEL_COMPILER */
|
jpayne@69
|
182 #endif
|
jpayne@69
|
183
|
jpayne@69
|
184 /* Py_OVERFLOWED(X)
|
jpayne@69
|
185 * Return 1 iff a libm function overflowed. Set errno to 0 before calling
|
jpayne@69
|
186 * a libm function, and invoke this macro after, passing the function
|
jpayne@69
|
187 * result.
|
jpayne@69
|
188 * Caution:
|
jpayne@69
|
189 * This isn't reliable. C99 no longer requires libm to set errno under
|
jpayne@69
|
190 * any exceptional condition, but does require +- HUGE_VAL return
|
jpayne@69
|
191 * values on overflow. A 754 box *probably* maps HUGE_VAL to a
|
jpayne@69
|
192 * double infinity, and we're cool if that's so, unless the input
|
jpayne@69
|
193 * was an infinity and an infinity is the expected result. A C89
|
jpayne@69
|
194 * system sets errno to ERANGE, so we check for that too. We're
|
jpayne@69
|
195 * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or
|
jpayne@69
|
196 * if the returned result is a NaN, or if a C89 box returns HUGE_VAL
|
jpayne@69
|
197 * in non-overflow cases.
|
jpayne@69
|
198 * X is evaluated more than once.
|
jpayne@69
|
199 * Some platforms have better way to spell this, so expect some #ifdef'ery.
|
jpayne@69
|
200 *
|
jpayne@69
|
201 * OpenBSD uses 'isinf()' because a compiler bug on that platform causes
|
jpayne@69
|
202 * the longer macro version to be mis-compiled. This isn't optimal, and
|
jpayne@69
|
203 * should be removed once a newer compiler is available on that platform.
|
jpayne@69
|
204 * The system that had the failure was running OpenBSD 3.2 on Intel, with
|
jpayne@69
|
205 * gcc 2.95.3.
|
jpayne@69
|
206 *
|
jpayne@69
|
207 * According to Tim's checkin, the FreeBSD systems use isinf() to work
|
jpayne@69
|
208 * around a FPE bug on that platform.
|
jpayne@69
|
209 */
|
jpayne@69
|
210 #if defined(__FreeBSD__) || defined(__OpenBSD__)
|
jpayne@69
|
211 #define Py_OVERFLOWED(X) isinf(X)
|
jpayne@69
|
212 #else
|
jpayne@69
|
213 #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \
|
jpayne@69
|
214 (X) == Py_HUGE_VAL || \
|
jpayne@69
|
215 (X) == -Py_HUGE_VAL))
|
jpayne@69
|
216 #endif
|
jpayne@69
|
217
|
jpayne@69
|
218 /* Return whether integral type *type* is signed or not. */
|
jpayne@69
|
219 #define _Py_IntegralTypeSigned(type) ((type)(-1) < 0)
|
jpayne@69
|
220 /* Return the maximum value of integral type *type*. */
|
jpayne@69
|
221 #define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
|
jpayne@69
|
222 /* Return the minimum value of integral type *type*. */
|
jpayne@69
|
223 #define _Py_IntegralTypeMin(type) ((_Py_IntegralTypeSigned(type)) ? -_Py_IntegralTypeMax(type) - 1 : 0)
|
jpayne@69
|
224 /* Check whether *v* is in the range of integral type *type*. This is most
|
jpayne@69
|
225 * useful if *v* is floating-point, since demoting a floating-point *v* to an
|
jpayne@69
|
226 * integral type that cannot represent *v*'s integral part is undefined
|
jpayne@69
|
227 * behavior. */
|
jpayne@69
|
228 #define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type))
|
jpayne@69
|
229
|
jpayne@69
|
230 #endif /* Py_PYMATH_H */
|