10c16b537SWarner Losh /* ****************************************************************** 2*37f1f268SConrad Meyer * Huffman encoder, part of New Generation Entropy library 3*37f1f268SConrad Meyer * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. 4*37f1f268SConrad Meyer * 5*37f1f268SConrad Meyer * You can contact the author at : 6*37f1f268SConrad Meyer * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy 7*37f1f268SConrad Meyer * - Public forum : https://groups.google.com/forum/#!forum/lz4c 8*37f1f268SConrad Meyer * 9*37f1f268SConrad Meyer * This source code is licensed under both the BSD-style license (found in the 10*37f1f268SConrad Meyer * LICENSE file in the root directory of this source tree) and the GPLv2 (found 11*37f1f268SConrad Meyer * in the COPYING file in the root directory of this source tree). 12*37f1f268SConrad Meyer * You may select, at your option, one of the above-listed licenses. 130c16b537SWarner Losh ****************************************************************** */ 140c16b537SWarner Losh 150c16b537SWarner Losh /* ************************************************************** 160c16b537SWarner Losh * Compiler specifics 170c16b537SWarner Losh ****************************************************************/ 180c16b537SWarner Losh #ifdef _MSC_VER /* Visual Studio */ 190c16b537SWarner Losh # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ 200c16b537SWarner Losh #endif 210c16b537SWarner Losh 220c16b537SWarner Losh 230c16b537SWarner Losh /* ************************************************************** 240c16b537SWarner Losh * Includes 250c16b537SWarner Losh ****************************************************************/ 260c16b537SWarner Losh #include <string.h> /* memcpy, memset */ 270c16b537SWarner Losh #include <stdio.h> /* printf (debug) */ 28*37f1f268SConrad Meyer #include "../common/compiler.h" 29*37f1f268SConrad Meyer #include "../common/bitstream.h" 300f743729SConrad Meyer #include "hist.h" 310c16b537SWarner Losh #define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */ 32*37f1f268SConrad Meyer #include "../common/fse.h" /* header compression */ 330c16b537SWarner Losh #define HUF_STATIC_LINKING_ONLY 34*37f1f268SConrad Meyer #include "../common/huf.h" 35*37f1f268SConrad Meyer #include "../common/error_private.h" 360c16b537SWarner Losh 370c16b537SWarner Losh 380c16b537SWarner Losh /* ************************************************************** 390c16b537SWarner Losh * Error Management 400c16b537SWarner Losh ****************************************************************/ 410c16b537SWarner Losh #define HUF_isError ERR_isError 420f743729SConrad Meyer #define HUF_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */ 430c16b537SWarner Losh 440c16b537SWarner Losh 450c16b537SWarner Losh /* ************************************************************** 460c16b537SWarner Losh * Utils 470c16b537SWarner Losh ****************************************************************/ 480c16b537SWarner Losh unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue) 490c16b537SWarner Losh { 500c16b537SWarner Losh return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1); 510c16b537SWarner Losh } 520c16b537SWarner Losh 530c16b537SWarner Losh 540c16b537SWarner Losh /* ******************************************************* 550c16b537SWarner Losh * HUF : Huffman block compression 560c16b537SWarner Losh *********************************************************/ 570c16b537SWarner Losh /* HUF_compressWeights() : 580c16b537SWarner Losh * Same as FSE_compress(), but dedicated to huff0's weights compression. 590c16b537SWarner Losh * The use case needs much less stack memory. 600c16b537SWarner Losh * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX. 610c16b537SWarner Losh */ 620c16b537SWarner Losh #define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6 630f743729SConrad Meyer static size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weightTable, size_t wtSize) 640c16b537SWarner Losh { 650c16b537SWarner Losh BYTE* const ostart = (BYTE*) dst; 660c16b537SWarner Losh BYTE* op = ostart; 670c16b537SWarner Losh BYTE* const oend = ostart + dstSize; 680c16b537SWarner Losh 69a0483764SConrad Meyer unsigned maxSymbolValue = HUF_TABLELOG_MAX; 700c16b537SWarner Losh U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER; 710c16b537SWarner Losh 720c16b537SWarner Losh FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)]; 730c16b537SWarner Losh BYTE scratchBuffer[1<<MAX_FSE_TABLELOG_FOR_HUFF_HEADER]; 740c16b537SWarner Losh 75a0483764SConrad Meyer unsigned count[HUF_TABLELOG_MAX+1]; 760c16b537SWarner Losh S16 norm[HUF_TABLELOG_MAX+1]; 770c16b537SWarner Losh 780c16b537SWarner Losh /* init conditions */ 790c16b537SWarner Losh if (wtSize <= 1) return 0; /* Not compressible */ 800c16b537SWarner Losh 810c16b537SWarner Losh /* Scan input and build symbol stats */ 820f743729SConrad Meyer { unsigned const maxCount = HIST_count_simple(count, &maxSymbolValue, weightTable, wtSize); /* never fails */ 830c16b537SWarner Losh if (maxCount == wtSize) return 1; /* only a single symbol in src : rle */ 840c16b537SWarner Losh if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */ 850c16b537SWarner Losh } 860c16b537SWarner Losh 870c16b537SWarner Losh tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue); 880c16b537SWarner Losh CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) ); 890c16b537SWarner Losh 900c16b537SWarner Losh /* Write table description header */ 91*37f1f268SConrad Meyer { CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend-op), norm, maxSymbolValue, tableLog) ); 920c16b537SWarner Losh op += hSize; 930c16b537SWarner Losh } 940c16b537SWarner Losh 950c16b537SWarner Losh /* Compress */ 960c16b537SWarner Losh CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, sizeof(scratchBuffer)) ); 97*37f1f268SConrad Meyer { CHECK_V_F(cSize, FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, CTable) ); 980c16b537SWarner Losh if (cSize == 0) return 0; /* not enough space for compressed data */ 990c16b537SWarner Losh op += cSize; 1000c16b537SWarner Losh } 1010c16b537SWarner Losh 102*37f1f268SConrad Meyer return (size_t)(op-ostart); 1030c16b537SWarner Losh } 1040c16b537SWarner Losh 1050c16b537SWarner Losh 1060c16b537SWarner Losh struct HUF_CElt_s { 1070c16b537SWarner Losh U16 val; 1080c16b537SWarner Losh BYTE nbBits; 1090c16b537SWarner Losh }; /* typedef'd to HUF_CElt within "huf.h" */ 1100c16b537SWarner Losh 1110c16b537SWarner Losh /*! HUF_writeCTable() : 1120c16b537SWarner Losh `CTable` : Huffman tree to save, using huf representation. 1130c16b537SWarner Losh @return : size of saved CTable */ 1140c16b537SWarner Losh size_t HUF_writeCTable (void* dst, size_t maxDstSize, 115a0483764SConrad Meyer const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog) 1160c16b537SWarner Losh { 1170c16b537SWarner Losh BYTE bitsToWeight[HUF_TABLELOG_MAX + 1]; /* precomputed conversion table */ 1180c16b537SWarner Losh BYTE huffWeight[HUF_SYMBOLVALUE_MAX]; 1190c16b537SWarner Losh BYTE* op = (BYTE*)dst; 1200c16b537SWarner Losh U32 n; 1210c16b537SWarner Losh 1220c16b537SWarner Losh /* check conditions */ 1230c16b537SWarner Losh if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge); 1240c16b537SWarner Losh 1250c16b537SWarner Losh /* convert to weight */ 1260c16b537SWarner Losh bitsToWeight[0] = 0; 1270c16b537SWarner Losh for (n=1; n<huffLog+1; n++) 1280c16b537SWarner Losh bitsToWeight[n] = (BYTE)(huffLog + 1 - n); 1290c16b537SWarner Losh for (n=0; n<maxSymbolValue; n++) 1300c16b537SWarner Losh huffWeight[n] = bitsToWeight[CTable[n].nbBits]; 1310c16b537SWarner Losh 1320c16b537SWarner Losh /* attempt weights compression by FSE */ 1330c16b537SWarner Losh { CHECK_V_F(hSize, HUF_compressWeights(op+1, maxDstSize-1, huffWeight, maxSymbolValue) ); 1340c16b537SWarner Losh if ((hSize>1) & (hSize < maxSymbolValue/2)) { /* FSE compressed */ 1350c16b537SWarner Losh op[0] = (BYTE)hSize; 1360c16b537SWarner Losh return hSize+1; 1370c16b537SWarner Losh } } 1380c16b537SWarner Losh 1390c16b537SWarner Losh /* write raw values as 4-bits (max : 15) */ 1400c16b537SWarner Losh if (maxSymbolValue > (256-128)) return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */ 1410c16b537SWarner Losh if (((maxSymbolValue+1)/2) + 1 > maxDstSize) return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */ 1420c16b537SWarner Losh op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue-1)); 1430c16b537SWarner Losh huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */ 1440c16b537SWarner Losh for (n=0; n<maxSymbolValue; n+=2) 1450c16b537SWarner Losh op[(n/2)+1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n+1]); 1460c16b537SWarner Losh return ((maxSymbolValue+1)/2) + 1; 1470c16b537SWarner Losh } 1480c16b537SWarner Losh 1490c16b537SWarner Losh 150*37f1f268SConrad Meyer size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned* hasZeroWeights) 1510c16b537SWarner Losh { 1520c16b537SWarner Losh BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1]; /* init not required, even though some static analyzer may complain */ 1530c16b537SWarner Losh U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */ 1540c16b537SWarner Losh U32 tableLog = 0; 1550c16b537SWarner Losh U32 nbSymbols = 0; 1560c16b537SWarner Losh 1570c16b537SWarner Losh /* get symbol weights */ 1580c16b537SWarner Losh CHECK_V_F(readSize, HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX+1, rankVal, &nbSymbols, &tableLog, src, srcSize)); 1590c16b537SWarner Losh 1600c16b537SWarner Losh /* check result */ 1610c16b537SWarner Losh if (tableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge); 1620c16b537SWarner Losh if (nbSymbols > *maxSymbolValuePtr+1) return ERROR(maxSymbolValue_tooSmall); 1630c16b537SWarner Losh 1640c16b537SWarner Losh /* Prepare base value per rank */ 1650c16b537SWarner Losh { U32 n, nextRankStart = 0; 1660c16b537SWarner Losh for (n=1; n<=tableLog; n++) { 1670c16b537SWarner Losh U32 current = nextRankStart; 1680c16b537SWarner Losh nextRankStart += (rankVal[n] << (n-1)); 1690c16b537SWarner Losh rankVal[n] = current; 1700c16b537SWarner Losh } } 1710c16b537SWarner Losh 1720c16b537SWarner Losh /* fill nbBits */ 173*37f1f268SConrad Meyer *hasZeroWeights = 0; 1740c16b537SWarner Losh { U32 n; for (n=0; n<nbSymbols; n++) { 1750c16b537SWarner Losh const U32 w = huffWeight[n]; 176*37f1f268SConrad Meyer *hasZeroWeights |= (w == 0); 177*37f1f268SConrad Meyer CTable[n].nbBits = (BYTE)(tableLog + 1 - w) & -(w != 0); 1780c16b537SWarner Losh } } 1790c16b537SWarner Losh 1800c16b537SWarner Losh /* fill val */ 1810c16b537SWarner Losh { U16 nbPerRank[HUF_TABLELOG_MAX+2] = {0}; /* support w=0=>n=tableLog+1 */ 1820c16b537SWarner Losh U16 valPerRank[HUF_TABLELOG_MAX+2] = {0}; 1830c16b537SWarner Losh { U32 n; for (n=0; n<nbSymbols; n++) nbPerRank[CTable[n].nbBits]++; } 1840c16b537SWarner Losh /* determine stating value per rank */ 1850c16b537SWarner Losh valPerRank[tableLog+1] = 0; /* for w==0 */ 1860c16b537SWarner Losh { U16 min = 0; 1870c16b537SWarner Losh U32 n; for (n=tableLog; n>0; n--) { /* start at n=tablelog <-> w=1 */ 1880c16b537SWarner Losh valPerRank[n] = min; /* get starting value within each rank */ 1890c16b537SWarner Losh min += nbPerRank[n]; 1900c16b537SWarner Losh min >>= 1; 1910c16b537SWarner Losh } } 1920c16b537SWarner Losh /* assign value within rank, symbol order */ 1930c16b537SWarner Losh { U32 n; for (n=0; n<nbSymbols; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; } 1940c16b537SWarner Losh } 1950c16b537SWarner Losh 1960c16b537SWarner Losh *maxSymbolValuePtr = nbSymbols - 1; 1970c16b537SWarner Losh return readSize; 1980c16b537SWarner Losh } 1990c16b537SWarner Losh 2000f743729SConrad Meyer U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue) 2010f743729SConrad Meyer { 2020f743729SConrad Meyer const HUF_CElt* table = (const HUF_CElt*)symbolTable; 2030f743729SConrad Meyer assert(symbolValue <= HUF_SYMBOLVALUE_MAX); 2040f743729SConrad Meyer return table[symbolValue].nbBits; 2050f743729SConrad Meyer } 2060f743729SConrad Meyer 2070c16b537SWarner Losh 2080c16b537SWarner Losh typedef struct nodeElt_s { 2090c16b537SWarner Losh U32 count; 2100c16b537SWarner Losh U16 parent; 2110c16b537SWarner Losh BYTE byte; 2120c16b537SWarner Losh BYTE nbBits; 2130c16b537SWarner Losh } nodeElt; 2140c16b537SWarner Losh 2150c16b537SWarner Losh static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits) 2160c16b537SWarner Losh { 2170c16b537SWarner Losh const U32 largestBits = huffNode[lastNonNull].nbBits; 2180c16b537SWarner Losh if (largestBits <= maxNbBits) return largestBits; /* early exit : no elt > maxNbBits */ 2190c16b537SWarner Losh 2200c16b537SWarner Losh /* there are several too large elements (at least >= 2) */ 2210c16b537SWarner Losh { int totalCost = 0; 2220c16b537SWarner Losh const U32 baseCost = 1 << (largestBits - maxNbBits); 223*37f1f268SConrad Meyer int n = (int)lastNonNull; 2240c16b537SWarner Losh 2250c16b537SWarner Losh while (huffNode[n].nbBits > maxNbBits) { 2260c16b537SWarner Losh totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits)); 2270c16b537SWarner Losh huffNode[n].nbBits = (BYTE)maxNbBits; 2280c16b537SWarner Losh n --; 2290c16b537SWarner Losh } /* n stops at huffNode[n].nbBits <= maxNbBits */ 2300c16b537SWarner Losh while (huffNode[n].nbBits == maxNbBits) n--; /* n end at index of smallest symbol using < maxNbBits */ 2310c16b537SWarner Losh 2320c16b537SWarner Losh /* renorm totalCost */ 2330c16b537SWarner Losh totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */ 2340c16b537SWarner Losh 2350c16b537SWarner Losh /* repay normalized cost */ 2360c16b537SWarner Losh { U32 const noSymbol = 0xF0F0F0F0; 2370c16b537SWarner Losh U32 rankLast[HUF_TABLELOG_MAX+2]; 2380c16b537SWarner Losh 2390c16b537SWarner Losh /* Get pos of last (smallest) symbol per rank */ 2400c16b537SWarner Losh memset(rankLast, 0xF0, sizeof(rankLast)); 2410c16b537SWarner Losh { U32 currentNbBits = maxNbBits; 242*37f1f268SConrad Meyer int pos; 2430c16b537SWarner Losh for (pos=n ; pos >= 0; pos--) { 2440c16b537SWarner Losh if (huffNode[pos].nbBits >= currentNbBits) continue; 2450c16b537SWarner Losh currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */ 246*37f1f268SConrad Meyer rankLast[maxNbBits-currentNbBits] = (U32)pos; 2470c16b537SWarner Losh } } 2480c16b537SWarner Losh 2490c16b537SWarner Losh while (totalCost > 0) { 250*37f1f268SConrad Meyer U32 nBitsToDecrease = BIT_highbit32((U32)totalCost) + 1; 2510c16b537SWarner Losh for ( ; nBitsToDecrease > 1; nBitsToDecrease--) { 252*37f1f268SConrad Meyer U32 const highPos = rankLast[nBitsToDecrease]; 253*37f1f268SConrad Meyer U32 const lowPos = rankLast[nBitsToDecrease-1]; 2540c16b537SWarner Losh if (highPos == noSymbol) continue; 2550c16b537SWarner Losh if (lowPos == noSymbol) break; 2560c16b537SWarner Losh { U32 const highTotal = huffNode[highPos].count; 2570c16b537SWarner Losh U32 const lowTotal = 2 * huffNode[lowPos].count; 2580c16b537SWarner Losh if (highTotal <= lowTotal) break; 2590c16b537SWarner Losh } } 2600c16b537SWarner Losh /* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */ 2610c16b537SWarner Losh /* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */ 2620c16b537SWarner Losh while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol)) 2630c16b537SWarner Losh nBitsToDecrease ++; 2640c16b537SWarner Losh totalCost -= 1 << (nBitsToDecrease-1); 2650c16b537SWarner Losh if (rankLast[nBitsToDecrease-1] == noSymbol) 2660c16b537SWarner Losh rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease]; /* this rank is no longer empty */ 2670c16b537SWarner Losh huffNode[rankLast[nBitsToDecrease]].nbBits ++; 2680c16b537SWarner Losh if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */ 2690c16b537SWarner Losh rankLast[nBitsToDecrease] = noSymbol; 2700c16b537SWarner Losh else { 2710c16b537SWarner Losh rankLast[nBitsToDecrease]--; 2720c16b537SWarner Losh if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease) 2730c16b537SWarner Losh rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */ 2740c16b537SWarner Losh } } /* while (totalCost > 0) */ 2750c16b537SWarner Losh 2760c16b537SWarner Losh while (totalCost < 0) { /* Sometimes, cost correction overshoot */ 2770c16b537SWarner Losh if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */ 2780c16b537SWarner Losh while (huffNode[n].nbBits == maxNbBits) n--; 2790c16b537SWarner Losh huffNode[n+1].nbBits--; 280*37f1f268SConrad Meyer assert(n >= 0); 281*37f1f268SConrad Meyer rankLast[1] = (U32)(n+1); 2820c16b537SWarner Losh totalCost++; 2830c16b537SWarner Losh continue; 2840c16b537SWarner Losh } 2850c16b537SWarner Losh huffNode[ rankLast[1] + 1 ].nbBits--; 2860c16b537SWarner Losh rankLast[1]++; 2870c16b537SWarner Losh totalCost ++; 2880c16b537SWarner Losh } } } /* there are several too large elements (at least >= 2) */ 2890c16b537SWarner Losh 2900c16b537SWarner Losh return maxNbBits; 2910c16b537SWarner Losh } 2920c16b537SWarner Losh 2930c16b537SWarner Losh typedef struct { 2940c16b537SWarner Losh U32 base; 2950c16b537SWarner Losh U32 current; 2960c16b537SWarner Losh } rankPos; 2970c16b537SWarner Losh 298*37f1f268SConrad Meyer typedef nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32]; 299*37f1f268SConrad Meyer 300*37f1f268SConrad Meyer #define RANK_POSITION_TABLE_SIZE 32 301*37f1f268SConrad Meyer 302*37f1f268SConrad Meyer typedef struct { 303*37f1f268SConrad Meyer huffNodeTable huffNodeTbl; 304*37f1f268SConrad Meyer rankPos rankPosition[RANK_POSITION_TABLE_SIZE]; 305*37f1f268SConrad Meyer } HUF_buildCTable_wksp_tables; 306*37f1f268SConrad Meyer 307*37f1f268SConrad Meyer static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValue, rankPos* rankPosition) 3080c16b537SWarner Losh { 3090c16b537SWarner Losh U32 n; 3100c16b537SWarner Losh 311*37f1f268SConrad Meyer memset(rankPosition, 0, sizeof(*rankPosition) * RANK_POSITION_TABLE_SIZE); 3120c16b537SWarner Losh for (n=0; n<=maxSymbolValue; n++) { 3130c16b537SWarner Losh U32 r = BIT_highbit32(count[n] + 1); 314*37f1f268SConrad Meyer rankPosition[r].base ++; 3150c16b537SWarner Losh } 316*37f1f268SConrad Meyer for (n=30; n>0; n--) rankPosition[n-1].base += rankPosition[n].base; 317*37f1f268SConrad Meyer for (n=0; n<32; n++) rankPosition[n].current = rankPosition[n].base; 3180c16b537SWarner Losh for (n=0; n<=maxSymbolValue; n++) { 3190c16b537SWarner Losh U32 const c = count[n]; 3200c16b537SWarner Losh U32 const r = BIT_highbit32(c+1) + 1; 321*37f1f268SConrad Meyer U32 pos = rankPosition[r].current++; 322*37f1f268SConrad Meyer while ((pos > rankPosition[r].base) && (c > huffNode[pos-1].count)) { 32319fcbaf1SConrad Meyer huffNode[pos] = huffNode[pos-1]; 32419fcbaf1SConrad Meyer pos--; 32519fcbaf1SConrad Meyer } 3260c16b537SWarner Losh huffNode[pos].count = c; 3270c16b537SWarner Losh huffNode[pos].byte = (BYTE)n; 3280c16b537SWarner Losh } 3290c16b537SWarner Losh } 3300c16b537SWarner Losh 3310c16b537SWarner Losh 3320c16b537SWarner Losh /** HUF_buildCTable_wksp() : 3330c16b537SWarner Losh * Same as HUF_buildCTable(), but using externally allocated scratch buffer. 334*37f1f268SConrad Meyer * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as sizeof(HUF_buildCTable_wksp_tables). 3350c16b537SWarner Losh */ 3360c16b537SWarner Losh #define STARTNODE (HUF_SYMBOLVALUE_MAX+1) 337*37f1f268SConrad Meyer 338a0483764SConrad Meyer size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize) 3390c16b537SWarner Losh { 340*37f1f268SConrad Meyer HUF_buildCTable_wksp_tables* const wksp_tables = (HUF_buildCTable_wksp_tables*)workSpace; 341*37f1f268SConrad Meyer nodeElt* const huffNode0 = wksp_tables->huffNodeTbl; 3420c16b537SWarner Losh nodeElt* const huffNode = huffNode0+1; 343*37f1f268SConrad Meyer int nonNullRank; 3440c16b537SWarner Losh int lowS, lowN; 345*37f1f268SConrad Meyer int nodeNb = STARTNODE; 346*37f1f268SConrad Meyer int n, nodeRoot; 3470c16b537SWarner Losh 3480c16b537SWarner Losh /* safety checks */ 34919fcbaf1SConrad Meyer if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */ 350*37f1f268SConrad Meyer if (wkspSize < sizeof(HUF_buildCTable_wksp_tables)) 351*37f1f268SConrad Meyer return ERROR(workSpace_tooSmall); 3520c16b537SWarner Losh if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT; 353*37f1f268SConrad Meyer if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) 354*37f1f268SConrad Meyer return ERROR(maxSymbolValue_tooLarge); 3550c16b537SWarner Losh memset(huffNode0, 0, sizeof(huffNodeTable)); 3560c16b537SWarner Losh 3570c16b537SWarner Losh /* sort, decreasing order */ 358*37f1f268SConrad Meyer HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition); 3590c16b537SWarner Losh 3600c16b537SWarner Losh /* init for parents */ 361*37f1f268SConrad Meyer nonNullRank = (int)maxSymbolValue; 3620c16b537SWarner Losh while(huffNode[nonNullRank].count == 0) nonNullRank--; 3630c16b537SWarner Losh lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb; 3640c16b537SWarner Losh huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count; 365*37f1f268SConrad Meyer huffNode[lowS].parent = huffNode[lowS-1].parent = (U16)nodeNb; 3660c16b537SWarner Losh nodeNb++; lowS-=2; 3670c16b537SWarner Losh for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30); 3680c16b537SWarner Losh huffNode0[0].count = (U32)(1U<<31); /* fake entry, strong barrier */ 3690c16b537SWarner Losh 3700c16b537SWarner Losh /* create parents */ 3710c16b537SWarner Losh while (nodeNb <= nodeRoot) { 372*37f1f268SConrad Meyer int const n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; 373*37f1f268SConrad Meyer int const n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; 3740c16b537SWarner Losh huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count; 375*37f1f268SConrad Meyer huffNode[n1].parent = huffNode[n2].parent = (U16)nodeNb; 3760c16b537SWarner Losh nodeNb++; 3770c16b537SWarner Losh } 3780c16b537SWarner Losh 3790c16b537SWarner Losh /* distribute weights (unlimited tree height) */ 3800c16b537SWarner Losh huffNode[nodeRoot].nbBits = 0; 3810c16b537SWarner Losh for (n=nodeRoot-1; n>=STARTNODE; n--) 3820c16b537SWarner Losh huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1; 3830c16b537SWarner Losh for (n=0; n<=nonNullRank; n++) 3840c16b537SWarner Losh huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1; 3850c16b537SWarner Losh 3860c16b537SWarner Losh /* enforce maxTableLog */ 387*37f1f268SConrad Meyer maxNbBits = HUF_setMaxHeight(huffNode, (U32)nonNullRank, maxNbBits); 3880c16b537SWarner Losh 3890c16b537SWarner Losh /* fill result into tree (val, nbBits) */ 3900c16b537SWarner Losh { U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0}; 3910c16b537SWarner Losh U16 valPerRank[HUF_TABLELOG_MAX+1] = {0}; 392*37f1f268SConrad Meyer int const alphabetSize = (int)(maxSymbolValue + 1); 3930c16b537SWarner Losh if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */ 3940c16b537SWarner Losh for (n=0; n<=nonNullRank; n++) 3950c16b537SWarner Losh nbPerRank[huffNode[n].nbBits]++; 3960c16b537SWarner Losh /* determine stating value per rank */ 3970c16b537SWarner Losh { U16 min = 0; 398*37f1f268SConrad Meyer for (n=(int)maxNbBits; n>0; n--) { 3990c16b537SWarner Losh valPerRank[n] = min; /* get starting value within each rank */ 4000c16b537SWarner Losh min += nbPerRank[n]; 4010c16b537SWarner Losh min >>= 1; 4020c16b537SWarner Losh } } 403*37f1f268SConrad Meyer for (n=0; n<alphabetSize; n++) 4040c16b537SWarner Losh tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */ 405*37f1f268SConrad Meyer for (n=0; n<alphabetSize; n++) 4060c16b537SWarner Losh tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */ 4070c16b537SWarner Losh } 4080c16b537SWarner Losh 4090c16b537SWarner Losh return maxNbBits; 4100c16b537SWarner Losh } 4110c16b537SWarner Losh 4120c16b537SWarner Losh /** HUF_buildCTable() : 41319fcbaf1SConrad Meyer * @return : maxNbBits 4140c16b537SWarner Losh * Note : count is used before tree is written, so they can safely overlap 4150c16b537SWarner Losh */ 416a0483764SConrad Meyer size_t HUF_buildCTable (HUF_CElt* tree, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits) 4170c16b537SWarner Losh { 418*37f1f268SConrad Meyer HUF_buildCTable_wksp_tables workspace; 419*37f1f268SConrad Meyer return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, &workspace, sizeof(workspace)); 4200c16b537SWarner Losh } 4210c16b537SWarner Losh 422*37f1f268SConrad Meyer size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) 4230c16b537SWarner Losh { 4240c16b537SWarner Losh size_t nbBits = 0; 4250c16b537SWarner Losh int s; 4260c16b537SWarner Losh for (s = 0; s <= (int)maxSymbolValue; ++s) { 4270c16b537SWarner Losh nbBits += CTable[s].nbBits * count[s]; 4280c16b537SWarner Losh } 4290c16b537SWarner Losh return nbBits >> 3; 4300c16b537SWarner Losh } 4310c16b537SWarner Losh 432*37f1f268SConrad Meyer int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) { 4330c16b537SWarner Losh int bad = 0; 4340c16b537SWarner Losh int s; 4350c16b537SWarner Losh for (s = 0; s <= (int)maxSymbolValue; ++s) { 4360c16b537SWarner Losh bad |= (count[s] != 0) & (CTable[s].nbBits == 0); 4370c16b537SWarner Losh } 4380c16b537SWarner Losh return !bad; 4390c16b537SWarner Losh } 4400c16b537SWarner Losh 44119fcbaf1SConrad Meyer size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); } 44219fcbaf1SConrad Meyer 44319fcbaf1SConrad Meyer FORCE_INLINE_TEMPLATE void 44419fcbaf1SConrad Meyer HUF_encodeSymbol(BIT_CStream_t* bitCPtr, U32 symbol, const HUF_CElt* CTable) 4450c16b537SWarner Losh { 4460c16b537SWarner Losh BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits); 4470c16b537SWarner Losh } 4480c16b537SWarner Losh 4490c16b537SWarner Losh #define HUF_FLUSHBITS(s) BIT_flushBits(s) 4500c16b537SWarner Losh 4510c16b537SWarner Losh #define HUF_FLUSHBITS_1(stream) \ 4520c16b537SWarner Losh if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*2+7) HUF_FLUSHBITS(stream) 4530c16b537SWarner Losh 4540c16b537SWarner Losh #define HUF_FLUSHBITS_2(stream) \ 4550c16b537SWarner Losh if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*4+7) HUF_FLUSHBITS(stream) 4560c16b537SWarner Losh 45719fcbaf1SConrad Meyer FORCE_INLINE_TEMPLATE size_t 45819fcbaf1SConrad Meyer HUF_compress1X_usingCTable_internal_body(void* dst, size_t dstSize, 45919fcbaf1SConrad Meyer const void* src, size_t srcSize, 46019fcbaf1SConrad Meyer const HUF_CElt* CTable) 4610c16b537SWarner Losh { 4620c16b537SWarner Losh const BYTE* ip = (const BYTE*) src; 4630c16b537SWarner Losh BYTE* const ostart = (BYTE*)dst; 4640c16b537SWarner Losh BYTE* const oend = ostart + dstSize; 4650c16b537SWarner Losh BYTE* op = ostart; 4660c16b537SWarner Losh size_t n; 4670c16b537SWarner Losh BIT_CStream_t bitC; 4680c16b537SWarner Losh 4690c16b537SWarner Losh /* init */ 4700c16b537SWarner Losh if (dstSize < 8) return 0; /* not enough space to compress */ 471*37f1f268SConrad Meyer { size_t const initErr = BIT_initCStream(&bitC, op, (size_t)(oend-op)); 4720c16b537SWarner Losh if (HUF_isError(initErr)) return 0; } 4730c16b537SWarner Losh 4740c16b537SWarner Losh n = srcSize & ~3; /* join to mod 4 */ 4750c16b537SWarner Losh switch (srcSize & 3) 4760c16b537SWarner Losh { 4770c16b537SWarner Losh case 3 : HUF_encodeSymbol(&bitC, ip[n+ 2], CTable); 4780c16b537SWarner Losh HUF_FLUSHBITS_2(&bitC); 4790c16b537SWarner Losh /* fall-through */ 4800c16b537SWarner Losh case 2 : HUF_encodeSymbol(&bitC, ip[n+ 1], CTable); 4810c16b537SWarner Losh HUF_FLUSHBITS_1(&bitC); 4820c16b537SWarner Losh /* fall-through */ 4830c16b537SWarner Losh case 1 : HUF_encodeSymbol(&bitC, ip[n+ 0], CTable); 4840c16b537SWarner Losh HUF_FLUSHBITS(&bitC); 4850c16b537SWarner Losh /* fall-through */ 4860c16b537SWarner Losh case 0 : /* fall-through */ 4870c16b537SWarner Losh default: break; 4880c16b537SWarner Losh } 4890c16b537SWarner Losh 4900c16b537SWarner Losh for (; n>0; n-=4) { /* note : n&3==0 at this stage */ 4910c16b537SWarner Losh HUF_encodeSymbol(&bitC, ip[n- 1], CTable); 4920c16b537SWarner Losh HUF_FLUSHBITS_1(&bitC); 4930c16b537SWarner Losh HUF_encodeSymbol(&bitC, ip[n- 2], CTable); 4940c16b537SWarner Losh HUF_FLUSHBITS_2(&bitC); 4950c16b537SWarner Losh HUF_encodeSymbol(&bitC, ip[n- 3], CTable); 4960c16b537SWarner Losh HUF_FLUSHBITS_1(&bitC); 4970c16b537SWarner Losh HUF_encodeSymbol(&bitC, ip[n- 4], CTable); 4980c16b537SWarner Losh HUF_FLUSHBITS(&bitC); 4990c16b537SWarner Losh } 5000c16b537SWarner Losh 5010c16b537SWarner Losh return BIT_closeCStream(&bitC); 5020c16b537SWarner Losh } 5030c16b537SWarner Losh 50419fcbaf1SConrad Meyer #if DYNAMIC_BMI2 5050c16b537SWarner Losh 50619fcbaf1SConrad Meyer static TARGET_ATTRIBUTE("bmi2") size_t 50719fcbaf1SConrad Meyer HUF_compress1X_usingCTable_internal_bmi2(void* dst, size_t dstSize, 50819fcbaf1SConrad Meyer const void* src, size_t srcSize, 50919fcbaf1SConrad Meyer const HUF_CElt* CTable) 51019fcbaf1SConrad Meyer { 51119fcbaf1SConrad Meyer return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); 51219fcbaf1SConrad Meyer } 51319fcbaf1SConrad Meyer 51419fcbaf1SConrad Meyer static size_t 51519fcbaf1SConrad Meyer HUF_compress1X_usingCTable_internal_default(void* dst, size_t dstSize, 51619fcbaf1SConrad Meyer const void* src, size_t srcSize, 51719fcbaf1SConrad Meyer const HUF_CElt* CTable) 51819fcbaf1SConrad Meyer { 51919fcbaf1SConrad Meyer return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); 52019fcbaf1SConrad Meyer } 52119fcbaf1SConrad Meyer 52219fcbaf1SConrad Meyer static size_t 52319fcbaf1SConrad Meyer HUF_compress1X_usingCTable_internal(void* dst, size_t dstSize, 52419fcbaf1SConrad Meyer const void* src, size_t srcSize, 52519fcbaf1SConrad Meyer const HUF_CElt* CTable, const int bmi2) 52619fcbaf1SConrad Meyer { 52719fcbaf1SConrad Meyer if (bmi2) { 52819fcbaf1SConrad Meyer return HUF_compress1X_usingCTable_internal_bmi2(dst, dstSize, src, srcSize, CTable); 52919fcbaf1SConrad Meyer } 53019fcbaf1SConrad Meyer return HUF_compress1X_usingCTable_internal_default(dst, dstSize, src, srcSize, CTable); 53119fcbaf1SConrad Meyer } 53219fcbaf1SConrad Meyer 53319fcbaf1SConrad Meyer #else 53419fcbaf1SConrad Meyer 53519fcbaf1SConrad Meyer static size_t 53619fcbaf1SConrad Meyer HUF_compress1X_usingCTable_internal(void* dst, size_t dstSize, 53719fcbaf1SConrad Meyer const void* src, size_t srcSize, 53819fcbaf1SConrad Meyer const HUF_CElt* CTable, const int bmi2) 53919fcbaf1SConrad Meyer { 54019fcbaf1SConrad Meyer (void)bmi2; 54119fcbaf1SConrad Meyer return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); 54219fcbaf1SConrad Meyer } 54319fcbaf1SConrad Meyer 54419fcbaf1SConrad Meyer #endif 54519fcbaf1SConrad Meyer 54619fcbaf1SConrad Meyer size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) 54719fcbaf1SConrad Meyer { 54819fcbaf1SConrad Meyer return HUF_compress1X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, /* bmi2 */ 0); 54919fcbaf1SConrad Meyer } 55019fcbaf1SConrad Meyer 55119fcbaf1SConrad Meyer 55219fcbaf1SConrad Meyer static size_t 55319fcbaf1SConrad Meyer HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize, 55419fcbaf1SConrad Meyer const void* src, size_t srcSize, 55519fcbaf1SConrad Meyer const HUF_CElt* CTable, int bmi2) 5560c16b537SWarner Losh { 5570c16b537SWarner Losh size_t const segmentSize = (srcSize+3)/4; /* first 3 segments */ 5580c16b537SWarner Losh const BYTE* ip = (const BYTE*) src; 5590c16b537SWarner Losh const BYTE* const iend = ip + srcSize; 5600c16b537SWarner Losh BYTE* const ostart = (BYTE*) dst; 5610c16b537SWarner Losh BYTE* const oend = ostart + dstSize; 5620c16b537SWarner Losh BYTE* op = ostart; 5630c16b537SWarner Losh 5640c16b537SWarner Losh if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */ 5650c16b537SWarner Losh if (srcSize < 12) return 0; /* no saving possible : too small input */ 5660c16b537SWarner Losh op += 6; /* jumpTable */ 5670c16b537SWarner Losh 568*37f1f268SConrad Meyer assert(op <= oend); 569*37f1f268SConrad Meyer { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) ); 5700c16b537SWarner Losh if (cSize==0) return 0; 57119fcbaf1SConrad Meyer assert(cSize <= 65535); 5720c16b537SWarner Losh MEM_writeLE16(ostart, (U16)cSize); 5730c16b537SWarner Losh op += cSize; 5740c16b537SWarner Losh } 5750c16b537SWarner Losh 5760c16b537SWarner Losh ip += segmentSize; 577*37f1f268SConrad Meyer assert(op <= oend); 578*37f1f268SConrad Meyer { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) ); 5790c16b537SWarner Losh if (cSize==0) return 0; 58019fcbaf1SConrad Meyer assert(cSize <= 65535); 5810c16b537SWarner Losh MEM_writeLE16(ostart+2, (U16)cSize); 5820c16b537SWarner Losh op += cSize; 5830c16b537SWarner Losh } 5840c16b537SWarner Losh 5850c16b537SWarner Losh ip += segmentSize; 586*37f1f268SConrad Meyer assert(op <= oend); 587*37f1f268SConrad Meyer { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) ); 5880c16b537SWarner Losh if (cSize==0) return 0; 58919fcbaf1SConrad Meyer assert(cSize <= 65535); 5900c16b537SWarner Losh MEM_writeLE16(ostart+4, (U16)cSize); 5910c16b537SWarner Losh op += cSize; 5920c16b537SWarner Losh } 5930c16b537SWarner Losh 5940c16b537SWarner Losh ip += segmentSize; 595*37f1f268SConrad Meyer assert(op <= oend); 596*37f1f268SConrad Meyer assert(ip <= iend); 597*37f1f268SConrad Meyer { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, (size_t)(iend-ip), CTable, bmi2) ); 5980c16b537SWarner Losh if (cSize==0) return 0; 5990c16b537SWarner Losh op += cSize; 6000c16b537SWarner Losh } 6010c16b537SWarner Losh 602*37f1f268SConrad Meyer return (size_t)(op-ostart); 6030c16b537SWarner Losh } 6040c16b537SWarner Losh 60519fcbaf1SConrad Meyer size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) 60619fcbaf1SConrad Meyer { 60719fcbaf1SConrad Meyer return HUF_compress4X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, /* bmi2 */ 0); 60819fcbaf1SConrad Meyer } 60919fcbaf1SConrad Meyer 610a0483764SConrad Meyer typedef enum { HUF_singleStream, HUF_fourStreams } HUF_nbStreams_e; 6110c16b537SWarner Losh 6120c16b537SWarner Losh static size_t HUF_compressCTable_internal( 6130c16b537SWarner Losh BYTE* const ostart, BYTE* op, BYTE* const oend, 6140c16b537SWarner Losh const void* src, size_t srcSize, 615a0483764SConrad Meyer HUF_nbStreams_e nbStreams, const HUF_CElt* CTable, const int bmi2) 6160c16b537SWarner Losh { 617a0483764SConrad Meyer size_t const cSize = (nbStreams==HUF_singleStream) ? 618*37f1f268SConrad Meyer HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), src, srcSize, CTable, bmi2) : 619*37f1f268SConrad Meyer HUF_compress4X_usingCTable_internal(op, (size_t)(oend - op), src, srcSize, CTable, bmi2); 6200c16b537SWarner Losh if (HUF_isError(cSize)) { return cSize; } 6210c16b537SWarner Losh if (cSize==0) { return 0; } /* uncompressible */ 6220c16b537SWarner Losh op += cSize; 6230c16b537SWarner Losh /* check compressibility */ 624*37f1f268SConrad Meyer assert(op >= ostart); 6250c16b537SWarner Losh if ((size_t)(op-ostart) >= srcSize-1) { return 0; } 626*37f1f268SConrad Meyer return (size_t)(op-ostart); 6270c16b537SWarner Losh } 6280c16b537SWarner Losh 62919fcbaf1SConrad Meyer typedef struct { 630a0483764SConrad Meyer unsigned count[HUF_SYMBOLVALUE_MAX + 1]; 63119fcbaf1SConrad Meyer HUF_CElt CTable[HUF_SYMBOLVALUE_MAX + 1]; 632*37f1f268SConrad Meyer HUF_buildCTable_wksp_tables buildCTable_wksp; 63319fcbaf1SConrad Meyer } HUF_compress_tables_t; 6340c16b537SWarner Losh 63519fcbaf1SConrad Meyer /* HUF_compress_internal() : 63619fcbaf1SConrad Meyer * `workSpace` must a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */ 637a0483764SConrad Meyer static size_t 638a0483764SConrad Meyer HUF_compress_internal (void* dst, size_t dstSize, 6390c16b537SWarner Losh const void* src, size_t srcSize, 6400c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog, 641a0483764SConrad Meyer HUF_nbStreams_e nbStreams, 6420c16b537SWarner Losh void* workSpace, size_t wkspSize, 64319fcbaf1SConrad Meyer HUF_CElt* oldHufTable, HUF_repeat* repeat, int preferRepeat, 64419fcbaf1SConrad Meyer const int bmi2) 6450c16b537SWarner Losh { 64619fcbaf1SConrad Meyer HUF_compress_tables_t* const table = (HUF_compress_tables_t*)workSpace; 6470c16b537SWarner Losh BYTE* const ostart = (BYTE*)dst; 6480c16b537SWarner Losh BYTE* const oend = ostart + dstSize; 6490c16b537SWarner Losh BYTE* op = ostart; 6500c16b537SWarner Losh 651*37f1f268SConrad Meyer HUF_STATIC_ASSERT(sizeof(*table) <= HUF_WORKSPACE_SIZE); 652*37f1f268SConrad Meyer 6530c16b537SWarner Losh /* checks & inits */ 65419fcbaf1SConrad Meyer if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */ 655a0483764SConrad Meyer if (wkspSize < HUF_WORKSPACE_SIZE) return ERROR(workSpace_tooSmall); 65619fcbaf1SConrad Meyer if (!srcSize) return 0; /* Uncompressed */ 65719fcbaf1SConrad Meyer if (!dstSize) return 0; /* cannot fit anything within dst budget */ 6580c16b537SWarner Losh if (srcSize > HUF_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); /* current block size limit */ 6590c16b537SWarner Losh if (huffLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge); 66019fcbaf1SConrad Meyer if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge); 6610c16b537SWarner Losh if (!maxSymbolValue) maxSymbolValue = HUF_SYMBOLVALUE_MAX; 6620c16b537SWarner Losh if (!huffLog) huffLog = HUF_TABLELOG_DEFAULT; 6630c16b537SWarner Losh 66419fcbaf1SConrad Meyer /* Heuristic : If old table is valid, use it for small inputs */ 6650c16b537SWarner Losh if (preferRepeat && repeat && *repeat == HUF_repeat_valid) { 66619fcbaf1SConrad Meyer return HUF_compressCTable_internal(ostart, op, oend, 66719fcbaf1SConrad Meyer src, srcSize, 668a0483764SConrad Meyer nbStreams, oldHufTable, bmi2); 6690c16b537SWarner Losh } 6700c16b537SWarner Losh 6710c16b537SWarner Losh /* Scan input and build symbol stats */ 672a0483764SConrad Meyer { CHECK_V_F(largest, HIST_count_wksp (table->count, &maxSymbolValue, (const BYTE*)src, srcSize, workSpace, wkspSize) ); 6730c16b537SWarner Losh if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; } /* single symbol, rle */ 6740f743729SConrad Meyer if (largest <= (srcSize >> 7)+4) return 0; /* heuristic : probably not compressible enough */ 6750c16b537SWarner Losh } 6760c16b537SWarner Losh 6770c16b537SWarner Losh /* Check validity of previous table */ 67819fcbaf1SConrad Meyer if ( repeat 67919fcbaf1SConrad Meyer && *repeat == HUF_repeat_check 68019fcbaf1SConrad Meyer && !HUF_validateCTable(oldHufTable, table->count, maxSymbolValue)) { 6810c16b537SWarner Losh *repeat = HUF_repeat_none; 6820c16b537SWarner Losh } 6830c16b537SWarner Losh /* Heuristic : use existing table for small inputs */ 6840c16b537SWarner Losh if (preferRepeat && repeat && *repeat != HUF_repeat_none) { 68519fcbaf1SConrad Meyer return HUF_compressCTable_internal(ostart, op, oend, 68619fcbaf1SConrad Meyer src, srcSize, 687a0483764SConrad Meyer nbStreams, oldHufTable, bmi2); 6880c16b537SWarner Losh } 6890c16b537SWarner Losh 6900c16b537SWarner Losh /* Build Huffman Tree */ 6910c16b537SWarner Losh huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue); 692a0483764SConrad Meyer { size_t const maxBits = HUF_buildCTable_wksp(table->CTable, table->count, 69319fcbaf1SConrad Meyer maxSymbolValue, huffLog, 694*37f1f268SConrad Meyer &table->buildCTable_wksp, sizeof(table->buildCTable_wksp)); 695a0483764SConrad Meyer CHECK_F(maxBits); 6960c16b537SWarner Losh huffLog = (U32)maxBits; 69719fcbaf1SConrad Meyer /* Zero unused symbols in CTable, so we can check it for validity */ 69819fcbaf1SConrad Meyer memset(table->CTable + (maxSymbolValue + 1), 0, 69919fcbaf1SConrad Meyer sizeof(table->CTable) - ((maxSymbolValue + 1) * sizeof(HUF_CElt))); 7000c16b537SWarner Losh } 7010c16b537SWarner Losh 7020c16b537SWarner Losh /* Write table description header */ 70319fcbaf1SConrad Meyer { CHECK_V_F(hSize, HUF_writeCTable (op, dstSize, table->CTable, maxSymbolValue, huffLog) ); 70419fcbaf1SConrad Meyer /* Check if using previous huffman table is beneficial */ 7050c16b537SWarner Losh if (repeat && *repeat != HUF_repeat_none) { 70619fcbaf1SConrad Meyer size_t const oldSize = HUF_estimateCompressedSize(oldHufTable, table->count, maxSymbolValue); 70719fcbaf1SConrad Meyer size_t const newSize = HUF_estimateCompressedSize(table->CTable, table->count, maxSymbolValue); 7080c16b537SWarner Losh if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) { 70919fcbaf1SConrad Meyer return HUF_compressCTable_internal(ostart, op, oend, 71019fcbaf1SConrad Meyer src, srcSize, 711a0483764SConrad Meyer nbStreams, oldHufTable, bmi2); 71219fcbaf1SConrad Meyer } } 71319fcbaf1SConrad Meyer 71419fcbaf1SConrad Meyer /* Use the new huffman table */ 7150c16b537SWarner Losh if (hSize + 12ul >= srcSize) { return 0; } 7160c16b537SWarner Losh op += hSize; 7170c16b537SWarner Losh if (repeat) { *repeat = HUF_repeat_none; } 71819fcbaf1SConrad Meyer if (oldHufTable) 71919fcbaf1SConrad Meyer memcpy(oldHufTable, table->CTable, sizeof(table->CTable)); /* Save new table */ 7200c16b537SWarner Losh } 72119fcbaf1SConrad Meyer return HUF_compressCTable_internal(ostart, op, oend, 72219fcbaf1SConrad Meyer src, srcSize, 723a0483764SConrad Meyer nbStreams, table->CTable, bmi2); 7240c16b537SWarner Losh } 7250c16b537SWarner Losh 7260c16b537SWarner Losh 7270c16b537SWarner Losh size_t HUF_compress1X_wksp (void* dst, size_t dstSize, 7280c16b537SWarner Losh const void* src, size_t srcSize, 7290c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog, 7300c16b537SWarner Losh void* workSpace, size_t wkspSize) 7310c16b537SWarner Losh { 73219fcbaf1SConrad Meyer return HUF_compress_internal(dst, dstSize, src, srcSize, 733a0483764SConrad Meyer maxSymbolValue, huffLog, HUF_singleStream, 73419fcbaf1SConrad Meyer workSpace, wkspSize, 73519fcbaf1SConrad Meyer NULL, NULL, 0, 0 /*bmi2*/); 7360c16b537SWarner Losh } 7370c16b537SWarner Losh 7380c16b537SWarner Losh size_t HUF_compress1X_repeat (void* dst, size_t dstSize, 7390c16b537SWarner Losh const void* src, size_t srcSize, 7400c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog, 7410c16b537SWarner Losh void* workSpace, size_t wkspSize, 74219fcbaf1SConrad Meyer HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2) 7430c16b537SWarner Losh { 74419fcbaf1SConrad Meyer return HUF_compress_internal(dst, dstSize, src, srcSize, 745a0483764SConrad Meyer maxSymbolValue, huffLog, HUF_singleStream, 74619fcbaf1SConrad Meyer workSpace, wkspSize, hufTable, 74719fcbaf1SConrad Meyer repeat, preferRepeat, bmi2); 7480c16b537SWarner Losh } 7490c16b537SWarner Losh 7500c16b537SWarner Losh size_t HUF_compress1X (void* dst, size_t dstSize, 7510c16b537SWarner Losh const void* src, size_t srcSize, 7520c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog) 7530c16b537SWarner Losh { 75419fcbaf1SConrad Meyer unsigned workSpace[HUF_WORKSPACE_SIZE_U32]; 7550c16b537SWarner Losh return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace)); 7560c16b537SWarner Losh } 7570c16b537SWarner Losh 75819fcbaf1SConrad Meyer /* HUF_compress4X_repeat(): 75919fcbaf1SConrad Meyer * compress input using 4 streams. 76019fcbaf1SConrad Meyer * provide workspace to generate compression tables */ 7610c16b537SWarner Losh size_t HUF_compress4X_wksp (void* dst, size_t dstSize, 7620c16b537SWarner Losh const void* src, size_t srcSize, 7630c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog, 7640c16b537SWarner Losh void* workSpace, size_t wkspSize) 7650c16b537SWarner Losh { 76619fcbaf1SConrad Meyer return HUF_compress_internal(dst, dstSize, src, srcSize, 767a0483764SConrad Meyer maxSymbolValue, huffLog, HUF_fourStreams, 76819fcbaf1SConrad Meyer workSpace, wkspSize, 76919fcbaf1SConrad Meyer NULL, NULL, 0, 0 /*bmi2*/); 7700c16b537SWarner Losh } 7710c16b537SWarner Losh 77219fcbaf1SConrad Meyer /* HUF_compress4X_repeat(): 77319fcbaf1SConrad Meyer * compress input using 4 streams. 77419fcbaf1SConrad Meyer * re-use an existing huffman compression table */ 7750c16b537SWarner Losh size_t HUF_compress4X_repeat (void* dst, size_t dstSize, 7760c16b537SWarner Losh const void* src, size_t srcSize, 7770c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog, 7780c16b537SWarner Losh void* workSpace, size_t wkspSize, 77919fcbaf1SConrad Meyer HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2) 7800c16b537SWarner Losh { 78119fcbaf1SConrad Meyer return HUF_compress_internal(dst, dstSize, src, srcSize, 782a0483764SConrad Meyer maxSymbolValue, huffLog, HUF_fourStreams, 78319fcbaf1SConrad Meyer workSpace, wkspSize, 78419fcbaf1SConrad Meyer hufTable, repeat, preferRepeat, bmi2); 7850c16b537SWarner Losh } 7860c16b537SWarner Losh 7870c16b537SWarner Losh size_t HUF_compress2 (void* dst, size_t dstSize, 7880c16b537SWarner Losh const void* src, size_t srcSize, 7890c16b537SWarner Losh unsigned maxSymbolValue, unsigned huffLog) 7900c16b537SWarner Losh { 79119fcbaf1SConrad Meyer unsigned workSpace[HUF_WORKSPACE_SIZE_U32]; 7920c16b537SWarner Losh return HUF_compress4X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace)); 7930c16b537SWarner Losh } 7940c16b537SWarner Losh 7950c16b537SWarner Losh size_t HUF_compress (void* dst, size_t maxDstSize, const void* src, size_t srcSize) 7960c16b537SWarner Losh { 79719fcbaf1SConrad Meyer return HUF_compress2(dst, maxDstSize, src, srcSize, 255, HUF_TABLELOG_DEFAULT); 7980c16b537SWarner Losh } 799