diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-09-26 01:54:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-26 01:54:45 -0400 |
| commit | 68bb3945708c43109c48bda3664176307d45b62c (patch) | |
| tree | afb9731e10cef9d192560b52cd9ae2cf179775c4 /lib/std/special/compiler_rt/floattitf.zig | |
| parent | 6128bc728d1e1024a178c16c2149f5b1a167a013 (diff) | |
| parent | 4637e8f9699af9c3c6cf4df50ef5bb67c7a318a4 (diff) | |
| download | zig-68bb3945708c43109c48bda3664176307d45b62c.tar.gz zig-68bb3945708c43109c48bda3664176307d45b62c.zip | |
Merge pull request #3315 from ziglang/mv-std-lib
Move std/ to lib/std/
Diffstat (limited to 'lib/std/special/compiler_rt/floattitf.zig')
| -rw-r--r-- | lib/std/special/compiler_rt/floattitf.zig | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/std/special/compiler_rt/floattitf.zig b/lib/std/special/compiler_rt/floattitf.zig new file mode 100644 index 0000000000..b05282e293 --- /dev/null +++ b/lib/std/special/compiler_rt/floattitf.zig @@ -0,0 +1,71 @@ +const builtin = @import("builtin"); +const is_test = builtin.is_test; +const std = @import("std"); +const maxInt = std.math.maxInt; + +const LDBL_MANT_DIG = 113; + +pub extern fn __floattitf(arg: i128) f128 { + @setRuntimeSafety(is_test); + + if (arg == 0) + return 0.0; + + var ai = arg; + const N: u32 = 128; + const si = ai >> @intCast(u7, (N - 1)); + ai = ((ai ^ si) -% si); + var a = @bitCast(u128, ai); + + const sd = @bitCast(i32, N - @clz(u128, 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 shift1_amt = @intCast(i32, sd - (LDBL_MANT_DIG + 2)); + const shift1_amt_u7 = @intCast(u7, shift1_amt); + + const shift2_amt = @intCast(i32, N + (LDBL_MANT_DIG + 2)) - sd; + const shift2_amt_u7 = @intCast(u7, shift2_amt); + + a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, maxInt(u128)) >> shift2_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 s = @bitCast(u128, arg) >> (128 - 64); + const high: u128 = (@intCast(u64, s) & 0x8000000000000000) | // sign + (@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 floattitf" { + _ = @import("floattitf_test.zig"); +} |
