test: math::floats fold-1 runtime regression

This commit is contained in:
2026-05-25 13:17:47 +09:00
parent 253f13fa3b
commit 389f314473
2 changed files with 183 additions and 0 deletions

View File

@@ -328,6 +328,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_siphash_run \
$(BIN)/test_checked_run \
$(BIN)/test_f64cgen_run \
$(BIN)/test_floats_run \
$(BIN)/test_bufio_run $(BIN)/test_random_run
$(BIN)/test_smoke: test/wcc/000_smoke.c $(LIB)/libwcc.a | $(BIN)
@@ -1065,6 +1066,10 @@ $(BIN)/test_f64cgen_run: test/wcc/951_f64cgen_run.c $(BIN)/ww $(BIN)/w6c \
$(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_floats_run: test/wcc/952_floats_run.c $(BIN)/ww $(BIN)/w6c \
$(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
sizelint:
@sh tools/sizelint

178
test/wcc/952_floats_run.c Normal file
View File

@@ -0,0 +1,178 @@
/*
* 952_floats_run — runtime regression net for lib/math/floats fold-1
* (f64bits, f64frombits, isnan, isinf, signf64, absf64, copysignf64,
* ispositivef64, isnegativef64). The classify/sign/bits surface rides
* the f64-codegen paths fixed by #96 (deref-load -> MOVSD/X0) and #97
* (compare consults PF for NaN); both stages emit byte-identical asm, so
* the 990-997 byte-id gates can't catch a reintroduction — only an
* executed-and-checked runtime probe can. This file is that probe.
*
* Table-driven like 951_f64cgen_run: each row is a self-contained ww
* program importing math; the C-side cstage `ww build -I lib` compiles
* it, we run the binary and assert the exit code. Self-checking rows
* return 0 on pass / a locator code on the first failing assertion;
* value-propagation rows return a computed result compared to want_exit.
*
* cstage-only by design (mirrors 951 + 969_checked_run): `ww_ww run` is
* broken (#95) and per-program wwstage byte-id is the 990-997 gates' job.
*
* NaN/INF operands are built at runtime through opaque zero()/one() fns
* so the checker can't const-fold them away (matches 951's #97 rows).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
struct row { const char *src; int want_exit; };
static const struct row rows[] = {
/* f64bits + f64frombits round-trip. 0x3FF0000000000000 == 1.0,
* 0x4045000000000000 == 42.0. Round-trip through tobits/frombits
* must preserve the bit pattern (both u64 and f64 deref-loads). */
{ "package main;\n"
"import math;\n"
"export fn main() i32 = {\n"
" if (math.f64bits(1.0) != 0x3FF0000000000000u64) { return 1; };\n"
" if (math.f64bits(0.0) != 0u64) { return 2; };\n"
" let rt: f64 = math.f64frombits(math.f64bits(-2.5));\n"
" if (rt != -2.5) { return 3; };\n"
" if (math.f64frombits(0x4045000000000000u64) != 42.0) { return 4; };\n"
" return 0;\n"
"};\n", 0 },
/* f64frombits value propagation: a u64 bit pattern reinterpreted as
* f64 then truncated to i32. On #96 the deref strands the value in a
* GPR and the i32 cast reads stale X0. */
{ "package main;\n"
"import math;\n"
"export fn main() i32 = {\n"
" return math.f64frombits(0x4045000000000000u64): i32;\n"
"};\n", 42 },
/* isnan + isinf. Runtime NaN/INF via opaque zero()/one() so the
* checker can't const-fold. isnan rides #97 (self-inequality must
* be true for NaN); isinf rides #96 (f64bits deref-load). */
{ "package main;\n"
"import math;\n"
"fn zero() f64 = { return 0.0; };\n"
"fn one() f64 = { return 1.0; };\n"
"export fn main() i32 = {\n"
" let nan: f64 = zero() / zero();\n"
" if (!math.isnan(nan)) { return 1; };\n"
" if (math.isnan(1.0)) { return 2; };\n"
" let inf: f64 = one() / zero();\n"
" if (!math.isinf(inf)) { return 3; };\n"
" if (!math.isinf(-inf)) { return 4; };\n"
" if (math.isinf(nan)) { return 5; };\n"
" if (math.isinf(1.23)) { return 6; };\n"
" return 0;\n"
"};\n", 0 },
/* signf64 + ispositivef64 + isnegativef64. +0.0 has a clear sign
* bit so signf64(0.0) == 1 (zero is positive). */
{ "package main;\n"
"import math;\n"
"export fn main() i32 = {\n"
" if (math.signf64(3.0) != 1i64) { return 1; };\n"
" if (math.signf64(-3.0) != -1i64) { return 2; };\n"
" if (math.signf64(0.0) != 1i64) { return 3; };\n"
" if (!math.ispositivef64(3.0)) { return 4; };\n"
" if (math.ispositivef64(-3.0)) { return 5; };\n"
" if (!math.isnegativef64(-3.0)) { return 6; };\n"
" if (math.isnegativef64(3.0)) { return 7; };\n"
" return 0;\n"
"};\n", 0 },
/* absf64 + copysignf64. absf64(NaN) stays NaN (early return before
* the bit-mask). copysign transfers y's sign onto x's magnitude. */
{ "package main;\n"
"import math;\n"
"fn zero() f64 = { return 0.0; };\n"
"export fn main() i32 = {\n"
" if (math.absf64(-3.0) != 3.0) { return 1; };\n"
" if (math.absf64(3.0) != 3.0) { return 2; };\n"
" let nan: f64 = zero() / zero();\n"
" if (!math.isnan(math.absf64(nan))) { return 3; };\n"
" if (math.copysignf64(3.0, -1.0) != -3.0) { return 4; };\n"
" if (math.copysignf64(-3.0, 1.0) != 3.0) { return 5; };\n"
" if (math.copysignf64(3.0, 1.0) != 3.0) { return 6; };\n"
" return 0;\n"
"};\n", 0 },
/* absf64 value propagation: f64 result truncated to i32 (rides #96
* f64-return through X0). */
{ "package main;\n"
"import math;\n"
"export fn main() i32 = {\n"
" return math.absf64(-7.0): i32;\n"
"};\n", 7 },
{ NULL, 0 }
};
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[1024];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwflt_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwflt_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char cmd[2048];
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build -I %s/lib %s",
tmpdir, bin, cwd, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row %d: build failed\n src: %s\n",
i, rows[i].src);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row %d: exit %d, want %d\n src: %s\n",
i, got, rows[i].want_exit, rows[i].src);
fail++;
}
unlink(src); unlink(outbin); rmdir(tmpdir);
}
if (fail) {
fprintf(stderr, "%d/%d floats tests failed\n", fail, n);
return 1;
}
printf("floats: %d/%d ok\n", n, n);
return 0;
}