1919) ]
2020#![ macro_use]
2121
22- use crate :: intrinsics;
23-
2422/// Arithmetic operations required by bignums.
2523pub trait FullOps : Sized {
26- /// Returns `(carry', v')` such that `carry' * 2^W + v' = self + other + carry`,
27- /// where `W` is the number of bits in `Self`.
28- fn full_add ( self , other : Self , carry : bool ) -> ( bool /* carry */ , Self ) ;
29-
30- /// Returns `(carry', v')` such that `carry' * 2^W + v' = self * other + carry`,
31- /// where `W` is the number of bits in `Self`.
32- fn full_mul ( self , other : Self , carry : Self ) -> ( Self /* carry */ , Self ) ;
33-
3424 /// Returns `(carry', v')` such that `carry' * 2^W + v' = self * other + other2 + carry`,
3525 /// where `W` is the number of bits in `Self`.
3626 fn full_mul_add ( self , other : Self , other2 : Self , carry : Self ) -> ( Self /* carry */ , Self ) ;
@@ -45,22 +35,6 @@ macro_rules! impl_full_ops {
4535 ( $( $ty: ty: add( $addfn: path) , mul/div( $bigty: ident) ; ) * ) => (
4636 $(
4737 impl FullOps for $ty {
48- fn full_add( self , other: $ty, carry: bool ) -> ( bool , $ty) {
49- // This cannot overflow; the output is between `0` and `2 * 2^nbits - 1`.
50- // FIXME: will LLVM optimize this into ADC or similar?
51- let ( v, carry1) = intrinsics:: add_with_overflow( self , other) ;
52- let ( v, carry2) = intrinsics:: add_with_overflow( v, if carry { 1 } else { 0 } ) ;
53- ( carry1 || carry2, v)
54- }
55-
56- fn full_mul( self , other: $ty, carry: $ty) -> ( $ty, $ty) {
57- // This cannot overflow;
58- // the output is between `0` and `2^nbits * (2^nbits - 1)`.
59- // FIXME: will LLVM optimize this into ADC or similar?
60- let v = ( self as $bigty) * ( other as $bigty) + ( carry as $bigty) ;
61- ( ( v >> <$ty>:: BITS ) as $ty, v as $ty)
62- }
63-
6438 fn full_mul_add( self , other: $ty, other2: $ty, carry: $ty) -> ( $ty, $ty) {
6539 // This cannot overflow;
6640 // the output is between `0` and `2^nbits * (2^nbits - 1)`.
@@ -173,12 +147,11 @@ macro_rules! define_bignum {
173147 pub fn add<' a>( & ' a mut self , other: & $name) -> & ' a mut $name {
174148 use crate :: cmp;
175149 use crate :: iter;
176- use crate :: num:: bignum:: FullOps ;
177150
178151 let mut sz = cmp:: max( self . size, other. size) ;
179152 let mut carry = false ;
180153 for ( a, b) in iter:: zip( & mut self . base[ ..sz] , & other. base[ ..sz] ) {
181- let ( c , v ) = ( * a) . full_add ( * b, carry) ;
154+ let ( v , c ) = ( * a) . carrying_add ( * b, carry) ;
182155 * a = v;
183156 carry = c;
184157 }
@@ -191,13 +164,11 @@ macro_rules! define_bignum {
191164 }
192165
193166 pub fn add_small( & mut self , other: $ty) -> & mut $name {
194- use crate :: num:: bignum:: FullOps ;
195-
196- let ( mut carry, v) = self . base[ 0 ] . full_add( other, false ) ;
167+ let ( v, mut carry) = self . base[ 0 ] . carrying_add( other, false ) ;
197168 self . base[ 0 ] = v;
198169 let mut i = 1 ;
199170 while carry {
200- let ( c , v ) = self . base[ i] . full_add ( 0 , carry) ;
171+ let ( v , c ) = self . base[ i] . carrying_add ( 0 , carry) ;
201172 self . base[ i] = v;
202173 carry = c;
203174 i += 1 ;
@@ -212,12 +183,11 @@ macro_rules! define_bignum {
212183 pub fn sub<' a>( & ' a mut self , other: & $name) -> & ' a mut $name {
213184 use crate :: cmp;
214185 use crate :: iter;
215- use crate :: num:: bignum:: FullOps ;
216186
217187 let sz = cmp:: max( self . size, other. size) ;
218188 let mut noborrow = true ;
219189 for ( a, b) in iter:: zip( & mut self . base[ ..sz] , & other. base[ ..sz] ) {
220- let ( c , v ) = ( * a) . full_add ( !* b, noborrow) ;
190+ let ( v , c ) = ( * a) . carrying_add ( !* b, noborrow) ;
221191 * a = v;
222192 noborrow = c;
223193 }
@@ -229,12 +199,10 @@ macro_rules! define_bignum {
229199 /// Multiplies itself by a digit-sized `other` and returns its own
230200 /// mutable reference.
231201 pub fn mul_small( & mut self , other: $ty) -> & mut $name {
232- use crate :: num:: bignum:: FullOps ;
233-
234202 let mut sz = self . size;
235203 let mut carry = 0 ;
236204 for a in & mut self . base[ ..sz] {
237- let ( c , v ) = ( * a) . full_mul ( other, carry) ;
205+ let ( v , c ) = ( * a) . carrying_mul ( other, carry) ;
238206 * a = v;
239207 carry = c;
240208 }
0 commit comments