ADT
ast-experimental
enum
forall-pointer-decay
pthread-emulation
qualifiedEnum
|
Last change
on this file since 49b3389 was 18783b4, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago |
|
attempt to remove tail-recursion optimization
|
-
Property mode
set to
100644
|
|
File size:
965 bytes
|
| Line | |
|---|
| 1 | // Resume Across Fixup
|
|---|
| 2 | #include <clock.hfa>
|
|---|
| 3 | #include <fstream.hfa>
|
|---|
| 4 | #include <stdlib.hfa> // strto
|
|---|
| 5 |
|
|---|
| 6 | void nounwind_fixup(unsigned int frames, void (*raised_rtn)(int &)) {
|
|---|
| 7 | if (frames) {
|
|---|
| 8 | nounwind_fixup(frames - 1, raised_rtn);
|
|---|
| 9 | if ( frames == -1 ) printf( "42" ); // prevent recursion optimizations
|
|---|
| 10 | } else {
|
|---|
| 11 | int fixup = 17;
|
|---|
| 12 | raised_rtn(fixup);
|
|---|
| 13 | }
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | int main(int argc, char * argv[]) {
|
|---|
| 17 | unsigned int times = 1;
|
|---|
| 18 | unsigned int total_frames = 1;
|
|---|
| 19 | if (1 < argc) {
|
|---|
| 20 | times = strto(argv[1], 0p, 10);
|
|---|
| 21 | }
|
|---|
| 22 | if (2 < argc) {
|
|---|
| 23 | total_frames = strto(argv[2], 0p, 10);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | void raised(int & fixup) {
|
|---|
| 27 | fixup = total_frames + 42; // use local scope => lexical link
|
|---|
| 28 | if ( total_frames == 42 ) printf( "42" );
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | Time start_time = timeHiRes();
|
|---|
| 32 | for (unsigned int count = 0 ; count < times ; ++count) {
|
|---|
| 33 | nounwind_fixup(total_frames, raised);
|
|---|
| 34 | }
|
|---|
| 35 | Time end_time = timeHiRes();
|
|---|
| 36 | sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
|
|---|
| 37 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.