fixedbitset/block/
mod.rs

1#![allow(clippy::undocumented_unsafe_blocks)]
2#![allow(dead_code)]
3// TODO: Remove once the transmutes are fixed
4#![allow(unknown_lints)]
5#![allow(clippy::missing_transmute_annotations)]
6
7use core::cmp::Ordering;
8use core::hash::{Hash, Hasher};
9
10#[cfg(all(
11    not(all(target_family = "wasm", target_feature = "simd128")),
12    not(target_feature = "sse2"),
13    not(target_feature = "avx"),
14    not(target_feature = "avx2"),
15))]
16mod default;
17#[cfg(all(
18    not(all(target_family = "wasm", target_feature = "simd128")),
19    not(target_feature = "sse2"),
20    not(target_feature = "avx"),
21    not(target_feature = "avx2"),
22))]
23pub use self::default::*;
24
25#[cfg(all(
26    any(target_arch = "x86", target_arch = "x86_64"),
27    target_feature = "sse2",
28    not(target_feature = "avx"),
29    not(target_feature = "avx2"),
30))]
31mod sse2;
32#[cfg(all(
33    any(target_arch = "x86", target_arch = "x86_64"),
34    target_feature = "sse2",
35    not(target_feature = "avx"),
36    not(target_feature = "avx2"),
37))]
38pub use self::sse2::*;
39
40#[cfg(all(
41    any(target_arch = "x86", target_arch = "x86_64"),
42    target_feature = "avx",
43    not(target_feature = "avx2")
44))]
45mod avx;
46#[cfg(all(
47    any(target_arch = "x86", target_arch = "x86_64"),
48    target_feature = "avx",
49    not(target_feature = "avx2")
50))]
51pub use self::avx::*;
52
53#[cfg(all(
54    any(target_arch = "x86", target_arch = "x86_64"),
55    target_feature = "avx2"
56))]
57mod avx2;
58#[cfg(all(
59    any(target_arch = "x86", target_arch = "x86_64"),
60    target_feature = "avx2"
61))]
62pub use self::avx2::*;
63
64#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
65mod wasm;
66#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
67pub use self::wasm::*;
68
69impl Block {
70    pub const USIZE_COUNT: usize = core::mem::size_of::<Self>() / core::mem::size_of::<usize>();
71    pub const NONE: Self = Self::from_usize_array([0; Self::USIZE_COUNT]);
72    pub const ALL: Self = Self::from_usize_array([usize::MAX; Self::USIZE_COUNT]);
73    pub const BITS: usize = core::mem::size_of::<Self>() * 8;
74
75    #[inline]
76    pub fn into_usize_array(self) -> [usize; Self::USIZE_COUNT] {
77        unsafe { core::mem::transmute(self.0) }
78    }
79
80    #[inline]
81    pub const fn from_usize_array(array: [usize; Self::USIZE_COUNT]) -> Self {
82        Self(unsafe { core::mem::transmute(array) })
83    }
84}
85
86impl Eq for Block {}
87
88impl PartialOrd for Block {
89    #[inline]
90    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
91        Some(self.cmp(other))
92    }
93}
94
95impl Ord for Block {
96    #[inline]
97    fn cmp(&self, other: &Self) -> Ordering {
98        self.into_usize_array().cmp(&other.into_usize_array())
99    }
100}
101
102impl Default for Block {
103    #[inline]
104    fn default() -> Self {
105        Self::NONE
106    }
107}
108
109impl Hash for Block {
110    #[inline]
111    fn hash<H: Hasher>(&self, hasher: &mut H) {
112        Hash::hash_slice(&self.into_usize_array(), hasher);
113    }
114}