File tree Expand file tree Collapse file tree 2 files changed +30
-27
lines changed Expand file tree Collapse file tree 2 files changed +30
-27
lines changed Original file line number Diff line number Diff line change @@ -148,42 +148,39 @@ macro_rules! impl_common {
148
148
Self { lo, hi }
149
149
}
150
150
}
151
- } ;
152
- }
153
151
154
- impl_common ! ( i256 ) ;
155
- impl_common ! ( u256 ) ;
152
+ impl ops :: Shr < u32 > for $ty {
153
+ type Output = Self ;
156
154
157
- impl ops :: Shr < u32 > for u256 {
158
- type Output = Self ;
155
+ fn shr ( mut self , rhs : u32 ) -> Self :: Output {
156
+ debug_assert! ( rhs < Self :: BITS , "attempt to shift right with overflow" ) ;
159
157
160
- fn shr ( mut self , rhs : u32 ) -> Self :: Output {
161
- debug_assert ! ( rhs < Self :: BITS , "attempted to shift right with overflow" ) ;
162
- if rhs >= Self :: BITS {
163
- return Self :: ZERO ;
164
- }
158
+ let half_bits = Self :: BITS / 2 ;
159
+ let low_mask = half_bits - 1 ;
160
+ let s = rhs & low_mask;
165
161
166
- if rhs == 0 {
167
- return self ;
168
- }
162
+ let lo = self . lo;
163
+ let hi = self . hi;
169
164
170
- if rhs < 128 {
171
- self . lo >>= rhs;
172
- self . lo |= self . hi << ( 128 - rhs) ;
173
- } else {
174
- self . lo = self . hi >> ( rhs - 128 ) ;
175
- }
165
+ self . hi = hi >> s;
176
166
177
- if rhs < 128 {
178
- self . hi >>= rhs;
179
- } else {
180
- self . hi = 0 ;
167
+ #[ allow( unused_comparisons) ]
168
+ if rhs & half_bits == 0 {
169
+ self . lo = ( hi << ( low_mask ^ s) << 1 ) as _;
170
+ self . lo |= lo >> s;
171
+ } else {
172
+ self . lo = self . hi as _;
173
+ self . hi = if hi < 0 { !0 } else { 0 } ;
174
+ }
175
+ self
176
+ }
181
177
}
182
-
183
- self
184
- }
178
+ } ;
185
179
}
186
180
181
+ impl_common ! ( i256) ;
182
+ impl_common ! ( u256) ;
183
+
187
184
impl HInt for u128 {
188
185
type D = u256 ;
189
186
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ pub trait MinInt:
9
9
+ ops:: BitOr < Output = Self >
10
10
+ ops:: Not < Output = Self >
11
11
+ ops:: Shl < u32 , Output = Self >
12
+ + ops:: Shr < u32 , Output = Self >
12
13
+ ops:: Add < Output = Self >
13
14
+ ops:: Sub < Output = Self >
14
15
{
@@ -103,6 +104,7 @@ pub trait Int:
103
104
fn carrying_add ( self , other : Self , carry : bool ) -> ( Self , bool ) ;
104
105
fn borrowing_sub ( self , other : Self , borrow : bool ) -> ( Self , bool ) ;
105
106
fn leading_zeros ( self ) -> u32 ;
107
+ fn trailing_zeros ( self ) -> u32 ;
106
108
fn ilog2 ( self ) -> u32 ;
107
109
}
108
110
@@ -168,6 +170,10 @@ macro_rules! int_impl_common {
168
170
<Self >:: leading_zeros( self )
169
171
}
170
172
173
+ fn trailing_zeros( self ) -> u32 {
174
+ <Self >:: trailing_zeros( self )
175
+ }
176
+
171
177
fn ilog2( self ) -> u32 {
172
178
// On our older MSRV, this resolves to the trait method. Which won't actually work,
173
179
// but this is only called behind other gates.
You can’t perform that action at this time.
0 commit comments