Skip to content

Commit

Permalink
Add f16 and f128 copysign and fabs
Browse files Browse the repository at this point in the history
Use the generic algorithm to make these two functions available whenever
`f16_enabled` or `f128_enabled` are true. These require the `unstable`
feature.
  • Loading branch information
tgross35 committed Oct 26, 2024
1 parent 00be5af commit 733b768
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/libm_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro_rules! libm_helper {
}
};

({$($func:tt);*}) => {
({$($func:tt;)*}) => {
$(
libm_helper! { $func }
)*
Expand Down Expand Up @@ -103,7 +103,7 @@ libm_helper! {
(fn trunc(x: f32) -> (f32); => truncf);
(fn y0(x: f32) -> (f32); => y0f);
(fn y1(x: f32) -> (f32); => y1f);
(fn yn(n: i32, x: f32) -> (f32); => ynf)
(fn yn(n: i32, x: f32) -> (f32); => ynf);
}
}

Expand Down Expand Up @@ -166,6 +166,24 @@ libm_helper! {
(fn trunc(x: f64) -> (f64); => trunc);
(fn y0(x: f64) -> (f64); => y0);
(fn y1(x: f64) -> (f64); => y1);
(fn yn(n: i32, x: f64) -> (f64); => yn)
(fn yn(n: i32, x: f64) -> (f64); => yn);
}
}

#[cfg(f16_enabled)]
libm_helper! {
f16,
funcs: {
(fn abs(x: f16) -> (f16); => fabsf16);
(fn copysign(x: f16, y: f16) -> (f16); => copysignf16);
}
}

#[cfg(f128_enabled)]
libm_helper! {
f128,
funcs: {
(fn abs(x: f128) -> (f128); => fabsf128);
(fn copysign(x: f128, y: f128) -> (f128); => copysignf128);
}
}
8 changes: 8 additions & 0 deletions src/math/copysignf128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Sign of Y, magnitude of X (f32)
///
/// Constructs a number with the magnitude (absolute value) of its
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysignf128(x: f128, y: f128) -> f128 {
super::generic::copysign::copysign(x, y)
}
8 changes: 8 additions & 0 deletions src/math/copysignf16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Sign of Y, magnitude of X (f32)
///
/// Constructs a number with the magnitude (absolute value) of its
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysignf16(x: f16, y: f16) -> f16 {
super::generic::copysign::copysign(x, y)
}
5 changes: 5 additions & 0 deletions src/math/fabsf128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Absolute value (magnitude) of a `f128` value.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fabsf128(x: f128) -> f128 {
super::generic::abs::abs(x)
}
5 changes: 5 additions & 0 deletions src/math/fabsf16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Absolute value (magnitude) of a `f16` value.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fabsf16(x: f16) -> f16 {
super::generic::abs::abs(x)
}
20 changes: 20 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,26 @@ pub use self::tgammaf::tgammaf;
pub use self::trunc::trunc;
pub use self::truncf::truncf;

cfg_if! {
if #[cfg(f16_enabled)] {
mod copysignf16;
mod fabsf16;

pub use self::fabsf16::fabsf16;
pub use self::copysignf16::copysignf16;
}
}

cfg_if! {
if #[cfg(f128_enabled)] {
mod copysignf128;
mod fabsf128;

pub use self::fabsf128::fabsf128;
pub use self::copysignf128::copysignf128;
}
}

// Private modules
mod expo2;
mod fenv;
Expand Down

0 comments on commit 733b768

Please sign in to comment.