Coverage Report

Created: 2026-09-14 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zstd/lib/compress/fse_compress.c
Line
Count
Source
1
/* ******************************************************************
2
 * FSE : Finite State Entropy encoder
3
 * Copyright (c) Meta Platforms, Inc. and affiliates.
4
 *
5
 *  You can contact the author at :
6
 *  - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
7
 *  - Public forum : https://groups.google.com/forum/#!forum/lz4c
8
 *
9
 * This source code is licensed under both the BSD-style license (found in the
10
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
 * in the COPYING file in the root directory of this source tree).
12
 * You may select, at your option, one of the above-listed licenses.
13
****************************************************************** */
14
15
/* **************************************************************
16
*  Includes
17
****************************************************************/
18
#include "../common/compiler.h"
19
#include "../common/mem.h"        /* U32, U16, etc. */
20
#include "../common/debug.h"      /* assert, DEBUGLOG */
21
#include "hist.h"       /* HIST_count_wksp */
22
#include "../common/bitstream.h"
23
#define FSE_STATIC_LINKING_ONLY
24
#include "../common/fse.h"
25
#include "../common/error_private.h"
26
#define ZSTD_DEPS_NEED_MALLOC
27
#define ZSTD_DEPS_NEED_MATH64
28
#include "../common/zstd_deps.h"  /* ZSTD_memset */
29
#include "../common/bits.h" /* ZSTD_highbit32 */
30
31
32
/* **************************************************************
33
*  Error Management
34
****************************************************************/
35
4.14k
#define FSE_isError ERR_isError
36
37
38
/* **************************************************************
39
*  Templates
40
****************************************************************/
41
/*
42
  designed to be included
43
  for type-specific functions (template emulation in C)
44
  Objective is to write these functions only once, for improved maintenance
45
*/
46
47
/* safety checks */
48
#ifndef FSE_FUNCTION_EXTENSION
49
#  error "FSE_FUNCTION_EXTENSION must be defined"
50
#endif
51
#ifndef FSE_FUNCTION_TYPE
52
#  error "FSE_FUNCTION_TYPE must be defined"
53
#endif
54
55
/* Function names */
56
#define FSE_CAT(X,Y) X##Y
57
#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
58
#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
59
60
61
/* Function templates */
62
63
/* FSE_buildCTable_wksp() :
64
 * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
65
 * wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)`
66
 * workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements
67
 */
68
size_t FSE_buildCTable_wksp(FSE_CTable* ct,
69
                      const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
70
                            void* workSpace, size_t wkspSize)
71
45.4k
{
72
45.4k
    U32 const tableSize = 1 << tableLog;
73
45.4k
    U32 const tableMask = tableSize - 1;
74
45.4k
    void* const ptr = ct;
75
45.4k
    U16* const tableU16 = ( (U16*) ptr) + 2;
76
45.4k
    void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ;
77
45.4k
    FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
78
45.4k
    U32 const step = FSE_TABLESTEP(tableSize);
79
45.4k
    U32 const maxSV1 = maxSymbolValue+1;
80
81
45.4k
    U16* cumul = (U16*)workSpace;   /* size = maxSV1 */
82
45.4k
    FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)(cumul + (maxSV1+1));  /* size = tableSize */
83
84
45.4k
    U32 highThreshold = tableSize-1;
85
86
45.4k
    assert(((size_t)workSpace & 1) == 0);  /* Must be 2 bytes-aligned */
87
45.4k
    if (FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) > wkspSize) return ERROR(tableLog_tooLarge);
88
    /* CTable header */
89
45.4k
    tableU16[-2] = (U16) tableLog;
90
45.4k
    tableU16[-1] = (U16) maxSymbolValue;
91
45.4k
    assert(tableLog < 16);   /* required for threshold strategy to work */
92
93
    /* For explanations on how to distribute symbol values over the table :
94
     * https://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
95
96
     #ifdef __clang_analyzer__
97
     ZSTD_memset(tableSymbol, 0, sizeof(*tableSymbol) * tableSize);   /* useless initialization, just to keep scan-build happy */
98
     #endif
99
100
    /* symbol start positions */
101
45.4k
    {   U32 u;
102
45.4k
        cumul[0] = 0;
103
1.48M
        for (u=1; u <= maxSV1; u++) {
104
1.44M
            if (normalizedCounter[u-1]==-1) {  /* Low proba symbol */
105
161k
                cumul[u] = cumul[u-1] + 1;
106
161k
                tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);
107
1.27M
            } else {
108
1.27M
                assert(normalizedCounter[u-1] >= 0);
109
1.27M
                cumul[u] = cumul[u-1] + (U16)normalizedCounter[u-1];
110
1.27M
                assert(cumul[u] >= cumul[u-1]);  /* no overflow */
111
1.27M
        }   }
112
45.4k
        cumul[maxSV1] = (U16)(tableSize+1);
113
45.4k
    }
114
115
    /* Spread symbols */
116
45.4k
    if (highThreshold == tableSize - 1) {
117
        /* Case for no low prob count symbols. Lay down 8 bytes at a time
118
         * to reduce branch misses since we are operating on a small block
119
         */
120
16.1k
        BYTE* const spread = tableSymbol + tableSize; /* size = tableSize + 8 (may write beyond tableSize) */
121
16.1k
        {   U64 const add = 0x0101010101010101ull;
122
16.1k
            size_t pos = 0;
123
16.1k
            U64 sv = 0;
124
16.1k
            U32 s;
125
299k
            for (s=0; s<maxSV1; ++s, sv += add) {
126
283k
                int i;
127
283k
                int const n = normalizedCounter[s];
128
283k
                MEM_write64(spread + pos, sv);
129
387k
                for (i = 8; i < n; i += 8) {
130
103k
                    MEM_write64(spread + pos + i, sv);
131
103k
                }
132
283k
                assert(n>=0);
133
283k
                pos += (size_t)n;
134
283k
            }
135
16.1k
        }
136
        /* Spread symbols across the table. Lack of lowprob symbols means that
137
         * we don't need variable sized inner loop, so we can unroll the loop and
138
         * reduce branch misses.
139
         */
140
16.1k
        {   size_t position = 0;
141
16.1k
            size_t s;
142
16.1k
            size_t const unroll = 2; /* Experimentally determined optimal unroll */
143
16.1k
            assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */
144
604k
            for (s = 0; s < (size_t)tableSize; s += unroll) {
145
588k
                size_t u;
146
1.76M
                for (u = 0; u < unroll; ++u) {
147
1.17M
                    size_t const uPosition = (position + (u * step)) & tableMask;
148
1.17M
                    tableSymbol[uPosition] = spread[s + u];
149
1.17M
                }
150
588k
                position = (position + (unroll * step)) & tableMask;
151
588k
            }
152
16.1k
            assert(position == 0);   /* Must have initialized all positions */
153
16.1k
        }
154
29.3k
    } else {
155
29.3k
        U32 position = 0;
156
29.3k
        U32 symbol;
157
1.18M
        for (symbol=0; symbol<maxSV1; symbol++) {
158
1.15M
            int nbOccurrences;
159
1.15M
            int const freq = normalizedCounter[symbol];
160
3.15M
            for (nbOccurrences=0; nbOccurrences<freq; nbOccurrences++) {
161
1.99M
                tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
162
1.99M
                position = (position + step) & tableMask;
163
2.15M
                while (position > highThreshold)
164
161k
                    position = (position + step) & tableMask;   /* Low proba area */
165
1.99M
        }   }
166
29.3k
        assert(position==0);  /* Must have initialized all positions */
167
29.3k
    }
168
169
    /* Build table */
170
3.37M
    {   U32 u; for (u=0; u<tableSize; u++) {
171
3.33M
        FSE_FUNCTION_TYPE s = tableSymbol[u];   /* note : static analyzer may not understand tableSymbol is properly initialized */
172
3.33M
        tableU16[cumul[s]++] = (U16) (tableSize+u);   /* TableU16 : sorted by symbol order; gives next state value */
173
3.33M
    }   }
174
175
    /* Build Symbol Transformation Table */
176
45.4k
    {   unsigned total = 0;
177
45.4k
        unsigned s;
178
1.48M
        for (s=0; s<=maxSymbolValue; s++) {
179
1.44M
            switch (normalizedCounter[s])
180
1.44M
            {
181
160k
            case  0:
182
                /* filling nonetheless, for compatibility with FSE_getMaxNbBits() */
183
160k
                symbolTT[s].deltaNbBits = ((tableLog+1) << 16) - (1<<tableLog);
184
160k
                break;
185
186
161k
            case -1:
187
875k
            case  1:
188
875k
                symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);
189
875k
                assert(total <= INT_MAX);
190
875k
                symbolTT[s].deltaFindState = (int)(total - 1);
191
875k
                total ++;
192
875k
                break;
193
403k
            default :
194
403k
                assert(normalizedCounter[s] > 1);
195
403k
                {   U32 const maxBitsOut = tableLog - ZSTD_highbit32 ((U32)normalizedCounter[s]-1);
196
403k
                    U32 const minStatePlus = (U32)normalizedCounter[s] << maxBitsOut;
197
403k
                    symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
198
403k
                    symbolTT[s].deltaFindState = (int)(total - (unsigned)normalizedCounter[s]);
199
403k
                    total +=  (unsigned)normalizedCounter[s];
200
403k
    }   }   }   }
201
202
#if 0  /* debug : symbol costs */
203
    DEBUGLOG(5, "\n --- table statistics : ");
204
    {   U32 symbol;
205
        for (symbol=0; symbol<=maxSymbolValue; symbol++) {
206
            DEBUGLOG(5, "%3u: w=%3i,   maxBits=%u, fracBits=%.2f",
207
                symbol, normalizedCounter[symbol],
208
                FSE_getMaxNbBits(symbolTT, symbol),
209
                (double)FSE_bitCost(symbolTT, tableLog, symbol, 8) / 256);
210
    }   }
211
#endif
212
213
45.4k
    return 0;
214
45.4k
}
215
216
217
218
#ifndef FSE_COMMONDEFS_ONLY
219
220
/*-**************************************************************
221
*  FSE NCount encoding
222
****************************************************************/
223
size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
224
45.3k
{
225
45.3k
    size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog
226
45.3k
                                   + 4 /* bitCount initialized at 4 */
227
45.3k
                                   + 2 /* first two symbols may use one additional bit each */) / 8)
228
45.3k
                                   + 1 /* round up to whole nb bytes */
229
45.3k
                                   + 2 /* additional two bytes for bitstream flush */;
230
45.3k
    return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND;  /* maxSymbolValue==0 ? use default */
231
45.3k
}
232
233
static size_t
234
FSE_writeNCount_generic (void* header, size_t headerBufferSize,
235
                   const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
236
                         unsigned writeIsSafe)
237
45.3k
{
238
45.3k
    BYTE* const ostart = (BYTE*) header;
239
45.3k
    BYTE* out = ostart;
240
45.3k
    BYTE* const oend = ostart + headerBufferSize;
241
45.3k
    int nbBits;
242
45.3k
    const int tableSize = 1 << tableLog;
243
45.3k
    int remaining;
244
45.3k
    int threshold;
245
45.3k
    U32 bitStream = 0;
246
45.3k
    int bitCount = 0;
247
45.3k
    unsigned symbol = 0;
248
45.3k
    unsigned const alphabetSize = maxSymbolValue + 1;
249
45.3k
    int previousIs0 = 0;
250
251
    /* Table Size */
252
45.3k
    bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
253
45.3k
    bitCount  += 4;
254
255
    /* Init */
256
45.3k
    remaining = tableSize+1;   /* +1 for extra accuracy */
257
45.3k
    threshold = tableSize;
258
45.3k
    nbBits = (int)tableLog+1;
259
260
548k
    while ((symbol < alphabetSize) && (remaining>1)) {  /* stops at 1 */
261
503k
        if (previousIs0) {
262
107k
            unsigned start = symbol;
263
576k
            while ((symbol < alphabetSize) && !normalizedCounter[symbol]) symbol++;
264
107k
            if (symbol == alphabetSize) break;   /* incorrect distribution */
265
115k
            while (symbol >= start+24) {
266
7.78k
                start+=24;
267
7.78k
                bitStream += 0xFFFFU << bitCount;
268
7.78k
                if ((!writeIsSafe) && (out > oend-2))
269
0
                    return ERROR(dstSize_tooSmall);   /* Buffer overflow */
270
7.78k
                out[0] = (BYTE) bitStream;
271
7.78k
                out[1] = (BYTE)(bitStream>>8);
272
7.78k
                out+=2;
273
7.78k
                bitStream>>=16;
274
7.78k
            }
275
178k
            while (symbol >= start+3) {
276
71.0k
                start+=3;
277
71.0k
                bitStream += 3U << bitCount;
278
71.0k
                bitCount += 2;
279
71.0k
            }
280
107k
            bitStream += (symbol-start) << bitCount;
281
107k
            bitCount += 2;
282
107k
            if (bitCount>16) {
283
19.9k
                if ((!writeIsSafe) && (out > oend - 2))
284
0
                    return ERROR(dstSize_tooSmall);   /* Buffer overflow */
285
19.9k
                out[0] = (BYTE)bitStream;
286
19.9k
                out[1] = (BYTE)(bitStream>>8);
287
19.9k
                out += 2;
288
19.9k
                bitStream >>= 16;
289
19.9k
                bitCount -= 16;
290
19.9k
        }   }
291
503k
        {   int count = normalizedCounter[symbol++];
292
503k
            int const max = (2*threshold-1) - remaining;
293
503k
            remaining -= count < 0 ? -count : count;
294
503k
            count++;   /* +1 for extra accuracy */
295
503k
            if (count>=threshold)
296
70.5k
                count += max;   /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
297
503k
            bitStream += (U32)count << bitCount;
298
503k
            bitCount  += nbBits;
299
503k
            bitCount  -= (count<max);
300
503k
            previousIs0  = (count==1);
301
503k
            if (remaining<1) return ERROR(GENERIC);
302
789k
            while (remaining<threshold) { nbBits--; threshold>>=1; }
303
503k
        }
304
503k
        if (bitCount>16) {
305
137k
            if ((!writeIsSafe) && (out > oend - 2))
306
0
                return ERROR(dstSize_tooSmall);   /* Buffer overflow */
307
137k
            out[0] = (BYTE)bitStream;
308
137k
            out[1] = (BYTE)(bitStream>>8);
309
137k
            out += 2;
310
137k
            bitStream >>= 16;
311
137k
            bitCount -= 16;
312
137k
    }   }
313
314
45.3k
    if (remaining != 1)
315
0
        return ERROR(GENERIC);  /* incorrect normalized distribution */
316
45.3k
    assert(symbol <= alphabetSize);
317
318
    /* flush remaining bitStream */
319
45.3k
    if ((!writeIsSafe) && (out > oend - 2))
320
0
        return ERROR(dstSize_tooSmall);   /* Buffer overflow */
321
45.3k
    out[0] = (BYTE)bitStream;
322
45.3k
    out[1] = (BYTE)(bitStream>>8);
323
45.3k
    out+= (bitCount+7) /8;
324
325
45.3k
    assert(out >= ostart);
326
45.3k
    return (size_t)(out-ostart);
327
45.3k
}
328
329
330
size_t FSE_writeNCount (void* buffer, size_t bufferSize,
331
                  const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
332
45.3k
{
333
45.3k
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported */
334
45.3k
    if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported */
335
336
45.3k
    if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))
337
0
        return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
338
339
45.3k
    return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1 /* write in buffer is safe */);
340
45.3k
}
341
342
343
/*-**************************************************************
344
*  FSE Compression Code
345
****************************************************************/
346
347
/* provides the minimum logSize to safely represent a distribution */
348
static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
349
94.2k
{
350
94.2k
    U32 minBitsSrc = ZSTD_highbit32((U32)(srcSize)) + 1;
351
94.2k
    U32 minBitsSymbols = ZSTD_highbit32(maxSymbolValue) + 2;
352
94.2k
    U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;
353
94.2k
    assert(srcSize > 1); /* Not supported, RLE should be used instead */
354
94.2k
    return minBits;
355
94.2k
}
356
357
unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
358
48.9k
{
359
48.9k
    U32 maxBitsSrc = ZSTD_highbit32((U32)(srcSize - 1)) - minus;
360
48.9k
    U32 tableLog = maxTableLog;
361
48.9k
    U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);
362
48.9k
    assert(srcSize > 1); /* Not supported, RLE should be used instead */
363
48.9k
    if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
364
48.9k
    if (maxBitsSrc < tableLog) tableLog = maxBitsSrc;   /* Accuracy can be reduced */
365
48.9k
    if (minBits > tableLog) tableLog = minBits;   /* Need a minimum to safely represent all symbol values */
366
48.9k
    if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
367
48.9k
    if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
368
48.9k
    return tableLog;
369
48.9k
}
370
371
unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
372
45.3k
{
373
45.3k
    return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);
374
45.3k
}
375
376
/* Secondary normalization method.
377
   To be used when primary method fails. */
378
379
static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue, short lowProbCount)
380
607
{
381
607
    short const NOT_YET_ASSIGNED = -2;
382
607
    U32 s;
383
607
    U32 distributed = 0;
384
607
    U32 ToDistribute;
385
386
    /* Init */
387
607
    U32 const lowThreshold = (U32)(total >> tableLog);
388
607
    U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
389
390
21.9k
    for (s=0; s<=maxSymbolValue; s++) {
391
21.3k
        if (count[s] == 0) {
392
9.24k
            norm[s]=0;
393
9.24k
            continue;
394
9.24k
        }
395
12.0k
        if (count[s] <= lowThreshold) {
396
3.08k
            norm[s] = lowProbCount;
397
3.08k
            distributed++;
398
3.08k
            total -= count[s];
399
3.08k
            continue;
400
3.08k
        }
401
8.97k
        if (count[s] <= lowOne) {
402
1.45k
            norm[s] = 1;
403
1.45k
            distributed++;
404
1.45k
            total -= count[s];
405
1.45k
            continue;
406
1.45k
        }
407
408
7.52k
        norm[s]=NOT_YET_ASSIGNED;
409
7.52k
    }
410
607
    ToDistribute = (1 << tableLog) - distributed;
411
412
607
    if (ToDistribute == 0)
413
0
        return 0;
414
415
607
    if ((total / ToDistribute) > lowOne) {
416
        /* risk of rounding to zero */
417
0
        lowOne = (U32)((total * 3) / (ToDistribute * 2));
418
0
        for (s=0; s<=maxSymbolValue; s++) {
419
0
            if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {
420
0
                norm[s] = 1;
421
0
                distributed++;
422
0
                total -= count[s];
423
0
                continue;
424
0
        }   }
425
0
        ToDistribute = (1 << tableLog) - distributed;
426
0
    }
427
428
607
    if (distributed == maxSymbolValue+1) {
429
        /* all values are pretty poor;
430
           probably incompressible data (should have already been detected);
431
           find max, then give all remaining points to max */
432
0
        U32 maxV = 0, maxC = 0;
433
0
        for (s=0; s<=maxSymbolValue; s++)
434
0
            if (count[s] > maxC) { maxV=s; maxC=count[s]; }
435
0
        norm[maxV] += (short)ToDistribute;
436
0
        return 0;
437
0
    }
438
439
607
    if (total == 0) {
440
        /* all of the symbols were low enough for the lowOne or lowThreshold */
441
42
        for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1))
442
39
            if (norm[s] > 0) { ToDistribute--; norm[s]++; }
443
3
        return 0;
444
3
    }
445
446
604
    {   U64 const vStepLog = 62 - tableLog;
447
604
        U64 const mid = (1ULL << (vStepLog-1)) - 1;
448
604
        U64 const rStep = ZSTD_div64((((U64)1<<vStepLog) * ToDistribute) + mid, (U32)total);   /* scale on remaining */
449
604
        U64 tmpTotal = mid;
450
21.7k
        for (s=0; s<=maxSymbolValue; s++) {
451
21.1k
            if (norm[s]==NOT_YET_ASSIGNED) {
452
7.52k
                U64 const end = tmpTotal + (count[s] * rStep);
453
7.52k
                U32 const sStart = (U32)(tmpTotal >> vStepLog);
454
7.52k
                U32 const sEnd = (U32)(end >> vStepLog);
455
7.52k
                U32 const weight = sEnd - sStart;
456
7.52k
                if (weight < 1)
457
0
                    return ERROR(GENERIC);
458
7.52k
                norm[s] = (short)weight;
459
7.52k
                tmpTotal = end;
460
7.52k
    }   }   }
461
462
604
    return 0;
463
604
}
464
465
size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
466
                           const unsigned* count, size_t total,
467
                           unsigned maxSymbolValue, unsigned useLowProbCount)
468
45.3k
{
469
    /* Sanity checks */
470
45.3k
    if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
471
45.3k
    if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported size */
472
45.3k
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported size */
473
45.3k
    if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC);   /* Too small tableLog, compression potentially impossible */
474
475
45.3k
    {   static U32 const rtbTable[] = {     0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
476
45.3k
        short const lowProbCount = useLowProbCount ? -1 : 1;
477
45.3k
        U64 const scale = 62 - tableLog;
478
45.3k
        U64 const step = ZSTD_div64((U64)1<<62, (U32)total);   /* <== here, one division ! */
479
45.3k
        U64 const vStep = 1ULL<<(scale-20);
480
45.3k
        int stillToDistribute = 1<<tableLog;
481
45.3k
        unsigned s;
482
45.3k
        unsigned largest=0;
483
45.3k
        short largestP=0;
484
45.3k
        U32 lowThreshold = (U32)(total >> tableLog);
485
486
1.01M
        for (s=0; s<=maxSymbolValue; s++) {
487
972k
            if (count[s] == total) return 0;   /* rle special case */
488
972k
            if (count[s] == 0) { normalizedCounter[s]=0; continue; }
489
395k
            if (count[s] <= lowThreshold) {
490
110k
                normalizedCounter[s] = lowProbCount;
491
110k
                stillToDistribute--;
492
285k
            } else {
493
285k
                short proba = (short)((count[s]*step) >> scale);
494
285k
                if (proba<8) {
495
175k
                    U64 restToBeat = vStep * rtbTable[proba];
496
175k
                    proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
497
175k
                }
498
285k
                if (proba > largestP) { largestP=proba; largest=s; }
499
285k
                normalizedCounter[s] = proba;
500
285k
                stillToDistribute -= proba;
501
285k
        }   }
502
45.3k
        if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
503
            /* corner case, need another normalization method */
504
607
            size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue, lowProbCount);
505
607
            if (FSE_isError(errorCode)) return errorCode;
506
607
        }
507
44.7k
        else normalizedCounter[largest] += (short)stillToDistribute;
508
45.3k
    }
509
510
#if 0
511
    {   /* Print Table (debug) */
512
        U32 s;
513
        U32 nTotal = 0;
514
        for (s=0; s<=maxSymbolValue; s++)
515
            RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]);
516
        for (s=0; s<=maxSymbolValue; s++)
517
            nTotal += abs(normalizedCounter[s]);
518
        if (nTotal != (1U<<tableLog))
519
            RAWLOG(2, "Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
520
        getchar();
521
    }
522
#endif
523
524
45.3k
    return tableLog;
525
45.3k
}
526
527
/* fake FSE_CTable, for rle input (always same symbol) */
528
size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue)
529
2.01k
{
530
2.01k
    void* ptr = ct;
531
2.01k
    U16* tableU16 = ( (U16*) ptr) + 2;
532
2.01k
    void* FSCTptr = (U32*)ptr + 2;
533
2.01k
    FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr;
534
535
    /* header */
536
2.01k
    tableU16[-2] = (U16) 0;
537
2.01k
    tableU16[-1] = (U16) symbolValue;
538
539
    /* Build table */
540
2.01k
    tableU16[0] = 0;
541
2.01k
    tableU16[1] = 0;   /* just in case */
542
543
    /* Build Symbol Transformation Table */
544
2.01k
    symbolTT[symbolValue].deltaNbBits = 0;
545
2.01k
    symbolTT[symbolValue].deltaFindState = 0;
546
547
2.01k
    return 0;
548
2.01k
}
549
550
551
static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
552
                           const void* src, size_t srcSize,
553
                           const FSE_CTable* ct, const unsigned fast)
554
3.53k
{
555
3.53k
    const BYTE* const istart = (const BYTE*) src;
556
3.53k
    const BYTE* const iend = istart + srcSize;
557
3.53k
    const BYTE* ip=iend;
558
559
3.53k
    BIT_CStream_t bitC;
560
3.53k
    FSE_CState_t CState1, CState2;
561
562
    /* init */
563
3.53k
    if (srcSize <= 2) return 0;
564
3.53k
    { size_t const initError = BIT_initCStream(&bitC, dst, dstSize);
565
3.53k
      if (FSE_isError(initError)) return 0; /* not enough space available to write a bitstream */ }
566
567
225k
#define FSE_FLUSHBITS(s)  (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
568
569
3.53k
    if (srcSize & 1) {
570
3.03k
        FSE_initCState2(&CState1, ct, *--ip);
571
3.03k
        FSE_initCState2(&CState2, ct, *--ip);
572
3.03k
        FSE_encodeSymbol(&bitC, &CState1, *--ip);
573
3.03k
        FSE_FLUSHBITS(&bitC);
574
3.03k
    } else {
575
506
        FSE_initCState2(&CState2, ct, *--ip);
576
506
        FSE_initCState2(&CState1, ct, *--ip);
577
506
    }
578
579
    /* join to mod 4 */
580
3.53k
    srcSize -= 2;
581
3.53k
    if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) {  /* test bit 2 */
582
31
        FSE_encodeSymbol(&bitC, &CState2, *--ip);
583
31
        FSE_encodeSymbol(&bitC, &CState1, *--ip);
584
31
        FSE_FLUSHBITS(&bitC);
585
31
    }
586
587
    /* 2 or 4 encoding per loop */
588
225k
    while ( ip>istart ) {
589
590
221k
        FSE_encodeSymbol(&bitC, &CState2, *--ip);
591
592
221k
        if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 )   /* this test must be static */
593
0
            FSE_FLUSHBITS(&bitC);
594
595
221k
        FSE_encodeSymbol(&bitC, &CState1, *--ip);
596
597
221k
        if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) {  /* this test must be static */
598
221k
            FSE_encodeSymbol(&bitC, &CState2, *--ip);
599
221k
            FSE_encodeSymbol(&bitC, &CState1, *--ip);
600
221k
        }
601
602
221k
        FSE_FLUSHBITS(&bitC);
603
221k
    }
604
605
3.53k
    FSE_flushCState(&bitC, &CState2);
606
3.53k
    FSE_flushCState(&bitC, &CState1);
607
3.53k
    return BIT_closeCStream(&bitC);
608
3.53k
}
609
610
size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
611
                           const void* src, size_t srcSize,
612
                           const FSE_CTable* ct)
613
3.53k
{
614
3.53k
    unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));
615
616
3.53k
    if (fast)
617
3.53k
        return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);
618
0
    else
619
0
        return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);
620
3.53k
}
621
622
623
0
size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
624
625
#endif   /* FSE_COMMONDEFS_ONLY */