https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118637
Bug ID: 118637 Summary: gcc fails to optimize unsigned division by 2 to shift right by 1 Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: davidfromonline at gmail dot com Target Milestone: --- The following code: ```c++ unsigned log2_divide(unsigned x) { unsigned result = 0; while (x /= 2) { ++result; } return result; } unsigned log2_shift(unsigned x) { unsigned result = 0; while (x >>= 1) { ++result; } return result; } ``` when compiled with `-O3` generates ```asm log2_divide(unsigned int): cmp edi, 1 jbe .L4 shr edi xor eax, eax .L3: mov edx, edi add eax, 1 shr edi cmp edx, 1 jne .L3 ret .L4: xor eax, eax ret log2_shift(unsigned int): mov eax, edi shr eax je .L7 bsr eax, eax add eax, 1 .L7: ret ``` I would expect `log2_divide` to generate the same code as `log2_shift`. See it live: https://godbolt.org/z/fahr3Moe3