Repository navigation
perf(filters): add bit-exact SSE2/LUT fast paths for invert0r, brightness, tint0r, bw0r, contrast0r, and gamma - #340
Open
Quantum-Quasar wants to merge 6 commits into
Conversation
added 6 commits
November 26, 2026 14:28
…ess fast path - Replace L1d-port-bound per-channel LUT loop with 4-pixel SSE2 16-bit integer arithmetic for brightness in [-256, 255], plus memcpy fast path when brightness == 0. - 1080p: 1.969 ms -> 0.804 ms (2.45x), 4K: 8.097 ms -> 3.882 ms (2.09x). - Verified 100% bit-exact (max_abs_diff = [0,0,0,0], exact SHA-256).
…ero-contrast fast path - Replace L1d-port-bound per-channel LUT loop with 4-pixel SSE2 16-bit branchless integer arithmetic for contrast in [-256, 255], plus memcpy fast path when contrast == 0. - 1080p: 1.961 ms -> 1.409 ms (1.39x), 4K: 8.036 ms -> 5.939 ms (1.35x). - Verified 100% bit-exact (max_abs_diff = [0,0,0,0], exact SHA-256).
… pixel stores - Precompute shifted uint32_t lut_r/lut_g/lut_b[256] in update_lut() and combine byte-indexed lookups into one 32-bit store per pixel, removing byte-store aliasing stalls. - 1080p: 1.978 ms -> 1.731 ms (1.14x), 4K: 8.018 ms -> 7.058 ms (1.14x). - Verified 100% bit-exact (max_abs_diff = [0,0,0,0], exact SHA-256).
- Replace byte-by-byte '255 - src[c]' loop (blocked from vectorization by
aliasing) with 4-pixel SSE2 _mm_xor_si128 using endian-independent
{0xFF, 0xFF, 0xFF, 0x00} mask.
- 1080p: 6.336 ms -> 0.584 ms (10.85x), 4K: 27.018 ms -> 2.893 ms (9.34x).
- Verified 100% bit-exact (max_abs_diff = [0,0,0,0], exact SHA-256).
…lhi /3 - Process 4 pixels per iteration in SSE2 using bit-masks/shifts (zero shuffles) and _mm_mulhi_epu16(sum, 21846), which is mathematically exact for (r+g+b)/3 across [0, 765]. - 1080p: 1.215 ms -> 0.866 ms (1.40x), 4K: 5.286 ms -> 3.435 ms (1.54x). - Verified 100% bit-exact (max_abs_diff = [0,0,0,0], exact SHA-256).
…nv255 LUT - Precompute exact float inv255_lut[256] in f0r_init() to eliminate 3 float divisions per pixel in tint_scalar. - Vectorize 4 pixels per iteration in SSE2: 4-wide float luma & tint term followed by 2x2-wide double blend/clamp/truncate matching tint_scalar IEEE-754 rounding bit-for-bit. - 1080p: 19.654 ms -> 7.77 ms (2.53x), 4K: 78.962 ms -> 31.08 ms (2.54x). - Verified 100% bit-exact (max_abs_diff = [0,0,0,0], exact SHA-256).
This branch has not been deployed
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.
Summary
Six bit-exact (
max_abs_diff = 0) optimizations across everyday tonal & utility filters:invert0r: Endian-safe SSE2 XOR byte inversion.brightness/contrast0r: Bit-exact SSE2 branchless arithmetic paths + identity fast paths.tint0r: Bit-exact SSE2 float/double path with precomputedinv255LUT.bw0r: Zero-shuffle SSE2 RGB average with exactmulhi /3.gamma: Pre-shifteduint32_tchannel LUTs with single 32-bit pixel stores.Benchmarks (3-Run Median, GCC 12.2.0
-O3 -DNDEBUG)invert0r0brightness0tint0r0bw0r0contrast0r0gamma0