Skip to content

Fix exact Integer bit window extraction - #283

Merged
284km merged 1 commit into
mainfrom
codex-misc-integer-bit-slices
Nov 25, 2026
Merged

284km merged 1 commit into
mainfrom
codex-misc-integer-bit-slices

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Integer two-argument bit reads returned only one bit for a negative-width Bignum window and for very wide positive windows. They now use exact shift/mask semantics, including promotion beyond a machine word. A window entirely below bit zero returns zero before allocating a shifted value; a mask wider than a nonnegative result needs no allocation.

This is a separate cause from the A1 numeric RED table in #243/#274. Before editing, the unchanged test_bignum.rb#test_aref2 failed on both main engines after 11 assertions; clean Ruby 4.0.10 passes all 32. The candidate passes all 32 on both engines. Corpus 1116 covers signed small/large receivers, finite and negative widths, left-shift promotion, huge positive Bignum windows, fully below-zero windows, and to_int callback order. It matches Ruby on evaluator and VM.

A1: exact Integer bit windows

Branch base: current origin/main d4e5ba5. The cause appears in the
previously recorded A1 numeric table (inventory PR #243, refined in
PR #274). Reference: clean Ruby 4.0.10; no reference gems are installed.

Before editing, the unchanged ruby/test_bignum.rb#test_aref2 was rerun
with --seed=1 and MERE_RUBY_GC_TIGHT=1, using the genuine -test-/integer
extension built separately for reference and Mere:

Execution Result Max RSS bytes
Reference 1 test, 32 assertions, 0 failures 22577152
Main evaluator 1 test, 11 assertions, 1 failure 181010432
Main VM 1 test, 11 assertions, 1 failure 181616640

The first failure is a negative-width Bignum window: x[5, -1] should be
x >> 5, but the fallback returns only bit 5. Very wide positive windows
also fall back to a single bit. Machine-Integer negative-width windows
use a word-sized shift which cannot promote a large left-shifted result.

No full unit suite or Rails component suite was run. The completed
related comparisons, fixed-seed tight-GC RSS, gates, record hygiene and
both complete corpus sweeps are recorded below.

The correction sends two-argument reads through the exact bit-window
helper for both Integer representations. A negative length is an arithmetic
shift without a mask. A window wholly below bit zero returns zero before
any large left shift. For a very wide positive mask, a nonnegative shifted
value whose bits already fit is returned without constructing the mask.
The obsolete word-sized two-argument helper is removed.

An exploratory, uncommitted small-Integer control found a separate existing
resource-error difference: 16[0, 2**62 - 1] raises NoMemoryError on Ruby,
which constructs the enormous mask, while main already returns 16. This
correction does not claim to emulate that allocation failure. Corpus 1116
uses wide masks with Bignum receivers, as the failing upstream test does;
finite small-Integer and negative-width promotion cases remain covered.

Final O2 focused validation uses the same compiler/mgz pins. Corpus 1116
matches clean Ruby on evaluator and VM. The unchanged Bignum test now
passes 1 test / 32 assertions on both engines, versus main's 11 assertions
and one failure. Candidate max RSS is 180469760 / 182272000 bytes.

The unchanged ruby/test_integer.rb#test_aref also passes all 414780
assertions on reference, main and candidate, on both engines. This covers
machine Integers, word-boundary values, single bits, two-argument windows,
finite/endless/beginless ranges, and negative begins. Runs use --seed=1,
MERE_RUBY_GC_TIGHT=1 and /usr/bin/time -l, one file at a time:

Engine Main max RSS bytes Candidate max RSS bytes
Evaluator 219840512 219545600
VM 225165312 237486080

Complete final O2 acceptance:

  • sh run_corpus.sh --both: 911/911 pass, hash indexing ON and OFF.
  • MERE_RUBY_VM=1 sh run_corpus.sh --both: 911/911 pass, indexing ON/OFF.
  • Both fresh commands pass all source gates and full record_hygiene.
  • The generated-C bracket-depth gate passes: 155 required / 1024 supplied.
  • This completed loop note passes explicit record_hygiene.

The final candidate binary SHA-256 is
17f5178da09b9e644cd112eac1c64798a5661089170e82f1b1ffae2312d953fe.
The baseline is the unchanged d4e5ba5 O2 build; both use Mere 9dec2ff6
and mgz 661a01d8. Corpus, implementation and the full completed record
are included in the first push. No workflow is changed.

@284km
284km marked this pull request as ready for review November 25, 2026 07:50

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed current head a2ac061 against the current main/merge-base diff, including the exact-shift helper, its range callers, coercion order and existing discussion. No new actionable findings. Routing both Integer representations through the exact helper and checking below-zero windows before shifting address the documented failure paths clearly.

Independent focused validation: corpus 1116, 800 deterministic additional signed bit-window cases, and five related Integer/Bignum corpus files match clean Ruby 4.0.10 byte-for-byte on both evaluator and VM with tight GC (14 comparisons). These checks reuse the recorded O2 binary, SHA-256 17f5178da09b9e644cd112eac1c64798a5661089170e82f1b1ffae2312d953fe: all 2,489 tracked regular files in its source checkout match the fetched head, and the three imported mgz sources match pin 661a01d8. This was not a fresh build or a current-main integration test. The initial reference invocation selected system Ruby; that setup mismatch was corrected by explicitly selecting Ruby 4.0.10 before the comparisons above. I did not rerun the full corpus or unchanged upstream Integer/Bignum suites.

Hosted acceptance remains incomplete: Linux core CI is still in progress and macOS/reference jobs are queued; successful auxiliary jobs do not establish completion of downstream core matrices. No merge performed: the recorded all-merges hold/new-failure review requirement remains active. This COMMENT records code-review results, not complete merge acceptance.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed a2ac061. No Critical / Major / Minor findings.

  • Every two-argument Integer#[beg, len] now goes through the exact int_slice_v: a negative width is n >> beg, a window entirely below bit 0 (or of width 0) is 0 without shifting, and a very wide mask over a nonnegative value that already fits is skipped instead of building 2**len. The old single-bit fallback for Bignum receivers and wide windows is gone, and the now-unused int_aref2_v is removed.
  • Ran on the author's build at this head (clean tree, binary newer than every .mere file), on the evaluator and MERE_RUBY_VM=1, against ruby 4.0.10: corpus 1116 is identical, and so is an extra probe: 0b1010[1, -1], -1[0, 70], width 0, (±2**64)[60, 8], negative begins (5[-3, 4], 5[-10, 3]), a 2**100 receiver with low/high/negative-begin windows and x[3, -1] == x >> 3, ranges (255[0..3], 255[2..], and the beginless-range ArgumentError), and single-bit reads.
  • The loop note has the local results our merge policy asks for: corpus --both and MERE_RUBY_VM=1 --both 911/911 with hash indexing ON and OFF, the source gates, record_hygiene, the bracket gate, test_bignum#test_aref2 (32/32 on both engines, main fails after 11) and test_integer#test_aref (414,780 assertions on both engines). The VM's single-run RSS for test_aref (225 → 237 MB) is within the run-to-run variation we see even with GC_TIGHT, so I do not count it.
  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments.

Merging.

@284km
284km merged commit 1a71c54 into main Nov 25, 2026
10 of 19 checks passed
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