@@ -21,7 +21,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
21
21
/// Computes `self + rhs`, returning [`None`] if overflow occurred.
22
22
#[ inline( always) ]
23
23
#[ must_use]
24
- pub fn checked_add ( self , rhs : Self ) -> Option < Self > {
24
+ pub const fn checked_add ( self , rhs : Self ) -> Option < Self > {
25
25
match self . overflowing_add ( rhs) {
26
26
( value, false ) => Some ( value) ,
27
27
_ => None ,
@@ -31,7 +31,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
31
31
/// Computes `-self`, returning [`None`] unless `self == 0`.
32
32
#[ inline( always) ]
33
33
#[ must_use]
34
- pub fn checked_neg ( self ) -> Option < Self > {
34
+ pub const fn checked_neg ( self ) -> Option < Self > {
35
35
match self . overflowing_neg ( ) {
36
36
( value, false ) => Some ( value) ,
37
37
_ => None ,
@@ -41,7 +41,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
41
41
/// Computes `self - rhs`, returning [`None`] if overflow occurred.
42
42
#[ inline( always) ]
43
43
#[ must_use]
44
- pub fn checked_sub ( self , rhs : Self ) -> Option < Self > {
44
+ pub const fn checked_sub ( self , rhs : Self ) -> Option < Self > {
45
45
match self . overflowing_sub ( rhs) {
46
46
( value, false ) => Some ( value) ,
47
47
_ => None ,
@@ -55,16 +55,18 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
55
55
/// then the wrapped value is returned.
56
56
#[ inline]
57
57
#[ must_use]
58
- pub fn overflowing_add ( mut self , rhs : Self ) -> ( Self , bool ) {
58
+ pub const fn overflowing_add ( mut self , rhs : Self ) -> ( Self , bool ) {
59
59
if BITS == 0 {
60
60
return ( Self :: ZERO , false ) ;
61
61
}
62
62
let mut carry = 0_u128 ;
63
+ let mut i = 0 ;
63
64
#[ allow( clippy:: cast_possible_truncation) ] // Intentional
64
- for ( lhs , & rhs ) in self . limbs . iter_mut ( ) . zip ( rhs . as_limbs ( ) ) {
65
- carry += u128:: from ( * lhs ) + u128 :: from ( rhs) ;
66
- * lhs = carry as u64 ;
65
+ while i < LIMBS {
66
+ carry += self . limbs [ i ] as u128 + rhs. limbs [ i ] as u128 ;
67
+ self . limbs [ i ] = carry as u64 ;
67
68
carry >>= 64 ;
69
+ i += 1 ;
68
70
}
69
71
let overflow = carry != 0 || self . limbs [ LIMBS - 1 ] > Self :: MASK ;
70
72
self . limbs [ LIMBS - 1 ] &= Self :: MASK ;
@@ -79,7 +81,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
79
81
/// overflow.
80
82
#[ inline( always) ]
81
83
#[ must_use]
82
- pub fn overflowing_neg ( self ) -> ( Self , bool ) {
84
+ pub const fn overflowing_neg ( self ) -> ( Self , bool ) {
83
85
Self :: ZERO . overflowing_sub ( self )
84
86
}
85
87
@@ -90,17 +92,19 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
90
92
/// occurred then the wrapped value is returned.
91
93
#[ inline]
92
94
#[ must_use]
93
- pub fn overflowing_sub ( mut self , rhs : Self ) -> ( Self , bool ) {
95
+ pub const fn overflowing_sub ( mut self , rhs : Self ) -> ( Self , bool ) {
94
96
if BITS == 0 {
95
97
return ( Self :: ZERO , false ) ;
96
98
}
97
99
let mut carry = 0_i128 ;
100
+ let mut i = 0 ;
98
101
#[ allow( clippy:: cast_possible_truncation) ] // Intentional
99
102
#[ allow( clippy:: cast_sign_loss) ] // Intentional
100
- for ( lhs, & rhs) in self . limbs . iter_mut ( ) . zip ( rhs. as_limbs ( ) ) {
101
- carry += i128:: from ( * lhs) - i128:: from ( rhs) ;
102
- * lhs = carry as u64 ;
103
- carry >>= 64 ; // Sign extending shift
103
+ while i < LIMBS {
104
+ carry += self . limbs [ i] as i128 - rhs. limbs [ i] as i128 ;
105
+ self . limbs [ i] = carry as u64 ;
106
+ carry >>= 64 ;
107
+ i += 1 ;
104
108
}
105
109
let overflow = carry != 0 || self . limbs [ LIMBS - 1 ] > Self :: MASK ;
106
110
self . limbs [ LIMBS - 1 ] &= Self :: MASK ;
@@ -111,7 +115,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
111
115
/// overflowing.
112
116
#[ inline( always) ]
113
117
#[ must_use]
114
- pub fn saturating_add ( self , rhs : Self ) -> Self {
118
+ pub const fn saturating_add ( self , rhs : Self ) -> Self {
115
119
match self . overflowing_add ( rhs) {
116
120
( value, false ) => value,
117
121
_ => Self :: MAX ,
@@ -122,7 +126,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
122
126
/// overflowing
123
127
#[ inline( always) ]
124
128
#[ must_use]
125
- pub fn saturating_sub ( self , rhs : Self ) -> Self {
129
+ pub const fn saturating_sub ( self , rhs : Self ) -> Self {
126
130
match self . overflowing_sub ( rhs) {
127
131
( value, false ) => value,
128
132
_ => Self :: ZERO ,
@@ -132,21 +136,21 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
132
136
/// Computes `self + rhs`, wrapping around at the boundary of the type.
133
137
#[ inline( always) ]
134
138
#[ must_use]
135
- pub fn wrapping_add ( self , rhs : Self ) -> Self {
139
+ pub const fn wrapping_add ( self , rhs : Self ) -> Self {
136
140
self . overflowing_add ( rhs) . 0
137
141
}
138
142
139
143
/// Computes `-self`, wrapping around at the boundary of the type.
140
144
#[ inline( always) ]
141
145
#[ must_use]
142
- pub fn wrapping_neg ( self ) -> Self {
146
+ pub const fn wrapping_neg ( self ) -> Self {
143
147
self . overflowing_neg ( ) . 0
144
148
}
145
149
146
150
/// Computes `self - rhs`, wrapping around at the boundary of the type.
147
151
#[ inline( always) ]
148
152
#[ must_use]
149
- pub fn wrapping_sub ( self , rhs : Self ) -> Self {
153
+ pub const fn wrapping_sub ( self , rhs : Self ) -> Self {
150
154
self . overflowing_sub ( rhs) . 0
151
155
}
152
156
}
0 commit comments