From 9718639d61b32d4efd2fac330ab1058732b3b758 Mon Sep 17 00:00:00 2001 From: Andrew Straw Date: Mon, 11 Apr 2022 21:17:44 +0200 Subject: [PATCH] rust-lang/portable-simd#276: Mention slice methods as_simd() and as_simd_mut() This links to a practical suggestion for how to solve the issues brought up in this section. --- beginners-guide.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/beginners-guide.md b/beginners-guide.md index 75158e5aa85..17ade06ae80 100644 --- a/beginners-guide.md +++ b/beginners-guide.md @@ -82,5 +82,10 @@ Fortunately, most SIMD types have a fairly predictable size. `i32x4` is bit-equi 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`]. +When working with slices, data correctly aligned for SIMD can be acquired using the [`as_simd`] and [`as_simd_mut`] methods of the slice primitive. + [`mem::transmute`]: https://doc.rust-lang.org/core/mem/fn.transmute.html [`mem::align_of`]: https://doc.rust-lang.org/core/mem/fn.align_of.html +[`as_simd`]: https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.as_simd +[`as_simd_mut`]: https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.as_simd_mut +