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.5 by pcg, Mon Dec 29 12:48:16 2003 UTC vs.
Revision 1.15 by root, Fri Nov 2 12:39:20 2007 UTC

1/* 1/*
2 * Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com> 2 * Copyright (c) 2000-2007 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,
8 * this list of conditions and the following disclaimer. 8 * this list of conditions and the following disclaimer.
9 * 9 *
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 * 13 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 22 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License version 2 (the "GPL"), in which case the
27 * provisions of the GPL are applicable instead of the above. If you wish to
28 * allow the use of your version of this file only under the terms of the
29 * GPL and not to allow others to use your version of this file under the
30 * BSD license, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the GPL. If
32 * you do not delete the provisions above, a recipient may use your version
33 * of this file under either the BSD or the GPL.
27 */ 34 */
28 35
29#include "lzfP.h" 36#include "lzfP.h"
30 37
31#define HSIZE (1 << (HLOG)) 38#define HSIZE (1 << (HLOG))
34 * don't play with this unless you benchmark! 41 * don't play with this unless you benchmark!
35 * decompression is not dependent on the hash function 42 * decompression is not dependent on the hash function
36 * the hashing function might seem strange, just believe me 43 * the hashing function might seem strange, just believe me
37 * it works ;) 44 * it works ;)
38 */ 45 */
46#ifndef FRST
39#define FRST(p) (((p[0]) << 8) + p[1]) 47# define FRST(p) (((p[0]) << 8) | p[1])
40#define NEXT(v,p) (((v) << 8) + p[2]) 48# define NEXT(v,p) (((v) << 8) | p[2])
41#define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) + h*3) & (HSIZE - 1)) 49# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
50/*# define IDX(h) ((ip[0] * 121 ^ ip[1] * 33 ^ ip[2] * 1) & (HSIZE-1))*/
51#endif
42/* 52/*
43 * 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.
44 * (h * 57321 >> (3*8 - HLOG)) 54 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
55 * the latter is also quite fast on newer CPUs, and compresses similarly.
56 *
45 * the next one is also quite good, albeit slow ;) 57 * the next one is also quite good, albeit slow ;)
46 * (int)(cos(h & 0xffffff) * 1e6) 58 * (int)(cos(h & 0xffffff) * 1e6)
47 */ 59 */
48 60
49#if 0 61#if 0
50/* original lzv-like hash function */ 62/* original lzv-like hash function, much worse and thus slower */
51# define FRST(p) (p[0] << 5) ^ p[1] 63# define FRST(p) (p[0] << 5) ^ p[1]
52# define NEXT(v,p) ((v) << 5) ^ p[2] 64# define NEXT(v,p) ((v) << 5) ^ p[2]
53# define IDX(h) ((h) & (HSIZE - 1)) 65# define IDX(h) ((h) & (HSIZE - 1))
54#endif 66#endif
55 67
56#define MAX_LIT (1 << 5) 68#define MAX_LIT (1 << 5)
57#define MAX_OFF (1 << 13) 69#define MAX_OFF (1 << 13)
58#define MAX_REF ((1 << 8) + (1 << 3)) 70#define MAX_REF ((1 << 8) + (1 << 3))
71
72#if (__i386 || __amd64) && __GNUC__ >= 3
73# define lzf_movsb(dst, src, len) \
74 asm ("rep movsb" \
75 : "=D" (dst), "=S" (src), "=c" (len) \
76 : "0" (dst), "1" (src), "2" (len));
77#endif
59 78
60/* 79/*
61 * compressed format 80 * compressed format
62 * 81 *
63 * 000LLLLL <L+1> ; literal 82 * 000LLLLL <L+1> ; literal
64 * LLLOOOOO oooooooo ; backref L 83 * LLLooooo oooooooo ; backref L
65 * 111OOOOO LLLLLLLL oooooooo ; backref L+7 84 * 111ooooo LLLLLLLL oooooooo ; backref L+7
66 * 85 *
67 */ 86 */
68 87
69unsigned int 88unsigned int
70lzf_compress (const void *const in_data, unsigned int in_len, 89lzf_compress (const void *const in_data, unsigned int in_len,
71 void *out_data, unsigned int out_len 90 void *out_data, unsigned int out_len
72#if LZF_STATE_ARG 91#if LZF_STATE_ARG
73 , LZF_STATE *htab 92 , LZF_STATE htab
74#endif 93#endif
75 ) 94 )
76{ 95{
77#if !LZF_STATE_ARG 96#if !LZF_STATE_ARG
78 LZF_STATE htab; 97 LZF_STATE htab;
87 unsigned int hval = FRST (ip); 106 unsigned int hval = FRST (ip);
88 unsigned long off; 107 unsigned long off;
89 int lit = 0; 108 int lit = 0;
90 109
91#if INIT_HTAB 110#if INIT_HTAB
92# if USE_MEMCPY
93 memset (htab, 0, sizeof (htab)); 111 memset (htab, 0, sizeof (htab));
94# else 112# if 0
95 for (hslot = htab; hslot < htab + HSIZE; hslot++) 113 for (hslot = htab; hslot < htab + HSIZE; hslot++)
96 *hslot++ = ip; 114 *hslot++ = ip;
97# endif 115# endif
98#endif 116#endif
99 117
100 for (;;) 118 for (;;)
101 { 119 {
125 /* match found at *ref++ */ 143 /* match found at *ref++ */
126 unsigned int len = 2; 144 unsigned int len = 2;
127 unsigned int maxlen = in_end - ip - len; 145 unsigned int maxlen = in_end - ip - len;
128 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 146 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
129 147
148 if (op + lit + 1 + 3 >= out_end)
149 return 0;
150
130 do 151 do
131 len++; 152 len++;
132 while (len < maxlen && ref[len] == ip[len]); 153 while (len < maxlen && ref[len] == ip[len]);
133
134 if (op + lit + 1 + 3 >= out_end)
135 return 0;
136 154
137 if (lit) 155 if (lit)
138 { 156 {
139 *op++ = lit - 1; 157 *op++ = lit - 1;
140 lit = -lit; 158 lit = -lit;
156 *op++ = len - 7; 174 *op++ = len - 7;
157 } 175 }
158 176
159 *op++ = off; 177 *op++ = off;
160 178
161#if ULTRA_FAST 179#if ULTRA_FAST || VERY_FAST
162 ip += len; 180 ip += len;
181#if VERY_FAST && !ULTRA_FAST
182 --ip;
183#endif
163 hval = FRST (ip); 184 hval = FRST (ip);
185
164 hval = NEXT (hval, ip); 186 hval = NEXT (hval, ip);
165 htab[IDX (hval)] = ip; 187 htab[IDX (hval)] = ip;
166 ip++; 188 ip++;
189
190#if VERY_FAST && !ULTRA_FAST
191 hval = NEXT (hval, ip);
192 htab[IDX (hval)] = ip;
193 ip++;
194#endif
167#else 195#else
168 do 196 do
169 { 197 {
170 hval = NEXT (hval, ip); 198 hval = NEXT (hval, ip);
171 htab[IDX (hval)] = ip; 199 htab[IDX (hval)] = ip;
187 { 215 {
188 if (op + 1 + MAX_LIT >= out_end) 216 if (op + 1 + MAX_LIT >= out_end)
189 return 0; 217 return 0;
190 218
191 *op++ = MAX_LIT - 1; 219 *op++ = MAX_LIT - 1;
192#if USE_MEMCPY 220
193 memcpy (op, ip - MAX_LIT, MAX_LIT); 221#ifdef lzf_movsb
194 op += MAX_LIT; 222 ip -= lit;
195 lit = 0; 223 lzf_movsb (op, ip, lit);
196#else 224#else
197 lit = -lit; 225 lit = -lit;
198 do 226 do
199 *op++ = ip[lit]; 227 *op++ = ip[lit];
200 while (++lit); 228 while (++lit);
206 { 234 {
207 if (op + lit + 1 >= out_end) 235 if (op + lit + 1 >= out_end)
208 return 0; 236 return 0;
209 237
210 *op++ = lit - 1; 238 *op++ = lit - 1;
239#ifdef lzf_movsb
240 ip -= lit;
241 lzf_movsb (op, ip, lit);
242#else
211 lit = -lit; 243 lit = -lit;
212 do 244 do
213 *op++ = ip[lit]; 245 *op++ = ip[lit];
214 while (++lit); 246 while (++lit);
247#endif
215 } 248 }
216 249
217 return op - (u8 *) out_data; 250 return op - (u8 *) out_data;
218} 251}
252

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines