source: doc/papers/concurrency/examples/Fib1.c@ 314c9d8

Last change on this file since 314c9d8 was 466fa01, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add diagrams and example programs

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