|
| 1 | +use super::sealed::Sealed; |
| 2 | +use crate::simd::{intrinsics, LaneCount, Mask, Simd, SimdPartialEq, SupportedLaneCount}; |
| 3 | + |
| 4 | +/// Operations on SIMD vectors of constant pointers. |
| 5 | +pub trait SimdConstPtr: Copy + Sealed { |
| 6 | + /// Vector of `usize` with the same number of lanes. |
| 7 | + type Usize; |
| 8 | + |
| 9 | + /// Vector of `isize` with the same number of lanes. |
| 10 | + type Isize; |
| 11 | + |
| 12 | + /// Vector of mutable pointers to the same type. |
| 13 | + type MutPtr; |
| 14 | + |
| 15 | + /// Mask type used for manipulating this SIMD vector type. |
| 16 | + type Mask; |
| 17 | + |
| 18 | + /// Returns `true` for each lane that is null. |
| 19 | + fn is_null(self) -> Self::Mask; |
| 20 | + |
| 21 | + /// Changes constness without changing the type. |
| 22 | + fn as_mut(self) -> Self::MutPtr; |
| 23 | + |
| 24 | + /// Gets the "address" portion of the pointer. |
| 25 | + /// |
| 26 | + /// This method discards pointer semantic metadata, so the result cannot be |
| 27 | + /// directly cast into a valid pointer. |
| 28 | + /// |
| 29 | + /// This method semantically discards *provenance* and |
| 30 | + /// *address-space* information. To properly restore that information, use [`Self::with_addr`]. |
| 31 | + /// |
| 32 | + /// Equivalent to calling [`pointer::addr`] on each lane. |
| 33 | + fn addr(self) -> Self::Usize; |
| 34 | + |
| 35 | + /// Creates a new pointer with the given address. |
| 36 | + /// |
| 37 | + /// This performs the same operation as a cast, but copies the *address-space* and |
| 38 | + /// *provenance* of `self` to the new pointer. |
| 39 | + /// |
| 40 | + /// Equivalent to calling [`pointer::with_addr`] on each lane. |
| 41 | + fn with_addr(self, addr: Self::Usize) -> Self; |
| 42 | + |
| 43 | + /// Gets the "address" portion of the pointer, and "exposes" the provenance part for future use |
| 44 | + /// in [`Self::from_exposed_addr`]. |
| 45 | + fn expose_addr(self) -> Self::Usize; |
| 46 | + |
| 47 | + /// Convert an address back to a pointer, picking up a previously "exposed" provenance. |
| 48 | + /// |
| 49 | + /// Equivalent to calling [`core::ptr::from_exposed_addr`] on each lane. |
| 50 | + fn from_exposed_addr(addr: Self::Usize) -> Self; |
| 51 | + |
| 52 | + /// Calculates the offset from a pointer using wrapping arithmetic. |
| 53 | + /// |
| 54 | + /// Equivalent to calling [`pointer::wrapping_offset`] on each lane. |
| 55 | + fn wrapping_offset(self, offset: Self::Isize) -> Self; |
| 56 | + |
| 57 | + /// Calculates the offset from a pointer using wrapping arithmetic. |
| 58 | + /// |
| 59 | + /// Equivalent to calling [`pointer::wrapping_add`] on each lane. |
| 60 | + fn wrapping_add(self, count: Self::Usize) -> Self; |
| 61 | + |
| 62 | + /// Calculates the offset from a pointer using wrapping arithmetic. |
| 63 | + /// |
| 64 | + /// Equivalent to calling [`pointer::wrapping_sub`] on each lane. |
| 65 | + fn wrapping_sub(self, count: Self::Usize) -> Self; |
| 66 | +} |
| 67 | + |
| 68 | +impl<T, const LANES: usize> Sealed for Simd<*const T, LANES> where |
| 69 | + LaneCount<LANES>: SupportedLaneCount |
| 70 | +{ |
| 71 | +} |
| 72 | + |
| 73 | +impl<T, const LANES: usize> SimdConstPtr for Simd<*const T, LANES> |
| 74 | +where |
| 75 | + LaneCount<LANES>: SupportedLaneCount, |
| 76 | +{ |
| 77 | + type Usize = Simd<usize, LANES>; |
| 78 | + type Isize = Simd<isize, LANES>; |
| 79 | + type MutPtr = Simd<*mut T, LANES>; |
| 80 | + type Mask = Mask<isize, LANES>; |
| 81 | + |
| 82 | + #[inline] |
| 83 | + fn is_null(self) -> Self::Mask { |
| 84 | + Simd::splat(core::ptr::null()).simd_eq(self) |
| 85 | + } |
| 86 | + |
| 87 | + #[inline] |
| 88 | + fn as_mut(self) -> Self::MutPtr { |
| 89 | + self.cast_ptr() |
| 90 | + } |
| 91 | + |
| 92 | + #[inline] |
| 93 | + fn addr(self) -> Self::Usize { |
| 94 | + // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic. |
| 95 | + // SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the |
| 96 | + // provenance). |
| 97 | + unsafe { core::mem::transmute_copy(&self) } |
| 98 | + } |
| 99 | + |
| 100 | + #[inline] |
| 101 | + fn with_addr(self, addr: Self::Usize) -> Self { |
| 102 | + // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic. |
| 103 | + // |
| 104 | + // In the mean-time, this operation is defined to be "as if" it was |
| 105 | + // a wrapping_offset, so we can emulate it as such. This should properly |
| 106 | + // restore pointer provenance even under today's compiler. |
| 107 | + self.cast_ptr::<*const u8>() |
| 108 | + .wrapping_offset(addr.cast::<isize>() - self.addr().cast::<isize>()) |
| 109 | + .cast_ptr() |
| 110 | + } |
| 111 | + |
| 112 | + #[inline] |
| 113 | + fn expose_addr(self) -> Self::Usize { |
| 114 | + // Safety: `self` is a pointer vector |
| 115 | + unsafe { intrinsics::simd_expose_addr(self) } |
| 116 | + } |
| 117 | + |
| 118 | + #[inline] |
| 119 | + fn from_exposed_addr(addr: Self::Usize) -> Self { |
| 120 | + // Safety: `self` is a pointer vector |
| 121 | + unsafe { intrinsics::simd_from_exposed_addr(addr) } |
| 122 | + } |
| 123 | + |
| 124 | + #[inline] |
| 125 | + fn wrapping_offset(self, count: Self::Isize) -> Self { |
| 126 | + // Safety: simd_arith_offset takes a vector of pointers and a vector of offsets |
| 127 | + unsafe { intrinsics::simd_arith_offset(self, count) } |
| 128 | + } |
| 129 | + |
| 130 | + #[inline] |
| 131 | + fn wrapping_add(self, count: Self::Usize) -> Self { |
| 132 | + self.wrapping_offset(count.cast()) |
| 133 | + } |
| 134 | + |
| 135 | + #[inline] |
| 136 | + fn wrapping_sub(self, count: Self::Usize) -> Self { |
| 137 | + self.wrapping_offset(-count.cast::<isize>()) |
| 138 | + } |
| 139 | +} |
0 commit comments