forge
Explore
Log in

Don't you dare go Hollow

Revert "ec-vp8 perf: idct_add_into -- fused strided reconstruct + AVX2 kernel"

Browse files at this commit

bb830d83ed5c9ac0a313d29f8d694d9706c7a8b3Ahmet Kaan GümüşParent: abc2cab
This reverts commit 4d6a9f2729070667f54554f1489603e0d29d0824.
crates/ec-vp8/src/decode.rsmodified+12 -4
@@ -1305,9 +1305,7 @@ fn ymode_as_bmode(y: u8) -> u8 {
1305 1305
1306 /// Add one 4x4 block's dequantized-coefficient residual to its1306 /// Add one 4x4 block's dequantized-coefficient residual to its
1307 /// prediction: reads the prediction patch from `pred`, clamps, writes1307 /// prediction: reads the prediction patch from `pred`, clamps, writes
1308-/// into the strided plane (§14.4/§14.5). The transform, the add and the1308+/// into the strided plane (§14.4/§14.5).
1309-/// strided store live in `transform::idct_add_strided` (scalar reference
1310-/// + AVX2 kernel).
1311 fn idct_add_into(1309 fn idct_add_into(
1312 plane: &mut [u8],1310 plane: &mut [u8],
1313 coeffs: &[i16; 16],1311 coeffs: &[i16; 16],
@@ -1317,5 +1315,15 @@ fn idct_add_into(
1317 dst_off: usize,1315 dst_off: usize,
1318 dst_stride: usize,1316 dst_stride: usize,
1319 ) {1317 ) {
1320- transform::idct_add_strided(plane, coeffs, pred, pred_stride, pred_off, dst_off, dst_stride);1318+ let mut p = [0u8; 16];
1319+ for i in 0..4 {
1320+ let s = pred_off + i * pred_stride;
1321+ p[i * 4..i * 4 + 4].copy_from_slice(&pred[s..s + 4]);
1322+ }
1323+ let mut out = [0u8; 16];
1324+ transform::idct4x4_add(coeffs, &p, &mut out);
1325+ for i in 0..4 {
1326+ let d = dst_off + i * dst_stride;
1327+ plane[d..d + 4].copy_from_slice(&out[i * 4..i * 4 + 4]);
1328+ }
1321 }1329 }
crates/ec-vp8/src/lib.rsmodified+1 -12
@@ -9,10 +9,7 @@
9 //!9 //!
10 //! # Unsafe10 //! # Unsafe
11 //!11 //!
12-//! The decoder logic is safe Rust. The one exception is the explicit12+//! This crate contains no `unsafe` code.
13-//! `std::arch::x86_64` SIMD kernels in `mc` and `transform` (each confined
14-//! to its own `#[allow(unsafe_code)]` module, dispatch-gated at run time
15-//! and lane-for-lane tested against its scalar reference).
16 13
17 #![deny(unsafe_code)]14 #![deny(unsafe_code)]
18 #![warn(missing_docs)]15 #![warn(missing_docs)]
@@ -66,11 +63,3 @@ pub(crate) fn debug_enabled() -> bool {
66 _ => true,63 _ => true,
67 }64 }
68 }65 }
69-
70-/// Cached AVX2 availability for the explicit-SIMD kernels (`mc`, `transform`).
71-#[cfg(target_arch = "x86_64")]
72-pub(crate) fn avx2_supported() -> bool {
73- static AVX2: std::sync::LazyLock<bool> =
74- std::sync::LazyLock::new(|| std::arch::is_x86_feature_detected!("avx2"));
75- *AVX2
76-}
crates/ec-vp8/src/transform.rsmodified+0 -303
@@ -242,243 +242,6 @@ pub fn idct4x4_add(coeffs: &[i16; 16], predict: &[u8], recon: &mut [u8]) {
242 }242 }
243 }243 }
244 244
245-/// Fused per-block reconstruction with strided prediction and strided
246-/// store (the per-4x4 hot path of `decode::idct_add_into`): inverse-
247-/// transforms `coeffs`, adds the 4x4 prediction block at
248-/// `pred[pred_off..]` (`pred_stride` between rows), clamps to 0..=255 and
249-/// stores into `plane` at `dst_off` (`dst_stride` between rows).
250-///
251-/// Dispatches to the AVX2 kernel when available; the scalar path is the
252-/// libvpx transcription and the lane-for-lane oracle.
253-pub(crate) fn idct_add_strided(
254- plane: &mut [u8],
255- coeffs: &[i16; 16],
256- pred: &[u8],
257- pred_stride: usize,
258- pred_off: usize,
259- dst_off: usize,
260- dst_stride: usize,
261-) {
262- debug_assert!(pred.len() >= pred_off + 3 * pred_stride + 4);
263- debug_assert!(plane.len() >= dst_off + 3 * dst_stride + 4);
264-
265- #[cfg(target_arch = "x86_64")]
266- {
267- if crate::avx2_supported() {
268- // SAFETY: AVX2 checked by `avx2_supported`; the bounds
269- // contracts are the debug_asserts above.
270- #[allow(unsafe_code)]
271- unsafe {
272- simd::idct_add_strided(coeffs, pred, pred_stride, pred_off, plane, dst_off, dst_stride);
273- }
274- return;
275- }
276- }
277-
278- let residual = idct4x4(coeffs);
279- for row in 0..4 {
280- let s = pred_off + row * pred_stride;
281- let d = dst_off + row * dst_stride;
282- for col in 0..4 {
283- plane[d + col] = (i32::from(residual[row * 4 + col])
284- + i32::from(pred[s + col]))
285- .clamp(0, 255) as u8;
286- }
287- }
288-}
289-
290-/// Explicit `std::arch::x86_64` kernel for [`idct_add_strided`].
291-///
292-/// The butterflies are the scalar `i32` arithmetic lane-parallel -- the
293-/// precision contract of this module is kept in full: products are exact
294-/// (`|i16 * K| <= 32767 * 35468 < 2^31`), butterfly sums never truncate
295-/// mid-network, and the ONLY narrowing points are the oracle's `short`
296-/// stores (pass one's `tmp`, pass two's residual), each a low-16-bit
297-/// truncation (`shuffle_epi8` byte picks), never saturation. The final
298-/// clamp is explicit `max 0 / min 255` in `i32` before a saturating pack
299-/// that therefore never fires beyond the clamp.
300-///
301-/// Lane layout through the two passes (one AVX2 register holds two 4-lane
302-/// groups; lane `4g + l` = group `g`, element `l`):
303-/// - coefficients are transposed to column groups (a column = a group);
304-/// the pass pairs lanes `(l0, l2)` / `(l1, l3)` = rows `(0, 2)` /
305-/// `(1, 3)` of that column, and its outputs land row-major
306-/// (the libvpx store-pattern transpose),
307-/// - a 3-unpack transpose hands pass two row groups,
308-/// - pass two pairs `(ip0, ip2)` / `(ip1, ip3)` of each row.
309-#[cfg(target_arch = "x86_64")]
310-#[allow(unsafe_code)]
311-#[allow(unsafe_op_in_unsafe_fn)] // the kernel's whole body is its contract
312-mod simd {
313- use super::{COSPI8SQRT2MINUS1, SINPI8SQRT2};
314- use std::arch::x86_64::*;
315-
316- /// `shuffle_epi8` mask: keep the low 16 bits of `i32` lane 0 of each
317- /// 128-bit half.
318- const TAKE0: [u8; 16] = [
319- 0, 1, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0, 1, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
320- ];
321- /// `shuffle_epi8` masks picking column `c`'s four `i16`s out of the
322- /// row-major 4x4 block held in one `ymm` (element `(r, c)` sits at
323- /// byte `8r + 2c`): the low 128-bit half gathers rows 0, 1, the high
324- /// half rows 2, 3.
325- const COLPAIR_MASKS: [[u8; 16]; 4] = [
326- [0, 1, 8, 9, 0x80, 0x80, 0x80, 0x80, 0, 1, 8, 9, 0x80, 0x80, 0x80, 0x80],
327- [2, 3, 10, 11, 0x80, 0x80, 0x80, 0x80, 2, 3, 10, 11, 0x80, 0x80, 0x80, 0x80],
328- [4, 5, 12, 13, 0x80, 0x80, 0x80, 0x80, 4, 5, 12, 13, 0x80, 0x80, 0x80, 0x80],
329- [6, 7, 14, 15, 0x80, 0x80, 0x80, 0x80, 6, 7, 14, 15, 0x80, 0x80, 0x80, 0x80],
330- ];
331-
332- #[inline]
333- fn shuffle_mask(m: &[u8; 16]) -> __m128i {
334- // SAFETY: reads a 16-byte constant.
335- unsafe { _mm_loadu_si128(m.as_ptr().cast()) }
336- }
337-
338- /// One 4-point inverse-DCT butterfly pass over the two 4-lane groups
339- /// of `x` (`i32` lanes), with the second pass's `(x + 4) >> 3` when
340- /// `SHIFT` is 3 (pass one runs with `SHIFT = 0`, `rnd = 0`).
341- ///
342- /// Returns `i16`-truncated values packed as eight `i16`s in one
343- /// `xmm`: groups `g_a`, `g_b` in the low/high 64 bits.
344- ///
345- /// # Safety
346- /// Requires AVX2.
347- #[target_feature(enable = "avx2")]
348- unsafe fn pass<const SHIFT: i32>(
349- x: __m256i,
350- sin: __m256i,
351- cos: __m256i,
352- rnd: __m256i,
353- ) -> __m128i {
354- // Dup the butterfly partners per half: [l0,l0,l2,l2], [l2,l2,l0,l0],
355- // [l1,l1,l3,l3], [l3,l3,l1,l1].
356- let x02 = _mm256_shuffle_epi32::<0xA0>(x);
357- let x20 = _mm256_shuffle_epi32::<0x0A>(x);
358- let x13 = _mm256_shuffle_epi32::<0xF5>(x);
359- let x31 = _mm256_shuffle_epi32::<0x5F>(x);
360- let a1 = _mm256_add_epi32(x02, x20); // l0 + l2, every lane
361- let b1 = _mm256_sub_epi32(x02, x20); // lanes 0,1 = l0 - l2
362- // `(v * K) >> 16`, exact in `i32` for every `i16` `v`.
363- let ms =
364- |v: __m256i, k: __m256i| _mm256_srai_epi32::<16>(_mm256_mullo_epi32(v, k));
365- let c1 = _mm256_sub_epi32(ms(x13, sin), _mm256_add_epi32(x31, ms(x31, cos)));
366- let d1 = _mm256_add_epi32(_mm256_add_epi32(x13, ms(x13, cos)), ms(x31, sin));
367- let y = |v: __m256i| {
368- _mm256_srai_epi32::<SHIFT>(_mm256_add_epi32(v, rnd))
369- };
370- let y0 = y(_mm256_add_epi32(a1, d1));
371- let y1 = y(_mm256_add_epi32(b1, c1));
372- let y2 = y(_mm256_sub_epi32(b1, c1));
373- let y3 = y(_mm256_sub_epi32(a1, d1));
374- // Truncate each output to `i16` (the oracle's store points) and
375- // assemble group lanes [y0, y1, y2, y3]. Only lane 0 of every y*
376- // holds the true value: `b1`'s lanes 2,3 hold the NEGATED
377- // butterfly (x20 puts l2 first there) and `d1`/`c1`'s lanes 2,3
378- // hold the (l1, l3) pair swapped, so the lane-2 dups of TAKE02
379- // would feed garbage into the assembly.
380- let take0 = _mm256_broadcastsi128_si256(shuffle_mask(&TAKE0));
381- let z0 = _mm256_shuffle_epi8(y0, take0);
382- let z1 = _mm256_shuffle_epi8(y1, take0);
383- let z2 = _mm256_shuffle_epi8(y2, take0);
384- let z3 = _mm256_shuffle_epi8(y3, take0);
385- let w01 = _mm256_unpacklo_epi16(z0, z1);
386- let w23 = _mm256_unpacklo_epi16(z2, z3);
387- let packed = _mm256_unpacklo_epi32(w01, w23);
388- // Each half of `packed` is [group's y0..y3 | dup bytes]; weave the
389- // two halves' low 64 bits into [g_a 4 i16 | g_b 4 i16].
390- let p128 = _mm256_castsi256_si128(packed);
391- let p128hi = _mm256_extracti128_si256::<1>(packed);
392- _mm_unpacklo_epi64(p128, p128hi)
393- }
394-
395- /// [`super::idct_add_strided`]'s AVX2 body: see the module docs for
396- /// the lane layout through the two passes.
397- ///
398- /// # Safety
399- /// Requires AVX2. `pred[pred_off..]` must hold `3 * pred_stride + 4`
400- /// bytes and `plane[dst_off..]` `3 * dst_stride + 4`.
401- pub(super) unsafe fn idct_add_strided(
402- coeffs: &[i16; 16],
403- pred: &[u8],
404- pred_stride: usize,
405- pred_off: usize,
406- plane: &mut [u8],
407- dst_off: usize,
408- dst_stride: usize,
409- ) {
410- debug_assert!(pred.len() >= pred_off + 3 * pred_stride + 4);
411- debug_assert!(plane.len() >= dst_off + 3 * dst_stride + 4);
412- let sin = _mm256_set1_epi32(SINPI8SQRT2);
413- let cos = _mm256_set1_epi32(COSPI8SQRT2MINUS1);
414- let zero = _mm256_setzero_si256();
415-
416- // Coefficients -> column groups (a group = one column, rows 0..3),
417- // widened to the butterflies' `i32` lanes. The whole 32-byte block
418- // is one ymm; each gather pulls column c's rows 0,1 (low half) and
419- // 2,3 (high half) and the unpack weaves them into one `xmm`.
420- let cx = _mm256_loadu_si256(coeffs.as_ptr().cast());
421- let col = |c: usize| {
422- // The pair mask is the same 8 useful bytes in both halves, so
423- // broadcasting the 128-bit mask covers both.
424- let pair = _mm256_shuffle_epi8(cx, _mm256_broadcastsi128_si256(shuffle_mask(&COLPAIR_MASKS[c])));
425- // rows 0,1 sit in the low half's bytes 0-3, rows 2,3 in the
426- // high half's -- `unpacklo_epi32` concatenates exactly those.
427- _mm_cvtepi16_epi32(_mm_unpacklo_epi32(
428- _mm256_castsi256_si128(pair),
429- _mm256_extracti128_si256::<1>(pair),
430- ))
431- };
432- let cx01 = _mm256_inserti128_si256(_mm256_castsi128_si256(col(0)), col(1), 1);
433- let cx23 = _mm256_inserti128_si256(_mm256_castsi128_si256(col(2)), col(3), 1);
434-
435- // Pass one: the row-major `short tmp` (the store-pattern
436- // transpose); each result xmm holds the tmp columns (c, c + 1)
437- // column-major.
438- let t01 = pass::<0>(cx01, sin, cos, zero);
439- let t23 = pass::<0>(cx23, sin, cos, zero);
440-
441- // Weave the column pairs into rows 0..4: `a` holds columns
442- // (0, 2) rows 0..4, `b` columns (1, 3); low/high interleave
443- // splits them into row-major rows (0, 1) / (2, 3).
444- let a = _mm_unpacklo_epi16(t01, t23);
445- let b = _mm_unpackhi_epi16(t01, t23);
446- let rows01 = _mm_unpacklo_epi16(a, b);
447- let rows23 = _mm_unpackhi_epi16(a, b);
448-
449- // Pass two on row groups, with the §14.4 rounding constant.
450- let r0 = _mm_cvtepi16_epi32(rows01);
451- let r1 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(rows01));
452- let r2 = _mm_cvtepi16_epi32(rows23);
453- let r3 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(rows23));
454- let rx01 = _mm256_inserti128_si256(_mm256_castsi128_si256(r0), r1, 1);
455- let rx23 = _mm256_inserti128_si256(_mm256_castsi128_si256(r2), r3, 1);
456- let rnd = _mm256_set1_epi32(4);
457- let y01 = pass::<3>(rx01, sin, cos, rnd); // rows 0, 1
458- let y23 = pass::<3>(rx23, sin, cos, rnd); // rows 2, 3
459-
460- // Residual (i16, truncated in `pass`) + prediction, clamp, store.
461- let mut add_store = |row16: __m128i, s_off: usize, d_off: usize| {
462- let mut q = [0u8; 4];
463- q.copy_from_slice(&pred[s_off..s_off + 4]);
464- let pv = _mm_cvtepu8_epi32(_mm_cvtsi32_si128(u32::from_le_bytes(q) as i32));
465- let s = _mm_add_epi32(_mm_cvtepi16_epi32(row16), pv);
466- let c = _mm_min_epi32(_mm_max_epi32(s, _mm_setzero_si128()), _mm_set1_epi32(255));
467- let u16 = _mm_packus_epi32(c, c);
468- let u8 = _mm_packus_epi16(u16, u16);
469- _mm_storeu_si32(plane.as_mut_ptr().add(d_off).cast(), u8);
470- };
471- add_store(y01, pred_off, dst_off);
472- add_store(_mm_srli_si128::<8>(y01), pred_off + pred_stride, dst_off + dst_stride);
473- add_store(y23, pred_off + 2 * pred_stride, dst_off + 2 * dst_stride);
474- add_store(
475- _mm_srli_si128::<8>(y23),
476- pred_off + 3 * pred_stride,
477- dst_off + 3 * dst_stride,
478- );
479- }
480-}
481-
482 /// Dequantization factors for one macroblock, indexed by plane and245 /// Dequantization factors for one macroblock, indexed by plane and
483 /// coefficient type (RFC 6386 §14.1; dixie `struct dequant_factors`).246 /// coefficient type (RFC 6386 §14.1; dixie `struct dequant_factors`).
484 #[derive(Clone, Copy, Debug, PartialEq, Eq)]247 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -874,70 +637,4 @@ mod tests {
874 }637 }
875 }638 }
876 }639 }
877-
878- /// AVX2 [`simd::idct_add_strided`] vs the scalar reference. The i32
879- /// lane network is exact for EVERY `i16` coefficient (products < 2^31,
880- /// truncations only at the oracle's `short` stores), so the
881- /// equivalence test pushes coefficients to the rails on purpose:
882- /// magnitudes at ±32767 put `i16 * 35468` within a factor of two of
883- /// the `i32` limit and the butterflies at their wrapping boundary --
884- /// exactly where a lane-order or precision slip would show.
885- #[test]
886- #[cfg(target_arch = "x86_64")]
887- fn simd_idct_add_strided_matches_the_scalar_reference() {
888- if !crate::avx2_supported() {
889- return;
890- }
891- let mut s = 0x9E37_79B9_7F4A_7C15u64;
892- let mut rnd = move || {
893- s ^= s << 13;
894- s ^= s >> 7;
895- s ^= s << 17;
896- s
897- };
898- for case in 0..4000u32 {
899- let mut coeffs = [0i16; 16];
900- for c in coeffs.iter_mut() {
901- *c = match rnd() % 4 {
902- 0 => i16::MAX,
903- 1 => i16::MIN,
904- _ => rnd() as i16,
905- };
906- }
907- let pred: Vec<u8> = (0..2048).map(|_| rnd() as u8).collect();
908- let pred_stride = [4usize, 8, 16, 24][(rnd() % 4) as usize];
909- let pred_off = (rnd() as usize) % 1024;
910- let dst_stride = [16usize, 32, 48][(rnd() % 3) as usize];
911- let dst_off = (rnd() as usize) % 1024;
912- debug_assert!(pred_off + 3 * pred_stride + 4 <= 2048);
913-
914- // Scalar reference (the libvpx transcription).
915- let mut want = vec![7u8; 4096];
916- let residual = idct4x4(&coeffs);
917- for row in 0..4 {
918- for col in 0..4 {
919- want[dst_off + row * dst_stride + col] = (i32::from(
920- residual[row * 4 + col],
921- ) + i32::from(pred[pred_off + row * pred_stride + col]))
922- .clamp(0, 255) as u8;
923- }
924- }
925-
926- let mut got = vec![7u8; 4096];
927- // SAFETY: avx2_supported() checked above.
928- #[allow(unsafe_code)]
929- unsafe {
930- simd::idct_add_strided(
931- &coeffs,
932- &pred,
933- pred_stride,
934- pred_off,
935- &mut got,
936- dst_off,
937- dst_stride,
938- );
939- }
940- assert_eq!(got, want, "case {case}");
941- }
942- }
943 }640 }