irox_fixedmath/
lib.rs

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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! Fixed-precision mathematics.
//!

#![forbid(unsafe_code)]
#![no_std]

use core::cmp::Ordering;
use core::fmt::Formatter;
use core::str::FromStr;
pub use irox_tools::f64::FloatExt;

macro_rules! consts {
    ($prim:ty, $val:literal, $shift:ident, $valt:ident, $mask:ident) => {
        const $shift: $prim = $val;
        const $valt: $prim = 1 << $shift;
        const $mask: $prim = $valt - 1;
    };
}
consts!(u32, 16, U32_SHIFT, U32_VAL, U32_MASK);
consts!(i32, 16, I32_SHIFT, I32_VAL, I32_MASK);
consts!(u64, 32, U64_SHIFT, U64_VAL, U64_MASK);
consts!(i64, 32, I64_SHIFT, I64_VAL, I64_MASK);
consts!(u128, 64, U128_SHIFT, U128_VAL, U128_MASK);
consts!(i128, 64, I128_SHIFT, I128_VAL, I128_MASK);

macro_rules! impl_partial_cmp_eq {
    ($prim:ty, $($typ:tt)+) => {
        impl core::cmp::PartialEq<$prim> for $($typ)+ {
            fn eq(&self, other: &$prim) -> bool {
                <$prim>::from(*self) == *other
            }
        }
        impl core::cmp::PartialOrd<$prim> for $($typ)+ {
            fn partial_cmp(&self, other: &$prim) -> Option<core::cmp::Ordering> {
                <$prim>::partial_cmp(&self.into(), &other)
            }
        }
    };
}

macro_rules! impl_fromf64 {
    ($shift:ident, $val:ident, $mask:ident, $($typ:tt)+) => {
        impl From<$($typ)+> for f64 {
            fn from(value: $($typ)+) -> Self {
                let val = (value.data >> $shift) as f64;
                val + ((value.data & $mask) as f64 / $val as f64)
            }
        }
    };
}
macro_rules! impl_from {
    ($prim:ty, $shift:expr, $($typ:tt)+) => {
        impl From<$($typ)+> for $prim {
            fn from(value: $($typ)+) -> Self {
                value.data >> $shift
            }
        }
    };
}

macro_rules! impl_ops {
    ($strukt:ty, $prim:ty, $next_prim:ty, $shift:ident, $($typ:tt)+) => {
        impl core::ops::Add<$strukt> for $($typ)+ {
            type Output = $strukt;

            fn add(self, rhs: $strukt) -> Self::Output {
                <$strukt>::from_raw_value(self.data.saturating_add(rhs.data))
            }
        }
        impl core::ops::Sub<$strukt> for $($typ)+ {
            type Output = $strukt;

            fn sub(self, rhs: $strukt) -> Self::Output {
                <$strukt>::from_raw_value(self.data.saturating_sub(rhs.data))
            }
        }
        impl core::ops::AddAssign<$($typ)+> for $strukt {
            fn add_assign(&mut self, rhs: $($typ)+) {
                self.data = self.data.saturating_add(rhs.data)
            }
        }

        impl core::ops::AddAssign<$($typ)+> for &mut $strukt {
            fn add_assign(&mut self, rhs: $($typ)+) {
                self.data = self.data.saturating_add(rhs.data)
            }
        }
        impl core::ops::SubAssign<$($typ)+> for $strukt {
            fn sub_assign(&mut self, rhs: $($typ)+) {
                self.data = self.data.saturating_sub(rhs.data)
            }
        }

        impl core::ops::SubAssign<$($typ)+> for &mut $strukt {
            fn sub_assign(&mut self, rhs: $($typ)+) {
                self.data = self.data.saturating_sub(rhs.data)
            }
        }
        impl core::ops::Mul<$($typ)+> for $strukt {
            type Output = $strukt;

            fn mul(self, rhs: $($typ)+) -> Self::Output {
                let o = (self.data as $next_prim * rhs.data as $next_prim) >> ($shift - 1);
                let add = o & 0x01;
                let o = (o >> 1) + add;
                <$strukt>::from_raw_value(o as $prim)
            }
        }

        impl core::ops::Mul<$($typ)+> for &mut $strukt {
            type Output = $strukt;

            fn mul(self, rhs: $($typ)+) -> Self::Output {
                let o = ((self.data as $next_prim).saturating_mul(rhs.data as $next_prim)) >> ($shift - 1);
                let add = o & 0x01;
                let o = (o >> 1) + add;
                <$strukt>::from_raw_value(o as $prim)
            }
        }
        impl core::ops::Div<$($typ)+> for $strukt {
            type Output = $strukt;

            fn div(self, rhs: $($typ)+) -> Self::Output {
                let a = (self.data as $next_prim) << $shift;
                let b = (rhs.data as $next_prim);
                let o = (a / b) as $prim;
                <$strukt>::from_raw_value(o)
            }
        }
    };
}
macro_rules! impl_mut_ops {
    ($strukt:ty, $prim:ty, $next_prim:ty, $shift:ident) => {
        impl core::ops::MulAssign for $strukt {
            fn mul_assign(&mut self, rhs: $strukt) {
                self.data = core::ops::Mul::mul(*self, rhs).data;
            }
        }
        impl core::ops::MulAssign for &mut $strukt {
            fn mul_assign(&mut self, rhs: &mut $strukt) {
                self.data = core::ops::Mul::mul(**self, rhs).data;
            }
        }
        impl core::ops::DivAssign for $strukt {
            fn div_assign(&mut self, rhs: $strukt) {
                self.data = core::ops::Div::div(*self, rhs).data;
            }
        }
        impl core::ops::DivAssign for &mut $strukt {
            fn div_assign(&mut self, rhs: &mut $strukt) {
                self.data = core::ops::Div::div(**self, rhs).data;
            }
        }
    };
}
macro_rules! impl_fmt_as_f64 {
    ($typ:ty, $f:path) => {
        impl $f for $typ {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                <dyn $f>::fmt(&Into::<f64>::into(self), f)
            }
        }
    };
}
macro_rules! impl_fmt_as_inner {
    ($typ:ty, $f:path) => {
        impl $f for $typ {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                <dyn $f>::fmt(&self.data, f)
            }
        }
    };
}
macro_rules! impl_unsigned_flops {
    ($typ:ty, $prim:ty, $lower_prim:ty, $shift:ident, $val:ident, $mask:ident) => {
        impl irox_tools::f64::FloatExt for $typ {
            type Type = Self;

            fn trunc(self) -> Self::Type {
                // just mask out the fractional bits leaving the whole bits.
                Self::from_raw_value(self.data & ($mask << $shift))
            }

            fn fract(self) -> Self::Type {
                // just mask out the whole bits leaving the fractional bits.
                Self::from_raw_value(self.data & $mask)
            }

            fn abs(self) -> Self::Type {
                // no change for unsigned flops.
                self
            }

            fn round(self) -> Self::Type {
                (self + Self::Type::ONE_HALF).trunc()
            }

            fn floor(self) -> Self::Type {
                // same as trunc for unsigned
                self.trunc()
            }

            fn ceil(self) -> Self::Type {
                if Self::Type::fract(&self) == 0 {
                    return self;
                }
                Self::Type::from_parts(self.whole() + 1, 0)
            }

            fn signum(self) -> Self::Type {
                1.into()
            }

            ///
            /// Implementation of Exponential Function from NIST DTMF eq 4.2.19: `<https://dlmf.nist.gov/4.2.E19>`
            fn exp(self) -> Self::Type {
                let mut out = Self::from_parts(1, 0);
                let i = self;
                let mut idx = 1u16;
                let mut next = self;

                while next.abs() != 0.0 {
                    out += next;
                    idx += 1;
                    next *= i / idx;
                }

                out
            }

            ///
            /// Implementation of Natural Logarithm using NIST DLMF eq 4.6.4: `<https://dlmf.nist.gov/4.6.E4>`
            fn ln(self) -> Self::Type {
                let z = self;
                if z == 0. {
                    return Self::Type::from_parts(1, 0);
                }
                let iter = (z - 1u8) / (z + 1u8);
                let mut out = Self::Type::default();
                let mut next = iter * 2u8;
                let mut idx = 1 as $lower_prim;
                let mut base = iter;
                while !next.is_zero() {
                    out += next;
                    idx += 2;
                    base *= iter * iter;
                    next = (base * 2 as $lower_prim) / idx;
                }
                out
            }

            fn powi(self, val: i32) -> Self::Type {
                let mut out = self;
                let i = self;
                for _ in 0..val.abs() {
                    out *= i;
                }
                out
            }

            ///
            /// Implementation of general power function using NIST DLMF eq 4.2.26: `<https://dlmf.nist.gov/4.2.E26>`
            fn powf(self, a: Self::Type) -> Self::Type {
                let z = self;

                (a * z.ln()).exp()
            }

            fn sqrt(self) -> Self::Type {
                self.powf(0.5.into())
            }
        }
    };
}
macro_rules! impl_prim_ops {
    ($typ:ty, $prim:ty, $rhs:ty) => {
        impl core::ops::Add<$rhs> for $typ {
            type Output = Self;

            fn add(self, rhs: $rhs) -> Self::Output {
                self + Self::from_parts(rhs as $prim, 0)
            }
        }
        impl core::ops::Add<$typ> for $rhs {
            type Output = $typ;

            fn add(self, rhs: $typ) -> Self::Output {
                rhs + self
            }
        }
        impl core::ops::Sub<$rhs> for $typ {
            type Output = Self;

            fn sub(self, rhs: $rhs) -> Self::Output {
                self - Self::from_parts(rhs as $prim, 0)
            }
        }
        impl core::ops::Sub<$typ> for $rhs {
            type Output = $typ;

            fn sub(self, rhs: $typ) -> Self::Output {
                <$typ>::from_parts(self as $prim, 0) - rhs
            }
        }
        impl core::ops::Mul<$rhs> for $typ {
            type Output = Self;

            fn mul(self, rhs: $rhs) -> Self::Output {
                self * Self::from_parts(rhs as $prim, 0)
            }
        }
        impl core::ops::Mul<$typ> for $rhs {
            type Output = $typ;

            fn mul(self, rhs: $typ) -> Self::Output {
                rhs * self
            }
        }
        impl core::ops::Div<$rhs> for $typ {
            type Output = Self;

            fn div(self, rhs: $rhs) -> Self::Output {
                self / Self::from_parts(rhs as $prim, 0)
            }
        }
        impl core::ops::Div<$typ> for $rhs {
            type Output = $typ;

            fn div(self, rhs: $typ) -> Self::Output {
                <$typ>::from_parts(self as $prim, 0) / rhs
            }
        }
        impl core::cmp::PartialEq<$rhs> for $typ {
            fn eq(&self, other: &$rhs) -> bool {
                (*self).eq(&Self::from_parts(*other as $prim, 0))
            }
        }
        impl core::cmp::PartialEq<$typ> for $rhs {
            fn eq(&self, other: &$typ) -> bool {
                <$typ>::from_parts(*self as $prim, 0).eq(other)
            }
        }
        impl core::cmp::PartialOrd<$rhs> for $typ {
            fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
                self.partial_cmp(&Self::from_parts(*other as $prim, 0))
            }
        }
        impl core::cmp::PartialOrd<$typ> for $rhs {
            fn partial_cmp(&self, other: &$typ) -> Option<Ordering> {
                <$typ>::from_parts(*self as $prim, 0).partial_cmp(other)
            }
        }
    };
}
macro_rules! impl_base {
    ($typ:ty, $prim:ty, $lower_prim:ty, $next_prim:ty, $shift:ident, $val:ident, $mask:ident) => {
        impl_fmt_as_f64!($typ, core::fmt::Display);
        impl_fmt_as_f64!($typ, core::fmt::LowerExp);
        impl_fmt_as_f64!($typ, core::fmt::UpperExp);
        impl_fmt_as_inner!($typ, core::fmt::LowerHex);
        impl_fmt_as_inner!($typ, core::fmt::UpperHex);

        impl core::fmt::Debug for $typ {
            fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
                let w = self.whole();
                let p = self.fract();
                f.write_fmt(format_args!("{}: {}/{}", stringify!($typ), w, p))
            }
        }

        impl $typ {
            pub const fn from_parts(whole: $lower_prim, fracs: $lower_prim) -> Self {
                Self {
                    data: (whole as $prim) << $shift | (fracs as $prim),
                }
            }
            pub const fn from_raw_value(data: $prim) -> Self {
                Self { data }
            }
            pub const fn trunc(&self) -> $lower_prim {
                (self.data >> $shift) as $lower_prim
            }
            pub const fn whole(&self) -> $lower_prim {
                self.trunc()
            }
            pub const fn fract(&self) -> $lower_prim {
                (self.data & $mask) as $lower_prim
            }
            pub const fn raw_value(&self) -> $prim {
                self.data
            }
            pub fn as_f64(&self) -> f64 {
                self.into()
            }
            pub const fn is_zero(&self) -> bool {
                self.data == 0 as $prim
            }
        }

        impl From<$prim> for $typ {
            fn from(data: $prim) -> Self {
                Self {
                    data: data << $shift,
                }
            }
        }
        impl From<f64> for $typ {
            fn from(value: f64) -> Self {
                let w = irox_tools::f64::FloatExt::floor(value) as $lower_prim;
                let f = irox_tools::f64::FloatExt::fract(value) * <$lower_prim>::MAX as f64;
                let f = irox_tools::f64::FloatExt::round(f) as $lower_prim;
                Self::from_parts(w, f)
            }
        }
        impl From<f32> for $typ {
            fn from(value: f32) -> Self {
                From::<f64>::from(value as f64)
            }
        }
        impl FromStr for $typ {
            type Err = <f64 as core::str::FromStr>::Err;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                let v = <f64 as core::str::FromStr>::from_str(s)?;
                Ok(v.into())
            }
        }
        impl core::iter::Sum for $typ {
            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
                let mut out = Self::default();
                for v in iter {
                    out += v;
                }
                out
            }
        }
        impl<'a> core::iter::Sum<&'a $typ> for $typ {
            fn sum<I: Iterator<Item = &'a $typ>>(iter: I) -> Self {
                let mut out = Self::default();
                for v in iter {
                    out *= *v;
                }
                out
            }
        }
        impl core::iter::Product for $typ {
            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
                let mut out = Self::default();
                for v in iter {
                    out *= v;
                }
                out
            }
        }
        impl<'a> core::iter::Product<&'a $typ> for $typ {
            fn product<I: Iterator<Item = &'a $typ>>(iter: I) -> Self {
                let mut out = Self::default();
                for v in iter {
                    out *= *v;
                }
                out
            }
        }

        impl_fromf64!($shift, $val, $mask, $typ);
        impl_fromf64!($shift, $val, $mask, &$typ);
        impl_fromf64!($shift, $val, $mask, &mut $typ);
        impl_from!($prim, $shift, $typ);
        impl_from!($prim, $shift, &$typ);
        impl_from!($prim, $shift, &mut $typ);
        impl_ops!($typ, $prim, $next_prim, $shift, $typ);
        impl_ops!($typ, $prim, $next_prim, $shift, &$typ);
        impl_ops!($typ, $prim, $next_prim, $shift, &mut $typ);

        impl_mut_ops!($typ, $prim, $next_prim, $shift);

        impl_partial_cmp_eq!(f64, $typ);
    };
}

///
/// Fixed precision [`u32`] - upper 16 bits is value, lower 16 bits are the fractional portion of the
/// value.  Each fractional portion is 1/[`u16::MAX`] ~= `1.5259e-5` or `0.000_015_259`, or about
/// `15.3 micro`, and can accurately represent SI-prefixes: `milli/1e-3`. The
/// whole portion can represent `0` -> [`u16::MAX`] (`65_535`)
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FixedU32 {
    data: u32,
}
impl FixedU32 {
    pub const E: FixedU32 = FixedU32::from_parts(2, 47_073);
    pub const PI: FixedU32 = FixedU32::from_parts(3, 9_279);
    pub const ONE_HALF: FixedU32 = FixedU32::from_parts(0, 32_768);
    pub const RESOLUTION: FixedU32 = FixedU32::from_parts(0, 1);
}
impl_base!(FixedU32, u32, u16, u64, U32_SHIFT, U32_VAL, U32_MASK);
impl_prim_ops!(FixedU32, u16, u8);
impl_prim_ops!(FixedU32, u16, u16);
impl_unsigned_flops!(FixedU32, u32, u16, U32_SHIFT, U32_VAL, U32_MASK);

///
/// Fixed precision [`i32`] - upper 16 bits is value, lower 16 bits are the fractional portion of the
/// value.  Each fractional portion is 1/[`u16::MAX`] ~= `1.5259e-5` or `0.000_015_259`, or about
/// `15.3 micro`, and can accurately represent SI-prefixes: `milli/1e-3`. The
/// whole portion can represent [`i16::MIN`] (`-32768`) -> [`i16::MAX`] (`32_727`)
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FixedI32 {
    data: i32,
}
impl_base!(FixedI32, i32, i16, i64, I32_SHIFT, I32_VAL, I32_MASK);
impl_prim_ops!(FixedI32, i16, u8);
impl_prim_ops!(FixedI32, i16, u16);

///
/// Fixed precision [`u64`] - upper 32 bits is value, lower 32 bits are the fractional portion of the
/// value.  Each fractional portion is 1/[`u32::MAX`] ~= `2.328306e-10` or `0.000_000_000_238_306`,
/// or about `238.3 pico`, and can accurately represent SI-prefixes `milli/1e-3`, `micro/1e-6`, and
/// `nano/1e-9`. The whole portion can represent `0` -> [`u32::MAX`] (`4_294_967_295`)
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FixedU64 {
    data: u64,
}
impl FixedU64 {
    pub const E: FixedU64 = FixedU64::from_parts(2, 3_084_996_962);
    pub const PI: FixedU64 = FixedU64::from_parts(3, 608_135_816);
    pub const ONE_HALF: FixedU64 = FixedU64::from_parts(0, 2_147_483_648);
    pub const RESOLUTION: FixedU64 = FixedU64::from_parts(0, 1);
}
impl_base!(FixedU64, u64, u32, u128, U64_SHIFT, U64_VAL, U64_MASK);
impl_unsigned_flops!(FixedU64, u64, u32, U64_SHIFT, U64_VAL, U64_MASK);
impl_prim_ops!(FixedU64, u32, u8);
impl_prim_ops!(FixedU64, u32, u16);
impl_prim_ops!(FixedU64, u32, u32);

///
/// Fixed precision [`i64`] - upper 32 bits is value, lower 32 bits are the fractional portion of the
/// value.  Each fractional portion is 1/[`u32::MAX`] ~= `2.328306e-10` or `0.000_000_000_238_306`,
/// or about `238.3 pico`, and can accurately represent SI-prefixes `milli/1e-3`, `micro/1e-6`, and
/// `nano/1e-9`. The whole portion can represent [`i32::MIN`] (`-2_147_483_648`) -> [`i32::MAX`] (`2_147_483_647`)
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FixedI64 {
    data: i64,
}
impl_base!(FixedI64, i64, i32, i128, I64_SHIFT, I64_VAL, I64_MASK);
impl_prim_ops!(FixedI64, i32, u8);
impl_prim_ops!(FixedI64, i32, u16);
impl_prim_ops!(FixedI64, i32, u32);

///
/// Fixed precision [`u128`] - upper 64 bits is value, lower 64 bits are the fractional portion of the
/// value.  Each fractional portion is 1/[`u64::MAX`] ~= `5.4210e-20` or
/// `0.000_000_000_000_000_000_054_210`, or about `54.2 zepto`, and can
/// accurately represent SI-prefixes `milli/1e-3`, `micro/1e-6`, `nano/1e-9`,
/// `pico/1e-12`, `femto/1e-15`, and `atto/1e-18`.  This is probably overkill
/// for what you need.  The whole portion can represent `0` -> [`u64::MAX`] (`1.84467E+19`)
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FixedU128 {
    data: u128,
}
impl FixedU128 {
    pub const E: FixedU128 = FixedU128::from_parts(2, 13_249_961_062_380_153_450);
    pub const PI: FixedU128 = FixedU128::from_parts(3, 2_611_923_443_488_327_891);
    pub const ONE_HALF: FixedU128 = FixedU128::from_parts(0, 9_223_372_036_854_775_808);
    pub const RESOLUTION: FixedU128 = FixedU128::from_parts(0, 1);
}
impl_base!(FixedU128, u128, u64, u128, U128_SHIFT, U128_VAL, U128_MASK);
//impl_unsigned_flops!(FixedU128, u128, u64, U128_SHIFT, U128_VAL, U128_MASK);
impl_prim_ops!(FixedU128, u64, u8);
impl_prim_ops!(FixedU128, u64, u16);
impl_prim_ops!(FixedU128, u64, u32);
impl_prim_ops!(FixedU128, u64, u64);

///
/// Fixed precision [`u128`] - upper 64 bits is value, lower 64 bits are the fractional portion of the
/// value.  Each fractional portion is 1/[`i64::MAX`] ~= `5.4210e-20` or
/// `0.000_000_000_000_000_000_054_210`, or about `54.2 zepto`, and can
/// accurately represent SI-prefixes `milli/1e-3`, `micro/1e-6`, `nano/1e-9`,
/// `pico/1e-12`, `femto/1e-15`, and `atto/1e-18`.  This is probably overkill
/// for what you need.  The whole portion can represent
/// [`i64::MIN`] (`-9_223_372_036_854_775_808`) -> [`i64::MAX`] (`9_223_372_036_854_775_807`)
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FixedI128 {
    data: i128,
}
impl_base!(FixedI128, i128, i64, i128, I128_SHIFT, I128_VAL, I128_MASK);