summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build/patches.zig8
-rw-r--r--docs/zstbi-fp-contract.md66
2 files changed, 73 insertions, 1 deletions
diff --git a/build/patches.zig b/build/patches.zig
index f58fb926..3d956d26 100644
--- a/build/patches.zig
+++ b/build/patches.zig
@@ -175,8 +175,14 @@ pub fn vaxis(b: *std.Build, patch: *std.Build.Step.Compile, vaxis_dep: *std.Buil
/// zstbi with stb_image and its writer cut out: of it only its resize is
/// left, for a terminal PNG's downscale (build/core.zig, "One PNG
/// decoder"). zstbi.deinit still resets the reader's and the writer's flip
-/// flags, and Image.deinit frees the resize's output.
+/// flags, and Image.deinit frees the resize's output. And no fused
+/// multiply-add in the resize: clang contracts `a * b + c` where the CPU
+/// has one (aarch64 always does), which rounds once, so a PNG downscaled
+/// on an Apple silicon Mac came out pixels apart from Linux's, and the same
+/// bytes must give the same terminal on every machine a client runs on
+/// (docs/zstbi-fp-contract.md).
const zstbi_cut = [_]Edit{
+ .{ .old = "#include <stdlib.h>\n", .new = "#include <stdlib.h>\n#pragma STDC FP_CONTRACT OFF\n" },
.{ .old = "#define STB_IMAGE_IMPLEMENTATION\n#include \"stb_image.h\"\n", .new = "void stbi_set_flip_vertically_on_load(int flag) { (void)flag; }\nvoid stbi_image_free(void *p) { zstbiFreePtr(p); }\n" },
.{ .old = "#define STB_IMAGE_WRITE_IMPLEMENTATION\n#include \"stb_image_write.h\"\n", .new = "void stbi_flip_vertically_on_write(int flag) { (void)flag; }\n" },
};
diff --git a/docs/zstbi-fp-contract.md b/docs/zstbi-fp-contract.md
new file mode 100644
index 00000000..95c067fd
--- /dev/null
+++ b/docs/zstbi-fp-contract.md
@@ -0,0 +1,66 @@
+# stb_image_resize: a downscale's pixels depend on the CPU's fused multiply-add
+
+A report for upstream (https://github.com/zig-gamedev/zstbi, which vendors
+nothings/stb's `stb_image_resize.h` v0.97), against the commit pardes pins
+(6b9bea1f, `build.zig.zon`). pardes patches its fetched copy at build time
+(build/patches.zig, `zstbi_cut`) until it is fixed there, if upstream wants
+it fixed.
+
+## What happens
+
+pardes downscales a kitty graphics PNG over 1280 pixels with zstbi's
+`stbir_resize_uint8` (core/term/TerminalImages.zig, `decodePng`). The
+resize is plain C on floats, with no SIMD of its own, and clang compiles C
+with `-ffp-contract=on`: where the target has a fused multiply-add,
+`a * b + c` becomes one instruction that rounds once instead of twice.
+aarch64 always has one; the x86-64 targets pardes builds for here do not
+use one. The aarch64 macOS binary has 66 `fmadd`/`fmla` in
+`stbir__resize_arbitrary` and 32 in `stbir__resample_horizontal_downsample`;
+the x86-64 Linux and FreeBSD binaries have no `vfmadd` anywhere. So the same
+PNG downscales to pixels a step apart on an Apple silicon Mac.
+
+pardes meets it because a client of a session runs its own emulator on the
+pty's bytes: a Mac client of a Linux session must end with the session's
+terminal, and a join compares the snapshot (which holds each image's
+pixels) of the two.
+
+## Measured
+
+Three PNGs sent by direct data (`f=100`): 1601x1203 (downscaled to
+1280x961), 700x2001 (to 447x1280) and 333x257 (not downscaled). On Linux
+x86-64 (SIMD on and off) and FreeBSD 15.1 amd64 the three images' pixels
+were the same; on the Mac (Darwin 25.6, arm64) the two downscaled ones
+differed and the third was the same, as were two 1024x768 images sent as
+raw RGBA (`f=32`). pardes's per-unit digest does not cover images, so only
+the snapshot differed.
+
+## The fix
+
+Contraction off for the whole translation unit, at the top of zstbi's C file:
+
+```diff
+--- a/src/zstbi.c
++++ b/src/zstbi.c
+@@ -1,3 +1,4 @@
+ #include <stdlib.h>
++#pragma STDC FP_CONTRACT OFF
+```
+
+(`-ffp-contract=off` in zstbi's build flags is the same.) With it the Mac
+binary has no fused multiply-add in `stbir_*`, and the Mac's pixels are
+Linux's: the three images, and the snapshot (with the page fix of
+docs/ghostty-page-size.md, every stream's).
+
+## What it costs
+
+A multiply and an add where there was one instruction, in a resize made
+once per image sent (a 1601x1203 picture's), on aarch64 only: not
+measured, as nothing else of pardes runs it.
+
+## Upstream today
+
+Not read beyond what is on this machine (no network): no copy of zstbi or
+stb is in `~/05-genizah`. The three zstbi copies in `zig-pkg/` (the pin and
+two others) all vendor `stb_image_resize.h` v0.97 with no pragma. stb's own
+successor, `stb_image_resize2.h`, has SIMD paths of its own; moving to it
+would be another question of the same kind, not a fix of this one.