Commit e355bcce36

LemonBoy <thatlemon@gmail.com>
2020-08-29 14:39:03
compiler-rt: Add missing floatdisf routine
Add __floatdisf and __aeabi_l2f Closes #6188
1 parent ff4fde7
Changed files (4)
lib/std/special/compiler_rt/floatdisf_test.zig
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2015-2020 Zig Contributors
+// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
+// The MIT license requires this copyright notice to be included in all copies
+// and substantial portions of the software.
+const __floatdisf = @import("floatXisf.zig").__floatdisf;
+const testing = @import("std").testing;
+
+fn test__floatdisf(a: i64, expected: f32) void {
+    const x = __floatdisf(a);
+    testing.expect(x == expected);
+}
+
+test "floatdisf" {
+    test__floatdisf(0, 0.0);
+    test__floatdisf(1, 1.0);
+    test__floatdisf(2, 2.0);
+    test__floatdisf(-1, -1.0);
+    test__floatdisf(-2, -2.0);
+    test__floatdisf(0x7FFFFF8000000000, 0x1.FFFFFEp+62);
+    test__floatdisf(0x7FFFFF0000000000, 0x1.FFFFFCp+62);
+    test__floatdisf(0x8000008000000000, -0x1.FFFFFEp+62);
+    test__floatdisf(0x8000010000000000, -0x1.FFFFFCp+62);
+    test__floatdisf(0x8000000000000000, -0x1.000000p+63);
+    test__floatdisf(0x8000000000000001, -0x1.000000p+63);
+    test__floatdisf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72EA000000, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72EB000000, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72EBFFFFFF, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72EC000000, 0x1.FEDCBCp+50);
+    test__floatdisf(0x0007FB72E8000001, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72E6000000, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72E7000000, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72E7FFFFFF, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72E4000001, 0x1.FEDCBAp+50);
+    test__floatdisf(0x0007FB72E4000000, 0x1.FEDCB8p+50);
+}
lib/std/special/compiler_rt/floattisf_test.zig
@@ -3,7 +3,7 @@
 // This file is part of [zig](https://ziglang.org/), which is MIT licensed.
 // The MIT license requires this copyright notice to be included in all copies
 // and substantial portions of the software.
-const __floattisf = @import("floattisf.zig").__floattisf;
+const __floattisf = @import("floatXisf.zig").__floattisf;
 const testing = @import("std").testing;
 
 fn test__floattisf(a: i128, expected: f32) void {
lib/std/special/compiler_rt/floattisf.zig → lib/std/special/compiler_rt/floatXisf.zig
@@ -4,25 +4,28 @@
 // The MIT license requires this copyright notice to be included in all copies
 // and substantial portions of the software.
 const builtin = @import("builtin");
-const is_test = builtin.is_test;
 const std = @import("std");
 const maxInt = std.math.maxInt;
 
 const FLT_MANT_DIG = 24;
 
-pub fn __floattisf(arg: i128) callconv(.C) f32 {
-    @setRuntimeSafety(is_test);
+fn __floatXisf(comptime T: type, arg: T) f32 {
+    @setRuntimeSafety(builtin.is_test);
+
+    const Z = std.meta.Int(false, T.bit_count);
+    const S = std.meta.Int(false, T.bit_count - @clz(Z, @as(Z, T.bit_count) - 1));
 
-    if (arg == 0)
-        return 0.0;
+    if (arg == 0) {
+        return @as(f32, 0.0);
+    }
 
     var ai = arg;
-    const N: u32 = 128;
-    const si = ai >> @intCast(u7, (N - 1));
+    const N: u32 = T.bit_count;
+    const si = ai >> @intCast(S, (N - 1));
     ai = ((ai ^ si) -% si);
-    var a = @bitCast(u128, ai);
+    var a = @bitCast(Z, ai);
 
-    const sd = @bitCast(i32, N - @clz(u128, a)); // number of significant digits
+    const sd = @bitCast(i32, N - @clz(Z, a)); // number of significant digits
     var e: i32 = sd - 1; // exponent
 
     if (sd > FLT_MANT_DIG) {
@@ -40,12 +43,12 @@ pub fn __floattisf(arg: i128) callconv(.C) f32 {
             FLT_MANT_DIG + 2 => {},
             else => {
                 const shift1_amt = @intCast(i32, sd - (FLT_MANT_DIG + 2));
-                const shift1_amt_u7 = @intCast(u7, shift1_amt);
+                const shift1_amt_u7 = @intCast(S, shift1_amt);
 
                 const shift2_amt = @intCast(i32, N + (FLT_MANT_DIG + 2)) - sd;
-                const shift2_amt_u7 = @intCast(u7, shift2_amt);
+                const shift2_amt_u7 = @intCast(S, shift2_amt);
 
-                a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, maxInt(u128)) >> shift2_amt_u7)) != 0);
+                a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(Z, maxInt(Z)) >> shift2_amt_u7)) != 0);
             },
         }
         // finish
@@ -53,17 +56,17 @@ pub fn __floattisf(arg: i128) callconv(.C) f32 {
         a += 1; // round - this step may add a significant bit
         a >>= 2; // dump Q and R
         // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits
-        if ((a & (@as(u128, 1) << FLT_MANT_DIG)) != 0) {
+        if ((a & (@as(Z, 1) << FLT_MANT_DIG)) != 0) {
             a >>= 1;
             e += 1;
         }
         // a is now rounded to FLT_MANT_DIG bits
     } else {
-        a <<= @intCast(u7, FLT_MANT_DIG - sd);
+        a <<= @intCast(S, FLT_MANT_DIG - sd);
         // a is now rounded to FLT_MANT_DIG bits
     }
 
-    const s = @bitCast(u128, arg) >> (128 - 32);
+    const s = @bitCast(Z, arg) >> (T.bit_count - 32);
     const r = (@intCast(u32, s) & 0x80000000) | // sign
         (@intCast(u32, (e + 127)) << 23) | // exponent
         (@truncate(u32, a) & 0x007fffff); // mantissa-high
@@ -71,6 +74,24 @@ pub fn __floattisf(arg: i128) callconv(.C) f32 {
     return @bitCast(f32, r);
 }
 
+pub fn __floatdisf(arg: i64) callconv(.C) f32 {
+    @setRuntimeSafety(builtin.is_test);
+    return @call(.{ .modifier = .always_inline }, __floatXisf, .{ i64, arg });
+}
+
+pub fn __floattisf(arg: i128) callconv(.C) f32 {
+    @setRuntimeSafety(builtin.is_test);
+    return @call(.{ .modifier = .always_inline }, __floatXisf, .{ i128, arg });
+}
+
+pub fn __aeabi_l2f(arg: i64) callconv(.AAPCS) f32 {
+    @setRuntimeSafety(false);
+    return @call(.{ .modifier = .always_inline }, __floatdisf, .{arg});
+}
+
 test "import floattisf" {
     _ = @import("floattisf_test.zig");
 }
+test "import floatdisf" {
+    _ = @import("floattisf_test.zig");
+}
lib/std/special/compiler_rt.zig
@@ -100,7 +100,8 @@ comptime {
     @export(@import("compiler_rt/floatditf.zig").__floatditf, .{ .name = "__floatditf", .linkage = linkage });
     @export(@import("compiler_rt/floattitf.zig").__floattitf, .{ .name = "__floattitf", .linkage = linkage });
     @export(@import("compiler_rt/floattidf.zig").__floattidf, .{ .name = "__floattidf", .linkage = linkage });
-    @export(@import("compiler_rt/floattisf.zig").__floattisf, .{ .name = "__floattisf", .linkage = linkage });
+    @export(@import("compiler_rt/floatXisf.zig").__floattisf, .{ .name = "__floattisf", .linkage = linkage });
+    @export(@import("compiler_rt/floatXisf.zig").__floatdisf, .{ .name = "__floatdisf", .linkage = linkage });
 
     @export(@import("compiler_rt/floatunditf.zig").__floatunditf, .{ .name = "__floatunditf", .linkage = linkage });
     @export(@import("compiler_rt/floatunsitf.zig").__floatunsitf, .{ .name = "__floatunsitf", .linkage = linkage });
@@ -204,6 +205,7 @@ comptime {
         @export(@import("compiler_rt/extendXfYf2.zig").__aeabi_f2d, .{ .name = "__aeabi_f2d", .linkage = linkage });
         @export(@import("compiler_rt/floatsiXf.zig").__aeabi_i2d, .{ .name = "__aeabi_i2d", .linkage = linkage });
         @export(@import("compiler_rt/floatdidf.zig").__aeabi_l2d, .{ .name = "__aeabi_l2d", .linkage = linkage });
+        @export(@import("compiler_rt/floatXisf.zig").__aeabi_l2f, .{ .name = "__aeabi_l2f", .linkage = linkage });
         @export(@import("compiler_rt/floatunsidf.zig").__aeabi_ui2d, .{ .name = "__aeabi_ui2d", .linkage = linkage });
         @export(@import("compiler_rt/floatundidf.zig").__aeabi_ul2d, .{ .name = "__aeabi_ul2d", .linkage = linkage });
         @export(@import("compiler_rt/floatunsisf.zig").__aeabi_ui2f, .{ .name = "__aeabi_ui2f", .linkage = linkage });