]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/flt2dec/decoder.rs
Changed issue number to 36105
[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 prelude::v1::*;
14
15 use {f32, f64};
16 use num::FpCategory;
17 use num::dec2flt::rawfp::RawFloat;
18
19 /// Decoded unsigned finite value, such that:
20 ///
21 /// - The original value equals to `mant * 2^exp`.
22 ///
23 /// - Any number from `(mant - minus) * 2^exp` to `(mant + plus) * 2^exp` will
24 ///   round to the original value. The range is inclusive only when
25 ///   `inclusive` is true.
26 #[derive(Copy, Clone, Debug, PartialEq)]
27 pub struct Decoded {
28     /// The scaled mantissa.
29     pub mant: u64,
30     /// The lower error range.
31     pub minus: u64,
32     /// The upper error range.
33     pub plus: u64,
34     /// The shared exponent in base 2.
35     pub exp: i16,
36     /// True when the error range is inclusive.
37     ///
38     /// In IEEE 754, this is true when the original mantissa was even.
39     pub inclusive: bool,
40 }
41
42 /// Decoded unsigned value.
43 #[derive(Copy, Clone, Debug, PartialEq)]
44 pub enum FullDecoded {
45     /// Not-a-number.
46     Nan,
47     /// Infinities, either positive or negative.
48     Infinite,
49     /// Zero, either positive or negative.
50     Zero,
51     /// Finite numbers with further decoded fields.
52     Finite(Decoded),
53 }
54
55 /// A floating point type which can be `decode`d.
56 pub trait DecodableFloat: RawFloat + Copy {
57     /// The minimum positive normalized value.
58     fn min_pos_norm_value() -> Self;
59 }
60
61 impl DecodableFloat for f32 {
62     fn min_pos_norm_value() -> Self { f32::MIN_POSITIVE }
63 }
64
65 impl DecodableFloat for f64 {
66     fn min_pos_norm_value() -> Self { f64::MIN_POSITIVE }
67 }
68
69 /// Returns a sign (true when negative) and `FullDecoded` value
70 /// from given floating point number.
71 pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
72     let (mant, exp, sign) = v.integer_decode2();
73     let even = (mant & 1) == 0;
74     let decoded = match v.classify() {
75         FpCategory::Nan => FullDecoded::Nan,
76         FpCategory::Infinite => FullDecoded::Infinite,
77         FpCategory::Zero => FullDecoded::Zero,
78         FpCategory::Subnormal => {
79             // neighbors: (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
80             // Float::integer_decode always preserves the exponent,
81             // so the mantissa is scaled for subnormals.
82             FullDecoded::Finite(Decoded { mant: mant, minus: 1, plus: 1,
83                                           exp: exp, inclusive: even })
84         }
85         FpCategory::Normal => {
86             let minnorm = <T as DecodableFloat>::min_pos_norm_value().integer_decode2();
87             if mant == minnorm.0 {
88                 // neighbors: (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
89                 // where maxmant = minnormmant * 2 - 1
90                 FullDecoded::Finite(Decoded { mant: mant << 2, minus: 1, plus: 2,
91                                               exp: exp - 2, inclusive: even })
92             } else {
93                 // neighbors: (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
94                 FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 1,
95                                               exp: exp - 1, inclusive: even })
96             }
97         }
98     };
99     (sign < 0, decoded)
100 }
101