Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Nov 3, 2023
1 parent 4b4ecc3 commit 81cd508
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ bool vuapi use_std_io_console_window();
bool vuapi is_flag_on(ulongptr flags, ulongptr flag);
intptr vuapi gcd(ulongptr count, ...); // UCLN
intptr vuapi lcm(ulongptr count, ...); // BCNN
float vuapi fast_sqrtf(const float number); // Estimates the square root of a 32-bit floating-point number (from Quake III Arena)
void vuapi hex_dump(const void* data, int size);

#include "template/math.tpl"
Expand Down
23 changes: 23 additions & 0 deletions src/details/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,27 @@ intptr vuapi lcm(ulongptr count, ...) // Least Common Multiples -> UCLN = | a1 *
return result;
}

// https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code
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;
}

float vuapi fast_sqrtf(const float number)
{
return 1.F / q_rsqrt(number);
}

} // namespace vu

0 comments on commit 81cd508

Please sign in to comment.