Skip to content

Commit

Permalink
Refactor for api control
Browse files Browse the repository at this point in the history
  • Loading branch information
binhdvo committed Jan 22, 2022
1 parent a74f7e8 commit 3b4954b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 111 deletions.
2 changes: 1 addition & 1 deletion lib/common/fse.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCou
FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);

/*! FSE_estimateCompressedSize():
Estimate compressed size of data.
Estimate compressed size of data based on sum of fractional bit counts, will be >= actual cost.
@return : estimated size of compressed data */
FSE_PUBLIC_API size_t FSE_estimateCompressedSize (const FSE_CTable* ct, const unsigned* count, unsigned maxSymbolValue, unsigned tableLog);

Expand Down
34 changes: 21 additions & 13 deletions lib/common/huf.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ extern "C" {
* Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
* 'dst' buffer must be already allocated.
* Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
* If depthStrategy = HUF_seek_neighbor additional huffman depths will be considered
* `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
* @return : size of compressed data (<= `dstCapacity`).
* Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
* if HUF_isError(return), compression failed (more details using HUF_getErrorName())
*/
typedef enum {
HUF_default,
HUF_seek_neighbors
} HUF_depthStrategy;
HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
const void* src, size_t srcSize);
const void* src, size_t srcSize, HUF_depthStrategy depthStrategy);

/** HUF_decompress() :
* Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
Expand Down Expand Up @@ -82,20 +87,22 @@ HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error c
/** HUF_compress2() :
* Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.
* `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .
* `tableLog` must be `<= HUF_TABLELOG_MAX` . */
* `tableLog` must be `<= HUF_TABLELOG_MAX` .
* If depthStrategy = HUF_seek_neighbor additional huffman depths will be considered */
HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
unsigned maxSymbolValue, unsigned tableLog);
unsigned maxSymbolValue, unsigned tableLog, HUF_depthStrategy depthStrategy);

/** HUF_compress4X_wksp() :
* Same as HUF_compress2(), but uses externally allocated `workSpace`.
* `workspace` must be at least as large as HUF_WORKSPACE_SIZE */
* `workspace` must be at least as large as HUF_WORKSPACE_SIZE
* If depthStrategy = HUF_seek_neighbor additional huffman depths will be considered */
#define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)
#define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))
HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
unsigned maxSymbolValue, unsigned tableLog,
void* workSpace, size_t wkspSize);
void* workSpace, size_t wkspSize, HUF_depthStrategy depthStrategy);

#endif /* HUF_H_298734234 */

Expand Down Expand Up @@ -185,10 +192,10 @@ size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
* For example, it's possible to compress several blocks using the same 'CTable',
* or to save and regenerate 'CTable' using external methods.
*/
unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, const unsigned* count, unsigned maxSymbolValue);
unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, const unsigned* count, unsigned maxSymbolValue, HUF_depthStrategy strategy);
size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */
size_t HUF_getCTableSize (const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
size_t HUF_getCTableSize_wksp (const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
size_t HUF_getEncodedCTableSize (const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
size_t HUF_getEncodedCTableSize_wksp (const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
Expand All @@ -206,12 +213,13 @@ typedef enum {
* If it uses hufTable it does not modify hufTable or repeat.
* If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
* If preferRepeat then the old table will always be used if valid.
* If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
* If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding
* If depthStrategy = HUF_seek_neighbor additional huffman depths will be considered */
size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
const void* src, size_t srcSize,
unsigned maxSymbolValue, unsigned tableLog,
void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);
HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible, HUF_depthStrategy depthStrategy);

/** HUF_buildCTable_wksp() :
* Same as HUF_buildCTable(), but using externally allocated scratch buffer.
Expand Down Expand Up @@ -303,8 +311,8 @@ size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* c
/* single stream variants */
/* ====================== */

size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U64 U64 */
size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, HUF_depthStrategy depthStrategy);
size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_depthStrategy depthStrategy); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U64 U64 */
size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
size_t HUF_compress1X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);
/** HUF_compress1X_repeat() :
Expand All @@ -317,7 +325,7 @@ size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
const void* src, size_t srcSize,
unsigned maxSymbolValue, unsigned tableLog,
void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);
HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible, HUF_depthStrategy depthStrategy);

size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
#ifndef HUF_FORCE_DECOMPRESS_X1
Expand Down
25 changes: 3 additions & 22 deletions lib/compress/fse_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ size_t FSE_writeNCount (void* buffer, size_t bufferSize,

size_t FSE_estimateNCountSize(const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
{
size_t result = 0;
int nbBits;
const int tableSize = 1 << tableLog;
int remaining;
Expand All @@ -362,19 +361,8 @@ size_t FSE_estimateNCountSize(const short* normalizedCounter, unsigned maxSymbol
unsigned start = symbol;
while ((symbol < alphabetSize) && !normalizedCounter[symbol]) symbol++;
if (symbol == alphabetSize) break; /* incorrect distribution */
while (symbol >= start + 24) {
start += 24;
result += 2;
}
while (symbol >= start + 3) {
start += 3;
bitCount += 2;
}
bitCount += 2;
if (bitCount > 16) {
result += 2;
bitCount -= 16;
}
bitCount += ((symbol - start) / 24) * 16;
bitCount += (((symbol - start) % 24 + 2) / 3) * 2;
}
{ int count = normalizedCounter[symbol++];
int const max = (2 * threshold - 1) - remaining;
Expand All @@ -387,16 +375,9 @@ size_t FSE_estimateNCountSize(const short* normalizedCounter, unsigned maxSymbol
previousIs0 = (count == 1);
while (remaining < threshold) { nbBits--; threshold >>= 1; }
}
if (bitCount > 16) {
result += 2;
bitCount -= 16;
}
}

/* flush remaining bitStream */
result += (bitCount + 7) / 8;

return result;
return (bitCount + 7) / 8;
}

/*-**************************************************************
Expand Down
Loading

0 comments on commit 3b4954b

Please sign in to comment.