4 * Common code for Lempel-Ziv matchfinding.
6 * Copyright (c) 2014 Eric Biggers. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "wimlib/types.h"
38 #ifndef MATCHFINDER_WINDOW_ORDER
39 # error "MATCHFINDER_WINDOW_ORDER must be defined!"
42 #ifndef MATCHFINDER_IS_SLIDING
43 # error "MATCHFINDER_IS_SLIDING must be defined!"
46 #define MATCHFINDER_WINDOW_SIZE ((size_t)1 << MATCHFINDER_WINDOW_ORDER)
48 #if MATCHFINDER_IS_SLIDING
49 # include "matchfinder_sliding.h"
51 # include "matchfinder_nonsliding.h"
54 #define MATCHFINDER_ALIGNMENT 8
57 # include "matchfinder_avx2.h"
58 # if MATCHFINDER_ALIGNMENT < 32
59 # undef MATCHFINDER_ALIGNMENT
60 # define MATCHFINDER_ALIGNMENT 32
65 # include "matchfinder_sse2.h"
66 # if MATCHFINDER_ALIGNMENT < 16
67 # undef MATCHFINDER_ALIGNMENT
68 # define MATCHFINDER_ALIGNMENT 16
73 * Representation of a match.
77 /* The number of bytes matched. */
80 /* The offset back from the current position that was matched. */
85 matchfinder_memset_init_okay(void)
87 /* All bytes must match in order to use memset. */
88 const pos_t v = MATCHFINDER_INITVAL;
89 if (sizeof(pos_t) == 2)
90 return (u8)v == (u8)(v >> 8);
91 if (sizeof(pos_t) == 4)
92 return (u8)v == (u8)(v >> 8) &&
93 (u8)v == (u8)(v >> 16) &&
94 (u8)v == (u8)(v >> 24);
99 * Initialize the hash table portion of the matchfinder.
101 * Essentially, this is an optimized memset().
103 * 'data' must be aligned to a MATCHFINDER_ALIGNMENT boundary.
106 matchfinder_init(pos_t *data, size_t num_entries)
108 const size_t size = num_entries * sizeof(data[0]);
111 if (matchfinder_init_avx2(data, size))
116 if (matchfinder_init_sse2(data, size))
120 if (matchfinder_memset_init_okay()) {
121 memset(data, (u8)MATCHFINDER_INITVAL, size);
125 for (size_t i = 0; i < num_entries; i++)
126 data[i] = MATCHFINDER_INITVAL;
129 #if MATCHFINDER_IS_SLIDING
131 * Slide the matchfinder by WINDOW_SIZE bytes.
133 * This must be called just after each WINDOW_SIZE bytes have been run through
136 * This will subtract WINDOW_SIZE bytes from each entry in the array specified.
137 * The effect is that all entries are updated to be relative to the current
138 * position, rather than the position WINDOW_SIZE bytes prior.
140 * Underflow is detected and replaced with signed saturation. This ensures that
141 * once the sliding window has passed over a position, that position forever
142 * remains out of bounds.
144 * The array passed in must contain all matchfinder data that is
145 * position-relative. Concretely, this will include the hash table as well as
146 * the table of positions that is used to link together the sequences in each
147 * hash bucket. Note that in the latter table, the links are 1-ary in the case
148 * of "hash chains", and 2-ary in the case of "binary trees". In either case,
149 * the links need to be rebased in the same way.
152 matchfinder_rebase(pos_t *data, size_t num_entries)
154 const size_t size = num_entries * sizeof(data[0]);
157 if (matchfinder_rebase_avx2(data, size))
162 if (matchfinder_rebase_sse2(data, size))
166 if (MATCHFINDER_WINDOW_SIZE == 32768) {
167 /* Branchless version for 32768 byte windows. If the value was
168 * already negative, clear all bits except the sign bit; this
169 * changes the value to -32768. Otherwise, set the sign bit;
170 * this is equivalent to subtracting 32768. */
171 for (size_t i = 0; i < num_entries; i++) {
173 u16 sign_bit = v & 0x8000;
174 v &= sign_bit - ((sign_bit >> 15) ^ 1);
181 for (size_t i = 0; i < num_entries; i++) {
183 data[i] -= (pos_t)-MATCHFINDER_WINDOW_SIZE;
185 data[i] = (pos_t)-MATCHFINDER_WINDOW_SIZE;
188 #endif /* MATCHFINDER_IS_SLIDING */