]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
11 //! Decodes a floating-point value into individual parts and error ranges.
15 use num
::dec2flt
::rawfp
::RawFloat
;
17 /// Decoded unsigned finite value, such that:
19 /// - The original value equals to `mant * 2^exp`.
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, Eq)]
26 /// The scaled mantissa.
28 /// The lower error range.
30 /// The upper error range.
32 /// The shared exponent in base 2.
34 /// True when the error range is inclusive.
36 /// In IEEE 754, this is true when the original mantissa was even.
40 /// Decoded unsigned value.
41 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
42 pub enum FullDecoded
{
45 /// Infinities, either positive or negative.
47 /// Zero, either positive or negative.
49 /// Finite numbers with further decoded fields.
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;
59 impl DecodableFloat
for f32 {
60 fn min_pos_norm_value() -> Self { f32::MIN_POSITIVE }
63 impl DecodableFloat
for f64 {
64 fn min_pos_norm_value() -> Self { f64::MIN_POSITIVE }
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_decode();
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
, minus
: 1, plus
: 1,
81 exp
, inclusive
: even
})
83 FpCategory
::Normal
=> {
84 let minnorm
= <T
as DecodableFloat
>::min_pos_norm_value().integer_decode();
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
})
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
})