diff options
| author | Andrea Orru <andrea@orru.io> | 2018-08-06 01:43:19 -0400 |
|---|---|---|
| committer | Andrea Orru <andrea@orru.io> | 2018-08-06 01:43:19 -0400 |
| commit | d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d (patch) | |
| tree | e9fa3caec533a0d1e2b434868b2fde1f9240e5c8 /std/special/compiler_rt/floatuntitf.zig | |
| parent | 06614b3fa09954464c2e2f32756cacedc178a282 (diff) | |
| parent | 63a23e848a62d5f167f8d5478de9766cb24aa6eb (diff) | |
| download | zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.tar.gz zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.zip | |
Merge branch 'master' into zen_stdlib
Diffstat (limited to 'std/special/compiler_rt/floatuntitf.zig')
| -rw-r--r-- | std/special/compiler_rt/floatuntitf.zig | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/std/special/compiler_rt/floatuntitf.zig b/std/special/compiler_rt/floatuntitf.zig new file mode 100644 index 0000000000..6354c89287 --- /dev/null +++ b/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"); +} |
