Skip to content

Commit

Permalink
Add benchmarks for floating point math
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgross35 committed May 20, 2024
1 parent 449643f commit 4d58cdb
Show file tree
Hide file tree
Showing 12 changed files with 1,242 additions and 0 deletions.
1 change: 1 addition & 0 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ else
run="cargo test --manifest-path testcrate/Cargo.toml --no-fail-fast --target $target"
$run
$run --release
$run --benches
$run --features c
$run --features c --release
$run --features no-asm
Expand Down
36 changes: 36 additions & 0 deletions testcrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ path = ".."
default-features = false
features = ["public-test-deps"]

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
paste = "1.0.15"

[target.'cfg(all(target_arch = "arm", not(any(target_env = "gnu", target_env = "musl")), target_os = "linux"))'.dev-dependencies]
test = { git = "https://github.com/japaric/utest" }
utest-cortex-m-qemu = { default-features = false, git = "https://github.com/japaric/utest" }
Expand All @@ -35,3 +39,35 @@ mem = ["compiler_builtins/mem"]
mangled-names = ["compiler_builtins/mangled-names"]
# Skip tests that rely on f128 symbols being available on the system
no-sys-f128 = []

[[bench]]
name = "float_add"
harness = false

[[bench]]
name = "float_sub"
harness = false

[[bench]]
name = "float_mul"
harness = false

[[bench]]
name = "float_div"
harness = false

[[bench]]
name = "float_cmp"
harness = false

[[bench]]
name = "float_conv"
harness = false

[[bench]]
name = "float_extend"
harness = false

[[bench]]
name = "float_trunc"
harness = false
59 changes: 59 additions & 0 deletions testcrate/benches/float_add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![feature(f128)]

use compiler_builtins::float::add;
use criterion::{criterion_group, criterion_main, Criterion};
use testcrate::float_bench;

float_bench! {
name: add_f32,
sig: (a: f32, b: f32) -> f32,
crate_fn: add::__addsf3,
sys_fn: __addsf3,
sys_available: all(),
asm: [
#[cfg(target_arch = "x86_64")]
asm!(
"addss xmm0, xmm1",
"ret",
);

#[cfg(target_arch = "aarch64")]
asm!(
"fadd s0, s0, s1",
"ret",
);
],
}

float_bench! {
name: add_f64,
sig: (a: f64, b: f64) -> f64,
crate_fn: add::__adddf3,
sys_fn: __adddf3,
sys_available: all(),
asm: [
#[cfg(target_arch = "x86_64")]
asm!(
"addsd xmm0, xmm1",
"ret",
);

#[cfg(target_arch = "aarch64")]
asm!(
"fadd d0, d0, d1",
"ret",
);
],
}

float_bench! {
name: add_f128,
sig: (a: f128, b: f128) -> f128,
crate_fn: add::__addtf3,
sys_fn: __addtf3,
sys_available: not(feature = "no-sys-f128"),
asm: []
}

criterion_group!(float_add, add_f32, add_f64, add_f128);
criterion_main!(float_add);
131 changes: 131 additions & 0 deletions testcrate/benches/float_cmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#![feature(f128)]

use criterion::{criterion_group, criterion_main, Criterion};
use testcrate::float_bench;

use compiler_builtins::float::cmp;

float_bench! {
name: cmp_f32_gt,
sig: (a: f32, b: f32) -> i32,
crate_fn: cmp::__gtsf2,
sys_fn: __gtsf2,
sys_available: all(),
asm: [
#[cfg(target_arch = "x86_64")]
asm!(
"xor eax, eax",
"ucomiss xmm0, xmm1",
"seta al",
"ret",
);

#[cfg(target_arch = "aarch64")]
asm!(
"fcmp s0, s1",
"cset w0, gt",
"ret",
);
],
}

float_bench! {
name: cmp_f32_unord,
sig: (a: f32, b: f32) -> i32,
crate_fn: cmp::__unordsf2,
sys_fn: __unordsf2,
sys_available: all(),
asm: [
#[cfg(target_arch = "x86_64")]
asm!(
"cmpneqss xmm0, xmm1",
"movd eax, xmm0",
"and eax, 1",
"ret",
);

#[cfg(target_arch = "aarch64")]
asm!(
"fcmp s0, s1",
"cset w0, eq",
"ret",
);
],
}

float_bench! {
name: cmp_f64_gt,
sig: (a: f64, b: f64) -> i32,
crate_fn: cmp::__gtdf2,
sys_fn: __gtdf2,
sys_available: all(),
asm: [
#[cfg(target_arch = "x86_64")]
asm!(
"xor eax, eax",
"ucomisd xmm0, xmm1",
"seta al",
"ret",
);

#[cfg(target_arch = "aarch64")]
asm!(
"fcmp d0, d1",
"cset w0, gt",
"ret",
);
],
}

float_bench! {
name: cmp_f64_unord,
sig: (a: f64, b: f64) -> i32,
crate_fn: cmp::__unorddf2,
sys_fn: __unorddf2,
sys_available: all(),
asm: [
#[cfg(target_arch = "x86_64")]
asm!(
"cmpeqsd xmm0, xmm1",
"movq rax, xmm0",
"and eax, 1",
"ret",
);

#[cfg(target_arch = "aarch64")]
asm!(
"fcmp d0, d1",
"cset w0, eq",
"ret",
);
],
}

float_bench! {
name: cmp_f128_gt,
sig: (a: f128, b: f128) -> i32,
crate_fn: cmp::__gttf2,
sys_fn: __gttf2,
sys_available: not(feature = "no-sys-f128"),
asm: []
}

float_bench! {
name: cmp_f128_unord,
sig: (a: f128, b: f128) -> i32,
crate_fn: cmp::__unordtf2,
sys_fn: __unordtf2,
sys_available: not(feature = "no-sys-f128"),
asm: []
}

criterion_group!(
float_cmp,
cmp_f32_gt,
cmp_f32_unord,
cmp_f64_gt,
cmp_f64_unord,
cmp_f128_gt,
cmp_f128_unord
);
criterion_main!(float_cmp);
Loading

0 comments on commit 4d58cdb

Please sign in to comment.