irox-fixedmath 0.2.0

Fixed Precision Math Primitives
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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

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

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

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

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, $sign:ty, $prim:ty, $lower_prim:ty, $shift:ident, $val:ident, $mask:ident) => {
        impl core::ops::Add<f64> for $typ {
            type Output = Self;
            fn add(self, rhs: f64) -> Self::Output {
                let v = <$typ>::from(rhs);
                self + v
            }
        }
        impl core::ops::AddAssign<f64> for $typ {
            fn add_assign(&mut self, rhs: f64) {
                *self = *self + rhs;
            }
        }
        impl core::ops::AddAssign<f64> for &mut $typ {
            fn add_assign(&mut self, rhs: f64) {
                **self = **self + rhs;
            }
        }
        impl core::ops::Sub<f64> for $typ {
            type Output = Self;

            fn sub(self, rhs: f64) -> Self::Output {
                let v = <$typ>::from(rhs);
                self - v
            }
        }
        impl irox_tools::f64::FloatExt for $typ {
            type Type = Self;
            type Size = $prim;

            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()
            }

            fn clamp(self, min: Self, max: Self) -> Self::Type {
                if self < min {
                    return min;
                } else if self > max {
                    return max;
                }
                self
            }

            ///
            /// 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 log10(self) -> Self::Type {
                self.ln() / Self::LN10
            }

            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())
            }
            fn to_bits(self) -> Self::Size {
                self.raw_value()
            }

            fn exponent(self) -> u16 {
                irox_tools::f64::FloatExt::exponent(self.as_f64())
            }

            fn significand(self) -> Self::Size {
                irox_tools::f64::FloatExt::significand(self.as_f64()) as $prim
            }

            fn sin(self) -> Self::Type {
                cordic(self).0
            }

            fn cos(self) -> Self::Type {
                cordic(self).1
            }
            fn tan(self) -> Self::Type {
                self.sin() / self.cos()
            }
            fn atan(self) -> Self::Type {
                todo!()
            }
            fn atan2(self, _o: Self) -> Self::Type {
                todo!()
            }
        }
        impl irox_tools::One for $typ {
            const ONE: Self = Self::from_parts(1, 0);
        }
        impl irox_tools::Zero for $typ {
            const ZERO: Self = Self::from_parts(0, 0);
        }
        impl irox_tools::ToF64 for $typ {
            fn to_f64(&self) -> f64 {
                self.as_f64()
            }
        }
        impl irox_tools::FromF64 for $typ {
            fn from_f64(value: f64) -> Self {
                value.into()
            }
        }
        impl irox_tools::ToSigned for $typ {
            type Output = $sign;
            fn to_signed(self) -> Self::Output {
                Self::Output {
                    data: self.data.cast(),
                }
            }
            fn negative_one() -> Self::Output {
                <$sign>::from_parts(-1, 0)
            }
        }
        impl irox_tools::PrimitiveMath for $typ {}
        impl irox_tools::FloatIsh for $typ {}
    };
}
macro_rules! impl_signed_flops {
    ($typ:ty, $prim:ty, $lower_prim:ty, $shift:ident, $val:ident, $mask:ident) => {
        impl core::ops::Add<f64> for $typ {
            type Output = Self;
            fn add(self, rhs: f64) -> Self::Output {
                let v = <$typ>::from(rhs);
                self + v
            }
        }
        impl core::ops::AddAssign<f64> for $typ {
            fn add_assign(&mut self, rhs: f64) {
                *self = *self + rhs;
            }
        }
        impl core::ops::AddAssign<f64> for &mut $typ {
            fn add_assign(&mut self, rhs: f64) {
                **self = **self + rhs;
            }
        }
        impl core::ops::Sub<f64> for $typ {
            type Output = Self;

            fn sub(self, rhs: f64) -> Self::Output {
                let v = <$typ>::from(rhs);
                self - v
            }
        }
        impl irox_tools::One for $typ {
            const ONE: Self = Self::from_parts(1, 0);
        }
        impl irox_tools::Zero for $typ {
            const ZERO: Self = Self::from_parts(0, 0);
        }
        impl irox_tools::ToF64 for $typ {
            fn to_f64(&self) -> f64 {
                self.as_f64()
            }
        }
        impl irox_tools::FromF64 for $typ {
            fn from_f64(value: f64) -> Self {
                value.into()
            }
        }
        impl irox_tools::ToSigned for $typ {
            type Output = Self;
            fn to_signed(self) -> Self::Output {
                self
            }
            fn negative_one() -> Self::Output {
                Self::from_parts(-1, 0)
            }
        }
        impl irox_tools::PrimitiveMath for $typ {}
        impl irox_tools::FloatIsh for $typ {}
        impl irox_tools::f64::FloatExt for $typ {
            type Type = Self;
            type Size = $prim;

            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 {
                let bm = $mask | ($mask << $shift);

                Self::from_raw_value(self.data & bm)
            }

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

            fn floor(self) -> Self::Type {
                todo!()
            }

            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()
            }

            fn clamp(self, min: Self, max: Self) -> Self::Type {
                if self < min {
                    return min;
                } else if self > max {
                    return max;
                }
                self
            }

            ///
            /// 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 log10(self) -> Self::Type {
                self.ln() / Self::LN10
            }
            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())
            }
            fn to_bits(self) -> Self::Size {
                self.raw_value()
            }

            fn exponent(self) -> u16 {
                irox_tools::f64::FloatExt::exponent(self.as_f64())
            }

            fn significand(self) -> Self::Size {
                irox_tools::f64::FloatExt::significand(self.as_f64()) as $prim
            }

            fn sin(self) -> Self::Type {
                cordic(self).0
            }

            fn cos(self) -> Self::Type {
                cordic(self).1
            }
            fn tan(self) -> Self::Type {
                self.sin() / self.cos()
            }
            fn atan(self) -> Self::Type {
                todo!()
            }
            fn atan2(self, _o: Self) -> Self::Type {
                todo!()
            }
        }
    };
}
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);
    pub const LN10: FixedU32 = FixedU32::from_parts(2, 19_830);
}
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, FixedI32, 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);
    pub const LN10: FixedU64 = FixedU64::from_parts(2, 1_299_593_075);
}
impl_base!(FixedU64, u64, u32, u128, U64_SHIFT, U64_VAL, U64_MASK);
impl_unsigned_flops!(FixedU64, FixedI64, 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);
    pub const LN10: FixedU128 = FixedU128::from_parts(2, 5_581_709_770_980_770_000);
}
impl_base!(FixedU128, u128, u64, u128, U128_SHIFT, U128_VAL, U128_MASK);
impl_unsigned_flops!(FixedU128, FixedI128, 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 FixedI128 {
    // pub const E: FixedI128 = FixedI128::from_parts(2, 13_249_961_062_380_153_450);
    pub const PI: FixedI128 = FixedI128::from_parts(3, 2_611_923_443_488_327_891);
    pub const ONE_HALF: FixedI128 = FixedI128::from_parts(0, 9_223_372_036_854_775_807);
    pub const RESOLUTION: FixedI128 = FixedI128::from_parts(0, 1);
    pub const LN10: FixedI128 = FixedI128::from_parts(2, 5_581_709_770_980_770_000);
}
impl_base!(FixedI128, i128, i64, i128, I128_SHIFT, I128_VAL, I128_MASK);
impl_signed_flops!(FixedI128, i128, i64, I128_SHIFT, I128_VAL, I128_MASK);
impl_prim_ops!(FixedI128, i64, u8);
impl_prim_ops!(FixedI128, i64, i8);
impl_prim_ops!(FixedI128, i64, u16);
impl_prim_ops!(FixedI128, i64, i16);
impl_prim_ops!(FixedI128, i64, u32);
impl_prim_ops!(FixedI128, i64, i32);
impl_prim_ops!(FixedI128, i64, u64);
impl_prim_ops!(FixedI128, i64, i64);

pub trait AsFixedPoint {
    fn as_fixed_u32(&self) -> FixedU32;
    fn as_fixed_i32(&self) -> FixedI32;
    fn as_fixed_u64(&self) -> FixedU64;
    fn as_fixed_i64(&self) -> FixedI64;
    fn as_fixed_u128(&self) -> FixedU128;
    fn as_fixed_i128(&self) -> FixedI128;
}
macro_rules! impl_as_fixedpt {
    ($ty:ty) => {
        impl AsFixedPoint for $ty {
            fn as_fixed_u32(&self) -> FixedU32 {
                FixedU32::from(*self)
            }

            fn as_fixed_i32(&self) -> FixedI32 {
                FixedI32::from(*self)
            }

            fn as_fixed_u64(&self) -> FixedU64 {
                FixedU64::from(*self)
            }

            fn as_fixed_i64(&self) -> FixedI64 {
                FixedI64::from(*self)
            }

            fn as_fixed_u128(&self) -> FixedU128 {
                FixedU128::from(*self)
            }

            fn as_fixed_i128(&self) -> FixedI128 {
                FixedI128::from(*self)
            }
        }
    };
}
impl_as_fixedpt!(f32);
impl_as_fixedpt!(f64);