Created
November 11, 2011 17:46
-
-
Save aprell/1358668 to your computer and use it in GitHub Desktop.
Jumping like Duff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// Found in the slow clone of Cilk procedures | |
// An application of Duff's device to jump directly into the middle of code | |
// See Cilk 5.4.6 Reference Manual | |
// | |
// The same technique can be used to implement stack-based coroutines | |
// See Simon Tatham's Coroutines in C | |
void func(int i) | |
{ | |
switch (i) { | |
case 0: goto _0; | |
case 1: goto _1; | |
case 2: goto _2; | |
} | |
if (0) { _0: printf("Hi 0!\n"); } | |
if (0) { _1: printf("Hi 1!\n"); } | |
if (0) { _2: printf("Hi 2!\n"); } | |
} | |
int main(void) | |
{ | |
int i; | |
for (i = 0; i < 3; i++) | |
func(i); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment