Skip to main content

core_simd/simd/
ptr.rs

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