@@ -76,6 +76,11 @@ use crate::simd::{
76
76
/// [`read`]: pointer::read
77
77
/// [`write`]: pointer::write
78
78
/// [as_simd]: slice::as_simd
79
+ //
80
+ // NOTE: Accessing the inner array directly in any way (e.g. by using the `.0` field syntax) or
81
+ // directly constructing an instance of the type (i.e. `let vector = Simd(array)`) should be
82
+ // avoided, as it will likely become illegal on `#[repr(simd)]` structs in the future. It also
83
+ // causes rustc to emit illegal LLVM IR in some cases.
79
84
#[ repr( simd) ]
80
85
pub struct Simd < T , const LANES : usize > ( [ T ; LANES ] )
81
86
where
@@ -138,6 +143,9 @@ where
138
143
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
139
144
// is always valid and `Simd<T, LANES>` never has a lower alignment
140
145
// than `[T; LANES]`.
146
+ //
147
+ // NOTE: This deliberately doesn't just use `&self.0`, see the comment
148
+ // on the struct definition for details.
141
149
unsafe { & * ( self as * const Self as * const [ T ; LANES ] ) }
142
150
}
143
151
@@ -146,20 +154,29 @@ where
146
154
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
147
155
// is always valid and `Simd<T, LANES>` never has a lower alignment
148
156
// than `[T; LANES]`.
157
+ //
158
+ // NOTE: This deliberately doesn't just use `&mut self.0`, see the comment
159
+ // on the struct definition for details.
149
160
unsafe { & mut * ( self as * mut Self as * mut [ T ; LANES ] ) }
150
161
}
151
162
152
163
/// Converts an array to a SIMD vector.
153
164
pub const fn from_array ( array : [ T ; LANES ] ) -> Self {
154
165
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
155
166
// is always valid.
167
+ //
168
+ // NOTE: This deliberately doesn't just use `Self(array)`, see the comment
169
+ // on the struct definition for details.
156
170
unsafe { core:: mem:: transmute_copy ( & array) }
157
171
}
158
172
159
173
/// Converts a SIMD vector to an array.
160
174
pub const fn to_array ( self ) -> [ T ; LANES ] {
161
175
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
162
176
// is always valid.
177
+ //
178
+ // NOTE: This deliberately doesn't just use `self.0`, see the comment
179
+ // on the struct definition for details.
163
180
unsafe { core:: mem:: transmute_copy ( & self ) }
164
181
}
165
182
0 commit comments