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

Commit 99c6572

Browse files
committed
Introduce generic abs and copysign
Add generic versions of `abs` and `copysign`, which will provide an entrypoint for adding `f16` and `f128`. Since this implementation is identical to the existing type-specific implementations, make use of it for `f32` and `f64`.
1 parent 8552890 commit 99c6572

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

src/math/copysign.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@
44
/// first argument, `x`, and the sign of its second argument, `y`.
55
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
66
pub fn copysign(x: f64, y: f64) -> f64 {
7-
let mut ux = x.to_bits();
8-
let uy = y.to_bits();
9-
ux &= (!0) >> 1;
10-
ux |= uy & (1 << 63);
11-
f64::from_bits(ux)
7+
super::generic::copysign(x, y)
128
}

src/math/copysignf.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@
44
/// first argument, `x`, and the sign of its second argument, `y`.
55
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
66
pub fn copysignf(x: f32, y: f32) -> f32 {
7-
let mut ux = x.to_bits();
8-
let uy = y.to_bits();
9-
ux &= 0x7fffffff;
10-
ux |= uy & 0x80000000;
11-
f32::from_bits(ux)
7+
super::generic::copysign(x, y)
128
}

src/math/fabs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn fabs(x: f64) -> f64 {
99
args: x,
1010
}
1111

12-
f64::from_bits(x.to_bits() & (u64::MAX / 2))
12+
super::generic::abs(x)
1313
}
1414

1515
#[cfg(test)]

src/math/fabsf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn fabsf(x: f32) -> f32 {
99
args: x,
1010
}
1111

12-
f32::from_bits(x.to_bits() & 0x7fffffff)
12+
super::generic::abs(x)
1313
}
1414

1515
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520

src/math/generic/abs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use super::super::Float;
2+
3+
/// Absolute value.
4+
pub fn abs<F: Float>(x: F) -> F {
5+
x.abs()
6+
}

src/math/generic/copysign.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use super::super::Float;
2+
3+
/// Copy the sign of `y` to `x`.
4+
pub fn copysign<F: Float>(x: F, y: F) -> F {
5+
let mut ux = x.to_bits();
6+
let uy = y.to_bits();
7+
ux &= !F::SIGN_MASK;
8+
ux |= uy & (F::SIGN_MASK);
9+
F::from_bits(ux)
10+
}

src/math/generic/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod abs;
2+
mod copysign;
3+
4+
pub use abs::abs;
5+
pub use copysign::copysign;

src/math/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ mod support;
8787
mod arch;
8888
mod expo2;
8989
mod fenv;
90+
mod generic;
9091
mod k_cos;
9192
mod k_cosf;
9293
mod k_expo2;

0 commit comments

Comments
 (0)