summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
Diffstat (limited to 'docs')
-rw-r--r--docs/zstbi-fp-contract.md66
1 files changed, 66 insertions, 0 deletions
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.