bench-criterion base program

This commit is contained in:
Christoph J. Scherr 2024-01-10 16:19:03 +01:00
parent 59f89cef80
commit 0fd04c359c
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
4 changed files with 39 additions and 28 deletions

29
Cargo.lock generated
View File

@ -3,34 +3,9 @@
version = 3
[[package]]
name = "c-bindings"
name = "bench-criterion"
version = "0.1.0"
dependencies = [
"cc",
"cty",
]
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
]
[[package]]
name = "cty"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]]
name = "libc"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "rs-basic"
version = "0.1.0"
version = "0.2.0"

View File

@ -2,14 +2,16 @@
publish = false
members = [
".",
"members/bench-criterion",
]
default-members = [
".",
"members/bench-criterion",
]
[package]
name = "rs-basic"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Christoph J. Scherr <softwar@cscherr.de>"]
publish = false

View File

@ -0,0 +1,8 @@
[package]
name = "bench-criterion"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,26 @@
fn main() {
let n: f32 = std::env::args().collect::<Vec<String>>()[1]
.parse::<f32>()
.expect("bad input number");
println!("rsqrt:\t\t{}", inverse_sqrt(n));
println!("f_rsqrt:\t{}", fast_inverse_sqrt(n));
}
fn inverse_sqrt(n: f32) -> f32 {
1f32 / n.sqrt()
}
union MixedIntFloat {
f: f32,
i: u32,
}
/// see https://en.wikipedia.org/wiki/Fast_inverse_square_root
fn fast_inverse_sqrt(n: f32) -> f32 {
let mut conv: MixedIntFloat = MixedIntFloat { f: n };
unsafe {
conv.i = 0x5f3759df - (conv.i >> 1);
conv.f *= 1.5f32 - (n* 0.5f32 * conv.f * conv.f);
conv.f
}
}