authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-06-28 21:37:58+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-06-30 21:58:59+12:00
logcb7bdc2da1b2d7a3e78b272928ced77ccdd12148
tree2740dd29af3a84596b73cf703e96bb61cbc4af5a
parent61ebfe6603c8fef26008683f96b62dab2c502429

compiler_rt: Add floatuntitf


2 files changed, 159 insertions(+), 0 deletions(-)

std/special/compiler_rt/floatuntitf.zig created+60
...@@ -0,0 +1,60 @@
1const builtin = @import("builtin");
2const is_test = builtin.is_test;
3
4const LDBL_MANT_DIG = 113;
5
6pub extern fn __floatuntitf(arg: u128) f128 {
7 @setRuntimeSafety(is_test);
8
9 if (arg == 0)
10 return 0.0;
11
12 var a = arg;
13 const N: u32 = @sizeOf(u128) * 8;
14 const sd = @bitCast(i32, N -% @clz(a)); // number of significant digits
15 var e: i32 = sd -% 1; // exponent
16 if (sd > LDBL_MANT_DIG) {
17 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
18 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
19 // 12345678901234567890123456
20 // 1 = msb 1 bit
21 // P = bit LDBL_MANT_DIG-1 bits to the right of 1
22 // Q = bit LDBL_MANT_DIG bits to the right of 1
23 // R = "or" of all bits to the right of Q
24 switch (sd) {
25 LDBL_MANT_DIG + 1 => {
26 a <<= 1;
27 },
28 LDBL_MANT_DIG + 2 => {},
29 else => {
30 const shift_amt = @bitCast(i32, N +% (LDBL_MANT_DIG + 2)) -% sd;
31 const shift_amt_u7 = @intCast(u7, shift_amt);
32 a = (a >> @intCast(u7, sd -% (LDBL_MANT_DIG + 2))) |
33 @boolToInt((a & (u128(@maxValue(u128)) >> shift_amt_u7)) != 0);
34 },
35 }
36 // finish
37 a |= @boolToInt((a & 4) != 0); // Or P into R
38 a +%= 1; // round - this step may add a significant bit
39 a >>= 2; // dump Q and R
40 // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits
41 if ((a & (u128(1) << LDBL_MANT_DIG)) != 0) {
42 a >>= 1;
43 e +%= 1;
44 }
45 // a is now rounded to LDBL_MANT_DIG bits
46 } else {
47 a <<= @intCast(u7, LDBL_MANT_DIG -% sd);
48 // a is now rounded to LDBL_MANT_DIG bits
49 }
50
51 const high: u128 = (@intCast(u64, (e +% 16383)) << 48) | // exponent
52 (@truncate(u64, a >> 64) & 0x0000ffffffffffff); // mantissa-high
53 const low = @truncate(u64, a); // mantissa-low
54
55 return @bitCast(f128, low | (high << 64));
56}
57
58test "import floatuntitf" {
59 _ = @import("floatuntitf_test.zig");
60}
std/special/compiler_rt/floatuntitf_test.zig created+99
...@@ -0,0 +1,99 @@
1const __floatuntitf = @import("floatuntitf.zig").__floatuntitf;
2const assert = @import("std").debug.assert;
3
4fn test__floatuntitf(a: u128, expected: f128) void {
5 const x = __floatuntitf(a);
6 assert(x == expected);
7}
8
9test "floatuntitf" {
10 test__floatuntitf(0, 0.0);
11
12 test__floatuntitf(1, 1.0);
13 test__floatuntitf(2, 2.0);
14 test__floatuntitf(20, 20.0);
15
16 test__floatuntitf(0x7FFFFF8000000000, 0x1.FFFFFEp+62);
17 test__floatuntitf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62);
18 test__floatuntitf(0x7FFFFF0000000000, 0x1.FFFFFCp+62);
19 test__floatuntitf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62);
20 test__floatuntitf(0x7FFFFFFFFFFFFFFF, 0xF.FFFFFFFFFFFFFFEp+59);
21 test__floatuntitf(0xFFFFFFFFFFFFFFFE, 0xF.FFFFFFFFFFFFFFEp+60);
22 test__floatuntitf(0xFFFFFFFFFFFFFFFF, 0xF.FFFFFFFFFFFFFFFp+60);
23
24 test__floatuntitf(0x8000008000000000, 0x8.000008p+60);
25 test__floatuntitf(0x8000000000000800, 0x8.0000000000008p+60);
26 test__floatuntitf(0x8000010000000000, 0x8.00001p+60);
27 test__floatuntitf(0x8000000000001000, 0x8.000000000001p+60);
28
29 test__floatuntitf(0x8000000000000000, 0x8p+60);
30 test__floatuntitf(0x8000000000000001, 0x8.000000000000001p+60);
31
32 test__floatuntitf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
33
34 test__floatuntitf(0x0007FB72EA000000, 0x1.FEDCBA8p+50);
35 test__floatuntitf(0x0007FB72EB000000, 0x1.FEDCBACp+50);
36 test__floatuntitf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50);
37 test__floatuntitf(0x0007FB72EC000000, 0x1.FEDCBBp+50);
38 test__floatuntitf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50);
39
40 test__floatuntitf(0x0007FB72E6000000, 0x1.FEDCB98p+50);
41 test__floatuntitf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50);
42 test__floatuntitf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50);
43 test__floatuntitf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50);
44 test__floatuntitf(0x0007FB72E4000000, 0x1.FEDCB9p+50);
45
46 test__floatuntitf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57);
47 test__floatuntitf(0x023479FD0E092DA1, 0x1.1A3CFE870496D08p+57);
48 test__floatuntitf(0x023479FD0E092DB0, 0x1.1A3CFE870496D8p+57);
49 test__floatuntitf(0x023479FD0E092DB8, 0x1.1A3CFE870496DCp+57);
50 test__floatuntitf(0x023479FD0E092DB6, 0x1.1A3CFE870496DBp+57);
51 test__floatuntitf(0x023479FD0E092DBF, 0x1.1A3CFE870496DF8p+57);
52 test__floatuntitf(0x023479FD0E092DC1, 0x1.1A3CFE870496E08p+57);
53 test__floatuntitf(0x023479FD0E092DC7, 0x1.1A3CFE870496E38p+57);
54 test__floatuntitf(0x023479FD0E092DC8, 0x1.1A3CFE870496E4p+57);
55 test__floatuntitf(0x023479FD0E092DCF, 0x1.1A3CFE870496E78p+57);
56 test__floatuntitf(0x023479FD0E092DD0, 0x1.1A3CFE870496E8p+57);
57 test__floatuntitf(0x023479FD0E092DD1, 0x1.1A3CFE870496E88p+57);
58 test__floatuntitf(0x023479FD0E092DD8, 0x1.1A3CFE870496ECp+57);
59 test__floatuntitf(0x023479FD0E092DDF, 0x1.1A3CFE870496EF8p+57);
60 test__floatuntitf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57);
61
62 test__floatuntitf(make_ti(0x023479FD0E092DC0, 0), 0x1.1A3CFE870496Ep+121);
63 test__floatuntitf(make_ti(0x023479FD0E092DA1, 1), 0x1.1A3CFE870496D08p+121);
64 test__floatuntitf(make_ti(0x023479FD0E092DB0, 2), 0x1.1A3CFE870496D8p+121);
65 test__floatuntitf(make_ti(0x023479FD0E092DB8, 3), 0x1.1A3CFE870496DCp+121);
66 test__floatuntitf(make_ti(0x023479FD0E092DB6, 4), 0x1.1A3CFE870496DBp+121);
67 test__floatuntitf(make_ti(0x023479FD0E092DBF, 5), 0x1.1A3CFE870496DF8p+121);
68 test__floatuntitf(make_ti(0x023479FD0E092DC1, 6), 0x1.1A3CFE870496E08p+121);
69 test__floatuntitf(make_ti(0x023479FD0E092DC7, 7), 0x1.1A3CFE870496E38p+121);
70 test__floatuntitf(make_ti(0x023479FD0E092DC8, 8), 0x1.1A3CFE870496E4p+121);
71 test__floatuntitf(make_ti(0x023479FD0E092DCF, 9), 0x1.1A3CFE870496E78p+121);
72 test__floatuntitf(make_ti(0x023479FD0E092DD0, 0), 0x1.1A3CFE870496E8p+121);
73 test__floatuntitf(make_ti(0x023479FD0E092DD1, 11), 0x1.1A3CFE870496E88p+121);
74 test__floatuntitf(make_ti(0x023479FD0E092DD8, 12), 0x1.1A3CFE870496ECp+121);
75 test__floatuntitf(make_ti(0x023479FD0E092DDF, 13), 0x1.1A3CFE870496EF8p+121);
76 test__floatuntitf(make_ti(0x023479FD0E092DE0, 14), 0x1.1A3CFE870496Fp+121);
77
78 test__floatuntitf(make_ti(0, 0xFFFFFFFFFFFFFFFF), 0x1.FFFFFFFFFFFFFFFEp+63);
79
80 test__floatuntitf(make_ti(0xFFFFFFFFFFFFFFFF, 0x0000000000000000), 0x1.FFFFFFFFFFFFFFFEp+127);
81 test__floatuntitf(make_ti(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 0x1.0000000000000000p+128);
82
83 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC2801), 0x1.23456789ABCDEF0123456789ABC3p+124);
84 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC3000), 0x1.23456789ABCDEF0123456789ABC3p+124);
85 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC37FF), 0x1.23456789ABCDEF0123456789ABC3p+124);
86 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC3800), 0x1.23456789ABCDEF0123456789ABC4p+124);
87 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC4000), 0x1.23456789ABCDEF0123456789ABC4p+124);
88 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC47FF), 0x1.23456789ABCDEF0123456789ABC4p+124);
89 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC4800), 0x1.23456789ABCDEF0123456789ABC4p+124);
90 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC4801), 0x1.23456789ABCDEF0123456789ABC5p+124);
91 test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC57FF), 0x1.23456789ABCDEF0123456789ABC5p+124);
92}
93
94fn make_ti(high: u64, low: u64) u128 {
95 var result: u128 = high;
96 result <<= 64;
97 result |= low;
98 return result;
99}