nandi/vflutter_ffipublic Fork 0
728acaab2594667f4998fc18812e7c1e2e4cc549
Commits
Clone
git clone https://git.rickub.com/nandi/vflutter_ffi.git
git clone ssh://git@rickub.com/nandi/vflutter_ffi.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

check_parity.sh · 55 lines · 1.9 KBBash Blame HistoryRaw
Add a WebAssembly backend for the example 728acaa nandithebull 22h ago1#!/usr/bin/env bash
2# The same Mandelbrot frame from the native kernel and the wasm kernel,
3# compared byte for byte.
4#
5# The point is not that floating point is deterministic in the abstract — it
6# is that these two builds go through different compilers, different libm
7# implementations (glibc vs emscripten's musl) and different word sizes
8# (64-bit vs wasm32), and still have to agree, or "the same V code everywhere"
9# is a slogan rather than a fact.
10set -euo pipefail
11cd "$(dirname "$0")/.."
12
13[ -f build/libvflutter.so ] || { echo "run ./tool/build.sh first"; exit 1; }
14[ -f example/web/vflutter.js ] || { echo "run ./tool/build_wasm.sh first"; exit 1; }
15
16work="$(mktemp -d)"
17trap 'rm -rf "$work"' EXIT
18
19cat > "$work/native_frame.c" <<'EOF'
20#include "vflutter.h"
21#include <stdio.h>
22#include <stdlib.h>
23int main(int argc, char **argv) {
24 (void)argc;
25 const int W = 240, H = 180, ITER = 300;
26 size_t n = (size_t)W * H * 4;
27 unsigned char *b = calloc(n, 1);
28 vf_init();
29 vf_mandelbrot(b, n, W, H, -0.5, 0.0, 3.0, ITER, 0, H);
30 FILE *f = fopen(argv[1], "wb");
31 fwrite(b, 1, n, f);
32 fclose(f);
33 return 0;
34}
35EOF
36
37cc -O2 -I src -o "$work/native_frame" "$work/native_frame.c" -L build -lvflutter
38LD_LIBRARY_PATH=build "$work/native_frame" "$work/native.bin"
39node tool/test_wasm.mjs "$work/wasm.bin" > /dev/null
40
41python3 - "$work/native.bin" "$work/wasm.bin" <<'EOF'
42import sys
43a = open(sys.argv[1], 'rb').read()
44b = open(sys.argv[2], 'rb').read()
45if len(a) != len(b):
46 sys.exit(f"FAIL size {len(a)} != {len(b)}")
47bad = [(i, x, y) for i, (x, y) in enumerate(zip(a, b)) if x != y]
48if bad:
49 worst = max(abs(x - y) for _, x, y in bad)
50 print(f"FAIL {len(bad)} of {len(a)} bytes differ (max delta {worst})")
51 for i, x, y in bad[:5]:
52 print(f" byte {i} (px {i//4}, ch {i%4}): native={x} wasm={y}")
53 sys.exit(1)
54print(f"ok native and wasm kernels agree on all {len(a)} bytes")
55EOF