forge
Explore
Log in

Don't you dare go Hollow

Gather a coefficient's context neighbours without per-neighbour edge tests

Browse files at this commit

02bd8ae4cb9ad7bf47b3311b419939e091058cbaAhmet Kaan GümüşParent: fb3fa69
  • perf
    • gather a coefficient's context neighbours without per-neighbour edge tests
Show full message
perf(av1): gather a coefficient's context neighbours without per-neighbour edge tests

`neighbour` was a third of `write_coeffs`'s self time: five (base) or three
(base-range) loads, each behind its own pair of edge compares plus a slice
bound check, for every coefficient of every candidate the search prices. A
coefficient clear of the far edges reaches all of them, so that case indexes
straight off the position. Byte-exact on all twelve ladder points; 1080p
ladder -0.71% instructions:u (110.95G -> 110.15G, ABAB x2).
crates/ec-av1/src/tile.rsmodified+25 -8
@@ -2473,10 +2473,22 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2473 if row == 0 && col == 0 {2473 if row == 0 && col == 0 {
2474 return 0;2474 return 0;
2475 }2475 }
2476- let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]2476+ // A coefficient two rows and two columns clear of the far edges reaches
2477- .iter()2477+ // all five neighbours, so the gather is five loads off one index with no
2478- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))2478+ // per-neighbour edge test -- which is where this writer spends a third of
2479- .sum();2479+ // its coefficient loop.
2480+ let p = row * side + col;
2481+ let mag: i32 = if row + 2 < side && col + 2 < side {
2482+ [p + side, p + 1, p + side + 1, p + 2 * side, p + 2]
2483+ .iter()
2484+ .map(|&i| grid[i].abs().min(3))
2485+ .sum()
2486+ } else {
2487+ [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]
2488+ .iter()
2489+ .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))
2490+ .sum()
2491+ };
2480 let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize;2492 let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize;
2481 (((mag + 1) >> 1).min(4) as usize) + offset2493 (((mag + 1) >> 1).min(4) as usize) + offset
2482 }2494 }
@@ -2485,10 +2497,15 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2485 /// of its three closest neighbours below and to the right, uncapped, and a2497 /// of its three closest neighbours below and to the right, uncapped, and a
2486 /// term separating the DC, the corner of the transform, and the rest.2498 /// term separating the DC, the corner of the transform, and the rest.
2487 fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {2499 fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2488- let mag: i32 = [(1, 0), (0, 1), (1, 1)]2500+ let p = row * side + col;
2489- .iter()2501+ let mag: i32 = if row + 1 < side && col + 1 < side {
2490- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs())2502+ grid[p + side].abs() + grid[p + 1].abs() + grid[p + side + 1].abs()
2491- .sum();2503+ } else {
2504+ [(1, 0), (0, 1), (1, 1)]
2505+ .iter()
2506+ .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs())
2507+ .sum()
2508+ };
2492 let mag = (((mag + 1) >> 1).min(6)) as usize;2509 let mag = (((mag + 1) >> 1).min(6)) as usize;
2493 if row == 0 && col == 0 {2510 if row == 0 && col == 0 {
2494 mag2511 mag