2020#![ macro_use]
2121
2222use crate :: intrinsics;
23- use crate :: mem;
2423
2524/// Arithmetic operations required by bignums.
2625pub trait FullOps : Sized {
@@ -58,25 +57,22 @@ macro_rules! impl_full_ops {
5857 // This cannot overflow;
5958 // the output is between `0` and `2^nbits * (2^nbits - 1)`.
6059 // FIXME: will LLVM optimize this into ADC or similar?
61- let nbits = mem:: size_of:: <$ty>( ) * 8 ;
6260 let v = ( self as $bigty) * ( other as $bigty) + ( carry as $bigty) ;
63- ( ( v >> nbits ) as $ty, v as $ty)
61+ ( ( v >> <$ty> :: BITS ) as $ty, v as $ty)
6462 }
6563
6664 fn full_mul_add( self , other: $ty, other2: $ty, carry: $ty) -> ( $ty, $ty) {
6765 // This cannot overflow;
6866 // the output is between `0` and `2^nbits * (2^nbits - 1)`.
69- let nbits = mem:: size_of:: <$ty>( ) * 8 ;
7067 let v = ( self as $bigty) * ( other as $bigty) + ( other2 as $bigty) +
7168 ( carry as $bigty) ;
72- ( ( v >> nbits ) as $ty, v as $ty)
69+ ( ( v >> <$ty> :: BITS ) as $ty, v as $ty)
7370 }
7471
7572 fn full_div_rem( self , other: $ty, borrow: $ty) -> ( $ty, $ty) {
7673 debug_assert!( borrow < other) ;
7774 // This cannot overflow; the output is between `0` and `other * (2^nbits - 1)`.
78- let nbits = mem:: size_of:: <$ty>( ) * 8 ;
79- let lhs = ( ( borrow as $bigty) << nbits) | ( self as $bigty) ;
75+ let lhs = ( ( borrow as $bigty) << <$ty>:: BITS ) | ( self as $bigty) ;
8076 let rhs = other as $bigty;
8177 ( ( lhs / rhs) as $ty, ( lhs % rhs) as $ty)
8278 }
@@ -128,13 +124,11 @@ macro_rules! define_bignum {
128124
129125 /// Makes a bignum from `u64` value.
130126 pub fn from_u64( mut v: u64 ) -> $name {
131- use crate :: mem;
132-
133127 let mut base = [ 0 ; $n] ;
134128 let mut sz = 0 ;
135129 while v > 0 {
136130 base[ sz] = v as $ty;
137- v >>= mem :: size_of :: <$ty>( ) * 8 ;
131+ v >>= <$ty>:: BITS ;
138132 sz += 1 ;
139133 }
140134 $name { size: sz, base: base }
@@ -150,9 +144,7 @@ macro_rules! define_bignum {
150144 /// Returns the `i`-th bit where bit 0 is the least significant one.
151145 /// In other words, the bit with weight `2^i`.
152146 pub fn get_bit( & self , i: usize ) -> u8 {
153- use crate :: mem;
154-
155- let digitbits = mem:: size_of:: <$ty>( ) * 8 ;
147+ let digitbits = <$ty>:: BITS as usize ;
156148 let d = i / digitbits;
157149 let b = i % digitbits;
158150 ( ( self . base[ d] >> b) & 1 ) as u8
@@ -166,8 +158,6 @@ macro_rules! define_bignum {
166158 /// Returns the number of bits necessary to represent this value. Note that zero
167159 /// is considered to need 0 bits.
168160 pub fn bit_length( & self ) -> usize {
169- use crate :: mem;
170-
171161 // Skip over the most significant digits which are zero.
172162 let digits = self . digits( ) ;
173163 let zeros = digits. iter( ) . rev( ) . take_while( |&&x| x == 0 ) . count( ) ;
@@ -180,7 +170,7 @@ macro_rules! define_bignum {
180170 }
181171 // This could be optimized with leading_zeros() and bit shifts, but that's
182172 // probably not worth the hassle.
183- let digitbits = mem :: size_of :: <$ty>( ) * 8 ;
173+ let digitbits = <$ty>:: BITS as usize ;
184174 let mut i = nonzero. len( ) * digitbits - 1 ;
185175 while self . get_bit( i) == 0 {
186176 i -= 1 ;
@@ -265,9 +255,7 @@ macro_rules! define_bignum {
265255
266256 /// Multiplies itself by `2^bits` and returns its own mutable reference.
267257 pub fn mul_pow2( & mut self , bits: usize ) -> & mut $name {
268- use crate :: mem;
269-
270- let digitbits = mem:: size_of:: <$ty>( ) * 8 ;
258+ let digitbits = <$ty>:: BITS as usize ;
271259 let digits = bits / digitbits;
272260 let bits = bits % digitbits;
273261
@@ -393,13 +381,11 @@ macro_rules! define_bignum {
393381 /// Divide self by another bignum, overwriting `q` with the quotient and `r` with the
394382 /// remainder.
395383 pub fn div_rem( & self , d: & $name, q: & mut $name, r: & mut $name) {
396- use crate :: mem;
397-
398384 // Stupid slow base-2 long division taken from
399385 // https://en.wikipedia.org/wiki/Division_algorithm
400386 // FIXME use a greater base ($ty) for the long division.
401387 assert!( !d. is_zero( ) ) ;
402- let digitbits = mem :: size_of :: <$ty>( ) * 8 ;
388+ let digitbits = <$ty>:: BITS as usize ;
403389 for digit in & mut q. base[ ..] {
404390 * digit = 0 ;
405391 }
@@ -462,10 +448,8 @@ macro_rules! define_bignum {
462448
463449 impl crate :: fmt:: Debug for $name {
464450 fn fmt( & self , f: & mut crate :: fmt:: Formatter <' _>) -> crate :: fmt:: Result {
465- use crate :: mem;
466-
467451 let sz = if self . size < 1 { 1 } else { self . size } ;
468- let digitlen = mem :: size_of :: <$ty>( ) * 2 ;
452+ let digitlen = <$ty>:: BITS as usize / 4 ;
469453
470454 write!( f, "{:#x}" , self . base[ sz - 1 ] ) ?;
471455 for & v in self . base[ ..sz - 1 ] . iter( ) . rev( ) {
0 commit comments