forge
Explore
Log in

Don't you dare go Hollow

Resolve the deblock kernels' bit-depth reads once per call

Browse files at this commit

801fe73e920a29f6052bc005df88f97285043276Ahmet Kaan GümüşParent: 583ed90
  • perf
    • resolve the deblock kernels' bit-depth reads once per call
Show full message
perf(av1): resolve the deblock kernels' bit-depth reads once per call

`filter4` runs once per deblocked sample and read the thread-local bit
depth nine times doing it -- once for its centring constant and once
inside each of `sclamp`'s eight calls -- while `filter_edge` re-read it
through `sample_max()` on all fifteen of its stores. Both are constant
for the whole call; same bounds, read once.

Byte-exact on seg1080/seg4k/seg4k_lr/palette; 4K 60.53 s -> 48.33 s
(1.252x, from 1.221x at the previous step), 1080p 10.74 s.
crates/ec-av1/src/decode.rsmodified+22 -16
@@ -15996,7 +15996,12 @@ fn filter4(mask: bool, thresh: i32, p1: i32, p0: i32, q0: i32, q1: i32) -> [i32;
1599615996 return [p1, p0, q0, q1];
1599715997 }
1599815998 let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh;
15999- let centre = 128i32 << (bit_depth() - 8);
15999+ // lane-perf6: `sclamp` re-read the thread-local bit depth on every one of
16000+ // this kernel's eight clamps, and this kernel runs per deblocked sample.
16001+ // Same bound, resolved once.
16002+ let scale = 1i32 << (bit_depth() - 8);
16003+ let sclamp = |v: i32| v.clamp(-128 * scale, 128 * scale - 1);
16004+ let centre = 128i32 * scale;
1600016005 let (ps1, ps0, qs0, qs1) = (p1 - centre, p0 - centre, q0 - centre, q1 - centre);
1600116006 let outer = if hev { sclamp(ps1 - qs1) } else { 0 };
1600216007 let filter = sclamp(outer + 3 * (qs0 - ps0));
@@ -16164,6 +16169,7 @@ fn filter_edge(
1616416169 let shift = bit_depth() - 8;
1616516170 let (blimit, limit, hev_thr) = (blimit << shift, limit << shift, hev_thr << shift);
1616616171 let flat_thresh = 1i32 << shift;
16172+ let smax = sample_max();
1616716173 for i in 0..4isize {
1616816174 let center = (base as isize + i * outer_stride) as usize;
1616916175 let idx = |k: isize| -> usize { (center as isize + k * tap_stride) as usize };
@@ -16175,10 +16181,10 @@ fn filter_edge(
1617516181 && (q1 - q0).abs() <= limit
1617616182 && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit;
1617716183 let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1);
16178- data[idx(-2)] = op1.clamp(0, sample_max()) as u16;
16179- data[idx(-1)] = op0.clamp(0, sample_max()) as u16;
16180- data[idx(0)] = oq0.clamp(0, sample_max()) as u16;
16181- data[idx(1)] = oq1.clamp(0, sample_max()) as u16;
16184+ data[idx(-2)] = op1.clamp(0, smax) as u16;
16185+ data[idx(-1)] = op0.clamp(0, smax) as u16;
16186+ data[idx(0)] = oq0.clamp(0, smax) as u16;
16187+ data[idx(1)] = oq1.clamp(0, smax) as u16;
1618216188 }
1618316189 6 => {
1618416190 let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2));
@@ -16192,10 +16198,10 @@ fn filter_edge(
1619216198 && (p2 - p0).abs() <= flat_thresh
1619316199 && (q2 - q0).abs() <= flat_thresh;
1619416200 let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2);
16195- data[idx(-2)] = op1.clamp(0, sample_max()) as u16;
16196- data[idx(-1)] = op0.clamp(0, sample_max()) as u16;
16197- data[idx(0)] = oq0.clamp(0, sample_max()) as u16;
16198- data[idx(1)] = oq1.clamp(0, sample_max()) as u16;
16201+ data[idx(-2)] = op1.clamp(0, smax) as u16;
16202+ data[idx(-1)] = op0.clamp(0, smax) as u16;
16203+ data[idx(0)] = oq0.clamp(0, smax) as u16;
16204+ data[idx(1)] = oq1.clamp(0, smax) as u16;
1619916205 }
1620016206 8 => {
1620116207 let (p3, p2, p1, p0, q0, q1, q2, q3) =
@@ -16215,12 +16221,12 @@ fn filter_edge(
1621516221 && (q3 - q0).abs() <= flat_thresh;
1621616222 let [op2, op1, op0, oq0, oq1, oq2] =
1621716223 filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3);
16218- data[idx(-3)] = op2.clamp(0, sample_max()) as u16;
16219- data[idx(-2)] = op1.clamp(0, sample_max()) as u16;
16220- data[idx(-1)] = op0.clamp(0, sample_max()) as u16;
16221- data[idx(0)] = oq0.clamp(0, sample_max()) as u16;
16222- data[idx(1)] = oq1.clamp(0, sample_max()) as u16;
16223- data[idx(2)] = oq2.clamp(0, sample_max()) as u16;
16224+ data[idx(-3)] = op2.clamp(0, smax) as u16;
16225+ data[idx(-2)] = op1.clamp(0, smax) as u16;
16226+ data[idx(-1)] = op0.clamp(0, smax) as u16;
16227+ data[idx(0)] = oq0.clamp(0, smax) as u16;
16228+ data[idx(1)] = oq1.clamp(0, smax) as u16;
16229+ data[idx(2)] = oq2.clamp(0, smax) as u16;
1622416230 }
1622516231 _ => {
1622616232 let (p6, p5, p4, p3, p2, p1, p0) =
@@ -16251,7 +16257,7 @@ fn filter_edge(
1625116257 q6,
1625216258 );
1625316259 for (k, v) in (-6..=5isize).zip(out) {
16254- data[idx(k)] = v.clamp(0, sample_max()) as u16;
16260+ data[idx(k)] = v.clamp(0, smax) as u16;
1625516261 }
1625616262 }
1625716263 }