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

            Bug ID: 106231
           Summary: sign-extension of the result of `__builtin_tzcnt()`
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

Given (godbolt: https://gcc.godbolt.org/z/hqKKW33T7):


```
long long
foo(long long x, unsigned bits)
  {
    return x + (unsigned) __builtin_ctz(bits);
  }
```

GCC output:
```
foo:
        bsf     eax, esi
        cdqe
        add     rax, rdi
        ret
```

Clang output:
```
foo:                                    # @foo
        bsf     eax, esi
        add     rax, rdi
        ret
```


The CDQE instruction is totally unnecessary, because BSF stores either a
non-negative value or an undefined one into its destination operand.

It's sad that `__builtin_ctz()` yields a signed type; but even we cast it, we
don't get anything better.

Reply via email to