huf_compress.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* ******************************************************************
  2. * Huffman encoder, part of New Generation Entropy library
  3. * Copyright (c) Yann Collet, Facebook, Inc.
  4. *
  5. * You can contact the author at :
  6. * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
  7. * - Public forum : https://groups.google.com/forum/#!forum/lz4c
  8. *
  9. * This source code is licensed under both the BSD-style license (found in the
  10. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  11. * in the COPYING file in the root directory of this source tree).
  12. * You may select, at your option, one of the above-listed licenses.
  13. ****************************************************************** */
  14. /* **************************************************************
  15. * Compiler specifics
  16. ****************************************************************/
  17. #ifdef _MSC_VER /* Visual Studio */
  18. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  19. #endif
  20. /* **************************************************************
  21. * Includes
  22. ****************************************************************/
  23. #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */
  24. #include "../common/compiler.h"
  25. #include "../common/bitstream.h"
  26. #include "hist.h"
  27. #define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */
  28. #include "../common/fse.h" /* header compression */
  29. #define HUF_STATIC_LINKING_ONLY
  30. #include "../common/huf.h"
  31. #include "../common/error_private.h"
  32. /* **************************************************************
  33. * Error Management
  34. ****************************************************************/
  35. #define HUF_isError ERR_isError
  36. #define HUF_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
  37. /* **************************************************************
  38. * Utils
  39. ****************************************************************/
  40. unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
  41. {
  42. return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1);
  43. }
  44. /* *******************************************************
  45. * HUF : Huffman block compression
  46. *********************************************************/
  47. /* HUF_compressWeights() :
  48. * Same as FSE_compress(), but dedicated to huff0's weights compression.
  49. * The use case needs much less stack memory.
  50. * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
  51. */
  52. #define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
  53. typedef struct {
  54. FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)];
  55. U32 scratchBuffer[FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(HUF_TABLELOG_MAX, MAX_FSE_TABLELOG_FOR_HUFF_HEADER)];
  56. unsigned count[HUF_TABLELOG_MAX+1];
  57. S16 norm[HUF_TABLELOG_MAX+1];
  58. } HUF_CompressWeightsWksp;
  59. static size_t HUF_compressWeights(void* dst, size_t dstSize, const void* weightTable, size_t wtSize, void* workspace, size_t workspaceSize)
  60. {
  61. BYTE* const ostart = (BYTE*) dst;
  62. BYTE* op = ostart;
  63. BYTE* const oend = ostart + dstSize;
  64. unsigned maxSymbolValue = HUF_TABLELOG_MAX;
  65. U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER;
  66. HUF_CompressWeightsWksp* wksp = (HUF_CompressWeightsWksp*)workspace;
  67. if (workspaceSize < sizeof(HUF_CompressWeightsWksp)) return ERROR(GENERIC);
  68. /* init conditions */
  69. if (wtSize <= 1) return 0; /* Not compressible */
  70. /* Scan input and build symbol stats */
  71. { unsigned const maxCount = HIST_count_simple(wksp->count, &maxSymbolValue, weightTable, wtSize); /* never fails */
  72. if (maxCount == wtSize) return 1; /* only a single symbol in src : rle */
  73. if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */
  74. }
  75. tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
  76. CHECK_F( FSE_normalizeCount(wksp->norm, tableLog, wksp->count, wtSize, maxSymbolValue, /* useLowProbCount */ 0) );
  77. /* Write table description header */
  78. { CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend-op), wksp->norm, maxSymbolValue, tableLog) );
  79. op += hSize;
  80. }
  81. /* Compress */
  82. CHECK_F( FSE_buildCTable_wksp(wksp->CTable, wksp->norm, maxSymbolValue, tableLog, wksp->scratchBuffer, sizeof(wksp->scratchBuffer)) );
  83. { CHECK_V_F(cSize, FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, wksp->CTable) );
  84. if (cSize == 0) return 0; /* not enough space for compressed data */
  85. op += cSize;
  86. }
  87. return (size_t)(op-ostart);
  88. }
  89. typedef struct {
  90. HUF_CompressWeightsWksp wksp;
  91. BYTE bitsToWeight[HUF_TABLELOG_MAX + 1]; /* precomputed conversion table */
  92. BYTE huffWeight[HUF_SYMBOLVALUE_MAX];
  93. } HUF_WriteCTableWksp;
  94. size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize,
  95. const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog,
  96. void* workspace, size_t workspaceSize)
  97. {
  98. BYTE* op = (BYTE*)dst;
  99. U32 n;
  100. HUF_WriteCTableWksp* wksp = (HUF_WriteCTableWksp*)workspace;
  101. /* check conditions */
  102. if (workspaceSize < sizeof(HUF_WriteCTableWksp)) return ERROR(GENERIC);
  103. if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge);
  104. /* convert to weight */
  105. wksp->bitsToWeight[0] = 0;
  106. for (n=1; n<huffLog+1; n++)
  107. wksp->bitsToWeight[n] = (BYTE)(huffLog + 1 - n);
  108. for (n=0; n<maxSymbolValue; n++)
  109. wksp->huffWeight[n] = wksp->bitsToWeight[CTable[n].nbBits];
  110. /* attempt weights compression by FSE */
  111. { CHECK_V_F(hSize, HUF_compressWeights(op+1, maxDstSize-1, wksp->huffWeight, maxSymbolValue, &wksp->wksp, sizeof(wksp->wksp)) );
  112. if ((hSize>1) & (hSize < maxSymbolValue/2)) { /* FSE compressed */
  113. op[0] = (BYTE)hSize;
  114. return hSize+1;
  115. } }
  116. /* write raw values as 4-bits (max : 15) */
  117. if (maxSymbolValue > (256-128)) return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */
  118. if (((maxSymbolValue+1)/2) + 1 > maxDstSize) return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */
  119. op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue-1));
  120. wksp->huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */
  121. for (n=0; n<maxSymbolValue; n+=2)
  122. op[(n/2)+1] = (BYTE)((wksp->huffWeight[n] << 4) + wksp->huffWeight[n+1]);
  123. return ((maxSymbolValue+1)/2) + 1;
  124. }
  125. /*! HUF_writeCTable() :
  126. `CTable` : Huffman tree to save, using huf representation.
  127. @return : size of saved CTable */
  128. size_t HUF_writeCTable (void* dst, size_t maxDstSize,
  129. const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog)
  130. {
  131. HUF_WriteCTableWksp wksp;
  132. return HUF_writeCTable_wksp(dst, maxDstSize, CTable, maxSymbolValue, huffLog, &wksp, sizeof(wksp));
  133. }
  134. size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned* hasZeroWeights)
  135. {
  136. BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1]; /* init not required, even though some static analyzer may complain */
  137. U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */
  138. U32 tableLog = 0;
  139. U32 nbSymbols = 0;
  140. /* get symbol weights */
  141. CHECK_V_F(readSize, HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX+1, rankVal, &nbSymbols, &tableLog, src, srcSize));
  142. *hasZeroWeights = (rankVal[0] > 0);
  143. /* check result */
  144. if (tableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
  145. if (nbSymbols > *maxSymbolValuePtr+1) return ERROR(maxSymbolValue_tooSmall);
  146. /* Prepare base value per rank */
  147. { U32 n, nextRankStart = 0;
  148. for (n=1; n<=tableLog; n++) {
  149. U32 curr = nextRankStart;
  150. nextRankStart += (rankVal[n] << (n-1));
  151. rankVal[n] = curr;
  152. } }
  153. /* fill nbBits */
  154. { U32 n; for (n=0; n<nbSymbols; n++) {
  155. const U32 w = huffWeight[n];
  156. CTable[n].nbBits = (BYTE)(tableLog + 1 - w) & -(w != 0);
  157. } }
  158. /* fill val */
  159. { U16 nbPerRank[HUF_TABLELOG_MAX+2] = {0}; /* support w=0=>n=tableLog+1 */
  160. U16 valPerRank[HUF_TABLELOG_MAX+2] = {0};
  161. { U32 n; for (n=0; n<nbSymbols; n++) nbPerRank[CTable[n].nbBits]++; }
  162. /* determine stating value per rank */
  163. valPerRank[tableLog+1] = 0; /* for w==0 */
  164. { U16 min = 0;
  165. U32 n; for (n=tableLog; n>0; n--) { /* start at n=tablelog <-> w=1 */
  166. valPerRank[n] = min; /* get starting value within each rank */
  167. min += nbPerRank[n];
  168. min >>= 1;
  169. } }
  170. /* assign value within rank, symbol order */
  171. { U32 n; for (n=0; n<nbSymbols; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; }
  172. }
  173. *maxSymbolValuePtr = nbSymbols - 1;
  174. return readSize;
  175. }
  176. U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue)
  177. {
  178. const HUF_CElt* table = (const HUF_CElt*)symbolTable;
  179. assert(symbolValue <= HUF_SYMBOLVALUE_MAX);
  180. return table[symbolValue].nbBits;
  181. }
  182. typedef struct nodeElt_s {
  183. U32 count;
  184. U16 parent;
  185. BYTE byte;
  186. BYTE nbBits;
  187. } nodeElt;
  188. /**
  189. * HUF_setMaxHeight():
  190. * Enforces maxNbBits on the Huffman tree described in huffNode.
  191. *
  192. * It sets all nodes with nbBits > maxNbBits to be maxNbBits. Then it adjusts
  193. * the tree to so that it is a valid canonical Huffman tree.
  194. *
  195. * @pre The sum of the ranks of each symbol == 2^largestBits,
  196. * where largestBits == huffNode[lastNonNull].nbBits.
  197. * @post The sum of the ranks of each symbol == 2^largestBits,
  198. * where largestBits is the return value <= maxNbBits.
  199. *
  200. * @param huffNode The Huffman tree modified in place to enforce maxNbBits.
  201. * @param lastNonNull The symbol with the lowest count in the Huffman tree.
  202. * @param maxNbBits The maximum allowed number of bits, which the Huffman tree
  203. * may not respect. After this function the Huffman tree will
  204. * respect maxNbBits.
  205. * @return The maximum number of bits of the Huffman tree after adjustment,
  206. * necessarily no more than maxNbBits.
  207. */
  208. static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
  209. {
  210. const U32 largestBits = huffNode[lastNonNull].nbBits;
  211. /* early exit : no elt > maxNbBits, so the tree is already valid. */
  212. if (largestBits <= maxNbBits) return largestBits;
  213. /* there are several too large elements (at least >= 2) */
  214. { int totalCost = 0;
  215. const U32 baseCost = 1 << (largestBits - maxNbBits);
  216. int n = (int)lastNonNull;
  217. /* Adjust any ranks > maxNbBits to maxNbBits.
  218. * Compute totalCost, which is how far the sum of the ranks is
  219. * we are over 2^largestBits after adjust the offending ranks.
  220. */
  221. while (huffNode[n].nbBits > maxNbBits) {
  222. totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
  223. huffNode[n].nbBits = (BYTE)maxNbBits;
  224. n--;
  225. }
  226. /* n stops at huffNode[n].nbBits <= maxNbBits */
  227. assert(huffNode[n].nbBits <= maxNbBits);
  228. /* n end at index of smallest symbol using < maxNbBits */
  229. while (huffNode[n].nbBits == maxNbBits) --n;
  230. /* renorm totalCost from 2^largestBits to 2^maxNbBits
  231. * note : totalCost is necessarily a multiple of baseCost */
  232. assert((totalCost & (baseCost - 1)) == 0);
  233. totalCost >>= (largestBits - maxNbBits);
  234. assert(totalCost > 0);
  235. /* repay normalized cost */
  236. { U32 const noSymbol = 0xF0F0F0F0;
  237. U32 rankLast[HUF_TABLELOG_MAX+2];
  238. /* Get pos of last (smallest = lowest cum. count) symbol per rank */
  239. ZSTD_memset(rankLast, 0xF0, sizeof(rankLast));
  240. { U32 currentNbBits = maxNbBits;
  241. int pos;
  242. for (pos=n ; pos >= 0; pos--) {
  243. if (huffNode[pos].nbBits >= currentNbBits) continue;
  244. currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */
  245. rankLast[maxNbBits-currentNbBits] = (U32)pos;
  246. } }
  247. while (totalCost > 0) {
  248. /* Try to reduce the next power of 2 above totalCost because we
  249. * gain back half the rank.
  250. */
  251. U32 nBitsToDecrease = BIT_highbit32((U32)totalCost) + 1;
  252. for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
  253. U32 const highPos = rankLast[nBitsToDecrease];
  254. U32 const lowPos = rankLast[nBitsToDecrease-1];
  255. if (highPos == noSymbol) continue;
  256. /* Decrease highPos if no symbols of lowPos or if it is
  257. * not cheaper to remove 2 lowPos than highPos.
  258. */
  259. if (lowPos == noSymbol) break;
  260. { U32 const highTotal = huffNode[highPos].count;
  261. U32 const lowTotal = 2 * huffNode[lowPos].count;
  262. if (highTotal <= lowTotal) break;
  263. } }
  264. /* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */
  265. assert(rankLast[nBitsToDecrease] != noSymbol || nBitsToDecrease == 1);
  266. /* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */
  267. while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol))
  268. nBitsToDecrease++;
  269. assert(rankLast[nBitsToDecrease] != noSymbol);
  270. /* Increase the number of bits to gain back half the rank cost. */
  271. totalCost -= 1 << (nBitsToDecrease-1);
  272. huffNode[rankLast[nBitsToDecrease]].nbBits++;
  273. /* Fix up the new rank.
  274. * If the new rank was empty, this symbol is now its smallest.
  275. * Otherwise, this symbol will be the largest in the new rank so no adjustment.
  276. */
  277. if (rankLast[nBitsToDecrease-1] == noSymbol)
  278. rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease];
  279. /* Fix up the old rank.
  280. * If the symbol was at position 0, meaning it was the highest weight symbol in the tree,
  281. * it must be the only symbol in its rank, so the old rank now has no symbols.
  282. * Otherwise, since the Huffman nodes are sorted by count, the previous position is now
  283. * the smallest node in the rank. If the previous position belongs to a different rank,
  284. * then the rank is now empty.
  285. */
  286. if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */
  287. rankLast[nBitsToDecrease] = noSymbol;
  288. else {
  289. rankLast[nBitsToDecrease]--;
  290. if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease)
  291. rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */
  292. }
  293. } /* while (totalCost > 0) */
  294. /* If we've removed too much weight, then we have to add it back.
  295. * To avoid overshooting again, we only adjust the smallest rank.
  296. * We take the largest nodes from the lowest rank 0 and move them
  297. * to rank 1. There's guaranteed to be enough rank 0 symbols because
  298. * TODO.
  299. */
  300. while (totalCost < 0) { /* Sometimes, cost correction overshoot */
  301. /* special case : no rank 1 symbol (using maxNbBits-1);
  302. * let's create one from largest rank 0 (using maxNbBits).
  303. */
  304. if (rankLast[1] == noSymbol) {
  305. while (huffNode[n].nbBits == maxNbBits) n--;
  306. huffNode[n+1].nbBits--;
  307. assert(n >= 0);
  308. rankLast[1] = (U32)(n+1);
  309. totalCost++;
  310. continue;
  311. }
  312. huffNode[ rankLast[1] + 1 ].nbBits--;
  313. rankLast[1]++;
  314. totalCost ++;
  315. }
  316. } /* repay normalized cost */
  317. } /* there are several too large elements (at least >= 2) */
  318. return maxNbBits;
  319. }
  320. typedef struct {
  321. U32 base;
  322. U32 curr;
  323. } rankPos;
  324. typedef nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32];
  325. #define RANK_POSITION_TABLE_SIZE 32
  326. typedef struct {
  327. huffNodeTable huffNodeTbl;
  328. rankPos rankPosition[RANK_POSITION_TABLE_SIZE];
  329. } HUF_buildCTable_wksp_tables;
  330. /**
  331. * HUF_sort():
  332. * Sorts the symbols [0, maxSymbolValue] by count[symbol] in decreasing order.
  333. *
  334. * @param[out] huffNode Sorted symbols by decreasing count. Only members `.count` and `.byte` are filled.
  335. * Must have (maxSymbolValue + 1) entries.
  336. * @param[in] count Histogram of the symbols.
  337. * @param[in] maxSymbolValue Maximum symbol value.
  338. * @param rankPosition This is a scratch workspace. Must have RANK_POSITION_TABLE_SIZE entries.
  339. */
  340. static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValue, rankPos* rankPosition)
  341. {
  342. int n;
  343. int const maxSymbolValue1 = (int)maxSymbolValue + 1;
  344. /* Compute base and set curr to base.
  345. * For symbol s let lowerRank = BIT_highbit32(count[n]+1) and rank = lowerRank + 1.
  346. * Then 2^lowerRank <= count[n]+1 <= 2^rank.
  347. * We attribute each symbol to lowerRank's base value, because we want to know where
  348. * each rank begins in the output, so for rank R we want to count ranks R+1 and above.
  349. */
  350. ZSTD_memset(rankPosition, 0, sizeof(*rankPosition) * RANK_POSITION_TABLE_SIZE);
  351. for (n = 0; n < maxSymbolValue1; ++n) {
  352. U32 lowerRank = BIT_highbit32(count[n] + 1);
  353. rankPosition[lowerRank].base++;
  354. }
  355. assert(rankPosition[RANK_POSITION_TABLE_SIZE - 1].base == 0);
  356. for (n = RANK_POSITION_TABLE_SIZE - 1; n > 0; --n) {
  357. rankPosition[n-1].base += rankPosition[n].base;
  358. rankPosition[n-1].curr = rankPosition[n-1].base;
  359. }
  360. /* Sort */
  361. for (n = 0; n < maxSymbolValue1; ++n) {
  362. U32 const c = count[n];
  363. U32 const r = BIT_highbit32(c+1) + 1;
  364. U32 pos = rankPosition[r].curr++;
  365. /* Insert into the correct position in the rank.
  366. * We have at most 256 symbols, so this insertion should be fine.
  367. */
  368. while ((pos > rankPosition[r].base) && (c > huffNode[pos-1].count)) {
  369. huffNode[pos] = huffNode[pos-1];
  370. pos--;
  371. }
  372. huffNode[pos].count = c;
  373. huffNode[pos].byte = (BYTE)n;
  374. }
  375. }
  376. /** HUF_buildCTable_wksp() :
  377. * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
  378. * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as sizeof(HUF_buildCTable_wksp_tables).
  379. */
  380. #define STARTNODE (HUF_SYMBOLVALUE_MAX+1)
  381. /* HUF_buildTree():
  382. * Takes the huffNode array sorted by HUF_sort() and builds an unlimited-depth Huffman tree.
  383. *
  384. * @param huffNode The array sorted by HUF_sort(). Builds the Huffman tree in this array.
  385. * @param maxSymbolValue The maximum symbol value.
  386. * @return The smallest node in the Huffman tree (by count).
  387. */
  388. static int HUF_buildTree(nodeElt* huffNode, U32 maxSymbolValue)
  389. {
  390. nodeElt* const huffNode0 = huffNode - 1;
  391. int nonNullRank;
  392. int lowS, lowN;
  393. int nodeNb = STARTNODE;
  394. int n, nodeRoot;
  395. /* init for parents */
  396. nonNullRank = (int)maxSymbolValue;
  397. while(huffNode[nonNullRank].count == 0) nonNullRank--;
  398. lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb;
  399. huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count;
  400. huffNode[lowS].parent = huffNode[lowS-1].parent = (U16)nodeNb;
  401. nodeNb++; lowS-=2;
  402. for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30);
  403. huffNode0[0].count = (U32)(1U<<31); /* fake entry, strong barrier */
  404. /* create parents */
  405. while (nodeNb <= nodeRoot) {
  406. int const n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
  407. int const n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
  408. huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count;
  409. huffNode[n1].parent = huffNode[n2].parent = (U16)nodeNb;
  410. nodeNb++;
  411. }
  412. /* distribute weights (unlimited tree height) */
  413. huffNode[nodeRoot].nbBits = 0;
  414. for (n=nodeRoot-1; n>=STARTNODE; n--)
  415. huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
  416. for (n=0; n<=nonNullRank; n++)
  417. huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
  418. return nonNullRank;
  419. }
  420. /**
  421. * HUF_buildCTableFromTree():
  422. * Build the CTable given the Huffman tree in huffNode.
  423. *
  424. * @param[out] CTable The output Huffman CTable.
  425. * @param huffNode The Huffman tree.
  426. * @param nonNullRank The last and smallest node in the Huffman tree.
  427. * @param maxSymbolValue The maximum symbol value.
  428. * @param maxNbBits The exact maximum number of bits used in the Huffman tree.
  429. */
  430. static void HUF_buildCTableFromTree(HUF_CElt* CTable, nodeElt const* huffNode, int nonNullRank, U32 maxSymbolValue, U32 maxNbBits)
  431. {
  432. /* fill result into ctable (val, nbBits) */
  433. int n;
  434. U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
  435. U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
  436. int const alphabetSize = (int)(maxSymbolValue + 1);
  437. for (n=0; n<=nonNullRank; n++)
  438. nbPerRank[huffNode[n].nbBits]++;
  439. /* determine starting value per rank */
  440. { U16 min = 0;
  441. for (n=(int)maxNbBits; n>0; n--) {
  442. valPerRank[n] = min; /* get starting value within each rank */
  443. min += nbPerRank[n];
  444. min >>= 1;
  445. } }
  446. for (n=0; n<alphabetSize; n++)
  447. CTable[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
  448. for (n=0; n<alphabetSize; n++)
  449. CTable[n].val = valPerRank[CTable[n].nbBits]++; /* assign value within rank, symbol order */
  450. }
  451. size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize)
  452. {
  453. HUF_buildCTable_wksp_tables* const wksp_tables = (HUF_buildCTable_wksp_tables*)workSpace;
  454. nodeElt* const huffNode0 = wksp_tables->huffNodeTbl;
  455. nodeElt* const huffNode = huffNode0+1;
  456. int nonNullRank;
  457. /* safety checks */
  458. if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
  459. if (wkspSize < sizeof(HUF_buildCTable_wksp_tables))
  460. return ERROR(workSpace_tooSmall);
  461. if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
  462. if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
  463. return ERROR(maxSymbolValue_tooLarge);
  464. ZSTD_memset(huffNode0, 0, sizeof(huffNodeTable));
  465. /* sort, decreasing order */
  466. HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition);
  467. /* build tree */
  468. nonNullRank = HUF_buildTree(huffNode, maxSymbolValue);
  469. /* enforce maxTableLog */
  470. maxNbBits = HUF_setMaxHeight(huffNode, (U32)nonNullRank, maxNbBits);
  471. if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */
  472. HUF_buildCTableFromTree(tree, huffNode, nonNullRank, maxSymbolValue, maxNbBits);
  473. return maxNbBits;
  474. }
  475. size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue)
  476. {
  477. size_t nbBits = 0;
  478. int s;
  479. for (s = 0; s <= (int)maxSymbolValue; ++s) {
  480. nbBits += CTable[s].nbBits * count[s];
  481. }
  482. return nbBits >> 3;
  483. }
  484. int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) {
  485. int bad = 0;
  486. int s;
  487. for (s = 0; s <= (int)maxSymbolValue; ++s) {
  488. bad |= (count[s] != 0) & (CTable[s].nbBits == 0);
  489. }
  490. return !bad;
  491. }
  492. size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); }
  493. FORCE_INLINE_TEMPLATE void
  494. HUF_encodeSymbol(BIT_CStream_t* bitCPtr, U32 symbol, const HUF_CElt* CTable)
  495. {
  496. BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits);
  497. }
  498. #define HUF_FLUSHBITS(s) BIT_flushBits(s)
  499. #define HUF_FLUSHBITS_1(stream) \
  500. if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*2+7) HUF_FLUSHBITS(stream)
  501. #define HUF_FLUSHBITS_2(stream) \
  502. if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*4+7) HUF_FLUSHBITS(stream)
  503. FORCE_INLINE_TEMPLATE size_t
  504. HUF_compress1X_usingCTable_internal_body(void* dst, size_t dstSize,
  505. const void* src, size_t srcSize,
  506. const HUF_CElt* CTable)
  507. {
  508. const BYTE* ip = (const BYTE*) src;
  509. BYTE* const ostart = (BYTE*)dst;
  510. BYTE* const oend = ostart + dstSize;
  511. BYTE* op = ostart;
  512. size_t n;
  513. BIT_CStream_t bitC;
  514. /* init */
  515. if (dstSize < 8) return 0; /* not enough space to compress */
  516. { size_t const initErr = BIT_initCStream(&bitC, op, (size_t)(oend-op));
  517. if (HUF_isError(initErr)) return 0; }
  518. n = srcSize & ~3; /* join to mod 4 */
  519. switch (srcSize & 3)
  520. {
  521. case 3 : HUF_encodeSymbol(&bitC, ip[n+ 2], CTable);
  522. HUF_FLUSHBITS_2(&bitC);
  523. /* fall-through */
  524. case 2 : HUF_encodeSymbol(&bitC, ip[n+ 1], CTable);
  525. HUF_FLUSHBITS_1(&bitC);
  526. /* fall-through */
  527. case 1 : HUF_encodeSymbol(&bitC, ip[n+ 0], CTable);
  528. HUF_FLUSHBITS(&bitC);
  529. /* fall-through */
  530. case 0 : /* fall-through */
  531. default: break;
  532. }
  533. for (; n>0; n-=4) { /* note : n&3==0 at this stage */
  534. HUF_encodeSymbol(&bitC, ip[n- 1], CTable);
  535. HUF_FLUSHBITS_1(&bitC);
  536. HUF_encodeSymbol(&bitC, ip[n- 2], CTable);
  537. HUF_FLUSHBITS_2(&bitC);
  538. HUF_encodeSymbol(&bitC, ip[n- 3], CTable);
  539. HUF_FLUSHBITS_1(&bitC);
  540. HUF_encodeSymbol(&bitC, ip[n- 4], CTable);
  541. HUF_FLUSHBITS(&bitC);
  542. }
  543. return BIT_closeCStream(&bitC);
  544. }
  545. #if DYNAMIC_BMI2
  546. static TARGET_ATTRIBUTE("bmi2") size_t
  547. HUF_compress1X_usingCTable_internal_bmi2(void* dst, size_t dstSize,
  548. const void* src, size_t srcSize,
  549. const HUF_CElt* CTable)
  550. {
  551. return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable);
  552. }
  553. static size_t
  554. HUF_compress1X_usingCTable_internal_default(void* dst, size_t dstSize,
  555. const void* src, size_t srcSize,
  556. const HUF_CElt* CTable)
  557. {
  558. return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable);
  559. }
  560. static size_t
  561. HUF_compress1X_usingCTable_internal(void* dst, size_t dstSize,
  562. const void* src, size_t srcSize,
  563. const HUF_CElt* CTable, const int bmi2)
  564. {
  565. if (bmi2) {
  566. return HUF_compress1X_usingCTable_internal_bmi2(dst, dstSize, src, srcSize, CTable);
  567. }
  568. return HUF_compress1X_usingCTable_internal_default(dst, dstSize, src, srcSize, CTable);
  569. }
  570. #else
  571. static size_t
  572. HUF_compress1X_usingCTable_internal(void* dst, size_t dstSize,
  573. const void* src, size_t srcSize,
  574. const HUF_CElt* CTable, const int bmi2)
  575. {
  576. (void)bmi2;
  577. return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable);
  578. }
  579. #endif
  580. size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
  581. {
  582. return HUF_compress1X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, /* bmi2 */ 0);
  583. }
  584. static size_t
  585. HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize,
  586. const void* src, size_t srcSize,
  587. const HUF_CElt* CTable, int bmi2)
  588. {
  589. size_t const segmentSize = (srcSize+3)/4; /* first 3 segments */
  590. const BYTE* ip = (const BYTE*) src;
  591. const BYTE* const iend = ip + srcSize;
  592. BYTE* const ostart = (BYTE*) dst;
  593. BYTE* const oend = ostart + dstSize;
  594. BYTE* op = ostart;
  595. if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */
  596. if (srcSize < 12) return 0; /* no saving possible : too small input */
  597. op += 6; /* jumpTable */
  598. assert(op <= oend);
  599. { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) );
  600. if (cSize==0) return 0;
  601. assert(cSize <= 65535);
  602. MEM_writeLE16(ostart, (U16)cSize);
  603. op += cSize;
  604. }
  605. ip += segmentSize;
  606. assert(op <= oend);
  607. { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) );
  608. if (cSize==0) return 0;
  609. assert(cSize <= 65535);
  610. MEM_writeLE16(ostart+2, (U16)cSize);
  611. op += cSize;
  612. }
  613. ip += segmentSize;
  614. assert(op <= oend);
  615. { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) );
  616. if (cSize==0) return 0;
  617. assert(cSize <= 65535);
  618. MEM_writeLE16(ostart+4, (U16)cSize);
  619. op += cSize;
  620. }
  621. ip += segmentSize;
  622. assert(op <= oend);
  623. assert(ip <= iend);
  624. { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, (size_t)(iend-ip), CTable, bmi2) );
  625. if (cSize==0) return 0;
  626. op += cSize;
  627. }
  628. return (size_t)(op-ostart);
  629. }
  630. size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
  631. {
  632. return HUF_compress4X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, /* bmi2 */ 0);
  633. }
  634. typedef enum { HUF_singleStream, HUF_fourStreams } HUF_nbStreams_e;
  635. static size_t HUF_compressCTable_internal(
  636. BYTE* const ostart, BYTE* op, BYTE* const oend,
  637. const void* src, size_t srcSize,
  638. HUF_nbStreams_e nbStreams, const HUF_CElt* CTable, const int bmi2)
  639. {
  640. size_t const cSize = (nbStreams==HUF_singleStream) ?
  641. HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), src, srcSize, CTable, bmi2) :
  642. HUF_compress4X_usingCTable_internal(op, (size_t)(oend - op), src, srcSize, CTable, bmi2);
  643. if (HUF_isError(cSize)) { return cSize; }
  644. if (cSize==0) { return 0; } /* uncompressible */
  645. op += cSize;
  646. /* check compressibility */
  647. assert(op >= ostart);
  648. if ((size_t)(op-ostart) >= srcSize-1) { return 0; }
  649. return (size_t)(op-ostart);
  650. }
  651. typedef struct {
  652. unsigned count[HUF_SYMBOLVALUE_MAX + 1];
  653. HUF_CElt CTable[HUF_SYMBOLVALUE_MAX + 1];
  654. union {
  655. HUF_buildCTable_wksp_tables buildCTable_wksp;
  656. HUF_WriteCTableWksp writeCTable_wksp;
  657. } wksps;
  658. } HUF_compress_tables_t;
  659. /* HUF_compress_internal() :
  660. * `workSpace_align4` must be aligned on 4-bytes boundaries,
  661. * and occupies the same space as a table of HUF_WORKSPACE_SIZE_U32 unsigned */
  662. static size_t
  663. HUF_compress_internal (void* dst, size_t dstSize,
  664. const void* src, size_t srcSize,
  665. unsigned maxSymbolValue, unsigned huffLog,
  666. HUF_nbStreams_e nbStreams,
  667. void* workSpace_align4, size_t wkspSize,
  668. HUF_CElt* oldHufTable, HUF_repeat* repeat, int preferRepeat,
  669. const int bmi2)
  670. {
  671. HUF_compress_tables_t* const table = (HUF_compress_tables_t*)workSpace_align4;
  672. BYTE* const ostart = (BYTE*)dst;
  673. BYTE* const oend = ostart + dstSize;
  674. BYTE* op = ostart;
  675. HUF_STATIC_ASSERT(sizeof(*table) <= HUF_WORKSPACE_SIZE);
  676. assert(((size_t)workSpace_align4 & 3) == 0); /* must be aligned on 4-bytes boundaries */
  677. /* checks & inits */
  678. if (wkspSize < HUF_WORKSPACE_SIZE) return ERROR(workSpace_tooSmall);
  679. if (!srcSize) return 0; /* Uncompressed */
  680. if (!dstSize) return 0; /* cannot fit anything within dst budget */
  681. if (srcSize > HUF_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); /* current block size limit */
  682. if (huffLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
  683. if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge);
  684. if (!maxSymbolValue) maxSymbolValue = HUF_SYMBOLVALUE_MAX;
  685. if (!huffLog) huffLog = HUF_TABLELOG_DEFAULT;
  686. /* Heuristic : If old table is valid, use it for small inputs */
  687. if (preferRepeat && repeat && *repeat == HUF_repeat_valid) {
  688. return HUF_compressCTable_internal(ostart, op, oend,
  689. src, srcSize,
  690. nbStreams, oldHufTable, bmi2);
  691. }
  692. /* Scan input and build symbol stats */
  693. { CHECK_V_F(largest, HIST_count_wksp (table->count, &maxSymbolValue, (const BYTE*)src, srcSize, workSpace_align4, wkspSize) );
  694. if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; } /* single symbol, rle */
  695. if (largest <= (srcSize >> 7)+4) return 0; /* heuristic : probably not compressible enough */
  696. }
  697. /* Check validity of previous table */
  698. if ( repeat
  699. && *repeat == HUF_repeat_check
  700. && !HUF_validateCTable(oldHufTable, table->count, maxSymbolValue)) {
  701. *repeat = HUF_repeat_none;
  702. }
  703. /* Heuristic : use existing table for small inputs */
  704. if (preferRepeat && repeat && *repeat != HUF_repeat_none) {
  705. return HUF_compressCTable_internal(ostart, op, oend,
  706. src, srcSize,
  707. nbStreams, oldHufTable, bmi2);
  708. }
  709. /* Build Huffman Tree */
  710. huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
  711. { size_t const maxBits = HUF_buildCTable_wksp(table->CTable, table->count,
  712. maxSymbolValue, huffLog,
  713. &table->wksps.buildCTable_wksp, sizeof(table->wksps.buildCTable_wksp));
  714. CHECK_F(maxBits);
  715. huffLog = (U32)maxBits;
  716. /* Zero unused symbols in CTable, so we can check it for validity */
  717. ZSTD_memset(table->CTable + (maxSymbolValue + 1), 0,
  718. sizeof(table->CTable) - ((maxSymbolValue + 1) * sizeof(HUF_CElt)));
  719. }
  720. /* Write table description header */
  721. { CHECK_V_F(hSize, HUF_writeCTable_wksp(op, dstSize, table->CTable, maxSymbolValue, huffLog,
  722. &table->wksps.writeCTable_wksp, sizeof(table->wksps.writeCTable_wksp)) );
  723. /* Check if using previous huffman table is beneficial */
  724. if (repeat && *repeat != HUF_repeat_none) {
  725. size_t const oldSize = HUF_estimateCompressedSize(oldHufTable, table->count, maxSymbolValue);
  726. size_t const newSize = HUF_estimateCompressedSize(table->CTable, table->count, maxSymbolValue);
  727. if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) {
  728. return HUF_compressCTable_internal(ostart, op, oend,
  729. src, srcSize,
  730. nbStreams, oldHufTable, bmi2);
  731. } }
  732. /* Use the new huffman table */
  733. if (hSize + 12ul >= srcSize) { return 0; }
  734. op += hSize;
  735. if (repeat) { *repeat = HUF_repeat_none; }
  736. if (oldHufTable)
  737. ZSTD_memcpy(oldHufTable, table->CTable, sizeof(table->CTable)); /* Save new table */
  738. }
  739. return HUF_compressCTable_internal(ostart, op, oend,
  740. src, srcSize,
  741. nbStreams, table->CTable, bmi2);
  742. }
  743. size_t HUF_compress1X_wksp (void* dst, size_t dstSize,
  744. const void* src, size_t srcSize,
  745. unsigned maxSymbolValue, unsigned huffLog,
  746. void* workSpace, size_t wkspSize)
  747. {
  748. return HUF_compress_internal(dst, dstSize, src, srcSize,
  749. maxSymbolValue, huffLog, HUF_singleStream,
  750. workSpace, wkspSize,
  751. NULL, NULL, 0, 0 /*bmi2*/);
  752. }
  753. size_t HUF_compress1X_repeat (void* dst, size_t dstSize,
  754. const void* src, size_t srcSize,
  755. unsigned maxSymbolValue, unsigned huffLog,
  756. void* workSpace, size_t wkspSize,
  757. HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2)
  758. {
  759. return HUF_compress_internal(dst, dstSize, src, srcSize,
  760. maxSymbolValue, huffLog, HUF_singleStream,
  761. workSpace, wkspSize, hufTable,
  762. repeat, preferRepeat, bmi2);
  763. }
  764. /* HUF_compress4X_repeat():
  765. * compress input using 4 streams.
  766. * provide workspace to generate compression tables */
  767. size_t HUF_compress4X_wksp (void* dst, size_t dstSize,
  768. const void* src, size_t srcSize,
  769. unsigned maxSymbolValue, unsigned huffLog,
  770. void* workSpace, size_t wkspSize)
  771. {
  772. return HUF_compress_internal(dst, dstSize, src, srcSize,
  773. maxSymbolValue, huffLog, HUF_fourStreams,
  774. workSpace, wkspSize,
  775. NULL, NULL, 0, 0 /*bmi2*/);
  776. }
  777. /* HUF_compress4X_repeat():
  778. * compress input using 4 streams.
  779. * re-use an existing huffman compression table */
  780. size_t HUF_compress4X_repeat (void* dst, size_t dstSize,
  781. const void* src, size_t srcSize,
  782. unsigned maxSymbolValue, unsigned huffLog,
  783. void* workSpace, size_t wkspSize,
  784. HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2)
  785. {
  786. return HUF_compress_internal(dst, dstSize, src, srcSize,
  787. maxSymbolValue, huffLog, HUF_fourStreams,
  788. workSpace, wkspSize,
  789. hufTable, repeat, preferRepeat, bmi2);
  790. }
  791. #ifndef ZSTD_NO_UNUSED_FUNCTIONS
  792. /** HUF_buildCTable() :
  793. * @return : maxNbBits
  794. * Note : count is used before tree is written, so they can safely overlap
  795. */
  796. size_t HUF_buildCTable (HUF_CElt* tree, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits)
  797. {
  798. HUF_buildCTable_wksp_tables workspace;
  799. return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, &workspace, sizeof(workspace));
  800. }
  801. size_t HUF_compress1X (void* dst, size_t dstSize,
  802. const void* src, size_t srcSize,
  803. unsigned maxSymbolValue, unsigned huffLog)
  804. {
  805. unsigned workSpace[HUF_WORKSPACE_SIZE_U32];
  806. return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace));
  807. }
  808. size_t HUF_compress2 (void* dst, size_t dstSize,
  809. const void* src, size_t srcSize,
  810. unsigned maxSymbolValue, unsigned huffLog)
  811. {
  812. unsigned workSpace[HUF_WORKSPACE_SIZE_U32];
  813. return HUF_compress4X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace));
  814. }
  815. size_t HUF_compress (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
  816. {
  817. return HUF_compress2(dst, maxDstSize, src, srcSize, 255, HUF_TABLELOG_DEFAULT);
  818. }
  819. #endif