forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

deblock_simd.rs461 lines, 16.3 KiBHistoryRawDownload
1use archmage::{X64V3Token, arcane};
2use std::arch::x86_64::*;
3
4/// Bounds-checked unaligned load of 16 `u16` samples at `at`.
5#[inline]
6fn load(data: &[u16], at: usize) -> __m256i {
7 let a: [u16; 16] = data[at..at + 16].try_into().unwrap();
8 bytemuck::cast(a)
9}
10
11/// Bounds-checked unaligned store of 16 `u16` samples at `at`.
12#[inline]
13fn store(data: &mut [u16], at: usize, v: __m256i) {
14 let a: [u16; 16] = bytemuck::cast(v);
15 data[at..at + 16].copy_from_slice(&a);
16}
17
18/// `a <= b` lane-wise, as a full-width mask.
19///
20/// # Safety
21/// Requires AVX2.
22#[target_feature(enable = "avx2")]
23#[inline]
24fn le(a: __m256i, b: __m256i) -> __m256i {
25 _mm256_cmpeq_epi16(_mm256_max_epi16(a, b), b)
26}
27
28/// `|a - b|` lane-wise.
29///
30/// # Safety
31/// Requires AVX2.
32#[target_feature(enable = "avx2")]
33#[inline]
34fn ad(a: __m256i, b: __m256i) -> __m256i {
35 _mm256_abs_epi16(_mm256_sub_epi16(a, b))
36}
37
38/// `signed_char_clamp_high`, lane-wise.
39///
40/// # Safety
41/// Requires AVX2.
42#[target_feature(enable = "avx2")]
43#[inline]
44fn sclamp(v: __m256i, lo: __m256i, hi: __m256i) -> __m256i {
45 _mm256_min_epi16(_mm256_max_epi16(v, lo), hi)
46}
47
48/// 16 rows of 16 `i16` transposed in place (unpack ladder, then the
49/// 128-bit lane swap AVX2's in-lane unpacks leave behind).
50///
51/// # Safety
52/// Requires AVX2.
53#[target_feature(enable = "avx2")]
54fn transpose16(t: &mut [__m256i; 16]) {
55 let mut a = [_mm256_setzero_si256(); 16];
56 for k in 0..8 {
57 a[2 * k] = _mm256_unpacklo_epi16(t[2 * k], t[2 * k + 1]);
58 a[2 * k + 1] = _mm256_unpackhi_epi16(t[2 * k], t[2 * k + 1]);
59 }
60 let mut b = [_mm256_setzero_si256(); 16];
61 for g in 0..4 {
62 let s = 4 * g;
63 b[s] = _mm256_unpacklo_epi32(a[s], a[s + 2]);
64 b[s + 1] = _mm256_unpackhi_epi32(a[s], a[s + 2]);
65 b[s + 2] = _mm256_unpacklo_epi32(a[s + 1], a[s + 3]);
66 b[s + 3] = _mm256_unpackhi_epi32(a[s + 1], a[s + 3]);
67 }
68 let mut c = [_mm256_setzero_si256(); 16];
69 for g in 0..2 {
70 let s = 8 * g;
71 for j in 0..4 {
72 c[s + 2 * j] = _mm256_unpacklo_epi64(b[s + j], b[s + j + 4]);
73 c[s + 2 * j + 1] = _mm256_unpackhi_epi64(b[s + j], b[s + j + 4]);
74 }
75 }
76 for i in 0..8 {
77 t[i] = _mm256_permute2x128_si256(c[i], c[i + 8], 0x20);
78 t[i + 8] = _mm256_permute2x128_si256(c[i], c[i + 8], 0x31);
79 }
80}
81
82/// The wide kernels' tap weights, in tap order and with the spec's
83/// rounding divisor as the row sum: `filter6` over `p2..q2`, `filter8`
84/// over `p3..q3`, `filter14` over `p6..q6`.
85const W6: [[i16; 6]; 4] = [
86 [3, 2, 2, 1, 0, 0],
87 [1, 2, 2, 2, 1, 0],
88 [0, 1, 2, 2, 2, 1],
89 [0, 0, 1, 2, 2, 3],
90];
91const W8: [[i16; 8]; 6] = [
92 [3, 2, 1, 1, 1, 0, 0, 0],
93 [2, 1, 2, 1, 1, 1, 0, 0],
94 [1, 1, 1, 2, 1, 1, 1, 0],
95 [0, 1, 1, 1, 2, 1, 1, 1],
96 [0, 0, 1, 1, 1, 2, 1, 2],
97 [0, 0, 0, 1, 1, 1, 2, 3],
98];
99#[allow(dead_code)] // superseded by the running-sum rows; kept as the weights' statement
100const W14: [[i16; 14]; 12] = [
101 [7, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
102 [5, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
103 [4, 1, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0],
104 [3, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0],
105 [2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0],
106 [1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 0],
107 [0, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1],
108 [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2],
109 [0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 3],
110 [0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 1, 4],
111 [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 5],
112 [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 7],
113];
114
115/// One weighted tap sum, rounded by `shift` (3 for the divide-by-8 rows,
116/// 4 for `filter14`'s divide-by-16 ones). The sum is non-negative and
117/// below `1 << 16`, so the logical shift is the arithmetic one.
118///
119/// # Safety
120/// Requires AVX2.
121///
122/// lane-deblock: the tap count, the weights and the shift are all const
123/// here (they were slice parameters, which left the weight `match` as a
124/// runtime branch and a `movzwl` per tap -- `testl`/`je`/`movzwl` were
125/// 34% of this kernel's samples, and the spilled tap slices another 17%).
126#[target_feature(enable = "avx2")]
127#[inline] // rust#145574: `inline(always)` is rejected on a `target_feature` fn
128fn wsum<const N: usize, const SHIFT: i32>(taps: &[__m256i; N], w: &[i16; N]) -> __m256i {
129 let mut s = _mm256_setzero_si256();
130 for i in 0..N {
131 match w[i] {
132 0 => {}
133 1 => s = _mm256_add_epi16(s, taps[i]),
134 2 => s = _mm256_add_epi16(s, _mm256_add_epi16(taps[i], taps[i])),
135 k => s = _mm256_add_epi16(s, _mm256_mullo_epi16(taps[i], _mm256_set1_epi16(k))),
136 }
137 }
138 let s = _mm256_add_epi16(s, _mm256_set1_epi16(1 << (SHIFT - 1)));
139 if SHIFT == 3 {
140 _mm256_srli_epi16(s, 3)
141 } else {
142 _mm256_srli_epi16(s, 4)
143 }
144}
145
146/// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in
147/// `t[8]`; the filtered taps are written back in place. Returns `false`
148/// when the filter mask is empty on every one of the 16 lines -- nothing
149/// was written and the caller can skip the whole write-back.
150///
151/// # Safety
152/// Requires AVX2.
153///
154/// lane-deblock2: `LEN` is a const parameter, so each edge length gets
155/// its own kernel holding only its own taps, and every wide row is
156/// clamped straight into `t` instead of into a shared twelve-entry
157/// result array (which the register allocator spilled -- 35% of this
158/// kernel's samples were `vmovdqa` to the stack).
159#[target_feature(enable = "avx2")]
160#[allow(clippy::too_many_arguments)]
161fn filter16<const LEN: u8>(
162 t: &mut [__m256i; 16],
163 blimit: i32,
164 limit: i32,
165 hev_thr: i32,
166 flat_thresh: i32,
167 scale: i32,
168 smax: i32,
169) -> bool {
170 let lim = _mm256_set1_epi16(limit as i16);
171 let blim = _mm256_set1_epi16(blimit as i16);
172 let hthr = _mm256_set1_epi16(hev_thr as i16);
173 let fthr = _mm256_set1_epi16(flat_thresh as i16);
174 let zero = _mm256_setzero_si256();
175 let smaxv = _mm256_set1_epi16(smax as i16);
176 macro_rules! put {
177 ($i:expr, $v:expr) => {
178 t[$i] = _mm256_min_epi16(_mm256_max_epi16($v, zero), smaxv);
179 };
180 }
181 let (p6, p5, p4, p3, p2, p1, p0) = (t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
182 let (q0, q1, q2, q3, q4, q5, q6) = (t[8], t[9], t[10], t[11], t[12], t[13], t[14]);
183
184 // `highbd_filter_mask`: the neighbour steps this kernel's length
185 // reaches, plus the shared `blimit` term.
186 let d1 = _mm256_max_epi16(ad(p1, p0), ad(q1, q0));
187 let mut md = d1;
188 if LEN >= 6 {
189 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p2, p1), ad(q2, q1)));
190 }
191 if LEN >= 8 {
192 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p3, p2), ad(q3, q2)));
193 }
194 let bl = _mm256_add_epi16(
195 _mm256_slli_epi16(ad(p0, q0), 1),
196 _mm256_srli_epi16(ad(p1, q1), 1),
197 );
198 let mask = _mm256_and_si256(le(md, lim), le(bl, blim));
199 // lane-deblock2: an empty mask leaves every tap at its loaded value
200 // (the flat masks are `& mask`, so they are empty too, and the final
201 // clamp is the identity on samples that came out of the plane), so
202 // the whole write-back -- including the vertical edge's second
203 // transpose -- is dead.
204 if _mm256_testz_si256(mask, mask) != 0 {
205 return false;
206 }
207 let hev = _mm256_cmpgt_epi16(d1, hthr);
208
209 // `filter4`, in the centred domain.
210 let centre = _mm256_set1_epi16((128 * scale) as i16);
211 let lo = _mm256_set1_epi16((-128 * scale) as i16);
212 let hi = _mm256_set1_epi16((128 * scale - 1) as i16);
213 let ps1 = _mm256_sub_epi16(p1, centre);
214 let ps0 = _mm256_sub_epi16(p0, centre);
215 let qs0 = _mm256_sub_epi16(q0, centre);
216 let qs1 = _mm256_sub_epi16(q1, centre);
217 let outer = _mm256_and_si256(hev, sclamp(_mm256_sub_epi16(ps1, qs1), lo, hi));
218 let f = sclamp(
219 _mm256_add_epi16(
220 outer,
221 _mm256_mullo_epi16(_mm256_set1_epi16(3), _mm256_sub_epi16(qs0, ps0)),
222 ),
223 lo,
224 hi,
225 );
226 let f1 = _mm256_srai_epi16(sclamp(_mm256_add_epi16(f, _mm256_set1_epi16(4)), lo, hi), 3);
227 let f2 = _mm256_srai_epi16(sclamp(_mm256_add_epi16(f, _mm256_set1_epi16(3)), lo, hi), 3);
228 let tap = _mm256_andnot_si256(
229 hev,
230 _mm256_srai_epi16(_mm256_add_epi16(f1, _mm256_set1_epi16(1)), 1),
231 );
232 let n_p1 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps1, tap), lo, hi), centre);
233 let n_p0 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps0, f2), lo, hi), centre);
234 let n_q0 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs0, f1), lo, hi), centre);
235 let n_q1 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs1, tap), lo, hi), centre);
236 let b_p1 = _mm256_blendv_epi8(p1, n_p1, mask);
237 let b_p0 = _mm256_blendv_epi8(p0, n_p0, mask);
238 let b_q0 = _mm256_blendv_epi8(q0, n_q0, mask);
239 let b_q1 = _mm256_blendv_epi8(q1, n_q1, mask);
240 if LEN == 4 {
241 put!(6, b_p1);
242 put!(7, b_p0);
243 put!(8, b_q0);
244 put!(9, b_q1);
245 return true;
246 }
247
248 // `highbd_flat_mask4`, over the same reach as this length's mask.
249 let mut fl = _mm256_max_epi16(
250 _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),
251 _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),
252 );
253 if LEN >= 8 {
254 fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));
255 }
256 let flat = _mm256_and_si256(le(fl, fthr), mask);
257 let any_flat = _mm256_testz_si256(flat, flat) == 0;
258 if LEN == 6 {
259 if any_flat {
260 let taps = [p2, p1, p0, q0, q1, q2];
261 put!(
262 6,
263 _mm256_blendv_epi8(b_p1, wsum::<_, 3>(&taps, &W6[0]), flat)
264 );
265 put!(
266 7,
267 _mm256_blendv_epi8(b_p0, wsum::<_, 3>(&taps, &W6[1]), flat)
268 );
269 put!(
270 8,
271 _mm256_blendv_epi8(b_q0, wsum::<_, 3>(&taps, &W6[2]), flat)
272 );
273 put!(
274 9,
275 _mm256_blendv_epi8(b_q1, wsum::<_, 3>(&taps, &W6[3]), flat)
276 );
277 } else {
278 put!(6, b_p1);
279 put!(7, b_p0);
280 put!(8, b_q0);
281 put!(9, b_q1);
282 }
283 return true;
284 }
285
286 // `filter8`'s six rows over `p2..q2`, the wide kernel's base.
287 let (o0, o1, o2, o3, o4, o5) = if any_flat {
288 let taps = [p3, p2, p1, p0, q0, q1, q2, q3];
289 (
290 _mm256_blendv_epi8(p2, wsum::<_, 3>(&taps, &W8[0]), flat),
291 _mm256_blendv_epi8(b_p1, wsum::<_, 3>(&taps, &W8[1]), flat),
292 _mm256_blendv_epi8(b_p0, wsum::<_, 3>(&taps, &W8[2]), flat),
293 _mm256_blendv_epi8(b_q0, wsum::<_, 3>(&taps, &W8[3]), flat),
294 _mm256_blendv_epi8(b_q1, wsum::<_, 3>(&taps, &W8[4]), flat),
295 _mm256_blendv_epi8(q2, wsum::<_, 3>(&taps, &W8[5]), flat),
296 )
297 } else {
298 (p2, b_p1, b_p0, b_q0, b_q1, q2)
299 };
300 macro_rules! put8 {
301 () => {
302 put!(5, o0);
303 put!(6, o1);
304 put!(7, o2);
305 put!(8, o3);
306 put!(9, o4);
307 put!(10, o5);
308 };
309 }
310 if LEN == 8 {
311 put8!();
312 return true;
313 }
314
315 let fl2 = _mm256_max_epi16(
316 _mm256_max_epi16(
317 _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),
318 _mm256_max_epi16(ad(p5, p0), ad(q5, q0)),
319 ),
320 _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
321 );
322 let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
323 if _mm256_testz_si256(flat2, flat2) != 0 {
324 // No line takes the wide rows: `t[2..5]`/`t[11..14]` keep the
325 // taps they were loaded with, exactly as the blend would.
326 put8!();
327 return true;
328 }
329 // lane-deblock: `filter14`'s twelve rows as a RUNNING sum.
330 // Consecutive `W14` rows differ by three or four taps (row j+1 - row
331 // j is a constant vector with two negative and two positive
332 // entries), so each output costs one add, one sub and the rounding
333 // shift instead of a fresh 14-tap weighted sum -- 168 tap operations
334 // become 44. Exact: every partial sum is taken mod 2^16 by
335 // `add/sub_epi16` and every ROW sum is a real `W14` row, at most
336 // `16 * 4095 + 8 < 2^16`, so the unsigned rounding shift sees the
337 // true value. `deblock_simd_kernel_matches_the_scalar_filter` pins
338 // it against `filter_edge` at 8/10/12-bit.
339 let mut s = _mm256_add_epi16(
340 _mm256_add_epi16(
341 _mm256_mullo_epi16(p6, _mm256_set1_epi16(7)),
342 _mm256_slli_epi16(_mm256_add_epi16(p5, p4), 1),
343 ),
344 _mm256_add_epi16(
345 _mm256_add_epi16(_mm256_add_epi16(p3, p2), _mm256_add_epi16(p1, p0)),
346 q0,
347 ),
348 );
349 let add2 = |a, b| _mm256_add_epi16(a, b);
350 macro_rules! row14 {
351 ($idx:literal, $base:expr) => {
352 put!(
353 $idx,
354 _mm256_blendv_epi8(
355 $base,
356 _mm256_srli_epi16(_mm256_add_epi16(s, _mm256_set1_epi16(8)), 4),
357 flat2,
358 )
359 );
360 };
361 ($idx:literal, $base:expr, $plus:expr, $minus:expr) => {
362 s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
363 row14!($idx, $base);
364 };
365 }
366 row14!(2, p5);
367 row14!(3, p4, add2(p3, q1), _mm256_slli_epi16(p6, 1));
368 row14!(4, p3, add2(p2, q2), add2(p6, p5));
369 row14!(5, o0, add2(p1, q3), add2(p6, p4));
370 row14!(6, o1, add2(p0, q4), add2(p6, p3));
371 row14!(7, o2, add2(q0, q5), add2(p6, p2));
372 row14!(8, o3, add2(q1, q6), add2(p6, p1));
373 row14!(9, o4, add2(q2, q6), add2(p5, p0));
374 row14!(10, o5, add2(q3, q6), add2(p4, q0));
375 row14!(11, q3, add2(q4, q6), add2(p3, q1));
376 row14!(12, q4, add2(q5, q6), add2(p2, q2));
377 row14!(13, q5, _mm256_slli_epi16(q6, 1), add2(p1, q3));
378 true
379}
380
381/// 16 edge lines at `base`. `vertical` names the edge: a vertical edge's
382/// 16 lines are 16 consecutive rows and its taps are consecutive samples
383/// (loaded as a 16x16 tile and transposed), a horizontal edge's are 16
384/// consecutive columns with each tap a whole row segment.
385///
386/// # Safety
387/// Requires AVX2. For a vertical edge `base % stride >= 8`, `base %
388/// stride + 8 <= stride` and 16 rows from `base` must be inside `data`;
389/// for a horizontal edge `base >= 8 * stride` and `base + 8 * stride`
390/// must be inside it.
391#[arcane]
392#[allow(clippy::too_many_arguments)]
393pub fn edge16(
394 _t: X64V3Token,
395 data: &mut [u16],
396 base: usize,
397 stride: usize,
398 vertical: bool,
399 len: u8,
400 blimit: i32,
401 limit: i32,
402 hev_thr: i32,
403 flat_thresh: i32,
404 scale: i32,
405 smax: i32,
406) {
407 let mut t = [_mm256_setzero_si256(); 16];
408 // The taps this length reads, as `t` indices.
409 let (lo, hi) = match len {
410 4 => (6, 10),
411 6 => (5, 11),
412 8 => (4, 12),
413 _ => (1, 15),
414 };
415 if vertical {
416 // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it.
417 #[allow(clippy::needless_range_loop)]
418 for j in 0..16 {
419 t[j] = load(data, base + j * stride - 8);
420 }
421 transpose16(&mut t);
422 } else {
423 let top = base - 8 * stride;
424 // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it.
425 #[allow(clippy::needless_range_loop)]
426 for i in lo..hi {
427 t[i] = load(data, top + i * stride);
428 }
429 }
430 // lane-deblock2: one monomorphised kernel per edge length, and an
431 // empty filter mask returns without any write-back at all.
432 let modified = match len {
433 4 => filter16::<4>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
434 6 => filter16::<6>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
435 8 => filter16::<8>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
436 _ => filter16::<14>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
437 };
438 if !modified {
439 return;
440 }
441 if vertical {
442 transpose16(&mut t);
443 // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it.
444 #[allow(clippy::needless_range_loop)]
445 for j in 0..16 {
446 store(data, base + j * stride - 8, t[j]);
447 }
448 } else {
449 let top = base - 8 * stride;
450 let (a, b) = match len {
451 4 | 6 => (6, 10),
452 8 => (5, 11),
453 _ => (2, 14),
454 };
455 // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it.
456 #[allow(clippy::needless_range_loop)]
457 for i in a..b {
458 store(data, top + i * stride, t[i]);
459 }
460 }
461}