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 X86_SSE2
12
13#include "../../zbuild.h"
14#include <immintrin.h>
15#include "../../deflate.h"
16#include "../../deflate_p.h"
17#include "../../functable.h"
18
19extern int read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
20void slide_hash_sse2(deflate_state *s);
21
22ZLIB_INTERNAL void fill_window_sse(deflate_state *s) {
23 register unsigned n;
24 unsigned more; /* Amount of free space at the end of the window. */
25 unsigned int wsize = s->w_size;
26
27 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
28
29 do {
30 more = (unsigned)(s->window_size -(unsigned long)s->lookahead -(unsigned long)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 memcpy(s->window, s->window+wsize, (unsigned)wsize);
50 s->match_start = (s->match_start >= wsize) ? s->match_start - wsize : 0;
51 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
52 s->block_start -= (long) wsize;
53
54 /* Slide the hash table (could be avoided with 32 bit values
55 at the expense of memory usage). We slide even when level == 0
56 to keep the hash table consistent if we switch back to level > 0
57 later. (Using level 0 permanently is not an optimal usage of
58 zlib, so we don't care about this pathological case.)
59 */
60 slide_hash_sse2(s);
61 more += wsize;
62 }
63 if (s->strm->avail_in == 0) break;
64
65 /* If there was no sliding:
66 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
67 * more == window_size - lookahead - strstart
68 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
69 * => more >= window_size - 2*WSIZE + 2
70 * In the BIG_MEM or MMAP case (not yet supported),
71 * window_size == input_size + MIN_LOOKAHEAD &&
72 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
73 * Otherwise, window_size == 2*WSIZE so more >= 2.
74 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
75 */
76 Assert(more >= 2, "more < 2");
77
78 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
79 s->lookahead += n;
80
81 /* Initialize the hash value now that we have some input: */
82 if (s->lookahead + s->insert >= MIN_MATCH) {
83 unsigned int str = s->strstart - s->insert;
84 s->ins_h = s->window[str];
85 if (str >= 1)
86 functable.insert_string(s, str + 2 - MIN_MATCH, 1);
87#if MIN_MATCH != 3
88#error Call insert_string() MIN_MATCH-3 more times
89 while (s->insert) {
90 functable.insert_string(s, str, 1);
91 str++;
92 s->insert--;
93 if (s->lookahead + s->insert < MIN_MATCH)
94 break;
95 }
96#else
97 unsigned int count;
98 if (UNLIKELY(s->lookahead == 1)){
99 count = s->insert - 1;
100 }else{
101 count = s->insert;
102 }
103 functable.insert_string(s, str, count);
104 s->insert -= count;
105#endif
106 }
107 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
108 * but this is not important since only literal bytes will be emitted.
109 */
110 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
111
112 /* If the WIN_INIT bytes after the end of the current data have never been
113 * written, then zero those bytes in order to avoid memory check reports of
114 * the use of uninitialized (or uninitialised as Julian writes) bytes by
115 * the longest match routines. Update the high water mark for the next
116 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
117 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
118 */
119 if (s->high_water < s->window_size) {
120 unsigned long curr = s->strstart + (unsigned long)(s->lookahead);
121 unsigned long init;
122
123 if (s->high_water < curr) {
124 /* Previous high water mark below current data -- zero WIN_INIT
125 * bytes or up to end of window, whichever is less.
126 */
127 init = s->window_size - curr;
128 if (init > WIN_INIT)
129 init = WIN_INIT;
130 memset(s->window + curr, 0, (unsigned)init);
131 s->high_water = curr + init;
132 } else if (s->high_water < (unsigned long)curr + WIN_INIT) {
133 /* High water mark at or above current data, but below current data
134 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
135 * to end of window, whichever is less.
136 */
137 init = (unsigned long)curr + WIN_INIT - s->high_water;
138 if (init > s->window_size - s->high_water)
139 init = s->window_size - s->high_water;
140 memset(s->window + s->high_water, 0, (unsigned)init);
141 s->high_water += init;
142 }
143 }
144
145 Assert((unsigned long)s->strstart <= s->window_size - MIN_LOOKAHEAD, "not enough room for search");
146}
147#endif
148