Skip to content

Commit 1b0356b

Browse files
committed
cargo clippied factor_bench and rollback the conversion for u128 (big_uint represents u128 nums with u64s and bit shifting)
1 parent 7bd5dc6 commit 1b0356b

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/uu/factor/benches/factor_bench.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use uucore::benchmark::run_util_function;
1111
#[divan::bench]
1212
fn factor_single_u64(bencher: Bencher) {
1313
bencher
14-
.with_inputs(|| 1000000 as u64)
14+
.with_inputs(|| 1000000_u64)
1515
.bench_values(|single_u64| {
1616
black_box(run_util_function(uumain, &[&single_u64.to_string()]));
1717
});
@@ -21,9 +21,9 @@ fn factor_single_u64(bencher: Bencher) {
2121
#[divan::bench]
2222
fn factor_multiple_u64s(bencher: Bencher) {
2323
bencher
24-
.with_inputs(|| (2 as u64, 1000000 as u64))
24+
.with_inputs(|| (2_u64, 1000000_u64))
2525
.bench_values(|(start_u64, end_u64)| {
26-
for u64_digit in start_u64..end_u64 + 1 {
26+
for u64_digit in start_u64..=end_u64 {
2727
black_box(run_util_function(uumain, &[&u64_digit.to_string()]));
2828
}
2929
});
@@ -33,7 +33,7 @@ fn factor_multiple_u64s(bencher: Bencher) {
3333
#[divan::bench]
3434
fn factor_single_u128(bencher: Bencher) {
3535
bencher
36-
.with_inputs(|| 18446744073709551616 as u128)
36+
.with_inputs(|| 18446744073709551616_u128)
3737
.bench_values(|single_u128| {
3838
black_box(run_util_function(uumain, &[&single_u128.to_string()]));
3939
});
@@ -45,10 +45,10 @@ fn factor_multiple_u128s(bencher: Bencher) {
4545
bencher
4646
.with_inputs(|| {
4747
// this is a range of 1 million different u128 integers
48-
(18446744073709551616 as u128, 18446744073710551616 as u128)
48+
(18446744073709551616_u128, 18446744073710551616_u128)
4949
})
5050
.bench_values(|(start_u128, end_u128)| {
51-
for u128_digit in start_u128..end_u128 + 1 {
51+
for u128_digit in start_u128..=end_u128 {
5252
black_box(run_util_function(uumain, &[&u128_digit.to_string()]));
5353
}
5454
});
@@ -61,7 +61,7 @@ fn factor_single_big_uint(bencher: Bencher) {
6161
bencher
6262
.with_inputs(|| "340_282_366_920_938_463_463_374_607_431_768_211_456")
6363
.bench_values(|single_big_uint| {
64-
black_box(run_util_function(uumain, &[&single_big_uint.to_string()]));
64+
black_box(run_util_function(uumain, &[single_big_uint]));
6565
});
6666
}
6767

@@ -70,11 +70,11 @@ fn factor_single_big_uint(bencher: Bencher) {
7070
fn factor_multiple_big_uint(bencher: Bencher) {
7171
// max u128 value is 340_282_366_920_938_463_463_374_607_431_768_211_455
7272
bencher
73-
.with_inputs(|| (768_211_456, 769_211_456))
73+
.with_inputs(|| (768_211_456_u64, 769_211_456_u64))
7474
.bench_values(|(start_big_uint, end_big_uint)| {
75-
for digit in start_big_uint..end_big_uint + 1 {
76-
let big_uint_str = format!("340282366920938463463374607431768211456{}", digit);
77-
black_box(run_util_function(uumain, &[&big_uint_str.to_string()]));
75+
for digit in start_big_uint..=end_big_uint {
76+
let big_uint_str = format!("340282366920938463463374607431768211456{digit}");
77+
black_box(run_util_function(uumain, &[&big_uint_str]));
7878
}
7979
});
8080
}

src/uu/factor/src/factor.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ fn print_factors_str(
4545
}
4646
// use num_prime's factorize128 algorithm for u128 integers
4747
else if x <= BigUint::from_u128(u128::MAX).unwrap() {
48-
// if it's a u128 integer, BigUint will have two u64 digits
49-
// in its Vec<u64>, which index 0 is treated as u64::MAX
50-
let big_uint_u64s = x.clone().to_u64_digits();
51-
let x = u64::MAX as u128 + big_uint_u64s[1] as u128;
48+
let rx = num_str.trim().parse::<u128>();
49+
let Ok(x) = rx else {
50+
// return Ok(). it's non-fatal and we should try the next number.
51+
show_warning!("{}: {}", num_str.maybe_quote(), rx.unwrap_err());
52+
set_exit_code(1);
53+
return Ok(());
54+
};
5255
let prime_factors = num_prime::nt_funcs::factorize128(x);
5356
write_result_u128(w, &x, prime_factors, print_exponents)
5457
.map_err_context(|| translate!("factor-error-write-error"))?;

0 commit comments

Comments
 (0)