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

Commit 2c0ecd0

Browse files
committed
Add fabsf16, fabsf128, copysignf16, and copysignf128
Use the generic implementations to provide these simple methods.
1 parent 1b35730 commit 2c0ecd0

File tree

8 files changed

+133
-3
lines changed

8 files changed

+133
-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 copysign(x: f16, y: f16) -> (f16); => copysignf16);
178+
(fn fabs(x: f16) -> (f16); => fabsf16);
179+
}
180+
}
181+
182+
#[cfg(f128_enabled)]
183+
libm_helper! {
184+
f128,
185+
funcs: {
186+
(fn copysign(x: f128, y: f128) -> (f128); => copysignf128);
187+
(fn fabs(x: f128) -> (f128); => fabsf128);
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 (f128)
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(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 (f16)
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(x, y)
8+
}

src/math/fabs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// Absolute value (magnitude) (f64)
2+
///
23
/// Calculates the absolute value (magnitude) of the argument `x`,
34
/// by direct manipulation of the bit representation of `x`.
45
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]

src/math/fabsf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// Absolute value (magnitude) (f32)
2+
///
23
/// Calculates the absolute value (magnitude) of the argument `x`,
34
/// by direct manipulation of the bit representation of `x`.
45
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]

src/math/fabsf128.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Absolute value (magnitude) (f128)
2+
///
3+
/// Calculates the absolute value (magnitude) of the argument `x`,
4+
/// by direct manipulation of the bit representation of `x`.
5+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6+
pub fn fabsf128(x: f128) -> f128 {
7+
select_implementation! {
8+
name: fabsf,
9+
use_intrinsic: target_arch = "wasm32",
10+
args: x,
11+
}
12+
13+
super::generic::fabs(x)
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn sanity_check() {
22+
assert_eq!(fabsf128(-1.0), 1.0);
23+
assert_eq!(fabsf128(2.8), 2.8);
24+
}
25+
26+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
27+
#[test]
28+
fn spec_tests() {
29+
assert!(fabsf128(f128::NAN).is_nan());
30+
for f in [0.0, -0.0].iter().copied() {
31+
assert_eq!(fabsf128(f), 0.0);
32+
}
33+
for f in [f128::INFINITY, f128::NEG_INFINITY].iter().copied() {
34+
assert_eq!(fabsf128(f), f128::INFINITY);
35+
}
36+
}
37+
}

src/math/fabsf16.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Absolute value (magnitude) (f16)
2+
///
3+
/// Calculates the absolute value (magnitude) of the argument `x`,
4+
/// by direct manipulation of the bit representation of `x`.
5+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6+
pub fn fabsf16(x: f16) -> f16 {
7+
select_implementation! {
8+
name: fabsf,
9+
use_intrinsic: target_arch = "wasm32",
10+
args: x,
11+
}
12+
13+
super::generic::fabs(x)
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn sanity_check() {
22+
assert_eq!(fabsf16(-1.0), 1.0);
23+
assert_eq!(fabsf16(2.8), 2.8);
24+
}
25+
26+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
27+
#[test]
28+
fn spec_tests() {
29+
assert!(fabsf16(f16::NAN).is_nan());
30+
for f in [0.0, -0.0].iter().copied() {
31+
assert_eq!(fabsf16(f), 0.0);
32+
}
33+
for f in [f16::INFINITY, f16::NEG_INFINITY].iter().copied() {
34+
assert_eq!(fabsf16(f), f16::INFINITY);
35+
}
36+
}
37+
}

src/math/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,26 @@ pub use self::tgammaf::tgammaf;
339339
pub use self::trunc::trunc;
340340
pub use self::truncf::truncf;
341341

342+
cfg_if! {
343+
if #[cfg(f16_enabled)] {
344+
mod copysignf16;
345+
mod fabsf16;
346+
347+
pub use self::copysignf16::copysignf16;
348+
pub use self::fabsf16::fabsf16;
349+
}
350+
}
351+
352+
cfg_if! {
353+
if #[cfg(f128_enabled)] {
354+
mod copysignf128;
355+
mod fabsf128;
356+
357+
pub use self::copysignf128::copysignf128;
358+
pub use self::fabsf128::fabsf128;
359+
}
360+
}
361+
342362
#[inline]
343363
fn get_high_word(x: f64) -> u32 {
344364
(x.to_bits() >> 32) as u32

0 commit comments

Comments
 (0)