Skip to content

Commit 3830a3b

Browse files
committed
Replace old pow_with_uint with the new pow func
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)
1 parent aaf8ba7 commit 3830a3b

File tree

3 files changed

+8
-44
lines changed

3 files changed

+8
-44
lines changed

src/libextra/json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<T : Iterator<char>> Parser<T> {
688688
}
689689
}
690690

691-
let exp: f64 = num::pow_with_uint(10u, exp);
691+
let exp: f64 = num::pow(10u as f64, exp);
692692
if neg_exp {
693693
res /= exp;
694694
} else {

src/libstd/num/mod.rs

-37
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() }
@@ -1698,10 +1667,4 @@ mod bench {
16981667
let v = vec::from_fn(1024, |n| n);
16991668
b.iter(|| {v.iter().fold(0, |old, new| num::pow(old, *new));});
17001669
}
1701-
1702-
#[bench]
1703-
fn bench_pow_with_uint_function(b: &mut BenchHarness) {
1704-
let v = vec::from_fn(1024, |n| n);
1705-
b.iter(|| {v.iter().fold(0, |old, new| num::pow_with_uint(old, *new));});
1706-
}
17071670
}

src/libstd/num/strconv.rs

+7-6
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)