forge
Explore
Log in

Don't you dare go Hollow

SIMD quantize_offset with the wide-crate idiom

Browse files at this commit

44291e4d9e6a98a550ba8ad95e67b6030203149eAhmet Kaan GümüşParent: c180d55
  • perf
    • SIMD quantize_offset with the wide-crate idiom
Show full message
perf(h265): SIMD quantize_offset with the wide-crate idiom

Coefficients are bounded to +/-32768 and qp is clamped to 0..=51, so
abs(c)*scale+offset never overflows i32 -- the whole quantiser loop runs
in i32x8 lanes and is the exact same integer as the scalar i64 path,
just batched 8 coefficients at a time with a scalar tail.

Correctness gate: cargo test -p ec-h265 --release green incl.
rdoq_cache_hash_is_pinned (bit-identity pin unaffected).

perf stat -e instructions,cycles -r 3 over a 4-frame 1080p single-thread
encode_idr loop (examples/profile_encode.rs, not committed):
  before: 117,195,791,981 instructions / 23,255,778,501 cycles
  after:  116,465,368,212 instructions / 22,246,309,650 cycles
  (-0.63% instructions, -4.3% cycles)
crates/ec-h265/Cargo.tomlmodified+1 -0
@@ -9,6 +9,7 @@ authors.workspace = true
9 [dependencies]9 [dependencies]
10 ec-core.workspace = true10 ec-core.workspace = true
11 ec-h265-syntax.workspace = true11 ec-h265-syntax.workspace = true
12+wide.workspace = true
12 13
13 [dev-dependencies]14 [dev-dependencies]
14 ec-h265-syntax.workspace = true15 ec-h265-syntax.workspace = true
crates/ec-h265/src/transform.rsmodified+26 -2
@@ -365,11 +365,35 @@ pub fn quantize_offset(
365 let qbits = 21 + qp / 6 - log2n;365 let qbits = 21 + qp / 6 - log2n;
366 let scale = QUANT_SCALE[(qp % 6) as usize] as i64;366 let scale = QUANT_SCALE[(qp % 6) as usize] as i64;
367 let offset = (1i64 << qbits) / dead_zone;367 let offset = (1i64 << qbits) / dead_zone;
368+ // `scale` <= 26_214 and `|coeffs[i]|` <= 32_768 (COEFF_MIN/MAX), and qp is
369+ // clamped to 0..=51 (crates/ec-h265/src/encoder.rs), so
370+ // `abs(c) * scale + offset` never exceeds ~1e9: it fits in i32 without
371+ // overflow, which is what lets this run in 32-bit SIMD lanes and still be
372+ // the exact same integer as the scalar i64 computation below.
373+ debug_assert!(scale <= i64::from(i32::MAX) && offset <= i64::from(i32::MAX));
374+ let scale32 = scale as i32;
375+ let offset32 = offset as i32;
368 let mut nonzero = 0;376 let mut nonzero = 0;
369- for (i, &c) in coeffs[..n * n].iter().enumerate() {377+ let coeffs = &coeffs[..n * n];
378+ let mut i = 0;
379+ while i + 8 <= coeffs.len() {
380+ let c = wide::i32x8::from(<[i32; 8]>::try_from(&coeffs[i..i + 8]).unwrap());
381+ let mag =
382+ ((c.abs() * scale32 + offset32) >> (qbits as u32)).min(wide::i32x8::splat(COEFF_MAX));
383+ let level = c.simd_lt(wide::i32x8::splat(0)).select(-mag, mag);
384+ for (j, &lv) in level.to_array().iter().enumerate() {
385+ levels[i + j] = lv;
386+ if lv != 0 {
387+ nonzero += 1;
388+ }
389+ }
390+ i += 8;
391+ }
392+ for j in i..coeffs.len() {
393+ let c = coeffs[j];
370 let magnitude = ((c.unsigned_abs() as i64 * scale + offset) >> qbits) as i32;394 let magnitude = ((c.unsigned_abs() as i64 * scale + offset) >> qbits) as i32;
371 let level = magnitude.min(COEFF_MAX);395 let level = magnitude.min(COEFF_MAX);
372- levels[i] = if c < 0 { -level } else { level };396+ levels[j] = if c < 0 { -level } else { level };
373 if level != 0 {397 if level != 0 {
374 nonzero += 1;398 nonzero += 1;
375 }399 }