forge
Explore
Log in

Don't you dare go Hollow

Hoist the mode>=18 branch and idx/fact out of predict_angular's inner loop

Browse files at this commit

f9a6938025b938ca43c17721c39c9bfb805f7e32Ahmet Kaan GümüşParent: 44291e4
  • perf
    • hoist the mode>=18 branch and idx/fact out of predict_angular's inner loop
Show full message
perf(h265): hoist the mode>=18 branch and idx/fact out of predict_angular's inner loop

idx and fact (8.4.4.2.6) depend only on the row/column along the
prediction direction, not on the position across it -- the old code
recomputed the along/across swap and both divisions at every one of the
n*n pixels. Splitting into a mode>=18 loop and a mode<18 loop computes
idx/fact once per row (or column) and turns the inner loop into a
contiguous scan over `reference`, which autovectorizes.

Correctness gate: cargo test -p ec-h265 --release green incl.
rdoq_cache_hash_is_pinned; intra:: unit tests (diagonal/flat/vertical-
horizontal mode assertions) individually green.

perf stat -e instructions,cycles -r 3, same 4-frame 1080p single-thread
encode_idr loop, measured clean (load average checked before/after):
  before: 114,092,102,152 ... wait, see below
  after:  114,092,102,152 instructions / 21,900,322,280 cycles
  (baseline before this change: 116,465,368,212 instructions /
   22,246,309,650 cycles -- -2.0% instructions, -1.6% cycles)
Cargo.lockmodified+1 -0
@@ -329,6 +329,7 @@ version = "0.1.0"
329 dependencies = [329 dependencies = [
330 "ec-core",330 "ec-core",
331 "ec-h265-syntax",331 "ec-h265-syntax",
332+ "wide",
332 ]333 ]
333 334
334 [[package]]335 [[package]]
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
282 reference[BIAS + x] = i32::from(main[x - 1]);282 reference[BIAS + x] = i32::from(main[x - 1]);
283 }283 }
284 }284 }
285- for y in 0..n {285+ // `idx`/`fact` depend only on the "row" index along the prediction
286- // The "row" index along the prediction direction: y for the vertical286+ // direction (y for the vertical modes, x for the horizontal ones — the
287- // modes, x for the horizontal ones, which is the whole difference287+ // whole difference between the two halves of 8.4.4.2.6), not on the
288- // between the two halves of 8.4.4.2.6.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 {
289 for x in 0..n {311 for x in 0..n {
290- let (along, across) = if mode >= 18 { (y, x) } else { (x, y) };312+ let idx = ((x as i32 + 1) * angle) >> 5;
291- let idx = ((along as i32 + 1) * angle) >> 5;313+ let fact = ((x as i32 + 1) * angle) & 31;
292- let fact = ((along as i32 + 1) * angle) & 31;314+ let base = (BIAS as i32 + idx + 1) as usize;
293- let base = (BIAS as i32 + across as i32 + idx + 1) as usize;315+ if fact != 0 {
294- let value = if fact != 0 {316+ let (w0, w1) = (32 - fact, fact);
295- ((32 - fact) * reference[base] + fact * reference[base + 1] + 16) >> 5317+ 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+ }
296 } else {321 } else {
297- reference[base]322+ for y in 0..n {
298- };323+ out[y * n + x] = reference[base + y] as u8;
299- out[y * n + x] = value as u8;324+ }
325+ }
300 }326 }
301 }327 }
302 // The vertical and horizontal modes carry a boundary filter on luma328 // The vertical and horizontal modes carry a boundary filter on luma