1212) ]
1313mod mask_impl;
1414
15- use crate :: simd:: {
16- cmp:: SimdPartialEq , intrinsics, LaneCount , Simd , SimdCast , SimdElement , SupportedLaneCount ,
17- } ;
15+ use crate :: simd:: { LaneCount , Simd , SimdCast , SimdElement , SupportedLaneCount } ;
1816use core:: cmp:: Ordering ;
1917use core:: { fmt, mem} ;
2018
@@ -35,7 +33,7 @@ mod sealed {
3533
3634 fn eq ( self , other : Self ) -> bool ;
3735
38- fn as_usize ( self ) -> usize ;
36+ fn to_usize ( self ) -> usize ;
3937
4038 type Unsigned : SimdElement ;
4139
@@ -60,14 +58,23 @@ macro_rules! impl_element {
6058 where
6159 LaneCount <N >: SupportedLaneCount ,
6260 {
63- ( value. simd_eq( Simd :: splat( 0 as _) ) | value. simd_eq( Simd :: splat( -1 as _) ) ) . all( )
61+ // We can't use `Simd` directly, because `Simd`'s functions call this function and
62+ // we will end up with an infinite loop.
63+ // Safety: `value` is an integer vector
64+ unsafe {
65+ use core:: intrinsics:: simd;
66+ let falses: Simd <Self , N > = simd:: simd_eq( value, Simd :: splat( 0 as _) ) ;
67+ let trues: Simd <Self , N > = simd:: simd_eq( value, Simd :: splat( -1 as _) ) ;
68+ let valid: Simd <Self , N > = simd:: simd_or( falses, trues) ;
69+ simd:: simd_reduce_all( valid)
70+ }
6471 }
6572
6673 #[ inline]
6774 fn eq( self , other: Self ) -> bool { self == other }
6875
6976 #[ inline]
70- fn as_usize ( self ) -> usize {
77+ fn to_usize ( self ) -> usize {
7178 self as usize
7279 }
7380
@@ -141,8 +148,9 @@ where
141148 // but these are "dependently-sized" types, so copy elision it is!
142149 unsafe {
143150 let bytes: [ u8 ; N ] = mem:: transmute_copy ( & array) ;
144- let bools: Simd < i8 , N > = intrinsics:: simd_ne ( Simd :: from_array ( bytes) , Simd :: splat ( 0u8 ) ) ;
145- Mask :: from_int_unchecked ( intrinsics:: simd_cast ( bools) )
151+ let bools: Simd < i8 , N > =
152+ core:: intrinsics:: simd:: simd_ne ( Simd :: from_array ( bytes) , Simd :: splat ( 0u8 ) ) ;
153+ Mask :: from_int_unchecked ( core:: intrinsics:: simd:: simd_cast ( bools) )
146154 }
147155 }
148156
@@ -160,7 +168,7 @@ where
160168 // This would be hypothetically valid as an "in-place" transmute,
161169 // but these are "dependently-sized" types, so copy elision it is!
162170 unsafe {
163- let mut bytes: Simd < i8 , N > = intrinsics:: simd_cast ( self . to_int ( ) ) ;
171+ let mut bytes: Simd < i8 , N > = core :: intrinsics:: simd :: simd_cast ( self . to_int ( ) ) ;
164172 bytes &= Simd :: splat ( 1i8 ) ;
165173 mem:: transmute_copy ( & bytes)
166174 }
@@ -175,7 +183,10 @@ where
175183 #[ must_use = "method returns a new mask and does not mutate the original value" ]
176184 pub unsafe fn from_int_unchecked ( value : Simd < T , N > ) -> Self {
177185 // Safety: the caller must confirm this invariant
178- unsafe { Self ( mask_impl:: Mask :: from_int_unchecked ( value) ) }
186+ unsafe {
187+ core:: intrinsics:: assume ( <T as Sealed >:: valid ( value) ) ;
188+ Self ( mask_impl:: Mask :: from_int_unchecked ( value) )
189+ }
179190 }
180191
181192 /// Converts a vector of integers to a mask, where 0 represents `false` and -1
@@ -374,23 +385,25 @@ where
374385 ) ;
375386
376387 // Safety: the input and output are integer vectors
377- let index: Simd < T , N > = unsafe { intrinsics:: simd_cast ( index) } ;
388+ let index: Simd < T , N > = unsafe { core :: intrinsics:: simd :: simd_cast ( index) } ;
378389
379390 let masked_index = self . select ( index, Self :: splat ( true ) . to_int ( ) ) ;
380391
381392 // Safety: the input and output are integer vectors
382- let masked_index: Simd < T :: Unsigned , N > = unsafe { intrinsics:: simd_cast ( masked_index) } ;
393+ let masked_index: Simd < T :: Unsigned , N > =
394+ unsafe { core:: intrinsics:: simd:: simd_cast ( masked_index) } ;
383395
384396 // Safety: the input is an integer vector
385- let min_index: T :: Unsigned = unsafe { intrinsics:: simd_reduce_min ( masked_index) } ;
397+ let min_index: T :: Unsigned =
398+ unsafe { core:: intrinsics:: simd:: simd_reduce_min ( masked_index) } ;
386399
387400 // Safety: the return value is the unsigned version of T
388401 let min_index: T = unsafe { core:: mem:: transmute_copy ( & min_index) } ;
389402
390403 if min_index. eq ( T :: TRUE ) {
391404 None
392405 } else {
393- Some ( min_index. as_usize ( ) )
406+ Some ( min_index. to_usize ( ) )
394407 }
395408 }
396409}
0 commit comments