1*01826a49SYabin Cui /*
2*01826a49SYabin Cui * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui * All rights reserved.
4*01826a49SYabin Cui *
5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui */
10*01826a49SYabin Cui
11*01826a49SYabin Cui /*-*************************************
12*01826a49SYabin Cui * Dependencies
13*01826a49SYabin Cui ***************************************/
14*01826a49SYabin Cui #include "zstd_compress_sequences.h"
15*01826a49SYabin Cui
16*01826a49SYabin Cui /**
17*01826a49SYabin Cui * -log2(x / 256) lookup table for x in [0, 256).
18*01826a49SYabin Cui * If x == 0: Return 0
19*01826a49SYabin Cui * Else: Return floor(-log2(x / 256) * 256)
20*01826a49SYabin Cui */
21*01826a49SYabin Cui static unsigned const kInverseProbabilityLog256[256] = {
22*01826a49SYabin Cui 0, 2048, 1792, 1642, 1536, 1453, 1386, 1329, 1280, 1236, 1197, 1162,
23*01826a49SYabin Cui 1130, 1100, 1073, 1047, 1024, 1001, 980, 960, 941, 923, 906, 889,
24*01826a49SYabin Cui 874, 859, 844, 830, 817, 804, 791, 779, 768, 756, 745, 734,
25*01826a49SYabin Cui 724, 714, 704, 694, 685, 676, 667, 658, 650, 642, 633, 626,
26*01826a49SYabin Cui 618, 610, 603, 595, 588, 581, 574, 567, 561, 554, 548, 542,
27*01826a49SYabin Cui 535, 529, 523, 517, 512, 506, 500, 495, 489, 484, 478, 473,
28*01826a49SYabin Cui 468, 463, 458, 453, 448, 443, 438, 434, 429, 424, 420, 415,
29*01826a49SYabin Cui 411, 407, 402, 398, 394, 390, 386, 382, 377, 373, 370, 366,
30*01826a49SYabin Cui 362, 358, 354, 350, 347, 343, 339, 336, 332, 329, 325, 322,
31*01826a49SYabin Cui 318, 315, 311, 308, 305, 302, 298, 295, 292, 289, 286, 282,
32*01826a49SYabin Cui 279, 276, 273, 270, 267, 264, 261, 258, 256, 253, 250, 247,
33*01826a49SYabin Cui 244, 241, 239, 236, 233, 230, 228, 225, 222, 220, 217, 215,
34*01826a49SYabin Cui 212, 209, 207, 204, 202, 199, 197, 194, 192, 190, 187, 185,
35*01826a49SYabin Cui 182, 180, 178, 175, 173, 171, 168, 166, 164, 162, 159, 157,
36*01826a49SYabin Cui 155, 153, 151, 149, 146, 144, 142, 140, 138, 136, 134, 132,
37*01826a49SYabin Cui 130, 128, 126, 123, 121, 119, 117, 115, 114, 112, 110, 108,
38*01826a49SYabin Cui 106, 104, 102, 100, 98, 96, 94, 93, 91, 89, 87, 85,
39*01826a49SYabin Cui 83, 82, 80, 78, 76, 74, 73, 71, 69, 67, 66, 64,
40*01826a49SYabin Cui 62, 61, 59, 57, 55, 54, 52, 50, 49, 47, 46, 44,
41*01826a49SYabin Cui 42, 41, 39, 37, 36, 34, 33, 31, 30, 28, 26, 25,
42*01826a49SYabin Cui 23, 22, 20, 19, 17, 16, 14, 13, 11, 10, 8, 7,
43*01826a49SYabin Cui 5, 4, 2, 1,
44*01826a49SYabin Cui };
45*01826a49SYabin Cui
ZSTD_getFSEMaxSymbolValue(FSE_CTable const * ctable)46*01826a49SYabin Cui static unsigned ZSTD_getFSEMaxSymbolValue(FSE_CTable const* ctable) {
47*01826a49SYabin Cui void const* ptr = ctable;
48*01826a49SYabin Cui U16 const* u16ptr = (U16 const*)ptr;
49*01826a49SYabin Cui U32 const maxSymbolValue = MEM_read16(u16ptr + 1);
50*01826a49SYabin Cui return maxSymbolValue;
51*01826a49SYabin Cui }
52*01826a49SYabin Cui
53*01826a49SYabin Cui /**
54*01826a49SYabin Cui * Returns true if we should use ncount=-1 else we should
55*01826a49SYabin Cui * use ncount=1 for low probability symbols instead.
56*01826a49SYabin Cui */
ZSTD_useLowProbCount(size_t const nbSeq)57*01826a49SYabin Cui static unsigned ZSTD_useLowProbCount(size_t const nbSeq)
58*01826a49SYabin Cui {
59*01826a49SYabin Cui /* Heuristic: This should cover most blocks <= 16K and
60*01826a49SYabin Cui * start to fade out after 16K to about 32K depending on
61*01826a49SYabin Cui * compressibility.
62*01826a49SYabin Cui */
63*01826a49SYabin Cui return nbSeq >= 2048;
64*01826a49SYabin Cui }
65*01826a49SYabin Cui
66*01826a49SYabin Cui /**
67*01826a49SYabin Cui * Returns the cost in bytes of encoding the normalized count header.
68*01826a49SYabin Cui * Returns an error if any of the helper functions return an error.
69*01826a49SYabin Cui */
ZSTD_NCountCost(unsigned const * count,unsigned const max,size_t const nbSeq,unsigned const FSELog)70*01826a49SYabin Cui static size_t ZSTD_NCountCost(unsigned const* count, unsigned const max,
71*01826a49SYabin Cui size_t const nbSeq, unsigned const FSELog)
72*01826a49SYabin Cui {
73*01826a49SYabin Cui BYTE wksp[FSE_NCOUNTBOUND];
74*01826a49SYabin Cui S16 norm[MaxSeq + 1];
75*01826a49SYabin Cui const U32 tableLog = FSE_optimalTableLog(FSELog, nbSeq, max);
76*01826a49SYabin Cui FORWARD_IF_ERROR(FSE_normalizeCount(norm, tableLog, count, nbSeq, max, ZSTD_useLowProbCount(nbSeq)), "");
77*01826a49SYabin Cui return FSE_writeNCount(wksp, sizeof(wksp), norm, max, tableLog);
78*01826a49SYabin Cui }
79*01826a49SYabin Cui
80*01826a49SYabin Cui /**
81*01826a49SYabin Cui * Returns the cost in bits of encoding the distribution described by count
82*01826a49SYabin Cui * using the entropy bound.
83*01826a49SYabin Cui */
ZSTD_entropyCost(unsigned const * count,unsigned const max,size_t const total)84*01826a49SYabin Cui static size_t ZSTD_entropyCost(unsigned const* count, unsigned const max, size_t const total)
85*01826a49SYabin Cui {
86*01826a49SYabin Cui unsigned cost = 0;
87*01826a49SYabin Cui unsigned s;
88*01826a49SYabin Cui
89*01826a49SYabin Cui assert(total > 0);
90*01826a49SYabin Cui for (s = 0; s <= max; ++s) {
91*01826a49SYabin Cui unsigned norm = (unsigned)((256 * count[s]) / total);
92*01826a49SYabin Cui if (count[s] != 0 && norm == 0)
93*01826a49SYabin Cui norm = 1;
94*01826a49SYabin Cui assert(count[s] < total);
95*01826a49SYabin Cui cost += count[s] * kInverseProbabilityLog256[norm];
96*01826a49SYabin Cui }
97*01826a49SYabin Cui return cost >> 8;
98*01826a49SYabin Cui }
99*01826a49SYabin Cui
100*01826a49SYabin Cui /**
101*01826a49SYabin Cui * Returns the cost in bits of encoding the distribution in count using ctable.
102*01826a49SYabin Cui * Returns an error if ctable cannot represent all the symbols in count.
103*01826a49SYabin Cui */
ZSTD_fseBitCost(FSE_CTable const * ctable,unsigned const * count,unsigned const max)104*01826a49SYabin Cui size_t ZSTD_fseBitCost(
105*01826a49SYabin Cui FSE_CTable const* ctable,
106*01826a49SYabin Cui unsigned const* count,
107*01826a49SYabin Cui unsigned const max)
108*01826a49SYabin Cui {
109*01826a49SYabin Cui unsigned const kAccuracyLog = 8;
110*01826a49SYabin Cui size_t cost = 0;
111*01826a49SYabin Cui unsigned s;
112*01826a49SYabin Cui FSE_CState_t cstate;
113*01826a49SYabin Cui FSE_initCState(&cstate, ctable);
114*01826a49SYabin Cui if (ZSTD_getFSEMaxSymbolValue(ctable) < max) {
115*01826a49SYabin Cui DEBUGLOG(5, "Repeat FSE_CTable has maxSymbolValue %u < %u",
116*01826a49SYabin Cui ZSTD_getFSEMaxSymbolValue(ctable), max);
117*01826a49SYabin Cui return ERROR(GENERIC);
118*01826a49SYabin Cui }
119*01826a49SYabin Cui for (s = 0; s <= max; ++s) {
120*01826a49SYabin Cui unsigned const tableLog = cstate.stateLog;
121*01826a49SYabin Cui unsigned const badCost = (tableLog + 1) << kAccuracyLog;
122*01826a49SYabin Cui unsigned const bitCost = FSE_bitCost(cstate.symbolTT, tableLog, s, kAccuracyLog);
123*01826a49SYabin Cui if (count[s] == 0)
124*01826a49SYabin Cui continue;
125*01826a49SYabin Cui if (bitCost >= badCost) {
126*01826a49SYabin Cui DEBUGLOG(5, "Repeat FSE_CTable has Prob[%u] == 0", s);
127*01826a49SYabin Cui return ERROR(GENERIC);
128*01826a49SYabin Cui }
129*01826a49SYabin Cui cost += (size_t)count[s] * bitCost;
130*01826a49SYabin Cui }
131*01826a49SYabin Cui return cost >> kAccuracyLog;
132*01826a49SYabin Cui }
133*01826a49SYabin Cui
134*01826a49SYabin Cui /**
135*01826a49SYabin Cui * Returns the cost in bits of encoding the distribution in count using the
136*01826a49SYabin Cui * table described by norm. The max symbol support by norm is assumed >= max.
137*01826a49SYabin Cui * norm must be valid for every symbol with non-zero probability in count.
138*01826a49SYabin Cui */
ZSTD_crossEntropyCost(short const * norm,unsigned accuracyLog,unsigned const * count,unsigned const max)139*01826a49SYabin Cui size_t ZSTD_crossEntropyCost(short const* norm, unsigned accuracyLog,
140*01826a49SYabin Cui unsigned const* count, unsigned const max)
141*01826a49SYabin Cui {
142*01826a49SYabin Cui unsigned const shift = 8 - accuracyLog;
143*01826a49SYabin Cui size_t cost = 0;
144*01826a49SYabin Cui unsigned s;
145*01826a49SYabin Cui assert(accuracyLog <= 8);
146*01826a49SYabin Cui for (s = 0; s <= max; ++s) {
147*01826a49SYabin Cui unsigned const normAcc = (norm[s] != -1) ? (unsigned)norm[s] : 1;
148*01826a49SYabin Cui unsigned const norm256 = normAcc << shift;
149*01826a49SYabin Cui assert(norm256 > 0);
150*01826a49SYabin Cui assert(norm256 < 256);
151*01826a49SYabin Cui cost += count[s] * kInverseProbabilityLog256[norm256];
152*01826a49SYabin Cui }
153*01826a49SYabin Cui return cost >> 8;
154*01826a49SYabin Cui }
155*01826a49SYabin Cui
156*01826a49SYabin Cui 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)157*01826a49SYabin Cui ZSTD_selectEncodingType(
158*01826a49SYabin Cui FSE_repeat* repeatMode, unsigned const* count, unsigned const max,
159*01826a49SYabin Cui size_t const mostFrequent, size_t nbSeq, unsigned const FSELog,
160*01826a49SYabin Cui FSE_CTable const* prevCTable,
161*01826a49SYabin Cui short const* defaultNorm, U32 defaultNormLog,
162*01826a49SYabin Cui ZSTD_defaultPolicy_e const isDefaultAllowed,
163*01826a49SYabin Cui ZSTD_strategy const strategy)
164*01826a49SYabin Cui {
165*01826a49SYabin Cui ZSTD_STATIC_ASSERT(ZSTD_defaultDisallowed == 0 && ZSTD_defaultAllowed != 0);
166*01826a49SYabin Cui if (mostFrequent == nbSeq) {
167*01826a49SYabin Cui *repeatMode = FSE_repeat_none;
168*01826a49SYabin Cui if (isDefaultAllowed && nbSeq <= 2) {
169*01826a49SYabin Cui /* Prefer set_basic over set_rle when there are 2 or fewer symbols,
170*01826a49SYabin Cui * since RLE uses 1 byte, but set_basic uses 5-6 bits per symbol.
171*01826a49SYabin Cui * If basic encoding isn't possible, always choose RLE.
172*01826a49SYabin Cui */
173*01826a49SYabin Cui DEBUGLOG(5, "Selected set_basic");
174*01826a49SYabin Cui return set_basic;
175*01826a49SYabin Cui }
176*01826a49SYabin Cui DEBUGLOG(5, "Selected set_rle");
177*01826a49SYabin Cui return set_rle;
178*01826a49SYabin Cui }
179*01826a49SYabin Cui if (strategy < ZSTD_lazy) {
180*01826a49SYabin Cui if (isDefaultAllowed) {
181*01826a49SYabin Cui size_t const staticFse_nbSeq_max = 1000;
182*01826a49SYabin Cui size_t const mult = 10 - strategy;
183*01826a49SYabin Cui size_t const baseLog = 3;
184*01826a49SYabin Cui size_t const dynamicFse_nbSeq_min = (((size_t)1 << defaultNormLog) * mult) >> baseLog; /* 28-36 for offset, 56-72 for lengths */
185*01826a49SYabin Cui assert(defaultNormLog >= 5 && defaultNormLog <= 6); /* xx_DEFAULTNORMLOG */
186*01826a49SYabin Cui assert(mult <= 9 && mult >= 7);
187*01826a49SYabin Cui if ( (*repeatMode == FSE_repeat_valid)
188*01826a49SYabin Cui && (nbSeq < staticFse_nbSeq_max) ) {
189*01826a49SYabin Cui DEBUGLOG(5, "Selected set_repeat");
190*01826a49SYabin Cui return set_repeat;
191*01826a49SYabin Cui }
192*01826a49SYabin Cui if ( (nbSeq < dynamicFse_nbSeq_min)
193*01826a49SYabin Cui || (mostFrequent < (nbSeq >> (defaultNormLog-1))) ) {
194*01826a49SYabin Cui DEBUGLOG(5, "Selected set_basic");
195*01826a49SYabin Cui /* The format allows default tables to be repeated, but it isn't useful.
196*01826a49SYabin Cui * When using simple heuristics to select encoding type, we don't want
197*01826a49SYabin Cui * to confuse these tables with dictionaries. When running more careful
198*01826a49SYabin Cui * analysis, we don't need to waste time checking both repeating tables
199*01826a49SYabin Cui * and default tables.
200*01826a49SYabin Cui */
201*01826a49SYabin Cui *repeatMode = FSE_repeat_none;
202*01826a49SYabin Cui return set_basic;
203*01826a49SYabin Cui }
204*01826a49SYabin Cui }
205*01826a49SYabin Cui } else {
206*01826a49SYabin Cui size_t const basicCost = isDefaultAllowed ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, count, max) : ERROR(GENERIC);
207*01826a49SYabin Cui size_t const repeatCost = *repeatMode != FSE_repeat_none ? ZSTD_fseBitCost(prevCTable, count, max) : ERROR(GENERIC);
208*01826a49SYabin Cui size_t const NCountCost = ZSTD_NCountCost(count, max, nbSeq, FSELog);
209*01826a49SYabin Cui size_t const compressedCost = (NCountCost << 3) + ZSTD_entropyCost(count, max, nbSeq);
210*01826a49SYabin Cui
211*01826a49SYabin Cui if (isDefaultAllowed) {
212*01826a49SYabin Cui assert(!ZSTD_isError(basicCost));
213*01826a49SYabin Cui assert(!(*repeatMode == FSE_repeat_valid && ZSTD_isError(repeatCost)));
214*01826a49SYabin Cui }
215*01826a49SYabin Cui assert(!ZSTD_isError(NCountCost));
216*01826a49SYabin Cui assert(compressedCost < ERROR(maxCode));
217*01826a49SYabin Cui DEBUGLOG(5, "Estimated bit costs: basic=%u\trepeat=%u\tcompressed=%u",
218*01826a49SYabin Cui (unsigned)basicCost, (unsigned)repeatCost, (unsigned)compressedCost);
219*01826a49SYabin Cui if (basicCost <= repeatCost && basicCost <= compressedCost) {
220*01826a49SYabin Cui DEBUGLOG(5, "Selected set_basic");
221*01826a49SYabin Cui assert(isDefaultAllowed);
222*01826a49SYabin Cui *repeatMode = FSE_repeat_none;
223*01826a49SYabin Cui return set_basic;
224*01826a49SYabin Cui }
225*01826a49SYabin Cui if (repeatCost <= compressedCost) {
226*01826a49SYabin Cui DEBUGLOG(5, "Selected set_repeat");
227*01826a49SYabin Cui assert(!ZSTD_isError(repeatCost));
228*01826a49SYabin Cui return set_repeat;
229*01826a49SYabin Cui }
230*01826a49SYabin Cui assert(compressedCost < basicCost && compressedCost < repeatCost);
231*01826a49SYabin Cui }
232*01826a49SYabin Cui DEBUGLOG(5, "Selected set_compressed");
233*01826a49SYabin Cui *repeatMode = FSE_repeat_check;
234*01826a49SYabin Cui return set_compressed;
235*01826a49SYabin Cui }
236*01826a49SYabin Cui
237*01826a49SYabin Cui typedef struct {
238*01826a49SYabin Cui S16 norm[MaxSeq + 1];
239*01826a49SYabin Cui U32 wksp[FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(MaxSeq, MaxFSELog)];
240*01826a49SYabin Cui } ZSTD_BuildCTableWksp;
241*01826a49SYabin Cui
242*01826a49SYabin Cui 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)243*01826a49SYabin Cui ZSTD_buildCTable(void* dst, size_t dstCapacity,
244*01826a49SYabin Cui FSE_CTable* nextCTable, U32 FSELog, symbolEncodingType_e type,
245*01826a49SYabin Cui unsigned* count, U32 max,
246*01826a49SYabin Cui const BYTE* codeTable, size_t nbSeq,
247*01826a49SYabin Cui const S16* defaultNorm, U32 defaultNormLog, U32 defaultMax,
248*01826a49SYabin Cui const FSE_CTable* prevCTable, size_t prevCTableSize,
249*01826a49SYabin Cui void* entropyWorkspace, size_t entropyWorkspaceSize)
250*01826a49SYabin Cui {
251*01826a49SYabin Cui BYTE* op = (BYTE*)dst;
252*01826a49SYabin Cui const BYTE* const oend = op + dstCapacity;
253*01826a49SYabin Cui DEBUGLOG(6, "ZSTD_buildCTable (dstCapacity=%u)", (unsigned)dstCapacity);
254*01826a49SYabin Cui
255*01826a49SYabin Cui switch (type) {
256*01826a49SYabin Cui case set_rle:
257*01826a49SYabin Cui FORWARD_IF_ERROR(FSE_buildCTable_rle(nextCTable, (BYTE)max), "");
258*01826a49SYabin Cui RETURN_ERROR_IF(dstCapacity==0, dstSize_tooSmall, "not enough space");
259*01826a49SYabin Cui *op = codeTable[0];
260*01826a49SYabin Cui return 1;
261*01826a49SYabin Cui case set_repeat:
262*01826a49SYabin Cui ZSTD_memcpy(nextCTable, prevCTable, prevCTableSize);
263*01826a49SYabin Cui return 0;
264*01826a49SYabin Cui case set_basic:
265*01826a49SYabin Cui FORWARD_IF_ERROR(FSE_buildCTable_wksp(nextCTable, defaultNorm, defaultMax, defaultNormLog, entropyWorkspace, entropyWorkspaceSize), ""); /* note : could be pre-calculated */
266*01826a49SYabin Cui return 0;
267*01826a49SYabin Cui case set_compressed: {
268*01826a49SYabin Cui ZSTD_BuildCTableWksp* wksp = (ZSTD_BuildCTableWksp*)entropyWorkspace;
269*01826a49SYabin Cui size_t nbSeq_1 = nbSeq;
270*01826a49SYabin Cui const U32 tableLog = FSE_optimalTableLog(FSELog, nbSeq, max);
271*01826a49SYabin Cui if (count[codeTable[nbSeq-1]] > 1) {
272*01826a49SYabin Cui count[codeTable[nbSeq-1]]--;
273*01826a49SYabin Cui nbSeq_1--;
274*01826a49SYabin Cui }
275*01826a49SYabin Cui assert(nbSeq_1 > 1);
276*01826a49SYabin Cui assert(entropyWorkspaceSize >= sizeof(ZSTD_BuildCTableWksp));
277*01826a49SYabin Cui (void)entropyWorkspaceSize;
278*01826a49SYabin Cui FORWARD_IF_ERROR(FSE_normalizeCount(wksp->norm, tableLog, count, nbSeq_1, max, ZSTD_useLowProbCount(nbSeq_1)), "FSE_normalizeCount failed");
279*01826a49SYabin Cui assert(oend >= op);
280*01826a49SYabin Cui { size_t const NCountSize = FSE_writeNCount(op, (size_t)(oend - op), wksp->norm, max, tableLog); /* overflow protected */
281*01826a49SYabin Cui FORWARD_IF_ERROR(NCountSize, "FSE_writeNCount failed");
282*01826a49SYabin Cui FORWARD_IF_ERROR(FSE_buildCTable_wksp(nextCTable, wksp->norm, max, tableLog, wksp->wksp, sizeof(wksp->wksp)), "FSE_buildCTable_wksp failed");
283*01826a49SYabin Cui return NCountSize;
284*01826a49SYabin Cui }
285*01826a49SYabin Cui }
286*01826a49SYabin Cui default: assert(0); RETURN_ERROR(GENERIC, "impossible to reach");
287*01826a49SYabin Cui }
288*01826a49SYabin Cui }
289*01826a49SYabin Cui
290*01826a49SYabin Cui 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)291*01826a49SYabin Cui ZSTD_encodeSequences_body(
292*01826a49SYabin Cui void* dst, size_t dstCapacity,
293*01826a49SYabin Cui FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable,
294*01826a49SYabin Cui FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable,
295*01826a49SYabin Cui FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable,
296*01826a49SYabin Cui seqDef const* sequences, size_t nbSeq, int longOffsets)
297*01826a49SYabin Cui {
298*01826a49SYabin Cui BIT_CStream_t blockStream;
299*01826a49SYabin Cui FSE_CState_t stateMatchLength;
300*01826a49SYabin Cui FSE_CState_t stateOffsetBits;
301*01826a49SYabin Cui FSE_CState_t stateLitLength;
302*01826a49SYabin Cui
303*01826a49SYabin Cui RETURN_ERROR_IF(
304*01826a49SYabin Cui ERR_isError(BIT_initCStream(&blockStream, dst, dstCapacity)),
305*01826a49SYabin Cui dstSize_tooSmall, "not enough space remaining");
306*01826a49SYabin Cui DEBUGLOG(6, "available space for bitstream : %i (dstCapacity=%u)",
307*01826a49SYabin Cui (int)(blockStream.endPtr - blockStream.startPtr),
308*01826a49SYabin Cui (unsigned)dstCapacity);
309*01826a49SYabin Cui
310*01826a49SYabin Cui /* first symbols */
311*01826a49SYabin Cui FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]);
312*01826a49SYabin Cui FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]);
313*01826a49SYabin Cui FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]);
314*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]);
315*01826a49SYabin Cui if (MEM_32bits()) BIT_flushBits(&blockStream);
316*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[nbSeq-1].mlBase, ML_bits[mlCodeTable[nbSeq-1]]);
317*01826a49SYabin Cui if (MEM_32bits()) BIT_flushBits(&blockStream);
318*01826a49SYabin Cui if (longOffsets) {
319*01826a49SYabin Cui U32 const ofBits = ofCodeTable[nbSeq-1];
320*01826a49SYabin Cui unsigned const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1);
321*01826a49SYabin Cui if (extraBits) {
322*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[nbSeq-1].offBase, extraBits);
323*01826a49SYabin Cui BIT_flushBits(&blockStream);
324*01826a49SYabin Cui }
325*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[nbSeq-1].offBase >> extraBits,
326*01826a49SYabin Cui ofBits - extraBits);
327*01826a49SYabin Cui } else {
328*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[nbSeq-1].offBase, ofCodeTable[nbSeq-1]);
329*01826a49SYabin Cui }
330*01826a49SYabin Cui BIT_flushBits(&blockStream);
331*01826a49SYabin Cui
332*01826a49SYabin Cui { size_t n;
333*01826a49SYabin Cui for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */
334*01826a49SYabin Cui BYTE const llCode = llCodeTable[n];
335*01826a49SYabin Cui BYTE const ofCode = ofCodeTable[n];
336*01826a49SYabin Cui BYTE const mlCode = mlCodeTable[n];
337*01826a49SYabin Cui U32 const llBits = LL_bits[llCode];
338*01826a49SYabin Cui U32 const ofBits = ofCode;
339*01826a49SYabin Cui U32 const mlBits = ML_bits[mlCode];
340*01826a49SYabin Cui DEBUGLOG(6, "encoding: litlen:%2u - matchlen:%2u - offCode:%7u",
341*01826a49SYabin Cui (unsigned)sequences[n].litLength,
342*01826a49SYabin Cui (unsigned)sequences[n].mlBase + MINMATCH,
343*01826a49SYabin Cui (unsigned)sequences[n].offBase);
344*01826a49SYabin Cui /* 32b*/ /* 64b*/
345*01826a49SYabin Cui /* (7)*/ /* (7)*/
346*01826a49SYabin Cui FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */
347*01826a49SYabin Cui FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */
348*01826a49SYabin Cui if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/
349*01826a49SYabin Cui FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */
350*01826a49SYabin Cui if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog)))
351*01826a49SYabin Cui BIT_flushBits(&blockStream); /* (7)*/
352*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[n].litLength, llBits);
353*01826a49SYabin Cui if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream);
354*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[n].mlBase, mlBits);
355*01826a49SYabin Cui if (MEM_32bits() || (ofBits+mlBits+llBits > 56)) BIT_flushBits(&blockStream);
356*01826a49SYabin Cui if (longOffsets) {
357*01826a49SYabin Cui unsigned const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1);
358*01826a49SYabin Cui if (extraBits) {
359*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[n].offBase, extraBits);
360*01826a49SYabin Cui BIT_flushBits(&blockStream); /* (7)*/
361*01826a49SYabin Cui }
362*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[n].offBase >> extraBits,
363*01826a49SYabin Cui ofBits - extraBits); /* 31 */
364*01826a49SYabin Cui } else {
365*01826a49SYabin Cui BIT_addBits(&blockStream, sequences[n].offBase, ofBits); /* 31 */
366*01826a49SYabin Cui }
367*01826a49SYabin Cui BIT_flushBits(&blockStream); /* (7)*/
368*01826a49SYabin Cui DEBUGLOG(7, "remaining space : %i", (int)(blockStream.endPtr - blockStream.ptr));
369*01826a49SYabin Cui } }
370*01826a49SYabin Cui
371*01826a49SYabin Cui DEBUGLOG(6, "ZSTD_encodeSequences: flushing ML state with %u bits", stateMatchLength.stateLog);
372*01826a49SYabin Cui FSE_flushCState(&blockStream, &stateMatchLength);
373*01826a49SYabin Cui DEBUGLOG(6, "ZSTD_encodeSequences: flushing Off state with %u bits", stateOffsetBits.stateLog);
374*01826a49SYabin Cui FSE_flushCState(&blockStream, &stateOffsetBits);
375*01826a49SYabin Cui DEBUGLOG(6, "ZSTD_encodeSequences: flushing LL state with %u bits", stateLitLength.stateLog);
376*01826a49SYabin Cui FSE_flushCState(&blockStream, &stateLitLength);
377*01826a49SYabin Cui
378*01826a49SYabin Cui { size_t const streamSize = BIT_closeCStream(&blockStream);
379*01826a49SYabin Cui RETURN_ERROR_IF(streamSize==0, dstSize_tooSmall, "not enough space");
380*01826a49SYabin Cui return streamSize;
381*01826a49SYabin Cui }
382*01826a49SYabin Cui }
383*01826a49SYabin Cui
384*01826a49SYabin Cui static size_t
ZSTD_encodeSequences_default(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)385*01826a49SYabin Cui ZSTD_encodeSequences_default(
386*01826a49SYabin Cui void* dst, size_t dstCapacity,
387*01826a49SYabin Cui FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable,
388*01826a49SYabin Cui FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable,
389*01826a49SYabin Cui FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable,
390*01826a49SYabin Cui seqDef const* sequences, size_t nbSeq, int longOffsets)
391*01826a49SYabin Cui {
392*01826a49SYabin Cui return ZSTD_encodeSequences_body(dst, dstCapacity,
393*01826a49SYabin Cui CTable_MatchLength, mlCodeTable,
394*01826a49SYabin Cui CTable_OffsetBits, ofCodeTable,
395*01826a49SYabin Cui CTable_LitLength, llCodeTable,
396*01826a49SYabin Cui sequences, nbSeq, longOffsets);
397*01826a49SYabin Cui }
398*01826a49SYabin Cui
399*01826a49SYabin Cui
400*01826a49SYabin Cui #if DYNAMIC_BMI2
401*01826a49SYabin Cui
402*01826a49SYabin Cui static BMI2_TARGET_ATTRIBUTE size_t
ZSTD_encodeSequences_bmi2(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)403*01826a49SYabin Cui ZSTD_encodeSequences_bmi2(
404*01826a49SYabin Cui void* dst, size_t dstCapacity,
405*01826a49SYabin Cui FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable,
406*01826a49SYabin Cui FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable,
407*01826a49SYabin Cui FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable,
408*01826a49SYabin Cui seqDef const* sequences, size_t nbSeq, int longOffsets)
409*01826a49SYabin Cui {
410*01826a49SYabin Cui return ZSTD_encodeSequences_body(dst, dstCapacity,
411*01826a49SYabin Cui CTable_MatchLength, mlCodeTable,
412*01826a49SYabin Cui CTable_OffsetBits, ofCodeTable,
413*01826a49SYabin Cui CTable_LitLength, llCodeTable,
414*01826a49SYabin Cui sequences, nbSeq, longOffsets);
415*01826a49SYabin Cui }
416*01826a49SYabin Cui
417*01826a49SYabin Cui #endif
418*01826a49SYabin Cui
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)419*01826a49SYabin Cui size_t ZSTD_encodeSequences(
420*01826a49SYabin Cui void* dst, size_t dstCapacity,
421*01826a49SYabin Cui FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable,
422*01826a49SYabin Cui FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable,
423*01826a49SYabin Cui FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable,
424*01826a49SYabin Cui seqDef const* sequences, size_t nbSeq, int longOffsets, int bmi2)
425*01826a49SYabin Cui {
426*01826a49SYabin Cui DEBUGLOG(5, "ZSTD_encodeSequences: dstCapacity = %u", (unsigned)dstCapacity);
427*01826a49SYabin Cui #if DYNAMIC_BMI2
428*01826a49SYabin Cui if (bmi2) {
429*01826a49SYabin Cui return ZSTD_encodeSequences_bmi2(dst, dstCapacity,
430*01826a49SYabin Cui CTable_MatchLength, mlCodeTable,
431*01826a49SYabin Cui CTable_OffsetBits, ofCodeTable,
432*01826a49SYabin Cui CTable_LitLength, llCodeTable,
433*01826a49SYabin Cui sequences, nbSeq, longOffsets);
434*01826a49SYabin Cui }
435*01826a49SYabin Cui #endif
436*01826a49SYabin Cui (void)bmi2;
437*01826a49SYabin Cui return ZSTD_encodeSequences_default(dst, dstCapacity,
438*01826a49SYabin Cui CTable_MatchLength, mlCodeTable,
439*01826a49SYabin Cui CTable_OffsetBits, ofCodeTable,
440*01826a49SYabin Cui CTable_LitLength, llCodeTable,
441*01826a49SYabin Cui sequences, nbSeq, longOffsets);
442*01826a49SYabin Cui }
443