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