]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/flt2dec/decoder.rs
Rollup merge of #36056 - birryree:E0194_new_error_format, r=jonathandturner
[rust.git] / src / libcore / num / flt2dec / decoder.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Decodes a floating-point value into individual parts and error ranges.
12
13 use {f32, f64};
14 use num::FpCategory;
15 use num::dec2flt::rawfp::RawFloat;
16
17 /// Decoded unsigned finite value, such that:
18 ///
19 /// - The original value equals to `mant * 2^exp`.
20 ///
21 /// - Any number from `(mant - minus) * 2^exp` to `(mant + plus) * 2^exp` will
22 ///   round to the original value. The range is inclusive only when
23 ///   `inclusive` is true.
24 #[derive(Copy, Clone, Debug, PartialEq)]
25 pub struct Decoded {
26     /// The scaled mantissa.
27     pub mant: u64,
28     /// The lower error range.
29     pub minus: u64,
30     /// The upper error range.
31     pub plus: u64,
32     /// The shared exponent in base 2.
33     pub exp: i16,
34     /// True when the error range is inclusive.
35     ///
36     /// In IEEE 754, this is true when the original mantissa was even.
37     pub inclusive: bool,
38 }
39
40 /// Decoded unsigned value.
41 #[derive(Copy, Clone, Debug, PartialEq)]
42 pub enum FullDecoded {
43     /// Not-a-number.
44     Nan,
45     /// Infinities, either positive or negative.
46     Infinite,
47     /// Zero, either positive or negative.
48     Zero,
49     /// Finite numbers with further decoded fields.
50     Finite(Decoded),
51 }
52
53 /// A floating point type which can be `decode`d.
54 pub trait DecodableFloat: RawFloat + Copy {
55     /// The minimum positive normalized value.
56     fn min_pos_norm_value() -> Self;
57 }
58
59 impl DecodableFloat for f32 {
60     fn min_pos_norm_value() -> Self { f32::MIN_POSITIVE }
61 }
62
63 impl DecodableFloat for f64 {
64     fn min_pos_norm_value() -> Self { f64::MIN_POSITIVE }
65 }
66
67 /// Returns a sign (true when negative) and `FullDecoded` value
68 /// from given floating point number.
69 pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
70     let (mant, exp, sign) = v.integer_decode2();
71     let even = (mant & 1) == 0;
72     let decoded = match v.classify() {
73         FpCategory::Nan => FullDecoded::Nan,
74         FpCategory::Infinite => FullDecoded::Infinite,
75         FpCategory::Zero => FullDecoded::Zero,
76         FpCategory::Subnormal => {
77             // neighbors: (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
78             // Float::integer_decode always preserves the exponent,
79             // so the mantissa is scaled for subnormals.
80             FullDecoded::Finite(Decoded { mant: mant, minus: 1, plus: 1,
81                                           exp: exp, inclusive: even })
82         }
83         FpCategory::Normal => {
84             let minnorm = <T as DecodableFloat>::min_pos_norm_value().integer_decode2();
85             if mant == minnorm.0 {
86                 // neighbors: (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
87                 // where maxmant = minnormmant * 2 - 1
88                 FullDecoded::Finite(Decoded { mant: mant << 2, minus: 1, plus: 2,
89                                               exp: exp - 2, inclusive: even })
90             } else {
91                 // neighbors: (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
92                 FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 1,
93                                               exp: exp - 1, inclusive: even })
94             }
95         }
96     };
97     (sign < 0, decoded)
98 }
99