From 345325838fc7d60edaa0c9a7fa5a7f1fa53b30d4 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Tue, 26 May 2026 10:54:35 +0900 Subject: [PATCH] lib/sort: faithful search + lbisect + rbisect port Ports ref/hare/sort/{search,bisect}.ha and the cmpfunc type (types.ha), replacing the experimental vtable placeholder. The powersort sort()/shuffle() surface stays out of scope. Divergences forced by ww's surface (rule-10 align-down, not behavioural): - cmp is a fn-VALUE param (cmpfunc), not Hare's *cmpfunc: ww renders functions-in-an-interface by value, as lib/io.ww's stream vtable does; *cmpfunc is not callable (no fn-ptr auto-deref) and &fn is *fn(...), unassignable to the alias. - no const (ww has none); *u8 base + uintptr stride (no [*] unbounded array, per 962); len() is i32 so cast : size; single-condition for, so Hare's afterthought is a body tail. - merged into one sort.ww (ww per-module convention; 900_stdlib smoke-compiles the file standalone, which a split breaks). 963_sort_run exercises all three on a []i32 with a real cmpfunc, mirroring +test.ha's search/lbisect/rbisect @test fns. The comparator binds its derefs to locals to dodge the pre-existing inline-deref-in-comparison cgen bug (#116); that bug is in the user comparator, not search/bisect, so the port is faithful. lib/sort is not compiler-imported: byte-id-neutral, no combined.ww change, 990-997 unaffected. --- Makefile | 5 ++ lib/sort/sort.ww | 119 ++++++++++++++++++++----- test/wcc/963_sort_run.c | 189 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 293 insertions(+), 20 deletions(-) create mode 100644 test/wcc/963_sort_run.c diff --git a/Makefile b/Makefile index 8a847f8f..1ea2cfd5 100644 --- a/Makefile +++ b/Makefile @@ -340,6 +340,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_opaque_decl_run \ $(BIN)/test_opaque_guards \ $(BIN)/test_opaque_assign_cast_run \ + $(BIN)/test_sort_run \ $(BIN)/test_bufio_run $(BIN)/test_random_run $(BIN)/test_smoke: test/wcc/000_smoke.c $(LIB)/libwcc.a | $(BIN) @@ -1113,6 +1114,10 @@ $(BIN)/test_opaque_assign_cast_run: test/wcc/962_opaque_assign_cast_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_sort_run: test/wcc/963_sort_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_f64crossmod_run: test/wcc/953_f64crossmod_run.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ $(LIB)/libwwrt.a | $(BIN) diff --git a/lib/sort/sort.ww b/lib/sort/sort.ww index bc49c005..7c48c242 100644 --- a/lib/sort/sort.ww +++ b/lib/sort/sort.ww @@ -1,29 +1,108 @@ -// sort — sorting helpers. The data is reached through a vtable so the -// algorithm stays generic without language-level generics. +// sort — operations on sorted slices: binary search + insertion-point +// bisection. Faithful port of Hare's sort::{search,lbisect,rbisect} and +// the cmpfunc type (ref/hare/sort/{search,bisect,types}.ha). The +// powersort sort()/insort()/shuffle() surface is out of scope here. +// +// Hare splits this across types.ha/search.ha/bisect.ha; ww merges a +// module into one file (same convention as lib/types/types.ww merging +// Hare's limits.ha + arch+x86_64.ha), and 900_stdlib smoke-compiles +// lib/sort/sort.ww standalone, which a split would break. +// +// Divergences from the Hare source, all forced by ww's surface (not +// behavioural — rule-10 align-down): +// • cmp is a fn-VALUE param (`cmp: cmpfunc`), not Hare's pointer +// (`cmp: *cmpfunc`). ww renders functions-in-an-interface by value +// exactly as lib/io.ww's stream vtable does (`read: fn(...)`); the +// `*cmpfunc` pointer form is not callable (no fn-ptr auto-deref) and +// `&fn` is `*fn(...)`, not assignable to the named alias `*cmpfunc`. +// • no `const` qualifier (ww has none; lib/io.ww drops it likewise). +// • Hare walks bytes through `in: *[*]u8` + `&ba[i*sz]`; ww has no +// unbounded-array `[*]`, so the base is `*u8` reinterpreted to +// `uintptr` and elements are `base + i*sz` (the stride idiom proven +// by test/wcc/962_opaque_assign_cast_run.c). +// • `len()` yields i32 in ww (slice length is i32 today), so the +// count is cast `: size`; Hare's len() is already size. +// • ww `for` is single-condition only (no `for (init; cond; post)`), +// so Hare's `for (cond; afterthought)` becomes a body-tail step. package sort; -type slice = struct { - ctx: *void, - len: i32, - less: fn(s: *slice, i: i32, j: i32) bool, - swap: fn(s: *slice, i: i32, j: i32) void, +// ref/hare/sort/types.ha:11 — comparator: <0 / 0 / >0 for a < / == / > b. +export type cmpfunc = fn(a: *opaque, b: *opaque) int; + +// ref/hare/sort/search.ha:6-27 — binary search; element index, or void. +export fn search(in: []opaque, sz: size, key: *opaque, cmp: cmpfunc) (size | void) = { + let base: uintptr = (in: *u8): uintptr; + let nmemb: size = len(in): size; + for (nmemb > 0) { + let v: *opaque = (base + (nmemb / 2 * sz): uintptr): *opaque; + let r: int = cmp(key, v); + if (r < 0) { + nmemb = nmemb / 2; + } else if (r > 0) { + base = (v: uintptr) + (sz: uintptr); + nmemb = nmemb - (nmemb / 2 + 1); + } else { + let offs: uintptr = (v: uintptr) - ((in: *u8): uintptr); + return (offs / (sz: uintptr)): size; + }; + }; + return void; }; -// Insertion sort, fine for small inputs and stable. We'll grow into -// quicksort later when we have heavier tests. -export fn sort(s: *slice) void = { - let i: i32 = 1; - for (i < s.len) { - let j: i32 = i; - for (j > 0) { - if (s.less(s, j, j - 1)) { - s.swap(s, j, j - 1); - j -= 1; - } else { - j = 0; +// ref/hare/sort/bisect.ha:7-33 — insertion index before the first +// occurrence of an equal element. +export fn lbisect(in: []opaque, sz: size, elem: *opaque, cmp: cmpfunc) size = { + let min: size = 0; + let max: size = len(in): size; + let base: uintptr = (in: *u8): uintptr; + for (min < max) { + let i: size = (max - min) / 2 + min; + let v: *opaque = (base + (i * sz): uintptr): *opaque; + let r: int = cmp(elem, v); + if (r < 0) { + max = i; + } else if (r > 0) { + min = i + 1; + } else { + if (i == 0) { return 0; }; + for (i > 0) { + let vp: *opaque = (base + ((i - 1) * sz): uintptr): *opaque; + let rr: int = cmp(elem, vp); + if (rr != 0) { break; }; + i = i - 1; }; + return i; }; - i += 1; }; + return max; +}; + +// ref/hare/sort/bisect.ha:38-65 — insertion index after the last +// occurrence of an equal element. +export fn rbisect(in: []opaque, sz: size, elem: *opaque, cmp: cmpfunc) size = { + let nmemb: size = len(in): size; + let min: size = 0; + let max: size = nmemb; + let base: uintptr = (in: *u8): uintptr; + for (min < max) { + let i: size = (max - min) / 2 + min; + let v: *opaque = (base + (i * sz): uintptr): *opaque; + let r: int = cmp(elem, v); + if (r < 0) { + max = i; + } else if (r > 0) { + min = i + 1; + } else { + i = i + 1; + for (i < nmemb) { + let vp: *opaque = (base + (i * sz): uintptr): *opaque; + let rr: int = cmp(elem, vp); + if (rr != 0) { break; }; + i = i + 1; + }; + return i; + }; + }; + return max; }; diff --git a/test/wcc/963_sort_run.c b/test/wcc/963_sort_run.c new file mode 100644 index 00000000..ee98e5a8 --- /dev/null +++ b/test/wcc/963_sort_run.c @@ -0,0 +1,189 @@ +/* + * 963_sort_run — runtime proof of lib/sort's search + lbisect + rbisect + * (faithful port of ref/hare/sort/{search,bisect}.ha). The three rows + * mirror the three relevant @test fns in ref/hare/sort/+test.ha + * (search:42-51, lbisect:8-23, rbisect:25-40), instantiated on a + * concrete []i32 with a real cmpfunc. + * + * lib/sort rides the opaque type-erasure surface proven by 962 + * (slice->[]opaque, *opaque<->*u8 reinterpret, uintptr byte stride). The + * functions are exercised only here — they are dead in the bootstrap, so + * no 990-997 gate touches them; this executed-and-checked probe is their + * coverage (per the bootstrap-coverage discipline). + * + * The comparator binds its derefs to locals (`let va = *pa; if (va < + * vb)`) rather than Hare's inline `if (*pa < *pb)`: the inline-deref-in- + * comparison shape is mis-compiled by a PRE-EXISTING cgen bug (#116 / + * task #18, the same family 962's header documents). The bug is in the + * user-supplied comparator, NOT in search/bisect — those bind `r = + * cmp(...)` then compare, so the lib port is faithful and unaffected. + * + * Table-driven like 958_types_sizelim_run / 952_floats_run: each row is a + * self-contained ww program `import sort;`. cstage `ww build -I lib` + * compiles it, we run the binary and assert the exit code (0 == every + * assertion held; a small locator otherwise). cstage-only by design + * (mirrors 958/962): lib/sort is not compiler-imported, so per-program + * wwstage byte-id is out of scope and the 990-997 gates are unaffected. + */ +#include +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return 1; +} + +#define INTS \ + "fn ints(a: *opaque, b: *opaque) int = {\n" \ + " let pa: *i32 = a: *i32;\n" \ + " let pb: *i32 = b: *i32;\n" \ + " let va: i32 = *pa;\n" \ + " let vb: i32 = *pb;\n" \ + " if (va < vb) { return -1; };\n" \ + " if (va > vb) { return 1; };\n" \ + " return 0;\n" \ + "};\n" + +struct row { const char *label; const char *src; int want_exit; }; + +static const struct row rows[] = { + /* search: every present key resolves to its index; an absent key + * is void. ref/hare/sort/+test.ha:42-51. */ + { "search", + "package main;\n" + "import sort;\n" + INTS + "export fn main() i32 = {\n" + " let nums: [10]i32 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n" + " let s: []i32 = nums[0:10];\n" + " let n: size = len(s): size;\n" + " let i: size = 0;\n" + " for (i < n) {\n" + " let key: i32 = nums[i];\n" + " let p: size = sort.search(s, size(i32), &key, ints) as size;\n" + " if (p != i) { return 1; };\n" + " i = i + 1;\n" + " };\n" + " let miss: i32 = 1337;\n" + " let r = sort.search(s, size(i32), &miss, ints);\n" + " if (!(r is void)) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + /* lbisect: first-occurrence index for present values; insertion + * point for absent ones. ref/hare/sort/+test.ha:8-23. */ + { "lbisect", + "package main;\n" + "import sort;\n" + INTS + "export fn main() i32 = {\n" + " let nums: [10]i32 = [1, 3, 4, 4, 5, 7, 9, 11, 11, 11];\n" + " let s: []i32 = nums[0:10];\n" + " let n: size = len(s): size;\n" + " let i: size = 0;\n" + " for (i < n) {\n" + " if (i == 0 || nums[i - 1] != nums[i]) {\n" + " let key: i32 = nums[i];\n" + " if (sort.lbisect(s, size(i32), &key, ints) != i) { return 1; };\n" + " };\n" + " i = i + 1;\n" + " };\n" + " let n0: i32 = 0; if (sort.lbisect(s, size(i32), &n0, ints) != 0) { return 2; };\n" + " let n6: i32 = 6; if (sort.lbisect(s, size(i32), &n6, ints) != 5) { return 3; };\n" + " let n8: i32 = 8; if (sort.lbisect(s, size(i32), &n8, ints) != 6) { return 4; };\n" + " let n12: i32 = 12; if (sort.lbisect(s, size(i32), &n12, ints) != n) { return 5; };\n" + " return 0;\n" + "};\n", 0 }, + /* rbisect: last-occurrence+1 index for present values; insertion + * point for absent ones. ref/hare/sort/+test.ha:25-40. */ + { "rbisect", + "package main;\n" + "import sort;\n" + INTS + "export fn main() i32 = {\n" + " let nums: [10]i32 = [1, 3, 4, 4, 5, 7, 9, 11, 11, 11];\n" + " let s: []i32 = nums[0:10];\n" + " let n: size = len(s): size;\n" + " let i: size = 0;\n" + " for (i < n) {\n" + " if (i == n - 1 || nums[i + 1] != nums[i]) {\n" + " let key: i32 = nums[i];\n" + " if (sort.rbisect(s, size(i32), &key, ints) != i + 1) { return 1; };\n" + " };\n" + " i = i + 1;\n" + " };\n" + " let n0: i32 = 0; if (sort.rbisect(s, size(i32), &n0, ints) != 0) { return 2; };\n" + " let n6: i32 = 6; if (sort.rbisect(s, size(i32), &n6, ints) != 5) { return 3; };\n" + " let n8: i32 = 8; if (sort.rbisect(s, size(i32), &n8, ints) != 6) { return 4; };\n" + " let n12: i32 = 12; if (sort.rbisect(s, size(i32), &n12, ints) != n) { return 5; };\n" + " return 0;\n" + "};\n", 0 }, + { NULL, 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/wwsort_%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/wwsort_%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[%s]: build failed\n", rows[i].label); + 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[%s]: exit %d, want %d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + unlink(src); unlink(outbin); rmdir(tmpdir); + } + if (fail) { + fprintf(stderr, "%d/%d sort tests failed\n", fail, n); + return 1; + } + printf("sort: %d/%d ok\n", n, n); + return 0; +}