xref: /freebsd/sys/contrib/zstd/lib/compress/fse_compress.c (revision 37f1f2684f2670b204080ef2d6c303becd28545f)
10c16b537SWarner Losh /* ******************************************************************
2*37f1f268SConrad Meyer  * FSE : Finite State Entropy encoder
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 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 *  Includes
170c16b537SWarner Losh ****************************************************************/
180c16b537SWarner Losh #include <stdlib.h>     /* malloc, free, qsort */
190c16b537SWarner Losh #include <string.h>     /* memcpy, memset */
20*37f1f268SConrad Meyer #include "../common/compiler.h"
21*37f1f268SConrad Meyer #include "../common/mem.h"        /* U32, U16, etc. */
22*37f1f268SConrad Meyer #include "../common/debug.h"      /* assert, DEBUGLOG */
230f743729SConrad Meyer #include "hist.h"       /* HIST_count_wksp */
24*37f1f268SConrad Meyer #include "../common/bitstream.h"
250c16b537SWarner Losh #define FSE_STATIC_LINKING_ONLY
26*37f1f268SConrad Meyer #include "../common/fse.h"
27*37f1f268SConrad Meyer #include "../common/error_private.h"
280c16b537SWarner Losh 
290c16b537SWarner Losh 
300c16b537SWarner Losh /* **************************************************************
310c16b537SWarner Losh *  Error Management
320c16b537SWarner Losh ****************************************************************/
330c16b537SWarner Losh #define FSE_isError ERR_isError
340c16b537SWarner Losh 
350c16b537SWarner Losh 
360c16b537SWarner Losh /* **************************************************************
370c16b537SWarner Losh *  Templates
380c16b537SWarner Losh ****************************************************************/
390c16b537SWarner Losh /*
400c16b537SWarner Losh   designed to be included
410c16b537SWarner Losh   for type-specific functions (template emulation in C)
420c16b537SWarner Losh   Objective is to write these functions only once, for improved maintenance
430c16b537SWarner Losh */
440c16b537SWarner Losh 
450c16b537SWarner Losh /* safety checks */
460c16b537SWarner Losh #ifndef FSE_FUNCTION_EXTENSION
470c16b537SWarner Losh #  error "FSE_FUNCTION_EXTENSION must be defined"
480c16b537SWarner Losh #endif
490c16b537SWarner Losh #ifndef FSE_FUNCTION_TYPE
500c16b537SWarner Losh #  error "FSE_FUNCTION_TYPE must be defined"
510c16b537SWarner Losh #endif
520c16b537SWarner Losh 
530c16b537SWarner Losh /* Function names */
540c16b537SWarner Losh #define FSE_CAT(X,Y) X##Y
550c16b537SWarner Losh #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
560c16b537SWarner Losh #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
570c16b537SWarner Losh 
580c16b537SWarner Losh 
590c16b537SWarner Losh /* Function templates */
600c16b537SWarner Losh 
610c16b537SWarner Losh /* FSE_buildCTable_wksp() :
620c16b537SWarner Losh  * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
630c16b537SWarner Losh  * wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)`
640c16b537SWarner Losh  * workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements
650c16b537SWarner Losh  */
660f743729SConrad Meyer size_t FSE_buildCTable_wksp(FSE_CTable* ct,
670f743729SConrad Meyer                       const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
680f743729SConrad Meyer                             void* workSpace, size_t wkspSize)
690c16b537SWarner Losh {
700c16b537SWarner Losh     U32 const tableSize = 1 << tableLog;
710c16b537SWarner Losh     U32 const tableMask = tableSize - 1;
720c16b537SWarner Losh     void* const ptr = ct;
730c16b537SWarner Losh     U16* const tableU16 = ( (U16*) ptr) + 2;
740c16b537SWarner Losh     void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ;
750c16b537SWarner Losh     FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
760c16b537SWarner Losh     U32 const step = FSE_TABLESTEP(tableSize);
770c16b537SWarner Losh     U32 cumul[FSE_MAX_SYMBOL_VALUE+2];
780c16b537SWarner Losh 
790c16b537SWarner Losh     FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)workSpace;
800c16b537SWarner Losh     U32 highThreshold = tableSize-1;
810c16b537SWarner Losh 
820c16b537SWarner Losh     /* CTable header */
830c16b537SWarner Losh     if (((size_t)1 << tableLog) * sizeof(FSE_FUNCTION_TYPE) > wkspSize) return ERROR(tableLog_tooLarge);
840c16b537SWarner Losh     tableU16[-2] = (U16) tableLog;
850c16b537SWarner Losh     tableU16[-1] = (U16) maxSymbolValue;
860f743729SConrad Meyer     assert(tableLog < 16);   /* required for threshold strategy to work */
870c16b537SWarner Losh 
880c16b537SWarner Losh     /* For explanations on how to distribute symbol values over the table :
890c16b537SWarner Losh      * http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
900c16b537SWarner Losh 
910f743729SConrad Meyer      #ifdef __clang_analyzer__
920f743729SConrad Meyer      memset(tableSymbol, 0, sizeof(*tableSymbol) * tableSize);   /* useless initialization, just to keep scan-build happy */
930f743729SConrad Meyer      #endif
940f743729SConrad Meyer 
950c16b537SWarner Losh     /* symbol start positions */
960c16b537SWarner Losh     {   U32 u;
970c16b537SWarner Losh         cumul[0] = 0;
980c16b537SWarner Losh         for (u=1; u <= maxSymbolValue+1; u++) {
990c16b537SWarner Losh             if (normalizedCounter[u-1]==-1) {  /* Low proba symbol */
1000c16b537SWarner Losh                 cumul[u] = cumul[u-1] + 1;
1010c16b537SWarner Losh                 tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);
1020c16b537SWarner Losh             } else {
1030c16b537SWarner Losh                 cumul[u] = cumul[u-1] + normalizedCounter[u-1];
1040c16b537SWarner Losh         }   }
1050c16b537SWarner Losh         cumul[maxSymbolValue+1] = tableSize+1;
1060c16b537SWarner Losh     }
1070c16b537SWarner Losh 
1080c16b537SWarner Losh     /* Spread symbols */
1090c16b537SWarner Losh     {   U32 position = 0;
1100c16b537SWarner Losh         U32 symbol;
1110c16b537SWarner Losh         for (symbol=0; symbol<=maxSymbolValue; symbol++) {
1122b9c00cbSConrad Meyer             int nbOccurrences;
1130f743729SConrad Meyer             int const freq = normalizedCounter[symbol];
1142b9c00cbSConrad Meyer             for (nbOccurrences=0; nbOccurrences<freq; nbOccurrences++) {
1150c16b537SWarner Losh                 tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
1160c16b537SWarner Losh                 position = (position + step) & tableMask;
1170f743729SConrad Meyer                 while (position > highThreshold)
1180f743729SConrad Meyer                     position = (position + step) & tableMask;   /* Low proba area */
1190c16b537SWarner Losh         }   }
1200c16b537SWarner Losh 
1210f743729SConrad Meyer         assert(position==0);  /* Must have initialized all positions */
1220c16b537SWarner Losh     }
1230c16b537SWarner Losh 
1240c16b537SWarner Losh     /* Build table */
1250c16b537SWarner Losh     {   U32 u; for (u=0; u<tableSize; u++) {
1260c16b537SWarner Losh         FSE_FUNCTION_TYPE s = tableSymbol[u];   /* note : static analyzer may not understand tableSymbol is properly initialized */
1270c16b537SWarner Losh         tableU16[cumul[s]++] = (U16) (tableSize+u);   /* TableU16 : sorted by symbol order; gives next state value */
1280c16b537SWarner Losh     }   }
1290c16b537SWarner Losh 
1300c16b537SWarner Losh     /* Build Symbol Transformation Table */
1310c16b537SWarner Losh     {   unsigned total = 0;
1320c16b537SWarner Losh         unsigned s;
1330c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++) {
1340c16b537SWarner Losh             switch (normalizedCounter[s])
1350c16b537SWarner Losh             {
1360f743729SConrad Meyer             case  0:
1370f743729SConrad Meyer                 /* filling nonetheless, for compatibility with FSE_getMaxNbBits() */
1380f743729SConrad Meyer                 symbolTT[s].deltaNbBits = ((tableLog+1) << 16) - (1<<tableLog);
1390f743729SConrad Meyer                 break;
1400c16b537SWarner Losh 
1410c16b537SWarner Losh             case -1:
1420c16b537SWarner Losh             case  1:
1430c16b537SWarner Losh                 symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);
1440c16b537SWarner Losh                 symbolTT[s].deltaFindState = total - 1;
1450c16b537SWarner Losh                 total ++;
1460c16b537SWarner Losh                 break;
1470c16b537SWarner Losh             default :
1480c16b537SWarner Losh                 {
1490c16b537SWarner Losh                     U32 const maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1);
1500c16b537SWarner Losh                     U32 const minStatePlus = normalizedCounter[s] << maxBitsOut;
1510c16b537SWarner Losh                     symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
1520c16b537SWarner Losh                     symbolTT[s].deltaFindState = total - normalizedCounter[s];
1530c16b537SWarner Losh                     total +=  normalizedCounter[s];
1540c16b537SWarner Losh     }   }   }   }
1550c16b537SWarner Losh 
1560f743729SConrad Meyer #if 0  /* debug : symbol costs */
1570f743729SConrad Meyer     DEBUGLOG(5, "\n --- table statistics : ");
1580f743729SConrad Meyer     {   U32 symbol;
1590f743729SConrad Meyer         for (symbol=0; symbol<=maxSymbolValue; symbol++) {
1600f743729SConrad Meyer             DEBUGLOG(5, "%3u: w=%3i,   maxBits=%u, fracBits=%.2f",
1610f743729SConrad Meyer                 symbol, normalizedCounter[symbol],
1620f743729SConrad Meyer                 FSE_getMaxNbBits(symbolTT, symbol),
1630f743729SConrad Meyer                 (double)FSE_bitCost(symbolTT, tableLog, symbol, 8) / 256);
1640f743729SConrad Meyer         }
1650f743729SConrad Meyer     }
1660f743729SConrad Meyer #endif
1670f743729SConrad Meyer 
1680c16b537SWarner Losh     return 0;
1690c16b537SWarner Losh }
1700c16b537SWarner Losh 
1710c16b537SWarner Losh 
1720c16b537SWarner Losh size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1730c16b537SWarner Losh {
1740c16b537SWarner Losh     FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE];   /* memset() is not necessary, even if static analyzer complain about it */
1750c16b537SWarner Losh     return FSE_buildCTable_wksp(ct, normalizedCounter, maxSymbolValue, tableLog, tableSymbol, sizeof(tableSymbol));
1760c16b537SWarner Losh }
1770c16b537SWarner Losh 
1780c16b537SWarner Losh 
1790c16b537SWarner Losh 
1800c16b537SWarner Losh #ifndef FSE_COMMONDEFS_ONLY
1810c16b537SWarner Losh 
1820f743729SConrad Meyer 
1830c16b537SWarner Losh /*-**************************************************************
1840f743729SConrad Meyer *  FSE NCount encoding
1850c16b537SWarner Losh ****************************************************************/
1860c16b537SWarner Losh size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
1870c16b537SWarner Losh {
1880c16b537SWarner Losh     size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 3;
1890c16b537SWarner Losh     return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND;  /* maxSymbolValue==0 ? use default */
1900c16b537SWarner Losh }
1910c16b537SWarner Losh 
1920f743729SConrad Meyer static size_t
1930f743729SConrad Meyer FSE_writeNCount_generic (void* header, size_t headerBufferSize,
1940c16b537SWarner Losh                    const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
1950c16b537SWarner Losh                          unsigned writeIsSafe)
1960c16b537SWarner Losh {
1970c16b537SWarner Losh     BYTE* const ostart = (BYTE*) header;
1980c16b537SWarner Losh     BYTE* out = ostart;
1990c16b537SWarner Losh     BYTE* const oend = ostart + headerBufferSize;
2000c16b537SWarner Losh     int nbBits;
2010c16b537SWarner Losh     const int tableSize = 1 << tableLog;
2020c16b537SWarner Losh     int remaining;
2030c16b537SWarner Losh     int threshold;
2040f743729SConrad Meyer     U32 bitStream = 0;
2050f743729SConrad Meyer     int bitCount = 0;
2060f743729SConrad Meyer     unsigned symbol = 0;
2070f743729SConrad Meyer     unsigned const alphabetSize = maxSymbolValue + 1;
2080f743729SConrad Meyer     int previousIs0 = 0;
2090c16b537SWarner Losh 
2100c16b537SWarner Losh     /* Table Size */
2110c16b537SWarner Losh     bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
2120c16b537SWarner Losh     bitCount  += 4;
2130c16b537SWarner Losh 
2140c16b537SWarner Losh     /* Init */
2150c16b537SWarner Losh     remaining = tableSize+1;   /* +1 for extra accuracy */
2160c16b537SWarner Losh     threshold = tableSize;
2170c16b537SWarner Losh     nbBits = tableLog+1;
2180c16b537SWarner Losh 
2190f743729SConrad Meyer     while ((symbol < alphabetSize) && (remaining>1)) {  /* stops at 1 */
2200f743729SConrad Meyer         if (previousIs0) {
2210f743729SConrad Meyer             unsigned start = symbol;
2220f743729SConrad Meyer             while ((symbol < alphabetSize) && !normalizedCounter[symbol]) symbol++;
2230f743729SConrad Meyer             if (symbol == alphabetSize) break;   /* incorrect distribution */
2240f743729SConrad Meyer             while (symbol >= start+24) {
2250c16b537SWarner Losh                 start+=24;
2260c16b537SWarner Losh                 bitStream += 0xFFFFU << bitCount;
2270f743729SConrad Meyer                 if ((!writeIsSafe) && (out > oend-2))
2280f743729SConrad Meyer                     return ERROR(dstSize_tooSmall);   /* Buffer overflow */
2290c16b537SWarner Losh                 out[0] = (BYTE) bitStream;
2300c16b537SWarner Losh                 out[1] = (BYTE)(bitStream>>8);
2310c16b537SWarner Losh                 out+=2;
2320c16b537SWarner Losh                 bitStream>>=16;
2330c16b537SWarner Losh             }
2340f743729SConrad Meyer             while (symbol >= start+3) {
2350c16b537SWarner Losh                 start+=3;
2360c16b537SWarner Losh                 bitStream += 3 << bitCount;
2370c16b537SWarner Losh                 bitCount += 2;
2380c16b537SWarner Losh             }
2390f743729SConrad Meyer             bitStream += (symbol-start) << bitCount;
2400c16b537SWarner Losh             bitCount += 2;
2410c16b537SWarner Losh             if (bitCount>16) {
2420f743729SConrad Meyer                 if ((!writeIsSafe) && (out > oend - 2))
2430f743729SConrad Meyer                     return ERROR(dstSize_tooSmall);   /* Buffer overflow */
2440c16b537SWarner Losh                 out[0] = (BYTE)bitStream;
2450c16b537SWarner Losh                 out[1] = (BYTE)(bitStream>>8);
2460c16b537SWarner Losh                 out += 2;
2470c16b537SWarner Losh                 bitStream >>= 16;
2480c16b537SWarner Losh                 bitCount -= 16;
2490c16b537SWarner Losh         }   }
2500f743729SConrad Meyer         {   int count = normalizedCounter[symbol++];
2510c16b537SWarner Losh             int const max = (2*threshold-1) - remaining;
2520c16b537SWarner Losh             remaining -= count < 0 ? -count : count;
2530c16b537SWarner Losh             count++;   /* +1 for extra accuracy */
2540f743729SConrad Meyer             if (count>=threshold)
2550f743729SConrad Meyer                 count += max;   /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
2560c16b537SWarner Losh             bitStream += count << bitCount;
2570c16b537SWarner Losh             bitCount  += nbBits;
2580c16b537SWarner Losh             bitCount  -= (count<max);
2590f743729SConrad Meyer             previousIs0  = (count==1);
2600c16b537SWarner Losh             if (remaining<1) return ERROR(GENERIC);
26119fcbaf1SConrad Meyer             while (remaining<threshold) { nbBits--; threshold>>=1; }
2620c16b537SWarner Losh         }
2630c16b537SWarner Losh         if (bitCount>16) {
2640f743729SConrad Meyer             if ((!writeIsSafe) && (out > oend - 2))
2650f743729SConrad Meyer                 return ERROR(dstSize_tooSmall);   /* Buffer overflow */
2660c16b537SWarner Losh             out[0] = (BYTE)bitStream;
2670c16b537SWarner Losh             out[1] = (BYTE)(bitStream>>8);
2680c16b537SWarner Losh             out += 2;
2690c16b537SWarner Losh             bitStream >>= 16;
2700c16b537SWarner Losh             bitCount -= 16;
2710c16b537SWarner Losh     }   }
2720c16b537SWarner Losh 
2730f743729SConrad Meyer     if (remaining != 1)
2740f743729SConrad Meyer         return ERROR(GENERIC);  /* incorrect normalized distribution */
2750f743729SConrad Meyer     assert(symbol <= alphabetSize);
2760f743729SConrad Meyer 
2770c16b537SWarner Losh     /* flush remaining bitStream */
2780f743729SConrad Meyer     if ((!writeIsSafe) && (out > oend - 2))
2790f743729SConrad Meyer         return ERROR(dstSize_tooSmall);   /* Buffer overflow */
2800c16b537SWarner Losh     out[0] = (BYTE)bitStream;
2810c16b537SWarner Losh     out[1] = (BYTE)(bitStream>>8);
2820c16b537SWarner Losh     out+= (bitCount+7) /8;
2830c16b537SWarner Losh 
2840c16b537SWarner Losh     return (out-ostart);
2850c16b537SWarner Losh }
2860c16b537SWarner Losh 
2870c16b537SWarner Losh 
2880f743729SConrad Meyer size_t FSE_writeNCount (void* buffer, size_t bufferSize,
2890f743729SConrad Meyer                   const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
2900c16b537SWarner Losh {
2910c16b537SWarner Losh     if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported */
2920c16b537SWarner Losh     if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported */
2930c16b537SWarner Losh 
2940c16b537SWarner Losh     if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))
2950c16b537SWarner Losh         return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
2960c16b537SWarner Losh 
2970f743729SConrad Meyer     return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1 /* write in buffer is safe */);
2980c16b537SWarner Losh }
2990c16b537SWarner Losh 
3000c16b537SWarner Losh 
3010c16b537SWarner Losh /*-**************************************************************
3020c16b537SWarner Losh *  FSE Compression Code
3030c16b537SWarner Losh ****************************************************************/
3040c16b537SWarner Losh 
3050c16b537SWarner Losh FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
3060c16b537SWarner Losh {
3070c16b537SWarner Losh     size_t size;
3080c16b537SWarner Losh     if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
3090c16b537SWarner Losh     size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
3100c16b537SWarner Losh     return (FSE_CTable*)malloc(size);
3110c16b537SWarner Losh }
3120c16b537SWarner Losh 
3130c16b537SWarner Losh void FSE_freeCTable (FSE_CTable* ct) { free(ct); }
3140c16b537SWarner Losh 
3150c16b537SWarner Losh /* provides the minimum logSize to safely represent a distribution */
3160c16b537SWarner Losh static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
3170c16b537SWarner Losh {
3180f743729SConrad Meyer     U32 minBitsSrc = BIT_highbit32((U32)(srcSize)) + 1;
3190c16b537SWarner Losh     U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2;
3200c16b537SWarner Losh     U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;
3210c16b537SWarner Losh     assert(srcSize > 1); /* Not supported, RLE should be used instead */
3220c16b537SWarner Losh     return minBits;
3230c16b537SWarner Losh }
3240c16b537SWarner Losh 
3250c16b537SWarner Losh unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
3260c16b537SWarner Losh {
3270c16b537SWarner Losh     U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus;
3280c16b537SWarner Losh     U32 tableLog = maxTableLog;
3290c16b537SWarner Losh     U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);
3300c16b537SWarner Losh     assert(srcSize > 1); /* Not supported, RLE should be used instead */
3310c16b537SWarner Losh     if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
3320c16b537SWarner Losh     if (maxBitsSrc < tableLog) tableLog = maxBitsSrc;   /* Accuracy can be reduced */
3330c16b537SWarner Losh     if (minBits > tableLog) tableLog = minBits;   /* Need a minimum to safely represent all symbol values */
3340c16b537SWarner Losh     if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
3350c16b537SWarner Losh     if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
3360c16b537SWarner Losh     return tableLog;
3370c16b537SWarner Losh }
3380c16b537SWarner Losh 
3390c16b537SWarner Losh unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
3400c16b537SWarner Losh {
3410c16b537SWarner Losh     return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);
3420c16b537SWarner Losh }
3430c16b537SWarner Losh 
3440c16b537SWarner Losh 
3450c16b537SWarner Losh /* Secondary normalization method.
3460c16b537SWarner Losh    To be used when primary method fails. */
3470c16b537SWarner Losh 
3480c16b537SWarner Losh static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue)
3490c16b537SWarner Losh {
3500c16b537SWarner Losh     short const NOT_YET_ASSIGNED = -2;
3510c16b537SWarner Losh     U32 s;
3520c16b537SWarner Losh     U32 distributed = 0;
3530c16b537SWarner Losh     U32 ToDistribute;
3540c16b537SWarner Losh 
3550c16b537SWarner Losh     /* Init */
3560c16b537SWarner Losh     U32 const lowThreshold = (U32)(total >> tableLog);
3570c16b537SWarner Losh     U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
3580c16b537SWarner Losh 
3590c16b537SWarner Losh     for (s=0; s<=maxSymbolValue; s++) {
3600c16b537SWarner Losh         if (count[s] == 0) {
3610c16b537SWarner Losh             norm[s]=0;
3620c16b537SWarner Losh             continue;
3630c16b537SWarner Losh         }
3640c16b537SWarner Losh         if (count[s] <= lowThreshold) {
3650c16b537SWarner Losh             norm[s] = -1;
3660c16b537SWarner Losh             distributed++;
3670c16b537SWarner Losh             total -= count[s];
3680c16b537SWarner Losh             continue;
3690c16b537SWarner Losh         }
3700c16b537SWarner Losh         if (count[s] <= lowOne) {
3710c16b537SWarner Losh             norm[s] = 1;
3720c16b537SWarner Losh             distributed++;
3730c16b537SWarner Losh             total -= count[s];
3740c16b537SWarner Losh             continue;
3750c16b537SWarner Losh         }
3760c16b537SWarner Losh 
3770c16b537SWarner Losh         norm[s]=NOT_YET_ASSIGNED;
3780c16b537SWarner Losh     }
3790c16b537SWarner Losh     ToDistribute = (1 << tableLog) - distributed;
3800c16b537SWarner Losh 
3810f743729SConrad Meyer     if (ToDistribute == 0)
3820f743729SConrad Meyer         return 0;
3830f743729SConrad Meyer 
3840c16b537SWarner Losh     if ((total / ToDistribute) > lowOne) {
3850c16b537SWarner Losh         /* risk of rounding to zero */
3860c16b537SWarner Losh         lowOne = (U32)((total * 3) / (ToDistribute * 2));
3870c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++) {
3880c16b537SWarner Losh             if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {
3890c16b537SWarner Losh                 norm[s] = 1;
3900c16b537SWarner Losh                 distributed++;
3910c16b537SWarner Losh                 total -= count[s];
3920c16b537SWarner Losh                 continue;
3930c16b537SWarner Losh         }   }
3940c16b537SWarner Losh         ToDistribute = (1 << tableLog) - distributed;
3950c16b537SWarner Losh     }
3960c16b537SWarner Losh 
3970c16b537SWarner Losh     if (distributed == maxSymbolValue+1) {
3980c16b537SWarner Losh         /* all values are pretty poor;
3990c16b537SWarner Losh            probably incompressible data (should have already been detected);
4000c16b537SWarner Losh            find max, then give all remaining points to max */
4010c16b537SWarner Losh         U32 maxV = 0, maxC = 0;
4020c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++)
40319fcbaf1SConrad Meyer             if (count[s] > maxC) { maxV=s; maxC=count[s]; }
4040c16b537SWarner Losh         norm[maxV] += (short)ToDistribute;
4050c16b537SWarner Losh         return 0;
4060c16b537SWarner Losh     }
4070c16b537SWarner Losh 
4080c16b537SWarner Losh     if (total == 0) {
4090c16b537SWarner Losh         /* all of the symbols were low enough for the lowOne or lowThreshold */
4100c16b537SWarner Losh         for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1))
41119fcbaf1SConrad Meyer             if (norm[s] > 0) { ToDistribute--; norm[s]++; }
4120c16b537SWarner Losh         return 0;
4130c16b537SWarner Losh     }
4140c16b537SWarner Losh 
4150c16b537SWarner Losh     {   U64 const vStepLog = 62 - tableLog;
4160c16b537SWarner Losh         U64 const mid = (1ULL << (vStepLog-1)) - 1;
4170c16b537SWarner Losh         U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total;   /* scale on remaining */
4180c16b537SWarner Losh         U64 tmpTotal = mid;
4190c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++) {
4200c16b537SWarner Losh             if (norm[s]==NOT_YET_ASSIGNED) {
4210c16b537SWarner Losh                 U64 const end = tmpTotal + (count[s] * rStep);
4220c16b537SWarner Losh                 U32 const sStart = (U32)(tmpTotal >> vStepLog);
4230c16b537SWarner Losh                 U32 const sEnd = (U32)(end >> vStepLog);
4240c16b537SWarner Losh                 U32 const weight = sEnd - sStart;
4250c16b537SWarner Losh                 if (weight < 1)
4260c16b537SWarner Losh                     return ERROR(GENERIC);
4270c16b537SWarner Losh                 norm[s] = (short)weight;
4280c16b537SWarner Losh                 tmpTotal = end;
4290c16b537SWarner Losh     }   }   }
4300c16b537SWarner Losh 
4310c16b537SWarner Losh     return 0;
4320c16b537SWarner Losh }
4330c16b537SWarner Losh 
4340c16b537SWarner Losh 
4350c16b537SWarner Losh size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
4360c16b537SWarner Losh                            const unsigned* count, size_t total,
4370c16b537SWarner Losh                            unsigned maxSymbolValue)
4380c16b537SWarner Losh {
4390c16b537SWarner Losh     /* Sanity checks */
4400c16b537SWarner Losh     if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
4410c16b537SWarner Losh     if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported size */
4420c16b537SWarner Losh     if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported size */
4430c16b537SWarner Losh     if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC);   /* Too small tableLog, compression potentially impossible */
4440c16b537SWarner Losh 
4450c16b537SWarner Losh     {   static U32 const rtbTable[] = {     0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
4460c16b537SWarner Losh         U64 const scale = 62 - tableLog;
4470c16b537SWarner Losh         U64 const step = ((U64)1<<62) / total;   /* <== here, one division ! */
4480c16b537SWarner Losh         U64 const vStep = 1ULL<<(scale-20);
4490c16b537SWarner Losh         int stillToDistribute = 1<<tableLog;
4500c16b537SWarner Losh         unsigned s;
4510c16b537SWarner Losh         unsigned largest=0;
4520c16b537SWarner Losh         short largestP=0;
4530c16b537SWarner Losh         U32 lowThreshold = (U32)(total >> tableLog);
4540c16b537SWarner Losh 
4550c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++) {
4560c16b537SWarner Losh             if (count[s] == total) return 0;   /* rle special case */
4570c16b537SWarner Losh             if (count[s] == 0) { normalizedCounter[s]=0; continue; }
4580c16b537SWarner Losh             if (count[s] <= lowThreshold) {
4590c16b537SWarner Losh                 normalizedCounter[s] = -1;
4600c16b537SWarner Losh                 stillToDistribute--;
4610c16b537SWarner Losh             } else {
4620c16b537SWarner Losh                 short proba = (short)((count[s]*step) >> scale);
4630c16b537SWarner Losh                 if (proba<8) {
4640c16b537SWarner Losh                     U64 restToBeat = vStep * rtbTable[proba];
4650c16b537SWarner Losh                     proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
4660c16b537SWarner Losh                 }
46719fcbaf1SConrad Meyer                 if (proba > largestP) { largestP=proba; largest=s; }
4680c16b537SWarner Losh                 normalizedCounter[s] = proba;
4690c16b537SWarner Losh                 stillToDistribute -= proba;
4700c16b537SWarner Losh         }   }
4710c16b537SWarner Losh         if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
4720c16b537SWarner Losh             /* corner case, need another normalization method */
4730c16b537SWarner Losh             size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
4740c16b537SWarner Losh             if (FSE_isError(errorCode)) return errorCode;
4750c16b537SWarner Losh         }
4760c16b537SWarner Losh         else normalizedCounter[largest] += (short)stillToDistribute;
4770c16b537SWarner Losh     }
4780c16b537SWarner Losh 
4790c16b537SWarner Losh #if 0
4800c16b537SWarner Losh     {   /* Print Table (debug) */
4810c16b537SWarner Losh         U32 s;
4820c16b537SWarner Losh         U32 nTotal = 0;
4830c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++)
4840f743729SConrad Meyer             RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]);
4850c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++)
4860c16b537SWarner Losh             nTotal += abs(normalizedCounter[s]);
4870c16b537SWarner Losh         if (nTotal != (1U<<tableLog))
4880f743729SConrad Meyer             RAWLOG(2, "Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
4890c16b537SWarner Losh         getchar();
4900c16b537SWarner Losh     }
4910c16b537SWarner Losh #endif
4920c16b537SWarner Losh 
4930c16b537SWarner Losh     return tableLog;
4940c16b537SWarner Losh }
4950c16b537SWarner Losh 
4960c16b537SWarner Losh 
4970c16b537SWarner Losh /* fake FSE_CTable, for raw (uncompressed) input */
4980c16b537SWarner Losh size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits)
4990c16b537SWarner Losh {
5000c16b537SWarner Losh     const unsigned tableSize = 1 << nbBits;
5010c16b537SWarner Losh     const unsigned tableMask = tableSize - 1;
5020c16b537SWarner Losh     const unsigned maxSymbolValue = tableMask;
5030c16b537SWarner Losh     void* const ptr = ct;
5040c16b537SWarner Losh     U16* const tableU16 = ( (U16*) ptr) + 2;
5050c16b537SWarner Losh     void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableSize>>1);   /* assumption : tableLog >= 1 */
5060c16b537SWarner Losh     FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
5070c16b537SWarner Losh     unsigned s;
5080c16b537SWarner Losh 
5090c16b537SWarner Losh     /* Sanity checks */
5100c16b537SWarner Losh     if (nbBits < 1) return ERROR(GENERIC);             /* min size */
5110c16b537SWarner Losh 
5120c16b537SWarner Losh     /* header */
5130c16b537SWarner Losh     tableU16[-2] = (U16) nbBits;
5140c16b537SWarner Losh     tableU16[-1] = (U16) maxSymbolValue;
5150c16b537SWarner Losh 
5160c16b537SWarner Losh     /* Build table */
5170c16b537SWarner Losh     for (s=0; s<tableSize; s++)
5180c16b537SWarner Losh         tableU16[s] = (U16)(tableSize + s);
5190c16b537SWarner Losh 
5200c16b537SWarner Losh     /* Build Symbol Transformation Table */
5210c16b537SWarner Losh     {   const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
5220c16b537SWarner Losh         for (s=0; s<=maxSymbolValue; s++) {
5230c16b537SWarner Losh             symbolTT[s].deltaNbBits = deltaNbBits;
5240c16b537SWarner Losh             symbolTT[s].deltaFindState = s-1;
5250c16b537SWarner Losh     }   }
5260c16b537SWarner Losh 
5270c16b537SWarner Losh     return 0;
5280c16b537SWarner Losh }
5290c16b537SWarner Losh 
5300c16b537SWarner Losh /* fake FSE_CTable, for rle input (always same symbol) */
5310c16b537SWarner Losh size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue)
5320c16b537SWarner Losh {
5330c16b537SWarner Losh     void* ptr = ct;
5340c16b537SWarner Losh     U16* tableU16 = ( (U16*) ptr) + 2;
5350c16b537SWarner Losh     void* FSCTptr = (U32*)ptr + 2;
5360c16b537SWarner Losh     FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr;
5370c16b537SWarner Losh 
5380c16b537SWarner Losh     /* header */
5390c16b537SWarner Losh     tableU16[-2] = (U16) 0;
5400c16b537SWarner Losh     tableU16[-1] = (U16) symbolValue;
5410c16b537SWarner Losh 
5420c16b537SWarner Losh     /* Build table */
5430c16b537SWarner Losh     tableU16[0] = 0;
5440c16b537SWarner Losh     tableU16[1] = 0;   /* just in case */
5450c16b537SWarner Losh 
5460c16b537SWarner Losh     /* Build Symbol Transformation Table */
5470c16b537SWarner Losh     symbolTT[symbolValue].deltaNbBits = 0;
5480c16b537SWarner Losh     symbolTT[symbolValue].deltaFindState = 0;
5490c16b537SWarner Losh 
5500c16b537SWarner Losh     return 0;
5510c16b537SWarner Losh }
5520c16b537SWarner Losh 
5530c16b537SWarner Losh 
5540c16b537SWarner Losh static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
5550c16b537SWarner Losh                            const void* src, size_t srcSize,
5560c16b537SWarner Losh                            const FSE_CTable* ct, const unsigned fast)
5570c16b537SWarner Losh {
5580c16b537SWarner Losh     const BYTE* const istart = (const BYTE*) src;
5590c16b537SWarner Losh     const BYTE* const iend = istart + srcSize;
5600c16b537SWarner Losh     const BYTE* ip=iend;
5610c16b537SWarner Losh 
5620c16b537SWarner Losh     BIT_CStream_t bitC;
5630c16b537SWarner Losh     FSE_CState_t CState1, CState2;
5640c16b537SWarner Losh 
5650c16b537SWarner Losh     /* init */
5660c16b537SWarner Losh     if (srcSize <= 2) return 0;
5670c16b537SWarner Losh     { size_t const initError = BIT_initCStream(&bitC, dst, dstSize);
5680c16b537SWarner Losh       if (FSE_isError(initError)) return 0; /* not enough space available to write a bitstream */ }
5690c16b537SWarner Losh 
5700c16b537SWarner Losh #define FSE_FLUSHBITS(s)  (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
5710c16b537SWarner Losh 
5720c16b537SWarner Losh     if (srcSize & 1) {
5730c16b537SWarner Losh         FSE_initCState2(&CState1, ct, *--ip);
5740c16b537SWarner Losh         FSE_initCState2(&CState2, ct, *--ip);
5750c16b537SWarner Losh         FSE_encodeSymbol(&bitC, &CState1, *--ip);
5760c16b537SWarner Losh         FSE_FLUSHBITS(&bitC);
5770c16b537SWarner Losh     } else {
5780c16b537SWarner Losh         FSE_initCState2(&CState2, ct, *--ip);
5790c16b537SWarner Losh         FSE_initCState2(&CState1, ct, *--ip);
5800c16b537SWarner Losh     }
5810c16b537SWarner Losh 
5820c16b537SWarner Losh     /* join to mod 4 */
5830c16b537SWarner Losh     srcSize -= 2;
5840c16b537SWarner Losh     if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) {  /* test bit 2 */
5850c16b537SWarner Losh         FSE_encodeSymbol(&bitC, &CState2, *--ip);
5860c16b537SWarner Losh         FSE_encodeSymbol(&bitC, &CState1, *--ip);
5870c16b537SWarner Losh         FSE_FLUSHBITS(&bitC);
5880c16b537SWarner Losh     }
5890c16b537SWarner Losh 
5900c16b537SWarner Losh     /* 2 or 4 encoding per loop */
5910c16b537SWarner Losh     while ( ip>istart ) {
5920c16b537SWarner Losh 
5930c16b537SWarner Losh         FSE_encodeSymbol(&bitC, &CState2, *--ip);
5940c16b537SWarner Losh 
5950c16b537SWarner Losh         if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 )   /* this test must be static */
5960c16b537SWarner Losh             FSE_FLUSHBITS(&bitC);
5970c16b537SWarner Losh 
5980c16b537SWarner Losh         FSE_encodeSymbol(&bitC, &CState1, *--ip);
5990c16b537SWarner Losh 
6000c16b537SWarner Losh         if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) {  /* this test must be static */
6010c16b537SWarner Losh             FSE_encodeSymbol(&bitC, &CState2, *--ip);
6020c16b537SWarner Losh             FSE_encodeSymbol(&bitC, &CState1, *--ip);
6030c16b537SWarner Losh         }
6040c16b537SWarner Losh 
6050c16b537SWarner Losh         FSE_FLUSHBITS(&bitC);
6060c16b537SWarner Losh     }
6070c16b537SWarner Losh 
6080c16b537SWarner Losh     FSE_flushCState(&bitC, &CState2);
6090c16b537SWarner Losh     FSE_flushCState(&bitC, &CState1);
6100c16b537SWarner Losh     return BIT_closeCStream(&bitC);
6110c16b537SWarner Losh }
6120c16b537SWarner Losh 
6130c16b537SWarner Losh size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
6140c16b537SWarner Losh                            const void* src, size_t srcSize,
6150c16b537SWarner Losh                            const FSE_CTable* ct)
6160c16b537SWarner Losh {
6170c16b537SWarner Losh     unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));
6180c16b537SWarner Losh 
6190c16b537SWarner Losh     if (fast)
6200c16b537SWarner Losh         return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);
6210c16b537SWarner Losh     else
6220c16b537SWarner Losh         return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);
6230c16b537SWarner Losh }
6240c16b537SWarner Losh 
6250c16b537SWarner Losh 
6260c16b537SWarner Losh size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
6270c16b537SWarner Losh 
6280c16b537SWarner Losh /* FSE_compress_wksp() :
6290c16b537SWarner Losh  * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
6300c16b537SWarner Losh  * `wkspSize` size must be `(1<<tableLog)`.
6310c16b537SWarner Losh  */
6320c16b537SWarner Losh size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
6330c16b537SWarner Losh {
6340c16b537SWarner Losh     BYTE* const ostart = (BYTE*) dst;
6350c16b537SWarner Losh     BYTE* op = ostart;
6360c16b537SWarner Losh     BYTE* const oend = ostart + dstSize;
6370c16b537SWarner Losh 
638a0483764SConrad Meyer     unsigned count[FSE_MAX_SYMBOL_VALUE+1];
6390c16b537SWarner Losh     S16   norm[FSE_MAX_SYMBOL_VALUE+1];
6400c16b537SWarner Losh     FSE_CTable* CTable = (FSE_CTable*)workSpace;
6410c16b537SWarner Losh     size_t const CTableSize = FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue);
6420c16b537SWarner Losh     void* scratchBuffer = (void*)(CTable + CTableSize);
6430c16b537SWarner Losh     size_t const scratchBufferSize = wkspSize - (CTableSize * sizeof(FSE_CTable));
6440c16b537SWarner Losh 
6450c16b537SWarner Losh     /* init conditions */
6460c16b537SWarner Losh     if (wkspSize < FSE_WKSP_SIZE_U32(tableLog, maxSymbolValue)) return ERROR(tableLog_tooLarge);
6470c16b537SWarner Losh     if (srcSize <= 1) return 0;  /* Not compressible */
6480c16b537SWarner Losh     if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
6490c16b537SWarner Losh     if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG;
6500c16b537SWarner Losh 
6510c16b537SWarner Losh     /* Scan input and build symbol stats */
652a0483764SConrad Meyer     {   CHECK_V_F(maxCount, HIST_count_wksp(count, &maxSymbolValue, src, srcSize, scratchBuffer, scratchBufferSize) );
6530c16b537SWarner Losh         if (maxCount == srcSize) return 1;   /* only a single symbol in src : rle */
6540c16b537SWarner Losh         if (maxCount == 1) return 0;         /* each symbol present maximum once => not compressible */
6550c16b537SWarner Losh         if (maxCount < (srcSize >> 7)) return 0;   /* Heuristic : not compressible enough */
6560c16b537SWarner Losh     }
6570c16b537SWarner Losh 
6580c16b537SWarner Losh     tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue);
6590c16b537SWarner Losh     CHECK_F( FSE_normalizeCount(norm, tableLog, count, srcSize, maxSymbolValue) );
6600c16b537SWarner Losh 
6610c16b537SWarner Losh     /* Write table description header */
6620c16b537SWarner Losh     {   CHECK_V_F(nc_err, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) );
6630c16b537SWarner Losh         op += nc_err;
6640c16b537SWarner Losh     }
6650c16b537SWarner Losh 
6660c16b537SWarner Losh     /* Compress */
6670c16b537SWarner Losh     CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, scratchBufferSize) );
6680c16b537SWarner Losh     {   CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, src, srcSize, CTable) );
6690c16b537SWarner Losh         if (cSize == 0) return 0;   /* not enough space for compressed data */
6700c16b537SWarner Losh         op += cSize;
6710c16b537SWarner Losh     }
6720c16b537SWarner Losh 
6730c16b537SWarner Losh     /* check compressibility */
6740c16b537SWarner Losh     if ( (size_t)(op-ostart) >= srcSize-1 ) return 0;
6750c16b537SWarner Losh 
6760c16b537SWarner Losh     return op-ostart;
6770c16b537SWarner Losh }
6780c16b537SWarner Losh 
6790c16b537SWarner Losh typedef struct {
6800c16b537SWarner Losh     FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)];
6810c16b537SWarner Losh     BYTE scratchBuffer[1 << FSE_MAX_TABLELOG];
6820c16b537SWarner Losh } fseWkspMax_t;
6830c16b537SWarner Losh 
6840c16b537SWarner Losh size_t FSE_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)
6850c16b537SWarner Losh {
6860c16b537SWarner Losh     fseWkspMax_t scratchBuffer;
6870f743729SConrad Meyer     DEBUG_STATIC_ASSERT(sizeof(scratchBuffer) >= FSE_WKSP_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE));   /* compilation failures here means scratchBuffer is not large enough */
6880c16b537SWarner Losh     if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
6890c16b537SWarner Losh     return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, &scratchBuffer, sizeof(scratchBuffer));
6900c16b537SWarner Losh }
6910c16b537SWarner Losh 
6920c16b537SWarner Losh size_t FSE_compress (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
6930c16b537SWarner Losh {
6940c16b537SWarner Losh     return FSE_compress2(dst, dstCapacity, src, srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG);
6950c16b537SWarner Losh }
6960c16b537SWarner Losh 
6970c16b537SWarner Losh 
6980c16b537SWarner Losh #endif   /* FSE_COMMONDEFS_ONLY */
699