1
- use crate :: LanesAtMost32 ;
2
-
3
1
/// A mask where each lane is represented by a single bit.
4
2
#[ derive( Copy , Clone , Debug , PartialOrd , PartialEq , Ord , Eq , Hash ) ]
5
3
#[ repr( transparent) ]
6
- pub struct BitMask < const LANES : usize > ( u64 )
4
+ pub struct BitMask < const LANES : usize > ( u64 ) ;
7
5
8
6
impl < const LANES : usize > BitMask < LANES >
9
- where
10
- Self : LanesAtMost32 ,
11
7
{
12
8
#[ inline]
13
9
pub fn splat ( value : bool ) -> Self {
@@ -25,13 +21,50 @@ where
25
21
26
22
#[ inline]
27
23
pub unsafe fn set_unchecked ( & mut self , lane : usize , value : bool ) {
28
- self . 0 ^= ( ( value ^ self . test ( lane) ) as u64 ) << lane
24
+ self . 0 ^= ( ( value ^ self . test_unchecked ( lane) ) as u64 ) << lane
25
+ }
26
+
27
+ #[ inline]
28
+ pub fn to_int < V , T > ( self ) -> V
29
+ where
30
+ V : Default + AsMut < [ T ; LANES ] > ,
31
+ T : From < i8 > ,
32
+ {
33
+ // TODO this should be an intrinsic sign-extension
34
+ let mut v = V :: default ( ) ;
35
+ for i in 0 ..LANES {
36
+ let lane = unsafe { self . test_unchecked ( i) } ;
37
+ v. as_mut ( ) [ i] = ( -( lane as i8 ) ) . into ( ) ;
38
+ }
39
+ v
40
+ }
41
+
42
+ #[ inline]
43
+ pub unsafe fn from_int_unchecked < V > ( value : V ) -> Self
44
+ where
45
+ V : crate :: LanesAtMost32 ,
46
+ {
47
+ let mask: V :: BitMask = crate :: intrinsics:: simd_bitmask ( value) ;
48
+ Self ( mask. into ( ) )
49
+ }
50
+
51
+ #[ inline]
52
+ pub fn to_bitmask ( self ) -> u64 {
53
+ self . 0
54
+ }
55
+
56
+ #[ inline]
57
+ pub fn any ( self ) -> bool {
58
+ self != Self :: splat ( false )
59
+ }
60
+
61
+ #[ inline]
62
+ pub fn all ( self ) -> bool {
63
+ self == Self :: splat ( true )
29
64
}
30
65
}
31
66
32
67
impl < const LANES : usize > core:: ops:: BitAnd for BitMask < LANES >
33
- where
34
- Self : LanesAtMost32 ,
35
68
{
36
69
type Output = Self ;
37
70
#[ inline]
41
74
}
42
75
43
76
impl < const LANES : usize > core:: ops:: BitAnd < bool > for BitMask < LANES >
44
- where
45
- Self : LanesAtMost32 ,
46
77
{
47
78
type Output = Self ;
48
79
#[ inline]
52
83
}
53
84
54
85
impl < const LANES : usize > core:: ops:: BitAnd < BitMask < LANES > > for bool
55
- where
56
- BitMask < LANES > : LanesAtMost32 ,
57
86
{
58
87
type Output = BitMask < LANES > ;
59
88
#[ inline]
63
92
}
64
93
65
94
impl < const LANES : usize > core:: ops:: BitOr for BitMask < LANES >
66
- where
67
- Self : LanesAtMost32 ,
68
95
{
69
96
type Output = Self ;
70
97
#[ inline]
@@ -73,31 +100,7 @@ where
73
100
}
74
101
}
75
102
76
- impl < const LANES : usize > core:: ops:: BitOr < bool > for BitMask < LANES >
77
- where
78
- Self : LanesAtMost32 ,
79
- {
80
- type Output = Self ;
81
- #[ inline]
82
- fn bitor ( self , rhs : bool ) -> Self {
83
- self | Self :: splat ( rhs)
84
- }
85
- }
86
-
87
- impl < const LANES : usize > core:: ops:: BitOr < BitMask < LANES > > for bool
88
- where
89
- BitMask < LANES > : LanesAtMost32 ,
90
- {
91
- type Output = BitMask < LANES > ;
92
- #[ inline]
93
- fn bitor ( self , rhs : BitMask < LANES > ) -> BitMask < LANES > {
94
- BitMask :: < LANES > :: splat ( self ) | rhs
95
- }
96
- }
97
-
98
103
impl < const LANES : usize > core:: ops:: BitXor for BitMask < LANES >
99
- where
100
- Self : LanesAtMost32 ,
101
104
{
102
105
type Output = Self ;
103
106
#[ inline]
@@ -106,95 +109,42 @@ where
106
109
}
107
110
}
108
111
109
- impl < const LANES : usize > core:: ops:: BitXor < bool > for BitMask < LANES >
110
- where
111
- Self : LanesAtMost32 ,
112
- {
113
- type Output = Self ;
114
- #[ inline]
115
- fn bitxor ( self , rhs : bool ) -> Self :: Output {
116
- self ^ Self :: splat ( rhs)
117
- }
118
- }
119
-
120
- impl < const LANES : usize > core:: ops:: BitXor < BitMask < LANES > > for bool
121
- where
122
- BitMask < LANES > : LanesAtMost32 ,
123
- {
124
- type Output = BitMask < LANES > ;
125
- #[ inline]
126
- fn bitxor ( self , rhs : BitMask < LANES > ) -> Self :: Output {
127
- BitMask :: < LANES > :: splat ( self ) ^ rhs
128
- }
129
- }
130
-
131
112
impl < const LANES : usize > core:: ops:: Not for BitMask < LANES >
132
- where
133
- Self : LanesAtMost32 ,
134
113
{
135
114
type Output = BitMask < LANES > ;
136
115
#[ inline]
137
116
fn not ( self ) -> Self :: Output {
138
- Self ( !self . 0 )
117
+ Self ( !self . 0 ) & Self :: splat ( true )
139
118
}
140
119
}
141
120
142
121
impl < const LANES : usize > core:: ops:: BitAndAssign for BitMask < LANES >
143
- where
144
- Self : LanesAtMost32 ,
145
122
{
146
123
#[ inline]
147
124
fn bitand_assign ( & mut self , rhs : Self ) {
148
125
self . 0 &= rhs. 0 ;
149
126
}
150
127
}
151
128
152
- impl < const LANES : usize > core:: ops:: BitAndAssign < bool > for BitMask < LANES >
153
- where
154
- Self : LanesAtMost32 ,
155
- {
156
- #[ inline]
157
- fn bitand_assign ( & mut self , rhs : bool ) {
158
- * self &= Self :: splat ( rhs) ;
159
- }
160
- }
161
-
162
129
impl < const LANES : usize > core:: ops:: BitOrAssign for BitMask < LANES >
163
- where
164
- Self : LanesAtMost32 ,
165
130
{
166
131
#[ inline]
167
132
fn bitor_assign ( & mut self , rhs : Self ) {
168
133
self . 0 |= rhs. 0 ;
169
134
}
170
135
}
171
136
172
- impl < const LANES : usize > core:: ops:: BitOrAssign < bool > for BitMask < LANES >
173
- where
174
- Self : LanesAtMost32 ,
175
- {
176
- #[ inline]
177
- fn bitor_assign ( & mut self , rhs : bool ) {
178
- * self |= Self :: splat ( rhs) ;
179
- }
180
- }
181
-
182
137
impl < const LANES : usize > core:: ops:: BitXorAssign for BitMask < LANES >
183
- where
184
- Self : LanesAtMost32 ,
185
138
{
186
139
#[ inline]
187
140
fn bitxor_assign ( & mut self , rhs : Self ) {
188
141
self . 0 ^= rhs. 0 ;
189
142
}
190
143
}
191
144
192
- impl < const LANES : usize > core:: ops:: BitXorAssign < bool > for BitMask < LANES >
193
- where
194
- Self : LanesAtMost32 ,
195
- {
196
- #[ inline]
197
- fn bitxor_assign ( & mut self , rhs : bool ) {
198
- * self ^= Self :: splat ( rhs) ;
199
- }
200
- }
145
+ pub type Mask8 < const LANES : usize > = BitMask < LANES > ;
146
+ pub type Mask16 < const LANES : usize > = BitMask < LANES > ;
147
+ pub type Mask32 < const LANES : usize > = BitMask < LANES > ;
148
+ pub type Mask64 < const LANES : usize > = BitMask < LANES > ;
149
+ pub type Mask128 < const LANES : usize > = BitMask < LANES > ;
150
+ pub type MaskSize < const LANES : usize > = BitMask < LANES > ;
0 commit comments