machina-softfloat 0.1.2

Pure software IEEE 754 floating-point library
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// SPDX-License-Identifier: MIT
// IEEE 754 floating-point type definitions.

use core::fmt;

// ---------------------------------------------------------------
// Bit-manipulation helper trait for unsigned integer backing types
// ---------------------------------------------------------------

pub trait BitOps:
    Copy + Clone + PartialEq + Eq + core::hash::Hash + fmt::Debug + Sized + 'static
{
    const ZERO: Self;
    const ONE: Self;
    const MAX: Self;
    const BITS: u32;
    fn shl(self, n: u32) -> Self;
    fn shr(self, n: u32) -> Self;
    fn bitand(self, other: Self) -> Self;
    fn bitor(self, other: Self) -> Self;
    fn bitxor(self, other: Self) -> Self;
    fn not(self) -> Self;
    fn wrapping_sub(self, other: Self) -> Self;
    fn wrapping_add(self, other: Self) -> Self;
    fn to_u128(self) -> u128;
    fn from_u128(v: u128) -> Self;
    fn is_zero(self) -> bool;
    fn leading_zeros(self) -> u32;
}

macro_rules! impl_bitops {
    ($ty:ty) => {
        impl BitOps for $ty {
            const ZERO: Self = 0;
            const ONE: Self = 1;
            const MAX: Self = <$ty>::MAX;
            const BITS: u32 = <$ty>::BITS;
            #[inline]
            fn shl(self, n: u32) -> Self {
                self << n
            }
            #[inline]
            fn shr(self, n: u32) -> Self {
                self >> n
            }
            #[inline]
            fn bitand(self, o: Self) -> Self {
                self & o
            }
            #[inline]
            fn bitor(self, o: Self) -> Self {
                self | o
            }
            #[inline]
            fn bitxor(self, o: Self) -> Self {
                self ^ o
            }
            #[inline]
            fn not(self) -> Self {
                !self
            }
            #[inline]
            fn wrapping_sub(self, o: Self) -> Self {
                <$ty>::wrapping_sub(self, o)
            }
            #[inline]
            fn wrapping_add(self, o: Self) -> Self {
                <$ty>::wrapping_add(self, o)
            }
            #[inline]
            fn to_u128(self) -> u128 {
                self as u128
            }
            #[inline]
            fn from_u128(v: u128) -> Self {
                v as Self
            }
            #[inline]
            fn is_zero(self) -> bool {
                self == 0
            }
            #[inline]
            fn leading_zeros(self) -> u32 {
                <$ty>::leading_zeros(self)
            }
        }
    };
}

impl_bitops!(u16);
impl_bitops!(u32);
impl_bitops!(u64);
impl_bitops!(u128);

// ---------------------------------------------------------------
// FloatFormat trait
// ---------------------------------------------------------------

pub trait FloatFormat: Copy + Clone + PartialEq + Eq {
    type Bits: BitOps;
    const EXP_BITS: u32;
    const FRAC_BITS: u32;
    const BIAS: i32;
    const HAS_EXPLICIT_INT: bool;
    fn to_bits(self) -> Self::Bits;
    fn from_bits(bits: Self::Bits) -> Self;
}

// ---------------------------------------------------------------
// Float16 -- IEEE 754 half precision: 1+5+10
// ---------------------------------------------------------------

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Float16(pub(crate) u16);

impl Float16 {
    pub const fn from_bits(u: u16) -> Self {
        Self(u)
    }
    pub const fn to_bits(self) -> u16 {
        self.0
    }

    pub fn is_nan(self) -> bool {
        let exp = (self.0 >> 10) & 0x1F;
        let frac = self.0 & 0x3FF;
        exp == 0x1F && frac != 0
    }
    pub fn is_inf(self) -> bool {
        let exp = (self.0 >> 10) & 0x1F;
        let frac = self.0 & 0x3FF;
        exp == 0x1F && frac == 0
    }
    pub fn is_zero(self) -> bool {
        self.0 & 0x7FFF == 0
    }
    pub fn is_neg(self) -> bool {
        self.0 & 0x8000 != 0
    }
}

impl fmt::Debug for Float16 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Float16(0x{:04X})", self.0)
    }
}

impl FloatFormat for Float16 {
    type Bits = u16;
    const EXP_BITS: u32 = 5;
    const FRAC_BITS: u32 = 10;
    const BIAS: i32 = 15;
    const HAS_EXPLICIT_INT: bool = false;
    fn to_bits(self) -> u16 {
        self.0
    }
    fn from_bits(bits: u16) -> Self {
        Self(bits)
    }
}

// ---------------------------------------------------------------
// BFloat16 -- Brain float: 1+8+7
// ---------------------------------------------------------------

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct BFloat16(pub(crate) u16);

impl BFloat16 {
    pub const fn from_bits(u: u16) -> Self {
        Self(u)
    }
    pub const fn to_bits(self) -> u16 {
        self.0
    }

    pub fn is_nan(self) -> bool {
        let exp = (self.0 >> 7) & 0xFF;
        let frac = self.0 & 0x7F;
        exp == 0xFF && frac != 0
    }
    pub fn is_inf(self) -> bool {
        let exp = (self.0 >> 7) & 0xFF;
        let frac = self.0 & 0x7F;
        exp == 0xFF && frac == 0
    }
    pub fn is_zero(self) -> bool {
        self.0 & 0x7FFF == 0
    }
    pub fn is_neg(self) -> bool {
        self.0 & 0x8000 != 0
    }
}

impl fmt::Debug for BFloat16 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "BFloat16(0x{:04X})", self.0)
    }
}

impl FloatFormat for BFloat16 {
    type Bits = u16;
    const EXP_BITS: u32 = 8;
    const FRAC_BITS: u32 = 7;
    const BIAS: i32 = 127;
    const HAS_EXPLICIT_INT: bool = false;
    fn to_bits(self) -> u16 {
        self.0
    }
    fn from_bits(bits: u16) -> Self {
        Self(bits)
    }
}

// ---------------------------------------------------------------
// Float32 -- IEEE 754 single precision: 1+8+23
// ---------------------------------------------------------------

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Float32(pub(crate) u32);

impl Float32 {
    pub const fn from_bits(u: u32) -> Self {
        Self(u)
    }
    pub const fn to_bits(self) -> u32 {
        self.0
    }

    pub fn is_nan(self) -> bool {
        let exp = (self.0 >> 23) & 0xFF;
        let frac = self.0 & 0x7F_FFFF;
        exp == 0xFF && frac != 0
    }
    pub fn is_inf(self) -> bool {
        let exp = (self.0 >> 23) & 0xFF;
        let frac = self.0 & 0x7F_FFFF;
        exp == 0xFF && frac == 0
    }
    pub fn is_zero(self) -> bool {
        self.0 & 0x7FFF_FFFF == 0
    }
    pub fn is_neg(self) -> bool {
        self.0 & 0x8000_0000 != 0
    }
    /// Construct from a native `f32` (bit reinterpret).
    pub fn from_f32(v: f32) -> Self {
        Self(v.to_bits())
    }
    /// Convert to native `f32` (bit reinterpret).
    pub fn to_f32(self) -> f32 {
        f32::from_bits(self.0)
    }
}

impl fmt::Debug for Float32 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Float32(0x{:08X})", self.0)
    }
}

impl FloatFormat for Float32 {
    type Bits = u32;
    const EXP_BITS: u32 = 8;
    const FRAC_BITS: u32 = 23;
    const BIAS: i32 = 127;
    const HAS_EXPLICIT_INT: bool = false;
    fn to_bits(self) -> u32 {
        self.0
    }
    fn from_bits(bits: u32) -> Self {
        Self(bits)
    }
}

// ---------------------------------------------------------------
// Float64 -- IEEE 754 double precision: 1+11+52
// ---------------------------------------------------------------

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Float64(pub(crate) u64);

impl Float64 {
    pub const fn from_bits(u: u64) -> Self {
        Self(u)
    }
    pub const fn to_bits(self) -> u64 {
        self.0
    }

    pub fn is_nan(self) -> bool {
        let exp = (self.0 >> 52) & 0x7FF;
        let frac = self.0 & 0xF_FFFF_FFFF_FFFF;
        exp == 0x7FF && frac != 0
    }
    pub fn is_inf(self) -> bool {
        let exp = (self.0 >> 52) & 0x7FF;
        let frac = self.0 & 0xF_FFFF_FFFF_FFFF;
        exp == 0x7FF && frac == 0
    }
    pub fn is_zero(self) -> bool {
        self.0 & 0x7FFF_FFFF_FFFF_FFFF == 0
    }
    pub fn is_neg(self) -> bool {
        self.0 & 0x8000_0000_0000_0000 != 0
    }
    pub fn from_f64(v: f64) -> Self {
        Self(v.to_bits())
    }
    pub fn to_f64(self) -> f64 {
        f64::from_bits(self.0)
    }
}

impl fmt::Debug for Float64 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Float64(0x{:016X})", self.0)
    }
}

impl FloatFormat for Float64 {
    type Bits = u64;
    const EXP_BITS: u32 = 11;
    const FRAC_BITS: u32 = 52;
    const BIAS: i32 = 1023;
    const HAS_EXPLICIT_INT: bool = false;
    fn to_bits(self) -> u64 {
        self.0
    }
    fn from_bits(bits: u64) -> Self {
        Self(bits)
    }
}

// ---------------------------------------------------------------
// Float128 -- IEEE 754 quad precision: 1+15+112
// ---------------------------------------------------------------

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Float128(pub(crate) u128);

impl Float128 {
    pub const fn from_bits(u: u128) -> Self {
        Self(u)
    }
    pub const fn to_bits(self) -> u128 {
        self.0
    }

    pub fn is_nan(self) -> bool {
        let exp = (self.0 >> 112) & 0x7FFF;
        let frac = self.0 & 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
        exp == 0x7FFF && frac != 0
    }
    pub fn is_inf(self) -> bool {
        let exp = (self.0 >> 112) & 0x7FFF;
        let frac = self.0 & 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
        exp == 0x7FFF && frac == 0
    }
    pub fn is_zero(self) -> bool {
        self.0 & ((1u128 << 127) - 1) == 0
    }
    pub fn is_neg(self) -> bool {
        self.0 & (1u128 << 127) != 0
    }
}

impl fmt::Debug for Float128 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Float128(0x{:032X})", self.0)
    }
}

impl FloatFormat for Float128 {
    type Bits = u128;
    const EXP_BITS: u32 = 15;
    const FRAC_BITS: u32 = 112;
    const BIAS: i32 = 16383;
    const HAS_EXPLICIT_INT: bool = false;
    fn to_bits(self) -> u128 {
        self.0
    }
    fn from_bits(bits: u128) -> Self {
        Self(bits)
    }
}

// ---------------------------------------------------------------
// FloatX80 -- x87 extended precision: 1+15+64 (explicit int bit)
// ---------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct FloatX80 {
    pub lo: u64,
    pub hi: u16,
}

impl FloatX80 {
    pub fn from_bits(u: u128) -> Self {
        Self {
            lo: u as u64,
            hi: (u >> 64) as u16,
        }
    }
    pub fn to_bits(self) -> u128 {
        (self.lo as u128) | ((self.hi as u128) << 64)
    }

    pub fn is_nan(self) -> bool {
        let exp = self.hi & 0x7FFF;
        // Explicit integer bit is bit 63 of lo
        if exp != 0x7FFF {
            return false;
        }
        // Inf has integer bit set and frac==0.
        // NaN has integer bit set and frac!=0, or
        // unnormal/pseudo forms.
        let j = (self.lo >> 63) & 1;
        let frac = self.lo & 0x7FFF_FFFF_FFFF_FFFF;
        if j == 1 && frac != 0 {
            return true;
        }
        // Pseudo-NaN: integer bit clear but exp==max
        if j == 0 {
            return true;
        }
        false
    }
    pub fn is_inf(self) -> bool {
        let exp = self.hi & 0x7FFF;
        if exp != 0x7FFF {
            return false;
        }
        // Integer bit must be set, fraction must be zero
        self.lo == 0x8000_0000_0000_0000
    }
    pub fn is_zero(self) -> bool {
        self.lo == 0 && (self.hi & 0x7FFF) == 0
    }
    pub fn is_neg(self) -> bool {
        self.hi & 0x8000 != 0
    }
}

impl fmt::Debug for FloatX80 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FloatX80(hi=0x{:04X}, lo=0x{:016X})", self.hi, self.lo)
    }
}

impl FloatFormat for FloatX80 {
    // Use u128 for uniformity in pack/unpack
    type Bits = u128;
    const EXP_BITS: u32 = 15;
    // 64 significand bits including explicit integer bit.
    // FRAC_BITS = 63 (fractional part only, the integer
    // bit is handled via HAS_EXPLICIT_INT).
    const FRAC_BITS: u32 = 63;
    const BIAS: i32 = 16383;
    const HAS_EXPLICIT_INT: bool = true;

    fn to_bits(self) -> u128 {
        FloatX80::to_bits(self)
    }
    fn from_bits(bits: u128) -> Self {
        FloatX80::from_bits(bits)
    }
}