-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Fix msvc compiler error C4146 (minus operator applied to unsigned type) #9916
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 msvc compiler error C4146 (minus operator applied to unsigned type) #9916
Conversation
src/bloom.cpp
Outdated
uint64_t nGenerationMask1 = -(uint64_t)(nGeneration & 1); | ||
uint64_t nGenerationMask2 = -(uint64_t)(nGeneration >> 1); | ||
uint64_t nGenerationMask1 = (uint64_t)(-(int64_t)(nGeneration & 1)); | ||
uint64_t nGenerationMask2 = (uint64_t)(-(int64_t)(nGeneration >> 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works around the error, but I don't think it's very elegant. What about just formulating it explicitly:
uint64_t nGenerationMask1 = ~(uint64_t)(nGeneration & 1) + 1;
uint64_t nGenerationMask2 = ~(uint64_t)(nGeneration >> 1) + 1;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even just:
uint64_t nGenerationMask1 = 0 - (uint64_t)(nGeneration & 1);
uint64_t nGenerationMask2 = 0 - (uint64_t)(nGeneration >> 1);
Not sure this is any more readable than the above though :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I confirmed three ways work well as same in my local msvc14.
The latter you show is simple but not feel as explicit bit calculation.
I like the former that is readable clearly as two's complement formula, then I'll adopt it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied. (commit --amend && push -f)
170887a
to
3f0a0cf
Compare
ugh. MSVC produces the most absurd errors for perfectly valid, strictly conforming code. I don't have a problem with silencing it in this way that this PR currently does it (though IMO it makes it less clear). The prior fix of casting to a signed type can actually result in undefined behavior (at least generally if not actually in our case due to the sizes we use). a=INT_MIN; a = -a; is undefined. (highlighting the foolishness of MSFT's war on bit manipulation. :P -- as someone trying to address this warning in an 'obvious way' would potentially introduce undefined behavior as a result!) |
Agreed, that this is necessary at all is silly. There's no ambiguity here. Unary minus -x equivalent to 0-x which is well-defined also for unsigned values which are defined to wrap around. |
Yes, we should take care not to meet undefined behavior. |
https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4146 Seems to indicate that 0-(uint64_t)(nGeneration & 1); will silence it-- if so I think that would be preferable. |
OK, I just understood. I'll adopt |
3f0a0cf
to
40aca33
Compare
utACK 40aca33 |
40aca33
to
ac3cb48
Compare
Amended the commit message to put a newline between the title and body, otherwise |
On msvc14, the compiler error C4146 (unary minus operator applied to unsigned type, result still unsigned) had been occured. Use '0 - x' styled formula instead of '-x' so as to fix the error.
ac3cb48
to
292112f
Compare
Oh, I see. I've inserted a new line as you say. Thanks for helpful tips. |
I also fixed another C4146 error in the commit below that is about unit test. Should I contain the commit in this PR #9916, or create an another PR for the commit? |
It's another minor thing, I'd say add it here. |
…ed type) On msvc14, int literal '-2147483648' is invalid, because '2147483648' is unsigned type and cant't apply minus operator to unsigned type. To define the int literal correctly, use '-2147483647 - 1' formula that is also used to define INT_MIN in limits.h.
Thanks, I added. |
re-UTack 8e0720b |
utACK 8e0720b |
… unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
…lied to unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
…lied to unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
…lied to unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
…lied to unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
…lied to unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
…lied to unsigned type) 8e0720b Fix msvc compiler error C4146 (unary minus operator applied to unsigned type) (kobake) 292112f Fix msvc compiler error C4146 (minus operator applied to unsigned type) (kobake) Tree-SHA512: 25f408daf7bf9ffe4b8b4bd62f6f6d326219189a9faf8f8c0a135c5a0cb0511af765aa2b6087a091c8863c701289bda49a2379b00cd9b10854d316a5c3fc3f8e
Backport bloom filter improvements Cherry-picked from the following upstream PRs: - bitcoin/bitcoin#7113 - bitcoin/bitcoin#7818 - Only the second commit (to resolve conflicts). - bitcoin/bitcoin#7934 - bitcoin/bitcoin#8655 - Partial backport to help resolve conflicts. - bitcoin/bitcoin#9060 - bitcoin/bitcoin#9223 - bitcoin/bitcoin#9644 - Partial backport to help resolve conflicts. - bitcoin/bitcoin#9916 - bitcoin/bitcoin#9750 - bitcoin/bitcoin#13176 - bitcoin/bitcoin#13948 - bitcoin/bitcoin#16073 - bitcoin/bitcoin#18670 - bitcoin/bitcoin#18806 - Reveals upstream's covert fix for CVE-2013-5700. - bitcoin/bitcoin#19968
Backport bloom filter improvements Cherry-picked from the following upstream PRs: - bitcoin/bitcoin#7113 - bitcoin/bitcoin#7818 - Only the second commit (to resolve conflicts). - bitcoin/bitcoin#7934 - bitcoin/bitcoin#8655 - Partial backport to help resolve conflicts. - bitcoin/bitcoin#9060 - bitcoin/bitcoin#9223 - bitcoin/bitcoin#9644 - Partial backport to help resolve conflicts. - bitcoin/bitcoin#9916 - bitcoin/bitcoin#9750 - bitcoin/bitcoin#13176 - bitcoin/bitcoin#13948 - bitcoin/bitcoin#16073 - bitcoin/bitcoin#18670 - bitcoin/bitcoin#18806 - Reveals upstream's covert fix for CVE-2013-5700. - bitcoin/bitcoin#19968
On msvc14, the error C4146 (unary minus operator applied to unsigned type, result still unsigned) had been occured.
Minus operator should be applied to signed type.