+ unsigned len_header;
+ unsigned main_symbol;
+ unsigned len_symbol;
+ unsigned offset_slot;
+ unsigned num_extra_bits;
+ u32 extra_bits;
+
+ if (len - LZX_MIN_MATCH_LEN < LZX_NUM_PRIMARY_LENS) {
+ len_header = len - LZX_MIN_MATCH_LEN;
+ len_symbol = LZX_LENCODE_NUM_SYMBOLS;
+ } else {
+ len_header = LZX_NUM_PRIMARY_LENS;
+ len_symbol = len - LZX_MIN_MATCH_LEN - LZX_NUM_PRIMARY_LENS;
+ c->freqs.len[len_symbol]++;
+ }
+
+ offset_slot = lzx_get_offset_slot_raw(offset + LZX_OFFSET_OFFSET);
+
+ main_symbol = LZX_NUM_CHARS + ((offset_slot << 3) | len_header);
+
+ c->freqs.main[main_symbol]++;
+
+ if (offset_slot >= 8)
+ c->freqs.aligned[(offset + LZX_OFFSET_OFFSET) & 7]++;
+
+ if (next_chosen_item) {
+
+ num_extra_bits = lzx_extra_offset_bits[offset_slot];
+
+ extra_bits = (offset + LZX_OFFSET_OFFSET) -
+ lzx_offset_slot_base[offset_slot];
+
+ *(*next_chosen_item)++ = (struct lzx_item) {
+ .data = (u64)main_symbol |
+ ((u64)len_symbol << 10) |
+ ((u64)num_extra_bits << 18) |
+ ((u64)extra_bits << 23),
+ };
+ }
+}
+
+/* Tally, and optionally record, the specified match or literal. */
+static inline void
+lzx_declare_item(struct lzx_compressor *c, u32 mc_item_data,
+ struct lzx_item **next_chosen_item)
+{
+ u32 len = mc_item_data & MC_LEN_MASK;
+ u32 offset_data = mc_item_data >> MC_OFFSET_SHIFT;
+
+ if (len == 1)
+ lzx_declare_literal(c, offset_data, next_chosen_item);
+ else if (offset_data < LZX_NUM_RECENT_OFFSETS)
+ lzx_declare_repeat_offset_match(c, len, offset_data,
+ next_chosen_item);
+ else
+ lzx_declare_explicit_offset_match(c, len,
+ offset_data - LZX_OFFSET_OFFSET,
+ next_chosen_item);
+}
+
+static inline void
+lzx_record_item_list(struct lzx_compressor *c,
+ struct lzx_mc_pos_data *cur_optimum_ptr,
+ struct lzx_item **next_chosen_item)
+{
+ struct lzx_mc_pos_data *end_optimum_ptr;
+ u32 saved_item;
+ u32 item;
+
+ /* The list is currently in reverse order (last item to first item).
+ * Reverse it. */
+ end_optimum_ptr = cur_optimum_ptr;
+ saved_item = cur_optimum_ptr->mc_item_data;
+ do {
+ item = saved_item;
+ cur_optimum_ptr -= item & MC_LEN_MASK;
+ saved_item = cur_optimum_ptr->mc_item_data;
+ cur_optimum_ptr->mc_item_data = item;
+ } while (cur_optimum_ptr != c->optimum);
+
+ /* Walk the list of items from beginning to end, tallying and recording
+ * each item. */
+ do {
+ lzx_declare_item(c, cur_optimum_ptr->mc_item_data, next_chosen_item);
+ cur_optimum_ptr += (cur_optimum_ptr->mc_item_data) & MC_LEN_MASK;
+ } while (cur_optimum_ptr != end_optimum_ptr);
+}
+
+static inline void
+lzx_tally_item_list(struct lzx_compressor *c, struct lzx_mc_pos_data *cur_optimum_ptr)
+{
+ /* Since we're just tallying the items, we don't need to reverse the
+ * list. Processing the items in reverse order is fine. */
+ do {
+ lzx_declare_item(c, cur_optimum_ptr->mc_item_data, NULL);
+ cur_optimum_ptr -= (cur_optimum_ptr->mc_item_data & MC_LEN_MASK);
+ } while (cur_optimum_ptr != c->optimum);