@@ -14,36 +14,41 @@ impl core::fmt::Display for TryFromMaskError {
14
14
}
15
15
16
16
macro_rules! define_mask {
17
- { $( #[ $attr: meta] ) * struct $name: ident<const $lanes: ident: usize >( $type: ty) ; } => {
17
+ {
18
+ $( #[ $attr: meta] ) *
19
+ struct $name: ident<const $lanes: ident: usize >(
20
+ crate :: $type: ident<$lanes2: ident>
21
+ ) ;
22
+ } => {
18
23
$( #[ $attr] ) *
19
24
#[ derive( Default , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
20
25
#[ repr( transparent) ]
21
- pub struct $name<const $lanes: usize >( $type)
26
+ pub struct $name<const $lanes: usize >( crate :: $type<$lanes2> )
22
27
where
23
- $type: crate :: LanesAtMost32 ;
28
+ crate :: $type< LANES > : crate :: LanesAtMost32 ;
24
29
25
30
impl <const LANES : usize > Copy for $name<LANES >
26
31
where
27
- $type: crate :: LanesAtMost32 ,
32
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
28
33
{ }
29
34
30
35
impl <const LANES : usize > Clone for $name<LANES >
31
36
where
32
- $type: crate :: LanesAtMost32 ,
37
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
33
38
{
34
39
#[ inline]
35
40
fn clone( & self ) -> Self {
36
41
* self
37
42
}
38
43
}
39
44
40
- impl <const $lanes : usize > $name<$lanes >
45
+ impl <const LANES : usize > $name<LANES >
41
46
where
42
- $type: crate :: LanesAtMost32 ,
47
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
43
48
{
44
49
/// Construct a mask by setting all lanes to the given value.
45
50
pub fn splat( value: bool ) -> Self {
46
- Self ( <$type>:: splat(
51
+ Self ( <crate :: $type< LANES > >:: splat(
47
52
if value {
48
53
-1
49
54
} else {
@@ -76,64 +81,73 @@ macro_rules! define_mask {
76
81
}
77
82
}
78
83
79
- /// Creates a mask from an integer vector.
84
+ /// Converts the mask to the equivalent integer representation, where -1 represents
85
+ /// "set" and 0 represents "unset".
86
+ #[ inline]
87
+ pub fn to_int( self ) -> crate :: $type<LANES > {
88
+ self . 0
89
+ }
90
+
91
+ /// Creates a mask from the equivalent integer representation, where -1 represents
92
+ /// "set" and 0 represents "unset".
80
93
///
81
- /// # Safety
82
- /// All lanes must be either 0 or -1.
94
+ /// Each provided lane must be either 0 or -1.
83
95
#[ inline]
84
- pub unsafe fn from_int_unchecked( value: $type) -> Self {
96
+ pub unsafe fn from_int_unchecked( value: crate :: $type< LANES > ) -> Self {
85
97
Self ( value)
86
98
}
87
99
88
- /// Creates a mask from an integer vector.
100
+ /// Creates a mask from the equivalent integer representation, where -1 represents
101
+ /// "set" and 0 represents "unset".
89
102
///
90
103
/// # Panics
91
104
/// Panics if any lane is not 0 or -1.
92
105
#[ inline]
93
- pub fn from_int( value: $type) -> Self {
106
+ pub fn from_int( value: crate :: $type< LANES > ) -> Self {
94
107
use core:: convert:: TryInto ;
95
108
value. try_into( ) . unwrap( )
96
109
}
97
110
}
98
111
99
- impl <const $lanes : usize > core:: convert:: From <bool > for $name<$lanes >
112
+ impl <const LANES : usize > core:: convert:: From <bool > for $name<LANES >
100
113
where
101
- $type: crate :: LanesAtMost32 ,
114
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
102
115
{
103
116
fn from( value: bool ) -> Self {
104
117
Self :: splat( value)
105
118
}
106
119
}
107
120
108
- impl <const $lanes : usize > core:: convert:: TryFrom <$type> for $name<$lanes >
121
+ impl <const LANES : usize > core:: convert:: TryFrom <crate :: $type< LANES >> for $name<LANES >
109
122
where
110
- $type: crate :: LanesAtMost32 ,
123
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
111
124
{
112
125
type Error = TryFromMaskError ;
113
- fn try_from( value: $type) -> Result <Self , Self :: Error > {
114
- if value. as_slice( ) . iter( ) . all( |x| * x == 0 || * x == -1 ) {
126
+ fn try_from( value: crate :: $type<LANES >) -> Result <Self , Self :: Error > {
127
+ let valid = ( value. lanes_eq( crate :: $type:: <LANES >:: splat( 0 ) ) | value. lanes_eq( crate :: $type:: <LANES >:: splat( -1 ) ) ) . all( ) ;
128
+ if valid {
115
129
Ok ( Self ( value) )
116
130
} else {
117
131
Err ( TryFromMaskError ( ( ) ) )
118
132
}
119
133
}
120
134
}
121
135
122
- impl <const $lanes : usize > core:: convert:: From <$name<$lanes >> for $type
136
+ impl <const LANES : usize > core:: convert:: From <$name<LANES >> for crate :: $type< LANES >
123
137
where
124
- $type: crate :: LanesAtMost32 ,
138
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
125
139
{
126
- fn from( value: $name<$lanes >) -> Self {
140
+ fn from( value: $name<LANES >) -> Self {
127
141
value. 0
128
142
}
129
143
}
130
144
131
- impl <const $lanes : usize > core:: convert:: From <crate :: BitMask <$lanes >> for $name<$lanes >
145
+ impl <const LANES : usize > core:: convert:: From <crate :: BitMask <LANES >> for $name<LANES >
132
146
where
133
- $type: crate :: LanesAtMost32 ,
134
- crate :: BitMask <$lanes >: crate :: LanesAtMost32 ,
147
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
148
+ crate :: BitMask <LANES >: crate :: LanesAtMost32 ,
135
149
{
136
- fn from( value: crate :: BitMask <$lanes >) -> Self {
150
+ fn from( value: crate :: BitMask <LANES >) -> Self {
137
151
// TODO use an intrinsic to do this efficiently (with LLVM's sext instruction)
138
152
let mut mask = Self :: splat( false ) ;
139
153
for lane in 0 ..LANES {
@@ -143,10 +157,10 @@ macro_rules! define_mask {
143
157
}
144
158
}
145
159
146
- impl <const $lanes : usize > core:: convert:: From <$name<$lanes >> for crate :: BitMask <$lanes >
160
+ impl <const LANES : usize > core:: convert:: From <$name<LANES >> for crate :: BitMask <LANES >
147
161
where
148
- $type: crate :: LanesAtMost32 ,
149
- crate :: BitMask <$lanes >: crate :: LanesAtMost32 ,
162
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
163
+ crate :: BitMask <LANES >: crate :: LanesAtMost32 ,
150
164
{
151
165
fn from( value: $name<$lanes>) -> Self {
152
166
// TODO use an intrinsic to do this efficiently (with LLVM's trunc instruction)
@@ -158,9 +172,9 @@ macro_rules! define_mask {
158
172
}
159
173
}
160
174
161
- impl <const $lanes : usize > core:: fmt:: Debug for $name<$lanes >
175
+ impl <const LANES : usize > core:: fmt:: Debug for $name<LANES >
162
176
where
163
- $type: crate :: LanesAtMost32 ,
177
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
164
178
{
165
179
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
166
180
f. debug_list( )
@@ -169,36 +183,36 @@ macro_rules! define_mask {
169
183
}
170
184
}
171
185
172
- impl <const $lanes : usize > core:: fmt:: Binary for $name<$lanes >
186
+ impl <const LANES : usize > core:: fmt:: Binary for $name<LANES >
173
187
where
174
- $type: crate :: LanesAtMost32 ,
188
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
175
189
{
176
190
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
177
191
core:: fmt:: Binary :: fmt( & self . 0 , f)
178
192
}
179
193
}
180
194
181
- impl <const $lanes : usize > core:: fmt:: Octal for $name<$lanes >
195
+ impl <const LANES : usize > core:: fmt:: Octal for $name<LANES >
182
196
where
183
- $type: crate :: LanesAtMost32 ,
197
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
184
198
{
185
199
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
186
200
core:: fmt:: Octal :: fmt( & self . 0 , f)
187
201
}
188
202
}
189
203
190
- impl <const $lanes : usize > core:: fmt:: LowerHex for $name<$lanes >
204
+ impl <const LANES : usize > core:: fmt:: LowerHex for $name<LANES >
191
205
where
192
- $type: crate :: LanesAtMost32 ,
206
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
193
207
{
194
208
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
195
209
core:: fmt:: LowerHex :: fmt( & self . 0 , f)
196
210
}
197
211
}
198
212
199
- impl <const $lanes : usize > core:: fmt:: UpperHex for $name<$lanes >
213
+ impl <const LANES : usize > core:: fmt:: UpperHex for $name<LANES >
200
214
where
201
- $type: crate :: LanesAtMost32 ,
215
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
202
216
{
203
217
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
204
218
core:: fmt:: UpperHex :: fmt( & self . 0 , f)
@@ -207,7 +221,7 @@ macro_rules! define_mask {
207
221
208
222
impl <const LANES : usize > core:: ops:: BitAnd for $name<LANES >
209
223
where
210
- $type: crate :: LanesAtMost32 ,
224
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
211
225
{
212
226
type Output = Self ;
213
227
#[ inline]
@@ -218,7 +232,7 @@ macro_rules! define_mask {
218
232
219
233
impl <const LANES : usize > core:: ops:: BitAnd <bool > for $name<LANES >
220
234
where
221
- $type: crate :: LanesAtMost32 ,
235
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
222
236
{
223
237
type Output = Self ;
224
238
#[ inline]
@@ -229,7 +243,7 @@ macro_rules! define_mask {
229
243
230
244
impl <const LANES : usize > core:: ops:: BitAnd <$name<LANES >> for bool
231
245
where
232
- $type: crate :: LanesAtMost32 ,
246
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
233
247
{
234
248
type Output = $name<LANES >;
235
249
#[ inline]
@@ -240,7 +254,7 @@ macro_rules! define_mask {
240
254
241
255
impl <const LANES : usize > core:: ops:: BitOr for $name<LANES >
242
256
where
243
- $type: crate :: LanesAtMost32 ,
257
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
244
258
{
245
259
type Output = Self ;
246
260
#[ inline]
@@ -251,7 +265,7 @@ macro_rules! define_mask {
251
265
252
266
impl <const LANES : usize > core:: ops:: BitOr <bool > for $name<LANES >
253
267
where
254
- $type: crate :: LanesAtMost32 ,
268
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
255
269
{
256
270
type Output = Self ;
257
271
#[ inline]
@@ -262,7 +276,7 @@ macro_rules! define_mask {
262
276
263
277
impl <const LANES : usize > core:: ops:: BitOr <$name<LANES >> for bool
264
278
where
265
- $type: crate :: LanesAtMost32 ,
279
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
266
280
{
267
281
type Output = $name<LANES >;
268
282
#[ inline]
@@ -273,7 +287,7 @@ macro_rules! define_mask {
273
287
274
288
impl <const LANES : usize > core:: ops:: BitXor for $name<LANES >
275
289
where
276
- $type: crate :: LanesAtMost32 ,
290
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
277
291
{
278
292
type Output = Self ;
279
293
#[ inline]
@@ -284,7 +298,7 @@ macro_rules! define_mask {
284
298
285
299
impl <const LANES : usize > core:: ops:: BitXor <bool > for $name<LANES >
286
300
where
287
- $type: crate :: LanesAtMost32 ,
301
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
288
302
{
289
303
type Output = Self ;
290
304
#[ inline]
@@ -295,7 +309,7 @@ macro_rules! define_mask {
295
309
296
310
impl <const LANES : usize > core:: ops:: BitXor <$name<LANES >> for bool
297
311
where
298
- $type: crate :: LanesAtMost32 ,
312
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
299
313
{
300
314
type Output = $name<LANES >;
301
315
#[ inline]
@@ -306,7 +320,7 @@ macro_rules! define_mask {
306
320
307
321
impl <const LANES : usize > core:: ops:: Not for $name<LANES >
308
322
where
309
- $type: crate :: LanesAtMost32 ,
323
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
310
324
{
311
325
type Output = $name<LANES >;
312
326
#[ inline]
@@ -317,7 +331,7 @@ macro_rules! define_mask {
317
331
318
332
impl <const LANES : usize > core:: ops:: BitAndAssign for $name<LANES >
319
333
where
320
- $type: crate :: LanesAtMost32 ,
334
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
321
335
{
322
336
#[ inline]
323
337
fn bitand_assign( & mut self , rhs: Self ) {
@@ -327,7 +341,7 @@ macro_rules! define_mask {
327
341
328
342
impl <const LANES : usize > core:: ops:: BitAndAssign <bool > for $name<LANES >
329
343
where
330
- $type: crate :: LanesAtMost32 ,
344
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
331
345
{
332
346
#[ inline]
333
347
fn bitand_assign( & mut self , rhs: bool ) {
@@ -337,7 +351,7 @@ macro_rules! define_mask {
337
351
338
352
impl <const LANES : usize > core:: ops:: BitOrAssign for $name<LANES >
339
353
where
340
- $type: crate :: LanesAtMost32 ,
354
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
341
355
{
342
356
#[ inline]
343
357
fn bitor_assign( & mut self , rhs: Self ) {
@@ -347,7 +361,7 @@ macro_rules! define_mask {
347
361
348
362
impl <const LANES : usize > core:: ops:: BitOrAssign <bool > for $name<LANES >
349
363
where
350
- $type: crate :: LanesAtMost32 ,
364
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
351
365
{
352
366
#[ inline]
353
367
fn bitor_assign( & mut self , rhs: bool ) {
@@ -357,7 +371,7 @@ macro_rules! define_mask {
357
371
358
372
impl <const LANES : usize > core:: ops:: BitXorAssign for $name<LANES >
359
373
where
360
- $type: crate :: LanesAtMost32 ,
374
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
361
375
{
362
376
#[ inline]
363
377
fn bitxor_assign( & mut self , rhs: Self ) {
@@ -367,13 +381,15 @@ macro_rules! define_mask {
367
381
368
382
impl <const LANES : usize > core:: ops:: BitXorAssign <bool > for $name<LANES >
369
383
where
370
- $type: crate :: LanesAtMost32 ,
384
+ crate :: $type< LANES > : crate :: LanesAtMost32 ,
371
385
{
372
386
#[ inline]
373
387
fn bitxor_assign( & mut self , rhs: bool ) {
374
388
* self ^= Self :: splat( rhs) ;
375
389
}
376
390
}
391
+
392
+ impl_full_mask_reductions! { $name, $type }
377
393
}
378
394
}
379
395
0 commit comments