@@ -20,13 +20,16 @@ pub trait IntegerMathOps<T: Integer<T>> {
20
20
fn gcd ( self , b : T ) -> T ;
21
21
fn lcm ( self , b : T ) -> T ;
22
22
fn mod_pow ( self , e : T , m : T ) -> T ;
23
- fn mod_inv ( self , m : T ) -> T ;
24
23
}
25
24
26
25
pub trait UnsignedMathOps < T : Unsigned < T > > {
27
26
fn sqrt ( self ) -> T ;
28
27
}
29
28
29
+ pub trait SignedMathOps < T : Signed < T > > {
30
+ fn mod_inv ( self , m : T ) -> T ;
31
+ }
32
+
30
33
impl < T : Integer < T > > IntegerMathOps < T > for T {
31
34
/// Greatest common divisor
32
35
fn gcd ( self , mut b : T ) -> T {
@@ -59,7 +62,27 @@ impl<T: Integer<T>> IntegerMathOps<T> for T {
59
62
60
63
c
61
64
}
65
+ }
66
+
67
+ impl < T : Unsigned < T > > UnsignedMathOps < T > for T {
68
+ // Integer square root. Once [`isqrt`] is stablized then this function can be removed.
69
+ fn sqrt ( self ) -> T {
70
+ let mut bit = T :: ONE << ( self . ilog2 ( ) >> T :: ONE ) ;
71
+ let mut root = bit;
62
72
73
+ while bit > T :: ONE {
74
+ bit = bit >> T :: ONE ;
75
+ let next = root | bit;
76
+ if next * next <= self {
77
+ root = next;
78
+ }
79
+ }
80
+
81
+ root
82
+ }
83
+ }
84
+
85
+ impl < T : Signed < T > > SignedMathOps < T > for T {
63
86
// Modular multiplicative inverse
64
87
fn mod_inv ( self , m : T ) -> T {
65
88
let mut t = T :: ZERO ;
@@ -79,21 +102,3 @@ impl<T: Integer<T>> IntegerMathOps<T> for T {
79
102
t
80
103
}
81
104
}
82
-
83
- impl < T : Unsigned < T > > UnsignedMathOps < T > for T {
84
- // Once [`isqrt`] is stablized then this function can be removed.
85
- fn sqrt ( self ) -> T {
86
- let mut bit = T :: ONE << ( self . ilog2 ( ) >> T :: ONE ) ;
87
- let mut root = bit;
88
-
89
- while bit > T :: ONE {
90
- bit = bit >> T :: ONE ;
91
- let next = root | bit;
92
- if next * next <= self {
93
- root = next;
94
- }
95
- }
96
-
97
- root
98
- }
99
- }
0 commit comments