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