File: duffs-device.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (76 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (3)
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
/*
  !!DESCRIPTION!! Implementation of Duff's device (loop unrolling).
  !!ORIGIN!!      
  !!LICENCE!!     GPL, read COPYING.GPL
*/

#include <stdio.h>
#include <limits.h>

#define ASIZE (100)

unsigned char success=0;
unsigned char failures=0;
unsigned char dummy=0;

#ifdef SUPPORT_BIT_TYPES
bit bit0 = 0;
#endif

void done()
{
  dummy++;
}

int acmp(char* a, char* b, int count)
{
  int i;

  for(i = 0; i < count; i++) {
    if(a[i] != b[i]) {
      return 1;
    }
  }
  return 0;
}

void duffit (char* to, char* 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(void)
{
  char a[ASIZE] = {1};
  char b[ASIZE] = {2};
  
  /* a and b should be different */
  if(!acmp(a, b, ASIZE)) {
    failures++;
  }
  
  duffit(a, b, ASIZE);
  
  /* a and b should be the same */
  if(acmp(a, b, ASIZE)) {
    failures++;
  }

  success=failures;
  done();
  printf("failures: %d\n",failures);

  return failures;
}