Created
July 29, 2013 10:53
-
-
Save spiridenok/6103570 to your computer and use it in GitHub Desktop.
Fast SQRT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static double fast_sqrt( const double number ) | |
{ | |
const double f = 1.5; | |
const double x = number * 0.5; | |
double y = number; | |
union | |
{ | |
double *double_repr; | |
long long *long_long_repr; | |
} sqrt_union; | |
sqrt_union.double_repr = &y; | |
long long i = *(sqrt_union.long_long_repr); | |
// for floats use: | |
// i = 0x5f3759df - (i >> 1); | |
i = 0x5fe6eb50c7b537a9LL - (i >> 1); | |
sqrt_union.long_long_repr = &i; | |
y = *(sqrt_union.double_repr); | |
y = y * (f - (x * y * y)); | |
y = y * (f - (x * y * y)); | |
return (number * y); | |
//0x5fe6eb50c7b537a9 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment