1- use crate :: LanesAtMost32 ;
2-
31/// A mask where each lane is represented by a single bit.
42#[ derive( Copy , Clone , Debug , PartialOrd , PartialEq , Ord , Eq , Hash ) ]
53#[ repr( transparent) ]
6- pub struct BitMask < const LANES : usize > ( u64 )
4+ pub struct BitMask < const LANES : usize > ( u64 ) ;
75
86impl < const LANES : usize > BitMask < LANES >
9- where
10- Self : LanesAtMost32 ,
117{
128 #[ inline]
139 pub fn splat ( value : bool ) -> Self {
@@ -25,13 +21,50 @@ where
2521
2622 #[ inline]
2723 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 )
2964 }
3065}
3166
3267impl < const LANES : usize > core:: ops:: BitAnd for BitMask < LANES >
33- where
34- Self : LanesAtMost32 ,
3568{
3669 type Output = Self ;
3770 #[ inline]
4174}
4275
4376impl < const LANES : usize > core:: ops:: BitAnd < bool > for BitMask < LANES >
44- where
45- Self : LanesAtMost32 ,
4677{
4778 type Output = Self ;
4879 #[ inline]
5283}
5384
5485impl < const LANES : usize > core:: ops:: BitAnd < BitMask < LANES > > for bool
55- where
56- BitMask < LANES > : LanesAtMost32 ,
5786{
5887 type Output = BitMask < LANES > ;
5988 #[ inline]
6392}
6493
6594impl < const LANES : usize > core:: ops:: BitOr for BitMask < LANES >
66- where
67- Self : LanesAtMost32 ,
6895{
6996 type Output = Self ;
7097 #[ inline]
@@ -73,31 +100,7 @@ where
73100 }
74101}
75102
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-
98103impl < const LANES : usize > core:: ops:: BitXor for BitMask < LANES >
99- where
100- Self : LanesAtMost32 ,
101104{
102105 type Output = Self ;
103106 #[ inline]
@@ -106,95 +109,42 @@ where
106109 }
107110}
108111
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-
131112impl < const LANES : usize > core:: ops:: Not for BitMask < LANES >
132- where
133- Self : LanesAtMost32 ,
134113{
135114 type Output = BitMask < LANES > ;
136115 #[ inline]
137116 fn not ( self ) -> Self :: Output {
138- Self ( !self . 0 )
117+ Self ( !self . 0 ) & Self :: splat ( true )
139118 }
140119}
141120
142121impl < const LANES : usize > core:: ops:: BitAndAssign for BitMask < LANES >
143- where
144- Self : LanesAtMost32 ,
145122{
146123 #[ inline]
147124 fn bitand_assign ( & mut self , rhs : Self ) {
148125 self . 0 &= rhs. 0 ;
149126 }
150127}
151128
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-
162129impl < const LANES : usize > core:: ops:: BitOrAssign for BitMask < LANES >
163- where
164- Self : LanesAtMost32 ,
165130{
166131 #[ inline]
167132 fn bitor_assign ( & mut self , rhs : Self ) {
168133 self . 0 |= rhs. 0 ;
169134 }
170135}
171136
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-
182137impl < const LANES : usize > core:: ops:: BitXorAssign for BitMask < LANES >
183- where
184- Self : LanesAtMost32 ,
185138{
186139 #[ inline]
187140 fn bitxor_assign ( & mut self , rhs : Self ) {
188141 self . 0 ^= rhs. 0 ;
189142 }
190143}
191144
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