blob: 2861fd9f108e4b3088075844fff30215ed58961c [file] [log] [blame]
Yann Collet4856a002015-01-24 01:58:16 +01001/* ******************************************************************
2 FSE : Finite State Entropy coder
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#ifndef FSE_COMMONDEFS_ONLY
36
37/****************************************************************
38* Tuning parameters
39****************************************************************/
40/* MEMORY_USAGE :
41* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
42* Increasing memory usage improves compression ratio
43* Reduced memory usage can improve speed, due to cache effect
44* Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
45#define FSE_MAX_MEMORY_USAGE 14
46#define FSE_DEFAULT_MEMORY_USAGE 13
47
48/* FSE_MAX_SYMBOL_VALUE :
49* Maximum symbol value authorized.
50* Required for proper stack allocation */
51#define FSE_MAX_SYMBOL_VALUE 255
52
53
54/****************************************************************
55* Generic function type & suffix (C template emulation)
56****************************************************************/
57#define FSE_FUNCTION_TYPE BYTE
58#define FSE_FUNCTION_EXTENSION
59
60#endif /* !FSE_COMMONDEFS_ONLY */
61
62
63/****************************************************************
64* Compiler specifics
65****************************************************************/
66#ifdef _MSC_VER /* Visual Studio */
67# define FORCE_INLINE static __forceinline
68# include <intrin.h> /* For Visual 2005 */
69# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
70# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
71#else
72# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
73# ifdef __GNUC__
74# define FORCE_INLINE static inline __attribute__((always_inline))
75# else
76# define FORCE_INLINE static inline
77# endif
78#endif
79
80
81/****************************************************************
82* Includes
83****************************************************************/
84#include <stdlib.h> /* malloc, free, qsort */
85#include <string.h> /* memcpy, memset */
86#include <stdio.h> /* printf (debug) */
87#include "fse_static.h"
88
89
Yann Colletad68a0e2015-02-26 00:29:15 +010090#ifndef MEM_ACCESS_MODULE
91#define MEM_ACCESS_MODULE
Yann Collet4856a002015-01-24 01:58:16 +010092/****************************************************************
93* Basic Types
94*****************************************************************/
95#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
96# include <stdint.h>
97typedef uint8_t BYTE;
98typedef uint16_t U16;
99typedef int16_t S16;
100typedef uint32_t U32;
101typedef int32_t S32;
102typedef uint64_t U64;
103typedef int64_t S64;
104#else
105typedef unsigned char BYTE;
106typedef unsigned short U16;
107typedef signed short S16;
108typedef unsigned int U32;
109typedef signed int S32;
110typedef unsigned long long U64;
111typedef signed long long S64;
112#endif
113
Yann Colletad68a0e2015-02-26 00:29:15 +0100114#endif /* MEM_ACCESS_MODULE */
Yann Collet4856a002015-01-24 01:58:16 +0100115
116/****************************************************************
117* Memory I/O
118*****************************************************************/
119static unsigned FSE_isLittleEndian(void)
120{
121 const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
122 return one.c[0];
123}
124
125static U32 FSE_read32(const void* memPtr)
126{
127 U32 val32;
128 memcpy(&val32, memPtr, 4);
129 return val32;
130}
131
132static U32 FSE_readLE32(const void* memPtr)
133{
134 if (FSE_isLittleEndian())
135 return FSE_read32(memPtr);
136 else
137 {
Yann Collet1cc58de2015-01-29 07:13:54 +0100138 const BYTE* p = (const BYTE*)memPtr;
Yann Collet4856a002015-01-24 01:58:16 +0100139 return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
140 }
141}
142
143static void FSE_writeLE32(void* memPtr, U32 val32)
144{
145 if (FSE_isLittleEndian())
146 {
147 memcpy(memPtr, &val32, 4);
148 }
149 else
150 {
Yann Collet1cc58de2015-01-29 07:13:54 +0100151 BYTE* p = (BYTE*)memPtr;
Yann Collet4856a002015-01-24 01:58:16 +0100152 p[0] = (BYTE)val32;
153 p[1] = (BYTE)(val32>>8);
154 p[2] = (BYTE)(val32>>16);
155 p[3] = (BYTE)(val32>>24);
156 }
157}
158
159static U64 FSE_read64(const void* memPtr)
160{
161 U64 val64;
162 memcpy(&val64, memPtr, 8);
163 return val64;
164}
165
166static U64 FSE_readLE64(const void* memPtr)
167{
168 if (FSE_isLittleEndian())
169 return FSE_read64(memPtr);
170 else
171 {
Yann Collet1cc58de2015-01-29 07:13:54 +0100172 const BYTE* p = (const BYTE*)memPtr;
Yann Collet4856a002015-01-24 01:58:16 +0100173 return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
174 + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
175 }
176}
177
178static void FSE_writeLE64(void* memPtr, U64 val64)
179{
180 if (FSE_isLittleEndian())
181 {
182 memcpy(memPtr, &val64, 8);
183 }
184 else
185 {
Yann Collet1cc58de2015-01-29 07:13:54 +0100186 BYTE* p = (BYTE*)memPtr;
Yann Collet4856a002015-01-24 01:58:16 +0100187 p[0] = (BYTE)val64;
188 p[1] = (BYTE)(val64>>8);
189 p[2] = (BYTE)(val64>>16);
190 p[3] = (BYTE)(val64>>24);
191 p[4] = (BYTE)(val64>>32);
192 p[5] = (BYTE)(val64>>40);
193 p[6] = (BYTE)(val64>>48);
194 p[7] = (BYTE)(val64>>56);
195 }
196}
197
198static size_t FSE_readLEST(const void* memPtr)
199{
200 if (sizeof(size_t)==4)
Yann Colletfb814172015-01-25 13:19:12 +0100201 return (size_t)FSE_readLE32(memPtr);
Yann Collet4856a002015-01-24 01:58:16 +0100202 else
Yann Colletfb814172015-01-25 13:19:12 +0100203 return (size_t)FSE_readLE64(memPtr);
Yann Collet4856a002015-01-24 01:58:16 +0100204}
205
206static void FSE_writeLEST(void* memPtr, size_t val)
207{
208 if (sizeof(size_t)==4)
209 FSE_writeLE32(memPtr, (U32)val);
210 else
211 FSE_writeLE64(memPtr, (U64)val);
212}
213
214
215/****************************************************************
216* Constants
217*****************************************************************/
218#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
219#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
220#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
221#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
222#define FSE_MIN_TABLELOG 5
223
224#define FSE_TABLELOG_ABSOLUTE_MAX 15
225#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
226#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
227#endif
228
229
230/****************************************************************
231* Error Management
232****************************************************************/
233#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
234
235
236/****************************************************************
237* Complex types
238****************************************************************/
239typedef struct
240{
241 int deltaFindState;
242 U16 maxState;
243 BYTE minBitsOut;
244 /* one byte padding */
245} FSE_symbolCompressionTransform;
246
247typedef struct
248{
249 U32 fakeTable[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)]; /* compatible with FSE_compressU16() */
250} CTable_max_t;
251
252
253/****************************************************************
254* Internal functions
255****************************************************************/
256FORCE_INLINE unsigned FSE_highbit32 (register U32 val)
257{
258# if defined(_MSC_VER) /* Visual */
259 unsigned long r;
260 _BitScanReverse ( &r, val );
261 return (unsigned) r;
262# elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */
263 return 31 - __builtin_clz (val);
264# else /* Software version */
265 static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
266 U32 v = val;
267 unsigned r;
268 v |= v >> 1;
269 v |= v >> 2;
270 v |= v >> 4;
271 v |= v >> 8;
272 v |= v >> 16;
273 r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
274 return r;
275# endif
276}
277
278
279#ifndef FSE_COMMONDEFS_ONLY
280
281unsigned FSE_isError(size_t code) { return (code > (size_t)(-FSE_ERROR_maxCode)); }
282
283#define FSE_GENERATE_STRING(STRING) #STRING,
284static const char* FSE_errorStrings[] = { FSE_LIST_ERRORS(FSE_GENERATE_STRING) };
285
286const char* FSE_getErrorName(size_t code)
287{
288 static const char* codeError = "Unspecified error code";
289 if (FSE_isError(code)) return FSE_errorStrings[-(int)(code)];
290 return codeError;
291}
292
293static short FSE_abs(short a)
294{
295 return a<0? -a : a;
296}
297
298
299/****************************************************************
300* Header bitstream management
301****************************************************************/
302size_t FSE_headerBound(unsigned maxSymbolValue, unsigned tableLog)
303{
304 size_t maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 1;
305 return maxSymbolValue ? maxHeaderSize : FSE_MAX_HEADERSIZE;
306}
307
308static size_t FSE_writeHeader_generic (void* header, size_t headerBufferSize,
309 const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
310 unsigned safeWrite)
311{
312 BYTE* const ostart = (BYTE*) header;
313 BYTE* out = ostart;
314 BYTE* const oend = ostart + headerBufferSize;
315 int nbBits;
316 const int tableSize = 1 << tableLog;
317 int remaining;
318 int threshold;
319 U32 bitStream;
320 int bitCount;
321 unsigned charnum = 0;
322 int previous0 = 0;
323
324 bitStream = 0;
325 bitCount = 0;
326 /* Table Size */
327 bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
328 bitCount += 4;
329
330 /* Init */
331 remaining = tableSize+1; /* +1 for extra accuracy */
332 threshold = tableSize;
333 nbBits = tableLog+1;
334
335 while (remaining>1) /* stops at 1 */
336 {
337 if (previous0)
338 {
339 unsigned start = charnum;
340 while (!normalizedCounter[charnum]) charnum++;
341 while (charnum >= start+24)
342 {
343 start+=24;
344 bitStream += 0xFFFF<<bitCount;
345 if ((!safeWrite) && (out > oend-2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
346 out[0] = (BYTE)bitStream;
347 out[1] = (BYTE)(bitStream>>8);
348 out+=2;
349 bitStream>>=16;
350 }
351 while (charnum >= start+3)
352 {
353 start+=3;
354 bitStream += 3 << bitCount;
355 bitCount += 2;
356 }
357 bitStream += (charnum-start) << bitCount;
358 bitCount += 2;
359 if (bitCount>16)
360 {
361 if ((!safeWrite) && (out > oend - 2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
362 out[0] = (BYTE)bitStream;
363 out[1] = (BYTE)(bitStream>>8);
364 out += 2;
365 bitStream >>= 16;
366 bitCount -= 16;
367 }
368 }
369 {
370 short count = normalizedCounter[charnum++];
371 const short max = (short)((2*threshold-1)-remaining);
372 remaining -= FSE_abs(count);
373 if (remaining<0) return (size_t)-FSE_ERROR_GENERIC;
374 count++; /* +1 for extra accuracy */
375 if (count>=threshold) count += max; /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
376 bitStream += count << bitCount;
377 bitCount += nbBits;
378 bitCount -= (count<max);
379 previous0 = (count==1);
380 while (remaining<threshold) nbBits--, threshold>>=1;
381 }
382 if (bitCount>16)
383 {
384 if ((!safeWrite) && (out > oend - 2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
385 out[0] = (BYTE)bitStream;
386 out[1] = (BYTE)(bitStream>>8);
387 out += 2;
388 bitStream >>= 16;
389 bitCount -= 16;
390 }
391 }
392
393 /* flush remaining bitStream */
394 if ((!safeWrite) && (out > oend - 2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
395 out[0] = (BYTE)bitStream;
396 out[1] = (BYTE)(bitStream>>8);
397 out+= (bitCount+7) /8;
398
399 if (charnum > maxSymbolValue + 1) return (size_t)-FSE_ERROR_GENERIC; /* Too many symbols written (a bit too late?) */
400
401 return (out-ostart);
402}
403
404
405size_t FSE_writeHeader (void* header, size_t headerBufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
406{
407 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported */
408 if (tableLog < FSE_MIN_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported */
409
410 if (headerBufferSize < FSE_headerBound(maxSymbolValue, tableLog))
411 return FSE_writeHeader_generic(header, headerBufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
412
413 return FSE_writeHeader_generic(header, headerBufferSize, normalizedCounter, maxSymbolValue, tableLog, 1);
414}
415
416
417size_t FSE_readHeader (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
418 const void* headerBuffer, size_t hbSize)
419{
420 const BYTE* const istart = (const BYTE*) headerBuffer;
421 const BYTE* ip = istart;
422 int nbBits;
423 int remaining;
424 int threshold;
425 U32 bitStream;
426 int bitCount;
427 unsigned charnum = 0;
428 int previous0 = 0;
429
430 bitStream = FSE_readLE32(ip);
431 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
432 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return (size_t)-FSE_ERROR_tableLog_tooLarge;
433 bitStream >>= 4;
434 bitCount = 4;
435 *tableLogPtr = nbBits;
436 remaining = (1<<nbBits)+1;
437 threshold = 1<<nbBits;
438 nbBits++;
439
440 while ((remaining>1) && (charnum<=*maxSVPtr))
441 {
442 if (previous0)
443 {
444 unsigned n0 = charnum;
445 while ((bitStream & 0xFFFF) == 0xFFFF)
446 {
447 n0+=24;
448 ip+=2;
449 bitStream = FSE_readLE32(ip) >> bitCount;
450 }
451 while ((bitStream & 3) == 3)
452 {
453 n0+=3;
454 bitStream>>=2;
455 bitCount+=2;
456 }
457 n0 += bitStream & 3;
458 bitCount += 2;
459 if (n0 > *maxSVPtr) return (size_t)-FSE_ERROR_GENERIC;
460 while (charnum < n0) normalizedCounter[charnum++] = 0;
461 ip += bitCount>>3;
462 bitCount &= 7;
463 bitStream = FSE_readLE32(ip) >> bitCount;
464 }
465 {
466 const short max = (short)((2*threshold-1)-remaining);
467 short count;
468
469 if ((bitStream & (threshold-1)) < (U32)max)
470 {
471 count = (short)(bitStream & (threshold-1));
472 bitCount += nbBits-1;
473 }
474 else
475 {
476 count = (short)(bitStream & (2*threshold-1));
477 if (count >= threshold) count -= max;
478 bitCount += nbBits;
479 }
480
481 count--; /* extra accuracy */
482 remaining -= FSE_abs(count);
483 normalizedCounter[charnum++] = count;
484 previous0 = !count;
485 while (remaining < threshold)
486 {
487 nbBits--;
488 threshold >>= 1;
489 }
490
491 ip += bitCount>>3;
492 bitCount &= 7;
493 bitStream = FSE_readLE32(ip) >> bitCount;
494 }
495 }
496 if (remaining != 1) return (size_t)-FSE_ERROR_GENERIC;
497 *maxSVPtr = charnum-1;
498
499 ip += bitCount>0;
500 if ((size_t)(ip-istart) >= hbSize) return (size_t)-FSE_ERROR_srcSize_wrong; /* arguably a bit late , tbd */
501 return ip-istart;
502}
503
504
505/****************************************************************
506* FSE Compression Code
507****************************************************************/
508/*
509CTable is a variable size structure which contains :
510 U16 tableLog;
511 U16 maxSymbolValue;
512 U16 nextStateNumber[1 << tableLog]; // This size is variable
513 FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1]; // This size is variable
514Allocation is manual, since C standard does not support variable-size structures.
515*/
516
517size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog)
518{
519 size_t size;
520 FSE_STATIC_ASSERT((size_t)FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)*4 >= sizeof(CTable_max_t)); /* A compilation error here means FSE_CTABLE_SIZE_U32 is not large enough */
521 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_GENERIC;
522 size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
523 return size;
524}
525
526void* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
527{
528 size_t size;
529 if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
530 size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
531 return malloc(size);
532}
533
534void FSE_freeCTable (void* CTable)
535{
536 free(CTable);
537}
538
Yann Collet4856a002015-01-24 01:58:16 +0100539
540unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
541{
542 U32 tableLog = maxTableLog;
543 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
544 if ((FSE_highbit32((U32)(srcSize - 1)) - 2) < tableLog) tableLog = FSE_highbit32((U32)(srcSize - 1)) - 2; /* Accuracy can be reduced */
Yann Colletbbfa7d72015-03-23 23:15:22 +0100545 if ((FSE_highbit32(maxSymbolValue)+2) > tableLog) tableLog = FSE_highbit32(maxSymbolValue)+2; /* Need a minimum to safely represent all symbol values */
Yann Collet4856a002015-01-24 01:58:16 +0100546 if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
547 if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
548 return tableLog;
549}
550
551
552typedef struct
553{
554 U32 id;
555 U32 count;
556} rank_t;
557
558int FSE_compareRankT(const void* r1, const void* r2)
559{
Yann Collet1cc58de2015-01-29 07:13:54 +0100560 const rank_t* R1 = (const rank_t*)r1;
561 const rank_t* R2 = (const rank_t*)r2;
Yann Collet4856a002015-01-24 01:58:16 +0100562
563 return 2 * (R1->count < R2->count) - 1;
564}
565
Yann Colleta3c75ba2015-02-21 03:31:59 +0100566
567#if 0
Yann Collet565b81d2015-01-29 06:51:30 +0100568static size_t FSE_adjustNormSlow(short* norm, int pointsToRemove, const unsigned* count, U32 maxSymbolValue)
569{
Yann Collet2ddf7e92015-02-08 20:26:47 +0100570 rank_t rank[FSE_MAX_SYMBOL_VALUE+2];
Yann Collet565b81d2015-01-29 06:51:30 +0100571 U32 s;
572
573 /* Init */
574 for (s=0; s<=maxSymbolValue; s++)
575 {
576 rank[s].id = s;
577 rank[s].count = count[s];
578 if (norm[s] <= 1) rank[s].count = 0;
579 }
Yann Collet2ddf7e92015-02-08 20:26:47 +0100580 rank[maxSymbolValue+1].id = 0;
581 rank[maxSymbolValue+1].count = 0; /* ensures comparison ends here in worst case */
Yann Collet565b81d2015-01-29 06:51:30 +0100582
583 /* Sort according to count */
584 qsort(rank, maxSymbolValue+1, sizeof(rank_t), FSE_compareRankT);
585
586 while(pointsToRemove)
587 {
588 int newRank = 1;
589 rank_t savedR;
590 if (norm[rank[0].id] == 1)
591 return (size_t)-FSE_ERROR_GENERIC;
592 norm[rank[0].id]--;
593 pointsToRemove--;
594 rank[0].count -= (rank[0].count + 6) >> 3;
595 if (norm[rank[0].id] == 1)
596 rank[0].count=0;
597 savedR = rank[0];
598 while (rank[newRank].count > savedR.count)
599 {
600 rank[newRank-1] = rank[newRank];
601 newRank++;
602 }
603 rank[newRank-1] = savedR;
604 }
605
606 return 0;
607}
Yann Collet565b81d2015-01-29 06:51:30 +0100608
Yann Colleta3c75ba2015-02-21 03:31:59 +0100609#else
610
Yann Collet26aa1ec2015-02-24 09:05:58 +0100611/* Secondary normalization method.
612 To be used when primary method fails. */
613
614static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue)
Yann Colleta3c75ba2015-02-21 03:31:59 +0100615{
616 U32 s;
Yann Colleta3c75ba2015-02-21 03:31:59 +0100617 U32 distributed = 0;
618 U32 ToDistribute;
619
620 /* Init */
Yann Colleta3c75ba2015-02-21 03:31:59 +0100621 U32 lowThreshold = (U32)(total >> tableLog);
622 U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
623
624 for (s=0; s<=maxSymbolValue; s++)
625 {
626 if (count[s] == 0)
627 {
628 norm[s]=0;
629 continue;
630 }
631 if (count[s] <= lowThreshold)
632 {
633 norm[s] = -1;
634 distributed++;
635 total -= count[s];
636 continue;
637 }
638 if (count[s] <= lowOne)
639 {
640 norm[s] = 1;
641 distributed++;
642 total -= count[s];
643 continue;
644 }
645 norm[s]=-2;
646 }
647 ToDistribute = (1 << tableLog) - distributed;
648
649 if ((total / ToDistribute) > lowOne)
650 {
651 /* risk of rounding to zero */
652 lowOne = (U32)((total * 3) / (ToDistribute * 2));
653 for (s=0; s<=maxSymbolValue; s++)
654 {
655 if ((norm[s] == -2) && (count[s] <= lowOne))
656 {
657 norm[s] = 1;
658 distributed++;
659 total -= count[s];
660 continue;
661 }
662 }
663 ToDistribute = (1 << tableLog) - distributed;
664 }
665
666 if (distributed == maxSymbolValue+1)
667 {
Yann Collet26aa1ec2015-02-24 09:05:58 +0100668 /* all values are pretty poor;
669 probably incompressible data (should have already been detected);
670 find max, then give all remaining points to max */
Yann Colleta3c75ba2015-02-21 03:31:59 +0100671 U32 maxV = 0, maxC =0;
672 for (s=0; s<=maxSymbolValue; s++)
673 if (count[s] > maxC) maxV=s, maxC=count[s];
674 norm[maxV] += ToDistribute;
675 return 0;
676 }
677
678 {
679 U64 const vStepLog = 62 - tableLog;
680 U64 const mid = (1ULL << (vStepLog-1)) - 1;
681 U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total; /* scale on remaining */
682 U64 tmpTotal = mid;
683 for (s=0; s<=maxSymbolValue; s++)
684 {
685 if (norm[s]==-2)
686 {
687 U64 end = tmpTotal + (count[s] * rStep);
688 U32 sStart = (U32)(tmpTotal >> vStepLog);
689 U32 sEnd = (U32)(end >> vStepLog);
690 U32 weight = sEnd - sStart;
691 if (weight < 1)
692 return (size_t)-FSE_ERROR_GENERIC;
693 norm[s] = weight;
694 tmpTotal = end;
695 }
696 }
697 }
698
699 return 0;
700}
701#endif
702
Yann Collet4856a002015-01-24 01:58:16 +0100703
704size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
705 const unsigned* count, size_t total,
706 unsigned maxSymbolValue)
707{
708 /* Sanity checks */
709 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
710 if (tableLog < FSE_MIN_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported size */
711 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported size */
712 if ((1U<<tableLog) <= maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; /* Too small tableLog, compression potentially impossible */
713
714 {
715 U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
716 U64 const scale = 62 - tableLog;
717 U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */
718 U64 const vStep = 1ULL<<(scale-20);
719 int stillToDistribute = 1<<tableLog;
720 unsigned s;
721 unsigned largest=0;
722 short largestP=0;
723 U32 lowThreshold = (U32)(total >> tableLog);
724
725 for (s=0; s<=maxSymbolValue; s++)
726 {
727 if (count[s] == total) return 0;
728 if (count[s] == 0)
729 {
730 normalizedCounter[s]=0;
731 continue;
732 }
733 if (count[s] <= lowThreshold)
734 {
735 normalizedCounter[s] = -1;
736 stillToDistribute--;
737 }
738 else
739 {
740 short proba = (short)((count[s]*step) >> scale);
741 if (proba<8)
742 {
Yann Collet2ddf7e92015-02-08 20:26:47 +0100743 U64 restToBeat = vStep * rtbTable[proba];
Yann Collet4856a002015-01-24 01:58:16 +0100744 proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
745 }
746 if (proba > largestP)
747 {
748 largestP=proba;
749 largest=s;
750 }
751 normalizedCounter[s] = proba;
752 stillToDistribute -= proba;
753 }
754 }
Yann Collet4856a002015-01-24 01:58:16 +0100755 if (-stillToDistribute >= (normalizedCounter[largest] >> 1))
756 {
Yann Collet26aa1ec2015-02-24 09:05:58 +0100757 /* corner case, need another normalization method */
758 size_t errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
Yann Collet565b81d2015-01-29 06:51:30 +0100759 if (FSE_isError(errorCode)) return errorCode;
Yann Collet4856a002015-01-24 01:58:16 +0100760 }
761 else normalizedCounter[largest] += (short)stillToDistribute;
762 }
763
764#if 0
765 { /* Print Table (debug) */
Yann Collet565b81d2015-01-29 06:51:30 +0100766 U32 s;
767 U32 nTotal = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100768 for (s=0; s<=maxSymbolValue; s++)
769 printf("%3i: %4i \n", s, normalizedCounter[s]);
Yann Collet565b81d2015-01-29 06:51:30 +0100770 for (s=0; s<=maxSymbolValue; s++)
771 nTotal += abs(normalizedCounter[s]);
772 if (nTotal != (1U<<tableLog))
773 printf("Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
Yann Collet4856a002015-01-24 01:58:16 +0100774 getchar();
775 }
776#endif
777
778 return tableLog;
779}
780
781
782/* fake CTable, for raw (uncompressed) input */
783size_t FSE_buildCTable_raw (void* CTable, unsigned nbBits)
784{
785 const unsigned tableSize = 1 << nbBits;
786 const unsigned tableMask = tableSize - 1;
787 const unsigned maxSymbolValue = tableMask;
788 U16* tableU16 = ( (U16*) CTable) + 2;
789 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) ((((U32*)CTable)+1) + (tableSize>>1));
790 unsigned s;
791
792 /* Sanity checks */
793 if (nbBits < 1) return (size_t)-FSE_ERROR_GENERIC; /* min size */
794 if (((size_t)CTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
795
796 /* header */
797 tableU16[-2] = (U16) nbBits;
798 tableU16[-1] = (U16) maxSymbolValue;
799
800 /* Build table */
801 for (s=0; s<tableSize; s++)
802 tableU16[s] = (U16)(tableSize + s);
803
804 /* Build Symbol Transformation Table */
805 for (s=0; s<=maxSymbolValue; s++)
806 {
807 symbolTT[s].minBitsOut = (BYTE)nbBits;
808 symbolTT[s].deltaFindState = s-1;
809 symbolTT[s].maxState = (U16)( (tableSize*2) - 1); /* ensures state <= maxState */
810 }
811
812 return 0;
813}
814
815
816/* fake CTable, for rle (100% always same symbol) input */
817size_t FSE_buildCTable_rle (void* CTable, BYTE symbolValue)
818{
819 const unsigned tableSize = 1;
820 U16* tableU16 = ( (U16*) CTable) + 2;
821 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) ((U32*)CTable + 2);
822
823 /* safety checks */
824 if (((size_t)CTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be 4 bytes aligned */
825
826 /* header */
827 tableU16[-2] = (U16) 0;
828 tableU16[-1] = (U16) symbolValue;
829
830 /* Build table */
831 tableU16[0] = 0;
832 tableU16[1] = 0; /* just in case */
833
834 /* Build Symbol Transformation Table */
835 {
836 symbolTT[symbolValue].minBitsOut = 0;
837 symbolTT[symbolValue].deltaFindState = 0;
838 symbolTT[symbolValue].maxState = (U16)(2*tableSize-1); /* ensures state <= maxState */
839 }
840
841 return 0;
842}
843
844
845void FSE_initCStream(FSE_CStream_t* bitC, void* start)
846{
847 bitC->bitContainer = 0;
848 bitC->bitPos = 0; /* reserved for unusedBits */
849 bitC->startPtr = (char*)start;
850 bitC->ptr = bitC->startPtr;
851}
852
853void FSE_initCState(FSE_CState_t* statePtr, const void* CTable)
854{
855 const U32 tableLog = ( (U16*) CTable) [0];
856 statePtr->value = (ptrdiff_t)1<<tableLog;
857 statePtr->stateTable = ((const U16*) CTable) + 2;
858 statePtr->symbolTT = (const U32*)CTable + 1 + (tableLog ? (1<<(tableLog-1)) : 1);
859 statePtr->stateLog = tableLog;
860}
861
862void FSE_addBits(FSE_CStream_t* bitC, size_t value, unsigned nbBits)
863{
864 static const unsigned mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF }; /* up to 25 bits */
865 bitC->bitContainer |= (value & mask[nbBits]) << bitC->bitPos;
866 bitC->bitPos += nbBits;
867}
868
869void FSE_encodeByte(FSE_CStream_t* bitC, FSE_CState_t* statePtr, BYTE symbol)
870{
871 const FSE_symbolCompressionTransform* const symbolTT = (const FSE_symbolCompressionTransform*) statePtr->symbolTT;
872 const U16* const stateTable = (const U16*) statePtr->stateTable;
873 int nbBitsOut = symbolTT[symbol].minBitsOut;
874 nbBitsOut -= (int)((symbolTT[symbol].maxState - statePtr->value) >> 31);
875 FSE_addBits(bitC, statePtr->value, nbBitsOut);
876 statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT[symbol].deltaFindState];
877}
878
879void FSE_flushBits(FSE_CStream_t* bitC)
880{
881 size_t nbBytes = bitC->bitPos >> 3;
882 FSE_writeLEST(bitC->ptr, bitC->bitContainer);
883 bitC->bitPos &= 7;
884 bitC->ptr += nbBytes;
885 bitC->bitContainer >>= nbBytes*8;
886}
887
888void FSE_flushCState(FSE_CStream_t* bitC, const FSE_CState_t* statePtr)
889{
890 FSE_addBits(bitC, statePtr->value, statePtr->stateLog);
891 FSE_flushBits(bitC);
892}
893
894
895size_t FSE_closeCStream(FSE_CStream_t* bitC)
896{
897 char* endPtr;
898
899 FSE_addBits(bitC, 1, 1);
900 FSE_flushBits(bitC);
901
902 endPtr = bitC->ptr;
903 endPtr += bitC->bitPos > 0;
904
905 return (endPtr - bitC->startPtr);
906}
907
908
909size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
910 const void* src, size_t srcSize,
911 const void* CTable)
912{
913 const BYTE* const istart = (const BYTE*) src;
914 const BYTE* ip;
915 const BYTE* const iend = istart + srcSize;
916
917 FSE_CStream_t bitC;
918 FSE_CState_t CState1, CState2;
919
920
921 /* init */
922 (void)dstSize; /* objective : ensure it fits into dstBuffer (Todo) */
923 FSE_initCStream(&bitC, dst);
924 FSE_initCState(&CState1, CTable);
925 CState2 = CState1;
926
927 ip=iend;
928
929 /* join to even */
930 if (srcSize & 1)
931 {
932 FSE_encodeByte(&bitC, &CState1, *--ip);
933 FSE_flushBits(&bitC);
934 }
935
936 /* join to mod 4 */
937 if ((sizeof(size_t)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) /* test bit 2 */
938 {
939 FSE_encodeByte(&bitC, &CState2, *--ip);
940 FSE_encodeByte(&bitC, &CState1, *--ip);
941 FSE_flushBits(&bitC);
942 }
943
944 /* 2 or 4 encoding per loop */
945 while (ip>istart)
946 {
947 FSE_encodeByte(&bitC, &CState2, *--ip);
948
949 if (sizeof(size_t)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */
950 FSE_flushBits(&bitC);
951
952 FSE_encodeByte(&bitC, &CState1, *--ip);
953
954 if (sizeof(size_t)*8 > FSE_MAX_TABLELOG*4+7 ) /* this test must be static */
955 {
956 FSE_encodeByte(&bitC, &CState2, *--ip);
957 FSE_encodeByte(&bitC, &CState1, *--ip);
958 }
959
960 FSE_flushBits(&bitC);
961 }
962
963 FSE_flushCState(&bitC, &CState2);
964 FSE_flushCState(&bitC, &CState1);
965 return FSE_closeCStream(&bitC);
966}
967
968
Yann Collet4856a002015-01-24 01:58:16 +0100969size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
970
971
972size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)
973{
974 const BYTE* const istart = (const BYTE*) src;
975 const BYTE* ip = istart;
976
977 BYTE* const ostart = (BYTE*) dst;
978 BYTE* op = ostart;
979 BYTE* const oend = ostart + dstSize;
980
981 U32 count[FSE_MAX_SYMBOL_VALUE+1];
982 S16 norm[FSE_MAX_SYMBOL_VALUE+1];
983 CTable_max_t CTable;
984 size_t errorCode;
985
986 /* early out */
987 if (dstSize < FSE_compressBound(srcSize)) return (size_t)-FSE_ERROR_dstSize_tooSmall;
988 if (srcSize <= 1) return srcSize; /* Uncompressed or RLE */
989 if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
990 if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG;
991
992 /* Scan input and build symbol stats */
993 errorCode = FSE_count (count, ip, srcSize, &maxSymbolValue);
994 if (FSE_isError(errorCode)) return errorCode;
Yann Colleta3c75ba2015-02-21 03:31:59 +0100995 if (errorCode == srcSize) return 1;
996 if (errorCode < (srcSize >> 7)) return 0; /* Heuristic : not compressible enough */
Yann Collet4856a002015-01-24 01:58:16 +0100997
998 tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue);
999 errorCode = FSE_normalizeCount (norm, tableLog, count, srcSize, maxSymbolValue);
1000 if (FSE_isError(errorCode)) return errorCode;
1001
1002 /* Write table description header */
1003 errorCode = FSE_writeHeader (op, FSE_MAX_HEADERSIZE, norm, maxSymbolValue, tableLog);
1004 if (FSE_isError(errorCode)) return errorCode;
1005 op += errorCode;
1006
1007 /* Compress */
1008 errorCode = FSE_buildCTable (&CTable, norm, maxSymbolValue, tableLog);
1009 if (FSE_isError(errorCode)) return errorCode;
1010 op += FSE_compress_usingCTable(op, oend - op, ip, srcSize, &CTable);
1011
1012 /* check compressibility */
1013 if ( (size_t)(op-ostart) >= srcSize-1 )
1014 return 0;
1015
1016 return op-ostart;
1017}
1018
1019
1020size_t FSE_compress (void* dst, size_t dstSize, const void* src, size_t srcSize)
1021{
1022 return FSE_compress2(dst, dstSize, src, (U32)srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG);
1023}
1024
1025
1026/*********************************************************
1027* Decompression (Byte symbols)
1028*********************************************************/
1029typedef struct
1030{
1031 U16 newState;
1032 BYTE symbol;
1033 BYTE nbBits;
1034} FSE_decode_t; /* size == U32 */
1035
1036/* Specific corner case : RLE compression */
1037size_t FSE_decompressRLE(void* dst, size_t originalSize,
1038 const void* cSrc, size_t cSrcSize)
1039{
1040 if (cSrcSize != 1) return (size_t)-FSE_ERROR_srcSize_wrong;
1041 memset(dst, *(BYTE*)cSrc, originalSize);
1042 return originalSize;
1043}
1044
1045
1046size_t FSE_buildDTable_rle (void* DTable, BYTE symbolValue)
1047{
Yann Collet1cc58de2015-01-29 07:13:54 +01001048 U32* const base32 = (U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001049 FSE_decode_t* const cell = (FSE_decode_t*)(base32 + 1);
1050
1051 /* Sanity check */
1052 if (((size_t)DTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
1053
1054 base32[0] = 0;
1055
1056 cell->newState = 0;
1057 cell->symbol = symbolValue;
1058 cell->nbBits = 0;
1059
1060 return 0;
1061}
1062
1063
1064size_t FSE_buildDTable_raw (void* DTable, unsigned nbBits)
1065{
Yann Collet1cc58de2015-01-29 07:13:54 +01001066 U32* const base32 = (U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001067 FSE_decode_t* dinfo = (FSE_decode_t*)(base32 + 1);
1068 const unsigned tableSize = 1 << nbBits;
1069 const unsigned tableMask = tableSize - 1;
1070 const unsigned maxSymbolValue = tableMask;
1071 unsigned s;
1072
1073 /* Sanity checks */
1074 if (nbBits < 1) return (size_t)-FSE_ERROR_GENERIC; /* min size */
1075 if (((size_t)DTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
1076
1077 /* Build Decoding Table */
1078 base32[0] = nbBits;
1079 for (s=0; s<=maxSymbolValue; s++)
1080 {
1081 dinfo[s].newState = 0;
1082 dinfo[s].symbol = (BYTE)s;
1083 dinfo[s].nbBits = (BYTE)nbBits;
1084 }
1085
1086 return 0;
1087}
1088
1089
1090/* FSE_initDStream
1091 * Initialize a FSE_DStream_t.
1092 * srcBuffer must point at the beginning of an FSE block.
1093 * The function result is the size of the FSE_block (== srcSize).
1094 * If srcSize is too small, the function will return an errorCode;
1095 */
1096size_t FSE_initDStream(FSE_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
1097{
1098 if (srcSize < 1) return (size_t)-FSE_ERROR_srcSize_wrong;
1099
1100 if (srcSize >= sizeof(bitD_t))
1101 {
1102 U32 contain32;
1103 bitD->start = (char*)srcBuffer;
1104 bitD->ptr = (char*)srcBuffer + srcSize - sizeof(bitD_t);
1105 bitD->bitContainer = FSE_readLEST(bitD->ptr);
1106 contain32 = ((BYTE*)srcBuffer)[srcSize-1];
1107 if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */
1108 bitD->bitsConsumed = 8 - FSE_highbit32(contain32);
1109 }
1110 else
1111 {
1112 U32 contain32;
1113 bitD->start = (char*)srcBuffer;
1114 bitD->ptr = bitD->start;
1115 bitD->bitContainer = *(BYTE*)(bitD->start);
1116 switch(srcSize)
1117 {
1118 case 7: bitD->bitContainer += (bitD_t)(((BYTE*)(bitD->start))[6]) << (sizeof(bitD_t)*8 - 16);
1119 case 6: bitD->bitContainer += (bitD_t)(((BYTE*)(bitD->start))[5]) << (sizeof(bitD_t)*8 - 24);
1120 case 5: bitD->bitContainer += (bitD_t)(((BYTE*)(bitD->start))[4]) << (sizeof(bitD_t)*8 - 32);
1121 case 4: bitD->bitContainer += (bitD_t)(((BYTE*)(bitD->start))[3]) << 24;
1122 case 3: bitD->bitContainer += (bitD_t)(((BYTE*)(bitD->start))[2]) << 16;
1123 case 2: bitD->bitContainer += (bitD_t)(((BYTE*)(bitD->start))[1]) << 8;
1124 default:;
1125 }
1126 contain32 = ((BYTE*)srcBuffer)[srcSize-1];
1127 if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */
1128 bitD->bitsConsumed = 8 - FSE_highbit32(contain32);
1129 bitD->bitsConsumed += (U32)(sizeof(bitD_t) - srcSize)*8;
1130 }
1131
1132 return srcSize;
1133}
1134
1135
1136/* FSE_readBits
1137 * Read next n bits from the bitContainer.
1138 * Use the fast variant *only* if n > 0.
1139 * Note : for this function to work properly on 32-bits, don't read more than maxNbBits==25
1140 * return : value extracted.
1141 */
1142bitD_t FSE_readBits(FSE_DStream_t* bitD, U32 nbBits)
1143{
1144 bitD_t value = ((bitD->bitContainer << bitD->bitsConsumed) >> 1) >> (((sizeof(bitD_t)*8)-1)-nbBits);
1145 bitD->bitsConsumed += nbBits;
1146 return value;
1147}
1148
1149bitD_t FSE_readBitsFast(FSE_DStream_t* bitD, U32 nbBits) /* only if nbBits >= 1 */
1150{
1151 bitD_t value = (bitD->bitContainer << bitD->bitsConsumed) >> ((sizeof(bitD_t)*8)-nbBits);
1152 bitD->bitsConsumed += nbBits;
1153 return value;
1154}
1155
1156unsigned FSE_reloadDStream(FSE_DStream_t* bitD)
1157{
1158 if (bitD->ptr >= bitD->start + sizeof(bitD_t))
1159 {
1160 bitD->ptr -= bitD->bitsConsumed >> 3;
1161 bitD->bitsConsumed &= 7;
1162 bitD->bitContainer = FSE_readLEST(bitD->ptr);
1163 return 0;
1164 }
1165 if (bitD->ptr == bitD->start)
1166 {
1167 if (bitD->bitsConsumed < sizeof(bitD_t)*8) return 1;
1168 if (bitD->bitsConsumed == sizeof(bitD_t)*8) return 2;
1169 return 3;
1170 }
1171 {
1172 U32 nbBytes = bitD->bitsConsumed >> 3;
1173 if (bitD->ptr - nbBytes < bitD->start)
1174 nbBytes = (U32)(bitD->ptr - bitD->start); /* note : necessarily ptr > start */
1175 bitD->ptr -= nbBytes;
1176 bitD->bitsConsumed -= nbBytes*8;
1177 bitD->bitContainer = FSE_readLEST(bitD->ptr); /* note : necessarily srcSize > sizeof(bitD) */
1178 return (bitD->ptr == bitD->start);
1179 }
1180}
1181
1182
1183void FSE_initDState(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD, const void* DTable)
1184{
Yann Collet1cc58de2015-01-29 07:13:54 +01001185 const U32* const base32 = (const U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001186 DStatePtr->state = FSE_readBits(bitD, base32[0]);
1187 FSE_reloadDStream(bitD);
1188 DStatePtr->table = base32 + 1;
1189}
1190
1191BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD)
1192{
1193 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
1194 const U32 nbBits = DInfo.nbBits;
1195 BYTE symbol = DInfo.symbol;
1196 bitD_t lowBits = FSE_readBits(bitD, nbBits);
1197
1198 DStatePtr->state = DInfo.newState + lowBits;
1199 return symbol;
1200}
1201
1202BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD)
1203{
1204 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
1205 const U32 nbBits = DInfo.nbBits;
1206 BYTE symbol = DInfo.symbol;
1207 bitD_t lowBits = FSE_readBitsFast(bitD, nbBits);
1208
1209 DStatePtr->state = DInfo.newState + lowBits;
1210 return symbol;
1211}
1212
1213/* FSE_endOfDStream
1214 Tells if bitD has reached end of bitStream or not */
1215
1216unsigned FSE_endOfDStream(const FSE_DStream_t* bitD)
1217{
1218 return FSE_reloadDStream((FSE_DStream_t*)bitD)==2;
1219}
1220
1221unsigned FSE_endOfDState(const FSE_DState_t* statePtr)
1222{
1223 return statePtr->state == 0;
1224}
1225
1226
1227FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
1228 void* dst, size_t maxDstSize,
1229 const void* cSrc, size_t cSrcSize,
1230 const void* DTable, unsigned fast)
1231{
1232 BYTE* const ostart = (BYTE*) dst;
1233 BYTE* op = ostart;
1234 BYTE* const omax = op + maxDstSize;
1235 BYTE* const olimit = omax-3;
1236
1237 FSE_DStream_t bitD;
1238 FSE_DState_t state1, state2;
1239 size_t errorCode;
1240
1241 /* Init */
1242 errorCode = FSE_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1243 if (FSE_isError(errorCode)) return errorCode;
1244
1245 FSE_initDState(&state1, &bitD, DTable);
1246 FSE_initDState(&state2, &bitD, DTable);
1247
1248
1249 /* 2 symbols per loop */
1250 while (!FSE_reloadDStream(&bitD) && (op<olimit))
1251 {
1252 *op++ = fast ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD);
1253
1254 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD_t)*8) /* This test must be static */
1255 FSE_reloadDStream(&bitD);
1256
1257 *op++ = fast ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD);
1258
1259 if (FSE_MAX_TABLELOG*4+7 < sizeof(bitD_t)*8) /* This test must be static */
1260 {
1261 *op++ = fast ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD);
1262 *op++ = fast ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD);
1263 }
1264 }
1265
1266 /* tail */
1267 while (1)
1268 {
1269 if ( (FSE_reloadDStream(&bitD)>2) || (op==omax) || (FSE_endOfDState(&state1) && FSE_endOfDStream(&bitD)) )
1270 break;
1271
1272 *op++ = fast ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD);
1273
1274 if ( (FSE_reloadDStream(&bitD)>2) || (op==omax) || (FSE_endOfDState(&state2) && FSE_endOfDStream(&bitD)) )
1275 break;
1276
1277 *op++ = fast ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD);
1278 }
1279
1280 /* end ? */
1281 if (FSE_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2) )
1282 return op-ostart;
1283
1284 if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */
1285
1286 return (size_t)-FSE_ERROR_corruptionDetected;
1287}
1288
1289
1290size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
1291 const void* cSrc, size_t cSrcSize,
1292 const void* DTable, size_t fastMode)
1293{
1294 /* select fast mode (static) */
1295 if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, DTable, 1);
1296 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, DTable, 0);
1297}
1298
1299
1300size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1301{
1302 const BYTE* const istart = (const BYTE*)cSrc;
1303 const BYTE* ip = istart;
1304 short counting[FSE_MAX_SYMBOL_VALUE+1];
Yann Collet2ddf7e92015-02-08 20:26:47 +01001305 FSE_decode_t DTable[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
Yann Collet4856a002015-01-24 01:58:16 +01001306 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
1307 unsigned tableLog;
1308 size_t errorCode, fastMode;
1309
1310 if (cSrcSize<2) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */
1311
1312 /* normal FSE decoding mode */
1313 errorCode = FSE_readHeader (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1314 if (FSE_isError(errorCode)) return errorCode;
1315 if (errorCode >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */
1316 ip += errorCode;
1317 cSrcSize -= errorCode;
1318
1319 fastMode = FSE_buildDTable (DTable, counting, maxSymbolValue, tableLog);
1320 if (FSE_isError(fastMode)) return fastMode;
1321
1322 /* always return, even if it is an error code */
1323 return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, DTable, fastMode);
1324}
1325
1326
1327#endif /* FSE_COMMONDEFS_ONLY */
1328
1329/*
1330 2nd part of the file
1331 designed to be included
1332 for type-specific functions (template equivalent in C)
1333 Objective is to write such functions only once, for better maintenance
1334*/
1335
1336/* safety checks */
1337#ifndef FSE_FUNCTION_EXTENSION
1338# error "FSE_FUNCTION_EXTENSION must be defined"
1339#endif
1340#ifndef FSE_FUNCTION_TYPE
1341# error "FSE_FUNCTION_TYPE must be defined"
1342#endif
1343
1344/* Function names */
1345#define FSE_CAT(X,Y) X##Y
1346#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
1347#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
1348
1349
1350/* Function templates */
1351size_t FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (unsigned* count, const FSE_FUNCTION_TYPE* source, size_t sourceSize, unsigned* maxSymbolValuePtr, unsigned safe)
1352{
1353 const FSE_FUNCTION_TYPE* ip = source;
1354 const FSE_FUNCTION_TYPE* const iend = ip+sourceSize;
1355 unsigned maxSymbolValue = *maxSymbolValuePtr;
1356 unsigned max=0;
1357 int s;
1358
1359 U32 Counting1[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1360 U32 Counting2[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1361 U32 Counting3[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1362 U32 Counting4[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1363
1364 /* safety checks */
1365 if (!sourceSize)
1366 {
1367 memset(count, 0, (maxSymbolValue + 1) * sizeof(FSE_FUNCTION_TYPE));
1368 *maxSymbolValuePtr = 0;
1369 return 0;
1370 }
1371 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return (size_t)-FSE_ERROR_GENERIC; /* maxSymbolValue too large : unsupported */
1372 if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE; /* 0 == default */
1373
1374 if ((safe) || (sizeof(FSE_FUNCTION_TYPE)>1))
1375 {
1376 /* check input values, to avoid count table overflow */
1377 while (ip < iend-3)
1378 {
1379 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting1[*ip++]++;
1380 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting2[*ip++]++;
1381 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting3[*ip++]++;
1382 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting4[*ip++]++;
1383 }
1384 }
1385 else
1386 {
1387 U32 cached = FSE_read32(ip); ip += 4;
1388 while (ip < iend-15)
1389 {
1390 U32 c = cached; cached = FSE_read32(ip); ip += 4;
1391 Counting1[(BYTE) c ]++;
1392 Counting2[(BYTE)(c>>8) ]++;
1393 Counting3[(BYTE)(c>>16)]++;
1394 Counting4[ c>>24 ]++;
1395 c = cached; cached = FSE_read32(ip); ip += 4;
1396 Counting1[(BYTE) c ]++;
1397 Counting2[(BYTE)(c>>8) ]++;
1398 Counting3[(BYTE)(c>>16)]++;
1399 Counting4[ c>>24 ]++;
1400 c = cached; cached = FSE_read32(ip); ip += 4;
1401 Counting1[(BYTE) c ]++;
1402 Counting2[(BYTE)(c>>8) ]++;
1403 Counting3[(BYTE)(c>>16)]++;
1404 Counting4[ c>>24 ]++;
1405 c = cached; cached = FSE_read32(ip); ip += 4;
1406 Counting1[(BYTE) c ]++;
1407 Counting2[(BYTE)(c>>8) ]++;
1408 Counting3[(BYTE)(c>>16)]++;
1409 Counting4[ c>>24 ]++;
1410 }
1411 ip-=4;
1412 }
1413
1414 /* finish last symbols */
1415 while (ip<iend) { if ((safe) && (*ip>maxSymbolValue)) return (size_t)-FSE_ERROR_GENERIC; Counting1[*ip++]++; }
1416
1417 for (s=0; s<=(int)maxSymbolValue; s++)
1418 {
1419 count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];
1420 if (count[s] > max) max = count[s];
1421 }
1422
1423 while (!count[maxSymbolValue]) maxSymbolValue--;
1424 *maxSymbolValuePtr = maxSymbolValue;
1425 return (int)max;
1426}
1427
1428/* hidden fast variant (unsafe) */
1429size_t FSE_FUNCTION_NAME(FSE_countFast, FSE_FUNCTION_EXTENSION) (unsigned* count, const FSE_FUNCTION_TYPE* source, size_t sourceSize, unsigned* maxSymbolValuePtr)
1430{
1431 return FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (count, source, sourceSize, maxSymbolValuePtr, 0);
1432}
1433
1434size_t FSE_FUNCTION_NAME(FSE_count, FSE_FUNCTION_EXTENSION) (unsigned* count, const FSE_FUNCTION_TYPE* source, size_t sourceSize, unsigned* maxSymbolValuePtr)
1435{
1436 if ((sizeof(FSE_FUNCTION_TYPE)==1) && (*maxSymbolValuePtr >= 255))
1437 {
1438 *maxSymbolValuePtr = 255;
1439 return FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (count, source, sourceSize, maxSymbolValuePtr, 0);
1440 }
1441 return FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (count, source, sourceSize, maxSymbolValuePtr, 1);
1442}
1443
1444
1445static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1446
1447size_t FSE_FUNCTION_NAME(FSE_buildCTable, FSE_FUNCTION_EXTENSION)
1448(void* CTable, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1449{
1450 const unsigned tableSize = 1 << tableLog;
1451 const unsigned tableMask = tableSize - 1;
1452 U16* tableU16 = ( (U16*) CTable) + 2;
1453 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) (((U32*)CTable) + 1 + (tableLog ? tableSize>>1 : 1) );
1454 const unsigned step = FSE_tableStep(tableSize);
1455 unsigned cumul[FSE_MAX_SYMBOL_VALUE+2];
1456 U32 position = 0;
1457 FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE];
1458 U32 highThreshold = tableSize-1;
1459 unsigned symbol;
1460 unsigned i;
1461
1462 /* safety checks */
1463 if (((size_t)CTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
1464
1465 /* header */
1466 tableU16[-2] = (U16) tableLog;
1467 tableU16[-1] = (U16) maxSymbolValue;
1468
1469 /* For explanations on how to distribute symbol values over the table :
1470 * http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
1471
1472 /* symbol start positions */
1473 cumul[0] = 0;
1474 for (i=1; i<=maxSymbolValue+1; i++)
1475 {
1476 if (normalizedCounter[i-1]==-1) /* Low prob symbol */
1477 {
1478 cumul[i] = cumul[i-1] + 1;
1479 tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(i-1);
1480 }
1481 else
1482 cumul[i] = cumul[i-1] + normalizedCounter[i-1];
1483 }
1484 cumul[maxSymbolValue+1] = tableSize+1;
1485
1486 /* Spread symbols */
1487 for (symbol=0; symbol<=maxSymbolValue; symbol++)
1488 {
1489 int nbOccurences;
1490 for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++)
1491 {
1492 tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
1493 position = (position + step) & tableMask;
1494 while (position > highThreshold) position = (position + step) & tableMask; /* Lowprob area */
1495 }
1496 }
1497
1498 if (position!=0) return (size_t)-FSE_ERROR_GENERIC; /* Must have gone through all positions */
1499
1500 /* Build table */
1501 for (i=0; i<tableSize; i++)
1502 {
1503 FSE_FUNCTION_TYPE s = tableSymbol[i];
1504 tableU16[cumul[s]++] = (U16) (tableSize+i); // Table U16 : sorted by symbol order; gives next state value
1505 }
1506
1507 // Build Symbol Transformation Table
1508 {
1509 unsigned s;
1510 unsigned total = 0;
1511 for (s=0; s<=maxSymbolValue; s++)
1512 {
1513 switch (normalizedCounter[s])
1514 {
1515 case 0:
1516 break;
1517 case -1:
1518 case 1:
1519 symbolTT[s].minBitsOut = (BYTE)tableLog;
1520 symbolTT[s].deltaFindState = total - 1;
1521 total ++;
1522 symbolTT[s].maxState = (U16)( (tableSize*2) - 1); /* ensures state <= maxState */
1523 break;
1524 default :
1525 symbolTT[s].minBitsOut = (BYTE)( (tableLog-1) - FSE_highbit32 (normalizedCounter[s]-1) );
1526 symbolTT[s].deltaFindState = total - normalizedCounter[s];
1527 total += normalizedCounter[s];
1528 symbolTT[s].maxState = (U16)( (normalizedCounter[s] << (symbolTT[s].minBitsOut+1)) - 1);
1529 }
1530 }
1531 }
1532
1533 return 0;
1534}
1535
1536
1537#define FSE_DECODE_TYPE FSE_TYPE_NAME(FSE_decode_t, FSE_FUNCTION_EXTENSION)
1538
1539void* FSE_FUNCTION_NAME(FSE_createDTable, FSE_FUNCTION_EXTENSION) (unsigned tableLog)
1540{
1541 if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
1542 return malloc( ((size_t)1<<tableLog) * sizeof (FSE_DECODE_TYPE) );
1543}
1544
1545void FSE_FUNCTION_NAME(FSE_freeDTable, FSE_FUNCTION_EXTENSION) (void* DTable)
1546{
1547 free(DTable);
1548}
1549
1550
1551size_t FSE_FUNCTION_NAME(FSE_buildDTable, FSE_FUNCTION_EXTENSION)
1552(void* DTable, const short* const normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1553{
Yann Collet1cc58de2015-01-29 07:13:54 +01001554 U32* const base32 = (U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001555 FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (base32+1);
1556 const U32 tableSize = 1 << tableLog;
1557 const U32 tableMask = tableSize-1;
1558 const U32 step = FSE_tableStep(tableSize);
1559 U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
1560 U32 position = 0;
1561 U32 highThreshold = tableSize-1;
1562 const S16 largeLimit= 1 << (tableLog-1);
1563 U32 noLarge = 1;
1564 U32 s;
1565
1566 /* Sanity Checks */
1567 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return (size_t)-FSE_ERROR_maxSymbolValue_tooLarge;
1568 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_tableLog_tooLarge;
1569
1570 /* Init, lay down lowprob symbols */
1571 base32[0] = tableLog;
1572 for (s=0; s<=maxSymbolValue; s++)
1573 {
1574 if (normalizedCounter[s]==-1)
1575 {
1576 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
1577 symbolNext[s] = 1;
1578 }
1579 else
1580 {
1581 if (normalizedCounter[s] >= largeLimit) noLarge=0;
1582 symbolNext[s] = normalizedCounter[s];
1583 }
1584 }
1585
1586 /* Spread symbols */
1587 for (s=0; s<=maxSymbolValue; s++)
1588 {
1589 int i;
1590 for (i=0; i<normalizedCounter[s]; i++)
1591 {
1592 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
1593 position = (position + step) & tableMask;
1594 while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1595 }
1596 }
1597
1598 if (position!=0) return (size_t)-FSE_ERROR_GENERIC; /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1599
1600 /* Build Decoding table */
1601 {
1602 U32 i;
1603 for (i=0; i<tableSize; i++)
1604 {
1605 FSE_FUNCTION_TYPE symbol = tableDecode[i].symbol;
1606 U16 nextState = symbolNext[symbol]++;
1607 tableDecode[i].nbBits = (BYTE) (tableLog - FSE_highbit32 ((U32)nextState) );
1608 tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1609 }
1610 }
1611
1612 return noLarge;
1613}