summaryrefslogtreecommitdiff
path: root/userspace/task1.c
blob: 793b7f8331ec2cc6d6ca97a90d6b3b5ba0f5f6ed (plain)
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
typedef unsigned long long ULL;

// a simple cache to speedup successive requests!
static ULL fib1_cached_index=0;
static ULL fib1_cached_value=0;

static ULL fib2_cached_index=1;
static ULL fib2_cached_value=1;


ULL fib(ULL i)
{
    if(i==0)return 0;
    if(i==1)return 1;
    if(i==fib1_cached_index)return fib1_cached_value;
    if(i==fib2_cached_index)return fib2_cached_value;

    ULL value=fib(i-1)+fib(i-2);

    fib1_cached_index=fib2_cached_index;
    fib1_cached_value=fib2_cached_value;

    fib2_cached_index=i;
    fib2_cached_value=value;

    return value;
}


int main(unsigned int argc, char **argv)
{
    ULL i=0;

    while(1)
    {
	i++;

	fib(i); //precalc

	// this will exhaust the stack so we call fib every time above
	#ifdef __linux__
	    if((i%100000000)==1)printf("gcc-fibonacci(%llu) = %llu \n",i,fib(i));
	#endif
	    if((i%100000000)==1)printf("fibonacci(%u...) = %u... \n",(unsigned)i,(unsigned)fib(i));
    }

    return 0;
}