15#if defined (__cplusplus)
32#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
33# define FSE_PUBLIC_API __attribute__ ((visibility ("default")))
34#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1)
35# define FSE_PUBLIC_API __declspec(dllexport)
36#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
37# define FSE_PUBLIC_API __declspec(dllimport)
39# define FSE_PUBLIC_API
43#define FSE_VERSION_MAJOR 0
44#define FSE_VERSION_MINOR 9
45#define FSE_VERSION_RELEASE 0
47#define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE
48#define FSE_QUOTE(str) #str
49#define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str)
50#define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION)
52#define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE)
107 const unsigned* count,
size_t srcSize,
unsigned maxSymbolValue,
unsigned useLowProbCount);
119 const short* normalizedCounter,
120 unsigned maxSymbolValue,
unsigned tableLog);
190 unsigned* maxSymbolValuePtr,
unsigned* tableLogPtr,
191 const void* rBuffer,
size_t rBuffSize);
197 unsigned* maxSymbolValuePtr,
unsigned* tableLogPtr,
198 const void* rBuffer,
size_t rBuffSize,
int bmi2);
233#if defined(FSE_STATIC_LINKING_ONLY) && !defined(FSE_H_FSE_STATIC_LINKING_ONLY)
234#define FSE_H_FSE_STATIC_LINKING_ONLY
244#define FSE_NCOUNTBOUND 512
245#define FSE_BLOCKBOUND(size) ((size) + ((size)>>7) + 4 + sizeof(size_t) )
246#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size))
249#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<((maxTableLog)-1)) + (((maxSymbolValue)+1)*2))
250#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<(maxTableLog)))
253#define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable))
254#define FSE_DTABLE_SIZE(maxTableLog) (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable))
272#define FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog) (((maxSymbolValue + 2) + (1ull << (tableLog)))/2 + sizeof(U64)/sizeof(U32) )
273#define FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) (sizeof(unsigned) * FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog))
274size_t FSE_buildCTable_wksp(
FSE_CTable* ct,
const short* normalizedCounter,
unsigned maxSymbolValue,
unsigned tableLog,
void* workSpace,
size_t wkspSize);
276#define FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) (sizeof(short) * (maxSymbolValue + 1) + (1ULL << maxTableLog) + 8)
277#define FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ((FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) + sizeof(unsigned) - 1) / sizeof(unsigned))
281#define FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) (FSE_DTABLE_SIZE_U32(maxTableLog) + 1 + FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) + (FSE_MAX_SYMBOL_VALUE + 1) / 2 + 1)
282#define FSE_DECOMPRESS_WKSP_SIZE(maxTableLog, maxSymbolValue) (FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(unsigned))
283size_t FSE_decompress_wksp_bmi2(
void* dst,
size_t dstCapacity,
const void* cSrc,
size_t cSrcSize,
unsigned maxLog,
void* workSpace,
size_t wkspSize,
int bmi2);
302 const void* stateTable;
303 const void* symbolTT;
307static void FSE_initCState(FSE_CState_t* CStatePtr,
const FSE_CTable* ct);
309static void FSE_encodeSymbol(
BIT_CStream_t* bitC, FSE_CState_t* CStatePtr,
unsigned symbol);
311static void FSE_flushCState(
BIT_CStream_t* bitC,
const FSE_CState_t* CStatePtr);
368static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr,
BIT_DStream_t* bitD);
370static unsigned FSE_endOfDState(
const FSE_DState_t* DStatePtr);
425static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr,
BIT_DStream_t* bitD);
435} FSE_symbolCompressionTransform;
439 const void* ptr = ct;
440 const U16* u16ptr = (
const U16*) ptr;
442 statePtr->value = (ptrdiff_t)1<<tableLog;
443 statePtr->stateTable = u16ptr+2;
444 statePtr->symbolTT = ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1);
445 statePtr->stateLog = tableLog;
454 FSE_initCState(statePtr, ct);
455 {
const FSE_symbolCompressionTransform symbolTT = ((
const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
456 const U16* stateTable = (
const U16*)(statePtr->stateTable);
457 U32 nbBitsOut = (
U32)((symbolTT.deltaNbBits + (1<<15)) >> 16);
458 statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits;
459 statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
465 FSE_symbolCompressionTransform
const symbolTT = ((
const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
466 const U16*
const stateTable = (
const U16*)(statePtr->stateTable);
467 U32 const nbBitsOut = (
U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);
468 BIT_addBits(bitC, (
size_t)statePtr->value, nbBitsOut);
469 statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
474 BIT_addBits(bitC, (
size_t)statePtr->value, statePtr->stateLog);
486 const FSE_symbolCompressionTransform* symbolTT = (
const FSE_symbolCompressionTransform*) symbolTTPtr;
487 return (symbolTT[symbolValue].deltaNbBits + ((1<<16)-1)) >> 16;
496 const FSE_symbolCompressionTransform* symbolTT = (
const FSE_symbolCompressionTransform*) symbolTTPtr;
497 U32 const minNbBits = symbolTT[symbolValue].deltaNbBits >> 16;
498 U32 const threshold = (minNbBits+1) << 16;
500 assert(accuracyLog < 31-tableLog);
501 {
U32 const tableSize = 1 << tableLog;
502 U32 const deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize);
503 U32 const normalizedDeltaFromThreshold = (deltaFromThreshold << accuracyLog) >> tableLog;
504 U32 const bitMultiplier = 1 << accuracyLog;
505 assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold);
506 assert(normalizedDeltaFromThreshold <= bitMultiplier);
507 return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold;
521 unsigned short newState;
522 unsigned char symbol;
523 unsigned char nbBits;
528 const void* ptr = dt;
529 const FSE_DTableHeader*
const DTableH = (
const FSE_DTableHeader*)ptr;
530 DStatePtr->state =
BIT_readBits(bitD, DTableH->tableLog);
532 DStatePtr->table = dt + 1;
537 FSE_decode_t
const DInfo = ((
const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
543 FSE_decode_t
const DInfo = ((
const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
544 U32 const nbBits = DInfo.nbBits;
546 DStatePtr->state = DInfo.newState + lowBits;
551 FSE_decode_t
const DInfo = ((
const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
552 U32 const nbBits = DInfo.nbBits;
553 BYTE const symbol = DInfo.symbol;
556 DStatePtr->state = DInfo.newState + lowBits;
564 FSE_decode_t
const DInfo = ((
const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
565 U32 const nbBits = DInfo.nbBits;
566 BYTE const symbol = DInfo.symbol;
569 DStatePtr->state = DInfo.newState + lowBits;
573MEM_STATIC unsigned FSE_endOfDState(
const FSE_DState_t* DStatePtr)
575 return DStatePtr->state == 0;
580#ifndef FSE_COMMONDEFS_ONLY
590#ifndef FSE_MAX_MEMORY_USAGE
591# define FSE_MAX_MEMORY_USAGE 14
593#ifndef FSE_DEFAULT_MEMORY_USAGE
594# define FSE_DEFAULT_MEMORY_USAGE 13
596#if (FSE_DEFAULT_MEMORY_USAGE > FSE_MAX_MEMORY_USAGE)
597# error "FSE_DEFAULT_MEMORY_USAGE must be <= FSE_MAX_MEMORY_USAGE"
603#ifndef FSE_MAX_SYMBOL_VALUE
604# define FSE_MAX_SYMBOL_VALUE 255
610#define FSE_FUNCTION_TYPE BYTE
611#define FSE_FUNCTION_EXTENSION
612#define FSE_DECODE_TYPE FSE_decode_t
621#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
622#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
623#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
624#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
625#define FSE_MIN_TABLELOG 5
627#define FSE_TABLELOG_ABSOLUTE_MAX 15
628#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
629# error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
632#define FSE_TABLESTEP(tableSize) (((tableSize)>>1) + ((tableSize)>>3) + 3)
638#if defined (__cplusplus)
MEM_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, unsigned nbBits)
MEM_STATIC void BIT_flushBits(BIT_CStream_t *bitC)
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, unsigned nbBits)
MEM_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD)
#define assert(condition)
FSE_PUBLIC_API size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSymbolValuePtr, unsigned *tableLogPtr, const void *rBuffer, size_t rBuffSize)
FSE_PUBLIC_API size_t FSE_writeNCount(void *buffer, size_t bufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
FSE_PUBLIC_API const char * FSE_getErrorName(size_t code)
FSE_PUBLIC_API size_t FSE_readNCount_bmi2(short *normalizedCounter, unsigned *maxSymbolValuePtr, unsigned *tableLogPtr, const void *rBuffer, size_t rBuffSize, int bmi2)
FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable *ct, 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_versionNumber(void)
FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
FSE_PUBLIC_API size_t FSE_compressBound(size_t size)
FSE_PUBLIC_API size_t FSE_compress_usingCTable(void *dst, size_t dstCapacity, const void *src, size_t srcSize, const FSE_CTable *ct)
size_t FSE_buildCTable_wksp(FSE_CTable *ct, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize)
unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
size_t FSE_buildCTable_rle(FSE_CTable *ct, BYTE symbolValue)
size_t FSE_decompress_wksp_bmi2(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workSpace, size_t wkspSize, int bmi2)
size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize)
MEM_STATIC U16 MEM_read16(const void *memPtr)