portal-rng: asm xoshiro256** + cross-impl tests + default seed=0

Completes ticket 0001 started in 27f468c. All three impls now carry
bit-identical xoshiro256**; portal state round-trips across process
boundaries in every producer x consumer cell (Python <-> C <-> asm).

asm impl:
- 4 new builtins: random-seed!, random-int, random-state, random-state!
- g_rng_state in BSS (4 x u64); rng_splitmix64_step, rng_seed, rng_next
- Binary portal header bumped LUMBDAB1/48 -> LUMBDAB2/80; carries
  32 bytes of rng state at offsets 40..64, reserved moved to 72
- No float support in asm, so (random) intentionally omitted there
- _start seeds with 0 so the stream is deterministic from startup

Python + C (supplements 27f468c):
- rng_seed(0) auto-invoked at module load / register_portal_builtins
  so (random) without explicit (random-seed!) returns a real value
  instead of the all-zero xoshiro fixed point

Tests:
- tests/functional.lsp: 7 new shared assertions (Python + C)
- asm/test.sh: 5 new asm-local assertions (142 -> 147)
- tests/portal-rng-save.lsp / portal-rng-load.lsp: portable S-expression
  portal that captures both state AND next-5 baseline so loader self-
  verifies without a separate harness
- tests/portal-cross-test.sh: 9 new producer x consumer RNG cells; all
  18 cells pass end-to-end

Verified: seed=42, (random-int 1000000) draws 1..10 =
558742 543102 559009 124193 317476 750584 200754 814407 344958 929085
identical in Python, C, and asm.

unmoad scan: zero new findings in added code.
This commit is contained in:
russell@unturf.com 2026-04-20 11:11:47 -04:00
parent 27f468c5b7
commit 54c4c651bb
12 changed files with 383 additions and 13 deletions

View file

@ -58,6 +58,30 @@
(assert-equal "isqrt-just-above" (isqrt 101) 10)
(assert-equal "isqrt-large" (isqrt 1000000000000) 1000000)
;;; ═══════════════════════════════════════════════════════════════
;;; Random (xoshiro256**) — deterministic stream, portal-serializable
;;; across Python + C + asm. See docs/tickets/0001-portal-rng.md.
;;; ═══════════════════════════════════════════════════════════════
(random-seed! 42)
(assert-equal "random-int-1" (random-int 1000000) 558742)
(assert-equal "random-int-2" (random-int 1000000) 543102)
(assert-equal "random-int-3" (random-int 1000000) 559009)
;; Re-seeding to same seed reproduces the stream.
(random-seed! 42)
(assert-equal "random-int-reseed-1" (random-int 1000000) 558742)
(assert-equal "random-int-reseed-2" (random-int 1000000) 543102)
;; random-state / random-state! round-trips state exactly.
(random-seed! 42)
(define saved-state (random-state))
(assert-equal "random-state-len" (length saved-state) 8)
(random-int 1000000) ; advance
(random-int 1000000) ; advance
(random-state! saved-state)
(assert-equal "random-state-restore" (random-int 1000000) 558742)
;;; ═══════════════════════════════════════════════════════════════
;;; Comparison
;;; ═══════════════════════════════════════════════════════════════