Skip to content

Commit

Permalink
Fast inverse square root
Browse files Browse the repository at this point in the history
  • Loading branch information
BSVino committed Feb 21, 2016
1 parent d2b7f5c commit 4145578
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ using std::vector;

int main(int argc, char* argv[])
{
for (int k = -6; k <= 14; k++)
{
float number = pow(2, k);
printf("invsqrt(%f) = %f\n", number, 1/sqrt(number));

int32_t* l = (int32_t*)&number;
int32_t exponent = (*l)>>23;
printf("my estimate: 2^%d*s -> 2^%d*s\n", exponent-127, 62 - exponent/2);

int32_t int_estimate = 0x5F000000 - ((*l)>>1);
float my_estimate = *(float*)&int_estimate;

int_estimate = 0x5f3759df - ((*l)>>1);
float orig_estimate = *(float*)&int_estimate;

printf("my: %f orig: %f\n", my_estimate, orig_estimate);

my_estimate = 1.5f*my_estimate - 0.5f*my_estimate*my_estimate*my_estimate*number;
orig_estimate = 1.5f*orig_estimate - 0.5f*orig_estimate*orig_estimate*orig_estimate*number;
printf("my: %f orig: %f\n\n", my_estimate, orig_estimate);
}




return 0;
mtsrand(0);

// Create a game
Expand Down

0 comments on commit 4145578

Please sign in to comment.