Skip to content

Ops: Add Saturating ops for floating point numbers #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ops/saturating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ macro_rules! saturating_impl {
};
}

macro_rules! float_saturating_impl {
Copy link
Author

@Dietr1ch Dietr1ch May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is not specific to floats, but it doesn't seem to be useful for other types yet and it requires that the type is naturally saturating.

($trait_name:ident, $method_name:ident, $t:ty, $method:ident) => {
impl $trait_name for $t {
#[inline]
fn $method_name(&self, v: &Self) -> Self {
self.$method(v)
}
}
};
}

/// Performs addition that saturates at the numeric bounds instead of overflowing.
pub trait SaturatingAdd: Sized + Add<Self, Output = Self> {
/// Saturating addition. Computes `self + other`, saturating at the relevant high or low boundary of
Expand All @@ -63,6 +74,9 @@ saturating_impl!(SaturatingAdd, saturating_add, i64);
saturating_impl!(SaturatingAdd, saturating_add, isize);
saturating_impl!(SaturatingAdd, saturating_add, i128);

float_saturating_impl!(SaturatingAdd, saturating_add, f32, add);
float_saturating_impl!(SaturatingAdd, saturating_add, f64, add);

/// Performs subtraction that saturates at the numeric bounds instead of overflowing.
pub trait SaturatingSub: Sized + Sub<Self, Output = Self> {
/// Saturating subtraction. Computes `self - other`, saturating at the relevant high or low boundary of
Expand All @@ -84,6 +98,9 @@ saturating_impl!(SaturatingSub, saturating_sub, i64);
saturating_impl!(SaturatingSub, saturating_sub, isize);
saturating_impl!(SaturatingSub, saturating_sub, i128);

float_saturating_impl!(SaturatingSub, saturating_sub, f32, sub);
float_saturating_impl!(SaturatingSub, saturating_sub, f64, sub);

/// Performs multiplication that saturates at the numeric bounds instead of overflowing.
pub trait SaturatingMul: Sized + Mul<Self, Output = Self> {
/// Saturating multiplication. Computes `self * other`, saturating at the relevant high or low boundary of
Expand All @@ -105,6 +122,9 @@ saturating_impl!(SaturatingMul, saturating_mul, i64);
saturating_impl!(SaturatingMul, saturating_mul, isize);
saturating_impl!(SaturatingMul, saturating_mul, i128);

float_saturating_impl!(SaturatingMul, saturating_mul, f32, mul);
float_saturating_impl!(SaturatingMul, saturating_mul, f64, mul);

// TODO: add SaturatingNeg for signed integer primitives once the saturating_neg() API is stable.

#[test]
Expand All @@ -118,13 +138,21 @@ fn test_saturating_traits() {
fn saturating_mul<T: SaturatingMul>(a: T, b: T) -> T {
a.saturating_mul(&b)
}

const INF: f32 = f32::INFINITY;
assert_eq!(saturating_add(255, 1), 255u8);
assert_eq!(saturating_add(127, 1), 127i8);
assert_eq!(saturating_add(-128, -1), -128i8);
assert_eq!(saturating_add(100.0, INF), INF);

assert_eq!(saturating_sub(0, 1), 0u8);
assert_eq!(saturating_sub(-128, 1), -128i8);
assert_eq!(saturating_sub(127, -1), 127i8);
assert_eq!(saturating_sub(100.0, INF), -INF);

assert_eq!(saturating_mul(255, 2), 255u8);
assert_eq!(saturating_mul(127, 2), 127i8);
assert_eq!(saturating_mul(-128, 2), -128i8);
assert_eq!(saturating_mul(100.0, INF), INF);
assert_eq!(saturating_mul(-100.0, INF), -INF);
}