Path: blob/master/Utilities/cmliblzma/liblzma/rangecoder/range_common.h
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file range_common.h5/// \brief Common things for range encoder and decoder6///7// Authors: Igor Pavlov8// Lasse Collin9//10///////////////////////////////////////////////////////////////////////////////1112#ifndef LZMA_RANGE_COMMON_H13#define LZMA_RANGE_COMMON_H1415// Skip common.h if building price_tablegen.c.16#ifndef BUILDING_PRICE_TABLEGEN17# include "common.h"18#endif192021///////////////22// Constants //23///////////////2425#define RC_SHIFT_BITS 826#define RC_TOP_BITS 2427#define RC_TOP_VALUE (UINT32_C(1) << RC_TOP_BITS)28#define RC_BIT_MODEL_TOTAL_BITS 1129#define RC_BIT_MODEL_TOTAL (UINT32_C(1) << RC_BIT_MODEL_TOTAL_BITS)30#define RC_MOVE_BITS 5313233////////////34// Macros //35////////////3637// Resets the probability so that both 0 and 1 have probability of 50 %38#define bit_reset(prob) \39prob = RC_BIT_MODEL_TOTAL >> 14041// This does the same for a complete bit tree.42// (A tree represented as an array.)43#define bittree_reset(probs, bit_levels) \44for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \45bit_reset((probs)[bt_i])464748//////////////////////49// Type definitions //50//////////////////////5152/// \brief Type of probabilities used with range coder53///54/// This needs to be at least 12-bit integer, so uint16_t is a logical choice.55/// However, on some architecture and compiler combinations, a bigger type56/// may give better speed, because the probability variables are accessed57/// a lot. On the other hand, bigger probability type increases cache58/// footprint, since there are 2 to 14 thousand probability variables in59/// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there60/// would be about 1.5 million variables).61///62/// With malicious files, the initialization speed of the LZMA decoder can63/// become important. In that case, smaller probability variables mean that64/// there is less bytes to write to RAM, which makes initialization faster.65/// With big probability type, the initialization can become so slow that it66/// can be a problem e.g. for email servers doing virus scanning.67///68/// I will be sticking to uint16_t unless some specific architectures69/// are *much* faster (20-50 %) with uint32_t.70///71/// Update in 2024: The branchless C and x86-64 assembly was written so that72/// probability is assumed to be uint16_t. (In contrast, LZMA SDK 23.0173/// assembly supports both types.)74typedef uint16_t probability;7576#endif777879