Skip to content

clang can optimize _tzcnt_u32 a bit more #64477

Closed
@lygstate

Description

@lygstate

The godbolt compile demo for _tzcnt_u32 under MSVC/ICL/GCC/Clang not optimize

The godbolt compile demo for _tzcnt_u32 under MSVC/ICL/GCC/Clang optimized

_tzcnt_u32 can be optimized to:

#include <stdio.h>

#ifdef __x86_64__

__attribute__((naked))
unsigned _tzcnt_u32(unsigned x)
{
__asm(
    "mov $0x20, %eax;\n"
    "bsf %edi, %eax;\n"
    "ret;\n"
);
}

#else
__attribute__((naked))
unsigned _tzcnt_u32(unsigned x)
{
__asm(
    "mov $0x20, %eax;\n"
    "bsf 4(%esp), %eax;\n"
    "ret;\n"
);
}
#endif

int main(int argc, char **argv)
{
    printf("hello %d\n", _tzcnt_u32(0));
    return 0;
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions