ruint/support/
num_integer.rs1#![cfg(feature = "num-integer")]
4
5use crate::Uint;
6use num_integer::{ExtendedGcd, Integer};
7
8impl<const BITS: usize, const LIMBS: usize> Integer for Uint<BITS, LIMBS> {
9 #[inline]
10 #[track_caller]
11 fn div_floor(&self, other: &Self) -> Self {
12 Self::wrapping_div(*self, *other)
13 }
14
15 #[inline]
16 #[track_caller]
17 fn mod_floor(&self, other: &Self) -> Self {
18 Self::wrapping_rem(*self, *other)
19 }
20
21 #[inline]
22 fn gcd(&self, other: &Self) -> Self {
23 <Self>::gcd(*self, *other)
24 }
25
26 #[inline]
27 #[track_caller]
28 fn lcm(&self, other: &Self) -> Self {
29 <Self>::lcm(*self, *other).unwrap()
30 }
31
32 #[inline]
33 fn is_multiple_of(&self, other: &Self) -> bool {
34 if other.is_zero() {
35 return self.is_zero();
36 }
37 *self % *other == Self::ZERO
38 }
39
40 #[inline]
41 fn is_even(&self) -> bool {
42 !self.bit(0)
43 }
44
45 #[inline]
46 fn is_odd(&self) -> bool {
47 self.bit(0)
48 }
49
50 #[inline]
51 #[track_caller]
52 fn div_rem(&self, other: &Self) -> (Self, Self) {
53 <Self>::div_rem(*self, *other)
54 }
55
56 #[inline]
57 #[track_caller]
58 fn div_ceil(&self, other: &Self) -> Self {
59 <Self>::div_ceil(*self, *other)
60 }
61
62 #[inline]
63 #[track_caller]
64 fn div_mod_floor(&self, other: &Self) -> (Self, Self) {
65 <Self>::div_rem(*self, *other)
67 }
68
69 #[inline]
70 fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self> {
71 let (gcd, x, y, _sign) = <Self>::gcd_extended(*self, *other);
72 ExtendedGcd { gcd, x, y }
73 }
74
75 #[inline]
76 fn dec(&mut self) {
77 *self -= Self::ONE;
78 }
79
80 #[inline]
81 fn inc(&mut self) {
82 *self += Self::ONE;
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_is_even() {
92 let mut a = Uint::<64, 1>::from(0u32);
93 for _ in 0..10 {
94 a.inc();
95 assert_eq!(a.is_even(), a.to::<u64>() % 2 == 0);
96 assert_eq!(a.is_odd(), a.to::<u64>() % 2 != 0);
97 }
98 }
99}