source:
doc/papers/concurrency/examples/Fib2.c
@
a659b31
Last change on this file since a659b31 was a573c22, checked in by , 5 years ago | |
---|---|
|
|
File size: 619 bytes |
Rev | Line | |
---|---|---|
[466fa01] | 1 | #include <stdio.h> |
2 | ||
3 | #define FIB_INIT { 0 } | |
[a573c22] | 4 | typedef struct { int restart; int fn1, fn2; } Fib; |
[466fa01] | 5 | int fib( Fib * f ) { |
[a573c22] | 6 | static void * states[] = { &&s0, &&s1, &&s2 }; |
7 | goto *states[f->restart]; | |
8 | s0: | |
[466fa01] | 9 | f->fn1 = 0; |
[a573c22] | 10 | f->restart = 1; |
[466fa01] | 11 | return f->fn1; |
[a573c22] | 12 | s1: |
[466fa01] | 13 | f->fn2 = f->fn1; |
14 | f->fn1 = 1; | |
[a573c22] | 15 | f->restart = 2; |
[466fa01] | 16 | return f->fn1; |
[a573c22] | 17 | s2:; |
[466fa01] | 18 | int fn = f->fn1 + f->fn2; |
19 | f->fn2 = f->fn1; | |
20 | f->fn1 = fn; | |
21 | return fn; | |
22 | } | |
23 | int 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.