Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame^] | 1 | // This file is distributed under the University of Illinois Open Source |
| 2 | // License. See LICENSE.TXT for details. |
| 3 | |
| 4 | // long double __gcc_qsub(long double x, long double y); |
| 5 | // This file implements the PowerPC 128-bit double-double add operation. |
| 6 | // This implementation is shamelessly cribbed from Apple's DDRT, circa 1993(!) |
| 7 | |
| 8 | #include "DD.h" |
| 9 | |
| 10 | long double __gcc_qsub(long double x, long double y) |
| 11 | { |
| 12 | static const uint32_t infinityHi = UINT32_C(0x7ff00000); |
| 13 | |
| 14 | DD dst = { .ld = x }, src = { .ld = y }; |
| 15 | |
| 16 | register double A = dst.hi, a = dst.lo, |
| 17 | B = -src.hi, b = -src.lo; |
| 18 | |
| 19 | // If both operands are zero: |
| 20 | if ((A == 0.0) && (B == 0.0)) { |
| 21 | dst.hi = A + B; |
| 22 | dst.lo = 0.0; |
| 23 | return dst.ld; |
| 24 | } |
| 25 | |
| 26 | // If either operand is NaN or infinity: |
| 27 | const doublebits abits = { .d = A }; |
| 28 | const doublebits bbits = { .d = B }; |
| 29 | if ((((uint32_t)(abits.x >> 32) & infinityHi) == infinityHi) || |
| 30 | (((uint32_t)(bbits.x >> 32) & infinityHi) == infinityHi)) { |
| 31 | dst.hi = A + B; |
| 32 | dst.lo = 0.0; |
| 33 | return dst.ld; |
| 34 | } |
| 35 | |
| 36 | // If the computation overflows: |
| 37 | // This may be playing things a little bit fast and loose, but it will do for a start. |
| 38 | const double testForOverflow = A + (B + (a + b)); |
| 39 | const doublebits testbits = { .d = testForOverflow }; |
| 40 | if (((uint32_t)(testbits.x >> 32) & infinityHi) == infinityHi) { |
| 41 | dst.hi = testForOverflow; |
| 42 | dst.lo = 0.0; |
| 43 | return dst.ld; |
| 44 | } |
| 45 | |
| 46 | double H, h; |
| 47 | double T, t; |
| 48 | double W, w; |
| 49 | double Y; |
| 50 | |
| 51 | H = B + (A - (A + B)); |
| 52 | T = b + (a - (a + b)); |
| 53 | h = A + (B - (A + B)); |
| 54 | t = a + (b - (a + b)); |
| 55 | |
| 56 | if (fabs(A) <= fabs(B)) |
| 57 | w = (a + b) + h; |
| 58 | else |
| 59 | w = (a + b) + H; |
| 60 | |
| 61 | W = (A + B) + w; |
| 62 | Y = (A + B) - W; |
| 63 | Y += w; |
| 64 | |
| 65 | if (fabs(a) <= fabs(b)) |
| 66 | w = t + Y; |
| 67 | else |
| 68 | w = T + Y; |
| 69 | |
| 70 | dst.hi = Y = W + w; |
| 71 | dst.lo = (W - Y) + w; |
| 72 | |
| 73 | return dst.ld; |
| 74 | } |