Merge pull request #86 from rust-lang/feat/saturating
Introduce saturating math
This commit is contained in:
commit
6620015a77
@ -62,4 +62,10 @@ extern "platform-intrinsic" {
|
||||
pub(crate) fn simd_shuffle16<T, U>(x: T, y: T, idx: [u32; 16]) -> U;
|
||||
pub(crate) fn simd_shuffle32<T, U>(x: T, y: T, idx: [u32; 32]) -> U;
|
||||
pub(crate) fn simd_shuffle64<T, U>(x: T, y: T, idx: [u32; 64]) -> U;
|
||||
|
||||
// {s,u}add.sat
|
||||
pub(crate) fn simd_saturating_add<T>(x: T, y: T) -> T;
|
||||
|
||||
// {s,u}sub.sat
|
||||
pub(crate) fn simd_saturating_sub<T>(x: T, y: T) -> T;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#![no_std]
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(repr_simd, platform_intrinsics, simd_ffi, const_generics)]
|
||||
#![feature(extended_key_value_attributes)]
|
||||
#![warn(missing_docs)]
|
||||
//! Portable SIMD module.
|
||||
|
||||
@ -16,6 +17,8 @@ mod intrinsics;
|
||||
mod ops;
|
||||
mod round;
|
||||
|
||||
mod math;
|
||||
|
||||
mod lanes_at_most_64;
|
||||
pub use lanes_at_most_64::LanesAtMost64;
|
||||
|
||||
|
88
crates/core_simd/src/math.rs
Normal file
88
crates/core_simd/src/math.rs
Normal file
@ -0,0 +1,88 @@
|
||||
macro_rules! impl_uint_arith {
|
||||
($(($name:ident, $n:ty)),+) => {
|
||||
$( impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 {
|
||||
|
||||
/// Lanewise saturating add.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use core_simd::*;
|
||||
#[doc = concat!("# use core::", stringify!($n), "::MAX;")]
|
||||
#[doc = concat!("let x = ", stringify!($name), "::from_array([2, 1, 0, MAX]);")]
|
||||
#[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")]
|
||||
/// let unsat = x + max;
|
||||
/// let sat = x.saturating_add(max);
|
||||
/// assert_eq!(x - 1, unsat);
|
||||
/// assert_eq!(sat, max);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn saturating_add(self, second: Self) -> Self {
|
||||
unsafe { crate::intrinsics::simd_saturating_add(self, second) }
|
||||
}
|
||||
|
||||
/// Lanewise saturating subtract.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use core_simd::*;
|
||||
#[doc = concat!("# use core::", stringify!($n), "::MAX;")]
|
||||
#[doc = concat!("let x = ", stringify!($name), "::from_array([2, 1, 0, MAX]);")]
|
||||
#[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")]
|
||||
/// let unsat = x - max;
|
||||
/// let sat = x.saturating_sub(max);
|
||||
/// assert_eq!(unsat, x + 1);
|
||||
#[doc = concat!("assert_eq!(sat, ", stringify!($name), "::splat(0));")]
|
||||
#[inline]
|
||||
pub fn saturating_sub(self, second: Self) -> Self {
|
||||
unsafe { crate::intrinsics::simd_saturating_sub(self, second) }
|
||||
}
|
||||
})+
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_int_arith {
|
||||
($(($name:ident, $n:ty)),+) => {
|
||||
$( impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost64 {
|
||||
|
||||
/// Lanewise saturating add.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use core_simd::*;
|
||||
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
|
||||
#[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, 0, 1, MAX]);")]
|
||||
#[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")]
|
||||
/// let unsat = x + max;
|
||||
/// let sat = x.saturating_add(max);
|
||||
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([-1, MAX, MIN, -2]));")]
|
||||
#[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([-1, MAX, MAX, MAX]));")]
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn saturating_add(self, second: Self) -> Self {
|
||||
unsafe { crate::intrinsics::simd_saturating_add(self, second) }
|
||||
}
|
||||
|
||||
/// Lanewise saturating subtract.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use core_simd::*;
|
||||
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
|
||||
#[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, -2, -1, MAX]);")]
|
||||
#[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")]
|
||||
/// let unsat = x - max;
|
||||
/// let sat = x.saturating_sub(max);
|
||||
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([1, MAX, MIN, 0]));")]
|
||||
#[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MIN, MIN, MIN, 0]));")]
|
||||
#[inline]
|
||||
pub fn saturating_sub(self, second: Self) -> Self {
|
||||
unsafe { crate::intrinsics::simd_saturating_sub(self, second) }
|
||||
}
|
||||
})+
|
||||
}
|
||||
}
|
||||
|
||||
use crate::vector::*;
|
||||
|
||||
impl_uint_arith! { (SimdU8, u8), (SimdU16, u16), (SimdU32, u32), (SimdU64, u64), (SimdUsize, usize) }
|
||||
impl_int_arith! { (SimdI8, i8), (SimdI16, i16), (SimdI32, i32), (SimdI64, i64), (SimdIsize, isize) }
|
Loading…
x
Reference in New Issue
Block a user