summaryrefslogtreecommitdiffstats
path: root/third_party/rust/packed_simd/src/masks.rs
blob: 04534eab2ba090aaabe78bef83b2ff558d2bc167 (plain)
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
//! Mask types

macro_rules! impl_mask_ty {
    ($id:ident : $elem_ty:ident | #[$doc:meta]) => {
        #[$doc]
        #[derive(Copy, Clone)]
        pub struct $id($elem_ty);

        impl crate::sealed::Seal for $id {}
        impl crate::sealed::Mask for $id {
            #[inline]
            fn test(&self) -> bool {
                $id::test(self)
            }
        }

        impl $id {
            /// Instantiate a mask with `value`
            #[inline]
            pub fn new(x: bool) -> Self {
                if x {
                    $id(!0)
                } else {
                    $id(0)
                }
            }
            /// Test if the mask is set
            #[inline]
            pub fn test(&self) -> bool {
                self.0 != 0
            }
        }

        impl Default for $id {
            #[inline]
            fn default() -> Self {
                $id(0)
            }
        }

        #[allow(clippy::partialeq_ne_impl)]
        impl PartialEq<$id> for $id {
            #[inline]
            fn eq(&self, other: &Self) -> bool {
                self.0 == other.0
            }
            #[inline]
            fn ne(&self, other: &Self) -> bool {
                self.0 != other.0
            }
        }

        impl Eq for $id {}

        impl PartialOrd<$id> for $id {
            #[inline]
            fn partial_cmp(&self, other: &Self) -> Option<crate::cmp::Ordering> {
                use crate::cmp::Ordering;
                if self == other {
                    Some(Ordering::Equal)
                } else if self.0 > other.0 {
                    // Note:
                    //  * false = 0_i
                    //  * true == !0_i == -1_i
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            }

            #[inline]
            fn lt(&self, other: &Self) -> bool {
                self.0 > other.0
            }
            #[inline]
            fn gt(&self, other: &Self) -> bool {
                self.0 < other.0
            }
            #[inline]
            fn le(&self, other: &Self) -> bool {
                self.0 >= other.0
            }
            #[inline]
            fn ge(&self, other: &Self) -> bool {
                self.0 <= other.0
            }
        }

        impl Ord for $id {
            #[inline]
            fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
                match self.partial_cmp(other) {
                    Some(x) => x,
                    None => unsafe { crate::hint::unreachable_unchecked() },
                }
            }
        }

        impl crate::hash::Hash for $id {
            #[inline]
            fn hash<H: crate::hash::Hasher>(&self, state: &mut H) {
                (self.0 != 0).hash(state);
            }
        }

        impl crate::fmt::Debug for $id {
            #[inline]
            fn fmt(&self, fmtter: &mut crate::fmt::Formatter<'_>) -> Result<(), crate::fmt::Error> {
                write!(fmtter, "{}({})", stringify!($id), self.0 != 0)
            }
        }
    };
}

impl_mask_ty!(m8: i8 | /// 8-bit wide mask.
);
impl_mask_ty!(m16: i16 | /// 16-bit wide mask.
);
impl_mask_ty!(m32: i32 | /// 32-bit wide mask.
);
impl_mask_ty!(m64: i64 | /// 64-bit wide mask.
);
impl_mask_ty!(m128: i128 | /// 128-bit wide mask.
);
impl_mask_ty!(msize: isize | /// isize-wide mask.
);