From ba161a5f3ec27b3d9ed69b46ec98abe4ecf87704 Mon Sep 17 00:00:00 2001 From: Jon Ruttan Date: Thu, 5 Nov 2026 15:28:03 -0500 Subject: [PATCH 1/2] fix(format): the base-conversion chunk is bounded by bigint's limb base, tested by division %py-fmt-chunk-of looked for the largest base^k under 10^9 by multiplying until the product reached it. On a 4-byte word 16^8 is 2^32, which multiplies to zero, and a zero never reaches a bound: loading the bundle allocated until the machine refused, which is why no state image and no Python ran on 32-bit arm (4.19 GB on a Raspberry Pi 4, then exit 2). The bound is bigint's own limb base, sized to the word (10^9 on an 8-byte word, 10^4 on a 4-byte one), so a chunk stays inside one limb on either, and it is tested by division, which cannot wrap. On 64-bit the chunks are what they were; on the Pi the load now peaks at 1.07 GB. Co-Authored-By: Claude Fable 5.1 --- python/format.x | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/python/format.x b/python/format.x index 113b3e5..c8fc020 100644 --- a/python/format.x +++ b/python/format.x @@ -33,6 +33,8 @@ ; rather than mis-consume arguments. (module python/core) +; The limb base the base conversion below chunks by: sized to the word. +(import x/num/bigint bigint-base) (def %py-fmt-code (fn (_ s i) (python-char-code (%str-ref s i)))) (def %py-fmt-digit? (fn (_ c) (if (>= c 48) (<= c 57) #f))) @@ -87,16 +89,20 @@ ; Base conversion for %o %x %X and bin/hex/oct. The digits are the platform's ; own (%number->str with a radix). A bigint's limbs are decimal, so it gives -; up its low chunk to a division by the largest base^k under 10^9 -- inside one -; limb, a division in one pass -- until what is left is a machine int, the top -; chunk. A chunk below the top is exactly k digits: those of base^k + r, less -; the leading 1. +; up its low chunk to a division by the largest base^k under the limb base -- +; inside one limb, a division in one pass -- until what is left is a machine +; int, the top chunk. A chunk below the top is exactly k digits: those of +; base^k + r, less the leading 1. +; The limb base is bigint's, sized to the word: 10^9 on an 8-byte word, 10^4 +; on a 4-byte one. The bound is tested by division, never by the product: +; 16^8 is 2^32, which a 4-byte word multiplies to zero, and a zero never +; reaches a bound. (def %py-fmt-chunk-of (fn (_ base) ; (pow . k) (def go (fn (self p k) - (if (>= (* p base) 1000000000) (pair p k) (self (* p base) (+ k 1))))) + (if (> p (/ (- bigint-base 1) base)) (pair p k) (self (* p base) (+ k 1))))) (go base 1))) (def %py-fmt-chunk-2 (%py-fmt-chunk-of 2)) (def %py-fmt-chunk-8 (%py-fmt-chunk-of 8)) From bf57948448b2fddfc2c94ef9631f313e47df1595 Mon Sep 17 00:00:00 2001 From: Jon Ruttan Date: Thu, 5 Nov 2026 15:52:22 -0500 Subject: [PATCH 2/2] fix(format): the chunks are made at first use, so the load order stays Importing bigint at the top of format.x moved it ahead of the compile ladder and python's tokenizer registrations, and on a 4-byte word that order left every integer literal reading as a symbol while bigint.x itself loaded. bigint-base is runtime-num.x's import into this module, bound by the time a program formats and not before; the three chunks are made then, and nothing about the load moves. Co-Authored-By: Claude Fable 5.1 --- python/format.x | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/python/format.x b/python/format.x index c8fc020..94162b6 100644 --- a/python/format.x +++ b/python/format.x @@ -33,8 +33,6 @@ ; rather than mis-consume arguments. (module python/core) -; The limb base the base conversion below chunks by: sized to the word. -(import x/num/bigint bigint-base) (def %py-fmt-code (fn (_ s i) (python-char-code (%str-ref s i)))) (def %py-fmt-digit? (fn (_ c) (if (>= c 48) (<= c 57) #f))) @@ -104,12 +102,23 @@ (fn (self p k) (if (> p (/ (- bigint-base 1) base)) (pair p k) (self (* p base) (+ k 1))))) (go base 1))) -(def %py-fmt-chunk-2 (%py-fmt-chunk-of 2)) -(def %py-fmt-chunk-8 (%py-fmt-chunk-of 8)) -(def %py-fmt-chunk-16 (%py-fmt-chunk-of 16)) +; Made at first use, not at load: bigint-base is runtime-num.x's import into +; this module, bound by the time a program formats and not before, and the +; load order stays what the sweeps were placed for. +(def %py-fmt-chunk-2 ()) +(def %py-fmt-chunk-8 ()) +(def %py-fmt-chunk-16 ()) +(def %py-fmt-chunks! + (fn (_) + (if (null? %py-fmt-chunk-16) + (do (set! %py-fmt-chunk-2 (%py-fmt-chunk-of 2)) + (set! %py-fmt-chunk-8 (%py-fmt-chunk-of 8)) + (set! %py-fmt-chunk-16 (%py-fmt-chunk-of 16))) + ()))) (def %py-fmt-base (fn (_ v0 base upper) (def v (python-boolnorm v0)) + (%py-fmt-chunks!) (def ck (match ((= base 2) %py-fmt-chunk-2)