Skip to content

Commit 4d58cdb

Browse files
committed
Add benchmarks for floating point math
This adds comparisons among the compiler-builtins function, system functions if available, and optionally handwritten assembly. These also serve as some additional testing since we check our functions against assembly operations.
1 parent 449643f commit 4d58cdb

12 files changed

+1242
-0
lines changed

ci/run.sh

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ else
2424
run="cargo test --manifest-path testcrate/Cargo.toml --no-fail-fast --target $target"
2525
$run
2626
$run --release
27+
$run --benches
2728
$run --features c
2829
$run --features c --release
2930
$run --features no-asm

testcrate/Cargo.toml

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ path = ".."
2121
default-features = false
2222
features = ["public-test-deps"]
2323

24+
[dev-dependencies]
25+
criterion = { version = "0.5.1", default-features = false }
26+
paste = "1.0.15"
27+
2428
[target.'cfg(all(target_arch = "arm", not(any(target_env = "gnu", target_env = "musl")), target_os = "linux"))'.dev-dependencies]
2529
test = { git = "https://github.com/japaric/utest" }
2630
utest-cortex-m-qemu = { default-features = false, git = "https://github.com/japaric/utest" }
@@ -35,3 +39,35 @@ mem = ["compiler_builtins/mem"]
3539
mangled-names = ["compiler_builtins/mangled-names"]
3640
# Skip tests that rely on f128 symbols being available on the system
3741
no-sys-f128 = []
42+
43+
[[bench]]
44+
name = "float_add"
45+
harness = false
46+
47+
[[bench]]
48+
name = "float_sub"
49+
harness = false
50+
51+
[[bench]]
52+
name = "float_mul"
53+
harness = false
54+
55+
[[bench]]
56+
name = "float_div"
57+
harness = false
58+
59+
[[bench]]
60+
name = "float_cmp"
61+
harness = false
62+
63+
[[bench]]
64+
name = "float_conv"
65+
harness = false
66+
67+
[[bench]]
68+
name = "float_extend"
69+
harness = false
70+
71+
[[bench]]
72+
name = "float_trunc"
73+
harness = false

testcrate/benches/float_add.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#![feature(f128)]
2+
3+
use compiler_builtins::float::add;
4+
use criterion::{criterion_group, criterion_main, Criterion};
5+
use testcrate::float_bench;
6+
7+
float_bench! {
8+
name: add_f32,
9+
sig: (a: f32, b: f32) -> f32,
10+
crate_fn: add::__addsf3,
11+
sys_fn: __addsf3,
12+
sys_available: all(),
13+
asm: [
14+
#[cfg(target_arch = "x86_64")]
15+
asm!(
16+
"addss xmm0, xmm1",
17+
"ret",
18+
);
19+
20+
#[cfg(target_arch = "aarch64")]
21+
asm!(
22+
"fadd s0, s0, s1",
23+
"ret",
24+
);
25+
],
26+
}
27+
28+
float_bench! {
29+
name: add_f64,
30+
sig: (a: f64, b: f64) -> f64,
31+
crate_fn: add::__adddf3,
32+
sys_fn: __adddf3,
33+
sys_available: all(),
34+
asm: [
35+
#[cfg(target_arch = "x86_64")]
36+
asm!(
37+
"addsd xmm0, xmm1",
38+
"ret",
39+
);
40+
41+
#[cfg(target_arch = "aarch64")]
42+
asm!(
43+
"fadd d0, d0, d1",
44+
"ret",
45+
);
46+
],
47+
}
48+
49+
float_bench! {
50+
name: add_f128,
51+
sig: (a: f128, b: f128) -> f128,
52+
crate_fn: add::__addtf3,
53+
sys_fn: __addtf3,
54+
sys_available: not(feature = "no-sys-f128"),
55+
asm: []
56+
}
57+
58+
criterion_group!(float_add, add_f32, add_f64, add_f128);
59+
criterion_main!(float_add);

testcrate/benches/float_cmp.rs

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#![feature(f128)]
2+
3+
use criterion::{criterion_group, criterion_main, Criterion};
4+
use testcrate::float_bench;
5+
6+
use compiler_builtins::float::cmp;
7+
8+
float_bench! {
9+
name: cmp_f32_gt,
10+
sig: (a: f32, b: f32) -> i32,
11+
crate_fn: cmp::__gtsf2,
12+
sys_fn: __gtsf2,
13+
sys_available: all(),
14+
asm: [
15+
#[cfg(target_arch = "x86_64")]
16+
asm!(
17+
"xor eax, eax",
18+
"ucomiss xmm0, xmm1",
19+
"seta al",
20+
"ret",
21+
);
22+
23+
#[cfg(target_arch = "aarch64")]
24+
asm!(
25+
"fcmp s0, s1",
26+
"cset w0, gt",
27+
"ret",
28+
);
29+
],
30+
}
31+
32+
float_bench! {
33+
name: cmp_f32_unord,
34+
sig: (a: f32, b: f32) -> i32,
35+
crate_fn: cmp::__unordsf2,
36+
sys_fn: __unordsf2,
37+
sys_available: all(),
38+
asm: [
39+
#[cfg(target_arch = "x86_64")]
40+
asm!(
41+
"cmpneqss xmm0, xmm1",
42+
"movd eax, xmm0",
43+
"and eax, 1",
44+
"ret",
45+
);
46+
47+
#[cfg(target_arch = "aarch64")]
48+
asm!(
49+
"fcmp s0, s1",
50+
"cset w0, eq",
51+
"ret",
52+
);
53+
],
54+
}
55+
56+
float_bench! {
57+
name: cmp_f64_gt,
58+
sig: (a: f64, b: f64) -> i32,
59+
crate_fn: cmp::__gtdf2,
60+
sys_fn: __gtdf2,
61+
sys_available: all(),
62+
asm: [
63+
#[cfg(target_arch = "x86_64")]
64+
asm!(
65+
"xor eax, eax",
66+
"ucomisd xmm0, xmm1",
67+
"seta al",
68+
"ret",
69+
);
70+
71+
#[cfg(target_arch = "aarch64")]
72+
asm!(
73+
"fcmp d0, d1",
74+
"cset w0, gt",
75+
"ret",
76+
);
77+
],
78+
}
79+
80+
float_bench! {
81+
name: cmp_f64_unord,
82+
sig: (a: f64, b: f64) -> i32,
83+
crate_fn: cmp::__unorddf2,
84+
sys_fn: __unorddf2,
85+
sys_available: all(),
86+
asm: [
87+
#[cfg(target_arch = "x86_64")]
88+
asm!(
89+
"cmpeqsd xmm0, xmm1",
90+
"movq rax, xmm0",
91+
"and eax, 1",
92+
"ret",
93+
);
94+
95+
#[cfg(target_arch = "aarch64")]
96+
asm!(
97+
"fcmp d0, d1",
98+
"cset w0, eq",
99+
"ret",
100+
);
101+
],
102+
}
103+
104+
float_bench! {
105+
name: cmp_f128_gt,
106+
sig: (a: f128, b: f128) -> i32,
107+
crate_fn: cmp::__gttf2,
108+
sys_fn: __gttf2,
109+
sys_available: not(feature = "no-sys-f128"),
110+
asm: []
111+
}
112+
113+
float_bench! {
114+
name: cmp_f128_unord,
115+
sig: (a: f128, b: f128) -> i32,
116+
crate_fn: cmp::__unordtf2,
117+
sys_fn: __unordtf2,
118+
sys_available: not(feature = "no-sys-f128"),
119+
asm: []
120+
}
121+
122+
criterion_group!(
123+
float_cmp,
124+
cmp_f32_gt,
125+
cmp_f32_unord,
126+
cmp_f64_gt,
127+
cmp_f64_unord,
128+
cmp_f128_gt,
129+
cmp_f128_unord
130+
);
131+
criterion_main!(float_cmp);

0 commit comments

Comments
 (0)