zstd-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.gitmodules +3 -0
- data/.rspec +2 -0
- data/.travis.yml +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +29 -0
- data/README.md +63 -0
- data/Rakefile +22 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/zstdruby/extconf.rb +23 -0
- data/ext/zstdruby/libzstd/.gitignore +2 -0
- data/ext/zstdruby/libzstd/Makefile +133 -0
- data/ext/zstdruby/libzstd/README.md +77 -0
- data/ext/zstdruby/libzstd/common/bitstream.h +414 -0
- data/ext/zstdruby/libzstd/common/entropy_common.c +227 -0
- data/ext/zstdruby/libzstd/common/error_private.c +43 -0
- data/ext/zstdruby/libzstd/common/error_private.h +76 -0
- data/ext/zstdruby/libzstd/common/fse.h +668 -0
- data/ext/zstdruby/libzstd/common/fse_decompress.c +329 -0
- data/ext/zstdruby/libzstd/common/huf.h +238 -0
- data/ext/zstdruby/libzstd/common/mem.h +372 -0
- data/ext/zstdruby/libzstd/common/xxhash.c +867 -0
- data/ext/zstdruby/libzstd/common/xxhash.h +309 -0
- data/ext/zstdruby/libzstd/common/zstd_common.c +77 -0
- data/ext/zstdruby/libzstd/common/zstd_errors.h +60 -0
- data/ext/zstdruby/libzstd/common/zstd_internal.h +270 -0
- data/ext/zstdruby/libzstd/compress/fse_compress.c +850 -0
- data/ext/zstdruby/libzstd/compress/huf_compress.c +609 -0
- data/ext/zstdruby/libzstd/compress/zstd_compress.c +3291 -0
- data/ext/zstdruby/libzstd/compress/zstd_opt.h +919 -0
- data/ext/zstdruby/libzstd/decompress/huf_decompress.c +885 -0
- data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +2154 -0
- data/ext/zstdruby/libzstd/deprecated/zbuff.h +210 -0
- data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +145 -0
- data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +74 -0
- data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1913 -0
- data/ext/zstdruby/libzstd/dictBuilder/divsufsort.h +67 -0
- data/ext/zstdruby/libzstd/dictBuilder/zdict.c +1012 -0
- data/ext/zstdruby/libzstd/dictBuilder/zdict.h +111 -0
- data/ext/zstdruby/libzstd/dll/example/Makefile +47 -0
- data/ext/zstdruby/libzstd/dll/example/README.md +69 -0
- data/ext/zstdruby/libzstd/dll/example/build_package.bat +17 -0
- data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +25 -0
- data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +179 -0
- data/ext/zstdruby/libzstd/dll/libzstd.def +86 -0
- data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +259 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2095 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v01.h +80 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3518 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v02.h +79 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3159 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v03.h +79 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3795 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v04.h +128 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v05.c +4056 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v05.h +149 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v06.c +4167 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v06.h +159 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v07.c +4540 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v07.h +173 -0
- data/ext/zstdruby/libzstd/libzstd.pc.in +14 -0
- data/ext/zstdruby/libzstd/zstd.h +673 -0
- data/ext/zstdruby/zstdruby.c +117 -0
- data/ext/zstdruby/zstdruby.h +6 -0
- data/lib/zstd-ruby.rb +6 -0
- data/lib/zstd-ruby/version.rb +3 -0
- data/zstd-ruby.gemspec +37 -0
- metadata +170 -0
@@ -0,0 +1,850 @@
|
|
1
|
+
/* ******************************************************************
|
2
|
+
FSE : Finite State Entropy encoder
|
3
|
+
Copyright (C) 2013-2015, Yann Collet.
|
4
|
+
|
5
|
+
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
6
|
+
|
7
|
+
Redistribution and use in source and binary forms, with or without
|
8
|
+
modification, are permitted provided that the following conditions are
|
9
|
+
met:
|
10
|
+
|
11
|
+
* Redistributions of source code must retain the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
13
|
+
* Redistributions in binary form must reproduce the above
|
14
|
+
copyright notice, this list of conditions and the following disclaimer
|
15
|
+
in the documentation and/or other materials provided with the
|
16
|
+
distribution.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
You can contact the author at :
|
31
|
+
- FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
|
32
|
+
- Public forum : https://groups.google.com/forum/#!forum/lz4c
|
33
|
+
****************************************************************** */
|
34
|
+
|
35
|
+
/* **************************************************************
|
36
|
+
* Compiler specifics
|
37
|
+
****************************************************************/
|
38
|
+
#ifdef _MSC_VER /* Visual Studio */
|
39
|
+
# define FORCE_INLINE static __forceinline
|
40
|
+
# include <intrin.h> /* For Visual 2005 */
|
41
|
+
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
42
|
+
# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
|
43
|
+
#else
|
44
|
+
# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
|
45
|
+
# ifdef __GNUC__
|
46
|
+
# define FORCE_INLINE static inline __attribute__((always_inline))
|
47
|
+
# else
|
48
|
+
# define FORCE_INLINE static inline
|
49
|
+
# endif
|
50
|
+
# else
|
51
|
+
# define FORCE_INLINE static
|
52
|
+
# endif /* __STDC_VERSION__ */
|
53
|
+
#endif
|
54
|
+
|
55
|
+
|
56
|
+
/* **************************************************************
|
57
|
+
* Includes
|
58
|
+
****************************************************************/
|
59
|
+
#include <stdlib.h> /* malloc, free, qsort */
|
60
|
+
#include <string.h> /* memcpy, memset */
|
61
|
+
#include <stdio.h> /* printf (debug) */
|
62
|
+
#include "bitstream.h"
|
63
|
+
#define FSE_STATIC_LINKING_ONLY
|
64
|
+
#include "fse.h"
|
65
|
+
|
66
|
+
|
67
|
+
/* **************************************************************
|
68
|
+
* Error Management
|
69
|
+
****************************************************************/
|
70
|
+
#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
|
71
|
+
|
72
|
+
|
73
|
+
/* **************************************************************
|
74
|
+
* Templates
|
75
|
+
****************************************************************/
|
76
|
+
/*
|
77
|
+
designed to be included
|
78
|
+
for type-specific functions (template emulation in C)
|
79
|
+
Objective is to write these functions only once, for improved maintenance
|
80
|
+
*/
|
81
|
+
|
82
|
+
/* safety checks */
|
83
|
+
#ifndef FSE_FUNCTION_EXTENSION
|
84
|
+
# error "FSE_FUNCTION_EXTENSION must be defined"
|
85
|
+
#endif
|
86
|
+
#ifndef FSE_FUNCTION_TYPE
|
87
|
+
# error "FSE_FUNCTION_TYPE must be defined"
|
88
|
+
#endif
|
89
|
+
|
90
|
+
/* Function names */
|
91
|
+
#define FSE_CAT(X,Y) X##Y
|
92
|
+
#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
|
93
|
+
#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
|
94
|
+
|
95
|
+
|
96
|
+
/* Function templates */
|
97
|
+
|
98
|
+
/* FSE_buildCTable_wksp() :
|
99
|
+
* Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
|
100
|
+
* wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)`
|
101
|
+
* workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements
|
102
|
+
*/
|
103
|
+
size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
|
104
|
+
{
|
105
|
+
U32 const tableSize = 1 << tableLog;
|
106
|
+
U32 const tableMask = tableSize - 1;
|
107
|
+
void* const ptr = ct;
|
108
|
+
U16* const tableU16 = ( (U16*) ptr) + 2;
|
109
|
+
void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ;
|
110
|
+
FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
|
111
|
+
U32 const step = FSE_TABLESTEP(tableSize);
|
112
|
+
U32 cumul[FSE_MAX_SYMBOL_VALUE+2];
|
113
|
+
|
114
|
+
FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)workSpace;
|
115
|
+
U32 highThreshold = tableSize-1;
|
116
|
+
|
117
|
+
/* CTable header */
|
118
|
+
if (((size_t)1 << tableLog) * sizeof(FSE_FUNCTION_TYPE) > wkspSize) return ERROR(tableLog_tooLarge);
|
119
|
+
tableU16[-2] = (U16) tableLog;
|
120
|
+
tableU16[-1] = (U16) maxSymbolValue;
|
121
|
+
|
122
|
+
/* For explanations on how to distribute symbol values over the table :
|
123
|
+
* http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
|
124
|
+
|
125
|
+
/* symbol start positions */
|
126
|
+
{ U32 u;
|
127
|
+
cumul[0] = 0;
|
128
|
+
for (u=1; u<=maxSymbolValue+1; u++) {
|
129
|
+
if (normalizedCounter[u-1]==-1) { /* Low proba symbol */
|
130
|
+
cumul[u] = cumul[u-1] + 1;
|
131
|
+
tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);
|
132
|
+
} else {
|
133
|
+
cumul[u] = cumul[u-1] + normalizedCounter[u-1];
|
134
|
+
} }
|
135
|
+
cumul[maxSymbolValue+1] = tableSize+1;
|
136
|
+
}
|
137
|
+
|
138
|
+
/* Spread symbols */
|
139
|
+
{ U32 position = 0;
|
140
|
+
U32 symbol;
|
141
|
+
for (symbol=0; symbol<=maxSymbolValue; symbol++) {
|
142
|
+
int nbOccurences;
|
143
|
+
for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++) {
|
144
|
+
tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
|
145
|
+
position = (position + step) & tableMask;
|
146
|
+
while (position > highThreshold) position = (position + step) & tableMask; /* Low proba area */
|
147
|
+
} }
|
148
|
+
|
149
|
+
if (position!=0) return ERROR(GENERIC); /* Must have gone through all positions */
|
150
|
+
}
|
151
|
+
|
152
|
+
/* Build table */
|
153
|
+
{ U32 u; for (u=0; u<tableSize; u++) {
|
154
|
+
FSE_FUNCTION_TYPE s = tableSymbol[u]; /* note : static analyzer may not understand tableSymbol is properly initialized */
|
155
|
+
tableU16[cumul[s]++] = (U16) (tableSize+u); /* TableU16 : sorted by symbol order; gives next state value */
|
156
|
+
} }
|
157
|
+
|
158
|
+
/* Build Symbol Transformation Table */
|
159
|
+
{ unsigned total = 0;
|
160
|
+
unsigned s;
|
161
|
+
for (s=0; s<=maxSymbolValue; s++) {
|
162
|
+
switch (normalizedCounter[s])
|
163
|
+
{
|
164
|
+
case 0: break;
|
165
|
+
|
166
|
+
case -1:
|
167
|
+
case 1:
|
168
|
+
symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);
|
169
|
+
symbolTT[s].deltaFindState = total - 1;
|
170
|
+
total ++;
|
171
|
+
break;
|
172
|
+
default :
|
173
|
+
{
|
174
|
+
U32 const maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1);
|
175
|
+
U32 const minStatePlus = normalizedCounter[s] << maxBitsOut;
|
176
|
+
symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
|
177
|
+
symbolTT[s].deltaFindState = total - normalizedCounter[s];
|
178
|
+
total += normalizedCounter[s];
|
179
|
+
} } } }
|
180
|
+
|
181
|
+
return 0;
|
182
|
+
}
|
183
|
+
|
184
|
+
|
185
|
+
size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
|
186
|
+
{
|
187
|
+
FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE]; /* memset() is not necessary, even if static analyzer complain about it */
|
188
|
+
return FSE_buildCTable_wksp(ct, normalizedCounter, maxSymbolValue, tableLog, tableSymbol, sizeof(tableSymbol));
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
#ifndef FSE_COMMONDEFS_ONLY
|
194
|
+
|
195
|
+
/*-**************************************************************
|
196
|
+
* FSE NCount encoding-decoding
|
197
|
+
****************************************************************/
|
198
|
+
size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
|
199
|
+
{
|
200
|
+
size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 3;
|
201
|
+
return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND; /* maxSymbolValue==0 ? use default */
|
202
|
+
}
|
203
|
+
|
204
|
+
static short FSE_abs(short a) { return (short)(a<0 ? -a : a); }
|
205
|
+
|
206
|
+
static size_t FSE_writeNCount_generic (void* header, size_t headerBufferSize,
|
207
|
+
const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
|
208
|
+
unsigned writeIsSafe)
|
209
|
+
{
|
210
|
+
BYTE* const ostart = (BYTE*) header;
|
211
|
+
BYTE* out = ostart;
|
212
|
+
BYTE* const oend = ostart + headerBufferSize;
|
213
|
+
int nbBits;
|
214
|
+
const int tableSize = 1 << tableLog;
|
215
|
+
int remaining;
|
216
|
+
int threshold;
|
217
|
+
U32 bitStream;
|
218
|
+
int bitCount;
|
219
|
+
unsigned charnum = 0;
|
220
|
+
int previous0 = 0;
|
221
|
+
|
222
|
+
bitStream = 0;
|
223
|
+
bitCount = 0;
|
224
|
+
/* Table Size */
|
225
|
+
bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
|
226
|
+
bitCount += 4;
|
227
|
+
|
228
|
+
/* Init */
|
229
|
+
remaining = tableSize+1; /* +1 for extra accuracy */
|
230
|
+
threshold = tableSize;
|
231
|
+
nbBits = tableLog+1;
|
232
|
+
|
233
|
+
while (remaining>1) { /* stops at 1 */
|
234
|
+
if (previous0) {
|
235
|
+
unsigned start = charnum;
|
236
|
+
while (!normalizedCounter[charnum]) charnum++;
|
237
|
+
while (charnum >= start+24) {
|
238
|
+
start+=24;
|
239
|
+
bitStream += 0xFFFFU << bitCount;
|
240
|
+
if ((!writeIsSafe) && (out > oend-2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */
|
241
|
+
out[0] = (BYTE) bitStream;
|
242
|
+
out[1] = (BYTE)(bitStream>>8);
|
243
|
+
out+=2;
|
244
|
+
bitStream>>=16;
|
245
|
+
}
|
246
|
+
while (charnum >= start+3) {
|
247
|
+
start+=3;
|
248
|
+
bitStream += 3 << bitCount;
|
249
|
+
bitCount += 2;
|
250
|
+
}
|
251
|
+
bitStream += (charnum-start) << bitCount;
|
252
|
+
bitCount += 2;
|
253
|
+
if (bitCount>16) {
|
254
|
+
if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */
|
255
|
+
out[0] = (BYTE)bitStream;
|
256
|
+
out[1] = (BYTE)(bitStream>>8);
|
257
|
+
out += 2;
|
258
|
+
bitStream >>= 16;
|
259
|
+
bitCount -= 16;
|
260
|
+
} }
|
261
|
+
{ short count = normalizedCounter[charnum++];
|
262
|
+
const short max = (short)((2*threshold-1)-remaining);
|
263
|
+
remaining -= FSE_abs(count);
|
264
|
+
if (remaining<1) return ERROR(GENERIC);
|
265
|
+
count++; /* +1 for extra accuracy */
|
266
|
+
if (count>=threshold) count += max; /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
|
267
|
+
bitStream += count << bitCount;
|
268
|
+
bitCount += nbBits;
|
269
|
+
bitCount -= (count<max);
|
270
|
+
previous0 = (count==1);
|
271
|
+
while (remaining<threshold) nbBits--, threshold>>=1;
|
272
|
+
}
|
273
|
+
if (bitCount>16) {
|
274
|
+
if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */
|
275
|
+
out[0] = (BYTE)bitStream;
|
276
|
+
out[1] = (BYTE)(bitStream>>8);
|
277
|
+
out += 2;
|
278
|
+
bitStream >>= 16;
|
279
|
+
bitCount -= 16;
|
280
|
+
} }
|
281
|
+
|
282
|
+
/* flush remaining bitStream */
|
283
|
+
if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall); /* Buffer overflow */
|
284
|
+
out[0] = (BYTE)bitStream;
|
285
|
+
out[1] = (BYTE)(bitStream>>8);
|
286
|
+
out+= (bitCount+7) /8;
|
287
|
+
|
288
|
+
if (charnum > maxSymbolValue + 1) return ERROR(GENERIC);
|
289
|
+
|
290
|
+
return (out-ostart);
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
|
295
|
+
{
|
296
|
+
if (tableLog > FSE_MAX_TABLELOG) return ERROR(GENERIC); /* Unsupported */
|
297
|
+
if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported */
|
298
|
+
|
299
|
+
if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))
|
300
|
+
return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
|
301
|
+
|
302
|
+
return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1);
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
/*-**************************************************************
|
308
|
+
* Counting histogram
|
309
|
+
****************************************************************/
|
310
|
+
/*! FSE_count_simple
|
311
|
+
This function counts byte values within `src`, and store the histogram into table `count`.
|
312
|
+
It doesn't use any additional memory.
|
313
|
+
But this function is unsafe : it doesn't check that all values within `src` can fit into `count`.
|
314
|
+
For this reason, prefer using a table `count` with 256 elements.
|
315
|
+
@return : count of most numerous element
|
316
|
+
*/
|
317
|
+
size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
|
318
|
+
const void* src, size_t srcSize)
|
319
|
+
{
|
320
|
+
const BYTE* ip = (const BYTE*)src;
|
321
|
+
const BYTE* const end = ip + srcSize;
|
322
|
+
unsigned maxSymbolValue = *maxSymbolValuePtr;
|
323
|
+
unsigned max=0;
|
324
|
+
|
325
|
+
memset(count, 0, (maxSymbolValue+1)*sizeof(*count));
|
326
|
+
if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }
|
327
|
+
|
328
|
+
while (ip<end) count[*ip++]++;
|
329
|
+
|
330
|
+
while (!count[maxSymbolValue]) maxSymbolValue--;
|
331
|
+
*maxSymbolValuePtr = maxSymbolValue;
|
332
|
+
|
333
|
+
{ U32 s; for (s=0; s<=maxSymbolValue; s++) if (count[s] > max) max = count[s]; }
|
334
|
+
|
335
|
+
return (size_t)max;
|
336
|
+
}
|
337
|
+
|
338
|
+
|
339
|
+
/* FSE_count_parallel_wksp() :
|
340
|
+
* Same as FSE_count_parallel(), but using an externally provided scratch buffer.
|
341
|
+
* `workSpace` size must be a minimum of `1024 * sizeof(unsigned)`` */
|
342
|
+
static size_t FSE_count_parallel_wksp(
|
343
|
+
unsigned* count, unsigned* maxSymbolValuePtr,
|
344
|
+
const void* source, size_t sourceSize,
|
345
|
+
unsigned checkMax, unsigned* const workSpace)
|
346
|
+
{
|
347
|
+
const BYTE* ip = (const BYTE*)source;
|
348
|
+
const BYTE* const iend = ip+sourceSize;
|
349
|
+
unsigned maxSymbolValue = *maxSymbolValuePtr;
|
350
|
+
unsigned max=0;
|
351
|
+
U32* const Counting1 = workSpace;
|
352
|
+
U32* const Counting2 = Counting1 + 256;
|
353
|
+
U32* const Counting3 = Counting2 + 256;
|
354
|
+
U32* const Counting4 = Counting3 + 256;
|
355
|
+
|
356
|
+
memset(Counting1, 0, 4*256*sizeof(unsigned));
|
357
|
+
|
358
|
+
/* safety checks */
|
359
|
+
if (!sourceSize) {
|
360
|
+
memset(count, 0, maxSymbolValue + 1);
|
361
|
+
*maxSymbolValuePtr = 0;
|
362
|
+
return 0;
|
363
|
+
}
|
364
|
+
if (!maxSymbolValue) maxSymbolValue = 255; /* 0 == default */
|
365
|
+
|
366
|
+
/* by stripes of 16 bytes */
|
367
|
+
{ U32 cached = MEM_read32(ip); ip += 4;
|
368
|
+
while (ip < iend-15) {
|
369
|
+
U32 c = cached; cached = MEM_read32(ip); ip += 4;
|
370
|
+
Counting1[(BYTE) c ]++;
|
371
|
+
Counting2[(BYTE)(c>>8) ]++;
|
372
|
+
Counting3[(BYTE)(c>>16)]++;
|
373
|
+
Counting4[ c>>24 ]++;
|
374
|
+
c = cached; cached = MEM_read32(ip); ip += 4;
|
375
|
+
Counting1[(BYTE) c ]++;
|
376
|
+
Counting2[(BYTE)(c>>8) ]++;
|
377
|
+
Counting3[(BYTE)(c>>16)]++;
|
378
|
+
Counting4[ c>>24 ]++;
|
379
|
+
c = cached; cached = MEM_read32(ip); ip += 4;
|
380
|
+
Counting1[(BYTE) c ]++;
|
381
|
+
Counting2[(BYTE)(c>>8) ]++;
|
382
|
+
Counting3[(BYTE)(c>>16)]++;
|
383
|
+
Counting4[ c>>24 ]++;
|
384
|
+
c = cached; cached = MEM_read32(ip); ip += 4;
|
385
|
+
Counting1[(BYTE) c ]++;
|
386
|
+
Counting2[(BYTE)(c>>8) ]++;
|
387
|
+
Counting3[(BYTE)(c>>16)]++;
|
388
|
+
Counting4[ c>>24 ]++;
|
389
|
+
}
|
390
|
+
ip-=4;
|
391
|
+
}
|
392
|
+
|
393
|
+
/* finish last symbols */
|
394
|
+
while (ip<iend) Counting1[*ip++]++;
|
395
|
+
|
396
|
+
if (checkMax) { /* verify stats will fit into destination table */
|
397
|
+
U32 s; for (s=255; s>maxSymbolValue; s--) {
|
398
|
+
Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
|
399
|
+
if (Counting1[s]) return ERROR(maxSymbolValue_tooSmall);
|
400
|
+
} }
|
401
|
+
|
402
|
+
{ U32 s; for (s=0; s<=maxSymbolValue; s++) {
|
403
|
+
count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];
|
404
|
+
if (count[s] > max) max = count[s];
|
405
|
+
} }
|
406
|
+
|
407
|
+
while (!count[maxSymbolValue]) maxSymbolValue--;
|
408
|
+
*maxSymbolValuePtr = maxSymbolValue;
|
409
|
+
return (size_t)max;
|
410
|
+
}
|
411
|
+
|
412
|
+
/* FSE_countFast_wksp() :
|
413
|
+
* Same as FSE_countFast(), but using an externally provided scratch buffer.
|
414
|
+
* `workSpace` size must be table of >= `1024` unsigned */
|
415
|
+
size_t FSE_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
|
416
|
+
const void* source, size_t sourceSize, unsigned* workSpace)
|
417
|
+
{
|
418
|
+
if (sourceSize < 1500) return FSE_count_simple(count, maxSymbolValuePtr, source, sourceSize);
|
419
|
+
return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 0, workSpace);
|
420
|
+
}
|
421
|
+
|
422
|
+
/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */
|
423
|
+
size_t FSE_countFast(unsigned* count, unsigned* maxSymbolValuePtr,
|
424
|
+
const void* source, size_t sourceSize)
|
425
|
+
{
|
426
|
+
unsigned tmpCounters[1024];
|
427
|
+
return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters);
|
428
|
+
}
|
429
|
+
|
430
|
+
/* FSE_count_wksp() :
|
431
|
+
* Same as FSE_count(), but using an externally provided scratch buffer.
|
432
|
+
* `workSpace` size must be table of >= `1024` unsigned */
|
433
|
+
size_t FSE_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
|
434
|
+
const void* source, size_t sourceSize, unsigned* workSpace)
|
435
|
+
{
|
436
|
+
if (*maxSymbolValuePtr < 255)
|
437
|
+
return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 1, workSpace);
|
438
|
+
*maxSymbolValuePtr = 255;
|
439
|
+
return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace);
|
440
|
+
}
|
441
|
+
|
442
|
+
size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr,
|
443
|
+
const void* src, size_t srcSize)
|
444
|
+
{
|
445
|
+
unsigned tmpCounters[1024];
|
446
|
+
return FSE_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters);
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
/*-**************************************************************
|
452
|
+
* FSE Compression Code
|
453
|
+
****************************************************************/
|
454
|
+
/*! FSE_sizeof_CTable() :
|
455
|
+
FSE_CTable is a variable size structure which contains :
|
456
|
+
`U16 tableLog;`
|
457
|
+
`U16 maxSymbolValue;`
|
458
|
+
`U16 nextStateNumber[1 << tableLog];` // This size is variable
|
459
|
+
`FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1];` // This size is variable
|
460
|
+
Allocation is manual (C standard does not support variable-size structures).
|
461
|
+
*/
|
462
|
+
size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog)
|
463
|
+
{
|
464
|
+
if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
|
465
|
+
return FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
|
466
|
+
}
|
467
|
+
|
468
|
+
FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
|
469
|
+
{
|
470
|
+
size_t size;
|
471
|
+
if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
|
472
|
+
size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
|
473
|
+
return (FSE_CTable*)malloc(size);
|
474
|
+
}
|
475
|
+
|
476
|
+
void FSE_freeCTable (FSE_CTable* ct) { free(ct); }
|
477
|
+
|
478
|
+
/* provides the minimum logSize to safely represent a distribution */
|
479
|
+
static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
|
480
|
+
{
|
481
|
+
U32 minBitsSrc = BIT_highbit32((U32)(srcSize - 1)) + 1;
|
482
|
+
U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2;
|
483
|
+
U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;
|
484
|
+
return minBits;
|
485
|
+
}
|
486
|
+
|
487
|
+
unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
|
488
|
+
{
|
489
|
+
U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus;
|
490
|
+
U32 tableLog = maxTableLog;
|
491
|
+
U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);
|
492
|
+
if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
|
493
|
+
if (maxBitsSrc < tableLog) tableLog = maxBitsSrc; /* Accuracy can be reduced */
|
494
|
+
if (minBits > tableLog) tableLog = minBits; /* Need a minimum to safely represent all symbol values */
|
495
|
+
if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
|
496
|
+
if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
|
497
|
+
return tableLog;
|
498
|
+
}
|
499
|
+
|
500
|
+
unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
|
501
|
+
{
|
502
|
+
return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);
|
503
|
+
}
|
504
|
+
|
505
|
+
|
506
|
+
/* Secondary normalization method.
|
507
|
+
To be used when primary method fails. */
|
508
|
+
|
509
|
+
static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue)
|
510
|
+
{
|
511
|
+
U32 s;
|
512
|
+
U32 distributed = 0;
|
513
|
+
U32 ToDistribute;
|
514
|
+
|
515
|
+
/* Init */
|
516
|
+
U32 const lowThreshold = (U32)(total >> tableLog);
|
517
|
+
U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
|
518
|
+
|
519
|
+
for (s=0; s<=maxSymbolValue; s++) {
|
520
|
+
if (count[s] == 0) {
|
521
|
+
norm[s]=0;
|
522
|
+
continue;
|
523
|
+
}
|
524
|
+
if (count[s] <= lowThreshold) {
|
525
|
+
norm[s] = -1;
|
526
|
+
distributed++;
|
527
|
+
total -= count[s];
|
528
|
+
continue;
|
529
|
+
}
|
530
|
+
if (count[s] <= lowOne) {
|
531
|
+
norm[s] = 1;
|
532
|
+
distributed++;
|
533
|
+
total -= count[s];
|
534
|
+
continue;
|
535
|
+
}
|
536
|
+
norm[s]=-2;
|
537
|
+
}
|
538
|
+
ToDistribute = (1 << tableLog) - distributed;
|
539
|
+
|
540
|
+
if ((total / ToDistribute) > lowOne) {
|
541
|
+
/* risk of rounding to zero */
|
542
|
+
lowOne = (U32)((total * 3) / (ToDistribute * 2));
|
543
|
+
for (s=0; s<=maxSymbolValue; s++) {
|
544
|
+
if ((norm[s] == -2) && (count[s] <= lowOne)) {
|
545
|
+
norm[s] = 1;
|
546
|
+
distributed++;
|
547
|
+
total -= count[s];
|
548
|
+
continue;
|
549
|
+
} }
|
550
|
+
ToDistribute = (1 << tableLog) - distributed;
|
551
|
+
}
|
552
|
+
|
553
|
+
if (distributed == maxSymbolValue+1) {
|
554
|
+
/* all values are pretty poor;
|
555
|
+
probably incompressible data (should have already been detected);
|
556
|
+
find max, then give all remaining points to max */
|
557
|
+
U32 maxV = 0, maxC = 0;
|
558
|
+
for (s=0; s<=maxSymbolValue; s++)
|
559
|
+
if (count[s] > maxC) maxV=s, maxC=count[s];
|
560
|
+
norm[maxV] += (short)ToDistribute;
|
561
|
+
return 0;
|
562
|
+
}
|
563
|
+
|
564
|
+
{ U64 const vStepLog = 62 - tableLog;
|
565
|
+
U64 const mid = (1ULL << (vStepLog-1)) - 1;
|
566
|
+
U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total; /* scale on remaining */
|
567
|
+
U64 tmpTotal = mid;
|
568
|
+
for (s=0; s<=maxSymbolValue; s++) {
|
569
|
+
if (norm[s]==-2) {
|
570
|
+
U64 const end = tmpTotal + (count[s] * rStep);
|
571
|
+
U32 const sStart = (U32)(tmpTotal >> vStepLog);
|
572
|
+
U32 const sEnd = (U32)(end >> vStepLog);
|
573
|
+
U32 const weight = sEnd - sStart;
|
574
|
+
if (weight < 1)
|
575
|
+
return ERROR(GENERIC);
|
576
|
+
norm[s] = (short)weight;
|
577
|
+
tmpTotal = end;
|
578
|
+
} } }
|
579
|
+
|
580
|
+
return 0;
|
581
|
+
}
|
582
|
+
|
583
|
+
|
584
|
+
size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
|
585
|
+
const unsigned* count, size_t total,
|
586
|
+
unsigned maxSymbolValue)
|
587
|
+
{
|
588
|
+
/* Sanity checks */
|
589
|
+
if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
|
590
|
+
if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported size */
|
591
|
+
if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported size */
|
592
|
+
if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */
|
593
|
+
|
594
|
+
{ U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
|
595
|
+
U64 const scale = 62 - tableLog;
|
596
|
+
U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */
|
597
|
+
U64 const vStep = 1ULL<<(scale-20);
|
598
|
+
int stillToDistribute = 1<<tableLog;
|
599
|
+
unsigned s;
|
600
|
+
unsigned largest=0;
|
601
|
+
short largestP=0;
|
602
|
+
U32 lowThreshold = (U32)(total >> tableLog);
|
603
|
+
|
604
|
+
for (s=0; s<=maxSymbolValue; s++) {
|
605
|
+
if (count[s] == total) return 0; /* rle special case */
|
606
|
+
if (count[s] == 0) { normalizedCounter[s]=0; continue; }
|
607
|
+
if (count[s] <= lowThreshold) {
|
608
|
+
normalizedCounter[s] = -1;
|
609
|
+
stillToDistribute--;
|
610
|
+
} else {
|
611
|
+
short proba = (short)((count[s]*step) >> scale);
|
612
|
+
if (proba<8) {
|
613
|
+
U64 restToBeat = vStep * rtbTable[proba];
|
614
|
+
proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
|
615
|
+
}
|
616
|
+
if (proba > largestP) largestP=proba, largest=s;
|
617
|
+
normalizedCounter[s] = proba;
|
618
|
+
stillToDistribute -= proba;
|
619
|
+
} }
|
620
|
+
if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
|
621
|
+
/* corner case, need another normalization method */
|
622
|
+
size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
|
623
|
+
if (FSE_isError(errorCode)) return errorCode;
|
624
|
+
}
|
625
|
+
else normalizedCounter[largest] += (short)stillToDistribute;
|
626
|
+
}
|
627
|
+
|
628
|
+
#if 0
|
629
|
+
{ /* Print Table (debug) */
|
630
|
+
U32 s;
|
631
|
+
U32 nTotal = 0;
|
632
|
+
for (s=0; s<=maxSymbolValue; s++)
|
633
|
+
printf("%3i: %4i \n", s, normalizedCounter[s]);
|
634
|
+
for (s=0; s<=maxSymbolValue; s++)
|
635
|
+
nTotal += abs(normalizedCounter[s]);
|
636
|
+
if (nTotal != (1U<<tableLog))
|
637
|
+
printf("Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
|
638
|
+
getchar();
|
639
|
+
}
|
640
|
+
#endif
|
641
|
+
|
642
|
+
return tableLog;
|
643
|
+
}
|
644
|
+
|
645
|
+
|
646
|
+
/* fake FSE_CTable, for raw (uncompressed) input */
|
647
|
+
size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits)
|
648
|
+
{
|
649
|
+
const unsigned tableSize = 1 << nbBits;
|
650
|
+
const unsigned tableMask = tableSize - 1;
|
651
|
+
const unsigned maxSymbolValue = tableMask;
|
652
|
+
void* const ptr = ct;
|
653
|
+
U16* const tableU16 = ( (U16*) ptr) + 2;
|
654
|
+
void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableSize>>1); /* assumption : tableLog >= 1 */
|
655
|
+
FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
|
656
|
+
unsigned s;
|
657
|
+
|
658
|
+
/* Sanity checks */
|
659
|
+
if (nbBits < 1) return ERROR(GENERIC); /* min size */
|
660
|
+
|
661
|
+
/* header */
|
662
|
+
tableU16[-2] = (U16) nbBits;
|
663
|
+
tableU16[-1] = (U16) maxSymbolValue;
|
664
|
+
|
665
|
+
/* Build table */
|
666
|
+
for (s=0; s<tableSize; s++)
|
667
|
+
tableU16[s] = (U16)(tableSize + s);
|
668
|
+
|
669
|
+
/* Build Symbol Transformation Table */
|
670
|
+
{ const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
|
671
|
+
for (s=0; s<=maxSymbolValue; s++) {
|
672
|
+
symbolTT[s].deltaNbBits = deltaNbBits;
|
673
|
+
symbolTT[s].deltaFindState = s-1;
|
674
|
+
} }
|
675
|
+
|
676
|
+
return 0;
|
677
|
+
}
|
678
|
+
|
679
|
+
/* fake FSE_CTable, for rle input (always same symbol) */
|
680
|
+
size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue)
|
681
|
+
{
|
682
|
+
void* ptr = ct;
|
683
|
+
U16* tableU16 = ( (U16*) ptr) + 2;
|
684
|
+
void* FSCTptr = (U32*)ptr + 2;
|
685
|
+
FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr;
|
686
|
+
|
687
|
+
/* header */
|
688
|
+
tableU16[-2] = (U16) 0;
|
689
|
+
tableU16[-1] = (U16) symbolValue;
|
690
|
+
|
691
|
+
/* Build table */
|
692
|
+
tableU16[0] = 0;
|
693
|
+
tableU16[1] = 0; /* just in case */
|
694
|
+
|
695
|
+
/* Build Symbol Transformation Table */
|
696
|
+
symbolTT[symbolValue].deltaNbBits = 0;
|
697
|
+
symbolTT[symbolValue].deltaFindState = 0;
|
698
|
+
|
699
|
+
return 0;
|
700
|
+
}
|
701
|
+
|
702
|
+
|
703
|
+
static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
|
704
|
+
const void* src, size_t srcSize,
|
705
|
+
const FSE_CTable* ct, const unsigned fast)
|
706
|
+
{
|
707
|
+
const BYTE* const istart = (const BYTE*) src;
|
708
|
+
const BYTE* const iend = istart + srcSize;
|
709
|
+
const BYTE* ip=iend;
|
710
|
+
|
711
|
+
BIT_CStream_t bitC;
|
712
|
+
FSE_CState_t CState1, CState2;
|
713
|
+
|
714
|
+
/* init */
|
715
|
+
if (srcSize <= 2) return 0;
|
716
|
+
{ size_t const initError = BIT_initCStream(&bitC, dst, dstSize);
|
717
|
+
if (FSE_isError(initError)) return 0; /* not enough space available to write a bitstream */ }
|
718
|
+
|
719
|
+
#define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
|
720
|
+
|
721
|
+
if (srcSize & 1) {
|
722
|
+
FSE_initCState2(&CState1, ct, *--ip);
|
723
|
+
FSE_initCState2(&CState2, ct, *--ip);
|
724
|
+
FSE_encodeSymbol(&bitC, &CState1, *--ip);
|
725
|
+
FSE_FLUSHBITS(&bitC);
|
726
|
+
} else {
|
727
|
+
FSE_initCState2(&CState2, ct, *--ip);
|
728
|
+
FSE_initCState2(&CState1, ct, *--ip);
|
729
|
+
}
|
730
|
+
|
731
|
+
/* join to mod 4 */
|
732
|
+
srcSize -= 2;
|
733
|
+
if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) { /* test bit 2 */
|
734
|
+
FSE_encodeSymbol(&bitC, &CState2, *--ip);
|
735
|
+
FSE_encodeSymbol(&bitC, &CState1, *--ip);
|
736
|
+
FSE_FLUSHBITS(&bitC);
|
737
|
+
}
|
738
|
+
|
739
|
+
/* 2 or 4 encoding per loop */
|
740
|
+
while ( ip>istart ) {
|
741
|
+
|
742
|
+
FSE_encodeSymbol(&bitC, &CState2, *--ip);
|
743
|
+
|
744
|
+
if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */
|
745
|
+
FSE_FLUSHBITS(&bitC);
|
746
|
+
|
747
|
+
FSE_encodeSymbol(&bitC, &CState1, *--ip);
|
748
|
+
|
749
|
+
if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) { /* this test must be static */
|
750
|
+
FSE_encodeSymbol(&bitC, &CState2, *--ip);
|
751
|
+
FSE_encodeSymbol(&bitC, &CState1, *--ip);
|
752
|
+
}
|
753
|
+
|
754
|
+
FSE_FLUSHBITS(&bitC);
|
755
|
+
}
|
756
|
+
|
757
|
+
FSE_flushCState(&bitC, &CState2);
|
758
|
+
FSE_flushCState(&bitC, &CState1);
|
759
|
+
return BIT_closeCStream(&bitC);
|
760
|
+
}
|
761
|
+
|
762
|
+
size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
|
763
|
+
const void* src, size_t srcSize,
|
764
|
+
const FSE_CTable* ct)
|
765
|
+
{
|
766
|
+
unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));
|
767
|
+
|
768
|
+
if (fast)
|
769
|
+
return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);
|
770
|
+
else
|
771
|
+
return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);
|
772
|
+
}
|
773
|
+
|
774
|
+
|
775
|
+
size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
|
776
|
+
|
777
|
+
#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return f
|
778
|
+
#define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
|
779
|
+
|
780
|
+
/* FSE_compress_wksp() :
|
781
|
+
* Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
|
782
|
+
* `wkspSize` size must be `(1<<tableLog)`.
|
783
|
+
*/
|
784
|
+
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)
|
785
|
+
{
|
786
|
+
BYTE* const ostart = (BYTE*) dst;
|
787
|
+
BYTE* op = ostart;
|
788
|
+
BYTE* const oend = ostart + dstSize;
|
789
|
+
|
790
|
+
U32 count[FSE_MAX_SYMBOL_VALUE+1];
|
791
|
+
S16 norm[FSE_MAX_SYMBOL_VALUE+1];
|
792
|
+
FSE_CTable* CTable = (FSE_CTable*)workSpace;
|
793
|
+
size_t const CTableSize = FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue);
|
794
|
+
void* scratchBuffer = (void*)(CTable + CTableSize);
|
795
|
+
size_t const scratchBufferSize = wkspSize - (CTableSize * sizeof(FSE_CTable));
|
796
|
+
|
797
|
+
/* init conditions */
|
798
|
+
if (wkspSize < FSE_WKSP_SIZE_U32(tableLog, maxSymbolValue)) return ERROR(tableLog_tooLarge);
|
799
|
+
if (srcSize <= 1) return 0; /* Not compressible */
|
800
|
+
if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
|
801
|
+
if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG;
|
802
|
+
|
803
|
+
/* Scan input and build symbol stats */
|
804
|
+
{ CHECK_V_F(maxCount, FSE_count(count, &maxSymbolValue, src, srcSize) );
|
805
|
+
if (maxCount == srcSize) return 1; /* only a single symbol in src : rle */
|
806
|
+
if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */
|
807
|
+
if (maxCount < (srcSize >> 7)) return 0; /* Heuristic : not compressible enough */
|
808
|
+
}
|
809
|
+
|
810
|
+
tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue);
|
811
|
+
CHECK_F( FSE_normalizeCount(norm, tableLog, count, srcSize, maxSymbolValue) );
|
812
|
+
|
813
|
+
/* Write table description header */
|
814
|
+
{ CHECK_V_F(nc_err, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) );
|
815
|
+
op += nc_err;
|
816
|
+
}
|
817
|
+
|
818
|
+
/* Compress */
|
819
|
+
CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, scratchBufferSize) );
|
820
|
+
{ CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, src, srcSize, CTable) );
|
821
|
+
if (cSize == 0) return 0; /* not enough space for compressed data */
|
822
|
+
op += cSize;
|
823
|
+
}
|
824
|
+
|
825
|
+
/* check compressibility */
|
826
|
+
if ( (size_t)(op-ostart) >= srcSize-1 ) return 0;
|
827
|
+
|
828
|
+
return op-ostart;
|
829
|
+
}
|
830
|
+
|
831
|
+
typedef struct {
|
832
|
+
FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)];
|
833
|
+
BYTE scratchBuffer[1 << FSE_MAX_TABLELOG];
|
834
|
+
} fseWkspMax_t;
|
835
|
+
|
836
|
+
size_t FSE_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)
|
837
|
+
{
|
838
|
+
fseWkspMax_t scratchBuffer;
|
839
|
+
FSE_STATIC_ASSERT(sizeof(scratchBuffer) >= FSE_WKSP_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)); /* compilation failures here means scratchBuffer is not large enough */
|
840
|
+
if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
|
841
|
+
return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, &scratchBuffer, sizeof(scratchBuffer));
|
842
|
+
}
|
843
|
+
|
844
|
+
size_t FSE_compress (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
845
|
+
{
|
846
|
+
return FSE_compress2(dst, dstCapacity, src, srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG);
|
847
|
+
}
|
848
|
+
|
849
|
+
|
850
|
+
#endif /* FSE_COMMONDEFS_ONLY */
|