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;
15996 return [p1, p0, q0, q1];15996 return [p1, p0, q0, q1];
15997 }15997 }
15998 let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh;15998 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;
16000 let (ps1, ps0, qs0, qs1) = (p1 - centre, p0 - centre, q0 - centre, q1 - centre);16005 let (ps1, ps0, qs0, qs1) = (p1 - centre, p0 - centre, q0 - centre, q1 - centre);
16001 let outer = if hev { sclamp(ps1 - qs1) } else { 0 };16006 let outer = if hev { sclamp(ps1 - qs1) } else { 0 };
16002 let filter = sclamp(outer + 3 * (qs0 - ps0));16007 let filter = sclamp(outer + 3 * (qs0 - ps0));
@@ -16164,6 +16169,7 @@ fn filter_edge(
16164 let shift = bit_depth() - 8;16169 let shift = bit_depth() - 8;
16165 let (blimit, limit, hev_thr) = (blimit << shift, limit << shift, hev_thr << shift);16170 let (blimit, limit, hev_thr) = (blimit << shift, limit << shift, hev_thr << shift);
16166 let flat_thresh = 1i32 << shift;16171 let flat_thresh = 1i32 << shift;
16172+ let smax = sample_max();
16167 for i in 0..4isize {16173 for i in 0..4isize {
16168 let center = (base as isize + i * outer_stride) as usize;16174 let center = (base as isize + i * outer_stride) as usize;
16169 let idx = |k: isize| -> usize { (center as isize + k * tap_stride) as usize };16175 let idx = |k: isize| -> usize { (center as isize + k * tap_stride) as usize };
@@ -16175,10 +16181,10 @@ fn filter_edge(
16175 && (q1 - q0).abs() <= limit16181 && (q1 - q0).abs() <= limit
16176 && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit;16182 && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit;
16177 let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1);16183 let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1);
16178- data[idx(-2)] = op1.clamp(0, sample_max()) as u16;16184+ data[idx(-2)] = op1.clamp(0, smax) as u16;
16179- data[idx(-1)] = op0.clamp(0, sample_max()) as u16;16185+ data[idx(-1)] = op0.clamp(0, smax) as u16;
16180- data[idx(0)] = oq0.clamp(0, sample_max()) as u16;16186+ data[idx(0)] = oq0.clamp(0, smax) as u16;
16181- data[idx(1)] = oq1.clamp(0, sample_max()) as u16;16187+ data[idx(1)] = oq1.clamp(0, smax) as u16;
16182 }16188 }
16183 6 => {16189 6 => {
16184 let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2));16190 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(
16192 && (p2 - p0).abs() <= flat_thresh16198 && (p2 - p0).abs() <= flat_thresh
16193 && (q2 - q0).abs() <= flat_thresh;16199 && (q2 - q0).abs() <= flat_thresh;
16194 let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2);16200 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;16201+ data[idx(-2)] = op1.clamp(0, smax) as u16;
16196- data[idx(-1)] = op0.clamp(0, sample_max()) as u16;16202+ data[idx(-1)] = op0.clamp(0, smax) as u16;
16197- data[idx(0)] = oq0.clamp(0, sample_max()) as u16;16203+ data[idx(0)] = oq0.clamp(0, smax) as u16;
16198- data[idx(1)] = oq1.clamp(0, sample_max()) as u16;16204+ data[idx(1)] = oq1.clamp(0, smax) as u16;
16199 }16205 }
16200 8 => {16206 8 => {
16201 let (p3, p2, p1, p0, q0, q1, q2, q3) =16207 let (p3, p2, p1, p0, q0, q1, q2, q3) =
@@ -16215,12 +16221,12 @@ fn filter_edge(
16215 && (q3 - q0).abs() <= flat_thresh;16221 && (q3 - q0).abs() <= flat_thresh;
16216 let [op2, op1, op0, oq0, oq1, oq2] =16222 let [op2, op1, op0, oq0, oq1, oq2] =
16217 filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3);16223 filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3);
16218- data[idx(-3)] = op2.clamp(0, sample_max()) as u16;16224+ data[idx(-3)] = op2.clamp(0, smax) as u16;
16219- data[idx(-2)] = op1.clamp(0, sample_max()) as u16;16225+ data[idx(-2)] = op1.clamp(0, smax) as u16;
16220- data[idx(-1)] = op0.clamp(0, sample_max()) as u16;16226+ data[idx(-1)] = op0.clamp(0, smax) as u16;
16221- data[idx(0)] = oq0.clamp(0, sample_max()) as u16;16227+ data[idx(0)] = oq0.clamp(0, smax) as u16;
16222- data[idx(1)] = oq1.clamp(0, sample_max()) as u16;16228+ data[idx(1)] = oq1.clamp(0, smax) as u16;
16223- data[idx(2)] = oq2.clamp(0, sample_max()) as u16;16229+ data[idx(2)] = oq2.clamp(0, smax) as u16;
16224 }16230 }
16225 _ => {16231 _ => {
16226 let (p6, p5, p4, p3, p2, p1, p0) =16232 let (p6, p5, p4, p3, p2, p1, p0) =
@@ -16251,7 +16257,7 @@ fn filter_edge(
16251 q6,16257 q6,
16252 );16258 );
16253 for (k, v) in (-6..=5isize).zip(out) {16259 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;
16255 }16261 }
16256 }16262 }
16257 }16263 }