Skip to content

Commit bf726ae

Browse files
committed
Consolidate test_next_up
Note that the behaviour of the f128 test is slightly changed to use the same nan mask as is used in test_float_bits_conv, which is the behaviour used by f16,f32,and f64.
1 parent 87bdc5d commit bf726ae

File tree

5 files changed

+68
-119
lines changed

5 files changed

+68
-119
lines changed

library/coretests/tests/floats/f128.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555;
3939
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
4040
// the intrinsics.
4141

42-
#[test]
43-
fn test_next_up() {
44-
let tiny = f128::from_bits(TINY_BITS);
45-
let tiny_up = f128::from_bits(TINY_UP_BITS);
46-
let max_down = f128::from_bits(MAX_DOWN_BITS);
47-
let largest_subnormal = f128::from_bits(LARGEST_SUBNORMAL_BITS);
48-
let smallest_normal = f128::from_bits(SMALLEST_NORMAL_BITS);
49-
assert_biteq!(f128::NEG_INFINITY.next_up(), f128::MIN);
50-
assert_biteq!(f128::MIN.next_up(), -max_down);
51-
assert_biteq!((-1.0 - f128::EPSILON).next_up(), -1.0f128);
52-
assert_biteq!((-smallest_normal).next_up(), -largest_subnormal);
53-
assert_biteq!((-tiny_up).next_up(), -tiny);
54-
assert_biteq!((-tiny).next_up(), -0.0f128);
55-
assert_biteq!((-0.0f128).next_up(), tiny);
56-
assert_biteq!(0.0f128.next_up(), tiny);
57-
assert_biteq!(tiny.next_up(), tiny_up);
58-
assert_biteq!(largest_subnormal.next_up(), smallest_normal);
59-
assert_biteq!(1.0f128.next_up(), 1.0 + f128::EPSILON);
60-
assert_biteq!(f128::MAX.next_up(), f128::INFINITY);
61-
assert_biteq!(f128::INFINITY.next_up(), f128::INFINITY);
62-
63-
// Check that NaNs roundtrip.
64-
let nan0 = f128::NAN;
65-
let nan1 = f128::from_bits(f128::NAN.to_bits() ^ 0x002a_aaaa);
66-
let nan2 = f128::from_bits(f128::NAN.to_bits() ^ 0x0055_5555);
67-
assert_biteq!(nan0.next_up(), nan0);
68-
assert_biteq!(nan1.next_up(), nan1);
69-
assert_biteq!(nan2.next_up(), nan2);
70-
}
71-
7242
#[test]
7343
fn test_next_down() {
7444
let tiny = f128::from_bits(TINY_BITS);

library/coretests/tests/floats/f16.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,6 @@ const NAN_MASK2: u16 = 0x0155;
4545
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
4646
// the intrinsics.
4747

48-
#[test]
49-
fn test_next_up() {
50-
let tiny = f16::from_bits(TINY_BITS);
51-
let tiny_up = f16::from_bits(TINY_UP_BITS);
52-
let max_down = f16::from_bits(MAX_DOWN_BITS);
53-
let largest_subnormal = f16::from_bits(LARGEST_SUBNORMAL_BITS);
54-
let smallest_normal = f16::from_bits(SMALLEST_NORMAL_BITS);
55-
assert_biteq!(f16::NEG_INFINITY.next_up(), f16::MIN);
56-
assert_biteq!(f16::MIN.next_up(), -max_down);
57-
assert_biteq!((-1.0 - f16::EPSILON).next_up(), -1.0f16);
58-
assert_biteq!((-smallest_normal).next_up(), -largest_subnormal);
59-
assert_biteq!((-tiny_up).next_up(), -tiny);
60-
assert_biteq!((-tiny).next_up(), -0.0f16);
61-
assert_biteq!((-0.0f16).next_up(), tiny);
62-
assert_biteq!(0.0f16.next_up(), tiny);
63-
assert_biteq!(tiny.next_up(), tiny_up);
64-
assert_biteq!(largest_subnormal.next_up(), smallest_normal);
65-
assert_biteq!(1.0f16.next_up(), 1.0 + f16::EPSILON);
66-
assert_biteq!(f16::MAX.next_up(), f16::INFINITY);
67-
assert_biteq!(f16::INFINITY.next_up(), f16::INFINITY);
68-
69-
// Check that NaNs roundtrip.
70-
let nan0 = f16::NAN;
71-
let nan1 = f16::from_bits(f16::NAN.to_bits() ^ NAN_MASK1);
72-
let nan2 = f16::from_bits(f16::NAN.to_bits() ^ NAN_MASK2);
73-
assert_biteq!(nan0.next_up(), nan0);
74-
assert_biteq!(nan1.next_up(), nan1);
75-
assert_biteq!(nan2.next_up(), nan2);
76-
}
77-
7848
#[test]
7949
fn test_next_down() {
8050
let tiny = f16::from_bits(TINY_BITS);

library/coretests/tests/floats/f32.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,6 @@ const NAN_MASK2: u32 = 0x0055_5555;
2929
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
3030
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
3131

32-
#[test]
33-
fn test_next_up() {
34-
let tiny = f32::from_bits(TINY_BITS);
35-
let tiny_up = f32::from_bits(TINY_UP_BITS);
36-
let max_down = f32::from_bits(MAX_DOWN_BITS);
37-
let largest_subnormal = f32::from_bits(LARGEST_SUBNORMAL_BITS);
38-
let smallest_normal = f32::from_bits(SMALLEST_NORMAL_BITS);
39-
assert_biteq!(f32::NEG_INFINITY.next_up(), f32::MIN);
40-
assert_biteq!(f32::MIN.next_up(), -max_down);
41-
assert_biteq!((-1.0f32 - f32::EPSILON).next_up(), -1.0f32);
42-
assert_biteq!((-smallest_normal).next_up(), -largest_subnormal);
43-
assert_biteq!((-tiny_up).next_up(), -tiny);
44-
assert_biteq!((-tiny).next_up(), -0.0f32);
45-
assert_biteq!((-0.0f32).next_up(), tiny);
46-
assert_biteq!(0.0f32.next_up(), tiny);
47-
assert_biteq!(tiny.next_up(), tiny_up);
48-
assert_biteq!(largest_subnormal.next_up(), smallest_normal);
49-
assert_biteq!(1.0f32.next_up(), 1.0 + f32::EPSILON);
50-
assert_biteq!(f32::MAX.next_up(), f32::INFINITY);
51-
assert_biteq!(f32::INFINITY.next_up(), f32::INFINITY);
52-
53-
// Check that NaNs roundtrip.
54-
let nan0 = f32::NAN;
55-
let nan1 = f32::from_bits(f32::NAN.to_bits() ^ NAN_MASK1);
56-
let nan2 = f32::from_bits(f32::NAN.to_bits() ^ NAN_MASK2);
57-
assert_biteq!(nan0.next_up(), nan0);
58-
assert_biteq!(nan1.next_up(), nan1);
59-
assert_biteq!(nan2.next_up(), nan2);
60-
}
61-
6232
#[test]
6333
fn test_next_down() {
6434
let tiny = f32::from_bits(TINY_BITS);

library/coretests/tests/floats/f64.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa;
2424
/// Second pattern over the mantissa
2525
const NAN_MASK2: u64 = 0x0005_5555_5555_5555;
2626

27-
#[test]
28-
fn test_next_up() {
29-
let tiny = f64::from_bits(TINY_BITS);
30-
let tiny_up = f64::from_bits(TINY_UP_BITS);
31-
let max_down = f64::from_bits(MAX_DOWN_BITS);
32-
let largest_subnormal = f64::from_bits(LARGEST_SUBNORMAL_BITS);
33-
let smallest_normal = f64::from_bits(SMALLEST_NORMAL_BITS);
34-
assert_biteq!(f64::NEG_INFINITY.next_up(), f64::MIN);
35-
assert_biteq!(f64::MIN.next_up(), -max_down);
36-
assert_biteq!((-1.0 - f64::EPSILON).next_up(), -1.0f64);
37-
assert_biteq!((-smallest_normal).next_up(), -largest_subnormal);
38-
assert_biteq!((-tiny_up).next_up(), -tiny);
39-
assert_biteq!((-tiny).next_up(), -0.0f64);
40-
assert_biteq!((-0.0f64).next_up(), tiny);
41-
assert_biteq!(0.0f64.next_up(), tiny);
42-
assert_biteq!(tiny.next_up(), tiny_up);
43-
assert_biteq!(largest_subnormal.next_up(), smallest_normal);
44-
assert_biteq!(1.0f64.next_up(), 1.0 + f64::EPSILON);
45-
assert_biteq!(f64::MAX.next_up(), f64::INFINITY);
46-
assert_biteq!(f64::INFINITY.next_up(), f64::INFINITY);
47-
48-
let nan0 = f64::NAN;
49-
let nan1 = f64::from_bits(f64::NAN.to_bits() ^ NAN_MASK1);
50-
let nan2 = f64::from_bits(f64::NAN.to_bits() ^ NAN_MASK2);
51-
assert_biteq!(nan0.next_up(), nan0);
52-
assert_biteq!(nan1.next_up(), nan1);
53-
assert_biteq!(nan2.next_up(), nan2);
54-
}
55-
5627
#[test]
5728
fn test_next_down() {
5829
let tiny = f64::from_bits(TINY_BITS);

library/coretests/tests/floats/mod.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,65 @@ trait TestableFloat {
66
const APPROX: Self;
77
const MIN_POSITIVE_NORMAL: Self;
88
const MAX_SUBNORMAL: Self;
9+
/// Smallest number
10+
const TINY: Self;
11+
/// Next smallest number
12+
const TINY_UP: Self;
13+
/// Exponent = 0b11...10, Significand 0b1111..10. Min val > 0
14+
const MAX_DOWN: Self;
15+
type Bits;
16+
/// First pattern over the mantissa
17+
const NAN_MASK1: Self::Bits;
18+
/// Second pattern over the mantissa
19+
const NAN_MASK2: Self::Bits;
920
}
1021

1122
impl TestableFloat for f16 {
1223
const APPROX: Self = 1e-3;
1324
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
1425
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
26+
const TINY: Self = Self::from_bits(0x1);
27+
const TINY_UP: Self = Self::from_bits(0x2);
28+
const MAX_DOWN: Self = Self::from_bits(0x7bfe);
29+
type Bits = u16;
30+
const NAN_MASK1: Self::Bits = 0x02aa;
31+
const NAN_MASK2: Self::Bits = 0x0155;
1532
}
1633

1734
impl TestableFloat for f32 {
1835
const APPROX: Self = 1e-6;
1936
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
2037
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
38+
const TINY: Self = Self::from_bits(0x1);
39+
const TINY_UP: Self = Self::from_bits(0x2);
40+
const MAX_DOWN: Self = Self::from_bits(0x7f7f_fffe);
41+
type Bits = u32;
42+
const NAN_MASK1: Self::Bits = 0x002a_aaaa;
43+
const NAN_MASK2: Self::Bits = 0x0055_5555;
2144
}
2245

2346
impl TestableFloat for f64 {
2447
const APPROX: Self = 1e-6;
2548
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
2649
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
50+
const TINY: Self = Self::from_bits(0x1);
51+
const TINY_UP: Self = Self::from_bits(0x2);
52+
const MAX_DOWN: Self = Self::from_bits(0x7fef_ffff_ffff_fffe);
53+
type Bits = u64;
54+
const NAN_MASK1: Self::Bits = 0x000a_aaaa_aaaa_aaaa;
55+
const NAN_MASK2: Self::Bits = 0x0005_5555_5555_5555;
2756
}
2857

2958
impl TestableFloat for f128 {
3059
const APPROX: Self = 1e-9;
3160
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
3261
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
62+
const TINY: Self = Self::from_bits(0x1);
63+
const TINY_UP: Self = Self::from_bits(0x2);
64+
const MAX_DOWN: Self = Self::from_bits(0x7ffefffffffffffffffffffffffffffe);
65+
type Bits = u128;
66+
const NAN_MASK1: Self::Bits = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa;
67+
const NAN_MASK2: Self::Bits = 0x00005555555555555555555555555555;
3368
}
3469

3570
/// Determine the tolerance for values of the argument type.
@@ -1019,3 +1054,36 @@ float_test! {
10191054
assert!((-Float::NAN).is_sign_negative());
10201055
}
10211056
}
1057+
1058+
float_test! {
1059+
name: next_up,
1060+
attrs: {
1061+
f16: #[cfg(any(miri, target_has_reliable_f16))],
1062+
f128: #[cfg(any(miri, target_has_reliable_f128))],
1063+
},
1064+
test<Float> {
1065+
let one: Float = 1.0;
1066+
let zero: Float = 0.0;
1067+
assert_biteq!(Float::NEG_INFINITY.next_up(), Float::MIN);
1068+
assert_biteq!(Float::MIN.next_up(), -Float::MAX_DOWN);
1069+
assert_biteq!((-one - Float::EPSILON).next_up(), -one);
1070+
assert_biteq!((-Float::MIN_POSITIVE_NORMAL).next_up(), -Float::MAX_SUBNORMAL);
1071+
assert_biteq!((-Float::TINY_UP).next_up(), -Float::TINY);
1072+
assert_biteq!((-Float::TINY).next_up(), -zero);
1073+
assert_biteq!((-zero).next_up(), Float::TINY);
1074+
assert_biteq!(zero.next_up(), Float::TINY);
1075+
assert_biteq!(Float::TINY.next_up(), Float::TINY_UP);
1076+
assert_biteq!(Float::MAX_SUBNORMAL.next_up(), Float::MIN_POSITIVE_NORMAL);
1077+
assert_biteq!(one.next_up(), 1.0 + Float::EPSILON);
1078+
assert_biteq!(Float::MAX.next_up(), Float::INFINITY);
1079+
assert_biteq!(Float::INFINITY.next_up(), Float::INFINITY);
1080+
1081+
// Check that NaNs roundtrip.
1082+
let nan0 = Float::NAN;
1083+
let nan1 = Float::from_bits(Float::NAN.to_bits() ^ Float::NAN_MASK1);
1084+
let nan2 = Float::from_bits(Float::NAN.to_bits() ^ Float::NAN_MASK2);
1085+
assert_biteq!(nan0.next_up(), nan0);
1086+
assert_biteq!(nan1.next_up(), nan1);
1087+
assert_biteq!(nan2.next_up(), nan2);
1088+
}
1089+
}

0 commit comments

Comments
 (0)