ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf_c.c
(Generate patch)

Comparing liblzf/lzf_c.c (file contents):
Revision 1.16 by root, Mon Nov 5 23:28:40 2007 UTC vs.
Revision 1.27 by ayin, Sun Nov 25 16:06:18 2007 UTC

45 * it works ;) 45 * it works ;)
46 */ 46 */
47#ifndef FRST 47#ifndef FRST
48# define FRST(p) (((p[0]) << 8) | p[1]) 48# define FRST(p) (((p[0]) << 8) | p[1])
49# define NEXT(v,p) (((v) << 8) | p[2]) 49# define NEXT(v,p) (((v) << 8) | p[2])
50# if ULTRA_FAST
51# define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
52# elif VERY_FAST
53# define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
54# else
50# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) 55# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
51/*# define IDX(h) ((ip[0] * 121 ^ ip[1] * 33 ^ ip[2] * 1) & (HSIZE-1))*/ 56# endif
52#endif 57#endif
53/* 58/*
54 * IDX works because it is very similar to a multiplicative hash, e.g. 59 * IDX works because it is very similar to a multiplicative hash, e.g.
55 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) 60 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
56 * the latter is also quite fast on newer CPUs, and compresses similarly. 61 * the latter is also quite fast on newer CPUs, and compresses similarly.
68 73
69#define MAX_LIT (1 << 5) 74#define MAX_LIT (1 << 5)
70#define MAX_OFF (1 << 13) 75#define MAX_OFF (1 << 13)
71#define MAX_REF ((1 << 8) + (1 << 3)) 76#define MAX_REF ((1 << 8) + (1 << 3))
72 77
73#if (__i386 || __amd64) && __GNUC__ >= 3 78#if __GNUC__ >= 3
74# define lzf_movsb(dst, src, len) \ 79# define expect(expr,value) __builtin_expect ((expr),(value))
75 asm ("rep movsb" \ 80# define inline inline
76 : "=D" (dst), "=S" (src), "=c" (len) \ 81#else
77 : "0" (dst), "1" (src), "2" (len)); 82# define expect(expr,value) (expr)
83# define inline static
78#endif 84#endif
85
86#define expect_false(expr) expect ((expr) != 0, 0)
87#define expect_true(expr) expect ((expr) != 0, 1)
79 88
80/* 89/*
81 * compressed format 90 * compressed format
82 * 91 *
83 * 000LLLLL <L+1> ; literal 92 * 000LLLLL <L+1> ; literal
102 u8 *op = (u8 *)out_data; 111 u8 *op = (u8 *)out_data;
103 const u8 *in_end = ip + in_len; 112 const u8 *in_end = ip + in_len;
104 u8 *out_end = op + out_len; 113 u8 *out_end = op + out_len;
105 const u8 *ref; 114 const u8 *ref;
106 115
107 unsigned int hval = FRST (ip); 116 unsigned int hval;
108 unsigned long off; 117 unsigned long off;
109 int lit = 0; 118 int lit;
119
120 if (!in_len || !out_len)
121 return 0;
110 122
111#if INIT_HTAB 123#if INIT_HTAB
112 memset (htab, 0, sizeof (htab)); 124 memset (htab, 0, sizeof (htab));
113# if 0 125# if 0
114 for (hslot = htab; hslot < htab + HSIZE; hslot++) 126 for (hslot = htab; hslot < htab + HSIZE; hslot++)
115 *hslot++ = ip; 127 *hslot++ = ip;
116# endif 128# endif
117#endif 129#endif
118 130
119 for (;;) 131 lit = 0; op++; /* start run */
132
133 hval = FRST (ip);
134 while (ip < in_end - 2)
120 { 135 {
121 if (ip < in_end - 2) 136 hval = NEXT (hval, ip);
137 hslot = htab + IDX (hval);
138 ref = *hslot; *hslot = ip;
139
140 if (1
141#if INIT_HTAB
142 && ref < ip /* the next test will actually take care of this, but this is faster */
143#endif
144 && (off = ip - ref - 1) < MAX_OFF
145 && ip + 4 < in_end
146 && ref > (u8 *)in_data
147#if STRICT_ALIGN
148 && ref[0] == ip[0]
149 && ref[1] == ip[1]
150 && ref[2] == ip[2]
151#else
152 && *(u16 *)ref == *(u16 *)ip
153 && ref[2] == ip[2]
154#endif
155 )
122 { 156 {
123 hval = NEXT (hval, ip);
124 hslot = htab + IDX (hval);
125 ref = *hslot; *hslot = ip;
126
127 if (1
128#if INIT_HTAB && !USE_MEMCPY
129 && ref < ip /* the next test will actually take care of this, but this is faster */
130#endif
131 && (off = ip - ref - 1) < MAX_OFF
132 && ip + 4 < in_end
133 && ref > (u8 *)in_data
134#if STRICT_ALIGN
135 && ref[0] == ip[0]
136 && ref[1] == ip[1]
137 && ref[2] == ip[2]
138#else
139 && *(u16 *)ref == *(u16 *)ip
140 && ref[2] == ip[2]
141#endif
142 )
143 {
144 /* match found at *ref++ */ 157 /* match found at *ref++ */
145 unsigned int len = 2; 158 unsigned int len = 2;
146 unsigned int maxlen = in_end - ip - len; 159 unsigned int maxlen = in_end - ip - len;
147 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 160 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
148 161
149 if (op + lit + 1 + 3 >= out_end) 162 op [- lit - 1] = lit - 1; /* stop run */
163 op -= !lit; /* undo run if length is zero */
164
165 if (expect_false (op + 3 + 1 >= out_end))
150 return 0; 166 return 0;
167
168 for (;;)
169 {
170 if (expect_true (maxlen > 16))
171 {
172 len++; if (ref [len] != ip [len]) break;
173 len++; if (ref [len] != ip [len]) break;
174 len++; if (ref [len] != ip [len]) break;
175 len++; if (ref [len] != ip [len]) break;
176
177 len++; if (ref [len] != ip [len]) break;
178 len++; if (ref [len] != ip [len]) break;
179 len++; if (ref [len] != ip [len]) break;
180 len++; if (ref [len] != ip [len]) break;
181
182 len++; if (ref [len] != ip [len]) break;
183 len++; if (ref [len] != ip [len]) break;
184 len++; if (ref [len] != ip [len]) break;
185 len++; if (ref [len] != ip [len]) break;
186
187 len++; if (ref [len] != ip [len]) break;
188 len++; if (ref [len] != ip [len]) break;
189 len++; if (ref [len] != ip [len]) break;
190 len++; if (ref [len] != ip [len]) break;
191 }
151 192
152 do 193 do
153 len++; 194 len++;
154 while (len < maxlen && ref[len] == ip[len]); 195 while (len < maxlen && ref[len] == ip[len]);
155 196
156 if (lit)
157 { 197 break;
158 *op++ = lit - 1;
159 lit = -lit;
160 do
161 *op++ = ip[lit];
162 while (++lit);
163 } 198 }
164 199
165 len -= 2; 200 len -= 2;
166 ip++; 201 ip++;
167 202
168 if (len < 7) 203 if (len < 7)
169 { 204 {
170 *op++ = (off >> 8) + (len << 5); 205 *op++ = (off >> 8) + (len << 5);
171 } 206 }
172 else 207 else
173 { 208 {
174 *op++ = (off >> 8) + ( 7 << 5); 209 *op++ = (off >> 8) + ( 7 << 5);
175 *op++ = len - 7; 210 *op++ = len - 7;
176 } 211 }
177 212
178 *op++ = off; 213 *op++ = off;
179 214
180#if ULTRA_FAST || VERY_FAST 215#if ULTRA_FAST || VERY_FAST
181 ip += len; 216 ip += len;
182#if VERY_FAST && !ULTRA_FAST 217#if VERY_FAST && !ULTRA_FAST
183 --ip; 218 --ip;
184#endif 219#endif
185 hval = FRST (ip); 220 hval = FRST (ip);
186 221
222 hval = NEXT (hval, ip);
223 htab[IDX (hval)] = ip;
224 ip++;
225
226#if VERY_FAST && !ULTRA_FAST
227 hval = NEXT (hval, ip);
228 htab[IDX (hval)] = ip;
229 ip++;
230#endif
231#else
232 do
233 {
187 hval = NEXT (hval, ip); 234 hval = NEXT (hval, ip);
188 htab[IDX (hval)] = ip; 235 htab[IDX (hval)] = ip;
189 ip++; 236 ip++;
190
191#if VERY_FAST && !ULTRA_FAST
192 hval = NEXT (hval, ip);
193 htab[IDX (hval)] = ip;
194 ip++;
195#endif
196#else
197 do
198 {
199 hval = NEXT (hval, ip);
200 htab[IDX (hval)] = ip;
201 ip++;
202 } 237 }
203 while (len--); 238 while (len--);
204#endif 239#endif
205 continue; 240
206 } 241 lit = 0; op++; /* start run */
207 } 242 }
208 else if (ip == in_end) 243 else
209 break;
210
211 /* one more literal byte we must copy */
212 lit++;
213 ip++;
214
215 if (lit == MAX_LIT)
216 { 244 {
217 if (op + 1 + MAX_LIT >= out_end) 245 /* one more literal byte we must copy */
246 if (expect_false (op >= out_end))
218 return 0; 247 return 0;
219 248
220 *op++ = MAX_LIT - 1; 249 lit++; *op++ = *ip++;
221 250
222#ifdef lzf_movsb 251 if (expect_false (lit == MAX_LIT))
223 ip -= lit;
224 lzf_movsb (op, ip, lit);
225#else
226 lit = -lit;
227 do 252 {
228 *op++ = ip[lit]; 253 op [- lit - 1] = lit - 1; /* stop run */
229 while (++lit); 254 lit = 0; op++; /* start run */
230#endif 255 }
231 } 256 }
232 } 257 }
233 258
234 if (lit) 259 if (op + 2 > out_end) /* at most 2 bytes can be missing here */
260 return 0;
261
262 while (ip < in_end)
235 { 263 {
236 if (op + lit + 1 >= out_end) 264 lit++; *op++ = *ip++;
237 return 0;
238
239 *op++ = lit - 1;
240#ifdef lzf_movsb
241 ip -= lit;
242 lzf_movsb (op, ip, lit);
243#else
244 lit = -lit;
245 do
246 *op++ = ip[lit];
247 while (++lit);
248#endif
249 } 265 }
250 266
267 op [- lit - 1] = lit - 1; /* end run */
268 op -= !lit; /* undo run if length is zero */
269
251 return op - (u8 *) out_data; 270 return op - (u8 *)out_data;
252} 271}
253 272

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines