ww/lex: fold float literals through strconv.stof64 — 1-ULP cs≠ww class (#62)

wwstage's parsef64 (naive i64-accumulator + pow-10 fold) diverged from
cstage's strtod: >19-digit mantissas overflowed the accumulator (sign-bit
garbage), DBL_MIN was +1 ULP, DBL_MAX -2 ULP — the #59.10 ratchet pin.
C-strtod oracle confirms cstage correctly rounded on every vector, so
wwstage aligns to it by dogfooding strconv.stof64 (correctly-rounded
decimal engine, already imported by lex.ww). Overflow literals now
reject in both stages (stof64 overflow -> errat, mirroring ERANGE).

Fix + #59.10 M_DIVERGE->M_ID graduation + pins land together per the
ratchet's designed flow (the gate trips loud demanding graduation):
oracle-pinned vectors in toktest.ww floatfold_cases (lexer-unit) and
989_floatlit_run (compiler fold: runtime bits + byte-id + overflow
reject parity). Retained subnormal accept-set asymmetry filed as task
#21, documented at the lexnum site.
This commit is contained in:
2026-06-04 23:12:00 +09:00
parent 74767c70cc
commit 60e61315bc
7 changed files with 340 additions and 233 deletions

View File

@@ -298,82 +298,6 @@ fn scanexp(l: *lex) void = {
};
};
// parsef64 — minimal decimal-float parser. Reads digits[.digits][eE[+-]digits]
// from the first `n` bytes of `s` (no leading sign — the lexer emits
// the unary minus as a separate token). The result rounds to the
// nearest f64 only via the trailing pow-10 multiply; this matches
// `strtod` to 1 ULP on typical literals and is good enough for the
// wwstage's own use (no float literals appear in the bootstrap
// source). Anything past `n` or non-digit is silently ignored.
fn parsef64(s: *u8, n: u64) f64 = {
let i: u64 = 0u64;
let intp: i64 = 0i64;
for (i < n) {
let b: u8 = s[i];
if (b < 48u8) { break; };
if (b > 57u8) { break; };
intp = intp * 10i64 + (b - 48u8): i64;
i += 1u64;
};
let frac: i64 = 0i64;
let fscale: i64 = 1i64;
if (i < n) {
if (s[i] == '.') {
i += 1u64;
for (i < n) {
let b: u8 = s[i];
if (b < 48u8) { break; };
if (b > 57u8) { break; };
frac = frac * 10i64 + (b - 48u8): i64;
fscale = fscale * 10i64;
i += 1u64;
};
};
};
let exp: i32 = 0;
let expneg: bool = false;
if (i < n) {
let e: u8 = s[i];
if (e == 'e' || e == 'E') {
i += 1u64;
if (i < n) {
if (s[i] == '-') {
expneg = true;
i += 1u64;
} else { if (s[i] == '+') {
i += 1u64;
};};
};
for (i < n) {
let b: u8 = s[i];
if (b < 48u8) { break; };
if (b > 57u8) { break; };
exp = exp * 10 + (b - 48u8): i32;
i += 1u64;
};
};
};
let result: f64 = intp: f64;
if (frac != 0i64) {
result = result + (frac: f64) / (fscale: f64);
};
if (exp != 0) {
// Use int-to-float casts so this file stays free of float
// literals — 990's wwdump diff relies on lib/ww/lex/lex.ww
// tokenising identically through C and ww, and the C dumper
// %g-formats TK_FLOAT.fval while the ww dumper currently
// skips it. Hiding the constants behind casts keeps both
// sides emitting `FLOAT` with no payload.
let factor: f64 = 1: f64;
let ten: f64 = 10: f64;
let k: i32 = 0;
for (k < exp) { factor = factor * ten; k += 1; };
if (expneg) { result = result / factor; }
else { result = result * factor; };
};
return result;
};
fn lexnum(l: *lex, start: *pos, out: *tok) void = {
out.kind = tkind.TK_INT;
out.file = start.file;
@@ -451,7 +375,30 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
i += 1u64;
};
clean[j] = 0u8;
let fv: f64 = parsef64(clean.ptr, j);
let cleanv: str;
cleanv.ptr = clean.ptr;
cleanv.len = j: i32;
// strconv's correctly-rounded decimal engine — cstage folds
// via strtod, and a leaner pow-10 fold here was 1-2 ULP off
// on long-mantissa/extreme literals (cs≠ww DATA bits, #62).
// `0: f64` cast, not a 0.0 literal: 990's wwdump diff relies
// on this file tokenising identically through C and ww, and
// the C dumper %g-formats TK_FLOAT.fval while the ww dumper
// skips it.
// Retained divergence (task #21): SUBNORMAL literals are
// accepted here correctly-rounded (Hare stof semantics)
// but rejected by cstage (glibc strtod flags partial
// underflow with ERANGE).
let fv: f64 = 0: f64;
match (strconv.stof64(cleanv, strconv.base.DEC)) {
case let v: f64 => { fv = v; };
case let e: strconv.invalid => {
errat(l, start, "bad float literal");
};
case let e: strconv.overflow => {
errat(l, start, "bad float literal");
};
};
out.fval = fv;
// Stash the IEEE bits in uval — cgen consumers read floats
// as integers (n.uval) to avoid an SSE round-trip when

View File

@@ -272,9 +272,62 @@ fn checkprint(fd: i32, t: *tok, want: str) void = {
if (os.remove("/tmp/ww_s9_tok.tmp") != 0) { fail(); };
};
fn checkfloat(src: str, want: u64) void = {
let l: lex;
lexinit(&l, "t", src.ptr, src.len: u64);
let t: tok;
lexnext(&l, &t);
if (t.kind != tkind.TK_FLOAT) { fail(); };
if (t.uval != want) { fail(); };
};
// #62 pin: lexnum's float fold routes through strconv.stof64 and must
// produce the IEEE-754 correctly-rounded bits cstage gets from strtod
// — any rounding slip is a cs≠ww DATA divergence. Vectors pinned
// against a C strtod oracle. Rows cover the classes the retired
// pow-10 fold got wrong: >19-digit mantissas (its i64 accumulator
// overflowed), DBL_MIN/DBL_MAX extremes, and the decimal-fraction
// 1-ULP double-rounding cases; plus halfway-to-even, exponent forms,
// the underscore strip, the 53-digit exact-halfway pair at the 2^-53
// boundary (tie rounds to even, tie+1 rounds up — also the only
// >19-digit FRACTION rows), and an exact power of two.
@test fn floatfold_cases() void = {
signalled = 400; checkfloat("1.0000000000000002", 0x3FF0000000000001u64);
signalled = 401; checkfloat("9007199254740993.0", 0x4340000000000000u64);
signalled = 402; checkfloat("1.2345e67", 0x4DDD4E421712C0B7u64);
signalled = 403; checkfloat("0.1", 0x3FB999999999999Au64);
signalled = 404; checkfloat("1.1", 0x3FF199999999999Au64);
signalled = 405;
checkfloat("123456789012345678901234567890.0", 0x45F8EE90FF6C373Eu64);
signalled = 406;
checkfloat("2.2250738585072014e-308", 0x0010000000000000u64);
signalled = 407; checkfloat("0.3", 0x3FD3333333333333u64);
signalled = 408; checkfloat("3.141592653589793", 0x400921FB54442D18u64);
signalled = 409;
checkfloat("1.7976931348623157e308", 0x7FEFFFFFFFFFFFFFu64);
signalled = 410;
checkfloat("1.7976931348623158e308", 0x7FEFFFFFFFFFFFFFu64);
signalled = 411; checkfloat("7.2057594037927933e16", 0x4370000000000000u64);
signalled = 412;
checkfloat("1000000000000000000000.0", 0x444B1AE4D6E2EF50u64);
signalled = 413; checkfloat("1_000.5", 0x408F440000000000u64);
signalled = 414;
checkfloat("1.00000000000000011102230246251565404236316680908203125",
0x3FF0000000000000u64);
signalled = 415;
checkfloat("1.00000000000000011102230246251565404236316680908203126",
0x3FF0000000000001u64);
signalled = 416; checkfloat("4503599627370497.5", 0x4330000000000002u64);
signalled = 417; checkfloat("0.5", 0x3FE0000000000000u64);
signalled = 418; checkfloat("1.0e308", 0x7FE1CCF385EBC8A0u64);
signalled = 419;
checkfloat("2.225073858507202e-308", 0x0010000000000001u64);
};
export fn main() i32 = {
signalled = 1; tokname_cases();
signalled = 2; kwlookup_cases();
signalled = 3; tokprint_cases();
signalled = 4; floatfold_cases();
return 0;
};