Skip to content

[x86-64 BMI2] Generating a bitstring with a min(64, LEN) 1 bits at the bottom should use BZHI #132714

Open
@Validark

Description

@Validark

This code: Zig Godbolt C Godbolt

export fn foo(len: u64) u64 {
    return ~if (len >= 64)
        @as(u64, 0)
    else
        ~@as(u64, 0) << @intCast(len);
}

Gives me this emit:

        mov     rcx, -1
        cmp     rdi, 64
        shlx    rax, rcx, rdi
        not     rax
        cmovae  rax, rcx

Should be:

        mov     rcx, -1
        bzhi    rax, rcx, rdi
        cmp     rdi, 64
        cmovae  rax, rcx

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions