0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include "../common/compiler.h"
0019 #include "../common/mem.h" /* U32, U16, etc. */
0020 #include "../common/debug.h" /* assert, DEBUGLOG */
0021 #include "hist.h" /* HIST_count_wksp */
0022 #include "../common/bitstream.h"
0023 #define FSE_STATIC_LINKING_ONLY
0024 #include "../common/fse.h"
0025 #include "../common/error_private.h"
0026 #define ZSTD_DEPS_NEED_MALLOC
0027 #define ZSTD_DEPS_NEED_MATH64
0028 #include "../common/zstd_deps.h" /* ZSTD_malloc, ZSTD_free, ZSTD_memcpy, ZSTD_memset */
0029
0030
0031
0032
0033
0034 #define FSE_isError ERR_isError
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 #ifndef FSE_FUNCTION_EXTENSION
0048 # error "FSE_FUNCTION_EXTENSION must be defined"
0049 #endif
0050 #ifndef FSE_FUNCTION_TYPE
0051 # error "FSE_FUNCTION_TYPE must be defined"
0052 #endif
0053
0054
0055 #define FSE_CAT(X,Y) X##Y
0056 #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
0057 #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 size_t FSE_buildCTable_wksp(FSE_CTable* ct,
0068 const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
0069 void* workSpace, size_t wkspSize)
0070 {
0071 U32 const tableSize = 1 << tableLog;
0072 U32 const tableMask = tableSize - 1;
0073 void* const ptr = ct;
0074 U16* const tableU16 = ( (U16*) ptr) + 2;
0075 void* const FSCT = ((U32*)ptr) + 1 + (tableLog ? tableSize>>1 : 1) ;
0076 FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
0077 U32 const step = FSE_TABLESTEP(tableSize);
0078
0079 U32* cumul = (U32*)workSpace;
0080 FSE_FUNCTION_TYPE* tableSymbol = (FSE_FUNCTION_TYPE*)(cumul + (maxSymbolValue + 2));
0081
0082 U32 highThreshold = tableSize-1;
0083
0084 if ((size_t)workSpace & 3) return ERROR(GENERIC);
0085 if (FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) > wkspSize) return ERROR(tableLog_tooLarge);
0086
0087 tableU16[-2] = (U16) tableLog;
0088 tableU16[-1] = (U16) maxSymbolValue;
0089 assert(tableLog < 16);
0090
0091
0092
0093
0094 #ifdef __clang_analyzer__
0095 ZSTD_memset(tableSymbol, 0, sizeof(*tableSymbol) * tableSize);
0096 #endif
0097
0098
0099 { U32 u;
0100 cumul[0] = 0;
0101 for (u=1; u <= maxSymbolValue+1; u++) {
0102 if (normalizedCounter[u-1]==-1) {
0103 cumul[u] = cumul[u-1] + 1;
0104 tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);
0105 } else {
0106 cumul[u] = cumul[u-1] + normalizedCounter[u-1];
0107 } }
0108 cumul[maxSymbolValue+1] = tableSize+1;
0109 }
0110
0111
0112 { U32 position = 0;
0113 U32 symbol;
0114 for (symbol=0; symbol<=maxSymbolValue; symbol++) {
0115 int nbOccurrences;
0116 int const freq = normalizedCounter[symbol];
0117 for (nbOccurrences=0; nbOccurrences<freq; nbOccurrences++) {
0118 tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
0119 position = (position + step) & tableMask;
0120 while (position > highThreshold)
0121 position = (position + step) & tableMask;
0122 } }
0123
0124 assert(position==0);
0125 }
0126
0127
0128 { U32 u; for (u=0; u<tableSize; u++) {
0129 FSE_FUNCTION_TYPE s = tableSymbol[u];
0130 tableU16[cumul[s]++] = (U16) (tableSize+u);
0131 } }
0132
0133
0134 { unsigned total = 0;
0135 unsigned s;
0136 for (s=0; s<=maxSymbolValue; s++) {
0137 switch (normalizedCounter[s])
0138 {
0139 case 0:
0140
0141 symbolTT[s].deltaNbBits = ((tableLog+1) << 16) - (1<<tableLog);
0142 break;
0143
0144 case -1:
0145 case 1:
0146 symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);
0147 symbolTT[s].deltaFindState = total - 1;
0148 total ++;
0149 break;
0150 default :
0151 {
0152 U32 const maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1);
0153 U32 const minStatePlus = normalizedCounter[s] << maxBitsOut;
0154 symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
0155 symbolTT[s].deltaFindState = total - normalizedCounter[s];
0156 total += normalizedCounter[s];
0157 } } } }
0158
0159 #if 0
0160 DEBUGLOG(5, "\n --- table statistics : ");
0161 { U32 symbol;
0162 for (symbol=0; symbol<=maxSymbolValue; symbol++) {
0163 DEBUGLOG(5, "%3u: w=%3i, maxBits=%u, fracBits=%.2f",
0164 symbol, normalizedCounter[symbol],
0165 FSE_getMaxNbBits(symbolTT, symbol),
0166 (double)FSE_bitCost(symbolTT, tableLog, symbol, 8) / 256);
0167 }
0168 }
0169 #endif
0170
0171 return 0;
0172 }
0173
0174
0175
0176
0177 #ifndef FSE_COMMONDEFS_ONLY
0178
0179
0180
0181
0182
0183 size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
0184 {
0185 size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 3;
0186 return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND;
0187 }
0188
0189 static size_t
0190 FSE_writeNCount_generic (void* header, size_t headerBufferSize,
0191 const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
0192 unsigned writeIsSafe)
0193 {
0194 BYTE* const ostart = (BYTE*) header;
0195 BYTE* out = ostart;
0196 BYTE* const oend = ostart + headerBufferSize;
0197 int nbBits;
0198 const int tableSize = 1 << tableLog;
0199 int remaining;
0200 int threshold;
0201 U32 bitStream = 0;
0202 int bitCount = 0;
0203 unsigned symbol = 0;
0204 unsigned const alphabetSize = maxSymbolValue + 1;
0205 int previousIs0 = 0;
0206
0207
0208 bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
0209 bitCount += 4;
0210
0211
0212 remaining = tableSize+1;
0213 threshold = tableSize;
0214 nbBits = tableLog+1;
0215
0216 while ((symbol < alphabetSize) && (remaining>1)) {
0217 if (previousIs0) {
0218 unsigned start = symbol;
0219 while ((symbol < alphabetSize) && !normalizedCounter[symbol]) symbol++;
0220 if (symbol == alphabetSize) break;
0221 while (symbol >= start+24) {
0222 start+=24;
0223 bitStream += 0xFFFFU << bitCount;
0224 if ((!writeIsSafe) && (out > oend-2))
0225 return ERROR(dstSize_tooSmall);
0226 out[0] = (BYTE) bitStream;
0227 out[1] = (BYTE)(bitStream>>8);
0228 out+=2;
0229 bitStream>>=16;
0230 }
0231 while (symbol >= start+3) {
0232 start+=3;
0233 bitStream += 3 << bitCount;
0234 bitCount += 2;
0235 }
0236 bitStream += (symbol-start) << bitCount;
0237 bitCount += 2;
0238 if (bitCount>16) {
0239 if ((!writeIsSafe) && (out > oend - 2))
0240 return ERROR(dstSize_tooSmall);
0241 out[0] = (BYTE)bitStream;
0242 out[1] = (BYTE)(bitStream>>8);
0243 out += 2;
0244 bitStream >>= 16;
0245 bitCount -= 16;
0246 } }
0247 { int count = normalizedCounter[symbol++];
0248 int const max = (2*threshold-1) - remaining;
0249 remaining -= count < 0 ? -count : count;
0250 count++;
0251 if (count>=threshold)
0252 count += max;
0253 bitStream += count << bitCount;
0254 bitCount += nbBits;
0255 bitCount -= (count<max);
0256 previousIs0 = (count==1);
0257 if (remaining<1) return ERROR(GENERIC);
0258 while (remaining<threshold) { nbBits--; threshold>>=1; }
0259 }
0260 if (bitCount>16) {
0261 if ((!writeIsSafe) && (out > oend - 2))
0262 return ERROR(dstSize_tooSmall);
0263 out[0] = (BYTE)bitStream;
0264 out[1] = (BYTE)(bitStream>>8);
0265 out += 2;
0266 bitStream >>= 16;
0267 bitCount -= 16;
0268 } }
0269
0270 if (remaining != 1)
0271 return ERROR(GENERIC);
0272 assert(symbol <= alphabetSize);
0273
0274
0275 if ((!writeIsSafe) && (out > oend - 2))
0276 return ERROR(dstSize_tooSmall);
0277 out[0] = (BYTE)bitStream;
0278 out[1] = (BYTE)(bitStream>>8);
0279 out+= (bitCount+7) /8;
0280
0281 return (out-ostart);
0282 }
0283
0284
0285 size_t FSE_writeNCount (void* buffer, size_t bufferSize,
0286 const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
0287 {
0288 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
0289 if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);
0290
0291 if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))
0292 return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
0293
0294 return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1 );
0295 }
0296
0297
0298
0299
0300
0301
0302 FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
0303 {
0304 size_t size;
0305 if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
0306 size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
0307 return (FSE_CTable*)ZSTD_malloc(size);
0308 }
0309
0310 void FSE_freeCTable (FSE_CTable* ct) { ZSTD_free(ct); }
0311
0312
0313 static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
0314 {
0315 U32 minBitsSrc = BIT_highbit32((U32)(srcSize)) + 1;
0316 U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2;
0317 U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;
0318 assert(srcSize > 1);
0319 return minBits;
0320 }
0321
0322 unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
0323 {
0324 U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus;
0325 U32 tableLog = maxTableLog;
0326 U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);
0327 assert(srcSize > 1);
0328 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
0329 if (maxBitsSrc < tableLog) tableLog = maxBitsSrc;
0330 if (minBits > tableLog) tableLog = minBits;
0331 if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
0332 if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
0333 return tableLog;
0334 }
0335
0336 unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
0337 {
0338 return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);
0339 }
0340
0341
0342
0343
0344 static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue, short lowProbCount)
0345 {
0346 short const NOT_YET_ASSIGNED = -2;
0347 U32 s;
0348 U32 distributed = 0;
0349 U32 ToDistribute;
0350
0351
0352 U32 const lowThreshold = (U32)(total >> tableLog);
0353 U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
0354
0355 for (s=0; s<=maxSymbolValue; s++) {
0356 if (count[s] == 0) {
0357 norm[s]=0;
0358 continue;
0359 }
0360 if (count[s] <= lowThreshold) {
0361 norm[s] = lowProbCount;
0362 distributed++;
0363 total -= count[s];
0364 continue;
0365 }
0366 if (count[s] <= lowOne) {
0367 norm[s] = 1;
0368 distributed++;
0369 total -= count[s];
0370 continue;
0371 }
0372
0373 norm[s]=NOT_YET_ASSIGNED;
0374 }
0375 ToDistribute = (1 << tableLog) - distributed;
0376
0377 if (ToDistribute == 0)
0378 return 0;
0379
0380 if ((total / ToDistribute) > lowOne) {
0381
0382 lowOne = (U32)((total * 3) / (ToDistribute * 2));
0383 for (s=0; s<=maxSymbolValue; s++) {
0384 if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {
0385 norm[s] = 1;
0386 distributed++;
0387 total -= count[s];
0388 continue;
0389 } }
0390 ToDistribute = (1 << tableLog) - distributed;
0391 }
0392
0393 if (distributed == maxSymbolValue+1) {
0394
0395
0396
0397 U32 maxV = 0, maxC = 0;
0398 for (s=0; s<=maxSymbolValue; s++)
0399 if (count[s] > maxC) { maxV=s; maxC=count[s]; }
0400 norm[maxV] += (short)ToDistribute;
0401 return 0;
0402 }
0403
0404 if (total == 0) {
0405
0406 for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1))
0407 if (norm[s] > 0) { ToDistribute--; norm[s]++; }
0408 return 0;
0409 }
0410
0411 { U64 const vStepLog = 62 - tableLog;
0412 U64 const mid = (1ULL << (vStepLog-1)) - 1;
0413 U64 const rStep = ZSTD_div64((((U64)1<<vStepLog) * ToDistribute) + mid, (U32)total);
0414 U64 tmpTotal = mid;
0415 for (s=0; s<=maxSymbolValue; s++) {
0416 if (norm[s]==NOT_YET_ASSIGNED) {
0417 U64 const end = tmpTotal + (count[s] * rStep);
0418 U32 const sStart = (U32)(tmpTotal >> vStepLog);
0419 U32 const sEnd = (U32)(end >> vStepLog);
0420 U32 const weight = sEnd - sStart;
0421 if (weight < 1)
0422 return ERROR(GENERIC);
0423 norm[s] = (short)weight;
0424 tmpTotal = end;
0425 } } }
0426
0427 return 0;
0428 }
0429
0430 size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
0431 const unsigned* count, size_t total,
0432 unsigned maxSymbolValue, unsigned useLowProbCount)
0433 {
0434
0435 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
0436 if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);
0437 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
0438 if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC);
0439
0440 { static U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
0441 short const lowProbCount = useLowProbCount ? -1 : 1;
0442 U64 const scale = 62 - tableLog;
0443 U64 const step = ZSTD_div64((U64)1<<62, (U32)total);
0444 U64 const vStep = 1ULL<<(scale-20);
0445 int stillToDistribute = 1<<tableLog;
0446 unsigned s;
0447 unsigned largest=0;
0448 short largestP=0;
0449 U32 lowThreshold = (U32)(total >> tableLog);
0450
0451 for (s=0; s<=maxSymbolValue; s++) {
0452 if (count[s] == total) return 0;
0453 if (count[s] == 0) { normalizedCounter[s]=0; continue; }
0454 if (count[s] <= lowThreshold) {
0455 normalizedCounter[s] = lowProbCount;
0456 stillToDistribute--;
0457 } else {
0458 short proba = (short)((count[s]*step) >> scale);
0459 if (proba<8) {
0460 U64 restToBeat = vStep * rtbTable[proba];
0461 proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
0462 }
0463 if (proba > largestP) { largestP=proba; largest=s; }
0464 normalizedCounter[s] = proba;
0465 stillToDistribute -= proba;
0466 } }
0467 if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
0468
0469 size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue, lowProbCount);
0470 if (FSE_isError(errorCode)) return errorCode;
0471 }
0472 else normalizedCounter[largest] += (short)stillToDistribute;
0473 }
0474
0475 #if 0
0476 {
0477 U32 s;
0478 U32 nTotal = 0;
0479 for (s=0; s<=maxSymbolValue; s++)
0480 RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]);
0481 for (s=0; s<=maxSymbolValue; s++)
0482 nTotal += abs(normalizedCounter[s]);
0483 if (nTotal != (1U<<tableLog))
0484 RAWLOG(2, "Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
0485 getchar();
0486 }
0487 #endif
0488
0489 return tableLog;
0490 }
0491
0492
0493
0494 size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits)
0495 {
0496 const unsigned tableSize = 1 << nbBits;
0497 const unsigned tableMask = tableSize - 1;
0498 const unsigned maxSymbolValue = tableMask;
0499 void* const ptr = ct;
0500 U16* const tableU16 = ( (U16*) ptr) + 2;
0501 void* const FSCT = ((U32*)ptr) + 1 + (tableSize>>1);
0502 FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
0503 unsigned s;
0504
0505
0506 if (nbBits < 1) return ERROR(GENERIC);
0507
0508
0509 tableU16[-2] = (U16) nbBits;
0510 tableU16[-1] = (U16) maxSymbolValue;
0511
0512
0513 for (s=0; s<tableSize; s++)
0514 tableU16[s] = (U16)(tableSize + s);
0515
0516
0517 { const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
0518 for (s=0; s<=maxSymbolValue; s++) {
0519 symbolTT[s].deltaNbBits = deltaNbBits;
0520 symbolTT[s].deltaFindState = s-1;
0521 } }
0522
0523 return 0;
0524 }
0525
0526
0527 size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue)
0528 {
0529 void* ptr = ct;
0530 U16* tableU16 = ( (U16*) ptr) + 2;
0531 void* FSCTptr = (U32*)ptr + 2;
0532 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr;
0533
0534
0535 tableU16[-2] = (U16) 0;
0536 tableU16[-1] = (U16) symbolValue;
0537
0538
0539 tableU16[0] = 0;
0540 tableU16[1] = 0;
0541
0542
0543 symbolTT[symbolValue].deltaNbBits = 0;
0544 symbolTT[symbolValue].deltaFindState = 0;
0545
0546 return 0;
0547 }
0548
0549
0550 static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
0551 const void* src, size_t srcSize,
0552 const FSE_CTable* ct, const unsigned fast)
0553 {
0554 const BYTE* const istart = (const BYTE*) src;
0555 const BYTE* const iend = istart + srcSize;
0556 const BYTE* ip=iend;
0557
0558 BIT_CStream_t bitC;
0559 FSE_CState_t CState1, CState2;
0560
0561
0562 if (srcSize <= 2) return 0;
0563 { size_t const initError = BIT_initCStream(&bitC, dst, dstSize);
0564 if (FSE_isError(initError)) return 0; }
0565
0566 #define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
0567
0568 if (srcSize & 1) {
0569 FSE_initCState2(&CState1, ct, *--ip);
0570 FSE_initCState2(&CState2, ct, *--ip);
0571 FSE_encodeSymbol(&bitC, &CState1, *--ip);
0572 FSE_FLUSHBITS(&bitC);
0573 } else {
0574 FSE_initCState2(&CState2, ct, *--ip);
0575 FSE_initCState2(&CState1, ct, *--ip);
0576 }
0577
0578
0579 srcSize -= 2;
0580 if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) {
0581 FSE_encodeSymbol(&bitC, &CState2, *--ip);
0582 FSE_encodeSymbol(&bitC, &CState1, *--ip);
0583 FSE_FLUSHBITS(&bitC);
0584 }
0585
0586
0587 while ( ip>istart ) {
0588
0589 FSE_encodeSymbol(&bitC, &CState2, *--ip);
0590
0591 if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 )
0592 FSE_FLUSHBITS(&bitC);
0593
0594 FSE_encodeSymbol(&bitC, &CState1, *--ip);
0595
0596 if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) {
0597 FSE_encodeSymbol(&bitC, &CState2, *--ip);
0598 FSE_encodeSymbol(&bitC, &CState1, *--ip);
0599 }
0600
0601 FSE_FLUSHBITS(&bitC);
0602 }
0603
0604 FSE_flushCState(&bitC, &CState2);
0605 FSE_flushCState(&bitC, &CState1);
0606 return BIT_closeCStream(&bitC);
0607 }
0608
0609 size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
0610 const void* src, size_t srcSize,
0611 const FSE_CTable* ct)
0612 {
0613 unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));
0614
0615 if (fast)
0616 return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);
0617 else
0618 return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);
0619 }
0620
0621
0622 size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
0623
0624
0625 #endif