Commit 92fd2df054

LemonBoy <thatlemon@gmail.com>
2019-05-08 22:31:07
compiler-rt: Add __truncdfsf2
Add AEABI builtin __aeabi_d2f
1 parent c00167e
Changed files (3)
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);
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);
+}
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);