Skip to content

Commit

Permalink
Merge pull request rust-lang#67 from rust-lang/limit-lanes
Browse files Browse the repository at this point in the history
Limit all types to 64 lanes
  • Loading branch information
workingjubilee authored Feb 15, 2021
2 parents 8aa7ba7 + faae170 commit cbca211
Show file tree
Hide file tree
Showing 23 changed files with 519 additions and 151 deletions.
5 changes: 4 additions & 1 deletion crates/core_simd/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ macro_rules! impl_fmt_trait {
{ $($type:ident => $(($trait:ident, $format:ident)),*;)* } => {
$( // repeat type
$( // repeat trait
impl<const LANES: usize> core::fmt::$trait for crate::$type<LANES> {
impl<const LANES: usize> core::fmt::$trait for crate::$type<LANES>
where
Self: crate::LanesAtMost64,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
$format(self.as_ref(), f)
}
Expand Down
35 changes: 35 additions & 0 deletions crates/core_simd/src/lanes_at_most_64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Implemented for bitmask sizes that are supported by the implementation.
pub trait LanesAtMost64 {}

macro_rules! impl_for {
{ $name:ident } => {
impl LanesAtMost64 for $name<1> {}
impl LanesAtMost64 for $name<2> {}
impl LanesAtMost64 for $name<4> {}
impl LanesAtMost64 for $name<8> {}
impl LanesAtMost64 for $name<16> {}
impl LanesAtMost64 for $name<32> {}
impl LanesAtMost64 for $name<64> {}
}
}

use crate::*;

impl_for! { SimdU8 }
impl_for! { SimdU16 }
impl_for! { SimdU32 }
impl_for! { SimdU64 }
impl_for! { SimdU128 }
impl_for! { SimdUsize }

impl_for! { SimdI8 }
impl_for! { SimdI16 }
impl_for! { SimdI32 }
impl_for! { SimdI64 }
impl_for! { SimdI128 }
impl_for! { SimdIsize }

impl_for! { SimdF32 }
impl_for! { SimdF64 }

impl_for! { BitMask }
3 changes: 3 additions & 0 deletions crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ mod intrinsics;
mod ops;
mod round;

mod lanes_at_most_64;
pub use lanes_at_most_64::LanesAtMost64;

mod masks;
pub use masks::*;

Expand Down
36 changes: 20 additions & 16 deletions crates/core_simd/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! from_transmute_x86 {
/// Implements common traits on the specified vector `$name`, holding multiple `$lanes` of `$type`.
macro_rules! impl_vector {
{ $name:ident, $type:ty } => {
impl<const LANES: usize> $name<LANES> {
impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 {
/// Construct a SIMD vector by setting all lanes to the given value.
pub const fn splat(value: $type) -> Self {
Self([value; LANES])
Expand Down Expand Up @@ -72,31 +72,31 @@ macro_rules! impl_vector {
}
}

impl<const LANES: usize> Copy for $name<LANES> {}
impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost64 {}

impl<const LANES: usize> Clone for $name<LANES> {
impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn clone(&self) -> Self {
*self
}
}

impl<const LANES: usize> Default for $name<LANES> {
impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn default() -> Self {
Self::splat(<$type>::default())
}
}

impl<const LANES: usize> PartialEq for $name<LANES> {
impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn eq(&self, other: &Self) -> bool {
// TODO use SIMD equality
self.to_array() == other.to_array()
}
}

impl<const LANES: usize> PartialOrd for $name<LANES> {
impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
// TODO use SIMD equalitya
Expand All @@ -105,44 +105,44 @@ macro_rules! impl_vector {
}

// array references
impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> {
impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn as_ref(&self) -> &[$type; LANES] {
&self.0
}
}

impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> {
impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn as_mut(&mut self) -> &mut [$type; LANES] {
&mut self.0
}
}

// slice references
impl<const LANES: usize> AsRef<[$type]> for $name<LANES> {
impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn as_ref(&self) -> &[$type] {
&self.0
}
}

impl<const LANES: usize> AsMut<[$type]> for $name<LANES> {
impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn as_mut(&mut self) -> &mut [$type] {
&mut self.0
}
}

// vector/array conversion
impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> {
impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost64 {
fn from(array: [$type; LANES]) -> Self {
Self(array)
}
}

// splat
impl<const LANES: usize> From<$type> for $name<LANES> {
impl<const LANES: usize> From<$type> for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn from(value: $type) -> Self {
Self::splat(value)
Expand All @@ -158,17 +158,17 @@ macro_rules! impl_integer_vector {
{ $name:ident, $type:ty } => {
impl_vector! { $name, $type }

impl<const LANES: usize> Eq for $name<LANES> {}
impl<const LANES: usize> Eq for $name<LANES> where Self: crate::LanesAtMost64 {}

impl<const LANES: usize> Ord for $name<LANES> {
impl<const LANES: usize> Ord for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
// TODO use SIMD cmp
self.to_array().cmp(other.as_ref())
}
}

impl<const LANES: usize> core::hash::Hash for $name<LANES> {
impl<const LANES: usize> core::hash::Hash for $name<LANES> where Self: crate::LanesAtMost64 {
#[inline]
fn hash<H>(&self, state: &mut H)
where
Expand All @@ -187,7 +187,11 @@ macro_rules! impl_float_vector {
{ $name:ident, $type:ty, $bits_ty:ident } => {
impl_vector! { $name, $type }

impl<const LANES: usize> $name<LANES> {
impl<const LANES: usize> $name<LANES>
where
Self: crate::LanesAtMost64,
crate::$bits_ty<LANES>: crate::LanesAtMost64,
{
/// Raw transmutation to an unsigned integer vector type with the
/// same size and number of lanes.
#[inline]
Expand Down
10 changes: 1 addition & 9 deletions crates/core_simd/src/masks/bitmask.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
/// Implemented for bitmask sizes that are supported by the implementation.
pub trait LanesAtMost64 {}
impl LanesAtMost64 for BitMask<1> {}
impl LanesAtMost64 for BitMask<2> {}
impl LanesAtMost64 for BitMask<4> {}
impl LanesAtMost64 for BitMask<8> {}
impl LanesAtMost64 for BitMask<16> {}
impl LanesAtMost64 for BitMask<32> {}
impl LanesAtMost64 for BitMask<64> {}
use crate::LanesAtMost64;

/// A mask where each lane is represented by a single bit.
#[derive(Copy, Clone, Debug)]
Expand Down
Loading

0 comments on commit cbca211

Please sign in to comment.