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

Commit c34bb5e

Browse files
committed
Introduce generic abs and copysign
Add generic versions of `abs` and `copysign`. Make use of it for the `f32` and `f64` versions of these functions.
1 parent f244032 commit c34bb5e

File tree

8 files changed

+21
-12
lines changed

8 files changed

+21
-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::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::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::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::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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use super::super::Float;
2+
3+
pub fn abs<F: Float>(x: F) -> F {
4+
x.abs()
5+
}

src/math/generic/copysign.rs

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

src/math/generic/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod abs;
2+
pub mod 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)