compiler_builtins\libm\src\math/
fminimum_fmaximum_num.rs

1/// Return the lesser of two arguments or, if either argument is NaN, NaN.
2///
3/// This coincides with IEEE 754-2019 `minimumNumber`. The result orders -0.0 < 0.0.
4#[cfg(f16_enabled)]
5#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6pub fn fminimum_numf16(x: f16, y: f16) -> f16 {
7    super::generic::fminimum_num(x, y)
8}
9
10/// Return the lesser of two arguments or, if either argument is NaN, NaN.
11///
12/// This coincides with IEEE 754-2019 `minimumNumber`. The result orders -0.0 < 0.0.
13#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
14pub fn fminimum_numf(x: f32, y: f32) -> f32 {
15    super::generic::fminimum_num(x, y)
16}
17
18/// Return the lesser of two arguments or, if either argument is NaN, NaN.
19///
20/// This coincides with IEEE 754-2019 `minimumNumber`. The result orders -0.0 < 0.0.
21#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
22pub fn fminimum_num(x: f64, y: f64) -> f64 {
23    super::generic::fminimum_num(x, y)
24}
25
26/// Return the lesser of two arguments or, if either argument is NaN, NaN.
27///
28/// This coincides with IEEE 754-2019 `minimumNumber`. The result orders -0.0 < 0.0.
29#[cfg(f128_enabled)]
30#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
31pub fn fminimum_numf128(x: f128, y: f128) -> f128 {
32    super::generic::fminimum_num(x, y)
33}
34
35/// Return the greater of two arguments or, if either argument is NaN, NaN.
36///
37/// This coincides with IEEE 754-2019 `maximumNumber`. The result orders -0.0 < 0.0.
38#[cfg(f16_enabled)]
39#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
40pub fn fmaximum_numf16(x: f16, y: f16) -> f16 {
41    super::generic::fmaximum_num(x, y)
42}
43
44/// Return the greater of two arguments or, if either argument is NaN, NaN.
45///
46/// This coincides with IEEE 754-2019 `maximumNumber`. The result orders -0.0 < 0.0.
47#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
48pub fn fmaximum_numf(x: f32, y: f32) -> f32 {
49    super::generic::fmaximum_num(x, y)
50}
51
52/// Return the greater of two arguments or, if either argument is NaN, NaN.
53///
54/// This coincides with IEEE 754-2019 `maximumNumber`. The result orders -0.0 < 0.0.
55#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
56pub fn fmaximum_num(x: f64, y: f64) -> f64 {
57    super::generic::fmaximum_num(x, y)
58}
59
60/// Return the greater of two arguments or, if either argument is NaN, NaN.
61///
62/// This coincides with IEEE 754-2019 `maximumNumber`. The result orders -0.0 < 0.0.
63#[cfg(f128_enabled)]
64#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
65pub fn fmaximum_numf128(x: f128, y: f128) -> f128 {
66    super::generic::fmaximum_num(x, y)
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use crate::support::{Float, Hexf};
73
74    fn fminimum_num_spec_test<F: Float>(f: impl Fn(F, F) -> F) {
75        let cases = [
76            (F::ZERO, F::ZERO, F::ZERO),
77            (F::ZERO, F::NEG_ZERO, F::NEG_ZERO),
78            (F::ZERO, F::ONE, F::ZERO),
79            (F::ZERO, F::NEG_ONE, F::NEG_ONE),
80            (F::ZERO, F::INFINITY, F::ZERO),
81            (F::ZERO, F::NEG_INFINITY, F::NEG_INFINITY),
82            (F::ZERO, F::NAN, F::ZERO),
83            (F::ZERO, F::NEG_NAN, F::ZERO),
84            (F::NEG_ZERO, F::ZERO, F::NEG_ZERO),
85            (F::NEG_ZERO, F::NEG_ZERO, F::NEG_ZERO),
86            (F::NEG_ZERO, F::ONE, F::NEG_ZERO),
87            (F::NEG_ZERO, F::NEG_ONE, F::NEG_ONE),
88            (F::NEG_ZERO, F::INFINITY, F::NEG_ZERO),
89            (F::NEG_ZERO, F::NEG_INFINITY, F::NEG_INFINITY),
90            (F::NEG_ZERO, F::NAN, F::NEG_ZERO),
91            (F::NEG_ZERO, F::NEG_NAN, F::NEG_ZERO),
92            (F::ONE, F::ZERO, F::ZERO),
93            (F::ONE, F::NEG_ZERO, F::NEG_ZERO),
94            (F::ONE, F::ONE, F::ONE),
95            (F::ONE, F::NEG_ONE, F::NEG_ONE),
96            (F::ONE, F::INFINITY, F::ONE),
97            (F::ONE, F::NEG_INFINITY, F::NEG_INFINITY),
98            (F::ONE, F::NAN, F::ONE),
99            (F::ONE, F::NEG_NAN, F::ONE),
100            (F::NEG_ONE, F::ZERO, F::NEG_ONE),
101            (F::NEG_ONE, F::NEG_ZERO, F::NEG_ONE),
102            (F::NEG_ONE, F::ONE, F::NEG_ONE),
103            (F::NEG_ONE, F::NEG_ONE, F::NEG_ONE),
104            (F::NEG_ONE, F::INFINITY, F::NEG_ONE),
105            (F::NEG_ONE, F::NEG_INFINITY, F::NEG_INFINITY),
106            (F::NEG_ONE, F::NAN, F::NEG_ONE),
107            (F::NEG_ONE, F::NEG_NAN, F::NEG_ONE),
108            (F::INFINITY, F::ZERO, F::ZERO),
109            (F::INFINITY, F::NEG_ZERO, F::NEG_ZERO),
110            (F::INFINITY, F::ONE, F::ONE),
111            (F::INFINITY, F::NEG_ONE, F::NEG_ONE),
112            (F::INFINITY, F::INFINITY, F::INFINITY),
113            (F::INFINITY, F::NEG_INFINITY, F::NEG_INFINITY),
114            (F::INFINITY, F::NAN, F::INFINITY),
115            (F::INFINITY, F::NEG_NAN, F::INFINITY),
116            (F::NEG_INFINITY, F::ZERO, F::NEG_INFINITY),
117            (F::NEG_INFINITY, F::NEG_ZERO, F::NEG_INFINITY),
118            (F::NEG_INFINITY, F::ONE, F::NEG_INFINITY),
119            (F::NEG_INFINITY, F::NEG_ONE, F::NEG_INFINITY),
120            (F::NEG_INFINITY, F::INFINITY, F::NEG_INFINITY),
121            (F::NEG_INFINITY, F::NEG_INFINITY, F::NEG_INFINITY),
122            (F::NEG_INFINITY, F::NAN, F::NEG_INFINITY),
123            (F::NEG_INFINITY, F::NEG_NAN, F::NEG_INFINITY),
124            (F::NAN, F::ZERO, F::ZERO),
125            (F::NAN, F::NEG_ZERO, F::NEG_ZERO),
126            (F::NAN, F::ONE, F::ONE),
127            (F::NAN, F::NEG_ONE, F::NEG_ONE),
128            (F::NAN, F::INFINITY, F::INFINITY),
129            (F::NAN, F::NEG_INFINITY, F::NEG_INFINITY),
130            (F::NAN, F::NAN, F::NAN),
131            (F::NEG_NAN, F::ZERO, F::ZERO),
132            (F::NEG_NAN, F::NEG_ZERO, F::NEG_ZERO),
133            (F::NEG_NAN, F::ONE, F::ONE),
134            (F::NEG_NAN, F::NEG_ONE, F::NEG_ONE),
135            (F::NEG_NAN, F::INFINITY, F::INFINITY),
136            (F::NEG_NAN, F::NEG_INFINITY, F::NEG_INFINITY),
137        ];
138
139        for (x, y, expected) in cases {
140            let actual = f(x, y);
141            assert_biteq!(actual, expected, "fminimum_num({}, {})", Hexf(x), Hexf(y));
142        }
143
144        // Ordering between NaNs does not matter
145        assert!(f(F::NAN, F::NEG_NAN).is_nan());
146        assert!(f(F::NEG_NAN, F::NAN).is_nan());
147        assert!(f(F::NEG_NAN, F::NEG_NAN).is_nan());
148    }
149
150    #[test]
151    #[cfg(f16_enabled)]
152    fn fminimum_num_spec_tests_f16() {
153        fminimum_num_spec_test::<f16>(fminimum_numf16);
154    }
155
156    #[test]
157    fn fminimum_num_spec_tests_f32() {
158        fminimum_num_spec_test::<f32>(fminimum_numf);
159    }
160
161    #[test]
162    fn fminimum_num_spec_tests_f64() {
163        fminimum_num_spec_test::<f64>(fminimum_num);
164    }
165
166    #[test]
167    #[cfg(f128_enabled)]
168    fn fminimum_num_spec_tests_f128() {
169        fminimum_num_spec_test::<f128>(fminimum_numf128);
170    }
171
172    fn fmaximum_num_spec_test<F: Float>(f: impl Fn(F, F) -> F) {
173        let cases = [
174            (F::ZERO, F::ZERO, F::ZERO),
175            (F::ZERO, F::NEG_ZERO, F::ZERO),
176            (F::ZERO, F::ONE, F::ONE),
177            (F::ZERO, F::NEG_ONE, F::ZERO),
178            (F::ZERO, F::INFINITY, F::INFINITY),
179            (F::ZERO, F::NEG_INFINITY, F::ZERO),
180            (F::ZERO, F::NAN, F::ZERO),
181            (F::ZERO, F::NEG_NAN, F::ZERO),
182            (F::NEG_ZERO, F::ZERO, F::ZERO),
183            (F::NEG_ZERO, F::NEG_ZERO, F::NEG_ZERO),
184            (F::NEG_ZERO, F::ONE, F::ONE),
185            (F::NEG_ZERO, F::NEG_ONE, F::NEG_ZERO),
186            (F::NEG_ZERO, F::INFINITY, F::INFINITY),
187            (F::NEG_ZERO, F::NEG_INFINITY, F::NEG_ZERO),
188            (F::NEG_ZERO, F::NAN, F::NEG_ZERO),
189            (F::NEG_ZERO, F::NEG_NAN, F::NEG_ZERO),
190            (F::ONE, F::ZERO, F::ONE),
191            (F::ONE, F::NEG_ZERO, F::ONE),
192            (F::ONE, F::ONE, F::ONE),
193            (F::ONE, F::NEG_ONE, F::ONE),
194            (F::ONE, F::INFINITY, F::INFINITY),
195            (F::ONE, F::NEG_INFINITY, F::ONE),
196            (F::ONE, F::NAN, F::ONE),
197            (F::ONE, F::NEG_NAN, F::ONE),
198            (F::NEG_ONE, F::ZERO, F::ZERO),
199            (F::NEG_ONE, F::NEG_ZERO, F::NEG_ZERO),
200            (F::NEG_ONE, F::ONE, F::ONE),
201            (F::NEG_ONE, F::NEG_ONE, F::NEG_ONE),
202            (F::NEG_ONE, F::INFINITY, F::INFINITY),
203            (F::NEG_ONE, F::NEG_INFINITY, F::NEG_ONE),
204            (F::NEG_ONE, F::NAN, F::NEG_ONE),
205            (F::NEG_ONE, F::NEG_NAN, F::NEG_ONE),
206            (F::INFINITY, F::ZERO, F::INFINITY),
207            (F::INFINITY, F::NEG_ZERO, F::INFINITY),
208            (F::INFINITY, F::ONE, F::INFINITY),
209            (F::INFINITY, F::NEG_ONE, F::INFINITY),
210            (F::INFINITY, F::INFINITY, F::INFINITY),
211            (F::INFINITY, F::NEG_INFINITY, F::INFINITY),
212            (F::INFINITY, F::NAN, F::INFINITY),
213            (F::INFINITY, F::NEG_NAN, F::INFINITY),
214            (F::NEG_INFINITY, F::ZERO, F::ZERO),
215            (F::NEG_INFINITY, F::NEG_ZERO, F::NEG_ZERO),
216            (F::NEG_INFINITY, F::ONE, F::ONE),
217            (F::NEG_INFINITY, F::NEG_ONE, F::NEG_ONE),
218            (F::NEG_INFINITY, F::INFINITY, F::INFINITY),
219            (F::NEG_INFINITY, F::NEG_INFINITY, F::NEG_INFINITY),
220            (F::NEG_INFINITY, F::NAN, F::NEG_INFINITY),
221            (F::NEG_INFINITY, F::NEG_NAN, F::NEG_INFINITY),
222            (F::NAN, F::ZERO, F::ZERO),
223            (F::NAN, F::NEG_ZERO, F::NEG_ZERO),
224            (F::NAN, F::ONE, F::ONE),
225            (F::NAN, F::NEG_ONE, F::NEG_ONE),
226            (F::NAN, F::INFINITY, F::INFINITY),
227            (F::NAN, F::NEG_INFINITY, F::NEG_INFINITY),
228            (F::NAN, F::NAN, F::NAN),
229            (F::NEG_NAN, F::ZERO, F::ZERO),
230            (F::NEG_NAN, F::NEG_ZERO, F::NEG_ZERO),
231            (F::NEG_NAN, F::ONE, F::ONE),
232            (F::NEG_NAN, F::NEG_ONE, F::NEG_ONE),
233            (F::NEG_NAN, F::INFINITY, F::INFINITY),
234            (F::NEG_NAN, F::NEG_INFINITY, F::NEG_INFINITY),
235        ];
236
237        for (x, y, expected) in cases {
238            let actual = f(x, y);
239            assert_biteq!(actual, expected, "fmaximum_num({}, {})", Hexf(x), Hexf(y));
240        }
241
242        // Ordering between NaNs does not matter
243        assert!(f(F::NAN, F::NEG_NAN).is_nan());
244        assert!(f(F::NEG_NAN, F::NAN).is_nan());
245        assert!(f(F::NEG_NAN, F::NEG_NAN).is_nan());
246    }
247
248    #[test]
249    #[cfg(f16_enabled)]
250    fn fmaximum_num_spec_tests_f16() {
251        fmaximum_num_spec_test::<f16>(fmaximum_numf16);
252    }
253
254    #[test]
255    fn fmaximum_num_spec_tests_f32() {
256        fmaximum_num_spec_test::<f32>(fmaximum_numf);
257    }
258
259    #[test]
260    fn fmaximum_num_spec_tests_f64() {
261        fmaximum_num_spec_test::<f64>(fmaximum_num);
262    }
263
264    #[test]
265    #[cfg(f128_enabled)]
266    fn fmaximum_num_spec_tests_f128() {
267        fmaximum_num_spec_test::<f128>(fmaximum_numf128);
268    }
269}