Skip to content

Instantly share code, notes, and snippets.

@htfy96
Last active March 6, 2016 14:18
Show Gist options
  • Save htfy96/882f2f291b77bdeefd58 to your computer and use it in GitHub Desktop.
Save htfy96/882f2f291b77bdeefd58 to your computer and use it in GitHub Desktop.
test_inv_sqrt.cpp
// Compile it with -std=c++11 and -O3
#include <cmath>
#include <iostream>
#include <chrono>
#include <vector>
#include <cstdlib>
#include <cstdint>
float Q_rsqrt( float number )
{
static_assert(sizeof(float)==sizeof(int32_t), "float isn't 32bit on your machine");
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( int32_t * ) &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;
}
using namespace std;
int main()
{
auto start_time = chrono::high_resolution_clock::now();
vector<float> v;
v.reserve(100000000);
for (int i=0; i<100000000; ++i)
{
v.push_back(Q_rsqrt(0.1 * i));
} //870ms
cout << chrono::duration_cast< chrono::milliseconds >(chrono::high_resolution_clock::now() - start_time).count() << "ms"<<endl;
v.clear();
v.reserve(100000000);
start_time = chrono::high_resolution_clock::now();
for (int i=0; i<100000000; ++i)
{
v.push_back(1/sqrt(0.1 * i)); //235ms
}
cout << chrono::duration_cast< chrono::milliseconds >(chrono::high_resolution_clock::now() - start_time).count() << "ms"<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment