Skip to content

Failure to recognize table-based ctz implementation #45779

@GabrielRavier

Description

@GabrielRavier
Bugzilla Link 46434
Version trunk
OS Linux
CC @topperc,@shubhamnarlawar77

Extended Description

int f(unsigned x)
{
    static const char table[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
    return table[((unsigned)((x & -x) * 0x077CB531U)) >> 27];
}

This can be optimized to __builtin_ctz(x);. This transformation is done by GCC (or at least https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90838 claims to do so, though I can't get it to trigger), but not by LLVM.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions