21static unsigned const kInverseProbabilityLog256[256] = {
22 0, 2048, 1792, 1642, 1536, 1453, 1386, 1329, 1280, 1236, 1197, 1162,
23 1130, 1100, 1073, 1047, 1024, 1001, 980, 960, 941, 923, 906, 889,
24 874, 859, 844, 830, 817, 804, 791, 779, 768, 756, 745, 734,
25 724, 714, 704, 694, 685, 676, 667, 658, 650, 642, 633, 626,
26 618, 610, 603, 595, 588, 581, 574, 567, 561, 554, 548, 542,
27 535, 529, 523, 517, 512, 506, 500, 495, 489, 484, 478, 473,
28 468, 463, 458, 453, 448, 443, 438, 434, 429, 424, 420, 415,
29 411, 407, 402, 398, 394, 390, 386, 382, 377, 373, 370, 366,
30 362, 358, 354, 350, 347, 343, 339, 336, 332, 329, 325, 322,
31 318, 315, 311, 308, 305, 302, 298, 295, 292, 289, 286, 282,
32 279, 276, 273, 270, 267, 264, 261, 258, 256, 253, 250, 247,
33 244, 241, 239, 236, 233, 230, 228, 225, 222, 220, 217, 215,
34 212, 209, 207, 204, 202, 199, 197, 194, 192, 190, 187, 185,
35 182, 180, 178, 175, 173, 171, 168, 166, 164, 162, 159, 157,
36 155, 153, 151, 149, 146, 144, 142, 140, 138, 136, 134, 132,
37 130, 128, 126, 123, 121, 119, 117, 115, 114, 112, 110, 108,
38 106, 104, 102, 100, 98, 96, 94, 93, 91, 89, 87, 85,
39 83, 82, 80, 78, 76, 74, 73, 71, 69, 67, 66, 64,
40 62, 61, 59, 57, 55, 54, 52, 50, 49, 47, 46, 44,
41 42, 41, 39, 37, 36, 34, 33, 31, 30, 28, 26, 25,
42 23, 22, 20, 19, 17, 16, 14, 13, 11, 10, 8, 7,
46static unsigned ZSTD_getFSEMaxSymbolValue(
FSE_CTable const* ctable) {
47 void const* ptr = ctable;
48 U16 const* u16ptr = (
U16 const*)ptr;
50 return maxSymbolValue;
57static unsigned ZSTD_useLowProbCount(
size_t const nbSeq)
70static size_t ZSTD_NCountCost(
unsigned const* count,
unsigned const max,
71 size_t const nbSeq,
unsigned const FSELog)
73 BYTE wksp[FSE_NCOUNTBOUND];
84static size_t ZSTD_entropyCost(
unsigned const* count,
unsigned const max,
size_t const total)
90 for (s = 0; s <=
max; ++s) {
91 unsigned norm = (unsigned)((256 * count[s]) /
total);
92 if (count[s] != 0 && norm == 0)
95 cost +=
count[s] * kInverseProbabilityLog256[norm];
106 unsigned const* count,
109 unsigned const kAccuracyLog = 8;
113 FSE_initCState(&cstate, ctable);
114 if (ZSTD_getFSEMaxSymbolValue(ctable) < max) {
115 DEBUGLOG(5,
"Repeat FSE_CTable has maxSymbolValue %u < %u",
116 ZSTD_getFSEMaxSymbolValue(ctable), max);
117 return ERROR(GENERIC);
119 for (s = 0; s <= max; ++s) {
120 unsigned const tableLog = cstate.stateLog;
121 unsigned const badCost = (tableLog + 1) << kAccuracyLog;
122 unsigned const bitCost = FSE_bitCost(cstate.symbolTT, tableLog, s, kAccuracyLog);
125 if (bitCost >= badCost) {
126 DEBUGLOG(5,
"Repeat FSE_CTable has Prob[%u] == 0", s);
127 return ERROR(GENERIC);
129 cost += (size_t)count[s] * bitCost;
131 return cost >> kAccuracyLog;
140 unsigned const* count,
unsigned const max)
142 unsigned const shift = 8 - accuracyLog;
146 for (s = 0; s <= max; ++s) {
147 unsigned const normAcc = (norm[s] != -1) ? (
unsigned)norm[s] : 1;
148 unsigned const norm256 = normAcc << shift;
151 cost += count[s] * kInverseProbabilityLog256[norm256];
158 FSE_repeat* repeatMode,
unsigned const* count,
unsigned const max,
159 size_t const mostFrequent,
size_t nbSeq,
unsigned const FSELog,
161 short const* defaultNorm,
U32 defaultNormLog,
166 if (mostFrequent == nbSeq) {
167 *repeatMode = FSE_repeat_none;
168 if (isDefaultAllowed && nbSeq <= 2) {
180 if (isDefaultAllowed) {
181 size_t const staticFse_nbSeq_max = 1000;
182 size_t const mult = 10 - strategy;
183 size_t const baseLog = 3;
184 size_t const dynamicFse_nbSeq_min = (((size_t)1 << defaultNormLog) * mult) >> baseLog;
185 assert(defaultNormLog >= 5 && defaultNormLog <= 6);
186 assert(mult <= 9 && mult >= 7);
187 if ( (*repeatMode == FSE_repeat_valid)
188 && (nbSeq < staticFse_nbSeq_max) ) {
192 if ( (nbSeq < dynamicFse_nbSeq_min)
193 || (mostFrequent < (nbSeq >> (defaultNormLog-1))) ) {
201 *repeatMode = FSE_repeat_none;
207 size_t const repeatCost = *repeatMode != FSE_repeat_none ?
ZSTD_fseBitCost(prevCTable, count, max) :
ERROR(GENERIC);
208 size_t const NCountCost = ZSTD_NCountCost(count, max, nbSeq, FSELog);
209 size_t const compressedCost = (NCountCost << 3) + ZSTD_entropyCost(count, max, nbSeq);
211 if (isDefaultAllowed) {
217 DEBUGLOG(5,
"Estimated bit costs: basic=%u\trepeat=%u\tcompressed=%u",
218 (
unsigned)basicCost, (
unsigned)repeatCost, (
unsigned)compressedCost);
219 if (basicCost <= repeatCost && basicCost <= compressedCost) {
222 *repeatMode = FSE_repeat_none;
225 if (repeatCost <= compressedCost) {
230 assert(compressedCost < basicCost && compressedCost < repeatCost);
232 DEBUGLOG(5,
"Selected set_compressed");
233 *repeatMode = FSE_repeat_check;
245 unsigned* count,
U32 max,
246 const BYTE* codeTable,
size_t nbSeq,
247 const S16* defaultNorm,
U32 defaultNormLog,
U32 defaultMax,
248 const FSE_CTable* prevCTable,
size_t prevCTableSize,
249 void* entropyWorkspace,
size_t entropyWorkspaceSize)
252 const BYTE*
const oend = op + dstCapacity;
253 DEBUGLOG(6,
"ZSTD_buildCTable (dstCapacity=%u)", (
unsigned)dstCapacity);
262 ZSTD_memcpy(nextCTable, prevCTable, prevCTableSize);
269 size_t nbSeq_1 = nbSeq;
271 if (count[codeTable[nbSeq-1]] > 1) {
272 count[codeTable[nbSeq-1]]--;
277 (void)entropyWorkspaceSize;
280 {
size_t const NCountSize =
FSE_writeNCount(op, (
size_t)(oend - op), wksp->
norm, max, tableLog);
292 void* dst,
size_t dstCapacity,
296 seqDef const* sequences,
size_t nbSeq,
int longOffsets)
299 FSE_CState_t stateMatchLength;
300 FSE_CState_t stateOffsetBits;
301 FSE_CState_t stateLitLength;
305 dstSize_tooSmall,
"not enough space remaining");
306 DEBUGLOG(6,
"available space for bitstream : %i (dstCapacity=%u)",
308 (
unsigned)dstCapacity);
311 FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]);
312 FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]);
313 FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]);
314 BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]);
316 BIT_addBits(&blockStream, sequences[nbSeq-1].mlBase, ML_bits[mlCodeTable[nbSeq-1]]);
319 U32 const ofBits = ofCodeTable[nbSeq-1];
322 BIT_addBits(&blockStream, sequences[nbSeq-1].offBase, extraBits);
325 BIT_addBits(&blockStream, sequences[nbSeq-1].offBase >> extraBits,
328 BIT_addBits(&blockStream, sequences[nbSeq-1].offBase, ofCodeTable[nbSeq-1]);
333 for (n=nbSeq-2 ; n<nbSeq ; n--) {
334 BYTE const llCode = llCodeTable[n];
335 BYTE const ofCode = ofCodeTable[n];
336 BYTE const mlCode = mlCodeTable[n];
337 U32 const llBits = LL_bits[llCode];
338 U32 const ofBits = ofCode;
339 U32 const mlBits = ML_bits[mlCode];
340 DEBUGLOG(6,
"encoding: litlen:%2u - matchlen:%2u - offCode:%7u",
341 (
unsigned)sequences[n].litLength,
342 (
unsigned)sequences[n].mlBase +
MINMATCH,
343 (
unsigned)sequences[n].offBase);
346 FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode);
347 FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode);
349 FSE_encodeSymbol(&blockStream, &stateLitLength, llCode);
352 BIT_addBits(&blockStream, sequences[n].litLength, llBits);
354 BIT_addBits(&blockStream, sequences[n].mlBase, mlBits);
359 BIT_addBits(&blockStream, sequences[n].offBase, extraBits);
362 BIT_addBits(&blockStream, sequences[n].offBase >> extraBits,
365 BIT_addBits(&blockStream, sequences[n].offBase, ofBits);
368 DEBUGLOG(7,
"remaining space : %i", (
int)(blockStream.
endPtr - blockStream.
ptr));
371 DEBUGLOG(6,
"ZSTD_encodeSequences: flushing ML state with %u bits", stateMatchLength.stateLog);
372 FSE_flushCState(&blockStream, &stateMatchLength);
373 DEBUGLOG(6,
"ZSTD_encodeSequences: flushing Off state with %u bits", stateOffsetBits.stateLog);
374 FSE_flushCState(&blockStream, &stateOffsetBits);
375 DEBUGLOG(6,
"ZSTD_encodeSequences: flushing LL state with %u bits", stateLitLength.stateLog);
376 FSE_flushCState(&blockStream, &stateLitLength);
385ZSTD_encodeSequences_default(
386 void* dst,
size_t dstCapacity,
390 seqDef const* sequences,
size_t nbSeq,
int longOffsets)
393 CTable_MatchLength, mlCodeTable,
394 CTable_OffsetBits, ofCodeTable,
395 CTable_LitLength, llCodeTable,
396 sequences, nbSeq, longOffsets);
403ZSTD_encodeSequences_bmi2(
404 void* dst,
size_t dstCapacity,
408 seqDef const* sequences,
size_t nbSeq,
int longOffsets)
411 CTable_MatchLength, mlCodeTable,
412 CTable_OffsetBits, ofCodeTable,
413 CTable_LitLength, llCodeTable,
414 sequences, nbSeq, longOffsets);
420 void* dst,
size_t dstCapacity,
424 seqDef const* sequences,
size_t nbSeq,
int longOffsets,
int bmi2)
426 DEBUGLOG(5,
"ZSTD_encodeSequences: dstCapacity = %u", (
unsigned)dstCapacity);
429 return ZSTD_encodeSequences_bmi2(dst, dstCapacity,
430 CTable_MatchLength, mlCodeTable,
431 CTable_OffsetBits, ofCodeTable,
432 CTable_LitLength, llCodeTable,
433 sequences, nbSeq, longOffsets);
437 return ZSTD_encodeSequences_default(dst, dstCapacity,
438 CTable_MatchLength, mlCodeTable,
439 CTable_OffsetBits, ofCodeTable,
440 CTable_LitLength, llCodeTable,
441 sequences, nbSeq, longOffsets);
MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC)
MEM_STATIC void BIT_flushBits(BIT_CStream_t *bitC)
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity)
MEM_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
#define STREAM_ACCUMULATOR_MIN
#define BMI2_TARGET_ATTRIBUTE
#define FORCE_INLINE_TEMPLATE
#define assert(condition)
#define RETURN_ERROR(err,...)
#define FORWARD_IF_ERROR(err,...)
ERR_STATIC unsigned ERR_isError(size_t code)
#define RETURN_ERROR_IF(cond, err,...)
FSE_PUBLIC_API size_t FSE_writeNCount(void *buffer, size_t bufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
FSE_PUBLIC_API size_t FSE_normalizeCount(short *normalizedCounter, unsigned tableLog, const unsigned *count, size_t srcSize, unsigned maxSymbolValue, unsigned useLowProbCount)
FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
size_t FSE_buildCTable_wksp(FSE_CTable *ct, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize)
size_t FSE_buildCTable_rle(FSE_CTable *ct, BYTE symbolValue)
MEM_STATIC unsigned MEM_32bits(void)
MEM_STATIC U16 MEM_read16(const void *memPtr)
constexpr dcon::demographics_key total(0)
MOD_PROV_LIST constexpr uint32_t count
U32 wksp[FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(MaxSeq, MaxFSELog)]
size_t ZSTD_crossEntropyCost(short const *norm, unsigned accuracyLog, unsigned const *count, unsigned const max)
FORCE_INLINE_TEMPLATE size_t ZSTD_encodeSequences_body(void *dst, size_t dstCapacity, FSE_CTable const *CTable_MatchLength, BYTE const *mlCodeTable, FSE_CTable const *CTable_OffsetBits, BYTE const *ofCodeTable, FSE_CTable const *CTable_LitLength, BYTE const *llCodeTable, seqDef const *sequences, size_t nbSeq, int longOffsets)
size_t ZSTD_fseBitCost(FSE_CTable const *ctable, unsigned const *count, unsigned const max)
size_t ZSTD_buildCTable(void *dst, size_t dstCapacity, FSE_CTable *nextCTable, U32 FSELog, symbolEncodingType_e type, unsigned *count, U32 max, const BYTE *codeTable, size_t nbSeq, const S16 *defaultNorm, U32 defaultNormLog, U32 defaultMax, const FSE_CTable *prevCTable, size_t prevCTableSize, void *entropyWorkspace, size_t entropyWorkspaceSize)
symbolEncodingType_e ZSTD_selectEncodingType(FSE_repeat *repeatMode, unsigned const *count, unsigned const max, size_t const mostFrequent, size_t nbSeq, unsigned const FSELog, FSE_CTable const *prevCTable, short const *defaultNorm, U32 defaultNormLog, ZSTD_defaultPolicy_e const isDefaultAllowed, ZSTD_strategy const strategy)
size_t ZSTD_encodeSequences(void *dst, size_t dstCapacity, FSE_CTable const *CTable_MatchLength, BYTE const *mlCodeTable, FSE_CTable const *CTable_OffsetBits, BYTE const *ofCodeTable, FSE_CTable const *CTable_LitLength, BYTE const *llCodeTable, seqDef const *sequences, size_t nbSeq, int longOffsets, int bmi2)
#define ZSTD_memcpy(d, s, l)
#define ZSTD_STATIC_ASSERT(c)