Skip to content

Commit 5adf051

Browse files
authored
Merge pull request #1937 from sayantn/intrinsic-fixes
use simd intrinsics for `vec_max` and `vec_min`
2 parents f8f09e2 + 339180f commit 5adf051

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

crates/core_arch/src/s390x/vector.rs

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,6 @@ struct PackedTuple<T, U> {
6060
#[allow(improper_ctypes)]
6161
#[rustfmt::skip]
6262
unsafe extern "unadjusted" {
63-
#[link_name = "llvm.smax.v16i8"] fn vmxb(a: vector_signed_char, b: vector_signed_char) -> vector_signed_char;
64-
#[link_name = "llvm.smax.v8i16"] fn vmxh(a: vector_signed_short, b: vector_signed_short) -> vector_signed_short;
65-
#[link_name = "llvm.smax.v4i32"] fn vmxf(a: vector_signed_int, b: vector_signed_int) -> vector_signed_int;
66-
#[link_name = "llvm.smax.v2i64"] fn vmxg(a: vector_signed_long_long, b: vector_signed_long_long) -> vector_signed_long_long;
67-
68-
#[link_name = "llvm.umax.v16i8"] fn vmxlb(a: vector_unsigned_char, b: vector_unsigned_char) -> vector_unsigned_char;
69-
#[link_name = "llvm.umax.v8i16"] fn vmxlh(a: vector_unsigned_short, b: vector_unsigned_short) -> vector_unsigned_short;
70-
#[link_name = "llvm.umax.v4i32"] fn vmxlf(a: vector_unsigned_int, b: vector_unsigned_int) -> vector_unsigned_int;
71-
#[link_name = "llvm.umax.v2i64"] fn vmxlg(a: vector_unsigned_long_long, b: vector_unsigned_long_long) -> vector_unsigned_long_long;
72-
73-
#[link_name = "llvm.smin.v16i8"] fn vmnb(a: vector_signed_char, b: vector_signed_char) -> vector_signed_char;
74-
#[link_name = "llvm.smin.v8i16"] fn vmnh(a: vector_signed_short, b: vector_signed_short) -> vector_signed_short;
75-
#[link_name = "llvm.smin.v4i32"] fn vmnf(a: vector_signed_int, b: vector_signed_int) -> vector_signed_int;
76-
#[link_name = "llvm.smin.v2i64"] fn vmng(a: vector_signed_long_long, b: vector_signed_long_long) -> vector_signed_long_long;
77-
78-
#[link_name = "llvm.umin.v16i8"] fn vmnlb(a: vector_unsigned_char, b: vector_unsigned_char) -> vector_unsigned_char;
79-
#[link_name = "llvm.umin.v8i16"] fn vmnlh(a: vector_unsigned_short, b: vector_unsigned_short) -> vector_unsigned_short;
80-
#[link_name = "llvm.umin.v4i32"] fn vmnlf(a: vector_unsigned_int, b: vector_unsigned_int) -> vector_unsigned_int;
81-
#[link_name = "llvm.umin.v2i64"] fn vmnlg(a: vector_unsigned_long_long, b: vector_unsigned_long_long) -> vector_unsigned_long_long;
82-
8363
#[link_name = "llvm.nearbyint.v4f32"] fn nearbyint_v4f32(a: vector_float) -> vector_float;
8464
#[link_name = "llvm.nearbyint.v2f64"] fn nearbyint_v2f64(a: vector_double) -> vector_double;
8565

@@ -683,17 +663,40 @@ mod sealed {
683663
unsafe fn vec_max(self, b: Other) -> Self::Result;
684664
}
685665

686-
test_impl! { vec_vmxsb (a: vector_signed_char, b: vector_signed_char) -> vector_signed_char [vmxb, vmxb] }
687-
test_impl! { vec_vmxsh (a: vector_signed_short, b: vector_signed_short) -> vector_signed_short [vmxh, vmxh] }
688-
test_impl! { vec_vmxsf (a: vector_signed_int, b: vector_signed_int) -> vector_signed_int [vmxf, vmxf] }
689-
test_impl! { vec_vmxsg (a: vector_signed_long_long, b: vector_signed_long_long) -> vector_signed_long_long [vmxg, vmxg] }
666+
macro_rules! impl_max {
667+
($name:ident, $a:ty, $instr:ident) => {
668+
#[inline]
669+
#[target_feature(enable = "vector")]
670+
#[cfg_attr(test, assert_instr($instr))]
671+
pub unsafe fn $name(a: $a, b: $a) -> $a {
672+
simd_select(simd_ge::<_, $a>(a, b), a, b)
673+
}
674+
675+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
676+
impl VectorMax<Self> for $a {
677+
type Result = Self;
678+
679+
#[inline]
680+
#[target_feature(enable = "vector")]
681+
unsafe fn vec_max(self, other: Self) -> Self {
682+
$name(self, other)
683+
}
684+
}
685+
};
686+
}
690687

691-
test_impl! { vec_vmxslb (a: vector_unsigned_char, b: vector_unsigned_char) -> vector_unsigned_char [vmxlb, vmxlb] }
692-
test_impl! { vec_vmxslh (a: vector_unsigned_short, b: vector_unsigned_short) -> vector_unsigned_short [vmxlh, vmxlh] }
693-
test_impl! { vec_vmxslf (a: vector_unsigned_int, b: vector_unsigned_int) -> vector_unsigned_int [vmxlf, vmxlf] }
694-
test_impl! { vec_vmxslg (a: vector_unsigned_long_long, b: vector_unsigned_long_long) -> vector_unsigned_long_long [vmxlg, vmxlg] }
688+
mod impl_max {
689+
use super::*;
695690

696-
impl_vec_trait! { [VectorMax vec_max] ~(vmxlb, vmxb, vmxlh, vmxh, vmxlf, vmxf, vmxlg, vmxg) }
691+
impl_max!(vec_vmxsc, vector_signed_char, vmxb);
692+
impl_max!(vec_vmxslc, vector_unsigned_char, vmxlb);
693+
impl_max!(vec_vmxsh, vector_signed_short, vmxh);
694+
impl_max!(vec_vmxslh, vector_unsigned_short, vmxlh);
695+
impl_max!(vec_vmxsf, vector_signed_int, vmxf);
696+
impl_max!(vec_vmxslf, vector_unsigned_int, vmxlf);
697+
impl_max!(vec_vmxsg, vector_signed_long_long, vmxg);
698+
impl_max!(vec_vmxslg, vector_unsigned_long_long, vmxlg);
699+
}
697700

698701
test_impl! { vec_vfmaxsb (a: vector_float, b: vector_float) -> vector_float [simd_fmax, "vector-enhancements-1" vfmaxsb ] }
699702
test_impl! { vec_vfmaxdb (a: vector_double, b: vector_double) -> vector_double [simd_fmax, "vector-enhancements-1" vfmaxdb] }
@@ -707,17 +710,40 @@ mod sealed {
707710
unsafe fn vec_min(self, b: Other) -> Self::Result;
708711
}
709712

710-
test_impl! { vec_vmnsb (a: vector_signed_char, b: vector_signed_char) -> vector_signed_char [vmnb, vmnb] }
711-
test_impl! { vec_vmnsh (a: vector_signed_short, b: vector_signed_short) -> vector_signed_short [vmnh, vmnh] }
712-
test_impl! { vec_vmnsf (a: vector_signed_int, b: vector_signed_int) -> vector_signed_int [vmnf, vmnf] }
713-
test_impl! { vec_vmnsg (a: vector_signed_long_long, b: vector_signed_long_long) -> vector_signed_long_long [vmng, vmng] }
713+
macro_rules! impl_min {
714+
($name:ident, $a:ty, $instr:ident) => {
715+
#[inline]
716+
#[target_feature(enable = "vector")]
717+
#[cfg_attr(test, assert_instr($instr))]
718+
pub unsafe fn $name(a: $a, b: $a) -> $a {
719+
simd_select(simd_le::<_, $a>(a, b), a, b)
720+
}
714721

715-
test_impl! { vec_vmnslb (a: vector_unsigned_char, b: vector_unsigned_char) -> vector_unsigned_char [vmnlb, vmnlb] }
716-
test_impl! { vec_vmnslh (a: vector_unsigned_short, b: vector_unsigned_short) -> vector_unsigned_short [vmnlh, vmnlh] }
717-
test_impl! { vec_vmnslf (a: vector_unsigned_int, b: vector_unsigned_int) -> vector_unsigned_int [vmnlf, vmnlf] }
718-
test_impl! { vec_vmnslg (a: vector_unsigned_long_long, b: vector_unsigned_long_long) -> vector_unsigned_long_long [vmnlg, vmnlg] }
722+
#[unstable(feature = "stdarch_s390x", issue = "135681")]
723+
impl VectorMin<Self> for $a {
724+
type Result = Self;
719725

720-
impl_vec_trait! { [VectorMin vec_min] ~(vmxlb, vmxb, vmxlh, vmxh, vmxlf, vmxf, vmxlg, vmxg) }
726+
#[inline]
727+
#[target_feature(enable = "vector")]
728+
unsafe fn vec_min(self, other: Self) -> Self {
729+
$name(self, other)
730+
}
731+
}
732+
};
733+
}
734+
735+
mod impl_min {
736+
use super::*;
737+
738+
impl_min!(vec_vmnsc, vector_signed_char, vmnb);
739+
impl_min!(vec_vmnslc, vector_unsigned_char, vmnlb);
740+
impl_min!(vec_vmnsh, vector_signed_short, vmnh);
741+
impl_min!(vec_vmnslh, vector_unsigned_short, vmnlh);
742+
impl_min!(vec_vmnsf, vector_signed_int, vmnf);
743+
impl_min!(vec_vmnslf, vector_unsigned_int, vmnlf);
744+
impl_min!(vec_vmnsg, vector_signed_long_long, vmng);
745+
impl_min!(vec_vmnslg, vector_unsigned_long_long, vmnlg);
746+
}
721747

722748
test_impl! { vec_vfminsb (a: vector_float, b: vector_float) -> vector_float [simd_fmin, "vector-enhancements-1" vfminsb] }
723749
test_impl! { vec_vfmindb (a: vector_double, b: vector_double) -> vector_double [simd_fmin, "vector-enhancements-1" vfmindb] }
@@ -2368,26 +2394,22 @@ mod sealed {
23682394
unsafe fn vec_packsu(self, b: Other) -> Self::Result;
23692395
}
23702396

2371-
unsafe fn simd_smax<T: Copy>(a: T, b: T) -> T {
2372-
simd_select::<T, T>(simd_gt::<T, T>(a, b), a, b)
2373-
}
2374-
23752397
#[inline]
23762398
#[target_feature(enable = "vector")]
23772399
#[cfg_attr(test, assert_instr(vpklsh))]
23782400
unsafe fn vpacksuh(a: vector_signed_short, b: vector_signed_short) -> vector_unsigned_char {
23792401
vpklsh(
2380-
simd_smax(a, vector_signed_short([0; 8])),
2381-
simd_smax(b, vector_signed_short([0; 8])),
2402+
vec_max(a, vector_signed_short([0; 8])),
2403+
vec_max(b, vector_signed_short([0; 8])),
23822404
)
23832405
}
23842406
#[inline]
23852407
#[target_feature(enable = "vector")]
23862408
#[cfg_attr(test, assert_instr(vpklsf))]
23872409
unsafe fn vpacksuf(a: vector_signed_int, b: vector_signed_int) -> vector_unsigned_short {
23882410
vpklsf(
2389-
simd_smax(a, vector_signed_int([0; 4])),
2390-
simd_smax(b, vector_signed_int([0; 4])),
2411+
vec_max(a, vector_signed_int([0; 4])),
2412+
vec_max(b, vector_signed_int([0; 4])),
23912413
)
23922414
}
23932415
#[inline]
@@ -2398,8 +2420,8 @@ mod sealed {
23982420
b: vector_signed_long_long,
23992421
) -> vector_unsigned_int {
24002422
vpklsg(
2401-
simd_smax(a, vector_signed_long_long([0; 2])),
2402-
simd_smax(b, vector_signed_long_long([0; 2])),
2423+
vec_max(a, vector_signed_long_long([0; 2])),
2424+
vec_max(b, vector_signed_long_long([0; 2])),
24032425
)
24042426
}
24052427

0 commit comments

Comments
 (0)