Skip to content

Port __clzsi2 from compiler_rt #4195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/std/special/compiler_rt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ comptime {
@export(@import("compiler_rt/negXf2.zig").__negsf2, .{ .name = "__negsf2", .linkage = linkage });
@export(@import("compiler_rt/negXf2.zig").__negdf2, .{ .name = "__negdf2", .linkage = linkage });

@export(@import("compiler_rt/clzsi2.zig").__clzsi2, .{ .name = "__clzsi2", .linkage = linkage });

if (is_arm_arch and !is_arm_64 and !is_test) {
@export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr0, .{ .name = "__aeabi_unwind_cpp_pr0", .linkage = linkage });
@export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr1, .{ .name = "__aeabi_unwind_cpp_pr1", .linkage = linkage });
Expand Down Expand Up @@ -177,7 +179,9 @@ comptime {
@export(@import("compiler_rt/arm.zig").__aeabi_memclr, .{ .name = "__aeabi_memclr4", .linkage = linkage });
@export(@import("compiler_rt/arm.zig").__aeabi_memclr, .{ .name = "__aeabi_memclr8", .linkage = linkage });

@export(@import("compiler_rt/arm.zig").__aeabi_read_tp, .{ .name = "__aeabi_read_tp", .linkage = linkage });
if (builtin.os == .linux) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing linux-specific here, what error message did you get? I suppose it's because the GBA cpu doesn't support the mrc opcode we're using here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only export that symbol under linux.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can't see anything about that symbol being exclusive to Linux in the specs.

@export(@import("compiler_rt/arm.zig").__aeabi_read_tp, .{ .name = "__aeabi_read_tp", .linkage = linkage });
}

@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 });
Expand Down
118 changes: 118 additions & 0 deletions lib/std/special/compiler_rt/clzsi2.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Ported from:
//
// https://github.com/llvm-mirror/compiler-rt/blob/f0745e8476f069296a7c71accedd061dce4cdf79/lib/builtins/clzsi2.c
// https://github.com/llvm-mirror/compiler-rt/blob/f0745e8476f069296a7c71accedd061dce4cdf79/lib/builtins/arm/clzsi2.S
const builtin = @import("builtin");

// Precondition: a != 0
fn __clzsi2_generic(a: i32) callconv(.C) i32 {
@setRuntimeSafety(builtin.is_test);

var x = @bitCast(u32, a);
var n: i32 = 32;

// Count first bit set using binary search, from Hacker's Delight
var y: u32 = 0;
inline for ([_]i32{ 16, 8, 4, 2, 1 }) |shift| {
y = x >> shift;
if (y != 0) {
n = n - shift;
x = y;
}
}

return n - @bitCast(i32, x);
}

fn __clzsi2_arm_clz(a: i32) callconv(.Naked) noreturn {
asm volatile (
\\ clz r0,r0
\\ bx lr
);
unreachable;
}

fn __clzsi2_arm32(a: i32) callconv(.Naked) noreturn {
asm volatile (
\\ // Assumption: n != 0
\\ // r0: n
\\ // r1: count of leading zeros in n + 1
\\ // r2: scratch register for shifted r0
\\ mov r1, #1
\\
\\ // Basic block:
\\ // if ((r0 >> SHIFT) == 0)
\\ // r1 += SHIFT;
\\ // else
\\ // r0 >>= SHIFT;
\\ // for descending powers of two as SHIFT.
\\ lsrs r2, r0, #16
\\ movne r0, r2
\\ addeq r1, #16
\\
\\ lsrs r2, r0, #8
\\ movne r0, r2
\\ addeq r1, #8
\\
\\ lsrs r2, r0, #4
\\ movne r0, r2
\\ addeq r1, #4
\\
\\ lsrs r2, r0, #2
\\ movne r0, r2
\\ addeq r1, #2
\\
\\ // The basic block invariants at this point are (r0 >> 2) == 0 and
\\ // r0 != 0. This means 1 <= r0 <= 3 and 0 <= (r0 >> 1) <= 1.
\\ //
\\ // r0 | (r0 >> 1) == 0 | (r0 >> 1) == 1 | -(r0 >> 1) | 1 - (r0 >> 1)f
\\ // ---+----------------+----------------+------------+--------------
\\ // 1 | 1 | 0 | 0 | 1
\\ // 2 | 0 | 1 | -1 | 0
\\ // 3 | 0 | 1 | -1 | 0
\\ //
\\ // The r1's initial value of 1 compensates for the 1 here.
\\ sub r0, r1, r0, lsr #1
\\ bx lr
);
unreachable;
}

const can_use_arm_clz = switch (builtin.arch) {
.arm, .armeb => |sub_arch| switch (sub_arch) {
.v4t => false,
.v6m => false,
else => true,
},
.thumb, .thumbeb => |sub_arch| switch (sub_arch) {
.v6,
.v6k,
.v5,
.v5te,
.v4t,
=> false,
else => true,
},
else => false,
};

const is_arm32_no_thumb = switch (builtin.arch) {
builtin.Arch.arm,
builtin.Arch.armeb,
=> true,
else => false,
};

pub const __clzsi2 = blk: {
if (comptime can_use_arm_clz) {
break :blk __clzsi2_arm_clz;
} else if (comptime is_arm32_no_thumb) {
break :blk __clzsi2_arm32;
} else {
break :blk __clzsi2_generic;
}
};

test "test clzsi2" {
_ = @import("clzsi2_test.zig");
}
Loading