Scalar operations on integer types up to 32 bits

This commit is contained in:
Sam Cappleman-Lynes 2017-06-29 18:29:14 +01:00
parent 94d570697c
commit 99873d06e5
3 changed files with 73 additions and 0 deletions

View File

@ -370,6 +370,7 @@ impl Add<BigInt> for BigInt {
} }
} }
promote_all_scalars!(impl Add for BigInt, add);
forward_all_scalar_binop_to_val_val_commutative!(impl Add<BigDigit> for BigInt, add); forward_all_scalar_binop_to_val_val_commutative!(impl Add<BigDigit> for BigInt, add);
impl Add<BigDigit> for BigInt { impl Add<BigDigit> for BigInt {
@ -468,6 +469,7 @@ impl Sub<BigInt> for BigInt {
} }
} }
promote_all_scalars!(impl Sub for BigInt, sub);
forward_all_scalar_binop_to_val_val!(impl Sub<BigDigit> for BigInt, sub); forward_all_scalar_binop_to_val_val!(impl Sub<BigDigit> for BigInt, sub);
impl Sub<BigDigit> for BigInt { impl Sub<BigDigit> for BigInt {
@ -536,6 +538,7 @@ impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt {
} }
} }
promote_all_scalars!(impl Mul for BigInt, mul);
forward_all_scalar_binop_to_val_val_commutative!(impl Mul<BigDigit> for BigInt, mul); forward_all_scalar_binop_to_val_val_commutative!(impl Mul<BigDigit> for BigInt, mul);
impl Mul<BigDigit> for BigInt { impl Mul<BigDigit> for BigInt {
@ -574,6 +577,7 @@ impl<'a, 'b> Div<&'b BigInt> for &'a BigInt {
} }
} }
promote_all_scalars!(impl Div for BigInt, div);
forward_all_scalar_binop_to_val_val!(impl Div<BigDigit> for BigInt, div); forward_all_scalar_binop_to_val_val!(impl Div<BigDigit> for BigInt, div);
impl Div<BigDigit> for BigInt { impl Div<BigDigit> for BigInt {
@ -634,6 +638,7 @@ impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt {
} }
} }
promote_all_scalars!(impl Rem for BigInt, rem);
forward_all_scalar_binop_to_val_val!(impl Rem<BigDigit> for BigInt, rem); forward_all_scalar_binop_to_val_val!(impl Rem<BigDigit> for BigInt, rem);
impl Rem<BigDigit> for BigInt { impl Rem<BigDigit> for BigInt {

View File

@ -394,6 +394,7 @@ impl<'a> Add<&'a BigUint> for BigUint {
} }
} }
promote_unsigned_scalars!(impl Add for BigUint, add);
forward_all_scalar_binop_to_val_val_commutative!(impl Add<BigDigit> for BigUint, add); forward_all_scalar_binop_to_val_val_commutative!(impl Add<BigDigit> for BigUint, add);
impl Add<BigDigit> for BigUint { impl Add<BigDigit> for BigUint {
@ -439,6 +440,7 @@ impl<'a> Sub<BigUint> for &'a BigUint {
} }
} }
promote_unsigned_scalars!(impl Sub for BigUint, sub);
forward_all_scalar_binop_to_val_val!(impl Sub<BigDigit> for BigUint, sub); forward_all_scalar_binop_to_val_val!(impl Sub<BigDigit> for BigUint, sub);
impl Sub<BigDigit> for BigUint { impl Sub<BigDigit> for BigUint {
@ -476,6 +478,7 @@ impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint {
} }
} }
promote_unsigned_scalars!(impl Mul for BigUint, mul);
forward_all_scalar_binop_to_val_val_commutative!(impl Mul<BigDigit> for BigUint, mul); forward_all_scalar_binop_to_val_val_commutative!(impl Mul<BigDigit> for BigUint, mul);
impl Mul<BigDigit> for BigUint { impl Mul<BigDigit> for BigUint {
@ -507,6 +510,7 @@ impl<'a, 'b> Div<&'b BigUint> for &'a BigUint {
} }
} }
promote_unsigned_scalars!(impl Div for BigUint, div);
forward_all_scalar_binop_to_val_val!(impl Div<BigDigit> for BigUint, div); forward_all_scalar_binop_to_val_val!(impl Div<BigDigit> for BigUint, div);
impl Div<BigDigit> for BigUint { impl Div<BigDigit> for BigUint {
@ -544,6 +548,7 @@ impl<'a, 'b> Rem<&'b BigUint> for &'a BigUint {
} }
} }
promote_unsigned_scalars!(impl Rem for BigUint, rem);
forward_all_scalar_binop_to_val_val!(impl Rem<BigDigit> for BigUint, rem); forward_all_scalar_binop_to_val_val!(impl Rem<BigDigit> for BigUint, rem);
impl Rem<BigDigit> for BigUint { impl Rem<BigDigit> for BigUint {

View File

@ -184,6 +184,50 @@ macro_rules! forward_scalar_ref_ref_binop {
} }
} }
macro_rules! promote_scalars {
(impl $imp:ident<$promo:ty> for $res:ty, $method:ident, $( $scalar:ty ),*) => {
$(
forward_all_scalar_binop_to_val_val!(impl $imp<$scalar> for $res, $method);
impl $imp<$scalar> for $res {
type Output = $res;
#[inline]
fn $method(self, other: $scalar) -> $res {
$imp::$method(self, other as $promo)
}
}
impl $imp<$res> for $scalar {
type Output = $res;
#[inline]
fn $method(self, other: $res) -> $res {
$imp::$method(self as $promo, other)
}
}
)*
}
}
macro_rules! promote_unsigned_scalars_to_u32 {
(impl $imp:ident for $res:ty, $method:ident) => {
#[cfg(target_pointer_width = "32")]
promote_scalars!(impl $imp<u32> for $res, $method, u8, u16, usize);
#[cfg(target_pointer_width = "64")]
promote_scalars!(impl $imp<u32> for $res, $method, u8, u16);
}
}
macro_rules! promote_signed_scalars_to_i32 {
(impl $imp:ident for $res:ty, $method:ident) => {
#[cfg(target_pointer_width = "32")]
promote_scalars!(impl $imp<i32> for $res, $method, i8, i16, isize);
#[cfg(target_pointer_width = "64")]
promote_scalars!(impl $imp<i32> for $res, $method, i8, i16);
}
}
// Forward everything to ref-ref, when reusing storage is not helpful // Forward everything to ref-ref, when reusing storage is not helpful
macro_rules! forward_all_binop_to_ref_ref { macro_rules! forward_all_binop_to_ref_ref {
(impl $imp:ident for $res:ty, $method:ident) => { (impl $imp:ident for $res:ty, $method:ident) => {
@ -224,4 +268,23 @@ macro_rules! forward_all_scalar_binop_to_val_val_commutative {
forward_scalar_val_val_binop_commutative!(impl $imp<$scalar> for $res, $method); forward_scalar_val_val_binop_commutative!(impl $imp<$scalar> for $res, $method);
forward_all_scalar_binop_to_val_val!(impl $imp<$scalar> for $res, $method); forward_all_scalar_binop_to_val_val!(impl $imp<$scalar> for $res, $method);
} }
}
macro_rules! promote_unsigned_scalars {
(impl $imp:ident for $res:ty, $method:ident) => {
promote_unsigned_scalars_to_u32!(impl $imp for $res, $method);
}
}
macro_rules! promote_signed_scalars {
(impl $imp:ident for $res:ty, $method:ident) => {
promote_signed_scalars_to_i32!(impl $imp for $res, $method);
}
}
macro_rules! promote_all_scalars {
(impl $imp:ident for $res:ty, $method:ident) => {
promote_unsigned_scalars!(impl $imp for $res, $method);
promote_signed_scalars!(impl $imp for $res, $method);
}
} }