      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161

    //! Caches run-time feature detection so that it only needs to be computed
    //! once.

    #![allow(dead_code)] // not used on all platforms

    use core::sync::atomic::Ordering;

    #[cfg(target_pointer_width = "64")]
    use core::sync::atomic::AtomicU64;

    #[cfg(target_pointer_width = "32")]
    use core::sync::atomic::AtomicU32;

    /// Sets the `bit` of `x`.
    #[inline]
    const fn set_bit(x: u64, bit: u32) -> u64 {
        x | 1 << bit
    }

    /// Tests the `bit` of `x`.
    #[inline]
    const fn test_bit(x: u64, bit: u32) -> bool {
        x & (1 << bit) != 0
    }

    /// Maximum number of features that can be cached.
    const CACHE_CAPACITY: u32 = 63;

    /// This type is used to initialize the cache
    #[derive(Copy, Clone)]
    pub(crate) struct Initializer(u64);

    impl Default for Initializer {
        fn default() -> Self {
            Initializer(0)
        }
    }

    impl Initializer {
        /// Tests the `bit` of the cache.
        #[allow(dead_code)]
        #[inline]
        pub(crate) fn test(&self, bit: u32) -> bool {
            // FIXME: this way of making sure that the cache is large enough is
            // brittle.
            debug_assert!(
                bit < CACHE_CAPACITY,
                "too many features, time to increase the cache size!"
            );
            test_bit(self.0, bit)
        }

        /// Sets the `bit` of the cache.
        #[inline]
        pub(crate) fn set(&mut self, bit: u32) {
            // FIXME: this way of making sure that the cache is large enough is
            // brittle.
            debug_assert!(
                bit < CACHE_CAPACITY,
                "too many features, time to increase the cache size!"
            );
            let v = self.0;
            self.0 = set_bit(v, bit);
        }
    }

    /// This global variable is a cache of the features supported by the CPU.
    static CACHE: Cache = Cache::uninitialized();

    /// Feature cache with capacity for `CACHE_CAPACITY` features.
    ///
    /// Note: the last feature bit is used to represent an
    /// uninitialized cache.
    #[cfg(target_pointer_width = "64")]
    struct Cache(AtomicU64);

    #[cfg(target_pointer_width = "64")]
    impl Cache {
        /// Creates an uninitialized cache.
        const fn uninitialized() -> Self {
            Cache(AtomicU64::new(u64::max_value()))
        }
        /// Is the cache uninitialized?
        #[inline]
        pub(crate) fn is_uninitialized(&self) -> bool {
            self.0.load(Ordering::Relaxed) == u64::max_value()
        }

        /// Is the `bit` in the cache set?
        #[inline]
        pub(crate) fn test(&self, bit: u32) -> bool {
            test_bit(CACHE.0.load(Ordering::Relaxed), bit)
        }

        /// Initializes the cache.
        #[inline]
        pub(crate) fn initialize(&self, value: Initializer) {
            self.0.store(value.0, Ordering::Relaxed);
        }
    }

    /// Feature cache with capacity for `CACHE_CAPACITY` features.
    ///
    /// Note: the last feature bit is used to represent an
    /// uninitialized cache.
    #[cfg(target_pointer_width = "32")]
    struct Cache(AtomicU32, AtomicU32);

    #[cfg(target_pointer_width = "32")]
    impl Cache {
        /// Creates an uninitialized cache.
        const fn uninitialized() -> Self {
            Cache(
                AtomicU32::new(u32::max_value()),
                AtomicU32::new(u32::max_value()),
            )
        }
        /// Is the cache uninitialized?
        #[inline]
        pub(crate) fn is_uninitialized(&self) -> bool {
            self.1.load(Ordering::Relaxed) == u32::max_value()
        }

        /// Is the `bit` in the cache set?
        #[inline]
        pub(crate) fn test(&self, bit: u32) -> bool {
            if bit < 32 {
                test_bit(CACHE.0.load(Ordering::Relaxed) as u64, bit)
            } else {
                test_bit(CACHE.1.load(Ordering::Relaxed) as u64, bit - 32)
            }
        }

        /// Initializes the cache.
        #[inline]
        pub(crate) fn initialize(&self, value: Initializer) {
            let lo: u32 = value.0 as u32;
            let hi: u32 = (value.0 >> 32) as u32;
            self.0.store(lo, Ordering::Relaxed);
            self.1.store(hi, Ordering::Relaxed);
        }
    }

    /// Test the `bit` of the storage. If the storage has not been initialized,
    /// initializes it with the result of `f()`.
    ///
    /// On its first invocation, it detects the CPU features and caches them in the
    /// `CACHE` global variable as an `AtomicU64`.
    ///
    /// It uses the `Feature` variant to index into this variable as a bitset. If
    /// the bit is set, the feature is enabled, and otherwise it is disabled.
    #[inline]
    pub(crate) fn test<F>(bit: u32, f: F) -> bool
    where
        F: FnOnce() -> Initializer,
    {
        if CACHE.is_uninitialized() {
            CACHE.initialize(f());
        }
        CACHE.test(bit)
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)