Commit dfa16a99

dfa16a99feaeMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified

Description

feat(runtime,codegen): bit ops on bigint operands

Phase 1 of --int-overflow=promote coverage (docs/promote-mode-plan.md).

In promote mode all int slots widen to sp_Bigint *, so any bitwise op (&, |, ^, <<, >>, ~) on those operands would otherwise emit sp_Bigint * & long long, which C rejects.

  • lib/sp_runtime.h: declare sp_bigint_and / _or / _xor / _shl / _shr / _not (forward decls; implementations in lib/sp_bigint.c).
  • lib/sp_bigint.c: implementations via int64 round-trip (sp_bigint_to_int -> native op -> sp_bigint_new_int). Promote- mode values are derived from small ints that fit in int64, so semantics are preserved for the common case. Values exceeding int64 lose precision through these helpers -- acceptable starting point, can swap in real mpz_and / mpz_ior / mpz_xor later if a workload needs it.
  • spinel_codegen.rb:
    • new expr_emit_is_bigint(nid) recursive walker: returns 1 iff the C expression would emit sp_Bigint *, looking through ParenthesesNode and recursing into arith/bitop CallNodes whose operands carry bigint type. Handles the multi-level chain shape ((a | b) & c) | d where only the deepest operand directly types as bigint.
    • compile_operator_expr widens lt to "bigint" when the recv's C emit is bigint (even when infer_type gives the stale int cache from annotate_all_node_types).
    • bigint block dispatches & / | / ^ to the new helpers, << / >> through sp_bigint_shl / sp_bigint_shr with the shift amount unboxed to int64.
    • same expr_emit_is_bigint check decides the rc boxing (avoid double-wrapping an already-bigint sub-expression with sp_bigint_new_int).

Optcarrot --int-overflow=promote C errors: 72 -> 20 (-72%). Default raise mode: 631/0/0. Optcarrot --int-overflow=wrap checksum 59662 unchanged.

Refs the promote-mode plan (commit 9f6aef7).

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

Details

Commit
dfa16a99feae
Parent
1ae7feaf16ce

lib/sp_bigint.cmodifiedView file

@@ -5374,6 +5374,49 @@
53745374 return z->sn < 0 ? -v : v;
53755375 }
53765376
5377+/* Bitwise ops on bigint (Phase 1 of --int-overflow=promote
5378+ coverage). Implemented via int64 round-trip: every bigint
5379+ produced by promotion in spinel is derived from a small int
5380+ that fits in int64, so the round-trip preserves Ruby semantics
5381+ for any value in [INT64_MIN, INT64_MAX]. Values exceeding that
5382+ range lose precision; that's acceptable as a starting point
5383+ (extremely rare in practice -- bitops on integers > 2^63
5384+ aren't a real workload yet). A future commit can swap in the
5385+ custom mpz_and / mpz_ior / mpz_xor / mpz_com implementations
5386+ if the int64 path proves insufficient. */
5387+sp_Bigint *sp_bigint_and(sp_Bigint *a, sp_Bigint *b) {
5388+ return sp_bigint_new_int(sp_bigint_to_int(a) & sp_bigint_to_int(b));
5389+}
5390+
5391+sp_Bigint *sp_bigint_or(sp_Bigint *a, sp_Bigint *b) {
5392+ return sp_bigint_new_int(sp_bigint_to_int(a) | sp_bigint_to_int(b));
5393+}
5394+
5395+sp_Bigint *sp_bigint_xor(sp_Bigint *a, sp_Bigint *b) {
5396+ return sp_bigint_new_int(sp_bigint_to_int(a) ^ sp_bigint_to_int(b));
5397+}
5398+
5399+sp_Bigint *sp_bigint_shr(sp_Bigint *a, int64_t n); /* forward decl for mutual recursion */
5400+
5401+sp_Bigint *sp_bigint_shl(sp_Bigint *a, int64_t n) {
5402+ if (n < 0) return sp_bigint_shr(a, -n);
5403+ if (n >= 64) return sp_bigint_new_int(0);
5404+ return sp_bigint_new_int(sp_bigint_to_int(a) << n);
5405+}
5406+
5407+sp_Bigint *sp_bigint_shr(sp_Bigint *a, int64_t n) {
5408+ if (n < 0) return sp_bigint_shl(a, -n);
5409+ if (n >= 64) {
5410+ /* arithmetic right shift: negative -> -1, positive -> 0 */
5411+ return sp_bigint_new_int(sp_bigint_to_int(a) < 0 ? -1 : 0);
5412+ }
5413+ return sp_bigint_new_int(sp_bigint_to_int(a) >> n);
5414+}
5415+
5416+sp_Bigint *sp_bigint_not(sp_Bigint *a) {
5417+ return sp_bigint_new_int(~sp_bigint_to_int(a));
5418+}
5419+
53775420 const char *sp_bigint_to_s(sp_Bigint *b) {
53785421 sp_bigint_init_ctx();
53795422 mpz_t *z = &b->mpz;