Skip to content

stdc_count_zeros(-1) gives -32 #111344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fragrans opened this issue Oct 7, 2024 · 5 comments
Closed

stdc_count_zeros(-1) gives -32 #111344

fragrans opened this issue Oct 7, 2024 · 5 comments
Labels
c23 incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) miscompilation waiting-for-response

Comments

@fragrans
Copy link

fragrans commented Oct 7, 2024

Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@fragrans
Copy link
Author

fragrans commented Oct 7, 2024

Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@dtcxzyw
Copy link
Member

dtcxzyw commented Oct 7, 2024

@fragrans Can you provide godbolt links?

@fragrans
Copy link
Author

fragrans commented Oct 7, 2024

Yes, but, it can't compile, because the its lack the stdbit.h for c23.

https://godbolt.org/z/GbW8W5Wo6

@erichkeane
Copy link
Collaborator

Where are you seeing the definition of stdc_count_zeros? The only implementation of this I see at the moment is llvm-libc, but this function is only defined (by standard) for unsigned types. So perhaps this is UB?

We start with a _Generic, which is:

#define stdc_count_ones(x)                                                     \
   _Generic((x),                                                                \
       unsigned char: stdc_count_ones_uc,                                       \
       unsigned short: stdc_count_ones_us,                                      \
       unsigned: stdc_count_ones_ui,                                            \
       unsigned long: stdc_count_ones_ul,                                       \
       unsigned long long: stdc_count_ones_ull)(x)

I see that we eventually just call:
return cpp::popcount<T>(static_cast<T>(~value));

as the implementation of this.

So I suspect there is some sort of conversion here happening along the way (what does _Generic do if no type is specified? ) that is resulting in -1 being 0?

@thesamesam thesamesam added the incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) label Nov 14, 2024
@AaronBallman
Copy link
Collaborator

Closing the issue as invalid due to the lack of a response or reproducer; if the issue persists, please reopen with more details.

@AaronBallman AaronBallman closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c23 incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) miscompilation waiting-for-response
Projects
None yet
Development

No branches or pull requests

5 participants