zstdlib 0.7.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +20 -0
  3. data/README.md +7 -1
  4. data/Rakefile +38 -8
  5. data/ext/{zstdlib → zstdlib_c}/extconf.rb +11 -6
  6. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.2/zstdlib.c +2 -2
  7. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.3/zstdlib.c +2 -2
  8. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.4/zstdlib.c +2 -2
  9. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.5/zstdlib.c +2 -2
  10. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.6/zstdlib.c +2 -2
  11. data/ext/{zstdlib → zstdlib_c}/ruby/zlib-2.7/zstdlib.c +2 -2
  12. data/ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c +4994 -0
  13. data/ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c +5076 -0
  14. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/adler32.c +0 -0
  15. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/compress.c +0 -0
  16. data/ext/zstdlib_c/zlib-1.2.12/crc32.c +1116 -0
  17. data/ext/zstdlib_c/zlib-1.2.12/crc32.h +9446 -0
  18. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/deflate.c +78 -30
  19. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/deflate.h +12 -15
  20. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/gzclose.c +0 -0
  21. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/gzguts.h +3 -2
  22. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/gzlib.c +5 -3
  23. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/gzread.c +5 -7
  24. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/gzwrite.c +25 -13
  25. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/infback.c +2 -1
  26. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inffast.c +14 -14
  27. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inffast.h +0 -0
  28. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inffixed.h +0 -0
  29. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inflate.c +39 -8
  30. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inflate.h +3 -2
  31. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inftrees.c +3 -3
  32. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/inftrees.h +0 -0
  33. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/trees.c +27 -48
  34. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/trees.h +0 -0
  35. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/uncompr.c +0 -0
  36. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/zconf.h +0 -0
  37. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/zlib.h +123 -100
  38. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/zutil.c +2 -2
  39. data/ext/{zstdlib/zlib-1.2.11 → zstdlib_c/zlib-1.2.12}/zutil.h +12 -9
  40. data/ext/{zstdlib → zstdlib_c}/zlib.mk +0 -0
  41. data/ext/{zstdlib → zstdlib_c}/zlibwrapper/zlibwrapper.c +1 -5
  42. data/ext/{zstdlib → zstdlib_c}/zlibwrapper.mk +0 -0
  43. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/bitstream.h +46 -22
  44. data/ext/zstdlib_c/zstd-1.5.2/lib/common/compiler.h +335 -0
  45. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/cpu.h +1 -3
  46. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/debug.c +1 -1
  47. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/debug.h +12 -19
  48. data/ext/zstdlib_c/zstd-1.5.2/lib/common/entropy_common.c +368 -0
  49. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/error_private.c +2 -1
  50. data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.h +159 -0
  51. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/fse.h +41 -12
  52. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/fse_decompress.c +139 -22
  53. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/huf.h +47 -23
  54. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/mem.h +87 -98
  55. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/pool.c +34 -23
  56. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/pool.h +4 -4
  57. data/ext/zstdlib_c/zstd-1.5.2/lib/common/portability_macros.h +137 -0
  58. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/threading.c +6 -5
  59. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/threading.h +0 -0
  60. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.c +24 -0
  61. data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.h +5686 -0
  62. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/zstd_common.c +10 -10
  63. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_deps.h +111 -0
  64. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/common/zstd_internal.h +191 -145
  65. data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_trace.h +163 -0
  66. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/clevels.h +134 -0
  67. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/fse_compress.c +89 -46
  68. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/hist.c +27 -29
  69. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/hist.h +2 -2
  70. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/huf_compress.c +1370 -0
  71. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress.c +2917 -868
  72. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_internal.h +458 -125
  73. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_literals.c +12 -11
  74. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_literals.h +4 -2
  75. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_sequences.c +41 -18
  76. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_sequences.h +1 -1
  77. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_superblock.c +26 -298
  78. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_compress_superblock.h +1 -1
  79. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_cwksp.h +234 -83
  80. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_double_fast.c +313 -138
  81. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_double_fast.h +1 -1
  82. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_fast.c +329 -150
  83. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_fast.h +1 -1
  84. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.c +2104 -0
  85. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.h +125 -0
  86. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_ldm.c +321 -216
  87. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_ldm.h +9 -2
  88. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm_geartab.h +106 -0
  89. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_opt.c +412 -166
  90. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstd_opt.h +1 -1
  91. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/compress/zstdmt_compress.c +169 -453
  92. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.h +113 -0
  93. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/huf_decompress.c +1044 -403
  94. data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress_amd64.S +585 -0
  95. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_ddict.c +9 -9
  96. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_ddict.h +2 -2
  97. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress.c +450 -105
  98. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_block.c +913 -273
  99. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_block.h +14 -5
  100. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/decompress/zstd_decompress_internal.h +59 -12
  101. data/ext/zstdlib_c/zstd-1.5.2/lib/zdict.h +452 -0
  102. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/lib/zstd.h +699 -214
  103. data/ext/{zstdlib/zstd-1.4.5/lib/common → zstdlib_c/zstd-1.5.2/lib}/zstd_errors.h +2 -1
  104. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzclose.c +0 -0
  105. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzcompatibility.h +1 -1
  106. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzguts.h +0 -0
  107. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzlib.c +0 -0
  108. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzread.c +0 -0
  109. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/gzwrite.c +0 -0
  110. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/zstd_zlibwrapper.c +133 -44
  111. data/ext/{zstdlib/zstd-1.4.5 → zstdlib_c/zstd-1.5.2}/zlibWrapper/zstd_zlibwrapper.h +1 -1
  112. data/ext/zstdlib_c/zstd.mk +15 -0
  113. data/lib/zstdlib.rb +2 -2
  114. metadata +118 -109
  115. data/ext/zstdlib/zlib-1.2.11/crc32.c +0 -442
  116. data/ext/zstdlib/zlib-1.2.11/crc32.h +0 -441
  117. data/ext/zstdlib/zstd-1.4.5/lib/common/compiler.h +0 -175
  118. data/ext/zstdlib/zstd-1.4.5/lib/common/entropy_common.c +0 -216
  119. data/ext/zstdlib/zstd-1.4.5/lib/common/error_private.h +0 -80
  120. data/ext/zstdlib/zstd-1.4.5/lib/common/xxhash.c +0 -864
  121. data/ext/zstdlib/zstd-1.4.5/lib/common/xxhash.h +0 -285
  122. data/ext/zstdlib/zstd-1.4.5/lib/compress/huf_compress.c +0 -798
  123. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.c +0 -1138
  124. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.h +0 -67
  125. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstdmt_compress.h +0 -192
  126. data/ext/zstdlib/zstd.mk +0 -14
@@ -0,0 +1,368 @@
1
+ /* ******************************************************************
2
+ * Common functions 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
+ /* *************************************
16
+ * Dependencies
17
+ ***************************************/
18
+ #include "mem.h"
19
+ #include "error_private.h" /* ERR_*, ERROR */
20
+ #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
21
+ #include "fse.h"
22
+ #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
23
+ #include "huf.h"
24
+
25
+
26
+ /*=== Version ===*/
27
+ unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
28
+
29
+
30
+ /*=== Error Management ===*/
31
+ unsigned FSE_isError(size_t code) { return ERR_isError(code); }
32
+ const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
33
+
34
+ unsigned HUF_isError(size_t code) { return ERR_isError(code); }
35
+ const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
36
+
37
+
38
+ /*-**************************************************************
39
+ * FSE NCount encoding-decoding
40
+ ****************************************************************/
41
+ static U32 FSE_ctz(U32 val)
42
+ {
43
+ assert(val != 0);
44
+ {
45
+ # if defined(_MSC_VER) /* Visual */
46
+ if (val != 0) {
47
+ unsigned long r;
48
+ _BitScanForward(&r, val);
49
+ return (unsigned)r;
50
+ } else {
51
+ /* Should not reach this code path */
52
+ __assume(0);
53
+ }
54
+ # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
55
+ return __builtin_ctz(val);
56
+ # elif defined(__ICCARM__) /* IAR Intrinsic */
57
+ return __CTZ(val);
58
+ # else /* Software version */
59
+ U32 count = 0;
60
+ while ((val & 1) == 0) {
61
+ val >>= 1;
62
+ ++count;
63
+ }
64
+ return count;
65
+ # endif
66
+ }
67
+ }
68
+
69
+ FORCE_INLINE_TEMPLATE
70
+ size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
71
+ const void* headerBuffer, size_t hbSize)
72
+ {
73
+ const BYTE* const istart = (const BYTE*) headerBuffer;
74
+ const BYTE* const iend = istart + hbSize;
75
+ const BYTE* ip = istart;
76
+ int nbBits;
77
+ int remaining;
78
+ int threshold;
79
+ U32 bitStream;
80
+ int bitCount;
81
+ unsigned charnum = 0;
82
+ unsigned const maxSV1 = *maxSVPtr + 1;
83
+ int previous0 = 0;
84
+
85
+ if (hbSize < 8) {
86
+ /* This function only works when hbSize >= 8 */
87
+ char buffer[8] = {0};
88
+ ZSTD_memcpy(buffer, headerBuffer, hbSize);
89
+ { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
90
+ buffer, sizeof(buffer));
91
+ if (FSE_isError(countSize)) return countSize;
92
+ if (countSize > hbSize) return ERROR(corruption_detected);
93
+ return countSize;
94
+ } }
95
+ assert(hbSize >= 8);
96
+
97
+ /* init */
98
+ ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
99
+ bitStream = MEM_readLE32(ip);
100
+ nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
101
+ if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
102
+ bitStream >>= 4;
103
+ bitCount = 4;
104
+ *tableLogPtr = nbBits;
105
+ remaining = (1<<nbBits)+1;
106
+ threshold = 1<<nbBits;
107
+ nbBits++;
108
+
109
+ for (;;) {
110
+ if (previous0) {
111
+ /* Count the number of repeats. Each time the
112
+ * 2-bit repeat code is 0b11 there is another
113
+ * repeat.
114
+ * Avoid UB by setting the high bit to 1.
115
+ */
116
+ int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
117
+ while (repeats >= 12) {
118
+ charnum += 3 * 12;
119
+ if (LIKELY(ip <= iend-7)) {
120
+ ip += 3;
121
+ } else {
122
+ bitCount -= (int)(8 * (iend - 7 - ip));
123
+ bitCount &= 31;
124
+ ip = iend - 4;
125
+ }
126
+ bitStream = MEM_readLE32(ip) >> bitCount;
127
+ repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
128
+ }
129
+ charnum += 3 * repeats;
130
+ bitStream >>= 2 * repeats;
131
+ bitCount += 2 * repeats;
132
+
133
+ /* Add the final repeat which isn't 0b11. */
134
+ assert((bitStream & 3) < 3);
135
+ charnum += bitStream & 3;
136
+ bitCount += 2;
137
+
138
+ /* This is an error, but break and return an error
139
+ * at the end, because returning out of a loop makes
140
+ * it harder for the compiler to optimize.
141
+ */
142
+ if (charnum >= maxSV1) break;
143
+
144
+ /* We don't need to set the normalized count to 0
145
+ * because we already memset the whole buffer to 0.
146
+ */
147
+
148
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
149
+ assert((bitCount >> 3) <= 3); /* For first condition to work */
150
+ ip += bitCount>>3;
151
+ bitCount &= 7;
152
+ } else {
153
+ bitCount -= (int)(8 * (iend - 4 - ip));
154
+ bitCount &= 31;
155
+ ip = iend - 4;
156
+ }
157
+ bitStream = MEM_readLE32(ip) >> bitCount;
158
+ }
159
+ {
160
+ int const max = (2*threshold-1) - remaining;
161
+ int count;
162
+
163
+ if ((bitStream & (threshold-1)) < (U32)max) {
164
+ count = bitStream & (threshold-1);
165
+ bitCount += nbBits-1;
166
+ } else {
167
+ count = bitStream & (2*threshold-1);
168
+ if (count >= threshold) count -= max;
169
+ bitCount += nbBits;
170
+ }
171
+
172
+ count--; /* extra accuracy */
173
+ /* When it matters (small blocks), this is a
174
+ * predictable branch, because we don't use -1.
175
+ */
176
+ if (count >= 0) {
177
+ remaining -= count;
178
+ } else {
179
+ assert(count == -1);
180
+ remaining += count;
181
+ }
182
+ normalizedCounter[charnum++] = (short)count;
183
+ previous0 = !count;
184
+
185
+ assert(threshold > 1);
186
+ if (remaining < threshold) {
187
+ /* This branch can be folded into the
188
+ * threshold update condition because we
189
+ * know that threshold > 1.
190
+ */
191
+ if (remaining <= 1) break;
192
+ nbBits = BIT_highbit32(remaining) + 1;
193
+ threshold = 1 << (nbBits - 1);
194
+ }
195
+ if (charnum >= maxSV1) break;
196
+
197
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
198
+ ip += bitCount>>3;
199
+ bitCount &= 7;
200
+ } else {
201
+ bitCount -= (int)(8 * (iend - 4 - ip));
202
+ bitCount &= 31;
203
+ ip = iend - 4;
204
+ }
205
+ bitStream = MEM_readLE32(ip) >> bitCount;
206
+ } }
207
+ if (remaining != 1) return ERROR(corruption_detected);
208
+ /* Only possible when there are too many zeros. */
209
+ if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
210
+ if (bitCount > 32) return ERROR(corruption_detected);
211
+ *maxSVPtr = charnum-1;
212
+
213
+ ip += (bitCount+7)>>3;
214
+ return ip-istart;
215
+ }
216
+
217
+ /* Avoids the FORCE_INLINE of the _body() function. */
218
+ static size_t FSE_readNCount_body_default(
219
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
220
+ const void* headerBuffer, size_t hbSize)
221
+ {
222
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
223
+ }
224
+
225
+ #if DYNAMIC_BMI2
226
+ BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2(
227
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
228
+ const void* headerBuffer, size_t hbSize)
229
+ {
230
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
231
+ }
232
+ #endif
233
+
234
+ size_t FSE_readNCount_bmi2(
235
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
236
+ const void* headerBuffer, size_t hbSize, int bmi2)
237
+ {
238
+ #if DYNAMIC_BMI2
239
+ if (bmi2) {
240
+ return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
241
+ }
242
+ #endif
243
+ (void)bmi2;
244
+ return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
245
+ }
246
+
247
+ size_t FSE_readNCount(
248
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
249
+ const void* headerBuffer, size_t hbSize)
250
+ {
251
+ return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
252
+ }
253
+
254
+
255
+ /*! HUF_readStats() :
256
+ Read compact Huffman tree, saved by HUF_writeCTable().
257
+ `huffWeight` is destination buffer.
258
+ `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
259
+ @return : size read from `src` , or an error Code .
260
+ Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
261
+ */
262
+ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
263
+ U32* nbSymbolsPtr, U32* tableLogPtr,
264
+ const void* src, size_t srcSize)
265
+ {
266
+ U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
267
+ return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0);
268
+ }
269
+
270
+ FORCE_INLINE_TEMPLATE size_t
271
+ HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
272
+ U32* nbSymbolsPtr, U32* tableLogPtr,
273
+ const void* src, size_t srcSize,
274
+ void* workSpace, size_t wkspSize,
275
+ int bmi2)
276
+ {
277
+ U32 weightTotal;
278
+ const BYTE* ip = (const BYTE*) src;
279
+ size_t iSize;
280
+ size_t oSize;
281
+
282
+ if (!srcSize) return ERROR(srcSize_wrong);
283
+ iSize = ip[0];
284
+ /* ZSTD_memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
285
+
286
+ if (iSize >= 128) { /* special header */
287
+ oSize = iSize - 127;
288
+ iSize = ((oSize+1)/2);
289
+ if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
290
+ if (oSize >= hwSize) return ERROR(corruption_detected);
291
+ ip += 1;
292
+ { U32 n;
293
+ for (n=0; n<oSize; n+=2) {
294
+ huffWeight[n] = ip[n/2] >> 4;
295
+ huffWeight[n+1] = ip[n/2] & 15;
296
+ } } }
297
+ else { /* header compressed with FSE (normal case) */
298
+ if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
299
+ /* max (hwSize-1) values decoded, as last one is implied */
300
+ oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2);
301
+ if (FSE_isError(oSize)) return oSize;
302
+ }
303
+
304
+ /* collect weight stats */
305
+ ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
306
+ weightTotal = 0;
307
+ { U32 n; for (n=0; n<oSize; n++) {
308
+ if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
309
+ rankStats[huffWeight[n]]++;
310
+ weightTotal += (1 << huffWeight[n]) >> 1;
311
+ } }
312
+ if (weightTotal == 0) return ERROR(corruption_detected);
313
+
314
+ /* get last non-null symbol weight (implied, total must be 2^n) */
315
+ { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
316
+ if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
317
+ *tableLogPtr = tableLog;
318
+ /* determine last weight */
319
+ { U32 const total = 1 << tableLog;
320
+ U32 const rest = total - weightTotal;
321
+ U32 const verif = 1 << BIT_highbit32(rest);
322
+ U32 const lastWeight = BIT_highbit32(rest) + 1;
323
+ if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
324
+ huffWeight[oSize] = (BYTE)lastWeight;
325
+ rankStats[lastWeight]++;
326
+ } }
327
+
328
+ /* check tree construction validity */
329
+ if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
330
+
331
+ /* results */
332
+ *nbSymbolsPtr = (U32)(oSize+1);
333
+ return iSize+1;
334
+ }
335
+
336
+ /* Avoids the FORCE_INLINE of the _body() function. */
337
+ static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
338
+ U32* nbSymbolsPtr, U32* tableLogPtr,
339
+ const void* src, size_t srcSize,
340
+ void* workSpace, size_t wkspSize)
341
+ {
342
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
343
+ }
344
+
345
+ #if DYNAMIC_BMI2
346
+ static BMI2_TARGET_ATTRIBUTE size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
347
+ U32* nbSymbolsPtr, U32* tableLogPtr,
348
+ const void* src, size_t srcSize,
349
+ void* workSpace, size_t wkspSize)
350
+ {
351
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
352
+ }
353
+ #endif
354
+
355
+ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
356
+ U32* nbSymbolsPtr, U32* tableLogPtr,
357
+ const void* src, size_t srcSize,
358
+ void* workSpace, size_t wkspSize,
359
+ int bmi2)
360
+ {
361
+ #if DYNAMIC_BMI2
362
+ if (bmi2) {
363
+ return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
364
+ }
365
+ #endif
366
+ (void)bmi2;
367
+ return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
368
+ }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -48,6 +48,7 @@ const char* ERR_getErrorString(ERR_enum code)
48
48
  case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
49
49
  case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
50
50
  case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
51
+ case PREFIX(srcBuffer_wrong): return "Source buffer is wrong";
51
52
  case PREFIX(maxCode):
52
53
  default: return notErrorCode;
53
54
  }
@@ -0,0 +1,159 @@
1
+ /*
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ /* Note : this module is expected to remain private, do not expose it */
12
+
13
+ #ifndef ERROR_H_MODULE
14
+ #define ERROR_H_MODULE
15
+
16
+ #if defined (__cplusplus)
17
+ extern "C" {
18
+ #endif
19
+
20
+
21
+ /* ****************************************
22
+ * Dependencies
23
+ ******************************************/
24
+ #include "../zstd_errors.h" /* enum list */
25
+ #include "compiler.h"
26
+ #include "debug.h"
27
+ #include "zstd_deps.h" /* size_t */
28
+
29
+
30
+ /* ****************************************
31
+ * Compiler-specific
32
+ ******************************************/
33
+ #if defined(__GNUC__)
34
+ # define ERR_STATIC static __attribute__((unused))
35
+ #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
36
+ # define ERR_STATIC static inline
37
+ #elif defined(_MSC_VER)
38
+ # define ERR_STATIC static __inline
39
+ #else
40
+ # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
41
+ #endif
42
+
43
+
44
+ /*-****************************************
45
+ * Customization (error_public.h)
46
+ ******************************************/
47
+ typedef ZSTD_ErrorCode ERR_enum;
48
+ #define PREFIX(name) ZSTD_error_##name
49
+
50
+
51
+ /*-****************************************
52
+ * Error codes handling
53
+ ******************************************/
54
+ #undef ERROR /* already defined on Visual Studio */
55
+ #define ERROR(name) ZSTD_ERROR(name)
56
+ #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
57
+
58
+ ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
59
+
60
+ ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
61
+
62
+ /* check and forward error code */
63
+ #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
64
+ #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
65
+
66
+
67
+ /*-****************************************
68
+ * Error Strings
69
+ ******************************************/
70
+
71
+ const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
72
+
73
+ ERR_STATIC const char* ERR_getErrorName(size_t code)
74
+ {
75
+ return ERR_getErrorString(ERR_getErrorCode(code));
76
+ }
77
+
78
+ /**
79
+ * Ignore: this is an internal helper.
80
+ *
81
+ * This is a helper function to help force C99-correctness during compilation.
82
+ * Under strict compilation modes, variadic macro arguments can't be empty.
83
+ * However, variadic function arguments can be. Using a function therefore lets
84
+ * us statically check that at least one (string) argument was passed,
85
+ * independent of the compilation flags.
86
+ */
87
+ static INLINE_KEYWORD UNUSED_ATTR
88
+ void _force_has_format_string(const char *format, ...) {
89
+ (void)format;
90
+ }
91
+
92
+ /**
93
+ * Ignore: this is an internal helper.
94
+ *
95
+ * We want to force this function invocation to be syntactically correct, but
96
+ * we don't want to force runtime evaluation of its arguments.
97
+ */
98
+ #define _FORCE_HAS_FORMAT_STRING(...) \
99
+ if (0) { \
100
+ _force_has_format_string(__VA_ARGS__); \
101
+ }
102
+
103
+ #define ERR_QUOTE(str) #str
104
+
105
+ /**
106
+ * Return the specified error if the condition evaluates to true.
107
+ *
108
+ * In debug modes, prints additional information.
109
+ * In order to do that (particularly, printing the conditional that failed),
110
+ * this can't just wrap RETURN_ERROR().
111
+ */
112
+ #define RETURN_ERROR_IF(cond, err, ...) \
113
+ if (cond) { \
114
+ RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
115
+ __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
116
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
117
+ RAWLOG(3, ": " __VA_ARGS__); \
118
+ RAWLOG(3, "\n"); \
119
+ return ERROR(err); \
120
+ }
121
+
122
+ /**
123
+ * Unconditionally return the specified error.
124
+ *
125
+ * In debug modes, prints additional information.
126
+ */
127
+ #define RETURN_ERROR(err, ...) \
128
+ do { \
129
+ RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
130
+ __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
131
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
132
+ RAWLOG(3, ": " __VA_ARGS__); \
133
+ RAWLOG(3, "\n"); \
134
+ return ERROR(err); \
135
+ } while(0);
136
+
137
+ /**
138
+ * If the provided expression evaluates to an error code, returns that error code.
139
+ *
140
+ * In debug modes, prints additional information.
141
+ */
142
+ #define FORWARD_IF_ERROR(err, ...) \
143
+ do { \
144
+ size_t const err_code = (err); \
145
+ if (ERR_isError(err_code)) { \
146
+ RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
147
+ __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
148
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
149
+ RAWLOG(3, ": " __VA_ARGS__); \
150
+ RAWLOG(3, "\n"); \
151
+ return err_code; \
152
+ } \
153
+ } while(0);
154
+
155
+ #if defined (__cplusplus)
156
+ }
157
+ #endif
158
+
159
+ #endif /* ERROR_H_MODULE */
@@ -1,7 +1,7 @@
1
1
  /* ******************************************************************
2
2
  * FSE : Finite State Entropy codec
3
3
  * Public Prototypes declaration
4
- * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4
+ * Copyright (c) Yann Collet, Facebook, Inc.
5
5
  *
6
6
  * You can contact the author at :
7
7
  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -23,7 +23,7 @@ extern "C" {
23
23
  /*-*****************************************
24
24
  * Dependencies
25
25
  ******************************************/
26
- #include <stddef.h> /* size_t, ptrdiff_t */
26
+ #include "zstd_deps.h" /* size_t, ptrdiff_t */
27
27
 
28
28
 
29
29
  /*-*****************************************
@@ -137,10 +137,16 @@ FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize
137
137
  /*! FSE_normalizeCount():
138
138
  normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
139
139
  'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
140
+ useLowProbCount is a boolean parameter which trades off compressed size for
141
+ faster header decoding. When it is set to 1, the compressed data will be slightly
142
+ smaller. And when it is set to 0, FSE_readNCount() and FSE_buildDTable() will be
143
+ faster. If you are compressing a small amount of data (< 2 KB) then useLowProbCount=0
144
+ is a good default, since header deserialization makes a big speed difference.
145
+ Otherwise, useLowProbCount=1 is a good default, since the speed difference is small.
140
146
  @return : tableLog,
141
147
  or an errorCode, which can be tested using FSE_isError() */
142
148
  FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog,
143
- const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
149
+ const unsigned* count, size_t srcSize, unsigned maxSymbolValue, unsigned useLowProbCount);
144
150
 
145
151
  /*! FSE_NCountWriteBound():
146
152
  Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
@@ -228,6 +234,13 @@ FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter,
228
234
  unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,
229
235
  const void* rBuffer, size_t rBuffSize);
230
236
 
237
+ /*! FSE_readNCount_bmi2():
238
+ * Same as FSE_readNCount() but pass bmi2=1 when your CPU supports BMI2 and 0 otherwise.
239
+ */
240
+ FSE_PUBLIC_API size_t FSE_readNCount_bmi2(short* normalizedCounter,
241
+ unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,
242
+ const void* rBuffer, size_t rBuffSize, int bmi2);
243
+
231
244
  /*! Constructor and Destructor of FSE_DTable.
232
245
  Note that its size depends on 'tableLog' */
233
246
  typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
@@ -288,12 +301,12 @@ If there is an error, the function will return an error code, which can be teste
288
301
  *******************************************/
289
302
  /* FSE buffer bounds */
290
303
  #define FSE_NCOUNTBOUND 512
291
- #define FSE_BLOCKBOUND(size) (size + (size>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)
304
+ #define FSE_BLOCKBOUND(size) ((size) + ((size)>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)
292
305
  #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
293
306
 
294
307
  /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */
295
- #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
296
- #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
308
+ #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<((maxTableLog)-1)) + (((maxSymbolValue)+1)*2))
309
+ #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<(maxTableLog)))
297
310
 
298
311
  /* or use the size to malloc() space directly. Pay attention to alignment restrictions though */
299
312
  #define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable))
@@ -309,9 +322,9 @@ unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsi
309
322
 
310
323
  /* FSE_compress_wksp() :
311
324
  * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
312
- * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
325
+ * FSE_COMPRESS_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
313
326
  */
314
- #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )
327
+ #define FSE_COMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )
315
328
  size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
316
329
 
317
330
  size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits);
@@ -322,18 +335,31 @@ size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
322
335
 
323
336
  /* FSE_buildCTable_wksp() :
324
337
  * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
325
- * `wkspSize` must be >= `(1<<tableLog)`.
338
+ * `wkspSize` must be >= `FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog)` of `unsigned`.
339
+ * See FSE_buildCTable_wksp() for breakdown of workspace usage.
326
340
  */
341
+ #define FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog) (((maxSymbolValue + 2) + (1ull << (tableLog)))/2 + sizeof(U64)/sizeof(U32) /* additional 8 bytes for potential table overwrite */)
342
+ #define FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) (sizeof(unsigned) * FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog))
327
343
  size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
328
344
 
345
+ #define FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) (sizeof(short) * (maxSymbolValue + 1) + (1ULL << maxTableLog) + 8)
346
+ #define FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ((FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) + sizeof(unsigned) - 1) / sizeof(unsigned))
347
+ FSE_PUBLIC_API size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
348
+ /**< Same as FSE_buildDTable(), using an externally allocated `workspace` produced with `FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxSymbolValue)` */
349
+
329
350
  size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
330
351
  /**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */
331
352
 
332
353
  size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
333
354
  /**< build a fake FSE_DTable, designed to always generate the same symbolValue */
334
355
 
335
- size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog);
336
- /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */
356
+ #define FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) (FSE_DTABLE_SIZE_U32(maxTableLog) + FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) + (FSE_MAX_SYMBOL_VALUE + 1) / 2 + 1)
357
+ #define FSE_DECOMPRESS_WKSP_SIZE(maxTableLog, maxSymbolValue) (FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(unsigned))
358
+ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize);
359
+ /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DECOMPRESS_WKSP_SIZE_U32(maxLog, maxSymbolValue)` */
360
+
361
+ 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);
362
+ /**< Same as FSE_decompress_wksp() but with dynamic BMI2 support. Pass 1 if your CPU supports BMI2 or 0 if it doesn't. */
337
363
 
338
364
  typedef enum {
339
365
  FSE_repeat_none, /**< Cannot use the previous table */
@@ -644,6 +670,9 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
644
670
  #ifndef FSE_DEFAULT_MEMORY_USAGE
645
671
  # define FSE_DEFAULT_MEMORY_USAGE 13
646
672
  #endif
673
+ #if (FSE_DEFAULT_MEMORY_USAGE > FSE_MAX_MEMORY_USAGE)
674
+ # error "FSE_DEFAULT_MEMORY_USAGE must be <= FSE_MAX_MEMORY_USAGE"
675
+ #endif
647
676
 
648
677
  /*!FSE_MAX_SYMBOL_VALUE :
649
678
  * Maximum symbol value authorized.
@@ -677,7 +706,7 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
677
706
  # error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
678
707
  #endif
679
708
 
680
- #define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3)
709
+ #define FSE_TABLESTEP(tableSize) (((tableSize)>>1) + ((tableSize)>>3) + 3)
681
710
 
682
711
 
683
712
  #endif /* FSE_STATIC_LINKING_ONLY */