File size: 8,852 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
extern "C" {
    fn extF80M_add(x: *const F80, y: *const F80, ptr: *mut F80);
    fn extF80M_sub(x: *const F80, y: *const F80, ptr: *mut F80);
    fn extF80M_mul(x: *const F80, y: *const F80, ptr: *mut F80);
    fn extF80M_div(x: *const F80, y: *const F80, ptr: *mut F80);
    //fn extF80M_rem(x: *const F80, y: *const F80, ptr: *mut F80);
    fn extF80M_sqrt(x: *const F80, ptr: *mut F80);

    fn extF80M_roundToInt(x: *const F80, rounding_mode: u8, raise_inexact: bool, dst: *mut F80);

    fn extF80M_eq(x: *const F80, y: *const F80) -> bool;
    //fn extF80M_eq_signaling(x: *const F80, y: *const F80) -> bool;

    //fn extF80M_le(x: *const F80, y: *const F80) -> bool;
    //fn extF80M_le_quiet(x: *const F80, y: *const F80) -> bool;
    fn extF80M_lt(x: *const F80, y: *const F80) -> bool;
    fn extF80M_lt_quiet(x: *const F80, y: *const F80) -> bool;

    fn extF80M_to_i32(src: *const F80, rounding_mode: u8, raise_inexact: bool) -> i32;
    fn extF80M_to_i64(src: *const F80, rounding_mode: u8, raise_inexact: bool) -> i64;
    fn i32_to_extF80M(src: i32, dst: *mut F80);
    fn i64_to_extF80M(src: i64, dst: *mut F80);

    fn f32_to_extF80M(src: i32, dst: *mut F80);
    fn f64_to_extF80M(src: u64, dst: *mut F80);
    fn extF80M_to_f32(src: *const F80) -> i32;
    fn extF80M_to_f64(src: *const F80) -> u64;

    static mut softfloat_roundingMode: u8;
    static mut extF80_roundingPrecision: u8;
    static mut softfloat_exceptionFlags: u8;
}

pub enum RoundingMode {
    NearEven,
    Trunc,
    Floor,
    Ceil,
}
pub enum Precision {
    P80,
    P64,
    P32,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct F80 {
    pub mantissa: u64,
    pub sign_exponent: u16,
}
impl F80 {
    pub const ZERO: F80 = F80 {
        mantissa: 0,
        sign_exponent: 0,
    };
    pub const ONE: F80 = F80 {
        mantissa: 0x8000000000000000,
        sign_exponent: 0x3FFF,
    };
    pub const LN_10: F80 = F80 {
        mantissa: 0x935D8DDDAAA8B000,
        sign_exponent: 0x4000,
    };
    pub const LN_2: F80 = F80 {
        mantissa: 0xB17217F7D1CF7800,
        sign_exponent: 0x3FFE,
    };
    pub const PI: F80 = F80 {
        mantissa: 0xC90FDAA22168C000,
        sign_exponent: 0x4000,
    };
    pub const LOG2_E: F80 = F80 {
        mantissa: 0xB8AA3B295C17F000,
        sign_exponent: 0x3FFF,
    };
    pub const INDEFINITE_NAN: F80 = F80 {
        mantissa: 0xC000000000000000,
        sign_exponent: 0x7FFF,
    };
    pub const POS_INFINITY: F80 = F80 {
        mantissa: 0x8000000000000000,
        sign_exponent: 0x7FFF,
    };
    pub const NEG_INFINITY: F80 = F80 {
        mantissa: 0x8000000000000000,
        sign_exponent: 0xFFFF,
    };

    pub fn sign(&self) -> bool { (self.sign_exponent >> 15) == 1 }
    pub fn exponent(&self) -> i16 { (self.sign_exponent as i16 & 0x7FFF) - 0x3FFF }

    pub fn of_i32(src: i32) -> F80 {
        let mut x = F80::ZERO;
        unsafe { i32_to_extF80M(src, &mut x) };
        x
    }
    pub fn of_i64(src: i64) -> F80 {
        let mut x = F80::ZERO;
        unsafe { i64_to_extF80M(src, &mut x) };
        x
    }

    pub fn of_f32(src: i32) -> F80 {
        let mut x = F80::ZERO;
        unsafe { f32_to_extF80M(src, &mut x) };
        x
    }

    pub fn of_f64(src: u64) -> F80 {
        let mut x = F80::ZERO;
        unsafe { f64_to_extF80M(src, &mut x) };
        x
    }
    fn of_f64x(src: f64) -> F80 { F80::of_f64(f64::to_bits(src)) }

    pub fn to_f32(&self) -> i32 { unsafe { extF80M_to_f32(self) } }
    pub fn to_f64(&self) -> u64 { unsafe { extF80M_to_f64(self) } }
    fn to_f64x(&self) -> f64 { f64::from_bits(self.to_f64()) }

    pub fn to_i32(&self) -> i32 { unsafe { extF80M_to_i32(self, softfloat_roundingMode, false) } }
    pub fn to_i64(&self) -> i64 { unsafe { extF80M_to_i64(self, softfloat_roundingMode, false) } }

    pub fn truncate_to_i32(&self) -> i32 { unsafe { extF80M_to_i32(self, 1, false) } }
    pub fn truncate_to_i64(&self) -> i64 { unsafe { extF80M_to_i64(self, 1, false) } }

    pub fn cos(self) -> F80 { F80::of_f64x(self.to_f64x().cos()) }
    pub fn sin(self) -> F80 { F80::of_f64x(self.to_f64x().sin()) }
    pub fn tan(self) -> F80 { F80::of_f64x(self.to_f64x().tan()) }
    pub fn atan(self) -> F80 { F80::of_f64x(self.to_f64x().atan()) }
    pub fn atan2(self, other: F80) -> F80 { F80::of_f64x(self.to_f64x().atan2(other.to_f64x())) }

    pub fn log2(self) -> F80 { F80::of_f64x(self.to_f64x().log2()) }
    pub fn ln(self) -> F80 { F80::of_f64x(self.to_f64x().ln()) }

    pub fn abs(self) -> F80 {
        F80 {
            mantissa: self.mantissa,
            sign_exponent: self.sign_exponent & !0x8000,
        }
    }
    pub fn two_pow(self) -> F80 { F80::of_f64x(2.0f64.powf(self.to_f64x())) }
    pub fn round(self) -> F80 {
        let mut result = F80::ZERO;
        unsafe { extF80M_roundToInt(&self, softfloat_roundingMode, false, &mut result) };
        result
    }
    pub fn trunc(self) -> F80 {
        let mut result = F80::ZERO;
        unsafe { extF80M_roundToInt(&self, 1, false, &mut result) };
        result
    }

    pub fn sqrt(self) -> F80 {
        let mut result = F80::ZERO;
        unsafe { extF80M_sqrt(&self, &mut result) };
        result
    }

    pub fn is_finite(self) -> bool {
        // TODO: Can probably be done more efficiently
        self != F80::POS_INFINITY && self != F80::NEG_INFINITY
    }
    pub fn is_nan(self) -> bool {
        // TODO: Can probably be done more efficiently
        self != self
    }

    pub fn set_rounding_mode(mode: RoundingMode) {
        unsafe {
            softfloat_roundingMode = match mode {
                RoundingMode::NearEven => 0,
                RoundingMode::Trunc => 1,
                RoundingMode::Floor => 2,
                RoundingMode::Ceil => 3,
            }
        };
    }
    pub fn set_precision(precision: Precision) {
        unsafe {
            extF80_roundingPrecision = match precision {
                Precision::P80 => 80,
                Precision::P64 => 64,
                Precision::P32 => 32,
            }
        };
    }

    pub fn get_exception_flags() -> u8 {
        let f = unsafe { softfloat_exceptionFlags };
        // translate softfloat's flags to x87 status flags
        f >> 4 & 1 | f >> 1 & 4 | f << 3 & 16
    }
    pub fn clear_exception_flags() { unsafe { softfloat_exceptionFlags = 0 } }

    pub fn partial_cmp_quiet(&self, other: &Self) -> Option<std::cmp::Ordering> {
        // TODO: Can probably be done more efficiently
        if unsafe { extF80M_lt_quiet(self, other) } {
            Some(std::cmp::Ordering::Less)
        }
        else if unsafe { extF80M_lt_quiet(other, self) } {
            Some(std::cmp::Ordering::Greater)
        }
        else if self == other {
            Some(std::cmp::Ordering::Equal)
        }
        else {
            None
        }
    }
}

impl std::ops::Add for F80 {
    type Output = F80;
    fn add(self, other: Self) -> Self {
        let mut result = F80::ZERO;
        unsafe { extF80M_add(&self, &other, &mut result) };
        result
    }
}
impl std::ops::Sub for F80 {
    type Output = F80;
    fn sub(self, other: Self) -> Self {
        let mut result = F80::ZERO;
        unsafe { extF80M_sub(&self, &other, &mut result) };
        result
    }
}
impl std::ops::Neg for F80 {
    type Output = F80;
    fn neg(self) -> Self {
        let mut result = self;
        result.sign_exponent ^= 1 << 15;
        result
    }
}
impl std::ops::Mul for F80 {
    type Output = F80;
    fn mul(self, other: Self) -> Self {
        let mut result = F80::ZERO;
        unsafe { extF80M_mul(&self, &other, &mut result) };
        result
    }
}
impl std::ops::Div for F80 {
    type Output = F80;
    fn div(self, other: Self) -> Self {
        let mut result = F80::ZERO;
        unsafe { extF80M_div(&self, &other, &mut result) };
        result
    }
}
impl std::ops::Rem for F80 {
    type Output = F80;
    fn rem(self, other: Self) -> Self {
        let quot = (self / other).trunc();
        self - quot * other
        // Uses round-to-nearest instead of truncation
        //let mut result = F80::ZERO;
        //unsafe {
        //    extF80M_rem(&self, &other, &mut result)
        //};
        //result
    }
}

impl PartialEq for F80 {
    fn eq(&self, other: &Self) -> bool { unsafe { extF80M_eq(self, other) } }
}
impl PartialOrd for F80 {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        // TODO: Can probably be done more efficiently
        if unsafe { extF80M_lt(self, other) } {
            Some(std::cmp::Ordering::Less)
        }
        else if unsafe { extF80M_lt(other, self) } {
            Some(std::cmp::Ordering::Greater)
        }
        else if self == other {
            Some(std::cmp::Ordering::Equal)
        }
        else {
            None
        }
    }
}