source: doc/papers/llheap/examples/Fib2.c@ a595ab7

Last change on this file since a595ab7 was 7bef8cf, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

start paper on llheap

  • Property mode set to 100644
File size: 619 bytes
Line 
1#include <stdio.h>
2
3#define FIB_INIT { 0 }
4typedef struct { int restart; int fn1, fn2; } Fib;
5int fib( Fib * f ) {
6 static void * states[] = { &&s0, &&s1, &&s2 };
7 goto *states[f->restart];
8 s0:
9 f->fn1 = 0;
10 f->restart = 1;
11 return f->fn1;
12 s1:
13 f->fn2 = f->fn1;
14 f->fn1 = 1;
15 f->restart = 2;
16 return f->fn1;
17 s2:;
18 int fn = f->fn1 + f->fn2;
19 f->fn2 = f->fn1;
20 f->fn1 = fn;
21 return fn;
22}
23int main() {
24 Fib f1 = FIB_INIT, f2 = FIB_INIT;
25 for ( int i = 0; i < 10; i += 1 ) {
26 printf( "%d %d\n", fib( &f1 ), fib( &f2 ) );
27 }
28}
29
30// Local Variables: //
31// tab-width: 4 //
32// compile-command: "gcc Fib2.c" //
33// End: //
Note: See TracBrowser for help on using the repository browser.