OLD | NEW |
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 * A description of the Rabin and Karp algorithm is given in the book | 42 * A description of the Rabin and Karp algorithm is given in the book |
43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. | 43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. |
44 * | 44 * |
45 * Fiala,E.R., and Greene,D.H. | 45 * Fiala,E.R., and Greene,D.H. |
46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 | 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 |
47 * | 47 * |
48 */ | 48 */ |
49 | 49 |
50 /* @(#) $Id$ */ | 50 /* @(#) $Id$ */ |
51 | 51 |
52 #include <assert.h> | |
53 | |
54 #include "deflate.h" | 52 #include "deflate.h" |
55 #include "x86.h" | |
56 | 53 |
57 const char deflate_copyright[] = | 54 const char deflate_copyright[] = |
58 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; | 55 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; |
59 /* | 56 /* |
60 If you use the zlib library in a product, an acknowledgment is welcome | 57 If you use the zlib library in a product, an acknowledgment is welcome |
61 in the documentation of your product. If for some reason you cannot | 58 in the documentation of your product. If for some reason you cannot |
62 include such an acknowledgment, I would appreciate that you keep this | 59 include such an acknowledgment, I would appreciate that you keep this |
63 copyright string in the executable of your product. | 60 copyright string in the executable of your product. |
64 */ | 61 */ |
65 | 62 |
(...skipping 15 matching lines...) Expand all Loading... |
81 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)); |
82 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)); |
83 #ifndef FASTEST | 80 #ifndef FASTEST |
84 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)); |
85 #endif | 82 #endif |
86 local block_state deflate_rle OF((deflate_state *s, int flush)); | 83 local block_state deflate_rle OF((deflate_state *s, int flush)); |
87 local block_state deflate_huff OF((deflate_state *s, int flush)); | 84 local block_state deflate_huff OF((deflate_state *s, int flush)); |
88 local void lm_init OF((deflate_state *s)); | 85 local void lm_init OF((deflate_state *s)); |
89 local void putShortMSB OF((deflate_state *s, uInt b)); | 86 local void putShortMSB OF((deflate_state *s, uInt b)); |
90 local void flush_pending OF((z_streamp strm)); | 87 local void flush_pending OF((z_streamp strm)); |
91 | 88 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); |
92 #ifdef ASMV | 89 #ifdef ASMV |
93 void match_init OF((void)); /* asm code initialization */ | 90 void match_init OF((void)); /* asm code initialization */ |
94 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)); |
95 #else | 92 #else |
96 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)); |
97 #endif | 94 #endif |
98 | 95 |
99 #ifdef DEBUG | 96 #ifdef DEBUG |
100 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, |
101 int length)); | 98 int length)); |
102 #endif | 99 #endif |
103 | 100 |
104 /* For fill_window_sse.c to use */ | |
105 ZLIB_INTERNAL int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); | |
106 | |
107 /* From crc32.c */ | |
108 extern void ZLIB_INTERNAL crc_reset(deflate_state *const s); | |
109 extern void ZLIB_INTERNAL crc_finalize(deflate_state *const s); | |
110 extern void ZLIB_INTERNAL copy_with_crc(z_streamp strm, Bytef *dst, long size); | |
111 | |
112 #ifdef _MSC_VER | |
113 #define INLINE __inline | |
114 #else | |
115 #define INLINE inline | |
116 #endif | |
117 | |
118 /* Inline optimisation */ | |
119 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str); | |
120 | |
121 /* =========================================================================== | 101 /* =========================================================================== |
122 * Local data | 102 * Local data |
123 */ | 103 */ |
124 | 104 |
125 #define NIL 0 | 105 #define NIL 0 |
126 /* Tail of hash chains */ | 106 /* Tail of hash chains */ |
127 | 107 |
128 #ifndef TOO_FAR | 108 #ifndef TOO_FAR |
129 # define TOO_FAR 4096 | 109 # define TOO_FAR 4096 |
130 #endif | 110 #endif |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 #endif | 157 #endif |
178 | 158 |
179 /* =========================================================================== | 159 /* =========================================================================== |
180 * Update a hash value with the given input byte | 160 * Update a hash value with the given input byte |
181 * IN assertion: all calls to to UPDATE_HASH are made with consecutive | 161 * IN assertion: all calls to to UPDATE_HASH are made with consecutive |
182 * input characters, so that a running hash key can be computed from the | 162 * input characters, so that a running hash key can be computed from the |
183 * previous key instead of complete recalculation each time. | 163 * previous key instead of complete recalculation each time. |
184 */ | 164 */ |
185 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) | 165 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) |
186 | 166 |
| 167 |
187 /* =========================================================================== | 168 /* =========================================================================== |
188 * Insert string str in the dictionary and set match_head to the previous head | 169 * Insert string str in the dictionary and set match_head to the previous head |
189 * of the hash chain (the most recent string with same hash key). Return | 170 * of the hash chain (the most recent string with same hash key). Return |
190 * the previous length of the hash chain. | 171 * the previous length of the hash chain. |
191 * If this file is compiled with -DFASTEST, the compression level is forced | 172 * If this file is compiled with -DFASTEST, the compression level is forced |
192 * to 1, and no hash chains are maintained. | 173 * to 1, and no hash chains are maintained. |
193 * IN assertion: all calls to to INSERT_STRING are made with consecutive | 174 * IN assertion: all calls to to INSERT_STRING are made with consecutive |
194 * input characters and the first MIN_MATCH bytes of str are valid | 175 * input characters and the first MIN_MATCH bytes of str are valid |
195 * (except for the last MIN_MATCH-1 bytes of the input file). | 176 * (except for the last MIN_MATCH-1 bytes of the input file). |
196 */ | 177 */ |
197 local INLINE Pos insert_string_c(deflate_state *const s, const Pos str) | |
198 { | |
199 Pos ret; | |
200 | |
201 UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]); | |
202 #ifdef FASTEST | 178 #ifdef FASTEST |
203 ret = s->head[s->ins_h]; | 179 #define INSERT_STRING(s, str, match_head) \ |
| 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)) |
204 #else | 183 #else |
205 ret = s->prev[str & s->w_mask] = s->head[s->ins_h]; | 184 #define INSERT_STRING(s, str, match_head) \ |
| 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)) |
206 #endif | 188 #endif |
207 s->head[s->ins_h] = str; | |
208 | |
209 return ret; | |
210 } | |
211 | |
212 local INLINE Pos insert_string(deflate_state *const s, const Pos str) | |
213 { | |
214 if (x86_cpu_enable_simd) | |
215 return insert_string_sse(s, str); | |
216 return insert_string_c(s, str); | |
217 } | |
218 | |
219 | 189 |
220 /* =========================================================================== | 190 /* =========================================================================== |
221 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). | 191 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). |
222 * prev[] will be initialized on the fly. | 192 * prev[] will be initialized on the fly. |
223 */ | 193 */ |
224 #define CLEAR_HASH(s) \ | 194 #define CLEAR_HASH(s) \ |
225 s->head[s->hash_size-1] = NIL; \ | 195 s->head[s->hash_size-1] = NIL; \ |
226 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); | 196 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); |
227 | 197 |
228 /* ========================================================================= */ | 198 /* ========================================================================= */ |
(...skipping 13 matching lines...) Expand all Loading... |
242 version, stream_size) | 212 version, stream_size) |
243 z_streamp strm; | 213 z_streamp strm; |
244 int level; | 214 int level; |
245 int method; | 215 int method; |
246 int windowBits; | 216 int windowBits; |
247 int memLevel; | 217 int memLevel; |
248 int strategy; | 218 int strategy; |
249 const char *version; | 219 const char *version; |
250 int stream_size; | 220 int stream_size; |
251 { | 221 { |
252 unsigned window_padding = 8; | |
253 deflate_state *s; | 222 deflate_state *s; |
254 int wrap = 1; | 223 int wrap = 1; |
255 static const char my_version[] = ZLIB_VERSION; | 224 static const char my_version[] = ZLIB_VERSION; |
256 | 225 |
257 ushf *overlay; | 226 ushf *overlay; |
258 /* We overlay pending_buf and d_buf+l_buf. This works since the average | 227 /* We overlay pending_buf and d_buf+l_buf. This works since the average |
259 * output size for (length,distance) codes is <= 24 bits. | 228 * output size for (length,distance) codes is <= 24 bits. |
260 */ | 229 */ |
261 | 230 |
262 x86_check_features(); | |
263 | |
264 if (version == Z_NULL || version[0] != my_version[0] || | 231 if (version == Z_NULL || version[0] != my_version[0] || |
265 stream_size != sizeof(z_stream)) { | 232 stream_size != sizeof(z_stream)) { |
266 return Z_VERSION_ERROR; | 233 return Z_VERSION_ERROR; |
267 } | 234 } |
268 if (strm == Z_NULL) return Z_STREAM_ERROR; | 235 if (strm == Z_NULL) return Z_STREAM_ERROR; |
269 | 236 |
270 strm->msg = Z_NULL; | 237 strm->msg = Z_NULL; |
271 if (strm->zalloc == (alloc_func)0) { | 238 if (strm->zalloc == (alloc_func)0) { |
272 strm->zalloc = zcalloc; | 239 strm->zalloc = zcalloc; |
273 strm->opaque = (voidpf)0; | 240 strm->opaque = (voidpf)0; |
(...skipping 26 matching lines...) Expand all Loading... |
300 if (s == Z_NULL) return Z_MEM_ERROR; | 267 if (s == Z_NULL) return Z_MEM_ERROR; |
301 strm->state = (struct internal_state FAR *)s; | 268 strm->state = (struct internal_state FAR *)s; |
302 s->strm = strm; | 269 s->strm = strm; |
303 | 270 |
304 s->wrap = wrap; | 271 s->wrap = wrap; |
305 s->gzhead = Z_NULL; | 272 s->gzhead = Z_NULL; |
306 s->w_bits = windowBits; | 273 s->w_bits = windowBits; |
307 s->w_size = 1 << s->w_bits; | 274 s->w_size = 1 << s->w_bits; |
308 s->w_mask = s->w_size - 1; | 275 s->w_mask = s->w_size - 1; |
309 | 276 |
310 if (x86_cpu_enable_simd) { | 277 s->hash_bits = memLevel + 7; |
311 s->hash_bits = 15; | |
312 } else { | |
313 s->hash_bits = memLevel + 7; | |
314 } | |
315 | |
316 s->hash_size = 1 << s->hash_bits; | 278 s->hash_size = 1 << s->hash_bits; |
317 s->hash_mask = s->hash_size - 1; | 279 s->hash_mask = s->hash_size - 1; |
318 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); | 280 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); |
319 | 281 |
320 s->window = (Bytef *) ZALLOC(strm, s->w_size + window_padding, 2*sizeof(Byte
)); | 282 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); |
321 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); | 283 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); |
322 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); | 284 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); |
323 s->class_bitmap = NULL; | 285 s->class_bitmap = NULL; |
324 zmemzero(&s->cookie_locations, sizeof(s->cookie_locations)); | 286 zmemzero(&s->cookie_locations, sizeof(s->cookie_locations)); |
325 strm->clas = 0; | 287 strm->clas = 0; |
326 | 288 |
327 s->high_water = 0; /* nothing written to s->window yet */ | 289 s->high_water = 0; /* nothing written to s->window yet */ |
328 | 290 |
329 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ | 291 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ |
330 | 292 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 s->strstart = length; | 340 s->strstart = length; |
379 s->block_start = (long)length; | 341 s->block_start = (long)length; |
380 | 342 |
381 /* Insert all strings in the hash table (except for the last two bytes). | 343 /* Insert all strings in the hash table (except for the last two bytes). |
382 * s->lookahead stays null, so s->ins_h will be recomputed at the next | 344 * s->lookahead stays null, so s->ins_h will be recomputed at the next |
383 * call of fill_window. | 345 * call of fill_window. |
384 */ | 346 */ |
385 s->ins_h = s->window[0]; | 347 s->ins_h = s->window[0]; |
386 UPDATE_HASH(s, s->ins_h, s->window[1]); | 348 UPDATE_HASH(s, s->ins_h, s->window[1]); |
387 for (n = 0; n <= length - MIN_MATCH; n++) { | 349 for (n = 0; n <= length - MIN_MATCH; n++) { |
388 insert_string(s, n); | 350 INSERT_STRING(s, n, hash_head); |
389 } | 351 } |
390 if (hash_head) hash_head = 0; /* to make compiler happy */ | 352 if (hash_head) hash_head = 0; /* to make compiler happy */ |
391 return Z_OK; | 353 return Z_OK; |
392 } | 354 } |
393 | 355 |
394 /* ========================================================================= */ | 356 /* ========================================================================= */ |
395 int ZEXPORT deflateReset (strm) | 357 int ZEXPORT deflateReset (strm) |
396 z_streamp strm; | 358 z_streamp strm; |
397 { | 359 { |
398 deflate_state *s; | 360 deflate_state *s; |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); | 606 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); |
645 | 607 |
646 s->strm = strm; /* just in case */ | 608 s->strm = strm; /* just in case */ |
647 old_flush = s->last_flush; | 609 old_flush = s->last_flush; |
648 s->last_flush = flush; | 610 s->last_flush = flush; |
649 | 611 |
650 /* Write the header */ | 612 /* Write the header */ |
651 if (s->status == INIT_STATE) { | 613 if (s->status == INIT_STATE) { |
652 #ifdef GZIP | 614 #ifdef GZIP |
653 if (s->wrap == 2) { | 615 if (s->wrap == 2) { |
654 crc_reset(s); | 616 strm->adler = crc32(0L, Z_NULL, 0); |
655 put_byte(s, 31); | 617 put_byte(s, 31); |
656 put_byte(s, 139); | 618 put_byte(s, 139); |
657 put_byte(s, 8); | 619 put_byte(s, 8); |
658 if (s->gzhead == Z_NULL) { | 620 if (s->gzhead == Z_NULL) { |
659 put_byte(s, 0); | 621 put_byte(s, 0); |
660 put_byte(s, 0); | 622 put_byte(s, 0); |
661 put_byte(s, 0); | 623 put_byte(s, 0); |
662 put_byte(s, 0); | 624 put_byte(s, 0); |
663 put_byte(s, 0); | 625 put_byte(s, 0); |
664 put_byte(s, s->level == 9 ? 2 : | 626 put_byte(s, s->level == 9 ? 2 : |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 } | 884 } |
923 } | 885 } |
924 Assert(strm->avail_out > 0, "bug2"); | 886 Assert(strm->avail_out > 0, "bug2"); |
925 | 887 |
926 if (flush != Z_FINISH) return Z_OK; | 888 if (flush != Z_FINISH) return Z_OK; |
927 if (s->wrap <= 0) return Z_STREAM_END; | 889 if (s->wrap <= 0) return Z_STREAM_END; |
928 | 890 |
929 /* Write the trailer */ | 891 /* Write the trailer */ |
930 #ifdef GZIP | 892 #ifdef GZIP |
931 if (s->wrap == 2) { | 893 if (s->wrap == 2) { |
932 crc_finalize(s); | |
933 put_byte(s, (Byte)(strm->adler & 0xff)); | 894 put_byte(s, (Byte)(strm->adler & 0xff)); |
934 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); | 895 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); |
935 put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); | 896 put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); |
936 put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); | 897 put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); |
937 put_byte(s, (Byte)(strm->total_in & 0xff)); | 898 put_byte(s, (Byte)(strm->total_in & 0xff)); |
938 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); | 899 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); |
939 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); | 900 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); |
940 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); | 901 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); |
941 } | 902 } |
942 else | 903 else |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 #endif /* MAXSEG_64K */ | 1006 #endif /* MAXSEG_64K */ |
1046 } | 1007 } |
1047 | 1008 |
1048 /* =========================================================================== | 1009 /* =========================================================================== |
1049 * Read a new buffer from the current input stream, update the adler32 | 1010 * Read a new buffer from the current input stream, update the adler32 |
1050 * and total number of bytes read. All deflate() input goes through | 1011 * and total number of bytes read. All deflate() input goes through |
1051 * this function so some applications may wish to modify it to avoid | 1012 * this function so some applications may wish to modify it to avoid |
1052 * allocating a large strm->next_in buffer and copying from it. | 1013 * allocating a large strm->next_in buffer and copying from it. |
1053 * (See also flush_pending()). | 1014 * (See also flush_pending()). |
1054 */ | 1015 */ |
1055 ZLIB_INTERNAL int read_buf(strm, buf, size) | 1016 local int read_buf(strm, buf, size) |
1056 z_streamp strm; | 1017 z_streamp strm; |
1057 Bytef *buf; | 1018 Bytef *buf; |
1058 unsigned size; | 1019 unsigned size; |
1059 { | 1020 { |
1060 unsigned len = strm->avail_in; | 1021 unsigned len = strm->avail_in; |
1061 | 1022 |
1062 if (len > size) len = size; | 1023 if (len > size) len = size; |
1063 if (len == 0) return 0; | 1024 if (len == 0) return 0; |
1064 | 1025 |
1065 strm->avail_in -= len; | 1026 strm->avail_in -= len; |
1066 | 1027 |
| 1028 if (strm->state->wrap == 1) { |
| 1029 strm->adler = adler32(strm->adler, strm->next_in, len); |
| 1030 } |
1067 #ifdef GZIP | 1031 #ifdef GZIP |
1068 if (strm->state->wrap == 2) { | 1032 else if (strm->state->wrap == 2) { |
1069 copy_with_crc(strm, buf, len); | 1033 strm->adler = crc32(strm->adler, strm->next_in, len); |
1070 } | 1034 } |
1071 else | |
1072 #endif | 1035 #endif |
1073 { | 1036 zmemcpy(buf, strm->next_in, len); |
1074 zmemcpy(buf, strm->next_in, len); | |
1075 if (strm->state->wrap == 1) | |
1076 strm->adler = adler32(strm->adler, buf, len); | |
1077 } | |
1078 strm->next_in += len; | 1037 strm->next_in += len; |
1079 strm->total_in += len; | 1038 strm->total_in += len; |
1080 | 1039 |
1081 return (int)len; | 1040 return (int)len; |
1082 } | 1041 } |
1083 | 1042 |
1084 /* =========================================================================== | 1043 /* =========================================================================== |
1085 * Initialize the "longest match" routines for a new zlib stream | 1044 * Initialize the "longest match" routines for a new zlib stream |
1086 */ | 1045 */ |
1087 local void lm_init (s) | 1046 local void lm_init (s) |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1479 /* =========================================================================== | 1438 /* =========================================================================== |
1480 * Fill the window when the lookahead becomes insufficient. | 1439 * Fill the window when the lookahead becomes insufficient. |
1481 * Updates strstart and lookahead. | 1440 * Updates strstart and lookahead. |
1482 * | 1441 * |
1483 * IN assertion: lookahead < MIN_LOOKAHEAD | 1442 * IN assertion: lookahead < MIN_LOOKAHEAD |
1484 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD | 1443 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD |
1485 * At least one byte has been read, or avail_in == 0; reads are | 1444 * At least one byte has been read, or avail_in == 0; reads are |
1486 * performed for at least two bytes (required for the zip translate_eol | 1445 * performed for at least two bytes (required for the zip translate_eol |
1487 * option -- not supported here). | 1446 * option -- not supported here). |
1488 */ | 1447 */ |
1489 local void fill_window_c(deflate_state *s); | 1448 local void fill_window(s) |
1490 | |
1491 local void fill_window(deflate_state *s) | |
1492 { | |
1493 if (x86_cpu_enable_simd) { | |
1494 fill_window_sse(s); | |
1495 return; | |
1496 } | |
1497 | |
1498 fill_window_c(s); | |
1499 } | |
1500 | |
1501 local void fill_window_c(s) | |
1502 deflate_state *s; | 1449 deflate_state *s; |
1503 { | 1450 { |
1504 register unsigned n, m; | 1451 register unsigned n, m; |
1505 register Posf *p; | 1452 register Posf *p; |
1506 unsigned more; /* Amount of free space at the end of the window. */ | 1453 unsigned more; /* Amount of free space at the end of the window. */ |
1507 uInt wsize = s->w_size; | 1454 uInt wsize = s->w_size; |
1508 | 1455 |
1509 do { | 1456 do { |
1510 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); | 1457 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
1511 | 1458 |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1757 return need_more; | 1704 return need_more; |
1758 } | 1705 } |
1759 if (s->lookahead == 0) break; /* flush the current block */ | 1706 if (s->lookahead == 0) break; /* flush the current block */ |
1760 } | 1707 } |
1761 | 1708 |
1762 /* Insert the string window[strstart .. strstart+2] in the | 1709 /* Insert the string window[strstart .. strstart+2] in the |
1763 * dictionary, and set hash_head to the head of the hash chain: | 1710 * dictionary, and set hash_head to the head of the hash chain: |
1764 */ | 1711 */ |
1765 hash_head = NIL; | 1712 hash_head = NIL; |
1766 if (s->lookahead >= MIN_MATCH) { | 1713 if (s->lookahead >= MIN_MATCH) { |
1767 hash_head = insert_string(s, s->strstart); | 1714 INSERT_STRING(s, s->strstart, hash_head); |
1768 } | 1715 } |
1769 | 1716 |
1770 /* Find the longest match, discarding those <= prev_length. | 1717 /* Find the longest match, discarding those <= prev_length. |
1771 * At this point we have always match_length < MIN_MATCH | 1718 * At this point we have always match_length < MIN_MATCH |
1772 */ | 1719 */ |
1773 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { | 1720 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { |
1774 /* To simplify the code, we prevent matches with the string | 1721 /* To simplify the code, we prevent matches with the string |
1775 * of window index 0 (in particular we have to avoid a match | 1722 * of window index 0 (in particular we have to avoid a match |
1776 * of the string with itself at the start of the input file). | 1723 * of the string with itself at the start of the input file). |
1777 */ | 1724 */ |
(...skipping 10 matching lines...) Expand all Loading... |
1788 | 1735 |
1789 /* Insert new strings in the hash table only if the match length | 1736 /* Insert new strings in the hash table only if the match length |
1790 * is not too large. This saves time but degrades compression. | 1737 * is not too large. This saves time but degrades compression. |
1791 */ | 1738 */ |
1792 #ifndef FASTEST | 1739 #ifndef FASTEST |
1793 if (s->match_length <= s->max_insert_length && | 1740 if (s->match_length <= s->max_insert_length && |
1794 s->lookahead >= MIN_MATCH) { | 1741 s->lookahead >= MIN_MATCH) { |
1795 s->match_length--; /* string at strstart already in table */ | 1742 s->match_length--; /* string at strstart already in table */ |
1796 do { | 1743 do { |
1797 s->strstart++; | 1744 s->strstart++; |
1798 hash_head = insert_string(s, s->strstart); | 1745 INSERT_STRING(s, s->strstart, hash_head); |
1799 /* strstart never exceeds WSIZE-MAX_MATCH, so there are | 1746 /* strstart never exceeds WSIZE-MAX_MATCH, so there are |
1800 * always MIN_MATCH bytes ahead. | 1747 * always MIN_MATCH bytes ahead. |
1801 */ | 1748 */ |
1802 } while (--s->match_length != 0); | 1749 } while (--s->match_length != 0); |
1803 s->strstart++; | 1750 s->strstart++; |
1804 } else | 1751 } else |
1805 #endif | 1752 #endif |
1806 { | 1753 { |
1807 s->strstart += s->match_length; | 1754 s->strstart += s->match_length; |
1808 s->match_length = 0; | 1755 s->match_length = 0; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1867 return need_more; | 1814 return need_more; |
1868 } | 1815 } |
1869 if (s->lookahead == 0) break; /* flush the current block */ | 1816 if (s->lookahead == 0) break; /* flush the current block */ |
1870 } | 1817 } |
1871 | 1818 |
1872 /* Insert the string window[strstart .. strstart+2] in the | 1819 /* Insert the string window[strstart .. strstart+2] in the |
1873 * dictionary, and set hash_head to the head of the hash chain: | 1820 * dictionary, and set hash_head to the head of the hash chain: |
1874 */ | 1821 */ |
1875 hash_head = NIL; | 1822 hash_head = NIL; |
1876 if (s->lookahead >= MIN_MATCH) { | 1823 if (s->lookahead >= MIN_MATCH) { |
1877 hash_head = insert_string(s, s->strstart); | 1824 INSERT_STRING(s, s->strstart, hash_head); |
1878 } | 1825 } |
1879 | 1826 |
1880 /* Find the longest match, discarding those <= prev_length. | 1827 /* Find the longest match, discarding those <= prev_length. |
1881 */ | 1828 */ |
1882 s->prev_length = s->match_length, s->prev_match = s->match_start; | 1829 s->prev_length = s->match_length, s->prev_match = s->match_start; |
1883 s->match_length = MIN_MATCH-1; | 1830 s->match_length = MIN_MATCH-1; |
1884 | 1831 |
1885 if (clas == Z_CLASS_COOKIE && first) { | 1832 if (clas == Z_CLASS_COOKIE && first) { |
1886 s->match_length = cookie_match(s, s->strstart, input_length); | 1833 s->match_length = cookie_match(s, s->strstart, input_length); |
1887 } else if (clas == Z_CLASS_STANDARD && | 1834 } else if (clas == Z_CLASS_STANDARD && |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1936 | 1883 |
1937 /* Insert in hash table all strings up to the end of the match. | 1884 /* Insert in hash table all strings up to the end of the match. |
1938 * strstart-1 and strstart are already inserted. If there is not | 1885 * strstart-1 and strstart are already inserted. If there is not |
1939 * enough lookahead, the last two strings are not inserted in | 1886 * enough lookahead, the last two strings are not inserted in |
1940 * the hash table. | 1887 * the hash table. |
1941 */ | 1888 */ |
1942 s->lookahead -= s->prev_length-1; | 1889 s->lookahead -= s->prev_length-1; |
1943 s->prev_length -= 2; | 1890 s->prev_length -= 2; |
1944 do { | 1891 do { |
1945 if (++s->strstart <= max_insert) { | 1892 if (++s->strstart <= max_insert) { |
1946 hash_head = insert_string(s, s->strstart); | 1893 INSERT_STRING(s, s->strstart, hash_head); |
1947 } | 1894 } |
1948 } while (--s->prev_length != 0); | 1895 } while (--s->prev_length != 0); |
1949 s->match_available = 0; | 1896 s->match_available = 0; |
1950 s->match_length = MIN_MATCH-1; | 1897 s->match_length = MIN_MATCH-1; |
1951 s->strstart++; | 1898 s->strstart++; |
1952 | 1899 |
1953 if (bflush) FLUSH_BLOCK(s, 0); | 1900 if (bflush) FLUSH_BLOCK(s, 0); |
1954 | 1901 |
1955 } else if (s->match_available) { | 1902 } else if (s->match_available) { |
1956 /* If there was no match at the previous position, output a | 1903 /* If there was no match at the previous position, output a |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2077 s->match_length = 0; | 2024 s->match_length = 0; |
2078 Tracevv((stderr,"%c", s->window[s->strstart])); | 2025 Tracevv((stderr,"%c", s->window[s->strstart])); |
2079 _tr_tally_lit (s, s->window[s->strstart], bflush); | 2026 _tr_tally_lit (s, s->window[s->strstart], bflush); |
2080 s->lookahead--; | 2027 s->lookahead--; |
2081 s->strstart++; | 2028 s->strstart++; |
2082 if (bflush) FLUSH_BLOCK(s, 0); | 2029 if (bflush) FLUSH_BLOCK(s, 0); |
2083 } | 2030 } |
2084 FLUSH_BLOCK(s, flush == Z_FINISH); | 2031 FLUSH_BLOCK(s, flush == Z_FINISH); |
2085 return flush == Z_FINISH ? finish_done : block_done; | 2032 return flush == Z_FINISH ? finish_done : block_done; |
2086 } | 2033 } |
2087 | |
2088 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will | |
2089 * use intrinsic without extra params | |
2090 */ | |
2091 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str) | |
2092 { | |
2093 Pos ret; | |
2094 unsigned *ip, val, h = 0; | |
2095 | |
2096 ip = (unsigned *)&s->window[str]; | |
2097 val = *ip; | |
2098 | |
2099 if (s->level >= 6) | |
2100 val &= 0xFFFFFF; | |
2101 | |
2102 /* Windows clang should use inline asm */ | |
2103 #if defined(_MSC_VER) && !defined(__clang__) | |
2104 h = _mm_crc32_u32(h, val); | |
2105 #elif defined(__i386__) || defined(__amd64__) | |
2106 __asm__ __volatile__ ( | |
2107 "crc32 %1,%0\n\t" | |
2108 : "+r" (h) | |
2109 : "r" (val) | |
2110 ); | |
2111 #else | |
2112 /* This should never happen */ | |
2113 assert(0); | |
2114 #endif | |
2115 | |
2116 ret = s->head[h & s->hash_mask]; | |
2117 s->head[h & s->hash_mask] = str; | |
2118 s->prev[str & s->w_mask] = ret; | |
2119 return ret; | |
2120 } | |
OLD | NEW |