multi_compress 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -3
  3. data/GET_STARTED.md +3 -3
  4. data/README.md +75 -66
  5. data/THIRD_PARTY_NOTICES.md +24 -0
  6. data/ext/multi_compress/brotli_dec_static_init.c +3 -0
  7. data/ext/multi_compress/brotli_enc_static_init.c +3 -0
  8. data/ext/multi_compress/extconf.rb +22 -1
  9. data/ext/multi_compress/multi_compress.c +152 -72
  10. data/ext/multi_compress/vendor/.vendored +2 -2
  11. data/ext/multi_compress/vendor/brotli/LICENSE +19 -0
  12. data/ext/multi_compress/vendor/brotli/c/common/constants.c +7 -7
  13. data/ext/multi_compress/vendor/brotli/c/common/constants.h +2 -5
  14. data/ext/multi_compress/vendor/brotli/c/common/context.c +2 -2
  15. data/ext/multi_compress/vendor/brotli/c/common/context.h +1 -2
  16. data/ext/multi_compress/vendor/brotli/c/common/dictionary.c +4 -5856
  17. data/ext/multi_compress/vendor/brotli/c/common/dictionary.h +1 -2
  18. data/ext/multi_compress/vendor/brotli/c/common/dictionary_inc.h +5847 -0
  19. data/ext/multi_compress/vendor/brotli/c/common/platform.c +0 -4
  20. data/ext/multi_compress/vendor/brotli/c/common/platform.h +182 -43
  21. data/ext/multi_compress/vendor/brotli/c/common/shared_dictionary.c +3 -7
  22. data/ext/multi_compress/vendor/brotli/c/common/shared_dictionary_internal.h +1 -1
  23. data/ext/multi_compress/vendor/brotli/c/common/static_init.h +56 -0
  24. data/ext/multi_compress/vendor/brotli/c/common/transform.c +6 -4
  25. data/ext/multi_compress/vendor/brotli/c/common/transform.h +1 -2
  26. data/ext/multi_compress/vendor/brotli/c/common/version.h +3 -3
  27. data/ext/multi_compress/vendor/brotli/c/dec/bit_reader.c +2 -3
  28. data/ext/multi_compress/vendor/brotli/c/dec/bit_reader.h +0 -4
  29. data/ext/multi_compress/vendor/brotli/c/dec/decode.c +128 -39
  30. data/ext/multi_compress/vendor/brotli/c/dec/huffman.c +2 -5
  31. data/ext/multi_compress/vendor/brotli/c/dec/huffman.h +0 -2
  32. data/ext/multi_compress/vendor/brotli/c/dec/prefix.c +67 -0
  33. data/ext/multi_compress/vendor/brotli/c/dec/prefix.h +18 -708
  34. data/ext/multi_compress/vendor/brotli/c/dec/prefix_inc.h +707 -0
  35. data/ext/multi_compress/vendor/brotli/c/dec/state.c +18 -15
  36. data/ext/multi_compress/vendor/brotli/c/dec/state.h +2 -6
  37. data/ext/multi_compress/vendor/brotli/c/dec/static_init.c +53 -0
  38. data/ext/multi_compress/vendor/brotli/c/dec/static_init.h +30 -0
  39. data/ext/multi_compress/vendor/brotli/c/enc/backward_references.c +32 -8
  40. data/ext/multi_compress/vendor/brotli/c/enc/backward_references.h +1 -5
  41. data/ext/multi_compress/vendor/brotli/c/enc/backward_references_hq.c +15 -15
  42. data/ext/multi_compress/vendor/brotli/c/enc/backward_references_hq.h +1 -5
  43. data/ext/multi_compress/vendor/brotli/c/enc/bit_cost.c +28 -4
  44. data/ext/multi_compress/vendor/brotli/c/enc/bit_cost.h +8 -40
  45. data/ext/multi_compress/vendor/brotli/c/enc/bit_cost_inc.h +1 -1
  46. data/ext/multi_compress/vendor/brotli/c/enc/block_splitter.c +9 -12
  47. data/ext/multi_compress/vendor/brotli/c/enc/block_splitter.h +0 -3
  48. data/ext/multi_compress/vendor/brotli/c/enc/block_splitter_inc.h +14 -8
  49. data/ext/multi_compress/vendor/brotli/c/enc/brotli_bit_stream.c +10 -9
  50. data/ext/multi_compress/vendor/brotli/c/enc/brotli_bit_stream.h +0 -6
  51. data/ext/multi_compress/vendor/brotli/c/enc/cluster.c +0 -2
  52. data/ext/multi_compress/vendor/brotli/c/enc/cluster.h +0 -2
  53. data/ext/multi_compress/vendor/brotli/c/enc/command.c +1 -1
  54. data/ext/multi_compress/vendor/brotli/c/enc/command.h +8 -10
  55. data/ext/multi_compress/vendor/brotli/c/enc/compound_dictionary.c +3 -5
  56. data/ext/multi_compress/vendor/brotli/c/enc/compound_dictionary.h +1 -4
  57. data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment.c +3 -13
  58. data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment.h +0 -2
  59. data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment_two_pass.c +5 -15
  60. data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment_two_pass.h +0 -2
  61. data/ext/multi_compress/vendor/brotli/c/enc/dictionary_hash.c +127 -1830
  62. data/ext/multi_compress/vendor/brotli/c/enc/dictionary_hash.h +23 -3
  63. data/ext/multi_compress/vendor/brotli/c/enc/dictionary_hash_inc.h +1829 -0
  64. data/ext/multi_compress/vendor/brotli/c/enc/encode.c +77 -52
  65. data/ext/multi_compress/vendor/brotli/c/enc/encoder_dict.c +9 -7
  66. data/ext/multi_compress/vendor/brotli/c/enc/encoder_dict.h +2 -4
  67. data/ext/multi_compress/vendor/brotli/c/enc/entropy_encode.c +3 -6
  68. data/ext/multi_compress/vendor/brotli/c/enc/entropy_encode.h +2 -4
  69. data/ext/multi_compress/vendor/brotli/c/enc/entropy_encode_static.h +18 -12
  70. data/ext/multi_compress/vendor/brotli/c/enc/fast_log.c +1 -1
  71. data/ext/multi_compress/vendor/brotli/c/enc/fast_log.h +2 -3
  72. data/ext/multi_compress/vendor/brotli/c/enc/find_match_length.h +0 -2
  73. data/ext/multi_compress/vendor/brotli/c/enc/hash.h +38 -31
  74. data/ext/multi_compress/vendor/brotli/c/enc/hash_base.h +38 -0
  75. data/ext/multi_compress/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +11 -1
  76. data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match64_inc.h +24 -7
  77. data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match64_simd_inc.h +304 -0
  78. data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match_inc.h +30 -11
  79. data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +4 -0
  80. data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match_simd_inc.h +278 -0
  81. data/ext/multi_compress/vendor/brotli/c/enc/histogram.c +1 -0
  82. data/ext/multi_compress/vendor/brotli/c/enc/histogram.h +0 -4
  83. data/ext/multi_compress/vendor/brotli/c/enc/literal_cost.c +4 -6
  84. data/ext/multi_compress/vendor/brotli/c/enc/literal_cost.h +0 -2
  85. data/ext/multi_compress/vendor/brotli/c/enc/matching_tag_mask.h +69 -0
  86. data/ext/multi_compress/vendor/brotli/c/enc/memory.c +0 -5
  87. data/ext/multi_compress/vendor/brotli/c/enc/memory.h +0 -4
  88. data/ext/multi_compress/vendor/brotli/c/enc/metablock.c +7 -9
  89. data/ext/multi_compress/vendor/brotli/c/enc/metablock.h +3 -3
  90. data/ext/multi_compress/vendor/brotli/c/enc/metablock_inc.h +4 -4
  91. data/ext/multi_compress/vendor/brotli/c/enc/params.h +0 -1
  92. data/ext/multi_compress/vendor/brotli/c/enc/prefix.h +0 -2
  93. data/ext/multi_compress/vendor/brotli/c/enc/quality.h +17 -10
  94. data/ext/multi_compress/vendor/brotli/c/enc/ringbuffer.h +1 -4
  95. data/ext/multi_compress/vendor/brotli/c/enc/state.h +2 -2
  96. data/ext/multi_compress/vendor/brotli/c/enc/static_dict.c +5 -11
  97. data/ext/multi_compress/vendor/brotli/c/enc/static_dict.h +1 -3
  98. data/ext/multi_compress/vendor/brotli/c/enc/static_dict_lut.c +224 -0
  99. data/ext/multi_compress/vendor/brotli/c/enc/static_dict_lut.h +20 -5837
  100. data/ext/multi_compress/vendor/brotli/c/enc/static_dict_lut_inc.h +5830 -0
  101. data/ext/multi_compress/vendor/brotli/c/enc/static_init.c +59 -0
  102. data/ext/multi_compress/vendor/brotli/c/enc/static_init.h +30 -0
  103. data/ext/multi_compress/vendor/brotli/c/enc/static_init_lazy.cc +26 -0
  104. data/ext/multi_compress/vendor/brotli/c/enc/utf8_util.c +1 -1
  105. data/ext/multi_compress/vendor/brotli/c/enc/utf8_util.h +0 -2
  106. data/ext/multi_compress/vendor/brotli/c/enc/write_bits.h +0 -2
  107. data/ext/multi_compress/vendor/brotli/c/include/brotli/decode.h +1 -1
  108. data/ext/multi_compress/vendor/brotli/c/include/brotli/encode.h +5 -1
  109. data/ext/multi_compress/vendor/brotli/c/include/brotli/port.h +4 -7
  110. data/ext/multi_compress/vendor/brotli/c/include/brotli/types.h +2 -2
  111. data/ext/multi_compress/vendor/lz4/LICENSE +12 -0
  112. data/ext/multi_compress/vendor/zstd/COPYING +339 -0
  113. data/ext/multi_compress/vendor/zstd/LICENSE +30 -0
  114. data/ext/multi_compress/vendor/zstd/lib/Makefile +67 -35
  115. data/ext/multi_compress/vendor/zstd/lib/README.md +33 -2
  116. data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +55 -0
  117. data/ext/multi_compress/vendor/zstd/lib/common/bits.h +205 -0
  118. data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +84 -108
  119. data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +170 -41
  120. data/ext/multi_compress/vendor/zstd/lib/common/cpu.h +37 -1
  121. data/ext/multi_compress/vendor/zstd/lib/common/debug.c +7 -1
  122. data/ext/multi_compress/vendor/zstd/lib/common/debug.h +21 -21
  123. data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +12 -40
  124. data/ext/multi_compress/vendor/zstd/lib/common/error_private.c +10 -2
  125. data/ext/multi_compress/vendor/zstd/lib/common/error_private.h +46 -47
  126. data/ext/multi_compress/vendor/zstd/lib/common/fse.h +8 -100
  127. data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +28 -116
  128. data/ext/multi_compress/vendor/zstd/lib/common/huf.h +79 -166
  129. data/ext/multi_compress/vendor/zstd/lib/common/mem.h +46 -66
  130. data/ext/multi_compress/vendor/zstd/lib/common/pool.c +27 -11
  131. data/ext/multi_compress/vendor/zstd/lib/common/pool.h +8 -11
  132. data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +45 -11
  133. data/ext/multi_compress/vendor/zstd/lib/common/threading.c +74 -14
  134. data/ext/multi_compress/vendor/zstd/lib/common/threading.h +5 -18
  135. data/ext/multi_compress/vendor/zstd/lib/common/xxhash.c +5 -11
  136. data/ext/multi_compress/vendor/zstd/lib/common/xxhash.h +2411 -1003
  137. data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +1 -36
  138. data/ext/multi_compress/vendor/zstd/lib/common/zstd_deps.h +13 -1
  139. data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +13 -182
  140. data/ext/multi_compress/vendor/zstd/lib/common/zstd_trace.h +6 -13
  141. data/ext/multi_compress/vendor/zstd/lib/compress/clevels.h +1 -1
  142. data/ext/multi_compress/vendor/zstd/lib/compress/fse_compress.c +15 -131
  143. data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +11 -1
  144. data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +8 -1
  145. data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +283 -189
  146. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +2419 -903
  147. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +423 -245
  148. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_literals.c +116 -40
  149. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_literals.h +16 -8
  150. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +10 -10
  151. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.h +8 -7
  152. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +254 -139
  153. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  154. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_cwksp.h +184 -95
  155. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +163 -81
  156. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.h +18 -14
  157. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +507 -197
  158. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.h +7 -14
  159. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +579 -484
  160. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.h +133 -65
  161. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +61 -40
  162. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +7 -15
  163. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm_geartab.h +1 -1
  164. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +352 -218
  165. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.h +37 -21
  166. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +238 -0
  167. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.h +33 -0
  168. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +239 -175
  169. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +5 -16
  170. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +543 -488
  171. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +78 -61
  172. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +4 -4
  173. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +1 -1
  174. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +295 -115
  175. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +430 -293
  176. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.h +7 -2
  177. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +11 -7
  178. data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff.h +1 -1
  179. data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff_common.c +1 -1
  180. data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff_compress.c +1 -1
  181. data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff_decompress.c +3 -1
  182. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +95 -46
  183. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.h +3 -9
  184. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.h +0 -10
  185. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +4 -4
  186. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +25 -97
  187. data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +1 -1
  188. data/ext/multi_compress/vendor/zstd/lib/dll/example/README.md +1 -1
  189. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_legacy.h +38 -1
  190. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +19 -50
  191. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.h +1 -1
  192. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +27 -80
  193. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.h +1 -1
  194. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +28 -83
  195. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.h +1 -1
  196. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +25 -74
  197. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.h +1 -1
  198. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +31 -76
  199. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.h +1 -1
  200. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +44 -88
  201. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.h +1 -1
  202. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +33 -84
  203. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.h +1 -1
  204. data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +65 -33
  205. data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +5 -5
  206. data/ext/multi_compress/vendor/zstd/lib/module.modulemap +13 -3
  207. data/ext/multi_compress/vendor/zstd/lib/zdict.h +65 -36
  208. data/ext/multi_compress/vendor/zstd/lib/zstd.h +890 -267
  209. data/ext/multi_compress/vendor/zstd/lib/zstd_errors.h +28 -16
  210. data/lib/multi_compress/version.rb +1 -1
  211. metadata +29 -2
@@ -1,7 +1,7 @@
1
1
  /* ******************************************************************
2
2
  * bitstream
3
3
  * Part of FSE library
4
- * Copyright (c) Yann Collet, Facebook, Inc.
4
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
6
6
  * You can contact the author at :
7
7
  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -14,9 +14,6 @@
14
14
  #ifndef BITSTREAM_H_MODULE
15
15
  #define BITSTREAM_H_MODULE
16
16
 
17
- #if defined (__cplusplus)
18
- extern "C" {
19
- #endif
20
17
  /*
21
18
  * This API consists of small unitary functions, which must be inlined for best performance.
22
19
  * Since link-time-optimization is not available for all compilers,
@@ -30,14 +27,14 @@ extern "C" {
30
27
  #include "compiler.h" /* UNLIKELY() */
31
28
  #include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */
32
29
  #include "error_private.h" /* error codes and messages */
33
-
30
+ #include "bits.h" /* ZSTD_highbit32 */
34
31
 
35
32
  /*=========================================
36
33
  * Target specific
37
34
  =========================================*/
38
35
  #ifndef ZSTD_NO_INTRINSICS
39
- # if defined(__BMI__) && defined(__GNUC__)
40
- # include <immintrin.h> /* support for bextr (experimental) */
36
+ # if (defined(__BMI__) || defined(__BMI2__)) && defined(__GNUC__)
37
+ # include <immintrin.h> /* support for bextr (experimental)/bzhi */
41
38
  # elif defined(__ICCARM__)
42
39
  # include <intrinsics.h>
43
40
  # endif
@@ -51,12 +48,13 @@ extern "C" {
51
48
  /*-******************************************
52
49
  * bitStream encoding API (write forward)
53
50
  ********************************************/
51
+ typedef size_t BitContainerType;
54
52
  /* bitStream can mix input from multiple sources.
55
53
  * A critical property of these streams is that they encode and decode in **reverse** direction.
56
54
  * So the first bit sequence you add will be the last to be read, like a LIFO stack.
57
55
  */
58
56
  typedef struct {
59
- size_t bitContainer;
57
+ BitContainerType bitContainer;
60
58
  unsigned bitPos;
61
59
  char* startPtr;
62
60
  char* ptr;
@@ -64,7 +62,7 @@ typedef struct {
64
62
  } BIT_CStream_t;
65
63
 
66
64
  MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
67
- MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
65
+ MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, BitContainerType value, unsigned nbBits);
68
66
  MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
69
67
  MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
70
68
 
@@ -73,7 +71,7 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
73
71
  * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
74
72
  *
75
73
  * bits are first added to a local register.
76
- * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
74
+ * Local register is BitContainerType, 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
77
75
  * Writing data into memory is an explicit operation, performed by the flushBits function.
78
76
  * Hence keep track how many bits are potentially stored into local register to avoid register overflow.
79
77
  * After a flushBits, a maximum of 7 bits might still be stored into local register.
@@ -90,28 +88,28 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
90
88
  * bitStream decoding API (read backward)
91
89
  **********************************************/
92
90
  typedef struct {
93
- size_t bitContainer;
91
+ BitContainerType bitContainer;
94
92
  unsigned bitsConsumed;
95
93
  const char* ptr;
96
94
  const char* start;
97
95
  const char* limitPtr;
98
96
  } BIT_DStream_t;
99
97
 
100
- typedef enum { BIT_DStream_unfinished = 0,
101
- BIT_DStream_endOfBuffer = 1,
102
- BIT_DStream_completed = 2,
103
- BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
104
- /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
98
+ typedef enum { BIT_DStream_unfinished = 0, /* fully refilled */
99
+ BIT_DStream_endOfBuffer = 1, /* still some bits left in bitstream */
100
+ BIT_DStream_completed = 2, /* bitstream entirely consumed, bit-exact */
101
+ BIT_DStream_overflow = 3 /* user requested more bits than present in bitstream */
102
+ } BIT_DStream_status; /* result of BIT_reloadDStream() */
105
103
 
106
104
  MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
107
- MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
105
+ MEM_STATIC BitContainerType BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
108
106
  MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
109
107
  MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
110
108
 
111
109
 
112
110
  /* Start by invoking BIT_initDStream().
113
111
  * A chunk of the bitStream is then stored into a local register.
114
- * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
112
+ * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (BitContainerType).
115
113
  * You can then retrieve bitFields stored into the local register, **in reverse order**.
116
114
  * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
117
115
  * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.
@@ -123,7 +121,7 @@ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
123
121
  /*-****************************************
124
122
  * unsafe API
125
123
  ******************************************/
126
- MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
124
+ MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, BitContainerType value, unsigned nbBits);
127
125
  /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
128
126
 
129
127
  MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
@@ -132,48 +130,6 @@ MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
132
130
  MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
133
131
  /* faster, but works only if nbBits >= 1 */
134
132
 
135
-
136
-
137
- /*-**************************************************************
138
- * Internal functions
139
- ****************************************************************/
140
- MEM_STATIC unsigned BIT_highbit32 (U32 val)
141
- {
142
- assert(val != 0);
143
- {
144
- # if defined(_MSC_VER) /* Visual */
145
- # if STATIC_BMI2 == 1
146
- return _lzcnt_u32(val) ^ 31;
147
- # else
148
- if (val != 0) {
149
- unsigned long r;
150
- _BitScanReverse(&r, val);
151
- return (unsigned)r;
152
- } else {
153
- /* Should not reach this code path */
154
- __assume(0);
155
- }
156
- # endif
157
- # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
158
- return __builtin_clz (val) ^ 31;
159
- # elif defined(__ICCARM__) /* IAR Intrinsic */
160
- return 31 - __CLZ(val);
161
- # else /* Software version */
162
- static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
163
- 11, 14, 16, 18, 22, 25, 3, 30,
164
- 8, 12, 20, 28, 15, 17, 24, 7,
165
- 19, 27, 23, 6, 26, 5, 4, 31 };
166
- U32 v = val;
167
- v |= v >> 1;
168
- v |= v >> 2;
169
- v |= v >> 4;
170
- v |= v >> 8;
171
- v |= v >> 16;
172
- return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
173
- # endif
174
- }
175
- }
176
-
177
133
  /*===== Local Constants =====*/
178
134
  static const unsigned BIT_mask[] = {
179
135
  0, 1, 3, 7, 0xF, 0x1F,
@@ -203,16 +159,31 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
203
159
  return 0;
204
160
  }
205
161
 
162
+ FORCE_INLINE_TEMPLATE BitContainerType BIT_getLowerBits(BitContainerType bitContainer, U32 const nbBits)
163
+ {
164
+ #if STATIC_BMI2 && !defined(ZSTD_NO_INTRINSICS)
165
+ # if (defined(__x86_64__) || defined(_M_X64)) && !defined(__ILP32__)
166
+ return _bzhi_u64(bitContainer, nbBits);
167
+ # else
168
+ DEBUG_STATIC_ASSERT(sizeof(bitContainer) == sizeof(U32));
169
+ return _bzhi_u32(bitContainer, nbBits);
170
+ # endif
171
+ #else
172
+ assert(nbBits < BIT_MASK_SIZE);
173
+ return bitContainer & BIT_mask[nbBits];
174
+ #endif
175
+ }
176
+
206
177
  /*! BIT_addBits() :
207
178
  * can add up to 31 bits into `bitC`.
208
179
  * Note : does not check for register overflow ! */
209
180
  MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
210
- size_t value, unsigned nbBits)
181
+ BitContainerType value, unsigned nbBits)
211
182
  {
212
183
  DEBUG_STATIC_ASSERT(BIT_MASK_SIZE == 32);
213
184
  assert(nbBits < BIT_MASK_SIZE);
214
185
  assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
215
- bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
186
+ bitC->bitContainer |= BIT_getLowerBits(value, nbBits) << bitC->bitPos;
216
187
  bitC->bitPos += nbBits;
217
188
  }
218
189
 
@@ -220,7 +191,7 @@ MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
220
191
  * works only if `value` is _clean_,
221
192
  * meaning all high bits above nbBits are 0 */
222
193
  MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
223
- size_t value, unsigned nbBits)
194
+ BitContainerType value, unsigned nbBits)
224
195
  {
225
196
  assert((value>>nbBits) == 0);
226
197
  assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
@@ -267,7 +238,7 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
267
238
  BIT_addBitsFast(bitC, 1, 1); /* endMark */
268
239
  BIT_flushBits(bitC);
269
240
  if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */
270
- return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
241
+ return (size_t)(bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
271
242
  }
272
243
 
273
244
 
@@ -291,35 +262,35 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
291
262
  bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
292
263
  bitD->bitContainer = MEM_readLEST(bitD->ptr);
293
264
  { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
294
- bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
265
+ bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
295
266
  if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
296
267
  } else {
297
268
  bitD->ptr = bitD->start;
298
269
  bitD->bitContainer = *(const BYTE*)(bitD->start);
299
270
  switch(srcSize)
300
271
  {
301
- case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
272
+ case 7: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
302
273
  ZSTD_FALLTHROUGH;
303
274
 
304
- case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
275
+ case 6: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
305
276
  ZSTD_FALLTHROUGH;
306
277
 
307
- case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
278
+ case 5: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
308
279
  ZSTD_FALLTHROUGH;
309
280
 
310
- case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
281
+ case 4: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[3]) << 24;
311
282
  ZSTD_FALLTHROUGH;
312
283
 
313
- case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
284
+ case 3: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[2]) << 16;
314
285
  ZSTD_FALLTHROUGH;
315
286
 
316
- case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
287
+ case 2: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[1]) << 8;
317
288
  ZSTD_FALLTHROUGH;
318
289
 
319
290
  default: break;
320
291
  }
321
292
  { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
322
- bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
293
+ bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0;
323
294
  if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
324
295
  }
325
296
  bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
@@ -328,12 +299,12 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
328
299
  return srcSize;
329
300
  }
330
301
 
331
- MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
302
+ FORCE_INLINE_TEMPLATE BitContainerType BIT_getUpperBits(BitContainerType bitContainer, U32 const start)
332
303
  {
333
304
  return bitContainer >> start;
334
305
  }
335
306
 
336
- MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
307
+ FORCE_INLINE_TEMPLATE BitContainerType BIT_getMiddleBits(BitContainerType bitContainer, U32 const start, U32 const nbBits)
337
308
  {
338
309
  U32 const regMask = sizeof(bitContainer)*8 - 1;
339
310
  /* if start > regMask, bitstream is corrupted, and result is undefined */
@@ -343,30 +314,20 @@ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 c
343
314
  * such cpus old (pre-Haswell, 2013) and their performance is not of that
344
315
  * importance.
345
316
  */
346
- #if defined(__x86_64__) || defined(_M_X86)
317
+ #if defined(__x86_64__) || defined(_M_X64)
347
318
  return (bitContainer >> (start & regMask)) & ((((U64)1) << nbBits) - 1);
348
319
  #else
349
320
  return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
350
321
  #endif
351
322
  }
352
323
 
353
- MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
354
- {
355
- #if defined(STATIC_BMI2) && STATIC_BMI2 == 1
356
- return _bzhi_u64(bitContainer, nbBits);
357
- #else
358
- assert(nbBits < BIT_MASK_SIZE);
359
- return bitContainer & BIT_mask[nbBits];
360
- #endif
361
- }
362
-
363
324
  /*! BIT_lookBits() :
364
325
  * Provides next n bits from local register.
365
326
  * local register is not modified.
366
327
  * On 32-bits, maxNbBits==24.
367
328
  * On 64-bits, maxNbBits==56.
368
329
  * @return : value extracted */
369
- MEM_STATIC FORCE_INLINE_ATTR size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
330
+ FORCE_INLINE_TEMPLATE BitContainerType BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
370
331
  {
371
332
  /* arbitrate between double-shift and shift+mask */
372
333
  #if 1
@@ -382,14 +343,14 @@ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_lookBits(const BIT_DStream_t* bitD, U3
382
343
 
383
344
  /*! BIT_lookBitsFast() :
384
345
  * unsafe version; only works if nbBits >= 1 */
385
- MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
346
+ MEM_STATIC BitContainerType BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
386
347
  {
387
348
  U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
388
349
  assert(nbBits >= 1);
389
350
  return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
390
351
  }
391
352
 
392
- MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
353
+ FORCE_INLINE_TEMPLATE void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
393
354
  {
394
355
  bitD->bitsConsumed += nbBits;
395
356
  }
@@ -398,23 +359,38 @@ MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
398
359
  * Read (consume) next n bits from local register and update.
399
360
  * Pay attention to not read more than nbBits contained into local register.
400
361
  * @return : extracted value. */
401
- MEM_STATIC FORCE_INLINE_ATTR size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
362
+ FORCE_INLINE_TEMPLATE BitContainerType BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
402
363
  {
403
- size_t const value = BIT_lookBits(bitD, nbBits);
364
+ BitContainerType const value = BIT_lookBits(bitD, nbBits);
404
365
  BIT_skipBits(bitD, nbBits);
405
366
  return value;
406
367
  }
407
368
 
408
369
  /*! BIT_readBitsFast() :
409
- * unsafe version; only works only if nbBits >= 1 */
410
- MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
370
+ * unsafe version; only works if nbBits >= 1 */
371
+ MEM_STATIC BitContainerType BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
411
372
  {
412
- size_t const value = BIT_lookBitsFast(bitD, nbBits);
373
+ BitContainerType const value = BIT_lookBitsFast(bitD, nbBits);
413
374
  assert(nbBits >= 1);
414
375
  BIT_skipBits(bitD, nbBits);
415
376
  return value;
416
377
  }
417
378
 
379
+ /*! BIT_reloadDStream_internal() :
380
+ * Simple variant of BIT_reloadDStream(), with two conditions:
381
+ * 1. bitstream is valid : bitsConsumed <= sizeof(bitD->bitContainer)*8
382
+ * 2. look window is valid after shifted down : bitD->ptr >= bitD->start
383
+ */
384
+ MEM_STATIC BIT_DStream_status BIT_reloadDStream_internal(BIT_DStream_t* bitD)
385
+ {
386
+ assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
387
+ bitD->ptr -= bitD->bitsConsumed >> 3;
388
+ assert(bitD->ptr >= bitD->start);
389
+ bitD->bitsConsumed &= 7;
390
+ bitD->bitContainer = MEM_readLEST(bitD->ptr);
391
+ return BIT_DStream_unfinished;
392
+ }
393
+
418
394
  /*! BIT_reloadDStreamFast() :
419
395
  * Similar to BIT_reloadDStream(), but with two differences:
420
396
  * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!
@@ -425,31 +401,35 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
425
401
  {
426
402
  if (UNLIKELY(bitD->ptr < bitD->limitPtr))
427
403
  return BIT_DStream_overflow;
428
- assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
429
- bitD->ptr -= bitD->bitsConsumed >> 3;
430
- bitD->bitsConsumed &= 7;
431
- bitD->bitContainer = MEM_readLEST(bitD->ptr);
432
- return BIT_DStream_unfinished;
404
+ return BIT_reloadDStream_internal(bitD);
433
405
  }
434
406
 
435
407
  /*! BIT_reloadDStream() :
436
408
  * Refill `bitD` from buffer previously set in BIT_initDStream() .
437
- * This function is safe, it guarantees it will not read beyond src buffer.
409
+ * This function is safe, it guarantees it will not never beyond src buffer.
438
410
  * @return : status of `BIT_DStream_t` internal register.
439
411
  * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
440
- MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
412
+ FORCE_INLINE_TEMPLATE BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
441
413
  {
442
- if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
414
+ /* note : once in overflow mode, a bitstream remains in this mode until it's reset */
415
+ if (UNLIKELY(bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8))) {
416
+ static const BitContainerType zeroFilled = 0;
417
+ bitD->ptr = (const char*)&zeroFilled; /* aliasing is allowed for char */
418
+ /* overflow detected, erroneous scenario or end of stream: no update */
443
419
  return BIT_DStream_overflow;
420
+ }
421
+
422
+ assert(bitD->ptr >= bitD->start);
444
423
 
445
424
  if (bitD->ptr >= bitD->limitPtr) {
446
- return BIT_reloadDStreamFast(bitD);
425
+ return BIT_reloadDStream_internal(bitD);
447
426
  }
448
427
  if (bitD->ptr == bitD->start) {
428
+ /* reached end of bitStream => no update */
449
429
  if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
450
430
  return BIT_DStream_completed;
451
431
  }
452
- /* start < ptr < limitPtr */
432
+ /* start < ptr < limitPtr => cautious update */
453
433
  { U32 nbBytes = bitD->bitsConsumed >> 3;
454
434
  BIT_DStream_status result = BIT_DStream_unfinished;
455
435
  if (bitD->ptr - nbBytes < bitD->start) {
@@ -471,8 +451,4 @@ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
471
451
  return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
472
452
  }
473
453
 
474
- #if defined (__cplusplus)
475
- }
476
- #endif
477
-
478
454
  #endif /* BITSTREAM_H_MODULE */