1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com> |
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
… | |
… | |
44 | * don't play with this unless you benchmark! |
44 | * don't play with this unless you benchmark! |
45 | * decompression is not dependent on the hash function |
45 | * decompression is not dependent on the hash function |
46 | * the hashing function might seem strange, just believe me |
46 | * the hashing function might seem strange, just believe me |
47 | * it works ;) |
47 | * it works ;) |
48 | */ |
48 | */ |
|
|
49 | #ifndef FRST |
49 | #define FRST(p) (((p[0]) << 8) + p[1]) |
50 | # define FRST(p) (((p[0]) << 8) | p[1]) |
50 | #define NEXT(v,p) (((v) << 8) + p[2]) |
51 | # define NEXT(v,p) (((v) << 8) | p[2]) |
51 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) + h*3) & (HSIZE - 1)) |
52 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
|
|
53 | /*# define IDX(h) ((ip[0] * 121 ^ ip[1] * 33 ^ ip[2] * 1) & (HSIZE-1))*/ |
|
|
54 | #endif |
52 | /* |
55 | /* |
53 | * IDX works because it is very similar to a multiplicative hash, e.g. |
56 | * IDX works because it is very similar to a multiplicative hash, e.g. |
54 | * (h * 57321 >> (3*8 - HLOG)) |
57 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
|
|
58 | * the latter is also quite fast on newer CPUs, and compresses similarly. |
|
|
59 | * |
55 | * the next one is also quite good, albeit slow ;) |
60 | * the next one is also quite good, albeit slow ;) |
56 | * (int)(cos(h & 0xffffff) * 1e6) |
61 | * (int)(cos(h & 0xffffff) * 1e6) |
57 | */ |
62 | */ |
58 | |
63 | |
59 | #if 0 |
64 | #if 0 |
60 | /* original lzv-like hash function */ |
65 | /* original lzv-like hash function, much worse and thus slower */ |
61 | # define FRST(p) (p[0] << 5) ^ p[1] |
66 | # define FRST(p) (p[0] << 5) ^ p[1] |
62 | # define NEXT(v,p) ((v) << 5) ^ p[2] |
67 | # define NEXT(v,p) ((v) << 5) ^ p[2] |
63 | # define IDX(h) ((h) & (HSIZE - 1)) |
68 | # define IDX(h) ((h) & (HSIZE - 1)) |
64 | #endif |
69 | #endif |
65 | |
70 | |
66 | #define MAX_LIT (1 << 5) |
71 | #define MAX_LIT (1 << 5) |
67 | #define MAX_OFF (1 << 13) |
72 | #define MAX_OFF (1 << 13) |
68 | #define MAX_REF ((1 << 8) + (1 << 3)) |
73 | #define MAX_REF ((1 << 8) + (1 << 3)) |
|
|
74 | |
|
|
75 | #if (__i386 || __amd64) && __GNUC__ >= 3 |
|
|
76 | # define lzf_movsb(dst, src, len) \ |
|
|
77 | asm ("rep movsb" \ |
|
|
78 | : "=D" (dst), "=S" (src), "=c" (len) \ |
|
|
79 | : "0" (dst), "1" (src), "2" (len)); |
|
|
80 | #endif |
69 | |
81 | |
70 | /* |
82 | /* |
71 | * compressed format |
83 | * compressed format |
72 | * |
84 | * |
73 | * 000LLLLL <L+1> ; literal |
85 | * 000LLLLL <L+1> ; literal |
74 | * LLLOOOOO oooooooo ; backref L |
86 | * LLLooooo oooooooo ; backref L |
75 | * 111OOOOO LLLLLLLL oooooooo ; backref L+7 |
87 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 |
76 | * |
88 | * |
77 | */ |
89 | */ |
78 | |
90 | |
79 | unsigned int |
91 | unsigned int |
80 | lzf_compress (const void *const in_data, unsigned int in_len, |
92 | lzf_compress (const void *const in_data, unsigned int in_len, |
81 | void *out_data, unsigned int out_len |
93 | void *out_data, unsigned int out_len |
82 | #if LZF_STATE_ARG |
94 | #if LZF_STATE_ARG |
83 | , LZF_STATE *htab |
95 | , LZF_STATE htab |
84 | #endif |
96 | #endif |
85 | ) |
97 | ) |
86 | { |
98 | { |
87 | #if !LZF_STATE_ARG |
99 | #if !LZF_STATE_ARG |
88 | LZF_STATE htab; |
100 | LZF_STATE htab; |
… | |
… | |
97 | unsigned int hval = FRST (ip); |
109 | unsigned int hval = FRST (ip); |
98 | unsigned long off; |
110 | unsigned long off; |
99 | int lit = 0; |
111 | int lit = 0; |
100 | |
112 | |
101 | #if INIT_HTAB |
113 | #if INIT_HTAB |
102 | # if USE_MEMCPY |
|
|
103 | memset (htab, 0, sizeof (htab)); |
114 | memset (htab, 0, sizeof (htab)); |
104 | # else |
115 | # if 0 |
105 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
116 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
106 | *hslot++ = ip; |
117 | *hslot++ = ip; |
107 | # endif |
118 | # endif |
108 | #endif |
119 | #endif |
109 | |
120 | |
110 | for (;;) |
121 | for (;;) |
111 | { |
122 | { |
… | |
… | |
135 | /* match found at *ref++ */ |
146 | /* match found at *ref++ */ |
136 | unsigned int len = 2; |
147 | unsigned int len = 2; |
137 | unsigned int maxlen = in_end - ip - len; |
148 | unsigned int maxlen = in_end - ip - len; |
138 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
149 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
139 | |
150 | |
|
|
151 | if (op + lit + 1 + 3 >= out_end) |
|
|
152 | return 0; |
|
|
153 | |
140 | do |
154 | do |
141 | len++; |
155 | len++; |
142 | while (len < maxlen && ref[len] == ip[len]); |
156 | while (len < maxlen && ref[len] == ip[len]); |
143 | |
|
|
144 | if (op + lit + 1 + 3 >= out_end) |
|
|
145 | return 0; |
|
|
146 | |
157 | |
147 | if (lit) |
158 | if (lit) |
148 | { |
159 | { |
149 | *op++ = lit - 1; |
160 | *op++ = lit - 1; |
150 | lit = -lit; |
161 | lit = -lit; |
… | |
… | |
166 | *op++ = len - 7; |
177 | *op++ = len - 7; |
167 | } |
178 | } |
168 | |
179 | |
169 | *op++ = off; |
180 | *op++ = off; |
170 | |
181 | |
171 | #if ULTRA_FAST |
182 | #if ULTRA_FAST || VERY_FAST |
172 | ip += len; |
183 | ip += len; |
|
|
184 | #if VERY_FAST && !ULTRA_FAST |
|
|
185 | --ip; |
|
|
186 | #endif |
173 | hval = FRST (ip); |
187 | hval = FRST (ip); |
|
|
188 | |
174 | hval = NEXT (hval, ip); |
189 | hval = NEXT (hval, ip); |
175 | htab[IDX (hval)] = ip; |
190 | htab[IDX (hval)] = ip; |
176 | ip++; |
191 | ip++; |
|
|
192 | |
|
|
193 | #if VERY_FAST && !ULTRA_FAST |
|
|
194 | hval = NEXT (hval, ip); |
|
|
195 | htab[IDX (hval)] = ip; |
|
|
196 | ip++; |
|
|
197 | #endif |
177 | #else |
198 | #else |
178 | do |
199 | do |
179 | { |
200 | { |
180 | hval = NEXT (hval, ip); |
201 | hval = NEXT (hval, ip); |
181 | htab[IDX (hval)] = ip; |
202 | htab[IDX (hval)] = ip; |
… | |
… | |
197 | { |
218 | { |
198 | if (op + 1 + MAX_LIT >= out_end) |
219 | if (op + 1 + MAX_LIT >= out_end) |
199 | return 0; |
220 | return 0; |
200 | |
221 | |
201 | *op++ = MAX_LIT - 1; |
222 | *op++ = MAX_LIT - 1; |
202 | #if USE_MEMCPY |
223 | |
203 | memcpy (op, ip - MAX_LIT, MAX_LIT); |
224 | #ifdef lzf_movsb |
204 | op += MAX_LIT; |
225 | ip -= lit; |
205 | lit = 0; |
226 | lzf_movsb (op, ip, lit); |
206 | #else |
227 | #else |
207 | lit = -lit; |
228 | lit = -lit; |
208 | do |
229 | do |
209 | *op++ = ip[lit]; |
230 | *op++ = ip[lit]; |
210 | while (++lit); |
231 | while (++lit); |
… | |
… | |
216 | { |
237 | { |
217 | if (op + lit + 1 >= out_end) |
238 | if (op + lit + 1 >= out_end) |
218 | return 0; |
239 | return 0; |
219 | |
240 | |
220 | *op++ = lit - 1; |
241 | *op++ = lit - 1; |
|
|
242 | #ifdef lzf_movsb |
|
|
243 | ip -= lit; |
|
|
244 | lzf_movsb (op, ip, lit); |
|
|
245 | #else |
221 | lit = -lit; |
246 | lit = -lit; |
222 | do |
247 | do |
223 | *op++ = ip[lit]; |
248 | *op++ = ip[lit]; |
224 | while (++lit); |
249 | while (++lit); |
|
|
250 | #endif |
225 | } |
251 | } |
226 | |
252 | |
227 | return op - (u8 *) out_data; |
253 | return op - (u8 *) out_data; |
228 | } |
254 | } |
|
|
255 | |