The AVX2 column pass against the scalar network over the full i32 range
Browse files at this commit1b7f34375c9a390521fb43ca807e145375c47843Parent: ef53b7e- test
- the AVX2 column pass against the scalar network over the full i32 range
1 changed file +84 -8
crates/ec-av1/src/transform.rsmodified+84 -8
| @@ -221,6 +221,23 @@ mod simd { | |||
| 221 | } | 221 | } |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | + /// [`super::inverse_dct_fixed`] over eight AVX2 lanes -- the network the | ||
| 225 | + /// column pass below runs, reachable on its own for the equivalence gate. | ||
| 226 | + /// | ||
| 227 | + /// # Safety | ||
| 228 | + /// The caller has checked for AVX2 ([`crate::mc::simd_level`]). | ||
| 229 | + #[target_feature(enable = "avx2")] | ||
| 230 | + pub(super) unsafe fn dct_x8_avx2(t: &mut [M256; 64], n: u32, r: usize) { | ||
| 231 | + match n { | ||
| 232 | + 2 => inverse_dct_n::<M256, 2>(t, r), | ||
| 233 | + 3 => inverse_dct_n::<M256, 3>(t, r), | ||
| 234 | + 4 => inverse_dct_n::<M256, 4>(t, r), | ||
| 235 | + 5 => inverse_dct_n::<M256, 5>(t, r), | ||
| 236 | + 6 => inverse_dct_n::<M256, 6>(t, r), | ||
| 237 | + _ => unreachable!("the inverse DCT is defined for 4..64"), | ||
| 238 | + } | ||
| 239 | + } | ||
| 240 | + | ||
| 224 | /// The whole DCT column pass of one transform unit: `residual` is the | 241 | /// The whole DCT column pass of one transform unit: `residual` is the |
| 225 | /// row-pass output (`h` rows of `w`), `out` the residual it writes. | 242 | /// row-pass output (`h` rows of `w`), `out` the residual it writes. |
| 226 | /// | 243 | /// |
| @@ -248,14 +265,8 @@ mod simd { | |||
| 248 | if _mm256_testz_si256(any, any) == 1 { | 265 | if _mm256_testz_si256(any, any) == 1 { |
| 249 | continue; | 266 | continue; |
| 250 | } | 267 | } |
| 251 | - match log2h { | 268 | + // SAFETY: this function's own AVX2 contract. |
| 252 | - 2 => inverse_dct_n::<M256, 2>(&mut t, r), | 269 | + unsafe { dct_x8_avx2(&mut t, log2h, r) }; |
| 253 | - 3 => inverse_dct_n::<M256, 3>(&mut t, r), | ||
| 254 | - 4 => inverse_dct_n::<M256, 4>(&mut t, r), | ||
| 255 | - 5 => inverse_dct_n::<M256, 5>(&mut t, r), | ||
| 256 | - 6 => inverse_dct_n::<M256, 6>(&mut t, r), | ||
| 257 | - _ => unreachable!("the inverse DCT is defined for 4..64"), | ||
| 258 | - } | ||
| 259 | // `round2(v, 4)` on eight lanes: the shift is arithmetic here | 270 | // `round2(v, 4)` on eight lanes: the shift is arithmetic here |
| 260 | // because the value stays in its own 32-bit lane. | 271 | // because the value stays in its own 32-bit lane. |
| 261 | let rnd = _mm256_set1_epi32(8); | 272 | let rnd = _mm256_set1_epi32(8); |
| @@ -2165,6 +2176,71 @@ mod tests { | |||
| 2165 | /// The eight-wide column kernel is the scalar network, lane for lane: a | 2176 | /// The eight-wide column kernel is the scalar network, lane for lane: a |
| 2166 | /// pseudo-random sweep over the full coefficient range at every size and | 2177 | /// pseudo-random sweep over the full coefficient range at every size and |
| 2167 | /// both clamp widths (`bit_depth` 8 and 10) has to agree bit for bit, or | 2178 | /// both clamp widths (`bit_depth` 8 and 10) has to agree bit for bit, or |
| 2179 | + /// The AVX2 column pass against the scalar network over the FULL `i32` | ||
| 2180 | + /// range, not only the dequantizer's own bound: the explicit intrinsics | ||
| 2181 | + /// claim exact 64-bit products, so the sweep includes `i32::MIN`/`MAX`, | ||
| 2182 | + /// both clamp boundaries at every `r` the decoder uses, and uniform | ||
| 2183 | + /// full-range noise. A single differing lane at any size would be a | ||
| 2184 | + /// silent pixel drift on whatever stream first reaches it. | ||
| 2185 | + #[cfg(target_arch = "x86_64")] | ||
| 2186 | + #[test] | ||
| 2187 | + fn the_avx2_dct_matches_the_scalar_one_over_the_full_range() { | ||
| 2188 | + if !std::arch::is_x86_feature_detected!("avx2") { | ||
| 2189 | + return; | ||
| 2190 | + } | ||
| 2191 | + let mut state = 0x9E37_79B9_7F4A_7C15u64; | ||
| 2192 | + let mut next = move || { | ||
| 2193 | + state ^= state << 13; | ||
| 2194 | + state ^= state >> 7; | ||
| 2195 | + state ^= state << 17; | ||
| 2196 | + state | ||
| 2197 | + }; | ||
| 2198 | + for n in 2..=6u32 { | ||
| 2199 | + for &r in &[16usize, 18, 20] { | ||
| 2200 | + let (hi, lo) = (((1i64 << (r - 1)) - 1) as i32, (-(1i64 << (r - 1))) as i32); | ||
| 2201 | + let edges = [0, 1, -1, hi, lo, hi - 1, lo + 1, i32::MAX, i32::MIN]; | ||
| 2202 | + for round in 0..96 { | ||
| 2203 | + let mut wide = [[0i32; 8]; 64]; | ||
| 2204 | + let mut cols = [[0i32; 64]; 8]; | ||
| 2205 | + for i in 0..(1usize << n) { | ||
| 2206 | + for l in 0..8 { | ||
| 2207 | + let raw = next(); | ||
| 2208 | + let v = match round % 3 { | ||
| 2209 | + // every extreme, and every pair of them | ||
| 2210 | + 0 => edges[(raw >> 32) as usize % edges.len()], | ||
| 2211 | + // uniform over the whole 32-bit range | ||
| 2212 | + 1 => raw as i32, | ||
| 2213 | + // the range a conformant stream stays inside | ||
| 2214 | + _ => raw as i32 % (hi / 2 + 1), | ||
| 2215 | + }; | ||
| 2216 | + wide[i][l] = v; | ||
| 2217 | + cols[l][i] = v; | ||
| 2218 | + } | ||
| 2219 | + } | ||
| 2220 | + let mut avx = [simd::splat([0i32; 8]); 64]; | ||
| 2221 | + for i in 0..(1usize << n) { | ||
| 2222 | + avx[i] = simd::splat(wide[i]); | ||
| 2223 | + } | ||
| 2224 | + // SAFETY: guarded by the feature detection above. | ||
| 2225 | + #[allow(unsafe_code)] | ||
| 2226 | + unsafe { | ||
| 2227 | + simd::dct_x8_avx2(&mut avx, n, r); | ||
| 2228 | + } | ||
| 2229 | + for (l, col) in cols.iter_mut().enumerate() { | ||
| 2230 | + inverse_dct_fixed(col, n, r); | ||
| 2231 | + for i in 0..(1usize << n) { | ||
| 2232 | + assert_eq!( | ||
| 2233 | + simd::lanes(avx[i])[l], | ||
| 2234 | + col[i], | ||
| 2235 | + "avx2 lane {l} of entry {i} at n={n} r={r} round={round}" | ||
| 2236 | + ); | ||
| 2237 | + } | ||
| 2238 | + } | ||
| 2239 | + } | ||
| 2240 | + } | ||
| 2241 | + } | ||
| 2242 | + } | ||
| 2243 | + | ||
| 2168 | /// the column pass would drift from the decoder on some block this | 2244 | /// the column pass would drift from the decoder on some block this |
| 2169 | /// crate's gate streams happen not to contain. | 2245 | /// crate's gate streams happen not to contain. |
| 2170 | #[test] | 2246 | #[test] |