blob: a81e2f381c7a649c4f61734e57328526b4899f00 [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
Yann Collet213089c2015-06-18 07:43:16 -0800308#ifndef __clang_analyzer__ /* clang static analyzer has difficulties with this function : seems to believe normalizedCounter is uninitialized */
309
Yann Collet4856a002015-01-24 01:58:16 +0100310static size_t FSE_writeHeader_generic (void* header, size_t headerBufferSize,
311 const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
312 unsigned safeWrite)
313{
314 BYTE* const ostart = (BYTE*) header;
315 BYTE* out = ostart;
316 BYTE* const oend = ostart + headerBufferSize;
317 int nbBits;
318 const int tableSize = 1 << tableLog;
319 int remaining;
320 int threshold;
321 U32 bitStream;
322 int bitCount;
323 unsigned charnum = 0;
324 int previous0 = 0;
325
326 bitStream = 0;
327 bitCount = 0;
328 /* Table Size */
329 bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
330 bitCount += 4;
331
332 /* Init */
333 remaining = tableSize+1; /* +1 for extra accuracy */
334 threshold = tableSize;
335 nbBits = tableLog+1;
336
337 while (remaining>1) /* stops at 1 */
338 {
339 if (previous0)
340 {
341 unsigned start = charnum;
342 while (!normalizedCounter[charnum]) charnum++;
343 while (charnum >= start+24)
344 {
345 start+=24;
Yann Collet213089c2015-06-18 07:43:16 -0800346 bitStream += 0xFFFFU << bitCount;
Yann Collet4856a002015-01-24 01:58:16 +0100347 if ((!safeWrite) && (out > oend-2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
Yann Collet213089c2015-06-18 07:43:16 -0800348 out[0] = (BYTE) bitStream;
Yann Collet4856a002015-01-24 01:58:16 +0100349 out[1] = (BYTE)(bitStream>>8);
350 out+=2;
351 bitStream>>=16;
352 }
353 while (charnum >= start+3)
354 {
355 start+=3;
356 bitStream += 3 << bitCount;
357 bitCount += 2;
358 }
359 bitStream += (charnum-start) << bitCount;
360 bitCount += 2;
361 if (bitCount>16)
362 {
363 if ((!safeWrite) && (out > oend - 2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
364 out[0] = (BYTE)bitStream;
365 out[1] = (BYTE)(bitStream>>8);
366 out += 2;
367 bitStream >>= 16;
368 bitCount -= 16;
369 }
370 }
371 {
372 short count = normalizedCounter[charnum++];
373 const short max = (short)((2*threshold-1)-remaining);
374 remaining -= FSE_abs(count);
Yann Collet213089c2015-06-18 07:43:16 -0800375 if (remaining<1) return (size_t)-FSE_ERROR_GENERIC;
Yann Collet4856a002015-01-24 01:58:16 +0100376 count++; /* +1 for extra accuracy */
377 if (count>=threshold) count += max; /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
378 bitStream += count << bitCount;
379 bitCount += nbBits;
380 bitCount -= (count<max);
381 previous0 = (count==1);
382 while (remaining<threshold) nbBits--, threshold>>=1;
383 }
384 if (bitCount>16)
385 {
386 if ((!safeWrite) && (out > oend - 2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
387 out[0] = (BYTE)bitStream;
388 out[1] = (BYTE)(bitStream>>8);
389 out += 2;
390 bitStream >>= 16;
391 bitCount -= 16;
392 }
393 }
394
395 /* flush remaining bitStream */
396 if ((!safeWrite) && (out > oend - 2)) return (size_t)-FSE_ERROR_GENERIC; /* Buffer overflow */
397 out[0] = (BYTE)bitStream;
398 out[1] = (BYTE)(bitStream>>8);
399 out+= (bitCount+7) /8;
400
401 if (charnum > maxSymbolValue + 1) return (size_t)-FSE_ERROR_GENERIC; /* Too many symbols written (a bit too late?) */
402
403 return (out-ostart);
404}
Yann Collet213089c2015-06-18 07:43:16 -0800405#endif // __clang_analyzer__
Yann Collet4856a002015-01-24 01:58:16 +0100406
407
408size_t FSE_writeHeader (void* header, size_t headerBufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
409{
410 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported */
411 if (tableLog < FSE_MIN_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported */
412
413 if (headerBufferSize < FSE_headerBound(maxSymbolValue, tableLog))
414 return FSE_writeHeader_generic(header, headerBufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
415
416 return FSE_writeHeader_generic(header, headerBufferSize, normalizedCounter, maxSymbolValue, tableLog, 1);
417}
418
419
420size_t FSE_readHeader (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
421 const void* headerBuffer, size_t hbSize)
422{
423 const BYTE* const istart = (const BYTE*) headerBuffer;
424 const BYTE* ip = istart;
425 int nbBits;
426 int remaining;
427 int threshold;
428 U32 bitStream;
429 int bitCount;
430 unsigned charnum = 0;
431 int previous0 = 0;
432
433 bitStream = FSE_readLE32(ip);
434 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
435 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return (size_t)-FSE_ERROR_tableLog_tooLarge;
436 bitStream >>= 4;
437 bitCount = 4;
438 *tableLogPtr = nbBits;
439 remaining = (1<<nbBits)+1;
440 threshold = 1<<nbBits;
441 nbBits++;
442
443 while ((remaining>1) && (charnum<=*maxSVPtr))
444 {
445 if (previous0)
446 {
447 unsigned n0 = charnum;
448 while ((bitStream & 0xFFFF) == 0xFFFF)
449 {
450 n0+=24;
451 ip+=2;
452 bitStream = FSE_readLE32(ip) >> bitCount;
453 }
454 while ((bitStream & 3) == 3)
455 {
456 n0+=3;
457 bitStream>>=2;
458 bitCount+=2;
459 }
460 n0 += bitStream & 3;
461 bitCount += 2;
462 if (n0 > *maxSVPtr) return (size_t)-FSE_ERROR_GENERIC;
463 while (charnum < n0) normalizedCounter[charnum++] = 0;
464 ip += bitCount>>3;
465 bitCount &= 7;
466 bitStream = FSE_readLE32(ip) >> bitCount;
467 }
468 {
469 const short max = (short)((2*threshold-1)-remaining);
470 short count;
471
472 if ((bitStream & (threshold-1)) < (U32)max)
473 {
474 count = (short)(bitStream & (threshold-1));
475 bitCount += nbBits-1;
476 }
477 else
478 {
479 count = (short)(bitStream & (2*threshold-1));
480 if (count >= threshold) count -= max;
481 bitCount += nbBits;
482 }
483
484 count--; /* extra accuracy */
485 remaining -= FSE_abs(count);
486 normalizedCounter[charnum++] = count;
487 previous0 = !count;
488 while (remaining < threshold)
489 {
490 nbBits--;
491 threshold >>= 1;
492 }
493
494 ip += bitCount>>3;
495 bitCount &= 7;
496 bitStream = FSE_readLE32(ip) >> bitCount;
497 }
498 }
499 if (remaining != 1) return (size_t)-FSE_ERROR_GENERIC;
500 *maxSVPtr = charnum-1;
501
502 ip += bitCount>0;
503 if ((size_t)(ip-istart) >= hbSize) return (size_t)-FSE_ERROR_srcSize_wrong; /* arguably a bit late , tbd */
504 return ip-istart;
505}
506
507
508/****************************************************************
509* FSE Compression Code
510****************************************************************/
511/*
512CTable is a variable size structure which contains :
513 U16 tableLog;
514 U16 maxSymbolValue;
515 U16 nextStateNumber[1 << tableLog]; // This size is variable
516 FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1]; // This size is variable
517Allocation is manual, since C standard does not support variable-size structures.
518*/
519
520size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog)
521{
522 size_t size;
523 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 */
524 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_GENERIC;
525 size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
526 return size;
527}
528
529void* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
530{
531 size_t size;
532 if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
533 size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
534 return malloc(size);
535}
536
537void FSE_freeCTable (void* CTable)
538{
539 free(CTable);
540}
541
Yann Collet4856a002015-01-24 01:58:16 +0100542
543unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
544{
545 U32 tableLog = maxTableLog;
546 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
547 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 +0100548 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 +0100549 if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
550 if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
551 return tableLog;
552}
553
554
555typedef struct
556{
557 U32 id;
558 U32 count;
559} rank_t;
560
561int FSE_compareRankT(const void* r1, const void* r2)
562{
Yann Collet1cc58de2015-01-29 07:13:54 +0100563 const rank_t* R1 = (const rank_t*)r1;
564 const rank_t* R2 = (const rank_t*)r2;
Yann Collet4856a002015-01-24 01:58:16 +0100565
566 return 2 * (R1->count < R2->count) - 1;
567}
568
Yann Colleta3c75ba2015-02-21 03:31:59 +0100569
570#if 0
Yann Collet565b81d2015-01-29 06:51:30 +0100571static size_t FSE_adjustNormSlow(short* norm, int pointsToRemove, const unsigned* count, U32 maxSymbolValue)
572{
Yann Collet2ddf7e92015-02-08 20:26:47 +0100573 rank_t rank[FSE_MAX_SYMBOL_VALUE+2];
Yann Collet565b81d2015-01-29 06:51:30 +0100574 U32 s;
575
576 /* Init */
577 for (s=0; s<=maxSymbolValue; s++)
578 {
579 rank[s].id = s;
580 rank[s].count = count[s];
581 if (norm[s] <= 1) rank[s].count = 0;
582 }
Yann Collet2ddf7e92015-02-08 20:26:47 +0100583 rank[maxSymbolValue+1].id = 0;
584 rank[maxSymbolValue+1].count = 0; /* ensures comparison ends here in worst case */
Yann Collet565b81d2015-01-29 06:51:30 +0100585
586 /* Sort according to count */
587 qsort(rank, maxSymbolValue+1, sizeof(rank_t), FSE_compareRankT);
588
589 while(pointsToRemove)
590 {
591 int newRank = 1;
592 rank_t savedR;
593 if (norm[rank[0].id] == 1)
594 return (size_t)-FSE_ERROR_GENERIC;
595 norm[rank[0].id]--;
596 pointsToRemove--;
597 rank[0].count -= (rank[0].count + 6) >> 3;
598 if (norm[rank[0].id] == 1)
599 rank[0].count=0;
600 savedR = rank[0];
601 while (rank[newRank].count > savedR.count)
602 {
603 rank[newRank-1] = rank[newRank];
604 newRank++;
605 }
606 rank[newRank-1] = savedR;
607 }
608
609 return 0;
610}
Yann Collet565b81d2015-01-29 06:51:30 +0100611
Yann Colleta3c75ba2015-02-21 03:31:59 +0100612#else
613
Yann Collet26aa1ec2015-02-24 09:05:58 +0100614/* Secondary normalization method.
615 To be used when primary method fails. */
616
617static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue)
Yann Colleta3c75ba2015-02-21 03:31:59 +0100618{
619 U32 s;
Yann Colleta3c75ba2015-02-21 03:31:59 +0100620 U32 distributed = 0;
621 U32 ToDistribute;
622
623 /* Init */
Yann Colleta3c75ba2015-02-21 03:31:59 +0100624 U32 lowThreshold = (U32)(total >> tableLog);
625 U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
626
627 for (s=0; s<=maxSymbolValue; s++)
628 {
629 if (count[s] == 0)
630 {
631 norm[s]=0;
632 continue;
633 }
634 if (count[s] <= lowThreshold)
635 {
636 norm[s] = -1;
637 distributed++;
638 total -= count[s];
639 continue;
640 }
641 if (count[s] <= lowOne)
642 {
643 norm[s] = 1;
644 distributed++;
645 total -= count[s];
646 continue;
647 }
648 norm[s]=-2;
649 }
650 ToDistribute = (1 << tableLog) - distributed;
651
652 if ((total / ToDistribute) > lowOne)
653 {
654 /* risk of rounding to zero */
655 lowOne = (U32)((total * 3) / (ToDistribute * 2));
656 for (s=0; s<=maxSymbolValue; s++)
657 {
658 if ((norm[s] == -2) && (count[s] <= lowOne))
659 {
660 norm[s] = 1;
661 distributed++;
662 total -= count[s];
663 continue;
664 }
665 }
666 ToDistribute = (1 << tableLog) - distributed;
667 }
668
669 if (distributed == maxSymbolValue+1)
670 {
Yann Collet26aa1ec2015-02-24 09:05:58 +0100671 /* all values are pretty poor;
672 probably incompressible data (should have already been detected);
673 find max, then give all remaining points to max */
Yann Colleta3c75ba2015-02-21 03:31:59 +0100674 U32 maxV = 0, maxC =0;
675 for (s=0; s<=maxSymbolValue; s++)
676 if (count[s] > maxC) maxV=s, maxC=count[s];
677 norm[maxV] += ToDistribute;
678 return 0;
679 }
680
681 {
682 U64 const vStepLog = 62 - tableLog;
683 U64 const mid = (1ULL << (vStepLog-1)) - 1;
684 U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total; /* scale on remaining */
685 U64 tmpTotal = mid;
686 for (s=0; s<=maxSymbolValue; s++)
687 {
688 if (norm[s]==-2)
689 {
690 U64 end = tmpTotal + (count[s] * rStep);
691 U32 sStart = (U32)(tmpTotal >> vStepLog);
692 U32 sEnd = (U32)(end >> vStepLog);
693 U32 weight = sEnd - sStart;
694 if (weight < 1)
695 return (size_t)-FSE_ERROR_GENERIC;
Yann Collet213089c2015-06-18 07:43:16 -0800696 norm[s] = (short)weight;
Yann Colleta3c75ba2015-02-21 03:31:59 +0100697 tmpTotal = end;
698 }
699 }
700 }
701
702 return 0;
703}
704#endif
705
Yann Collet4856a002015-01-24 01:58:16 +0100706
707size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
708 const unsigned* count, size_t total,
709 unsigned maxSymbolValue)
710{
711 /* Sanity checks */
712 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
713 if (tableLog < FSE_MIN_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported size */
714 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_GENERIC; /* Unsupported size */
715 if ((1U<<tableLog) <= maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; /* Too small tableLog, compression potentially impossible */
716
717 {
718 U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
719 U64 const scale = 62 - tableLog;
720 U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */
721 U64 const vStep = 1ULL<<(scale-20);
722 int stillToDistribute = 1<<tableLog;
723 unsigned s;
724 unsigned largest=0;
725 short largestP=0;
726 U32 lowThreshold = (U32)(total >> tableLog);
727
728 for (s=0; s<=maxSymbolValue; s++)
729 {
730 if (count[s] == total) return 0;
731 if (count[s] == 0)
732 {
733 normalizedCounter[s]=0;
734 continue;
735 }
736 if (count[s] <= lowThreshold)
737 {
738 normalizedCounter[s] = -1;
739 stillToDistribute--;
740 }
741 else
742 {
743 short proba = (short)((count[s]*step) >> scale);
744 if (proba<8)
745 {
Yann Collet2ddf7e92015-02-08 20:26:47 +0100746 U64 restToBeat = vStep * rtbTable[proba];
Yann Collet4856a002015-01-24 01:58:16 +0100747 proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
748 }
749 if (proba > largestP)
750 {
751 largestP=proba;
752 largest=s;
753 }
754 normalizedCounter[s] = proba;
755 stillToDistribute -= proba;
756 }
757 }
Yann Collet4856a002015-01-24 01:58:16 +0100758 if (-stillToDistribute >= (normalizedCounter[largest] >> 1))
759 {
Yann Collet26aa1ec2015-02-24 09:05:58 +0100760 /* corner case, need another normalization method */
761 size_t errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
Yann Collet565b81d2015-01-29 06:51:30 +0100762 if (FSE_isError(errorCode)) return errorCode;
Yann Collet4856a002015-01-24 01:58:16 +0100763 }
764 else normalizedCounter[largest] += (short)stillToDistribute;
765 }
766
767#if 0
768 { /* Print Table (debug) */
Yann Collet565b81d2015-01-29 06:51:30 +0100769 U32 s;
770 U32 nTotal = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100771 for (s=0; s<=maxSymbolValue; s++)
772 printf("%3i: %4i \n", s, normalizedCounter[s]);
Yann Collet565b81d2015-01-29 06:51:30 +0100773 for (s=0; s<=maxSymbolValue; s++)
774 nTotal += abs(normalizedCounter[s]);
775 if (nTotal != (1U<<tableLog))
776 printf("Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
Yann Collet4856a002015-01-24 01:58:16 +0100777 getchar();
778 }
779#endif
780
781 return tableLog;
782}
783
784
785/* fake CTable, for raw (uncompressed) input */
786size_t FSE_buildCTable_raw (void* CTable, unsigned nbBits)
787{
788 const unsigned tableSize = 1 << nbBits;
789 const unsigned tableMask = tableSize - 1;
790 const unsigned maxSymbolValue = tableMask;
791 U16* tableU16 = ( (U16*) CTable) + 2;
792 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) ((((U32*)CTable)+1) + (tableSize>>1));
793 unsigned s;
794
795 /* Sanity checks */
796 if (nbBits < 1) return (size_t)-FSE_ERROR_GENERIC; /* min size */
797 if (((size_t)CTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
798
799 /* header */
800 tableU16[-2] = (U16) nbBits;
801 tableU16[-1] = (U16) maxSymbolValue;
802
803 /* Build table */
804 for (s=0; s<tableSize; s++)
805 tableU16[s] = (U16)(tableSize + s);
806
807 /* Build Symbol Transformation Table */
808 for (s=0; s<=maxSymbolValue; s++)
809 {
810 symbolTT[s].minBitsOut = (BYTE)nbBits;
811 symbolTT[s].deltaFindState = s-1;
812 symbolTT[s].maxState = (U16)( (tableSize*2) - 1); /* ensures state <= maxState */
813 }
814
815 return 0;
816}
817
818
819/* fake CTable, for rle (100% always same symbol) input */
820size_t FSE_buildCTable_rle (void* CTable, BYTE symbolValue)
821{
822 const unsigned tableSize = 1;
823 U16* tableU16 = ( (U16*) CTable) + 2;
824 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) ((U32*)CTable + 2);
825
826 /* safety checks */
827 if (((size_t)CTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be 4 bytes aligned */
828
829 /* header */
830 tableU16[-2] = (U16) 0;
831 tableU16[-1] = (U16) symbolValue;
832
833 /* Build table */
834 tableU16[0] = 0;
835 tableU16[1] = 0; /* just in case */
836
837 /* Build Symbol Transformation Table */
838 {
839 symbolTT[symbolValue].minBitsOut = 0;
840 symbolTT[symbolValue].deltaFindState = 0;
841 symbolTT[symbolValue].maxState = (U16)(2*tableSize-1); /* ensures state <= maxState */
842 }
843
844 return 0;
845}
846
847
848void FSE_initCStream(FSE_CStream_t* bitC, void* start)
849{
850 bitC->bitContainer = 0;
851 bitC->bitPos = 0; /* reserved for unusedBits */
852 bitC->startPtr = (char*)start;
853 bitC->ptr = bitC->startPtr;
854}
855
856void FSE_initCState(FSE_CState_t* statePtr, const void* CTable)
857{
Yann Collet213089c2015-06-18 07:43:16 -0800858 const U32 tableLog = ( (const U16*) CTable) [0];
Yann Collet4856a002015-01-24 01:58:16 +0100859 statePtr->value = (ptrdiff_t)1<<tableLog;
860 statePtr->stateTable = ((const U16*) CTable) + 2;
861 statePtr->symbolTT = (const U32*)CTable + 1 + (tableLog ? (1<<(tableLog-1)) : 1);
862 statePtr->stateLog = tableLog;
863}
864
865void FSE_addBits(FSE_CStream_t* bitC, size_t value, unsigned nbBits)
866{
867 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 */
868 bitC->bitContainer |= (value & mask[nbBits]) << bitC->bitPos;
869 bitC->bitPos += nbBits;
870}
871
872void FSE_encodeByte(FSE_CStream_t* bitC, FSE_CState_t* statePtr, BYTE symbol)
873{
874 const FSE_symbolCompressionTransform* const symbolTT = (const FSE_symbolCompressionTransform*) statePtr->symbolTT;
875 const U16* const stateTable = (const U16*) statePtr->stateTable;
876 int nbBitsOut = symbolTT[symbol].minBitsOut;
877 nbBitsOut -= (int)((symbolTT[symbol].maxState - statePtr->value) >> 31);
878 FSE_addBits(bitC, statePtr->value, nbBitsOut);
879 statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT[symbol].deltaFindState];
880}
881
882void FSE_flushBits(FSE_CStream_t* bitC)
883{
884 size_t nbBytes = bitC->bitPos >> 3;
885 FSE_writeLEST(bitC->ptr, bitC->bitContainer);
886 bitC->bitPos &= 7;
887 bitC->ptr += nbBytes;
888 bitC->bitContainer >>= nbBytes*8;
889}
890
891void FSE_flushCState(FSE_CStream_t* bitC, const FSE_CState_t* statePtr)
892{
893 FSE_addBits(bitC, statePtr->value, statePtr->stateLog);
894 FSE_flushBits(bitC);
895}
896
897
898size_t FSE_closeCStream(FSE_CStream_t* bitC)
899{
900 char* endPtr;
901
902 FSE_addBits(bitC, 1, 1);
903 FSE_flushBits(bitC);
904
905 endPtr = bitC->ptr;
906 endPtr += bitC->bitPos > 0;
907
908 return (endPtr - bitC->startPtr);
909}
910
911
912size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
913 const void* src, size_t srcSize,
914 const void* CTable)
915{
916 const BYTE* const istart = (const BYTE*) src;
917 const BYTE* ip;
918 const BYTE* const iend = istart + srcSize;
919
920 FSE_CStream_t bitC;
921 FSE_CState_t CState1, CState2;
922
923
924 /* init */
925 (void)dstSize; /* objective : ensure it fits into dstBuffer (Todo) */
926 FSE_initCStream(&bitC, dst);
927 FSE_initCState(&CState1, CTable);
928 CState2 = CState1;
929
930 ip=iend;
931
932 /* join to even */
933 if (srcSize & 1)
934 {
935 FSE_encodeByte(&bitC, &CState1, *--ip);
936 FSE_flushBits(&bitC);
937 }
938
939 /* join to mod 4 */
940 if ((sizeof(size_t)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) /* test bit 2 */
941 {
942 FSE_encodeByte(&bitC, &CState2, *--ip);
943 FSE_encodeByte(&bitC, &CState1, *--ip);
944 FSE_flushBits(&bitC);
945 }
946
947 /* 2 or 4 encoding per loop */
948 while (ip>istart)
949 {
950 FSE_encodeByte(&bitC, &CState2, *--ip);
951
952 if (sizeof(size_t)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */
953 FSE_flushBits(&bitC);
954
955 FSE_encodeByte(&bitC, &CState1, *--ip);
956
957 if (sizeof(size_t)*8 > FSE_MAX_TABLELOG*4+7 ) /* this test must be static */
958 {
959 FSE_encodeByte(&bitC, &CState2, *--ip);
960 FSE_encodeByte(&bitC, &CState1, *--ip);
961 }
962
963 FSE_flushBits(&bitC);
964 }
965
966 FSE_flushCState(&bitC, &CState2);
967 FSE_flushCState(&bitC, &CState1);
968 return FSE_closeCStream(&bitC);
969}
970
971
Yann Collet4856a002015-01-24 01:58:16 +0100972size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
973
974
975size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)
976{
977 const BYTE* const istart = (const BYTE*) src;
978 const BYTE* ip = istart;
979
980 BYTE* const ostart = (BYTE*) dst;
981 BYTE* op = ostart;
982 BYTE* const oend = ostart + dstSize;
983
984 U32 count[FSE_MAX_SYMBOL_VALUE+1];
985 S16 norm[FSE_MAX_SYMBOL_VALUE+1];
986 CTable_max_t CTable;
987 size_t errorCode;
988
989 /* early out */
990 if (dstSize < FSE_compressBound(srcSize)) return (size_t)-FSE_ERROR_dstSize_tooSmall;
991 if (srcSize <= 1) return srcSize; /* Uncompressed or RLE */
992 if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
993 if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG;
994
995 /* Scan input and build symbol stats */
996 errorCode = FSE_count (count, ip, srcSize, &maxSymbolValue);
997 if (FSE_isError(errorCode)) return errorCode;
Yann Colleta3c75ba2015-02-21 03:31:59 +0100998 if (errorCode == srcSize) return 1;
999 if (errorCode < (srcSize >> 7)) return 0; /* Heuristic : not compressible enough */
Yann Collet4856a002015-01-24 01:58:16 +01001000
1001 tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue);
1002 errorCode = FSE_normalizeCount (norm, tableLog, count, srcSize, maxSymbolValue);
1003 if (FSE_isError(errorCode)) return errorCode;
1004
1005 /* Write table description header */
1006 errorCode = FSE_writeHeader (op, FSE_MAX_HEADERSIZE, norm, maxSymbolValue, tableLog);
1007 if (FSE_isError(errorCode)) return errorCode;
1008 op += errorCode;
1009
1010 /* Compress */
1011 errorCode = FSE_buildCTable (&CTable, norm, maxSymbolValue, tableLog);
1012 if (FSE_isError(errorCode)) return errorCode;
1013 op += FSE_compress_usingCTable(op, oend - op, ip, srcSize, &CTable);
1014
1015 /* check compressibility */
1016 if ( (size_t)(op-ostart) >= srcSize-1 )
1017 return 0;
1018
1019 return op-ostart;
1020}
1021
1022
1023size_t FSE_compress (void* dst, size_t dstSize, const void* src, size_t srcSize)
1024{
1025 return FSE_compress2(dst, dstSize, src, (U32)srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG);
1026}
1027
1028
1029/*********************************************************
1030* Decompression (Byte symbols)
1031*********************************************************/
1032typedef struct
1033{
1034 U16 newState;
1035 BYTE symbol;
1036 BYTE nbBits;
1037} FSE_decode_t; /* size == U32 */
1038
1039/* Specific corner case : RLE compression */
1040size_t FSE_decompressRLE(void* dst, size_t originalSize,
1041 const void* cSrc, size_t cSrcSize)
1042{
1043 if (cSrcSize != 1) return (size_t)-FSE_ERROR_srcSize_wrong;
Yann Collet213089c2015-06-18 07:43:16 -08001044 memset(dst, *(const BYTE*)cSrc, originalSize);
Yann Collet4856a002015-01-24 01:58:16 +01001045 return originalSize;
1046}
1047
1048
1049size_t FSE_buildDTable_rle (void* DTable, BYTE symbolValue)
1050{
Yann Collet1cc58de2015-01-29 07:13:54 +01001051 U32* const base32 = (U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001052 FSE_decode_t* const cell = (FSE_decode_t*)(base32 + 1);
1053
1054 /* Sanity check */
1055 if (((size_t)DTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
1056
1057 base32[0] = 0;
1058
1059 cell->newState = 0;
1060 cell->symbol = symbolValue;
1061 cell->nbBits = 0;
1062
1063 return 0;
1064}
1065
1066
1067size_t FSE_buildDTable_raw (void* DTable, unsigned nbBits)
1068{
Yann Collet1cc58de2015-01-29 07:13:54 +01001069 U32* const base32 = (U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001070 FSE_decode_t* dinfo = (FSE_decode_t*)(base32 + 1);
1071 const unsigned tableSize = 1 << nbBits;
1072 const unsigned tableMask = tableSize - 1;
1073 const unsigned maxSymbolValue = tableMask;
1074 unsigned s;
1075
1076 /* Sanity checks */
1077 if (nbBits < 1) return (size_t)-FSE_ERROR_GENERIC; /* min size */
1078 if (((size_t)DTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
1079
1080 /* Build Decoding Table */
1081 base32[0] = nbBits;
1082 for (s=0; s<=maxSymbolValue; s++)
1083 {
1084 dinfo[s].newState = 0;
1085 dinfo[s].symbol = (BYTE)s;
1086 dinfo[s].nbBits = (BYTE)nbBits;
1087 }
1088
1089 return 0;
1090}
1091
1092
1093/* FSE_initDStream
1094 * Initialize a FSE_DStream_t.
1095 * srcBuffer must point at the beginning of an FSE block.
1096 * The function result is the size of the FSE_block (== srcSize).
1097 * If srcSize is too small, the function will return an errorCode;
1098 */
1099size_t FSE_initDStream(FSE_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
1100{
1101 if (srcSize < 1) return (size_t)-FSE_ERROR_srcSize_wrong;
1102
1103 if (srcSize >= sizeof(bitD_t))
1104 {
1105 U32 contain32;
Yann Collet213089c2015-06-18 07:43:16 -08001106 bitD->start = (const char*)srcBuffer;
1107 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD_t);
Yann Collet4856a002015-01-24 01:58:16 +01001108 bitD->bitContainer = FSE_readLEST(bitD->ptr);
Yann Collet213089c2015-06-18 07:43:16 -08001109 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet4856a002015-01-24 01:58:16 +01001110 if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */
1111 bitD->bitsConsumed = 8 - FSE_highbit32(contain32);
1112 }
1113 else
1114 {
1115 U32 contain32;
Yann Collet213089c2015-06-18 07:43:16 -08001116 bitD->start = (const char*)srcBuffer;
Yann Collet4856a002015-01-24 01:58:16 +01001117 bitD->ptr = bitD->start;
Yann Collet213089c2015-06-18 07:43:16 -08001118 bitD->bitContainer = *(const BYTE*)(bitD->start);
Yann Collet4856a002015-01-24 01:58:16 +01001119 switch(srcSize)
1120 {
Yann Collet213089c2015-06-18 07:43:16 -08001121 case 7: bitD->bitContainer += (bitD_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(bitD_t)*8 - 16);
1122 case 6: bitD->bitContainer += (bitD_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(bitD_t)*8 - 24);
1123 case 5: bitD->bitContainer += (bitD_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(bitD_t)*8 - 32);
1124 case 4: bitD->bitContainer += (bitD_t)(((const BYTE*)(bitD->start))[3]) << 24;
1125 case 3: bitD->bitContainer += (bitD_t)(((const BYTE*)(bitD->start))[2]) << 16;
1126 case 2: bitD->bitContainer += (bitD_t)(((const BYTE*)(bitD->start))[1]) << 8;
Yann Collet4856a002015-01-24 01:58:16 +01001127 default:;
1128 }
Yann Collet213089c2015-06-18 07:43:16 -08001129 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet4856a002015-01-24 01:58:16 +01001130 if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */
1131 bitD->bitsConsumed = 8 - FSE_highbit32(contain32);
1132 bitD->bitsConsumed += (U32)(sizeof(bitD_t) - srcSize)*8;
1133 }
1134
1135 return srcSize;
1136}
1137
1138
1139/* FSE_readBits
1140 * Read next n bits from the bitContainer.
Yann Collet213089c2015-06-18 07:43:16 -08001141 * On 32-bits, don't read more than maxNbBits==25
1142 * On 64-bits, don't read more than maxNbBits==57
1143 * Use the fast variant *only* if n >= 1.
Yann Collet4856a002015-01-24 01:58:16 +01001144 * return : value extracted.
1145 */
1146bitD_t FSE_readBits(FSE_DStream_t* bitD, U32 nbBits)
1147{
Yann Collet213089c2015-06-18 07:43:16 -08001148 bitD_t value = ((bitD->bitContainer << (bitD->bitsConsumed & ((sizeof(bitD_t)*8)-1))) >> 1) >> (((sizeof(bitD_t)*8)-1)-nbBits);
Yann Collet4856a002015-01-24 01:58:16 +01001149 bitD->bitsConsumed += nbBits;
1150 return value;
1151}
1152
Yann Collet213089c2015-06-18 07:43:16 -08001153bitD_t FSE_readBitsFast(FSE_DStream_t* bitD, U32 nbBits) /* only if nbBits >= 1 !! */
Yann Collet4856a002015-01-24 01:58:16 +01001154{
1155 bitD_t value = (bitD->bitContainer << bitD->bitsConsumed) >> ((sizeof(bitD_t)*8)-nbBits);
1156 bitD->bitsConsumed += nbBits;
1157 return value;
1158}
1159
1160unsigned FSE_reloadDStream(FSE_DStream_t* bitD)
1161{
1162 if (bitD->ptr >= bitD->start + sizeof(bitD_t))
1163 {
1164 bitD->ptr -= bitD->bitsConsumed >> 3;
1165 bitD->bitsConsumed &= 7;
1166 bitD->bitContainer = FSE_readLEST(bitD->ptr);
1167 return 0;
1168 }
1169 if (bitD->ptr == bitD->start)
1170 {
1171 if (bitD->bitsConsumed < sizeof(bitD_t)*8) return 1;
1172 if (bitD->bitsConsumed == sizeof(bitD_t)*8) return 2;
1173 return 3;
1174 }
1175 {
1176 U32 nbBytes = bitD->bitsConsumed >> 3;
1177 if (bitD->ptr - nbBytes < bitD->start)
1178 nbBytes = (U32)(bitD->ptr - bitD->start); /* note : necessarily ptr > start */
1179 bitD->ptr -= nbBytes;
1180 bitD->bitsConsumed -= nbBytes*8;
1181 bitD->bitContainer = FSE_readLEST(bitD->ptr); /* note : necessarily srcSize > sizeof(bitD) */
1182 return (bitD->ptr == bitD->start);
1183 }
1184}
1185
1186
1187void FSE_initDState(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD, const void* DTable)
1188{
Yann Collet1cc58de2015-01-29 07:13:54 +01001189 const U32* const base32 = (const U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001190 DStatePtr->state = FSE_readBits(bitD, base32[0]);
1191 FSE_reloadDStream(bitD);
1192 DStatePtr->table = base32 + 1;
1193}
1194
1195BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD)
1196{
1197 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
1198 const U32 nbBits = DInfo.nbBits;
1199 BYTE symbol = DInfo.symbol;
1200 bitD_t lowBits = FSE_readBits(bitD, nbBits);
1201
1202 DStatePtr->state = DInfo.newState + lowBits;
1203 return symbol;
1204}
1205
1206BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD)
1207{
1208 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
1209 const U32 nbBits = DInfo.nbBits;
1210 BYTE symbol = DInfo.symbol;
1211 bitD_t lowBits = FSE_readBitsFast(bitD, nbBits);
1212
1213 DStatePtr->state = DInfo.newState + lowBits;
1214 return symbol;
1215}
1216
1217/* FSE_endOfDStream
1218 Tells if bitD has reached end of bitStream or not */
1219
1220unsigned FSE_endOfDStream(const FSE_DStream_t* bitD)
1221{
Yann Collet213089c2015-06-18 07:43:16 -08001222 return ((bitD->ptr == bitD->start) && (bitD->bitsConsumed == sizeof(bitD_t)*8));
Yann Collet4856a002015-01-24 01:58:16 +01001223}
1224
1225unsigned FSE_endOfDState(const FSE_DState_t* statePtr)
1226{
1227 return statePtr->state == 0;
1228}
1229
1230
1231FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
1232 void* dst, size_t maxDstSize,
1233 const void* cSrc, size_t cSrcSize,
1234 const void* DTable, unsigned fast)
1235{
1236 BYTE* const ostart = (BYTE*) dst;
1237 BYTE* op = ostart;
1238 BYTE* const omax = op + maxDstSize;
1239 BYTE* const olimit = omax-3;
1240
1241 FSE_DStream_t bitD;
1242 FSE_DState_t state1, state2;
1243 size_t errorCode;
1244
1245 /* Init */
1246 errorCode = FSE_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1247 if (FSE_isError(errorCode)) return errorCode;
1248
1249 FSE_initDState(&state1, &bitD, DTable);
1250 FSE_initDState(&state2, &bitD, DTable);
1251
1252
1253 /* 2 symbols per loop */
1254 while (!FSE_reloadDStream(&bitD) && (op<olimit))
1255 {
1256 *op++ = fast ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD);
1257
1258 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD_t)*8) /* This test must be static */
1259 FSE_reloadDStream(&bitD);
1260
1261 *op++ = fast ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD);
1262
1263 if (FSE_MAX_TABLELOG*4+7 < sizeof(bitD_t)*8) /* This test must be static */
1264 {
1265 *op++ = fast ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD);
1266 *op++ = fast ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD);
1267 }
1268 }
1269
1270 /* tail */
Yann Collet213089c2015-06-18 07:43:16 -08001271 /* note : FSE_reloadDStream(&bitD) >= 1; Ends at exactly 2 */
Yann Collet4856a002015-01-24 01:58:16 +01001272 while (1)
1273 {
Yann Collet213089c2015-06-18 07:43:16 -08001274 if ( (FSE_reloadDStream(&bitD)>2) || (op==omax) || (FSE_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) )
Yann Collet4856a002015-01-24 01:58:16 +01001275 break;
1276
1277 *op++ = fast ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD);
1278
Yann Collet213089c2015-06-18 07:43:16 -08001279 if ( (FSE_reloadDStream(&bitD)>2) || (op==omax) || (FSE_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) )
Yann Collet4856a002015-01-24 01:58:16 +01001280 break;
1281
1282 *op++ = fast ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD);
1283 }
1284
1285 /* end ? */
1286 if (FSE_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2) )
1287 return op-ostart;
1288
1289 if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */
1290
1291 return (size_t)-FSE_ERROR_corruptionDetected;
1292}
1293
1294
1295size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
1296 const void* cSrc, size_t cSrcSize,
1297 const void* DTable, size_t fastMode)
1298{
1299 /* select fast mode (static) */
1300 if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, DTable, 1);
1301 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, DTable, 0);
1302}
1303
1304
1305size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1306{
1307 const BYTE* const istart = (const BYTE*)cSrc;
1308 const BYTE* ip = istart;
1309 short counting[FSE_MAX_SYMBOL_VALUE+1];
Yann Collet2ddf7e92015-02-08 20:26:47 +01001310 FSE_decode_t DTable[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
Yann Collet4856a002015-01-24 01:58:16 +01001311 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
1312 unsigned tableLog;
1313 size_t errorCode, fastMode;
1314
1315 if (cSrcSize<2) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */
1316
1317 /* normal FSE decoding mode */
1318 errorCode = FSE_readHeader (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1319 if (FSE_isError(errorCode)) return errorCode;
1320 if (errorCode >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */
1321 ip += errorCode;
1322 cSrcSize -= errorCode;
1323
1324 fastMode = FSE_buildDTable (DTable, counting, maxSymbolValue, tableLog);
1325 if (FSE_isError(fastMode)) return fastMode;
1326
1327 /* always return, even if it is an error code */
1328 return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, DTable, fastMode);
1329}
1330
1331
1332#endif /* FSE_COMMONDEFS_ONLY */
1333
1334/*
1335 2nd part of the file
1336 designed to be included
1337 for type-specific functions (template equivalent in C)
1338 Objective is to write such functions only once, for better maintenance
1339*/
1340
1341/* safety checks */
1342#ifndef FSE_FUNCTION_EXTENSION
1343# error "FSE_FUNCTION_EXTENSION must be defined"
1344#endif
1345#ifndef FSE_FUNCTION_TYPE
1346# error "FSE_FUNCTION_TYPE must be defined"
1347#endif
1348
1349/* Function names */
1350#define FSE_CAT(X,Y) X##Y
1351#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
1352#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
1353
1354
1355/* Function templates */
1356size_t FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (unsigned* count, const FSE_FUNCTION_TYPE* source, size_t sourceSize, unsigned* maxSymbolValuePtr, unsigned safe)
1357{
1358 const FSE_FUNCTION_TYPE* ip = source;
1359 const FSE_FUNCTION_TYPE* const iend = ip+sourceSize;
1360 unsigned maxSymbolValue = *maxSymbolValuePtr;
1361 unsigned max=0;
1362 int s;
1363
1364 U32 Counting1[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1365 U32 Counting2[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1366 U32 Counting3[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1367 U32 Counting4[FSE_MAX_SYMBOL_VALUE+1] = { 0 };
1368
1369 /* safety checks */
1370 if (!sourceSize)
1371 {
1372 memset(count, 0, (maxSymbolValue + 1) * sizeof(FSE_FUNCTION_TYPE));
1373 *maxSymbolValuePtr = 0;
1374 return 0;
1375 }
1376 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return (size_t)-FSE_ERROR_GENERIC; /* maxSymbolValue too large : unsupported */
1377 if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE; /* 0 == default */
1378
1379 if ((safe) || (sizeof(FSE_FUNCTION_TYPE)>1))
1380 {
1381 /* check input values, to avoid count table overflow */
1382 while (ip < iend-3)
1383 {
1384 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting1[*ip++]++;
1385 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting2[*ip++]++;
1386 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting3[*ip++]++;
1387 if (*ip>maxSymbolValue) return (size_t)-FSE_ERROR_GENERIC; Counting4[*ip++]++;
1388 }
1389 }
1390 else
1391 {
1392 U32 cached = FSE_read32(ip); ip += 4;
1393 while (ip < iend-15)
1394 {
1395 U32 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 c = cached; cached = FSE_read32(ip); ip += 4;
1411 Counting1[(BYTE) c ]++;
1412 Counting2[(BYTE)(c>>8) ]++;
1413 Counting3[(BYTE)(c>>16)]++;
1414 Counting4[ c>>24 ]++;
1415 }
1416 ip-=4;
1417 }
1418
1419 /* finish last symbols */
1420 while (ip<iend) { if ((safe) && (*ip>maxSymbolValue)) return (size_t)-FSE_ERROR_GENERIC; Counting1[*ip++]++; }
1421
1422 for (s=0; s<=(int)maxSymbolValue; s++)
1423 {
1424 count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];
1425 if (count[s] > max) max = count[s];
1426 }
1427
1428 while (!count[maxSymbolValue]) maxSymbolValue--;
1429 *maxSymbolValuePtr = maxSymbolValue;
1430 return (int)max;
1431}
1432
1433/* hidden fast variant (unsafe) */
1434size_t FSE_FUNCTION_NAME(FSE_countFast, FSE_FUNCTION_EXTENSION) (unsigned* count, const FSE_FUNCTION_TYPE* source, size_t sourceSize, unsigned* maxSymbolValuePtr)
1435{
1436 return FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (count, source, sourceSize, maxSymbolValuePtr, 0);
1437}
1438
1439size_t FSE_FUNCTION_NAME(FSE_count, FSE_FUNCTION_EXTENSION) (unsigned* count, const FSE_FUNCTION_TYPE* source, size_t sourceSize, unsigned* maxSymbolValuePtr)
1440{
1441 if ((sizeof(FSE_FUNCTION_TYPE)==1) && (*maxSymbolValuePtr >= 255))
1442 {
1443 *maxSymbolValuePtr = 255;
1444 return FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (count, source, sourceSize, maxSymbolValuePtr, 0);
1445 }
1446 return FSE_FUNCTION_NAME(FSE_count_generic, FSE_FUNCTION_EXTENSION) (count, source, sourceSize, maxSymbolValuePtr, 1);
1447}
1448
1449
Yann Collet213089c2015-06-18 07:43:16 -08001450#ifndef __clang_analyzer__ /* clang static analyzer doesn't understand that tableSymbol is necessarily entirely initialized */
1451
Yann Collet4856a002015-01-24 01:58:16 +01001452static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1453
1454size_t FSE_FUNCTION_NAME(FSE_buildCTable, FSE_FUNCTION_EXTENSION)
1455(void* CTable, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1456{
1457 const unsigned tableSize = 1 << tableLog;
1458 const unsigned tableMask = tableSize - 1;
1459 U16* tableU16 = ( (U16*) CTable) + 2;
1460 FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) (((U32*)CTable) + 1 + (tableLog ? tableSize>>1 : 1) );
1461 const unsigned step = FSE_tableStep(tableSize);
1462 unsigned cumul[FSE_MAX_SYMBOL_VALUE+2];
1463 U32 position = 0;
1464 FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE];
1465 U32 highThreshold = tableSize-1;
1466 unsigned symbol;
1467 unsigned i;
1468
1469 /* safety checks */
1470 if (((size_t)CTable) & 3) return (size_t)-FSE_ERROR_GENERIC; /* Must be allocated of 4 bytes boundaries */
1471
1472 /* header */
1473 tableU16[-2] = (U16) tableLog;
1474 tableU16[-1] = (U16) maxSymbolValue;
1475
1476 /* For explanations on how to distribute symbol values over the table :
1477 * http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
1478
1479 /* symbol start positions */
1480 cumul[0] = 0;
1481 for (i=1; i<=maxSymbolValue+1; i++)
1482 {
1483 if (normalizedCounter[i-1]==-1) /* Low prob symbol */
1484 {
1485 cumul[i] = cumul[i-1] + 1;
1486 tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(i-1);
1487 }
1488 else
1489 cumul[i] = cumul[i-1] + normalizedCounter[i-1];
1490 }
1491 cumul[maxSymbolValue+1] = tableSize+1;
1492
1493 /* Spread symbols */
1494 for (symbol=0; symbol<=maxSymbolValue; symbol++)
1495 {
1496 int nbOccurences;
1497 for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++)
1498 {
1499 tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
1500 position = (position + step) & tableMask;
1501 while (position > highThreshold) position = (position + step) & tableMask; /* Lowprob area */
1502 }
1503 }
1504
1505 if (position!=0) return (size_t)-FSE_ERROR_GENERIC; /* Must have gone through all positions */
1506
1507 /* Build table */
1508 for (i=0; i<tableSize; i++)
1509 {
1510 FSE_FUNCTION_TYPE s = tableSymbol[i];
1511 tableU16[cumul[s]++] = (U16) (tableSize+i); // Table U16 : sorted by symbol order; gives next state value
1512 }
1513
1514 // Build Symbol Transformation Table
1515 {
1516 unsigned s;
1517 unsigned total = 0;
1518 for (s=0; s<=maxSymbolValue; s++)
1519 {
1520 switch (normalizedCounter[s])
1521 {
1522 case 0:
1523 break;
1524 case -1:
1525 case 1:
1526 symbolTT[s].minBitsOut = (BYTE)tableLog;
1527 symbolTT[s].deltaFindState = total - 1;
1528 total ++;
1529 symbolTT[s].maxState = (U16)( (tableSize*2) - 1); /* ensures state <= maxState */
1530 break;
1531 default :
1532 symbolTT[s].minBitsOut = (BYTE)( (tableLog-1) - FSE_highbit32 (normalizedCounter[s]-1) );
1533 symbolTT[s].deltaFindState = total - normalizedCounter[s];
1534 total += normalizedCounter[s];
1535 symbolTT[s].maxState = (U16)( (normalizedCounter[s] << (symbolTT[s].minBitsOut+1)) - 1);
1536 }
1537 }
1538 }
1539
1540 return 0;
1541}
1542
Yann Collet213089c2015-06-18 07:43:16 -08001543#endif // __clang_analyzer__
1544
Yann Collet4856a002015-01-24 01:58:16 +01001545
1546#define FSE_DECODE_TYPE FSE_TYPE_NAME(FSE_decode_t, FSE_FUNCTION_EXTENSION)
1547
1548void* FSE_FUNCTION_NAME(FSE_createDTable, FSE_FUNCTION_EXTENSION) (unsigned tableLog)
1549{
1550 if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
1551 return malloc( ((size_t)1<<tableLog) * sizeof (FSE_DECODE_TYPE) );
1552}
1553
1554void FSE_FUNCTION_NAME(FSE_freeDTable, FSE_FUNCTION_EXTENSION) (void* DTable)
1555{
1556 free(DTable);
1557}
1558
1559
1560size_t FSE_FUNCTION_NAME(FSE_buildDTable, FSE_FUNCTION_EXTENSION)
1561(void* DTable, const short* const normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1562{
Yann Collet1cc58de2015-01-29 07:13:54 +01001563 U32* const base32 = (U32*)DTable;
Yann Collet4856a002015-01-24 01:58:16 +01001564 FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (base32+1);
1565 const U32 tableSize = 1 << tableLog;
1566 const U32 tableMask = tableSize-1;
1567 const U32 step = FSE_tableStep(tableSize);
1568 U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
1569 U32 position = 0;
1570 U32 highThreshold = tableSize-1;
Yann Collet213089c2015-06-18 07:43:16 -08001571 const S16 largeLimit= (S16)(1 << (tableLog-1));
Yann Collet4856a002015-01-24 01:58:16 +01001572 U32 noLarge = 1;
1573 U32 s;
1574
1575 /* Sanity Checks */
1576 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return (size_t)-FSE_ERROR_maxSymbolValue_tooLarge;
1577 if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_tableLog_tooLarge;
1578
1579 /* Init, lay down lowprob symbols */
1580 base32[0] = tableLog;
1581 for (s=0; s<=maxSymbolValue; s++)
1582 {
1583 if (normalizedCounter[s]==-1)
1584 {
1585 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
1586 symbolNext[s] = 1;
1587 }
1588 else
1589 {
1590 if (normalizedCounter[s] >= largeLimit) noLarge=0;
1591 symbolNext[s] = normalizedCounter[s];
1592 }
1593 }
1594
1595 /* Spread symbols */
1596 for (s=0; s<=maxSymbolValue; s++)
1597 {
1598 int i;
1599 for (i=0; i<normalizedCounter[s]; i++)
1600 {
1601 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
1602 position = (position + step) & tableMask;
1603 while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1604 }
1605 }
1606
1607 if (position!=0) return (size_t)-FSE_ERROR_GENERIC; /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1608
1609 /* Build Decoding table */
1610 {
1611 U32 i;
1612 for (i=0; i<tableSize; i++)
1613 {
1614 FSE_FUNCTION_TYPE symbol = tableDecode[i].symbol;
1615 U16 nextState = symbolNext[symbol]++;
1616 tableDecode[i].nbBits = (BYTE) (tableLog - FSE_highbit32 ((U32)nextState) );
1617 tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1618 }
1619 }
1620
1621 return noLarge;
1622}