fixed-bigint 0.2.5

Fixed-size big integer implementation for Rust
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
use super::{const_div_rem, const_mul, maybe_panic, FixedUInt, MachineWord, PanicReason};
use crate::const_numtraits::{
    ConstBounded, ConstCheckedDiv, ConstCheckedMul, ConstCheckedRem, ConstOverflowingMul,
    ConstSaturatingMul, ConstWrappingMul, ConstZero,
};
use crate::machineword::ConstMachineWord;

impl<T: MachineWord, const N: usize> num_traits::ops::overflowing::OverflowingMul
    for FixedUInt<T, N>
{
    fn overflowing_mul(&self, other: &Self) -> (Self, bool) {
        <Self as ConstOverflowingMul>::overflowing_mul(self, other)
    }
}

c0nst::c0nst! {
    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstOverflowingMul for FixedUInt<T, N> {
        fn overflowing_mul(&self, other: &Self) -> (Self, bool) {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::WORD_BITS);
            (Self { array }, overflow)
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstWrappingMul for FixedUInt<T, N> {
        fn wrapping_mul(&self, other: &Self) -> Self {
            let (array, _) = const_mul::<T, N, false>(&self.array, &other.array, Self::WORD_BITS);
            Self { array }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstCheckedMul for FixedUInt<T, N> {
        fn checked_mul(&self, other: &Self) -> Option<Self> {
            let (res, overflow) = <Self as ConstOverflowingMul>::overflowing_mul(self, other);
            if overflow { None } else { Some(res) }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstSaturatingMul for FixedUInt<T, N> {
        fn saturating_mul(&self, other: &Self) -> Self {
            let (res, overflow) = <Self as ConstOverflowingMul>::overflowing_mul(self, other);
            if overflow { <Self as ConstBounded>::max_value() } else { res }
        }
    }
}

c0nst::c0nst! {
    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Mul for FixedUInt<T, N> {
        type Output = Self;
        fn mul(self, other: Self) -> Self::Output {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            Self { array }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Mul<&FixedUInt<T, N>> for FixedUInt<T, N> {
        type Output = Self;
        fn mul(self, other: &FixedUInt<T, N>) -> Self::Output {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            Self { array }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Mul<FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn mul(self, other: FixedUInt<T, N>) -> Self::Output {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::Output::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            FixedUInt { array }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Mul<&FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn mul(self, other: &FixedUInt<T, N>) -> Self::Output {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::Output::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            FixedUInt { array }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Mul<&&FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn mul(self, other: &&FixedUInt<T, N>) -> Self::Output {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::Output::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            FixedUInt { array }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::MulAssign for FixedUInt<T, N> {
        fn mul_assign(&mut self, other: Self) {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            *self = Self { array };
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::MulAssign<&FixedUInt<T, N>> for FixedUInt<T, N> {
        fn mul_assign(&mut self, other: &FixedUInt<T, N>) {
            let (array, overflow) = const_mul::<T, N, true>(&self.array, &other.array, Self::WORD_BITS);
            if overflow {
                maybe_panic(PanicReason::Mul);
            }
            *self = Self { array };
        }
    }
}

// num_traits wrappers - delegate to const versions
impl<T: MachineWord, const N: usize> num_traits::WrappingMul for FixedUInt<T, N> {
    fn wrapping_mul(&self, other: &Self) -> Self {
        <Self as ConstWrappingMul>::wrapping_mul(self, other)
    }
}

impl<T: MachineWord, const N: usize> num_traits::CheckedMul for FixedUInt<T, N> {
    fn checked_mul(&self, other: &Self) -> Option<Self> {
        <Self as ConstCheckedMul>::checked_mul(self, other)
    }
}

impl<T: MachineWord, const N: usize> num_traits::ops::saturating::SaturatingMul
    for FixedUInt<T, N>
{
    fn saturating_mul(&self, other: &Self) -> Self {
        <Self as ConstSaturatingMul>::saturating_mul(self, other)
    }
}

c0nst::c0nst! {
    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Div for FixedUInt<T, N> {
        type Output = Self;
        fn div(self, other: Self) -> Self::Output {
            Self { array: const_div_rem(&self.array, &other.array).0 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Div<&FixedUInt<T, N>> for FixedUInt<T, N> {
        type Output = Self;
        fn div(self, other: &FixedUInt<T, N>) -> Self::Output {
            Self { array: const_div_rem(&self.array, &other.array).0 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Div<FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn div(self, other: FixedUInt<T, N>) -> Self::Output {
            Self::Output { array: const_div_rem(&self.array, &other.array).0 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Div<&FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn div(self, other: &FixedUInt<T, N>) -> Self::Output {
            Self::Output { array: const_div_rem(&self.array, &other.array).0 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::DivAssign for FixedUInt<T, N> {
        fn div_assign(&mut self, other: Self) {
            self.array = const_div_rem(&self.array, &other.array).0;
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::DivAssign<&FixedUInt<T, N>> for FixedUInt<T, N> {
        fn div_assign(&mut self, other: &FixedUInt<T, N>) {
            self.array = const_div_rem(&self.array, &other.array).0;
        }
    }
}

c0nst::c0nst! {
    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstCheckedDiv for FixedUInt<T, N> {
        fn checked_div(&self, other: &Self) -> Option<Self> {
            if <Self as ConstZero>::is_zero(other) {
                None
            } else {
                Some(*self / *other)
            }
        }
    }
}

impl<T: MachineWord, const N: usize> num_traits::CheckedDiv for FixedUInt<T, N> {
    fn checked_div(&self, other: &Self) -> Option<Self> {
        <Self as ConstCheckedDiv>::checked_div(self, other)
    }
}

c0nst::c0nst! {
    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Rem for FixedUInt<T, N> {
        type Output = Self;
        fn rem(self, other: Self) -> Self::Output {
            Self { array: const_div_rem(&self.array, &other.array).1 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Rem<&FixedUInt<T, N>> for FixedUInt<T, N> {
        type Output = Self;
        fn rem(self, other: &FixedUInt<T, N>) -> Self::Output {
            Self { array: const_div_rem(&self.array, &other.array).1 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Rem<FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn rem(self, other: FixedUInt<T, N>) -> Self::Output {
            Self::Output { array: const_div_rem(&self.array, &other.array).1 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::Rem<&FixedUInt<T, N>> for &FixedUInt<T, N> {
        type Output = FixedUInt<T, N>;
        fn rem(self, other: &FixedUInt<T, N>) -> Self::Output {
            Self::Output { array: const_div_rem(&self.array, &other.array).1 }
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::RemAssign for FixedUInt<T, N> {
        fn rem_assign(&mut self, other: Self) {
            self.array = const_div_rem(&self.array, &other.array).1;
        }
    }

    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst core::ops::RemAssign<&FixedUInt<T, N>> for FixedUInt<T, N> {
        fn rem_assign(&mut self, other: &FixedUInt<T, N>) {
            self.array = const_div_rem(&self.array, &other.array).1;
        }
    }
}

c0nst::c0nst! {
    impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize> c0nst ConstCheckedRem for FixedUInt<T, N> {
        fn checked_rem(&self, other: &Self) -> Option<Self> {
            if <Self as ConstZero>::is_zero(other) {
                None
            } else {
                Some(*self % *other)
            }
        }
    }
}

impl<T: MachineWord, const N: usize> num_traits::CheckedRem for FixedUInt<T, N> {
    fn checked_rem(&self, other: &Self) -> Option<Self> {
        <Self as ConstCheckedRem>::checked_rem(self, other)
    }
}

// Test wrappers for const mul traits
#[cfg(test)]
c0nst::c0nst! {
    pub c0nst fn const_wrapping_mul<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
        a: &FixedUInt<T, N>,
        b: &FixedUInt<T, N>
    ) -> FixedUInt<T, N> {
        <FixedUInt<T, N> as ConstWrappingMul>::wrapping_mul(a, b)
    }

    pub c0nst fn const_checked_mul<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
        a: &FixedUInt<T, N>,
        b: &FixedUInt<T, N>
    ) -> Option<FixedUInt<T, N>> {
        <FixedUInt<T, N> as ConstCheckedMul>::checked_mul(a, b)
    }

    pub c0nst fn const_saturating_mul<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
        a: &FixedUInt<T, N>,
        b: &FixedUInt<T, N>
    ) -> FixedUInt<T, N> {
        <FixedUInt<T, N> as ConstSaturatingMul>::saturating_mul(a, b)
    }

    pub c0nst fn const_overflowing_mul<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
        a: &FixedUInt<T, N>,
        b: &FixedUInt<T, N>
    ) -> (FixedUInt<T, N>, bool) {
        <FixedUInt<T, N> as ConstOverflowingMul>::overflowing_mul(a, b)
    }

    pub c0nst fn const_checked_div<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
        a: &FixedUInt<T, N>,
        b: &FixedUInt<T, N>
    ) -> Option<FixedUInt<T, N>> {
        <FixedUInt<T, N> as ConstCheckedDiv>::checked_div(a, b)
    }

    pub c0nst fn const_checked_rem<T: [c0nst] ConstMachineWord + MachineWord, const N: usize>(
        a: &FixedUInt<T, N>,
        b: &FixedUInt<T, N>
    ) -> Option<FixedUInt<T, N>> {
        <FixedUInt<T, N> as ConstCheckedRem>::checked_rem(a, b)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_mul() {
        let a = FixedUInt::<u8, 2>::from(123u8);
        let b = FixedUInt::<u8, 2>::from(234u8);
        let c = a * b;
        assert_eq!(c, FixedUInt::<u8, 2>::from(28782u16));
    }

    #[test]
    fn test_mul_combinations() {
        let a = FixedUInt::<u8, 2>::from(12u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let expected = FixedUInt::<u8, 2>::from(36u8);

        // value * value
        assert_eq!(a * b, expected);
        // value * ref
        assert_eq!(a * &b, expected);
        // ref * value
        assert_eq!(&a * b, expected);
        // ref * ref
        assert_eq!(&a * &b, expected);
    }

    #[test]
    fn test_div_combinations() {
        let a = FixedUInt::<u8, 2>::from(36u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let expected = FixedUInt::<u8, 2>::from(12u8);

        // value / value
        assert_eq!(a / b, expected);
        // value / ref
        assert_eq!(a / &b, expected);
        // ref / value
        assert_eq!(&a / b, expected);
        // ref / ref
        assert_eq!(&a / &b, expected);
    }

    #[test]
    fn test_rem_combinations() {
        let a = FixedUInt::<u8, 2>::from(37u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let expected = FixedUInt::<u8, 2>::from(1u8); // 37 % 3 = 1

        // value % value
        assert_eq!(a % b, expected);
        // value % ref
        assert_eq!(a % &b, expected);
        // ref % value
        assert_eq!(&a % b, expected);
        // ref % ref
        assert_eq!(&a % &b, expected);
    }

    #[test]
    fn test_const_mul_traits() {
        let a = FixedUInt::<u8, 2>::from(12u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let expected = FixedUInt::<u8, 2>::from(36u8);

        // ConstWrappingMul
        assert_eq!(const_wrapping_mul(&a, &b), expected);

        // ConstCheckedMul - no overflow
        assert_eq!(const_checked_mul(&a, &b), Some(expected));

        // ConstSaturatingMul - no overflow
        assert_eq!(const_saturating_mul(&a, &b), expected);

        // ConstOverflowingMul - no overflow
        let (result, overflow) = const_overflowing_mul(&a, &b);
        assert_eq!(result, expected);
        assert!(!overflow);

        // Test overflow cases: 256 * 256 = 65536 which overflows 16 bits
        let large = FixedUInt::<u8, 2>::from(256u16); // 0x100

        // ConstCheckedMul - with overflow
        assert_eq!(const_checked_mul(&large, &large), None);

        // ConstSaturatingMul - with overflow saturates to max
        let saturated = const_saturating_mul(&large, &large);
        assert_eq!(saturated, FixedUInt::<u8, 2>::from(0xFFFFu16));

        // ConstOverflowingMul - with overflow
        let (_, overflow) = const_overflowing_mul(&large, &large);
        assert!(overflow);

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt { array: [12, 0] };
            const B: FixedUInt<u8, 2> = FixedUInt { array: [3, 0] };

            const WRAPPING_RESULT: FixedUInt<u8, 2> = const_wrapping_mul(&A, &B);
            assert_eq!(WRAPPING_RESULT.array, [36, 0]);

            const CHECKED_RESULT: Option<FixedUInt<u8, 2>> = const_checked_mul(&A, &B);
            assert!(CHECKED_RESULT.is_some());

            const SATURATING_RESULT: FixedUInt<u8, 2> = const_saturating_mul(&A, &B);
            assert_eq!(SATURATING_RESULT.array, [36, 0]);

            const OVERFLOWING_RESULT: (FixedUInt<u8, 2>, bool) = const_overflowing_mul(&A, &B);
            assert_eq!(OVERFLOWING_RESULT.0.array, [36, 0]);
            assert!(!OVERFLOWING_RESULT.1);
        }
    }

    #[test]
    fn test_const_checked_div_rem() {
        let a = FixedUInt::<u8, 2>::from(36u8);
        let b = FixedUInt::<u8, 2>::from(5u8);
        let zero = FixedUInt::<u8, 2>::from(0u8);

        // ConstCheckedDiv - valid division
        assert_eq!(
            const_checked_div(&a, &b),
            Some(FixedUInt::<u8, 2>::from(7u8))
        ); // 36 / 5 = 7

        // ConstCheckedDiv - divide by zero
        assert_eq!(const_checked_div(&a, &zero), None);

        // ConstCheckedRem - valid remainder
        assert_eq!(
            const_checked_rem(&a, &b),
            Some(FixedUInt::<u8, 2>::from(1u8))
        ); // 36 % 5 = 1

        // ConstCheckedRem - remainder by zero
        assert_eq!(const_checked_rem(&a, &zero), None);

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt { array: [36, 0] };
            const B: FixedUInt<u8, 2> = FixedUInt { array: [5, 0] };
            const ZERO: FixedUInt<u8, 2> = FixedUInt { array: [0, 0] };

            const CHECKED_DIV_OK: Option<FixedUInt<u8, 2>> = const_checked_div(&A, &B);
            const CHECKED_DIV_ZERO: Option<FixedUInt<u8, 2>> = const_checked_div(&A, &ZERO);
            const CHECKED_REM_OK: Option<FixedUInt<u8, 2>> = const_checked_rem(&A, &B);
            const CHECKED_REM_ZERO: Option<FixedUInt<u8, 2>> = const_checked_rem(&A, &ZERO);

            assert_eq!(CHECKED_DIV_OK, Some(FixedUInt { array: [7, 0] }));
            assert_eq!(CHECKED_DIV_ZERO, None);
            assert_eq!(CHECKED_REM_OK, Some(FixedUInt { array: [1, 0] }));
            assert_eq!(CHECKED_REM_ZERO, None);
        }
    }
}