OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Fill Window with SSE2-optimized hash shifting |
| 3 * |
| 4 * Copyright (C) 2013 Intel Corporation |
| 5 * Authors: |
| 6 * Arjan van de Ven <arjan@linux.intel.com> |
| 7 * Jim Kukunas <james.t.kukunas@linux.intel.com> |
| 8 * |
| 9 * For conditions of distribution and use, see copyright notice in zlib.h |
| 10 */ |
| 11 #ifdef HAVE_SSE2 |
| 12 |
| 13 #include <immintrin.h> |
| 14 #include "deflate.h" |
| 15 |
| 16 extern int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); |
| 17 |
| 18 void fill_window_sse(deflate_state *s) |
| 19 { |
| 20 const __m128i xmm_wsize = _mm_set1_epi16(s->w_size); |
| 21 |
| 22 register unsigned n; |
| 23 register Posf *p; |
| 24 unsigned more; /* Amount of free space at the end of the window. */ |
| 25 uInt wsize = s->w_size; |
| 26 |
| 27 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); |
| 28 |
| 29 do { |
| 30 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
| 31 |
| 32 /* Deal with !@#$% 64K limit: */ |
| 33 if (sizeof(int) <= 2) { |
| 34 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
| 35 more = wsize; |
| 36 |
| 37 } else if (more == (unsigned)(-1)) { |
| 38 /* Very unlikely, but possible on 16 bit machine if |
| 39 * strstart == 0 && lookahead == 1 (input done a byte at time) |
| 40 */ |
| 41 more--; |
| 42 } |
| 43 } |
| 44 |
| 45 /* If the window is almost full and there is insufficient lookahead, |
| 46 * move the upper half to the lower one to make room in the upper half. |
| 47 */ |
| 48 if (s->strstart >= wsize+MAX_DIST(s)) { |
| 49 |
| 50 zmemcpy(s->window, s->window+wsize, (unsigned)wsize); |
| 51 s->match_start -= wsize; |
| 52 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ |
| 53 s->block_start -= (long) wsize; |
| 54 |
| 55 /* Slide the hash table (could be avoided with 32 bit values |
| 56 at the expense of memory usage). We slide even when level == 0 |
| 57 to keep the hash table consistent if we switch back to level > 0 |
| 58 later. (Using level 0 permanently is not an optimal usage of |
| 59 zlib, so we don't care about this pathological case.) |
| 60 */ |
| 61 n = s->hash_size; |
| 62 p = &s->head[n]; |
| 63 p -= 8; |
| 64 do { |
| 65 __m128i value, result; |
| 66 |
| 67 value = _mm_loadu_si128((__m128i *)p); |
| 68 result = _mm_subs_epu16(value, xmm_wsize); |
| 69 _mm_storeu_si128((__m128i *)p, result); |
| 70 |
| 71 p -= 8; |
| 72 n -= 8; |
| 73 } while (n > 0); |
| 74 |
| 75 n = wsize; |
| 76 #ifndef FASTEST |
| 77 p = &s->prev[n]; |
| 78 p -= 8; |
| 79 do { |
| 80 __m128i value, result; |
| 81 |
| 82 value = _mm_loadu_si128((__m128i *)p); |
| 83 result = _mm_subs_epu16(value, xmm_wsize); |
| 84 _mm_storeu_si128((__m128i *)p, result); |
| 85 |
| 86 p -= 8; |
| 87 n -= 8; |
| 88 } while (n > 0); |
| 89 #endif |
| 90 more += wsize; |
| 91 } |
| 92 if (s->strm->avail_in == 0) break; |
| 93 |
| 94 /* If there was no sliding: |
| 95 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && |
| 96 * more == window_size - lookahead - strstart |
| 97 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) |
| 98 * => more >= window_size - 2*WSIZE + 2 |
| 99 * In the BIG_MEM or MMAP case (not yet supported), |
| 100 * window_size == input_size + MIN_LOOKAHEAD && |
| 101 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. |
| 102 * Otherwise, window_size == 2*WSIZE so more >= 2. |
| 103 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. |
| 104 */ |
| 105 Assert(more >= 2, "more < 2"); |
| 106 |
| 107 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); |
| 108 s->lookahead += n; |
| 109 |
| 110 /* Initialize the hash value now that we have some input: */ |
| 111 if (s->lookahead >= MIN_MATCH) { |
| 112 uInt str = s->strstart; |
| 113 s->ins_h = s->window[str]; |
| 114 if (str >= 1) |
| 115 UPDATE_HASH(s, s->ins_h, str + 1 - (MIN_MATCH-1)); |
| 116 #if MIN_MATCH != 3 |
| 117 Call UPDATE_HASH() MIN_MATCH-3 more times |
| 118 #endif |
| 119 } |
| 120 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, |
| 121 * but this is not important since only literal bytes will be emitted. |
| 122 */ |
| 123 |
| 124 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
| 125 |
| 126 /* If the WIN_INIT bytes after the end of the current data have never been |
| 127 * written, then zero those bytes in order to avoid memory check reports of |
| 128 * the use of uninitialized (or uninitialised as Julian writes) bytes by |
| 129 * the longest match routines. Update the high water mark for the next |
| 130 * time through here. WIN_INIT is set to MAX_MATCH since the longest match |
| 131 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. |
| 132 */ |
| 133 if (s->high_water < s->window_size) { |
| 134 ulg curr = s->strstart + (ulg)(s->lookahead); |
| 135 ulg init; |
| 136 |
| 137 if (s->high_water < curr) { |
| 138 /* Previous high water mark below current data -- zero WIN_INIT |
| 139 * bytes or up to end of window, whichever is less. |
| 140 */ |
| 141 init = s->window_size - curr; |
| 142 if (init > WIN_INIT) |
| 143 init = WIN_INIT; |
| 144 zmemzero(s->window + curr, (unsigned)init); |
| 145 s->high_water = curr + init; |
| 146 } |
| 147 else if (s->high_water < (ulg)curr + WIN_INIT) { |
| 148 /* High water mark at or above current data, but below current data |
| 149 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up |
| 150 * to end of window, whichever is less. |
| 151 */ |
| 152 init = (ulg)curr + WIN_INIT - s->high_water; |
| 153 if (init > s->window_size - s->high_water) |
| 154 init = s->window_size - s->high_water; |
| 155 zmemzero(s->window + s->high_water, (unsigned)init); |
| 156 s->high_water += init; |
| 157 } |
| 158 } |
| 159 |
| 160 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, |
| 161 "not enough room for search"); |
| 162 } |
| 163 #endif |
OLD | NEW |