1 | #ifndef Py_PYMATH_H |
2 | #define Py_PYMATH_H |
3 | |
4 | #include "pyconfig.h" /* include for defines */ |
5 | |
6 | /************************************************************************** |
7 | Symbols and macros to supply platform-independent interfaces to mathematical |
8 | functions and constants |
9 | **************************************************************************/ |
10 | |
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. |
14 | * |
15 | *Note: PC/pyconfig.h defines copysign as _copysign |
16 | */ |
17 | #ifndef HAVE_COPYSIGN |
18 | extern double copysign(double, double); |
19 | #endif |
20 | |
21 | #ifndef HAVE_ROUND |
22 | extern double round(double); |
23 | #endif |
24 | |
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 definition 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 | |
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 | |
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 | |
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 |
94 | #if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1 |
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. |
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. |
111 | * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf |
112 | */ |
113 | #ifndef Py_IS_INFINITY |
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 |
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 |
129 | #if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1 |
130 | #define Py_IS_FINITE(X) isfinite(X) |
131 | #elif defined HAVE_FINITE |
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 | #if !defined(__INTEL_COMPILER) |
156 | #define Py_NAN (Py_HUGE_VAL * 0.) |
157 | #else /* __INTEL_COMPILER */ |
158 | #if defined(ICC_NAN_STRICT) |
159 | #pragma float_control(push) |
160 | #pragma float_control(precise, on) |
161 | #pragma float_control(except, on) |
162 | #if defined(_MSC_VER) |
163 | __declspec(noinline) |
164 | #else /* Linux */ |
165 | __attribute__((noinline)) |
166 | #endif /* _MSC_VER */ |
167 | static double __icc_nan() |
168 | { |
169 | return sqrt(-1.0); |
170 | } |
171 | #pragma float_control (pop) |
172 | #define Py_NAN __icc_nan() |
173 | #else /* ICC_NAN_RELAXED as default for Intel Compiler */ |
174 | static union { unsigned char buf[8]; double __icc_nan; } __nan_store = {0,0,0,0,0,0,0xf8,0x7f}; |
175 | #define Py_NAN (__nan_store.__icc_nan) |
176 | #endif /* ICC_NAN_STRICT */ |
177 | #endif /* __INTEL_COMPILER */ |
178 | #endif |
179 | |
180 | /* Py_OVERFLOWED(X) |
181 | * Return 1 iff a libm function overflowed. Set errno to 0 before calling |
182 | * a libm function, and invoke this macro after, passing the function |
183 | * result. |
184 | * Caution: |
185 | * This isn't reliable. C99 no longer requires libm to set errno under |
186 | * any exceptional condition, but does require +- HUGE_VAL return |
187 | * values on overflow. A 754 box *probably* maps HUGE_VAL to a |
188 | * double infinity, and we're cool if that's so, unless the input |
189 | * was an infinity and an infinity is the expected result. A C89 |
190 | * system sets errno to ERANGE, so we check for that too. We're |
191 | * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or |
192 | * if the returned result is a NaN, or if a C89 box returns HUGE_VAL |
193 | * in non-overflow cases. |
194 | * X is evaluated more than once. |
195 | * Some platforms have better way to spell this, so expect some #ifdef'ery. |
196 | * |
197 | * OpenBSD uses 'isinf()' because a compiler bug on that platform causes |
198 | * the longer macro version to be mis-compiled. This isn't optimal, and |
199 | * should be removed once a newer compiler is available on that platform. |
200 | * The system that had the failure was running OpenBSD 3.2 on Intel, with |
201 | * gcc 2.95.3. |
202 | * |
203 | * According to Tim's checkin, the FreeBSD systems use isinf() to work |
204 | * around a FPE bug on that platform. |
205 | */ |
206 | #if defined(__FreeBSD__) || defined(__OpenBSD__) |
207 | #define Py_OVERFLOWED(X) isinf(X) |
208 | #else |
209 | #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \ |
210 | (X) == Py_HUGE_VAL || \ |
211 | (X) == -Py_HUGE_VAL)) |
212 | #endif |
213 | |
214 | #endif /* Py_PYMATH_H */ |
215 | |