Skip to content

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
dyne:masterfrom
Quantum-Quasar:perf/sse2-tonal-filters
Open

Quantum-Quasar wants to merge 6 commits into
dyne:masterfrom
Quantum-Quasar:perf/sse2-tonal-filters

Conversation

@Quantum-Quasar

Copy link
Copy Markdown

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 precomputed inv255 LUT.
  • bw0r: Zero-shuffle SSE2 RGB average with exact mulhi /3.
  • gamma: Pre-shifted uint32_t channel LUTs with single 32-bit pixel stores.

Benchmarks (3-Run Median, GCC 12.2.0 -O3 -DNDEBUG)

Plugin 1080p Before → After 1080p Speedup 4K Before → After 4K Speedup Max Diff
invert0r 6.17 → 0.66 ms 9.39× 25.03 → 2.79 ms 8.96× 0
brightness 2.04 → 0.88 ms 2.33× 7.46 → 3.41 ms 2.19× 0
tint0r 14.68 → 7.68 ms 1.91× 57.64 → 31.15 ms 1.85× 0
bw0r 1.21 → 0.88 ms 1.37× 4.93 → 3.44 ms 1.43× 0
contrast0r 1.99 → 1.45 ms 1.37× 7.39 → 5.53 ms 1.34× 0
gamma 2.15 → 1.72 ms 1.25× 7.49 → 6.71 ms 1.12× 0

frei0r perf 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

No deployments
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