Skip to content

[NFC] Address compiler warnings: C4146 - Use two's complement instead of negation #7562

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

alsepkow
Copy link
Contributor

Replaces uses of the unary - operator on signed integers with the equivalent (sort of, see the details below) expression '~N + 1', assigning the result to an unsigned type. This avoids undefined behavior in edge cases and ensures correctness when certain conditions are met.

Details:
This transformation is valid when:

The signed value N is guaranteed to be negative.
The result is stored in an unsigned type that can represent the full range of the signed type (e.g., uint64_t for int64_t).
The system uses two's complement representation (as is standard on modern platforms).
While -N is undefined for the minimum representable value (e.g., INT64_MIN), the expression ~N + 1 remains well-defined and yields the correct bit pattern. Assigning this result to an appropriately sized unsigned type preserves the intended two's complement interpretation without triggering undefined behavior.

Addresses #7561.

@alsepkow alsepkow force-pushed the user/alsepkow/EO_4146_AbsoluteValue_1 branch from 24266e7 to 445a944 Compare June 20, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

1 participant