From 917103710e6f2a6c5a28a3630e25f59a46e3cd21 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 22:26:54 +0200 Subject: [PATCH 1/8] compiler-rt: Add __floatsidf & __floatsisf Also add their AEABI aliases, __aeabi_i2f & __aeabi_i2d --- CMakeLists.txt | 1 + std/special/compiler_rt.zig | 4 ++ std/special/compiler_rt/floatsiXf.zig | 90 +++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 std/special/compiler_rt/floatsiXf.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bffaa4ce8..cbcd498251 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -677,6 +677,7 @@ set(ZIG_STD_FILES "special/compiler_rt/fixunstfdi.zig" "special/compiler_rt/fixunstfsi.zig" "special/compiler_rt/fixunstfti.zig" + "special/compiler_rt/floatsiXf.zig" "special/compiler_rt/floattidf.zig" "special/compiler_rt/floattisf.zig" "special/compiler_rt/floattitf.zig" diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 2faec8eab7..53c364e07e 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -64,6 +64,8 @@ comptime { @export("__divsf3", @import("compiler_rt/divsf3.zig").__divsf3, linkage); @export("__divdf3", @import("compiler_rt/divdf3.zig").__divdf3, linkage); + @export("__floatsidf", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); + @export("__floatsisf", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage); @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage); @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage); @@ -151,6 +153,7 @@ comptime { @export("__aeabi_memcmp4", __aeabi_memcmp, linkage); @export("__aeabi_memcmp8", __aeabi_memcmp, linkage); + @export("__aeabi_i2d", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); @export("__aeabi_fneg", @import("compiler_rt/negXf2.zig").__negsf2, linkage); @export("__aeabi_dneg", @import("compiler_rt/negXf2.zig").__negdf2, linkage); @@ -170,6 +173,7 @@ comptime { @export("__aeabi_h2f", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage); @export("__aeabi_f2h", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage); + @export("__aeabi_i2f", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); @export("__aeabi_fadd", @import("compiler_rt/addXf3.zig").__addsf3, linkage); @export("__aeabi_dadd", @import("compiler_rt/addXf3.zig").__adddf3, linkage); @export("__aeabi_fsub", @import("compiler_rt/addXf3.zig").__subsf3, linkage); diff --git a/std/special/compiler_rt/floatsiXf.zig b/std/special/compiler_rt/floatsiXf.zig new file mode 100644 index 0000000000..e96304b471 --- /dev/null +++ b/std/special/compiler_rt/floatsiXf.zig @@ -0,0 +1,90 @@ +const builtin = @import("builtin"); +const std = @import("std"); +const maxInt = std.math.maxInt; + +fn floatsiXf(comptime T: type, a: i32) T { + @setRuntimeSafety(builtin.is_test); + + const Z = @IntType(false, T.bit_count); + const S = @IntType(false, T.bit_count - @clz(Z(T.bit_count) - 1)); + + if (a == 0) { + return T(0.0); + } + + const significandBits = std.math.floatMantissaBits(T); + const exponentBits = std.math.floatExponentBits(T); + const exponentBias = ((1 << exponentBits - 1) - 1); + + const implicitBit = Z(1) << significandBits; + const signBit = Z(1 << Z.bit_count - 1); + + const sign = a >> 31; + // Take absolute value of a via abs(x) = (x^(x >> 31)) - (x >> 31). + const abs_a = (a ^ sign) -% sign; + // The exponent is the width of abs(a) + const exp = Z(31 - @clz(abs_a)); + + const sign_bit = if (sign < 0) signBit else 0; + + var mantissa: Z = undefined; + // Shift a into the significand field and clear the implicit bit. + if (exp <= significandBits) { + // No rounding needed + const shift = @intCast(S, significandBits - exp); + mantissa = @intCast(Z, @bitCast(u32, abs_a)) << shift ^ implicitBit; + } else { + const shift = @intCast(S, exp - significandBits); + // Round to the nearest number after truncation + mantissa = @intCast(Z, @bitCast(u32, abs_a)) >> shift ^ implicitBit; + // Align to the left and check if the truncated part is halfway over + const round = @bitCast(u32, abs_a) << @intCast(u5, 31 - shift); + mantissa += @boolToInt(round > 0x80000000); + // Tie to even + mantissa += mantissa & 1; + } + + // Use the addition instead of a or since we may have a carry from the + // mantissa to the exponent + var result = mantissa; + result += (exp + exponentBias) << significandBits; + result += sign_bit; + + return @bitCast(T, result); +} + +pub extern fn __floatsisf(arg: i32) f32 { + @setRuntimeSafety(builtin.is_test); + return @inlineCall(floatsiXf, f32, arg); +} + +pub extern fn __floatsidf(arg: i32) f64 { + @setRuntimeSafety(builtin.is_test); + return @inlineCall(floatsiXf, f64, arg); +} + +fn test_one_floatsidf(a: i32, expected: u64) void { + const r = __floatsidf(a); + std.testing.expect(@bitCast(u64, r) == expected); +} + +fn test_one_floatsisf(a: i32, expected: u32) void { + const r = __floatsisf(a); + std.testing.expect(@bitCast(u32, r) == expected); +} + +test "floatsidf" { + test_one_floatsidf(0, 0x0000000000000000); + test_one_floatsidf(1, 0x3ff0000000000000); + test_one_floatsidf(-1, 0xbff0000000000000); + test_one_floatsidf(0x7FFFFFFF, 0x41dfffffffc00000); + test_one_floatsidf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xc1e0000000000000); +} + +test "floatsisf" { + test_one_floatsisf(0, 0x00000000); + test_one_floatsisf(1, 0x3f800000); + test_one_floatsisf(-1, 0xbf800000); + test_one_floatsisf(0x7FFFFFFF, 0x4f000000); + test_one_floatsisf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xcf000000); +} From c00167e91a452bb3c051c66157686e11487df94b Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 22:30:20 +0200 Subject: [PATCH 2/8] compiler-rt: More division intrinsics --- std/special/compiler_rt.zig | 63 +++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 53c364e07e..ddd241539c 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -111,6 +111,7 @@ comptime { @export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage); @export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage); + @export("__divmoddi4", __divmoddi4, linkage); @export("__divsi3", __divsi3, linkage); @export("__divdi3", __divdi3, linkage); @export("__udivsi3", __udivsi3, linkage); @@ -126,10 +127,11 @@ comptime { @export("__negdf2", @import("compiler_rt/negXf2.zig").__negdf2, linkage); if (is_arm_arch and !is_arm_64) { + @export("__aeabi_ldivmod", __aeabi_ldivmod, linkage); @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage); @export("__aeabi_idiv", __divsi3, linkage); - @export("__aeabi_idivmod", __divmodsi4, linkage); + @export("__aeabi_idivmod", __aeabi_idivmod, linkage); @export("__aeabi_uidiv", __udivsi3, linkage); @export("__aeabi_uidivmod", __aeabi_uidivmod, linkage); @@ -256,6 +258,14 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn } } +extern fn __divmoddi4(a: i64, b: i64, rem: *i64) i64 { + @setRuntimeSafety(is_test); + + const d = __divdi3(a, b); + rem.* = a -% (d *% b); + return d; +} + extern fn __divdi3(a: i64, b: i64) i64 { @setRuntimeSafety(is_test); @@ -296,14 +306,35 @@ extern fn __umoddi3(a: u64, b: u64) u64 { return r; } -const AeabiUlDivModResult = extern struct { - quot: u64, - rem: u64, -}; -extern fn __aeabi_uldivmod(numerator: u64, denominator: u64) AeabiUlDivModResult { +extern fn __aeabi_uidivmod(n: u32, d: u32) extern struct{q: u32, r: u32} { @setRuntimeSafety(is_test); - var result: AeabiUlDivModResult = undefined; - result.quot = __udivmoddi4(numerator, denominator, &result.rem); + + var result: @typeOf(__aeabi_uidivmod).ReturnType = undefined; + result.q = __udivmodsi4(n, d, &result.r); + return result; +} + +extern fn __aeabi_uldivmod(n: u64, d: u64) extern struct{q: u64, r: u64} { + @setRuntimeSafety(is_test); + + var result: @typeOf(__aeabi_uldivmod).ReturnType = undefined; + result.q = __udivmoddi4(n, d, &result.r); + return result; +} + +extern fn __aeabi_idivmod(n: i32, d: i32) extern struct{q: i32, r: i32} { + @setRuntimeSafety(is_test); + + var result: @typeOf(__aeabi_idivmod).ReturnType = undefined; + result.q = __divmodsi4(n, d, &result.r); + return result; +} + +extern fn __aeabi_ldivmod(n: i64, d: i64) extern struct{q: i64, r:i64} { + @setRuntimeSafety(is_test); + + var result: @typeOf(__aeabi_ldivmod).ReturnType = undefined; + result.q = __divmoddi4(n, d, &result.r); return result; } @@ -397,22 +428,6 @@ test "usesThumb1" { //etc. } -nakedcc fn __aeabi_uidivmod() void { - @setRuntimeSafety(false); - asm volatile ( - \\ push { lr } - \\ sub sp, sp, #4 - \\ mov r2, sp - \\ bl __udivmodsi4 - \\ ldr r1, [sp] - \\ add sp, sp, #4 - \\ pop { pc } - : - : - : "r2", "r1" - ); -} - nakedcc fn __aeabi_memcpy() noreturn { @setRuntimeSafety(false); if (use_thumb_1) { From 92fd2df054d1e1523489c6a959354f5e75f647a4 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 22:31:07 +0200 Subject: [PATCH 3/8] compiler-rt: Add __truncdfsf2 Add AEABI builtin __aeabi_d2f --- std/special/compiler_rt.zig | 4 +++ std/special/compiler_rt/truncXfYf2.zig | 4 +++ std/special/compiler_rt/truncXfYf2_test.zig | 37 +++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index ddd241539c..0898d132f9 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -86,6 +86,8 @@ comptime { @export("__trunctfdf2", @import("compiler_rt/truncXfYf2.zig").__trunctfdf2, linkage); @export("__trunctfsf2", @import("compiler_rt/truncXfYf2.zig").__trunctfsf2, linkage); + @export("__truncdfsf2", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage); + @export("__fixunssfsi", @import("compiler_rt/fixunssfsi.zig").__fixunssfsi, linkage); @export("__fixunssfdi", @import("compiler_rt/fixunssfdi.zig").__fixunssfdi, linkage); @export("__fixunssfti", @import("compiler_rt/fixunssfti.zig").__fixunssfti, linkage); @@ -176,6 +178,8 @@ comptime { @export("__aeabi_f2h", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage); @export("__aeabi_i2f", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); + @export("__aeabi_d2f", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage); + @export("__aeabi_fadd", @import("compiler_rt/addXf3.zig").__addsf3, linkage); @export("__aeabi_dadd", @import("compiler_rt/addXf3.zig").__adddf3, linkage); @export("__aeabi_fsub", @import("compiler_rt/addXf3.zig").__subsf3, linkage); diff --git a/std/special/compiler_rt/truncXfYf2.zig b/std/special/compiler_rt/truncXfYf2.zig index b385090a93..e4c4aa38a7 100644 --- a/std/special/compiler_rt/truncXfYf2.zig +++ b/std/special/compiler_rt/truncXfYf2.zig @@ -16,6 +16,10 @@ pub extern fn __trunctfdf2(a: f128) f64 { return truncXfYf2(f64, f128, a); } +pub extern fn __truncdfsf2(a: f64) f32 { + return truncXfYf2(f32, f64, a); +} + inline fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t { const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits); const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits); diff --git a/std/special/compiler_rt/truncXfYf2_test.zig b/std/special/compiler_rt/truncXfYf2_test.zig index 429372c3f1..eccf7efb7e 100644 --- a/std/special/compiler_rt/truncXfYf2_test.zig +++ b/std/special/compiler_rt/truncXfYf2_test.zig @@ -200,3 +200,40 @@ test "trunctfdf2" { test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000); test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab); } + +const __truncdfsf2 = @import("truncXfYf2.zig").__truncdfsf2; + +fn test__truncdfsf2(a: f64, expected: u32) void { + const x = __truncdfsf2(a); + + const rep = @bitCast(u32, x); + if (rep == expected) { + return; + } + // test other possible NaN representation(signal NaN) + else if (expected == 0x7fc00000) { + if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) { + return; + } + } + + @import("std").debug.warn("got 0x{x} wanted 0x{x}\n", rep, expected); + + @panic("__trunctfsf2 test failure"); +} + +test "truncdfsf2" { + // nan & qnan + test__truncdfsf2(@bitCast(f64, u64(0x7ff8000000000000)), 0x7fc00000); + test__truncdfsf2(@bitCast(f64, u64(0x7ff0000000000001)), 0x7fc00000); + // inf + test__truncdfsf2(@bitCast(f64, u64(0x7ff0000000000000)), 0x7f800000); + test__truncdfsf2(@bitCast(f64, u64(0xfff0000000000000)), 0xff800000); + + test__truncdfsf2(0.0, 0x0); + test__truncdfsf2(1.0, 0x3f800000); + test__truncdfsf2(-1.0, 0xbf800000); + + // huge number becomes inf + test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000); +} From d4434dfb6744b82429060b4118ab71c55dff2141 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 22:35:14 +0200 Subject: [PATCH 4/8] compiler-rt: Add __floatundidf & __floatunsidf Add AEABI builtins __aeabi_ul2d, __aeabi_ui2d --- CMakeLists.txt | 2 + std/special/compiler_rt.zig | 5 ++ std/special/compiler_rt/floatundidf.zig | 24 ++++++++++ std/special/compiler_rt/floatundidf_test.zig | 50 ++++++++++++++++++++ std/special/compiler_rt/floatunsidf.zig | 33 +++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 std/special/compiler_rt/floatundidf.zig create mode 100644 std/special/compiler_rt/floatundidf_test.zig create mode 100644 std/special/compiler_rt/floatunsidf.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index cbcd498251..8ec86d7e6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -678,9 +678,11 @@ set(ZIG_STD_FILES "special/compiler_rt/fixunstfsi.zig" "special/compiler_rt/fixunstfti.zig" "special/compiler_rt/floatsiXf.zig" + "special/compiler_rt/floatunsidf.zig" "special/compiler_rt/floattidf.zig" "special/compiler_rt/floattisf.zig" "special/compiler_rt/floattitf.zig" + "special/compiler_rt/floatundidf.zig" "special/compiler_rt/floatunditf.zig" "special/compiler_rt/floatunsitf.zig" "special/compiler_rt/floatuntidf.zig" diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 0898d132f9..e78d58f00b 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -66,6 +66,9 @@ comptime { @export("__floatsidf", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); @export("__floatsisf", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); + @export("__floatunsidf", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); + @export("__floatundidf", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); + @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage); @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage); @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage); @@ -158,6 +161,8 @@ comptime { @export("__aeabi_memcmp8", __aeabi_memcmp, linkage); @export("__aeabi_i2d", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); + @export("__aeabi_ui2d", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); + @export("__aeabi_ul2d", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); @export("__aeabi_fneg", @import("compiler_rt/negXf2.zig").__negsf2, linkage); @export("__aeabi_dneg", @import("compiler_rt/negXf2.zig").__negdf2, linkage); diff --git a/std/special/compiler_rt/floatundidf.zig b/std/special/compiler_rt/floatundidf.zig new file mode 100644 index 0000000000..68759a2acd --- /dev/null +++ b/std/special/compiler_rt/floatundidf.zig @@ -0,0 +1,24 @@ +const builtin = @import("builtin"); +const std = @import("std"); + +const twop52: f64 = 0x1.0p52; +const twop84: f64 = 0x1.0p84; +const twop84_plus_twop52: f64 = 0x1.00000001p84; + +pub extern fn __floatundidf(a: u64) f64 { + @setRuntimeSafety(builtin.is_test); + + if (a == 0) return 0; + + var high = @bitCast(u64, twop84); + var low = @bitCast(u64, twop52); + + high |= a >> 32; + low |= a & 0xFFFFFFFF; + + return (@bitCast(f64, high) - twop84_plus_twop52) + @bitCast(f64, low); +} + +test "import floatundidf" { + _ = @import("floatundidf_test.zig"); +} diff --git a/std/special/compiler_rt/floatundidf_test.zig b/std/special/compiler_rt/floatundidf_test.zig new file mode 100644 index 0000000000..084ada51c9 --- /dev/null +++ b/std/special/compiler_rt/floatundidf_test.zig @@ -0,0 +1,50 @@ +const __floatundidf = @import("floatundidf.zig").__floatundidf; +const testing = @import("std").testing; + +fn test__floatundidf(a: u64, expected: f64) void { + const r = __floatundidf(a); + testing.expect(r == expected); +} + +test "floatundidf" { + test__floatundidf(0, 0.0); + test__floatundidf(1, 1.0); + test__floatundidf(2, 2.0); + test__floatundidf(20, 20.0); + test__floatundidf(0x7FFFFF8000000000, 0x1.FFFFFEp+62); + test__floatundidf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62); + test__floatundidf(0x7FFFFF0000000000, 0x1.FFFFFCp+62); + test__floatundidf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62); + test__floatundidf(0x8000008000000000, 0x1.000001p+63); + test__floatundidf(0x8000000000000800, 0x1.0000000000001p+63); + test__floatundidf(0x8000010000000000, 0x1.000002p+63); + test__floatundidf(0x8000000000001000, 0x1.0000000000002p+63); + test__floatundidf(0x8000000000000000, 0x1p+63); + test__floatundidf(0x8000000000000001, 0x1p+63); + test__floatundidf(0x0007FB72E8000000, 0x1.FEDCBAp+50); + test__floatundidf(0x0007FB72EA000000, 0x1.FEDCBA8p+50); + test__floatundidf(0x0007FB72EB000000, 0x1.FEDCBACp+50); + test__floatundidf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50); + test__floatundidf(0x0007FB72EC000000, 0x1.FEDCBBp+50); + test__floatundidf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50); + test__floatundidf(0x0007FB72E6000000, 0x1.FEDCB98p+50); + test__floatundidf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50); + test__floatundidf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50); + test__floatundidf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50); + test__floatundidf(0x0007FB72E4000000, 0x1.FEDCB9p+50); + test__floatundidf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DA1, 0x1.1A3CFE870496Dp+57); + test__floatundidf(0x023479FD0E092DB0, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DB8, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DB6, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DBF, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DC1, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DC7, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DC8, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DCF, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DD0, 0x1.1A3CFE870496Ep+57); + test__floatundidf(0x023479FD0E092DD1, 0x1.1A3CFE870496Fp+57); + test__floatundidf(0x023479FD0E092DD8, 0x1.1A3CFE870496Fp+57); + test__floatundidf(0x023479FD0E092DDF, 0x1.1A3CFE870496Fp+57); + test__floatundidf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57); +} diff --git a/std/special/compiler_rt/floatunsidf.zig b/std/special/compiler_rt/floatunsidf.zig new file mode 100644 index 0000000000..db02894448 --- /dev/null +++ b/std/special/compiler_rt/floatunsidf.zig @@ -0,0 +1,33 @@ +const builtin = @import("builtin"); +const std = @import("std"); +const maxInt = std.math.maxInt; + +const implicitBit = u64(1) << 52; + +pub extern fn __floatunsidf(arg: u32) f64 { + @setRuntimeSafety(builtin.is_test); + + if (arg == 0) return 0.0; + + // The exponent is the width of abs(a) + const exp = u64(31) - @clz(arg); + // Shift a into the significand field and clear the implicit bit + const shift = @intCast(u6, 52 - exp); + const mant = u64(arg) << shift ^ implicitBit; + + return @bitCast(f64, mant | (exp + 1023) << 52); +} + +fn test_one_floatunsidf(a: u32, expected: u64) void { + const r = __floatunsidf(a); + std.testing.expect(@bitCast(u64, r) == expected); +} + +test "floatsidf" { + // Test the produced bit pattern + test_one_floatunsidf(0, 0x0000000000000000); + test_one_floatunsidf(1, 0x3ff0000000000000); + test_one_floatunsidf(0x7FFFFFFF, 0x41dfffffffc00000); + test_one_floatunsidf(@intCast(u32, 0x80000000), 0x41e0000000000000); + test_one_floatunsidf(@intCast(u32, 0xFFFFFFFF), 0x41efffffffe00000); +} From 2be066d057946eae592307f8f16679626b85b31d Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 22:38:16 +0200 Subject: [PATCH 5/8] compiler-rt: Add __floatdidf Add AEABI builtin __aeabi_l2d --- CMakeLists.txt | 1 + std/special/compiler_rt.zig | 3 ++ std/special/compiler_rt/floatdidf.zig | 22 +++++++++ std/special/compiler_rt/floatdidf_test.zig | 53 ++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 std/special/compiler_rt/floatdidf.zig create mode 100644 std/special/compiler_rt/floatdidf_test.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ec86d7e6d..991dc8519c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -677,6 +677,7 @@ set(ZIG_STD_FILES "special/compiler_rt/fixunstfdi.zig" "special/compiler_rt/fixunstfsi.zig" "special/compiler_rt/fixunstfti.zig" + "special/compiler_rt/floatdidf.zig" "special/compiler_rt/floatsiXf.zig" "special/compiler_rt/floatunsidf.zig" "special/compiler_rt/floattidf.zig" diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index e78d58f00b..8784aa2b19 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -66,6 +66,7 @@ comptime { @export("__floatsidf", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); @export("__floatsisf", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); + @export("__floatdidf", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage); @export("__floatunsidf", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); @export("__floatundidf", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); @@ -161,8 +162,10 @@ comptime { @export("__aeabi_memcmp8", __aeabi_memcmp, linkage); @export("__aeabi_i2d", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); + @export("__aeabi_l2d", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage); @export("__aeabi_ui2d", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); @export("__aeabi_ul2d", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); + @export("__aeabi_fneg", @import("compiler_rt/negXf2.zig").__negsf2, linkage); @export("__aeabi_dneg", @import("compiler_rt/negXf2.zig").__negdf2, linkage); diff --git a/std/special/compiler_rt/floatdidf.zig b/std/special/compiler_rt/floatdidf.zig new file mode 100644 index 0000000000..1610136413 --- /dev/null +++ b/std/special/compiler_rt/floatdidf.zig @@ -0,0 +1,22 @@ +const builtin = @import("builtin"); +const std = @import("std"); + +const twop52: f64 = 0x1.0p52; +const twop32: f64 = 0x1.0p32; + +pub extern fn __floatdidf(a: i64) f64 { + @setRuntimeSafety(builtin.is_test); + + if (a == 0) return 0; + + var low = @bitCast(i64, twop52); + const high = @intToFloat(f64, @truncate(i32, a >> 32)) * twop32; + + low |= @bitCast(i64, a & 0xFFFFFFFF); + + return (high - twop52) + @bitCast(f64, low); +} + +test "import floatdidf" { + _ = @import("floatdidf_test.zig"); +} diff --git a/std/special/compiler_rt/floatdidf_test.zig b/std/special/compiler_rt/floatdidf_test.zig new file mode 100644 index 0000000000..c854183809 --- /dev/null +++ b/std/special/compiler_rt/floatdidf_test.zig @@ -0,0 +1,53 @@ +const __floatdidf = @import("floatdidf.zig").__floatdidf; +const testing = @import("std").testing; + +fn test__floatdidf(a: i64, expected: f64) void { + const r = __floatdidf(a); + testing.expect(r == expected); +} + +test "floatdidf" { + test__floatdidf(0, 0.0); + test__floatdidf(1, 1.0); + test__floatdidf(2, 2.0); + test__floatdidf(20, 20.0); + test__floatdidf(-1, -1.0); + test__floatdidf(-2, -2.0); + test__floatdidf(-20, -20.0); + test__floatdidf(0x7FFFFF8000000000, 0x1.FFFFFEp+62); + test__floatdidf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62); + test__floatdidf(0x7FFFFF0000000000, 0x1.FFFFFCp+62); + test__floatdidf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62); + test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000008000000000)), -0x1.FFFFFEp+62); + test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000800)), -0x1.FFFFFFFFFFFFEp+62); + test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000010000000000)), -0x1.FFFFFCp+62); + test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000001000)), -0x1.FFFFFFFFFFFFCp+62); + test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000000)), -0x1.000000p+63); + test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000001)), -0x1.000000p+63); + test__floatdidf(0x0007FB72E8000000, 0x1.FEDCBAp+50); + test__floatdidf(0x0007FB72EA000000, 0x1.FEDCBA8p+50); + test__floatdidf(0x0007FB72EB000000, 0x1.FEDCBACp+50); + test__floatdidf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50); + test__floatdidf(0x0007FB72EC000000, 0x1.FEDCBBp+50); + test__floatdidf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50); + test__floatdidf(0x0007FB72E6000000, 0x1.FEDCB98p+50); + test__floatdidf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50); + test__floatdidf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50); + test__floatdidf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50); + test__floatdidf(0x0007FB72E4000000, 0x1.FEDCB9p+50); + test__floatdidf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DA1, 0x1.1A3CFE870496Dp+57); + test__floatdidf(0x023479FD0E092DB0, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DB8, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DB6, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DBF, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DC1, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DC7, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DC8, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DCF, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DD0, 0x1.1A3CFE870496Ep+57); + test__floatdidf(0x023479FD0E092DD1, 0x1.1A3CFE870496Fp+57); + test__floatdidf(0x023479FD0E092DD8, 0x1.1A3CFE870496Fp+57); + test__floatdidf(0x023479FD0E092DDF, 0x1.1A3CFE870496Fp+57); + test__floatdidf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57); +} From 2dce137d926150d34ff7a3650da48c211e34771a Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 23:54:43 +0200 Subject: [PATCH 6/8] compiler-rt: Add __extendsfdf2 Add AEABI builtin __aeabi_f2d --- std/special/compiler_rt.zig | 3 +++ std/special/compiler_rt/extendXfYf2.zig | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 8784aa2b19..2f2c901f94 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -92,6 +92,8 @@ comptime { @export("__truncdfsf2", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage); + @export("__extendsfdf2", @import("compiler_rt/extendXfYf2.zig").__extendsfdf2, linkage); + @export("__fixunssfsi", @import("compiler_rt/fixunssfsi.zig").__fixunssfsi, linkage); @export("__fixunssfdi", @import("compiler_rt/fixunssfdi.zig").__fixunssfdi, linkage); @export("__fixunssfti", @import("compiler_rt/fixunssfti.zig").__fixunssfti, linkage); @@ -161,6 +163,7 @@ comptime { @export("__aeabi_memcmp4", __aeabi_memcmp, linkage); @export("__aeabi_memcmp8", __aeabi_memcmp, linkage); + @export("__aeabi_f2d", @import("compiler_rt/extendXfYf2.zig").__extendsfdf2, linkage); @export("__aeabi_i2d", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); @export("__aeabi_l2d", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage); @export("__aeabi_ui2d", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); diff --git a/std/special/compiler_rt/extendXfYf2.zig b/std/special/compiler_rt/extendXfYf2.zig index 953072b9e3..42559784bb 100644 --- a/std/special/compiler_rt/extendXfYf2.zig +++ b/std/special/compiler_rt/extendXfYf2.zig @@ -2,21 +2,27 @@ const std = @import("std"); const builtin = @import("builtin"); const is_test = builtin.is_test; +pub extern fn __extendsfdf2(a: f32) f64 { + return @inlineCall(extendXfYf2, f64, f32, @bitCast(u32, a)); +} + pub extern fn __extenddftf2(a: f64) f128 { - return extendXfYf2(f128, f64, @bitCast(u64, a)); + return @inlineCall(extendXfYf2, f128, f64, @bitCast(u64, a)); } pub extern fn __extendsftf2(a: f32) f128 { - return extendXfYf2(f128, f32, @bitCast(u32, a)); + return @inlineCall(extendXfYf2, f128, f32, @bitCast(u32, a)); } pub extern fn __extendhfsf2(a: u16) f32 { - return extendXfYf2(f32, f16, a); + return @inlineCall(extendXfYf2, f32, f16, a); } const CHAR_BIT = 8; -inline fn extendXfYf2(comptime dst_t: type, comptime src_t: type, a: @IntType(false, @typeInfo(src_t).Float.bits)) dst_t { +fn extendXfYf2(comptime dst_t: type, comptime src_t: type, a: @IntType(false, @typeInfo(src_t).Float.bits)) dst_t { + @setRuntimeSafety(builtin.is_test); + const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits); const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits); const srcSigBits = std.math.floatMantissaBits(src_t); From 383b8a032eb24f355eaaa560d471c0399df78121 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 8 May 2019 23:48:58 +0200 Subject: [PATCH 7/8] compiler-rt: Add __aeabi_unwind_cpp_pr{0,1,2} --- std/special/compiler_rt.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 2f2c901f94..0169fd154e 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -135,6 +135,10 @@ comptime { @export("__negdf2", @import("compiler_rt/negXf2.zig").__negdf2, linkage); if (is_arm_arch and !is_arm_64) { + @export("__aeabi_unwind_cpp_pr0", __aeabi_unwind_cpp_pr0, strong_linkage); + @export("__aeabi_unwind_cpp_pr1", __aeabi_unwind_cpp_pr1, linkage); + @export("__aeabi_unwind_cpp_pr2", __aeabi_unwind_cpp_pr2, linkage); + @export("__aeabi_ldivmod", __aeabi_ldivmod, linkage); @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage); @@ -273,6 +277,16 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn } } +extern fn __aeabi_unwind_cpp_pr0() void { + unreachable; +} +extern fn __aeabi_unwind_cpp_pr1() void { + unreachable; +} +extern fn __aeabi_unwind_cpp_pr2() void { + unreachable; +} + extern fn __divmoddi4(a: i64, b: i64, rem: *i64) i64 { @setRuntimeSafety(is_test); From 4208fa9ad9102471060c1909effaa6a9315155af Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 9 May 2019 00:25:50 +0200 Subject: [PATCH 8/8] compiler-rt: add __floatsitf --- std/special/compiler_rt.zig | 1 + std/special/compiler_rt/floatsiXf.zig | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 0169fd154e..9b532a98e8 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -67,6 +67,7 @@ comptime { @export("__floatsidf", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); @export("__floatsisf", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); @export("__floatdidf", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage); + @export("__floatsitf", @import("compiler_rt/floatsiXf.zig").__floatsitf, linkage); @export("__floatunsidf", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); @export("__floatundidf", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); diff --git a/std/special/compiler_rt/floatsiXf.zig b/std/special/compiler_rt/floatsiXf.zig index e96304b471..83b3940c1e 100644 --- a/std/special/compiler_rt/floatsiXf.zig +++ b/std/special/compiler_rt/floatsiXf.zig @@ -63,6 +63,16 @@ pub extern fn __floatsidf(arg: i32) f64 { return @inlineCall(floatsiXf, f64, arg); } +pub extern fn __floatsitf(arg: i32) f128 { + @setRuntimeSafety(builtin.is_test); + return @inlineCall(floatsiXf, f128, arg); +} + +fn test_one_floatsitf(a: i32, expected: u128) void { + const r = __floatsitf(a); + std.testing.expect(@bitCast(u128, r) == expected); +} + fn test_one_floatsidf(a: i32, expected: u64) void { const r = __floatsidf(a); std.testing.expect(@bitCast(u64, r) == expected); @@ -88,3 +98,12 @@ test "floatsisf" { test_one_floatsisf(0x7FFFFFFF, 0x4f000000); test_one_floatsisf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xcf000000); } + +test "floatsitf" { + test_one_floatsitf(0, 0); + test_one_floatsitf(0x7FFFFFFF, 0x401dfffffffc00000000000000000000); + test_one_floatsitf(0x12345678, 0x401b2345678000000000000000000000); + test_one_floatsitf(-0x12345678, 0xc01b2345678000000000000000000000); + test_one_floatsitf(@bitCast(i32, @intCast(u32, 0xffffffff)), 0xbfff0000000000000000000000000000); + test_one_floatsitf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xc01e0000000000000000000000000000); +}