-
Notifications
You must be signed in to change notification settings - Fork 120
Fix all instances of MSVC warning C4146 (unsigned negation) #307
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
Conversation
Unsigned negation is a well-defined operation in C and C++ and it is commonly used when doing bit manipulation of unsigned integers, because it builds a high-pass bitmask. For instance, if `X` is known to be a power of two, `-X` is precisely the mask needed to align a value down to a multiple of `X` by means of a bit-wise `&` operation. However, it can occasionally be flagging a bug, when the original intent was mathematical negation. For this reason, an explicit fix is preferred to suppressing this warning. This change fixes all the instances of unsigned negation of the form `-X` to `0 - X`. It also includes a drive-by spot-fix for the macro `left_bits(x)` whose parameter was missing the usual parenthesis. This was benign in all the uses of the macro.
|
@DougLea, FYI. |
|
Since the dlmalloc implementation is not maintaned inside boost, we've avoided altering the original file, so that it can be more easily updated if new versions are released. |
|
@igaztanaga I fully understand the concern. On the other hand, there have been no changes to the original in the past 12 years, and several changes of this sort (keeping up with issues raised by evolving toolsets and even fixing typos) in this copy of it. I have notified the original author above (and earlier in a short private email exchange). I'll be happy to adapt this change as needed based on his feedback, if any, or incorporate a new version if he wishes to publish it. |
|
Hi all, my team would really benefit from this change, and I know other folks on the same boat. Adding a vote of this fix going in. Thank you! |
|
I still don't understand why is this a problem. The warning is disabled in the file, it should not appear and should not affect any other code. |
|
@igaztanaga "why is this a problem" -- There are short ways to answer and long ways to answer, and I'm not the most qualified to do either, but I'll try the short-ish.
I'd add that it's good code hygiene. Disabling a warning hides potential issues, both current and future, indiscriminately. A better way, arguably, if you wish to hide a particular instance of a warning, is with a suppression ( But suppressions are verbose, and a better way (IMHO) is just to change the code to be more explicit about its intent so the warning is no longer issued. In this case, the code is using negation on unsigned quantities (mostly In case there's concern about code generation, all three main compilers generate the same code either way: https://godbolt.org/z/f994nKW3e Ok, that wasn't really that short-ish :-P. I hope you'll consider both the merit of this change and the needs of (some of) your users (multiple users within Microsoft that got this issue flagged). Thank you. |
|
As original author... This change has no impact on compiled code; it just works around a limitation in MS tool chain. So I don't object to seeing it done in the boost version of dlmalloc. But I'm not very tempted to update the original. |
Unsigned negation is a well-defined operation in C and C++ and it is commonly used when doing bit manipulation of unsigned integers, because it builds a high-pass bitmask.
For instance, if
Xis known to be a power of two,-Xis precisely the mask needed to align a value down to a multiple ofXby means of a bit-wise&operation.However, it can occasionally be flagging a bug, when the original intent was mathematical negation. For this reason, an explicit fix is preferred to suppressing this warning.
This change fixes all the instances of #303 by changing unsigned negation of the form
-Xto the more explicit0 - X, and removes the suppression.Ran all the tests under "test" and "bench". Fixed two more instances of the warning in one of the unit tests.
I also included a drive-by spot-fix for the macro
left_bits(x)whose parameter was missing the usual parenthesis. This was benign in all the uses of the macro.