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

            Bug ID: 97503
           Summary: Suboptimal use of cntlzw and cntlzd
           Product: gcc
           Version: 10.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: christophe.leroy at csgroup dot eu
  Target Milestone: ---

int f(int x)
{
        return x ? __builtin_clz(x) : 32;
}

Is built as

00000000 <f>:
   0:   2c 03 00 00     cmpwi   r3,0
   4:   40 82 00 0c     bne     10 <f+0x10>
   8:   38 60 00 20     li      r3,32
   c:   4e 80 00 20     blr
  10:   7c 63 00 34     cntlzw  r3,r3
  14:   4e 80 00 20     blr


I would expect

00000000 <f>:
   0:   7c 63 00 34     cntlzw  r3,r3
   4:   4e 80 00 20     blr

Because cntlzw (Count Leading Zeros Word) is documentated in powerpc
instruction set as returning 0 to 32 inclusive

The same applies to the 64 bits version:

long f(long x)
{
        return x ? __builtin_clzll(x) : 64;
}

0000000000000000 <.f>:
   0:   2c 23 00 00     cmpdi   r3,0
   4:   41 82 00 0c     beq     10 <.f+0x10>
   8:   7c 63 00 74     cntlzd  r3,r3
   c:   4e 80 00 20     blr
  10:   38 60 00 40     li      r3,64
  14:   4e 80 00 20     blr

Reply via email to