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

@@ -7017,82 +7017,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;
@@ -7170,7 +7094,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

@@ -7017,82 +7017,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;
@@ -7170,7 +7094,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