authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-25 16:31:43-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-25 17:21:09-07:00
logd930e015c75d60e22d0a116a1f4fa0df4d68b6b6
treefc97f77211741891ed67400964b01484a7785ad9
parent6c0114e04436404b1c59b66125733350b4be2f5b

compiler_rt: Implement __divxf3 for f80


3 files changed, 274 insertions(+), 9 deletions(-)

lib/std/special/compiler_rt.zig+7-9
......@@ -253,6 +253,8 @@ comptime {
253253 @export(__divsf3, .{ .name = "__divsf3", .linkage = linkage });
254254 const __divdf3 = @import("compiler_rt/divdf3.zig").__divdf3;
255255 @export(__divdf3, .{ .name = "__divdf3", .linkage = linkage });
256 const __divxf3 = @import("compiler_rt/divxf3.zig").__divxf3;
257 @export(__divxf3, .{ .name = "__divxf3", .linkage = linkage });
256258 const __divtf3 = @import("compiler_rt/divtf3.zig").__divtf3;
257259 @export(__divtf3, .{ .name = "__divtf3", .linkage = linkage });
258260
......@@ -725,17 +727,13 @@ comptime {
725727 }
726728
727729 if (!is_test) {
728 @export(fmodl, .{ .name = "fmodl", .linkage = linkage });
729730 if (long_double_is_f80) {
730 @export(fmodl, .{ .name = "fmodx", .linkage = linkage });
731 } else {
732 @export(fmodx, .{ .name = "fmodx", .linkage = linkage });
733 }
734 if (long_double_is_f128) {
735 @export(fmodl, .{ .name = "fmodq", .linkage = linkage });
736 } else {
737 @export(fmodq, .{ .name = "fmodq", .linkage = linkage });
731 @export(fmodx, .{ .name = "fmodl", .linkage = linkage });
732 } else if (long_double_is_f128) {
733 @export(fmodq, .{ .name = "fmodl", .linkage = linkage });
738734 }
735 @export(fmodx, .{ .name = "fmodx", .linkage = linkage });
736 @export(fmodq, .{ .name = "fmodq", .linkage = linkage });
739737
740738 @export(floorf, .{ .name = "floorf", .linkage = linkage });
741739 @export(floor, .{ .name = "floor", .linkage = linkage });
lib/std/special/compiler_rt/divxf3.zig created+202
......@@ -0,0 +1,202 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const normalize = @import("divdf3.zig").normalize;
4const wideMultiply = @import("divdf3.zig").wideMultiply;
5
6pub fn __divxf3(a: f80, b: f80) callconv(.C) f80 {
7 @setRuntimeSafety(builtin.is_test);
8 const T = f80;
9 const Z = std.meta.Int(.unsigned, @bitSizeOf(T));
10
11 const significandBits = std.math.floatMantissaBits(T);
12 const fractionalBits = std.math.floatFractionalBits(T);
13 const exponentBits = std.math.floatExponentBits(T);
14
15 const signBit = (@as(Z, 1) << (significandBits + exponentBits));
16 const maxExponent = ((1 << exponentBits) - 1);
17 const exponentBias = (maxExponent >> 1);
18
19 const integerBit = (@as(Z, 1) << fractionalBits);
20 const quietBit = integerBit >> 1;
21 const significandMask = (@as(Z, 1) << significandBits) - 1;
22
23 const absMask = signBit - 1;
24 const qnanRep = @bitCast(Z, std.math.nan(T)) | quietBit;
25 const infRep = @bitCast(Z, std.math.inf(T));
26
27 const aExponent = @truncate(u32, (@bitCast(Z, a) >> significandBits) & maxExponent);
28 const bExponent = @truncate(u32, (@bitCast(Z, b) >> significandBits) & maxExponent);
29 const quotientSign: Z = (@bitCast(Z, a) ^ @bitCast(Z, b)) & signBit;
30
31 var aSignificand: Z = @bitCast(Z, a) & significandMask;
32 var bSignificand: Z = @bitCast(Z, b) & significandMask;
33 var scale: i32 = 0;
34
35 // Detect if a or b is zero, denormal, infinity, or NaN.
36 if (aExponent -% 1 >= maxExponent - 1 or bExponent -% 1 >= maxExponent - 1) {
37 const aAbs: Z = @bitCast(Z, a) & absMask;
38 const bAbs: Z = @bitCast(Z, b) & absMask;
39
40 // NaN / anything = qNaN
41 if (aAbs > infRep) return @bitCast(T, @bitCast(Z, a) | quietBit);
42 // anything / NaN = qNaN
43 if (bAbs > infRep) return @bitCast(T, @bitCast(Z, b) | quietBit);
44
45 if (aAbs == infRep) {
46 // infinity / infinity = NaN
47 if (bAbs == infRep) {
48 return @bitCast(T, qnanRep);
49 }
50 // infinity / anything else = +/- infinity
51 else {
52 return @bitCast(T, aAbs | quotientSign);
53 }
54 }
55
56 // anything else / infinity = +/- 0
57 if (bAbs == infRep) return @bitCast(T, quotientSign);
58
59 if (aAbs == 0) {
60 // zero / zero = NaN
61 if (bAbs == 0) {
62 return @bitCast(T, qnanRep);
63 }
64 // zero / anything else = +/- zero
65 else {
66 return @bitCast(T, quotientSign);
67 }
68 }
69 // anything else / zero = +/- infinity
70 if (bAbs == 0) return @bitCast(T, infRep | quotientSign);
71
72 // one or both of a or b is denormal, the other (if applicable) is a
73 // normal number. Renormalize one or both of a and b, and set scale to
74 // include the necessary exponent adjustment.
75 if (aAbs < integerBit) scale +%= normalize(T, &aSignificand);
76 if (bAbs < integerBit) scale -%= normalize(T, &bSignificand);
77 }
78 var quotientExponent: i32 = @bitCast(i32, aExponent -% bExponent) +% scale;
79
80 // Align the significand of b as a Q63 fixed-point number in the range
81 // [1, 2.0) and get a Q64 approximate reciprocal using a small minimax
82 // polynomial approximation: reciprocal = 3/4 + 1/sqrt(2) - b/2. This
83 // is accurate to about 3.5 binary digits.
84 const q63b = @intCast(u64, bSignificand);
85 var recip64 = @as(u64, 0x7504f333F9DE6484) -% q63b;
86 // 0x7504f333F9DE6484 / 2^64 + 1 = 3/4 + 1/sqrt(2)
87
88 // Now refine the reciprocal estimate using a Newton-Raphson iteration:
89 //
90 // x1 = x0 * (2 - x0 * b)
91 //
92 // This doubles the number of correct binary digits in the approximation
93 // with each iteration.
94 var correction64: u64 = undefined;
95 correction64 = @truncate(u64, ~(@as(u128, recip64) *% q63b >> 64) +% 1);
96 recip64 = @truncate(u64, @as(u128, recip64) *% correction64 >> 63);
97 correction64 = @truncate(u64, ~(@as(u128, recip64) *% q63b >> 64) +% 1);
98 recip64 = @truncate(u64, @as(u128, recip64) *% correction64 >> 63);
99 correction64 = @truncate(u64, ~(@as(u128, recip64) *% q63b >> 64) +% 1);
100 recip64 = @truncate(u64, @as(u128, recip64) *% correction64 >> 63);
101 correction64 = @truncate(u64, ~(@as(u128, recip64) *% q63b >> 64) +% 1);
102 recip64 = @truncate(u64, @as(u128, recip64) *% correction64 >> 63);
103 correction64 = @truncate(u64, ~(@as(u128, recip64) *% q63b >> 64) +% 1);
104 recip64 = @truncate(u64, @as(u128, recip64) *% correction64 >> 63);
105
106 // The reciprocal may have overflowed to zero if the upper half of b is
107 // exactly 1.0. This would sabatoge the full-width final stage of the
108 // computation that follows, so we adjust the reciprocal down by one bit.
109 recip64 -%= 1;
110
111 // We need to perform one more iteration to get us to 112 binary digits;
112 // The last iteration needs to happen with extra precision.
113
114 // NOTE: This operation is equivalent to __multi3, which is not implemented
115 // in some architechures
116 var reciprocal: u128 = undefined;
117 var correction: u128 = undefined;
118 var dummy: u128 = undefined;
119 wideMultiply(u128, recip64, q63b, &dummy, &correction);
120
121 correction = -%correction;
122
123 const cHi = @truncate(u64, correction >> 64);
124 const cLo = @truncate(u64, correction);
125
126 var r64cH: u128 = undefined;
127 var r64cL: u128 = undefined;
128 wideMultiply(u128, recip64, cHi, &dummy, &r64cH);
129 wideMultiply(u128, recip64, cLo, &dummy, &r64cL);
130
131 reciprocal = r64cH + (r64cL >> 64);
132
133 // Adjust the final 128-bit reciprocal estimate downward to ensure that it
134 // is strictly smaller than the infinitely precise exact reciprocal. Because
135 // the computation of the Newton-Raphson step is truncating at every step,
136 // this adjustment is small; most of the work is already done.
137 reciprocal -%= 2;
138
139 // The numerical reciprocal is accurate to within 2^-112, lies in the
140 // interval [0.5, 1.0), and is strictly smaller than the true reciprocal
141 // of b. Multiplying a by this reciprocal thus gives a numerical q = a/b
142 // in Q127 with the following properties:
143 //
144 // 1. q < a/b
145 // 2. q is in the interval [0.5, 2.0)
146 // 3. The error in q is bounded away from 2^-63 (actually, we have
147 // many bits to spare, but this is all we need).
148
149 // We need a 128 x 128 multiply high to compute q.
150 var quotient128: u128 = undefined;
151 var quotientLo: u128 = undefined;
152 wideMultiply(u128, aSignificand << 2, reciprocal, &quotient128, &quotientLo);
153
154 // Two cases: quotient is in [0.5, 1.0) or quotient is in [1.0, 2.0).
155 // Right shift the quotient if it falls in the [1,2) range and adjust the
156 // exponent accordingly.
157 var quotient: u64 = if (quotient128 < (integerBit << 1)) b: {
158 quotientExponent -= 1;
159 break :b @intCast(u64, quotient128);
160 } else @intCast(u64, quotient128 >> 1);
161
162 // We are going to compute a residual of the form
163 //
164 // r = a - q*b
165 //
166 // We know from the construction of q that r satisfies:
167 //
168 // 0 <= r < ulp(q)*b
169 //
170 // If r is greater than 1/2 ulp(q)*b, then q rounds up. Otherwise, we
171 // already have the correct result. The exact halfway case cannot occur.
172 var residual: u64 = -%(quotient *% q63b);
173
174 const writtenExponent = quotientExponent + exponentBias;
175 if (writtenExponent >= maxExponent) {
176 // If we have overflowed the exponent, return infinity.
177 return @bitCast(T, infRep | quotientSign);
178 } else if (writtenExponent < 1) {
179 if (writtenExponent == 0) {
180 // Check whether the rounded result is normal.
181 if (residual > (bSignificand >> 1)) { // round
182 if (quotient == (integerBit - 1)) // If the rounded result is normal, return it
183 return @bitCast(T, @bitCast(Z, std.math.floatMin(T)) | quotientSign);
184 }
185 }
186 // Flush denormals to zero. In the future, it would be nice to add
187 // code to round them correctly.
188 return @bitCast(T, quotientSign);
189 } else {
190 const round = @boolToInt(residual > (bSignificand >> 1));
191 // Insert the exponent
192 var absResult = quotient | (@intCast(Z, writtenExponent) << significandBits);
193 // Round
194 absResult +%= round;
195 // Insert the sign and return
196 return @bitCast(T, absResult | quotientSign | integerBit);
197 }
198}
199
200test {
201 _ = @import("divxf3_test.zig");
202}
lib/std/special/compiler_rt/divxf3_test.zig created+65
......@@ -0,0 +1,65 @@
1const std = @import("std");
2const math = std.math;
3const testing = std.testing;
4
5const __divxf3 = @import("divxf3.zig").__divxf3;
6
7fn compareResult(result: f80, expected: u80) bool {
8 const rep = @bitCast(u80, result);
9
10 if (rep == expected) return true;
11 // test other possible NaN representations (signal NaN)
12 if (math.isNan(result) and math.isNan(@bitCast(f80, expected))) return true;
13
14 return false;
15}
16
17fn expect__divxf3_result(a: f80, b: f80, expected: u80) !void {
18 const x = __divxf3(a, b);
19 const ret = compareResult(x, expected);
20 try testing.expect(ret == true);
21}
22
23fn test__divxf3(a: f80, b: f80) !void {
24 const integerBit = 1 << math.floatFractionalBits(f80);
25 const x = __divxf3(a, b);
26
27 // Next float (assuming normal, non-zero result)
28 const x_plus_eps = @bitCast(f80, (@bitCast(u80, x) + 1) | integerBit);
29 // Prev float (assuming normal, non-zero result)
30 const x_minus_eps = @bitCast(f80, (@bitCast(u80, x) - 1) | integerBit);
31
32 // Make sure result is more accurate than the adjacent floats
33 const err_x = std.math.fabs(@mulAdd(f80, x, b, -a));
34 const err_x_plus_eps = std.math.fabs(@mulAdd(f80, x_plus_eps, b, -a));
35 const err_x_minus_eps = std.math.fabs(@mulAdd(f80, x_minus_eps, b, -a));
36
37 try testing.expect(err_x_minus_eps > err_x);
38 try testing.expect(err_x_plus_eps > err_x);
39}
40
41test "divxf3" {
42 // qNaN / any = qNaN
43 try expect__divxf3_result(math.qnan_f80, 0x1.23456789abcdefp+5, 0x7fffC000000000000000);
44 // NaN / any = NaN
45 try expect__divxf3_result(math.nan_f80, 0x1.23456789abcdefp+5, 0x7fffC000000000000000);
46 // inf / any(except inf and nan) = inf
47 try expect__divxf3_result(math.inf(f80), 0x1.23456789abcdefp+5, 0x7fff8000000000000000);
48 // inf / inf = nan
49 try expect__divxf3_result(math.inf(f80), math.inf(f80), 0x7fffC000000000000000);
50 // inf / nan = nan
51 try expect__divxf3_result(math.inf(f80), math.nan(f80), 0x7fffC000000000000000);
52
53 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.eedcbaba3a94546558237654321fp-1);
54 try test__divxf3(0x1.a2b34c56d745382f9abf2c3dfeffp-50, 0x1.ed2c3ba15935332532287654321fp-9);
55 try test__divxf3(0x1.2345f6aaaa786555f42432abcdefp+456, 0x1.edacbba9874f765463544dd3621fp+6400);
56 try test__divxf3(0x1.2d3456f789ba6322bc665544edefp-234, 0x1.eddcdba39f3c8b7a36564354321fp-4455);
57 try test__divxf3(0x1.2345f6b77b7a8953365433abcdefp+234, 0x1.edcba987d6bb3aa467754354321fp-4055);
58 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.a2b34c56d745382f9abf2c3dfeffp-50);
59 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.1234567890abcdef987654321123p0);
60 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.12394205810257120adae8929f23p+16);
61 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.febdcefa1231245f9abf2c3dfeffp-50);
62
63 // Result rounds down to zero
64 try expect__divxf3_result(6.72420628622418701252535563464350521E-4932, 2.0, 0x0);
65}