[clang] [clang] Implement __builtin_popcountg (PR #82359)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 26 15:29:48 PST 2024


overmighty wrote:

Maybe the semantic analysis and docs should have been changed to match GCC's better.

GCC's docs say their `__builtin_popcountg` takes a "type-generic unsigned integer" but that "No integral argument promotions are performed on the argument." Our builtin accepts signed integers too, but the argument goes through integer promotion, so the builtin can return an incorrect value if we give it a signed integer.

For example, this program will output 32 with Clang on `x86_64-unknown-linux-gnu`:

```c
#include <stdio.h>

int main(void) {
    char foo = -1;
    int pop = __builtin_popcountg(foo);
    printf("%d\n", pop);
}
```

GCC refuses to compile it: https://godbolt.org/z/chh4WGT4v.

Should I open a new issue?

https://github.com/llvm/llvm-project/pull/82359


More information about the cfe-commits mailing list