@@ -2,6 +2,7 @@ use crate::simd::{
22 intrinsics, LaneCount , Mask , MaskElement , SimdCast , SimdCastPtr , SimdConstPtr , SimdMutPtr ,
33 SimdPartialOrd , SupportedLaneCount , Swizzle ,
44} ;
5+ use core:: convert:: { TryFrom , TryInto } ;
56
67/// A SIMD vector with the shape of `[T; N]` but the operations of `T`.
78///
@@ -109,7 +110,7 @@ where
109110 T : SimdElement ,
110111{
111112 /// Number of elements in this vector.
112- pub const N : usize = N ;
113+ pub const LANES : usize = N ;
113114
114115 /// Returns the number of elements in this SIMD vector.
115116 ///
@@ -123,7 +124,7 @@ where
123124 /// ```
124125 #[ inline]
125126 pub const fn lanes ( & self ) -> usize {
126- Self :: N
127+ Self :: LANES
127128 }
128129
129130 /// Constructs a new SIMD vector with all elements set to the given value.
@@ -159,9 +160,9 @@ where
159160 /// ```
160161 #[ inline]
161162 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 .
165166 //
166167 // NOTE: This deliberately doesn't just use `&self.0`, see the comment
167168 // on the struct definition for details.
@@ -171,9 +172,9 @@ where
171172 /// Returns a mutable array reference containing the entire SIMD vector.
172173 #[ inline]
173174 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 .
177178 //
178179 // NOTE: This deliberately doesn't just use `&mut self.0`, see the comment
179180 // on the struct definition for details.
@@ -269,14 +270,12 @@ where
269270 #[ track_caller]
270271 pub const fn from_slice ( slice : & [ T ] ) -> Self {
271272 assert ! (
272- slice. len( ) >= Self :: N ,
273+ slice. len( ) >= Self :: LANES ,
273274 "slice length must be at least the number of elements"
274275 ) ;
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 ( ) ) }
280279 }
281280
282281 /// Writes a SIMD vector to the first `N` elements of a slice.
@@ -301,14 +300,12 @@ where
301300 #[ track_caller]
302301 pub fn copy_to_slice ( self , slice : & mut [ T ] ) {
303302 assert ! (
304- slice. len( ) >= Self :: N ,
303+ slice. len( ) >= Self :: LANES ,
305304 "slice length must be at least the number of elements"
306305 ) ;
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 ( ) ) }
312309 }
313310
314311 /// Performs elementwise conversion of a SIMD vector's elements to another SIMD-valid type.
@@ -902,7 +899,7 @@ where
902899 type Error = core:: array:: TryFromSliceError ;
903900
904901 #[ inline]
905- fn try_from ( slice : & [ T ] ) -> Result < Self , Self :: Error > {
902+ fn try_from ( slice : & [ T ] ) -> Result < Self , core :: array :: TryFromSliceError > {
906903 Ok ( Self :: from_array ( slice. try_into ( ) ?) )
907904 }
908905}
@@ -915,7 +912,7 @@ where
915912 type Error = core:: array:: TryFromSliceError ;
916913
917914 #[ inline]
918- fn try_from ( slice : & mut [ T ] ) -> Result < Self , Self :: Error > {
915+ fn try_from ( slice : & mut [ T ] ) -> Result < Self , core :: array :: TryFromSliceError > {
919916 Ok ( Self :: from_array ( slice. try_into ( ) ?) )
920917 }
921918}
0 commit comments