Path: blob/main/sys/contrib/zstd/lib/compress/fse_compress.c
48378 views
/* ******************************************************************1* FSE : Finite State Entropy encoder2* Copyright (c) Yann Collet, Facebook, Inc.3*4* You can contact the author at :5* - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy6* - Public forum : https://groups.google.com/forum/#!forum/lz4c7*8* This source code is licensed under both the BSD-style license (found in the9* LICENSE file in the root directory of this source tree) and the GPLv2 (found10* in the COPYING file in the root directory of this source tree).11* You may select, at your option, one of the above-listed licenses.12****************************************************************** */1314/* **************************************************************15* Includes16****************************************************************/17#include "../common/compiler.h"18#include "../common/mem.h" /* U32, U16, etc. */19#include "../common/debug.h" /* assert, DEBUGLOG */20#include "hist.h" /* HIST_count_wksp */21#include "../common/bitstream.h"22#define FSE_STATIC_LINKING_ONLY23#include "../common/fse.h"24#include "../common/error_private.h"25#define ZSTD_DEPS_NEED_MALLOC26#define ZSTD_DEPS_NEED_MATH6427#include "../common/zstd_deps.h" /* ZSTD_malloc, ZSTD_free, ZSTD_memcpy, ZSTD_memset */282930/* **************************************************************31* Error Management32****************************************************************/33#define FSE_isError ERR_isError343536/* **************************************************************37* Templates38****************************************************************/39/*40designed to be included41for type-specific functions (template emulation in C)42Objective is to write these functions only once, for improved maintenance43*/4445/* safety checks */46#ifndef FSE_FUNCTION_EXTENSION47# error "FSE_FUNCTION_EXTENSION must be defined"48#endif49#ifndef FSE_FUNCTION_TYPE50# error "FSE_FUNCTION_TYPE must be defined"51#endif5253/* Function names */54#define FSE_CAT(X,Y) X##Y55#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)56#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)575859/* Function templates */6061/* FSE_buildCTable_wksp() :62* Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).63* wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)`64* workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements65*/66size_t FSE_buildCTable_wksp(FSE_CTable* ct,67const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,68void* workSpace, size_t wkspSize)69{70U32 const tableSize = 1 << tableLog;71U32 const tableMask = tableSize - 1;72void* const ptr = ct;73U16* const tableU16 = ( (U16*) ptr) + 2;74void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ;75FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);76U32 const step = FSE_TABLESTEP(tableSize);77U32 const maxSV1 = maxSymbolValue+1;7879U16* cumul = (U16*)workSpace; /* size = maxSV1 */80FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)(cumul + (maxSV1+1)); /* size = tableSize */8182U32 highThreshold = tableSize-1;8384assert(((size_t)workSpace & 1) == 0); /* Must be 2 bytes-aligned */85if (FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) > wkspSize) return ERROR(tableLog_tooLarge);86/* CTable header */87tableU16[-2] = (U16) tableLog;88tableU16[-1] = (U16) maxSymbolValue;89assert(tableLog < 16); /* required for threshold strategy to work */9091/* For explanations on how to distribute symbol values over the table :92* http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */9394#ifdef __clang_analyzer__95ZSTD_memset(tableSymbol, 0, sizeof(*tableSymbol) * tableSize); /* useless initialization, just to keep scan-build happy */96#endif9798/* symbol start positions */99{ U32 u;100cumul[0] = 0;101for (u=1; u <= maxSV1; u++) {102if (normalizedCounter[u-1]==-1) { /* Low proba symbol */103cumul[u] = cumul[u-1] + 1;104tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);105} else {106assert(normalizedCounter[u-1] >= 0);107cumul[u] = cumul[u-1] + (U16)normalizedCounter[u-1];108assert(cumul[u] >= cumul[u-1]); /* no overflow */109} }110cumul[maxSV1] = (U16)(tableSize+1);111}112113/* Spread symbols */114if (highThreshold == tableSize - 1) {115/* Case for no low prob count symbols. Lay down 8 bytes at a time116* to reduce branch misses since we are operating on a small block117*/118BYTE* const spread = tableSymbol + tableSize; /* size = tableSize + 8 (may write beyond tableSize) */119{ U64 const add = 0x0101010101010101ull;120size_t pos = 0;121U64 sv = 0;122U32 s;123for (s=0; s<maxSV1; ++s, sv += add) {124int i;125int const n = normalizedCounter[s];126MEM_write64(spread + pos, sv);127for (i = 8; i < n; i += 8) {128MEM_write64(spread + pos + i, sv);129}130assert(n>=0);131pos += (size_t)n;132}133}134/* Spread symbols across the table. Lack of lowprob symbols means that135* we don't need variable sized inner loop, so we can unroll the loop and136* reduce branch misses.137*/138{ size_t position = 0;139size_t s;140size_t const unroll = 2; /* Experimentally determined optimal unroll */141assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */142for (s = 0; s < (size_t)tableSize; s += unroll) {143size_t u;144for (u = 0; u < unroll; ++u) {145size_t const uPosition = (position + (u * step)) & tableMask;146tableSymbol[uPosition] = spread[s + u];147}148position = (position + (unroll * step)) & tableMask;149}150assert(position == 0); /* Must have initialized all positions */151}152} else {153U32 position = 0;154U32 symbol;155for (symbol=0; symbol<maxSV1; symbol++) {156int nbOccurrences;157int const freq = normalizedCounter[symbol];158for (nbOccurrences=0; nbOccurrences<freq; nbOccurrences++) {159tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;160position = (position + step) & tableMask;161while (position > highThreshold)162position = (position + step) & tableMask; /* Low proba area */163} }164assert(position==0); /* Must have initialized all positions */165}166167/* Build table */168{ U32 u; for (u=0; u<tableSize; u++) {169FSE_FUNCTION_TYPE s = tableSymbol[u]; /* note : static analyzer may not understand tableSymbol is properly initialized */170tableU16[cumul[s]++] = (U16) (tableSize+u); /* TableU16 : sorted by symbol order; gives next state value */171} }172173/* Build Symbol Transformation Table */174{ unsigned total = 0;175unsigned s;176for (s=0; s<=maxSymbolValue; s++) {177switch (normalizedCounter[s])178{179case 0:180/* filling nonetheless, for compatibility with FSE_getMaxNbBits() */181symbolTT[s].deltaNbBits = ((tableLog+1) << 16) - (1<<tableLog);182break;183184case -1:185case 1:186symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);187assert(total <= INT_MAX);188symbolTT[s].deltaFindState = (int)(total - 1);189total ++;190break;191default :192assert(normalizedCounter[s] > 1);193{ U32 const maxBitsOut = tableLog - BIT_highbit32 ((U32)normalizedCounter[s]-1);194U32 const minStatePlus = (U32)normalizedCounter[s] << maxBitsOut;195symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;196symbolTT[s].deltaFindState = (int)(total - (unsigned)normalizedCounter[s]);197total += (unsigned)normalizedCounter[s];198} } } }199200#if 0 /* debug : symbol costs */201DEBUGLOG(5, "\n --- table statistics : ");202{ U32 symbol;203for (symbol=0; symbol<=maxSymbolValue; symbol++) {204DEBUGLOG(5, "%3u: w=%3i, maxBits=%u, fracBits=%.2f",205symbol, normalizedCounter[symbol],206FSE_getMaxNbBits(symbolTT, symbol),207(double)FSE_bitCost(symbolTT, tableLog, symbol, 8) / 256);208} }209#endif210211return 0;212}213214215216#ifndef FSE_COMMONDEFS_ONLY217218/*-**************************************************************219* FSE NCount encoding220****************************************************************/221size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)222{223size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog224+ 4 /* bitCount initialized at 4 */225+ 2 /* first two symbols may use one additional bit each */) / 8)226+ 1 /* round up to whole nb bytes */227+ 2 /* additional two bytes for bitstream flush */;228return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND; /* maxSymbolValue==0 ? use default */229}230231static size_t232FSE_writeNCount_generic (void* header, size_t headerBufferSize,233const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,234unsigned writeIsSafe)235{236BYTE* const ostart = (BYTE*) header;237BYTE* out = ostart;238BYTE* const oend = ostart + headerBufferSize;239int nbBits;240const int tableSize = 1 << tableLog;241int remaining;242int threshold;243U32 bitStream = 0;244int bitCount = 0;245unsigned symbol = 0;246unsigned const alphabetSize = maxSymbolValue + 1;247int previousIs0 = 0;248249/* Table Size */250bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;251bitCount += 4;252253/* Init */254remaining = tableSize+1; /* +1 for extra accuracy */255threshold = tableSize;256nbBits = tableLog+1;257258while ((symbol < alphabetSize) && (remaining>1)) { /* stops at 1 */259if (previousIs0) {260unsigned start = symbol;261while ((symbol < alphabetSize) && !normalizedCounter[symbol]) symbol++;262if (symbol == alphabetSize) break; /* incorrect distribution */263while (symbol >= start+24) {264start+=24;265bitStream += 0xFFFFU << bitCount;266if ((!writeIsSafe) && (out > oend-2))267return ERROR(dstSize_tooSmall); /* Buffer overflow */268out[0] = (BYTE) bitStream;269out[1] = (BYTE)(bitStream>>8);270out+=2;271bitStream>>=16;272}273while (symbol >= start+3) {274start+=3;275bitStream += 3 << bitCount;276bitCount += 2;277}278bitStream += (symbol-start) << bitCount;279bitCount += 2;280if (bitCount>16) {281if ((!writeIsSafe) && (out > oend - 2))282return ERROR(dstSize_tooSmall); /* Buffer overflow */283out[0] = (BYTE)bitStream;284out[1] = (BYTE)(bitStream>>8);285out += 2;286bitStream >>= 16;287bitCount -= 16;288} }289{ int count = normalizedCounter[symbol++];290int const max = (2*threshold-1) - remaining;291remaining -= count < 0 ? -count : count;292count++; /* +1 for extra accuracy */293if (count>=threshold)294count += max; /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */295bitStream += count << bitCount;296bitCount += nbBits;297bitCount -= (count<max);298previousIs0 = (count==1);299if (remaining<1) return ERROR(GENERIC);300while (remaining<threshold) { nbBits--; threshold>>=1; }301}302if (bitCount>16) {303if ((!writeIsSafe) && (out > oend - 2))304return ERROR(dstSize_tooSmall); /* Buffer overflow */305out[0] = (BYTE)bitStream;306out[1] = (BYTE)(bitStream>>8);307out += 2;308bitStream >>= 16;309bitCount -= 16;310} }311312if (remaining != 1)313return ERROR(GENERIC); /* incorrect normalized distribution */314assert(symbol <= alphabetSize);315316/* flush remaining bitStream */317if ((!writeIsSafe) && (out > oend - 2))318return ERROR(dstSize_tooSmall); /* Buffer overflow */319out[0] = (BYTE)bitStream;320out[1] = (BYTE)(bitStream>>8);321out+= (bitCount+7) /8;322323return (out-ostart);324}325326327size_t FSE_writeNCount (void* buffer, size_t bufferSize,328const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)329{330if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported */331if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported */332333if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))334return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);335336return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1 /* write in buffer is safe */);337}338339340/*-**************************************************************341* FSE Compression Code342****************************************************************/343344FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)345{346size_t size;347if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;348size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);349return (FSE_CTable*)ZSTD_malloc(size);350}351352void FSE_freeCTable (FSE_CTable* ct) { ZSTD_free(ct); }353354/* provides the minimum logSize to safely represent a distribution */355static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)356{357U32 minBitsSrc = BIT_highbit32((U32)(srcSize)) + 1;358U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2;359U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;360assert(srcSize > 1); /* Not supported, RLE should be used instead */361return minBits;362}363364unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)365{366U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus;367U32 tableLog = maxTableLog;368U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);369assert(srcSize > 1); /* Not supported, RLE should be used instead */370if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;371if (maxBitsSrc < tableLog) tableLog = maxBitsSrc; /* Accuracy can be reduced */372if (minBits > tableLog) tableLog = minBits; /* Need a minimum to safely represent all symbol values */373if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;374if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;375return tableLog;376}377378unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)379{380return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);381}382383/* Secondary normalization method.384To be used when primary method fails. */385386static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue, short lowProbCount)387{388short const NOT_YET_ASSIGNED = -2;389U32 s;390U32 distributed = 0;391U32 ToDistribute;392393/* Init */394U32 const lowThreshold = (U32)(total >> tableLog);395U32 lowOne = (U32)((total * 3) >> (tableLog + 1));396397for (s=0; s<=maxSymbolValue; s++) {398if (count[s] == 0) {399norm[s]=0;400continue;401}402if (count[s] <= lowThreshold) {403norm[s] = lowProbCount;404distributed++;405total -= count[s];406continue;407}408if (count[s] <= lowOne) {409norm[s] = 1;410distributed++;411total -= count[s];412continue;413}414415norm[s]=NOT_YET_ASSIGNED;416}417ToDistribute = (1 << tableLog) - distributed;418419if (ToDistribute == 0)420return 0;421422if ((total / ToDistribute) > lowOne) {423/* risk of rounding to zero */424lowOne = (U32)((total * 3) / (ToDistribute * 2));425for (s=0; s<=maxSymbolValue; s++) {426if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {427norm[s] = 1;428distributed++;429total -= count[s];430continue;431} }432ToDistribute = (1 << tableLog) - distributed;433}434435if (distributed == maxSymbolValue+1) {436/* all values are pretty poor;437probably incompressible data (should have already been detected);438find max, then give all remaining points to max */439U32 maxV = 0, maxC = 0;440for (s=0; s<=maxSymbolValue; s++)441if (count[s] > maxC) { maxV=s; maxC=count[s]; }442norm[maxV] += (short)ToDistribute;443return 0;444}445446if (total == 0) {447/* all of the symbols were low enough for the lowOne or lowThreshold */448for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1))449if (norm[s] > 0) { ToDistribute--; norm[s]++; }450return 0;451}452453{ U64 const vStepLog = 62 - tableLog;454U64 const mid = (1ULL << (vStepLog-1)) - 1;455U64 const rStep = ZSTD_div64((((U64)1<<vStepLog) * ToDistribute) + mid, (U32)total); /* scale on remaining */456U64 tmpTotal = mid;457for (s=0; s<=maxSymbolValue; s++) {458if (norm[s]==NOT_YET_ASSIGNED) {459U64 const end = tmpTotal + (count[s] * rStep);460U32 const sStart = (U32)(tmpTotal >> vStepLog);461U32 const sEnd = (U32)(end >> vStepLog);462U32 const weight = sEnd - sStart;463if (weight < 1)464return ERROR(GENERIC);465norm[s] = (short)weight;466tmpTotal = end;467} } }468469return 0;470}471472size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,473const unsigned* count, size_t total,474unsigned maxSymbolValue, unsigned useLowProbCount)475{476/* Sanity checks */477if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;478if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported size */479if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported size */480if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */481482{ static U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };483short const lowProbCount = useLowProbCount ? -1 : 1;484U64 const scale = 62 - tableLog;485U64 const step = ZSTD_div64((U64)1<<62, (U32)total); /* <== here, one division ! */486U64 const vStep = 1ULL<<(scale-20);487int stillToDistribute = 1<<tableLog;488unsigned s;489unsigned largest=0;490short largestP=0;491U32 lowThreshold = (U32)(total >> tableLog);492493for (s=0; s<=maxSymbolValue; s++) {494if (count[s] == total) return 0; /* rle special case */495if (count[s] == 0) { normalizedCounter[s]=0; continue; }496if (count[s] <= lowThreshold) {497normalizedCounter[s] = lowProbCount;498stillToDistribute--;499} else {500short proba = (short)((count[s]*step) >> scale);501if (proba<8) {502U64 restToBeat = vStep * rtbTable[proba];503proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;504}505if (proba > largestP) { largestP=proba; largest=s; }506normalizedCounter[s] = proba;507stillToDistribute -= proba;508} }509if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {510/* corner case, need another normalization method */511size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue, lowProbCount);512if (FSE_isError(errorCode)) return errorCode;513}514else normalizedCounter[largest] += (short)stillToDistribute;515}516517#if 0518{ /* Print Table (debug) */519U32 s;520U32 nTotal = 0;521for (s=0; s<=maxSymbolValue; s++)522RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]);523for (s=0; s<=maxSymbolValue; s++)524nTotal += abs(normalizedCounter[s]);525if (nTotal != (1U<<tableLog))526RAWLOG(2, "Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);527getchar();528}529#endif530531return tableLog;532}533534535/* fake FSE_CTable, for raw (uncompressed) input */536size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits)537{538const unsigned tableSize = 1 << nbBits;539const unsigned tableMask = tableSize - 1;540const unsigned maxSymbolValue = tableMask;541void* const ptr = ct;542U16* const tableU16 = ( (U16*) ptr) + 2;543void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableSize>>1); /* assumption : tableLog >= 1 */544FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);545unsigned s;546547/* Sanity checks */548if (nbBits < 1) return ERROR(GENERIC); /* min size */549550/* header */551tableU16[-2] = (U16) nbBits;552tableU16[-1] = (U16) maxSymbolValue;553554/* Build table */555for (s=0; s<tableSize; s++)556tableU16[s] = (U16)(tableSize + s);557558/* Build Symbol Transformation Table */559{ const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);560for (s=0; s<=maxSymbolValue; s++) {561symbolTT[s].deltaNbBits = deltaNbBits;562symbolTT[s].deltaFindState = s-1;563} }564565return 0;566}567568/* fake FSE_CTable, for rle input (always same symbol) */569size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue)570{571void* ptr = ct;572U16* tableU16 = ( (U16*) ptr) + 2;573void* FSCTptr = (U32*)ptr + 2;574FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr;575576/* header */577tableU16[-2] = (U16) 0;578tableU16[-1] = (U16) symbolValue;579580/* Build table */581tableU16[0] = 0;582tableU16[1] = 0; /* just in case */583584/* Build Symbol Transformation Table */585symbolTT[symbolValue].deltaNbBits = 0;586symbolTT[symbolValue].deltaFindState = 0;587588return 0;589}590591592static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,593const void* src, size_t srcSize,594const FSE_CTable* ct, const unsigned fast)595{596const BYTE* const istart = (const BYTE*) src;597const BYTE* const iend = istart + srcSize;598const BYTE* ip=iend;599600BIT_CStream_t bitC;601FSE_CState_t CState1, CState2;602603/* init */604if (srcSize <= 2) return 0;605{ size_t const initError = BIT_initCStream(&bitC, dst, dstSize);606if (FSE_isError(initError)) return 0; /* not enough space available to write a bitstream */ }607608#define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))609610if (srcSize & 1) {611FSE_initCState2(&CState1, ct, *--ip);612FSE_initCState2(&CState2, ct, *--ip);613FSE_encodeSymbol(&bitC, &CState1, *--ip);614FSE_FLUSHBITS(&bitC);615} else {616FSE_initCState2(&CState2, ct, *--ip);617FSE_initCState2(&CState1, ct, *--ip);618}619620/* join to mod 4 */621srcSize -= 2;622if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) { /* test bit 2 */623FSE_encodeSymbol(&bitC, &CState2, *--ip);624FSE_encodeSymbol(&bitC, &CState1, *--ip);625FSE_FLUSHBITS(&bitC);626}627628/* 2 or 4 encoding per loop */629while ( ip>istart ) {630631FSE_encodeSymbol(&bitC, &CState2, *--ip);632633if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */634FSE_FLUSHBITS(&bitC);635636FSE_encodeSymbol(&bitC, &CState1, *--ip);637638if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) { /* this test must be static */639FSE_encodeSymbol(&bitC, &CState2, *--ip);640FSE_encodeSymbol(&bitC, &CState1, *--ip);641}642643FSE_FLUSHBITS(&bitC);644}645646FSE_flushCState(&bitC, &CState2);647FSE_flushCState(&bitC, &CState1);648return BIT_closeCStream(&bitC);649}650651size_t FSE_compress_usingCTable (void* dst, size_t dstSize,652const void* src, size_t srcSize,653const FSE_CTable* ct)654{655unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));656657if (fast)658return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);659else660return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);661}662663664size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }665666#ifndef ZSTD_NO_UNUSED_FUNCTIONS667/* FSE_compress_wksp() :668* Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).669* `wkspSize` size must be `(1<<tableLog)`.670*/671size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)672{673BYTE* const ostart = (BYTE*) dst;674BYTE* op = ostart;675BYTE* const oend = ostart + dstSize;676677unsigned count[FSE_MAX_SYMBOL_VALUE+1];678S16 norm[FSE_MAX_SYMBOL_VALUE+1];679FSE_CTable* CTable = (FSE_CTable*)workSpace;680size_t const CTableSize = FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue);681void* scratchBuffer = (void*)(CTable + CTableSize);682size_t const scratchBufferSize = wkspSize - (CTableSize * sizeof(FSE_CTable));683684/* init conditions */685if (wkspSize < FSE_COMPRESS_WKSP_SIZE_U32(tableLog, maxSymbolValue)) return ERROR(tableLog_tooLarge);686if (srcSize <= 1) return 0; /* Not compressible */687if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE;688if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG;689690/* Scan input and build symbol stats */691{ CHECK_V_F(maxCount, HIST_count_wksp(count, &maxSymbolValue, src, srcSize, scratchBuffer, scratchBufferSize) );692if (maxCount == srcSize) return 1; /* only a single symbol in src : rle */693if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */694if (maxCount < (srcSize >> 7)) return 0; /* Heuristic : not compressible enough */695}696697tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue);698CHECK_F( FSE_normalizeCount(norm, tableLog, count, srcSize, maxSymbolValue, /* useLowProbCount */ srcSize >= 2048) );699700/* Write table description header */701{ CHECK_V_F(nc_err, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) );702op += nc_err;703}704705/* Compress */706CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, scratchBufferSize) );707{ CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, src, srcSize, CTable) );708if (cSize == 0) return 0; /* not enough space for compressed data */709op += cSize;710}711712/* check compressibility */713if ( (size_t)(op-ostart) >= srcSize-1 ) return 0;714715return op-ostart;716}717718typedef struct {719FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)];720union {721U32 hist_wksp[HIST_WKSP_SIZE_U32];722BYTE scratchBuffer[1 << FSE_MAX_TABLELOG];723} workspace;724} fseWkspMax_t;725726size_t FSE_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)727{728fseWkspMax_t scratchBuffer;729DEBUG_STATIC_ASSERT(sizeof(scratchBuffer) >= FSE_COMPRESS_WKSP_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)); /* compilation failures here means scratchBuffer is not large enough */730if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);731return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, &scratchBuffer, sizeof(scratchBuffer));732}733734size_t FSE_compress (void* dst, size_t dstCapacity, const void* src, size_t srcSize)735{736return FSE_compress2(dst, dstCapacity, src, srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG);737}738#endif739740#endif /* FSE_COMMONDEFS_ONLY */741742743