Merge branch 'lane-perf5' (037ed4f1): AVX2/SSE4.1 kernels for the MC vertical/horizontal passes and film-grain luma with runtime dispatch and scalar reference; ec-av1 moves from forbid to deny(unsafe_code) with six allow sites; byte-exact, 1080p -5.5%
Browse files at this commit3 changed files +595 -15
- crates/ec-av1/src/film_grain.rs +172 -5
- crates/ec-av1/src/lib.rs +12 -1
- crates/ec-av1/src/mc.rs +411 -9
crates/ec-av1/src/film_grain.rsmodified+172 -5
| @@ -444,12 +444,120 @@ fn add_noise_to_block( | ||
| 444 | 444 | let grow = ((lg_row0 + i) * lg_stride + lg_col0) as usize; |
| 445 | 445 | let pixels = &mut picture.y[prow..prow + width]; |
| 446 | 446 | let grain = &luma_grain[grow..grow + width]; |
| 447 | - for (p, &g) in pixels.iter_mut().zip(grain) { | |
| 448 | - let delta = | |
| 449 | - (scaling_y[*p as usize] * g + rounding_offset) >> scaling_shift; | |
| 450 | - *p = ((*p as i32 + delta).clamp(min_luma, max_luma)) as u16; | |
| 451 | - } | |
| 447 | + luma_noise_row( | |
| 448 | + pixels, grain, scaling_y, rounding_offset, scaling_shift, min_luma, max_luma, | |
| 449 | + ); | |
| 450 | + } | |
| 451 | + } | |
| 452 | +} | |
| 453 | + | |
| 454 | +/// lane-perf5: the luma noise row (spec 7.18.3.5's `add_noise_to_block` luma | |
| 455 | +/// arm), 7% of a 1080p decode with film grain. AVX2 does 8 samples at a time | |
| 456 | +/// -- the scaling-LUT read is a `vpgatherdd`, the rest is the same i32 | |
| 457 | +/// multiply, `Round2` and clip; every other CPU runs the scalar loop, which | |
| 458 | +/// stays the reference (`simd_matches_scalar_luma_noise_row` pins the pair). | |
| 459 | +#[allow(unsafe_code)] | |
| 460 | +fn luma_noise_row( | |
| 461 | + pixels: &mut [u16], | |
| 462 | + grain: &[i32], | |
| 463 | + scaling: &[i32], | |
| 464 | + rounding_offset: i32, | |
| 465 | + scaling_shift: i32, | |
| 466 | + min_luma: i32, | |
| 467 | + max_luma: i32, | |
| 468 | +) { | |
| 469 | + #[cfg(target_arch = "x86_64")] | |
| 470 | + if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 { | |
| 471 | + // SAFETY: reached only when the CPU reports avx2; the kernel reads | |
| 472 | + // `grain[..pixels.len()]` and gathers only in-range LUT entries. | |
| 473 | + unsafe { | |
| 474 | + simd::luma_noise_row_avx2( | |
| 475 | + pixels, grain, scaling, rounding_offset, scaling_shift, min_luma, max_luma, | |
| 476 | + ) | |
| 477 | + }; | |
| 478 | + return; | |
| 479 | + } | |
| 480 | + luma_noise_row_scalar( | |
| 481 | + pixels, grain, scaling, rounding_offset, scaling_shift, min_luma, max_luma, | |
| 482 | + ); | |
| 483 | +} | |
| 484 | + | |
| 485 | +/// The scalar reference for [`luma_noise_row`]. | |
| 486 | +fn luma_noise_row_scalar( | |
| 487 | + pixels: &mut [u16], | |
| 488 | + grain: &[i32], | |
| 489 | + scaling: &[i32], | |
| 490 | + rounding_offset: i32, | |
| 491 | + scaling_shift: i32, | |
| 492 | + min_luma: i32, | |
| 493 | + max_luma: i32, | |
| 494 | +) { | |
| 495 | + for (p, &g) in pixels.iter_mut().zip(grain) { | |
| 496 | + let delta = (scaling[*p as usize] * g + rounding_offset) >> scaling_shift; | |
| 497 | + *p = ((*p as i32 + delta).clamp(min_luma, max_luma)) as u16; | |
| 498 | + } | |
| 499 | +} | |
| 500 | + | |
| 501 | +#[cfg(target_arch = "x86_64")] | |
| 502 | +#[allow(unsafe_code)] | |
| 503 | +#[allow(unsafe_op_in_unsafe_fn)] // each kernel's whole body is its contract | |
| 504 | +mod simd { | |
| 505 | + use std::arch::x86_64::*; | |
| 506 | + | |
| 507 | + /// [`super::luma_noise_row`]'s AVX2 kernel, 8 samples per iteration. | |
| 508 | + /// | |
| 509 | + /// The gather index is clamped to the LUT's last entry. The scalar form | |
| 510 | + /// indexes unclamped -- it would panic rather than read out of bounds -- | |
| 511 | + /// so the clamp is only a bound on the load: the LUT covers the whole | |
| 512 | + /// sample range the grain bit depth allows, so a real decode never | |
| 513 | + /// reaches it, and the two forms agree wherever the scalar one does not | |
| 514 | + /// panic. | |
| 515 | + /// | |
| 516 | + /// # Safety | |
| 517 | + /// Requires AVX2; `grain` must be at least `pixels.len()` long and | |
| 518 | + /// `scaling` non-empty. | |
| 519 | + #[target_feature(enable = "avx2")] | |
| 520 | + #[allow(clippy::too_many_arguments)] | |
| 521 | + pub unsafe fn luma_noise_row_avx2( | |
| 522 | + pixels: &mut [u16], | |
| 523 | + grain: &[i32], | |
| 524 | + scaling: &[i32], | |
| 525 | + rounding_offset: i32, | |
| 526 | + scaling_shift: i32, | |
| 527 | + min_luma: i32, | |
| 528 | + max_luma: i32, | |
| 529 | + ) { | |
| 530 | + let n = pixels.len().min(grain.len()); | |
| 531 | + let last = _mm256_set1_epi32(scaling.len() as i32 - 1); | |
| 532 | + let rnd = _mm256_set1_epi32(rounding_offset); | |
| 533 | + let sh = _mm_cvtsi32_si128(scaling_shift); | |
| 534 | + let lo = _mm256_set1_epi32(min_luma); | |
| 535 | + let hi = _mm256_set1_epi32(max_luma); | |
| 536 | + let pp = pixels.as_mut_ptr(); | |
| 537 | + let gp = grain.as_ptr(); | |
| 538 | + let mut c = 0usize; | |
| 539 | + while c + 8 <= n { | |
| 540 | + let p16 = _mm_loadu_si128(pp.add(c).cast()); | |
| 541 | + let p32 = _mm256_cvtepu16_epi32(p16); | |
| 542 | + let idx = _mm256_min_epi32(p32, last); | |
| 543 | + let s = _mm256_i32gather_epi32(scaling.as_ptr(), idx, 4); | |
| 544 | + let g = _mm256_loadu_si256(gp.add(c).cast()); | |
| 545 | + let delta = _mm256_sra_epi32(_mm256_add_epi32(_mm256_mullo_epi32(s, g), rnd), sh); | |
| 546 | + let v = _mm256_max_epi32( | |
| 547 | + _mm256_min_epi32(_mm256_add_epi32(p32, delta), hi), | |
| 548 | + lo, | |
| 549 | + ); | |
| 550 | + // Every lane is already inside `[min_luma, max_luma]`, so the | |
| 551 | + // unsigned saturation of `packus` is exact; `permute4x64` undoes | |
| 552 | + // the pack's per-128-bit-lane interleave. | |
| 553 | + let packed = _mm256_permute4x64_epi64(_mm256_packus_epi32(v, v), 0b0000_1000); | |
| 554 | + _mm_storeu_si128(pp.add(c).cast(), _mm256_castsi256_si128(packed)); | |
| 555 | + c += 8; | |
| 452 | 556 | } |
| 557 | + super::luma_noise_row_scalar( | |
| 558 | + &mut pixels[c..], &grain[c..], scaling, rounding_offset, scaling_shift, min_luma, | |
| 559 | + max_luma, | |
| 560 | + ); | |
| 453 | 561 | } |
| 454 | 562 | } |
| 455 | 563 | |
| @@ -987,3 +1095,62 @@ pub(crate) fn apply_grain( | ||
| 987 | 1095 | |
| 988 | 1096 | out |
| 989 | 1097 | } |
| 1098 | + | |
| 1099 | +#[cfg(test)] | |
| 1100 | +mod perf5_tests { | |
| 1101 | + /// lane-perf5: the AVX2 luma-noise kernel against the scalar reference it | |
| 1102 | + /// replaces, over the domain a decode hands it -- 8/10/12-bit sample and | |
| 1103 | + /// LUT ranges, both grain signs, every `grain_scaling_minus_8` shift, the | |
| 1104 | + /// restricted and full clip ranges, and row lengths that exercise the | |
| 1105 | + /// 8-lane body and every tail length. | |
| 1106 | + #[test] | |
| 1107 | + fn simd_matches_scalar_luma_noise_row() { | |
| 1108 | + let mut state = 0x1234_5678_9abc_def0u64; | |
| 1109 | + let mut rng = move || { | |
| 1110 | + state ^= state << 13; | |
| 1111 | + state ^= state >> 7; | |
| 1112 | + state ^= state << 17; | |
| 1113 | + state | |
| 1114 | + }; | |
| 1115 | + let mut checked = 0usize; | |
| 1116 | + for bd_shift in 0..3u32 { | |
| 1117 | + let lut_len = 256usize << bd_shift; | |
| 1118 | + let sample_max = (lut_len - 1) as u16; | |
| 1119 | + let scaling: Vec<i32> = (0..lut_len).map(|_| (rng() % 256) as i32).collect(); | |
| 1120 | + for scaling_shift in 8..12i32 { | |
| 1121 | + let rounding_offset = 1i32 << (scaling_shift - 1); | |
| 1122 | + for &(lo, hi) in &[(0i32, sample_max as i32), (16 << bd_shift, 235 << bd_shift)] { | |
| 1123 | + for len in [1usize, 4, 7, 8, 9, 16, 31, 32, 64, 128] { | |
| 1124 | + let pixels: Vec<u16> = | |
| 1125 | + (0..len).map(|_| (rng() % (u64::from(sample_max) + 1)) as u16).collect(); | |
| 1126 | + let grain: Vec<i32> = | |
| 1127 | + (0..len).map(|_| (rng() % 513) as i32 - 256).collect(); | |
| 1128 | + let mut want = pixels.clone(); | |
| 1129 | + super::luma_noise_row_scalar( | |
| 1130 | + &mut want, &grain, &scaling, rounding_offset, scaling_shift, lo, hi, | |
| 1131 | + ); | |
| 1132 | + let mut got = pixels.clone(); | |
| 1133 | + super::luma_noise_row( | |
| 1134 | + &mut got, &grain, &scaling, rounding_offset, scaling_shift, lo, hi, | |
| 1135 | + ); | |
| 1136 | + assert_eq!(got, want, "dispatch len={len} shift={scaling_shift}"); | |
| 1137 | + #[cfg(target_arch = "x86_64")] | |
| 1138 | + if std::arch::is_x86_feature_detected!("avx2") { | |
| 1139 | + let mut avx = pixels.clone(); | |
| 1140 | + #[allow(unsafe_code)] | |
| 1141 | + unsafe { | |
| 1142 | + super::simd::luma_noise_row_avx2( | |
| 1143 | + &mut avx, &grain, &scaling, rounding_offset, scaling_shift, | |
| 1144 | + lo, hi, | |
| 1145 | + ) | |
| 1146 | + }; | |
| 1147 | + assert_eq!(avx, want, "avx2 len={len} shift={scaling_shift}"); | |
| 1148 | + } | |
| 1149 | + checked += 1; | |
| 1150 | + } | |
| 1151 | + } | |
| 1152 | + } | |
| 1153 | + } | |
| 1154 | + assert_eq!(checked, 3 * 4 * 2 * 10); | |
| 1155 | + } | |
| 1156 | +} |
crates/ec-av1/src/lib.rsmodified+12 -1
| @@ -8,8 +8,19 @@ | ||
| 8 | 8 | //! |
| 9 | 9 | //! AV1 is covered by the AOMedia Patent License 1.0; this crate is an |
| 10 | 10 | //! independent implementation written from the specification alone. |
| 11 | +//! | |
| 12 | +//! # Unsafe | |
| 13 | +//! | |
| 14 | +//! This crate is `deny(unsafe_code)` rather than `forbid`: the two motion | |
| 15 | +//! compensation inner loops are the largest single share of a decode's time | |
| 16 | +//! and are hand-vectorised with `std::arch::x86_64` intrinsics, which are | |
| 17 | +//! unsafe by signature. The exception is confined to [`mc`]'s `simd` module | |
| 18 | +//! (plus its dispatch and its tests, each carrying its own | |
| 19 | +//! `#[allow(unsafe_code)]`); every kernel there is checked lane-for-lane | |
| 20 | +//! against the scalar function it replaces, which stays the reference | |
| 21 | +//! implementation and is what every other architecture runs. | |
| 11 | 22 | |
| 12 | -#![forbid(unsafe_code)] | |
| 23 | +#![deny(unsafe_code)] | |
| 13 | 24 | #![warn(missing_docs)] |
| 14 | 25 | |
| 15 | 26 | pub mod bits; |
crates/ec-av1/src/mc.rsmodified+411 -9
| @@ -243,6 +243,265 @@ fn sample( | ||
| 243 | 243 | i32::from(reference[cy * stride + cx]) |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | +/// lane-perf5: which explicit-SIMD kernels this CPU can run. The scalar | |
| 247 | +/// functions below stay the reference implementation -- every SIMD kernel is | |
| 248 | +/// checked against them by `simd_matches_scalar_*` -- and every non-x86_64 | |
| 249 | +/// build, plus any x86_64 CPU without AVX2 or SSE4.1, decodes through them. | |
| 250 | +#[derive(Clone, Copy, PartialEq, Eq, Debug)] | |
| 251 | +#[allow(dead_code)] // every variant is constructed only on x86_64 | |
| 252 | +pub(crate) enum SimdLevel { | |
| 253 | + Scalar, | |
| 254 | + /// `_mm_madd_epi16` (SSE2) horizontal pass, `_mm_mullo_epi32` (SSE4.1) | |
| 255 | + /// vertical pass -- the pair is gated on SSE4.1, the later of the two. | |
| 256 | + Sse41, | |
| 257 | + Avx2, | |
| 258 | +} | |
| 259 | + | |
| 260 | +/// The level, detected once per process (spec-irrelevant: pure dispatch). | |
| 261 | +pub(crate) fn simd_level() -> SimdLevel { | |
| 262 | + static LEVEL: std::sync::OnceLock<SimdLevel> = std::sync::OnceLock::new(); | |
| 263 | + *LEVEL.get_or_init(|| { | |
| 264 | + #[cfg(target_arch = "x86_64")] | |
| 265 | + { | |
| 266 | + if std::arch::is_x86_feature_detected!("avx2") { | |
| 267 | + return SimdLevel::Avx2; | |
| 268 | + } | |
| 269 | + if std::arch::is_x86_feature_detected!("sse4.1") { | |
| 270 | + return SimdLevel::Sse41; | |
| 271 | + } | |
| 272 | + } | |
| 273 | + SimdLevel::Scalar | |
| 274 | + }) | |
| 275 | +} | |
| 276 | + | |
| 277 | +/// The horizontal pass's inner loop over a contiguous window (`src` holds the | |
| 278 | +/// row's whole footprint, `out.len() + 7` samples): SIMD where available, | |
| 279 | +/// [`hpass_contig_scalar`] otherwise. Both produce the same `i32`s -- the | |
| 280 | +/// SIMD form multiplies the same `i16` pairs, sums them in `i32` and applies | |
| 281 | +/// the same `Round2(sum, InterRound0)`. | |
| 282 | +#[allow(unsafe_code)] | |
| 283 | +#[inline] | |
| 284 | +fn hpass_contig(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | |
| 285 | + #[cfg(target_arch = "x86_64")] | |
| 286 | + { | |
| 287 | + match simd_level() { | |
| 288 | + SimdLevel::Avx2 => { | |
| 289 | + // SAFETY: the AVX2 arm is reached only when the CPU reports | |
| 290 | + // avx2, and the kernel reads at most `out.len() + 7` samples. | |
| 291 | + unsafe { simd::hpass_avx2(src, t16, out) }; | |
| 292 | + return; | |
| 293 | + } | |
| 294 | + SimdLevel::Sse41 => { | |
| 295 | + // SAFETY: as above; the kernel needs only SSE2, which every | |
| 296 | + // x86_64 CPU has. | |
| 297 | + unsafe { simd::hpass_sse2(src, t16, out) }; | |
| 298 | + return; | |
| 299 | + } | |
| 300 | + SimdLevel::Scalar => {} | |
| 301 | + } | |
| 302 | + } | |
| 303 | + hpass_contig_scalar(src, t16, out); | |
| 304 | +} | |
| 305 | + | |
| 306 | +/// The scalar reference for [`hpass_contig`] (lane-perf2's loop). | |
| 307 | +fn hpass_contig_scalar(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | |
| 308 | + // Both factors are 16-bit (a tap is |x| <= 128, a sample fits the bit | |
| 309 | + // depth), so the products are exactly the i32 ones but the widening | |
| 310 | + // multiply is one instruction per pair. | |
| 311 | + for (o, w) in out.iter_mut().zip(src.windows(8)) { | |
| 312 | + let mut sum = 0i32; | |
| 313 | + for t in 0..8 { | |
| 314 | + sum += i32::from(t16[t]) * i32::from(w[t] as i16); | |
| 315 | + } | |
| 316 | + *o = round2(sum, INTER_ROUND_0); | |
| 317 | + } | |
| 318 | +} | |
| 319 | + | |
| 320 | +/// lane-perf5: explicit `std::arch::x86_64` kernels for the two motion | |
| 321 | +/// compensation passes -- the 18.7% of a 1080p decode that lane-perf4 left in | |
| 322 | +/// `hpass_row` + `vpass_row`. Nothing here is spec logic: each kernel is the | |
| 323 | +/// scalar function's arithmetic, lane-parallel, with identical rounding. | |
| 324 | +#[allow(unsafe_code)] | |
| 325 | +#[cfg(target_arch = "x86_64")] | |
| 326 | +#[allow(unsafe_op_in_unsafe_fn)] // each kernel's whole body is its contract | |
| 327 | +mod simd { | |
| 328 | + use super::{round2, INTER_ROUND_0}; | |
| 329 | + use std::arch::x86_64::*; | |
| 330 | + | |
| 331 | + /// `[t0,t1]`, `[t2,t3]`, `[t4,t5]`, `[t6,t7]` packed as `i32`s, the shape | |
| 332 | + /// `_mm*_madd_epi16` wants: lane `j` of `madd(samples, set1(pair))` is | |
| 333 | + /// `s[2j]*t_even + s[2j+1]*t_odd`, i.e. two taps of one output at once. | |
| 334 | + #[inline] | |
| 335 | + fn tap_pairs(t: &[i16; 8]) -> [i32; 4] { | |
| 336 | + let pack = |a: i16, b: i16| (a as u16 as i32) | ((b as i32) << 16); | |
| 337 | + [pack(t[0], t[1]), pack(t[2], t[3]), pack(t[4], t[5]), pack(t[6], t[7])] | |
| 338 | + } | |
| 339 | + | |
| 340 | + /// Horizontal pass, AVX2: 16 outputs per iteration. Each 256-bit `madd` | |
| 341 | + /// covers 8 outputs two taps at a time, but only every other output (the | |
| 342 | + /// pairs are consecutive samples), so even and odd output columns are | |
| 343 | + /// accumulated in two registers and interleaved back into order by | |
| 344 | + /// `unpack` + `permute2x128`. | |
| 345 | + /// | |
| 346 | + /// # Safety | |
| 347 | + /// Requires AVX2. `src.len()` must be at least `out.len() + 7`. | |
| 348 | + #[target_feature(enable = "avx2")] | |
| 349 | + pub unsafe fn hpass_avx2(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | |
| 350 | + debug_assert!(src.len() >= out.len() + 7); | |
| 351 | + let n = out.len(); | |
| 352 | + let tp = tap_pairs(t16); | |
| 353 | + let tv = [ | |
| 354 | + _mm256_set1_epi32(tp[0]), | |
| 355 | + _mm256_set1_epi32(tp[1]), | |
| 356 | + _mm256_set1_epi32(tp[2]), | |
| 357 | + _mm256_set1_epi32(tp[3]), | |
| 358 | + ]; | |
| 359 | + let rnd = _mm256_set1_epi32(1 << (INTER_ROUND_0 - 1)); | |
| 360 | + let sp = src.as_ptr(); | |
| 361 | + let op = out.as_mut_ptr(); | |
| 362 | + let mut c = 0usize; | |
| 363 | + while c + 16 <= n { | |
| 364 | + let mut e = _mm256_setzero_si256(); | |
| 365 | + let mut o = _mm256_setzero_si256(); | |
| 366 | + for k in 0..4 { | |
| 367 | + let se = _mm256_loadu_si256(sp.add(c + 2 * k).cast()); | |
| 368 | + e = _mm256_add_epi32(e, _mm256_madd_epi16(se, tv[k])); | |
| 369 | + let so = _mm256_loadu_si256(sp.add(c + 2 * k + 1).cast()); | |
| 370 | + o = _mm256_add_epi32(o, _mm256_madd_epi16(so, tv[k])); | |
| 371 | + } | |
| 372 | + e = _mm256_srai_epi32(_mm256_add_epi32(e, rnd), INTER_ROUND_0 as i32); | |
| 373 | + o = _mm256_srai_epi32(_mm256_add_epi32(o, rnd), INTER_ROUND_0 as i32); | |
| 374 | + let lo = _mm256_unpacklo_epi32(e, o); | |
| 375 | + let hi = _mm256_unpackhi_epi32(e, o); | |
| 376 | + _mm256_storeu_si256(op.add(c).cast(), _mm256_permute2x128_si256(lo, hi, 0x20)); | |
| 377 | + _mm256_storeu_si256(op.add(c + 8).cast(), _mm256_permute2x128_si256(lo, hi, 0x31)); | |
| 378 | + c += 16; | |
| 379 | + } | |
| 380 | + if c < n { | |
| 381 | + hpass_sse2(&src[c..], t16, &mut out[c..]); | |
| 382 | + } | |
| 383 | + } | |
| 384 | + | |
| 385 | + /// Vertical pass, AVX2: 8 output columns per iteration, one | |
| 386 | + /// `_mm256_mullo_epi32` + `_mm256_add_epi32` per non-zero tap. Exact: | |
| 387 | + /// an intermediate is at most ~2^17 and a tap at most 128, so the eight | |
| 388 | + /// products stay well inside `i32` and `mullo`'s low half is the whole | |
| 389 | + /// product. `acc` is left unrounded, as the scalar form leaves it. | |
| 390 | + /// | |
| 391 | + /// # Safety | |
| 392 | + /// Requires AVX2. `intermediate` must hold `(row + 8) * block_w` elements | |
| 393 | + /// and `acc` at least `block_w`. | |
| 394 | + #[target_feature(enable = "avx2")] | |
| 395 | + pub unsafe fn vpass_avx2( | |
| 396 | + intermediate: &[i32], | |
| 397 | + block_w: usize, | |
| 398 | + row: usize, | |
| 399 | + taps: &[i32; 8], | |
| 400 | + acc: &mut [i32], | |
| 401 | + ) { | |
| 402 | + debug_assert!(intermediate.len() >= (row + 8) * block_w && acc.len() >= block_w); | |
| 403 | + let ip = intermediate.as_ptr().add(row * block_w); | |
| 404 | + let ap = acc.as_mut_ptr(); | |
| 405 | + let mut c = 0usize; | |
| 406 | + while c + 8 <= block_w { | |
| 407 | + let mut v = _mm256_setzero_si256(); | |
| 408 | + for (t, &tap) in taps.iter().enumerate() { | |
| 409 | + if tap == 0 { | |
| 410 | + continue; | |
| 411 | + } | |
| 412 | + let s = _mm256_loadu_si256(ip.add(t * block_w + c).cast()); | |
| 413 | + v = _mm256_add_epi32(v, _mm256_mullo_epi32(_mm256_set1_epi32(tap), s)); | |
| 414 | + } | |
| 415 | + _mm256_storeu_si256(ap.add(c).cast(), v); | |
| 416 | + c += 8; | |
| 417 | + } | |
| 418 | + // Widths are multiples of 4, so at most one 4-column tail is left. | |
| 419 | + for cc in c..block_w { | |
| 420 | + let mut sum = 0i32; | |
| 421 | + for (t, &tap) in taps.iter().enumerate() { | |
| 422 | + sum += tap * intermediate[(row + t) * block_w + cc]; | |
| 423 | + } | |
| 424 | + acc[cc] = sum; | |
| 425 | + } | |
| 426 | + } | |
| 427 | + | |
| 428 | + /// Vertical pass, SSE4.1 (`_mm_mullo_epi32` is SSE4.1, unlike the | |
| 429 | + /// horizontal pass's SSE2 `madd`): [`vpass_avx2`] at 4 columns. | |
| 430 | + /// | |
| 431 | + /// # Safety | |
| 432 | + /// Requires SSE4.1; same slice contract as [`vpass_avx2`]. | |
| 433 | + #[target_feature(enable = "sse4.1")] | |
| 434 | + pub unsafe fn vpass_sse41( | |
| 435 | + intermediate: &[i32], | |
| 436 | + block_w: usize, | |
| 437 | + row: usize, | |
| 438 | + taps: &[i32; 8], | |
| 439 | + acc: &mut [i32], | |
| 440 | + ) { | |
| 441 | + debug_assert!(intermediate.len() >= (row + 8) * block_w && acc.len() >= block_w); | |
| 442 | + let ip = intermediate.as_ptr().add(row * block_w); | |
| 443 | + let ap = acc.as_mut_ptr(); | |
| 444 | + let mut c = 0usize; | |
| 445 | + while c + 4 <= block_w { | |
| 446 | + let mut v = _mm_setzero_si128(); | |
| 447 | + for (t, &tap) in taps.iter().enumerate() { | |
| 448 | + if tap == 0 { | |
| 449 | + continue; | |
| 450 | + } | |
| 451 | + let s = _mm_loadu_si128(ip.add(t * block_w + c).cast()); | |
| 452 | + v = _mm_add_epi32(v, _mm_mullo_epi32(_mm_set1_epi32(tap), s)); | |
| 453 | + } | |
| 454 | + _mm_storeu_si128(ap.add(c).cast(), v); | |
| 455 | + c += 4; | |
| 456 | + } | |
| 457 | + debug_assert_eq!(c, block_w, "every AV1 block width is a multiple of 4"); | |
| 458 | + } | |
| 459 | + | |
| 460 | + /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per | |
| 461 | + /// iteration; the remainder (a width-4 block, or a 4-column tail) is the | |
| 462 | + /// scalar loop, which is where 4-tap kernels mostly land anyway. | |
| 463 | + /// | |
| 464 | + /// # Safety | |
| 465 | + /// `src.len()` must be at least `out.len() + 7`. SSE2 is baseline x86_64. | |
| 466 | + pub unsafe fn hpass_sse2(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | |
| 467 | + debug_assert!(src.len() >= out.len() + 7); | |
| 468 | + let n = out.len(); | |
| 469 | + let tp = tap_pairs(t16); | |
| 470 | + let tv = [ | |
| 471 | + _mm_set1_epi32(tp[0]), | |
| 472 | + _mm_set1_epi32(tp[1]), | |
| 473 | + _mm_set1_epi32(tp[2]), | |
| 474 | + _mm_set1_epi32(tp[3]), | |
| 475 | + ]; | |
| 476 | + let rnd = _mm_set1_epi32(1 << (INTER_ROUND_0 - 1)); | |
| 477 | + let sp = src.as_ptr(); | |
| 478 | + let op = out.as_mut_ptr(); | |
| 479 | + let mut c = 0usize; | |
| 480 | + while c + 8 <= n { | |
| 481 | + let mut e = _mm_setzero_si128(); | |
| 482 | + let mut o = _mm_setzero_si128(); | |
| 483 | + for k in 0..4 { | |
| 484 | + let se = _mm_loadu_si128(sp.add(c + 2 * k).cast()); | |
| 485 | + e = _mm_add_epi32(e, _mm_madd_epi16(se, tv[k])); | |
| 486 | + let so = _mm_loadu_si128(sp.add(c + 2 * k + 1).cast()); | |
| 487 | + o = _mm_add_epi32(o, _mm_madd_epi16(so, tv[k])); | |
| 488 | + } | |
| 489 | + e = _mm_srai_epi32(_mm_add_epi32(e, rnd), INTER_ROUND_0 as i32); | |
| 490 | + o = _mm_srai_epi32(_mm_add_epi32(o, rnd), INTER_ROUND_0 as i32); | |
| 491 | + _mm_storeu_si128(op.add(c).cast(), _mm_unpacklo_epi32(e, o)); | |
| 492 | + _mm_storeu_si128(op.add(c + 4).cast(), _mm_unpackhi_epi32(e, o)); | |
| 493 | + c += 8; | |
| 494 | + } | |
| 495 | + for (o, w) in out[c..].iter_mut().zip(src[c..].windows(8)) { | |
| 496 | + let mut sum = 0i32; | |
| 497 | + for t in 0..8 { | |
| 498 | + sum += i32::from(t16[t]) * i32::from(w[t] as i16); | |
| 499 | + } | |
| 500 | + *o = round2(sum, INTER_ROUND_0); | |
| 501 | + } | |
| 502 | + } | |
| 503 | +} | |
| 504 | + | |
| 246 | 505 | /// lane-perf2: one row of the separable filter's horizontal pass, dav1d |
| 247 | 506 | /// style. When the row's whole 8-tap footprint (`x0-3 .. x0+block_w+4`) lies |
| 248 | 507 | /// inside the reference's true extent -- the overwhelmingly common case -- |
| @@ -264,13 +523,7 @@ fn hpass_row(reference: &[u16], row_base: usize, true_width: usize, x0: i32, tap | ||
| 264 | 523 | taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16, |
| 265 | 524 | taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16, |
| 266 | 525 | ]; |
| 267 | - for (o, w) in out.iter_mut().zip(src.windows(8)) { | |
| 268 | - let mut sum = 0i32; | |
| 269 | - for t in 0..8 { | |
| 270 | - sum += i32::from(t16[t]) * i32::from(w[t] as i16); | |
| 271 | - } | |
| 272 | - *o = round2(sum, INTER_ROUND_0); | |
| 273 | - } | |
| 526 | + hpass_contig(src, &t16, out); | |
| 274 | 527 | return; |
| 275 | 528 | } |
| 276 | 529 | } |
| @@ -332,14 +585,38 @@ fn horizontal_pass_unscaled( | ||
| 332 | 585 | /// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the |
| 333 | 586 | /// eight slots. `acc` is left holding the unrounded sums. |
| 334 | 587 | #[inline] |
| 588 | +#[allow(unsafe_code)] | |
| 335 | 589 | fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { |
| 336 | - acc.fill(0); | |
| 590 | + #[cfg(target_arch = "x86_64")] | |
| 591 | + { | |
| 592 | + match simd_level() { | |
| 593 | + // SAFETY: the arm is reached only when the CPU reports the | |
| 594 | + // feature; the kernel reads `intermediate[(row+t)*block_w .. | |
| 595 | + // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which | |
| 596 | + // the debug asserts pin. | |
| 597 | + SimdLevel::Avx2 => { | |
| 598 | + unsafe { simd::vpass_avx2(intermediate, block_w, row, taps, acc) }; | |
| 599 | + return; | |
| 600 | + } | |
| 601 | + SimdLevel::Sse41 => { | |
| 602 | + unsafe { simd::vpass_sse41(intermediate, block_w, row, taps, acc) }; | |
| 603 | + return; | |
| 604 | + } | |
| 605 | + SimdLevel::Scalar => {} | |
| 606 | + } | |
| 607 | + } | |
| 608 | + vpass_row_scalar(intermediate, block_w, row, taps, acc); | |
| 609 | +} | |
| 610 | + | |
| 611 | +/// The scalar reference for [`vpass_row`] (lane-perf2's tap-major loop). | |
| 612 | +fn vpass_row_scalar(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { | |
| 613 | + acc[..block_w].fill(0); | |
| 337 | 614 | for (t, &tap) in taps.iter().enumerate() { |
| 338 | 615 | if tap == 0 { |
| 339 | 616 | continue; |
| 340 | 617 | } |
| 341 | 618 | let base = (row + t) * block_w; |
| 342 | - for (a, &s) in acc.iter_mut().zip(&intermediate[base..base + block_w]) { | |
| 619 | + for (a, &s) in acc[..block_w].iter_mut().zip(&intermediate[base..base + block_w]) { | |
| 343 | 620 | *a += tap * s; |
| 344 | 621 | } |
| 345 | 622 | } |
| @@ -1521,4 +1798,129 @@ mod tests { | ||
| 1521 | 1798 | } |
| 1522 | 1799 | } |
| 1523 | 1800 | } |
| 1801 | + | |
| 1802 | + /// lane-perf5: every explicit-SIMD kernel is checked against the scalar | |
| 1803 | + /// reference it replaces, over the whole domain a decode can hand it -- | |
| 1804 | + /// all block widths (4..=128), all 16 sub-pel phases, all four filter | |
| 1805 | + /// kinds with both their wide and narrow (4-tap) tables, and 8/10/12-bit | |
| 1806 | + /// sample ranges. The dispatch itself is exercised through | |
| 1807 | + /// [`super::hpass_contig`] (whatever this CPU selected). | |
| 1808 | + #[allow(unsafe_code)] | |
| 1809 | + #[test] | |
| 1810 | + fn simd_matches_scalar_horizontal_pass() { | |
| 1811 | + let mut state = 0x2545_f491_4f6c_dd1du64; | |
| 1812 | + let mut rng = move || { | |
| 1813 | + state ^= state << 13; | |
| 1814 | + state ^= state >> 7; | |
| 1815 | + state ^= state << 17; | |
| 1816 | + state | |
| 1817 | + }; | |
| 1818 | + let kinds = [ | |
| 1819 | + InterpFilterKind::Regular, | |
| 1820 | + InterpFilterKind::Smooth, | |
| 1821 | + InterpFilterKind::Sharp, | |
| 1822 | + InterpFilterKind::Bilinear, | |
| 1823 | + ]; | |
| 1824 | + let mut checked = 0usize; | |
| 1825 | + for depth_max in [255u16, 1023, 4095] { | |
| 1826 | + for &kind in &kinds { | |
| 1827 | + let (wide, narrow) = kind.tables(); | |
| 1828 | + for table in [wide, narrow] { | |
| 1829 | + for frac in 0..16usize { | |
| 1830 | + let taps = &table[frac]; | |
| 1831 | + let t16 = [ | |
| 1832 | + taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16, | |
| 1833 | + taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16, | |
| 1834 | + ]; | |
| 1835 | + for w in [4usize, 8, 12, 16, 20, 32, 64, 128] { | |
| 1836 | + let src: Vec<u16> = (0..w + 7) | |
| 1837 | + .map(|_| (rng() % (u64::from(depth_max) + 1)) as u16) | |
| 1838 | + .collect(); | |
| 1839 | + let mut want = vec![0i32; w]; | |
| 1840 | + super::hpass_contig_scalar(&src, &t16, &mut want); | |
| 1841 | + let mut got = vec![0i32; w]; | |
| 1842 | + super::hpass_contig(&src, &t16, &mut got); | |
| 1843 | + assert_eq!(got, want, "dispatch w={w} frac={frac} max={depth_max}"); | |
| 1844 | + #[cfg(target_arch = "x86_64")] | |
| 1845 | + { | |
| 1846 | + let mut sse = vec![0i32; w]; | |
| 1847 | + unsafe { super::simd::hpass_sse2(&src, &t16, &mut sse) }; | |
| 1848 | + assert_eq!(sse, want, "sse w={w} frac={frac} max={depth_max}"); | |
| 1849 | + if std::arch::is_x86_feature_detected!("avx2") { | |
| 1850 | + let mut avx = vec![0i32; w]; | |
| 1851 | + unsafe { super::simd::hpass_avx2(&src, &t16, &mut avx) }; | |
| 1852 | + assert_eq!(avx, want, "avx2 w={w} frac={frac} max={depth_max}"); | |
| 1853 | + } | |
| 1854 | + } | |
| 1855 | + checked += 1; | |
| 1856 | + } | |
| 1857 | + } | |
| 1858 | + } | |
| 1859 | + } | |
| 1860 | + } | |
| 1861 | + assert_eq!(checked, 3 * 4 * 2 * 16 * 8); | |
| 1862 | + } | |
| 1863 | + | |
| 1864 | + /// lane-perf5: the vertical pass's SIMD kernels against the scalar | |
| 1865 | + /// reference, over the same domain as | |
| 1866 | + /// [`simd_matches_scalar_horizontal_pass`] -- all widths, all phases, | |
| 1867 | + /// both wide and narrow (4-tap) tables of all four filter kinds, and | |
| 1868 | + /// intermediates spanning the 8/10/12-bit ranges the horizontal pass can | |
| 1869 | + /// produce (a 12-bit sample times the filter gain, both signs). | |
| 1870 | + #[allow(unsafe_code)] | |
| 1871 | + #[test] | |
| 1872 | + fn simd_matches_scalar_vertical_pass() { | |
| 1873 | + let mut state = 0x9e37_79b9_7f4a_7c15u64; | |
| 1874 | + let mut rng = move || { | |
| 1875 | + state ^= state << 13; | |
| 1876 | + state ^= state >> 7; | |
| 1877 | + state ^= state << 17; | |
| 1878 | + state | |
| 1879 | + }; | |
| 1880 | + let kinds = [ | |
| 1881 | + InterpFilterKind::Regular, | |
| 1882 | + InterpFilterKind::Smooth, | |
| 1883 | + InterpFilterKind::Sharp, | |
| 1884 | + InterpFilterKind::Bilinear, | |
| 1885 | + ]; | |
| 1886 | + let mut checked = 0usize; | |
| 1887 | + for span in [255i32 * 16, 1023 * 16, 4095 * 22] { | |
| 1888 | + for &kind in &kinds { | |
| 1889 | + let (wide, narrow) = kind.tables(); | |
| 1890 | + for table in [wide, narrow] { | |
| 1891 | + for frac in 0..16usize { | |
| 1892 | + let taps = &table[frac]; | |
| 1893 | + for w in [4usize, 8, 12, 16, 20, 32, 64, 128] { | |
| 1894 | + let rows = 12usize; | |
| 1895 | + let inter: Vec<i32> = (0..rows * w) | |
| 1896 | + .map(|_| (rng() % (2 * span as u64 + 1)) as i32 - span) | |
| 1897 | + .collect(); | |
| 1898 | + for row in [0usize, 1, 4] { | |
| 1899 | + let mut want = vec![0i32; w]; | |
| 1900 | + super::vpass_row_scalar(&inter, w, row, taps, &mut want); | |
| 1901 | + let mut got = vec![0i32; w]; | |
| 1902 | + super::vpass_row(&inter, w, row, taps, &mut got); | |
| 1903 | + assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}"); | |
| 1904 | + #[cfg(target_arch = "x86_64")] | |
| 1905 | + { | |
| 1906 | + if std::arch::is_x86_feature_detected!("sse4.1") { | |
| 1907 | + let mut sse = vec![0i32; w]; | |
| 1908 | + unsafe { super::simd::vpass_sse41(&inter, w, row, taps, &mut sse) }; | |
| 1909 | + assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}"); | |
| 1910 | + } | |
| 1911 | + if std::arch::is_x86_feature_detected!("avx2") { | |
| 1912 | + let mut avx = vec![0i32; w]; | |
| 1913 | + unsafe { super::simd::vpass_avx2(&inter, w, row, taps, &mut avx) }; | |
| 1914 | + assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}"); | |
| 1915 | + } | |
| 1916 | + } | |
| 1917 | + checked += 1; | |
| 1918 | + } | |
| 1919 | + } | |
| 1920 | + } | |
| 1921 | + } | |
| 1922 | + } | |
| 1923 | + } | |
| 1924 | + assert_eq!(checked, 3 * 4 * 2 * 16 * 8 * 3); | |
| 1925 | + } | |
| 1524 | 1926 | } |