4 * Fast match extension for Lempel-Ziv matchfinding.
6 * The author dedicates this file to the public domain.
7 * You can do whatever you want with this file.
10 #ifndef _WIMLIB_LZ_EXTEND_H
11 #define _WIMLIB_LZ_EXTEND_H
13 #include "wimlib/types.h"
15 #if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
16 # define HAVE_FAST_LZ_EXTEND 1
18 # define HAVE_FAST_LZ_EXTEND 0
21 /* Return the number of bytes at @matchptr that match the bytes at @strptr, up
22 * to a maximum of @max_len. Initially, @start_len bytes are matched. */
24 lz_extend(const u8 * const strptr, const u8 * const matchptr,
25 const u32 start_len, const u32 max_len)
29 #if HAVE_FAST_LZ_EXTEND
31 while (len + sizeof(unsigned long) <= max_len) {
34 x = *(const unsigned long *)&matchptr[len] ^
35 *(const unsigned long *)&strptr[len];
37 return len + (__builtin_ctzl(x) >> 3);
38 len += sizeof(unsigned long);
41 if (sizeof(unsigned int) < sizeof(unsigned long) &&
42 len + sizeof(unsigned int) <= max_len)
46 x = *(const unsigned int *)&matchptr[len] ^
47 *(const unsigned int *)&strptr[len];
49 return len + (__builtin_ctz(x) >> 3);
50 len += sizeof(unsigned int);
53 if (sizeof(unsigned int) == 4) {
54 if (len < max_len && matchptr[len] == strptr[len]) {
56 if (len < max_len && matchptr[len] == strptr[len]) {
58 if (len < max_len && matchptr[len] == strptr[len]) {
66 #endif /* HAVE_FAST_LZ_EXTEND */
68 while (len < max_len && matchptr[len] == strptr[len])
74 #endif /* _WIMLIB_LZ_EXTEND_H */