Commit cb7bdc2da1

Marc Tiehuis <marctiehuis@gmail.com>
2018-06-28 11:37:58
compiler_rt: Add floatuntitf
1 parent 61ebfe6
Changed files (2)
std
std/special/compiler_rt/floatuntitf.zig
@@ -0,0 +1,60 @@
+const builtin = @import("builtin");
+const is_test = builtin.is_test;
+
+const LDBL_MANT_DIG = 113;
+
+pub extern fn __floatuntitf(arg: u128) f128 {
+    @setRuntimeSafety(is_test);
+
+    if (arg == 0)
+        return 0.0;
+
+    var a = arg;
+    const N: u32 = @sizeOf(u128) * 8;
+    const sd = @bitCast(i32, N -% @clz(a)); // number of significant digits
+    var e: i32 = sd -% 1; // exponent
+    if (sd > LDBL_MANT_DIG) {
+        //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
+        //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
+        //                                                12345678901234567890123456
+        //  1 = msb 1 bit
+        //  P = bit LDBL_MANT_DIG-1 bits to the right of 1
+        //  Q = bit LDBL_MANT_DIG bits to the right of 1
+        //  R = "or" of all bits to the right of Q
+        switch (sd) {
+            LDBL_MANT_DIG + 1 => {
+                a <<= 1;
+            },
+            LDBL_MANT_DIG + 2 => {},
+            else => {
+                const shift_amt = @bitCast(i32, N +% (LDBL_MANT_DIG + 2)) -% sd;
+                const shift_amt_u7 = @intCast(u7, shift_amt);
+                a = (a >> @intCast(u7, sd -% (LDBL_MANT_DIG + 2))) |
+                    @boolToInt((a & (u128(@maxValue(u128)) >> shift_amt_u7)) != 0);
+            },
+        }
+        // finish
+        a |= @boolToInt((a & 4) != 0); // Or P into R
+        a +%= 1; // round - this step may add a significant bit
+        a >>= 2; // dump Q and R
+        // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits
+        if ((a & (u128(1) << LDBL_MANT_DIG)) != 0) {
+            a >>= 1;
+            e +%= 1;
+        }
+        // a is now rounded to LDBL_MANT_DIG bits
+    } else {
+        a <<= @intCast(u7, LDBL_MANT_DIG -% sd);
+        // a is now rounded to LDBL_MANT_DIG bits
+    }
+
+    const high: u128 = (@intCast(u64, (e +% 16383)) << 48) | // exponent
+        (@truncate(u64, a >> 64) & 0x0000ffffffffffff); // mantissa-high
+    const low = @truncate(u64, a); // mantissa-low
+
+    return @bitCast(f128, low | (high << 64));
+}
+
+test "import floatuntitf" {
+    _ = @import("floatuntitf_test.zig");
+}
std/special/compiler_rt/floatuntitf_test.zig
@@ -0,0 +1,99 @@
+const __floatuntitf = @import("floatuntitf.zig").__floatuntitf;
+const assert = @import("std").debug.assert;
+
+fn test__floatuntitf(a: u128, expected: f128) void {
+    const x = __floatuntitf(a);
+    assert(x == expected);
+}
+
+test "floatuntitf" {
+    test__floatuntitf(0, 0.0);
+
+    test__floatuntitf(1, 1.0);
+    test__floatuntitf(2, 2.0);
+    test__floatuntitf(20, 20.0);
+
+    test__floatuntitf(0x7FFFFF8000000000, 0x1.FFFFFEp+62);
+    test__floatuntitf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62);
+    test__floatuntitf(0x7FFFFF0000000000, 0x1.FFFFFCp+62);
+    test__floatuntitf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62);
+    test__floatuntitf(0x7FFFFFFFFFFFFFFF, 0xF.FFFFFFFFFFFFFFEp+59);
+    test__floatuntitf(0xFFFFFFFFFFFFFFFE, 0xF.FFFFFFFFFFFFFFEp+60);
+    test__floatuntitf(0xFFFFFFFFFFFFFFFF, 0xF.FFFFFFFFFFFFFFFp+60);
+
+    test__floatuntitf(0x8000008000000000, 0x8.000008p+60);
+    test__floatuntitf(0x8000000000000800, 0x8.0000000000008p+60);
+    test__floatuntitf(0x8000010000000000, 0x8.00001p+60);
+    test__floatuntitf(0x8000000000001000, 0x8.000000000001p+60);
+
+    test__floatuntitf(0x8000000000000000, 0x8p+60);
+    test__floatuntitf(0x8000000000000001, 0x8.000000000000001p+60);
+
+    test__floatuntitf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
+
+    test__floatuntitf(0x0007FB72EA000000, 0x1.FEDCBA8p+50);
+    test__floatuntitf(0x0007FB72EB000000, 0x1.FEDCBACp+50);
+    test__floatuntitf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50);
+    test__floatuntitf(0x0007FB72EC000000, 0x1.FEDCBBp+50);
+    test__floatuntitf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50);
+
+    test__floatuntitf(0x0007FB72E6000000, 0x1.FEDCB98p+50);
+    test__floatuntitf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50);
+    test__floatuntitf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50);
+    test__floatuntitf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50);
+    test__floatuntitf(0x0007FB72E4000000, 0x1.FEDCB9p+50);
+
+    test__floatuntitf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57);
+    test__floatuntitf(0x023479FD0E092DA1, 0x1.1A3CFE870496D08p+57);
+    test__floatuntitf(0x023479FD0E092DB0, 0x1.1A3CFE870496D8p+57);
+    test__floatuntitf(0x023479FD0E092DB8, 0x1.1A3CFE870496DCp+57);
+    test__floatuntitf(0x023479FD0E092DB6, 0x1.1A3CFE870496DBp+57);
+    test__floatuntitf(0x023479FD0E092DBF, 0x1.1A3CFE870496DF8p+57);
+    test__floatuntitf(0x023479FD0E092DC1, 0x1.1A3CFE870496E08p+57);
+    test__floatuntitf(0x023479FD0E092DC7, 0x1.1A3CFE870496E38p+57);
+    test__floatuntitf(0x023479FD0E092DC8, 0x1.1A3CFE870496E4p+57);
+    test__floatuntitf(0x023479FD0E092DCF, 0x1.1A3CFE870496E78p+57);
+    test__floatuntitf(0x023479FD0E092DD0, 0x1.1A3CFE870496E8p+57);
+    test__floatuntitf(0x023479FD0E092DD1, 0x1.1A3CFE870496E88p+57);
+    test__floatuntitf(0x023479FD0E092DD8, 0x1.1A3CFE870496ECp+57);
+    test__floatuntitf(0x023479FD0E092DDF, 0x1.1A3CFE870496EF8p+57);
+    test__floatuntitf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57);
+
+    test__floatuntitf(make_ti(0x023479FD0E092DC0, 0), 0x1.1A3CFE870496Ep+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DA1, 1), 0x1.1A3CFE870496D08p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DB0, 2), 0x1.1A3CFE870496D8p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DB8, 3), 0x1.1A3CFE870496DCp+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DB6, 4), 0x1.1A3CFE870496DBp+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DBF, 5), 0x1.1A3CFE870496DF8p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DC1, 6), 0x1.1A3CFE870496E08p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DC7, 7), 0x1.1A3CFE870496E38p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DC8, 8), 0x1.1A3CFE870496E4p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DCF, 9), 0x1.1A3CFE870496E78p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DD0, 0), 0x1.1A3CFE870496E8p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DD1, 11), 0x1.1A3CFE870496E88p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DD8, 12), 0x1.1A3CFE870496ECp+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DDF, 13), 0x1.1A3CFE870496EF8p+121);
+    test__floatuntitf(make_ti(0x023479FD0E092DE0, 14), 0x1.1A3CFE870496Fp+121);
+
+    test__floatuntitf(make_ti(0, 0xFFFFFFFFFFFFFFFF), 0x1.FFFFFFFFFFFFFFFEp+63);
+
+    test__floatuntitf(make_ti(0xFFFFFFFFFFFFFFFF, 0x0000000000000000), 0x1.FFFFFFFFFFFFFFFEp+127);
+    test__floatuntitf(make_ti(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 0x1.0000000000000000p+128);
+
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC2801), 0x1.23456789ABCDEF0123456789ABC3p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC3000), 0x1.23456789ABCDEF0123456789ABC3p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC37FF), 0x1.23456789ABCDEF0123456789ABC3p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC3800), 0x1.23456789ABCDEF0123456789ABC4p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC4000), 0x1.23456789ABCDEF0123456789ABC4p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC47FF), 0x1.23456789ABCDEF0123456789ABC4p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC4800), 0x1.23456789ABCDEF0123456789ABC4p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC4801), 0x1.23456789ABCDEF0123456789ABC5p+124);
+    test__floatuntitf(make_ti(0x123456789ABCDEF0, 0x123456789ABC57FF), 0x1.23456789ABCDEF0123456789ABC5p+124);
+}
+
+fn make_ti(high: u64, low: u64) u128 {
+    var result: u128 = high;
+    result <<= 64;
+    result |= low;
+    return result;
+}