Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: third_party/zlib/deflate.c

Issue 552123005: Integrate SIMD optimisations for zlib (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix issues in fallback (non-SIMD) code Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* deflate.c -- compress data using the deflation algorithm 1 /* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler 2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6 /* 6 /*
7 * ALGORITHM 7 * ALGORITHM
8 * 8 *
9 * The "deflation" process depends on being able to identify portions 9 * The "deflation" process depends on being able to identify portions
10 * of the input text which are identical to earlier input (within a 10 * of the input text which are identical to earlier input (within a
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 local block_state deflate_stored OF((deflate_state *s, int flush, int clas)); 78 local block_state deflate_stored OF((deflate_state *s, int flush, int clas));
79 local block_state deflate_fast OF((deflate_state *s, int flush, int clas)); 79 local block_state deflate_fast OF((deflate_state *s, int flush, int clas));
80 #ifndef FASTEST 80 #ifndef FASTEST
81 local block_state deflate_slow OF((deflate_state *s, int flush, int clas)); 81 local block_state deflate_slow OF((deflate_state *s, int flush, int clas));
82 #endif 82 #endif
83 local block_state deflate_rle OF((deflate_state *s, int flush)); 83 local block_state deflate_rle OF((deflate_state *s, int flush));
84 local block_state deflate_huff OF((deflate_state *s, int flush)); 84 local block_state deflate_huff OF((deflate_state *s, int flush));
85 local void lm_init OF((deflate_state *s)); 85 local void lm_init OF((deflate_state *s));
86 local void putShortMSB OF((deflate_state *s, uInt b)); 86 local void putShortMSB OF((deflate_state *s, uInt b));
87 local void flush_pending OF((z_streamp strm)); 87 local void flush_pending OF((z_streamp strm));
88 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); 88 ZLIB_INTERNAL int read_buf OF((z_streamp strm, Bytef *buf, unsigned size) );
agl 2014/09/23 21:41:40 you need to adjust the spacing here so that the li
89 #ifdef ASMV 89 #ifdef ASMV
90 void match_init OF((void)); /* asm code initialization */ 90 void match_init OF((void)); /* asm code initialization */
91 uInt longest_match OF((deflate_state *s, IPos cur_match, int clas)); 91 uInt longest_match OF((deflate_state *s, IPos cur_match, int clas));
92 #else 92 #else
93 local uInt longest_match OF((deflate_state *s, IPos cur_match, int clas)); 93 local uInt longest_match OF((deflate_state *s, IPos cur_match, int clas));
94 #endif 94 #endif
95 95
96 #ifdef DEBUG 96 #ifdef DEBUG
97 local void check_match OF((deflate_state *s, IPos start, IPos match, 97 local void check_match OF((deflate_state *s, IPos start, IPos match,
98 int length)); 98 int length));
99 #endif 99 #endif
100 100
101 extern void crc_reset(deflate_state *const s);
102 extern void crc_finalize(deflate_state *const s);
103 extern void copy_with_crc(z_streamp strm, Bytef *dst, long size);
104
101 /* =========================================================================== 105 /* ===========================================================================
102 * Local data 106 * Local data
103 */ 107 */
104 108
105 #define NIL 0 109 #define NIL 0
106 /* Tail of hash chains */ 110 /* Tail of hash chains */
107 111
108 #ifndef TOO_FAR 112 #ifndef TOO_FAR
109 # define TOO_FAR 4096 113 # define TOO_FAR 4096
110 #endif 114 #endif
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 * meaning. 153 * meaning.
150 */ 154 */
151 155
152 #define EQUAL 0 156 #define EQUAL 0
153 /* result of memcmp for equal strings */ 157 /* result of memcmp for equal strings */
154 158
155 #ifndef NO_DUMMY_DECL 159 #ifndef NO_DUMMY_DECL
156 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ 160 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
157 #endif 161 #endif
158 162
159 /* ===========================================================================
160 * Update a hash value with the given input byte
161 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
162 * input characters, so that a running hash key can be computed from the
163 * previous key instead of complete recalculation each time.
164 */
165 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
166 163
164 #ifdef _MSC_VER
165 #define INLINE __inline
166 #else
167 #define INLINE inline
168 #endif
167 169
168 /* =========================================================================== 170 /* ===========================================================================
169 * Insert string str in the dictionary and set match_head to the previous head 171 * Insert string str in the dictionary and set match_head to the previous head
170 * of the hash chain (the most recent string with same hash key). Return 172 * of the hash chain (the most recent string with same hash key). Return
171 * the previous length of the hash chain. 173 * the previous length of the hash chain.
172 * If this file is compiled with -DFASTEST, the compression level is forced 174 * If this file is compiled with -DFASTEST, the compression level is forced
173 * to 1, and no hash chains are maintained. 175 * to 1, and no hash chains are maintained.
174 * IN assertion: all calls to to INSERT_STRING are made with consecutive 176 * IN assertion: all calls to to INSERT_STRING are made with consecutive
175 * input characters and the first MIN_MATCH bytes of str are valid 177 * input characters and the first MIN_MATCH bytes of str are valid
176 * (except for the last MIN_MATCH-1 bytes of the input file). 178 * (except for the last MIN_MATCH-1 bytes of the input file).
177 */ 179 */
180 #ifdef USE_SSE4_2_CRC_HASH
181 #include "x86.h"
182 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str)
183 {
184 Pos ret;
185 unsigned *ip, val, h = 0;
186
187 ip = (unsigned *)&s->window[str];
188 val = *ip;
189
190 if (s->level >= 6)
191 val &= 0xFFFFFF;
192
193 __asm__ __volatile__ (
194 "crc32 %1,%0\n\t"
195 : "+r" (h)
196 : "r" (val)
197 );
198
199 ret = s->head[h & s->hash_mask];
200 s->head[h & s->hash_mask] = str;
201 s->prev[str & s->w_mask] = ret;
202 return ret;
203 }
204 #endif
205
206 local INLINE Pos insert_string_c(deflate_state *const s, const Pos str)
207 {
208 Pos ret;
209
210 UPDATE_HASH(s, s->ins_h, str);
178 #ifdef FASTEST 211 #ifdef FASTEST
179 #define INSERT_STRING(s, str, match_head) \ 212 ret = s->head[s->ins_h];
180 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
181 match_head = s->head[s->ins_h], \
182 s->head[s->ins_h] = (Pos)(str))
183 #else 213 #else
184 #define INSERT_STRING(s, str, match_head) \ 214 ret = s->prev[str & s->w_mask] = s->head[s->ins_h];
185 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
186 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
187 s->head[s->ins_h] = (Pos)(str))
188 #endif 215 #endif
216 s->head[s->ins_h] = str;
217
218 return ret;
219 }
220
221 local INLINE Pos insert_string(deflate_state *const s, const Pos str)
222 {
223 #ifdef USE_SSE4_2_CRC_HASH
224 if (x86_cpu_has_sse42)
225 return insert_string_sse(s, str);
226 #endif
227 return insert_string_c(s, str);
228 }
229
189 230
190 /* =========================================================================== 231 /* ===========================================================================
191 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 232 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
192 * prev[] will be initialized on the fly. 233 * prev[] will be initialized on the fly.
193 */ 234 */
194 #define CLEAR_HASH(s) \ 235 #define CLEAR_HASH(s) \
195 s->head[s->hash_size-1] = NIL; \ 236 s->head[s->hash_size-1] = NIL; \
196 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 237 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
197 238
239 #ifdef CHECK_SSE2
240 #include "x86.h"
241 #endif
242
198 /* ========================================================================= */ 243 /* ========================================================================= */
199 int ZEXPORT deflateInit_(strm, level, version, stream_size) 244 int ZEXPORT deflateInit_(strm, level, version, stream_size)
200 z_streamp strm; 245 z_streamp strm;
201 int level; 246 int level;
202 const char *version; 247 const char *version;
203 int stream_size; 248 int stream_size;
204 { 249 {
205 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 250 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
206 Z_DEFAULT_STRATEGY, version, stream_size); 251 Z_DEFAULT_STRATEGY, version, stream_size);
207 /* To do: ignore strm->next_in if we use it as window */ 252 /* To do: ignore strm->next_in if we use it as window */
208 } 253 }
209 254
210 /* ========================================================================= */ 255 /* ========================================================================= */
211 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 256 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
212 version, stream_size) 257 version, stream_size)
213 z_streamp strm; 258 z_streamp strm;
214 int level; 259 int level;
215 int method; 260 int method;
216 int windowBits; 261 int windowBits;
217 int memLevel; 262 int memLevel;
218 int strategy; 263 int strategy;
219 const char *version; 264 const char *version;
220 int stream_size; 265 int stream_size;
221 { 266 {
267 unsigned window_padding = 0;
222 deflate_state *s; 268 deflate_state *s;
223 int wrap = 1; 269 int wrap = 1;
224 static const char my_version[] = ZLIB_VERSION; 270 static const char my_version[] = ZLIB_VERSION;
225 271
226 ushf *overlay; 272 ushf *overlay;
227 /* We overlay pending_buf and d_buf+l_buf. This works since the average 273 /* We overlay pending_buf and d_buf+l_buf. This works since the average
228 * output size for (length,distance) codes is <= 24 bits. 274 * output size for (length,distance) codes is <= 24 bits.
229 */ 275 */
230 276
277 #if defined(CHECK_SSE2) || defined(USE_SSE4_2_CRC_HASH)
278 x86_check_features();
279 #endif
280
231 if (version == Z_NULL || version[0] != my_version[0] || 281 if (version == Z_NULL || version[0] != my_version[0] ||
232 stream_size != sizeof(z_stream)) { 282 stream_size != sizeof(z_stream)) {
233 return Z_VERSION_ERROR; 283 return Z_VERSION_ERROR;
234 } 284 }
235 if (strm == Z_NULL) return Z_STREAM_ERROR; 285 if (strm == Z_NULL) return Z_STREAM_ERROR;
236 286
237 strm->msg = Z_NULL; 287 strm->msg = Z_NULL;
238 if (strm->zalloc == (alloc_func)0) { 288 if (strm->zalloc == (alloc_func)0) {
239 strm->zalloc = zcalloc; 289 strm->zalloc = zcalloc;
240 strm->opaque = (voidpf)0; 290 strm->opaque = (voidpf)0;
(...skipping 26 matching lines...) Expand all
267 if (s == Z_NULL) return Z_MEM_ERROR; 317 if (s == Z_NULL) return Z_MEM_ERROR;
268 strm->state = (struct internal_state FAR *)s; 318 strm->state = (struct internal_state FAR *)s;
269 s->strm = strm; 319 s->strm = strm;
270 320
271 s->wrap = wrap; 321 s->wrap = wrap;
272 s->gzhead = Z_NULL; 322 s->gzhead = Z_NULL;
273 s->w_bits = windowBits; 323 s->w_bits = windowBits;
274 s->w_size = 1 << s->w_bits; 324 s->w_size = 1 << s->w_bits;
275 s->w_mask = s->w_size - 1; 325 s->w_mask = s->w_size - 1;
276 326
327 #ifdef USE_SSE4_2_CRC_HASH
328 if (x86_cpu_has_sse42)
329 s->hash_bits = 15;
330 else
331 #endif
277 s->hash_bits = memLevel + 7; 332 s->hash_bits = memLevel + 7;
333
278 s->hash_size = 1 << s->hash_bits; 334 s->hash_size = 1 << s->hash_bits;
279 s->hash_mask = s->hash_size - 1; 335 s->hash_mask = s->hash_size - 1;
280 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 336 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
281 337
282 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 338 #ifdef HAVE_PCLMULQDQ
339 window_padding = 8;
340 #endif
341
342 s->window = (Bytef *) ZALLOC(strm, s->w_size + window_padding, 2*sizeof(Byte ));
283 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 343 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
284 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 344 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
285 s->class_bitmap = NULL; 345 s->class_bitmap = NULL;
286 zmemzero(&s->cookie_locations, sizeof(s->cookie_locations)); 346 zmemzero(&s->cookie_locations, sizeof(s->cookie_locations));
287 strm->clas = 0; 347 strm->clas = 0;
288 348
289 s->high_water = 0; /* nothing written to s->window yet */ 349 s->high_water = 0; /* nothing written to s->window yet */
290 350
291 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 351 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
292 352
(...skipping 20 matching lines...) Expand all
313 373
314 /* ========================================================================= */ 374 /* ========================================================================= */
315 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) 375 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
316 z_streamp strm; 376 z_streamp strm;
317 const Bytef *dictionary; 377 const Bytef *dictionary;
318 uInt dictLength; 378 uInt dictLength;
319 { 379 {
320 deflate_state *s; 380 deflate_state *s;
321 uInt length = dictLength; 381 uInt length = dictLength;
322 uInt n; 382 uInt n;
323 IPos hash_head = 0;
324 383
325 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || 384 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
326 strm->state->wrap == 2 || 385 strm->state->wrap == 2 ||
327 (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) 386 (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
328 return Z_STREAM_ERROR; 387 return Z_STREAM_ERROR;
329 388
330 s = strm->state; 389 s = strm->state;
331 if (s->wrap) 390 if (s->wrap)
332 strm->adler = adler32(strm->adler, dictionary, dictLength); 391 strm->adler = adler32(strm->adler, dictionary, dictLength);
333 392
334 if (length < MIN_MATCH) return Z_OK; 393 if (length < MIN_MATCH) return Z_OK;
335 if (length > s->w_size) { 394 if (length > s->w_size) {
336 length = s->w_size; 395 length = s->w_size;
337 dictionary += dictLength - length; /* use the tail of the dictionary */ 396 dictionary += dictLength - length; /* use the tail of the dictionary */
338 } 397 }
339 zmemcpy(s->window, dictionary, length); 398 zmemcpy(s->window, dictionary, length);
340 s->strstart = length; 399 s->strstart = length;
341 s->block_start = (long)length; 400 s->block_start = (long)length;
342 401
343 /* Insert all strings in the hash table (except for the last two bytes). 402 /* Insert all strings in the hash table (except for the last two bytes).
344 * s->lookahead stays null, so s->ins_h will be recomputed at the next 403 * s->lookahead stays null, so s->ins_h will be recomputed at the next
345 * call of fill_window. 404 * call of fill_window.
346 */ 405 */
347 s->ins_h = s->window[0]; 406 s->ins_h = s->window[0];
348 UPDATE_HASH(s, s->ins_h, s->window[1]); 407 UPDATE_HASH(s, s->ins_h, 2 - MIN_MATCH);
349 for (n = 0; n <= length - MIN_MATCH; n++) { 408 for (n = 0; n <= length - MIN_MATCH; n++) {
350 INSERT_STRING(s, n, hash_head); 409 insert_string(s, n);
351 } 410 }
352 if (hash_head) hash_head = 0; /* to make compiler happy */
353 return Z_OK; 411 return Z_OK;
354 } 412 }
355 413
356 /* ========================================================================= */ 414 /* ========================================================================= */
357 int ZEXPORT deflateReset (strm) 415 int ZEXPORT deflateReset (strm)
358 z_streamp strm; 416 z_streamp strm;
359 { 417 {
360 deflate_state *s; 418 deflate_state *s;
361 419
362 if (strm == Z_NULL || strm->state == Z_NULL || 420 if (strm == Z_NULL || strm->state == Z_NULL ||
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 664 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
607 665
608 s->strm = strm; /* just in case */ 666 s->strm = strm; /* just in case */
609 old_flush = s->last_flush; 667 old_flush = s->last_flush;
610 s->last_flush = flush; 668 s->last_flush = flush;
611 669
612 /* Write the header */ 670 /* Write the header */
613 if (s->status == INIT_STATE) { 671 if (s->status == INIT_STATE) {
614 #ifdef GZIP 672 #ifdef GZIP
615 if (s->wrap == 2) { 673 if (s->wrap == 2) {
616 strm->adler = crc32(0L, Z_NULL, 0); 674 crc_reset(s);
617 put_byte(s, 31); 675 put_byte(s, 31);
618 put_byte(s, 139); 676 put_byte(s, 139);
619 put_byte(s, 8); 677 put_byte(s, 8);
620 if (s->gzhead == Z_NULL) { 678 if (s->gzhead == Z_NULL) {
621 put_byte(s, 0); 679 put_byte(s, 0);
622 put_byte(s, 0); 680 put_byte(s, 0);
623 put_byte(s, 0); 681 put_byte(s, 0);
624 put_byte(s, 0); 682 put_byte(s, 0);
625 put_byte(s, 0); 683 put_byte(s, 0);
626 put_byte(s, s->level == 9 ? 2 : 684 put_byte(s, s->level == 9 ? 2 :
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 } 942 }
885 } 943 }
886 Assert(strm->avail_out > 0, "bug2"); 944 Assert(strm->avail_out > 0, "bug2");
887 945
888 if (flush != Z_FINISH) return Z_OK; 946 if (flush != Z_FINISH) return Z_OK;
889 if (s->wrap <= 0) return Z_STREAM_END; 947 if (s->wrap <= 0) return Z_STREAM_END;
890 948
891 /* Write the trailer */ 949 /* Write the trailer */
892 #ifdef GZIP 950 #ifdef GZIP
893 if (s->wrap == 2) { 951 if (s->wrap == 2) {
952 crc_finalize(s);
894 put_byte(s, (Byte)(strm->adler & 0xff)); 953 put_byte(s, (Byte)(strm->adler & 0xff));
895 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 954 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
896 put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); 955 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
897 put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); 956 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
898 put_byte(s, (Byte)(strm->total_in & 0xff)); 957 put_byte(s, (Byte)(strm->total_in & 0xff));
899 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); 958 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
900 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); 959 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
901 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); 960 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
902 } 961 }
903 else 962 else
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 #endif /* MAXSEG_64K */ 1065 #endif /* MAXSEG_64K */
1007 } 1066 }
1008 1067
1009 /* =========================================================================== 1068 /* ===========================================================================
1010 * Read a new buffer from the current input stream, update the adler32 1069 * Read a new buffer from the current input stream, update the adler32
1011 * and total number of bytes read. All deflate() input goes through 1070 * and total number of bytes read. All deflate() input goes through
1012 * this function so some applications may wish to modify it to avoid 1071 * this function so some applications may wish to modify it to avoid
1013 * allocating a large strm->next_in buffer and copying from it. 1072 * allocating a large strm->next_in buffer and copying from it.
1014 * (See also flush_pending()). 1073 * (See also flush_pending()).
1015 */ 1074 */
1016 local int read_buf(strm, buf, size) 1075 ZLIB_INTERNAL int read_buf(strm, buf, size)
1017 z_streamp strm; 1076 z_streamp strm;
1018 Bytef *buf; 1077 Bytef *buf;
1019 unsigned size; 1078 unsigned size;
1020 { 1079 {
1021 unsigned len = strm->avail_in; 1080 unsigned len = strm->avail_in;
1022 1081
1023 if (len > size) len = size; 1082 if (len > size) len = size;
1024 if (len == 0) return 0; 1083 if (len == 0) return 0;
1025 1084
1026 strm->avail_in -= len; 1085 strm->avail_in -= len;
1027 1086
1028 if (strm->state->wrap == 1) { 1087 #ifdef GZIP
1029 strm->adler = adler32(strm->adler, strm->next_in, len); 1088 if (strm->state->wrap == 2) {
1089 copy_with_crc(strm, buf, len);
1030 } 1090 }
1031 #ifdef GZIP 1091 else
1032 else if (strm->state->wrap == 2) { 1092 #endif
1033 strm->adler = crc32(strm->adler, strm->next_in, len); 1093 {
1094 zmemcpy(buf, strm->next_in, len);
1095 if (strm->state->wrap == 1)
1096 strm->adler = adler32(strm->adler, buf, len);
1034 } 1097 }
1035 #endif
1036 zmemcpy(buf, strm->next_in, len);
1037 strm->next_in += len; 1098 strm->next_in += len;
1038 strm->total_in += len; 1099 strm->total_in += len;
1039 1100
1040 return (int)len; 1101 return (int)len;
1041 } 1102 }
1042 1103
1043 /* =========================================================================== 1104 /* ===========================================================================
1044 * Initialize the "longest match" routines for a new zlib stream 1105 * Initialize the "longest match" routines for a new zlib stream
1045 */ 1106 */
1046 local void lm_init (s) 1107 local void lm_init (s)
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
1438 /* =========================================================================== 1499 /* ===========================================================================
1439 * Fill the window when the lookahead becomes insufficient. 1500 * Fill the window when the lookahead becomes insufficient.
1440 * Updates strstart and lookahead. 1501 * Updates strstart and lookahead.
1441 * 1502 *
1442 * IN assertion: lookahead < MIN_LOOKAHEAD 1503 * IN assertion: lookahead < MIN_LOOKAHEAD
1443 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 1504 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1444 * At least one byte has been read, or avail_in == 0; reads are 1505 * At least one byte has been read, or avail_in == 0; reads are
1445 * performed for at least two bytes (required for the zip translate_eol 1506 * performed for at least two bytes (required for the zip translate_eol
1446 * option -- not supported here). 1507 * option -- not supported here).
1447 */ 1508 */
1448 local void fill_window(s) 1509 #ifdef HAVE_SSE2
1510 extern void fill_window_sse(deflate_state *s);
1511 #endif
1512 local void fill_window_c(deflate_state *s);
1513
1514 local void fill_window(deflate_state *s)
1515 {
1516 #ifdef HAVE_SSE2
1517 #ifdef CHECK_SSE2
1518 if (x86_cpu_has_sse2) {
1519 #endif
1520 fill_window_sse(s);
1521 return;
1522 #ifdef CHECK_SSE2
1523 }
1524 #endif
1525 #endif
1526
1527 fill_window_c(s);
1528 }
1529
1530 local void fill_window_c(s)
1449 deflate_state *s; 1531 deflate_state *s;
1450 { 1532 {
1451 register unsigned n, m; 1533 register unsigned n;
1452 register Posf *p; 1534 register Posf *p;
1453 unsigned more; /* Amount of free space at the end of the window. */ 1535 unsigned more; /* Amount of free space at the end of the window. */
1454 uInt wsize = s->w_size; 1536 uInt wsize = s->w_size;
1455 1537
1456 do { 1538 do {
1457 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 1539 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1458 1540
1459 /* Deal with !@#$% 64K limit: */ 1541 /* Deal with !@#$% 64K limit: */
1460 if (sizeof(int) <= 2) { 1542 if (sizeof(int) <= 2) {
1461 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 1543 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
(...skipping 19 matching lines...) Expand all
1481 1563
1482 /* Slide the hash table (could be avoided with 32 bit values 1564 /* Slide the hash table (could be avoided with 32 bit values
1483 at the expense of memory usage). We slide even when level == 0 1565 at the expense of memory usage). We slide even when level == 0
1484 to keep the hash table consistent if we switch back to level > 0 1566 to keep the hash table consistent if we switch back to level > 0
1485 later. (Using level 0 permanently is not an optimal usage of 1567 later. (Using level 0 permanently is not an optimal usage of
1486 zlib, so we don't care about this pathological case.) 1568 zlib, so we don't care about this pathological case.)
1487 */ 1569 */
1488 n = s->hash_size; 1570 n = s->hash_size;
1489 p = &s->head[n]; 1571 p = &s->head[n];
1490 do { 1572 do {
1573 unsigned m;
1491 m = *--p; 1574 m = *--p;
1492 *p = (Pos)(m >= wsize ? m-wsize : NIL); 1575 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1493 } while (--n); 1576 } while (--n);
1494 1577
1495 n = wsize; 1578 n = wsize;
1496 #ifndef FASTEST 1579 #ifndef FASTEST
1497 p = &s->prev[n]; 1580 p = &s->prev[n];
1498 do { 1581 do {
1582 unsigned m;
1499 m = *--p; 1583 m = *--p;
1500 *p = (Pos)(m >= wsize ? m-wsize : NIL); 1584 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1501 /* If n is not on any hash chain, prev[n] is garbage but 1585 /* If n is not on any hash chain, prev[n] is garbage but
1502 * its value will never be used. 1586 * its value will never be used.
1503 */ 1587 */
1504 } while (--n); 1588 } while (--n);
1505 #endif 1589 #endif
1506 1590
1507 for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) { 1591 for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) {
1508 if (s->cookie_locations[n] > wsize) { 1592 if (s->cookie_locations[n] > wsize) {
(...skipping 28 matching lines...) Expand all
1537 1621
1538 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 1622 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1539 if (s->class_bitmap != NULL) { 1623 if (s->class_bitmap != NULL) {
1540 class_set(s, s->strstart + s->lookahead, n, s->strm->clas); 1624 class_set(s, s->strstart + s->lookahead, n, s->strm->clas);
1541 } 1625 }
1542 s->lookahead += n; 1626 s->lookahead += n;
1543 1627
1544 /* Initialize the hash value now that we have some input: */ 1628 /* Initialize the hash value now that we have some input: */
1545 if (s->lookahead >= MIN_MATCH) { 1629 if (s->lookahead >= MIN_MATCH) {
1546 s->ins_h = s->window[s->strstart]; 1630 s->ins_h = s->window[s->strstart];
1547 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1631 UPDATE_HASH(s, s->ins_h, s->strstart+1 - (MIN_MATCH - 1));
1548 #if MIN_MATCH != 3 1632 #if MIN_MATCH != 3
1549 Call UPDATE_HASH() MIN_MATCH-3 more times 1633 Call UPDATE_HASH() MIN_MATCH-3 more times
1550 #endif 1634 #endif
1551 } 1635 }
1552 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 1636 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1553 * but this is not important since only literal bytes will be emitted. 1637 * but this is not important since only literal bytes will be emitted.
1554 */ 1638 */
1555 1639
1556 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 1640 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1557 1641
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1704 return need_more; 1788 return need_more;
1705 } 1789 }
1706 if (s->lookahead == 0) break; /* flush the current block */ 1790 if (s->lookahead == 0) break; /* flush the current block */
1707 } 1791 }
1708 1792
1709 /* Insert the string window[strstart .. strstart+2] in the 1793 /* Insert the string window[strstart .. strstart+2] in the
1710 * dictionary, and set hash_head to the head of the hash chain: 1794 * dictionary, and set hash_head to the head of the hash chain:
1711 */ 1795 */
1712 hash_head = NIL; 1796 hash_head = NIL;
1713 if (s->lookahead >= MIN_MATCH) { 1797 if (s->lookahead >= MIN_MATCH) {
1714 INSERT_STRING(s, s->strstart, hash_head); 1798 hash_head = insert_string(s, s->strstart);
1715 } 1799 }
1716 1800
1717 /* Find the longest match, discarding those <= prev_length. 1801 /* Find the longest match, discarding those <= prev_length.
1718 * At this point we have always match_length < MIN_MATCH 1802 * At this point we have always match_length < MIN_MATCH
1719 */ 1803 */
1720 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 1804 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1721 /* To simplify the code, we prevent matches with the string 1805 /* To simplify the code, we prevent matches with the string
1722 * of window index 0 (in particular we have to avoid a match 1806 * of window index 0 (in particular we have to avoid a match
1723 * of the string with itself at the start of the input file). 1807 * of the string with itself at the start of the input file).
1724 */ 1808 */
(...skipping 10 matching lines...) Expand all
1735 1819
1736 /* Insert new strings in the hash table only if the match length 1820 /* Insert new strings in the hash table only if the match length
1737 * is not too large. This saves time but degrades compression. 1821 * is not too large. This saves time but degrades compression.
1738 */ 1822 */
1739 #ifndef FASTEST 1823 #ifndef FASTEST
1740 if (s->match_length <= s->max_insert_length && 1824 if (s->match_length <= s->max_insert_length &&
1741 s->lookahead >= MIN_MATCH) { 1825 s->lookahead >= MIN_MATCH) {
1742 s->match_length--; /* string at strstart already in table */ 1826 s->match_length--; /* string at strstart already in table */
1743 do { 1827 do {
1744 s->strstart++; 1828 s->strstart++;
1745 INSERT_STRING(s, s->strstart, hash_head); 1829 hash_head = insert_string(s, s->strstart);
1746 /* strstart never exceeds WSIZE-MAX_MATCH, so there are 1830 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1747 * always MIN_MATCH bytes ahead. 1831 * always MIN_MATCH bytes ahead.
1748 */ 1832 */
1749 } while (--s->match_length != 0); 1833 } while (--s->match_length != 0);
1750 s->strstart++; 1834 s->strstart++;
1751 } else 1835 } else
1752 #endif 1836 #endif
1753 { 1837 {
1754 s->strstart += s->match_length; 1838 s->strstart += s->match_length;
1755 s->match_length = 0; 1839 s->match_length = 0;
1756 s->ins_h = s->window[s->strstart]; 1840 s->ins_h = s->window[s->strstart];
1757 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1841 UPDATE_HASH(s, s->ins_h, s->strstart+2 - (MIN_MATCH));
1758 #if MIN_MATCH != 3 1842 #if MIN_MATCH != 3
1759 Call UPDATE_HASH() MIN_MATCH-3 more times 1843 Call UPDATE_HASH() MIN_MATCH-3 more times
1760 #endif 1844 #endif
1761 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1845 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1762 * matter since it will be recomputed at next deflate call. 1846 * matter since it will be recomputed at next deflate call.
1763 */ 1847 */
1764 } 1848 }
1765 } else { 1849 } else {
1766 /* No match, output a literal byte */ 1850 /* No match, output a literal byte */
1767 Tracevv((stderr,"%c", s->window[s->strstart])); 1851 Tracevv((stderr,"%c", s->window[s->strstart]));
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1814 return need_more; 1898 return need_more;
1815 } 1899 }
1816 if (s->lookahead == 0) break; /* flush the current block */ 1900 if (s->lookahead == 0) break; /* flush the current block */
1817 } 1901 }
1818 1902
1819 /* Insert the string window[strstart .. strstart+2] in the 1903 /* Insert the string window[strstart .. strstart+2] in the
1820 * dictionary, and set hash_head to the head of the hash chain: 1904 * dictionary, and set hash_head to the head of the hash chain:
1821 */ 1905 */
1822 hash_head = NIL; 1906 hash_head = NIL;
1823 if (s->lookahead >= MIN_MATCH) { 1907 if (s->lookahead >= MIN_MATCH) {
1824 INSERT_STRING(s, s->strstart, hash_head); 1908 hash_head = insert_string(s, s->strstart);
1825 } 1909 }
1826 1910
1827 /* Find the longest match, discarding those <= prev_length. 1911 /* Find the longest match, discarding those <= prev_length.
1828 */ 1912 */
1829 s->prev_length = s->match_length, s->prev_match = s->match_start; 1913 s->prev_length = s->match_length, s->prev_match = s->match_start;
1830 s->match_length = MIN_MATCH-1; 1914 s->match_length = MIN_MATCH-1;
1831 1915
1832 if (clas == Z_CLASS_COOKIE && first) { 1916 if (clas == Z_CLASS_COOKIE && first) {
1833 s->match_length = cookie_match(s, s->strstart, input_length); 1917 s->match_length = cookie_match(s, s->strstart, input_length);
1834 } else if (clas == Z_CLASS_STANDARD && 1918 } else if (clas == Z_CLASS_STANDARD &&
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1883 1967
1884 /* Insert in hash table all strings up to the end of the match. 1968 /* Insert in hash table all strings up to the end of the match.
1885 * strstart-1 and strstart are already inserted. If there is not 1969 * strstart-1 and strstart are already inserted. If there is not
1886 * enough lookahead, the last two strings are not inserted in 1970 * enough lookahead, the last two strings are not inserted in
1887 * the hash table. 1971 * the hash table.
1888 */ 1972 */
1889 s->lookahead -= s->prev_length-1; 1973 s->lookahead -= s->prev_length-1;
1890 s->prev_length -= 2; 1974 s->prev_length -= 2;
1891 do { 1975 do {
1892 if (++s->strstart <= max_insert) { 1976 if (++s->strstart <= max_insert) {
1893 INSERT_STRING(s, s->strstart, hash_head); 1977 hash_head = insert_string(s, s->strstart);
1894 } 1978 }
1895 } while (--s->prev_length != 0); 1979 } while (--s->prev_length != 0);
1896 s->match_available = 0; 1980 s->match_available = 0;
1897 s->match_length = MIN_MATCH-1; 1981 s->match_length = MIN_MATCH-1;
1898 s->strstart++; 1982 s->strstart++;
1899 1983
1900 if (bflush) FLUSH_BLOCK(s, 0); 1984 if (bflush) FLUSH_BLOCK(s, 0);
1901 1985
1902 } else if (s->match_available) { 1986 } else if (s->match_available) {
1903 /* If there was no match at the previous position, output a 1987 /* If there was no match at the previous position, output a
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
2024 s->match_length = 0; 2108 s->match_length = 0;
2025 Tracevv((stderr,"%c", s->window[s->strstart])); 2109 Tracevv((stderr,"%c", s->window[s->strstart]));
2026 _tr_tally_lit (s, s->window[s->strstart], bflush); 2110 _tr_tally_lit (s, s->window[s->strstart], bflush);
2027 s->lookahead--; 2111 s->lookahead--;
2028 s->strstart++; 2112 s->strstart++;
2029 if (bflush) FLUSH_BLOCK(s, 0); 2113 if (bflush) FLUSH_BLOCK(s, 0);
2030 } 2114 }
2031 FLUSH_BLOCK(s, flush == Z_FINISH); 2115 FLUSH_BLOCK(s, flush == Z_FINISH);
2032 return flush == Z_FINISH ? finish_done : block_done; 2116 return flush == Z_FINISH ? finish_done : block_done;
2033 } 2117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698