Duff デバイスを実装しようとしましたが、うまくいきません。何もコピーしません。元のバージョンとより明確なバージョンを実装しました。
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;
}
出力は次のとおりです。
21, 34, 12, 64, 13, 9, 56, 54, 90, 1,
22, 0, 0, 0, 0, 0, 0, 0, 0, 0,
何もコピーしません。元のバージョンでは、何らかの理由でポインターをインクリメントしませんが、インクリメントする必要があると思います(関数で行います)。
EDIT 報告されたように、私のコードにエラーがあります:私はそれを変更しました:
send(b, a, 10);
しかし、今の出力は次のとおりです。
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
EDIT 2 コードを機能させるための最後の編集:
duff_function(b, a);