You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.5 KiB

  1. #include "Python.h"
  2. #ifdef X87_DOUBLE_ROUNDING
  3. /* On x86 platforms using an x87 FPU, this function is called from the
  4. Py_FORCE_DOUBLE macro (defined in pymath.h) to force a floating-point
  5. number out of an 80-bit x87 FPU register and into a 64-bit memory location,
  6. thus rounding from extended precision to double precision. */
  7. double _Py_force_double(double x)
  8. {
  9. volatile double y;
  10. y = x;
  11. return y;
  12. }
  13. #endif
  14. #ifdef HAVE_GCC_ASM_FOR_X87
  15. /* inline assembly for getting and setting the 387 FPU control word on
  16. gcc/x86 */
  17. unsigned short _Py_get_387controlword(void) {
  18. unsigned short cw;
  19. __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
  20. return cw;
  21. }
  22. void _Py_set_387controlword(unsigned short cw) {
  23. __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
  24. }
  25. #endif
  26. #ifndef HAVE_HYPOT
  27. double hypot(double x, double y)
  28. {
  29. double yx;
  30. x = fabs(x);
  31. y = fabs(y);
  32. if (x < y) {
  33. double temp = x;
  34. x = y;
  35. y = temp;
  36. }
  37. if (x == 0.)
  38. return 0.;
  39. else {
  40. yx = y/x;
  41. return x*sqrt(1.+yx*yx);
  42. }
  43. }
  44. #endif /* HAVE_HYPOT */
  45. #ifndef HAVE_COPYSIGN
  46. double
  47. copysign(double x, double y)
  48. {
  49. /* use atan2 to distinguish -0. from 0. */
  50. if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
  51. return fabs(x);
  52. } else {
  53. return -fabs(x);
  54. }
  55. }
  56. #endif /* HAVE_COPYSIGN */
  57. #ifndef HAVE_ROUND
  58. double
  59. round(double x)
  60. {
  61. double absx, y;
  62. absx = fabs(x);
  63. y = floor(absx);
  64. if (absx - y >= 0.5)
  65. y += 1.0;
  66. return copysign(y, x);
  67. }
  68. #endif /* HAVE_ROUND */