Skip to content

Commit 6bbd69a

Browse files
Add a runnable example program also
1 parent 4b54558 commit 6bbd69a

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

examples/main_example.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#![feature(bench_black_box)]
2+
3+
use core::hint::black_box;
4+
use staticstep::*;
5+
6+
// You can run this with "--emit asm" as an easy way of comparing the assembly output from this
7+
// crate's functions to the assembly output from a traditional loop.
8+
9+
#[inline(never)]
10+
fn inc_by_exclusive() {
11+
let mut j = 0;
12+
for i in (0..32768).inc_by::<16>() {
13+
j += black_box(i);
14+
}
15+
println!("{}", j);
16+
}
17+
18+
#[inline(never)]
19+
fn inc_by_inclusive() {
20+
let mut j = 0;
21+
for i in (0..=32768).inc_by::<16>() {
22+
j += black_box(i);
23+
}
24+
println!("{}", j);
25+
}
26+
27+
#[inline(never)]
28+
fn dec_by_exclusive() {
29+
let mut j = 0;
30+
for i in (32768..0).dec_by::<16>() {
31+
j += black_box(i);
32+
}
33+
println!("{}", j);
34+
}
35+
36+
#[inline(never)]
37+
fn dec_by_inclusive() {
38+
let mut j = 0;
39+
for i in (32768..=0).dec_by::<16>() {
40+
j += black_box(i);
41+
}
42+
println!("{}", j);
43+
}
44+
45+
#[inline(never)]
46+
fn while_loop_inc_exclusive() {
47+
let mut j = 0;
48+
let mut i = 0;
49+
while i < 32768 {
50+
j += black_box(i);
51+
i += 16;
52+
}
53+
println!("{}", j);
54+
}
55+
56+
#[inline(never)]
57+
fn while_loop_inc_inclusive() {
58+
let mut j = 0;
59+
let mut i = 0;
60+
while i <= 32768 {
61+
j += black_box(i);
62+
i += 16;
63+
}
64+
println!("{}", j);
65+
}
66+
67+
#[inline(never)]
68+
fn while_loop_dec_exclusive() {
69+
let mut j = 0usize;
70+
let mut i = 32768isize;
71+
while i > 0 {
72+
j += black_box(i as usize);
73+
i -= 16;
74+
}
75+
println!("{}", j);
76+
}
77+
78+
#[inline(never)]
79+
fn while_loop_dec_inclusive() {
80+
let mut j = 0usize;
81+
let mut i = 32768isize;
82+
while i >= 0 {
83+
j += black_box(i as usize);
84+
i -= 16;
85+
}
86+
println!("{}", j);
87+
}
88+
89+
fn main() {
90+
inc_by_exclusive();
91+
inc_by_inclusive();
92+
dec_by_exclusive();
93+
dec_by_inclusive();
94+
while_loop_inc_exclusive();
95+
while_loop_inc_inclusive();
96+
while_loop_dec_exclusive();
97+
while_loop_dec_inclusive();
98+
}

0 commit comments

Comments
 (0)