Skip to content

Instantly share code, notes, and snippets.

@evanricard
Last active February 11, 2020 22:51
Show Gist options
  • Save evanricard/a53ab52493b6fc4d17747180207c75cd to your computer and use it in GitHub Desktop.
Save evanricard/a53ab52493b6fc4d17747180207c75cd to your computer and use it in GitHub Desktop.
Loop unrolling
static void DuffsDevice(uint count, int[] A, int[] B)
{
uint i = 0;
uint n = (count + 7) / 8;
switch (count % 8) {
case 0: A[i] = B[i++]; goto case 7;
case 7: A[i] = B[i++]; goto case 6;
case 6: A[i] = B[i++]; goto case 5;
case 5: A[i] = B[i++]; goto case 4;
case 4: A[i] = B[i++]; goto case 3;
case 3: A[i] = B[i++]; goto case 2;
case 2: A[i] = B[i++]; goto case 1;
case 1: A[i] = B[i++]; break;
}
while (--n > 0) {
A[i] = B[i];
A[i + 1] = B[i + 1];
A[i + 2] = B[i + 2];
A[i + 3] = B[i + 3];
A[i + 4] = B[i + 4];
A[i + 5] = B[i + 5];
A[i + 6] = B[i + 6];
A[i + 7] = B[i + 7];
i += 8;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment