summaryrefslogtreecommitdiff
path: root/zlib/crc32.h
blob: 8b5f0187ed70151bbdc9e96e3518d00ffee5e979 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * \file crc32.h
 */

#if defined(__i386__)	/* { */

#define partial_crc32		__z_partial_crc32
#define partial_crc32_copy	__z_partial_crc32_copy

struct __crc32_fool_gcc {
	char	x[32768];
};

#if 0
#define PREFETCH(p)	__asm__ __volatile__("prefetch %0": : "m" (*(struct __crc32_fool_gcc *)(p)))
#else
#define PREFETCH(p)	(*(long *)(struct __crc32_fool_gcc *)(p))
#endif

#if 0
#define PREFETCH(p) do ; while (0)
#endif

static inline void partial_crc32_prep(uLong *crcp)
	/*@modifies *crcp @*/
{
	*crcp ^= 0xffffffffL;
}

static inline void partial_crc32_finish(uLong *crcp)
	/*@modifies *crcp @*/
{
	*crcp ^= 0xffffffffL;
}

static inline uLong get_crc_from_partial(uLong *crcp)
	/*@modifies *crcp @*/
{
	return *crcp ^ 0xffffffffL;
}

/*@observer@*/ /*@unchecked@*/
extern const uLongf crc_table[256];

#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);

static __inline__ uLong __partial_crc32(uLong crc, const Bytef *buf, uInt len, int copy, Bytef *dst)
	/*@modifies *dst @*/
{
	uInt n = len / 4;

#if 1	/* { */

#define DOlong(p, d) do { \
	int i; \
	crc ^= *(p); \
	if (copy) \
		*(d) = *(p); \
	crc = crc_table[crc & 0xff] ^ (crc >> 8); \
	crc = crc_table[crc & 0xff] ^ (crc >> 8); \
	crc = crc_table[crc & 0xff] ^ (crc >> 8); \
	crc = crc_table[crc & 0xff] ^ (crc >> 8); \
} while(0)

#if 0	/* { */
#define UPDcrc(x)	crc =
#define DOlong(p) \
	__asm__ __volatile__(	\
		"xorl	%1,%0;"	\
	  : "=a" (crc)	\
	  : "0" (crc)	\
	  : "ebx"	\
	)
#endif	/* } */

	if (n) {
		long *x = (long *)buf;
		long *y;
		int j = n / 8;

		PREFETCH(x);
		PREFETCH(x+8);
		PREFETCH(x+16);

		if (copy)
			y = (long *)dst;

		while (n >= 8) {
			PREFETCH(x+24);
			/* I hate gcc.  If I turn on loop unrolling,
			 * everything else is slowed down. */
			DOlong(x+0, y+0);
			DOlong(x+1, y+1);
			DOlong(x+2, y+2);
			DOlong(x+3, y+3);
			DOlong(x+4, y+4);
			DOlong(x+5, y+5);
			DOlong(x+6, y+6);
			DOlong(x+7, y+7);
			x += 8;
			y += 8;
			n -= 8;
		}

		while (n--) {
			DOlong(x, y);
			x++;
			y++;
		}

		buf = (Bytef *)x;
		if (copy)
			dst = (Bytef *)y;
	}
	len &= 3;
	while (len--) {
		if (copy)
			*dst++ = *buf;
		DO1(buf);
	}

#else /* } { */

	if (n) {
		do {
			DO4(buf);
		} while (--n);
	}

	len &= 3;
	switch(len) {
	case 3:
		if (copy)
			*dst++ = *buf;
		DO1(buf);
	case 2:
		if (copy)
			*dst++ = *buf;
		DO1(buf);
	case 1:
		if (copy)
			*dst++ = *buf;
		DO1(buf);
	}

#endif /* } */

	return crc;
}

#undef DO1
#undef DOlong

#if 0
#define partial_crc32(crc,buf,len) __partial_crc32(crc, buf, len, 0, 0)
#define partial_crc32_copy(crc,buf,len,dst) __partial_crc32((crc), (buf), (len), 1, (dst))
#endif

extern uLong partial_crc32(uLong crc, const Bytef *buf, uInt len) __attribute__((regparm(3)));
extern uLong partial_crc32_copy(uLong crcp, const Bytef *buf, uInt len, Bytef *dst) __attribute__((regparm(3)));

#endif /* } */