Skip to content

Commit 143539e

Browse files
folkertdevAmanieu
authored andcommitted
add vec_find_any_eq_cc and vec_find_any_ne_cc
1 parent ed8a610 commit 143539e

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

Diff for: crates/core_arch/src/macros.rs

+24
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ macro_rules! types {
102102
// a simd type with exactly one element.
103103
unsafe { simd_shuffle!(one, one, [0; $len]) }
104104
}
105+
106+
/// Returns an array reference containing the entire SIMD vector.
107+
$v const fn as_array(&self) -> &[$elem_type; $len] {
108+
// SAFETY: this type is just an overaligned `[T; N]` with
109+
// potential padding at the end, so pointer casting to a
110+
// `&[T; N]` is safe.
111+
//
112+
// NOTE: This deliberately doesn't just use `&self.0` because it may soon be banned
113+
// see https://github.com/rust-lang/compiler-team/issues/838
114+
unsafe { &*(self as *const Self as *const [$elem_type; $len]) }
115+
116+
}
117+
118+
/// Returns a mutable array reference containing the entire SIMD vector.
119+
#[inline]
120+
$v fn as_mut_array(&mut self) -> &mut [$elem_type; $len] {
121+
// SAFETY: this type is just an overaligned `[T; N]` with
122+
// potential padding at the end, so pointer casting to a
123+
// `&mut [T; N]` is safe.
124+
//
125+
// NOTE: This deliberately doesn't just use `&mut self.0` because it may soon be banned
126+
// see https://github.com/rust-lang/compiler-team/issues/838
127+
unsafe { &mut *(self as *mut Self as *mut [$elem_type; $len]) }
128+
}
105129
}
106130

107131
$(#[$stability])+

Diff for: crates/core_arch/src/s390x/vector.rs

+157
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ types! {
5151
pub struct vector_double(2 x f64);
5252
}
5353

54+
#[repr(packed)]
55+
struct PackedTuple<T, U> {
56+
x: T,
57+
y: U,
58+
}
59+
5460
#[allow(improper_ctypes)]
5561
#[rustfmt::skip]
5662
unsafe extern "unadjusted" {
@@ -124,6 +130,10 @@ unsafe extern "unadjusted" {
124130
#[link_name = "llvm.s390.vfaeb"] fn vfaeb(a: vector_signed_char, b: vector_signed_char, c: i32) -> vector_signed_char;
125131
#[link_name = "llvm.s390.vfaeh"] fn vfaeh(a: vector_signed_short, b: vector_signed_short, c: i32) -> vector_signed_short;
126132
#[link_name = "llvm.s390.vfaef"] fn vfaef(a: vector_signed_int, b: vector_signed_int, c: i32) -> vector_signed_int;
133+
134+
#[link_name = "llvm.s390.vfaebs"] fn vfaebs(a: vector_signed_char, b: vector_signed_char, c: i32) -> PackedTuple<vector_signed_char, i32>;
135+
#[link_name = "llvm.s390.vfaehs"] fn vfaehs(a: vector_signed_short, b: vector_signed_short, c: i32) -> PackedTuple<vector_signed_short, i32>;
136+
#[link_name = "llvm.s390.vfaefs"] fn vfaefs(a: vector_signed_int, b: vector_signed_int, c: i32) -> PackedTuple<vector_signed_int, i32>;
127137
}
128138

129139
impl_from! { i8x16, u8x16, i16x8, u16x8, i32x4, u32x4, i64x2, u64x2, f32x4, f64x2 }
@@ -1554,6 +1564,21 @@ mod sealed {
15541564
}
15551565

15561566
macro_rules! impl_vfae {
1567+
([cc $Trait:ident $m:ident] $imm:literal $($fun:ident $ty:ident)*) => {
1568+
$(
1569+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
1570+
impl $Trait<Self> for $ty {
1571+
type Result = t_b!($ty);
1572+
#[inline]
1573+
#[target_feature(enable = "vector")]
1574+
unsafe fn $m(self, b: Self, c: *mut i32) -> Self::Result {
1575+
let PackedTuple { x, y } = $fun::<$imm>(transmute(self), transmute(b));
1576+
c.write(y);
1577+
transmute(x)
1578+
}
1579+
}
1580+
)*
1581+
};
15571582
([idx $Trait:ident $m:ident] $imm:literal $($fun:ident $ty:ident $r:ident)*) => {
15581583
$(
15591584
#[unstable(feature = "stdarch_s390x", issue = "135681")]
@@ -1661,6 +1686,74 @@ mod sealed {
16611686
vfaef vector_unsigned_int vector_unsigned_int
16621687
vfaef vector_bool_int vector_unsigned_int
16631688
}
1689+
1690+
#[inline]
1691+
#[target_feature(enable = "vector")]
1692+
#[cfg_attr(test, assert_instr(vfaebs, IMM = 0))]
1693+
unsafe fn vfaebs<const IMM: i32>(
1694+
a: vector_signed_char,
1695+
b: vector_signed_char,
1696+
) -> PackedTuple<vector_signed_char, i32> {
1697+
super::vfaebs(a, b, IMM)
1698+
}
1699+
#[inline]
1700+
#[target_feature(enable = "vector")]
1701+
#[cfg_attr(test, assert_instr(vfaehs, IMM = 0))]
1702+
unsafe fn vfaehs<const IMM: i32>(
1703+
a: vector_signed_short,
1704+
b: vector_signed_short,
1705+
) -> PackedTuple<vector_signed_short, i32> {
1706+
super::vfaehs(a, b, IMM)
1707+
}
1708+
#[inline]
1709+
#[target_feature(enable = "vector")]
1710+
#[cfg_attr(test, assert_instr(vfaefs, IMM = 0))]
1711+
unsafe fn vfaefs<const IMM: i32>(
1712+
a: vector_signed_int,
1713+
b: vector_signed_int,
1714+
) -> PackedTuple<vector_signed_int, i32> {
1715+
super::vfaefs(a, b, IMM)
1716+
}
1717+
1718+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
1719+
pub trait VectorFindAnyEqCC<Other> {
1720+
type Result;
1721+
unsafe fn vec_find_any_eq_cc(self, other: Other, c: *mut i32) -> Self::Result;
1722+
}
1723+
1724+
impl_vfae! { [cc VectorFindAnyEqCC vec_find_any_eq_cc] 4
1725+
vfaebs vector_signed_char
1726+
vfaebs vector_unsigned_char
1727+
vfaebs vector_bool_char
1728+
1729+
vfaehs vector_signed_short
1730+
vfaehs vector_unsigned_short
1731+
vfaehs vector_bool_short
1732+
1733+
vfaefs vector_signed_int
1734+
vfaefs vector_unsigned_int
1735+
vfaefs vector_bool_int
1736+
}
1737+
1738+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
1739+
pub trait VectorFindAnyNeCC<Other> {
1740+
type Result;
1741+
unsafe fn vec_find_any_ne_cc(self, other: Other, c: *mut i32) -> Self::Result;
1742+
}
1743+
1744+
impl_vfae! { [cc VectorFindAnyNeCC vec_find_any_ne_cc] 12
1745+
vfaebs vector_signed_char
1746+
vfaebs vector_unsigned_char
1747+
vfaebs vector_bool_char
1748+
1749+
vfaehs vector_signed_short
1750+
vfaehs vector_unsigned_short
1751+
vfaehs vector_bool_short
1752+
1753+
vfaefs vector_signed_int
1754+
vfaefs vector_unsigned_int
1755+
vfaefs vector_bool_int
1756+
}
16641757
}
16651758

16661759
/// Vector element-wise addition.
@@ -2486,6 +2579,34 @@ where
24862579
a.vec_find_any_ne_idx(b)
24872580
}
24882581

2582+
#[inline]
2583+
#[target_feature(enable = "vector")]
2584+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
2585+
pub unsafe fn vec_find_any_eq_cc<T, U>(
2586+
a: T,
2587+
b: U,
2588+
c: *mut i32,
2589+
) -> <T as sealed::VectorFindAnyEqCC<U>>::Result
2590+
where
2591+
T: sealed::VectorFindAnyEqCC<U>,
2592+
{
2593+
a.vec_find_any_eq_cc(b, c)
2594+
}
2595+
2596+
#[inline]
2597+
#[target_feature(enable = "vector")]
2598+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
2599+
pub unsafe fn vec_find_any_ne_cc<T, U>(
2600+
a: T,
2601+
b: U,
2602+
c: *mut i32,
2603+
) -> <T as sealed::VectorFindAnyNeCC<U>>::Result
2604+
where
2605+
T: sealed::VectorFindAnyNeCC<U>,
2606+
{
2607+
a.vec_find_any_ne_cc(b, c)
2608+
}
2609+
24892610
#[cfg(test)]
24902611
mod tests {
24912612
use super::*;
@@ -3040,4 +3161,40 @@ mod tests {
30403161
[1, 2, 3, 4],
30413162
[0, 16, 0, 0]
30423163
}
3164+
3165+
#[simd_test(enable = "vector")]
3166+
fn test_vec_find_any_eq_cc() {
3167+
let mut c = 0i32;
3168+
3169+
let a = vector_unsigned_int([1, 2, 3, 4]);
3170+
let b = vector_unsigned_int([5, 3, 7, 8]);
3171+
3172+
let d = unsafe { vec_find_any_eq_cc(a, b, &mut c) };
3173+
assert_eq!(c, 1);
3174+
assert_eq!(d.as_array(), &[0, 0, -1, 0]);
3175+
3176+
let a = vector_unsigned_int([1, 2, 3, 4]);
3177+
let b = vector_unsigned_int([5, 6, 7, 8]);
3178+
let d = unsafe { vec_find_any_eq_cc(a, b, &mut c) };
3179+
assert_eq!(c, 3);
3180+
assert_eq!(d.as_array(), &[0, 0, 0, 0]);
3181+
}
3182+
3183+
#[simd_test(enable = "vector")]
3184+
fn test_vec_find_any_ne_cc() {
3185+
let mut c = 0i32;
3186+
3187+
let a = vector_unsigned_int([1, 2, 3, 4]);
3188+
let b = vector_unsigned_int([5, 3, 7, 8]);
3189+
3190+
let d = unsafe { vec_find_any_ne_cc(a, b, &mut c) };
3191+
assert_eq!(c, 1);
3192+
assert_eq!(d.as_array(), &[-1, -1, 0, -1]);
3193+
3194+
let a = vector_unsigned_int([1, 2, 3, 4]);
3195+
let b = vector_unsigned_int([1, 2, 3, 4]);
3196+
let d = unsafe { vec_find_any_ne_cc(a, b, &mut c) };
3197+
assert_eq!(c, 3);
3198+
assert_eq!(d.as_array(), &[0, 0, 0, 0]);
3199+
}
30433200
}

0 commit comments

Comments
 (0)