diff --git a/Cargo.lock b/Cargo.lock index 1196d1f..acb6567 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 0108720..b9b9cb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] publish = false diff --git a/members/bench-criterion/Cargo.toml b/members/bench-criterion/Cargo.toml new file mode 100644 index 0000000..568036e --- /dev/null +++ b/members/bench-criterion/Cargo.toml @@ -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] diff --git a/members/bench-criterion/src/main.rs b/members/bench-criterion/src/main.rs new file mode 100644 index 0000000..f1fe01c --- /dev/null +++ b/members/bench-criterion/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + let n: f32 = std::env::args().collect::>()[1] + .parse::() + .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 + } +}