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.9 by root, Tue Mar 8 19:59:52 2005 UTC

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,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 24 * 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 25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE. 26 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License version 2 (the "GPL"), in which case the
30 * provisions of the GPL are applicable instead of the above. If you wish to
31 * allow the use of your version of this file only under the terms of the
32 * GPL and not to allow others to use your version of this file under the
33 * BSD license, indicate your decision by deleting the provisions above and
34 * replace them with the notice and other provisions required by the GPL. If
35 * you do not delete the provisions above, a recipient may use your version
36 * of this file under either the BSD or the GPL.
27 */ 37 */
28 38
29#include "lzfP.h" 39#include "lzfP.h"
30 40
31#define HSIZE (1 << (HLOG)) 41#define HSIZE (1 << (HLOG))
34 * don't play with this unless you benchmark! 44 * don't play with this unless you benchmark!
35 * decompression is not dependent on the hash function 45 * decompression is not dependent on the hash function
36 * the hashing function might seem strange, just believe me 46 * the hashing function might seem strange, just believe me
37 * it works ;) 47 * it works ;)
38 */ 48 */
39#define FRST(p) (((p[0]) << 8) + p[1]) 49#define FRST(p) (((p[0]) << 8) | p[1])
40#define NEXT(v,p) (((v) << 8) + p[2]) 50#define NEXT(v,p) (((v) << 8) | p[2])
41#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))
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 sligthly better
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
67 */ 79 */
68 80
69unsigned int 81unsigned int
70lzf_compress (const void *const in_data, unsigned int in_len, 82lzf_compress (const void *const in_data, unsigned int in_len,
71 void *out_data, unsigned int out_len 83 void *out_data, unsigned int out_len
72#if !LZF_STATE_ARG 84#if LZF_STATE_ARG
73 , LZF_STATE *htab 85 , LZF_STATE *htab
74#endif 86#endif
75 ) 87 )
76{ 88{
77#if LZF_STATE_ARG 89#if !LZF_STATE_ARG
78 LZF_STATE htab; 90 LZF_STATE htab;
79#endif 91#endif
80 const u8 **hslot; 92 const u8 **hslot;
81 const u8 *ip = (const u8 *)in_data; 93 const u8 *ip = (const u8 *)in_data;
82 u8 *op = (u8 *)out_data; 94 u8 *op = (u8 *)out_data;
95 for (hslot = htab; hslot < htab + HSIZE; hslot++) 107 for (hslot = htab; hslot < htab + HSIZE; hslot++)
96 *hslot++ = ip; 108 *hslot++ = ip;
97# endif 109# endif
98#endif 110#endif
99 111
100 do 112 for (;;)
101 { 113 {
114 if (ip < in_end - 2)
115 {
102 hval = NEXT (hval, ip); 116 hval = NEXT (hval, ip);
103 hslot = htab + IDX (hval); 117 hslot = htab + IDX (hval);
104 ref = *hslot; *hslot = ip; 118 ref = *hslot; *hslot = ip;
105 119
106 if (1 120 if (1
107#if INIT_HTAB && !USE_MEMCPY 121#if INIT_HTAB && !USE_MEMCPY
108 && ref < ip /* the next test will actually take care of this, but it is faster */ 122 && ref < ip /* the next test will actually take care of this, but this is faster */
109#endif 123#endif
110 && (off = ip - ref - 1) < MAX_OFF 124 && (off = ip - ref - 1) < MAX_OFF
111 && ip + 4 < in_end 125 && ip + 4 < in_end
112 && ref > (u8 *)in_data 126 && ref > (u8 *)in_data
113#if STRICT_ALIGN 127#if STRICT_ALIGN
114 && ref[0] == ip[0] 128 && ref[0] == ip[0]
115 && ref[1] == ip[1] 129 && ref[1] == ip[1]
116 && ref[2] == ip[2] 130 && ref[2] == ip[2]
117#else 131#else
118 && *(u16 *)ref == *(u16 *)ip 132 && *(u16 *)ref == *(u16 *)ip
119 && ref[2] == ip[2] 133 && ref[2] == ip[2]
120#endif 134#endif
121 ) 135 )
122 { 136 {
123 /* match found at *ref++ */ 137 /* match found at *ref++ */
124 unsigned int len = 2; 138 unsigned int len = 2;
125 unsigned int maxlen = in_end - ip - len; 139 unsigned int maxlen = in_end - ip - len;
126 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 140 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
127 141
128 do 142 if (op + lit + 1 + 3 >= out_end)
129 len++; 143 return 0;
144
145 do
146 len++;
130 while (len < maxlen && ref[len] == ip[len]); 147 while (len < maxlen && ref[len] == ip[len]);
131 148
132 if (op + lit + 1 + 3 >= out_end) 149 if (lit)
133 return 0; 150 {
134
135 if (lit)
136 {
137 *op++ = lit - 1; 151 *op++ = lit - 1;
138 lit = -lit; 152 lit = -lit;
139 do 153 do
140 *op++ = ip[lit]; 154 *op++ = ip[lit];
141 while (++lit); 155 while (++lit);
142 } 156 }
143 157
144 len -= 2; 158 len -= 2;
145 ip++; 159 ip++;
146 160
147 if (len < 7) 161 if (len < 7)
148 { 162 {
149 *op++ = (off >> 8) + (len << 5); 163 *op++ = (off >> 8) + (len << 5);
150 } 164 }
151 else 165 else
152 { 166 {
153 *op++ = (off >> 8) + ( 7 << 5); 167 *op++ = (off >> 8) + ( 7 << 5);
154 *op++ = len - 7; 168 *op++ = len - 7;
155 } 169 }
156 170
157 *op++ = off; 171 *op++ = off;
158 172
159#if ULTRA_FAST 173#if ULTRA_FAST || VERY_FAST
160 ip += len; 174 ip += len;
175#if VERY_FAST && !ULTRA_FAST
176 --ip;
177#endif
161 hval = FRST (ip); 178 hval = FRST (ip);
179
162 hval = NEXT (hval, ip); 180 hval = NEXT (hval, ip);
163 htab[IDX (hval)] = ip; 181 htab[IDX (hval)] = ip;
164 ip++; 182 ip++;
183
184#if VERY_FAST && !ULTRA_FAST
185 hval = NEXT (hval, ip);
186 htab[IDX (hval)] = ip;
187 ip++;
188#endif
165#else 189#else
166 do 190 do
167 { 191 {
168 hval = NEXT (hval, ip); 192 hval = NEXT (hval, ip);
169 htab[IDX (hval)] = ip; 193 htab[IDX (hval)] = ip;
194 ip++;
195 }
196 while (len--);
197#endif
198 continue;
199 }
200 }
201 else if (ip == in_end)
202 break;
203
204 /* one more literal byte we must copy */
205 lit++;
170 ip++; 206 ip++;
171 }
172 while (len--);
173#endif
174 }
175 else
176 {
177 /* one more literal byte we must copy */
178 lit++;
179 ip++;
180 207
181 if (lit == MAX_LIT) 208 if (lit == MAX_LIT)
182 { 209 {
183 if (op + 1 + MAX_LIT >= out_end) 210 if (op + 1 + MAX_LIT >= out_end)
184 return 0; 211 return 0;
185 212
186 *op++ = MAX_LIT - 1; 213 *op++ = MAX_LIT - 1;
187#if USE_MEMCPY 214#if USE_MEMCPY
188 memcpy (op, ip - MAX_LIT, MAX_LIT); 215 memcpy (op, ip - MAX_LIT, MAX_LIT);
189 op += MAX_LIT; 216 op += MAX_LIT;
190 lit = 0; 217 lit = 0;
191#else 218#else
192 lit = -lit; 219 lit = -lit;
193 do 220 do
194 *op++ = ip[lit]; 221 *op++ = ip[lit];
195 while (++lit); 222 while (++lit);
196#endif 223#endif
197 } 224 }
198 }
199 } 225 }
200 while (ip < in_end);
201 226
202 if (lit) 227 if (lit)
203 { 228 {
204 if (op + lit + 1 >= out_end) 229 if (op + lit + 1 >= out_end)
205 return 0; 230 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines