-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.c
71 lines (56 loc) · 1.64 KB
/
tmp.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
float FastInvSqrt (float number);
int
main ()
{
float x = 10.0;
int N = 100000001;
int* arr = malloc(sizeof(int) * N);
int i = 0;
for (i = 0; i < N; i++) {
arr[i] = rand();
}
clock_t start2 = clock ();
volatile float z = 0;
do
{
z += 1.0 / sqrt (arr[i]+z);
i++;
}
while (i < N);
clock_t end2 = clock ();
printf("%f\n", z);
double time2 = (end2 - start2) / (double) CLOCKS_PER_SEC;
printf ("1/sqrt() spends %13f sec.\n\n", time2);
x = 10.0;
i = 0;
clock_t start1 = clock ();
do
{
z += FastInvSqrt (arr[i]+z);
i++;
}
while (i < N);
clock_t end1 = clock ();
printf("%f\n", z);
double time1 = (end1 - start1) / (double) CLOCKS_PER_SEC;
printf ("FastInvSqrt() spends %f sec.\n\n", time1);
printf ("fast inverse square root is faster %f times than 1/sqrt().\n", time2/time1);
return 0;
}
float
FastInvSqrt (float x)
{
float xhalf = 0.5F * x;
int i = *(int *) &x; // store floating-point bits in integer
i = 0x5f3759df - (i >> 1); // initial guess for Newton's method
x = *(float *) &i; // convert new bits into float
x = x * (1.5 - xhalf * x * x); // One round of Newton's method
//x = x * (1.5 - xhalf * x * x); // One round of Newton's method
//x = x * (1.5 - xhalf * x * x); // One round of Newton's method
//x = x * (1.5 - xhalf * x * x); // One round of Newton's method
return x;
}