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 | #define FRST(p) (((p[0]) << 8) + p[1]) |
49 | #define FRST(p) (((p[0]) << 8) | p[1]) |
50 | #define NEXT(v,p) (((v) << 8) + p[2]) |
50 | #define NEXT(v,p) (((v) << 8) | p[2]) |
51 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) + h*3) & (HSIZE - 1)) |
51 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
52 | /* |
52 | /* |
53 | * IDX works because it is very similar to a multiplicative hash, e.g. |
53 | * IDX works because it is very similar to a multiplicative hash, e.g. |
54 | * (h * 57321 >> (3*8 - HLOG)) |
54 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
|
|
55 | * the latter is also quite fast on newer CPUs, and sligthly better |
|
|
56 | * |
55 | * the next one is also quite good, albeit slow ;) |
57 | * the next one is also quite good, albeit slow ;) |
56 | * (int)(cos(h & 0xffffff) * 1e6) |
58 | * (int)(cos(h & 0xffffff) * 1e6) |
57 | */ |
59 | */ |
58 | |
60 | |
59 | #if 0 |
61 | #if 0 |
60 | /* original lzv-like hash function */ |
62 | /* original lzv-like hash function, much worse and thus slower */ |
61 | # define FRST(p) (p[0] << 5) ^ p[1] |
63 | # define FRST(p) (p[0] << 5) ^ p[1] |
62 | # define NEXT(v,p) ((v) << 5) ^ p[2] |
64 | # define NEXT(v,p) ((v) << 5) ^ p[2] |
63 | # define IDX(h) ((h) & (HSIZE - 1)) |
65 | # define IDX(h) ((h) & (HSIZE - 1)) |
64 | #endif |
66 | #endif |
65 | |
67 | |
… | |
… | |
135 | /* match found at *ref++ */ |
137 | /* match found at *ref++ */ |
136 | unsigned int len = 2; |
138 | unsigned int len = 2; |
137 | unsigned int maxlen = in_end - ip - len; |
139 | unsigned int maxlen = in_end - ip - len; |
138 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
140 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
139 | |
141 | |
|
|
142 | if (op + lit + 1 + 3 >= out_end) |
|
|
143 | return 0; |
|
|
144 | |
140 | do |
145 | do |
141 | len++; |
146 | len++; |
142 | while (len < maxlen && ref[len] == ip[len]); |
147 | while (len < maxlen && ref[len] == ip[len]); |
143 | |
|
|
144 | if (op + lit + 1 + 3 >= out_end) |
|
|
145 | return 0; |
|
|
146 | |
148 | |
147 | if (lit) |
149 | if (lit) |
148 | { |
150 | { |
149 | *op++ = lit - 1; |
151 | *op++ = lit - 1; |
150 | lit = -lit; |
152 | lit = -lit; |
… | |
… | |
166 | *op++ = len - 7; |
168 | *op++ = len - 7; |
167 | } |
169 | } |
168 | |
170 | |
169 | *op++ = off; |
171 | *op++ = off; |
170 | |
172 | |
171 | #if ULTRA_FAST |
173 | #if ULTRA_FAST || VERY_FAST |
172 | ip += len; |
174 | ip += len; |
|
|
175 | #if VERY_FAST && !ULTRA_FAST |
|
|
176 | --ip; |
|
|
177 | #endif |
173 | hval = FRST (ip); |
178 | hval = FRST (ip); |
|
|
179 | |
174 | hval = NEXT (hval, ip); |
180 | hval = NEXT (hval, ip); |
175 | htab[IDX (hval)] = ip; |
181 | htab[IDX (hval)] = ip; |
176 | ip++; |
182 | ip++; |
|
|
183 | |
|
|
184 | #if VERY_FAST && !ULTRA_FAST |
|
|
185 | hval = NEXT (hval, ip); |
|
|
186 | htab[IDX (hval)] = ip; |
|
|
187 | ip++; |
|
|
188 | #endif |
177 | #else |
189 | #else |
178 | do |
190 | do |
179 | { |
191 | { |
180 | hval = NEXT (hval, ip); |
192 | hval = NEXT (hval, ip); |
181 | htab[IDX (hval)] = ip; |
193 | htab[IDX (hval)] = ip; |