Skip to content
Browse files

ex23

  • Loading branch information...
1 parent 2d3268b commit 863fe33c4caf874922d420e0f131aed80d02d315 Sam Breed committed Feb 26, 2012
Showing with 104 additions and 1 deletion.
  1. +1 −1 Makefile
  2. +103 −0 ex23.c
View
2 Makefile
@@ -1,5 +1,5 @@
CFLAGS=-Wall -g
-SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16 ex17 ex18 ex20
+SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16 ex17 ex18 ex20 ex23
all: $(SOURCES)
View
103 ex23.c
@@ -0,0 +1,103 @@
+#include <stdio.h>
+#include <string.h>
+#include "dbg.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;
+
+ // setup the from to have some stuff
+ memset(from, 'x', 1000);
+ // set it to a failure mode
+ memset(to, 'y', 1000);
+ check(valid_copy(to, 1000, 'y'), "Not initialized right");
+
+ // use normal copy to
+ 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);
+
+ // my version
+ rc = zeds_device(from, to, 1000);
+ check(rc = 1000, "Zed's device failed: %d", rc);
+ check(valid_copy(to, 1000, 'x'), "Zed's device copy failed");
+
+ return 0;
+error:
+ return 1;
+}
+

0 comments on commit 863fe33

Please sign in to comment.
Something went wrong with that request. Please try again.