From a49ca3b5ea92c8f99e104c6dbf39791e0c043d71 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Sat, 27 Jun 2026 23:12:16 -0400 Subject: [PATCH] jolt-wrap64 fast path: skip the mask when already in signed-64 range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chez fixnums are 61-bit, so the bignum bitwise-and mask allocates for any value past 2^60 — and unchecked-* ran it on every result, even small in-range ones. An exact integer already in [-2^63, 2^63) is its own wrap, so return it directly; only an out-of-range result (a multiply overflowing into 128 bits) needs the mask. ~30% on in-range unchecked-add loops, neutral on full-64-bit multiply. Note: the 64-bit arithmetic floor on Chez stays ~31ns/multiply (bignum, no native 64-bit int); the test.check distribution hangs are dominated by generator/dispatch overhead, not arithmetic — this is a general win for long-heavy code, not a fix for those. --- host/chez/seq.ss | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/host/chez/seq.ss b/host/chez/seq.ss index 58ca46b..b924cc2 100644 --- a/host/chez/seq.ss +++ b/host/chez/seq.ss @@ -162,9 +162,16 @@ (define unc-mask64 #xFFFFFFFFFFFFFFFF) (define unc-2^63 #x8000000000000000) (define unc-2^64 #x10000000000000000) +(define unc-neg-2^63 (- unc-2^63)) +;; Wrap to a signed 64-bit value. Fast path: an exact integer already in +;; [-2^63, 2^63) is its own wrap — skip the bignum mask, which on Chez (61-bit +;; fixnums) allocates for any value past 2^60. Only an out-of-range result (a +;; multiply overflowing into 128 bits) needs the mask + sign fixup. (define (jolt-wrap64 x) - (let ((m (bitwise-and (if (and (number? x) (exact? x) (integer? x)) x (exact (floor x))) unc-mask64))) - (if (>= m unc-2^63) (- m unc-2^64) m))) + (if (and (exact? x) (integer? x) (>= x unc-neg-2^63) (< x unc-2^63)) + x + (let ((m (bitwise-and (if (and (number? x) (exact? x) (integer? x)) x (exact (floor x))) unc-mask64))) + (if (>= m unc-2^63) (- m unc-2^64) m)))) ;; unchecked-* only WRAP integer (long) math; on a flonum OR ratio operand they ;; are an ordinary numeric op, since *unchecked-math* never wraps a non-long — ;; Clojure's unchecked-add falls back to regular arithmetic for non-primitives: