@@ -2,6 +2,7 @@ use crate::simd::{
2
2
intrinsics, LaneCount , Mask , MaskElement , SimdCast , SimdCastPtr , SimdConstPtr , SimdMutPtr ,
3
3
SimdPartialOrd , SupportedLaneCount , Swizzle ,
4
4
} ;
5
+ use core:: convert:: { TryFrom , TryInto } ;
5
6
6
7
/// A SIMD vector with the shape of `[T; N]` but the operations of `T`.
7
8
///
@@ -109,7 +110,7 @@ where
109
110
T : SimdElement ,
110
111
{
111
112
/// Number of elements in this vector.
112
- pub const N : usize = N ;
113
+ pub const LANES : usize = N ;
113
114
114
115
/// Returns the number of elements in this SIMD vector.
115
116
///
@@ -123,7 +124,7 @@ where
123
124
/// ```
124
125
#[ inline]
125
126
pub const fn lanes ( & self ) -> usize {
126
- Self :: N
127
+ Self :: LANES
127
128
}
128
129
129
130
/// Constructs a new SIMD vector with all elements set to the given value.
@@ -159,9 +160,9 @@ where
159
160
/// ```
160
161
#[ inline]
161
162
pub const fn as_array ( & self ) -> & [ T ; N ] {
162
- // SAFETY: Transmuting between `Simd<T, N>` and `[T; N]`
163
- // is always valid and `Simd<T, N>` never has a lower alignment
164
- // than ` [T; N]`.
163
+ // SAFETY: `Simd<T, N>` is just an overaligned `[T; N]` with
164
+ // potential padding at the end, so pointer casting to a
165
+ // `& [T; N]` is safe .
165
166
//
166
167
// NOTE: This deliberately doesn't just use `&self.0`, see the comment
167
168
// on the struct definition for details.
@@ -171,9 +172,9 @@ where
171
172
/// Returns a mutable array reference containing the entire SIMD vector.
172
173
#[ inline]
173
174
pub fn as_mut_array ( & mut self ) -> & mut [ T ; N ] {
174
- // SAFETY: Transmuting between `Simd<T, N>` and `[T; N]`
175
- // is always valid and `Simd<T, N>` never has a lower alignment
176
- // than ` [T; N]`.
175
+ // SAFETY: `Simd<T, N>` is just an overaligned `[T; N]` with
176
+ // potential padding at the end, so pointer casting to a
177
+ // `&mut [T; N]` is safe .
177
178
//
178
179
// NOTE: This deliberately doesn't just use `&mut self.0`, see the comment
179
180
// on the struct definition for details.
@@ -269,14 +270,12 @@ where
269
270
#[ track_caller]
270
271
pub const fn from_slice ( slice : & [ T ] ) -> Self {
271
272
assert ! (
272
- slice. len( ) >= Self :: N ,
273
+ slice. len( ) >= Self :: LANES ,
273
274
"slice length must be at least the number of elements"
274
275
) ;
275
- assert ! ( core:: mem:: size_of:: <Self >( ) == Self :: N * core:: mem:: size_of:: <T >( ) ) ;
276
- // Safety:
277
- // - We've checked the length is sufficient.
278
- // - `T` and `Simd<T, N>` are Copy types.
279
- unsafe { slice. as_ptr ( ) . cast :: < Self > ( ) . read_unaligned ( ) }
276
+ // SAFETY: We just checked that the slice contains
277
+ // at least `N` elements.
278
+ unsafe { Self :: load ( slice. as_ptr ( ) . cast ( ) ) }
280
279
}
281
280
282
281
/// Writes a SIMD vector to the first `N` elements of a slice.
@@ -301,14 +300,12 @@ where
301
300
#[ track_caller]
302
301
pub fn copy_to_slice ( self , slice : & mut [ T ] ) {
303
302
assert ! (
304
- slice. len( ) >= Self :: N ,
303
+ slice. len( ) >= Self :: LANES ,
305
304
"slice length must be at least the number of elements"
306
305
) ;
307
- assert ! ( core:: mem:: size_of:: <Self >( ) == Self :: N * core:: mem:: size_of:: <T >( ) ) ;
308
- // Safety:
309
- // - We've checked the length is sufficient
310
- // - `T` and `Simd<T, N>` are Copy types.
311
- unsafe { slice. as_mut_ptr ( ) . cast :: < Self > ( ) . write_unaligned ( self ) }
306
+ // SAFETY: We just checked that the slice contains
307
+ // at least `N` elements.
308
+ unsafe { self . store ( slice. as_mut_ptr ( ) . cast ( ) ) }
312
309
}
313
310
314
311
/// Performs elementwise conversion of a SIMD vector's elements to another SIMD-valid type.
@@ -902,7 +899,7 @@ where
902
899
type Error = core:: array:: TryFromSliceError ;
903
900
904
901
#[ inline]
905
- fn try_from ( slice : & [ T ] ) -> Result < Self , Self :: Error > {
902
+ fn try_from ( slice : & [ T ] ) -> Result < Self , core :: array :: TryFromSliceError > {
906
903
Ok ( Self :: from_array ( slice. try_into ( ) ?) )
907
904
}
908
905
}
@@ -915,7 +912,7 @@ where
915
912
type Error = core:: array:: TryFromSliceError ;
916
913
917
914
#[ inline]
918
- fn try_from ( slice : & mut [ T ] ) -> Result < Self , Self :: Error > {
915
+ fn try_from ( slice : & mut [ T ] ) -> Result < Self , core :: array :: TryFromSliceError > {
919
916
Ok ( Self :: from_array ( slice. try_into ( ) ?) )
920
917
}
921
918
}
0 commit comments