Created
November 8, 2018 09:30
-
-
Save eush77/011c542b3899b4db37858a6bab91b0b9 to your computer and use it in GitHub Desktop.
Repetition macros for C preprocessor. Inspired by https://stackoverflow.com/a/53110569
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 "rep.cpp" | |
#include <stdio.h> | |
int main(void) { | |
unsigned num = 0; | |
// Repeat the block 1011_2 = 11 times. | |
REP4(1, 0, 1, 1, { | |
printf("%u\n", num++); | |
}) | |
} |
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
#define IF0(m) | |
#define IF1(m) m | |
#define DUP1(m) m m | |
#define DUP2(m) DUP1(DUP1(m)) | |
#define DUP3(m) DUP1(DUP2(m)) | |
#define REP1(b0, m) { IF##b0(m) } | |
#define REP2(b1, b0, m) { IF##b1(DUP1(m)) REP1(b0, m) } | |
#define REP3(b2, b1, b0, m) { IF##b2(DUP2(m)) REP2(b1, b0, m) } | |
#define REP4(b3, b2, b1, b0, m) { IF##b3(DUP3(m)) REP3(b2, b1, b0, m) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment