1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| /* explain https://www.youtube.com/watch?v=p8u_k2LIZyo */
|
| #include <stdio.h>
|
| float Q_rsqrt(float number)
| {
| long i;
| float x2, y;
| const float threehalfs = 1.5F;
|
| x2 = number*0.5F;
| y = number;
| i = *(long *) &y;
| i = 0x5f3759df - (i >> 1);
| printf("i = %lu\n", i);
| y = *( float *) &i;
| printf("y = %f\n", y);
| y = y * (threehalfs - (x2*y*y));
|
| return y;
| }
|
| int main()
| {
| float n = 5.0;
| float f = Q_rsqrt(n);
| printf("1/sqrt(%f) = %f", n, f);
| return 0;
| }
|
|