pymath.h 7.4 KB

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