Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Introduce generic abs and copysign #376

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions src/math/copysign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysign(x: f64, y: f64) -> f64 {
let mut ux = x.to_bits();
let uy = y.to_bits();
ux &= (!0) >> 1;
ux |= uy & (1 << 63);
f64::from_bits(ux)
super::generic::copysign(x, y)
}
6 changes: 1 addition & 5 deletions src/math/copysignf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysignf(x: f32, y: f32) -> f32 {
let mut ux = x.to_bits();
let uy = y.to_bits();
ux &= 0x7fffffff;
ux |= uy & 0x80000000;
f32::from_bits(ux)
super::generic::copysign(x, y)
}
2 changes: 1 addition & 1 deletion src/math/fabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn fabs(x: f64) -> f64 {
args: x,
}

f64::from_bits(x.to_bits() & (u64::MAX / 2))
super::generic::abs(x)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/math/fabsf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn fabsf(x: f32) -> f32 {
args: x,
}

f32::from_bits(x.to_bits() & 0x7fffffff)
super::generic::abs(x)
}

// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
Expand Down
6 changes: 6 additions & 0 deletions src/math/generic/abs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::super::Float;

/// Absolute value.
pub fn abs<F: Float>(x: F) -> F {
x.abs()
}
10 changes: 10 additions & 0 deletions src/math/generic/copysign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::super::Float;

/// Copy the sign of `y` to `x`.
pub fn copysign<F: Float>(x: F, y: F) -> F {
let mut ux = x.to_bits();
let uy = y.to_bits();
ux &= !F::SIGN_MASK;
ux |= uy & (F::SIGN_MASK);
F::from_bits(ux)
}
5 changes: 5 additions & 0 deletions src/math/generic/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod abs;
mod copysign;

pub use abs::abs;
pub use copysign::copysign;
1 change: 1 addition & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod support;
mod arch;
mod expo2;
mod fenv;
mod generic;
mod k_cos;
mod k_cosf;
mod k_expo2;
Expand Down