Skip to content

Commit 0682c31

Browse files
Merge pull request rust-lang#80 from rust-lang/feature/comparisons
Add classification functions
2 parents 4e6d440 + e6a5309 commit 0682c31

22 files changed

+537
-299
lines changed

crates/core_simd/src/comparisons.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use crate::LanesAtMost32;
2+
3+
macro_rules! implement_mask_ops {
4+
{ $($vector:ident => $mask:ident ($inner_mask_ty:ident, $inner_ty:ident),)* } => {
5+
$(
6+
impl<const LANES: usize> crate::$vector<LANES>
7+
where
8+
crate::$vector<LANES>: LanesAtMost32,
9+
crate::$inner_ty<LANES>: LanesAtMost32,
10+
{
11+
/// Test if each lane is equal to the corresponding lane in `other`.
12+
#[inline]
13+
pub fn lanes_eq(self, other: Self) -> crate::$mask<LANES> {
14+
unsafe {
15+
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_eq(self, other))
16+
.into()
17+
}
18+
}
19+
20+
/// Test if each lane is not equal to the corresponding lane in `other`.
21+
#[inline]
22+
pub fn lanes_ne(self, other: Self) -> crate::$mask<LANES> {
23+
unsafe {
24+
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_ne(self, other))
25+
.into()
26+
}
27+
}
28+
29+
/// Test if each lane is less than the corresponding lane in `other`.
30+
#[inline]
31+
pub fn lanes_lt(self, other: Self) -> crate::$mask<LANES> {
32+
unsafe {
33+
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_lt(self, other))
34+
.into()
35+
}
36+
}
37+
38+
/// Test if each lane is greater than the corresponding lane in `other`.
39+
#[inline]
40+
pub fn lanes_gt(self, other: Self) -> crate::$mask<LANES> {
41+
unsafe {
42+
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_gt(self, other))
43+
.into()
44+
}
45+
}
46+
47+
/// Test if each lane is less than or equal to the corresponding lane in `other`.
48+
#[inline]
49+
pub fn lanes_le(self, other: Self) -> crate::$mask<LANES> {
50+
unsafe {
51+
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_le(self, other))
52+
.into()
53+
}
54+
}
55+
56+
/// Test if each lane is greater than or equal to the corresponding lane in `other`.
57+
#[inline]
58+
pub fn lanes_ge(self, other: Self) -> crate::$mask<LANES> {
59+
unsafe {
60+
crate::$inner_mask_ty::from_int_unchecked(crate::intrinsics::simd_ge(self, other))
61+
.into()
62+
}
63+
}
64+
}
65+
)*
66+
}
67+
}
68+
69+
implement_mask_ops! {
70+
SimdI8 => Mask8 (SimdMask8, SimdI8),
71+
SimdI16 => Mask16 (SimdMask16, SimdI16),
72+
SimdI32 => Mask32 (SimdMask32, SimdI32),
73+
SimdI64 => Mask64 (SimdMask64, SimdI64),
74+
SimdI128 => Mask128 (SimdMask128, SimdI128),
75+
SimdIsize => MaskSize (SimdMaskSize, SimdIsize),
76+
77+
SimdU8 => Mask8 (SimdMask8, SimdI8),
78+
SimdU16 => Mask16 (SimdMask16, SimdI16),
79+
SimdU32 => Mask32 (SimdMask32, SimdI32),
80+
SimdU64 => Mask64 (SimdMask64, SimdI64),
81+
SimdU128 => Mask128 (SimdMask128, SimdI128),
82+
SimdUsize => MaskSize (SimdMaskSize, SimdIsize),
83+
84+
SimdF32 => Mask32 (SimdMask32, SimdI32),
85+
SimdF64 => Mask64 (SimdMask64, SimdI64),
86+
}

crates/core_simd/src/first.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Implements common traits on the specified vector `$name`, holding multiple `$lanes` of `$type`.
22
macro_rules! impl_vector {
33
{ $name:ident, $type:ty } => {
4-
impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 {
4+
impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost32 {
55
/// Construct a SIMD vector by setting all lanes to the given value.
66
pub const fn splat(value: $type) -> Self {
77
Self([value; LANES])
@@ -44,31 +44,31 @@ macro_rules! impl_vector {
4444
}
4545
}
4646

47-
impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost64 {}
47+
impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost32 {}
4848

49-
impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost64 {
49+
impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost32 {
5050
#[inline]
5151
fn clone(&self) -> Self {
5252
*self
5353
}
5454
}
5555

56-
impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost64 {
56+
impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost32 {
5757
#[inline]
5858
fn default() -> Self {
5959
Self::splat(<$type>::default())
6060
}
6161
}
6262

63-
impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost64 {
63+
impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost32 {
6464
#[inline]
6565
fn eq(&self, other: &Self) -> bool {
6666
// TODO use SIMD equality
6767
self.to_array() == other.to_array()
6868
}
6969
}
7070

71-
impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost64 {
71+
impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost32 {
7272
#[inline]
7373
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
7474
// TODO use SIMD equalitya
@@ -77,43 +77,43 @@ macro_rules! impl_vector {
7777
}
7878

7979
// array references
80-
impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
80+
impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
8181
#[inline]
8282
fn as_ref(&self) -> &[$type; LANES] {
8383
&self.0
8484
}
8585
}
8686

87-
impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
87+
impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
8888
#[inline]
8989
fn as_mut(&mut self) -> &mut [$type; LANES] {
9090
&mut self.0
9191
}
9292
}
9393

9494
// slice references
95-
impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost64 {
95+
impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
9696
#[inline]
9797
fn as_ref(&self) -> &[$type] {
9898
&self.0
9999
}
100100
}
101101

102-
impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost64 {
102+
impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
103103
#[inline]
104104
fn as_mut(&mut self) -> &mut [$type] {
105105
&mut self.0
106106
}
107107
}
108108

109109
// vector/array conversion
110-
impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
110+
impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
111111
fn from(array: [$type; LANES]) -> Self {
112112
Self(array)
113113
}
114114
}
115115

116-
impl <const LANES: usize> From<$name<LANES>> for [$type; LANES] where $name<LANES>: crate::LanesAtMost64 {
116+
impl <const LANES: usize> From<$name<LANES>> for [$type; LANES] where $name<LANES>: crate::LanesAtMost32 {
117117
fn from(vector: $name<LANES>) -> Self {
118118
vector.to_array()
119119
}

crates/core_simd/src/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! impl_fmt_trait {
3535
$( // repeat trait
3636
impl<const LANES: usize> core::fmt::$trait for crate::$type<LANES>
3737
where
38-
Self: crate::LanesAtMost64,
38+
Self: crate::LanesAtMost32,
3939
{
4040
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
4141
$format(self.as_ref(), f)

crates/core_simd/src/intrinsics.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ extern "platform-intrinsic" {
6161
pub(crate) fn simd_shuffle8<T, U>(x: T, y: T, idx: [u32; 8]) -> U;
6262
pub(crate) fn simd_shuffle16<T, U>(x: T, y: T, idx: [u32; 16]) -> U;
6363
pub(crate) fn simd_shuffle32<T, U>(x: T, y: T, idx: [u32; 32]) -> U;
64-
pub(crate) fn simd_shuffle64<T, U>(x: T, y: T, idx: [u32; 64]) -> U;
6564

6665
// {s,u}add.sat
6766
pub(crate) fn simd_saturating_add<T>(x: T, y: T) -> T;

crates/core_simd/src/lanes_at_most_64.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/// Implemented for bitmask sizes that are supported by the implementation.
2-
pub trait LanesAtMost64 {}
2+
pub trait LanesAtMost32 {}
33

44
macro_rules! impl_for {
55
{ $name:ident } => {
6-
impl LanesAtMost64 for $name<1> {}
7-
impl LanesAtMost64 for $name<2> {}
8-
impl LanesAtMost64 for $name<4> {}
9-
impl LanesAtMost64 for $name<8> {}
10-
impl LanesAtMost64 for $name<16> {}
11-
impl LanesAtMost64 for $name<32> {}
12-
impl LanesAtMost64 for $name<64> {}
6+
impl LanesAtMost32 for $name<1> {}
7+
impl LanesAtMost32 for $name<2> {}
8+
impl LanesAtMost32 for $name<4> {}
9+
impl LanesAtMost32 for $name<8> {}
10+
impl LanesAtMost32 for $name<16> {}
11+
impl LanesAtMost32 for $name<32> {}
1312
}
1413
}
1514

crates/core_simd/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod permute;
1212
#[macro_use]
1313
mod transmute;
1414

15+
mod comparisons;
1516
mod fmt;
1617
mod intrinsics;
1718
mod ops;
@@ -20,7 +21,7 @@ mod round;
2021
mod math;
2122

2223
mod lanes_at_most_64;
23-
pub use lanes_at_most_64::LanesAtMost64;
24+
pub use lanes_at_most_64::LanesAtMost32;
2425

2526
mod masks;
2627
pub use masks::*;

0 commit comments

Comments
 (0)