Change as_slice to as_array

This commit is contained in:
Caleb Zulawski 2021-06-28 00:41:21 +00:00
parent f178dda187
commit fdd7d6e252
6 changed files with 21 additions and 53 deletions

View file

@ -7,13 +7,13 @@ macro_rules! impl_vector {
Self([value; LANES])
}
/// Returns a slice containing the entire SIMD vector.
pub const fn as_slice(&self) -> &[$type] {
/// Returns an array reference containing the entire SIMD vector.
pub const fn as_array(&self) -> &[$type; LANES] {
&self.0
}
/// Returns a mutable slice containing the entire SIMD vector.
pub fn as_mut_slice(&mut self) -> &mut [$type] {
/// Returns a mutable array reference containing the entire SIMD vector.
pub fn as_mut_array(&mut self) -> &mut [$type; LANES] {
&mut self.0
}
@ -24,23 +24,7 @@ macro_rules! impl_vector {
/// Converts a SIMD vector to an array.
pub const fn to_array(self) -> [$type; LANES] {
// workaround for rust-lang/rust#80108
// TODO fix this
#[cfg(target_arch = "wasm32")]
{
let mut arr = [self.0[0]; LANES];
let mut i = 0;
while i < LANES {
arr[i] = self.0[i];
i += 1;
}
arr
}
#[cfg(not(target_arch = "wasm32"))]
{
self.0
}
self.0
}
}

View file

@ -331,7 +331,7 @@ macro_rules! impl_unsigned_int_ops {
#[inline]
fn div(self, rhs: Self) -> Self::Output {
if rhs.as_slice()
if rhs.as_array()
.iter()
.any(|x| *x == 0)
{
@ -340,8 +340,8 @@ macro_rules! impl_unsigned_int_ops {
// Guards for div(MIN, -1),
// this check only applies to signed ints
if <$scalar>::MIN != 0 && self.as_slice().iter()
.zip(rhs.as_slice().iter())
if <$scalar>::MIN != 0 && self.as_array().iter()
.zip(rhs.as_array().iter())
.any(|(x,y)| *x == <$scalar>::MIN && *y == -1 as _) {
panic!("attempt to divide with overflow");
}
@ -363,7 +363,7 @@ macro_rules! impl_unsigned_int_ops {
panic!("attempt to divide by zero");
}
if <$scalar>::MIN != 0 &&
self.as_slice().iter().any(|x| *x == <$scalar>::MIN) &&
self.as_array().iter().any(|x| *x == <$scalar>::MIN) &&
rhs == -1 as _ {
panic!("attempt to divide with overflow");
}
@ -421,7 +421,7 @@ macro_rules! impl_unsigned_int_ops {
#[inline]
fn rem(self, rhs: Self) -> Self::Output {
if rhs.as_slice()
if rhs.as_array()
.iter()
.any(|x| *x == 0)
{
@ -430,8 +430,8 @@ macro_rules! impl_unsigned_int_ops {
// Guards for rem(MIN, -1)
// this branch applies the check only to signed ints
if <$scalar>::MIN != 0 && self.as_slice().iter()
.zip(rhs.as_slice().iter())
if <$scalar>::MIN != 0 && self.as_array().iter()
.zip(rhs.as_array().iter())
.any(|(x,y)| *x == <$scalar>::MIN && *y == -1 as _) {
panic!("attempt to calculate the remainder with overflow");
}
@ -453,7 +453,7 @@ macro_rules! impl_unsigned_int_ops {
panic!("attempt to calculate the remainder with a divisor of zero");
}
if <$scalar>::MIN != 0 &&
self.as_slice().iter().any(|x| *x == <$scalar>::MIN) &&
self.as_array().iter().any(|x| *x == <$scalar>::MIN) &&
rhs == -1 as _ {
panic!("attempt to calculate the remainder with overflow");
}
@ -512,7 +512,7 @@ macro_rules! impl_unsigned_int_ops {
#[inline]
fn shl(self, rhs: Self) -> Self::Output {
// TODO there is probably a better way of doing this
if rhs.as_slice()
if rhs.as_array()
.iter()
.copied()
.any(invalid_shift_rhs)
@ -577,7 +577,7 @@ macro_rules! impl_unsigned_int_ops {
#[inline]
fn shr(self, rhs: Self) -> Self::Output {
// TODO there is probably a better way of doing this
if rhs.as_slice()
if rhs.as_array()
.iter()
.copied()
.any(invalid_shift_rhs)

View file

@ -64,7 +64,7 @@ macro_rules! impl_float_reductions {
pub fn horizontal_sum(self) -> $scalar {
// LLVM sum is inaccurate on i586
if cfg!(all(target_arch = "x86", not(target_feature = "sse2"))) {
self.as_slice().iter().sum()
self.as_array().iter().sum()
} else {
unsafe { crate::intrinsics::simd_reduce_add_ordered(self, 0.) }
}
@ -75,7 +75,7 @@ macro_rules! impl_float_reductions {
pub fn horizontal_product(self) -> $scalar {
// LLVM product is inaccurate on i586
if cfg!(all(target_arch = "x86", not(target_feature = "sse2"))) {
self.as_slice().iter().product()
self.as_array().iter().product()
} else {
unsafe { crate::intrinsics::simd_reduce_mul_ordered(self, 1.) }
}

View file

@ -12,7 +12,7 @@ macro_rules! impl_integer_vector {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
// TODO use SIMD cmp
self.to_array().cmp(other.as_ref())
self.as_array().cmp(other.as_ref())
}
}
@ -22,7 +22,7 @@ macro_rules! impl_integer_vector {
where
H: core::hash::Hasher
{
self.as_slice().hash(state)
self.as_array().hash(state)
}
}

View file

@ -30,12 +30,6 @@ pub trait Vector: sealed::Sealed {
/// Generates a SIMD vector with the same value in every lane.
#[must_use]
fn splat(val: Self::Scalar) -> Self;
/// Returns a slice containing the entire SIMD vector.
fn as_slice(&self) -> &[Self::Scalar];
/// Returns a mutable slice containing the entire SIMD vector.
fn as_mut_slice(&mut self) -> &mut [Self::Scalar];
}
macro_rules! impl_vector_for {
@ -60,16 +54,6 @@ macro_rules! impl_vector_for {
fn splat(val: Self::Scalar) -> Self {
Self::splat(val)
}
#[inline]
fn as_slice(&self) -> &[Self::Scalar] {
self.as_slice()
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [Self::Scalar] {
self.as_mut_slice()
}
}
};
}

View file

@ -12,7 +12,7 @@ macro_rules! impl_unsigned_vector {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
// TODO use SIMD cmp
self.to_array().cmp(other.as_ref())
self.as_array().cmp(other.as_ref())
}
}
@ -22,7 +22,7 @@ macro_rules! impl_unsigned_vector {
where
H: core::hash::Hasher
{
self.as_slice().hash(state)
self.as_array().hash(state)
}
}
}