forge
Explore
Log in

Don't you dare go Hollow

ec-vp8, ec-av1, ec-h265: SIMD kernels take archmage tokens instead of bare #[target_feature]; builds with AVX2 enabled globally (target-cpu=native/x86-64-v3) no longer fail with E0133

Browse files at this commit

d8ca21eb05a5b32c0ce9f0bbd1d866d3ca7771cfAhmet Kaan GümüşParent: 712f5aa
Cargo.lockmodified+2 -1
@@ -364,10 +364,10 @@ dependencies = [
364364 name = "ec-av1"
365365 version = "0.1.0"
366366 dependencies = [
367+ "archmage",
367368 "bytemuck",
368369 "ec-av1-syntax",
369370 "ec-core",
370- "multiversion",
371371 "stats_alloc",
372372 "tracking-allocator",
373373 ]
@@ -726,6 +726,7 @@ dependencies = [
726726 name = "ec-vp8"
727727 version = "0.1.0"
728728 dependencies = [
729+ "archmage",
729730 "bytemuck",
730731 "ec-core",
731732 "multiversion",
crates/ec-av1/Cargo.tomlmodified+1 -1
@@ -9,8 +9,8 @@ authors.workspace = true
99 [dependencies]
1010 ec-core.workspace = true
1111 ec-av1-syntax.workspace = true
12+archmage = "=0.9.31-beta.1"
1213 bytemuck = "1"
13-multiversion = "0.9"
1414
1515 [features]
1616 default = ["gate-counters"]
crates/ec-av1/src/decode/cdef.rsmodified+29 -32
@@ -1,5 +1,6 @@
11 //! CDEF constants and filter kernels. //! av1split: moved out of `decode/mod.rs`; the items are unchanged apart from visibility. (D14)
22
3+use archmage::SimdToken;
34 use super::*;
45
56 /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary
@@ -156,12 +157,11 @@ pub(super) fn cdef_find_dir_partials(win: &[i16], bstride: usize, coeff_shift: i
156157 /// Runtime dispatch of the direction search's partial sums: the AVX2 clone
157158 /// runs [`cdef_simd::find_dir_partials_avx2`], the baseline clone the scalar
158159 /// form. Both read the same eight rows of eight samples.
159-#[multiversion::multiversion(targets("x86_64+avx2"))]
160160 pub(super) fn cdef_find_dir_partials_dispatch(win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] {
161- multiversion::target::match_target! {
162- "x86_64+avx2" => cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift),
163- _ => cdef_find_dir_partials(win, bstride, coeff_shift),
161+ if let Some(t) = archmage::X64V3Token::summon() {
162+ return cdef_simd::find_dir_partials_avx2(t, win, bstride, coeff_shift);
164163 }
164+ cdef_find_dir_partials(win, bstride, coeff_shift)
165165 }
166166
167167 pub(super) fn cdef_find_dir(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) {
@@ -223,7 +223,6 @@ pub(super) fn cdef_adjust_strength(strength: i32, var: i32) -> i32 {
223223 /// chroma) in place, `sample(row, col)` reading the *unfiltered* frame (a
224224 /// previously-filtered neighbour must never feed this block's own sum).
225225 #[allow(clippy::too_many_arguments)]
226-#[multiversion::multiversion(targets("x86_64+avx2"))]
227226 pub(super) fn cdef_filter_block(
228227 src: &[i16],
229228 dst: &mut [u16],
@@ -243,34 +242,32 @@ pub(super) fn cdef_filter_block(
243242 fctx: &crate::decode::FrameCtx,
244243 ) {
245244 let smax = sample_max(fctx);
246- multiversion::target::match_target! {
247- "x86_64+avx2" => {
248- if (bw == 4 || bw == 8) && bh.is_multiple_of(2) {
249- // The kernel reads `src`'s whole (bw + 4) x (bh + 4) window
250- // and writes exactly the `bh` runs of `bw` samples the
251- // scalar form writes.
252- cdef_simd::filter_block_avx2(
253- src,
254- dst,
255- stride,
256- ox,
257- oy,
258- bw,
259- bh,
260- pri_strength,
261- sec_strength,
262- dir,
263- pri_damping,
264- sec_damping,
265- enable_primary,
266- enable_secondary,
267- coeff_shift,
268- smax,
269- );
270- return;
271- }
245+ if let Some(t) = archmage::X64V3Token::summon() {
246+ if (bw == 4 || bw == 8) && bh.is_multiple_of(2) {
247+ // The kernel reads `src`'s whole (bw + 4) x (bh + 4) window
248+ // and writes exactly the `bh` runs of `bw` samples the
249+ // scalar form writes.
250+ cdef_simd::filter_block_avx2(
251+ t,
252+ src,
253+ dst,
254+ stride,
255+ ox,
256+ oy,
257+ bw,
258+ bh,
259+ pri_strength,
260+ sec_strength,
261+ dir,
262+ pri_damping,
263+ sec_damping,
264+ enable_primary,
265+ enable_secondary,
266+ coeff_shift,
267+ smax,
268+ );
269+ return;
272270 }
273- _ => {}
274271 }
275272 cdef_filter_block_scalar(
276273 src,
crates/ec-av1/src/decode/cdef_simd.rsmodified+5 -3
@@ -2,6 +2,7 @@ use super::{
22 CDEF_BORDER, CDEF_DIRECTIONS, CDEF_PRI_TAPS, CDEF_SEC_TAPS, CDEF_VERY_LARGE,
33 cdef_constrain_shift,
44 };
5+use archmage::{X64V3Token, arcane};
56 use std::arch::x86_64::*;
67
78 /// Eight `i16` samples starting at `at`, as one 128-bit register.
@@ -60,8 +61,8 @@ fn add4(dst: &mut [i32; 16], off: usize, v: __m128i) {
6061 ///
6162 /// `win` must hold [`super::cdef_gather`]'s 8x8 window grown by
6263 /// `CDEF_BORDER` at row stride `bstride`.
63-#[target_feature(enable = "avx2")]
64-pub fn find_dir_partials_avx2(win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] {
64+#[arcane]
65+pub fn find_dir_partials_avx2(_t: X64V3Token, win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] {
6566 let mut partial = [[0i32; 16]; 8];
6667 let shift = _mm_cvtsi32_si128(coeff_shift);
6768 let c128 = _mm256_set1_epi32(128);
@@ -105,9 +106,10 @@ pub fn find_dir_partials_avx2(win: &[i16], bstride: usize, coeff_shift: i32) ->
105106 /// loads read up to 152 entries past its start, i.e. past the
106107 /// `(bw + 4) x (bh + 4)` samples themselves) and `dst` the `bh` rows of
107108 /// `bw` samples at `(ox, oy)`.
108-#[target_feature(enable = "avx2")]
109+#[arcane]
109110 #[allow(clippy::too_many_arguments)]
110111 pub fn filter_block_avx2(
112+ _t: X64V3Token,
111113 src: &[i16],
112114 dst: &mut [u16],
113115 stride: usize,
crates/ec-av1/src/decode/deblock.rsmodified+21 -22
@@ -1,5 +1,6 @@
11 //! Deblocking filters: the edge filters, the deblock plane drivers, the filter-replay capture and the pooled plane scratch. Moved out of `decode/mod.rs` (av1split D12); the items are unchanged apart from visibility.
22
3+use archmage::SimdToken;
34 use super::*;
45
56 /// `update_sharpness`'s per-level `(blimit, limit, hev_thr)` triple.
@@ -335,7 +336,6 @@ pub(super) fn filter_edge(
335336 #[allow(unused_variables)]
336337 // Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
337338 #[allow(clippy::too_many_arguments)]
338-#[multiversion::multiversion(targets("x86_64+avx2"))]
339339 pub(super) fn filter_edge16(
340340 data: &mut [u16],
341341 base: usize,
@@ -346,27 +346,26 @@ pub(super) fn filter_edge16(
346346 sharpness: u8,
347347 fctx: &crate::decode::FrameCtx,
348348 ) -> bool {
349- multiversion::target::match_target! {
350- "x86_64+avx2" => {
351- let (blimit, limit, hev_thr) = deblock_thresholds(level, sharpness);
352- let shift = bit_depth(fctx) - 8;
353- deblock_simd::edge16(
354- data,
355- base,
356- stride,
357- vertical,
358- len,
359- blimit << shift,
360- limit << shift,
361- hev_thr << shift,
362- 1 << shift,
363- 1 << shift,
364- sample_max(fctx),
365- );
366- true
367- }
368- _ => false,
369- }
349+ let Some(t) = archmage::X64V3Token::summon() else {
350+ return false;
351+ };
352+ let (blimit, limit, hev_thr) = deblock_thresholds(level, sharpness);
353+ let shift = bit_depth(fctx) - 8;
354+ deblock_simd::edge16(
355+ t,
356+ data,
357+ base,
358+ stride,
359+ vertical,
360+ len,
361+ blimit << shift,
362+ limit << shift,
363+ hev_thr << shift,
364+ 1 << shift,
365+ 1 << shift,
366+ sample_max(fctx),
367+ );
368+ true
370369 }
371370
372371 /// One plane's worth of spec 7.14: every vertical edge across the plane,
crates/ec-av1/src/decode/deblock_simd.rsmodified+3 -1
@@ -1,3 +1,4 @@
1+use archmage::{X64V3Token, arcane};
12 use std::arch::x86_64::*;
23
34 /// Bounds-checked unaligned load of 16 `u16` samples at `at`.
@@ -387,9 +388,10 @@ fn filter16<const LEN: u8>(
387388 /// stride + 8 <= stride` and 16 rows from `base` must be inside `data`;
388389 /// for a horizontal edge `base >= 8 * stride` and `base + 8 * stride`
389390 /// must be inside it.
390-#[target_feature(enable = "avx2")]
391+#[arcane]
391392 #[allow(clippy::too_many_arguments)]
392393 pub fn edge16(
394+ _t: X64V3Token,
393395 data: &mut [u16],
394396 base: usize,
395397 stride: usize,
crates/ec-av1/src/decode/tests.rsmodified+11 -12
@@ -1725,12 +1725,10 @@ fn cdef_find_dir_matches_the_per_sample_reference() {
17251725 }
17261726 }
17271727
1728-/// The CDEF filter through a `multiversion` clone, so the AVX2 kernel is
1729-/// reached by a safe call: the AVX2 clone runs `filter_block_avx2`, the
1730-/// baseline clone the scalar form (the same split `cdef_filter_block` uses).
1728+/// The CDEF filter with a summoned AVX2 token, so the AVX2 kernel is reached
1729+/// by a safe call: the token runs `filter_block_avx2`, its absence the scalar form (the same split `cdef_filter_block` uses).
17311730 #[cfg(target_arch = "x86_64")]
17321731 #[allow(clippy::too_many_arguments)]
1733-#[multiversion::multiversion(targets("x86_64+avx2"))]
17341732 fn cdef_filter_block_under_test(
17351733 src: &[i16],
17361734 dst: &mut [u16],
@@ -1749,16 +1747,16 @@ fn cdef_filter_block_under_test(
17491747 coeff_shift: i32,
17501748 smax: i32,
17511749 ) {
1752- multiversion::target::match_target! {
1753- "x86_64+avx2" => super::cdef_simd::filter_block_avx2(
1754- src, dst, stride, ox, oy, bw, bh, pri_strength, sec_strength, dir,
1750+ if let Some(t) = archmage::X64V3Token::summon() {
1751+ return super::cdef_simd::filter_block_avx2(
1752+ t, src, dst, stride, ox, oy, bw, bh, pri_strength, sec_strength, dir,
17551753 pri_damping, sec_damping, enable_primary, enable_secondary, coeff_shift, smax,
1756- ),
1757- _ => super::cdef_filter_block_scalar(
1758- src, dst, stride, ox, oy, bw, bh, pri_strength, sec_strength, dir,
1759- pri_damping, sec_damping, enable_primary, enable_secondary, coeff_shift, smax,
1760- ),
1754+ );
17611755 }
1756+ super::cdef_filter_block_scalar(
1757+ src, dst, stride, ox, oy, bw, bh, pri_strength, sec_strength, dir,
1758+ pri_damping, sec_damping, enable_primary, enable_secondary, coeff_shift, smax,
1759+ )
17621760 }
17631761
17641762 /// lane-perf7 step 1: the AVX2 CDEF kernel must be value-identical to
@@ -2098,6 +2096,7 @@ fn filter_intra_classes_carry_their_own_libaom_default_row() {
20982096 );
20992097 }
21002098 }
2099+use archmage::SimdToken;
21012100 use super::*;
21022101
21032102 /// lane-tx64x16 r3: the two new 4:1 chroma scans are a TRANSPOSED PAIR --
crates/ec-av1/src/film_grain.rsmodified+52 -57
@@ -21,6 +21,7 @@
2121 //! [`crate::stream::decode_stream`] is the only caller: it applies this to
2222 //! the frame it pushes onto its output `Vec<Picture>`, after saving the
2323 //! clean decode into the reference slot bank.
24+use archmage::SimdToken;
2425 use crate::hits::hit;
2526
2627 use crate::decode::WithRef;
@@ -620,7 +621,6 @@ fn luma_noise_row(
620621 /// 32x32 grain block is 30 wide (32 minus the 2-column overlap), never a
621622 /// multiple of 8. Hoisting the row loop into the kernel pays the prologue
622623 /// once per block and lets the tail take a 4-wide SSE step.
623-#[multiversion::multiversion(targets("x86_64+avx2"))]
624624 #[allow(clippy::too_many_arguments)]
625625 fn luma_noise_rows(
626626 pixels: &mut [u16],
@@ -635,26 +635,24 @@ fn luma_noise_rows(
635635 min_luma: i32,
636636 max_luma: i32,
637637 ) {
638- multiversion::target::match_target! {
639- "x86_64+avx2" => {
640- if n >= 4 {
641- simd::luma_noise_rows_avx2(
642- pixels,
643- p_stride,
644- grain,
645- g_stride,
646- rows,
647- n,
648- scaling,
649- rounding_offset,
650- scaling_shift,
651- min_luma,
652- max_luma,
653- );
654- return;
655- }
638+ if let Some(t) = archmage::X64V3Token::summon() {
639+ if n >= 4 {
640+ simd::luma_noise_rows_avx2(
641+ t,
642+ pixels,
643+ p_stride,
644+ grain,
645+ g_stride,
646+ rows,
647+ n,
648+ scaling,
649+ rounding_offset,
650+ scaling_shift,
651+ min_luma,
652+ max_luma,
653+ );
654+ return;
656655 }
657- _ => {}
658656 }
659657 for r in 0..rows {
660658 luma_noise_row_scalar(
@@ -736,7 +734,6 @@ fn chroma_noise_row(
736734 /// [`chroma_noise_row`] over `rows` consecutive chroma rows of a block; see
737735 /// [`luma_noise_rows`] for why the row loop lives inside the kernel.
738736 /// `pair` is `ss_x == 1`; see [`chroma_noise_row`] for the two luma shapes.
739-#[multiversion::multiversion(targets("x86_64+avx2"))]
740737 #[allow(clippy::too_many_arguments)]
741738 fn chroma_noise_rows(
742739 chroma: &mut [u16],
@@ -754,17 +751,14 @@ fn chroma_noise_rows(
754751 p: ChromaNoise,
755752 pair: bool,
756753 ) {
757- multiversion::target::match_target! {
758- "x86_64+avx2" => {
759- if pair && n >= 4 {
760- simd::chroma_noise_rows_avx2(
761- chroma, c_stride, luma, l_stride, grain, g_stride, rows, n, scaling, luma_mult,
762- mult, offset, p,
763- );
764- return;
765- }
754+ if let Some(t) = archmage::X64V3Token::summon() {
755+ if pair && n >= 4 {
756+ simd::chroma_noise_rows_avx2(
757+ t, chroma, c_stride, luma, l_stride, grain, g_stride, rows, n, scaling, luma_mult,
758+ mult, offset, p,
759+ );
760+ return;
766761 }
767- _ => {}
768762 }
769763 let lrow = if pair { 2 * n } else { n };
770764 for r in 0..rows {
@@ -834,6 +828,7 @@ fn luma_noise_row_scalar(
834828
835829 #[cfg(target_arch = "x86_64")]
836830 mod simd {
831+ use archmage::{X64V3Token, arcane};
837832 use std::arch::x86_64::*;
838833
839834 // Bounds-checked slice <-> vector moves (no raw pointers).
@@ -899,10 +894,11 @@ mod simd {
899894 /// panic.
900895 ///
901896 /// `grain` must be at least `pixels.len()` long and `scaling` non-empty.
902- #[target_feature(enable = "avx2")]
897+ #[arcane]
903898 #[allow(clippy::too_many_arguments)]
904899 #[cfg(test)]
905900 pub fn luma_noise_row_avx2(
901+ t: X64V3Token,
906902 pixels: &mut [u16],
907903 grain: &[i32],
908904 scaling: &[i32],
@@ -913,6 +909,7 @@ mod simd {
913909 ) {
914910 let n = pixels.len().min(grain.len());
915911 luma_noise_rows_avx2(
912+ t,
916913 pixels,
917914 n,
918915 grain,
@@ -933,9 +930,10 @@ mod simd {
933930 ///
934931 /// `pixels`/`grain` must cover the `rows x n` window at their strides and
935932 /// `scaling` must be non-empty.
936- #[target_feature(enable = "avx2")]
933+ #[arcane]
937934 #[allow(clippy::too_many_arguments)]
938935 pub fn luma_noise_rows_avx2(
936+ _t: X64V3Token,
939937 pixels: &mut [u16],
940938 p_stride: usize,
941939 grain: &[i32],
@@ -1010,10 +1008,11 @@ mod simd {
10101008 ///
10111009 /// `luma` must be at least `2 * chroma.len()` long, `grain` at least
10121010 /// `chroma.len()`, and `scaling` non-empty.
1013- #[target_feature(enable = "avx2")]
1011+ #[arcane]
10141012 #[allow(clippy::too_many_arguments)]
10151013 #[cfg(test)]
10161014 pub fn chroma_noise_row_avx2(
1015+ t: X64V3Token,
10171016 chroma: &mut [u16],
10181017 luma: &[u16],
10191018 grain: &[i32],
@@ -1025,6 +1024,7 @@ mod simd {
10251024 ) {
10261025 let n = chroma.len().min(grain.len()).min(luma.len() / 2);
10271026 chroma_noise_rows_avx2(
1027+ t,
10281028 chroma,
10291029 n,
10301030 luma,
@@ -1047,9 +1047,10 @@ mod simd {
10471047 ///
10481048 /// The three slices must cover the `rows x n` window (`2 * n` for luma) at
10491049 /// their strides and `scaling` must be non-empty.
1050- #[target_feature(enable = "avx2")]
1050+ #[arcane]
10511051 #[allow(clippy::too_many_arguments)]
10521052 pub fn chroma_noise_rows_avx2(
1053+ _t: X64V3Token,
10531054 chroma: &mut [u16],
10541055 c_stride: usize,
10551056 luma: &[u16],
@@ -2140,10 +2141,10 @@ pub(crate) fn apply_grain(
21402141
21412142 #[cfg(test)]
21422143 mod perf5_tests {
2144+ use archmage::SimdToken;
21432145 /// Runs the safe AVX2 kernel inside a `target_feature(avx2)` clone (the
21442146 /// caller checks `is_x86_feature_detected!("avx2")` first).
21452147 #[cfg(target_arch = "x86_64")]
2146- #[multiversion::multiversion(targets("x86_64+avx2"))]
21472148 #[allow(unused_variables)]
21482149 fn avx2_luma_row(
21492150 pixels: &mut [u16],
@@ -2154,24 +2155,21 @@ mod perf5_tests {
21542155 min_luma: i32,
21552156 max_luma: i32,
21562157 ) {
2157- multiversion::target::match_target! {
2158- "x86_64+avx2" => {
2159- super::simd::luma_noise_row_avx2(
2160- pixels,
2161- grain,
2162- scaling,
2163- rounding_offset,
2164- scaling_shift,
2165- min_luma,
2166- max_luma,
2167- );
2168- }
2169- _ => {}
2158+ if let Some(t) = archmage::X64V3Token::summon() {
2159+ super::simd::luma_noise_row_avx2(
2160+ t,
2161+ pixels,
2162+ grain,
2163+ scaling,
2164+ rounding_offset,
2165+ scaling_shift,
2166+ min_luma,
2167+ max_luma,
2168+ );
21702169 }
21712170 }
21722171
21732172 #[cfg(target_arch = "x86_64")]
2174- #[multiversion::multiversion(targets("x86_64+avx2"))]
21752173 #[allow(clippy::too_many_arguments)]
21762174 #[allow(unused_variables)]
21772175 fn avx2_chroma_row(
@@ -2184,13 +2182,10 @@ mod perf5_tests {
21842182 offset: i32,
21852183 p: super::ChromaNoise,
21862184 ) {
2187- multiversion::target::match_target! {
2188- "x86_64+avx2" => {
2189- super::simd::chroma_noise_row_avx2(
2190- chroma, luma, grain, scaling, luma_mult, mult, offset, p,
2191- );
2192- }
2193- _ => {}
2185+ if let Some(t) = archmage::X64V3Token::summon() {
2186+ super::simd::chroma_noise_row_avx2(
2187+ t, chroma, luma, grain, scaling, luma_mult, mult, offset, p,
2188+ );
21942189 }
21952190 }
21962191
crates/ec-av1/src/mc.rsmodified+56 -67
@@ -8,6 +8,7 @@
88 //! in 1/16-pel steps -- luma motion vectors are stored at 1/8-pel and always
99 //! land on an even step; chroma vectors, once scaled for subsampling, can
1010 //! land on any of the 16.
11+use archmage::SimdToken;
1112 use crate::hits::hit;
1213
1314 /// `Round2` (spec 4.7): halves round up.
@@ -326,9 +327,8 @@ pub(crate) fn hpass_contig(src: &[u16], t16: &[i16; 8], out: &mut [i16], round0:
326327 /// lane-mc2: `rows` rows of [`hpass_contig`], `src_stride` apart in `src` and
327328 /// packed `block_w` apart in `out`. The tap vectors and the SIMD dispatch are
328329 /// set up once for the whole block instead of once per row.
329-// Runtime dispatch is `multiversion`'s: the AVX2 clone runs the AVX2 kernel,
330-// the SSE4.1 clone the SSE2 one, anything else the scalar loop below.
331-#[multiversion::multiversion(targets("x86_64+avx2", "x86_64+sse4.1"))]
330+// Runtime dispatch is an archmage token summon: the AVX2 token runs the AVX2
331+// kernel, the SSE4.1 token the SSE2 one, anything else the scalar loop below.
332332 pub(crate) fn hpass_rows(
333333 src: &[u16],
334334 src_stride: usize,
@@ -339,16 +339,16 @@ pub(crate) fn hpass_rows(
339339 round0: u32,
340340 ) {
341341 #[cfg(target_arch = "x86_64")]
342- multiversion::target::match_target! {
343- "x86_64+avx2" => {
344- simd::hpass_avx2(src, src_stride, rows, block_w, t16, out, round0);
342+ {
343+ if let Some(t) = archmage::X64V3Token::summon() {
344+ simd::hpass_avx2(t, src, src_stride, rows, block_w, t16, out, round0);
345+ return;
345346 }
346- "x86_64+sse4.1" => {
347- simd::hpass_sse2(src, src_stride, rows, block_w, t16, out, round0);
347+ if let Some(t) = archmage::X64V2Token::summon() {
348+ simd::hpass_sse2(t, src, src_stride, rows, block_w, t16, out, round0);
349+ return;
348350 }
349- _ => hpass_rows_scalar_all(src, src_stride, rows, block_w, t16, out, round0),
350351 }
351- #[cfg(not(target_arch = "x86_64"))]
352352 hpass_rows_scalar_all(src, src_stride, rows, block_w, t16, out, round0);
353353 }
354354
@@ -391,13 +391,13 @@ fn hpass_contig_scalar(src: &[u16], t16: &[i16; 8], out: &mut [i16], round0: u32
391391 /// `hpass_row` + `vpass_row`. Nothing here is spec logic: each kernel is the
392392 /// scalar function's arithmetic, lane-parallel, with identical rounding.
393393 ///
394-/// Every kernel is a safe `#[target_feature]` function, called only from a
395-/// `multiversion` clone that has the feature enabled (or, for SSE2, from any
396-/// x86_64 code). Loads and stores go through `bytemuck` on slices, so every
394+/// Every kernel is a safe `#[arcane]` (or `#[target_feature]` helper) function,
395+/// called only with the archmage token of its feature, obtained by `summon`. Loads and stores go through `bytemuck` on slices, so every
397396 /// access is bounds-checked.
398397 #[cfg(target_arch = "x86_64")]
399398 mod simd {
400399 use super::{round2, sat16};
400+ use archmage::{X64V2Token, X64V3Token, arcane};
401401 use bytemuck::Pod;
402402 use std::arch::x86_64::*;
403403 use std::mem::size_of;
@@ -468,8 +468,9 @@ mod simd {
468468 /// `unpack` + `permute2x128`.
469469 ///
470470 /// Requires AVX2. `src.len()` must be at least `out.len() + 7`.
471- #[target_feature(enable = "avx2")]
471+ #[arcane]
472472 pub fn hpass_avx2(
473+ t: X64V3Token,
473474 src: &[u16],
474475 src_stride: usize,
475476 rows: usize,
@@ -515,7 +516,7 @@ mod simd {
515516 c += 16;
516517 }
517518 if c < block_w {
518- hpass_sse2(&sp[c..], 0, 1, block_w - c, t16, &mut op[c..], round0);
519+ hpass_sse2(t.v2(), &sp[c..], 0, 1, block_w - c, t16, &mut op[c..], round0);
519520 }
520521 }
521522 }
@@ -529,8 +530,9 @@ mod simd {
529530 ///
530531 /// Requires AVX2. `intermediate` must hold `(row + 8) * block_w` elements
531532 /// and `acc` at least `block_w`.
532- #[target_feature(enable = "avx2")]
533+ #[arcane]
533534 pub fn vpass_avx2(
535+ _t: X64V3Token,
534536 intermediate: &[i16],
535537 block_w: usize,
536538 src_stride: usize,
@@ -645,10 +647,11 @@ mod simd {
645647 ///
646648 /// Requires AVX2. `intermediate` must hold `(row + 8) * block_w` elements
647649 /// and `dst` at least `block_w`.
648- #[target_feature(enable = "avx2")]
650+ #[arcane]
649651 // Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
650652 #[allow(clippy::too_many_arguments)]
651653 pub fn vpass_row_u16_avx2(
654+ _t: X64V3Token,
652655 intermediate: &[i16],
653656 block_w: usize,
654657 src_stride: usize,
@@ -768,10 +771,11 @@ mod simd {
768771 /// [`vpass_row_u16_avx2`] without the 16-column step.
769772 ///
770773 /// Requires SSE4.1; same slice contract as [`vpass_row_u16_avx2`].
771- #[target_feature(enable = "sse4.1")]
774+ #[arcane]
772775 // Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
773776 #[allow(clippy::too_many_arguments)]
774777 pub fn vpass_row_u16_sse41(
778+ _t: X64V2Token,
775779 intermediate: &[i16],
776780 block_w: usize,
777781 src_stride: usize,
@@ -803,8 +807,9 @@ mod simd {
803807 /// Vertical pass, SSE2: [`vpass_avx2`] without the 16-column step.
804808 ///
805809 /// SSE2 is baseline x86_64; same slice contract as [`vpass_avx2`].
806- #[target_feature(enable = "sse2")]
810+ #[arcane]
807811 pub fn vpass_sse41(
812+ _t: X64V2Token,
808813 intermediate: &[i16],
809814 block_w: usize,
810815 src_stride: usize,
@@ -834,8 +839,9 @@ mod simd {
834839 /// scalar loop, which is where 4-tap kernels mostly land anyway.
835840 ///
836841 /// `src.len()` must be at least `out.len() + 7`. SSE2 is baseline x86_64.
837- #[target_feature(enable = "sse2")]
842+ #[arcane]
838843 pub fn hpass_sse2(
844+ _t: X64V2Token,
839845 src: &[u16],
840846 src_stride: usize,
841847 rows: usize,
@@ -889,12 +895,12 @@ mod simd {
889895 }
890896 }
891897
892- // Test-only entry points that run one kernel under a `multiversion`
893- // clone with the needed feature, so the unit tests can exercise each
894- // tier directly (callers check the feature first).
898+ // Test-only entry points that run one kernel with a summoned token, so
899+ // the unit tests can exercise each tier directly (callers check the
900+ // feature first).
895901 #[cfg(test)]
896902 pub mod entry {
897- #[multiversion::multiversion(targets("x86_64+sse4.1"))]
903+ use archmage::SimdToken;
898904 #[allow(unused_variables)]
899905 pub fn hpass_sse2(
900906 src: &[u16],
@@ -905,13 +911,10 @@ mod simd {
905911 out: &mut [i16],
906912 round0: u32,
907913 ) {
908- multiversion::target::match_target! {
909- "x86_64+sse4.1" => super::hpass_sse2(src, src_stride, rows, block_w, t16, out, round0),
910- _ => unreachable!("sse4.1 entry on a CPU without sse4.1"),
911- }
914+ let t = archmage::X64V2Token::summon().expect("sse4.1 entry on a CPU without sse4.1");
915+ super::hpass_sse2(t, src, src_stride, rows, block_w, t16, out, round0)
912916 }
913917
914- #[multiversion::multiversion(targets("x86_64+sse4.1"))]
915918 #[allow(unused_variables)]
916919 pub fn vpass_sse41(
917920 intermediate: &[i16],
@@ -921,13 +924,10 @@ mod simd {
921924 taps: &[i32; 8],
922925 acc: &mut [i32],
923926 ) {
924- multiversion::target::match_target! {
925- "x86_64+sse4.1" => super::vpass_sse41(intermediate, block_w, src_stride, rows, taps, acc),
926- _ => unreachable!("sse4.1 entry on a CPU without sse4.1"),
927- }
927+ let t = archmage::X64V2Token::summon().expect("sse4.1 entry on a CPU without sse4.1");
928+ super::vpass_sse41(t, intermediate, block_w, src_stride, rows, taps, acc)
928929 }
929930
930- #[multiversion::multiversion(targets("x86_64+avx2"))]
931931 #[allow(unused_variables)]
932932 pub fn hpass_avx2(
933933 src: &[u16],
@@ -938,13 +938,10 @@ mod simd {
938938 out: &mut [i16],
939939 round0: u32,
940940 ) {
941- multiversion::target::match_target! {
942- "x86_64+avx2" => super::hpass_avx2(src, src_stride, rows, block_w, t16, out, round0),
943- _ => unreachable!("avx2 entry on a CPU without avx2"),
944- }
941+ let t = archmage::X64V3Token::summon().expect("avx2 entry on a CPU without avx2");
942+ super::hpass_avx2(t, src, src_stride, rows, block_w, t16, out, round0)
945943 }
946944
947- #[multiversion::multiversion(targets("x86_64+avx2"))]
948945 #[allow(unused_variables)]
949946 pub fn vpass_avx2(
950947 intermediate: &[i16],
@@ -954,13 +951,10 @@ mod simd {
954951 taps: &[i32; 8],
955952 acc: &mut [i32],
956953 ) {
957- multiversion::target::match_target! {
958- "x86_64+avx2" => super::vpass_avx2(intermediate, block_w, src_stride, rows, taps, acc),
959- _ => unreachable!("avx2 entry on a CPU without avx2"),
960- }
954+ let t = archmage::X64V3Token::summon().expect("avx2 entry on a CPU without avx2");
955+ super::vpass_avx2(t, intermediate, block_w, src_stride, rows, taps, acc)
961956 }
962957
963- #[multiversion::multiversion(targets("x86_64+avx2"))]
964958 #[allow(unused_variables, clippy::too_many_arguments)]
965959 pub fn vpass_row_u16_avx2(
966960 intermediate: &[i16],
@@ -972,13 +966,10 @@ mod simd {
972966 dst: &mut [u16],
973967 round1: u32,
974968 ) {
975- multiversion::target::match_target! {
976- "x86_64+avx2" => super::vpass_row_u16_avx2(intermediate, block_w, src_stride, rows, taps, max, dst, round1),
977- _ => unreachable!("avx2 entry on a CPU without avx2"),
978- }
969+ let t = archmage::X64V3Token::summon().expect("avx2 entry on a CPU without avx2");
970+ super::vpass_row_u16_avx2(t, intermediate, block_w, src_stride, rows, taps, max, dst, round1)
979971 }
980972
981- #[multiversion::multiversion(targets("x86_64+sse4.1"))]
982973 #[allow(unused_variables, clippy::too_many_arguments)]
983974 pub fn vpass_row_u16_sse41(
984975 intermediate: &[i16],
@@ -990,10 +981,8 @@ mod simd {
990981 dst: &mut [u16],
991982 round1: u32,
992983 ) {
993- multiversion::target::match_target! {
994- "x86_64+sse4.1" => super::vpass_row_u16_sse41(intermediate, block_w, src_stride, rows, taps, max, dst, round1),
995- _ => unreachable!("sse4.1 entry on a CPU without sse4.1"),
996- }
984+ let t = archmage::X64V2Token::summon().expect("sse4.1 entry on a CPU without sse4.1");
985+ super::vpass_row_u16_sse41(t, intermediate, block_w, src_stride, rows, taps, max, dst, round1)
997986 }
998987 }
999988 }
@@ -1136,7 +1125,6 @@ fn horizontal_pass_unscaled(
11361125 /// striding the intermediate by `block_w` per tap. A zero tap is skipped --
11371126 /// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the
11381127 /// eight slots. `acc` is left holding the unrounded sums.
1139-#[multiversion::multiversion(targets("x86_64+avx2", "x86_64+sse4.1"))]
11401128 pub(crate) fn vpass_row(
11411129 intermediate: &[i16],
11421130 block_w: usize,
@@ -1146,16 +1134,16 @@ pub(crate) fn vpass_row(
11461134 acc: &mut [i32],
11471135 ) {
11481136 #[cfg(target_arch = "x86_64")]
1149- multiversion::target::match_target! {
1150- "x86_64+avx2" => {
1151- simd::vpass_avx2(intermediate, block_w, src_stride, rows, taps, acc);
1137+ {
1138+ if let Some(t) = archmage::X64V3Token::summon() {
1139+ simd::vpass_avx2(t, intermediate, block_w, src_stride, rows, taps, acc);
1140+ return;
11521141 }
1153- "x86_64+sse4.1" => {
1154- simd::vpass_sse41(intermediate, block_w, src_stride, rows, taps, acc);
1142+ if let Some(t) = archmage::X64V2Token::summon() {
1143+ simd::vpass_sse41(t, intermediate, block_w, src_stride, rows, taps, acc);
1144+ return;
11551145 }
1156- _ => vpass_row_scalar_all(intermediate, block_w, src_stride, rows, taps, acc),
11571146 }
1158- #[cfg(not(target_arch = "x86_64"))]
11591147 vpass_row_scalar_all(intermediate, block_w, src_stride, rows, taps, acc);
11601148 }
11611149
@@ -1184,7 +1172,6 @@ fn vpass_row_scalar_all(
11841172 /// entry points whose destination is `u16` samples. The unrounded `i32` sums
11851173 /// used to be stored to a scratch row and read straight back by a rounding
11861174 /// loop; here they never leave the register.
1187-#[multiversion::multiversion(targets("x86_64+avx2", "x86_64+sse4.1"))]
11881175 // Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
11891176 #[allow(clippy::too_many_arguments)]
11901177 fn vpass_row_u16(
@@ -1198,9 +1185,10 @@ fn vpass_row_u16(
11981185 round1: u32,
11991186 ) {
12001187 #[cfg(target_arch = "x86_64")]
1201- multiversion::target::match_target! {
1202- "x86_64+avx2" => {
1188+ {
1189+ if let Some(t) = archmage::X64V3Token::summon() {
12031190 simd::vpass_row_u16_avx2(
1191+ t,
12041192 intermediate,
12051193 block_w,
12061194 src_stride,
@@ -1210,9 +1198,11 @@ fn vpass_row_u16(
12101198 dst,
12111199 round1,
12121200 );
1201+ return;
12131202 }
1214- "x86_64+sse4.1" => {
1203+ if let Some(t) = archmage::X64V2Token::summon() {
12151204 simd::vpass_row_u16_sse41(
1205+ t,
12161206 intermediate,
12171207 block_w,
12181208 src_stride,
@@ -1222,10 +1212,9 @@ fn vpass_row_u16(
12221212 dst,
12231213 round1,
12241214 );
1215+ return;
12251216 }
1226- _ => vpass_row_u16_scalar_all(intermediate, block_w, src_stride, rows, taps, max, dst, round1),
12271217 }
1228- #[cfg(not(target_arch = "x86_64"))]
12291218 vpass_row_u16_scalar_all(
12301219 intermediate,
12311220 block_w,
crates/ec-av1/src/restoration.rsmodified+7 -6
@@ -3,8 +3,8 @@
33 //! `av1_loop_restoration_corners_in_sb`, restoration.c:1277) and spec 7.17's
44 //! two pixel filters (Wiener, self-guided/SGR). See `lanes/lr.report.md` for
55 //! the full derivation (libaom `decodeframe.c`/`restoration.c` line refs).
6+use archmage::SimdToken;
67 use crate::hits::hit;
7-use multiversion::target::match_target;
88
99 use crate::msac::SymbolDecoder;
1010 use ec_av1_syntax::obu::floor_log2;
@@ -1165,7 +1165,6 @@ fn ab_point(a: i32, b: i32, n: i32, s: i32, bd_shift: u32, one_by_x: i64) -> (i3
11651165
11661166 /// One grid row's `(A, B)` pairs: [`ab_point`] per element, eight at a time
11671167 /// under AVX2 (the kernel is that arithmetic lane-wise, same widths).
1168-#[multiversion::multiversion(targets("x86_64+avx2"))]
11691168 #[allow(clippy::too_many_arguments)]
11701169 fn ab_row(
11711170 a_row: &[i32],
@@ -1176,9 +1175,9 @@ fn ab_row(
11761175 one_by_x: i64,
11771176 out: &mut [(i32, i64)],
11781177 ) {
1179- let done = match_target! {
1180- "x86_64+avx2" => sgr_simd::ab_row_avx2(a_row, b_row, n, s, bd_shift, one_by_x as i32, out),
1181- _ => 0usize,
1178+ let done = match archmage::X64V3Token::summon() {
1179+ Some(t) => sgr_simd::ab_row_avx2(t, a_row, b_row, n, s, bd_shift, one_by_x as i32, out),
1180+ None => 0usize,
11821181 };
11831182 for gj in done..out.len() {
11841183 out[gj] = ab_point(a_row[gj], b_row[gj], n, s, bd_shift, one_by_x);
@@ -1189,6 +1188,7 @@ fn ab_row(
11891188 #[cfg(target_arch = "x86_64")]
11901189 mod sgr_simd {
11911190 use super::{SGRPROJ_MTABLE_BITS, SGRPROJ_RECIP_BITS, SGR_X_BY_XPLUS1};
1191+ use archmage::{X64V3Token, arcane};
11921192 use std::arch::x86_64::*;
11931193
11941194 /// Eight grid points of `super::ab_point` per iteration, returning how
@@ -1199,9 +1199,10 @@ mod sgr_simd {
11991199 /// `p * s < 2^38`, `(256 - A) * one_by_x < 2^21` and that times
12001200 /// `b < 2^17` is `< 2^38` -- so the two 64-bit products below lose no
12011201 /// bit, and the unsigned `mul_epu32` sees no negative operand.
1202- #[target_feature(enable = "avx2")]
1202+ #[arcane]
12031203 #[allow(clippy::too_many_arguments)]
12041204 pub fn ab_row_avx2(
1205+ _t: X64V3Token,
12051206 a_row: &[i32],
12061207 b_row: &[i32],
12071208 n: i32,
crates/ec-av1/src/transform.rsmodified+17 -24
@@ -195,6 +195,7 @@ impl Lane for [i32; 8] {
195195 #[cfg(target_arch = "x86_64")]
196196 mod simd {
197197 use super::{COS_BITS, Kern, inverse_dct_n};
198+ use archmage::{SimdToken, X64V3Token, arcane};
198199 use std::arch::x86_64::*;
199200
200201 /// Eight `i32` lanes in one AVX2 register.
@@ -232,8 +233,8 @@ mod simd {
232233 /// bits (`|a| < 2^31`, `|c| <= 2^12`), and only the low 32 bits of the
233234 /// shifted sum are kept -- which is what the scalar `as i32` keeps, so
234235 /// the logical shift stands in for the arithmetic one exactly.
235- #[target_feature(enable = "avx2")]
236- pub(super) fn dct_x8_avx2(t: &mut [M256; 64], n: u32, r: usize) {
236+ #[arcane]
237+ pub(super) fn dct_x8_avx2(_tok: X64V3Token, t: &mut [M256; 64], n: u32, r: usize) {
237238 let btf = |a: M256, b: M256, c: i64, s: i64| {
238239 let cv = _mm256_set1_epi32(c as i32);
239240 let sv = _mm256_set1_epi32(s as i32);
@@ -292,8 +293,9 @@ mod simd {
292293
293294 /// The whole DCT column pass of one transform unit: `residual` is the
294295 /// row-pass output (`h` rows of `w`), `out` the residual it writes.
295- #[target_feature(enable = "avx2")]
296+ #[arcane]
296297 pub(super) fn column_pass_dct(
298+ tok: X64V3Token,
297299 residual: &[i32],
298300 out: &mut [i32],
299301 w: usize,
@@ -316,7 +318,7 @@ mod simd {
316318 if _mm256_testz_si256(any, any) == 1 {
317319 continue;
318320 }
319- dct_x8_avx2(&mut t, log2h, r);
321+ dct_x8_avx2(tok, &mut t, log2h, r);
320322 // `round2(v, 4)` on eight lanes: the shift is arithmetic here
321323 // because the value stays in its own 32-bit lane.
322324 let rnd = _mm256_set1_epi32(8);
@@ -330,9 +332,7 @@ mod simd {
330332 }
331333 }
332334
333- /// [`column_pass_dct`] when this CPU has AVX2 (the multiversion AVX2
334- /// clone), or `false` with nothing done.
335- #[multiversion::multiversion(targets("x86_64+avx2"))]
335+ /// [`column_pass_dct`] when this CPU has AVX2, or `false` with nothing done.
336336 pub(super) fn column_pass_dct_dispatch(
337337 residual: &[i32],
338338 out: &mut [i32],
@@ -342,15 +342,12 @@ mod simd {
342342 r: usize,
343343 ud_flip: bool,
344344 ) -> bool {
345- multiversion::target::match_target! {
346- "x86_64+avx2" => {
347- column_pass_dct(residual, out, w, h, log2h, r, ud_flip);
345+ match archmage::X64V3Token::summon() {
346+ Some(t) => {
347+ column_pass_dct(t, residual, out, w, h, log2h, r, ud_flip);
348348 true
349- },
350- _ => {
351- let _ = (residual, out, w, h, log2h, r, ud_flip);
352- false
353- },
349+ }
350+ None => false,
354351 }
355352 }
356353
@@ -369,17 +366,13 @@ mod simd {
369366 /// [`dct_x8_avx2`] when this CPU has AVX2; `false` with nothing done
370367 /// otherwise. For the equivalence gate.
371368 #[cfg(test)]
372- #[multiversion::multiversion(targets("x86_64+avx2"))]
373369 pub(super) fn dct_x8_dispatch(t: &mut [M256; 64], n: u32, r: usize) -> bool {
374- multiversion::target::match_target! {
375- "x86_64+avx2" => {
376- dct_x8_avx2(t, n, r);
370+ match archmage::X64V3Token::summon() {
371+ Some(tok) => {
372+ dct_x8_avx2(tok, t, n, r);
377373 true
378- },
379- _ => {
380- let _ = (t, n, r);
381- false
382- },
374+ }
375+ None => false,
383376 }
384377 }
385378 }
crates/ec-h265/src/simd_tx.rsmodified+54 -29
@@ -19,6 +19,7 @@ pub(crate) use x86::*;
1919 #[cfg(target_arch = "x86_64")]
2020 mod x86 {
2121 use super::*;
22+ use archmage::{SimdToken, X64V3Token, arcane};
2223 use multiversion::target::match_target;
2324 use std::arch::x86_64::*;
2425
@@ -356,8 +357,8 @@ mod x86 {
356357 _mm256_set1_epi32
357358 );
358359
359- #[target_feature(enable = "avx2")]
360- pub(super) fn fwd(residual: &[i32], coeffs: &mut [i32], n: usize) {
360+ #[arcane]
361+ pub(super) fn fwd(_t: X64V3Token, residual: &[i32], coeffs: &mut [i32], n: usize) {
361362 match n {
362363 8 => forward_2d::<64, 192>(residual, coeffs, n, false),
363364 16 => forward_2d::<256, 768>(residual, coeffs, n, false),
@@ -365,8 +366,14 @@ mod x86 {
365366 }
366367 }
367368
368- #[target_feature(enable = "avx2")]
369- pub(super) fn inv(scaled: &[i32], residual: &mut [i32], n: usize, bd_shift: i32) {
369+ #[arcane]
370+ pub(super) fn inv(
371+ _t: X64V3Token,
372+ scaled: &[i32],
373+ residual: &mut [i32],
374+ n: usize,
375+ bd_shift: i32,
376+ ) {
370377 match n {
371378 8 => inverse_2d::<64, 128>(scaled, residual, n, false, bd_shift),
372379 16 => inverse_2d::<256, 512>(scaled, residual, n, false, bd_shift),
@@ -411,13 +418,19 @@ mod x86 {
411418 _mm_set1_epi32
412419 );
413420
414- #[target_feature(enable = "avx2")]
415- pub(super) fn fwd(residual: &[i32], coeffs: &mut [i32], dst: bool) {
421+ #[arcane]
422+ pub(super) fn fwd(_t: X64V3Token, residual: &[i32], coeffs: &mut [i32], dst: bool) {
416423 forward_2d::<16, 48>(residual, coeffs, 4, dst)
417424 }
418425
419- #[target_feature(enable = "avx2")]
420- pub(super) fn inv(scaled: &[i32], residual: &mut [i32], dst: bool, bd_shift: i32) {
426+ #[arcane]
427+ pub(super) fn inv(
428+ _t: X64V3Token,
429+ scaled: &[i32],
430+ residual: &mut [i32],
431+ dst: bool,
432+ bd_shift: i32,
433+ ) {
421434 inverse_2d::<16, 32>(scaled, residual, 4, dst, bd_shift)
422435 }
423436 }
@@ -434,14 +447,17 @@ mod x86 {
434447 assert!(residual.len() >= n * n && coeffs.len() >= n * n && matches!(n, 4 | 8 | 16 | 32));
435448 assert!(!dst || n == 4);
436449 match_target! {
437- "x86_64+avx2" => {
438- if n == 4 {
439- v128::fwd(residual, coeffs, dst)
440- } else {
441- v256::fwd(residual, coeffs, n)
450+ "x86_64+avx2" => match X64V3Token::summon() {
451+ Some(t) => {
452+ if n == 4 {
453+ v128::fwd(t, residual, coeffs, dst)
454+ } else {
455+ v256::fwd(t, residual, coeffs, n)
456+ }
457+ true
442458 }
443- true
444- }
459+ None => false,
460+ },
445461 _ => false
446462 }
447463 }
@@ -459,14 +475,17 @@ mod x86 {
459475 assert!(scaled.len() >= n * n && residual.len() >= n * n && matches!(n, 4 | 8 | 16 | 32));
460476 assert!(!dst || n == 4);
461477 match_target! {
462- "x86_64+avx2" => {
463- if n == 4 {
464- v128::inv(scaled, residual, dst, bd_shift)
465- } else {
466- v256::inv(scaled, residual, n, bd_shift)
478+ "x86_64+avx2" => match X64V3Token::summon() {
479+ Some(t) => {
480+ if n == 4 {
481+ v128::inv(t, scaled, residual, dst, bd_shift)
482+ } else {
483+ v256::inv(t, scaled, residual, n, bd_shift)
484+ }
485+ true
467486 }
468- true
469- }
487+ None => false,
488+ },
470489 _ => {
471490 let _ = bd_shift;
472491 false
@@ -474,8 +493,9 @@ mod x86 {
474493 }
475494 }
476495
477- #[target_feature(enable = "avx2")]
496+ #[arcane]
478497 fn quantize_inner(
498+ _t: X64V3Token,
479499 coeffs: &[i32],
480500 levels: &mut [i32],
481501 count: usize,
@@ -524,7 +544,8 @@ mod x86 {
524544 assert!(count.is_multiple_of(8) && coeffs.len() >= count && levels.len() >= count);
525545 assert!((0..32).contains(&qbits));
526546 match_target! {
527- "x86_64+avx2" => Some(quantize_inner(coeffs, levels, count, scale, offset, qbits)),
547+ "x86_64+avx2" => X64V3Token::summon()
548+ .map(|t| quantize_inner(t, coeffs, levels, count, scale, offset, qbits)),
528549 _ => {
529550 let _ = (scale, offset);
530551 None
@@ -532,8 +553,9 @@ mod x86 {
532553 }
533554 }
534555
535- #[target_feature(enable = "avx2")]
556+ #[arcane]
536557 fn dequant_inner(
558+ _t: X64V3Token,
537559 levels: &[i32],
538560 scaled: &mut [i32],
539561 count: usize,
@@ -594,10 +616,13 @@ mod x86 {
594616 }
595617 assert!(scaled.len() >= count);
596618 match_target! {
597- "x86_64+avx2" => {
598- dequant_inner(levels, scaled, count, mul as i32, round as i32, bd_shift);
599- true
600- }
619+ "x86_64+avx2" => match X64V3Token::summon() {
620+ Some(t) => {
621+ dequant_inner(t, levels, scaled, count, mul as i32, round as i32, bd_shift);
622+ true
623+ }
624+ None => false,
625+ },
601626 _ => false
602627 }
603628 }
crates/ec-vp8/Cargo.tomlmodified+1 -0
@@ -7,6 +7,7 @@ license.workspace = true
77 authors.workspace = true
88
99 [dependencies]
10+archmage = "=0.9.31-beta.1"
1011 bytemuck = "1"
1112 ec-core.workspace = true
1213 multiversion = "0.9"
crates/ec-vp8/src/lib.rsmodified+2 -2
@@ -9,8 +9,8 @@
99 //!
1010 //! # Safety
1111 //!
12-//! Every SIMD kernel is a safe `#[target_feature]` function, dispatched at
13-//! runtime by `multiversion`; the crate-level `forbid` below keeps it so.
12+//! Every SIMD kernel is a safe `#[arcane]` function taking a `X64V3Token`
13+//! proof of AVX2, obtained at runtime; the crate-level `forbid` below keeps it so.
1414
1515 #![forbid(unsafe_code)]
1616 #![warn(missing_docs)]
crates/ec-vp8/src/mc.rsmodified+26 -16
@@ -21,6 +21,7 @@
2121 //! are reproduced exactly (`i32 >> 3` and `& 7` on negatives).
2222 //!
2323 //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386
24+use archmage::SimdToken;
2425 use multiversion::target::match_target;
2526
2627 /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes.
@@ -212,10 +213,12 @@ fn sixtap_2d(
212213
213214 match_target! {
214215 "x86_64+avx2" => {
215- if (sy + rows - 2) * stride + sx + 19 <= src.len() {
216- simd::sixtap_h(src, stride, sx, sy, hf, &mut tmp, rows);
217- simd::sixtap_v(&tmp, vf, dst, dst_stride, bw, bh);
218- return;
216+ if let Some(t) = archmage::X64V3Token::summon() {
217+ if (sy + rows - 2) * stride + sx + 19 <= src.len() {
218+ simd::sixtap_h(t, src, stride, sx, sy, hf, &mut tmp, rows);
219+ simd::sixtap_v(t, &tmp, vf, dst, dst_stride, bw, bh);
220+ return;
221+ }
219222 }
220223 }
221224 _ => {}
@@ -292,10 +295,10 @@ fn sixtap_v_scalar(
292295 /// worst-case input bytes.
293296 ///
294297 /// The kernels are safe functions. Their only precondition is AVX2, which
295-/// the `#[target_feature]` attribute states; they can be called only from a
296-/// context that has AVX2 enabled, which is what [`sixtap_2d`] and
297-/// [`bilinear_2d`] guarantee by dispatching at runtime. Loads and stores go
298-/// through `bytemuck` on slices, so every access is bounds-checked.
298+/// the `X64V3Token` argument proves; the token comes only from `summon()`,
299+/// which checks the CPU at runtime, so [`sixtap_2d`] and [`bilinear_2d`]
300+/// pass it only when AVX2 is present. Loads and stores go through `bytemuck`
301+/// on slices, so every access is bounds-checked.
299302 ///
300303 /// Exactness notes shared by the six-tap kernels:
301304 /// - `_mm*_madd_epi16` multiplies *pairs of samples* by a packed tap pair
@@ -312,6 +315,7 @@ fn sixtap_v_scalar(
312315 #[cfg(target_arch = "x86_64")]
313316 mod simd {
314317 use super::{VP8_FILTER_SHIFT, VP8_FILTER_WEIGHT};
318+ use archmage::{X64V3Token, arcane};
315319 use std::arch::x86_64::*;
316320
317321 /// Tap pair `[t0, t1]` packed into one `i32`, the operand shape
@@ -366,8 +370,9 @@ mod simd {
366370 /// The caller guarantees that for every `i < rows`, the 21 bytes
367371 /// starting at `(sy + i - 2) * stride + sx - 2` are in `src` (with
368372 /// `sx, sy >= 2`); the slice indexing below enforces it.
369- #[target_feature(enable = "avx2")]
373+ #[arcane]
370374 pub(super) fn sixtap_h(
375+ _t: X64V3Token,
371376 src: &[u8],
372377 stride: usize,
373378 sx: usize,
@@ -420,8 +425,9 @@ mod simd {
420425 /// Six-tap second pass: `bh` output rows of `bw` (4/8/16) samples from
421426 /// the 16-lane-strided intermediate. `dst` must hold `bh` rows of `bw`
422427 /// bytes at `dst_stride` (slice indexing enforces it); `bw` is 4, 8 or 16.
423- #[target_feature(enable = "avx2")]
428+ #[arcane]
424429 pub(super) fn sixtap_v(
430+ _t: X64V3Token,
425431 tmp: &[u8; 21 * 16],
426432 vf: &[i32; 6],
427433 dst: &mut [u8],
@@ -479,8 +485,9 @@ mod simd {
479485 /// The caller guarantees that for every `i < rows`, the 17 bytes
480486 /// starting at `(sy + i) * stride + sx` are in `src`; the slice
481487 /// indexing below enforces it.
482- #[target_feature(enable = "avx2")]
488+ #[arcane]
483489 pub(super) fn bilinear_h(
490+ _t: X64V3Token,
484491 src: &[u8],
485492 stride: usize,
486493 sx: usize,
@@ -513,8 +520,9 @@ mod simd {
513520 /// exact-`i16` shape as [`bilinear_h`]. `dst` must hold `bh` rows of
514521 /// `bw` bytes at `dst_stride` (slice indexing enforces it); `bw` is
515522 /// 4, 8 or 16.
516- #[target_feature(enable = "avx2")]
523+ #[arcane]
517524 pub(super) fn bilinear_v(
525+ _t: X64V3Token,
518526 tmp: &[u8; 17 * 16],
519527 vf: &[i32; 2],
520528 dst: &mut [u8],
@@ -601,10 +609,12 @@ fn bilinear_2d(
601609
602610 match_target! {
603611 "x86_64+avx2" => {
604- if (sy + rows - 1) * stride + sx + 16 < src.len() {
605- simd::bilinear_h(src, stride, sx, sy, hf, &mut tmp, rows);
606- simd::bilinear_v(&tmp, vf, dst, dst_stride, bw, bh);
607- return;
612+ if let Some(t) = archmage::X64V3Token::summon() {
613+ if (sy + rows - 1) * stride + sx + 16 < src.len() {
614+ simd::bilinear_h(t, src, stride, sx, sy, hf, &mut tmp, rows);
615+ simd::bilinear_v(t, &tmp, vf, dst, dst_stride, bw, bh);
616+ return;
617+ }
608618 }
609619 }
610620 _ => {}