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.4 by pcg, Tue Dec 23 04:52:00 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;
79#endif 98#endif
80 const u8 **hslot; 99 const u8 **hslot;
81 const u8 *ip = (const u8 *)in_data; 100 const u8 *ip = (const u8 *)in_data;
82 u8 *op = (u8 *)out_data; 101 u8 *op = (u8 *)out_data;
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 do 118 for (;;)
101 { 119 {
120 if (ip < in_end - 2)
121 {
102 hval = NEXT (hval, ip); 122 hval = NEXT (hval, ip);
103 hslot = htab + IDX (hval); 123 hslot = htab + IDX (hval);
104 ref = *hslot; *hslot = ip; 124 ref = *hslot; *hslot = ip;
105 125
106 if (1 126 if (1
107#if INIT_HTAB && !USE_MEMCPY 127#if INIT_HTAB && !USE_MEMCPY
108 && ref < ip /* the next test will actually take care of this, but it is faster */ 128 && ref < ip /* the next test will actually take care of this, but this is faster */
109#endif 129#endif
110 && (off = ip - ref - 1) < MAX_OFF 130 && (off = ip - ref - 1) < MAX_OFF
111 && ip + 4 < in_end 131 && ip + 4 < in_end
112 && ref > (u8 *)in_data 132 && ref > (u8 *)in_data
113#if STRICT_ALIGN 133#if STRICT_ALIGN
114 && ref[0] == ip[0] 134 && ref[0] == ip[0]
115 && ref[1] == ip[1] 135 && ref[1] == ip[1]
116 && ref[2] == ip[2] 136 && ref[2] == ip[2]
117#else 137#else
118 && *(u16 *)ref == *(u16 *)ip 138 && *(u16 *)ref == *(u16 *)ip
119 && ref[2] == ip[2] 139 && ref[2] == ip[2]
120#endif 140#endif
121 ) 141 )
122 { 142 {
123 /* match found at *ref++ */ 143 /* match found at *ref++ */
124 unsigned int len = 2; 144 unsigned int len = 2;
125 unsigned int maxlen = in_end - ip - len; 145 unsigned int maxlen = in_end - ip - len;
126 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 146 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
127 147
128 do 148 if (op + lit + 1 + 3 >= out_end)
129 len++; 149 return 0;
150
151 do
152 len++;
130 while (len < maxlen && ref[len] == ip[len]); 153 while (len < maxlen && ref[len] == ip[len]);
131 154
132 if (op + lit + 1 + 3 >= out_end) 155 if (lit)
133 return 0; 156 {
134
135 if (lit)
136 {
137 *op++ = lit - 1; 157 *op++ = lit - 1;
138 lit = -lit; 158 lit = -lit;
139 do 159 do
140 *op++ = ip[lit]; 160 *op++ = ip[lit];
141 while (++lit); 161 while (++lit);
142 } 162 }
143 163
144 len -= 2; 164 len -= 2;
145 ip++; 165 ip++;
146 166
147 if (len < 7) 167 if (len < 7)
148 { 168 {
149 *op++ = (off >> 8) + (len << 5); 169 *op++ = (off >> 8) + (len << 5);
150 } 170 }
151 else 171 else
152 { 172 {
153 *op++ = (off >> 8) + ( 7 << 5); 173 *op++ = (off >> 8) + ( 7 << 5);
154 *op++ = len - 7; 174 *op++ = len - 7;
155 } 175 }
156 176
157 *op++ = off; 177 *op++ = off;
158 178
159#if ULTRA_FAST 179#if ULTRA_FAST || VERY_FAST
160 ip += len; 180 ip += len;
181#if VERY_FAST && !ULTRA_FAST
182 --ip;
183#endif
161 hval = FRST (ip); 184 hval = FRST (ip);
185
162 hval = NEXT (hval, ip); 186 hval = NEXT (hval, ip);
163 htab[IDX (hval)] = ip; 187 htab[IDX (hval)] = ip;
164 ip++; 188 ip++;
165#else 189
166 do 190#if VERY_FAST && !ULTRA_FAST
167 {
168 hval = NEXT (hval, ip); 191 hval = NEXT (hval, ip);
169 htab[IDX (hval)] = ip; 192 htab[IDX (hval)] = ip;
193 ip++;
194#endif
195#else
196 do
197 {
198 hval = NEXT (hval, ip);
199 htab[IDX (hval)] = ip;
200 ip++;
201 }
202 while (len--);
203#endif
204 continue;
205 }
206 }
207 else if (ip == in_end)
208 break;
209
210 /* one more literal byte we must copy */
211 lit++;
170 ip++; 212 ip++;
171 }
172 while (len--);
173#endif
174 }
175 else
176 {
177 /* one more literal byte we must copy */
178 lit++;
179 ip++;
180 213
181 if (lit == MAX_LIT) 214 if (lit == MAX_LIT)
182 { 215 {
183 if (op + 1 + MAX_LIT >= out_end) 216 if (op + 1 + MAX_LIT >= out_end)
184 return 0; 217 return 0;
185 218
186 *op++ = MAX_LIT - 1; 219 *op++ = MAX_LIT - 1;
187#if USE_MEMCPY 220
188 memcpy (op, ip - MAX_LIT, MAX_LIT); 221#ifdef lzf_movsb
189 op += MAX_LIT; 222 ip -= lit;
190 lit = 0; 223 lzf_movsb (op, ip, lit);
191#else 224#else
192 lit = -lit; 225 lit = -lit;
193 do 226 do
194 *op++ = ip[lit]; 227 *op++ = ip[lit];
195 while (++lit); 228 while (++lit);
196#endif 229#endif
197 } 230 }
198 }
199 } 231 }
200 while (ip < in_end);
201 232
202 if (lit) 233 if (lit)
203 { 234 {
204 if (op + lit + 1 >= out_end) 235 if (op + lit + 1 >= out_end)
205 return 0; 236 return 0;
206 237
207 *op++ = lit - 1; 238 *op++ = lit - 1;
239#ifdef lzf_movsb
240 ip -= lit;
241 lzf_movsb (op, ip, lit);
242#else
208 lit = -lit; 243 lit = -lit;
209 do 244 do
210 *op++ = ip[lit]; 245 *op++ = ip[lit];
211 while (++lit); 246 while (++lit);
247#endif
212 } 248 }
213 249
214 return op - (u8 *) out_data; 250 return op - (u8 *) out_data;
215} 251}
252

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines