https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99887

            Bug ID: 99887
           Summary: Failure to optimize log2 pattern to clz
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

static inline unsigned int_log2_rec(unsigned x)
{
  return x == 0 ? 0 : int_log2_rec(x >> 1) + 1;
}

unsigned int_log2(unsigned x)
{
  return x == 0 ? 0 : int_log2_rec(x) - 1;
}

This can be optimized to `return x == 0 ? x : 31 - __builtin_clz(x);`. This
transformation is done by LLVM, but not by GCC.

Reply via email to