compiler_builtins\libm\src\math/
copysign.rs

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(f16_enabled)]
6#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
7pub fn copysignf16(x: f16, y: f16) -> f16 {
8    super::generic::copysign(x, y)
9}
10
11/// Sign of Y, magnitude of X (f32)
12///
13/// Constructs a number with the magnitude (absolute value) of its
14/// first argument, `x`, and the sign of its second argument, `y`.
15#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
16pub fn copysignf(x: f32, y: f32) -> f32 {
17    super::generic::copysign(x, y)
18}
19
20/// Sign of Y, magnitude of X (f64)
21///
22/// Constructs a number with the magnitude (absolute value) of its
23/// first argument, `x`, and the sign of its second argument, `y`.
24#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
25pub fn copysign(x: f64, y: f64) -> f64 {
26    super::generic::copysign(x, y)
27}
28
29/// Sign of Y, magnitude of X (f128)
30///
31/// Constructs a number with the magnitude (absolute value) of its
32/// first argument, `x`, and the sign of its second argument, `y`.
33#[cfg(f128_enabled)]
34#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
35pub fn copysignf128(x: f128, y: f128) -> f128 {
36    super::generic::copysign(x, y)
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::support::Float;
43
44    fn spec_test<F: Float>(f: impl Fn(F, F) -> F) {
45        assert_biteq!(f(F::ZERO, F::ZERO), F::ZERO);
46        assert_biteq!(f(F::NEG_ZERO, F::ZERO), F::ZERO);
47        assert_biteq!(f(F::ZERO, F::NEG_ZERO), F::NEG_ZERO);
48        assert_biteq!(f(F::NEG_ZERO, F::NEG_ZERO), F::NEG_ZERO);
49
50        assert_biteq!(f(F::ONE, F::ONE), F::ONE);
51        assert_biteq!(f(F::NEG_ONE, F::ONE), F::ONE);
52        assert_biteq!(f(F::ONE, F::NEG_ONE), F::NEG_ONE);
53        assert_biteq!(f(F::NEG_ONE, F::NEG_ONE), F::NEG_ONE);
54
55        assert_biteq!(f(F::INFINITY, F::INFINITY), F::INFINITY);
56        assert_biteq!(f(F::NEG_INFINITY, F::INFINITY), F::INFINITY);
57        assert_biteq!(f(F::INFINITY, F::NEG_INFINITY), F::NEG_INFINITY);
58        assert_biteq!(f(F::NEG_INFINITY, F::NEG_INFINITY), F::NEG_INFINITY);
59
60        // Not required but we expect it
61        assert_biteq!(f(F::NAN, F::NAN), F::NAN);
62        assert_biteq!(f(F::NAN, F::ONE), F::NAN);
63        assert_biteq!(f(F::NAN, F::NEG_ONE), F::NEG_NAN);
64        assert_biteq!(f(F::NAN, F::NEG_NAN), F::NEG_NAN);
65        assert_biteq!(f(F::NEG_NAN, F::NAN), F::NAN);
66        assert_biteq!(f(F::NEG_NAN, F::ONE), F::NAN);
67        assert_biteq!(f(F::NEG_NAN, F::NEG_ONE), F::NEG_NAN);
68        assert_biteq!(f(F::NEG_NAN, F::NEG_NAN), F::NEG_NAN);
69        assert_biteq!(f(F::ONE, F::NAN), F::ONE);
70        assert_biteq!(f(F::ONE, F::NEG_NAN), F::NEG_ONE);
71        assert_biteq!(f(F::NEG_ONE, F::NAN), F::ONE);
72        assert_biteq!(f(F::NEG_ONE, F::NEG_NAN), F::NEG_ONE);
73    }
74
75    #[test]
76    #[cfg(f16_enabled)]
77    fn spec_tests_f16() {
78        spec_test::<f16>(copysignf16);
79    }
80
81    #[test]
82    fn spec_tests_f32() {
83        spec_test::<f32>(copysignf);
84    }
85
86    #[test]
87    fn spec_tests_f64() {
88        spec_test::<f64>(copysign);
89    }
90
91    #[test]
92    #[cfg(f128_enabled)]
93    fn spec_tests_f128() {
94        spec_test::<f128>(copysignf128);
95    }
96}