Skip to content

perf(jpx): fuse and vectorize the 9/7 rounding with the DC level shift (#503) - #504

Merged
michaelstonis merged 2 commits into
spec/33-broadside-v1from
503-jpx-round-simd
Nov 12, 2026
Merged

michaelstonis merged 2 commits into
spec/33-broadside-v1from
503-jpx-round-simd

Conversation

@michaelstonis

Copy link
Copy Markdown
Owner

Closes #503.

After #477 the 9/7 rounding before the DC level shift (JpxDecoder.RoundToIntegers) was 10% of pdfium-tests/fx/action/123.pdf's render, more than the wavelet. It was a full scalar pass writing integers back in place, and JpxPlane.Write then read the buffer again with 64-bit arithmetic and a column check per sample.

Approach

  • One pass per row: column clipping worked out once per tile-component; for components of at most 16 bits the rounding is fused into the shift and clip (JpxLevelShift.RoundShift: round(clamp(v, -shift, max - shift)) + shift, NaN as 0; rounding is monotone and fixes integers, so clamping first gives the old result). The 5/3 path (Shift) clamps before adding so the 32-bit sum cannot overflow. Components deeper than 16 bits keep the old rounding and 64-bit path.
  • Vector128 path, 8 samples per iteration: NaN masked with Equals(x, x), Max/Min clamp, rounding by adding 1.5 x 2^23 (the add itself rounds to nearest, ties to even), packed to 16 bits with two constant byte Shuffles (no 32-bit Narrow). Scalar fallback by Vector128.IsHardwareAccelerated.

Bit-exact

  • JpxLevelShiftKernelTests (SimdKernel trait, also passes with DOTNET_EnableHWIntrinsic=0), depths 1 to 16: vector and dispatcher vs scalar, and scalar vs the replaced round-then-shift code, over special values (NaN payloads, infinities, signed zeros, denormals, extremes, every half-integer around and between the bounds with neighbors one ulp away) and 4 million random floats; 5/3 over every integer in [-70,000, 70,000], the 32-bit extremes and a million random integers.
  • All 153 JPX tests pass, RealWorldJpxTests against the fetched corpora (jpx-corpus-references.txt) included. Decoded samples of 150 dumped JPX streams and vectors at reductions 0 to 2 (212 SHA-256) and every page of the 25 JPX corpus files at 72 and 150 DPI hash identically before and after.

Speed (ns/sample, 64 Ki samples, before / scalar / vector; local, Node 22 for the browser builds)

Build 9/7 5/3
JIT 1.248 / 0.710 / 0.171 0.651 / 0.575 / 0.128
Native AOT 1.525 / 0.733 / 0.176 0.874 / 0.801 / 0.128
Browser interpreter 20.986 / 19.446 / 1.559 4.223 / 3.376 / 1.136
Browser AOT 3.223 / 2.917 / 0.542 0.671 / 0.752 / 0.211

BenchmarkDotNet JpxLevelShiftBenchmarks (JIT, short): RoundThenShift 95.9 µs, RoundShift 11.6 µs, Shift 8.5 µs, Allocated -. No build is gated. The jpx-shift and jpx-round-shift KernelBenchmarks lines from the blazor-wasm job are in a comment below.

Whole render (best pass ms, every page at 96 DPI, documents opened afresh): 123.pdf Native AOT 51.7 to 41.4 (-20%), browser AOT 96.9 to 75.6 (-22%), interpreter 1,088 to 956 (-12%); scan-moon-memo-jpx.pdf -25% / -26% / -22%; #352's pages (issue5567, issue5481, issue5549) -2% to -6%, being 87-91% tier-1. Details in docs/benchmarks/simd-kernels.md.

@michaelstonis

Copy link
Copy Markdown
Owner Author

blazor-wasm job (run 43130314820):

kernel aot jpx-round-shift: scalar 10.681 ns/item, vector 1.144 ns/item, speed-up 9.33x, equal yes, accelerated true
kernel aot jpx-shift: scalar 2.801 ns/item, vector 0.262 ns/item, speed-up 10.68x, equal yes, accelerated true
kernel interpreter jpx-round-shift: scalar 85.449 ns/item, vector 4.196 ns/item, speed-up 20.36x, equal yes, accelerated true
kernel interpreter jpx-shift: scalar 7.200 ns/item, vector 2.694 ns/item, speed-up 2.67x, equal yes, accelerated true

@michaelstonis
michaelstonis merged commit 982bea2 into spec/33-broadside-v1 Nov 12, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant