]> git.sur5r.net Git - cc65/blob - test/val/duffs-device.c
fixed optimization bug where array index is 16-bit, e.g. arr16[i & 0x7f7f]
[cc65] / test / val / duffs-device.c
1 /*
2   !!DESCRIPTION!! Implementation of Duff's device (loop unrolling).
3   !!ORIGIN!!      
4   !!LICENCE!!     GPL, read COPYING.GPL
5 */
6
7 #include <stdio.h>
8 #include <limits.h>
9
10 #define ASIZE (100)
11
12 unsigned char success=0;
13 unsigned char failures=0;
14 unsigned char dummy=0;
15
16 #ifdef SUPPORT_BIT_TYPES
17 bit bit0 = 0;
18 #endif
19
20 void done()
21 {
22   dummy++;
23 }
24
25 int acmp(char* a, char* b, int count)
26 {
27   int i;
28
29   for(i = 0; i < count; i++) {
30     if(a[i] != b[i]) {
31       return 1;
32     }
33   }
34   return 0;
35 }
36
37 void duffit (char* to, char* from, int count) 
38 {
39   int n = (count + 7) / 8;
40
41   switch(count % 8) {
42     case 0: do {    *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     } while(--n > 0);
51   }
52 }
53
54 int main(void)
55 {
56   char a[ASIZE] = {1};
57   char b[ASIZE] = {2};
58   
59   /* a and b should be different */
60   if(!acmp(a, b, ASIZE)) {
61     failures++;
62   }
63   
64   duffit(a, b, ASIZE);
65   
66   /* a and b should be the same */
67   if(acmp(a, b, ASIZE)) {
68     failures++;
69   }
70
71   success=failures;
72   done();
73   printf("failures: %d\n",failures);
74
75   return failures;
76 }