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