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

Commit 87f0631

Browse files
committed
Add f16 and f128 copysign and fabs
Use the generic algorithm to make these two functions available whenever `f16_enabled` or `f128_enabled` are true. These require the `unstable` feature.
1 parent 1ed935d commit 87f0631

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

src/libm_helper.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! libm_helper {
3030
}
3131
};
3232

33-
({$($func:tt);*}) => {
33+
({$($func:tt;)*}) => {
3434
$(
3535
libm_helper! { $func }
3636
)*
@@ -103,7 +103,7 @@ libm_helper! {
103103
(fn trunc(x: f32) -> (f32); => truncf);
104104
(fn y0(x: f32) -> (f32); => y0f);
105105
(fn y1(x: f32) -> (f32); => y1f);
106-
(fn yn(n: i32, x: f32) -> (f32); => ynf)
106+
(fn yn(n: i32, x: f32) -> (f32); => ynf);
107107
}
108108
}
109109

@@ -166,6 +166,24 @@ libm_helper! {
166166
(fn trunc(x: f64) -> (f64); => trunc);
167167
(fn y0(x: f64) -> (f64); => y0);
168168
(fn y1(x: f64) -> (f64); => y1);
169-
(fn yn(n: i32, x: f64) -> (f64); => yn)
169+
(fn yn(n: i32, x: f64) -> (f64); => yn);
170+
}
171+
}
172+
173+
#[cfg(f16_enabled)]
174+
libm_helper! {
175+
f16,
176+
funcs: {
177+
(fn abs(x: f16) -> (f16); => fabsf16);
178+
(fn copysign(x: f16, y: f16) -> (f16); => copysignf16);
179+
}
180+
}
181+
182+
#[cfg(f128_enabled)]
183+
libm_helper! {
184+
f128,
185+
funcs: {
186+
(fn abs(x: f128) -> (f128); => fabsf128);
187+
(fn copysign(x: f128, y: f128) -> (f128); => copysignf128);
170188
}
171189
}

src/math/copysignf128.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Sign of Y, magnitude of X (f32)
2+
///
3+
/// Constructs a number with the magnitude (absolute value) of its
4+
/// first argument, `x`, and the sign of its second argument, `y`.
5+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6+
pub fn copysignf128(x: f128, y: f128) -> f128 {
7+
super::generic::copysign::copysign(x, y)
8+
}

src/math/copysignf16.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Sign of Y, magnitude of X (f32)
2+
///
3+
/// Constructs a number with the magnitude (absolute value) of its
4+
/// first argument, `x`, and the sign of its second argument, `y`.
5+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6+
pub fn copysignf16(x: f16, y: f16) -> f16 {
7+
super::generic::copysign::copysign(x, y)
8+
}

src/math/fabsf128.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Absolute value (magnitude) of a `f128` value.
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn fabsf128(x: f128) -> f128 {
4+
super::generic::abs::abs(x)
5+
}

src/math/fabsf16.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Absolute value (magnitude) of a `f16` value.
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn fabsf16(x: f16) -> f16 {
4+
super::generic::abs::abs(x)
5+
}

src/math/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,26 @@ pub use self::tgammaf::tgammaf;
385385
pub use self::trunc::trunc;
386386
pub use self::truncf::truncf;
387387

388+
cfg_if! {
389+
if #[cfg(f16_enabled)] {
390+
mod copysignf16;
391+
mod fabsf16;
392+
393+
pub use self::fabsf16::fabsf16;
394+
pub use self::copysignf16::copysignf16;
395+
}
396+
}
397+
398+
cfg_if! {
399+
if #[cfg(f128_enabled)] {
400+
mod copysignf128;
401+
mod fabsf128;
402+
403+
pub use self::fabsf128::fabsf128;
404+
pub use self::copysignf128::copysignf128;
405+
}
406+
}
407+
388408
// Private modules
389409
mod expo2;
390410
mod fenv;

0 commit comments

Comments
 (0)