master
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: u80 = @bitCast(result);
9
10 if (rep == expected) return true;
11 // test other possible NaN representations (signal NaN)
12 if (math.isNan(result) and math.isNan(@as(f80, @bitCast(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: f80 = @bitCast((@as(u80, @bitCast(x)) + 1) | integerBit);
29 // Prev float (assuming normal, non-zero result)
30 const x_minus_eps: f80 = @bitCast((@as(u80, @bitCast(x)) - 1) | integerBit);
31
32 // Make sure result is more accurate than the adjacent floats
33 const err_x = @abs(@mulAdd(f80, x, b, -a));
34 const err_x_plus_eps = @abs(@mulAdd(f80, x_plus_eps, b, -a));
35 const err_x_minus_eps = @abs(@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 // NaN / any = NaN
43 try expect__divxf3_result(math.nan(f80), 0x1.23456789abcdefp+5, 0x7fffC000000000000000);
44 // inf / any(except inf and nan) = inf
45 try expect__divxf3_result(math.inf(f80), 0x1.23456789abcdefp+5, 0x7fff8000000000000000);
46 // inf / inf = nan
47 try expect__divxf3_result(math.inf(f80), math.inf(f80), 0x7fffC000000000000000);
48 // inf / nan = nan
49 try expect__divxf3_result(math.inf(f80), math.nan(f80), 0x7fffC000000000000000);
50
51 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.eedcbaba3a94546558237654321fp-1);
52 try test__divxf3(0x1.a2b34c56d745382f9abf2c3dfeffp-50, 0x1.ed2c3ba15935332532287654321fp-9);
53 try test__divxf3(0x1.2345f6aaaa786555f42432abcdefp+456, 0x1.edacbba9874f765463544dd3621fp+6400);
54 try test__divxf3(0x1.2d3456f789ba6322bc665544edefp-234, 0x1.eddcdba39f3c8b7a36564354321fp-4455);
55 try test__divxf3(0x1.2345f6b77b7a8953365433abcdefp+234, 0x1.edcba987d6bb3aa467754354321fp-4055);
56 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.a2b34c56d745382f9abf2c3dfeffp-50);
57 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.1234567890abcdef987654321123p0);
58 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.12394205810257120adae8929f23p+16);
59 try test__divxf3(0x1.a23b45362464523375893ab4cdefp+5, 0x1.febdcefa1231245f9abf2c3dfeffp-50);
60
61 // Result rounds down to zero
62 try expect__divxf3_result(6.72420628622418701252535563464350521E-4932, 2.0, 0x0);
63}