switch statement perplexing

46 views Asked by At

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.

    {
    int n = (count + 7) / 8;

    switch(count % 8) {
        case 0: do { *to++ = *from++;
        case 7: *to++ = *from++;
        case 6: *to++ = *from++;
        case 5: *to++ = *from++;
        case 4: *to++ = *from++;
        case 3: *to++ = *from++;
        case 2: *to++ = *from++;
        case 1: *to++ = *from++;
        } while(--n > 0);
    }

I understand the do while, the value of n is being decremented with each iteration. I also understand that loose compiler rules allow other cases to jump inside the do loop (weird but I understand it)

But since the switch statement is a function of (count % 8) and nothing is changing or acting on the value of count why is count changing in the first place to produce a different modulo remainder inside the switch?

Assume that (count % 8) produces 7 on the first pass. After processing case 7: the value of count remains the same, therefore the value of (count % 8) should remain the same so case 6: should not be true, nor should any other case and without a default n should decrement, and the next iteration of the do loop should begin with the value of count unchanged. So it seems that the loop would wind down to 0, count would never change, so it would only execute case 7 with every pass making all the other code pointless.

But if that were true then Duff's device would not work because it clearly relies on int(n + 7)/8 to produce 8 repetitions of a value of n per value of count and paired with (count % 8) implies that count is indeed decrementing to produce a loop of decreasing modulo remainders between 7 and 0.

From my newbie perspective it would seem for this to work one would need a --count in that loop somewhere. So my conclusion is that I don't understand how switch works. I'd appreciate any explanations.

1

There are 1 answers

0
Jack Galt On

After doing a little testing with various versions I can safely say that count is not changing value, only n is changing value. Of course that means the original code would work exactly the same without the switch by simply executing the assignment 8 times consecutively inside the do:while which makes me wonder what the point of the switch is to begin with in this case, other than pure obfuscation.

As far as how the switch works, I've discovered through the testing that it works essentially as I expected except that it executes EVERY case regardless of value unless you include a break in each case. Further evidence that count wasn't changing, ergo the lack of breaks in Duff's code coupled with the unchanging value of count further support the assertion that the switch was completely pointless.

This would produce the exact same result;

{
//integer division by 8 produces 8 iterations of the same value
int n = (count + 7) / 8; 

do {
    *to++ = *from++; //therefore since the loop will be 1/8th
    *to++ = *from++;
    *to++ = *from++; //the value of count you need to perform 
    *to++ = *from++;
    *to++ = *from++; //8 assignments per iteration of n
    *to++ = *from++;
    *to++ = *from++; //this produces the same result without
    *to++ = *from++;
    *to++ = *from++; //the switch
} while(--n > 0);

}