Skip to content

Optimize ARM64 NEON LUT1D, LUT3D, RGBA pack, and diagonal matrix ops - #2367

Open
wrong-nebula wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
wrong-nebula:perf/neon-lut-pack-scale
Open

wrong-nebula wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
wrong-nebula:perf/neon-lut-pack-scale

Conversation

@wrong-nebula

Copy link
Copy Markdown

Summary

This PR optimizes OpenColorIO's ARM64 (OCIO_USE_SSE2NEON) hot CPU paths and diagonal matrix ScaleRenderer / ScaleWithOffsetRenderer loops while keeping outputs bit-exact across all bit depths, NaN/Inf edge cases, and odd pixel counts.

What Changed

  1. src/OpenColorIO/SSE2.h (SSE2RGBAPack under OCIO_USE_SSE2NEON)

    • BIT_DEPTH_UINT8: Single-instruction vqtbl1q_u8 RGBA deinterleave on Load; vcvtnq_u32_f32 + 2-level vsliq_n_u32 pack on Store (eliminates sse2neon's 2-instruction frint32x+fcvtzs on ARMv8.5+ and mrs fpcr + 4-way rounding-mode switch on baseline ARMv8.0-A, plus 3× ext.16b + 3× orr.16b). Because sse2_clamp guarantees finite inputs in [0, 255], vcvtnq_u32_f32 is bit-identical to default round-to-nearest-even _mm_cvtps_epi32.
    • SSE2RGBAPack16 (UINT10, UINT12, UINT16): vld4_u16 + vmovl_u16 on Load (removes 4×4 transpose); vcvtnq_u32_f32 + vsliq_n_u32(..., 16) + vzip1q/vzip2q on Store (hardware-ablated faster than vst4_u16 on Apple Silicon).
    • BIT_DEPTH_F16: vld4_u16 + vcvt_f32_f16 on Load and vcvt_f16_f32 + vst4_u16 on Store, eliminating the 4×4 transpose on both paths while remaining portable across Clang, GCC, and MSVC ARM64 (_M_ARM64).
    • BIT_DEPTH_F32: vld4q_f32 / vst4q_f32 replacing 4× load/store + sse2RGBATranspose_4x4.
  2. src/OpenColorIO/ops/lut1d/Lut1DOpCPU_SSE2.cpp (apply_lut_sse2 under OCIO_USE_SSE2NEON)

    • Removes stack-spilled indices_p/n[4] and buffer_p/n[4] gather buffers and computes next_i = (i < max_idx) ? i + 1 : i directly in GPR from extracted prev_i lanes (Max1DLUTLength = 300000 < 2^24), cutting NEON→GPR cross-domain transfers in half (24 → 12 per 4 pixels) and eliminating 3× float fadd/fminnm/fcvtzs chains.
  3. src/OpenColorIO/ops/lut3d/Lut3DOpCPU_SSE2.cpp (applyTetrahedralSSE2 under OCIO_USE_SSE2NEON)

    • Stores 16-byte voxel indices (no * 4.0f pre-scale) and uses vshll_n_u32(..., 4) in gather_rgb_neon, eliminating 2× fmul * 4.0f and 16× per-lane GPR lsl #2 shifts per 4 pixels.
    • Replaces sse2neon's 11-instruction ext.16b+zip1.2s+mov.d transpose sequence with a 7-instruction 3-channel vzip1q/vzip2q transpose.
    • Uses direct vbslq_f32 in blendv_ps_sse2 on full-lane comparison masks.
  4. src/OpenColorIO/ops/lut3d/Lut3DOpCPU.cpp (Lut3DRenderer::apply trilinear)

    • Uses _mm_loadu_ps(in) instead of 4-scalar _mm_set_ps.
    • Under OCIO_USE_SSE2NEON, computes the 8 cube-corner voxel offsets directly in GPR from (l0, l1, l2) via vrndmq_f32 + vcvtq_u32_f32 and loads via vld1q_f32(optLut + idx * 4), eliminating the SSE2-emulated split-multiply/shuffle GetLut3DIndices + LookupNearest4 path (~55 instructions/pixel).
  5. src/OpenColorIO/ops/matrix/MatrixOpCPU.cpp (ScaleRenderer, ScaleWithOffsetRenderer)

    • Adds explicit #if OCIO_USE_SSE2 128-bit SIMD loops (_mm_loadu_ps / _mm_mul_ps / _mm_add_ps / _mm_storeu_ps), fixing the compiler alias-check fallback to scalar 1-float-at-a-time execution on in-place (inImg == outImg) buffers across both ARM64 and x86.

Benchmarks (Apple M1, paired alternating dlopen A/B, 21 interleaved pairs, 16K RGBA pixels, median ± IQR)

Kernel Baseline (µs) Optimized (µs) Speedup
ScaleWithOffsetRenderer (in-place) 25.42 ± 3.16 6.06 ± 0.12 4.19×
ScaleWithOffsetRenderer (disjoint) 8.27 ± 0.04 6.06 ± 1.15 1.36×
ScaleRenderer (in-place) 18.21 ± 0.62 5.94 ± 0.17 3.07×
Lut3D Trilinear 33³ (Lut3DRenderer) 157.69 ± 11.79 73.62 ± 3.38 2.14×
SSE2RGBAPack<UINT8> Load+Store 14.06 ± 0.27 9.05 ± 0.11 1.55×
Lut1D F32→U8 (4096) 49.04 ± 0.98 33.31 ± 1.11 1.47×
SSE2RGBAPack<F16> Load+Store 8.07 ± 0.28 5.51 ± 0.19 1.46×
Lut1D F32→U16 (4096) 49.19 ± 0.90 34.05 ± 0.87 1.45×
Lut1D F32→F16 (4096) 46.93 ± 1.21 33.65 ± 0.73 1.40×
SSE2RGBAPack<UINT16> Load+Store 13.27 ± 0.20 9.69 ± 0.47 1.37×
Lut1D F32→F32 (4096) 45.52 ± 1.58 34.82 ± 0.50 1.31×
Lut3D Tetrahedral 33³ (applyTetrahedralSSE2) 77.18 ± 2.15 65.74 ± 1.43 1.17×

Verification

  • Bit-exactness: 0-ULP / byte-for-byte identical across all 6 Lut1D output bit depths (UINT8, UINT10, UINT12, UINT16, F16, F32), Lut3D Tetrahedral and Trilinear, all SSE2RGBAPack roundtrips, ScaleRenderer (in-place & disjoint), and ScaleWithOffsetRenderer (-ffp-contract=off), tested with NaN, ±Inf, -0.0, subnormals, out-of-range values, and odd remainder pixel counts (1, 2, 3, 4, 5, 7, 13, 1023, 16384).
  • Formal SMT equivalence: All 10 index/pack/permute/blend/conversion transformations verified UNSAT in Z3 over the full OCIO Max1DLUTLength (300000) and Max3DLUTLength (129) domains.
  • Sanitizers & warnings: Clean under -fsanitize=address,undefined -fno-sanitize-recover=all and -Wall -Wextra -Werror on both Apple Silicon and baseline -march=armv8-a.

Signed-off-by: wrong-nebula <263823322+wrong-nebula@users.noreply.github.com>
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

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