File: float_tests.rs

package info (click to toggle)
rust-lexical-parse-float 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,012 kB
  • sloc: makefile: 2
file content (165 lines) | stat: -rw-r--r-- 4,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use lexical_parse_float::float::{self, RawFloat};
use lexical_parse_float::limits::ExactFloat;
use lexical_util::num::Float;

#[test]
fn exponent_fast_path_test() {
    assert_eq!(f32::min_exponent_fast_path(10), -10);
    assert_eq!(f32::max_exponent_fast_path(10), 10);
    assert_eq!(f32::max_exponent_disguised_fast_path(10), 17);

    assert_eq!(f64::min_exponent_fast_path(10), -22);
    assert_eq!(f64::max_exponent_fast_path(10), 22);
    assert_eq!(f64::max_exponent_disguised_fast_path(10), 37);
}

fn slow_f32_power(exponent: usize, radix: u32) -> f32 {
    let mut value: f32 = 1.0;
    for _ in 0..exponent {
        value *= radix as f32;
    }
    value
}

fn slow_f64_power(exponent: usize, radix: u32) -> f64 {
    let mut value: f64 = 1.0;
    for _ in 0..exponent {
        value *= radix as f64;
    }
    value
}

fn pow_fast_path(radix: u32) {
    for exponent in 0..f32::exponent_limit(radix).1 + 1 {
        let exponent = exponent as usize;
        let actual = f32::pow_fast_path(exponent, radix);
        assert_eq!(actual, slow_f32_power(exponent, radix));
    }
    for exponent in 0..f64::exponent_limit(radix).1 + 1 {
        let exponent = exponent as usize;
        let actual = f64::pow_fast_path(exponent, radix);
        assert_eq!(actual, slow_f64_power(exponent, radix));
    }
}

#[test]
#[cfg_attr(miri, ignore)]
fn pow_fast_path_test() {
    pow_fast_path(10);
    if cfg!(feature = "power-of-two") {
        pow_fast_path(2);
        pow_fast_path(4);
        pow_fast_path(8);
        pow_fast_path(16);
        pow_fast_path(32);
    }
    if cfg!(feature = "radix") {
        pow_fast_path(3);
        pow_fast_path(5);
        pow_fast_path(6);
        pow_fast_path(7);
        pow_fast_path(9);
        pow_fast_path(11);
        pow_fast_path(12);
        pow_fast_path(13);
        pow_fast_path(14);
        pow_fast_path(15);
        pow_fast_path(17);
        pow_fast_path(18);
        pow_fast_path(19);
        pow_fast_path(20);
        pow_fast_path(21);
        pow_fast_path(22);
        pow_fast_path(23);
        pow_fast_path(24);
        pow_fast_path(25);
        pow_fast_path(26);
        pow_fast_path(27);
        pow_fast_path(28);
        pow_fast_path(29);
        pow_fast_path(30);
        pow_fast_path(31);
        pow_fast_path(33);
        pow_fast_path(34);
        pow_fast_path(35);
        pow_fast_path(36);
    }
}

fn slow_int_power(exponent: usize, radix: u32) -> u64 {
    let mut value: u64 = 1;
    for _ in 0..exponent {
        value *= radix as u64;
    }
    value
}

fn int_pow_fast_path(radix: u32) {
    for exponent in 0..f64::mantissa_limit(radix) {
        let exponent = exponent as usize;
        let actual = f64::int_pow_fast_path(exponent, radix);
        assert_eq!(actual, slow_int_power(exponent, radix));
    }
}

#[test]
fn int_pow_fast_path_test() {
    int_pow_fast_path(10);
    if cfg!(feature = "power-of-two") {
        int_pow_fast_path(2);
        int_pow_fast_path(4);
        int_pow_fast_path(8);
        int_pow_fast_path(16);
        int_pow_fast_path(32);
    }
    if cfg!(feature = "radix") {
        int_pow_fast_path(3);
        int_pow_fast_path(5);
        int_pow_fast_path(6);
        int_pow_fast_path(7);
        int_pow_fast_path(9);
        int_pow_fast_path(11);
        int_pow_fast_path(12);
        int_pow_fast_path(13);
        int_pow_fast_path(14);
        int_pow_fast_path(15);
        int_pow_fast_path(17);
        int_pow_fast_path(18);
        int_pow_fast_path(19);
        int_pow_fast_path(20);
        int_pow_fast_path(21);
        int_pow_fast_path(22);
        int_pow_fast_path(23);
        int_pow_fast_path(24);
        int_pow_fast_path(25);
        int_pow_fast_path(26);
        int_pow_fast_path(27);
        int_pow_fast_path(28);
        int_pow_fast_path(29);
        int_pow_fast_path(30);
        int_pow_fast_path(31);
        int_pow_fast_path(33);
        int_pow_fast_path(34);
        int_pow_fast_path(35);
        int_pow_fast_path(36);
    }
}

fn extended_to_float<F: RawFloat>(mantissa: u64, exponent: i32, expected: F) {
    let fp = float::ExtendedFloat80 {
        mant: mantissa,
        exp: exponent,
    };
    assert_eq!(float::extended_to_float::<F>(fp), expected);
}

#[test]
fn extended_to_float_test() {
    let max_mant = (1 << f64::MANTISSA_SIZE) - 1;
    let max_exp = f64::INFINITE_POWER - 1;
    extended_to_float::<f64>(0, 0, 0.0);
    extended_to_float::<f64>(1, 0, 5e-324);
    extended_to_float::<f64>(max_mant, max_exp, f64::MAX);
    extended_to_float::<f64>(0, 1076, 9007199254740992.0);
    extended_to_float::<f64>(1, 1076, 9007199254740994.0);
}