| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef LZMA_RANGE_DECODER_H |
| 14 |
#define LZMA_RANGE_DECODER_H |
| 15 |
|
| 16 |
#include "range_common.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
| 51 |
# else |
| 52 |
# define LZMA_RANGE_DECODER_CONFIG 0 |
| 53 |
# endif |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#define RC_BIT_MODEL_OFFSET \ |
| 65 |
((UINT32_C(1) << RC_MOVE_BITS) - 1 - RC_BIT_MODEL_TOTAL) |
| 66 |
|
| 67 |
|
| 68 |
typedef struct { |
| 69 |
uint32_t range; |
| 70 |
uint32_t code; |
| 71 |
uint32_t init_bytes_left; |
| 72 |
} lzma_range_decoder; |
| 73 |
|
| 74 |
|
| 75 |
|
| 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) |
| 79 |
{ |
| 80 |
while (rc->init_bytes_left > 0) { |
| 81 |
if (*in_pos == in_size) |
| 82 |
return LZMA_OK; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00) |
| 88 |
return LZMA_DATA_ERROR; |
| 89 |
|
| 90 |
rc->code = (rc->code << 8) | in[*in_pos]; |
| 91 |
++*in_pos; |
| 92 |
--rc->init_bytes_left; |
| 93 |
} |
| 94 |
|
| 95 |
return LZMA_STREAM_END; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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) \ |
| 108 |
? rc_in_ptr \ |
| 109 |
: rc_in_end - (fast_mode_in_required); \ |
| 110 |
(void)rc_in_fast_end; \ |
| 111 |
uint32_t rc_bound |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#define rc_is_fast_allowed() (rc_in_ptr < rc_in_fast_end) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#define rc_from_local(range_decoder, in_pos) \ |
| 120 |
do { \ |
| 121 |
range_decoder = rc; \ |
| 122 |
in_pos = (size_t)(rc_in_ptr - in); \ |
| 123 |
} while (0) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#define rc_reset(range_decoder) \ |
| 128 |
do { \ |
| 129 |
(range_decoder).range = UINT32_MAX; \ |
| 130 |
(range_decoder).code = 0; \ |
| 131 |
(range_decoder).init_bytes_left = 5; \ |
| 132 |
} while (0) |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#define rc_is_finished(range_decoder) \ |
| 139 |
((range_decoder).code == 0) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#define rc_normalize() \ |
| 144 |
do { \ |
| 145 |
if (rc.range < RC_TOP_VALUE) { \ |
| 146 |
rc.range <<= RC_SHIFT_BITS; \ |
| 147 |
rc.code = (rc.code << RC_SHIFT_BITS) | *rc_in_ptr++; \ |
| 148 |
} \ |
| 149 |
} while (0) |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#define rc_normalize_safe(seq) \ |
| 158 |
do { \ |
| 159 |
if (rc.range < RC_TOP_VALUE) { \ |
| 160 |
if (rc_in_ptr == rc_in_end) { \ |
| 161 |
coder->sequence = seq; \ |
| 162 |
goto out; \ |
| 163 |
} \ |
| 164 |
rc.range <<= RC_SHIFT_BITS; \ |
| 165 |
rc.code = (rc.code << RC_SHIFT_BITS) | *rc_in_ptr++; \ |
| 166 |
} \ |
| 167 |
} while (0) |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#define rc_if_0(prob) \ |
| 182 |
rc_normalize(); \ |
| 183 |
rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \ |
| 184 |
if (rc.code < rc_bound) |
| 185 |
|
| 186 |
|
| 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) |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#define rc_update_0(prob) \ |
| 199 |
do { \ |
| 200 |
rc.range = rc_bound; \ |
| 201 |
prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \ |
| 202 |
\ |
| 203 |
} while (0) |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
#define rc_update_1(prob) \ |
| 209 |
do { \ |
| 210 |
rc.range -= rc_bound; \ |
| 211 |
rc.code -= rc_bound; \ |
| 212 |
prob -= (prob) >> RC_MOVE_BITS; \ |
| 213 |
} while (0) |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#define rc_bit_last(prob, action0, action1) \ |
| 221 |
do { \ |
| 222 |
rc_if_0(prob) { \ |
| 223 |
rc_update_0(prob); \ |
| 224 |
action0; \ |
| 225 |
} else { \ |
| 226 |
rc_update_1(prob); \ |
| 227 |
action1; \ |
| 228 |
} \ |
| 229 |
} while (0) |
| 230 |
|
| 231 |
|
| 232 |
#define rc_bit_last_safe(prob, action0, action1, seq) \ |
| 233 |
do { \ |
| 234 |
rc_if_0_safe(prob, seq) { \ |
| 235 |
rc_update_0(prob); \ |
| 236 |
action0; \ |
| 237 |
} else { \ |
| 238 |
rc_update_1(prob); \ |
| 239 |
action1; \ |
| 240 |
} \ |
| 241 |
} while (0) |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#define rc_bit(prob, action0, action1) \ |
| 247 |
rc_bit_last(prob, \ |
| 248 |
symbol <<= 1; action0, \ |
| 249 |
symbol = (symbol << 1) + 1; action1); |
| 250 |
|
| 251 |
|
| 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, \ |
| 256 |
seq); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#define rc_bittree_bit(prob) \ |
| 269 |
rc_bit(prob, , ) |
| 270 |
|
| 271 |
#define rc_bittree3(probs, final_add) \ |
| 272 |
do { \ |
| 273 |
symbol = 1; \ |
| 274 |
rc_bittree_bit(probs[symbol]); \ |
| 275 |
rc_bittree_bit(probs[symbol]); \ |
| 276 |
rc_bittree_bit(probs[symbol]); \ |
| 277 |
symbol += (uint32_t)(final_add); \ |
| 278 |
} while (0) |
| 279 |
|
| 280 |
#define rc_bittree6(probs, final_add) \ |
| 281 |
do { \ |
| 282 |
symbol = 1; \ |
| 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); \ |
| 290 |
} while (0) |
| 291 |
|
| 292 |
#define rc_bittree8(probs, final_add) \ |
| 293 |
do { \ |
| 294 |
symbol = 1; \ |
| 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); \ |
| 304 |
} while (0) |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
#define rc_bittree_rev4(probs) \ |
| 309 |
do { \ |
| 310 |
symbol = 0; \ |
| 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); \ |
| 315 |
} while (0) |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
#define rc_bit_add_if_1(probs, dest, value_to_add_if_1) \ |
| 328 |
rc_bit(probs[symbol], \ |
| 329 |
, \ |
| 330 |
dest += value_to_add_if_1); |
| 331 |
|
| 332 |
|
| 333 |
|
| 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) |
| 341 |
|
| 342 |
#define rc_matched_literal(probs_base_var, match_byte) \ |
| 343 |
do { \ |
| 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; \ |
| 348 |
symbol = 1; \ |
| 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; \ |
| 357 |
} while (0) |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
#define rc_direct(dest, count_var) \ |
| 367 |
do { \ |
| 368 |
dest = (dest << 1) + 1; \ |
| 369 |
rc_normalize(); \ |
| 370 |
rc.range >>= 1; \ |
| 371 |
rc.code -= rc.range; \ |
| 372 |
rc_bound = UINT32_C(0) - (rc.code >> 31); \ |
| 373 |
dest += rc_bound; \ |
| 374 |
rc.code += rc.range & rc_bound; \ |
| 375 |
} while (--count_var > 0) |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
#define rc_direct_safe(dest, count_var, seq) \ |
| 380 |
do { \ |
| 381 |
rc_normalize_safe(seq); \ |
| 382 |
rc.range >>= 1; \ |
| 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) |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
#define rc_c_bit(prob, action_bit, action_neg) \ |
| 397 |
do { \ |
| 398 |
probability *p = &(prob); \ |
| 399 |
rc_normalize(); \ |
| 400 |
rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * *p; \ |
| 401 |
uint32_t rc_mask = rc.code >= rc_bound; \ |
| 402 |
action_bit; \ |
| 403 |
\ |
| 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; \ |
| 411 |
action_neg; \ |
| 412 |
rc_mask = ~rc_mask; \ |
| 413 |
rc_mask &= RC_BIT_MODEL_OFFSET; \ |
| 414 |
*p -= (*p + rc_mask) >> RC_MOVE_BITS; \ |
| 415 |
} while (0) |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
#if LZMA_RANGE_DECODER_CONFIG & 0x01 |
| 428 |
#undef rc_bittree_bit |
| 429 |
#define rc_bittree_bit(prob) \ |
| 430 |
rc_c_bit(prob, \ |
| 431 |
symbol = (symbol << 1) + rc_mask, \ |
| 432 |
) |
| 433 |
#endif |
| 434 |
|
| 435 |
#if LZMA_RANGE_DECODER_CONFIG & 0x02 |
| 436 |
#undef rc_bittree_rev4 |
| 437 |
#define rc_bittree_rev4(probs) \ |
| 438 |
do { \ |
| 439 |
symbol = 0; \ |
| 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, ); \ |
| 444 |
} while (0) |
| 445 |
#endif |
| 446 |
|
| 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 |
| 454 |
|
| 455 |
|
| 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 |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
#if LZMA_RANGE_DECODER_CONFIG & 0x1F0 |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
#define rc_asm_y(str) str |
| 477 |
#define rc_asm_n(str) |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
#define rc_asm_normalize \ |
| 490 |
"cmp %[top_value], %[range]\n\t" \ |
| 491 |
"jae 1f\n\t" \ |
| 492 |
"shl %[shift_bits], %[code]\n\t" \ |
| 493 |
"mov (%[in_ptr]), %b[code]\n\t" \ |
| 494 |
"shl %[shift_bits], %[range]\n\t" \ |
| 495 |
"inc %[in_ptr]\n" \ |
| 496 |
"1:\n" |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 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" |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
#define rc_asm_bittree(a, b, first_only, middle_only, last_only) \ |
| 594 |
first_only( \ |
| 595 |
"movzwl 2(%[probs_base]), %[prob" #a "]\n\t" \ |
| 596 |
"mov $2, %[symbol]\n\t" \ |
| 597 |
"movzwl 4(%[probs_base]), %[prob" #b "]\n\t" \ |
| 598 |
) \ |
| 599 |
middle_only( \ |
| 600 |
\ |
| 601 |
"movzwl (%[probs_base], %q[symbol], 4), %[prob" #b "]\n\t" \ |
| 602 |
) \ |
| 603 |
last_only( \ |
| 604 |
"add %[symbol], %[symbol]\n\t" \ |
| 605 |
) \ |
| 606 |
\ |
| 607 |
rc_asm_normalize \ |
| 608 |
rc_asm_calc("prob" #a) \ |
| 609 |
\ |
| 610 |
"cmovae %[t0], %[range]\n\t" \ |
| 611 |
\ |
| 612 |
first_only( \ |
| 613 |
"movzwl 6(%[probs_base]), %[t0]\n\t" \ |
| 614 |
"cmovae %[t0], %[prob" #b "]\n\t" \ |
| 615 |
) \ |
| 616 |
middle_only( \ |
| 617 |
"movzwl 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" \ |
| 620 |
) \ |
| 621 |
\ |
| 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" \ |
| 626 |
\ |
| 627 |
first_only( \ |
| 628 |
"sbb $-1, %[symbol]\n\t" \ |
| 629 |
) \ |
| 630 |
middle_only( \ |
| 631 |
"sbb $-1, %[symbol]\n\t" \ |
| 632 |
) \ |
| 633 |
last_only( \ |
| 634 |
"sbb %[last_sbb], %[symbol]\n\t" \ |
| 635 |
) \ |
| 636 |
\ |
| 637 |
"shr %[move_bits], %[t0]\n\t" \ |
| 638 |
"sub %[t0], %[prob" #a "]\n\t" \ |
| 639 |
\ |
| 640 |
"mov %w[prob" #a "], (%[probs_base], %q[t1], 1)\n\t" |
| 641 |
|
| 642 |
|
| 643 |
#define rc_asm_bittree_n(probs_base_var, final_add, asm_str) \ |
| 644 |
do { \ |
| 645 |
uint32_t t0; \ |
| 646 |
uint32_t t1; \ |
| 647 |
uint32_t t_prob0; \ |
| 648 |
uint32_t t_prob1; \ |
| 649 |
\ |
| 650 |
__asm__( \ |
| 651 |
asm_str \ |
| 652 |
: \ |
| 653 |
[range] "+&r"(rc.range), \ |
| 654 |
[code] "+&r"(rc.code), \ |
| 655 |
[t0] "=&r"(t0), \ |
| 656 |
[t1] "=&r"(t1), \ |
| 657 |
[prob0] "=&r"(t_prob0), \ |
| 658 |
[prob1] "=&r"(t_prob1), \ |
| 659 |
[symbol] "=&r"(symbol), \ |
| 660 |
[in_ptr] "+&r"(rc_in_ptr) \ |
| 661 |
: \ |
| 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) \ |
| 669 |
: \ |
| 670 |
"cc", "memory"); \ |
| 671 |
} while (0) |
| 672 |
|
| 673 |
|
| 674 |
#if LZMA_RANGE_DECODER_CONFIG & 0x010 |
| 675 |
#undef rc_bittree3 |
| 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) \ |
| 681 |
) |
| 682 |
|
| 683 |
#undef rc_bittree6 |
| 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) \ |
| 692 |
) |
| 693 |
|
| 694 |
#undef rc_bittree8 |
| 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) \ |
| 705 |
) |
| 706 |
#endif |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
#define rc_asm_bittree_rev(a, b, add, dcur, dnext0, dnext1, \ |
| 717 |
first_only, middle_only, last_only) \ |
| 718 |
first_only( \ |
| 719 |
"movzwl 2(%[probs_base]), %[prob" #a "]\n\t" \ |
| 720 |
"xor %[symbol], %[symbol]\n\t" \ |
| 721 |
"movzwl 4(%[probs_base]), %[prob" #b "]\n\t" \ |
| 722 |
) \ |
| 723 |
middle_only( \ |
| 724 |
"movzwl " #dnext0 "(%[probs_base], %q[symbol], 2), " \ |
| 725 |
"%[prob" #b "]\n\t" \ |
| 726 |
) \ |
| 727 |
\ |
| 728 |
rc_asm_normalize \ |
| 729 |
rc_asm_calc("prob" #a) \ |
| 730 |
\ |
| 731 |
"cmovae %[t0], %[range]\n\t" \ |
| 732 |
\ |
| 733 |
first_only( \ |
| 734 |
"movzwl 6(%[probs_base]), %[t0]\n\t" \ |
| 735 |
"cmovae %[t0], %[prob" #b "]\n\t" \ |
| 736 |
) \ |
| 737 |
middle_only( \ |
| 738 |
"movzwl " #dnext1 "(%[probs_base], %q[symbol], 2), %[t0]\n\t" \ |
| 739 |
"cmovae %[t0], %[prob" #b "]\n\t" \ |
| 740 |
) \ |
| 741 |
\ |
| 742 |
"lea " #add "(%q[symbol]), %[t0]\n\t" \ |
| 743 |
"cmovb %[t1], %[code]\n\t" \ |
| 744 |
middle_only( \ |
| 745 |
"mov %[symbol], %[t1]\n\t" \ |
| 746 |
) \ |
| 747 |
last_only( \ |
| 748 |
"mov %[symbol], %[t1]\n\t" \ |
| 749 |
) \ |
| 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" \ |
| 753 |
\ |
| 754 |
"shr %[move_bits], %[t0]\n\t" \ |
| 755 |
"sub %[t0], %[prob" #a "]\n\t" \ |
| 756 |
first_only( \ |
| 757 |
"mov %w[prob" #a "], 2(%[probs_base])\n\t" \ |
| 758 |
) \ |
| 759 |
middle_only( \ |
| 760 |
"mov %w[prob" #a "], " \ |
| 761 |
#dcur "(%[probs_base], %q[t1], 2)\n\t" \ |
| 762 |
) \ |
| 763 |
last_only( \ |
| 764 |
"mov %w[prob" #a "], " \ |
| 765 |
#dcur "(%[probs_base], %q[t1], 2)\n\t" \ |
| 766 |
) |
| 767 |
|
| 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) \ |
| 776 |
) |
| 777 |
#endif |
| 778 |
|
| 779 |
|
| 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) \ |
| 783 |
do { \ |
| 784 |
uint32_t t0; \ |
| 785 |
uint32_t t1; \ |
| 786 |
uint32_t t2 = (value_to_add_if_1); \ |
| 787 |
uint32_t t_prob; \ |
| 788 |
uint32_t t_index; \ |
| 789 |
\ |
| 790 |
__asm__( \ |
| 791 |
"movzwl (%[probs_base], %q[symbol], 2), %[prob]\n\t" \ |
| 792 |
"mov %[symbol], %[index]\n\t" \ |
| 793 |
\ |
| 794 |
"add %[dest], %[t2]\n\t" \ |
| 795 |
"add %[symbol], %[symbol]\n\t" \ |
| 796 |
\ |
| 797 |
rc_asm_normalize \ |
| 798 |
rc_asm_calc("prob") \ |
| 799 |
\ |
| 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" \ |
| 804 |
\ |
| 805 |
"cmovae %[t2], %[dest]\n\t" \ |
| 806 |
"sbb $-1, %[symbol]\n\t" \ |
| 807 |
\ |
| 808 |
"sar %[move_bits], %[t0]\n\t" \ |
| 809 |
"sub %[t0], %[prob]\n\t" \ |
| 810 |
"mov %w[prob], (%[probs_base], %q[index], 2)" \ |
| 811 |
: \ |
| 812 |
[range] "+&r"(rc.range), \ |
| 813 |
[code] "+&r"(rc.code), \ |
| 814 |
[t0] "=&r"(t0), \ |
| 815 |
[t1] "=&r"(t1), \ |
| 816 |
[prob] "=&r"(t_prob), \ |
| 817 |
[index] "=&r"(t_index), \ |
| 818 |
[symbol] "+&r"(symbol), \ |
| 819 |
[t2] "+&r"(t2), \ |
| 820 |
[dest] "+&r"(dest_var), \ |
| 821 |
[in_ptr] "+&r"(rc_in_ptr) \ |
| 822 |
: \ |
| 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) \ |
| 829 |
: \ |
| 830 |
"cc", "memory"); \ |
| 831 |
} while (0) |
| 832 |
#endif |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 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" \ |
| 846 |
\ |
| 847 |
"movzwl (%[probs_base], %q[symbol], 2), %[prob]\n\t" \ |
| 848 |
\ |
| 849 |
"add %[symbol], %[symbol]\n\t" \ |
| 850 |
\ |
| 851 |
nonlast_only( \ |
| 852 |
"xor %[match_bit], %[offset]\n\t" \ |
| 853 |
"add %[match_byte], %[match_byte]\n\t" \ |
| 854 |
) \ |
| 855 |
\ |
| 856 |
rc_asm_normalize \ |
| 857 |
rc_asm_calc("prob") \ |
| 858 |
\ |
| 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" \ |
| 864 |
\ |
| 865 |
nonlast_only( \ |
| 866 |
"cmovae %[match_bit], %[offset]\n\t" \ |
| 867 |
"mov %[match_byte], %[match_bit]\n\t" \ |
| 868 |
) \ |
| 869 |
\ |
| 870 |
"sbb $-1, %[symbol]\n\t" \ |
| 871 |
\ |
| 872 |
"shr %[move_bits], %[t0]\n\t" \ |
| 873 |
\ |
| 874 |
"and $0x1FF, %[symbol]\n\t" \ |
| 875 |
"sub %[t0], %[prob]\n\t" \ |
| 876 |
\ |
| 877 |
\ |
| 878 |
"mov %w[prob], (%[probs_base], %q[t1], 1)\n\t" |
| 879 |
|
| 880 |
|
| 881 |
#if LZMA_RANGE_DECODER_CONFIG & 0x080 |
| 882 |
#undef rc_matched_literal |
| 883 |
#define rc_matched_literal(probs_base_var, match_byte_value) \ |
| 884 |
do { \ |
| 885 |
uint32_t t0; \ |
| 886 |
uint32_t t1; \ |
| 887 |
uint32_t t_prob; \ |
| 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; \ |
| 891 |
symbol = 1; \ |
| 892 |
\ |
| 893 |
__asm__( \ |
| 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) \ |
| 902 |
: \ |
| 903 |
[range] "+&r"(rc.range), \ |
| 904 |
[code] "+&r"(rc.code), \ |
| 905 |
[t0] "=&r"(t0), \ |
| 906 |
[t1] "=&r"(t1), \ |
| 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) \ |
| 913 |
: \ |
| 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) \ |
| 920 |
: \ |
| 921 |
"cc", "memory"); \ |
| 922 |
} while (0) |
| 923 |
#endif |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
#if LZMA_RANGE_DECODER_CONFIG & 0x100 |
| 928 |
#undef rc_direct |
| 929 |
#define rc_direct(dest_var, count_var) \ |
| 930 |
do { \ |
| 931 |
uint32_t t0; \ |
| 932 |
uint32_t t1; \ |
| 933 |
\ |
| 934 |
__asm__( \ |
| 935 |
"2:\n\t" \ |
| 936 |
"add %[dest], %[dest]\n\t" \ |
| 937 |
"lea 1(%q[dest]), %[t1]\n\t" \ |
| 938 |
\ |
| 939 |
rc_asm_normalize \ |
| 940 |
\ |
| 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" \ |
| 946 |
"dec %[count]\n\t" \ |
| 947 |
"jnz 2b\n\t" \ |
| 948 |
: \ |
| 949 |
[range] "+&r"(rc.range), \ |
| 950 |
[code] "+&r"(rc.code), \ |
| 951 |
[t0] "=&r"(t0), \ |
| 952 |
[t1] "=&r"(t1), \ |
| 953 |
[dest] "+&r"(dest_var), \ |
| 954 |
[count] "+&r"(count_var), \ |
| 955 |
[in_ptr] "+&r"(rc_in_ptr) \ |
| 956 |
: \ |
| 957 |
[top_value] "n"(RC_TOP_VALUE), \ |
| 958 |
[shift_bits] "n"(RC_SHIFT_BITS) \ |
| 959 |
: \ |
| 960 |
"cc", "memory"); \ |
| 961 |
} while (0) |
| 962 |
#endif |
| 963 |
|
| 964 |
#endif |
| 965 |
|
| 966 |
#endif |