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 #include <immintrin.h> |
| 12 #include "deflate.h" |
| 13 |
| 14 #define UPDATE_HASH(s,h,i) \ |
| 15 {\ |
| 16 if (s->level < 6) { \ |
| 17 h = (3483 * (s->window[i]) +\ |
| 18 23081* (s->window[i+1]) +\ |
| 19 6954 * (s->window[i+2]) +\ |
| 20 20947* (s->window[i+3])) & s->hash_mask;\ |
| 21 } else {\ |
| 22 h = (25881* (s->window[i]) +\ |
| 23 24674* (s->window[i+1]) +\ |
| 24 25811* (s->window[i+2])) & s->hash_mask;\ |
| 25 }\ |
| 26 }\ |
| 27 |
| 28 extern int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); |
| 29 |
| 30 void fill_window_sse(deflate_state *s) |
| 31 { |
| 32 const __m128i xmm_wsize = _mm_set1_epi16(s->w_size); |
| 33 |
| 34 register unsigned n; |
| 35 register Posf *p; |
| 36 unsigned more; /* Amount of free space at the end of the window. */ |
| 37 uInt wsize = s->w_size; |
| 38 |
| 39 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); |
| 40 |
| 41 do { |
| 42 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
| 43 |
| 44 /* Deal with !@#$% 64K limit: */ |
| 45 if (sizeof(int) <= 2) { |
| 46 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
| 47 more = wsize; |
| 48 |
| 49 } else if (more == (unsigned)(-1)) { |
| 50 /* Very unlikely, but possible on 16 bit machine if |
| 51 * strstart == 0 && lookahead == 1 (input done a byte at time) |
| 52 */ |
| 53 more--; |
| 54 } |
| 55 } |
| 56 |
| 57 /* If the window is almost full and there is insufficient lookahead, |
| 58 * move the upper half to the lower one to make room in the upper half. |
| 59 */ |
| 60 if (s->strstart >= wsize+MAX_DIST(s)) { |
| 61 |
| 62 zmemcpy(s->window, s->window+wsize, (unsigned)wsize); |
| 63 s->match_start -= wsize; |
| 64 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ |
| 65 s->block_start -= (long) wsize; |
| 66 |
| 67 /* Slide the hash table (could be avoided with 32 bit values |
| 68 at the expense of memory usage). We slide even when level == 0 |
| 69 to keep the hash table consistent if we switch back to level > 0 |
| 70 later. (Using level 0 permanently is not an optimal usage of |
| 71 zlib, so we don't care about this pathological case.) |
| 72 */ |
| 73 n = s->hash_size; |
| 74 p = &s->head[n]; |
| 75 p -= 8; |
| 76 do { |
| 77 __m128i value, result; |
| 78 |
| 79 value = _mm_loadu_si128((__m128i *)p); |
| 80 result = _mm_subs_epu16(value, xmm_wsize); |
| 81 _mm_storeu_si128((__m128i *)p, result); |
| 82 |
| 83 p -= 8; |
| 84 n -= 8; |
| 85 } while (n > 0); |
| 86 |
| 87 n = wsize; |
| 88 #ifndef FASTEST |
| 89 p = &s->prev[n]; |
| 90 p -= 8; |
| 91 do { |
| 92 __m128i value, result; |
| 93 |
| 94 value = _mm_loadu_si128((__m128i *)p); |
| 95 result = _mm_subs_epu16(value, xmm_wsize); |
| 96 _mm_storeu_si128((__m128i *)p, result); |
| 97 |
| 98 p -= 8; |
| 99 n -= 8; |
| 100 } while (n > 0); |
| 101 #endif |
| 102 more += wsize; |
| 103 } |
| 104 if (s->strm->avail_in == 0) break; |
| 105 |
| 106 /* If there was no sliding: |
| 107 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && |
| 108 * more == window_size - lookahead - strstart |
| 109 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) |
| 110 * => more >= window_size - 2*WSIZE + 2 |
| 111 * In the BIG_MEM or MMAP case (not yet supported), |
| 112 * window_size == input_size + MIN_LOOKAHEAD && |
| 113 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. |
| 114 * Otherwise, window_size == 2*WSIZE so more >= 2. |
| 115 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. |
| 116 */ |
| 117 Assert(more >= 2, "more < 2"); |
| 118 |
| 119 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); |
| 120 s->lookahead += n; |
| 121 |
| 122 /* Initialize the hash value now that we have some input: */ |
| 123 if (s->lookahead >= MIN_MATCH) { |
| 124 uInt str = s->strstart; |
| 125 s->ins_h = s->window[str]; |
| 126 if (str >= 1) |
| 127 UPDATE_HASH(s, s->ins_h, str + 1 - (MIN_MATCH-1)); |
| 128 #if MIN_MATCH != 3 |
| 129 Call UPDATE_HASH() MIN_MATCH-3 more times |
| 130 #endif |
| 131 } |
| 132 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, |
| 133 * but this is not important since only literal bytes will be emitted. |
| 134 */ |
| 135 |
| 136 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
| 137 |
| 138 /* If the WIN_INIT bytes after the end of the current data have never been |
| 139 * written, then zero those bytes in order to avoid memory check reports of |
| 140 * the use of uninitialized (or uninitialised as Julian writes) bytes by |
| 141 * the longest match routines. Update the high water mark for the next |
| 142 * time through here. WIN_INIT is set to MAX_MATCH since the longest match |
| 143 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. |
| 144 */ |
| 145 if (s->high_water < s->window_size) { |
| 146 ulg curr = s->strstart + (ulg)(s->lookahead); |
| 147 ulg init; |
| 148 |
| 149 if (s->high_water < curr) { |
| 150 /* Previous high water mark below current data -- zero WIN_INIT |
| 151 * bytes or up to end of window, whichever is less. |
| 152 */ |
| 153 init = s->window_size - curr; |
| 154 if (init > WIN_INIT) |
| 155 init = WIN_INIT; |
| 156 zmemzero(s->window + curr, (unsigned)init); |
| 157 s->high_water = curr + init; |
| 158 } |
| 159 else if (s->high_water < (ulg)curr + WIN_INIT) { |
| 160 /* High water mark at or above current data, but below current data |
| 161 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up |
| 162 * to end of window, whichever is less. |
| 163 */ |
| 164 init = (ulg)curr + WIN_INIT - s->high_water; |
| 165 if (init > s->window_size - s->high_water) |
| 166 init = s->window_size - s->high_water; |
| 167 zmemzero(s->window + s->high_water, (unsigned)init); |
| 168 s->high_water += init; |
| 169 } |
| 170 } |
| 171 |
| 172 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, |
| 173 "not enough room for search"); |
| 174 } |
| 175 |
| 176 ZLIB_INTERNAL Pos insert_string_sse(deflate_state *const s, const Pos str) |
| 177 { |
| 178 Pos ret; |
| 179 unsigned *ip, val, h = 0; |
| 180 |
| 181 ip = (unsigned *)&s->window[str]; |
| 182 val = *ip; |
| 183 |
| 184 if (s->level >= 6) |
| 185 val &= 0xFFFFFF; |
| 186 |
| 187 #ifndef _MSC_VER |
| 188 __asm__ __volatile__ ( |
| 189 "crc32 %1,%0\n\t" |
| 190 : "+r" (h) |
| 191 : "r" (val) |
| 192 ); |
| 193 #else |
| 194 h = _mm_crc32_u32(h, val); |
| 195 #endif |
| 196 |
| 197 ret = s->head[h & s->hash_mask]; |
| 198 s->head[h & s->hash_mask] = str; |
| 199 s->prev[str & s->w_mask] = ret; |
| 200 return ret; |
| 201 } |
OLD | NEW |