Skip to content
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

Fix -Wsign-compare gcc warning #833

Closed
wants to merge 1 commit into from
Closed

Conversation

tjstum
Copy link
Contributor

@tjstum tjstum commented Dec 23, 2024

gcc warns about this code:
warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]

From what I can tell, this is a difference between clang and gcc, as explored on godbolt

gcc warns about this code:
`warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]`

From what I can tell, this is a difference between clang and gcc, as
explored [on godbolt](https://godbolt.org/z/T3TKsdvTr)
@tjstum
Copy link
Contributor Author

tjstum commented Dec 23, 2024

I know that the purification ritual comment mentions -Wsign-conversion, so I can understand if this is feeling too purification-y. But this does issue a warning with regular -Wall in modern gcc (even going back to gcc 8) as shown in that godbolt link.

Please also let me know if you have a better way you'd like this formatted. There are not a lot (any?) examples of multi-line for loops that I could find.

Comment on lines +448 to +449
for (size_t i = 1;
i < fc->nargs - static_cast<size_t>(has_var_kwargs); ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since int can represent the entire range of values of the original types on the right, integer promotion converts each value to int. Then subtraction results in an int. So, may I suggest:

    for (int i = 1; i < fc->nargs - has_var_kwargs; ++i) {

I feel this is more natural/readable (and it fits on one line). As a bonus, the generated code might be better. On Compiler Explorer, I think it's nicer to uncheck the "Intel asm syntax" box. Note that the int code is smaller using 32-bit integers than for 64-bit size_t, because addq takes one more byte than addl, etc.

64bit: 48 83 c3 01 add $0x1,%rbx ==> 32bit: 83 c3 01 add $0x1,%ebx
64bit: 48 39 eb cmp %rbp,%rbx ==> 32bit: 39 eb cmp %ebp,%ebx

@wjakob
Copy link
Owner

wjakob commented Jan 6, 2025

I committed a fix directly. I am not a fan of static_cast for simple integer type conversions.

@wjakob wjakob closed this Jan 6, 2025
@tjstum
Copy link
Contributor Author

tjstum commented Jan 6, 2025

Sounds good. Thanks for the fix!

@tjstum tjstum deleted the signedness branch January 6, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants