Is this valid C code?

Possible duplicate:
What does the code do?

void duff(register char *to, register char *from, register int count)
{
    register 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);
    }
}

Is the above C code? If so, what is he trying to achieve and why would someone do something like this above?

+3
source share
3 answers

It's called a Duff device, and you can read about it on wikipedia .

: . , , Duff, , .

, , , :

void memcpy(char* dst, char* src, size_t count)
{
   begin:
     if (count-- == 0) return;
     *(dst++) = *(src++);
     goto begin;
}

15 , :

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

, " " "".

duff, , :

, , , , , , , , , , , , , , , , , , ,

+9

. .

, , , , , , ceil (n/8).

register - , , , , .

, (memcpy(), , , ), , , , .

+5

Mandatory link to Duff Device on Wikipedia .

+2
source

Source: https://habr.com/ru/post/1757602/


All Articles