Repository navigation
bugc: wrap narrow arithmetic to its type's width - #340
Merged
Merged
Conversation
gnidan
force-pushed
the
bugc-signed-ops
branch
from
October 6, 2026 06:38
a9108c2 to
35323e0
Compare
Contributor
|
gnidan
force-pushed
the
bugc-narrow-wrap
branch
from
October 6, 2026 06:45
ec122d7 to
afdb9c4
Compare
Arithmetic on an integer type narrower than 256 bits computed in a full
word and kept the full result, so `(255 as uint8) + (1 as uint8)` was
256 and `(127 as int8) + (1 as int8)` was 128, out of the type's range.
BUG has no overflow checks, so wrap the result to the type's width, as a
Solidity `unchecked { }` block does: after `+`, `-`, `*`, unary `-`, and
signed `/`, cast the result from a word to its type. An unsigned result
keeps its low bits and a signed result sign-extends, as a cast does.
Unsigned `/` and `%`, and signed `%`, cannot leave the type's range, so
they need no cast.
The optimizer folds the cast as it folds any other. Folded arithmetic
now wraps at the word, as the EVM does, where a constant result past
2^256 or below zero failed to compile.
gnidan
force-pushed
the
bugc-narrow-wrap
branch
from
October 6, 2026 06:56
afdb9c4 to
0a93acd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Arithmetic on an integer type narrower than 256 bits now wraps to the type's width, as in a Solidity
unchecked { }block. BUG has no overflow checks anywhere, so this is the only consistent rule, and it matches what casts already do since #334. It also keeps every value within its type, which the exact-byte debug pointers from #328 rely on.(255 as uint8) + (1 as uint8)(0 as uint8) - (1 as uint8)(16 as uint8) * (16 as uint8)(127 as int8) + (1 as int8)(-128 as int8) / (-1 as int8)-(-128 as int8)After
+,-,*, unary-, and signed/on a narrow type, irgen emits a cast of the result from a word to its type, so codegen and constant folding both go throughcastSteps: an unsigned result gets anANDwith the type's mask, and a signed one aSIGNEXTEND. Unsigned/and%and signed%can't leave the type's range, so they get no cast. 256-bit arithmetic is unchanged.Constant folding needed one fix of its own: folded
+,-, and*didn't wrap at the word, so a constant 256-bit result past 2^256 or below zero failed to compile (PUSH is not a function). Folded results now wrap at the word, and the cast after a narrow one folds as any cast does.Code size: no change on any of bugc's examples at O0 through O3, since none of them do arithmetic on narrow types. Each wrap costs 3 bytes for 8-bit types (
PUSH1 0xff ANDorPUSH1 0 SIGNEXTEND), up to 18 for auint128mask. I didn't drop wraps that a narrow storage write or a narrowing cast would make redundant; that can come later if it matters.The new tests run
+ - * / %and negation at the boundaries of each of uint8 through uint256 and int8 through int256, with operands from storage, calldata, and literals (folded), at every optimization level, plus wrapped results fed into comparisons, division, casts, storage, and mixed-width arithmetic.