You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fse_decompress.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* ******************************************************************
  2. * FSE : Finite State Entropy decoder
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. *
  5. * You can contact the author at :
  6. * - FSE 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. * Includes
  16. ****************************************************************/
  17. #include "debug.h" /* assert */
  18. #include "bitstream.h"
  19. #include "compiler.h"
  20. #define FSE_STATIC_LINKING_ONLY
  21. #include "fse.h"
  22. #include "error_private.h"
  23. #define ZSTD_DEPS_NEED_MALLOC
  24. #include "zstd_deps.h"
  25. #include "bits.h" /* ZSTD_highbit32 */
  26. /* **************************************************************
  27. * Error Management
  28. ****************************************************************/
  29. #define FSE_isError ERR_isError
  30. #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
  31. /* **************************************************************
  32. * Templates
  33. ****************************************************************/
  34. /*
  35. designed to be included
  36. for type-specific functions (template emulation in C)
  37. Objective is to write these functions only once, for improved maintenance
  38. */
  39. /* safety checks */
  40. #ifndef FSE_FUNCTION_EXTENSION
  41. # error "FSE_FUNCTION_EXTENSION must be defined"
  42. #endif
  43. #ifndef FSE_FUNCTION_TYPE
  44. # error "FSE_FUNCTION_TYPE must be defined"
  45. #endif
  46. /* Function names */
  47. #define FSE_CAT(X,Y) X##Y
  48. #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
  49. #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
  50. static size_t FSE_buildDTable_internal(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
  51. {
  52. void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
  53. FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
  54. U16* symbolNext = (U16*)workSpace;
  55. BYTE* spread = (BYTE*)(symbolNext + maxSymbolValue + 1);
  56. U32 const maxSV1 = maxSymbolValue + 1;
  57. U32 const tableSize = 1 << tableLog;
  58. U32 highThreshold = tableSize-1;
  59. /* Sanity Checks */
  60. if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(maxSymbolValue_tooLarge);
  61. if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
  62. if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
  63. /* Init, lay down lowprob symbols */
  64. { FSE_DTableHeader DTableH;
  65. DTableH.tableLog = (U16)tableLog;
  66. DTableH.fastMode = 1;
  67. { S16 const largeLimit= (S16)(1 << (tableLog-1));
  68. U32 s;
  69. for (s=0; s<maxSV1; s++) {
  70. if (normalizedCounter[s]==-1) {
  71. tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
  72. symbolNext[s] = 1;
  73. } else {
  74. if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
  75. symbolNext[s] = normalizedCounter[s];
  76. } } }
  77. ZSTD_memcpy(dt, &DTableH, sizeof(DTableH));
  78. }
  79. /* Spread symbols */
  80. if (highThreshold == tableSize - 1) {
  81. size_t const tableMask = tableSize-1;
  82. size_t const step = FSE_TABLESTEP(tableSize);
  83. /* First lay down the symbols in order.
  84. * We use a uint64_t to lay down 8 bytes at a time. This reduces branch
  85. * misses since small blocks generally have small table logs, so nearly
  86. * all symbols have counts <= 8. We ensure we have 8 bytes at the end of
  87. * our buffer to handle the over-write.
  88. */
  89. {
  90. U64 const add = 0x0101010101010101ull;
  91. size_t pos = 0;
  92. U64 sv = 0;
  93. U32 s;
  94. for (s=0; s<maxSV1; ++s, sv += add) {
  95. int i;
  96. int const n = normalizedCounter[s];
  97. MEM_write64(spread + pos, sv);
  98. for (i = 8; i < n; i += 8) {
  99. MEM_write64(spread + pos + i, sv);
  100. }
  101. pos += n;
  102. }
  103. }
  104. /* Now we spread those positions across the table.
  105. * The benefit of doing it in two stages is that we avoid the
  106. * variable size inner loop, which caused lots of branch misses.
  107. * Now we can run through all the positions without any branch misses.
  108. * We unroll the loop twice, since that is what empirically worked best.
  109. */
  110. {
  111. size_t position = 0;
  112. size_t s;
  113. size_t const unroll = 2;
  114. assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */
  115. for (s = 0; s < (size_t)tableSize; s += unroll) {
  116. size_t u;
  117. for (u = 0; u < unroll; ++u) {
  118. size_t const uPosition = (position + (u * step)) & tableMask;
  119. tableDecode[uPosition].symbol = spread[s + u];
  120. }
  121. position = (position + (unroll * step)) & tableMask;
  122. }
  123. assert(position == 0);
  124. }
  125. } else {
  126. U32 const tableMask = tableSize-1;
  127. U32 const step = FSE_TABLESTEP(tableSize);
  128. U32 s, position = 0;
  129. for (s=0; s<maxSV1; s++) {
  130. int i;
  131. for (i=0; i<normalizedCounter[s]; i++) {
  132. tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
  133. position = (position + step) & tableMask;
  134. while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
  135. } }
  136. if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  137. }
  138. /* Build Decoding table */
  139. { U32 u;
  140. for (u=0; u<tableSize; u++) {
  141. FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
  142. U32 const nextState = symbolNext[symbol]++;
  143. tableDecode[u].nbBits = (BYTE) (tableLog - ZSTD_highbit32(nextState) );
  144. tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
  145. } }
  146. return 0;
  147. }
  148. size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
  149. {
  150. return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize);
  151. }
  152. #ifndef FSE_COMMONDEFS_ONLY
  153. /*-*******************************************************
  154. * Decompression (Byte symbols)
  155. *********************************************************/
  156. FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(
  157. void* dst, size_t maxDstSize,
  158. const void* cSrc, size_t cSrcSize,
  159. const FSE_DTable* dt, const unsigned fast)
  160. {
  161. BYTE* const ostart = (BYTE*) dst;
  162. BYTE* op = ostart;
  163. BYTE* const omax = op + maxDstSize;
  164. BYTE* const olimit = omax-3;
  165. BIT_DStream_t bitD;
  166. FSE_DState_t state1;
  167. FSE_DState_t state2;
  168. /* Init */
  169. CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
  170. FSE_initDState(&state1, &bitD, dt);
  171. FSE_initDState(&state2, &bitD, dt);
  172. #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
  173. /* 4 symbols per loop */
  174. for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {
  175. op[0] = FSE_GETSYMBOL(&state1);
  176. if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  177. BIT_reloadDStream(&bitD);
  178. op[1] = FSE_GETSYMBOL(&state2);
  179. if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  180. { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
  181. op[2] = FSE_GETSYMBOL(&state1);
  182. if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
  183. BIT_reloadDStream(&bitD);
  184. op[3] = FSE_GETSYMBOL(&state2);
  185. }
  186. /* tail */
  187. /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
  188. while (1) {
  189. if (op>(omax-2)) return ERROR(dstSize_tooSmall);
  190. *op++ = FSE_GETSYMBOL(&state1);
  191. if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
  192. *op++ = FSE_GETSYMBOL(&state2);
  193. break;
  194. }
  195. if (op>(omax-2)) return ERROR(dstSize_tooSmall);
  196. *op++ = FSE_GETSYMBOL(&state2);
  197. if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
  198. *op++ = FSE_GETSYMBOL(&state1);
  199. break;
  200. } }
  201. return op-ostart;
  202. }
  203. typedef struct {
  204. short ncount[FSE_MAX_SYMBOL_VALUE + 1];
  205. FSE_DTable dtable[1]; /* Dynamically sized */
  206. } FSE_DecompressWksp;
  207. FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body(
  208. void* dst, size_t dstCapacity,
  209. const void* cSrc, size_t cSrcSize,
  210. unsigned maxLog, void* workSpace, size_t wkspSize,
  211. int bmi2)
  212. {
  213. const BYTE* const istart = (const BYTE*)cSrc;
  214. const BYTE* ip = istart;
  215. unsigned tableLog;
  216. unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
  217. FSE_DecompressWksp* const wksp = (FSE_DecompressWksp*)workSpace;
  218. DEBUG_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0);
  219. if (wkspSize < sizeof(*wksp)) return ERROR(GENERIC);
  220. /* normal FSE decoding mode */
  221. {
  222. size_t const NCountLength = FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2);
  223. if (FSE_isError(NCountLength)) return NCountLength;
  224. if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
  225. assert(NCountLength <= cSrcSize);
  226. ip += NCountLength;
  227. cSrcSize -= NCountLength;
  228. }
  229. if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge);
  230. assert(sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog) <= wkspSize);
  231. workSpace = (BYTE*)workSpace + sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);
  232. wkspSize -= sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog);
  233. CHECK_F( FSE_buildDTable_internal(wksp->dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize) );
  234. {
  235. const void* ptr = wksp->dtable;
  236. const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;
  237. const U32 fastMode = DTableH->fastMode;
  238. /* select fast mode (static) */
  239. if (fastMode) return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 1);
  240. return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 0);
  241. }
  242. }
  243. /* Avoids the FORCE_INLINE of the _body() function. */
  244. static size_t FSE_decompress_wksp_body_default(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)
  245. {
  246. return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0);
  247. }
  248. #if DYNAMIC_BMI2
  249. BMI2_TARGET_ATTRIBUTE static size_t FSE_decompress_wksp_body_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)
  250. {
  251. return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1);
  252. }
  253. #endif
  254. size_t FSE_decompress_wksp_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize, int bmi2)
  255. {
  256. #if DYNAMIC_BMI2
  257. if (bmi2) {
  258. return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
  259. }
  260. #endif
  261. (void)bmi2;
  262. return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
  263. }
  264. #endif /* FSE_COMMONDEFS_ONLY */