Skip to content

perf(images): vectorize YCbCr/YCCK to RGB/CMYK rows with Vector128 (#471) - #490

Merged
michaelstonis merged 2 commits into
spec/33-broadside-v1from
471-simd-ycbcr-rows
Nov 10, 2026
Merged

michaelstonis merged 2 commits into
spec/33-broadside-v1from
471-simd-ycbcr-rows

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Closes #471.

What

  • JpegColor.YccToRgb and YcckToCmyk (the 8-bit DCT colour transforms, also behind YCbCrConversion.ToRgb) dispatch to a Vector128 path when it is accelerated (src/Broadside/Filters/Dct/JpegColor.Vector128.cs); the old loops are YccToRgbScalar/YcckToCmykScalar and finish each row. 12-bit paths are unchanged.
  • The arithmetic is libjpeg's 16-bit fixed point in 32-bit lanes: each widened 16-bit chroma lane splits into an even and an odd 32-bit lane (& 0xFFFF, >>> 16), the -128 K and rounding terms fold into one bias, and the two results OR back into 16-bit lanes in sample order (even >>> 16, odd & 0xFFFF0000), with no 32-bit Narrow. Luma is added in 16-bit lanes, then Min/Max, Narrow and constant byte shuffles to interleave. CMYK is 255 ^ the RGB result (the clamp is symmetric), K copied.
  • Bit-exact: JpegColorKernelTests (SimdKernel category, so CI reruns it with hardware intrinsics off) compares vector and dispatcher with scalar on all 2^24 (Y, Cb, Cr) triples, every short length 0–70 and offset 0–15, with an overrun guard. YCCK uses the same triples with K varying alongside, plus random 4-channel rows.
  • No allocations: JpegColorBenchmarks (in DctBenchmarks.cs) reads - for all four cases. The new cases are added to bench/baselines/gate/allocations.json at 0. No existing entry changes. DctAllocationTests still pass.
  • Browser: the kernel is linked into the Blazor sample, and KernelBenchmarks has ycc-to-rgb and ycck-to-cmyk cases.
  • The SIMD section of the adding-a-benchmark skill now has the both-builds rule (no speed-up below 1 in either browser build; gate per platform rather than regress) and the measured operation subset. Numbers are in docs/benchmarks/simd-kernels.md. The conformance row for §7.4.8 lists the new test.

Why #470's example was slower than scalar in the browser AOT build

I timed single operations in a browser-wasm console app under Node (both builds, local). In the AOT build, the Vector128.ShiftRightLogical / ShiftLeft / ShiftRightArithmetic methods cost 3.3–3.4 ns per vector. That is about 8x any other operation (0.2–0.4 ns) and 3.6x the >>>/<</>> operators (0.9 ns). The methods do not become a single Wasm SIMD shift there. The example kernel does six per 16 items. The three suspects were not the cause: every variant with span loads, LoadUnsafe, MemoryMarshal.Cast removed, or Narrow replaced by shuffles stayed at 0.62–0.65x until the shifts changed. With the operators it is 2.9x faster than scalar. SimdExampleKernel now uses the operators.

The interpreter has its own trap. Vector128.Create(span)/CopyTo(span) cost 3.4x LoadUnsafe/StoreUnsafe there (6.2 against 1.8 ns per vector), which halves a kernel's gain. Both kernels therefore use LoadUnsafe/StoreUnsafe on refs, after slicing the spans to the row so every access is in bounds. No per-platform gate is needed: the vector path is faster in all four builds.

Numbers (ns per pixel, scalar → vector)

Build YCbCr → RGB YCCK → CMYK example-multiply
JIT (M4 Max) 1.398 → 0.415 (3.37x) 1.521 → 0.475 (3.20x) 8.08x
Native AOT (M4 Max) 1.732 → 0.416 (4.17x) 1.885 → 0.483 (3.91x) 8.42x
Browser interpreter (CI) 22.221 → 5.054 (4.40x) 26.512 → 6.104 (4.34x) 8.30x
Browser AOT (CI) 11.730 → 2.480 (4.73x) 14.687 → 2.861 (5.13x) 3.58x (#470: 0.56x)
Browser interpreter (Node, local) 8.974 → 4.012 (2.24x) 10.758 → 4.595 (2.34x) 6.81x
Browser AOT (Node, local) 3.309 → 0.715 (4.63x) 3.758 → 0.785 (4.79x) 2.94x (was 0.63x)

blazor-wasm job:

kernel aot example-multiply: scalar 1.836 ns/item, vector 0.513 ns/item, speed-up 3.58x, equal yes, accelerated true
kernel aot ycc-to-rgb: scalar 11.730 ns/item, vector 2.480 ns/item, speed-up 4.73x, equal yes, accelerated true
kernel aot ycck-to-cmyk: scalar 14.687 ns/item, vector 2.861 ns/item, speed-up 5.13x, equal yes, accelerated true
kernel interpreter example-multiply: scalar 10.681 ns/item, vector 1.287 ns/item, speed-up 8.30x, equal yes, accelerated true
kernel interpreter ycc-to-rgb: scalar 22.221 ns/item, vector 5.054 ns/item, speed-up 4.40x, equal yes, accelerated true
kernel interpreter ycck-to-cmyk: scalar 26.512 ns/item, vector 6.104 ns/item, speed-up 4.34x, equal yes, accelerated true

BenchmarkDotNet, JpegColorBenchmarks, one 2,550-sample row, JIT, M4 Max:

Method Mean Allocated
YccToRgbScalar 3.742 µs -
YccToRgb 1.089 µs -
YcckToCmykScalar 3.835 µs -
YcckToCmyk 1.252 µs -

DctBenchmarks.Decode, --job short, base → head (M4 Max):

In-process toolchain, Allocated - on both sides:

Vector Base Head Change
testorig (4:2:0) 178.3 µs 143.8 µs -19%
sampling-444 25.5 µs 22.1 µs -13%
restart-blocks 179.8 µs 143.8 µs -20%
multiscan 20.0 µs 16.2 µs -19%
progressive 243.1 µs 208.3 µs -14%
arithmetic-progressive 462.0 µs 421.9 µs -9%
ycck 27.9 µs 24.4 µs -12%
gray-2x2 (no transform) 9.30 µs 9.26 µs 0%

Verification

  • dotnet test --project tests/Broadside.Tests -c Release -- --filter-trait "Category=SimdKernel": 8 passed, and 8 passed again with DOTNET_EnableHWIntrinsic=0.
  • All Broadside.Tests.Filters.Dct.* and YCbCrConversionTests: 286 passed.
  • The Blazor sample builds with -warnaserror. CI runs the rest.

)

- JpegColor.YccToRgb and YcckToCmyk dispatch to a Vector128 path
  (JpegColor.Vector128.cs) when it is accelerated: libjpeg's 16-bit fixed
  point in 32-bit lanes (even/odd split and repack into 16-bit lanes),
  clamp with Min/Max, Narrow, constant byte shuffles to interleave.
  Bit-exact with the scalar transform on all 2^24 (Y, Cb, Cr) triples.
- Only operations that are fast in every build: LoadUnsafe/StoreUnsafe
  (span Create/CopyTo cost the browser interpreter 3.4x) and the shift
  operators. The Vector128.ShiftRightLogical/ShiftLeft/ShiftRightArithmetic
  methods cost about 8x in the browser AOT build, which is why #470's
  example kernel was slower than scalar there; it now uses the operators.
- JpegColorKernelTests (SimdKernel category), JpegColorBenchmarks, browser
  KernelBenchmarks cases, docs/benchmarks/simd-kernels.md, and the both-builds
  rule in the adding-a-benchmark skill.
@michaelstonis
michaelstonis merged commit 18e176c into spec/33-broadside-v1 Nov 10, 2026
31 checks passed
@michaelstonis
michaelstonis deleted the 471-simd-ycbcr-rows branch November 10, 2026 20:25
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