Skip to content

Instantly share code, notes, and snippets.

@dnbaker
Last active February 20, 2018 03:32
Show Gist options
  • Save dnbaker/2ec828bce545d4085b022b1fbc94d765 to your computer and use it in GitHub Desktop.
Save dnbaker/2ec828bce545d4085b022b1fbc94d765 to your computer and use it in GitHub Desktop.
Easy Duff Unrolling
// Set ITER to be the code to execute at each iteration.
// Only use this when you can't guarantee your number of elements is a multiple of 8 (or some higher power of 2)
// If you want to save some time, you can initialize your accumulator with the first entry of the set and decrement len.
#define DO_DUFF(len, ITER) \
do { \
if(len) {\
std::uint64_t loop = (len + 7) >> 3;\
switch(len & 7) {\
case 0: do {\
ITER; [[fallthrough]];\
case 7: ITER; [[fallthrough]]; case 6: ITER; [[fallthrough]]; case 5: ITER; [[fallthrough]];\
case 4: ITER; [[fallthrough]]; case 3: ITER; [[fallthrough]]; case 2: ITER; [[fallthrough]]; case 1: ITER;\
} while (--loop);\
}\
}\
} while(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment