diff options
| author | gifnksm <makoto.nksm+github@gmail.com> | 2013-04-28 10:58:19 +0900 |
|---|---|---|
| committer | gifnksm <makoto.nksm+github@gmail.com> | 2013-04-28 10:59:58 +0900 |
| commit | 92f0dc6b4bbab3bcfc96c0588776032d5f8e4966 (patch) | |
| tree | a7ee429935dfa3916d7549676f70b522ee1b80d1 /src/libstd/num | |
| parent | 01b3490a551bc46c3d2b99d1a811ee93201f32f3 (diff) | |
| download | rust-92f0dc6b4bbab3bcfc96c0588776032d5f8e4966.tar.gz rust-92f0dc6b4bbab3bcfc96c0588776032d5f8e4966.zip | |
libstd: inlining almost every methods in bigint module.
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/bigint.rs | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index 551e04e2122..3ea94eababb 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -53,15 +53,19 @@ pub mod BigDigit { priv static hi_mask: uint = (-1 as uint) << bits; priv static lo_mask: uint = (-1 as uint) >> bits; + #[inline(always)] priv fn get_hi(n: uint) -> BigDigit { (n >> bits) as BigDigit } + #[inline(always)] priv fn get_lo(n: uint) -> BigDigit { (n & lo_mask) as BigDigit } /// Split one machine sized unsigned integer into two BigDigits. + #[inline(always)] pub fn from_uint(n: uint) -> (BigDigit, BigDigit) { (get_hi(n), get_lo(n)) } /// Join two BigDigits into one machine sized unsigned integer + #[inline(always)] pub fn to_uint(hi: BigDigit, lo: BigDigit) -> uint { (lo as uint) | ((hi as uint) << bits) } @@ -78,32 +82,40 @@ pub struct BigUint { } impl Eq for BigUint { + #[inline(always)] fn eq(&self, other: &BigUint) -> bool { self.equals(other) } + #[inline(always)] fn ne(&self, other: &BigUint) -> bool { !self.equals(other) } } impl TotalEq for BigUint { + #[inline(always)] fn equals(&self, other: &BigUint) -> bool { match self.cmp(other) { Equal => true, _ => false } } } impl Ord for BigUint { + #[inline(always)] fn lt(&self, other: &BigUint) -> bool { match self.cmp(other) { Less => true, _ => false} } + #[inline(always)] fn le(&self, other: &BigUint) -> bool { match self.cmp(other) { Less | Equal => true, _ => false } } + #[inline(always)] fn ge(&self, other: &BigUint) -> bool { match self.cmp(other) { Greater | Equal => true, _ => false } } + #[inline(always)] fn gt(&self, other: &BigUint) -> bool { match self.cmp(other) { Greater => true, _ => false } } } impl TotalOrd for BigUint { + #[inline(always)] fn cmp(&self, other: &BigUint) -> Ordering { let s_len = self.data.len(), o_len = other.data.len(); if s_len < o_len { return Less; } @@ -121,16 +133,19 @@ impl TotalOrd for BigUint { } impl ToStr for BigUint { + #[inline(always)] fn to_str(&self) -> ~str { self.to_str_radix(10) } } impl from_str::FromStr for BigUint { + #[inline(always)] fn from_str(s: &str) -> Option<BigUint> { FromStrRadix::from_str_radix(s, 10) } } impl Shl<uint, BigUint> for BigUint { + #[inline(always)] fn shl(&self, rhs: &uint) -> BigUint { let n_unit = *rhs / BigDigit::bits; let n_bits = *rhs % BigDigit::bits; @@ -139,6 +154,7 @@ impl Shl<uint, BigUint> for BigUint { } impl Shr<uint, BigUint> for BigUint { + #[inline(always)] fn shr(&self, rhs: &uint) -> BigUint { let n_unit = *rhs / BigDigit::bits; let n_bits = *rhs % BigDigit::bits; @@ -147,18 +163,22 @@ impl Shr<uint, BigUint> for BigUint { } impl Zero for BigUint { + #[inline(always)] fn zero() -> BigUint { BigUint::new(~[]) } + #[inline(always)] fn is_zero(&self) -> bool { self.data.is_empty() } } impl One for BigUint { + #[inline(always)] fn one() -> BigUint { BigUint::new(~[1]) } } impl Unsigned for BigUint {} impl Add<BigUint, BigUint> for BigUint { + #[inline(always)] fn add(&self, other: &BigUint) -> BigUint { let new_len = uint::max(self.data.len(), other.data.len()); @@ -178,6 +198,7 @@ impl Add<BigUint, BigUint> for BigUint { } impl Sub<BigUint, BigUint> for BigUint { + #[inline(always)] fn sub(&self, other: &BigUint) -> BigUint { let new_len = uint::max(self.data.len(), other.data.len()); @@ -233,6 +254,7 @@ impl Mul<BigUint, BigUint> for BigUint { return ll + mm.shl_unit(half_len) + hh.shl_unit(half_len * 2); + #[inline(always)] fn mul_digit(a: &BigUint, n: BigDigit) -> BigUint { if n == 0 { return Zero::zero(); } if n == 1 { return copy *a; } @@ -249,6 +271,7 @@ impl Mul<BigUint, BigUint> for BigUint { return BigUint::new(prod + [carry]); } + #[inline(always)] fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) { let mid = uint::min(a.data.len(), n); return (BigUint::from_slice(vec::slice(a.data, mid, @@ -256,6 +279,7 @@ impl Mul<BigUint, BigUint> for BigUint { BigUint::from_slice(vec::slice(a.data, 0, mid))); } + #[inline(always)] fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) { match a.cmp(&b) { Less => (Less, b - a), @@ -267,6 +291,7 @@ impl Mul<BigUint, BigUint> for BigUint { } impl Quot<BigUint, BigUint> for BigUint { + #[inline(always)] fn quot(&self, other: &BigUint) -> BigUint { let (q, _) = self.quot_rem(other); return q; @@ -274,6 +299,7 @@ impl Quot<BigUint, BigUint> for BigUint { } impl Rem<BigUint, BigUint> for BigUint { + #[inline(always)] fn rem(&self, other: &BigUint) -> BigUint { let (_, r) = self.quot_rem(other); return r; @@ -281,6 +307,7 @@ impl Rem<BigUint, BigUint> for BigUint { } impl Neg<BigUint> for BigUint { + #[inline(always)] fn neg(&self) -> BigUint { fail!() } } @@ -420,16 +447,19 @@ impl Integer for BigUint { } impl IntConvertible for BigUint { + #[inline(always)] fn to_int(&self) -> int { uint::min(self.to_uint(), int::max_value as uint) as int } + #[inline(always)] fn from_int(n: int) -> BigUint { if (n < 0) { Zero::zero() } else { BigUint::from_uint(n as uint) } } } impl ToStrRadix for BigUint { + #[inline(always)] fn to_str_radix(&self, radix: uint) -> ~str { assert!(1 < radix && radix <= 16); let (base, max_len) = get_radix_base(radix); @@ -438,6 +468,7 @@ impl ToStrRadix for BigUint { } return fill_concat(convert_base(copy *self, base), radix, max_len); + #[inline(always)] fn convert_base(n: BigUint, base: uint) -> ~[BigDigit] { let divider = BigUint::from_uint(base); let mut result = ~[]; @@ -453,6 +484,7 @@ impl ToStrRadix for BigUint { return result; } + #[inline(always)] fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str { if v.is_empty() { return ~"0" } let s = str::concat(vec::reversed(v).map(|n| { @@ -466,14 +498,16 @@ impl ToStrRadix for BigUint { impl FromStrRadix for BigUint { /// Creates and initializes an BigUint. + #[inline(always)] pub fn from_str_radix(s: &str, radix: uint) -> Option<BigUint> { BigUint::parse_bytes(str::to_bytes(s), radix) } } -pub impl BigUint { +impl BigUint { /// Creates and initializes an BigUint. + #[inline(always)] pub fn new(v: ~[BigDigit]) -> BigUint { // omit trailing zeros let new_len = v.rposition(|n| *n != 0).map_default(0, |p| *p + 1); @@ -485,6 +519,7 @@ pub impl BigUint { } /// Creates and initializes an BigUint. + #[inline(always)] pub fn from_uint(n: uint) -> BigUint { match BigDigit::from_uint(n) { (0, 0) => Zero::zero(), @@ -494,11 +529,13 @@ pub impl BigUint { } /// Creates and initializes an BigUint. + #[inline(always)] pub fn from_slice(slice: &[BigDigit]) -> BigUint { return BigUint::new(vec::from_slice(slice)); } /// Creates and initializes an BigUint. + #[inline(always)] pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<BigUint> { let (base, unit_len) = get_radix_base(radix); @@ -521,6 +558,7 @@ pub impl BigUint { } } + #[inline(always)] pub fn to_uint(&self) -> uint { match self.data.len() { 0 => 0, @@ -530,12 +568,14 @@ pub impl BigUint { } } + #[inline(always)] priv fn shl_unit(self, n_unit: uint) -> BigUint { if n_unit == 0 || self.is_zero() { return self; } return BigUint::new(vec::from_elem(n_unit, 0) + self.data); } + #[inline(always)] priv fn shl_bits(self, n_bits: uint) -> BigUint { if n_bits == 0 || self.is_zero() { return self; } @@ -551,6 +591,7 @@ pub impl BigUint { return BigUint::new(shifted + [carry]); } + #[inline(always)] priv fn shr_unit(self, n_unit: uint) -> BigUint { if n_unit == 0 { return self; } if self.data.len() < n_unit { return Zero::zero(); } @@ -559,6 +600,7 @@ pub impl BigUint { ); } + #[inline(always)] priv fn shr_bits(self, n_bits: uint) -> BigUint { if n_bits == 0 || self.data.is_empty() { return self; } @@ -573,6 +615,7 @@ pub impl BigUint { } #[cfg(target_arch = "x86_64")] +#[inline(always)] priv fn get_radix_base(radix: uint) -> (uint, uint) { assert!(1 < radix && radix <= 16); match radix { @@ -598,6 +641,7 @@ priv fn get_radix_base(radix: uint) -> (uint, uint) { #[cfg(target_arch = "arm")] #[cfg(target_arch = "x86")] #[cfg(target_arch = "mips")] +#[inline(always)] priv fn get_radix_base(radix: uint) -> (uint, uint) { assert!(1 < radix && radix <= 16); match radix { @@ -625,21 +669,26 @@ priv fn get_radix_base(radix: uint) -> (uint, uint) { pub enum Sign { Minus, Zero, Plus } impl Ord for Sign { + #[inline(always)] fn lt(&self, other: &Sign) -> bool { match self.cmp(other) { Less => true, _ => false} } + #[inline(always)] fn le(&self, other: &Sign) -> bool { match self.cmp(other) { Less | Equal => true, _ => false } } + #[inline(always)] fn ge(&self, other: &Sign) -> bool { match self.cmp(other) { Greater | Equal => true, _ => false } } + #[inline(always)] fn gt(&self, other: &Sign) -> bool { match self.cmp(other) { Greater => true, _ => false } } } impl TotalOrd for Sign { + #[inline(always)] fn cmp(&self, other: &Sign) -> Ordering { match (*self, *other) { (Minus, Minus) | (Zero, Zero) | (Plus, Plus) => Equal, @@ -651,6 +700,7 @@ impl TotalOrd for Sign { impl Neg<Sign> for Sign { /// Negate Sign value. + #[inline(always)] fn neg(&self) -> Sign { match *self { Minus => Plus, @@ -667,32 +717,40 @@ pub struct BigInt { } impl Eq for BigInt { + #[inline(always)] fn eq(&self, other: &BigInt) -> bool { self.equals(other) } + #[inline(always)] fn ne(&self, other: &BigInt) -> bool { !self.equals(other) } } impl TotalEq for BigInt { + #[inline(always)] fn equals(&self, other: &BigInt) -> bool { match self.cmp(other) { Equal => true, _ => false } } } impl Ord for BigInt { + #[inline(always)] fn lt(&self, other: &BigInt) -> bool { match self.cmp(other) { Less => true, _ => false} } + #[inline(always)] fn le(&self, other: &BigInt) -> bool { match self.cmp(other) { Less | Equal => true, _ => false } } + #[inline(always)] fn ge(&self, other: &BigInt) -> bool { match self.cmp(other) { Greater | Equal => true, _ => false } } + #[inline(always)] fn gt(&self, other: &BigInt) -> bool { match self.cmp(other) { Greater => true, _ => false } } } impl TotalOrd for BigInt { + #[inline(always)] fn cmp(&self, other: &BigInt) -> Ordering { let scmp = self.sign.cmp(&other.sign); if scmp != Equal { return scmp; } @@ -706,42 +764,50 @@ impl TotalOrd for BigInt { } impl ToStr for BigInt { + #[inline(always)] fn to_str(&self) -> ~str { self.to_str_radix(10) } } impl from_str::FromStr for BigInt { + #[inline(always)] fn from_str(s: &str) -> Option<BigInt> { FromStrRadix::from_str_radix(s, 10) } } impl Shl<uint, BigInt> for BigInt { + #[inline(always)] fn shl(&self, rhs: &uint) -> BigInt { BigInt::from_biguint(self.sign, self.data << *rhs) } } impl Shr<uint, BigInt> for BigInt { + #[inline(always)] fn shr(&self, rhs: &uint) -> BigInt { BigInt::from_biguint(self.sign, self.data >> *rhs) } } impl Zero for BigInt { + #[inline(always)] fn zero() -> BigInt { BigInt::from_biguint(Zero, Zero::zero()) } + #[inline(always)] fn is_zero(&self) -> bool { self.sign == Zero } } impl One for BigInt { + #[inline(always)] fn one() -> BigInt { BigInt::from_biguint(Plus, One::one()) } } impl Signed for BigInt { + #[inline(always)] fn abs(&self) -> BigInt { match self.sign { Plus | Zero => copy *self, @@ -749,6 +815,7 @@ impl Signed for BigInt { } } + #[inline(always)] fn signum(&self) -> BigInt { match self.sign { Plus => BigInt::from_biguint(Plus, One::one()), @@ -757,12 +824,15 @@ impl Signed for BigInt { } } + #[inline(always)] fn is_positive(&self) -> bool { self.sign == Plus } + #[inline(always)] fn is_negative(&self) -> bool { self.sign == Minus } } impl Add<BigInt, BigInt> for BigInt { + #[inline(always)] fn add(&self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { (Zero, _) => copy *other, @@ -777,6 +847,7 @@ impl Add<BigInt, BigInt> for BigInt { } impl Sub<BigInt, BigInt> for BigInt { + #[inline(always)] fn sub(&self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { (Zero, _) => -other, @@ -794,6 +865,7 @@ impl Sub<BigInt, BigInt> for BigInt { } impl Mul<BigInt, BigInt> for BigInt { + #[inline(always)] fn mul(&self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { (Zero, _) | (_, Zero) => Zero::zero(), @@ -808,6 +880,7 @@ impl Mul<BigInt, BigInt> for BigInt { } impl Quot<BigInt, BigInt> for BigInt { + #[inline(always)] fn quot(&self, other: &BigInt) -> BigInt { let (q, _) = self.quot_rem(other); return q; @@ -815,6 +888,7 @@ impl Quot<BigInt, BigInt> for BigInt { } impl Rem<BigInt, BigInt> for BigInt { + #[inline(always)] fn rem(&self, other: &BigInt) -> BigInt { let (_, r) = self.quot_rem(other); return r; @@ -822,6 +896,7 @@ impl Rem<BigInt, BigInt> for BigInt { } impl Neg<BigInt> for BigInt { + #[inline(always)] fn neg(&self) -> BigInt { BigInt::from_biguint(self.sign.neg(), copy self.data) } @@ -910,6 +985,7 @@ impl Integer for BigInt { } impl IntConvertible for BigInt { + #[inline(always)] fn to_int(&self) -> int { match self.sign { Plus => uint::min(self.to_uint(), int::max_value as uint) as int, @@ -919,6 +995,7 @@ impl IntConvertible for BigInt { } } + #[inline(always)] fn from_int(n: int) -> BigInt { if n > 0 { return BigInt::from_biguint(Plus, BigUint::from_uint(n as uint)); @@ -933,6 +1010,7 @@ impl IntConvertible for BigInt { } impl ToStrRadix for BigInt { + #[inline(always)] fn to_str_radix(&self, radix: uint) -> ~str { match self.sign { Plus => self.data.to_str_radix(radix), @@ -944,6 +1022,7 @@ impl ToStrRadix for BigInt { impl FromStrRadix for BigInt { /// Creates and initializes an BigInt. + #[inline(always)] fn from_str_radix(s: &str, radix: uint) -> Option<BigInt> { BigInt::parse_bytes(str::to_bytes(s), radix) @@ -952,11 +1031,13 @@ impl FromStrRadix for BigInt { pub impl BigInt { /// Creates and initializes an BigInt. + #[inline(always)] pub fn new(sign: Sign, v: ~[BigDigit]) -> BigInt { BigInt::from_biguint(sign, BigUint::new(v)) } /// Creates and initializes an BigInt. + #[inline(always)] pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt { if sign == Zero || data.is_zero() { return BigInt { sign: Zero, data: Zero::zero() }; @@ -965,17 +1046,20 @@ pub impl BigInt { } /// Creates and initializes an BigInt. + #[inline(always)] pub fn from_uint(n: uint) -> BigInt { if n == 0 { return Zero::zero(); } return BigInt::from_biguint(Plus, BigUint::from_uint(n)); } /// Creates and initializes an BigInt. + #[inline(always)] pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt { BigInt::from_biguint(sign, BigUint::from_slice(slice)) } /// Creates and initializes an BigInt. + #[inline(always)] pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<BigInt> { if buf.is_empty() { return None; } @@ -989,6 +1073,7 @@ pub impl BigInt { .map(|bu| BigInt::from_biguint(sign, *bu)); } + #[inline(always)] fn to_uint(&self) -> uint { match self.sign { Plus => self.data.to_uint(), |
