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