Skip to content

Commit e886dc5

Browse files
committed
portable-simd: use simd_arith_offset to avoid ptr-int transmutation
1 parent 7f945b2 commit e886dc5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

library/portable-simd/crates/core_simd/src/intrinsics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ extern "platform-intrinsic" {
6161
/// xor
6262
pub(crate) fn simd_xor<T>(x: T, y: T) -> T;
6363

64+
/// getelementptr (without inbounds)
65+
#[cfg(not(bootstrap))]
66+
pub(crate) fn simd_arith_offset<T, U>(ptrs: T, offsets: U) -> T;
67+
6468
/// fptoui/fptosi/uitofp/sitofp
6569
/// casting floats to integers is truncating, so it is safe to convert values like e.g. 1.5
6670
/// but the truncated value must fit in the target type or the result is poison.

library/portable-simd/crates/core_simd/src/vector/ptr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Private implementation details of public gather/scatter APIs.
2+
#[cfg(not(bootstrap))]
3+
use crate::simd::intrinsics;
24
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
5+
#[cfg(bootstrap)]
36
use core::mem;
47

58
/// A vector of *const T.
@@ -21,12 +24,16 @@ where
2124
#[inline]
2225
#[must_use]
2326
pub fn wrapping_add(self, addend: Simd<usize, LANES>) -> Self {
27+
#[cfg(bootstrap)]
2428
// Safety: converting pointers to usize and vice-versa is safe
2529
// (even if using that pointer is not)
2630
unsafe {
2731
let x: Simd<usize, LANES> = mem::transmute_copy(&self);
2832
mem::transmute_copy(&{ x + (addend * Simd::splat(mem::size_of::<T>())) })
2933
}
34+
#[cfg(not(bootstrap))]
35+
// Safety: this intrinsic doesn't have a precondition
36+
unsafe { intrinsics::simd_arith_offset(self, addend) }
3037
}
3138
}
3239

@@ -49,11 +56,15 @@ where
4956
#[inline]
5057
#[must_use]
5158
pub fn wrapping_add(self, addend: Simd<usize, LANES>) -> Self {
59+
#[cfg(bootstrap)]
5260
// Safety: converting pointers to usize and vice-versa is safe
5361
// (even if using that pointer is not)
5462
unsafe {
5563
let x: Simd<usize, LANES> = mem::transmute_copy(&self);
5664
mem::transmute_copy(&{ x + (addend * Simd::splat(mem::size_of::<T>())) })
5765
}
66+
#[cfg(not(bootstrap))]
67+
// Safety: this intrinsic doesn't have a precondition
68+
unsafe { intrinsics::simd_arith_offset(self, addend) }
5869
}
5970
}

0 commit comments

Comments
 (0)