@@ -176,6 +176,17 @@ impl FromStr for BigUint {
176
176
}
177
177
}
178
178
179
+ /// Convert a u32 chunk (len is either 1 or 2) to a single u64 digit
180
+ #[ inline]
181
+ fn u32_chunk_to_u64 ( chunk : & [ u32 ] ) -> u64 {
182
+ // raw could have odd length
183
+ let mut digit = chunk[ 0 ] as u64 ;
184
+ if let Some ( & hi) = chunk. get ( 1 ) {
185
+ digit |= ( hi as u64 ) << 32 ;
186
+ }
187
+ digit
188
+ }
189
+
179
190
// Convert from a power of two radix (bits == ilog2(radix)) where bits evenly divides
180
191
// BigDigit::BITS
181
192
fn from_bitwise_digits_le ( v : & [ u8 ] , bits : u8 ) -> BigUint {
@@ -2399,35 +2410,24 @@ pub struct IterU64Digits<'a> {
2399
2410
#[ cfg( not( u64_digit) ) ]
2400
2411
impl < ' a > IterU64Digits < ' a > {
2401
2412
fn new ( data : & ' a [ u32 ] ) -> Self {
2402
- IterU32Digits { it : data. chunks ( 2 ) }
2413
+ IterU64Digits { it : data. chunks ( 2 ) }
2403
2414
}
2404
2415
}
2416
+
2405
2417
#[ cfg( not( u64_digit) ) ]
2406
2418
impl < ' a > Iterator for IterU64Digits < ' a > {
2407
2419
type Item = u64 ;
2408
2420
fn next ( & mut self ) -> Option < u64 > {
2409
- self . it . next ( |chunk| {
2410
- let mut digit = chunk[ 0 ] as u64 ;
2411
- if let Some ( & hi) = chunk. get ( 1 ) {
2412
- digit |= ( hi as u64 ) << 32 ;
2413
- }
2414
- digit
2415
- } )
2421
+ self . it . next ( ) . map ( u32_chunk_to_u64)
2416
2422
}
2417
2423
2418
2424
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2419
2425
let len = self . len ( ) ;
2420
2426
( len, Some ( len) )
2421
2427
}
2422
2428
2423
- fn last ( self ) -> Option < u32 > {
2424
- self . data . last ( ) . map ( |& last| {
2425
- if self . last_hi_is_zero {
2426
- last as u32
2427
- } else {
2428
- ( last >> 32 ) as u32
2429
- }
2430
- } )
2429
+ fn last ( self ) -> Option < u64 > {
2430
+ self . it . last ( ) . map ( u32_chunk_to_u64)
2431
2431
}
2432
2432
2433
2433
fn count ( self ) -> usize {
@@ -2437,7 +2437,7 @@ impl<'a> Iterator for IterU64Digits<'a> {
2437
2437
#[ cfg( not( u64_digit) ) ]
2438
2438
impl < ' a > ExactSizeIterator for IterU64Digits < ' a > {
2439
2439
fn len ( & self ) -> usize {
2440
- self . data . len ( ) * 2 - usize :: from ( self . last_hi_is_zero )
2440
+ self . it . len ( )
2441
2441
}
2442
2442
}
2443
2443
@@ -2531,14 +2531,7 @@ impl BigUint {
2531
2531
self . data . extend_from_slice ( slice) ;
2532
2532
2533
2533
#[ cfg( u64_digit) ]
2534
- self . data . extend ( slice. chunks ( 2 ) . map ( |chunk| {
2535
- // raw could have odd length
2536
- let mut digit = BigDigit :: from ( chunk[ 0 ] ) ;
2537
- if let Some ( & hi) = chunk. get ( 1 ) {
2538
- digit |= BigDigit :: from ( hi) << 32 ;
2539
- }
2540
- digit
2541
- } ) ) ;
2534
+ self . data . extend ( slice. chunks ( 2 ) . map ( u32_chunk_to_u64) ) ;
2542
2535
2543
2536
self . normalize ( ) ;
2544
2537
}
0 commit comments