Repository navigation
perf(images): vectorize YCbCr/YCCK to RGB/CMYK rows with Vector128 (#471) - #490
Merged
Merged
Conversation
) - 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.
This was referenced Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #471.
What
JpegColor.YccToRgbandYcckToCmyk(the 8-bit DCT colour transforms, also behindYCbCrConversion.ToRgb) dispatch to aVector128path when it is accelerated (src/Broadside/Filters/Dct/JpegColor.Vector128.cs); the old loops areYccToRgbScalar/YcckToCmykScalarand finish each row. 12-bit paths are unchanged.& 0xFFFF,>>> 16), the-128 Kand 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-bitNarrow. Luma is added in 16-bit lanes, thenMin/Max,Narrowand constant byte shuffles to interleave. CMYK is255 ^the RGB result (the clamp is symmetric), K copied.JpegColorKernelTests(SimdKernelcategory, 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.JpegColorBenchmarks(inDctBenchmarks.cs) reads-for all four cases. The new cases are added tobench/baselines/gate/allocations.jsonat 0. No existing entry changes.DctAllocationTestsstill pass.KernelBenchmarkshasycc-to-rgbandycck-to-cmykcases.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-wasmconsole app under Node (both builds, local). In the AOT build, theVector128.ShiftRightLogical/ShiftLeft/ShiftRightArithmeticmethods 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.Castremoved, orNarrowreplaced by shuffles stayed at 0.62–0.65x until the shifts changed. With the operators it is 2.9x faster than scalar.SimdExampleKernelnow uses the operators.The interpreter has its own trap.
Vector128.Create(span)/CopyTo(span)cost 3.4xLoadUnsafe/StoreUnsafethere (6.2 against 1.8 ns per vector), which halves a kernel's gain. Both kernels therefore useLoadUnsafe/StoreUnsafeon 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)
blazor-wasm job:
BenchmarkDotNet,
JpegColorBenchmarks, one 2,550-sample row, JIT, M4 Max:DctBenchmarks.Decode,--job short, base → head (M4 Max):In-process toolchain,
Allocated-on both sides:Verification
dotnet test --project tests/Broadside.Tests -c Release -- --filter-trait "Category=SimdKernel": 8 passed, and 8 passed again withDOTNET_EnableHWIntrinsic=0.Broadside.Tests.Filters.Dct.*andYCbCrConversionTests: 286 passed.-warnaserror. CI runs the rest.