From 60e61315bc98fee8be11f0ba78b9938bb0d23ed5 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Thu, 4 Jun 2026 23:12:00 +0900 Subject: [PATCH] =?UTF-8?q?ww/lex:=20fold=20float=20literals=20through=20s?= =?UTF-8?q?trconv.stof64=20=E2=80=94=201-ULP=20cs=E2=89=A0ww=20class=20(#6?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Makefile | 5 + lib/ww/lex/lex.ww | 101 ++++--------- lib/ww/lex/toktest.ww | 53 +++++++ selfhost/cmd/w6c/main.combined.ww | 101 ++++--------- selfhost/cmd/wwdump/main.combined.ww | 101 ++++--------- test/wcc/989_floatlit_run.c | 205 +++++++++++++++++++++++++++ test/wcc/989_lib_byteid.c | 7 +- 7 files changed, 340 insertions(+), 233 deletions(-) create mode 100644 test/wcc/989_floatlit_run.c diff --git a/Makefile b/Makefile index 8e15ffaf..e8a715b9 100644 --- a/Makefile +++ b/Makefile @@ -427,6 +427,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_siphash_run $(BIN)/test_sha256_run \ $(BIN)/test_regex_run \ $(BIN)/test_lib_byteid \ + $(BIN)/test_floatlit_run \ $(BIN)/test_checked_run \ $(BIN)/test_floatarr_run \ $(BIN)/test_deref_narrow_run \ @@ -1830,6 +1831,10 @@ $(BIN)/test_lib_byteid: test/wcc/989_lib_byteid.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_floatlit_run: test/wcc/989_floatlit_run.c $(BIN)/ww $(BIN)/w6c \ + $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_bufio_run: test/wcc/998_bufio_run.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/lib/ww/lex/lex.ww b/lib/ww/lex/lex.ww index e5c85d41..47c8635a 100644 --- a/lib/ww/lex/lex.ww +++ b/lib/ww/lex/lex.ww @@ -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 diff --git a/lib/ww/lex/toktest.ww b/lib/ww/lex/toktest.ww index 5dd81e1e..eb901ba4 100644 --- a/lib/ww/lex/toktest.ww +++ b/lib/ww/lex/toktest.ww @@ -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; }; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e6f75c7b..2966ab84 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -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 diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index c1f2485c..bd322de4 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -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 diff --git a/test/wcc/989_floatlit_run.c b/test/wcc/989_floatlit_run.c new file mode 100644 index 00000000..4969b8f4 --- /dev/null +++ b/test/wcc/989_floatlit_run.c @@ -0,0 +1,205 @@ +/* + * 989_floatlit_run — runtime + byte-id net for #62: the two stages + * folded float LITERALS differently. cstage folds via strtod + * (correctly rounded); the wwstage lexer used a pow-10 accumulation + * fold that was 1-2 ULP off on decimal fractions, overflowed its i64 + * accumulator past 19 mantissa digits, and missed DBL_MIN/DBL_MAX by + * up to 2 ULP — a pinned cs≠ww DATA divergence (989 ratchet #59.10). + * Fix: lexnum folds through strconv.stof64 (the Hare-ported + * correctly-rounded decimal engine). + * + * Fixture 1 (vectors): each row reads back a literal's IEEE bits via + * a *u64 reinterpret and compares against the C-strtod-oracle bit + * pattern; the exit code pinpoints the failing row. Carries (a) + * cstage `ww build` + run asserting exit 0 (rule-10: convergence + * targets the runtime-CORRECT side) and (b) w6c vs w6c_ww `.s` cmp + * (byte-id — the wwstage fold itself). + * + * Fixture 2 (overflow): 1.7976931348623159e308 rounds above DBL_MAX — + * strtod sets ERANGE so cstage rejects; wwstage must reject too + * (stof64 overflow → errat). Both compilers must exit non-zero. + * + * Retained divergence (task #21): a SUBNORMAL literal (e.g. + * 2.2250738585072011e-308) is rejected by cstage (glibc strtod flags + * partial underflow with ERANGE) but accepted correctly-rounded by + * wwstage (Hare stof semantics) — accept-set asymmetry only, not a + * bits divergence; pre-existing on master (old parsef64 accepted it + * with garbage bits). + * + * 9xx is full; shares the 989 prefix per the 989_sha256 precedent. + */ +#include +#include +#include +#include +#include + +static const char *vectors_src = + "package main;\n" + "fn bits(v: f64) u64 = {\n" + " let x: f64 = v;\n" + " let p: *u64 = (&x): *u64;\n" + " return *p;\n" + "};\n" + "export fn main() i32 = {\n" + " if (bits(1.0000000000000002) != 0x3FF0000000000001u64) { return 1; };\n" + " if (bits(9007199254740993.0) != 0x4340000000000000u64) { return 2; };\n" + " if (bits(1.2345e67) != 0x4DDD4E421712C0B7u64) { return 3; };\n" + " if (bits(0.1) != 0x3FB999999999999Au64) { return 4; };\n" + " if (bits(1.1) != 0x3FF199999999999Au64) { return 5; };\n" + " if (bits(123456789012345678901234567890.0) != 0x45F8EE90FF6C373Eu64) { return 6; };\n" + " if (bits(2.2250738585072014e-308) != 0x0010000000000000u64) { return 7; };\n" + " if (bits(0.3) != 0x3FD3333333333333u64) { return 8; };\n" + " if (bits(3.141592653589793) != 0x400921FB54442D18u64) { return 9; };\n" + " if (bits(1.7976931348623157e308) != 0x7FEFFFFFFFFFFFFFu64) { return 10; };\n" + " if (bits(1.7976931348623158e308) != 0x7FEFFFFFFFFFFFFFu64) { return 11; };\n" + " if (bits(7.2057594037927933e16) != 0x4370000000000000u64) { return 12; };\n" + " if (bits(1000000000000000000000.0) != 0x444B1AE4D6E2EF50u64) { return 13; };\n" + " if (bits(1_000.5) != 0x408F440000000000u64) { return 14; };\n" + " if (bits(1.00000000000000011102230246251565404236316680908203125) != 0x3FF0000000000000u64) { return 15; };\n" + " if (bits(1.00000000000000011102230246251565404236316680908203126) != 0x3FF0000000000001u64) { return 16; };\n" + " if (bits(4503599627370497.5) != 0x4330000000000002u64) { return 17; };\n" + " if (bits(0.5) != 0x3FE0000000000000u64) { return 18; };\n" + " if (bits(1.0e308) != 0x7FE1CCF385EBC8A0u64) { return 19; };\n" + " if (bits(2.225073858507202e-308) != 0x0010000000000001u64) { return 20; };\n" + " return 0;\n" + "};\n"; + +static const char *overflow_src = + "package main;\n" + "export fn main() i32 = {\n" + " let a: f64 = 1.7976931348623159e308;\n" + " return 0;\n" + "};\n"; + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa); + int cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +static int +writesrc(const char *path, const char *src) +{ + FILE *f = fopen(path, "wb"); + if (f == NULL) return -1; + fputs(src, f); + fclose(f); + return 0; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[1024]; + if (bin[0] != '/') { + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char w6c[1100], w6c_ww[1100]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + if (access(w6c_ww, X_OK) != 0) { + fprintf(stderr, "floatlit: w6c_ww missing — cannot run " + "the cs==ww byte-id gate (the whole point of this test)\n"); + return 1; + } + + int fail = 0; + char src[64], cmd[2048]; + + /* fixture 1: vectors — cstage build+run, then byte-id */ + snprintf(src, sizeof src, "/tmp/wwflit_%d.ww", getpid()); + if (writesrc(src, vectors_src) != 0) return 1; + + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwflit_%d_d", getpid()); + mkdir(tmpdir, 0755); + + snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", tmpdir, bin, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "floatlit: cstage build failed\n"); + fail++; + } else { + char outbin[128]; + snprintf(outbin, sizeof outbin, "%s/wwflit_%d", tmpdir, + getpid()); + int got = runwait(outbin); + if (got != 0) { + fprintf(stderr, "floatlit: vector row %d has wrong " + "bits at runtime (cstage)\n", got); + fail++; + } + unlink(outbin); + } + rmdir(tmpdir); + + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwflit_%d_cs.s", getpid()); + snprintf(ws_s, sizeof ws_s, "/tmp/wwflit_%d_ww.s", getpid()); + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "floatlit: w6c failed on vectors\n"); + fail++; + } else { + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c_ww, ws_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "floatlit: w6c_ww failed on vectors\n"); + fail++; + } else if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, "floatlit: cstage/wwstage .s DIFFER " + "(rule-10 byte-id violation)\n"); + fail++; + } + } + unlink(src); unlink(cs_s); unlink(ws_s); + + /* fixture 2: overflow literal — BOTH stages must reject */ + snprintf(src, sizeof src, "/tmp/wwflit_%d_ovf.ww", getpid()); + if (writesrc(src, overflow_src) != 0) return 1; + snprintf(cmd, sizeof cmd, "%s %s >/dev/null 2>&1", w6c, src); + if (runwait(cmd) == 0) { + fprintf(stderr, "floatlit: w6c ACCEPTED overflow literal\n"); + fail++; + } + snprintf(cmd, sizeof cmd, "%s %s >/dev/null 2>&1", w6c_ww, src); + if (runwait(cmd) == 0) { + fprintf(stderr, "floatlit: w6c_ww ACCEPTED overflow literal\n"); + fail++; + } + unlink(src); + + if (fail) { + fprintf(stderr, "floatlit: %d check(s) failed\n", fail); + return 1; + } + printf("floatlit: vectors byte-id + runtime-correct, " + "overflow rejected by both stages\n"); + return 0; +} diff --git a/test/wcc/989_lib_byteid.c b/test/wcc/989_lib_byteid.c index b4359ea2..eb66dd0a 100644 --- a/test/wcc/989_lib_byteid.c +++ b/test/wcc/989_lib_byteid.c @@ -82,6 +82,10 @@ static const struct ent ents[] = { { .fixture = "lib/regex/regex_test.ww", .mode = M_ID }, { .fixture = "lib/strconv/test/decimaltest.ww", .mode = M_ID }, { .fixture = "lib/strconv/test/ftostest.ww", .mode = M_ID }, + /* graduated from #59.10 DIVERGE by the #62 float-literal fold fix + * (wwstage lexer now folds through strconv.stof64, matching + * cstage's strtod bit-for-bit) */ + { .fixture = "lib/strconv/test/stoftest.ww", .mode = M_ID }, { .fixture = "lib/strconv/test/inttest.ww", .mode = M_ID }, { .fixture = "lib/strings/stringstest.ww", .mode = M_ID }, { .fixture = "lib/temp/temptest.ww", .mode = M_ID }, @@ -125,8 +129,7 @@ static const struct ent ents[] = { .mode = M_DIVERGE, .cite = "#59.8" }, { .fixture = "lib/os/stattest.ww", .mode = M_DIVERGE, .cite = "#59.9" }, - { .fixture = "lib/strconv/test/stoftest.ww", - .mode = M_DIVERGE, .cite = "#59.10" }, + /* #59.10 stoftest graduated to M_ID above (#62 fix) */ { .fixture = "lib/ww/lex/toktest.ww", .mode = M_DIVERGE, .cite = "#59.11" }, /* asttest resolves `import tok` via -I lib/ww/lex, cf 905 */