Skip to content

#914: i128/u128 end to end — parse, primitive traits, acceptance - #2399

Merged
ehartford merged 2 commits into
mainfrom
i128-parse-display
Oct 14, 2026
Merged

ehartford merged 2 commits into
mainfrom
i128-parse-display

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

What

The implementation remainder of #914 (plan: docs/proposals/i128.md). Stages 1–2 already landed — #1922 full-width formatting and D125 exact constants — and I verified on main before this change that boundary literals, negated wide literals (INT128_MIN), the sign/hex specs and checked overflow all behave. This PR lands stages 3–4:

  • Parsing symmetry: parse_i128/parse_u128 in lib/std/string.w, plain With over checked u128 magnitude arithmetic — string_to_int's exact rule (0 on invalid, digits until the first non-digit). INT128_MIN is spelled without negating 2^127 in width; a magnitude past the signed range panics by name, past u128 by checked arithmetic.
  • Primitive traits: i128/u128 were absent from the per-width lists in lib/std/traits.w/builtins.w, so print(x) for x: i128 failed its Display bound while f"{x}" worked. Clone, Debug, Display, ToString, Eq, Ord, Default now cover both widths.
  • Acceptance: boundary literals with >> 100 layer discriminators, div/mod sign matrix, format/parse round-trips, add-overflow and parse-overflow panic fixtures, above-max/below-min literal error fixtures, and a __int128 c_import round-trip. --dump-abi on fn f(x: i128, y: u128) -> i128 shows plain COPY params — no ABI special-casing, per the plan's non-goals.

Failing-first evidence: with the installed compiler, parse_i128 is an undefined variable and print(x: i128) fails the Display bound; all eight fixtures pass on this branch's stage1.

For Eric's blessing (stage 6, not in this PR)

docs/spec/004_types.md §4.1 lines 5–6 list signed integers to i64 and unsigned to u64; i128/u128 appear only in the reserved-names clause (line 27). Proposed exact wording:

Signed integers: `i8`, `i16`, `i32`, `i64`, `i128`
Unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128`

Say the word and it lands as its own commit; #914 stays open for that sentence after this merges.

Gate and battery results posted when run.

Stages 1-2 of docs/proposals/i128.md already landed (#1922 full-width formatting, D125 exact constants): boundary literals, negated wide literals and checked overflow all verified on main before this change. This lands the remainder. Stage 3: parse_i128/parse_u128 in std.string, written in plain With over checked u128 magnitude arithmetic — string_to_int's exact rule (0 on invalid, digits until non-digit), INT128_MIN spelled without negating 2^127 in width, out-of-range magnitudes panic. First-class traits: i128/u128 were missing from the primitive lists every other width is on, so print(x) for x: i128 failed its Display bound while f"{x}" worked; Clone, Debug, Display, ToString, Eq, Ord and Default now cover both widths. Stage 4 acceptance: boundary literals with >>100 layer discriminators, div/mod sign matrix, format/parse round-trips, overflow and parse-range panic fixtures, literal above-max/below-min error fixtures, and a __int128 c_import round-trip. --dump-abi on fn f(x: i128, y: u128) shows plain COPY params — no ABI special-casing. Stage 6 (the spec SS4.1 primitive-list sentence) is Eric's to bless and is proposed in the PR, not landed here.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery run 1 at 3b9f841: RED — and it earned its keep. behavior-tests failed in behav_project_overflow_modes: compiling std's new parse_u128_magnitude under a project's overflow = "saturate" emitted invalid IR —

Intrinsic name not mangled correctly for type arguments! Should be: llvm.uadd.sat.i128
ptr @llvm.uadd.sat.i64
===== INVALID LLVM FUNCTION parse_u128_magnitude =====

Root cause (CodegenDispatch mir_build_saturating_bin_op): the intrinsic name enumerated widths 8/16/32 and defaulted to 64, and the saturating multiply widened i128 to i128 with 64-bit clamp bounds. Any saturate-mode i128/u128 arithmetic was broken; std simply never contained any before. Fixed in f2dbb4a by making the scalar path width-generic exactly as the vector path already was (name carries the width, multiply widens via wl_int_type_n to i256, clamps via cg_int_*). unit-return-review's red was my mid-battery edit being unstaged; test/test-with-audits were dependency skips. Targeted re-verification on stage1: behav_project_overflow_modes, the new behav_914_i128_saturating_ops, and all #914 fixtures pass. Rerunning the battery on f2dbb4a.

mir_build_saturating_bin_op mangled the intrinsic as llvm.uadd.sat.i64 for every width past 32 and widened an i128 multiply to i128 with 64-bit clamp bounds, so any saturate-mode program touching i128/u128 arithmetic emitted invalid IR (the battery's behav_project_overflow_modes, compiling std's new parse_u128 under overflow=saturate, is what caught it). The intrinsic name now carries the operand width, the multiply widens through wl_int_type_n (i256 for i128), and the clamp bounds come from the width-generic cg_int_* helpers the vector path already uses — one derivation per fact. Fixture: behav_914_i128_saturating_ops exercises the explicit +|/-|/*| operators at both 128-bit boundaries, which share the mode's lowering.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery run 2 at e9eac05: GREEN (seed-driven).

  • gate rc=0 wall=610s
  • uat rc=0 wall=17s
  • battery-checks rc=0 wall=1459s
  • verdict.w: rc=0 green; status.txt: GREEN / BATTERY_DONE

Marking ready and merging. #914 itself stays open only for the SS4.1 sentence proposed above.

@ehartford
ehartford marked this pull request as ready for review October 14, 2026 01:12
@ehartford
ehartford merged commit 3d03845 into main Oct 14, 2026
@ehartford
ehartford deleted the i128-parse-display branch October 14, 2026 01:12
ehartford added a commit that referenced this pull request Oct 14, 2026
…d 2026-10-13) (#2401)

The compiler implements i128/u128 end to end (#914: exact literals, formatting, parsing, traits, checked arithmetic, c_import __int128), but §4.1 claimed only to i64/u64; the types appeared solely in the reserved-names clause. The list now claims what ships. Wording proposed in #2399 and blessed by Eric verbatim.

Co-authored-by: Eric Hartford <[email protected]>
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