-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex23.c
98 lines (80 loc) · 1.89 KB
/
ex23.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "dbg.h"
#include <stdio.h>
#include <string.h>
int normal_copy(char* from, char* to, int count) {
int i = 0;
for (i = 0; i < count; i++) {
to[i] = from[i];
}
return i;
}
int duffs_device(char* from, char* to, 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);
}
}
return count;
}
int zeds_device(char* from, char* to, int count) {
{
int n = (count + 7) / 8;
switch (count % 8) {
case 0:
again:
*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++;
if (--n > 0) goto again;
}
}
return count;
}
int valid_copy(char* data, int count, char expects) {
int i = 0;
for (i = 0; i < count; i++) {
if (data[i] != expects) {
log_err("[%d] %c != %c", i, data[i], expects);
return 0;
}
}
return 1;
}
int main(int argc, char* argv[]) {
char from[1000] = { 'a' };
char to[1000] = { 'c' };
int rc = 0;
memset(from, 'x', 1000);
memset(to, 'y', 1000);
check(valid_copy(to, 1000, 'y'), "Not initialized right");
rc = normal_copy(from, to, 1000);
check(rc == 1000, "Normal copy failed: %d", rc);
check(valid_copy(to, 1000, 'x'), "Normal copy failed");
// reset
memset(to, 'y', 1000);
rc = duffs_device(from, to, 1000);
check(rc == 1000, "Duffs device failed: %d", rc);
check(valid_copy(to, 1000, 'x'), "Duffs device failed");
memset(to, 'y', 1000);
rc = zeds_device(from, to, 1000);
check(rc == 1000, "Zeds device failed: %d", rc);
check(valid_copy(to, 1000, 'x'), "Zeds device failed copy");
return 0;
error:
return 1;
}