Skip to content

Commit 3aec4a2

Browse files
Merge pull request rust-lang#43 from rust-lang/docs/layout
Document size/align of SIMD types
2 parents 4baa8c2 + a5bdb8b commit 3aec4a2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

beginners-guide.md

+11
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ This is no bug in Rust, or soundness hole in the type system. You just plain can
6565
This is why the various Rust targets *don't* enable many CPU feature flags by default: requiring a more advanced CPU makes the final binary *less* portable.
6666

6767
So please select an appropriate CPU feature level when building your programs.
68+
69+
## Size, Alignment, and Unsafe Code
70+
71+
Most of the portable SIMD API is designed to allow the user to gloss over the details of different architectures and avoid using unsafe code. However, there are plenty of reasons to want to use unsafe code with these SIMD types, such as using an intrinsic function from `core::arch` to further accelerate particularly specialized SIMD operations on a given platform, while still using the portable API elsewhere. For these cases, there are some rules to keep in mind.
72+
73+
Fortunately, most SIMD types have a fairly predictable size. `i32x4` is bit-equivalent to `[i32; 4]` and so can be bitcast to it, e.g. using [`mem::transmute`], though the API usually offers a safe cast you can use instead.
74+
75+
However, this is not the same as alignment. Computer architectures generally prefer aligned accesses, especially when moving data between memory and vector registers, and while some support specialized operations that can bend the rules to help with this, unaligned access is still typically slow, or even undefined behavior. In addition, different architectures can require different alignments when interacting with their native SIMD types. For this reason, any `#[repr(simd)]` type has a non-portable alignment. If it is necessary to directly interact with the alignment of these types, it should be via [`mem::align_of`].
76+
77+
[`mem::transmute`]: https://doc.rust-lang.org/core/mem/fn.transmute.html
78+
[`mem::align_of`]: https://doc.rust-lang.org/core/mem/fn.align_of.html

0 commit comments

Comments
 (0)