Put the AVX2 feature on the Lane methods themselves
Browse files at this commitb848139b194dfda57e7cc4dca3554ecd9f68fcb7Parent: 1b7f343- perf
- put the AVX2 feature on the Lane methods themselves
1 changed file +58 -40
- crates/ec-av1/src/transform.rs +58 -40
crates/ec-av1/src/transform.rsmodified+58 -40
| @@ -111,11 +111,26 @@ fn clamp_range(x: i32, r: usize) -> i32 { | ||
| 111 | 111 | /// output share the whole network). The two impls are elementwise-identical |
| 112 | 112 | /// integer arithmetic, so the eight-wide pass is byte-exact with the scalar |
| 113 | 113 | /// one by construction -- the network itself is written once, above. |
| 114 | +// `unsafe fn` only so the AVX2 impl can carry `#[target_feature]` (rustc | |
| 115 | +// rejects it on a SAFE trait method); the two scalar impls below require | |
| 116 | +// nothing of their caller. | |
| 117 | +#[allow(unsafe_code)] | |
| 114 | 118 | trait Lane: Copy { |
| 115 | 119 | /// One butterfly rotation's two outputs, at pre-multiplied `cos`/`sin`. |
| 116 | - fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); | |
| 120 | + /// | |
| 121 | + /// # Safety | |
| 122 | + /// An impl may require CPU features: `M256`'s is `#[target_feature]` AVX2, | |
| 123 | + /// which is the only way its intrinsics compile INLINE -- as plain safe | |
| 124 | + /// methods every `_mm256_*` stayed an out-of-line call (13% of the 4K | |
| 125 | + /// segment's profile). `M256` values exist only inside | |
| 126 | + /// [`simd::column_pass_dct`], which its caller has feature-detected; the | |
| 127 | + /// scalar impls require nothing. | |
| 128 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self); | |
| 117 | 129 | /// One Hadamard rotation's two outputs, clamped to `r` bits. |
| 118 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self); | |
| 130 | + /// | |
| 131 | + /// # Safety | |
| 132 | + /// As [`Lane::btf`]. | |
| 133 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self); | |
| 119 | 134 | } |
| 120 | 135 | |
| 121 | 136 | /// `Round2(x, COS_BITS)` on a butterfly's 64-bit product sum. |
| @@ -123,21 +138,23 @@ fn round_cos(x: i64) -> i32 { | ||
| 123 | 138 | ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32 |
| 124 | 139 | } |
| 125 | 140 | |
| 141 | +#[allow(unsafe_code)] | |
| 126 | 142 | impl Lane for i32 { |
| 127 | - fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 143 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 128 | 144 | let (ta, tb) = (i64::from(a), i64::from(b)); |
| 129 | 145 | (round_cos(ta * c - tb * s), round_cos(ta * s + tb * c)) |
| 130 | 146 | } |
| 131 | 147 | |
| 132 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 148 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 133 | 149 | (clamp_range(a.wrapping_add(b), r), clamp_range(a.wrapping_sub(b), r)) |
| 134 | 150 | } |
| 135 | 151 | } |
| 136 | 152 | |
| 137 | 153 | /// Eight columns at once. Every operation is the [`i32`] impl's, lane by lane, |
| 138 | 154 | /// which is what the optimiser turns into one vector op per line. |
| 155 | +#[allow(unsafe_code)] | |
| 139 | 156 | impl Lane for [i32; 8] { |
| 140 | - fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 157 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 141 | 158 | let mut x = [0i32; 8]; |
| 142 | 159 | let mut y = [0i32; 8]; |
| 143 | 160 | for l in 0..8 { |
| @@ -148,7 +165,7 @@ impl Lane for [i32; 8] { | ||
| 148 | 165 | (x, y) |
| 149 | 166 | } |
| 150 | 167 | |
| 151 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 168 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 152 | 169 | let mut x = [0i32; 8]; |
| 153 | 170 | let mut y = [0i32; 8]; |
| 154 | 171 | for l in 0..8 { |
| @@ -182,42 +199,38 @@ mod simd { | ||
| 182 | 199 | /// bits (`|a| < 2^31`, `|c| <= 2^12`), and only the low 32 bits of the |
| 183 | 200 | /// shifted sum are kept -- which is what the scalar `as i32` keeps, so |
| 184 | 201 | /// the logical shift below stands in for the arithmetic one exactly. |
| 185 | - #[inline(always)] | |
| 186 | - fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 187 | - unsafe { | |
| 188 | - let cv = _mm256_set1_epi32(c as i32); | |
| 189 | - let sv = _mm256_set1_epi32(s as i32); | |
| 190 | - let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1)); | |
| 191 | - let (ae, be) = (a.0, b.0); | |
| 192 | - let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1)); | |
| 193 | - let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| { | |
| 194 | - let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v)); | |
| 195 | - let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u)); | |
| 196 | - ( | |
| 197 | - _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(p, rnd)), | |
| 198 | - _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(q, rnd)), | |
| 199 | - ) | |
| 200 | - }; | |
| 201 | - let (xe, ye) = half(ae, be, cv, sv); | |
| 202 | - let (xo, yo) = half(ao, bo, cv, sv); | |
| 202 | + #[target_feature(enable = "avx2")] | |
| 203 | + unsafe fn btf(a: Self, b: Self, c: i64, s: i64) -> (Self, Self) { | |
| 204 | + let cv = _mm256_set1_epi32(c as i32); | |
| 205 | + let sv = _mm256_set1_epi32(s as i32); | |
| 206 | + let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1)); | |
| 207 | + let (ae, be) = (a.0, b.0); | |
| 208 | + let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1)); | |
| 209 | + let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| { | |
| 210 | + let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v)); | |
| 211 | + let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u)); | |
| 203 | 212 | ( |
| 204 | - M256(_mm256_blend_epi32::<0xAA>(xe, _mm256_slli_epi64::<32>(xo))), | |
| 205 | - M256(_mm256_blend_epi32::<0xAA>(ye, _mm256_slli_epi64::<32>(yo))), | |
| 213 | + _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(p, rnd)), | |
| 214 | + _mm256_srli_epi64::<{ COS_BITS as i32 }>(_mm256_add_epi64(q, rnd)), | |
| 206 | 215 | ) |
| 207 | - } | |
| 216 | + }; | |
| 217 | + let (xe, ye) = half(ae, be, cv, sv); | |
| 218 | + let (xo, yo) = half(ao, bo, cv, sv); | |
| 219 | + ( | |
| 220 | + M256(_mm256_blend_epi32::<0xAA>(xe, _mm256_slli_epi64::<32>(xo))), | |
| 221 | + M256(_mm256_blend_epi32::<0xAA>(ye, _mm256_slli_epi64::<32>(yo))), | |
| 222 | + ) | |
| 208 | 223 | } |
| 209 | 224 | |
| 210 | - #[inline(always)] | |
| 211 | - fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 212 | - unsafe { | |
| 213 | - let hi = _mm256_set1_epi32(((1i64 << (r - 1)) - 1) as i32); | |
| 214 | - let lo = _mm256_set1_epi32((-(1i64 << (r - 1))) as i32); | |
| 215 | - let clamp = |v| _mm256_min_epi32(_mm256_max_epi32(v, lo), hi); | |
| 216 | - ( | |
| 217 | - M256(clamp(_mm256_add_epi32(a.0, b.0))), | |
| 218 | - M256(clamp(_mm256_sub_epi32(a.0, b.0))), | |
| 219 | - ) | |
| 220 | - } | |
| 225 | + #[target_feature(enable = "avx2")] | |
| 226 | + unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) { | |
| 227 | + let hi = _mm256_set1_epi32(((1i64 << (r - 1)) - 1) as i32); | |
| 228 | + let lo = _mm256_set1_epi32((-(1i64 << (r - 1))) as i32); | |
| 229 | + let clamp = |v| _mm256_min_epi32(_mm256_max_epi32(v, lo), hi); | |
| 230 | + ( | |
| 231 | + M256(clamp(_mm256_add_epi32(a.0, b.0))), | |
| 232 | + M256(clamp(_mm256_sub_epi32(a.0, b.0))), | |
| 233 | + ) | |
| 221 | 234 | } |
| 222 | 235 | } |
| 223 | 236 | |
| @@ -298,7 +311,10 @@ mod simd { | ||
| 298 | 311 | #[inline(always)] |
| 299 | 312 | fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { |
| 300 | 313 | let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); |
| 301 | - let (x, y) = T::btf(t[a], t[b], c, s); | |
| 314 | + // SAFETY: [`Lane::btf`] -- an `M256` reaches here only from the | |
| 315 | + // feature-detected [`simd::column_pass_dct`]. | |
| 316 | + #[allow(unsafe_code)] | |
| 317 | + let (x, y) = unsafe { T::btf(t[a], t[b], c, s) }; | |
| 302 | 318 | t[a] = x; |
| 303 | 319 | t[b] = y; |
| 304 | 320 | if flip { |
| @@ -311,7 +327,9 @@ fn butterfly<T: Lane>(t: &mut [T; 64], a: usize, b: usize, angle: i32, flip: boo | ||
| 311 | 327 | #[inline(always)] |
| 312 | 328 | fn hadamard<T: Lane>(t: &mut [T; 64], a: usize, b: usize, flip: bool, r: usize) { |
| 313 | 329 | let (a, b) = if flip { (b, a) } else { (a, b) }; |
| 314 | - let (x, y) = T::had(t[a], t[b], r); | |
| 330 | + // SAFETY: as [`butterfly`]'s. | |
| 331 | + #[allow(unsafe_code)] | |
| 332 | + let (x, y) = unsafe { T::had(t[a], t[b], r) }; | |
| 315 | 333 | t[a] = x; |
| 316 | 334 | t[b] = y; |
| 317 | 335 | } |