Crates.io | q_rsqrt |
lib.rs | q_rsqrt |
version | 0.1.1 |
source | src |
created_at | 2022-06-27 05:16:42.50546 |
updated_at | 2022-06-27 05:20:52.923209 |
description | An implementation of the fast inverse square root function from quake 3 |
homepage | |
repository | https://github.com/ThatNerdUKnow/Q_rsqrt |
max_upload_size | |
id | 613926 |
size | 3,433 |
This is a implementation of the fast inverse square root function from quake 3.
It can be up to three times as fast as using the .sqrt()
method on a float32
Keep in mind that fast inverse square root is only accurate within a one percent margin of error
Here's the original implementation:
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}