Commit 18d6523888

Andrew Kelley <andrew@ziglang.org>
2022-04-29 02:11:14
compiler-rt: upgrade to stage2 fn ptr semantics
1 parent c5e8477
Changed files (2)
lib
std
special
lib/std/special/compiler_rt/clzsi2_test.zig
@@ -1,13 +1,21 @@
+const builtin = @import("builtin");
 const clz = @import("count0bits.zig");
 const testing = @import("std").testing;
 
 fn test__clzsi2(a: u32, expected: i32) !void {
-    // XXX At high optimization levels this test may be horribly miscompiled if
-    // one of the naked implementations is selected.
-    var nakedClzsi2 = clz.__clzsi2;
-    var actualClzsi2 = @ptrCast(fn (a: i32) callconv(.C) i32, nakedClzsi2);
-    var x = @bitCast(i32, a);
-    var result = actualClzsi2(x);
+    const nakedClzsi2 = clz.__clzsi2;
+    const fnProto = fn (a: i32) callconv(.C) i32;
+    const fnProtoPtr = switch (builtin.zig_backend) {
+        .stage1 => fnProto,
+        else => *const fnProto,
+    };
+    const fn_ptr = switch (builtin.zig_backend) {
+        .stage1 => nakedClzsi2,
+        else => &nakedClzsi2,
+    };
+    const actualClzsi2 = @ptrCast(fnProtoPtr, fn_ptr);
+    const x = @bitCast(i32, a);
+    const result = actualClzsi2(x);
     try testing.expectEqual(expected, result);
 }
 
lib/std/special/compiler_rt/truncXfYf2.zig
@@ -4,6 +4,8 @@ const native_arch = builtin.cpu.arch;
 
 // AArch64 is the only ABI (at the moment) to support f16 arguments without the
 // need for extending them to wider fp types.
+// TODO remove this; do this type selection in the language rather than
+// here in compiler-rt.
 pub const F16T = if (native_arch.isAARCH64()) f16 else u16;
 
 pub fn __truncsfhf2(a: f32) callconv(.C) F16T {
@@ -140,7 +142,8 @@ inline fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t
         }
     }
 
-    const result: dst_rep_t align(@alignOf(dst_t)) = absResult | @truncate(dst_rep_t, sign >> @intCast(SrcShift, srcBits - dstBits));
+    const result: dst_rep_t align(@alignOf(dst_t)) = absResult |
+        @truncate(dst_rep_t, sign >> @intCast(SrcShift, srcBits - dstBits));
     return @bitCast(dst_t, result);
 }