w6c+wcc+selfhost+lib: int-cast truncate + use_alias, 5 new modules
Two cgen/check bugs surfaced by new lib modules, plus the modules
themselves (crc64, siphash, random, base64, base32).
1. `(big_u64): u32` (and `: u16`, `: u8`, `: bool`) didn't truncate.
N_CAST emitted nothing for int↔int; the value stayed in AX with
its upper bits intact and downstream CMPQ/DIVQ misread the slot.
The TK_TILDE path already had clamp logic for the same reason —
N_CAST was the missing case. Both stages now MOVL r,r for u32 and
ANDQ $mask for u8/u16/bool. Signed-narrow (i8/i16/i32) stays
no-op until w6a grows reg-reg MOVSBQ/MOVSWQ/MOVSXD. selfhost
cgcast walks alias chains via aliaslookup before checking
primsize/typenameisunsigned so `(u: random)` where
`type random = u64` still bypasses the clamp.
See cmd/w6c/cgen.c N_CAST and selfhost/cmd/wcc/cgenexpr.ww cgcast.
2. `mod.mod` type refs (`random.random` when the imported module
declares `export type random = u64;`) failed with "unknown type".
The driver concatenates imports into one flat scope, so SK_USE
`random` collided with SK_TYPE `random` and scope_define silently
dropped the use. resolve_typename's leaf lookup required
`kind == SK_USE` and gave up. Adds a `use_alias` flag to Sym; the
pass-1 decl scan now marks colliding syms in both directions
(use-after-type and type-after-use). resolve_typename and the
N_DOT cexpr branch treat `use_alias` like SK_USE for qualified
lookup. selfhost check.ww was already lenient on this path so no
ww-side change was needed; bootstrap fixed point (990-995) holds.
See cmd/wcc/check.c installdecl pass + N_DOT/resolve_typename and
cmd/wcc/ww.h Sym.use_alias.
New modules under lib/, each with @test vectors in *_test.ww and wired
into test/wcc/900_stdlib.c (26 modules → all compile):
- lib/hash/crc64 ECMA, ISO (mirror of crc32 shape)
- lib/hash/siphash SipHash-2-4, buffer-based sum/sum24
- lib/math/random SplitMix64 (init, next, u32n, u64n)
- lib/encoding/base64 RFC 4648 std + url-safe encode/decode + sizes
- lib/encoding/base32 RFC 4648 std + base32hex encode/decode + sizes
This commit is contained in:
56
lib/math/random/random.ww
Normal file
56
lib/math/random/random.ww
Normal file
@@ -0,0 +1,56 @@
|
||||
// math/random — SplitMix64 PRNG.
|
||||
//
|
||||
// Mirrors Hare's math::random surface. The state is a single u64;
|
||||
// Hare types it as `random = u64`. Callers thread a pointer through
|
||||
// next/u32n/u64n so each call advances the state in place.
|
||||
// Deterministic — same seed reproduces the same sequence.
|
||||
|
||||
export type random = u64;
|
||||
|
||||
// init — initialize a generator with `seed`. Same seed reproduces the
|
||||
// same sequence on every run. Mirrors Hare's random::init.
|
||||
export fn init(seed: u64) random = { return seed: random; };
|
||||
|
||||
// next — return a pseudo-random 64-bit value and advance the state.
|
||||
// SplitMix64, per Hare's random::next.
|
||||
export fn next(r: *random) u64 = {
|
||||
let s: u64 = (*r): u64 + 0x9E3779B97F4A7C15u64;
|
||||
*r = s: random;
|
||||
let a: u64 = s;
|
||||
a = (a ^ (a >> 30u64)) * 0xBF58476D1CE4E5B9u64;
|
||||
a = (a ^ (a >> 27u64)) * 0x94D049BB133111EBu64;
|
||||
return a ^ (a >> 31u64);
|
||||
};
|
||||
|
||||
// u32n — pseudo-random u32 in [0, n). n must be > 0. Uses Lemire's
|
||||
// fast unbiased mapping (mulhi-then-leftover-reject). Mirrors Hare's
|
||||
// random::u32n.
|
||||
export fn u32n(r: *random, n: u32) u32 = {
|
||||
let x: u32 = next(r): u32;
|
||||
let prod: u64 = (x: u64) * (n: u64);
|
||||
let leftover: u32 = prod: u32;
|
||||
if (leftover < n) {
|
||||
// thresh = -n mod n (two's-complement on u32).
|
||||
let neg: u32 = 0u32 - n;
|
||||
let thresh: u32 = neg % n;
|
||||
for (leftover < thresh) {
|
||||
x = next(r): u32;
|
||||
prod = (x: u64) * (n: u64);
|
||||
leftover = prod: u32;
|
||||
};
|
||||
};
|
||||
return (prod >> 32u64): u32;
|
||||
};
|
||||
|
||||
// u64n — pseudo-random u64 in [0, n). n must be > 0. Power-of-2 fast
|
||||
// path; otherwise rejection-sample to avoid modulo bias. Mirrors
|
||||
// Hare's random::u64n.
|
||||
export fn u64n(r: *random, n: u64) u64 = {
|
||||
if ((n & (n - 1u64)) == 0u64) { return next(r) & (n - 1u64); };
|
||||
// max = U64_MAX - (U64_MAX+1) % n = -1 - (-n % n)
|
||||
let neg: u64 = (0u64 - n);
|
||||
let max: u64 = 0xFFFFFFFFFFFFFFFFu64 - (neg % n);
|
||||
let out: u64 = next(r);
|
||||
for (out > max) { out = next(r); };
|
||||
return out % n;
|
||||
};
|
||||
61
lib/math/random/random_test.ww
Normal file
61
lib/math/random/random_test.ww
Normal file
@@ -0,0 +1,61 @@
|
||||
use random;
|
||||
|
||||
@test fn seq() void = {
|
||||
let r: random.random = random.init(1234567u64);
|
||||
if (random.next(&r) != 6457827717110365317u64) { let _: i32 = 1/0; };
|
||||
if (random.next(&r) != 3203168211198807973u64) { let _: i32 = 1/0; };
|
||||
if (random.next(&r) != 9817491932198370423u64) { let _: i32 = 1/0; };
|
||||
if (random.next(&r) != 4593380528125082431u64) { let _: i32 = 1/0; };
|
||||
if (random.next(&r) != 16408922859458223821u64) { let _: i32 = 1/0; };
|
||||
};
|
||||
|
||||
@test fn deterministic() void = {
|
||||
let a: random.random = random.init(42u64);
|
||||
let b: random.random = random.init(42u64);
|
||||
let i: i32 = 0;
|
||||
for (i < 32) {
|
||||
if (random.next(&a) != random.next(&b)) { let _: i32 = 1/0; };
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
@test fn u32n_inrange() void = {
|
||||
let r: random.random = random.init(7u64);
|
||||
let i: i32 = 0;
|
||||
for (i < 200) {
|
||||
let v: u32 = random.u32n(&r, 17u32);
|
||||
// v stored as u64 with possible high bits — mask before compare.
|
||||
let vv: u64 = (v: u64) & 0xFFFFFFFFu64;
|
||||
if (vv >= 17u64) { let _: i32 = 1/0; };
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
@test fn u64n_pow2() void = {
|
||||
let r: random.random = random.init(99u64);
|
||||
let i: i32 = 0;
|
||||
for (i < 200) {
|
||||
let v: u64 = random.u64n(&r, 16u64);
|
||||
if (v >= 16u64) { let _: i32 = 1/0; };
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
@test fn u64n_nonpow2() void = {
|
||||
let r: random.random = random.init(123u64);
|
||||
let i: i32 = 0;
|
||||
for (i < 200) {
|
||||
let v: u64 = random.u64n(&r, 100u64);
|
||||
if (v >= 100u64) { let _: i32 = 1/0; };
|
||||
i += 1;
|
||||
};
|
||||
};
|
||||
|
||||
export fn main() i32 = {
|
||||
seq();
|
||||
deterministic();
|
||||
u32n_inrange();
|
||||
u64n_pow2();
|
||||
u64n_nonpow2();
|
||||
return 0;
|
||||
};
|
||||
Reference in New Issue
Block a user