Commit 8d087c0f

8d087c0f84ebMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +1 added · ~5 modified

Description

feat: --int-overflow=raise|wrap|promote flag with three working modes

Replaces the temporary --no-overflow-check flag from a358656 with a three-way mode selector that also wires up the BIGINT.md option-α promote mode. Default stays raise. The wrapper exports SPINEL_INT_OVERFLOW so analyze.rb can rewrite int locals to bigint when mode is promote; raise / wrap stay cc-time preprocessor switches inside sp_runtime.h.

raise default. Overflow on + - * raises RangeError. ~5-10% slowdown on int-heavy hot loops.

wrap Bare + - * with two's-complement silent wrap. Zero overhead; matches spinel's pre-overflow-detection behaviour. make optcarrot now builds with this mode (the NES emulator's per-frame math doesn't actually overflow, but the spec-allowed UB in the inner loops plays better with -O3's loop vectoriser without the extra branch).

promote Every int local is force-rewritten to bigint at analyze time. The existing bigint codegen path (sp_bigint_add / _sub / _mul, sp_bigint_to_s for puts, etc.) takes over so arithmetic past mrb_int width keeps producing the full Ruby answer rather than raising or wrapping. The coverage today is the locals-only path -- method param / return / ivar promotion is a follow-up. Verified manually for a linear-sum chain that the pattern detector misses: n = 0; 10.times { n += 1e18 }; puts n prints 10000000000000000000 instead of the wrapped -8446744073709551616.

Bug fix surfaced alongside: sp_bigint_to_s's "small number fast path" downcast every two-limb mpz to int64 unconditionally, wrapping any value in [INT64_MAX+1, UINT64_MAX] -- exactly the window the loop-multiplication pattern detector hits at 2**63 and the new promote mode hits whenever a sum crosses the int64 boundary. Guard the fast path on the high limb's top bit being clear; otherwise fall through to mpz_get_str.

Bootstrap green; 617 tests pass; optcarrot checksum 59662 at ~680 fps (back at the pre-overflow-detection baseline because wrap mode is now the default for make optcarrot).

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
8d087c0f84eb
Parent
aa11881b2533

MakefilemodifiedView file

@@ -536,7 +536,7 @@
536536 git clone --depth=1 --branch=$(OPTCARROT_BRANCH) $(OPTCARROT_REPO) $(OPTCARROT_DIR); \
537537 fi
538538 @ruby $(OPTCARROT_DIR)/tools/pack-for-spinel.rb > build/optcarrot-single.rb
539- @./spinel build/optcarrot-single.rb -o build/optcarrot-single
539+ @./spinel --int-overflow=wrap build/optcarrot-single.rb -o build/optcarrot-single
540540 @out=$$($(TIMEOUT60) ./build/optcarrot-single 2>&1); \
541541 echo "$$out"; \
542542 if echo "$$out" | grep -qE "^fps: [0-9.]+$$" && echo "$$out" | grep -q "^checksum: 59662$$"; then \