1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file range_decoder.h
6 /// \brief Range Decoder
8 // Authors: Igor Pavlov
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef LZMA_RANGE_DECODER_H
14 #define LZMA_RANGE_DECODER_H
16 #include "range_common.h"
19 // Choose the range decoder variants to use using a bitmask.
20 // If no bits are set, only the basic version is used.
21 // If more than one version is selected for the same feature,
22 // the last one on the list below is used.
24 // Bitwise-or of the following enable branchless C versions:
25 // 0x01 normal bittrees
26 // 0x02 fixed-sized reverse bittrees
27 // 0x04 variable-sized reverse bittrees (not faster)
28 // 0x08 matched literal (not faster)
30 // GCC & Clang compatible x86-64 inline assembly:
31 // 0x010 normal bittrees
32 // 0x020 fixed-sized reverse bittrees
33 // 0x040 variable-sized reverse bittrees
34 // 0x080 matched literal
37 // The default can be overridden at build time by defining
38 // LZMA_RANGE_DECODER_CONFIG to the desired mask.
40 // 2024-02-22: Feedback from benchmarks:
41 // - Brancless C (0x003) can be better than basic on x86-64 but often it's
42 // slightly worse on other archs. Since asm is much better on x86-64,
43 // branchless C is not used at all.
44 // - With x86-64 asm, there are slight differences between GCC and Clang
45 // and different processors. Overall 0x1F0 seems to be the best choice.
46 #ifndef LZMA_RANGE_DECODER_CONFIG
47 # if defined(__x86_64__) && !defined(__ILP32__) \
48 && !defined(__NVCOMPILER) \
49 && (defined(__GNUC__) || defined(__clang__))
50 # define LZMA_RANGE_DECODER_CONFIG 0x1F0
52 # define LZMA_RANGE_DECODER_CONFIG 0
57 // Negative RC_BIT_MODEL_TOTAL but the lowest RC_MOVE_BITS are flipped.
58 // This is useful for updating probability variables in branchless decoding:
60 // uint32_t decoded_bit = ...;
61 // probability tmp = RC_BIT_MODEL_OFFSET;
62 // tmp &= decoded_bit - 1;
63 // prob -= (prob + tmp) >> RC_MOVE_BITS;
64 #define RC_BIT_MODEL_OFFSET \
65 ((UINT32_C(1) << RC_MOVE_BITS) - 1 - RC_BIT_MODEL_TOTAL)
71 uint32_t init_bytes_left
;
75 /// Reads the first five bytes to initialize the range decoder.
76 static inline lzma_ret
77 rc_read_init(lzma_range_decoder
*rc
, const uint8_t *restrict in
,
78 size_t *restrict in_pos
, size_t in_size
)
80 while (rc
->init_bytes_left
> 0) {
81 if (*in_pos
== in_size
)
84 // The first byte is always 0x00. It could have been omitted
85 // in LZMA2 but it wasn't, so one byte is wasted in every
87 if (rc
->init_bytes_left
== 5 && in
[*in_pos
] != 0x00)
88 return LZMA_DATA_ERROR
;
90 rc
->code
= (rc
->code
<< 8) | in
[*in_pos
];
92 --rc
->init_bytes_left
;
95 return LZMA_STREAM_END
;
99 /// Makes local copies of range decoder and *in_pos variables. Doing this
100 /// improves speed significantly. The range decoder macros expect also
101 /// variables 'in' and 'in_size' to be defined.
102 #define rc_to_local(range_decoder, in_pos, fast_mode_in_required) \
103 lzma_range_decoder rc = range_decoder; \
104 const uint8_t *rc_in_ptr = in + (in_pos); \
105 const uint8_t *rc_in_end = in + in_size; \
106 const uint8_t *rc_in_fast_end \
107 = (rc_in_end - rc_in_ptr) <= (fast_mode_in_required) \
109 : rc_in_end - (fast_mode_in_required); \
110 (void)rc_in_fast_end; /* Silence a warning with HAVE_SMALL. */ \
114 /// Evaluates to true if there is enough input remaining to use fast mode.
115 #define rc_is_fast_allowed() (rc_in_ptr < rc_in_fast_end)
118 /// Stores the local copes back to the range decoder structure.
119 #define rc_from_local(range_decoder, in_pos) \
121 range_decoder = rc; \
122 in_pos = (size_t)(rc_in_ptr - in); \
126 /// Resets the range decoder structure.
127 #define rc_reset(range_decoder) \
129 (range_decoder).range = UINT32_MAX; \
130 (range_decoder).code = 0; \
131 (range_decoder).init_bytes_left = 5; \
135 /// When decoding has been properly finished, rc.code is always zero unless
136 /// the input stream is corrupt. So checking this can catch some corrupt
137 /// files especially if they don't have any other integrity check.
138 #define rc_is_finished(range_decoder) \
139 ((range_decoder).code == 0)
142 // Read the next input byte if needed.
143 #define rc_normalize() \
145 if (rc.range < RC_TOP_VALUE) { \
146 rc.range <<= RC_SHIFT_BITS; \
147 rc.code = (rc.code << RC_SHIFT_BITS) | *rc_in_ptr++; \
152 /// If more input is needed but there is
153 /// no more input available, "goto out" is used to jump out of the main
154 /// decoder loop. The "_safe" macros are used in the Resumable decoder
155 /// mode in order to save the sequence to continue decoding from that
157 #define rc_normalize_safe(seq) \
159 if (rc.range < RC_TOP_VALUE) { \
160 if (rc_in_ptr == rc_in_end) { \
161 coder->sequence = seq; \
164 rc.range <<= RC_SHIFT_BITS; \
165 rc.code = (rc.code << RC_SHIFT_BITS) | *rc_in_ptr++; \
170 /// Start decoding a bit. This must be used together with rc_update_0()
171 /// and rc_update_1():
174 /// rc_update_0(prob);
177 /// rc_update_1(prob);
178 /// // Do something else
181 #define rc_if_0(prob) \
183 rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
184 if (rc.code < rc_bound)
187 #define rc_if_0_safe(prob, seq) \
188 rc_normalize_safe(seq); \
189 rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
190 if (rc.code < rc_bound)
193 /// Update the range decoder state and the used probability variable to
194 /// match a decoded bit of 0.
196 /// The x86-64 assembly uses the commented method but it seems that,
197 /// at least on x86-64, the first version is slightly faster as C code.
198 #define rc_update_0(prob) \
200 rc.range = rc_bound; \
201 prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
202 /* prob -= ((prob) + RC_BIT_MODEL_OFFSET) >> RC_MOVE_BITS; */ \
206 /// Update the range decoder state and the used probability variable to
207 /// match a decoded bit of 1.
208 #define rc_update_1(prob) \
210 rc.range -= rc_bound; \
211 rc.code -= rc_bound; \
212 prob -= (prob) >> RC_MOVE_BITS; \
216 /// Decodes one bit and runs action0 or action1 depending on the decoded bit.
217 /// This macro is used as the last step in bittree reverse decoders since
218 /// those don't use "symbol" for anything else than indexing the probability
220 #define rc_bit_last(prob, action0, action1) \
232 #define rc_bit_last_safe(prob, action0, action1, seq) \
234 rc_if_0_safe(prob, seq) { \
244 /// Decodes one bit, updates "symbol", and runs action0 or action1 depending
245 /// on the decoded bit.
246 #define rc_bit(prob, action0, action1) \
248 symbol <<= 1; action0, \
249 symbol = (symbol << 1) + 1; action1);
252 #define rc_bit_safe(prob, action0, action1, seq) \
253 rc_bit_last_safe(prob, \
254 symbol <<= 1; action0, \
255 symbol = (symbol << 1) + 1; action1, \
258 // Unroll fixed-sized bittree decoding.
260 // A compile-time constant in final_add can be used to get rid of the high bit
261 // from symbol that is used for the array indexing (1U << bittree_bits).
262 // final_add may also be used to add offset to the result (LZMA length
263 // decoder does that).
265 // The reason to have final_add here is that in the asm code the addition
266 // can be done for free: in x86-64 there is SBB instruction with -1 as
267 // the immediate value, and final_add is combined with that value.
268 #define rc_bittree_bit(prob) \
271 #define rc_bittree3(probs, final_add) \
274 rc_bittree_bit(probs[symbol]); \
275 rc_bittree_bit(probs[symbol]); \
276 rc_bittree_bit(probs[symbol]); \
277 symbol += (uint32_t)(final_add); \
280 #define rc_bittree6(probs, final_add) \
283 rc_bittree_bit(probs[symbol]); \
284 rc_bittree_bit(probs[symbol]); \
285 rc_bittree_bit(probs[symbol]); \
286 rc_bittree_bit(probs[symbol]); \
287 rc_bittree_bit(probs[symbol]); \
288 rc_bittree_bit(probs[symbol]); \
289 symbol += (uint32_t)(final_add); \
292 #define rc_bittree8(probs, final_add) \
295 rc_bittree_bit(probs[symbol]); \
296 rc_bittree_bit(probs[symbol]); \
297 rc_bittree_bit(probs[symbol]); \
298 rc_bittree_bit(probs[symbol]); \
299 rc_bittree_bit(probs[symbol]); \
300 rc_bittree_bit(probs[symbol]); \
301 rc_bittree_bit(probs[symbol]); \
302 rc_bittree_bit(probs[symbol]); \
303 symbol += (uint32_t)(final_add); \
307 // Fixed-sized reverse bittree
308 #define rc_bittree_rev4(probs) \
311 rc_bit_last(probs[symbol + 1], , symbol += 1); \
312 rc_bit_last(probs[symbol + 2], , symbol += 2); \
313 rc_bit_last(probs[symbol + 4], , symbol += 4); \
314 rc_bit_last(probs[symbol + 8], , symbol += 8); \
318 // Decode one bit from variable-sized reverse bittree. The loop is done
319 // in the code that uses this macro. This could be changed if the assembly
320 // version benefited from having the loop done in assembly but it didn't
321 // seem so in early 2024.
323 // Also, if the loop was done here, the loop counter would likely be local
324 // to the macro so that it wouldn't modify yet another input variable.
325 // If a _safe version of a macro with a loop was done then a modifiable
326 // input variable couldn't be avoided though.
327 #define rc_bit_add_if_1(probs, dest, value_to_add_if_1) \
328 rc_bit(probs[symbol], \
330 dest += value_to_add_if_1);
334 #define decode_with_match_bit \
335 t_match_byte <<= 1; \
336 t_match_bit = t_match_byte & t_offset; \
337 t_subcoder_index = t_offset + t_match_bit + symbol; \
338 rc_bit(probs[t_subcoder_index], \
339 t_offset &= ~t_match_bit, \
340 t_offset &= t_match_bit)
342 #define rc_matched_literal(probs_base_var, match_byte) \
344 uint32_t t_match_byte = (match_byte); \
345 uint32_t t_match_bit; \
346 uint32_t t_subcoder_index; \
347 uint32_t t_offset = 0x100; \
349 decode_with_match_bit; \
350 decode_with_match_bit; \
351 decode_with_match_bit; \
352 decode_with_match_bit; \
353 decode_with_match_bit; \
354 decode_with_match_bit; \
355 decode_with_match_bit; \
356 decode_with_match_bit; \
360 /// Decode a bit without using a probability.
362 // NOTE: GCC 13 and Clang/LLVM 16 can, at least on x86-64, optimize the bound
363 // calculation to use an arithmetic right shift so there's no need to provide
364 // the alternative code which, according to C99/C11/C23 6.3.1.3-p3 isn't
365 // perfectly portable: rc_bound = (uint32_t)((int32_t)rc.code >> 31);
366 #define rc_direct(dest, count_var) \
368 dest = (dest << 1) + 1; \
371 rc.code -= rc.range; \
372 rc_bound = UINT32_C(0) - (rc.code >> 31); \
374 rc.code += rc.range & rc_bound; \
375 } while (--count_var > 0)
379 #define rc_direct_safe(dest, count_var, seq) \
381 rc_normalize_safe(seq); \
383 rc.code -= rc.range; \
384 rc_bound = UINT32_C(0) - (rc.code >> 31); \
385 rc.code += rc.range & rc_bound; \
386 dest = (dest << 1) + (rc_bound + 1); \
387 } while (--count_var > 0)
394 /// Decode a bit using a branchless method. This reduces the number of
395 /// mispredicted branches and thus can improve speed.
396 #define rc_c_bit(prob, action_bit, action_neg) \
398 probability *p = &(prob); \
400 rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * *p; \
401 uint32_t rc_mask = rc.code >= rc_bound; /* rc_mask = decoded bit */ \
402 action_bit; /* action when rc_mask is 0 or 1 */ \
403 /* rc_mask becomes 0 if bit is 0 and 0xFFFFFFFF if bit is 1: */ \
404 rc_mask = 0U - rc_mask; \
405 rc.range &= rc_mask; /* If bit 0: set rc.range = 0 */ \
406 rc_bound ^= rc_mask; \
407 rc_bound -= rc_mask; /* If bit 1: rc_bound = 0U - rc_bound */ \
408 rc.range += rc_bound; \
409 rc_bound &= rc_mask; \
410 rc.code += rc_bound; \
411 action_neg; /* action when rc_mask is 0 or 0xFFFFFFFF */ \
412 rc_mask = ~rc_mask; /* If bit 0: all bits are set in rc_mask */ \
413 rc_mask &= RC_BIT_MODEL_OFFSET; \
414 *p -= (*p + rc_mask) >> RC_MOVE_BITS; \
418 // Testing on x86-64 give an impression that only the normal bittrees and
419 // the fixed-sized reverse bittrees are worth the branchless C code.
420 // It should be tested on other archs for which there isn't assembly code
423 // Using addition in "(symbol << 1) + rc_mask" allows use of x86 LEA
424 // or RISC-V SH1ADD instructions. Compilers might infer it from
425 // "(symbol << 1) | rc_mask" too if they see that mask is 0 or 1 but
426 // the use of addition doesn't require such analysis from compilers.
427 #if LZMA_RANGE_DECODER_CONFIG & 0x01
428 #undef rc_bittree_bit
429 #define rc_bittree_bit(prob) \
431 symbol = (symbol << 1) + rc_mask, \
433 #endif // LZMA_RANGE_DECODER_CONFIG & 0x01
435 #if LZMA_RANGE_DECODER_CONFIG & 0x02
436 #undef rc_bittree_rev4
437 #define rc_bittree_rev4(probs) \
440 rc_c_bit(probs[symbol + 1], symbol += rc_mask, ); \
441 rc_c_bit(probs[symbol + 2], symbol += rc_mask << 1, ); \
442 rc_c_bit(probs[symbol + 4], symbol += rc_mask << 2, ); \
443 rc_c_bit(probs[symbol + 8], symbol += rc_mask << 3, ); \
445 #endif // LZMA_RANGE_DECODER_CONFIG & 0x02
447 #if LZMA_RANGE_DECODER_CONFIG & 0x04
448 #undef rc_bit_add_if_1
449 #define rc_bit_add_if_1(probs, dest, value_to_add_if_1) \
450 rc_c_bit(probs[symbol], \
451 symbol = (symbol << 1) + rc_mask, \
452 dest += (value_to_add_if_1) & rc_mask)
453 #endif // LZMA_RANGE_DECODER_CONFIG & 0x04
456 #if LZMA_RANGE_DECODER_CONFIG & 0x08
457 #undef decode_with_match_bit
458 #define decode_with_match_bit \
459 t_match_byte <<= 1; \
460 t_match_bit = t_match_byte & t_offset; \
461 t_subcoder_index = t_offset + t_match_bit + symbol; \
462 rc_c_bit(probs[t_subcoder_index], \
463 symbol = (symbol << 1) + rc_mask, \
464 t_offset &= ~t_match_bit ^ rc_mask)
465 #endif // LZMA_RANGE_DECODER_CONFIG & 0x08
472 #if LZMA_RANGE_DECODER_CONFIG & 0x1F0
474 // rc_asm_y and rc_asm_n are used as arguments to macros to control which
475 // strings to include or omit.
476 #define rc_asm_y(str) str
477 #define rc_asm_n(str)
479 // There are a few possible variations for normalization.
480 // This is the smallest variant which is also used by LZMA SDK.
482 // - This has partial register write (the MOV from (%[in_ptr])).
484 // - INC saves one byte in code size over ADD. False dependency on
485 // partial flags from INC shouldn't become a problem on any processor
486 // because the instructions after normalization don't read the flags
487 // until SUB which sets all flags.
489 #define rc_asm_normalize \
490 "cmp %[top_value], %[range]\n\t" \
492 "shl %[shift_bits], %[code]\n\t" \
493 "mov (%[in_ptr]), %b[code]\n\t" \
494 "shl %[shift_bits], %[range]\n\t" \
498 // rc_asm_calc(prob) is roughly equivalent to the C version of rc_if_0(prob)...
500 // rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob);
501 // if (rc.code < rc_bound)
503 // ...but the bound is stored in "range":
506 // range = (range >> RC_BIT_MODEL_TOTAL_BITS) * (prob);
511 // The carry flag (CF) from the last subtraction holds the negation of
512 // the decoded bit (if CF==0 then the decoded bit is 1).
513 // The values in t0 and t1 are needed for rc_update_0(prob) and
514 // rc_update_1(prob). If the bit is 0, rc_update_0(prob)...
516 // rc.range = rc_bound;
518 // ...has already been done but the "code -= range" has to be reverted using
519 // the old value stored in t1. (Also, prob needs to be updated.)
521 // If the bit is 1, rc_update_1(prob)...
523 // rc.range -= rc_bound;
524 // rc.code -= rc_bound;
526 // ...is already done for "code" but the value for "range" needs to be taken
527 // from t0. (Also, prob needs to be updated here as well.)
529 // The assignments from t0 and t1 can be done in a branchless manner with CMOV
530 // after the instructions from this macro. The CF from SUB tells which moves
532 #define rc_asm_calc(prob) \
533 "mov %[range], %[t0]\n\t" \
534 "shr %[bit_model_total_bits], %[range]\n\t" \
535 "imul %[" prob "], %[range]\n\t" \
536 "sub %[range], %[t0]\n\t" \
537 "mov %[code], %[t1]\n\t" \
538 "sub %[range], %[code]\n\t"
540 // Also, prob needs to be updated: The update math depends on the decoded bit.
541 // It can be expressed in a few slightly different ways but this is fairly
544 // prob -= (prob + (bit ? 0 : RC_BIT_MODEL_OFFSET)) >> RC_MOVE_BITS;
546 // To do it in branchless way when the negation of the decoded bit is in CF,
547 // both "prob" and "prob + RC_BIT_MODEL_OFFSET" are needed. Then the desired
548 // value can be picked with CMOV. The addition can be done using LEA without
551 // (This prob update method is a tiny bit different from LZMA SDK 23.01.
552 // In the LZMA SDK a single register is reserved solely for a constant to
553 // be used with CMOV when updating prob. That is fine since there are enough
554 // free registers to do so. The method used here uses one fewer register,
555 // which is valuable with inline assembly.)
559 // In bittree decoding, each (unrolled) loop iteration decodes one bit
560 // and needs one prob variable. To make it faster, the prob variable of
561 // the iteration N+1 is loaded during iteration N. There are two possible
562 // prob variables to choose from for N+1. Both are loaded from memory and
563 // the correct one is chosen with CMOV using the same CF as is used for
564 // other things described above.
566 // This preloading/prefetching requires an extra register. To avoid
567 // useless moves from "preloaded prob register" to "current prob register",
568 // the macros swap between the two registers for odd and even iterations.
572 // Finally, the decoded bit has to be stored in "symbol". Since the negation
573 // of the bit is in CF, this can be done with SBB: symbol -= CF - 1. That is,
574 // if the decoded bit is 0 (CF==1) the operation is a no-op "symbol -= 0"
575 // and when bit is 1 (CF==0) the operation is "symbol -= 0 - 1" which is
576 // the same as "symbol += 1".
578 // The instructions for all things are intertwined for a few reasons:
579 // - freeing temporary registers for new use
580 // - not modifying CF too early
581 // - instruction scheduling
583 // The first and last iterations can cheat a little. For example,
584 // on the first iteration "symbol" is known to start from 1 so it
585 // doesn't need to be read; it can even be immediately initialized
586 // to 2 to prepare for the second iteration of the loop.
590 // a = number of the current prob variable (0 or 1)
591 // b = number of the next prob variable (1 or 0)
592 // *_only = rc_asm_y or _n to include or exclude code marked with them
593 #define rc_asm_bittree(a, b, first_only, middle_only, last_only) \
595 "movzw 2(%[probs_base]), %[prob" #a "]\n\t" \
596 "mov $2, %[symbol]\n\t" \
597 "movzw 4(%[probs_base]), %[prob" #b "]\n\t" \
600 /* Note the scaling of 4 instead of 2: */ \
601 "movzw (%[probs_base], %q[symbol], 4), %[prob" #b "]\n\t" \
604 "add %[symbol], %[symbol]\n\t" \
608 rc_asm_calc("prob" #a) \
610 "cmovae %[t0], %[range]\n\t" \
613 "movzw 6(%[probs_base]), %[t0]\n\t" \
614 "cmovae %[t0], %[prob" #b "]\n\t" \
617 "movzw 2(%[probs_base], %q[symbol], 4), %[t0]\n\t" \
618 "lea (%q[symbol], %q[symbol]), %[symbol]\n\t" \
619 "cmovae %[t0], %[prob" #b "]\n\t" \
622 "lea %c[bit_model_offset](%q[prob" #a "]), %[t0]\n\t" \
623 "cmovb %[t1], %[code]\n\t" \
624 "mov %[symbol], %[t1]\n\t" \
625 "cmovae %[prob" #a "], %[t0]\n\t" \
628 "sbb $-1, %[symbol]\n\t" \
631 "sbb $-1, %[symbol]\n\t" \
634 "sbb %[last_sbb], %[symbol]\n\t" \
637 "shr %[move_bits], %[t0]\n\t" \
638 "sub %[t0], %[prob" #a "]\n\t" \
639 /* Scaling of 1 instead of 2 because symbol <<= 1. */ \
640 "mov %w[prob" #a "], (%[probs_base], %q[t1], 1)\n\t"
642 // NOTE: The order of variables in __asm__ can affect speed and code size.
643 #define rc_asm_bittree_n(probs_base_var, final_add, asm_str) \
653 [range] "+&r"(rc.range), \
654 [code] "+&r"(rc.code), \
657 [prob0] "=&r"(t_prob0), \
658 [prob1] "=&r"(t_prob1), \
659 [symbol] "=&r"(symbol), \
660 [in_ptr] "+&r"(rc_in_ptr) \
662 [probs_base] "r"(probs_base_var), \
663 [last_sbb] "n"(-1 - (final_add)), \
664 [top_value] "n"(RC_TOP_VALUE), \
665 [shift_bits] "n"(RC_SHIFT_BITS), \
666 [bit_model_total_bits] "n"(RC_BIT_MODEL_TOTAL_BITS), \
667 [bit_model_offset] "n"(RC_BIT_MODEL_OFFSET), \
668 [move_bits] "n"(RC_MOVE_BITS) \
674 #if LZMA_RANGE_DECODER_CONFIG & 0x010
676 #define rc_bittree3(probs_base_var, final_add) \
677 rc_asm_bittree_n(probs_base_var, final_add, \
678 rc_asm_bittree(0, 1, rc_asm_y, rc_asm_n, rc_asm_n) \
679 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_y, rc_asm_n) \
680 rc_asm_bittree(0, 1, rc_asm_n, rc_asm_n, rc_asm_y) \
684 #define rc_bittree6(probs_base_var, final_add) \
685 rc_asm_bittree_n(probs_base_var, final_add, \
686 rc_asm_bittree(0, 1, rc_asm_y, rc_asm_n, rc_asm_n) \
687 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_y, rc_asm_n) \
688 rc_asm_bittree(0, 1, rc_asm_n, rc_asm_y, rc_asm_n) \
689 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_y, rc_asm_n) \
690 rc_asm_bittree(0, 1, rc_asm_n, rc_asm_y, rc_asm_n) \
691 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_n, rc_asm_y) \
695 #define rc_bittree8(probs_base_var, final_add) \
696 rc_asm_bittree_n(probs_base_var, final_add, \
697 rc_asm_bittree(0, 1, rc_asm_y, rc_asm_n, rc_asm_n) \
698 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_y, rc_asm_n) \
699 rc_asm_bittree(0, 1, rc_asm_n, rc_asm_y, rc_asm_n) \
700 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_y, rc_asm_n) \
701 rc_asm_bittree(0, 1, rc_asm_n, rc_asm_y, rc_asm_n) \
702 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_y, rc_asm_n) \
703 rc_asm_bittree(0, 1, rc_asm_n, rc_asm_y, rc_asm_n) \
704 rc_asm_bittree(1, 0, rc_asm_n, rc_asm_n, rc_asm_y) \
706 #endif // LZMA_RANGE_DECODER_CONFIG & 0x010
709 // Fixed-sized reverse bittree
711 // This uses the indexing that constructs the final value in symbol directly.
713 // dcur = -, 4, 8, 16
714 // dnext0 = 4, 8, 16, -
715 // dnext0 = 6, 12, 24, -
716 #define rc_asm_bittree_rev(a, b, add, dcur, dnext0, dnext1, \
717 first_only, middle_only, last_only) \
719 "movzw 2(%[probs_base]), %[prob" #a "]\n\t" \
720 "xor %[symbol], %[symbol]\n\t" \
721 "movzw 4(%[probs_base]), %[prob" #b "]\n\t" \
724 "movzw " #dnext0 "(%[probs_base], %q[symbol], 2), " \
725 "%[prob" #b "]\n\t" \
729 rc_asm_calc("prob" #a) \
731 "cmovae %[t0], %[range]\n\t" \
734 "movzw 6(%[probs_base]), %[t0]\n\t" \
735 "cmovae %[t0], %[prob" #b "]\n\t" \
738 "movzw " #dnext1 "(%[probs_base], %q[symbol], 2), %[t0]\n\t" \
739 "cmovae %[t0], %[prob" #b "]\n\t" \
742 "lea " #add "(%q[symbol]), %[t0]\n\t" \
743 "cmovb %[t1], %[code]\n\t" \
745 "mov %[symbol], %[t1]\n\t" \
748 "mov %[symbol], %[t1]\n\t" \
750 "cmovae %[t0], %[symbol]\n\t" \
751 "lea %c[bit_model_offset](%q[prob" #a "]), %[t0]\n\t" \
752 "cmovae %[prob" #a "], %[t0]\n\t" \
754 "shr %[move_bits], %[t0]\n\t" \
755 "sub %[t0], %[prob" #a "]\n\t" \
757 "mov %w[prob" #a "], 2(%[probs_base])\n\t" \
760 "mov %w[prob" #a "], " \
761 #dcur "(%[probs_base], %q[t1], 2)\n\t" \
764 "mov %w[prob" #a "], " \
765 #dcur "(%[probs_base], %q[t1], 2)\n\t" \
768 #if LZMA_RANGE_DECODER_CONFIG & 0x020
769 #undef rc_bittree_rev4
770 #define rc_bittree_rev4(probs_base_var) \
771 rc_asm_bittree_n(probs_base_var, 4, \
772 rc_asm_bittree_rev(0, 1, 1, -, 4, 6, rc_asm_y, rc_asm_n, rc_asm_n) \
773 rc_asm_bittree_rev(1, 0, 2, 4, 8, 12, rc_asm_n, rc_asm_y, rc_asm_n) \
774 rc_asm_bittree_rev(0, 1, 4, 8, 16, 24, rc_asm_n, rc_asm_y, rc_asm_n) \
775 rc_asm_bittree_rev(1, 0, 8, 16, -, -, rc_asm_n, rc_asm_n, rc_asm_y) \
777 #endif // LZMA_RANGE_DECODER_CONFIG & 0x020
780 #if LZMA_RANGE_DECODER_CONFIG & 0x040
781 #undef rc_bit_add_if_1
782 #define rc_bit_add_if_1(probs_base_var, dest_var, value_to_add_if_1) \
786 uint32_t t2 = (value_to_add_if_1); \
791 "movzw (%[probs_base], %q[symbol], 2), %[prob]\n\t" \
792 "mov %[symbol], %[index]\n\t" \
794 "add %[dest], %[t2]\n\t" \
795 "add %[symbol], %[symbol]\n\t" \
798 rc_asm_calc("prob") \
800 "cmovae %[t0], %[range]\n\t" \
801 "lea %c[bit_model_offset](%q[prob]), %[t0]\n\t" \
802 "cmovb %[t1], %[code]\n\t" \
803 "cmovae %[prob], %[t0]\n\t" \
805 "cmovae %[t2], %[dest]\n\t" \
806 "sbb $-1, %[symbol]\n\t" \
808 "sar %[move_bits], %[t0]\n\t" \
809 "sub %[t0], %[prob]\n\t" \
810 "mov %w[prob], (%[probs_base], %q[index], 2)" \
812 [range] "+&r"(rc.range), \
813 [code] "+&r"(rc.code), \
816 [prob] "=&r"(t_prob), \
817 [index] "=&r"(t_index), \
818 [symbol] "+&r"(symbol), \
820 [dest] "+&r"(dest_var), \
821 [in_ptr] "+&r"(rc_in_ptr) \
823 [probs_base] "r"(probs_base_var), \
824 [top_value] "n"(RC_TOP_VALUE), \
825 [shift_bits] "n"(RC_SHIFT_BITS), \
826 [bit_model_total_bits] "n"(RC_BIT_MODEL_TOTAL_BITS), \
827 [bit_model_offset] "n"(RC_BIT_MODEL_OFFSET), \
828 [move_bits] "n"(RC_MOVE_BITS) \
832 #endif // LZMA_RANGE_DECODER_CONFIG & 0x040
835 // Literal decoding uses a normal 8-bit bittree but literal with match byte
836 // is more complex in picking the probability variable from the correct
837 // subtree. This doesn't use preloading/prefetching of the next prob because
838 // there are four choices instead of two.
840 // FIXME? The first iteration starts with symbol = 1 so it could be optimized
842 #define rc_asm_matched_literal(nonlast_only) \
843 "add %[offset], %[symbol]\n\t" \
844 "and %[offset], %[match_bit]\n\t" \
845 "add %[match_bit], %[symbol]\n\t" \
847 "movzw (%[probs_base], %q[symbol], 2), %[prob]\n\t" \
849 "add %[symbol], %[symbol]\n\t" \
852 "xor %[match_bit], %[offset]\n\t" \
853 "add %[match_byte], %[match_byte]\n\t" \
857 rc_asm_calc("prob") \
859 "cmovae %[t0], %[range]\n\t" \
860 "lea %c[bit_model_offset](%q[prob]), %[t0]\n\t" \
861 "cmovb %[t1], %[code]\n\t" \
862 "mov %[symbol], %[t1]\n\t" \
863 "cmovae %[prob], %[t0]\n\t" \
866 "cmovae %[match_bit], %[offset]\n\t" \
867 "mov %[match_byte], %[match_bit]\n\t" \
870 "sbb $-1, %[symbol]\n\t" \
872 "shr %[move_bits], %[t0]\n\t" \
873 /* Undo symbol += match_bit + offset: */ \
874 "and $0x1FF, %[symbol]\n\t" \
875 "sub %[t0], %[prob]\n\t" \
877 /* Scaling of 1 instead of 2 because symbol <<= 1. */ \
878 "mov %w[prob], (%[probs_base], %q[t1], 1)\n\t"
881 #if LZMA_RANGE_DECODER_CONFIG & 0x080
882 #undef rc_matched_literal
883 #define rc_matched_literal(probs_base_var, match_byte_value) \
888 uint32_t t_match_byte = (uint32_t)(match_byte_value) << 1; \
889 uint32_t t_match_bit = t_match_byte; \
890 uint32_t t_offset = 0x100; \
894 rc_asm_matched_literal(rc_asm_y) \
895 rc_asm_matched_literal(rc_asm_y) \
896 rc_asm_matched_literal(rc_asm_y) \
897 rc_asm_matched_literal(rc_asm_y) \
898 rc_asm_matched_literal(rc_asm_y) \
899 rc_asm_matched_literal(rc_asm_y) \
900 rc_asm_matched_literal(rc_asm_y) \
901 rc_asm_matched_literal(rc_asm_n) \
903 [range] "+&r"(rc.range), \
904 [code] "+&r"(rc.code), \
907 [prob] "=&r"(t_prob), \
908 [match_bit] "+&r"(t_match_bit), \
909 [symbol] "+&r"(symbol), \
910 [match_byte] "+&r"(t_match_byte), \
911 [offset] "+&r"(t_offset), \
912 [in_ptr] "+&r"(rc_in_ptr) \
914 [probs_base] "r"(probs_base_var), \
915 [top_value] "n"(RC_TOP_VALUE), \
916 [shift_bits] "n"(RC_SHIFT_BITS), \
917 [bit_model_total_bits] "n"(RC_BIT_MODEL_TOTAL_BITS), \
918 [bit_model_offset] "n"(RC_BIT_MODEL_OFFSET), \
919 [move_bits] "n"(RC_MOVE_BITS) \
923 #endif // LZMA_RANGE_DECODER_CONFIG & 0x080
926 // Doing the loop in asm instead of C seems to help a little.
927 #if LZMA_RANGE_DECODER_CONFIG & 0x100
929 #define rc_direct(dest_var, count_var) \
936 "add %[dest], %[dest]\n\t" \
937 "lea 1(%q[dest]), %[t1]\n\t" \
941 "shr $1, %[range]\n\t" \
942 "mov %[code], %[t0]\n\t" \
943 "sub %[range], %[code]\n\t" \
944 "cmovns %[t1], %[dest]\n\t" \
945 "cmovs %[t0], %[code]\n\t" \
949 [range] "+&r"(rc.range), \
950 [code] "+&r"(rc.code), \
953 [dest] "+&r"(dest_var), \
954 [count] "+&r"(count_var), \
955 [in_ptr] "+&r"(rc_in_ptr) \
957 [top_value] "n"(RC_TOP_VALUE), \
958 [shift_bits] "n"(RC_SHIFT_BITS) \
962 #endif // LZMA_RANGE_DECODER_CONFIG & 0x100