forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-h265simd': quantize SIMD + angular-intra hoist

Browse files at this commit

820bda29ad3a3fbec60f204ce510612aa8c21021Ahmet Kaan GümüşParent: cd61dc0f9a6938
quantize_offset in wide i32x8 lanes (bounds-proven bit-identical, no
i64 widen needed) and predict_angular's per-pixel branch and divisions
hoisted to once per row/column. Together -2.6% instructions on the
1080p encode loop; the pinned encode hash is unchanged.
Cargo.lockmodified+1 -0
@@ -329,6 +329,7 @@ version = "0.1.0"
329329 dependencies = [
330330 "ec-core",
331331 "ec-h265-syntax",
332+ "wide",
332333 ]
333334
334335 [[package]]
crates/ec-h265/Cargo.tomlmodified+1 -0
@@ -9,6 +9,7 @@ authors.workspace = true
99 [dependencies]
1010 ec-core.workspace = true
1111 ec-h265-syntax.workspace = true
12+wide.workspace = true
1213
1314 [dev-dependencies]
1415 ec-h265-syntax.workspace = true
crates/ec-h265/src/intra.rsmodified+39 -13
@@ -282,21 +282,47 @@ fn predict_angular(refs: &Refs, mode: u8, n: usize, is_luma: bool, out: &mut [u8
282282 reference[BIAS + x] = i32::from(main[x - 1]);
283283 }
284284 }
285- for y in 0..n {
286- // The "row" index along the prediction direction: y for the vertical
287- // modes, x for the horizontal ones, which is the whole difference
288- // between the two halves of 8.4.4.2.6.
285+ // `idx`/`fact` depend only on the "row" index along the prediction
286+ // direction (y for the vertical modes, x for the horizontal ones — the
287+ // whole difference between the two halves of 8.4.4.2.6), not on the
288+ // position across it. The mode>=18 branch and the idx/fact division used
289+ // to be recomputed at every one of the n*n pixels; hoisting them to one
290+ // per row/column turns the inner loop into a contiguous, near-linear scan
291+ // over `reference`, which is both fewer instructions and autovectorizes.
292+ if mode >= 18 {
293+ for y in 0..n {
294+ let idx = ((y as i32 + 1) * angle) >> 5;
295+ let fact = ((y as i32 + 1) * angle) & 31;
296+ let base = (BIAS as i32 + idx + 1) as usize;
297+ let row = &mut out[y * n..y * n + n];
298+ if fact != 0 {
299+ let (w0, w1) = (32 - fact, fact);
300+ for x in 0..n {
301+ row[x] =
302+ ((w0 * reference[base + x] + w1 * reference[base + x + 1] + 16) >> 5) as u8;
303+ }
304+ } else {
305+ for x in 0..n {
306+ row[x] = reference[base + x] as u8;
307+ }
308+ }
309+ }
310+ } else {
289311 for x in 0..n {
290- let (along, across) = if mode >= 18 { (y, x) } else { (x, y) };
291- let idx = ((along as i32 + 1) * angle) >> 5;
292- let fact = ((along as i32 + 1) * angle) & 31;
293- let base = (BIAS as i32 + across as i32 + idx + 1) as usize;
294- let value = if fact != 0 {
295- ((32 - fact) * reference[base] + fact * reference[base + 1] + 16) >> 5
312+ let idx = ((x as i32 + 1) * angle) >> 5;
313+ let fact = ((x as i32 + 1) * angle) & 31;
314+ let base = (BIAS as i32 + idx + 1) as usize;
315+ if fact != 0 {
316+ let (w0, w1) = (32 - fact, fact);
317+ for y in 0..n {
318+ out[y * n + x] =
319+ ((w0 * reference[base + y] + w1 * reference[base + y + 1] + 16) >> 5) as u8;
320+ }
296321 } else {
297- reference[base]
298- };
299- out[y * n + x] = value as u8;
322+ for y in 0..n {
323+ out[y * n + x] = reference[base + y] as u8;
324+ }
325+ }
300326 }
301327 }
302328 // The vertical and horizontal modes carry a boundary filter on luma
crates/ec-h265/src/transform.rsmodified+26 -2
@@ -365,11 +365,35 @@ pub fn quantize_offset(
365365 let qbits = 21 + qp / 6 - log2n;
366366 let scale = QUANT_SCALE[(qp % 6) as usize] as i64;
367367 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;
368376 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];
370394 let magnitude = ((c.unsigned_abs() as i64 * scale + offset) >> qbits) as i32;
371395 let level = magnitude.min(COEFF_MAX);
372- levels[i] = if c < 0 { -level } else { level };
396+ levels[j] = if c < 0 { -level } else { level };
373397 if level != 0 {
374398 nonzero += 1;
375399 }