Skip to content

Commit

Permalink
Add round to the Float trait instead
Browse files Browse the repository at this point in the history
  • Loading branch information
CryZe committed Aug 10, 2019
1 parent b2a1271 commit 43d2e51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
8 changes: 8 additions & 0 deletions palette/src/float.rs
Expand Up @@ -57,6 +57,8 @@ mod no_std_float_trait {
/// `y.atan2(x)` computes the inverse tangent of `y / x`, in the
/// corresponding quadrant
fn atan2(self, other: Self) -> Self;
/// `x.round()` returns the nearest integer to a number. Round half-way cases away from 0.0.
fn round(self) -> Self;
}

impl Float for f32 {
Expand All @@ -78,6 +80,9 @@ mod no_std_float_trait {
fn atan2(self, other: f32) -> f32 {
F32Ext::atan2(self, other)
}
fn round(self) -> f32 {
F32Ext::round(self)
}
}

impl Float for f64 {
Expand All @@ -99,5 +104,8 @@ mod no_std_float_trait {
fn atan2(self, other: f64) -> f64 {
F64Ext::atan2(self, other)
}
fn round(self) -> f64 {
F64Ext::round(self)
}
}
}
32 changes: 6 additions & 26 deletions palette/src/lib.rs
Expand Up @@ -377,26 +377,6 @@ pub mod white_point;

pub mod float;

#[cfg(any(feature = "std", test))]
fn round_f32(x: f32) -> f32 {
x.round()
}

#[cfg(not(any(feature = "std", test)))]
fn round_f32(x: f32) -> f32 {
libm::F32Ext::round(x)
}

#[cfg(any(feature = "std", test))]
fn round_f64(x: f64) -> f64 {
x.round()
}

#[cfg(not(any(feature = "std", test)))]
fn round_f64(x: f64) -> f64 {
libm::F64Ext::round(x)
}

fn clamp<T: PartialOrd>(v: T, min: T, max: T) -> T {
if v < min {
min
Expand Down Expand Up @@ -573,7 +553,7 @@ impl Component for f32 {
let scaled = *self * cast::<f32, _>(T::max_intensity());

if T::LIMITED {
cast(clamp(round_f32(scaled), 0.0, cast(T::max_intensity())))
cast(clamp(Float::round(scaled), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -591,7 +571,7 @@ impl Component for f64 {
let scaled = *self * cast::<f64, _>(T::max_intensity());

if T::LIMITED {
cast(clamp(round_f64(scaled), 0.0, cast(T::max_intensity())))
cast(clamp(Float::round(scaled), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -610,7 +590,7 @@ impl Component for u8 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(round_f64(scaled), 0.0, cast(T::max_intensity())))
cast(clamp(Float::round(scaled), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -629,7 +609,7 @@ impl Component for u16 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(round_f64(scaled), 0.0, cast(T::max_intensity())))
cast(clamp(Float::round(scaled), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -648,7 +628,7 @@ impl Component for u32 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(round_f64(scaled), 0.0, cast(T::max_intensity())))
cast(clamp(Float::round(scaled), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -667,7 +647,7 @@ impl Component for u64 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(round_f64(scaled), 0.0, cast(T::max_intensity())))
cast(clamp(Float::round(scaled), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand Down

0 comments on commit 43d2e51

Please sign in to comment.