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.

96 lines
1.9 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. #ifdef _Py_MEMORY_SANITIZER
  18. __attribute__((no_sanitize_memory))
  19. #endif
  20. unsigned short _Py_get_387controlword(void) {
  21. unsigned short cw;
  22. __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
  23. return cw;
  24. }
  25. void _Py_set_387controlword(unsigned short cw) {
  26. __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
  27. }
  28. #endif
  29. #ifndef HAVE_HYPOT
  30. double hypot(double x, double y)
  31. {
  32. double yx;
  33. x = fabs(x);
  34. y = fabs(y);
  35. if (x < y) {
  36. double temp = x;
  37. x = y;
  38. y = temp;
  39. }
  40. if (x == 0.)
  41. return 0.;
  42. else {
  43. yx = y/x;
  44. return x*sqrt(1.+yx*yx);
  45. }
  46. }
  47. #endif /* HAVE_HYPOT */
  48. #ifndef HAVE_COPYSIGN
  49. double
  50. copysign(double x, double y)
  51. {
  52. /* use atan2 to distinguish -0. from 0. */
  53. if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
  54. return fabs(x);
  55. } else {
  56. return -fabs(x);
  57. }
  58. }
  59. #endif /* HAVE_COPYSIGN */
  60. #ifndef HAVE_ROUND
  61. double
  62. round(double x)
  63. {
  64. double absx, y;
  65. absx = fabs(x);
  66. y = floor(absx);
  67. if (absx - y >= 0.5)
  68. y += 1.0;
  69. return copysign(y, x);
  70. }
  71. #endif /* HAVE_ROUND */
  72. static const unsigned int BitLengthTable[32] = {
  73. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
  75. };
  76. unsigned int _Py_bit_length(unsigned long d) {
  77. unsigned int d_bits = 0;
  78. while (d >= 32) {
  79. d_bits += 6;
  80. d >>= 6;
  81. }
  82. d_bits += BitLengthTable[d];
  83. return d_bits;
  84. }