fse_static.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include <contrib/libs/zstd06/renames.h>
  2. /* ******************************************************************
  3. FSE : Finite State Entropy coder
  4. header file for static linking (only)
  5. Copyright (C) 2013-2015, Yann Collet
  6. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the following disclaimer
  14. in the documentation and/or other materials provided with the
  15. distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. You can contact the author at :
  28. - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  29. - Public forum : https://groups.google.com/forum/#!forum/lz4c
  30. ****************************************************************** */
  31. #ifndef FSE_STATIC_H
  32. #define FSE_STATIC_H
  33. #if defined (__cplusplus)
  34. extern "C" {
  35. #endif
  36. /* *****************************************
  37. * Dependencies
  38. *******************************************/
  39. #include "fse.h"
  40. #include "bitstream.h"
  41. /* *****************************************
  42. * Static allocation
  43. *******************************************/
  44. /* FSE buffer bounds */
  45. #define FSE_NCOUNTBOUND 512
  46. #define FSE_BLOCKBOUND(size) (size + (size>>7))
  47. #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
  48. /* It is possible to statically allocate FSE CTable/DTable as a table of unsigned using below macros */
  49. #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
  50. #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
  51. /* *****************************************
  52. * FSE advanced API
  53. *******************************************/
  54. size_t FSE_countFast(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
  55. /* same as FSE_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr */
  56. size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits);
  57. /* build a fake FSE_CTable, designed to not compress an input, where each symbol uses nbBits */
  58. size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
  59. /* build a fake FSE_CTable, designed to compress always the same symbolValue */
  60. size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
  61. /* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
  62. size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
  63. /* build a fake FSE_DTable, designed to always generate the same symbolValue */
  64. /* *****************************************
  65. * FSE symbol compression API
  66. *******************************************/
  67. /*!
  68. This API consists of small unitary functions, which highly benefit from being inlined.
  69. You will want to enable link-time-optimization to ensure these functions are properly inlined in your binary.
  70. Visual seems to do it automatically.
  71. For gcc or clang, you'll need to add -flto flag at compilation and linking stages.
  72. If none of these solutions is applicable, include "fse.c" directly.
  73. */
  74. typedef struct
  75. {
  76. ptrdiff_t value;
  77. const void* stateTable;
  78. const void* symbolTT;
  79. unsigned stateLog;
  80. } FSE_CState_t;
  81. static void FSE_initCState(FSE_CState_t* CStatePtr, const FSE_CTable* ct);
  82. static void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* CStatePtr, unsigned symbol);
  83. static void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* CStatePtr);
  84. /*!
  85. These functions are inner components of FSE_compress_usingCTable().
  86. They allow the creation of custom streams, mixing multiple tables and bit sources.
  87. A key property to keep in mind is that encoding and decoding are done **in reverse direction**.
  88. So the first symbol you will encode is the last you will decode, like a LIFO stack.
  89. You will need a few variables to track your CStream. They are :
  90. FSE_CTable ct; // Provided by FSE_buildCTable()
  91. BIT_CStream_t bitStream; // bitStream tracking structure
  92. FSE_CState_t state; // State tracking structure (can have several)
  93. The first thing to do is to init bitStream and state.
  94. size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize);
  95. FSE_initCState(&state, ct);
  96. Note that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError();
  97. You can then encode your input data, byte after byte.
  98. FSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time.
  99. Remember decoding will be done in reverse direction.
  100. FSE_encodeByte(&bitStream, &state, symbol);
  101. At any time, you can also add any bit sequence.
  102. Note : maximum allowed nbBits is 25, for compatibility with 32-bits decoders
  103. BIT_addBits(&bitStream, bitField, nbBits);
  104. The above methods don't commit data to memory, they just store it into local register, for speed.
  105. Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
  106. Writing data to memory is a manual operation, performed by the flushBits function.
  107. BIT_flushBits(&bitStream);
  108. Your last FSE encoding operation shall be to flush your last state value(s).
  109. FSE_flushState(&bitStream, &state);
  110. Finally, you must close the bitStream.
  111. The function returns the size of CStream in bytes.
  112. If data couldn't fit into dstBuffer, it will return a 0 ( == not compressible)
  113. If there is an error, it returns an errorCode (which can be tested using FSE_isError()).
  114. size_t size = BIT_closeCStream(&bitStream);
  115. */
  116. /* *****************************************
  117. * FSE symbol decompression API
  118. *******************************************/
  119. typedef struct
  120. {
  121. size_t state;
  122. const void* table; /* precise table may vary, depending on U16 */
  123. } FSE_DState_t;
  124. static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);
  125. static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
  126. static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
  127. /*!
  128. Let's now decompose FSE_decompress_usingDTable() into its unitary components.
  129. You will decode FSE-encoded symbols from the bitStream,
  130. and also any other bitFields you put in, **in reverse order**.
  131. You will need a few variables to track your bitStream. They are :
  132. BIT_DStream_t DStream; // Stream context
  133. FSE_DState_t DState; // State context. Multiple ones are possible
  134. FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable()
  135. The first thing to do is to init the bitStream.
  136. errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
  137. You should then retrieve your initial state(s)
  138. (in reverse flushing order if you have several ones) :
  139. errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
  140. You can then decode your data, symbol after symbol.
  141. For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
  142. Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
  143. unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
  144. You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
  145. Note : maximum allowed nbBits is 25, for 32-bits compatibility
  146. size_t bitField = BIT_readBits(&DStream, nbBits);
  147. All above operations only read from local register (which size depends on size_t).
  148. Refueling the register from memory is manually performed by the reload method.
  149. endSignal = FSE_reloadDStream(&DStream);
  150. BIT_reloadDStream() result tells if there is still some more data to read from DStream.
  151. BIT_DStream_unfinished : there is still some data left into the DStream.
  152. BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
  153. BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
  154. BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
  155. When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
  156. to properly detect the exact end of stream.
  157. After each decoded symbol, check if DStream is fully consumed using this simple test :
  158. BIT_reloadDStream(&DStream) >= BIT_DStream_completed
  159. When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
  160. Checking if DStream has reached its end is performed by :
  161. BIT_endOfDStream(&DStream);
  162. Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
  163. FSE_endOfDState(&DState);
  164. */
  165. /* *****************************************
  166. * FSE unsafe API
  167. *******************************************/
  168. static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
  169. /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
  170. /* *****************************************
  171. * Implementation of inlined functions
  172. *******************************************/
  173. typedef struct {
  174. int deltaFindState;
  175. U32 deltaNbBits;
  176. } FSE_symbolCompressionTransform; /* total 8 bytes */
  177. MEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct)
  178. {
  179. const void* ptr = ct;
  180. const U16* u16ptr = (const U16*) ptr;
  181. const U32 tableLog = MEM_read16(ptr);
  182. statePtr->value = (ptrdiff_t)1<<tableLog;
  183. statePtr->stateTable = u16ptr+2;
  184. statePtr->symbolTT = ((const U32*)ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1));
  185. statePtr->stateLog = tableLog;
  186. }
  187. MEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U32 symbol)
  188. {
  189. FSE_initCState(statePtr, ct);
  190. {
  191. const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
  192. const U16* stateTable = (const U16*)(statePtr->stateTable);
  193. U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1<<15)) >> 16);
  194. statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits;
  195. statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
  196. }
  197. }
  198. MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, U32 symbol)
  199. {
  200. const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
  201. const U16* const stateTable = (const U16*)(statePtr->stateTable);
  202. U32 nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);
  203. BIT_addBits(bitC, statePtr->value, nbBitsOut);
  204. statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
  205. }
  206. MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePtr)
  207. {
  208. BIT_addBits(bitC, statePtr->value, statePtr->stateLog);
  209. BIT_flushBits(bitC);
  210. }
  211. /*<===== Decompression =====>*/
  212. typedef struct {
  213. U16 tableLog;
  214. U16 fastMode;
  215. } FSE_DTableHeader; /* sizeof U32 */
  216. typedef struct
  217. {
  218. unsigned short newState;
  219. unsigned char symbol;
  220. unsigned char nbBits;
  221. } FSE_decode_t; /* size == U32 */
  222. MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)
  223. {
  224. const void* ptr = dt;
  225. const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr;
  226. DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);
  227. BIT_reloadDStream(bitD);
  228. DStatePtr->table = dt + 1;
  229. }
  230. MEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr)
  231. {
  232. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  233. return DInfo.symbol;
  234. }
  235. MEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
  236. {
  237. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  238. U32 const nbBits = DInfo.nbBits;
  239. size_t const lowBits = BIT_readBits(bitD, nbBits);
  240. DStatePtr->state = DInfo.newState + lowBits;
  241. }
  242. MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
  243. {
  244. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  245. U32 const nbBits = DInfo.nbBits;
  246. BYTE const symbol = DInfo.symbol;
  247. size_t const lowBits = BIT_readBits(bitD, nbBits);
  248. DStatePtr->state = DInfo.newState + lowBits;
  249. return symbol;
  250. }
  251. /*! FSE_decodeSymbolFast() :
  252. unsafe, only works if no symbol has a probability > 50% */
  253. MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
  254. {
  255. FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
  256. U32 const nbBits = DInfo.nbBits;
  257. BYTE const symbol = DInfo.symbol;
  258. size_t const lowBits = BIT_readBitsFast(bitD, nbBits);
  259. DStatePtr->state = DInfo.newState + lowBits;
  260. return symbol;
  261. }
  262. MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
  263. {
  264. return DStatePtr->state == 0;
  265. }
  266. #ifndef FSE_COMMONDEFS_ONLY
  267. /* **************************************************************
  268. * Tuning parameters
  269. ****************************************************************/
  270. /*!MEMORY_USAGE :
  271. * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  272. * Increasing memory usage improves compression ratio
  273. * Reduced memory usage can improve speed, due to cache effect
  274. * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
  275. #define FSE_MAX_MEMORY_USAGE 14
  276. #define FSE_DEFAULT_MEMORY_USAGE 13
  277. /*!FSE_MAX_SYMBOL_VALUE :
  278. * Maximum symbol value authorized.
  279. * Required for proper stack allocation */
  280. #define FSE_MAX_SYMBOL_VALUE 255
  281. /* **************************************************************
  282. * template functions type & suffix
  283. ****************************************************************/
  284. #define FSE_FUNCTION_TYPE BYTE
  285. #define FSE_FUNCTION_EXTENSION
  286. #define FSE_DECODE_TYPE FSE_decode_t
  287. #endif /* !FSE_COMMONDEFS_ONLY */
  288. /* ***************************************************************
  289. * Constants
  290. *****************************************************************/
  291. #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
  292. #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
  293. #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
  294. #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
  295. #define FSE_MIN_TABLELOG 5
  296. #define FSE_TABLELOG_ABSOLUTE_MAX 15
  297. #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
  298. #error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
  299. #endif
  300. #define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3)
  301. #if defined (__cplusplus)
  302. }
  303. #endif
  304. #endif /* FSE_STATIC_H */