Line | |
---|
1 | /* The original Duff's device, with unsupported switch/do combination |
---|
2 | void send(short *to, short *from, int count) |
---|
3 | { |
---|
4 | int n=(count+7)/8; |
---|
5 | switch(count%8){ |
---|
6 | case 0: do{ *to = *from++; |
---|
7 | case 7: *to = *from++; |
---|
8 | case 6: *to = *from++; |
---|
9 | case 5: *to = *from++; |
---|
10 | case 4: *to = *from++; |
---|
11 | case 3: *to = *from++; |
---|
12 | case 2: *to = *from++; |
---|
13 | case 1: *to = *from++; |
---|
14 | }while(--n>0); |
---|
15 | } |
---|
16 | } |
---|
17 | */ |
---|
18 | |
---|
19 | /* The original Duff's device, altered for copying |
---|
20 | void copy(short *to, short *from, int count) |
---|
21 | { |
---|
22 | int n=(count+7)/8; |
---|
23 | switch(count%8){ |
---|
24 | case 0: do{ *to++ = *from++; |
---|
25 | case 7: *to++ = *from++; |
---|
26 | case 6: *to++ = *from++; |
---|
27 | case 5: *to++ = *from++; |
---|
28 | case 4: *to++ = *from++; |
---|
29 | case 3: *to++ = *from++; |
---|
30 | case 2: *to++ = *from++; |
---|
31 | case 1: *to++ = *from++; |
---|
32 | }while(--n>0); |
---|
33 | } |
---|
34 | } |
---|
35 | */ |
---|
36 | |
---|
37 | /* Using goto instead: */ |
---|
38 | void copy(short *to, short *from, int count) |
---|
39 | { |
---|
40 | int n=(count+7)/8; |
---|
41 | switch(count%8){ |
---|
42 | case 0: dolab: *to++ = *from++; |
---|
43 | case 7: *to++ = *from++; |
---|
44 | case 6: *to++ = *from++; |
---|
45 | case 5: *to++ = *from++; |
---|
46 | case 4: *to++ = *from++; |
---|
47 | case 3: *to++ = *from++; |
---|
48 | case 2: *to++ = *from++; |
---|
49 | case 1: *to++ = *from++; |
---|
50 | if (--n>0) goto dolab; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | int main(void) { |
---|
55 | short arr1[3] = {1,2,3}; |
---|
56 | short arr2[3]; |
---|
57 | |
---|
58 | copy(arr2,arr1,3); |
---|
59 | |
---|
60 | return arr2[0] + arr2[1] + arr2[2]; |
---|
61 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.