1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use super::*;
use bitvec::prelude::*;

/// A floating point number, also contains the format information.
#[derive(Debug, Clone)]
pub struct Float {
    pub format: Format,
    pub bits: BitVec<usize, Msb0>,
}

impl Float {
    /// Create from the given format and bit pattern.
    /// 
    /// # Arguments
    /// 
    /// * `format` - The format of the float.
    /// * `bits` - The bit pattern of the float.
    pub fn from_bitvec(format: Format, bits: BitVec<usize, Msb0>) -> Float {
        Float {
            format,
            bits,
        }
    }

    /// Create from the given format and components.
    /// 
    /// # Arguments
    /// 
    /// * `format` - The format of the float.
    /// * `comps` - The components of the float.
    pub fn from_comps(format: Format, comps: Components) -> Result<Float, error::Error> {
        let mut bits = BitVec::<usize, Msb0>::new();

        if let Some(neg) = comps.neg {
            bits.push(neg);
        }

        let exp_len = comps.exp.len();
        let mant_len = comps.mant.len();

        let exp = comps.exp.chars().map(|c| c == '1');
        let mant = comps.mant.chars().map(|c| c == '1');

        if exp_len < format.exp as usize {
            bits.extend(std::iter::repeat(false).take(format.exp as usize - exp_len));
            bits.extend(exp);
        } else if exp.clone().take(exp_len - format.exp as usize).any(|b| b == true) {
            return Err(error::Error::InsufficientExponentBits);
        } else {
            bits.extend(exp.skip(exp_len - format.exp as usize));
        }

        if mant_len < format.mant as usize {
            bits.extend(std::iter::repeat(false).take(format.mant as usize - mant_len));
            bits.extend(mant);
        } else if mant.clone().take(mant_len - format.mant as usize).any(|b| b == true) {
            return Err(error::Error::InsufficientMantissaBits);
        } else {
            bits.extend(mant.skip(mant_len - format.mant as usize));
        }

        Ok(Float::from_bitvec(format, bits))
    }

    /// Create from the given field bit patterns.
    /// 
    /// # Arguments
    /// 
    /// * `neg` - Whether the number is signed and the sign.
    /// * `exp` - The exponent bit pattern of the number.
    /// * `mant` - The mantissa bit pattern of the number.
    pub fn from_fields(neg: Option<bool>, exp: String, mant: String) -> Result<Float, error::Error> {
        Float::from_comps(
            Format::ieee_binary32(),
            Components {
                neg,
                exp,
                mant,
            }
        )
    }
    
    /// Decompose into components.
    pub fn to_comps(&self) -> Components {
        let signed = self.format.signed;
        let exp_range = signed as usize..(signed as usize + self.format.exp as usize);
        let mant_range = exp_range.end..(exp_range.end + self.format.mant as usize);

        let neg = match signed {
            true => Some(self.bits[0]),
            false => None,
        };

        let exp = self.bits[exp_range]
            .into_iter()
            .map(|b| if b == true { '1' } else { '0' })
            .collect::<String>();

        let mant = self.bits[mant_range]
            .into_iter()
            .map(|b| if b == true { '1' } else { '0' })
            .collect::<String>();

        Components { neg, exp, mant }
    }
}

impl From<f32> for Float {
    /// Create from the given `f32`, using IEEE binary32 format.
    fn from(f: f32) -> Float {
        let bits = f.to_bits();
        let format = Format::ieee_binary32();
        Float::from_bitvec(format, bits.view_bits::<Msb0>().iter().collect())
    }
}

impl Into<f32> for Float {
    /// Convert into a `f32`.
    /// May cause a lost of information.
    /// May panic.
    fn into(self) -> f32 {
        let Components { neg, exp, mant } = self.to_comps();

        let exp = i32::from_str_radix(&exp, 2).unwrap();
        let mant = u128::from_str_radix(&mant, 2).unwrap();

        let sign = match neg {
            Some(true) => -1f32,
            Some(false) => 1f32,
            None => 1f32,
        };

        let exp = 2f32.powi((exp - self.format.exp_excess) as i32);
        let mant = mant as f32 / (2f32.powi(self.format.mant as i32)) + 1f32;

        sign * exp * mant
    }
}

impl From<f64> for Float {
    /// Create from the given `f64`, using IEEE binary64 format.
    fn from(f: f64) -> Float {
        let bits = f.to_bits();
        let format = Format::ieee_binary64();
        Float::from_bitvec(format, bits.view_bits::<Msb0>().iter().collect())
    }
}

impl Into<f64> for Float {
    /// Convert into a `f64`.
    /// May cause a lost of information.
    /// May panic.
    fn into(self) -> f64 {
        let Components { neg, exp, mant } = self.to_comps();

        let exp = i32::from_str_radix(&exp, 2).unwrap();
        let mant = u128::from_str_radix(&mant, 2).unwrap();

        let sign = match neg {
            Some(true) => -1f64,
            Some(false) => 1f64,
            None => 1f64,
        };

        let exp = 2f64.powi((exp - self.format.exp_excess) as i32);
        let mant = mant as f64 / (2f64.powi(self.format.mant as i32)) + 1f64;

        sign * exp * mant
    }
}