Changeset 90152a4 for tests/coroutine/fibonacci.c
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b7c89aa
- Parents:
- f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 moved
-
tests/coroutine/fibonacci.c (moved) (moved from src/tests/coroutine/coroutine.c ) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
tests/coroutine/fibonacci.c
rf9feab8 r90152a4 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // fibonacci.c -- 7 // fibonacci.c -- 3-state finite-state machine 8 8 // 9 9 // Author : Thierry Delisle 10 10 // Created On : Thu Jun 8 07:29:37 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S un Sep 17 21:38:15 201713 // Update Count : 712 // Last Modified On : Sat Aug 18 11:21:58 2018 13 // Update Count : 24 14 14 // 15 15 16 #include <fstream >17 #include <coroutine >16 #include <fstream.hfa> 17 #include <coroutine.hfa> 18 18 19 coroutine Fibonacci { 20 int fn; // used for communication 21 }; 19 coroutine Fibonacci { int fn; }; // used for communication 22 20 23 void ?{}( Fibonacci & this ) { 24 this.fn = 0; 25 } 26 27 void main( Fibonacci & this ) { 21 void main( Fibonacci & fib ) with( fib ) { // called on first resume 28 22 int fn1, fn2; // retained between resumes 29 this.fn = 0; // case 0 30 fn1 = this.fn; 23 fn = 0; fn1 = fn; // 1st case 31 24 suspend(); // restart last resume 32 33 this.fn = 1; // case 1 34 fn2 = fn1; fn1 = this.fn; 25 fn = 1; fn2 = fn1; fn1 = fn; // 2nd case 35 26 suspend(); // restart last resume 36 37 for ( ;; ) { // general case 38 this.fn = fn1 + fn2; 39 fn2 = fn1; fn1 = this.fn; 27 for () { 28 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case 40 29 suspend(); // restart last resume 41 30 } // for 42 31 } 43 32 44 int next( Fibonacci & this) {45 resume( this); // restart last suspend46 return this.fn;33 int next( Fibonacci & fib ) with( fib ) { 34 resume( fib ); // restart last suspend 35 return fn; 47 36 } 48 37 49 38 int main() { 50 39 Fibonacci f1, f2; 51 for ( int i = 1; i <= 10; i += 1 ) {40 for ( 10 ) { // print N Fibonacci values 52 41 sout | next( f1 ) | next( f2 ) | endl; 53 42 } // for
Note:
See TracChangeset
for help on using the changeset viewer.