Skip to content

Commit d428753

Browse files
Merge pull request rust-lang#154 from rust-lang/feature/generic-element-type
Change vectors to be generic over element type.
2 parents 50eb35e + 4aafd8e commit d428753

40 files changed

+1839
-2039
lines changed

crates/core_simd/src/comparisons.rs

+42-70
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,49 @@
1-
use crate::{LaneCount, SupportedLaneCount};
2-
3-
macro_rules! implement_mask_ops {
4-
{ $($vector:ident => $mask:ident ($inner_ty:ident),)* } => {
5-
$(
6-
impl<const LANES: usize> crate::$vector<LANES>
7-
where
8-
LaneCount<LANES>: SupportedLaneCount,
9-
{
10-
/// Test if each lane is equal to the corresponding lane in `other`.
11-
#[inline]
12-
pub fn lanes_eq(self, other: Self) -> crate::$mask<LANES> {
13-
unsafe {
14-
crate::$mask::from_int_unchecked(crate::intrinsics::simd_eq(self, other))
15-
}
16-
}
17-
18-
/// Test if each lane is not equal to the corresponding lane in `other`.
19-
#[inline]
20-
pub fn lanes_ne(self, other: Self) -> crate::$mask<LANES> {
21-
unsafe {
22-
crate::$mask::from_int_unchecked(crate::intrinsics::simd_ne(self, other))
23-
}
24-
}
25-
26-
/// Test if each lane is less than the corresponding lane in `other`.
27-
#[inline]
28-
pub fn lanes_lt(self, other: Self) -> crate::$mask<LANES> {
29-
unsafe {
30-
crate::$mask::from_int_unchecked(crate::intrinsics::simd_lt(self, other))
31-
}
32-
}
33-
34-
/// Test if each lane is greater than the corresponding lane in `other`.
35-
#[inline]
36-
pub fn lanes_gt(self, other: Self) -> crate::$mask<LANES> {
37-
unsafe {
38-
crate::$mask::from_int_unchecked(crate::intrinsics::simd_gt(self, other))
39-
}
40-
}
41-
42-
/// Test if each lane is less than or equal to the corresponding lane in `other`.
43-
#[inline]
44-
pub fn lanes_le(self, other: Self) -> crate::$mask<LANES> {
45-
unsafe {
46-
crate::$mask::from_int_unchecked(crate::intrinsics::simd_le(self, other))
47-
}
48-
}
1+
use crate::{LaneCount, Mask, Simd, SimdElement, SupportedLaneCount};
2+
3+
impl<T, const LANES: usize> Simd<T, LANES>
4+
where
5+
T: SimdElement + PartialEq,
6+
LaneCount<LANES>: SupportedLaneCount,
7+
{
8+
/// Test if each lane is equal to the corresponding lane in `other`.
9+
#[inline]
10+
pub fn lanes_eq(self, other: Self) -> Mask<T::Mask, LANES> {
11+
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_eq(self, other)) }
12+
}
4913

50-
/// Test if each lane is greater than or equal to the corresponding lane in `other`.
51-
#[inline]
52-
pub fn lanes_ge(self, other: Self) -> crate::$mask<LANES> {
53-
unsafe {
54-
crate::$mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other))
55-
}
56-
}
57-
}
58-
)*
14+
/// Test if each lane is not equal to the corresponding lane in `other`.
15+
#[inline]
16+
pub fn lanes_ne(self, other: Self) -> Mask<T::Mask, LANES> {
17+
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_ne(self, other)) }
5918
}
6019
}
6120

62-
implement_mask_ops! {
63-
SimdI8 => Mask8 (SimdI8),
64-
SimdI16 => Mask16 (SimdI16),
65-
SimdI32 => Mask32 (SimdI32),
66-
SimdI64 => Mask64 (SimdI64),
67-
SimdIsize => MaskSize (SimdIsize),
21+
impl<T, const LANES: usize> Simd<T, LANES>
22+
where
23+
T: SimdElement + PartialOrd,
24+
LaneCount<LANES>: SupportedLaneCount,
25+
{
26+
/// Test if each lane is less than the corresponding lane in `other`.
27+
#[inline]
28+
pub fn lanes_lt(self, other: Self) -> Mask<T::Mask, LANES> {
29+
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_lt(self, other)) }
30+
}
6831

69-
SimdU8 => Mask8 (SimdI8),
70-
SimdU16 => Mask16 (SimdI16),
71-
SimdU32 => Mask32 (SimdI32),
72-
SimdU64 => Mask64 (SimdI64),
73-
SimdUsize => MaskSize (SimdIsize),
32+
/// Test if each lane is greater than the corresponding lane in `other`.
33+
#[inline]
34+
pub fn lanes_gt(self, other: Self) -> Mask<T::Mask, LANES> {
35+
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_gt(self, other)) }
36+
}
7437

75-
SimdF32 => Mask32 (SimdI32),
76-
SimdF64 => Mask64 (SimdI64),
38+
/// Test if each lane is less than or equal to the corresponding lane in `other`.
39+
#[inline]
40+
pub fn lanes_le(self, other: Self) -> Mask<T::Mask, LANES> {
41+
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_le(self, other)) }
42+
}
43+
44+
/// Test if each lane is greater than or equal to the corresponding lane in `other`.
45+
#[inline]
46+
pub fn lanes_ge(self, other: Self) -> Mask<T::Mask, LANES> {
47+
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other)) }
48+
}
7749
}

crates/core_simd/src/fmt.rs

+25-77
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,36 @@
1-
macro_rules! debug_wrapper {
2-
{ $($trait:ident => $name:ident,)* } => {
1+
macro_rules! impl_fmt_trait {
2+
{ $($trait:ident,)* } => {
33
$(
4-
pub(crate) fn $name<T: core::fmt::$trait>(slice: &[T], f: &mut core::fmt::Formatter) -> core::fmt::Result {
5-
#[repr(transparent)]
6-
struct Wrapper<'a, T: core::fmt::$trait>(&'a T);
4+
impl<T, const LANES: usize> core::fmt::$trait for crate::Simd<T, LANES>
5+
where
6+
crate::LaneCount<LANES>: crate::SupportedLaneCount,
7+
T: crate::SimdElement + core::fmt::$trait,
8+
{
9+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
10+
#[repr(transparent)]
11+
struct Wrapper<'a, T: core::fmt::$trait>(&'a T);
712

8-
impl<T: core::fmt::$trait> core::fmt::Debug for Wrapper<'_, T> {
9-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
10-
self.0.fmt(f)
13+
impl<T: core::fmt::$trait> core::fmt::Debug for Wrapper<'_, T> {
14+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
15+
self.0.fmt(f)
16+
}
1117
}
12-
}
13-
14-
f.debug_list()
15-
.entries(slice.iter().map(|x| Wrapper(x)))
16-
.finish()
17-
}
18-
)*
19-
}
20-
}
2118

22-
debug_wrapper! {
23-
Debug => format,
24-
Binary => format_binary,
25-
LowerExp => format_lower_exp,
26-
UpperExp => format_upper_exp,
27-
Octal => format_octal,
28-
LowerHex => format_lower_hex,
29-
UpperHex => format_upper_hex,
30-
}
31-
32-
macro_rules! impl_fmt_trait {
33-
{ $($type:ident => $(($trait:ident, $format:ident)),*;)* } => {
34-
$( // repeat type
35-
$( // repeat trait
36-
impl<const LANES: usize> core::fmt::$trait for crate::$type<LANES>
37-
where
38-
crate::LaneCount<LANES>: crate::SupportedLaneCount,
39-
{
40-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
41-
$format(self.as_ref(), f)
42-
}
19+
f.debug_list()
20+
.entries(self.as_array().iter().map(|x| Wrapper(x)))
21+
.finish()
4322
}
44-
)*
23+
}
4524
)*
46-
};
47-
{ integers: $($type:ident,)* } => {
48-
impl_fmt_trait! {
49-
$($type =>
50-
(Debug, format),
51-
(Binary, format_binary),
52-
(LowerExp, format_lower_exp),
53-
(UpperExp, format_upper_exp),
54-
(Octal, format_octal),
55-
(LowerHex, format_lower_hex),
56-
(UpperHex, format_upper_hex);
57-
)*
58-
}
59-
};
60-
{ floats: $($type:ident,)* } => {
61-
impl_fmt_trait! {
62-
$($type =>
63-
(Debug, format),
64-
(LowerExp, format_lower_exp),
65-
(UpperExp, format_upper_exp);
66-
)*
67-
}
68-
};
69-
{ masks: $($type:ident,)* } => {
70-
impl_fmt_trait! {
71-
$($type =>
72-
(Debug, format);
73-
)*
74-
}
7525
}
7626
}
7727

7828
impl_fmt_trait! {
79-
integers:
80-
SimdU8, SimdU16, SimdU32, SimdU64,
81-
SimdI8, SimdI16, SimdI32, SimdI64,
82-
SimdUsize, SimdIsize,
83-
}
84-
85-
impl_fmt_trait! {
86-
floats:
87-
SimdF32, SimdF64,
29+
Debug,
30+
Binary,
31+
LowerExp,
32+
UpperExp,
33+
Octal,
34+
LowerHex,
35+
UpperHex,
8836
}

crates/core_simd/src/iter.rs

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
1-
use crate::{LaneCount, SupportedLaneCount};
1+
use crate::{LaneCount, Simd, SupportedLaneCount};
2+
use core::{
3+
iter::{Product, Sum},
4+
ops::{Add, Mul},
5+
};
26

37
macro_rules! impl_traits {
4-
{ $type:ident } => {
5-
impl<const LANES: usize> core::iter::Sum<Self> for crate::$type<LANES>
8+
{ $type:ty } => {
9+
impl<const LANES: usize> Sum<Self> for Simd<$type, LANES>
610
where
711
LaneCount<LANES>: SupportedLaneCount,
812
{
9-
fn sum<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
10-
iter.fold(Default::default(), core::ops::Add::add)
13+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
14+
iter.fold(Simd::splat(0 as $type), Add::add)
1115
}
1216
}
1317

14-
impl<const LANES: usize> core::iter::Product<Self> for crate::$type<LANES>
18+
impl<const LANES: usize> core::iter::Product<Self> for Simd<$type, LANES>
1519
where
1620
LaneCount<LANES>: SupportedLaneCount,
1721
{
18-
fn product<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
19-
iter.fold(Default::default(), core::ops::Mul::mul)
22+
fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
23+
iter.fold(Simd::splat(1 as $type), Mul::mul)
2024
}
2125
}
2226

23-
impl<'a, const LANES: usize> core::iter::Sum<&'a Self> for crate::$type<LANES>
27+
impl<'a, const LANES: usize> Sum<&'a Self> for Simd<$type, LANES>
2428
where
2529
LaneCount<LANES>: SupportedLaneCount,
2630
{
27-
fn sum<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
28-
iter.fold(Default::default(), core::ops::Add::add)
31+
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
32+
iter.fold(Simd::splat(0 as $type), Add::add)
2933
}
3034
}
3135

32-
impl<'a, const LANES: usize> core::iter::Product<&'a Self> for crate::$type<LANES>
36+
impl<'a, const LANES: usize> Product<&'a Self> for Simd<$type, LANES>
3337
where
3438
LaneCount<LANES>: SupportedLaneCount,
3539
{
36-
fn product<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
37-
iter.fold(Default::default(), core::ops::Mul::mul)
40+
fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
41+
iter.fold(Simd::splat(1 as $type), Mul::mul)
3842
}
3943
}
4044
}
4145
}
4246

43-
impl_traits! { SimdF32 }
44-
impl_traits! { SimdF64 }
45-
impl_traits! { SimdU8 }
46-
impl_traits! { SimdU16 }
47-
impl_traits! { SimdU32 }
48-
impl_traits! { SimdU64 }
49-
impl_traits! { SimdUsize }
50-
impl_traits! { SimdI8 }
51-
impl_traits! { SimdI16 }
52-
impl_traits! { SimdI32 }
53-
impl_traits! { SimdI64 }
54-
impl_traits! { SimdIsize }
47+
impl_traits! { f32 }
48+
impl_traits! { f64 }
49+
impl_traits! { u8 }
50+
impl_traits! { u16 }
51+
impl_traits! { u32 }
52+
impl_traits! { u64 }
53+
impl_traits! { usize }
54+
impl_traits! { i8 }
55+
impl_traits! { i16 }
56+
impl_traits! { i32 }
57+
impl_traits! { i64 }
58+
impl_traits! { isize }

crates/core_simd/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(incomplete_features)]
33
#![feature(
44
const_evaluatable_checked,
5+
const_fn_trait_bound,
56
const_generics,
67
platform_intrinsics,
78
repr_simd,

0 commit comments

Comments
 (0)