#ifndef _LZX_COMMON_H
#define _LZX_COMMON_H
-#include "wimlib/bitops.h"
#include "wimlib/lzx_constants.h"
#include "wimlib/types.h"
extern const u8 lzx_extra_offset_bits[LZX_MAX_OFFSET_SLOTS];
-/*
- * Return the offset slot for the specified match offset.
- *
- * This returns the smallest i such that:
- *
- * offset + LZX_OFFSET_ADJUSTMENT >= lzx_offset_slot_base[i]
- *
- * However, the actual implementation below takes advantage of the regularity of
- * the offset slot bases to calculate the slot directly from the adjusted offset
- * without actually looking at the array.
- */
-static inline unsigned
-lzx_get_offset_slot(u32 offset)
-{
- u32 adjusted_offset = offset + LZX_OFFSET_ADJUSTMENT;
- if (adjusted_offset >= 196608) {
- return (adjusted_offset >> 17) + 34;
- } else {
- unsigned mssb_idx = fls32(adjusted_offset);
- return (mssb_idx << 1) |
- ((adjusted_offset >> (mssb_idx - 1)) & 1);
- }
-}
-
-static inline unsigned
-lzx_main_symbol_for_literal(unsigned literal)
-{
- return literal;
-}
-
-static inline unsigned
-lzx_main_symbol_for_match(unsigned offset_slot, unsigned len_header)
-{
- return LZX_NUM_CHARS + (offset_slot * LZX_NUM_LEN_HEADERS) + len_header;
-}
-
extern unsigned
lzx_get_window_order(size_t max_bufsize);
-extern unsigned
-lzx_get_num_offset_slots(unsigned window_order);
-
extern unsigned
lzx_get_num_main_syms(unsigned window_order);
return max(order, LZX_MIN_WINDOW_ORDER);
}
+/* Given a valid LZX window order, return the number of symbols that will exist
+ * in the main Huffman code. */
unsigned
-lzx_get_num_offset_slots(unsigned window_order)
+lzx_get_num_main_syms(unsigned window_order)
{
/* Note: one would expect that the maximum match offset would be
* 'window_size - LZX_MIN_MATCH_LEN', which would occur if the first two
* disallows this case. This reduces the number of needed offset slots
* by 1. */
u32 window_size = (u32)1 << window_order;
- u32 max_offset = window_size - LZX_MIN_MATCH_LEN - 1;
- return 1 + lzx_get_offset_slot(max_offset);
-}
+ u32 max_adjusted_offset = (window_size - LZX_MIN_MATCH_LEN - 1) +
+ LZX_OFFSET_ADJUSTMENT;
+ unsigned num_offset_slots = 30;
+ while (max_adjusted_offset >= lzx_offset_slot_base[num_offset_slots])
+ num_offset_slots++;
-/* Given a valid LZX window order, return the number of symbols that will exist
- * in the main Huffman code. */
-unsigned
-lzx_get_num_main_syms(unsigned window_order)
-{
- return LZX_NUM_CHARS + (lzx_get_num_offset_slots(window_order) *
- LZX_NUM_LEN_HEADERS);
+ return LZX_NUM_CHARS + (num_offset_slots * LZX_NUM_LEN_HEADERS);
}
static void
* of coding the literal is integrated into the queue update
* code below. */
literal = *in_next++;
- cost = cur_node->cost +
- c->costs.main[lzx_main_symbol_for_literal(literal)];
+ cost = cur_node->cost + c->costs.main[literal];
/* Advance to the next position. */
cur_node++;
static void
lzx_compute_match_costs(struct lzx_compressor *c)
{
- unsigned num_offset_slots = lzx_get_num_offset_slots(c->window_order);
+ unsigned num_offset_slots = (c->num_main_syms - LZX_NUM_CHARS) / LZX_NUM_LEN_HEADERS;
struct lzx_costs *costs = &c->costs;
for (unsigned offset_slot = 0; offset_slot < num_offset_slots; offset_slot++) {
u32 extra_cost = (u32)lzx_extra_offset_bits[offset_slot] * LZX_BIT_COST;
- unsigned main_symbol = lzx_main_symbol_for_match(offset_slot, 0);
+ unsigned main_symbol = LZX_NUM_CHARS + (offset_slot * LZX_NUM_LEN_HEADERS);
unsigned i;
#if LZX_CONSIDER_ALIGNED_COSTS