[Bug target/126973] New: Worse X86 codegen for arbitrary __builtin_clzg/__builtin_ctzg compared to clang.

kaelfandrew at gmail dot com via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126973

            Bug ID: 126973
           Summary: Worse X86 codegen for arbitrary
                    __builtin_clzg/__builtin_ctzg compared to clang.
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kaelfandrew at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/zTrYKvvxK

```
#define N 16

int
clzg_uN (unsigned _BitInt(N) x)
{
  return __builtin_clzg (x);
}

int
ctzg_uN (unsigned _BitInt(N) x)
{
  return __builtin_ctzg (x);
}
```

With -O2 and mostly N <= 64, Clang produce much better code:

N = 16
``` GCC
"clzg_uN":
        movzx   edi, di
        bsr     eax, edi
        xor     eax, 31
        sub     eax, 16
        ret
"ctzg_uN":
        movzx   eax, di
        rep bsf eax, eax
        ret
```

``` Clang
clzg_uN:
        bsr     ax, di
        xor     eax, 15
        movzx   eax, ax
        ret

ctzg_uN:
        rep       bsf eax, edi
        ret
```

N = 37
```GCC
"clzg_uN":
        movabs  rax, 137438953471
        and     rdi, rax
        bsr     rax, rdi
        xor     rax, 63
        sub     eax, 27
        ret
"ctzg_uN":
        movabs  rax, 137438953471
        and     rdi, rax
        xor     eax, eax
        rep bsf rax, rdi
        ret
```

```Clang
clzg_uN:
        shl     rdi, 27
        bsr     rax, rdi
        xor     eax, 63
        ret

ctzg_uN:
        rep       bsf rax, rdi
        ret
```
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.