Last active
September 26, 2019 07:39
-
-
Save alifarazz/21e0000e79d5fa8a94f5924a1edeafdc to your computer and use it in GitHub Desktop.
A simple implementation of Duff's device
This file contains hidden or 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> | |
| 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