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