ADT
arm-eh
ast-experimental
cleanup-dtors
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
pthread-emulation
qualifiedEnum
Last change
on this file since 9131e54 was 2e041e27, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago |
add examples directory for testing code in the paper
|
-
Property mode
set to
100644
|
File size:
1.6 KB
|
Line | |
---|
1 | #include <thread.hfa>
|
---|
2 | #include <fstream.hfa>
|
---|
3 |
|
---|
4 | int fn1, fn2, state = 1;
|
---|
5 | int fib_gvar() {
|
---|
6 | int fn;
|
---|
7 | choose ( state ) {
|
---|
8 | case 1: fn = 0; fn1 = fn; state = 2;
|
---|
9 | case 2: fn = 1; fn2 = fn1; fn1 = fn; state = 3;
|
---|
10 | case 3: fn = fn1 + fn2; fn2 = fn1; fn1 = fn;
|
---|
11 | }
|
---|
12 | return fn;
|
---|
13 | }
|
---|
14 |
|
---|
15 | #define FIB_INIT { 0, 1 }
|
---|
16 | typedef struct { int fn1, fn2; } Fib;
|
---|
17 | int fib_state( Fib & f ) with( f ) {
|
---|
18 | int ret = fn2;
|
---|
19 | int fn = fn1 + fn2;
|
---|
20 | fn2 = fn1; fn1 = fn;
|
---|
21 | return ret;
|
---|
22 | }
|
---|
23 |
|
---|
24 | coroutine Fib1 { int fn; }; // used for communication
|
---|
25 | void main( Fib1 & fib ) with( fib ) { // called on first resume
|
---|
26 | fn = 0; int fn1 = fn; suspend();
|
---|
27 | fn = 1; int fn2 = fn1; fn1 = fn; suspend();
|
---|
28 | for () {
|
---|
29 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; suspend();
|
---|
30 | }
|
---|
31 | }
|
---|
32 | int next( Fib1 & fib ) with( fib ) { resume( fib ); return fn; }
|
---|
33 |
|
---|
34 | coroutine Fib2 { int ret; }; // used for communication
|
---|
35 | void main( Fib2 & fib ) with( fib ) { // called on first resume
|
---|
36 | int fn, fn1 = 1, fn2 = 0; // precompute first two states
|
---|
37 | for () {
|
---|
38 | ret = fn2;
|
---|
39 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case
|
---|
40 | suspend(); // restart last resume
|
---|
41 | }
|
---|
42 | }
|
---|
43 | int next( Fib2 & fib ) with( fib ) {
|
---|
44 | resume( fib ); // restart last suspend
|
---|
45 | return ret;
|
---|
46 | }
|
---|
47 |
|
---|
48 | int main() {
|
---|
49 | for ( 10 )
|
---|
50 | sout | fib_gvar();
|
---|
51 | sout | nl;
|
---|
52 |
|
---|
53 | Fib f1 = FIB_INIT, f2 = FIB_INIT;
|
---|
54 | for ( 10 )
|
---|
55 | sout | fib_state( f1 ) | fib_state( f2 );
|
---|
56 | sout | nl;
|
---|
57 |
|
---|
58 | Fib1 f1, f2;
|
---|
59 | for ( 10 )
|
---|
60 | sout | next( f1 ) | next( f2 );
|
---|
61 | sout | nl;
|
---|
62 |
|
---|
63 | Fib2 f1, f2;
|
---|
64 | for ( 10 )
|
---|
65 | sout | next( (Fib2 &)f1 ) | next( (Fib2 &)f2 );
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Local Variables: //
|
---|
69 | // tab-width: 4 //
|
---|
70 | // fill-column: 120 //
|
---|
71 | // compile-command: "cfa Fib.cfa" //
|
---|
72 | // End: //
|
---|
Note:
See
TracBrowser
for help on using the repository browser.