Commit 5c94b774

5c94b774e4aeMerged

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

Description

codegen,runtime: bitwise ops on a bignum receiver / 64-bit-mask idiom

bignum & MASK64 (and |/^/<</>>) -- the 64-bit-unsigned masking idiom every PRNG/hash uses -- regressed once literals wider than int64 became bignums (aac15527): a bignum receiver hit "unsupported call", and even the lowered cases were wrong because the runtime bitwise helpers round-tripped through int64.

Three coordinated fixes:

  • runtime: sp_bigint_and/or/xor operate limb-wise on two non-negative bignums (true magnitude), instead of truncating both to int64 -- so a value/result exceeding int64 (0x9e37…c16 & MASK64) stays exact. A negative operand still uses the int64 path (two's-complement masking is uncommon).
  • codegen+analyze: a bignum-receiver &/|/^/<</>> lowers to sp_bigint_* (promoting an int/poly second operand), and infers TY_BIGINT.
  • codegen+analyze: a literal 1 << 64 (the (1 << 64) - 1 mask) overflows int64, so it infers TY_BIGINT and emits sp_bigint_shl rather than a UB C 1LL << 64LL.

A full SplitMix64 sequence now matches CRuby byte-for-byte.

Fixes #1516.

make test 999 / make bench 57 / optcarrot checksum 59662.

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

Details

Commit
5c94b774e4ae
Parent
123dc3bccff7

lib/sp_bigint.cmodifiedView file

@@ -5401,27 +5401,35 @@
54015401 return z->sn < 0 ? -v : v;
54025402 }
54035403
5404-/* Bitwise ops on bigint (Phase 1 of --int-overflow=promote
5405- coverage). Implemented via int64 round-trip: every bigint
5406- produced by promotion in spinel is derived from a small int
5407- that fits in int64, so the round-trip preserves Ruby semantics
5408- for any value in [INT64_MIN, INT64_MAX]. Values exceeding that
5409- range lose precision; that's acceptable as a starting point
5410- (extremely rare in practice -- bitops on integers > 2^63
5411- aren't a real workload yet). A future commit can swap in the
5412- custom mpz_and / mpz_ior / mpz_xor / mpz_com implementations
5413- if the int64 path proves insufficient. */
5414-sp_Bigint *sp_bigint_and(sp_Bigint *a, sp_Bigint *b) {
5415- return sp_bigint_new_int(sp_bigint_to_int(a) & sp_bigint_to_int(b));
5404+/* Bitwise AND/OR/XOR on bigints. For two non-negative operands -- the
5405+ 64-bit-mask idiom `bignum & MASK64` (PRNGs/hashing) -- operate limb-wise on
5406+ the true magnitude, so a value/result exceeding int64 stays exact (the old
5407+ int64 round-trip truncated `0x9e37…c16 & MASK64` to its signed value). A
5408+ negative operand follows Ruby's infinite two's-complement, which is uncommon
5409+ for bit masking, so it still routes through the int64 path. */
5410+static sp_Bigint *sp_bigint_bitwise(sp_Bigint *a, sp_Bigint *b, char op) {
5411+ if (!a || !b || a->mpz.sn < 0 || b->mpz.sn < 0) {
5412+ int64_t x = sp_bigint_to_int(a), y = sp_bigint_to_int(b);
5413+ int64_t r = (op == '&') ? (x & y) : (op == '|') ? (x | y) : (x ^ y);
5414+ return sp_bigint_new_int(r);
5415+ }
5416+ size_t na = a->mpz.sz, nb = b->mpz.sz;
5417+ size_t n = (op == '&') ? (na < nb ? na : nb) : (na > nb ? na : nb);
5418+ sp_Bigint *r = sp_bigint_alloc();
5419+ mpz_t z;
5420+ mpz_init_heap(sp_mpz_ctx, &z, n);
5421+ for (size_t i = 0; i < n; i++) {
5422+ mp_limb la = dg(&a->mpz, i), lb = dg(&b->mpz, i);
5423+ z.p[i] = (op == '&') ? (la & lb) : (op == '|') ? (la | lb) : (la ^ lb);
5424+ }
5425+ z.sn = (n > 0) ? 1 : 0;
5426+ trim(&z);
5427+ r->mpz = z;
5428+ return r;
54165429 }
5417-
5418-sp_Bigint *sp_bigint_or(sp_Bigint *a, sp_Bigint *b) {
5419- return sp_bigint_new_int(sp_bigint_to_int(a) | sp_bigint_to_int(b));
5420-}
5421-
5422-sp_Bigint *sp_bigint_xor(sp_Bigint *a, sp_Bigint *b) {
5423- return sp_bigint_new_int(sp_bigint_to_int(a) ^ sp_bigint_to_int(b));
5424-}
5430+sp_Bigint *sp_bigint_and(sp_Bigint *a, sp_Bigint *b) { return sp_bigint_bitwise(a, b, '&'); }
5431+sp_Bigint *sp_bigint_or(sp_Bigint *a, sp_Bigint *b) { return sp_bigint_bitwise(a, b, '|'); }
5432+sp_Bigint *sp_bigint_xor(sp_Bigint *a, sp_Bigint *b) { return sp_bigint_bitwise(a, b, '^'); }
54255433
54265434 sp_Bigint *sp_bigint_shr(sp_Bigint *a, int64_t n); /* forward decl for mutual recursion */
54275435