Skip to content

Commit

Permalink
Merge pull request rust-lang#100 from rust-lang/fix-sat-math
Browse files Browse the repository at this point in the history
Fix sat math at the cost of Simd{U,I}128 for now
  • Loading branch information
workingjubilee authored Apr 26, 2021
2 parents 24ebae8 + 92d643b commit a9a1c9d
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 86 deletions.
2 changes: 0 additions & 2 deletions crates/core_simd/src/comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ implement_mask_ops! {
SimdI16 => Mask16 (SimdMask16, SimdI16),
SimdI32 => Mask32 (SimdMask32, SimdI32),
SimdI64 => Mask64 (SimdMask64, SimdI64),
SimdI128 => Mask128 (SimdMask128, SimdI128),
SimdIsize => MaskSize (SimdMaskSize, SimdIsize),

SimdU8 => Mask8 (SimdMask8, SimdI8),
SimdU16 => Mask16 (SimdMask16, SimdI16),
SimdU32 => Mask32 (SimdMask32, SimdI32),
SimdU64 => Mask64 (SimdMask64, SimdI64),
SimdU128 => Mask128 (SimdMask128, SimdI128),
SimdUsize => MaskSize (SimdMaskSize, SimdIsize),

SimdF32 => Mask32 (SimdMask32, SimdI32),
Expand Down
4 changes: 2 additions & 2 deletions crates/core_simd/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ macro_rules! impl_fmt_trait {

impl_fmt_trait! {
integers:
SimdU8, SimdU16, SimdU32, SimdU64, SimdU128,
SimdI8, SimdI16, SimdI32, SimdI64, SimdI128,
SimdU8, SimdU16, SimdU32, SimdU64,
SimdI8, SimdI16, SimdI32, SimdI64,
SimdUsize, SimdIsize,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ 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 }
Expand Down
4 changes: 2 additions & 2 deletions crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ mod round;

mod math;

mod lanes_at_most_64;
pub use lanes_at_most_64::LanesAtMost32;
mod lanes_at_most_32;
pub use lanes_at_most_32::LanesAtMost32;

mod masks;
pub use masks::*;
Expand Down
6 changes: 0 additions & 6 deletions crates/core_simd/src/masks/full_masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,6 @@ define_mask! {
struct SimdMask64<const LANES: usize>(crate::SimdI64<LANES>);
}

define_mask! {
/// A mask equivalent to [SimdI128](crate::SimdI128), where all bits in the lane must be either set
/// or unset.
struct SimdMask128<const LANES: usize>(crate::SimdI128<LANES>);
}

define_mask! {
/// A mask equivalent to [SimdIsize](crate::SimdIsize), where all bits in the lane must be either set
/// or unset.
Expand Down
16 changes: 1 addition & 15 deletions crates/core_simd/src/masks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use full_masks::*;
mod bitmask;
pub use bitmask::*;

use crate::{LanesAtMost32, SimdI128, SimdI16, SimdI32, SimdI64, SimdI8, SimdIsize};
use crate::{LanesAtMost32, SimdI16, SimdI32, SimdI64, SimdI8, SimdIsize};

macro_rules! define_opaque_mask {
{
Expand Down Expand Up @@ -387,14 +387,6 @@ define_opaque_mask! {
@bits SimdI64
}

define_opaque_mask! {
/// Mask for vectors with `LANES` 128-bit elements.
///
/// The layout of this type is unspecified.
struct Mask128<const LANES: usize>(SimdMask128<LANES>);
@bits SimdI128
}

define_opaque_mask! {
/// Mask for vectors with `LANES` pointer-width elements.
///
Expand Down Expand Up @@ -448,12 +440,6 @@ pub type mask64x4 = Mask64<4>;
/// Vector of eight 64-bit masks
pub type mask64x8 = Mask64<8>;

/// Vector of two 128-bit masks
pub type mask128x2 = Mask128<2>;

/// Vector of four 128-bit masks
pub type mask128x4 = Mask128<4>;

/// Vector of two pointer-width masks
pub type masksizex2 = MaskSize<2>;

Expand Down
35 changes: 27 additions & 8 deletions crates/core_simd/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! impl_uint_arith {
($(($name:ident, $n:ty)),+) => {
($(($name:ident, $n:ident)),+) => {
$( impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost32 {

/// Lanewise saturating add.
Expand Down Expand Up @@ -41,7 +41,7 @@ macro_rules! impl_uint_arith {
}

macro_rules! impl_int_arith {
($(($name:ident, $n:ty)),+) => {
($(($name:ident, $n:ident)),+) => {
$( impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost32 {

/// Lanewise saturating add.
Expand Down Expand Up @@ -79,16 +79,34 @@ macro_rules! impl_int_arith {
unsafe { crate::intrinsics::simd_saturating_sub(self, second) }
}

/// Lanewise absolute value, implemented in Rust.
/// Every lane becomes its absolute value.
///
/// # Examples
/// ```
/// # use core_simd::*;
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
#[doc = concat!("let xs = ", stringify!($name), "::from_array([MIN, MIN +1, -5, 0]);")]
#[doc = concat!("assert_eq!(xs.abs(), ", stringify!($name), "::from_array([MIN, MAX, 5, 0]));")]
/// ```
#[inline]
pub fn abs(self) -> Self {
const SHR: $n = <$n>::BITS as $n - 1;
let m = self >> SHR;
(self^m) - m
}

/// Lanewise saturating absolute value, implemented in Rust.
/// As abs(), except the MIN value becomes MAX instead of itself.
///
/// # Examples
/// ```
/// # use core_simd::*;
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
#[doc = concat!("let x = ", stringify!($name), "::splat([MIN, -2, 0, 3]);")]
/// let unsat = x.abs();
/// let sat = x.saturating_abs();
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([MIN, 2, 0, 3]);")]
#[doc = concat!("let xs = ", stringify!($name), "::from_array([MIN, -2, 0, 3]);")]
/// let unsat = xs.abs();
/// let sat = xs.saturating_abs();
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([MIN, 2, 0, 3]));")]
#[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MAX, 2, 0, 3]));")]
/// ```
#[inline]
Expand All @@ -103,12 +121,13 @@ macro_rules! impl_int_arith {
/// As neg(), except the MIN value becomes MAX instead of itself.
///
/// # Examples
/// ```
/// # use core_simd::*;
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
#[doc = concat!("let x = ", stringify!($name), "::splat([MIN, -2, 3, MAX]);")]
#[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, -2, 3, MAX]);")]
/// let unsat = -x;
/// let sat = x.saturating_neg();
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([MIN, 2, -3, MIN + 1]);")]
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([MIN, 2, -3, MIN + 1]));")]
#[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MAX, 2, -3, MIN + 1]));")]
/// ```
#[inline]
Expand Down
2 changes: 0 additions & 2 deletions crates/core_simd/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ impl_unsigned_int_ops! {
u16 => SimdU16;
u32 => SimdU32;
u64 => SimdU64;
u128 => SimdU128;
usize => SimdUsize;
}

Expand All @@ -662,7 +661,6 @@ impl_signed_int_ops! {
i16 => SimdI16;
i32 => SimdI32;
i64 => SimdI64;
i128 => SimdI128;
isize => SimdIsize;
}

Expand Down
17 changes: 0 additions & 17 deletions crates/core_simd/src/vector/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ from_transmute_x86! { unsafe isizex4 => __m256i }
//#[cfg(target_pointer_width = "64")]
//from_transmute_x86! { unsafe isizex8 => __m512i }

/// A SIMD vector of containing `LANES` `i128` values.
#[repr(simd)]
pub struct SimdI128<const LANES: usize>([i128; LANES])
where
Self: crate::LanesAtMost32;

impl_integer_vector! { SimdI128, i128, Mask128, SimdI128 }

from_transmute_x86! { unsafe i128x2 => __m256i }
//from_transmute_x86! { unsafe i128x4 => __m512i }

/// A SIMD vector of containing `LANES` `i16` values.
#[repr(simd)]
pub struct SimdI16<const LANES: usize>([i16; LANES])
Expand Down Expand Up @@ -132,12 +121,6 @@ pub type isizex4 = SimdIsize<4>;
/// Vector of eight `isize` values
pub type isizex8 = SimdIsize<8>;

/// Vector of two `i128` values
pub type i128x2 = SimdI128<2>;

/// Vector of four `i128` values
pub type i128x4 = SimdI128<4>;

/// Vector of four `i16` values
pub type i16x4 = SimdI16<4>;

Expand Down
17 changes: 0 additions & 17 deletions crates/core_simd/src/vector/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ from_transmute_x86! { unsafe usizex4 => __m256i }
//#[cfg(target_pointer_width = "64")]
//from_transmute_x86! { unsafe usizex8 => __m512i }

/// A SIMD vector of containing `LANES` `u128` values.
#[repr(simd)]
pub struct SimdU128<const LANES: usize>([u128; LANES])
where
Self: crate::LanesAtMost32;

impl_unsigned_vector! { SimdU128, u128 }

from_transmute_x86! { unsafe u128x2 => __m256i }
//from_transmute_x86! { unsafe u128x4 => __m512i }

/// A SIMD vector of containing `LANES` `u16` values.
#[repr(simd)]
pub struct SimdU16<const LANES: usize>([u16; LANES])
Expand Down Expand Up @@ -117,12 +106,6 @@ pub type usizex4 = SimdUsize<4>;
/// Vector of eight `usize` values
pub type usizex8 = SimdUsize<8>;

/// Vector of two `u128` values
pub type u128x2 = SimdU128<2>;

/// Vector of four `u128` values
pub type u128x4 = SimdU128<4>;

/// Vector of four `u16` values
pub type u16x4 = SimdU16<4>;

Expand Down
3 changes: 0 additions & 3 deletions crates/core_simd/tests/i128_ops.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/core_simd/tests/mask_ops_impl/mask128.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/core_simd/tests/mask_ops_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ mod mask8;
mod mask16;
mod mask32;
mod mask64;
mod mask128;
mod masksize;
3 changes: 0 additions & 3 deletions crates/core_simd/tests/u128_ops.rs

This file was deleted.

4 changes: 0 additions & 4 deletions crates/test_helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,11 @@ macro_rules! test_lanes {
core_simd::SimdU16<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdU32<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdU64<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdU128<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdUsize<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI8<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI16<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI32<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI64<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI128<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdIsize<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdF32<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdF64<$lanes>: core_simd::LanesAtMost32,
Expand Down Expand Up @@ -345,13 +343,11 @@ macro_rules! test_lanes_panic {
core_simd::SimdU16<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdU32<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdU64<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdU128<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdUsize<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI8<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI16<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI32<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI64<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdI128<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdIsize<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdF32<$lanes>: core_simd::LanesAtMost32,
core_simd::SimdF64<$lanes>: core_simd::LanesAtMost32,
Expand Down

0 comments on commit a9a1c9d

Please sign in to comment.