Skip to content

Commit 77a2abf

Browse files
committed
add vec_find_any_eq_cc and vec_find_any_ne_cc
1 parent bc9bdcc commit 77a2abf

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

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])+

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 }
@@ -1558,6 +1568,21 @@ mod sealed {
15581568
}
15591569

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

16701763
/// Vector element-wise addition.
@@ -2490,6 +2583,34 @@ where
24902583
a.vec_find_any_ne_idx(b)
24912584
}
24922585

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

0 commit comments

Comments
 (0)