Skip to content

Commit bbdaad0

Browse files
committed
Auto merge of #41302 - rkruppe:dec2flt-assoc-consts, r=BurntSushi
Use associated constants in core::num::dec2flt
2 parents ba37798 + e9c74bc commit bbdaad0

File tree

4 files changed

+99
-152
lines changed

4 files changed

+99
-152
lines changed

Diff for: src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#![feature(allow_internal_unstable)]
7171
#![feature(asm)]
7272
#![feature(associated_type_defaults)]
73+
#![feature(associated_consts)]
7374
#![feature(cfg_target_feature)]
7475
#![feature(cfg_target_has_atomic)]
7576
#![feature(concat_idents)]

Diff for: src/libcore/num/dec2flt/algorithm.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,17 @@ mod fpu_precision {
106106
/// a bignum.
107107
pub fn fast_path<T: RawFloat>(integral: &[u8], fractional: &[u8], e: i64) -> Option<T> {
108108
let num_digits = integral.len() + fractional.len();
109-
// log_10(f64::max_sig) ~ 15.95. We compare the exact value to max_sig near the end,
109+
// log_10(f64::MAX_SIG) ~ 15.95. We compare the exact value to MAX_SIG near the end,
110110
// this is just a quick, cheap rejection (and also frees the rest of the code from
111111
// worrying about underflow).
112112
if num_digits > 16 {
113113
return None;
114114
}
115-
if e.abs() >= T::ceil_log5_of_max_sig() as i64 {
115+
if e.abs() >= T::CEIL_LOG5_OF_MAX_SIG as i64 {
116116
return None;
117117
}
118118
let f = num::from_str_unchecked(integral.iter().chain(fractional.iter()));
119-
if f > T::max_sig() {
119+
if f > T::MAX_SIG {
120120
return None;
121121
}
122122

@@ -154,14 +154,14 @@ pub fn fast_path<T: RawFloat>(integral: &[u8], fractional: &[u8], e: i64) -> Opt
154154
/// > the best possible approximation that uses p bits of significand.)
155155
pub fn bellerophon<T: RawFloat>(f: &Big, e: i16) -> T {
156156
let slop;
157-
if f <= &Big::from_u64(T::max_sig()) {
157+
if f <= &Big::from_u64(T::MAX_SIG) {
158158
// The cases abs(e) < log5(2^N) are in fast_path()
159159
slop = if e >= 0 { 0 } else { 3 };
160160
} else {
161161
slop = if e >= 0 { 1 } else { 4 };
162162
}
163163
let z = rawfp::big_to_fp(f).mul(&power_of_ten(e)).normalize();
164-
let exp_p_n = 1 << (P - T::sig_bits() as u32);
164+
let exp_p_n = 1 << (P - T::SIG_BITS as u32);
165165
let lowbits: i64 = (z.f % exp_p_n) as i64;
166166
// Is the slop large enough to make a difference when
167167
// rounding to n bits?
@@ -210,14 +210,14 @@ fn algorithm_r<T: RawFloat>(f: &Big, e: i16, z0: T) -> T {
210210
if d2 < y {
211211
let mut d2_double = d2;
212212
d2_double.mul_pow2(1);
213-
if m == T::min_sig() && d_negative && d2_double > y {
213+
if m == T::MIN_SIG && d_negative && d2_double > y {
214214
z = prev_float(z);
215215
} else {
216216
return z;
217217
}
218218
} else if d2 == y {
219219
if m % 2 == 0 {
220-
if m == T::min_sig() && d_negative {
220+
if m == T::MIN_SIG && d_negative {
221221
z = prev_float(z);
222222
} else {
223223
return z;
@@ -303,12 +303,12 @@ pub fn algorithm_m<T: RawFloat>(f: &Big, e: i16) -> T {
303303
quick_start::<T>(&mut u, &mut v, &mut k);
304304
let mut rem = Big::from_small(0);
305305
let mut x = Big::from_small(0);
306-
let min_sig = Big::from_u64(T::min_sig());
307-
let max_sig = Big::from_u64(T::max_sig());
306+
let min_sig = Big::from_u64(T::MIN_SIG);
307+
let max_sig = Big::from_u64(T::MAX_SIG);
308308
loop {
309309
u.div_rem(&v, &mut x, &mut rem);
310-
if k == T::min_exp_int() {
311-
// We have to stop at the minimum exponent, if we wait until `k < T::min_exp_int()`,
310+
if k == T::MIN_EXP_INT {
311+
// We have to stop at the minimum exponent, if we wait until `k < T::MIN_EXP_INT`,
312312
// then we'd be off by a factor of two. Unfortunately this means we have to special-
313313
// case normal numbers with the minimum exponent.
314314
// FIXME find a more elegant formulation, but run the `tiny-pow10` test to make sure
@@ -318,8 +318,8 @@ pub fn algorithm_m<T: RawFloat>(f: &Big, e: i16) -> T {
318318
}
319319
return underflow(x, v, rem);
320320
}
321-
if k > T::max_exp_int() {
322-
return T::infinity2();
321+
if k > T::MAX_EXP_INT {
322+
return T::INFINITY;
323323
}
324324
if x < min_sig {
325325
u.mul_pow2(1);
@@ -345,18 +345,18 @@ fn quick_start<T: RawFloat>(u: &mut Big, v: &mut Big, k: &mut i16) {
345345
// The target ratio is one where u/v is in an in-range significand. Thus our termination
346346
// condition is log2(u / v) being the significand bits, plus/minus one.
347347
// FIXME Looking at the second bit could improve the estimate and avoid some more divisions.
348-
let target_ratio = T::sig_bits() as i16;
348+
let target_ratio = T::SIG_BITS as i16;
349349
let log2_u = u.bit_length() as i16;
350350
let log2_v = v.bit_length() as i16;
351351
let mut u_shift: i16 = 0;
352352
let mut v_shift: i16 = 0;
353353
assert!(*k == 0);
354354
loop {
355-
if *k == T::min_exp_int() {
355+
if *k == T::MIN_EXP_INT {
356356
// Underflow or subnormal. Leave it to the main function.
357357
break;
358358
}
359-
if *k == T::max_exp_int() {
359+
if *k == T::MAX_EXP_INT {
360360
// Overflow. Leave it to the main function.
361361
break;
362362
}
@@ -376,7 +376,7 @@ fn quick_start<T: RawFloat>(u: &mut Big, v: &mut Big, k: &mut i16) {
376376
}
377377

378378
fn underflow<T: RawFloat>(x: Big, v: Big, rem: Big) -> T {
379-
if x < Big::from_u64(T::min_sig()) {
379+
if x < Big::from_u64(T::MIN_SIG) {
380380
let q = num::to_u64(&x);
381381
let z = rawfp::encode_subnormal(q);
382382
return round_by_remainder(v, rem, q, z);
@@ -395,9 +395,9 @@ fn underflow<T: RawFloat>(x: Big, v: Big, rem: Big) -> T {
395395
// needs to be rounded up. Only when the rounded off bits are 1/2 and the remainder
396396
// is zero, we have a half-to-even situation.
397397
let bits = x.bit_length();
398-
let lsb = bits - T::sig_bits() as usize;
398+
let lsb = bits - T::SIG_BITS as usize;
399399
let q = num::get_bits(&x, lsb, bits);
400-
let k = T::min_exp_int() + lsb as i16;
400+
let k = T::MIN_EXP_INT + lsb as i16;
401401
let z = rawfp::encode_normal(Unpacked::new(q, k));
402402
let q_even = q % 2 == 0;
403403
match num::compare_with_half_ulp(&x, lsb) {

Diff for: src/libcore/num/dec2flt/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> {
214214
let (sign, s) = extract_sign(s);
215215
let flt = match parse_decimal(s) {
216216
ParseResult::Valid(decimal) => convert(decimal)?,
217-
ParseResult::ShortcutToInf => T::infinity2(),
218-
ParseResult::ShortcutToZero => T::zero2(),
217+
ParseResult::ShortcutToInf => T::INFINITY,
218+
ParseResult::ShortcutToZero => T::ZERO,
219219
ParseResult::Invalid => match s {
220-
"inf" => T::infinity2(),
221-
"NaN" => T::nan2(),
220+
"inf" => T::INFINITY,
221+
"NaN" => T::NAN,
222222
_ => { return Err(pfe_invalid()); }
223223
}
224224
};
@@ -254,7 +254,7 @@ fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, ParseFloatError> {
254254
// FIXME These bounds are rather conservative. A more careful analysis of the failure modes
255255
// of Bellerophon could allow using it in more cases for a massive speed up.
256256
let exponent_in_range = table::MIN_E <= e && e <= table::MAX_E;
257-
let value_in_range = upper_bound <= T::max_normal_digits() as u64;
257+
let value_in_range = upper_bound <= T::MAX_NORMAL_DIGITS as u64;
258258
if exponent_in_range && value_in_range {
259259
Ok(algorithm::bellerophon(&f, e))
260260
} else {
@@ -315,17 +315,17 @@ fn bound_intermediate_digits(decimal: &Decimal, e: i64) -> u64 {
315315
fn trivial_cases<T: RawFloat>(decimal: &Decimal) -> Option<T> {
316316
// There were zeros but they were stripped by simplify()
317317
if decimal.integral.is_empty() && decimal.fractional.is_empty() {
318-
return Some(T::zero2());
318+
return Some(T::ZERO);
319319
}
320320
// This is a crude approximation of ceil(log10(the real value)). We don't need to worry too
321321
// much about overflow here because the input length is tiny (at least compared to 2^64) and
322322
// the parser already handles exponents whose absolute value is greater than 10^18
323323
// (which is still 10^19 short of 2^64).
324324
let max_place = decimal.exp + decimal.integral.len() as i64;
325-
if max_place > T::inf_cutoff() {
326-
return Some(T::infinity2());
327-
} else if max_place < T::zero_cutoff() {
328-
return Some(T::zero2());
325+
if max_place > T::INF_CUTOFF {
326+
return Some(T::INFINITY);
327+
} else if max_place < T::ZERO_CUTOFF {
328+
return Some(T::ZERO);
329329
}
330330
None
331331
}

0 commit comments

Comments
 (0)