#square-root #sqrt #root #square

fast_inv_sqrt

快速逆平方根算法实现

2个稳定版本

使用旧的Rust 2015

1.0.1 2016年7月29日
1.0.0 2016年6月4日

#1245算法

Download history 2/week @ 2023-11-27 22/week @ 2024-02-19 22/week @ 2024-02-26 17/week @ 2024-03-04

61 每月下载量
用于 shanimation-rs

WTFPL OR MIT OR Apache-2.0

14KB
445

fast_inv_sqrt

这完全是出于娱乐和测试crates.io发布而制作的,但实际上可能可用。

InvSqrt32 trait为原始数值类型提供inv_sqrt32()函数。InvSqrt64提供inv_sqrt64()。

如何使用它?

Cargo.toml

[dependencies]
fast_inv_sqrt = "~1.0"

代码

extern crate fast_inv_sqrt;

use fast_inv_sqrt::InvSqrt32;
use fast_inv_sqrt::InvSqrt64;

fn main() {
	let f: f32 = 1.234;
	println!("{}", f.inv_sqrt32());

	let i: i8 = 55;
	println!("{}", i.inv_sqrt64());
}

基准测试

基准测试需要夜间编译器。

"ref"基准测试使用f1 / f2.sqrt() "impl"基准测试使用f1 * f2.inv_sqrtXX()

$ cargo bench --features 'nightly'

test test32::bench_plain_impl ... bench:           6 ns/iter (+/- 0)
test test32::bench_plain_ref  ... bench:          13 ns/iter (+/- 0)

test test32::bench_real_impl  ... bench:           6 ns/iter (+/- 0)
test test32::bench_real_ref   ... bench:          13 ns/iter (+/- 0)

test test64::bench_plain_impl ... bench:           6 ns/iter (+/- 0)
test test64::bench_plain_ref  ... bench:          20 ns/iter (+/- 0)

test test64::bench_real_impl  ... bench:           6 ns/iter (+/- 0)
test test64::bench_real_ref   ... bench:          20 ns/iter (+/- 0)

功能'omit-checking'禁用了对传入值是否为_sign_positive()和is_normal()的检查,并会对非规范化值和负值产生无效结果,但这是快速的

test test32::bench_plain_impl ... bench:           2 ns/iter (+/- 0)
test test32::bench_plain_ref  ... bench:          13 ns/iter (+/- 0)

test test32::bench_real_impl  ... bench:           3 ns/iter (+/- 0)
test test32::bench_real_ref   ... bench:          13 ns/iter (+/- 0)

test test64::bench_plain_impl ... bench:           2 ns/iter (+/- 0)
test test64::bench_plain_ref  ... bench:          20 ns/iter (+/- 0)

test test64::bench_real_impl  ... bench:           3 ns/iter (+/- 0)
test test64::bench_real_ref   ... bench:          20 ns/iter (+/- 0)

没有运行时依赖