2021-08-27 12:01:30 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Calculate the inverse inverse root.
|
|
|
|
* @details
|
|
|
|
* Two implementation to calculate inverse inverse root,
|
|
|
|
* from Quake III Arena (C++ version) and with standard lib (cmath)
|
|
|
|
*/
|
|
|
|
|
2021-08-27 12:13:26 +02:00
|
|
|
#include <cmath> /// for `std::sqrt`
|
2021-08-27 18:46:01 +02:00
|
|
|
#include <iostream> /// for IO operations
|
2021-08-27 12:13:26 +02:00
|
|
|
#include <type_traits> /// for `static_assert`
|
2021-08-27 12:01:30 +02:00
|
|
|
|
|
|
|
/**
|
2021-08-27 18:46:35 +02:00
|
|
|
* @brief This is the function that calculates the fast inverse square root.
|
2021-08-27 21:41:35 +02:00
|
|
|
* The following code is the fast inverse square root implementation from
|
|
|
|
* Quake III Arena (Adapted for C++) More information can be found at
|
|
|
|
* [Wikipedia](https://en.wikipedia.org/wiki/Fast_inverse_square_root)
|
2021-08-27 12:01:30 +02:00
|
|
|
* @tparam T floating type
|
2021-08-27 21:41:35 +02:00
|
|
|
* @tparam iterations inverse square root, the greater the number of
|
|
|
|
* iterations, the more exact the result will be (1 or 2).
|
2021-08-27 12:01:30 +02:00
|
|
|
* @param x value to calculate
|
|
|
|
* @return T return inverse square root
|
|
|
|
*/
|
|
|
|
template <typename T, char iterations = 2>
|
|
|
|
inline T Fast_InvSqrt(T x) {
|
2021-08-27 21:45:09 +02:00
|
|
|
using Tint = typename std::conditional<sizeof(T) == 8, std::int64_t,
|
|
|
|
std::int32_t>::type;
|
2021-08-27 12:01:30 +02:00
|
|
|
T y = x;
|
|
|
|
T x2 = y * 0.5;
|
|
|
|
Tint i = *(Tint *)&y;
|
2021-08-27 21:41:35 +02:00
|
|
|
i = (sizeof(T) == 8 ? 0x5fe6eb50c7b537a9 : 0x5f3759df) - (i >> 1);
|
2021-08-27 12:01:30 +02:00
|
|
|
y = *(T *)&i;
|
2021-08-27 21:51:23 +02:00
|
|
|
|
|
|
|
y = *reinterpret_cast<T *>(&i);
|
|
|
|
|
2021-08-27 12:01:30 +02:00
|
|
|
y = y * (1.5 - (x2 * y * y));
|
2021-08-27 21:45:09 +02:00
|
|
|
if (iterations == 2) {
|
2021-08-27 12:01:30 +02:00
|
|
|
y = y * (1.5 - (x2 * y * y));
|
2021-08-27 21:45:09 +02:00
|
|
|
}
|
2021-08-27 12:01:30 +02:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-27 18:47:11 +02:00
|
|
|
* @brief This is the function that calculates the fast inverse square root.
|
2021-08-27 12:01:30 +02:00
|
|
|
* The following code is the fast inverse square root with standard lib (cmath)
|
|
|
|
* More info:
|
|
|
|
* https://www.linkedin.com/pulse/fast-inverse-square-root-still-armin-kassemi-langroodi
|
|
|
|
* @tparam T floating type
|
|
|
|
* @param x value to calculate
|
|
|
|
* @return T return inverse square root
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T Standard_InvSqrt(T number) {
|
|
|
|
T squareRoot = sqrt(number);
|
|
|
|
return 1.0f / squareRoot;
|
|
|
|
}
|
|
|
|
|
2021-08-27 18:47:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
2021-08-27 12:01:30 +02:00
|
|
|
int main() {
|
|
|
|
std::cout << "The Fast inverse square root of 36 is: "
|
|
|
|
<< Fast_InvSqrt<float, 1>(36.0f) << std::endl;
|
|
|
|
std::cout << "The Fast inverse square root of 36 is: "
|
|
|
|
<< Fast_InvSqrt<double, 2>(36.0f) << " (2 iterations)"
|
|
|
|
<< std::endl;
|
|
|
|
std::cout << "The Standard inverse square root of 36 is: "
|
|
|
|
<< Standard_InvSqrt<float>(36.0f) << std::endl;
|
|
|
|
}
|