From 226a6dfe2909e054568298196785c944a1b5c4fa Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Fri, 2 Jan 2015 18:25:50 -0600 Subject: [PATCH 1/1] Misc. cleanups --- include/wimlib/bt_matchfinder.h | 5 ++- include/wimlib/decompress_common.h | 20 +++++++++++- include/wimlib/hc_matchfinder.h | 28 ++++++++-------- include/wimlib/lz_extend.h | 25 +++++++-------- include/wimlib/matchfinder_common.h | 5 ++- include/wimlib/unaligned.h | 20 ------------ src/compress_common.c | 1 - src/decompress_common.c | 1 - src/error.c | 50 ++++++++++++++--------------- 9 files changed, 78 insertions(+), 77 deletions(-) diff --git a/include/wimlib/bt_matchfinder.h b/include/wimlib/bt_matchfinder.h index 75858a33..43654c5b 100644 --- a/include/wimlib/bt_matchfinder.h +++ b/include/wimlib/bt_matchfinder.h @@ -27,7 +27,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once +#ifndef _BT_MATCHFINDER_H +#define _BT_MATCHFINDER_H #include "wimlib/lz_extend.h" #include "wimlib/lz_hash3.h" @@ -226,3 +227,5 @@ bt_matchfinder_skip_position(struct bt_matchfinder * const restrict mf, } } } + +#endif /* _BT_MATCHFINDER_H */ diff --git a/include/wimlib/decompress_common.h b/include/wimlib/decompress_common.h index b6927bed..8ed98ca8 100644 --- a/include/wimlib/decompress_common.h +++ b/include/wimlib/decompress_common.h @@ -10,7 +10,6 @@ #ifndef _WIMLIB_DECOMPRESS_COMMON_H #define _WIMLIB_DECOMPRESS_COMMON_H -#include "wimlib/assert.h" #include "wimlib/compiler.h" #include "wimlib/endianness.h" #include "wimlib/types.h" @@ -253,6 +252,25 @@ make_huffman_decode_table(u16 decode_table[], unsigned num_syms, unsigned num_bits, const u8 lens[], unsigned max_codeword_len); +static inline void +copy_word_unaligned(const void *src, void *dst) +{ + store_word_unaligned(load_word_unaligned(src), dst); +} + +static inline machine_word_t +repeat_byte(u8 b) +{ + machine_word_t v; + + BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8); + + v = b; + v |= v << 8; + v |= v << 16; + v |= v << ((WORDSIZE == 8) ? 32 : 0); + return v; +} /* * Copy an LZ77 match at (dst - offset) to dst. diff --git a/include/wimlib/hc_matchfinder.h b/include/wimlib/hc_matchfinder.h index d880b248..02271ff4 100644 --- a/include/wimlib/hc_matchfinder.h +++ b/include/wimlib/hc_matchfinder.h @@ -27,7 +27,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once +#ifndef _HC_MATCHFINDER_H +#define _HC_MATCHFINDER_H #include "wimlib/lz_extend.h" #include "wimlib/lz_hash3.h" @@ -52,7 +53,7 @@ struct hc_matchfinder { pos_t mf_data[HC_MATCHFINDER_TOTAL_LENGTH]; struct { pos_t hash_tab[HC_MATCHFINDER_HASH_LENGTH]; - pos_t child_tab[MATCHFINDER_WINDOW_SIZE]; + pos_t next_tab[MATCHFINDER_WINDOW_SIZE]; }; }; } _aligned_attribute(MATCHFINDER_ALIGNMENT); @@ -123,7 +124,7 @@ hc_matchfinder_longest_match(struct hc_matchfinder * const restrict mf, first_3_bytes = load_u24_unaligned(in_next); hash = lz_hash_u24(first_3_bytes, HC_MATCHFINDER_HASH_ORDER); cur_match = mf->hash_tab[hash]; - mf->child_tab[in_next - in_base] = cur_match; + mf->next_tab[in_next - in_base] = cur_match; mf->hash_tab[hash] = in_next - in_base; if (unlikely(best_len >= max_len)) @@ -144,7 +145,7 @@ hc_matchfinder_longest_match(struct hc_matchfinder * const restrict mf, break; /* Not a match; keep trying. */ - cur_match = mf->child_tab[ + cur_match = mf->next_tab[ matchfinder_slot_for_match(cur_match)]; if (!matchfinder_match_in_window(cur_match, in_base, in_next)) @@ -158,7 +159,7 @@ hc_matchfinder_longest_match(struct hc_matchfinder * const restrict mf, best_len = lz_extend(in_next, best_matchptr, 3, max_len); if (best_len >= nice_len) goto out; - cur_match = mf->child_tab[matchfinder_slot_for_match(cur_match)]; + cur_match = mf->next_tab[matchfinder_slot_for_match(cur_match)]; if (!matchfinder_match_in_window(cur_match, in_base, in_next)) goto out; if (!--depth_remaining) @@ -181,18 +182,17 @@ hc_matchfinder_longest_match(struct hc_matchfinder * const restrict mf, #endif break; - cur_match = mf->child_tab[matchfinder_slot_for_match(cur_match)]; + cur_match = mf->next_tab[matchfinder_slot_for_match(cur_match)]; if (!matchfinder_match_in_window(cur_match, in_base, in_next)) goto out; if (!--depth_remaining) goto out; } - #if UNALIGNED_ACCESS_IS_FAST - len = 4; - #else - len = 0; - #endif + if (UNALIGNED_ACCESS_IS_FAST) + len = 4; + else + len = 0; len = lz_extend(in_next, matchptr, len, max_len); if (len > best_len) { best_len = len; @@ -200,7 +200,7 @@ hc_matchfinder_longest_match(struct hc_matchfinder * const restrict mf, if (best_len >= nice_len) goto out; } - cur_match = mf->child_tab[matchfinder_slot_for_match(cur_match)]; + cur_match = mf->next_tab[matchfinder_slot_for_match(cur_match)]; if (!matchfinder_match_in_window(cur_match, in_base, in_next)) goto out; if (!--depth_remaining) @@ -240,8 +240,10 @@ hc_matchfinder_skip_positions(struct hc_matchfinder * restrict mf, do { hash = lz_hash(in_next, HC_MATCHFINDER_HASH_ORDER); - mf->child_tab[in_next - in_base] = mf->hash_tab[hash]; + mf->next_tab[in_next - in_base] = mf->hash_tab[hash]; mf->hash_tab[hash] = in_next - in_base; in_next++; } while (--count); } + +#endif /* _HC_MATCHFINDER_H */ diff --git a/include/wimlib/lz_extend.h b/include/wimlib/lz_extend.h index d47e0976..a3780e1d 100644 --- a/include/wimlib/lz_extend.h +++ b/include/wimlib/lz_extend.h @@ -20,10 +20,9 @@ lz_extend(const u8 * const strptr, const u8 * const matchptr, const u32 start_len, const u32 max_len) { u32 len = start_len; + machine_word_t v_word; - if (UNALIGNED_ACCESS_IS_FAST && CPU_IS_LITTLE_ENDIAN) { - - machine_word_t v_word; + if (UNALIGNED_ACCESS_IS_FAST) { if (likely(max_len - len >= 4 * WORDSIZE)) { @@ -48,18 +47,18 @@ lz_extend(const u8 * const strptr, const u8 * const matchptr, goto word_differs; len += WORDSIZE; } + } - while (len < max_len && matchptr[len] == strptr[len]) - len++; - return len; + while (len < max_len && matchptr[len] == strptr[len]) + len++; + return len; - word_differs: - return len + (ffsw(v_word) >> 3); - } else { - while (len < max_len && matchptr[len] == strptr[len]) - len++; - return len; - } +word_differs: + if (CPU_IS_LITTLE_ENDIAN) + len += (ffsw(v_word) >> 3); + else + len += (flsw(v_word) >> 3); + return len; } #endif /* _WIMLIB_LZ_EXTEND_H */ diff --git a/include/wimlib/matchfinder_common.h b/include/wimlib/matchfinder_common.h index afaab9cb..66a966a3 100644 --- a/include/wimlib/matchfinder_common.h +++ b/include/wimlib/matchfinder_common.h @@ -29,7 +29,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once +#ifndef _MATCHFINDER_COMMON_H +#define _MATCHFINDER_COMMON_H #include "wimlib/types.h" @@ -186,3 +187,5 @@ matchfinder_rebase(pos_t *data, size_t num_entries) } } #endif /* MATCHFINDER_IS_SLIDING */ + +#endif /* _MATCHFINDER_COMMON_H */ diff --git a/include/wimlib/unaligned.h b/include/wimlib/unaligned.h index ccc86f01..5f920e75 100644 --- a/include/wimlib/unaligned.h +++ b/include/wimlib/unaligned.h @@ -46,26 +46,6 @@ DEFINE_UNALIGNED_TYPE(machine_word_t); #define load_word_unaligned load_machine_word_t_unaligned #define store_word_unaligned store_machine_word_t_unaligned -static inline void -copy_word_unaligned(const void *src, void *dst) -{ - store_word_unaligned(load_word_unaligned(src), dst); -} - -static inline machine_word_t -repeat_byte(u8 b) -{ - machine_word_t v; - - BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8); - - v = b; - v |= v << 8; - v |= v << 16; - v |= v << ((WORDSIZE == 8) ? 32 : 0); - return v; -} - static inline u16 get_unaligned_u16_le(const void *p) { diff --git a/src/compress_common.c b/src/compress_common.c index 7ca7f5bb..f7430958 100644 --- a/src/compress_common.c +++ b/src/compress_common.c @@ -14,7 +14,6 @@ # include "config.h" #endif -#include "wimlib/assert.h" #include "wimlib/compress_common.h" #include "wimlib/util.h" diff --git a/src/decompress_common.c b/src/decompress_common.c index f89fc4f3..49cd5ba0 100644 --- a/src/decompress_common.c +++ b/src/decompress_common.c @@ -14,7 +14,6 @@ # include "config.h" #endif -#include "wimlib/assert.h" #include "wimlib/decompress_common.h" #include diff --git a/src/error.c b/src/error.c index 627d1c27..30087aef 100644 --- a/src/error.c +++ b/src/error.c @@ -55,36 +55,34 @@ static bool wimlib_owns_error_file = false; #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG) static void -wimlib_vmsg(const tchar *tag, const tchar *format, - va_list va, bool perror) +wimlib_vmsg(const tchar *tag, const tchar *format, va_list va, bool perror) { -#if !defined(ENABLE_DEBUG) - if (wimlib_print_errors) +#ifndef ENABLE_DEBUG + if (!wimlib_print_errors) + return; #endif - { - int errno_save = errno; - fflush(stdout); - tfputs(tag, wimlib_error_file); - tvfprintf(wimlib_error_file, format, va); - if (perror && errno_save != 0) { - tchar buf[64]; - int res; - res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf)); - if (res) { - tsprintf(buf, - T("unknown error (errno=%d)"), - errno_save); - } - #ifdef WIN32 - if (errno_save == EBUSY) - tstrcpy(buf, T("Resource busy")); - #endif - tfprintf(wimlib_error_file, T(": %"TS), buf); + int errno_save = errno; + fflush(stdout); + tfputs(tag, wimlib_error_file); + tvfprintf(wimlib_error_file, format, va); + if (perror && errno_save != 0) { + tchar buf[64]; + int res; + res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf)); + if (res) { + tsprintf(buf, + T("unknown error (errno=%d)"), + errno_save); } - tputc(T('\n'), wimlib_error_file); - fflush(wimlib_error_file); - errno = errno_save; + #ifdef WIN32 + if (errno_save == EBUSY) + tstrcpy(buf, T("Resource busy")); + #endif + tfprintf(wimlib_error_file, T(": %"TS), buf); } + tputc(T('\n'), wimlib_error_file); + fflush(wimlib_error_file); + errno = errno_save; } #endif -- 2.43.0