Debian Codegraph @

.cargo/vendor/rav1e/src/cpu_features/x86.rs

raw
1 // Copyright (c) 2019-2022, The rav1e contributors. All rights reserved
2 //
3 // This source code is subject to the terms of the BSD 2 Clause License and
4 // the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5 // was not distributed with this source code in the LICENSE file, you can
6 // obtain it at www.aomedia.org/license/software. If the Alliance for Open
7 // Media Patent License 1.0 was not distributed with this source code in the
8 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10 use arg_enum_proc_macro::ArgEnum;
11 use std::env;
12 use std::str::FromStr;
13
14 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, ArgEnum)]
15 pub enum CpuFeatureLevel {
16 RUST,
17 SSE2,
18 SSSE3,
19 #[arg_enum(alias = "sse4.1")]
20 SSE4_1,
21 AVX2,
22 AVX512,
23 #[arg_enum(alias = "avx512vpclmulqdq")]
24 AVX512ICL,
25 }
26
27 impl CpuFeatureLevel {
28 #[cfg(test)]
29 pub(crate) const fn all() -> &'static [Self] {
30 use CpuFeatureLevel::*;
31 &[RUST, SSE2, SSSE3, SSE4_1, AVX2, AVX512, AVX512ICL]
32 }
33
34 pub const fn len() -> usize {
35 CpuFeatureLevel::AVX512ICL as usize + 1
36 }
37
38 #[inline(always)]
39 pub const fn as_index(self) -> usize {
40 self as usize
41 }
42 }
43
44 impl Default for CpuFeatureLevel {
45 fn default() -> CpuFeatureLevel {
46 fn avx512_detected() -> bool {
47 is_x86_feature_detected!("avx512bw")
48 && is_x86_feature_detected!("avx512cd")
49 && is_x86_feature_detected!("avx512dq")
50 && is_x86_feature_detected!("avx512f")
51 && is_x86_feature_detected!("avx512vl")
52 }
53 #[allow(deprecated)] // Until MSRV >= 1.69.0
54 fn avx512icl_detected() -> bool {
55 // Per dav1d, these are the flags needed.
56 avx512_detected()
57 && is_x86_feature_detected!("avx512vnni")
58 && is_x86_feature_detected!("avx512ifma")
59 && is_x86_feature_detected!("avx512vbmi")
60 && is_x86_feature_detected!("avx512vbmi2")
61 && is_x86_feature_detected!("avx512vpopcntdq")
62 && is_x86_feature_detected!("avx512bitalg")
63 && is_x86_feature_detected!("avx512gfni")
64 && is_x86_feature_detected!("avx512vaes")
65 && is_x86_feature_detected!("avx512vpclmulqdq")
66 }
67
68 let detected: CpuFeatureLevel = if avx512icl_detected() {
69 CpuFeatureLevel::AVX512ICL
70 } else if avx512_detected() {
71 CpuFeatureLevel::AVX512
72 } else if is_x86_feature_detected!("avx2") {
73 CpuFeatureLevel::AVX2
74 } else if is_x86_feature_detected!("sse4.1") {
75 CpuFeatureLevel::SSE4_1
76 } else if is_x86_feature_detected!("ssse3") {
77 CpuFeatureLevel::SSSE3
78 } else if is_x86_feature_detected!("sse2") {
79 CpuFeatureLevel::SSE2
80 } else {
81 CpuFeatureLevel::RUST
82 };
83 let manual: CpuFeatureLevel = match env::var("RAV1E_CPU_TARGET") {
84 Ok(feature) => CpuFeatureLevel::from_str(&feature).unwrap_or(detected),
85 Err(_e) => detected,
86 };
87 if manual > detected {
88 detected
89 } else {
90 manual
91 }
92 }
93 }
94
95 // Create a static lookup table for CPUFeatureLevel enums
96 // Note: keys are CpuFeatureLevels without any prefix (no CpuFeatureLevel::)
97 macro_rules! cpu_function_lookup_table {
98 // version for default visibility
99 ($name:ident: [$type:ty], default: $empty:expr, [$(($key:ident, $value:expr)),*]) => {
100 static $name: [$type; crate::cpu_features::CpuFeatureLevel::len()] = {
101 use crate::cpu_features::CpuFeatureLevel;
102 #[allow(unused_mut)]
103 let mut out: [$type; CpuFeatureLevel::len()] = [$empty; CpuFeatureLevel::len()];
104
105 // Can't use out[0][.] == $empty in static as of rust 1.40
106 #[allow(unused_mut)]
107 let mut set: [bool; CpuFeatureLevel::len()] = [false; CpuFeatureLevel::len()];
108
109 #[allow(unused_imports)]
110 use CpuFeatureLevel::*;
111 $(
112 out[$key as usize] = $value;
113 set[$key as usize] = true;
114 )*
115 cpu_function_lookup_table!(waterfall_cpu_features(out, set, [SSE2, SSSE3, SSE4_1, AVX2, AVX512, AVX512ICL]));
116 out
117 };
118 };
119 ($pub:vis, $name:ident: [$type:ty], default: $empty:expr, [$(($key:ident, $value:expr)),*]) => {
120 $pub cpu_function_lookup_table!($name: [$type], default: $empty, [$(($key, $value)),*]);
121 };
122
123 // Fill empty output functions with the existent functions they support.
124 // cpus should be in order of lowest cpu level to highest
125 // Used like an internal function
126 // Put in here to avoid adding more public macros
127 (waterfall_cpu_features($out:ident, $set:ident, [$($cpu:ident),*])) => {
128 // Use an array to emulate if statements (not supported in static as of
129 // rust 1.40). Setting best[0] (false) and best[1] (true) is equivalent to
130 // doing nothing and overriding our value respectively.
131 #[allow(unused_assignments)]
132 let mut best = [$out[0], $out[0]];
133 $(
134 // If the current entry has a function, update out best function.
135 best[$set[$cpu as usize] as usize] = $out[$cpu as usize];
136 // Update our current entry. Does nothing if it already had a function.
137 $out[$cpu as usize] = best[1];
138 )*
139 };
140
141 // use $name_$key as our values
142 ($pub:vis, $name:ident: [$type:ty], default: $empty:expr, [$($key:ident),*]) => {
143 paste::item!{
144 cpu_function_lookup_table!(
145 $pub, $name: [$type], default: $empty, [$(($key, [<$name _$key>])),*]
146 );
147 }
148 };
149
150 // version for default visibility
151 ($name:ident: [$type:ty], default: $empty:expr, [$($key:ident),*]) => {
152 paste::item!{
153 cpu_function_lookup_table!(
154 $name: [$type], default: $empty, [$(($key, [<$name _$key>])),*]
155 );
156 }
157 };
158 }