Skip to content

Commit 016bf1f

Browse files
authored
Always Run Extended Multiplication Benchmarks (#48)
1 parent 5718516 commit 016bf1f

File tree

2 files changed

+47
-81
lines changed

2 files changed

+47
-81
lines changed

bench/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ harness = false
1010

1111
[features]
1212
llvm-intrinsics = ["ethnum/llvm-intrinsics"]
13-
extra-muls = []
1413

1514
[dependencies]
1615
ethnum = { path = ".." }

bench/benches/num.rs

Lines changed: 47 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
22
#[cfg(not(any(feature = "primitive-types", feature = "ruint")))]
3-
use ethnum::U256;
3+
use ethnum::{I256, U256};
44
#[cfg(feature = "primitive-types")]
55
use primitive_types::U256;
66
#[cfg(feature = "ruint")]
@@ -40,17 +40,21 @@ fn arithmetic(c: &mut Criterion) {
4040
_ => "#",
4141
}
4242
};
43-
44-
#[allow(dead_code)]
45-
fn name_fn(x: U256) -> String {
46-
let n = x.leading_zeros() / 64;
47-
match n {
48-
0 => String::from("####"),
49-
1 => String::from("###"),
50-
2 => String::from("##"),
51-
_ => String::from("#"),
43+
#[cfg(not(any(feature = "primitive-types", feature = "ruint")))]
44+
let iname = |x: I256| {
45+
let neg = x.is_negative();
46+
let n = x.abs().leading_zeros() / 64;
47+
match (neg, n) {
48+
(false, 0) => "####",
49+
(false, 1) => "###",
50+
(false, 2) => "##",
51+
(false, _) => "#",
52+
(true, 0) => "-####",
53+
(true, 1) => "-###",
54+
(true, 2) => "-##",
55+
(true, _) => "-#",
5256
}
53-
}
57+
};
5458

5559
c.bench_function("U256::add", |b| {
5660
b.iter(|| black_box(nums[0]) + black_box(nums[1]))
@@ -71,90 +75,53 @@ fn arithmetic(c: &mut Criterion) {
7175
);
7276
}
7377

74-
#[cfg(not(feature = "extra-muls"))]
7578
c.bench_function("U256::mul", |b| {
7679
b.iter(|| black_box(nums[3]) * black_box(nums[5]))
7780
});
7881

79-
#[cfg(not(any(feature = "primitive-types", feature = "extra-muls")))]
80-
c.bench_function("U256::wrapping_mul", |b| {
81-
b.iter(|| black_box(nums[0]).wrapping_mul(black_box(nums[1])))
82-
});
82+
for (x, y, tag) in [
83+
(nums[0], nums[1], "overflow"),
84+
(nums[3], nums[5], "no overflow"),
85+
(nums[3], nums[4], "overflow"),
86+
(nums[5], nums[3], "no overflow"),
87+
(nums[4], nums[3], "overflow"),
88+
(nums[6], nums[7], "no overflow"),
89+
] {
90+
let name = format!("{}*{} ({})", name(x), name(y), tag);
8391

84-
#[cfg(feature = "extra-muls")]
85-
{
86-
fn get_name(
87-
x1: U256,
88-
y1: U256,
89-
will_overflow1: bool,
90-
x_sign: bool,
91-
y_sign: bool,
92-
) -> String {
93-
format!(
94-
"{}{} * {}{} {}",
95-
if x_sign { "-" } else { " " },
96-
name_fn(x1),
97-
if y_sign { "-" } else { " " },
98-
name_fn(y1),
99-
if will_overflow1 {
100-
"overflow"
101-
} else {
102-
"no overflow"
103-
}
104-
)
105-
}
106-
for (x, y, will_overflow) in [
107-
(nums[0], nums[1], true),
108-
(nums[3], nums[5], false),
109-
(nums[3], nums[4], true),
110-
(nums[5], nums[3], false),
111-
(nums[4], nums[3], true),
112-
(nums[6], nums[7], false),
92+
#[cfg(not(feature = "primitive-types"))]
93+
c.bench_with_input(
94+
BenchmarkId::new("U256::wrapping_mul", &name),
95+
&(x, y),
96+
|b, &(x, y)| b.iter(|| black_box(x).wrapping_mul(black_box(y))),
97+
);
98+
99+
c.bench_with_input(
100+
BenchmarkId::new("U256::overflowing_mul", &name),
101+
&(x, y),
102+
|b, &(x, y)| b.iter(|| black_box(x).overflowing_mul(black_box(y))),
103+
);
104+
105+
#[cfg(not(any(feature = "primitive-types", feature = "ruint")))]
106+
for (x, y) in [
107+
(x.as_i256(), y.as_i256()),
108+
(x.as_i256(), y.as_i256().wrapping_neg()),
109+
(x.as_i256().wrapping_neg(), y.as_i256()),
110+
(x.as_i256().wrapping_neg(), y.as_i256().wrapping_neg()),
113111
] {
112+
let name = format!("{}*{} ({})", iname(x), iname(y), tag);
113+
114114
c.bench_with_input(
115-
BenchmarkId::new(
116-
"U256::wrapping_mul",
117-
get_name(x, y, will_overflow, false, false),
118-
),
115+
BenchmarkId::new("I256::wrapping_mul", &name),
119116
&(x, y),
120117
|b, &(x, y)| b.iter(|| black_box(x).wrapping_mul(black_box(y))),
121118
);
122119

123120
c.bench_with_input(
124-
BenchmarkId::new(
125-
"U256::overflowing_mul",
126-
get_name(x, y, will_overflow, false, false),
127-
),
121+
BenchmarkId::new("I256::overflowing_mul", &name),
128122
&(x, y),
129123
|b, &(x, y)| b.iter(|| black_box(x).overflowing_mul(black_box(y))),
130124
);
131-
132-
for (x_sign, y_sign) in [(false, false), (false, true), (true, false), (true, true)] {
133-
let signed_x = if x_sign { 0 - x.as_i256() } else { x.as_i256() };
134-
let signed_y = if y_sign { 0 - y.as_i256() } else { y.as_i256() };
135-
136-
c.bench_with_input(
137-
BenchmarkId::new(
138-
"I256::wrapping_mul",
139-
get_name(x, y, will_overflow, x_sign, y_sign),
140-
),
141-
&(signed_x, signed_y),
142-
|b, &(signed_x, signed_y)| {
143-
b.iter(|| black_box(signed_x).wrapping_mul(black_box(signed_y)))
144-
},
145-
);
146-
147-
c.bench_with_input(
148-
BenchmarkId::new(
149-
"I256::overflowing_mul",
150-
get_name(x, y, will_overflow, x_sign, y_sign),
151-
),
152-
&(signed_x, signed_y),
153-
|b, &(signed_x, signed_y)| {
154-
b.iter(|| black_box(signed_x).overflowing_mul(black_box(signed_y)))
155-
},
156-
);
157-
}
158125
}
159126
}
160127

0 commit comments

Comments
 (0)