Skip to content

Commit 9ed1beb

Browse files
committedMay 30, 2014
Implement __divtf3 for IEEE quad precision.
Patch by: GuanHong Liu Differential Revision: http://reviews.llvm.org/D2800 llvm-svn: 209886
1 parent 3ef452e commit 9ed1beb

File tree

2 files changed

+297
-0
lines changed

2 files changed

+297
-0
lines changed
 

‎compiler-rt/lib/builtins/divtf3.c

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
//===-- lib/divtf3.c - Quad-precision division --------------------*- C -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is dual licensed under the MIT and the University of Illinois Open
6+
// Source Licenses. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file implements quad-precision soft-float division
11+
// with the IEEE-754 default rounding (to nearest, ties to even).
12+
//
13+
// For simplicity, this implementation currently flushes denormals to zero.
14+
// It should be a fairly straightforward exercise to implement gradual
15+
// underflow with correct rounding.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#define QUAD_PRECISION
20+
#include "fp_lib.h"
21+
22+
#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
23+
COMPILER_RT_ABI fp_t __divtf3(fp_t a, fp_t b) {
24+
25+
const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
26+
const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
27+
const rep_t quotientSign = (toRep(a) ^ toRep(b)) & signBit;
28+
29+
rep_t aSignificand = toRep(a) & significandMask;
30+
rep_t bSignificand = toRep(b) & significandMask;
31+
int scale = 0;
32+
33+
// Detect if a or b is zero, denormal, infinity, or NaN.
34+
if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
35+
36+
const rep_t aAbs = toRep(a) & absMask;
37+
const rep_t bAbs = toRep(b) & absMask;
38+
39+
// NaN / anything = qNaN
40+
if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
41+
// anything / NaN = qNaN
42+
if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
43+
44+
if (aAbs == infRep) {
45+
// infinity / infinity = NaN
46+
if (bAbs == infRep) return fromRep(qnanRep);
47+
// infinity / anything else = +/- infinity
48+
else return fromRep(aAbs | quotientSign);
49+
}
50+
51+
// anything else / infinity = +/- 0
52+
if (bAbs == infRep) return fromRep(quotientSign);
53+
54+
if (!aAbs) {
55+
// zero / zero = NaN
56+
if (!bAbs) return fromRep(qnanRep);
57+
// zero / anything else = +/- zero
58+
else return fromRep(quotientSign);
59+
}
60+
// anything else / zero = +/- infinity
61+
if (!bAbs) return fromRep(infRep | quotientSign);
62+
63+
// one or both of a or b is denormal, the other (if applicable) is a
64+
// normal number. Renormalize one or both of a and b, and set scale to
65+
// include the necessary exponent adjustment.
66+
if (aAbs < implicitBit) scale += normalize(&aSignificand);
67+
if (bAbs < implicitBit) scale -= normalize(&bSignificand);
68+
}
69+
70+
// Or in the implicit significand bit. (If we fell through from the
71+
// denormal path it was already set by normalize( ), but setting it twice
72+
// won't hurt anything.)
73+
aSignificand |= implicitBit;
74+
bSignificand |= implicitBit;
75+
int quotientExponent = aExponent - bExponent + scale;
76+
77+
// Align the significand of b as a Q63 fixed-point number in the range
78+
// [1, 2.0) and get a Q64 approximate reciprocal using a small minimax
79+
// polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
80+
// is accurate to about 3.5 binary digits.
81+
const uint64_t q63b = bSignificand >> 49;
82+
uint64_t recip64 = UINT64_C(0x7504f333F9DE6484) - q63b;
83+
// 0x7504f333F9DE6484 / 2^64 + 1 = 3/4 + 1/sqrt(2)
84+
85+
// Now refine the reciprocal estimate using a Newton-Raphson iteration:
86+
//
87+
// x1 = x0 * (2 - x0 * b)
88+
//
89+
// This doubles the number of correct binary digits in the approximation
90+
// with each iteration.
91+
uint64_t correction64;
92+
correction64 = -((rep_t)recip64 * q63b >> 64);
93+
recip64 = (rep_t)recip64 * correction64 >> 63;
94+
correction64 = -((rep_t)recip64 * q63b >> 64);
95+
recip64 = (rep_t)recip64 * correction64 >> 63;
96+
correction64 = -((rep_t)recip64 * q63b >> 64);
97+
recip64 = (rep_t)recip64 * correction64 >> 63;
98+
correction64 = -((rep_t)recip64 * q63b >> 64);
99+
recip64 = (rep_t)recip64 * correction64 >> 63;
100+
correction64 = -((rep_t)recip64 * q63b >> 64);
101+
recip64 = (rep_t)recip64 * correction64 >> 63;
102+
103+
// recip64 might have overflowed to exactly zero in the preceeding
104+
// computation if the high word of b is exactly 1.0. This would sabotage
105+
// the full-width final stage of the computation that follows, so we adjust
106+
// recip64 downward by one bit.
107+
recip64--;
108+
109+
// We need to perform one more iteration to get us to 112 binary digits;
110+
// The last iteration needs to happen with extra precision.
111+
const uint64_t q127blo = bSignificand << 15;
112+
rep_t correction, reciprocal;
113+
114+
// NOTE: This operation is equivalent to __multi3, which is not implemented
115+
// in some architechure
116+
rep_t r64q63, r64q127, r64cH, r64cL, dummy;
117+
wideMultiply((rep_t)recip64, (rep_t)q63b, &dummy, &r64q63);
118+
wideMultiply((rep_t)recip64, (rep_t)q127blo, &dummy, &r64q127);
119+
120+
correction = -(r64q63 + (r64q127 >> 64));
121+
122+
uint64_t cHi = correction >> 64;
123+
uint64_t cLo = correction;
124+
125+
wideMultiply((rep_t)recip64, (rep_t)cHi, &dummy, &r64cH);
126+
wideMultiply((rep_t)recip64, (rep_t)cLo, &dummy, &r64cL);
127+
128+
reciprocal = r64cH + (r64cL >> 64);
129+
130+
// We already adjusted the 64-bit estimate, now we need to adjust the final
131+
// 128-bit reciprocal estimate downward to ensure that it is strictly smaller
132+
// than the infinitely precise exact reciprocal. Because the computation
133+
// of the Newton-Raphson step is truncating at every step, this adjustment
134+
// is small; most of the work is already done.
135+
reciprocal -= 2;
136+
137+
// The numerical reciprocal is accurate to within 2^-112, lies in the
138+
// interval [0.5, 1.0), and is strictly smaller than the true reciprocal
139+
// of b. Multiplying a by this reciprocal thus gives a numerical q = a/b
140+
// in Q127 with the following properties:
141+
//
142+
// 1. q < a/b
143+
// 2. q is in the interval [0.5, 2.0)
144+
// 3. the error in q is bounded away from 2^-113 (actually, we have a
145+
// couple of bits to spare, but this is all we need).
146+
147+
// We need a 128 x 128 multiply high to compute q, which isn't a basic
148+
// operation in C, so we need to be a little bit fussy.
149+
rep_t quotient, quotientLo;
150+
wideMultiply(aSignificand << 2, reciprocal, &quotient, &quotientLo);
151+
152+
// Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
153+
// In either case, we are going to compute a residual of the form
154+
//
155+
// r = a - q*b
156+
//
157+
// We know from the construction of q that r satisfies:
158+
//
159+
// 0 <= r < ulp(q)*b
160+
//
161+
// if r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
162+
// already have the correct result. The exact halfway case cannot occur.
163+
// We also take this time to right shift quotient if it falls in the [1,2)
164+
// range and adjust the exponent accordingly.
165+
rep_t residual;
166+
rep_t qb;
167+
168+
if (quotient < (implicitBit << 1)) {
169+
wideMultiply(quotient, bSignificand, &dummy, &qb);
170+
residual = (aSignificand << 113) - qb;
171+
quotientExponent--;
172+
} else {
173+
quotient >>= 1;
174+
wideMultiply(quotient, bSignificand, &dummy, &qb);
175+
residual = (aSignificand << 112) - qb;
176+
}
177+
178+
const int writtenExponent = quotientExponent + exponentBias;
179+
180+
if (writtenExponent >= maxExponent) {
181+
// If we have overflowed the exponent, return infinity.
182+
return fromRep(infRep | quotientSign);
183+
}
184+
else if (writtenExponent < 1) {
185+
// Flush denormals to zero. In the future, it would be nice to add
186+
// code to round them correctly.
187+
return fromRep(quotientSign);
188+
}
189+
else {
190+
const bool round = (residual << 1) >= bSignificand;
191+
// Clear the implicit bit
192+
rep_t absResult = quotient & significandMask;
193+
// Insert the exponent
194+
absResult |= (rep_t)writtenExponent << significandBits;
195+
// Round
196+
absResult += round;
197+
// Insert the sign and return
198+
const long double result = fromRep(absResult | quotientSign);
199+
return result;
200+
}
201+
}
202+
203+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===--------------- divtf3_test.c - Test __divtf3 ------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is dual licensed under the MIT and the University of Illinois Open
6+
// Source Licenses. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file tests __divtf3 for the compiler_rt library.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include <stdio.h>
15+
16+
#if __LDBL_MANT_DIG__ == 113
17+
18+
#include "fp_test.h"
19+
20+
// Returns: a / b
21+
long double __divtf3(long double a, long double b);
22+
23+
int test__divtf3(long double a, long double b,
24+
uint64_t expectedHi, uint64_t expectedLo)
25+
{
26+
long double x = __divtf3(a, b);
27+
int ret = compareResultLD(x, expectedHi, expectedLo);
28+
29+
if (ret){
30+
printf("error in test__divtf3(%.20Lf, %.20Lf) = %.20Lf, "
31+
"expected %.20Lf\n", a, b, x,
32+
fromRep128(expectedHi, expectedLo));
33+
}
34+
return ret;
35+
}
36+
37+
char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0};
38+
39+
#endif
40+
41+
int main()
42+
{
43+
#if __LDBL_MANT_DIG__ == 113
44+
// qNaN / any = qNaN
45+
if (test__divtf3(makeQNaN128(),
46+
0x1.23456789abcdefp+5L,
47+
UINT64_C(0x7fff800000000000),
48+
UINT64_C(0x0)))
49+
return 1;
50+
// NaN / any = NaN
51+
if (test__divtf3(makeNaN128(UINT64_C(0x800030000000)),
52+
0x1.23456789abcdefp+5L,
53+
UINT64_C(0x7fff800000000000),
54+
UINT64_C(0x0)))
55+
return 1;
56+
// inf / any = inf
57+
if (test__divtf3(makeInf128(),
58+
0x1.23456789abcdefp+5L,
59+
UINT64_C(0x7fff000000000000),
60+
UINT64_C(0x0)))
61+
return 1;
62+
// any / any
63+
if (test__divtf3(0x1.a23b45362464523375893ab4cdefp+5L,
64+
0x1.eedcbaba3a94546558237654321fp-1L,
65+
UINT64_C(0x4004b0b72924d407),
66+
UINT64_C(0x0717e84356c6eba2)))
67+
return 1;
68+
if (test__divtf3(0x1.a2b34c56d745382f9abf2c3dfeffp-50L,
69+
0x1.ed2c3ba15935332532287654321fp-9L,
70+
UINT64_C(0x3fd5b2af3f828c9b),
71+
UINT64_C(0x40e51f64cde8b1f2)))
72+
return 15;
73+
if (test__divtf3(0x1.2345f6aaaa786555f42432abcdefp+456L,
74+
0x1.edacbba9874f765463544dd3621fp+6400L,
75+
UINT64_C(0x28c62e15dc464466),
76+
UINT64_C(0xb5a07586348557ac)))
77+
return 1;
78+
if (test__divtf3(0x1.2d3456f789ba6322bc665544edefp-234L,
79+
0x1.eddcdba39f3c8b7a36564354321fp-4455L,
80+
UINT64_C(0x507b38442b539266),
81+
UINT64_C(0x22ce0f1d024e1252)))
82+
return 1;
83+
if (test__divtf3(0x1.2345f6b77b7a8953365433abcdefp+234L,
84+
0x1.edcba987d6bb3aa467754354321fp-4055L,
85+
UINT64_C(0x50bf2e02f0798d36),
86+
UINT64_C(0x5e6fcb6b60044078)))
87+
return 1;
88+
89+
#else
90+
printf("skipped\n");
91+
92+
#endif
93+
return 0;
94+
}

0 commit comments

Comments
 (0)