File tree 2 files changed +12
-40
lines changed 2 files changed +12
-40
lines changed Original file line number Diff line number Diff line change @@ -915,26 +915,10 @@ macro_rules! int_impl {
915
915
#[ inline]
916
916
pub const fn checked_isqrt( self ) -> Option <Self > {
917
917
if self < 0 {
918
- return None ;
919
- } else if self < 2 {
920
- return Some ( self ) ;
921
- }
922
-
923
- let mut x: Self = self ;
924
- let mut c: Self = 0 ;
925
- let mut d: Self = 1 << ( self . ilog2( ) & !1 ) ;
926
-
927
- while ( d != 0 ) {
928
- if x >= c + d {
929
- x -= c + d;
930
- c = ( c >> 1 ) + d;
931
- } else {
932
- c >>= 1 ;
933
- }
934
- d >>= 2 ;
918
+ None
919
+ } else {
920
+ Some ( ( self as $UnsignedT) . isqrt( ) as Self )
935
921
}
936
-
937
- return Some ( c) ;
938
922
}
939
923
940
924
/// Saturating integer addition. Computes `self + rhs`, saturating at the numeric
@@ -2118,27 +2102,15 @@ macro_rules! int_impl {
2118
2102
without modifying the original"]
2119
2103
#[ inline]
2120
2104
pub const fn isqrt( self ) -> Self {
2121
- if self < 0 {
2122
- panic!( "argument of integer square root must be non-negative" )
2123
- } else if self < 2 {
2124
- return self ;
2125
- }
2126
-
2127
- let mut x: Self = self ;
2128
- let mut c: Self = 0 ;
2129
- let mut d: Self = 1 << ( self . ilog2( ) & !1 ) ;
2130
-
2131
- while ( d != 0 ) {
2132
- if x >= c + d {
2133
- x -= c + d;
2134
- c = ( c >> 1 ) + d;
2135
- } else {
2136
- c >>= 1 ;
2137
- }
2138
- d >>= 2 ;
2105
+ // I would like to implement it as
2106
+ // ```
2107
+ // self.checked_isqrt().expect("argument of integer square root must be non-negative")
2108
+ // ```
2109
+ // but `expect` is not yet stable as a `const fn`.
2110
+ match self . checked_isqrt( ) {
2111
+ Some ( sqrt) => sqrt,
2112
+ None => panic!( "argument of integer square root must be non-negative" ) ,
2139
2113
}
2140
-
2141
- return c;
2142
2114
}
2143
2115
2144
2116
/// Calculates the quotient of Euclidean division of `self` by `rhs`.
Original file line number Diff line number Diff line change @@ -2011,7 +2011,7 @@ macro_rules! uint_impl {
2011
2011
d >>= 2 ;
2012
2012
}
2013
2013
2014
- return c ;
2014
+ c
2015
2015
}
2016
2016
2017
2017
/// Performs Euclidean division.
You can’t perform that action at this time.
0 commit comments