Skip to content

bugc: wrap narrow arithmetic to its type's width - #340

Merged
gnidan merged 1 commit into
mainfrom
bugc-narrow-wrap
Oct 6, 2026
Merged

gnidan merged 1 commit into
mainfrom
bugc-narrow-wrap

Conversation

@gnidan

@gnidan gnidan commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

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.

result before
(255 as uint8) + (1 as uint8) 0 256
(0 as uint8) - (1 as uint8) 255 2^256 - 1
(16 as uint8) * (16 as uint8) 0 256
(127 as int8) + (1 as int8) -128 128
(-128 as int8) / (-1 as int8) -128 128
-(-128 as int8) -128 128

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 through castSteps: an unsigned result gets an AND with the type's mask, and a signed one a SIGNEXTEND. 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 AND or PUSH1 0 SIGNEXTEND), up to 18 for a uint128 mask. 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.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-06 14:18 UTC

Base automatically changed from bugc-signed-ops to main October 6, 2026 06:54
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
gnidan merged commit a87fc4e into main Oct 6, 2026
11 of 12 checks passed
@gnidan
gnidan deleted the bugc-narrow-wrap branch October 6, 2026 14:14
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.

1 participant