]> git.proxmox.com Git - rustc.git/blame - src/stdarch/crates/std_detect/src/detect/cache.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / stdarch / crates / std_detect / src / detect / cache.rs
CommitLineData
0531ce1d
XL
1//! Caches run-time feature detection so that it only needs to be computed
2//! once.
3
4#![allow(dead_code)] // not used on all platforms
5
532ac7d7 6use crate::sync::atomic::Ordering;
0531ce1d 7
74b04a01 8use crate::sync::atomic::AtomicUsize;
0531ce1d
XL
9
10/// Sets the `bit` of `x`.
8faf50e0
XL
11#[inline]
12const fn set_bit(x: u64, bit: u32) -> u64 {
0531ce1d
XL
13 x | 1 << bit
14}
15
16/// Tests the `bit` of `x`.
8faf50e0
XL
17#[inline]
18const fn test_bit(x: u64, bit: u32) -> bool {
0531ce1d
XL
19 x & (1 << bit) != 0
20}
21
74b04a01
XL
22/// Unset the `bit of `x`.
23#[inline]
24const fn unset_bit(x: u64, bit: u32) -> u64 {
25 x & !(1 << bit)
26}
27
0531ce1d 28/// Maximum number of features that can be cached.
74b04a01 29const CACHE_CAPACITY: u32 = 62;
0531ce1d
XL
30
31/// This type is used to initialize the cache
32#[derive(Copy, Clone)]
8faf50e0 33pub(crate) struct Initializer(u64);
0531ce1d 34
48663c56 35#[allow(clippy::use_self)]
0531ce1d
XL
36impl Default for Initializer {
37 fn default() -> Self {
38 Initializer(0)
39 }
40}
41
74b04a01
XL
42// NOTE: the `debug_assert!` would catch that we do not add more Features than
43// the one fitting our cache.
0531ce1d
XL
44impl Initializer {
45 /// Tests the `bit` of the cache.
46 #[allow(dead_code)]
8faf50e0 47 #[inline]
0731742a 48 pub(crate) fn test(self, bit: u32) -> bool {
0531ce1d
XL
49 debug_assert!(
50 bit < CACHE_CAPACITY,
51 "too many features, time to increase the cache size!"
52 );
53 test_bit(self.0, bit)
54 }
55
56 /// Sets the `bit` of the cache.
8faf50e0
XL
57 #[inline]
58 pub(crate) fn set(&mut self, bit: u32) {
0531ce1d
XL
59 debug_assert!(
60 bit < CACHE_CAPACITY,
61 "too many features, time to increase the cache size!"
62 );
63 let v = self.0;
64 self.0 = set_bit(v, bit);
65 }
74b04a01
XL
66
67 /// Unsets the `bit` of the cache.
68 #[inline]
69 pub(crate) fn unset(&mut self, bit: u32) {
70 debug_assert!(
71 bit < CACHE_CAPACITY,
72 "too many features, time to increase the cache size!"
73 );
74 let v = self.0;
75 self.0 = unset_bit(v, bit);
76 }
0531ce1d
XL
77}
78
79/// This global variable is a cache of the features supported by the CPU.
74b04a01
XL
80// Note: on x64, we only use the first slot
81static CACHE: [Cache; 2] = [Cache::uninitialized(), Cache::uninitialized()];
0531ce1d 82
74b04a01 83/// Feature cache with capacity for `usize::max_value() - 1` features.
0531ce1d
XL
84///
85/// Note: the last feature bit is used to represent an
86/// uninitialized cache.
74b04a01
XL
87///
88/// Note: we can use `Relaxed` atomic operations, because we are only interested
89/// in the effects of operations on a single memory location. That is, we only
90/// need "modification order", and not the full-blown "happens before". However,
91/// we use `SeqCst` just to be on the safe side.
92struct Cache(AtomicUsize);
0531ce1d 93
0531ce1d 94impl Cache {
74b04a01
XL
95 const CAPACITY: u32 = (core::mem::size_of::<usize>() * 8 - 1) as u32;
96 const MASK: usize = (1 << Cache::CAPACITY) - 1;
97
0531ce1d 98 /// Creates an uninitialized cache.
48663c56 99 #[allow(clippy::declare_interior_mutable_const)]
0531ce1d 100 const fn uninitialized() -> Self {
74b04a01 101 Cache(AtomicUsize::new(usize::max_value()))
0531ce1d
XL
102 }
103 /// Is the cache uninitialized?
8faf50e0
XL
104 #[inline]
105 pub(crate) fn is_uninitialized(&self) -> bool {
74b04a01 106 self.0.load(Ordering::SeqCst) == usize::max_value()
0531ce1d
XL
107 }
108
109 /// Is the `bit` in the cache set?
8faf50e0
XL
110 #[inline]
111 pub(crate) fn test(&self, bit: u32) -> bool {
74b04a01 112 test_bit(self.0.load(Ordering::SeqCst) as u64, bit)
0531ce1d
XL
113 }
114
115 /// Initializes the cache.
8faf50e0 116 #[inline]
74b04a01
XL
117 fn initialize(&self, value: usize) {
118 self.0.store(value, Ordering::SeqCst);
0531ce1d
XL
119 }
120}
121
74b04a01
XL
122cfg_if::cfg_if! {
123 if #[cfg(feature = "std_detect_env_override")] {
124 #[inline(never)]
125 fn initialize(mut value: Initializer) {
126 if let Ok(disable) = crate::env::var("RUST_STD_DETECT_UNSTABLE") {
127 for v in disable.split(" ") {
128 let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
129 }
130 }
131 do_initialize(value);
132 }
133 } else {
134 #[inline]
135 fn initialize(value: Initializer) {
136 do_initialize(value);
0531ce1d
XL
137 }
138 }
74b04a01 139}
0531ce1d 140
74b04a01
XL
141#[inline]
142fn do_initialize(value: Initializer) {
143 CACHE[0].initialize((value.0) as usize & Cache::MASK);
144 CACHE[1].initialize((value.0 >> Cache::CAPACITY) as usize & Cache::MASK);
0531ce1d
XL
145}
146
532ac7d7 147/// Tests the `bit` of the storage. If the storage has not been initialized,
0531ce1d
XL
148/// initializes it with the result of `f()`.
149///
150/// On its first invocation, it detects the CPU features and caches them in the
83c7162d 151/// `CACHE` global variable as an `AtomicU64`.
0531ce1d
XL
152///
153/// It uses the `Feature` variant to index into this variable as a bitset. If
154/// the bit is set, the feature is enabled, and otherwise it is disabled.
74b04a01
XL
155///
156/// If the feature `std_detect_env_override` is enabled looks for the env
157/// variable `RUST_STD_DETECT_UNSTABLE` and uses its its content to disable
158/// Features that would had been otherwise detected.
8faf50e0
XL
159#[inline]
160pub(crate) fn test<F>(bit: u32, f: F) -> bool
0531ce1d
XL
161where
162 F: FnOnce() -> Initializer,
163{
74b04a01
XL
164 let (bit, idx) = if bit < Cache::CAPACITY {
165 (bit, 0)
166 } else {
167 (bit - Cache::CAPACITY, 1)
168 };
169
170 if CACHE[idx].is_uninitialized() {
171 initialize(f())
0531ce1d 172 }
74b04a01 173 CACHE[idx].test(bit)
0531ce1d 174}