fracints_internals 0.2.0

Internal utilities for the `fracints` system of crates
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
#[macro_export]
macro_rules! impl_signed {
    (
        $ty:ident,
        $s:expr,
        $iX:ident,
        $uX:ident,
        $to_string:ident,
        $from_str:ident,
        $normalized_mul:expr,
        $normalized_div:expr,
        $sqrt_fast:expr,
        $n:expr,
        $to_int:ident,
        $c:expr
    ) => {
        // TODO make inner type private, not doing this currently because we need const
        // traits
        /// Signed Fractional Integer of the unit range.
        ///
        /// Note: in the future we want to make the inner type private, but not
        /// currently because we don't have stable const traits.
        #[allow(non_camel_case_types)]
        #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
        #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $ty(pub $iX);

        impl Fracint for $ty {
            type Int = $iX;

            const BITS: usize = $uX::BITS as usize;
            const MAX: Self = Self($iX::MAX);
            const MIN: Self = Self($iX::MIN);
            const NEG_ONE: Self = Self(-$iX::MAX);
            const ONE: Self = Self($iX::MAX);
            const SIGNED: bool = true;
            const ULP: Self = Self(1);
            const ZERO: Self = Self(0);

            fn from_int(x: Self::Int) -> Self {
                Self(x)
            }

            fn as_int(self) -> Self::Int {
                self.0
            }

            fn overflowing_abs(self) -> (Self, bool) {
                (self.wrapping_abs(), self == Self::MIN)
            }

            fn wrapping_neg(self) -> Self {
                Self(self.0.wrapping_neg())
            }

            fn overflowing_neg(self) -> (Self, bool) {
                (self.wrapping_neg(), self == Self::MIN)
            }

            fn saturating_neg(self) -> Self {
                self.checked_neg().unwrap_or(Self::ONE)
            }

            fn saturating_inv(self) -> $iX {
                $iX::MAX.checked_div(self.0).unwrap_or(-1)
            }

            fn wrapping_add(self, rhs: Self) -> Self {
                Self(self.0.wrapping_add(rhs.0))
            }

            fn overflowing_add(self, rhs: Self) -> (Self, bool) {
                let (internal, overflow) = self.0.overflowing_add(rhs.0);
                if internal == $iX::MIN {
                    (Self(internal), true)
                } else {
                    (Self(internal), overflow)
                }
            }

            fn saturating_add(self, rhs: Self) -> Self {
                // note that $ty::MAX added to 0 does not overflow but $ty::MIN added to 0 does
                // overflow, and $ty::MIN.wrapping_add($ty::ZERO) and
                // $ty::ZERO.wrapping_add($ty::MIN) both overflow which means that
                // `rhs <= $ty::ZERO` catches all the cases
                self.checked_add(rhs).unwrap_or_else(|| {
                    if rhs <= Self::ZERO {
                        Self::NEG_ONE
                    } else {
                        Self::ONE
                    }
                })
            }

            fn wrapping_sub(self, rhs: Self) -> Self {
                Self(self.0.wrapping_sub(rhs.0))
            }

            fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
                let (internal, overflow) = self.0.overflowing_sub(rhs.0);
                if internal == $iX::MIN {
                    (Self(internal), true)
                } else {
                    (Self(internal), overflow)
                }
            }

            fn saturating_sub(self, rhs: Self) -> Self {
                self.checked_sub(rhs).unwrap_or_else(|| {
                    if rhs <= Self::ZERO {
                        Self::ONE
                    } else {
                        Self::NEG_ONE
                    }
                })
            }

            fn wrapping_mul(self, rhs: Self) -> Self {
                Self($normalized_mul(self.0, rhs.0))
            }

            fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
                (
                    self.wrapping_mul(rhs),
                    self == Self::MIN && rhs == Self::MIN,
                )
            }

            fn saturating_mul(self, rhs: Self) -> Self {
                if self == Self::MIN && rhs == Self::MIN {
                    Self::ONE
                } else {
                    self.wrapping_mul(rhs)
                }
            }

            fn saturating_mul_int(self, rhs: $iX) -> Self {
                let mut res = Self(self.0.saturating_mul(rhs));
                if res == Self::MIN {
                    res = Self::NEG_ONE;
                }
                res
            }

            fn saturating_div(self, rhs: Self) -> Self {
                if rhs == Self::ZERO {
                    self.signum()
                } else if self.saturating_abs() >= rhs.saturating_abs() {
                    //self == rhs == 0 eliminated above
                    if (self < Self::ZERO) != (rhs < Self::ZERO) {
                        Self::NEG_ONE
                    } else {
                        Self::ONE
                    }
                } else {
                    Self($normalized_div(self.0, rhs.0))
                }
            }

            fn saturating_div_int(self, rhs: Self::Int) -> Self {
                if rhs == 0 {
                    self.signum()
                } else if (self == Self::MIN) && (rhs == -1) {
                    Self::ONE
                } else {
                    Self(self.0.wrapping_div(rhs))
                }
            }

            fn sqrt_fast(self) -> Self {
                $sqrt_fast(self)
            }

            /// Generates a random fracint from the given entropy.
            /// Note: if `fiN::MIN` is generated, `fiN::ZERO` is returned instead
            ///
            /// # Examples
            ///
            /// ```
            /// use fracints::*;
            ///
            /// let mut rng = rand::rng();
            /// println!("{}", fi128::rand(&mut rng));
            /// ```
            #[cfg(feature = "rand_support")]
            fn rand<R: rand_core::Rng + ?Sized>(rng: &mut R) -> Self {
                // TODO this seems to be slow in some cases, use `next_u32` and `next_u64` when
                // possible
                let mut dst = Self::ZERO.0.to_le_bytes();
                rng.fill_bytes(&mut dst);
                let x = Self(Self::Int::from_le_bytes(dst));
                if x == Self::MIN { Self::ZERO } else { x }
            }

            fn from_f32(f: f32) -> Option<Self> {
                if f.abs() > 1.0 {
                    return None
                }
                if f == 1.0 {
                    return Some(Self::ONE);
                } else if f == -1.0 {
                    return Some(Self::NEG_ONE);
                }
                let mut f = F32::from_f32(f);
                let mut x: FP<inlawi_ty!($n)> = FP::new(true, InlAwi::zero(), $n - 1).unwrap();
                FP::truncate_(&mut x, &mut f);
                Some(Self::from_int(x.$to_int()))
            }

            fn from_f64(f: f64) -> Option<Self> {
                if f.abs() > 1.0 {
                    return None
                }
                if f == 1.0 {
                    return Some(Self::ONE);
                } else if f == -1.0 {
                    return Some(Self::NEG_ONE);
                }
                let mut f = F64::from_f64(f);
                let mut x: FP<inlawi_ty!($n)> = FP::new(true, InlAwi::zero(), $n - 1).unwrap();
                FP::truncate_(&mut x, &mut f);
                Some(Self::from_int(x.$to_int()))
            }

            fn to_f32(self) -> f32 {
                let mut f: FP<inlawi_ty!($n)> =
                    FP::new(true, InlAwi::from(self.as_int()), $n - 1).unwrap();
                // the msnb is never greater than 2^0 so is never anywhere near unrepresentable
                FP::try_to_f32(&mut f).unwrap()
            }

            fn to_f64(self) -> f64 {
                let mut f: FP<inlawi_ty!($n)> =
                    FP::new(true, InlAwi::from(self.as_int()), $n - 1).unwrap();
                // the msnb is never greater than 2^0 so is never anywhere near unrepresentable
                FP::try_to_f64(&mut f).unwrap()
            }
        }

        impl $ty {
            // TODO
            #[doc(hidden)]
            pub fn __todo() {
                let _ = (
                    $c.num_4divtau,
                    $c.num_4divtau_sqr,
                    $c.cos_taylor_iters,
                    $c.sin_taylor_iters,
                );
            }
            /*
            // TODO I suspect that the polynomial method should work a lot better with cos_sin,
            // I have seen rational beziers get 0.0004 before, and more segments with
            // parameterization correction common to each should get full precision.

            /// This is a slower
            /// Intended to only be called for `-0.5 <= self <= 0.5`. This function has
            /// overflows outside of this range.
            fn cos_taudiv4_taylor_base(self) -> $ty {
                // This is based on the equation
                // cos((tau/4) * x) =
                // ( (4/tau)^2 - (x^2)/2 ) / (4/tau)^2
                // + x^4 / ( (4/tau)^4 * 4! )
                // - x^6 / ( (4/tau)^6 * 6! )
                // + x^8 / ( (4/tau)^8 * 8! )
                // - ...

                // rounding to one and preventing overflow
                let cutoff = (self.0 as $uX) >> (Self::BITS / 2);
                if cutoff == 0 || cutoff == ($uX::MAX >> (Self::BITS / 2)) {
                    return Self::ONE;
                }

                let theta_sqr = self * self;
                let mut sum = $c.num_4divtau_sqr - (theta_sqr / 2);
                // overflow cannot happen due to the cutoff around zero
                sum /= $c.num_4divtau_sqr;
                // this completes the first term, which has to be computed differently from the
                // others

                let mut factorial_num: $iX = 2;
                let mut factorial_mul: $iX = 2;
                let mut numerator = theta_sqr;
                let mut num_4divtau_mul = $c.num_4divtau_sqr;
                for i in 0..$c.cos_taylor_iters {
                    // update multipliers
                    num_4divtau_mul *= $c.num_4divtau_sqr;
                    numerator *= theta_sqr;
                    factorial_num += 1;
                    factorial_mul *= factorial_num;
                    factorial_num += 1;
                    factorial_mul *= factorial_num;
                    // The strategy is to first divide the numerator by the factorial, then
                    // divide by the fiN.
                    let temp0 = numerator / factorial_mul;
                    if temp0 == $ty::ZERO {
                        return sum;
                    }
                    let temp1 = temp0 / num_4divtau_mul;
                    if (i & 0b1) == 0 {
                        sum += temp1;
                    } else {
                        sum -= temp1;
                    }
                }
                sum
            }

            /// Intended to only be called for `-0.5 <= theta <= 0.5`. This function has
            /// overflows outside of this range.
            fn sin_taudiv4_taylor_base(self) -> $ty {
                // This is based on the equation
                // sin((tau/4) * x) =
                // ( x*((4/tau)^2) - (x^3)/6 ) / (4/tau)^3
                // + x^5 / ( (4/tau)^5 * 5! )
                // - x^7 / ( (4/tau)^7 * 7! )
                // + x^9 / ( (4/tau)^9 * 9! )
                // - ...

                // Early return. The numerator has two `x` in it, so no other checking.
                if self == $ty::ZERO {
                    return $ty::ZERO;
                }
                let theta_sqr = self * self;
                let mut numerator = theta_sqr * self;
                let mut sum = (self * $c.num_4divtau_sqr) - (numerator / 6);
                let mut num_4divtau_mul = $c.num_4divtau_sqr * $c.num_4divtau;
                // `sum` cannot be more than `num_4divtau_mul`, assuming the range above
                sum /= num_4divtau_mul;
                // this completes the first term, which has to be computed differently from the
                // others

                let mut factorial_num: $iX = 3;
                let mut factorial_mul: $iX = 6;
                for i in 0..$c.sin_taylor_iters {
                    // update multipliers
                    num_4divtau_mul *= $c.num_4divtau_sqr;
                    numerator *= theta_sqr;
                    factorial_num += 1;
                    factorial_mul *= factorial_num;
                    factorial_num += 1;
                    factorial_mul *= factorial_num;

                    let temp0 = numerator / factorial_mul;
                    if temp0 == $ty::ZERO {
                        return sum;
                    }
                    let temp1 = temp0 / num_4divtau_mul;
                    if (i & 0b1) == 0 {
                        sum += temp1;
                    } else {
                        sum -= temp1;
                    }
                }
                sum
            }

            /// Calculates `cos((tau/4) * theta)` or `cos((pi/2) * theta)`.
            /// By having a (tau/4) constant and cleverly rearranging the taylor series,
            /// this provides a basic way to calculate cosine for fracints.
            /// Max Error:
            /// TODO ULPS
            pub fn cos_taudiv4_taylor(self) -> $ty {
                if self >= ($ty::MIN / -2) {
                    -self.wrapping_add($ty::MIN).sin_taudiv4_taylor_base()
                } else if self >= ($ty::MIN / 2) {
                    self.cos_taudiv4_taylor_base()
                } else {
                    self.wrapping_sub($ty::MIN).sin_taudiv4_taylor_base()
                }
            }

            /// Calculates `sin((tau/4) * theta)` or `sin((pi/2) * theta)`.
            /// By having a (tau/4) constant and cleverly rearranging the taylor series,
            /// this provides a basic way to calculate sine for fracints.
            /// Max Error:
            /// TODO ULPS
            pub fn sin_taudiv4_taylor(self) -> $ty {
                if self >= ($ty::MIN / -2) {
                    self.wrapping_add($ty::MIN).cos_taudiv4_taylor_base()
                } else if self >= ($ty::MIN / 2) {
                    self.sin_taudiv4_taylor_base()
                } else {
                    -self.wrapping_sub($ty::MIN).cos_taudiv4_taylor_base()
                }
            }

            pub fn cos_sin_pi_taylor(self) -> ($ty, $ty) {
                // this compares the highest two bits of `self` offset by a eighth of a circle
                // to determine which combination to use.
                let o = self.wrapping_add($ty::MIN / -4).0 as $uX;
                match (
                    (o & (1 << ($ty::BITS - 2))) != 0,
                    (o & (1 << ($ty::BITS - 1))) != 0,
                ) {
                    (false, false) => {
                        let t = self * 2;
                        (t.cos_taudiv4_taylor_base(), t.sin_taudiv4_taylor_base())
                    }
                    (true, false) => {
                        let t = self.wrapping_sub($ty::MIN / -2) * 2;
                        (-t.sin_taudiv4_taylor_base(), t.cos_taudiv4_taylor_base())
                    }
                    (false, true) => {
                        let t = self.wrapping_add($ty::MIN) * 2;
                        (-t.cos_taudiv4_taylor_base(), -t.sin_taudiv4_taylor_base())
                    }
                    (true, true) => {
                        let t = self.wrapping_add($ty::MIN / -2) * 2;
                        (t.sin_taudiv4_taylor_base(), -t.cos_taudiv4_taylor_base())
                    }
                }
            }
            */
        }

        impl fmt::Debug for $ty {
            /// Converts to a base 10 string representation
            ///
            /// `fiN::ONE` and `fiN::NEG_ONE` are special cased to "1.0" and "-1.0"
            /// respectively.
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                // TODO use constant sized buffers
                write!(f, "{}({})", stringify!($ty), $to_string(self.0))
            }
        }

        impl fmt::Display for $ty {
            /// Converts to a base 10 string representation
            ///
            /// `fiN::ONE` and `fiN::NEG_ONE` are special cased to "1.0" and "-1.0"
            /// respectively.
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                // TODO use constant sized buffers
                write!(f, "{}", $to_string(self.0))
            }
        }

        impl FromStr for $ty {
            type Err = FracintSerdeError;

            // TODO have a function for determining max number of chars with respect to
            // radix

            /// Conversion from a string representation.
            ///
            /// The input can start with a '-' to make the output negative. Then it can
            /// optionally start with a "0b", "0o", or a "0x" prefix to use radix 2, 8,
            /// or 16 respectively, otherwise it is parsed as radix 10. There must
            /// be an integer part with digits in the correct radix. If including
            /// the fraction, a '.' followed by one more digits in the correct radix
            /// should be added. Finally, an exponent can be added by 'e' or 'p'
            /// (except radix 16 which must use 'p') and then a number in the same
            /// radix is used. The exponent is applied as `* radix^exponent` before
            /// round-to-even. '_'s can be used throughout the integer, fraction,
            /// and exponent parts as long as one term is not all underspaces.
            ///
            /// `s` can be arbitrarily long but significance changes stops after a
            /// number of chars.
            ///
            /// The number must be in the range `(-1.0,1.0)` or else an overflow error
            /// is returned. 1.0 is special cased to map to `fiN::ONE' even though
            /// it is not exactly representable, and -1.0 is special cased to map to
            /// `fiN::NEG_ONE` to avoid introducing `fiN::MIN`.
            ///
            /// See the [FracintParseError] documentation for parsing errors and
            /// examples.
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                $from_str(s).map(|x| Self(x))
            }
        }

        impl Neg for $ty {
            type Output = Self;

            fn neg(self) -> Self::Output {
                self.saturating_neg()
            }
        }

        impl Add for $ty {
            type Output = Self;

            fn add(self, rhs: Self) -> Self::Output {
                self.saturating_add(rhs)
            }
        }

        impl AddAssign for $ty {
            fn add_assign(&mut self, rhs: Self) {
                *self = *self + rhs;
            }
        }

        impl Sub for $ty {
            type Output = Self;

            fn sub(self, rhs: Self) -> Self::Output {
                self.saturating_sub(rhs)
            }
        }

        impl SubAssign for $ty {
            fn sub_assign(&mut self, rhs: Self) {
                *self = *self - rhs;
            }
        }

        impl Mul for $ty {
            type Output = Self;

            fn mul(self, rhs: Self) -> Self::Output {
                self.saturating_mul(rhs)
            }
        }

        impl Mul<$iX> for $ty {
            type Output = Self;

            fn mul(self, rhs: $iX) -> Self::Output {
                self.saturating_mul_int(rhs)
            }
        }

        impl MulAssign for $ty {
            fn mul_assign(&mut self, rhs: Self) {
                *self = *self * rhs;
            }
        }

        impl MulAssign<$iX> for $ty {
            fn mul_assign(&mut self, rhs: $iX) {
                *self = *self * rhs;
            }
        }

        impl Div for $ty {
            type Output = $ty;

            fn div(self, rhs: Self) -> Self {
                self.saturating_div(rhs)
            }
        }

        impl Div<$iX> for $ty {
            type Output = Self;

            fn div(self, rhs: $iX) -> Self::Output {
                self.saturating_div_int(rhs)
            }
        }

        impl DivAssign for $ty {
            fn div_assign(&mut self, rhs: Self) {
                *self = *self / rhs;
            }
        }

        impl DivAssign<$iX> for $ty {
            fn div_assign(&mut self, rhs: $iX) {
                *self = *self / rhs;
            }
        }

        impl Shl<usize> for $ty {
            type Output = Self;

            fn shl(self, rhs: usize) -> Self {
                $ty(self.0 << rhs)
            }
        }

        impl ShlAssign<usize> for $ty {
            fn shl_assign(&mut self, rhs: usize) {
                self.0 <<= rhs
            }
        }

        impl Shr<usize> for $ty {
            type Output = Self;

            fn shr(self, rhs: usize) -> Self {
                $ty(self.0 >> rhs)
            }
        }

        impl ShrAssign<usize> for $ty {
            fn shr_assign(&mut self, rhs: usize) {
                self.0 >>= rhs
            }
        }

        impl Not for $ty {
            type Output = Self;

            fn not(self) -> Self {
                $ty(!self.0)
            }
        }

        impl BitOr for $ty {
            type Output = Self;

            fn bitor(self, rhs: Self) -> Self {
                $ty(self.0 | rhs.0)
            }
        }

        impl BitOrAssign for $ty {
            fn bitor_assign(&mut self, rhs: Self) {
                *self = *self | rhs;
            }
        }

        impl BitAnd for $ty {
            type Output = Self;

            fn bitand(self, rhs: Self) -> Self {
                $ty(self.0 & rhs.0)
            }
        }

        impl BitAndAssign for $ty {
            fn bitand_assign(&mut self, rhs: Self) {
                *self = *self & rhs;
            }
        }

        impl BitXor for $ty {
            type Output = Self;

            fn bitxor(self, rhs: Self) -> Self {
                $ty(self.0 ^ rhs.0)
            }
        }

        impl BitXorAssign for $ty {
            fn bitxor_assign(&mut self, rhs: Self) {
                *self = *self ^ rhs;
            }
        }

        impl Sum for $ty {
            fn sum<I>(iter: I) -> Self
            where
                I: Iterator<Item = Self>,
            {
                iter.fold(Self::ZERO, Add::add)
            }
        }

        impl Product for $ty {
            fn product<I>(iter: I) -> Self
            where
                I: Iterator<Item = Self>,
            {
                iter.fold(Self::ONE, Mul::mul)
            }
        }

        impl<'a> Sum<&'a $ty> for $ty {
            fn sum<I>(iter: I) -> Self
            where
                I: Iterator<Item = &'a Self>,
            {
                iter.fold(Self::ZERO, |acc, elem| acc + *elem)
            }
        }

        impl<'a> Product<&'a $ty> for $ty {
            fn product<I>(iter: I) -> Self
            where
                I: Iterator<Item = &'a Self>,
            {
                iter.fold(Self::ONE, |acc, elem| acc * *elem)
            }
        }
    };
}