WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit f91a2da

Browse files
authored
Merge initial Int support (#730)
* Add `Int` type using two's complement representation (#695) * Add `Int` bit ops (#697)
2 parents f8c4ac7 + 9470032 commit f91a2da

28 files changed

+3548
-66
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ harness = false
7676
[[bench]]
7777
name = "uint"
7878
harness = false
79+
80+
[[bench]]
81+
name = "int"
82+
harness = false

benches/int.rs

Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
1+
use std::ops::Div;
2+
3+
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
4+
use num_traits::WrappingSub;
5+
use rand_core::OsRng;
6+
7+
use crypto_bigint::{NonZero, Random, I1024, I128, I2048, I256, I4096, I512};
8+
9+
fn bench_mul(c: &mut Criterion) {
10+
let mut group = c.benchmark_group("wrapping ops");
11+
12+
group.bench_function("split_mul, I128xI128", |b| {
13+
b.iter_batched(
14+
|| (I256::random(&mut OsRng), I256::random(&mut OsRng)),
15+
|(x, y)| black_box(x.split_mul(&y)),
16+
BatchSize::SmallInput,
17+
)
18+
});
19+
20+
group.bench_function("split_mul, I256xI256", |b| {
21+
b.iter_batched(
22+
|| (I256::random(&mut OsRng), I256::random(&mut OsRng)),
23+
|(x, y)| black_box(x.split_mul(&y)),
24+
BatchSize::SmallInput,
25+
)
26+
});
27+
28+
group.bench_function("split_mul, I512xI512", |b| {
29+
b.iter_batched(
30+
|| (I512::random(&mut OsRng), I512::random(&mut OsRng)),
31+
|(x, y)| black_box(x.split_mul(&y)),
32+
BatchSize::SmallInput,
33+
)
34+
});
35+
36+
group.bench_function("split_mul, I1024xI1024", |b| {
37+
b.iter_batched(
38+
|| (I1024::random(&mut OsRng), I1024::random(&mut OsRng)),
39+
|(x, y)| black_box(x.split_mul(&y)),
40+
BatchSize::SmallInput,
41+
)
42+
});
43+
44+
group.bench_function("split_mul, I2048xI2048", |b| {
45+
b.iter_batched(
46+
|| (I2048::random(&mut OsRng), I2048::random(&mut OsRng)),
47+
|(x, y)| black_box(x.split_mul(&y)),
48+
BatchSize::SmallInput,
49+
)
50+
});
51+
52+
group.bench_function("split_mul, I4096xI4096", |b| {
53+
b.iter_batched(
54+
|| (I4096::random(&mut OsRng), I4096::random(&mut OsRng)),
55+
|(x, y)| black_box(x.split_mul(&y)),
56+
BatchSize::SmallInput,
57+
)
58+
});
59+
}
60+
61+
fn bench_widening_mul(c: &mut Criterion) {
62+
let mut group = c.benchmark_group("widening ops");
63+
64+
group.bench_function("widening_mul, I128xI128", |b| {
65+
b.iter_batched(
66+
|| (I128::random(&mut OsRng), I128::random(&mut OsRng)),
67+
|(x, y)| black_box(x.widening_mul(&y)),
68+
BatchSize::SmallInput,
69+
)
70+
});
71+
72+
group.bench_function("widening_mul, I256xI256", |b| {
73+
b.iter_batched(
74+
|| (I256::random(&mut OsRng), I256::random(&mut OsRng)),
75+
|(x, y)| black_box(x.widening_mul(&y)),
76+
BatchSize::SmallInput,
77+
)
78+
});
79+
80+
group.bench_function("widening_mul, I512xI512", |b| {
81+
b.iter_batched(
82+
|| (I512::random(&mut OsRng), I512::random(&mut OsRng)),
83+
|(x, y)| black_box(x.widening_mul(&y)),
84+
BatchSize::SmallInput,
85+
)
86+
});
87+
88+
group.bench_function("widening_mul, I1024xI1024", |b| {
89+
b.iter_batched(
90+
|| (I1024::random(&mut OsRng), I1024::random(&mut OsRng)),
91+
|(x, y)| black_box(x.widening_mul(&y)),
92+
BatchSize::SmallInput,
93+
)
94+
});
95+
96+
group.bench_function("widening_mul, I2048xI2048", |b| {
97+
b.iter_batched(
98+
|| (I2048::random(&mut OsRng), I2048::random(&mut OsRng)),
99+
|(x, y)| black_box(x.widening_mul(&y)),
100+
BatchSize::SmallInput,
101+
)
102+
});
103+
104+
group.bench_function("widening_mul, I4096xI4096", |b| {
105+
b.iter_batched(
106+
|| (I4096::random(&mut OsRng), I4096::random(&mut OsRng)),
107+
|(x, y)| black_box(x.widening_mul(&y)),
108+
BatchSize::SmallInput,
109+
)
110+
});
111+
}
112+
113+
fn bench_div(c: &mut Criterion) {
114+
let mut group = c.benchmark_group("wrapping ops");
115+
116+
group.bench_function("div, I256/I128, full size", |b| {
117+
b.iter_batched(
118+
|| {
119+
let x = I256::random(&mut OsRng);
120+
let y = I128::random(&mut OsRng).resize::<{ I256::LIMBS }>();
121+
(x, NonZero::new(y).unwrap())
122+
},
123+
|(x, y)| black_box(x.div(&y)),
124+
BatchSize::SmallInput,
125+
)
126+
});
127+
128+
group.bench_function("div, I512/I256, full size", |b| {
129+
b.iter_batched(
130+
|| {
131+
let x = I512::random(&mut OsRng);
132+
let y = I256::random(&mut OsRng).resize::<{ I512::LIMBS }>();
133+
(x, NonZero::new(y).unwrap())
134+
},
135+
|(x, y)| black_box(x.div(&y)),
136+
BatchSize::SmallInput,
137+
)
138+
});
139+
140+
group.bench_function("div, I1024/I512, full size", |b| {
141+
b.iter_batched(
142+
|| {
143+
let x = I1024::random(&mut OsRng);
144+
let y = I512::random(&mut OsRng).resize::<{ I1024::LIMBS }>();
145+
(x, NonZero::new(y).unwrap())
146+
},
147+
|(x, y)| black_box(x.div(&y)),
148+
BatchSize::SmallInput,
149+
)
150+
});
151+
152+
group.bench_function("div, I2048/I1024, full size", |b| {
153+
b.iter_batched(
154+
|| {
155+
let x = I2048::random(&mut OsRng);
156+
let y = I1024::random(&mut OsRng).resize::<{ I2048::LIMBS }>();
157+
(x, NonZero::new(y).unwrap())
158+
},
159+
|(x, y)| black_box(x.div(&y)),
160+
BatchSize::SmallInput,
161+
)
162+
});
163+
164+
group.bench_function("div, I4096/I2048, full size", |b| {
165+
b.iter_batched(
166+
|| {
167+
let x = I4096::random(&mut OsRng);
168+
let y = I2048::random(&mut OsRng).resize::<{ I4096::LIMBS }>();
169+
(x, NonZero::new(y).unwrap())
170+
},
171+
|(x, y)| black_box(x.div(&y)),
172+
BatchSize::SmallInput,
173+
)
174+
});
175+
176+
group.finish();
177+
}
178+
179+
fn bench_add(c: &mut Criterion) {
180+
let mut group = c.benchmark_group("wrapping ops");
181+
182+
group.bench_function("add, I128+I128", |b| {
183+
b.iter_batched(
184+
|| {
185+
let x = I128::random(&mut OsRng);
186+
let y = I128::random(&mut OsRng);
187+
(x, y)
188+
},
189+
|(x, y)| black_box(x.wrapping_add(&y)),
190+
BatchSize::SmallInput,
191+
)
192+
});
193+
194+
group.bench_function("add, I256+I256", |b| {
195+
b.iter_batched(
196+
|| {
197+
let x = I256::random(&mut OsRng);
198+
let y = I256::random(&mut OsRng);
199+
(x, y)
200+
},
201+
|(x, y)| black_box(x.wrapping_add(&y)),
202+
BatchSize::SmallInput,
203+
)
204+
});
205+
206+
group.bench_function("add, I512+I512", |b| {
207+
b.iter_batched(
208+
|| {
209+
let x = I512::random(&mut OsRng);
210+
let y = I512::random(&mut OsRng);
211+
(x, y)
212+
},
213+
|(x, y)| black_box(x.wrapping_add(&y)),
214+
BatchSize::SmallInput,
215+
)
216+
});
217+
218+
group.bench_function("add, I1024+I1024", |b| {
219+
b.iter_batched(
220+
|| {
221+
let x = I1024::random(&mut OsRng);
222+
let y = I1024::random(&mut OsRng);
223+
(x, y)
224+
},
225+
|(x, y)| black_box(x.wrapping_add(&y)),
226+
BatchSize::SmallInput,
227+
)
228+
});
229+
230+
group.bench_function("add, I2048+I2048", |b| {
231+
b.iter_batched(
232+
|| {
233+
let x = I2048::random(&mut OsRng);
234+
let y = I2048::random(&mut OsRng);
235+
(x, y)
236+
},
237+
|(x, y)| black_box(x.wrapping_add(&y)),
238+
BatchSize::SmallInput,
239+
)
240+
});
241+
242+
group.bench_function("add, I4096+I4096", |b| {
243+
b.iter_batched(
244+
|| {
245+
let x = I4096::random(&mut OsRng);
246+
let y = I4096::random(&mut OsRng);
247+
(x, y)
248+
},
249+
|(x, y)| black_box(x.wrapping_add(&y)),
250+
BatchSize::SmallInput,
251+
)
252+
});
253+
254+
group.finish();
255+
}
256+
257+
fn bench_sub(c: &mut Criterion) {
258+
let mut group = c.benchmark_group("wrapping ops");
259+
260+
group.bench_function("sub, I128-I128", |b| {
261+
b.iter_batched(
262+
|| {
263+
let x = I128::random(&mut OsRng);
264+
let y = I128::random(&mut OsRng);
265+
(x, y)
266+
},
267+
|(x, y)| black_box(x.wrapping_sub(&y)),
268+
BatchSize::SmallInput,
269+
)
270+
});
271+
272+
group.bench_function("sub, I256-I256", |b| {
273+
b.iter_batched(
274+
|| {
275+
let x = I256::random(&mut OsRng);
276+
let y = I256::random(&mut OsRng);
277+
(x, y)
278+
},
279+
|(x, y)| black_box(x.wrapping_sub(&y)),
280+
BatchSize::SmallInput,
281+
)
282+
});
283+
284+
group.bench_function("sub, I512-I512", |b| {
285+
b.iter_batched(
286+
|| {
287+
let x = I512::random(&mut OsRng);
288+
let y = I512::random(&mut OsRng);
289+
(x, y)
290+
},
291+
|(x, y)| black_box(x.wrapping_sub(&y)),
292+
BatchSize::SmallInput,
293+
)
294+
});
295+
296+
group.bench_function("sub, I1024-I1024", |b| {
297+
b.iter_batched(
298+
|| {
299+
let x = I1024::random(&mut OsRng);
300+
let y = I1024::random(&mut OsRng);
301+
(x, y)
302+
},
303+
|(x, y)| black_box(x.wrapping_sub(&y)),
304+
BatchSize::SmallInput,
305+
)
306+
});
307+
308+
group.bench_function("sub, I2048-I2048", |b| {
309+
b.iter_batched(
310+
|| {
311+
let x = I2048::random(&mut OsRng);
312+
let y = I2048::random(&mut OsRng);
313+
(x, y)
314+
},
315+
|(x, y)| black_box(x.wrapping_sub(&y)),
316+
BatchSize::SmallInput,
317+
)
318+
});
319+
320+
group.bench_function("sub, I4096-I4096", |b| {
321+
b.iter_batched(
322+
|| {
323+
let x = I4096::random(&mut OsRng);
324+
let y = I4096::random(&mut OsRng);
325+
(x, y)
326+
},
327+
|(x, y)| black_box(x.wrapping_sub(&y)),
328+
BatchSize::SmallInput,
329+
)
330+
});
331+
332+
group.finish();
333+
}
334+
335+
criterion_group!(
336+
benches,
337+
bench_mul,
338+
bench_widening_mul,
339+
bench_div,
340+
bench_add,
341+
bench_sub,
342+
);
343+
344+
criterion_main!(benches);

0 commit comments

Comments
 (0)