13#ifndef LZMA_RANGE_DECODER_H
14#define LZMA_RANGE_DECODER_H
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
64#define RC_BIT_MODEL_OFFSET \
65 ((UINT32_C(1) << RC_MOVE_BITS) - 1 - RC_BIT_MODEL_TOTAL)
71 uint32_t init_bytes_left;
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; \
115#define rc_is_fast_allowed() (rc_in_ptr < rc_in_fast_end)
119#define rc_from_local(range_decoder, in_pos) \
121 range_decoder = rc; \
122 in_pos = (size_t)(rc_in_ptr - in); \
127#define rc_reset(range_decoder) \
129 (range_decoder).range = UINT32_MAX; \
130 (range_decoder).code = 0; \
131 (range_decoder).init_bytes_left = 5; \
138#define rc_is_finished(range_decoder) \
139 ((range_decoder).code == 0)
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++; \
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++; \
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)
198#define rc_update_0(prob) \
200 rc.range = rc_bound; \
201 prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
208#define rc_update_1(prob) \
210 rc.range -= rc_bound; \
211 rc.code -= rc_bound; \
212 prob -= (prob) >> RC_MOVE_BITS; \
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) { \
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, \
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); \
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); \
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; \
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)
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; \
404 rc_mask = 0U - rc_mask; \
405 rc.range &= rc_mask; \
406 rc_bound ^= rc_mask; \
407 rc_bound -= rc_mask; \
408 rc.range += rc_bound; \
409 rc_bound &= rc_mask; \
410 rc.code += rc_bound; \
412 rc_mask = ~rc_mask; \
413 rc_mask &= RC_BIT_MODEL_OFFSET; \
414 *p -= (*p + rc_mask) >> RC_MOVE_BITS; \
427#if LZMA_RANGE_DECODER_CONFIG & 0x01
429#define rc_bittree_bit(prob) \
431 symbol = (symbol << 1) + rc_mask, \
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, ); \
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)
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)
472#if LZMA_RANGE_DECODER_CONFIG & 0x1F0
476#define rc_asm_y(str) str
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" \
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"
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" \
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" \
640 "mov %w[prob" #a "], (%[probs_base], %q[t1], 1)\n\t"
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) \
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) \
771rc_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) \
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) \
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" \
874 "and $0x1FF, %[symbol]\n\t" \
875 "sub %[t0], %[prob]\n\t" \
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) \
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) \
Definition range_decoder.h:20
uint32_t init_bytes_left
Definition range_decoder.h:23
uint32_t code
Definition range_decoder.h:22
lzma_ret
Return values used by several functions in liblzma.
Definition base.h:57
@ LZMA_DATA_ERROR
Data is corrupt.
Definition base.h:172
@ LZMA_STREAM_END
End of stream was reached.
Definition base.h:63
@ LZMA_OK
Operation completed successfully.
Definition base.h:58
const lzma_allocator const uint8_t size_t * in_pos
Definition block.h:579
const lzma_allocator const uint8_t size_t in_size
Definition block.h:527
Common things for range encoder and decoder.