In the C programming language, Duff's device is a technique of manually implementing loop unrolling by interleaving `do-while loop` and a `switch` statement.
Questions tagged [duffs-device]
22 questions
1
vote
2 answers
Switch and do-while in c++
#include
int main() {
int n = 3, i=0;
switch (n % 2)
{
case 0:
do {
++i;
case 1:
++i;
} while (--n > 0);
}
std::cout << i;
}
Why is the output 5 and not 6 ? Can…

Florin Drutu
- 21
- 1
1
vote
0 answers
How do if/else work intermixed with switch/case interact?
In a question about switch and goto, this answer used this code to avoid using a goto altogether:
switch(color)
case YELLOW:
if(AlsoHasCriteriaX)
case GREEN:
case RED:
case BLUE:
Paint();
else
default:
Print("Ugly color, no…

Johnathan Gross
- 678
- 6
- 19
1
vote
1 answer
switch statement perplexing
I know this has been hashed out before, I've read every question and answer on switch that I can find. My question isn't about syntax or function of Duff's Device but about switch in general this just happens to illustrate the question well.
{
…

Jack Galt
- 83
- 8
1
vote
2 answers
duff device not working
I've tried to implement the Duff device, but it's not working. It doesn't copy anything. I've implemented the original version and a more clear version:
void duff_function(int *a, int *b, int length)
{
int n = (length + 7) / 8;
switch(length…

AR89
- 3,548
- 7
- 31
- 46
0
votes
2 answers
Nested case statements
Can some one please explain the nesting of case statements into another. I'm referring to the Duffs Device where all other case statements are inside the do-while loop associated with case 0. I cant get my head around it. It seems to me that it…

Bazooka
- 1,428
- 4
- 15
- 24
-1
votes
3 answers
A pragmatic loop unrolling technique
I'm finding a pragmatic loop unrolling technique example.
I think Duff's device is a one of nice tip.
But Duff's device's destination is never increased. It could be useful for embeded programmer who copies data to serial device, not general…

Benjamin
- 10,085
- 19
- 80
- 130
-3
votes
3 answers
How Do while inside switch statement works in c
I have a code snippet where a do while statement is inside switch condition of case0, by default the case value is case1 but it seems executing case0. The output of the program print is 6. How is this possible, can someone explain the flow of code…

Sandeep M Nath
- 9
- 3