Explicit SIMD for both motion-compensation passes
Browse files at this commit66498f4432aa304f173109ae34c42e62e5444415Parent: ea4deed- perf
- explicit SIMD for both motion-compensation passes
2 changed files +421 -10
- crates/ec-av1/src/lib.rs +12 -1
- crates/ec-av1/src/mc.rs +409 -9
crates/ec-av1/src/lib.rsmodified+12 -1
| @@ -8,8 +8,19 @@ | |||
| 8 | //! | 8 | //! |
| 9 | //! AV1 is covered by the AOMedia Patent License 1.0; this crate is an | 9 | //! AV1 is covered by the AOMedia Patent License 1.0; this crate is an |
| 10 | //! independent implementation written from the specification alone. | 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 | #![warn(missing_docs)] | 24 | #![warn(missing_docs)] |
| 14 | 25 | ||
| 15 | pub mod bits; | 26 | pub mod bits; |
crates/ec-av1/src/mc.rsmodified+409 -9
| @@ -243,6 +243,263 @@ fn sample( | |||
| 243 | i32::from(reference[cy * stride + cx]) | 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 | +enum SimdLevel { | ||
| 252 | + Scalar, | ||
| 253 | + /// `_mm_madd_epi16` (SSE2) horizontal pass, `_mm_mullo_epi32` (SSE4.1) | ||
| 254 | + /// vertical pass -- the pair is gated on SSE4.1, the later of the two. | ||
| 255 | + Sse41, | ||
| 256 | + Avx2, | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | +/// The level, detected once per process (spec-irrelevant: pure dispatch). | ||
| 260 | +fn simd_level() -> SimdLevel { | ||
| 261 | + static LEVEL: std::sync::OnceLock<SimdLevel> = std::sync::OnceLock::new(); | ||
| 262 | + *LEVEL.get_or_init(|| { | ||
| 263 | + #[cfg(target_arch = "x86_64")] | ||
| 264 | + { | ||
| 265 | + if std::arch::is_x86_feature_detected!("avx2") { | ||
| 266 | + return SimdLevel::Avx2; | ||
| 267 | + } | ||
| 268 | + if std::arch::is_x86_feature_detected!("sse4.1") { | ||
| 269 | + return SimdLevel::Sse41; | ||
| 270 | + } | ||
| 271 | + } | ||
| 272 | + SimdLevel::Scalar | ||
| 273 | + }) | ||
| 274 | +} | ||
| 275 | + | ||
| 276 | +/// The horizontal pass's inner loop over a contiguous window (`src` holds the | ||
| 277 | +/// row's whole footprint, `out.len() + 7` samples): SIMD where available, | ||
| 278 | +/// [`hpass_contig_scalar`] otherwise. Both produce the same `i32`s -- the | ||
| 279 | +/// SIMD form multiplies the same `i16` pairs, sums them in `i32` and applies | ||
| 280 | +/// the same `Round2(sum, InterRound0)`. | ||
| 281 | +#[allow(unsafe_code)] | ||
| 282 | +#[inline] | ||
| 283 | +fn hpass_contig(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | ||
| 284 | + #[cfg(target_arch = "x86_64")] | ||
| 285 | + { | ||
| 286 | + match simd_level() { | ||
| 287 | + SimdLevel::Avx2 => { | ||
| 288 | + // SAFETY: the AVX2 arm is reached only when the CPU reports | ||
| 289 | + // avx2, and the kernel reads at most `out.len() + 7` samples. | ||
| 290 | + unsafe { simd::hpass_avx2(src, t16, out) }; | ||
| 291 | + return; | ||
| 292 | + } | ||
| 293 | + SimdLevel::Sse41 => { | ||
| 294 | + // SAFETY: as above; the kernel needs only SSE2, which every | ||
| 295 | + // x86_64 CPU has. | ||
| 296 | + unsafe { simd::hpass_sse2(src, t16, out) }; | ||
| 297 | + return; | ||
| 298 | + } | ||
| 299 | + SimdLevel::Scalar => {} | ||
| 300 | + } | ||
| 301 | + } | ||
| 302 | + hpass_contig_scalar(src, t16, out); | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +/// The scalar reference for [`hpass_contig`] (lane-perf2's loop). | ||
| 306 | +fn hpass_contig_scalar(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | ||
| 307 | + // Both factors are 16-bit (a tap is |x| <= 128, a sample fits the bit | ||
| 308 | + // depth), so the products are exactly the i32 ones but the widening | ||
| 309 | + // multiply is one instruction per pair. | ||
| 310 | + for (o, w) in out.iter_mut().zip(src.windows(8)) { | ||
| 311 | + let mut sum = 0i32; | ||
| 312 | + for t in 0..8 { | ||
| 313 | + sum += i32::from(t16[t]) * i32::from(w[t] as i16); | ||
| 314 | + } | ||
| 315 | + *o = round2(sum, INTER_ROUND_0); | ||
| 316 | + } | ||
| 317 | +} | ||
| 318 | + | ||
| 319 | +/// lane-perf5: explicit `std::arch::x86_64` kernels for the two motion | ||
| 320 | +/// compensation passes -- the 18.7% of a 1080p decode that lane-perf4 left in | ||
| 321 | +/// `hpass_row` + `vpass_row`. Nothing here is spec logic: each kernel is the | ||
| 322 | +/// scalar function's arithmetic, lane-parallel, with identical rounding. | ||
| 323 | +#[allow(unsafe_code)] | ||
| 324 | +#[cfg(target_arch = "x86_64")] | ||
| 325 | +mod simd { | ||
| 326 | + use super::{round2, INTER_ROUND_0}; | ||
| 327 | + use std::arch::x86_64::*; | ||
| 328 | + | ||
| 329 | + /// `[t0,t1]`, `[t2,t3]`, `[t4,t5]`, `[t6,t7]` packed as `i32`s, the shape | ||
| 330 | + /// `_mm*_madd_epi16` wants: lane `j` of `madd(samples, set1(pair))` is | ||
| 331 | + /// `s[2j]*t_even + s[2j+1]*t_odd`, i.e. two taps of one output at once. | ||
| 332 | + #[inline] | ||
| 333 | + fn tap_pairs(t: &[i16; 8]) -> [i32; 4] { | ||
| 334 | + let pack = |a: i16, b: i16| (a as u16 as i32) | ((b as i32) << 16); | ||
| 335 | + [pack(t[0], t[1]), pack(t[2], t[3]), pack(t[4], t[5]), pack(t[6], t[7])] | ||
| 336 | + } | ||
| 337 | + | ||
| 338 | + /// Horizontal pass, AVX2: 16 outputs per iteration. Each 256-bit `madd` | ||
| 339 | + /// covers 8 outputs two taps at a time, but only every other output (the | ||
| 340 | + /// pairs are consecutive samples), so even and odd output columns are | ||
| 341 | + /// accumulated in two registers and interleaved back into order by | ||
| 342 | + /// `unpack` + `permute2x128`. | ||
| 343 | + /// | ||
| 344 | + /// # Safety | ||
| 345 | + /// Requires AVX2. `src.len()` must be at least `out.len() + 7`. | ||
| 346 | + #[target_feature(enable = "avx2")] | ||
| 347 | + pub unsafe fn hpass_avx2(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | ||
| 348 | + debug_assert!(src.len() >= out.len() + 7); | ||
| 349 | + let n = out.len(); | ||
| 350 | + let tp = tap_pairs(t16); | ||
| 351 | + let tv = [ | ||
| 352 | + _mm256_set1_epi32(tp[0]), | ||
| 353 | + _mm256_set1_epi32(tp[1]), | ||
| 354 | + _mm256_set1_epi32(tp[2]), | ||
| 355 | + _mm256_set1_epi32(tp[3]), | ||
| 356 | + ]; | ||
| 357 | + let rnd = _mm256_set1_epi32(1 << (INTER_ROUND_0 - 1)); | ||
| 358 | + let sp = src.as_ptr(); | ||
| 359 | + let op = out.as_mut_ptr(); | ||
| 360 | + let mut c = 0usize; | ||
| 361 | + while c + 16 <= n { | ||
| 362 | + let mut e = _mm256_setzero_si256(); | ||
| 363 | + let mut o = _mm256_setzero_si256(); | ||
| 364 | + for k in 0..4 { | ||
| 365 | + let se = _mm256_loadu_si256(sp.add(c + 2 * k).cast()); | ||
| 366 | + e = _mm256_add_epi32(e, _mm256_madd_epi16(se, tv[k])); | ||
| 367 | + let so = _mm256_loadu_si256(sp.add(c + 2 * k + 1).cast()); | ||
| 368 | + o = _mm256_add_epi32(o, _mm256_madd_epi16(so, tv[k])); | ||
| 369 | + } | ||
| 370 | + e = _mm256_srai_epi32(_mm256_add_epi32(e, rnd), INTER_ROUND_0 as i32); | ||
| 371 | + o = _mm256_srai_epi32(_mm256_add_epi32(o, rnd), INTER_ROUND_0 as i32); | ||
| 372 | + let lo = _mm256_unpacklo_epi32(e, o); | ||
| 373 | + let hi = _mm256_unpackhi_epi32(e, o); | ||
| 374 | + _mm256_storeu_si256(op.add(c).cast(), _mm256_permute2x128_si256(lo, hi, 0x20)); | ||
| 375 | + _mm256_storeu_si256(op.add(c + 8).cast(), _mm256_permute2x128_si256(lo, hi, 0x31)); | ||
| 376 | + c += 16; | ||
| 377 | + } | ||
| 378 | + if c < n { | ||
| 379 | + hpass_sse2(&src[c..], t16, &mut out[c..]); | ||
| 380 | + } | ||
| 381 | + } | ||
| 382 | + | ||
| 383 | + /// Vertical pass, AVX2: 8 output columns per iteration, one | ||
| 384 | + /// `_mm256_mullo_epi32` + `_mm256_add_epi32` per non-zero tap. Exact: | ||
| 385 | + /// an intermediate is at most ~2^17 and a tap at most 128, so the eight | ||
| 386 | + /// products stay well inside `i32` and `mullo`'s low half is the whole | ||
| 387 | + /// product. `acc` is left unrounded, as the scalar form leaves it. | ||
| 388 | + /// | ||
| 389 | + /// # Safety | ||
| 390 | + /// Requires AVX2. `intermediate` must hold `(row + 8) * block_w` elements | ||
| 391 | + /// and `acc` at least `block_w`. | ||
| 392 | + #[target_feature(enable = "avx2")] | ||
| 393 | + pub unsafe fn vpass_avx2( | ||
| 394 | + intermediate: &[i32], | ||
| 395 | + block_w: usize, | ||
| 396 | + row: usize, | ||
| 397 | + taps: &[i32; 8], | ||
| 398 | + acc: &mut [i32], | ||
| 399 | + ) { | ||
| 400 | + debug_assert!(intermediate.len() >= (row + 8) * block_w && acc.len() >= block_w); | ||
| 401 | + let ip = intermediate.as_ptr().add(row * block_w); | ||
| 402 | + let ap = acc.as_mut_ptr(); | ||
| 403 | + let mut c = 0usize; | ||
| 404 | + while c + 8 <= block_w { | ||
| 405 | + let mut v = _mm256_setzero_si256(); | ||
| 406 | + for (t, &tap) in taps.iter().enumerate() { | ||
| 407 | + if tap == 0 { | ||
| 408 | + continue; | ||
| 409 | + } | ||
| 410 | + let s = _mm256_loadu_si256(ip.add(t * block_w + c).cast()); | ||
| 411 | + v = _mm256_add_epi32(v, _mm256_mullo_epi32(_mm256_set1_epi32(tap), s)); | ||
| 412 | + } | ||
| 413 | + _mm256_storeu_si256(ap.add(c).cast(), v); | ||
| 414 | + c += 8; | ||
| 415 | + } | ||
| 416 | + // Widths are multiples of 4, so at most one 4-column tail is left. | ||
| 417 | + for cc in c..block_w { | ||
| 418 | + let mut sum = 0i32; | ||
| 419 | + for (t, &tap) in taps.iter().enumerate() { | ||
| 420 | + sum += tap * intermediate[(row + t) * block_w + cc]; | ||
| 421 | + } | ||
| 422 | + acc[cc] = sum; | ||
| 423 | + } | ||
| 424 | + } | ||
| 425 | + | ||
| 426 | + /// Vertical pass, SSE4.1 (`_mm_mullo_epi32` is SSE4.1, unlike the | ||
| 427 | + /// horizontal pass's SSE2 `madd`): [`vpass_avx2`] at 4 columns. | ||
| 428 | + /// | ||
| 429 | + /// # Safety | ||
| 430 | + /// Requires SSE4.1; same slice contract as [`vpass_avx2`]. | ||
| 431 | + #[target_feature(enable = "sse4.1")] | ||
| 432 | + pub unsafe fn vpass_sse41( | ||
| 433 | + intermediate: &[i32], | ||
| 434 | + block_w: usize, | ||
| 435 | + row: usize, | ||
| 436 | + taps: &[i32; 8], | ||
| 437 | + acc: &mut [i32], | ||
| 438 | + ) { | ||
| 439 | + debug_assert!(intermediate.len() >= (row + 8) * block_w && acc.len() >= block_w); | ||
| 440 | + let ip = intermediate.as_ptr().add(row * block_w); | ||
| 441 | + let ap = acc.as_mut_ptr(); | ||
| 442 | + let mut c = 0usize; | ||
| 443 | + while c + 4 <= block_w { | ||
| 444 | + let mut v = _mm_setzero_si128(); | ||
| 445 | + for (t, &tap) in taps.iter().enumerate() { | ||
| 446 | + if tap == 0 { | ||
| 447 | + continue; | ||
| 448 | + } | ||
| 449 | + let s = _mm_loadu_si128(ip.add(t * block_w + c).cast()); | ||
| 450 | + v = _mm_add_epi32(v, _mm_mullo_epi32(_mm_set1_epi32(tap), s)); | ||
| 451 | + } | ||
| 452 | + _mm_storeu_si128(ap.add(c).cast(), v); | ||
| 453 | + c += 4; | ||
| 454 | + } | ||
| 455 | + debug_assert_eq!(c, block_w, "every AV1 block width is a multiple of 4"); | ||
| 456 | + } | ||
| 457 | + | ||
| 458 | + /// Horizontal pass, SSE2: [`hpass_avx2`]'s scheme at 8 outputs per | ||
| 459 | + /// iteration; the remainder (a width-4 block, or a 4-column tail) is the | ||
| 460 | + /// scalar loop, which is where 4-tap kernels mostly land anyway. | ||
| 461 | + /// | ||
| 462 | + /// # Safety | ||
| 463 | + /// `src.len()` must be at least `out.len() + 7`. SSE2 is baseline x86_64. | ||
| 464 | + pub unsafe fn hpass_sse2(src: &[u16], t16: &[i16; 8], out: &mut [i32]) { | ||
| 465 | + debug_assert!(src.len() >= out.len() + 7); | ||
| 466 | + let n = out.len(); | ||
| 467 | + let tp = tap_pairs(t16); | ||
| 468 | + let tv = [ | ||
| 469 | + _mm_set1_epi32(tp[0]), | ||
| 470 | + _mm_set1_epi32(tp[1]), | ||
| 471 | + _mm_set1_epi32(tp[2]), | ||
| 472 | + _mm_set1_epi32(tp[3]), | ||
| 473 | + ]; | ||
| 474 | + let rnd = _mm_set1_epi32(1 << (INTER_ROUND_0 - 1)); | ||
| 475 | + let sp = src.as_ptr(); | ||
| 476 | + let op = out.as_mut_ptr(); | ||
| 477 | + let mut c = 0usize; | ||
| 478 | + while c + 8 <= n { | ||
| 479 | + let mut e = _mm_setzero_si128(); | ||
| 480 | + let mut o = _mm_setzero_si128(); | ||
| 481 | + for k in 0..4 { | ||
| 482 | + let se = _mm_loadu_si128(sp.add(c + 2 * k).cast()); | ||
| 483 | + e = _mm_add_epi32(e, _mm_madd_epi16(se, tv[k])); | ||
| 484 | + let so = _mm_loadu_si128(sp.add(c + 2 * k + 1).cast()); | ||
| 485 | + o = _mm_add_epi32(o, _mm_madd_epi16(so, tv[k])); | ||
| 486 | + } | ||
| 487 | + e = _mm_srai_epi32(_mm_add_epi32(e, rnd), INTER_ROUND_0 as i32); | ||
| 488 | + o = _mm_srai_epi32(_mm_add_epi32(o, rnd), INTER_ROUND_0 as i32); | ||
| 489 | + _mm_storeu_si128(op.add(c).cast(), _mm_unpacklo_epi32(e, o)); | ||
| 490 | + _mm_storeu_si128(op.add(c + 4).cast(), _mm_unpackhi_epi32(e, o)); | ||
| 491 | + c += 8; | ||
| 492 | + } | ||
| 493 | + for (o, w) in out[c..].iter_mut().zip(src[c..].windows(8)) { | ||
| 494 | + let mut sum = 0i32; | ||
| 495 | + for t in 0..8 { | ||
| 496 | + sum += i32::from(t16[t]) * i32::from(w[t] as i16); | ||
| 497 | + } | ||
| 498 | + *o = round2(sum, INTER_ROUND_0); | ||
| 499 | + } | ||
| 500 | + } | ||
| 501 | +} | ||
| 502 | + | ||
| 246 | /// lane-perf2: one row of the separable filter's horizontal pass, dav1d | 503 | /// lane-perf2: one row of the separable filter's horizontal pass, dav1d |
| 247 | /// style. When the row's whole 8-tap footprint (`x0-3 .. x0+block_w+4`) lies | 504 | /// style. When the row's whole 8-tap footprint (`x0-3 .. x0+block_w+4`) lies |
| 248 | /// inside the reference's true extent -- the overwhelmingly common case -- | 505 | /// inside the reference's true extent -- the overwhelmingly common case -- |
| @@ -264,13 +521,7 @@ fn hpass_row(reference: &[u16], row_base: usize, true_width: usize, x0: i32, tap | |||
| 264 | taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16, | 521 | taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16, |
| 265 | taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16, | 522 | taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16, |
| 266 | ]; | 523 | ]; |
| 267 | - for (o, w) in out.iter_mut().zip(src.windows(8)) { | 524 | + hpass_contig(src, &t16, out); |
| 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 | - } | ||
| 274 | return; | 525 | return; |
| 275 | } | 526 | } |
| 276 | } | 527 | } |
| @@ -332,14 +583,38 @@ fn horizontal_pass_unscaled( | |||
| 332 | /// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the | 583 | /// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the |
| 333 | /// eight slots. `acc` is left holding the unrounded sums. | 584 | /// eight slots. `acc` is left holding the unrounded sums. |
| 334 | #[inline] | 585 | #[inline] |
| 586 | +#[allow(unsafe_code)] | ||
| 335 | fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { | 587 | fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { |
| 336 | - acc.fill(0); | 588 | + #[cfg(target_arch = "x86_64")] |
| 589 | + { | ||
| 590 | + match simd_level() { | ||
| 591 | + // SAFETY: the arm is reached only when the CPU reports the | ||
| 592 | + // feature; the kernel reads `intermediate[(row+t)*block_w .. | ||
| 593 | + // + block_w]` for t in 0..8 and writes `acc[..block_w]`, which | ||
| 594 | + // the debug asserts pin. | ||
| 595 | + SimdLevel::Avx2 => { | ||
| 596 | + unsafe { simd::vpass_avx2(intermediate, block_w, row, taps, acc) }; | ||
| 597 | + return; | ||
| 598 | + } | ||
| 599 | + SimdLevel::Sse41 => { | ||
| 600 | + unsafe { simd::vpass_sse41(intermediate, block_w, row, taps, acc) }; | ||
| 601 | + return; | ||
| 602 | + } | ||
| 603 | + SimdLevel::Scalar => {} | ||
| 604 | + } | ||
| 605 | + } | ||
| 606 | + vpass_row_scalar(intermediate, block_w, row, taps, acc); | ||
| 607 | +} | ||
| 608 | + | ||
| 609 | +/// The scalar reference for [`vpass_row`] (lane-perf2's tap-major loop). | ||
| 610 | +fn vpass_row_scalar(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { | ||
| 611 | + acc[..block_w].fill(0); | ||
| 337 | for (t, &tap) in taps.iter().enumerate() { | 612 | for (t, &tap) in taps.iter().enumerate() { |
| 338 | if tap == 0 { | 613 | if tap == 0 { |
| 339 | continue; | 614 | continue; |
| 340 | } | 615 | } |
| 341 | let base = (row + t) * block_w; | 616 | let base = (row + t) * block_w; |
| 342 | - for (a, &s) in acc.iter_mut().zip(&intermediate[base..base + block_w]) { | 617 | + for (a, &s) in acc[..block_w].iter_mut().zip(&intermediate[base..base + block_w]) { |
| 343 | *a += tap * s; | 618 | *a += tap * s; |
| 344 | } | 619 | } |
| 345 | } | 620 | } |
| @@ -1521,4 +1796,129 @@ mod tests { | |||
| 1521 | } | 1796 | } |
| 1522 | } | 1797 | } |
| 1523 | } | 1798 | } |
| 1799 | + | ||
| 1800 | + /// lane-perf5: every explicit-SIMD kernel is checked against the scalar | ||
| 1801 | + /// reference it replaces, over the whole domain a decode can hand it -- | ||
| 1802 | + /// all block widths (4..=128), all 16 sub-pel phases, all four filter | ||
| 1803 | + /// kinds with both their wide and narrow (4-tap) tables, and 8/10/12-bit | ||
| 1804 | + /// sample ranges. The dispatch itself is exercised through | ||
| 1805 | + /// [`super::hpass_contig`] (whatever this CPU selected). | ||
| 1806 | + #[allow(unsafe_code)] | ||
| 1807 | + #[test] | ||
| 1808 | + fn simd_matches_scalar_horizontal_pass() { | ||
| 1809 | + let mut state = 0x2545_f491_4f6c_dd1du64; | ||
| 1810 | + let mut rng = move || { | ||
| 1811 | + state ^= state << 13; | ||
| 1812 | + state ^= state >> 7; | ||
| 1813 | + state ^= state << 17; | ||
| 1814 | + state | ||
| 1815 | + }; | ||
| 1816 | + let kinds = [ | ||
| 1817 | + InterpFilterKind::Regular, | ||
| 1818 | + InterpFilterKind::Smooth, | ||
| 1819 | + InterpFilterKind::Sharp, | ||
| 1820 | + InterpFilterKind::Bilinear, | ||
| 1821 | + ]; | ||
| 1822 | + let mut checked = 0usize; | ||
| 1823 | + for depth_max in [255u16, 1023, 4095] { | ||
| 1824 | + for &kind in &kinds { | ||
| 1825 | + let (wide, narrow) = kind.tables(); | ||
| 1826 | + for table in [wide, narrow] { | ||
| 1827 | + for frac in 0..16usize { | ||
| 1828 | + let taps = &table[frac]; | ||
| 1829 | + let t16 = [ | ||
| 1830 | + taps[0] as i16, taps[1] as i16, taps[2] as i16, taps[3] as i16, | ||
| 1831 | + taps[4] as i16, taps[5] as i16, taps[6] as i16, taps[7] as i16, | ||
| 1832 | + ]; | ||
| 1833 | + for w in [4usize, 8, 12, 16, 20, 32, 64, 128] { | ||
| 1834 | + let src: Vec<u16> = (0..w + 7) | ||
| 1835 | + .map(|_| (rng() % (u64::from(depth_max) + 1)) as u16) | ||
| 1836 | + .collect(); | ||
| 1837 | + let mut want = vec![0i32; w]; | ||
| 1838 | + super::hpass_contig_scalar(&src, &t16, &mut want); | ||
| 1839 | + let mut got = vec![0i32; w]; | ||
| 1840 | + super::hpass_contig(&src, &t16, &mut got); | ||
| 1841 | + assert_eq!(got, want, "dispatch w={w} frac={frac} max={depth_max}"); | ||
| 1842 | + #[cfg(target_arch = "x86_64")] | ||
| 1843 | + { | ||
| 1844 | + let mut sse = vec![0i32; w]; | ||
| 1845 | + unsafe { super::simd::hpass_sse2(&src, &t16, &mut sse) }; | ||
| 1846 | + assert_eq!(sse, want, "sse w={w} frac={frac} max={depth_max}"); | ||
| 1847 | + if std::arch::is_x86_feature_detected!("avx2") { | ||
| 1848 | + let mut avx = vec![0i32; w]; | ||
| 1849 | + unsafe { super::simd::hpass_avx2(&src, &t16, &mut avx) }; | ||
| 1850 | + assert_eq!(avx, want, "avx2 w={w} frac={frac} max={depth_max}"); | ||
| 1851 | + } | ||
| 1852 | + } | ||
| 1853 | + checked += 1; | ||
| 1854 | + } | ||
| 1855 | + } | ||
| 1856 | + } | ||
| 1857 | + } | ||
| 1858 | + } | ||
| 1859 | + assert_eq!(checked, 3 * 4 * 2 * 16 * 8); | ||
| 1860 | + } | ||
| 1861 | + | ||
| 1862 | + /// lane-perf5: the vertical pass's SIMD kernels against the scalar | ||
| 1863 | + /// reference, over the same domain as | ||
| 1864 | + /// [`simd_matches_scalar_horizontal_pass`] -- all widths, all phases, | ||
| 1865 | + /// both wide and narrow (4-tap) tables of all four filter kinds, and | ||
| 1866 | + /// intermediates spanning the 8/10/12-bit ranges the horizontal pass can | ||
| 1867 | + /// produce (a 12-bit sample times the filter gain, both signs). | ||
| 1868 | + #[allow(unsafe_code)] | ||
| 1869 | + #[test] | ||
| 1870 | + fn simd_matches_scalar_vertical_pass() { | ||
| 1871 | + let mut state = 0x9e37_79b9_7f4a_7c15u64; | ||
| 1872 | + let mut rng = move || { | ||
| 1873 | + state ^= state << 13; | ||
| 1874 | + state ^= state >> 7; | ||
| 1875 | + state ^= state << 17; | ||
| 1876 | + state | ||
| 1877 | + }; | ||
| 1878 | + let kinds = [ | ||
| 1879 | + InterpFilterKind::Regular, | ||
| 1880 | + InterpFilterKind::Smooth, | ||
| 1881 | + InterpFilterKind::Sharp, | ||
| 1882 | + InterpFilterKind::Bilinear, | ||
| 1883 | + ]; | ||
| 1884 | + let mut checked = 0usize; | ||
| 1885 | + for span in [255i32 * 16, 1023 * 16, 4095 * 22] { | ||
| 1886 | + for &kind in &kinds { | ||
| 1887 | + let (wide, narrow) = kind.tables(); | ||
| 1888 | + for table in [wide, narrow] { | ||
| 1889 | + for frac in 0..16usize { | ||
| 1890 | + let taps = &table[frac]; | ||
| 1891 | + for w in [4usize, 8, 12, 16, 20, 32, 64, 128] { | ||
| 1892 | + let rows = 12usize; | ||
| 1893 | + let inter: Vec<i32> = (0..rows * w) | ||
| 1894 | + .map(|_| (rng() % (2 * span as u64 + 1)) as i32 - span) | ||
| 1895 | + .collect(); | ||
| 1896 | + for row in [0usize, 1, 4] { | ||
| 1897 | + let mut want = vec![0i32; w]; | ||
| 1898 | + super::vpass_row_scalar(&inter, w, row, taps, &mut want); | ||
| 1899 | + let mut got = vec![0i32; w]; | ||
| 1900 | + super::vpass_row(&inter, w, row, taps, &mut got); | ||
| 1901 | + assert_eq!(got, want, "dispatch w={w} frac={frac} row={row}"); | ||
| 1902 | + #[cfg(target_arch = "x86_64")] | ||
| 1903 | + { | ||
| 1904 | + if std::arch::is_x86_feature_detected!("sse4.1") { | ||
| 1905 | + let mut sse = vec![0i32; w]; | ||
| 1906 | + unsafe { super::simd::vpass_sse41(&inter, w, row, taps, &mut sse) }; | ||
| 1907 | + assert_eq!(sse, want, "sse4.1 w={w} frac={frac} row={row}"); | ||
| 1908 | + } | ||
| 1909 | + if std::arch::is_x86_feature_detected!("avx2") { | ||
| 1910 | + let mut avx = vec![0i32; w]; | ||
| 1911 | + unsafe { super::simd::vpass_avx2(&inter, w, row, taps, &mut avx) }; | ||
| 1912 | + assert_eq!(avx, want, "avx2 w={w} frac={frac} row={row}"); | ||
| 1913 | + } | ||
| 1914 | + } | ||
| 1915 | + checked += 1; | ||
| 1916 | + } | ||
| 1917 | + } | ||
| 1918 | + } | ||
| 1919 | + } | ||
| 1920 | + } | ||
| 1921 | + } | ||
| 1922 | + assert_eq!(checked, 3 * 4 * 2 * 16 * 8 * 3); | ||
| 1923 | + } | ||
| 1524 | } | 1924 | } |