Skip to content

Commit 764f2cb

Browse files
committed
auto merge of #11649 : FlaPer87/rust/pow, r=cmr
There was an old and barely used implementation of pow, which expected both parameters to be uint and required more traits to be implemented. Since a new implementation for `pow` landed, I'm proposing to remove this old impl in favor of the new one. The benchmark shows that the new implementation is faster than the one being removed: ``` test num::bench::bench_pow_function ..bench: 9429 ns/iter (+/- 2055) test num::bench::bench_pow_with_uint_function ...bench: 28476 ns/iter (+/- 2202) ```
2 parents 0e6455e + 3830a3b commit 764f2cb

File tree

3 files changed

+23
-38
lines changed

3 files changed

+23
-38
lines changed

src/libextra/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<T : Iterator<char>> Parser<T> {
909909
}
910910
}
911911

912-
let exp: f64 = num::pow_with_uint(10u, exp);
912+
let exp: f64 = num::pow(10u as f64, exp);
913913
if neg_exp {
914914
res /= exp;
915915
} else {

src/libstd/num/mod.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -993,37 +993,6 @@ pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
993993
FromStrRadix::from_str_radix(str, radix)
994994
}
995995

996-
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
997-
///
998-
/// Returns `radix^pow` as `T`.
999-
///
1000-
/// Note:
1001-
/// Also returns `1` for `0^0`, despite that technically being an
1002-
/// undefined number. The reason for this is twofold:
1003-
/// - If code written to use this function cares about that special case, it's
1004-
/// probably going to catch it before making the call.
1005-
/// - If code written to use this function doesn't care about it, it's
1006-
/// probably assuming that `x^0` always equals `1`.
1007-
///
1008-
pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
1009-
let _0: T = Zero::zero();
1010-
let _1: T = One::one();
1011-
1012-
if pow == 0u { return _1; }
1013-
if radix == 0u { return _0; }
1014-
let mut my_pow = pow;
1015-
let mut total = _1;
1016-
let mut multiplier = cast(radix).unwrap();
1017-
while (my_pow > 0u) {
1018-
if my_pow % 2u == 1u {
1019-
total = total * multiplier;
1020-
}
1021-
my_pow = my_pow / 2u;
1022-
multiplier = multiplier * multiplier;
1023-
}
1024-
total
1025-
}
1026-
1027996
impl<T: Zero + 'static> Zero for @T {
1028997
fn zero() -> @T { @Zero::zero() }
1029998
fn is_zero(&self) -> bool { (**self).is_zero() }
@@ -1684,3 +1653,18 @@ mod tests {
16841653
assert_pow(2u64, 50);
16851654
}
16861655
}
1656+
1657+
1658+
#[cfg(test)]
1659+
mod bench {
1660+
use num;
1661+
use vec;
1662+
use prelude::*;
1663+
use extra::test::BenchHarness;
1664+
1665+
#[bench]
1666+
fn bench_pow_function(b: &mut BenchHarness) {
1667+
let v = vec::from_fn(1024, |n| n);
1668+
b.iter(|| {v.iter().fold(0, |old, new| num::pow(old, *new));});
1669+
}
1670+
}

src/libstd/num/strconv.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use str::{StrSlice};
2020
use str;
2121
use vec::{CopyableVector, ImmutableVector, MutableVector};
2222
use vec::OwnedVector;
23-
use num::{NumCast, Zero, One, cast, pow_with_uint, Integer};
23+
use num;
24+
use num::{NumCast, Zero, One, cast, Integer};
2425
use num::{Round, Float, FPNaN, FPInfinite, ToPrimitive};
2526

2627
pub enum ExponentFormat {
@@ -648,10 +649,10 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
648649

649650
if exp_found {
650651
let c = buf[i] as char;
651-
let base = match (c, exponent) {
652+
let base: T = match (c, exponent) {
652653
// c is never _ so don't need to handle specially
653-
('e', ExpDec) | ('E', ExpDec) => 10u,
654-
('p', ExpBin) | ('P', ExpBin) => 2u,
654+
('e', ExpDec) | ('E', ExpDec) => cast(10u).unwrap(),
655+
('p', ExpBin) | ('P', ExpBin) => cast(2u).unwrap(),
655656
_ => return None // char doesn't fit given exponent format
656657
};
657658

@@ -664,9 +665,9 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
664665
match exp {
665666
Some(exp_pow) => {
666667
multiplier = if exp_pow < 0 {
667-
_1 / pow_with_uint::<T>(base, (-exp_pow.to_int().unwrap()) as uint)
668+
_1 / num::pow(base, (-exp_pow.to_int().unwrap()) as uint)
668669
} else {
669-
pow_with_uint::<T>(base, exp_pow.to_int().unwrap() as uint)
670+
num::pow(base, exp_pow.to_int().unwrap() as uint)
670671
}
671672
}
672673
None => return None // invalid exponent -> invalid number

0 commit comments

Comments
 (0)