34 lines
881 B
C
34 lines
881 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
extern float inverse_sqrt(float);
|
|
extern float inverse_sqrt(float);
|
|
extern int square_of_greater(int, int);
|
|
|
|
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;
|
|
}
|
|
|
|
int main() {
|
|
printf("hello, world!\n");
|
|
float n = 4.0;
|
|
float i = inverse_sqrt(n);
|
|
printf("%f = inverse_sqrt(%f)\n", i, n);
|
|
printf("%f = Q_rsqrt(%f)\n", Q_rsqrt(4.0), 4.0);
|
|
printf("%d = square_of_greater(4,5)\n", square_of_greater(4, 5));
|
|
return 0;
|
|
}
|