Skip to content

Instantly share code, notes, and snippets.

@alifarazz
Last active September 26, 2019 07:39
Show Gist options
  • Select an option

  • Save alifarazz/21e0000e79d5fa8a94f5924a1edeafdc to your computer and use it in GitHub Desktop.

Select an option

Save alifarazz/21e0000e79d5fa8a94f5924a1edeafdc to your computer and use it in GitHub Desktop.
A simple implementation of Duff's device
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int n, i;
if (argc != 2)
return 1;
n = atoi(argv[1]);
i = (n - 1) / 8 + 1;
off = n % 8;
if (n)
switch (off) {
case 0: do {
printf("%d\n", n--);
case 7:
printf("%d\n", n--);
case 6:
printf("%d\n", n--);
case 5:
printf("%d\n", n--);
case 4:
printf("%d\n", n--);
case 3:
printf("%d\n", n--);
case 2:
printf("%d\n", n--);
case 1:
printf("%d\n", n--);
} while (--i);
}
printf("final value: %d\n", n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment