lizard_common.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. Lizard - Fast LZ compression algorithm
  3. Copyright (C) 2011-2015, Yann Collet
  4. Copyright (C) 2016-2017, Przemyslaw Skibinski <[email protected]>
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - Lizard source repository : https://github.com/inikep/lizard
  28. */
  29. #ifndef LIZARD_COMMON_H_2983
  30. #define LIZARD_COMMON_H_2983
  31. #if defined (__cplusplus)
  32. extern "C" {
  33. #endif
  34. /*-************************************
  35. * Memory routines
  36. **************************************/
  37. #include <stdlib.h> /* malloc, calloc, free */
  38. #include <string.h> /* memset, memcpy */
  39. #include <stdint.h> /* intptr_t */
  40. #include "entropy/mem.h"
  41. #include "lizard_compress.h" /* LIZARD_GCC_VERSION */
  42. //#define LIZARD_USE_LOGS
  43. #define LIZARD_LOG_COMPRESS(...) //printf(__VA_ARGS__)
  44. #define LIZARD_LOG_DECOMPRESS(...) //printf(__VA_ARGS__)
  45. #define LIZARD_LOG_COMPRESS_LZ4(...) //printf(__VA_ARGS__)
  46. #define COMPLOG_CODEWORDS_LZ4(...) //printf(__VA_ARGS__)
  47. #define LIZARD_LOG_DECOMPRESS_LZ4(...) //printf(__VA_ARGS__)
  48. #define DECOMPLOG_CODEWORDS_LZ4(...) //printf(__VA_ARGS__)
  49. #define LIZARD_LOG_COMPRESS_LIZv1(...) //printf(__VA_ARGS__)
  50. #define COMPLOG_CODEWORDS_LIZv1(...) //printf(__VA_ARGS__)
  51. #define LIZARD_LOG_DECOMPRESS_LIZv1(...) //printf(__VA_ARGS__)
  52. #define DECOMPLOG_CODEWORDS_LIZv1(...) //printf(__VA_ARGS__)
  53. /*-************************************
  54. * Common Constants
  55. **************************************/
  56. #define MINMATCH 4
  57. //#define USE_LZ4_ONLY
  58. //#define LIZARD_USE_TEST
  59. #define LIZARD_DICT_SIZE (1<<24)
  60. #define WILDCOPYLENGTH 16
  61. #define LASTLITERALS WILDCOPYLENGTH
  62. #define MFLIMIT (WILDCOPYLENGTH+MINMATCH)
  63. #define LIZARD_MAX_PRICE (1<<28)
  64. #define LIZARD_INIT_LAST_OFFSET 0
  65. #define LIZARD_MAX_16BIT_OFFSET (1<<16)
  66. #define MM_LONGOFF 16
  67. #define LIZARD_BLOCK_SIZE_PAD (LIZARD_BLOCK_SIZE+32)
  68. #define LIZARD_COMPRESS_ADD_BUF (5*LIZARD_BLOCK_SIZE_PAD)
  69. #ifndef LIZARD_NO_HUFFMAN
  70. #define LIZARD_COMPRESS_ADD_HUF HUF_compressBound(LIZARD_BLOCK_SIZE_PAD)
  71. #define LIZARD_HUF_BLOCK_SIZE LIZARD_BLOCK_SIZE
  72. #else
  73. #define LIZARD_COMPRESS_ADD_HUF 0
  74. #define LIZARD_HUF_BLOCK_SIZE 1
  75. #endif
  76. /* LZ4 codewords */
  77. #define ML_BITS_LZ4 4
  78. #define ML_MASK_LZ4 ((1U<<ML_BITS_LZ4)-1)
  79. #define RUN_BITS_LZ4 (8-ML_BITS_LZ4)
  80. #define RUN_MASK_LZ4 ((1U<<RUN_BITS_LZ4)-1)
  81. /* LIZv1 codewords */
  82. #define ML_BITS_LIZv1 4
  83. #define RUN_BITS_LIZv1 3
  84. #define ML_RUN_BITS (ML_BITS_LIZv1 + RUN_BITS_LIZv1)
  85. #define MAX_SHORT_LITLEN 7
  86. #define MAX_SHORT_MATCHLEN 15
  87. #define LIZARD_LAST_LONG_OFF 31
  88. /* header byte */
  89. #define LIZARD_FLAG_LITERALS 1
  90. #define LIZARD_FLAG_FLAGS 2
  91. #define LIZARD_FLAG_OFFSET16 4
  92. #define LIZARD_FLAG_OFFSET24 8
  93. #define LIZARD_FLAG_LEN 16
  94. #define LIZARD_FLAG_UNCOMPRESSED 128
  95. /* stream numbers */
  96. #define LIZARD_STREAM_LITERALS 0
  97. #define LIZARD_STREAM_FLAGS 1
  98. #define LIZARD_STREAM_OFFSET16 2
  99. #define LIZARD_STREAM_OFFSET24 3
  100. #define LIZARD_STREAM_LEN 4
  101. #define LIZARD_STREAM_UNCOMPRESSED 5
  102. typedef enum { Lizard_parser_fastSmall, Lizard_parser_fast, Lizard_parser_fastBig, Lizard_parser_noChain, Lizard_parser_hashChain, Lizard_parser_priceFast, Lizard_parser_lowestPrice, Lizard_parser_optimalPrice, Lizard_parser_optimalPriceBT } Lizard_parser_type; /* from faster to stronger */
  103. typedef enum { Lizard_coderwords_LZ4, Lizard_coderwords_LIZv1 } Lizard_decompress_type;
  104. typedef struct
  105. {
  106. U32 windowLog; /* largest match distance : impact decompression buffer size */
  107. U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */
  108. U32 hashLog; /* dispatch table : larger == more memory, faster*/
  109. U32 hashLog3; /* dispatch table : larger == more memory, faster*/
  110. U32 searchNum; /* nb of searches : larger == more compression, slower*/
  111. U32 searchLength; /* size of matches : larger == faster decompression */
  112. U32 minMatchLongOff; /* min match size with offsets >= 1<<16 */
  113. U32 sufficientLength; /* used only by optimal parser: size of matches which is acceptable: larger == more compression, slower */
  114. U32 fullSearch; /* used only by optimal parser: perform full search of matches: 1 == more compression, slower */
  115. Lizard_parser_type parserType;
  116. Lizard_decompress_type decompressType;
  117. } Lizard_parameters;
  118. struct Lizard_stream_s
  119. {
  120. const BYTE* end; /* next block here to continue on current prefix */
  121. const BYTE* base; /* All index relative to this position */
  122. const BYTE* dictBase; /* alternate base for extDict */
  123. U32 dictLimit; /* below that point, need extDict */
  124. U32 lowLimit; /* below that point, no more dict */
  125. U32 nextToUpdate; /* index from which to continue dictionary update */
  126. U32 allocatedMemory;
  127. int compressionLevel;
  128. Lizard_parameters params;
  129. U32 hashTableSize;
  130. U32 chainTableSize;
  131. U32* chainTable;
  132. U32* hashTable;
  133. int last_off;
  134. const BYTE* off24pos;
  135. U32 huffType;
  136. U32 comprStreamLen;
  137. BYTE* huffBase;
  138. BYTE* huffEnd;
  139. BYTE* offset16Base;
  140. BYTE* offset24Base;
  141. BYTE* lenBase;
  142. BYTE* literalsBase;
  143. BYTE* flagsBase;
  144. BYTE* offset16Ptr;
  145. BYTE* offset24Ptr;
  146. BYTE* lenPtr;
  147. BYTE* literalsPtr;
  148. BYTE* flagsPtr;
  149. BYTE* offset16End;
  150. BYTE* offset24End;
  151. BYTE* lenEnd;
  152. BYTE* literalsEnd;
  153. BYTE* flagsEnd;
  154. U32 flagFreq[256];
  155. U32 litFreq[256];
  156. U32 litSum, flagSum;
  157. U32 litPriceSum, log2LitSum, log2FlagSum;
  158. U32 cachedPrice;
  159. U32 cachedLitLength;
  160. const BYTE* cachedLiterals;
  161. const BYTE* diffBase;
  162. const BYTE* srcBase;
  163. const BYTE* destBase;
  164. };
  165. struct Lizard_dstream_s
  166. {
  167. const BYTE* offset16Ptr;
  168. const BYTE* offset24Ptr;
  169. const BYTE* lenPtr;
  170. const BYTE* literalsPtr;
  171. const BYTE* flagsPtr;
  172. const BYTE* offset16End;
  173. const BYTE* offset24End;
  174. const BYTE* lenEnd;
  175. const BYTE* literalsEnd;
  176. const BYTE* flagsEnd;
  177. const BYTE* diffBase;
  178. intptr_t last_off;
  179. };
  180. typedef struct Lizard_dstream_s Lizard_dstream_t;
  181. /* *************************************
  182. * HC Pre-defined compression levels
  183. ***************************************/
  184. #define LIZARD_WINDOWLOG_LZ4 16
  185. #define LIZARD_CHAINLOG_LZ4 LIZARD_WINDOWLOG_LZ4
  186. #define LIZARD_HASHLOG_LZ4 18
  187. #define LIZARD_HASHLOG_LZ4SM 12
  188. #define LIZARD_WINDOWLOG_LIZv1 22
  189. #define LIZARD_CHAINLOG_LIZv1 LIZARD_WINDOWLOG_LIZv1
  190. #define LIZARD_HASHLOG_LIZv1 18
  191. static const Lizard_parameters Lizard_defaultParameters[LIZARD_MAX_CLEVEL+1-LIZARD_MIN_CLEVEL] =
  192. {
  193. /* windLog, contentLog, HashLog, H3, Snum, SL, MMLongOff, SuffL, FS, Parser function, Decompressor type */
  194. { LIZARD_WINDOWLOG_LZ4, 0, LIZARD_HASHLOG_LZ4SM, 0, 0, 0, 0, 0, 0, Lizard_parser_fastSmall, Lizard_coderwords_LZ4 }, // level 10
  195. { LIZARD_WINDOWLOG_LZ4, 0, LIZARD_HASHLOG_LZ4, 0, 0, 0, 0, 0, 0, Lizard_parser_fast, Lizard_coderwords_LZ4 }, // level 11
  196. { LIZARD_WINDOWLOG_LZ4, 0, LIZARD_HASHLOG_LZ4, 0, 0, 0, 0, 0, 0, Lizard_parser_noChain, Lizard_coderwords_LZ4 }, // level 12
  197. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 2, 5, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 13
  198. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 4, 5, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 14
  199. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 8, 5, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 15
  200. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 16, 4, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 16
  201. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 256, 4, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 17
  202. { LIZARD_WINDOWLOG_LZ4, LIZARD_WINDOWLOG_LZ4+1, LIZARD_HASHLOG_LZ4, 16, 16, 4, 0, 1<<10, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LZ4 }, // level 18
  203. { LIZARD_WINDOWLOG_LZ4, LIZARD_WINDOWLOG_LZ4+1, 23, 16, 256, 4, 0, 1<<10, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LZ4 }, // level 19
  204. /* windLog, contentLog, HashLog, H3, Snum, SL, MMLongOff, SuffL, FS, Parser function, Decompressor type */
  205. { LIZARD_WINDOWLOG_LIZv1, 0, 14, 0, 1, 5, MM_LONGOFF, 0, 0, Lizard_parser_fastBig, Lizard_coderwords_LIZv1 }, // level 20
  206. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 14, 13, 1, 5, MM_LONGOFF, 0, 0, Lizard_parser_priceFast, Lizard_coderwords_LIZv1 }, // level 21
  207. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, LIZARD_HASHLOG_LIZv1, 13, 1, 5, MM_LONGOFF, 0, 0, Lizard_parser_priceFast, Lizard_coderwords_LIZv1 }, // level 22
  208. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, LIZARD_HASHLOG_LIZv1, 13, 1, 5, MM_LONGOFF, 64, 0, Lizard_parser_lowestPrice, Lizard_coderwords_LIZv1 }, // level 23
  209. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 23, 16, 2, 5, MM_LONGOFF, 64, 0, Lizard_parser_lowestPrice, Lizard_coderwords_LIZv1 }, // level 24
  210. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 23, 16, 8, 4, MM_LONGOFF, 64, 0, Lizard_parser_lowestPrice, Lizard_coderwords_LIZv1 }, // level 25
  211. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1+1, 23, 16, 8, 4, MM_LONGOFF, 64, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 26
  212. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1+1, 23, 16, 128, 4, MM_LONGOFF, 64, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 27
  213. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1+1, 23, 24, 1<<10, 4, MM_LONGOFF, 1<<10, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 28
  214. { 24, 25, 23, 24, 1<<10, 4, MM_LONGOFF, 1<<10, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 29
  215. #ifndef LIZARD_NO_HUFFMAN
  216. /* windLog, contentLog, HashLog, H3, Snum, SL, MMLongOff, SuffL, FS, Parser function, Decompressor type */
  217. { LIZARD_WINDOWLOG_LZ4, 0, LIZARD_HASHLOG_LZ4SM, 0, 0, 0, 0, 0, 0, Lizard_parser_fastSmall, Lizard_coderwords_LZ4 }, // level 30
  218. { LIZARD_WINDOWLOG_LZ4, 0, LIZARD_HASHLOG_LZ4, 0, 0, 0, 0, 0, 0, Lizard_parser_fast, Lizard_coderwords_LZ4 }, // level 31
  219. { LIZARD_WINDOWLOG_LZ4, 0, 14, 0, 0, 0, 0, 0, 0, Lizard_parser_noChain, Lizard_coderwords_LZ4 }, // level 32
  220. { LIZARD_WINDOWLOG_LZ4, 0, LIZARD_HASHLOG_LZ4, 0, 0, 0, 0, 0, 0, Lizard_parser_noChain, Lizard_coderwords_LZ4 }, // level 33
  221. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 2, 5, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 34
  222. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 4, 5, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 35
  223. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 8, 5, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 36
  224. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 16, 4, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 37
  225. { LIZARD_WINDOWLOG_LZ4, LIZARD_CHAINLOG_LZ4, LIZARD_HASHLOG_LZ4, 0, 256, 4, 0, 0, 0, Lizard_parser_hashChain, Lizard_coderwords_LZ4 }, // level 38
  226. { LIZARD_WINDOWLOG_LZ4, LIZARD_WINDOWLOG_LZ4+1, 23, 16, 256, 4, 0, 1<<10, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LZ4 }, // level 39
  227. /* windLog, contentLog, HashLog, H3, Snum, SL, MMLongOff, SuffL, FS, Parser function, Decompressor type */
  228. { LIZARD_WINDOWLOG_LIZv1, 0, 14, 0, 1, 5, MM_LONGOFF, 0, 0, Lizard_parser_fastBig, Lizard_coderwords_LIZv1 }, // level 40
  229. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 14, 13, 1, 5, MM_LONGOFF, 0, 0, Lizard_parser_priceFast, Lizard_coderwords_LIZv1 }, // level 41
  230. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, LIZARD_HASHLOG_LIZv1, 13, 1, 5, MM_LONGOFF, 0, 0, Lizard_parser_priceFast, Lizard_coderwords_LIZv1 }, // level 42
  231. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, LIZARD_HASHLOG_LIZv1, 13, 1, 5, MM_LONGOFF, 64, 0, Lizard_parser_lowestPrice, Lizard_coderwords_LIZv1 }, // level 43
  232. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 23, 16, 2, 5, MM_LONGOFF, 64, 0, Lizard_parser_lowestPrice, Lizard_coderwords_LIZv1 }, // level 44
  233. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 23, 16, 8, 4, MM_LONGOFF, 64, 0, Lizard_parser_lowestPrice, Lizard_coderwords_LIZv1 }, // level 45
  234. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1, 23, 16, 8, 4, MM_LONGOFF, 64, 0, Lizard_parser_optimalPrice, Lizard_coderwords_LIZv1 }, // level 46
  235. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1+1, 23, 16, 8, 4, MM_LONGOFF, 64, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 47
  236. { LIZARD_WINDOWLOG_LIZv1, LIZARD_CHAINLOG_LIZv1+1, 23, 16, 128, 4, MM_LONGOFF, 64, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 48
  237. { 24, 25, 23, 24, 1<<10, 4, MM_LONGOFF, 1<<10, 1, Lizard_parser_optimalPriceBT, Lizard_coderwords_LIZv1 }, // level 49
  238. #endif
  239. // { 10, 10, 10, 0, 0, 4, 0, 0, 0, Lizard_fast }, // min values
  240. // { 24, 24, 28, 24, 1<<24, 7, 0, 1<<24, 2, Lizard_optimal_price }, // max values
  241. };
  242. /*-************************************
  243. * Compiler Options
  244. **************************************/
  245. #ifdef _MSC_VER /* Visual Studio */
  246. # define FORCE_INLINE static __forceinline
  247. # include <intrin.h>
  248. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  249. # pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */
  250. #else
  251. # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
  252. # if defined(__GNUC__) || defined(__clang__)
  253. # define FORCE_INLINE static inline __attribute__((always_inline))
  254. # else
  255. # define FORCE_INLINE static inline
  256. # endif
  257. # else
  258. # define FORCE_INLINE static
  259. # endif /* __STDC_VERSION__ */
  260. #endif /* _MSC_VER */
  261. #define LIZARD_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  262. #if (LIZARD_GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
  263. # define expect(expr,value) (__builtin_expect ((expr),(value)) )
  264. #else
  265. # define expect(expr,value) (expr)
  266. #endif
  267. #define likely(expr) expect((expr) != 0, 1)
  268. #define unlikely(expr) expect((expr) != 0, 0)
  269. #define KB *(1 <<10)
  270. #define MB *(1 <<20)
  271. #define GB *(1U<<30)
  272. #define ALLOCATOR(n,s) calloc(n,s)
  273. #define FREEMEM free
  274. #define MEM_INIT memset
  275. #ifndef MAX
  276. #define MAX(a,b) ((a)>(b))?(a):(b)
  277. #endif
  278. #ifndef MIN
  279. #define MIN(a,b) ((a)<(b)?(a):(b))
  280. #endif
  281. #if MINMATCH == 3
  282. #define MEM_readMINMATCH(ptr) (U32)(MEM_read32(ptr)<<8)
  283. #else
  284. #define MEM_readMINMATCH(ptr) (U32)(MEM_read32(ptr))
  285. #endif
  286. /*-************************************
  287. * Reading and writing into memory
  288. **************************************/
  289. #define STEPSIZE sizeof(size_t)
  290. MEM_STATIC void Lizard_copy8(void* dst, const void* src)
  291. {
  292. memcpy(dst,src,8);
  293. }
  294. /* customized variant of memcpy, which can overwrite up to 7 bytes beyond dstEnd */
  295. MEM_STATIC void Lizard_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
  296. {
  297. BYTE* d = (BYTE*)dstPtr;
  298. const BYTE* s = (const BYTE*)srcPtr;
  299. BYTE* const e = (BYTE*)dstEnd;
  300. #if 0
  301. const size_t l2 = 8 - (((size_t)d) & (sizeof(void*)-1));
  302. Lizard_copy8(d,s); if (d>e-9) return;
  303. d+=l2; s+=l2;
  304. #endif /* join to align */
  305. do { Lizard_copy8(d,s); d+=8; s+=8; } while (d<e);
  306. }
  307. MEM_STATIC void Lizard_wildCopy16(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd)
  308. {
  309. do {
  310. Lizard_copy8(dstPtr, srcPtr);
  311. Lizard_copy8(dstPtr+8, srcPtr+8);
  312. dstPtr += 16;
  313. srcPtr += 16;
  314. }
  315. while (dstPtr < dstEnd);
  316. }
  317. /*
  318. * LIZARD_FORCE_SW_BITCOUNT
  319. * Define this parameter if your target system or compiler does not support hardware bit count
  320. */
  321. #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */
  322. # define LIZARD_FORCE_SW_BITCOUNT
  323. #endif
  324. /* **************************************
  325. * Function body to include for inlining
  326. ****************************************/
  327. MEM_STATIC U32 Lizard_highbit32(U32 val)
  328. {
  329. # if defined(_MSC_VER) /* Visual */
  330. unsigned long r=0;
  331. _BitScanReverse(&r, val);
  332. return (unsigned)r;
  333. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  334. return 31 - __builtin_clz(val);
  335. # else /* Software version */
  336. static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
  337. U32 v = val;
  338. int r;
  339. v |= v >> 1;
  340. v |= v >> 2;
  341. v |= v >> 4;
  342. v |= v >> 8;
  343. v |= v >> 16;
  344. r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
  345. return r;
  346. # endif
  347. }
  348. /*-************************************
  349. * Common functions
  350. **************************************/
  351. MEM_STATIC unsigned Lizard_NbCommonBytes (register size_t val)
  352. {
  353. if (MEM_isLittleEndian()) {
  354. if (MEM_64bits()) {
  355. # if defined(_MSC_VER) && defined(_WIN64) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  356. unsigned long r = 0;
  357. _BitScanForward64( &r, (U64)val );
  358. return (int)(r>>3);
  359. # elif (defined(__clang__) || (LIZARD_GCC_VERSION >= 304)) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  360. return (__builtin_ctzll((U64)val) >> 3);
  361. # else
  362. static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
  363. return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
  364. # endif
  365. } else /* 32 bits */ {
  366. # if defined(_MSC_VER) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  367. unsigned long r;
  368. _BitScanForward( &r, (U32)val );
  369. return (int)(r>>3);
  370. # elif (defined(__clang__) || (LIZARD_GCC_VERSION >= 304)) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  371. return (__builtin_ctz((U32)val) >> 3);
  372. # else
  373. static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
  374. return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
  375. # endif
  376. }
  377. } else /* Big Endian CPU */ {
  378. if (MEM_64bits()) {
  379. # if defined(_MSC_VER) && defined(_WIN64) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  380. unsigned long r = 0;
  381. _BitScanReverse64( &r, val );
  382. return (unsigned)(r>>3);
  383. # elif (defined(__clang__) || (LIZARD_GCC_VERSION >= 304)) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  384. return (__builtin_clzll((U64)val) >> 3);
  385. # else
  386. unsigned r;
  387. if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
  388. if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
  389. r += (!val);
  390. return r;
  391. # endif
  392. } else /* 32 bits */ {
  393. # if defined(_MSC_VER) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  394. unsigned long r = 0;
  395. _BitScanReverse( &r, (unsigned long)val );
  396. return (unsigned)(r>>3);
  397. # elif (defined(__clang__) || (LIZARD_GCC_VERSION >= 304)) && !defined(LIZARD_FORCE_SW_BITCOUNT)
  398. return (__builtin_clz((U32)val) >> 3);
  399. # else
  400. unsigned r;
  401. if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
  402. r += (!val);
  403. return r;
  404. # endif
  405. }
  406. }
  407. }
  408. MEM_STATIC unsigned Lizard_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit)
  409. {
  410. const BYTE* const pStart = pIn;
  411. while (likely(pIn<pInLimit-(STEPSIZE-1))) {
  412. size_t diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
  413. if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
  414. pIn += Lizard_NbCommonBytes(diff);
  415. return (unsigned)(pIn - pStart);
  416. }
  417. if (MEM_64bits()) if ((pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
  418. if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
  419. if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
  420. return (unsigned)(pIn - pStart);
  421. }
  422. /* alias to functions with compressionLevel=1 */
  423. int Lizard_sizeofState_MinLevel(void);
  424. int Lizard_compress_MinLevel(const char* source, char* dest, int sourceSize, int maxDestSize);
  425. int Lizard_compress_extState_MinLevel (void* state, const char* source, char* dest, int inputSize, int maxDestSize);
  426. Lizard_stream_t* Lizard_resetStream_MinLevel (Lizard_stream_t* streamPtr);
  427. Lizard_stream_t* Lizard_createStream_MinLevel(void);
  428. #if defined (__cplusplus)
  429. }
  430. #endif
  431. #endif /* LIZARD_COMMON_H_2983827168210 */