pymath.h 8.4 KB

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