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.8 by root, Thu Mar 3 17:06:44 2005 UTC vs.
Revision 1.15 by root, Fri Nov 2 12:39:20 2007 UTC

1/* 1/*
2 * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> 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-
44 * don't play with this unless you benchmark! 41 * don't play with this unless you benchmark!
45 * decompression is not dependent on the hash function 42 * decompression is not dependent on the hash function
46 * the hashing function might seem strange, just believe me 43 * the hashing function might seem strange, just believe me
47 * it works ;) 44 * it works ;)
48 */ 45 */
46#ifndef FRST
49#define FRST(p) (((p[0]) << 8) + p[1]) 47# define FRST(p) (((p[0]) << 8) | p[1])
50#define NEXT(v,p) (((v) << 8) + p[2]) 48# define NEXT(v,p) (((v) << 8) | p[2])
51#define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (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
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)) & (HSIZE - 1)) 54 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
55 * the latter is also quite fast on newer CPUs, and sligthly better 55 * the latter is also quite fast on newer CPUs, and compresses similarly.
56 * 56 *
57 * the next one is also quite good, albeit slow ;) 57 * the next one is also quite good, albeit slow ;)
58 * (int)(cos(h & 0xffffff) * 1e6) 58 * (int)(cos(h & 0xffffff) * 1e6)
59 */ 59 */
60 60
65# define IDX(h) ((h) & (HSIZE - 1)) 65# define IDX(h) ((h) & (HSIZE - 1))
66#endif 66#endif
67 67
68#define MAX_LIT (1 << 5) 68#define MAX_LIT (1 << 5)
69#define MAX_OFF (1 << 13) 69#define MAX_OFF (1 << 13)
70#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
71 78
72/* 79/*
73 * compressed format 80 * compressed format
74 * 81 *
75 * 000LLLLL <L+1> ; literal 82 * 000LLLLL <L+1> ; literal
76 * LLLOOOOO oooooooo ; backref L 83 * LLLooooo oooooooo ; backref L
77 * 111OOOOO LLLLLLLL oooooooo ; backref L+7 84 * 111ooooo LLLLLLLL oooooooo ; backref L+7
78 * 85 *
79 */ 86 */
80 87
81unsigned int 88unsigned int
82lzf_compress (const void *const in_data, unsigned int in_len, 89lzf_compress (const void *const in_data, unsigned int in_len,
83 void *out_data, unsigned int out_len 90 void *out_data, unsigned int out_len
84#if LZF_STATE_ARG 91#if LZF_STATE_ARG
85 , LZF_STATE *htab 92 , LZF_STATE htab
86#endif 93#endif
87 ) 94 )
88{ 95{
89#if !LZF_STATE_ARG 96#if !LZF_STATE_ARG
90 LZF_STATE htab; 97 LZF_STATE htab;
99 unsigned int hval = FRST (ip); 106 unsigned int hval = FRST (ip);
100 unsigned long off; 107 unsigned long off;
101 int lit = 0; 108 int lit = 0;
102 109
103#if INIT_HTAB 110#if INIT_HTAB
104# if USE_MEMCPY
105 memset (htab, 0, sizeof (htab)); 111 memset (htab, 0, sizeof (htab));
106# else 112# if 0
107 for (hslot = htab; hslot < htab + HSIZE; hslot++) 113 for (hslot = htab; hslot < htab + HSIZE; hslot++)
108 *hslot++ = ip; 114 *hslot++ = ip;
109# endif 115# endif
110#endif 116#endif
111 117
112 for (;;) 118 for (;;)
113 { 119 {
137 /* match found at *ref++ */ 143 /* match found at *ref++ */
138 unsigned int len = 2; 144 unsigned int len = 2;
139 unsigned int maxlen = in_end - ip - len; 145 unsigned int maxlen = in_end - ip - len;
140 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 146 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
141 147
148 if (op + lit + 1 + 3 >= out_end)
149 return 0;
150
142 do 151 do
143 len++; 152 len++;
144 while (len < maxlen && ref[len] == ip[len]); 153 while (len < maxlen && ref[len] == ip[len]);
145
146 if (op + lit + 1 + 3 >= out_end)
147 return 0;
148 154
149 if (lit) 155 if (lit)
150 { 156 {
151 *op++ = lit - 1; 157 *op++ = lit - 1;
152 lit = -lit; 158 lit = -lit;
168 *op++ = len - 7; 174 *op++ = len - 7;
169 } 175 }
170 176
171 *op++ = off; 177 *op++ = off;
172 178
173#if ULTRA_FAST 179#if ULTRA_FAST || VERY_FAST
174 ip += len; 180 ip += len;
181#if VERY_FAST && !ULTRA_FAST
182 --ip;
183#endif
175 hval = FRST (ip); 184 hval = FRST (ip);
185
176 hval = NEXT (hval, ip); 186 hval = NEXT (hval, ip);
177 htab[IDX (hval)] = ip; 187 htab[IDX (hval)] = ip;
178 ip++; 188 ip++;
189
190#if VERY_FAST && !ULTRA_FAST
191 hval = NEXT (hval, ip);
192 htab[IDX (hval)] = ip;
193 ip++;
194#endif
179#else 195#else
180 do 196 do
181 { 197 {
182 hval = NEXT (hval, ip); 198 hval = NEXT (hval, ip);
183 htab[IDX (hval)] = ip; 199 htab[IDX (hval)] = ip;
199 { 215 {
200 if (op + 1 + MAX_LIT >= out_end) 216 if (op + 1 + MAX_LIT >= out_end)
201 return 0; 217 return 0;
202 218
203 *op++ = MAX_LIT - 1; 219 *op++ = MAX_LIT - 1;
204#if USE_MEMCPY 220
205 memcpy (op, ip - MAX_LIT, MAX_LIT); 221#ifdef lzf_movsb
206 op += MAX_LIT; 222 ip -= lit;
207 lit = 0; 223 lzf_movsb (op, ip, lit);
208#else 224#else
209 lit = -lit; 225 lit = -lit;
210 do 226 do
211 *op++ = ip[lit]; 227 *op++ = ip[lit];
212 while (++lit); 228 while (++lit);
218 { 234 {
219 if (op + lit + 1 >= out_end) 235 if (op + lit + 1 >= out_end)
220 return 0; 236 return 0;
221 237
222 *op++ = lit - 1; 238 *op++ = lit - 1;
239#ifdef lzf_movsb
240 ip -= lit;
241 lzf_movsb (op, ip, lit);
242#else
223 lit = -lit; 243 lit = -lit;
224 do 244 do
225 *op++ = ip[lit]; 245 *op++ = ip[lit];
226 while (++lit); 246 while (++lit);
247#endif
227 } 248 }
228 249
229 return op - (u8 *) out_data; 250 return op - (u8 *) out_data;
230} 251}
252

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines