summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 22:31:10 -0300
committerGabriel Schneider <[email protected]>2026-11-20 00:39:13 -0300
commit406f34120e3013c1900abfadb7a812dfd54ba89e (patch)
treef7ddf1f045c8bdfb71ecf42f20658e885481df62 /docs
parentdcf6d58f8aacefd9a66817ef4589f68ce2d42e8a (diff)
downloadpardes-406f34120e3013c1900abfadb7a812dfd54ba89e.tar.gz
pardes-406f34120e3013c1900abfadb7a812dfd54ba89e.zip
build: no fused multiply-add in zstbi's resize (build/patches.zig `zstbi_cut`: `#pragma STDC FP_CONTRACT OFF`, docs/zstbi-fp-contract.md), so a kitty PNG over 1280 pixels downscales to the same pixels on an Apple silicon Mac as on Linux and FreeBSD, as a client that replays its session's bytes needs: clang contracts `a * b + c` where the CPU has a fused multiply-add, aarch64 always does (the Mac binary had 98 fmadd/fmla in stbir__resize_arbitrary and stbir__resample_horizontal_downsample; the x86-64 ones have none), a fused one rounds once, and the two downscaled pictures of the cross-check's PNG stream came out other pixels on the Mac (the snapshot differed; images are not in the digest) while the one under the bound and raw RGBA did not; with the pragma the Mac's are Linux's; the proof, the cross-check driver built from this tree (and the change before) with no patch of its own (only its step added to a scratch copy): its 18 lines (eight streams by two read sizes, two at 7 bytes: digest and full snapshot after two resizes) the same on Linux x86-64, FreeBSD 15.1 amd64 and the Mac (Darwin 25.6 arm64) (.scratch/sketch/reports/pardes-term-xcheck.md, "Landed"); no zstbi or stb upstream is on this machine to compare; tui term-test 15/15, kitty-test 45/45, core-test 848/991 (143 skipped), shell-test 1027/1030, image-harness and kitty-harness pass; gui unit-test 1625/1629 (4 skipped; the perf check's BaselineHarnessMismatch, the base's), image-harness and kitty-harness pass; the macOS library builds for Linux and aarch64-macos, the FreeBSD tui builds (load 19 to 47); the frame and perf gate (gpu/xcheck-c3, against tree1, with the change before): 77/77 frames byte-identical, perfab corpus2x -0.076%, perfops 97 of 97 within 0.1%
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
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.