pymath.h 7.0 KB

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