duff device not working

120 views Asked by At

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 % 8)
    {
        case 0: *a++ = *b++;    // I dereference, copy, and then increment the pointer, * > ++
        case 7: *a++ = *b++;
        case 6: *a++ = *b++;
        case 5: *a++ = *b++;
        case 4: *a++ = *b++;
        case 3: *a++ = *b++;
        case 2: *a++ = *b++;
        case 1: *a++ = *b++;
    }
    while ( --n > 0)
    {
        *a++ = *b++;
        *a++ = *b++;
        *a++ = *b++;
        *a++ = *b++;
        *a++ = *b++;
        *a++ = *b++;
        *a++ = *b++;
        *a++ = *b++;
    }

}

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

int main()
{
    int a[10] = {21, 34, 12, 64, 13, 9, 56, 54, 90, 1};
    int b[10] = {22};
    for (int i = 0; i < 10; ++i)
    {
       printf("%d, ", a[i]);
    }
    printf("\n");
    duff_function(a, b, 10);
    //send(a, b, 10);

    for(int i = 0; i < 10; ++i)
        printf("%d, ", b[i]);
    printf("\n");
    return 0;
}

The output is:

21, 34, 12, 64, 13, 9, 56, 54, 90, 1, 
22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

It doesn't copy anything. In the original version for some reason it doesn't increment the pointer to, but I think that I should increment it (I do it in my function).

EDIT As reported there is an error in my code: I've changed it:

send(b, a, 10);

But now the output is:

1, 0, 0, 0, 0, 0, 0, 0, 0, 0,

EDIT 2 Last edit to make the code work:

duff_function(b, a);
2

There are 2 answers

3
mch On BEST ANSWER

you are copying from b to a, but you want to copy from a to b.

change every *a++ = *b++; to *b++ = *a++;

you can also do just memcpy(b, a, length * sizeof *a);

0
user3629249 On

this function:

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

contains several problems.

1) the while statement 
   places the majority of the switch case statements 
   at a different scope level from the first case statement.

2) stepping through the code
   int n = (10+7)%8 = 2
   switch(2)
      (jumping into the center of a do/while loop is 
      a very poor program practice)
      do{
      case 2: *to = *from++;
      case 1: *to = *from++; // which overlays the prior line
      } while( --n >0 ) // on first pass decrement 'n' to 1
      case 7:   overlays prior *to with from[2]
      case 6:   overlays prior *t0 with from[3]
      case 5:   ... from[4]
      case 4:   ... from[5]
      case 3:   ... from[6]
      case 2:   ... from[7]
      case 1"   ... from[8]
         while( --n >0 ) fails, exiting loop

      final result, *to = from[8]

this is probably not what you wanted to do