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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
//! An IEEE-compliant 8-bit float type for testing purposes.
use std::cmp::{self, Ordering};
use std::{fmt, ops};
use crate::Float;
/// Sometimes verifying float logic is easiest when all values can quickly be checked exhaustively
/// or by hand.
///
/// IEEE-754 compliant type that includes a 1 bit sign, 4 bit exponent, and 3 bit significand.
/// Bias is -7.
///
/// Based on <https://en.wikipedia.org/wiki/Minifloat#Example_8-bit_float_(1.4.3)>.
#[derive(Clone, Copy)]
#[repr(transparent)]
#[allow(non_camel_case_types)]
pub struct f8(u8);
impl Float for f8 {
type Int = u8;
type SignedInt = i8;
const ZERO: Self = Self(0b0_0000_000);
const NEG_ZERO: Self = Self(0b1_0000_000);
const ONE: Self = Self(0b0_0111_000);
const NEG_ONE: Self = Self(0b1_0111_000);
const MAX: Self = Self(0b0_1110_111);
const MIN: Self = Self(0b1_1110_111);
const INFINITY: Self = Self(0b0_1111_000);
const NEG_INFINITY: Self = Self(0b1_1111_000);
const NAN: Self = Self(0b0_1111_100);
const NEG_NAN: Self = Self(0b1_1111_100);
const MIN_POSITIVE_NORMAL: Self = Self(1 << Self::SIG_BITS);
// FIXME: incorrect values
const EPSILON: Self = Self::ZERO;
const PI: Self = Self::ZERO;
const NEG_PI: Self = Self::ZERO;
const FRAC_PI_2: Self = Self::ZERO;
const BITS: u32 = 8;
const SIG_BITS: u32 = 3;
const SIGN_MASK: Self::Int = 0b1_0000_000;
const SIG_MASK: Self::Int = 0b0_0000_111;
const EXP_MASK: Self::Int = 0b0_1111_000;
const IMPLICIT_BIT: Self::Int = 0b0_0001_000;
fn to_bits(self) -> Self::Int {
self.0
}
fn to_bits_signed(self) -> Self::SignedInt {
self.0 as i8
}
fn is_nan(self) -> bool {
self.0 & Self::EXP_MASK == Self::EXP_MASK && self.0 & Self::SIG_MASK != 0
}
fn is_infinite(self) -> bool {
self.0 & Self::EXP_MASK == Self::EXP_MASK && self.0 & Self::SIG_MASK == 0
}
fn is_sign_negative(self) -> bool {
self.0 & Self::SIGN_MASK != 0
}
fn from_bits(a: Self::Int) -> Self {
Self(a)
}
fn abs(self) -> Self {
libm::generic::fabs(self)
}
fn copysign(self, other: Self) -> Self {
libm::generic::copysign(self, other)
}
fn fma(self, _y: Self, _z: Self) -> Self {
unimplemented!()
}
fn normalize(_significand: Self::Int) -> (i32, Self::Int) {
unimplemented!()
}
}
impl f8 {
pub const ALL_LEN: usize = 240;
/// All non-infinite non-NaN values of `f8`
pub const ALL: [Self; Self::ALL_LEN] = [
// -m*2^7
Self(0b1_1110_111), // -240
Self(0b1_1110_110),
Self(0b1_1110_101),
Self(0b1_1110_100),
Self(0b1_1110_011),
Self(0b1_1110_010),
Self(0b1_1110_001),
Self(0b1_1110_000), // -128
// -m*2^6
Self(0b1_1101_111), // -120
Self(0b1_1101_110),
Self(0b1_1101_101),
Self(0b1_1101_100),
Self(0b1_1101_011),
Self(0b1_1101_010),
Self(0b1_1101_001),
Self(0b1_1101_000), // -64
// -m*2^5
Self(0b1_1100_111), // -60
Self(0b1_1100_110),
Self(0b1_1100_101),
Self(0b1_1100_100),
Self(0b1_1100_011),
Self(0b1_1100_010),
Self(0b1_1100_001),
Self(0b1_1100_000), // -32
// -m*2^4
Self(0b1_1011_111), // -30
Self(0b1_1011_110),
Self(0b1_1011_101),
Self(0b1_1011_100),
Self(0b1_1011_011),
Self(0b1_1011_010),
Self(0b1_1011_001),
Self(0b1_1011_000), // -16
// -m*2^3
Self(0b1_1010_111), // -15
Self(0b1_1010_110),
Self(0b1_1010_101),
Self(0b1_1010_100),
Self(0b1_1010_011),
Self(0b1_1010_010),
Self(0b1_1010_001),
Self(0b1_1010_000), // -8
// -m*2^2
Self(0b1_1001_111), // -7.5
Self(0b1_1001_110),
Self(0b1_1001_101),
Self(0b1_1001_100),
Self(0b1_1001_011),
Self(0b1_1001_010),
Self(0b1_1001_001),
Self(0b1_1001_000), // -4
// -m*2^1
Self(0b1_1000_111), // -3.75
Self(0b1_1000_110),
Self(0b1_1000_101),
Self(0b1_1000_100),
Self(0b1_1000_011),
Self(0b1_1000_010),
Self(0b1_1000_001),
Self(0b1_1000_000), // -2
// -m*2^0
Self(0b1_0111_111), // -1.875
Self(0b1_0111_110),
Self(0b1_0111_101),
Self(0b1_0111_100),
Self(0b1_0111_011),
Self(0b1_0111_010),
Self(0b1_0111_001),
Self(0b1_0111_000), // -1
// -m*2^-1
Self(0b1_0110_111), // −0.9375
Self(0b1_0110_110),
Self(0b1_0110_101),
Self(0b1_0110_100),
Self(0b1_0110_011),
Self(0b1_0110_010),
Self(0b1_0110_001),
Self(0b1_0110_000), // -0.5
// -m*2^-2
Self(0b1_0101_111), // −0.46875
Self(0b1_0101_110),
Self(0b1_0101_101),
Self(0b1_0101_100),
Self(0b1_0101_011),
Self(0b1_0101_010),
Self(0b1_0101_001),
Self(0b1_0101_000), // -0.25
// -m*2^-3
Self(0b1_0100_111), // −0.234375
Self(0b1_0100_110),
Self(0b1_0100_101),
Self(0b1_0100_100),
Self(0b1_0100_011),
Self(0b1_0100_010),
Self(0b1_0100_001),
Self(0b1_0100_000), // -0.125
// -m*2^-4
Self(0b1_0011_111), // −0.1171875
Self(0b1_0011_110),
Self(0b1_0011_101),
Self(0b1_0011_100),
Self(0b1_0011_011),
Self(0b1_0011_010),
Self(0b1_0011_001),
Self(0b1_0011_000), // −0.0625
// -m*2^-5
Self(0b1_0010_111), // −0.05859375
Self(0b1_0010_110),
Self(0b1_0010_101),
Self(0b1_0010_100),
Self(0b1_0010_011),
Self(0b1_0010_010),
Self(0b1_0010_001),
Self(0b1_0010_000), // −0.03125
// -m*2^-6
Self(0b1_0001_111), // −0.029296875
Self(0b1_0001_110),
Self(0b1_0001_101),
Self(0b1_0001_100),
Self(0b1_0001_011),
Self(0b1_0001_010),
Self(0b1_0001_001),
Self(0b1_0001_000), // −0.015625
// -m*2^-7 subnormal numbers
Self(0b1_0000_111), // −0.013671875
Self(0b1_0000_110),
Self(0b1_0000_101),
Self(0b1_0000_100),
Self(0b1_0000_011),
Self(0b1_0000_010),
Self(0b1_0000_001), // −0.001953125
// Zeroes
Self(0b1_0000_000), // -0.0
Self(0b0_0000_000), // 0.0
// m*2^-7 // subnormal numbers
Self(0b0_0000_001),
Self(0b0_0000_010),
Self(0b0_0000_011),
Self(0b0_0000_100),
Self(0b0_0000_101),
Self(0b0_0000_110),
Self(0b0_0000_111), // 0.013671875
// m*2^-6
Self(0b0_0001_000), // 0.015625
Self(0b0_0001_001),
Self(0b0_0001_010),
Self(0b0_0001_011),
Self(0b0_0001_100),
Self(0b0_0001_101),
Self(0b0_0001_110),
Self(0b0_0001_111), // 0.029296875
// m*2^-5
Self(0b0_0010_000), // 0.03125
Self(0b0_0010_001),
Self(0b0_0010_010),
Self(0b0_0010_011),
Self(0b0_0010_100),
Self(0b0_0010_101),
Self(0b0_0010_110),
Self(0b0_0010_111), // 0.05859375
// m*2^-4
Self(0b0_0011_000), // 0.0625
Self(0b0_0011_001),
Self(0b0_0011_010),
Self(0b0_0011_011),
Self(0b0_0011_100),
Self(0b0_0011_101),
Self(0b0_0011_110),
Self(0b0_0011_111), // 0.1171875
// m*2^-3
Self(0b0_0100_000), // 0.125
Self(0b0_0100_001),
Self(0b0_0100_010),
Self(0b0_0100_011),
Self(0b0_0100_100),
Self(0b0_0100_101),
Self(0b0_0100_110),
Self(0b0_0100_111), // 0.234375
// m*2^-2
Self(0b0_0101_000), // 0.25
Self(0b0_0101_001),
Self(0b0_0101_010),
Self(0b0_0101_011),
Self(0b0_0101_100),
Self(0b0_0101_101),
Self(0b0_0101_110),
Self(0b0_0101_111), // 0.46875
// m*2^-1
Self(0b0_0110_000), // 0.5
Self(0b0_0110_001),
Self(0b0_0110_010),
Self(0b0_0110_011),
Self(0b0_0110_100),
Self(0b0_0110_101),
Self(0b0_0110_110),
Self(0b0_0110_111), // 0.9375
// m*2^0
Self(0b0_0111_000), // 1
Self(0b0_0111_001),
Self(0b0_0111_010),
Self(0b0_0111_011),
Self(0b0_0111_100),
Self(0b0_0111_101),
Self(0b0_0111_110),
Self(0b0_0111_111), // 1.875
// m*2^1
Self(0b0_1000_000), // 2
Self(0b0_1000_001),
Self(0b0_1000_010),
Self(0b0_1000_011),
Self(0b0_1000_100),
Self(0b0_1000_101),
Self(0b0_1000_110),
Self(0b0_1000_111), // 3.75
// m*2^2
Self(0b0_1001_000), // 4
Self(0b0_1001_001),
Self(0b0_1001_010),
Self(0b0_1001_011),
Self(0b0_1001_100),
Self(0b0_1001_101),
Self(0b0_1001_110),
Self(0b0_1001_111), // 7.5
// m*2^3
Self(0b0_1010_000), // 8
Self(0b0_1010_001),
Self(0b0_1010_010),
Self(0b0_1010_011),
Self(0b0_1010_100),
Self(0b0_1010_101),
Self(0b0_1010_110),
Self(0b0_1010_111), // 15
// m*2^4
Self(0b0_1011_000), // 16
Self(0b0_1011_001),
Self(0b0_1011_010),
Self(0b0_1011_011),
Self(0b0_1011_100),
Self(0b0_1011_101),
Self(0b0_1011_110),
Self(0b0_1011_111), // 30
// m*2^5
Self(0b0_1100_000), // 32
Self(0b0_1100_001),
Self(0b0_1100_010),
Self(0b0_1100_011),
Self(0b0_1100_100),
Self(0b0_1100_101),
Self(0b0_1100_110),
Self(0b0_1100_111), // 60
// m*2^6
Self(0b0_1101_000), // 64
Self(0b0_1101_001),
Self(0b0_1101_010),
Self(0b0_1101_011),
Self(0b0_1101_100),
Self(0b0_1101_101),
Self(0b0_1101_110),
Self(0b0_1101_111), // 120
// m*2^7
Self(0b0_1110_000), // 128
Self(0b0_1110_001),
Self(0b0_1110_010),
Self(0b0_1110_011),
Self(0b0_1110_100),
Self(0b0_1110_101),
Self(0b0_1110_110),
Self(0b0_1110_111), // 240
];
}
impl ops::Add for f8 {
type Output = Self;
fn add(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl ops::Sub for f8 {
type Output = Self;
fn sub(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl ops::Mul for f8 {
type Output = Self;
fn mul(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl ops::Div for f8 {
type Output = Self;
fn div(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl ops::Neg for f8 {
type Output = Self;
fn neg(self) -> Self::Output {
Self(self.0 ^ Self::SIGN_MASK)
}
}
impl ops::Rem for f8 {
type Output = Self;
fn rem(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl ops::AddAssign for f8 {
fn add_assign(&mut self, _rhs: Self) {
unimplemented!()
}
}
impl ops::SubAssign for f8 {
fn sub_assign(&mut self, _rhs: Self) {
unimplemented!()
}
}
impl ops::MulAssign for f8 {
fn mul_assign(&mut self, _rhs: Self) {
unimplemented!()
}
}
impl cmp::PartialEq for f8 {
fn eq(&self, other: &Self) -> bool {
if self.is_nan() || other.is_nan() {
false
} else if self.abs().to_bits() | other.abs().to_bits() == 0 {
true
} else {
self.0 == other.0
}
}
}
impl cmp::PartialOrd for f8 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let inf_rep = f8::EXP_MASK;
let a_abs = self.abs().to_bits();
let b_abs = other.abs().to_bits();
// If either a or b is NaN, they are unordered.
if a_abs > inf_rep || b_abs > inf_rep {
return None;
}
// If a and b are both zeros, they are equal.
if a_abs | b_abs == 0 {
return Some(Ordering::Equal);
}
let a_srep = self.to_bits_signed();
let b_srep = other.to_bits_signed();
let res = a_srep.cmp(&b_srep);
if a_srep & b_srep >= 0 {
// If at least one of a and b is positive, we get the same result comparing
// a and b as signed integers as we would with a fp_ting-point compare.
Some(res)
} else {
// Otherwise, both are negative, so we need to flip the sense of the
// comparison to get the correct result.
Some(res.reverse())
}
}
}
impl fmt::Display for f8 {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
unimplemented!()
}
}
impl fmt::Debug for f8 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Binary::fmt(self, f)
}
}
impl fmt::Binary for f8 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let v = self.0;
write!(
f,
"0b{:b}_{:04b}_{:03b}",
v >> 7,
(v & Self::EXP_MASK) >> Self::SIG_BITS,
v & Self::SIG_MASK
)
}
}
impl fmt::LowerHex for f8 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub const fn hf8(s: &str) -> f8 {
let Ok(bits) = libm::support::hex_float::parse_hex_exact(s, 8, 3) else {
panic!()
};
f8(bits as u8)
}
|