Skip to content

Instantly share code, notes, and snippets.

@aprell
Created November 11, 2011 17:46
Show Gist options
  • Save aprell/1358668 to your computer and use it in GitHub Desktop.
Save aprell/1358668 to your computer and use it in GitHub Desktop.
Jumping like Duff
#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