Skip to main content

core_simd/simd/
ptr.rs

1//! Traits for vectors of pointers.
2
3mod const_ptr;
4mod mut_ptr;
5
6pub use const_ptr::*;
7pub use mut_ptr::*;
8
9use crate::simd::Simd;
10
11/// Creates pointers with the given addresses and no provenance.
12///
13/// Equivalent to calling [`core::ptr::without_provenance`] on each element.
14#[inline]
15pub fn without_provenance<T, const N: usize>(addr: Simd<usize, N>) -> Simd<*const T, N> {
16    // An int-to-pointer transmute currently has exactly the intended semantics: it creates a
17    // pointer without provenance. Note that this is *not* a stable guarantee about transmute
18    // semantics, it relies on sysroot crates having special status.
19    // SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
20    // pointer).
21    unsafe { core::mem::transmute_copy(&addr) }
22}
23
24/// Creates mutable pointers with the given addresses and no provenance.
25///
26/// Equivalent to calling [`core::ptr::without_provenance_mut`] on each element.
27#[inline]
28pub fn without_provenance_mut<T, const N: usize>(addr: Simd<usize, N>) -> Simd<*mut T, N> {
29    // An int-to-pointer transmute currently has exactly the intended semantics: it creates a
30    // pointer without provenance. Note that this is *not* a stable guarantee about transmute
31    // semantics, it relies on sysroot crates having special status.
32    // SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
33    // pointer).
34    unsafe { core::mem::transmute_copy(&addr) }
35}
36
37/// Converts addresses back to pointers, picking up some previously "exposed" provenance.
38///
39/// Equivalent to calling [`core::ptr::with_exposed_provenance`] on each element.
40#[inline]
41pub fn with_exposed_provenance<T, const N: usize>(addr: Simd<usize, N>) -> Simd<*const T, N> {
42    // SAFETY: addr is a vector of usize
43    unsafe { core::intrinsics::simd::simd_with_exposed_provenance(addr) }
44}
45
46/// Converts addresses back to mutable pointers, picking up some previously "exposed" provenance.
47///
48/// Equivalent to calling [`core::ptr::with_exposed_provenance_mut`] on each element.
49#[inline]
50pub fn with_exposed_provenance_mut<T, const N: usize>(addr: Simd<usize, N>) -> Simd<*mut T, N> {
51    // SAFETY: addr is a vector of usize
52    unsafe { core::intrinsics::simd::simd_with_exposed_provenance(addr) }
53}