-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathZstdDec.c
4067 lines (3709 loc) · 115 KB
/
ZstdDec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ZstdDec.c -- Zstd Decoder
2024-06-18 : the code was developed by Igor Pavlov, using Zstandard format
specification and original zstd decoder code as reference code.
original zstd decoder code: Copyright (c) Facebook, Inc. All rights reserved.
This source code is licensed under BSD 3-Clause License.
*/
#include "Precomp.h"
#include <string.h>
#include <stdlib.h>
// #include <stdio.h>
#include "Alloc.h"
#include "Xxh64.h"
#include "ZstdDec.h"
#include "CpuArch.h"
#if defined(MY_CPU_ARM64)
#include <arm_neon.h>
#endif
/* original-zstd still doesn't support window larger than 2 GiB.
So we also limit our decoder for 2 GiB window: */
#if defined(MY_CPU_64BIT) && 0 == 1
#define MAX_WINDOW_SIZE_LOG 41
#else
#define MAX_WINDOW_SIZE_LOG 31
#endif
typedef
#if MAX_WINDOW_SIZE_LOG < 32
UInt32
#else
size_t
#endif
CZstdDecOffset;
// for debug: simpler and smaller code but slow:
// #define Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
// #define SHOW_STAT
#ifdef SHOW_STAT
#include <stdio.h>
static unsigned g_Num_Blocks_Compressed = 0;
static unsigned g_Num_Blocks_memcpy = 0;
static unsigned g_Num_Wrap_memmove_Num = 0;
static unsigned g_Num_Wrap_memmove_Bytes = 0;
static unsigned g_NumSeqs_total = 0;
// static unsigned g_NumCopy = 0;
static unsigned g_NumOver = 0;
static unsigned g_NumOver2 = 0;
static unsigned g_Num_Match = 0;
static unsigned g_Num_Lits = 0;
static unsigned g_Num_LitsBig = 0;
static unsigned g_Num_Lit0 = 0;
static unsigned g_Num_Rep0 = 0;
static unsigned g_Num_Rep1 = 0;
static unsigned g_Num_Rep2 = 0;
static unsigned g_Num_Rep3 = 0;
static unsigned g_Num_Threshold_0 = 0;
static unsigned g_Num_Threshold_1 = 0;
static unsigned g_Num_Threshold_0sum = 0;
static unsigned g_Num_Threshold_1sum = 0;
#define STAT_UPDATE(v) v
#else
#define STAT_UPDATE(v)
#endif
#define STAT_INC(v) STAT_UPDATE(v++;)
typedef struct
{
const Byte *ptr;
size_t len;
}
CInBufPair;
#if defined(MY_CPU_ARM_OR_ARM64) || defined(MY_CPU_X86_OR_AMD64)
#if (defined(__clang__) && (__clang_major__ >= 6)) \
|| (defined(__GNUC__) && (__GNUC__ >= 6))
// disable for debug:
#define Z7_ZSTD_DEC_USE_BSR
#elif defined(_MSC_VER) && (_MSC_VER >= 1300)
// #if defined(MY_CPU_ARM_OR_ARM64)
#if (_MSC_VER >= 1600)
#include <intrin.h>
#endif
// disable for debug:
#define Z7_ZSTD_DEC_USE_BSR
#endif
#endif
#ifdef Z7_ZSTD_DEC_USE_BSR
#if defined(__clang__) || defined(__GNUC__)
#define MY_clz(x) ((unsigned)__builtin_clz((UInt32)x))
#else // #if defined(_MSC_VER)
#ifdef MY_CPU_ARM_OR_ARM64
#define MY_clz _CountLeadingZeros
#endif // MY_CPU_X86_OR_AMD64
#endif // _MSC_VER
#elif !defined(Z7_ZSTD_DEC_USE_LOG_TABLE)
#define Z7_ZSTD_DEC_USE_LOG_TABLE
#endif
static
Z7_FORCE_INLINE
unsigned GetHighestSetBit_32_nonzero_big(UInt32 num)
{
// (num != 0)
#ifdef MY_clz
return 31 - MY_clz(num);
#elif defined(Z7_ZSTD_DEC_USE_BSR)
{
unsigned long zz;
_BitScanReverse(&zz, num);
return zz;
}
#else
{
int i = -1;
for (;;)
{
i++;
num >>= 1;
if (num == 0)
return (unsigned)i;
}
}
#endif
}
#ifdef Z7_ZSTD_DEC_USE_LOG_TABLE
#define R1(a) a, a
#define R2(a) R1(a), R1(a)
#define R3(a) R2(a), R2(a)
#define R4(a) R3(a), R3(a)
#define R5(a) R4(a), R4(a)
#define R6(a) R5(a), R5(a)
#define R7(a) R6(a), R6(a)
#define R8(a) R7(a), R7(a)
#define R9(a) R8(a), R8(a)
#define Z7_ZSTD_FSE_MAX_ACCURACY 9
// states[] values in FSE_Generate() can use (Z7_ZSTD_FSE_MAX_ACCURACY + 1) bits.
static const Byte k_zstd_LogTable[2 << Z7_ZSTD_FSE_MAX_ACCURACY] =
{
R1(0), R1(1), R2(2), R3(3), R4(4), R5(5), R6(6), R7(7), R8(8), R9(9)
};
#define GetHighestSetBit_32_nonzero_small(num) (k_zstd_LogTable[num])
#else
#define GetHighestSetBit_32_nonzero_small GetHighestSetBit_32_nonzero_big
#endif
#ifdef MY_clz
#define UPDATE_BIT_OFFSET_FOR_PADDING(b, bitOffset) \
bitOffset -= (CBitCtr)(MY_clz(b) - 23);
#elif defined(Z7_ZSTD_DEC_USE_BSR)
#define UPDATE_BIT_OFFSET_FOR_PADDING(b, bitOffset) \
{ unsigned long zz; _BitScanReverse(&zz, b); bitOffset -= 8; bitOffset += zz; }
#else
#define UPDATE_BIT_OFFSET_FOR_PADDING(b, bitOffset) \
for (;;) { bitOffset--; if (b & 0x80) { break; } b <<= 1; }
#endif
#define SET_bitOffset_TO_PAD(bitOffset, src, srcLen) \
{ \
unsigned lastByte = (src)[(size_t)(srcLen) - 1]; \
if (lastByte == 0) return SZ_ERROR_DATA; \
bitOffset = (CBitCtr)((srcLen) * 8); \
UPDATE_BIT_OFFSET_FOR_PADDING(lastByte, bitOffset) \
}
#ifndef Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
#define SET_bitOffset_TO_PAD_and_SET_BIT_SIZE(bitOffset, src, srcLen_res) \
{ \
unsigned lastByte = (src)[(size_t)(srcLen_res) - 1]; \
if (lastByte == 0) return SZ_ERROR_DATA; \
srcLen_res *= 8; \
bitOffset = (CBitCtr)srcLen_res; \
UPDATE_BIT_OFFSET_FOR_PADDING(lastByte, bitOffset) \
}
#endif
/*
typedef Int32 CBitCtr_signed;
typedef Int32 CBitCtr;
*/
// /*
typedef ptrdiff_t CBitCtr_signed;
typedef ptrdiff_t CBitCtr;
// */
#define MATCH_LEN_MIN 3
#define kBlockSizeMax (1u << 17)
// #define Z7_ZSTD_DEC_PRINT_TABLE
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
#define NUM_OFFSET_SYMBOLS_PREDEF 29
#endif
#define NUM_OFFSET_SYMBOLS_MAX (MAX_WINDOW_SIZE_LOG + 1) // 32
#define NUM_LL_SYMBOLS 36
#define NUM_ML_SYMBOLS 53
#define FSE_NUM_SYMBOLS_MAX 53 // NUM_ML_SYMBOLS
// /*
#if !defined(MY_CPU_X86) || defined(__PIC__) || defined(MY_CPU_64BIT)
#define Z7_ZSTD_DEC_USE_BASES_IN_OBJECT
#endif
// */
// for debug:
// #define Z7_ZSTD_DEC_USE_BASES_LOCAL
// #define Z7_ZSTD_DEC_USE_BASES_IN_OBJECT
#define GLOBAL_TABLE(n) k_ ## n
#if defined(Z7_ZSTD_DEC_USE_BASES_LOCAL)
#define BASES_TABLE(n) a_ ## n
#elif defined(Z7_ZSTD_DEC_USE_BASES_IN_OBJECT)
#define BASES_TABLE(n) p->m_ ## n
#else
#define BASES_TABLE(n) GLOBAL_TABLE(n)
#endif
#define Z7_ZSTD_DEC_USE_ML_PLUS3
#if defined(Z7_ZSTD_DEC_USE_BASES_LOCAL) || \
defined(Z7_ZSTD_DEC_USE_BASES_IN_OBJECT)
#define SEQ_EXTRA_TABLES(n) \
Byte n ## SEQ_LL_EXTRA [NUM_LL_SYMBOLS]; \
Byte n ## SEQ_ML_EXTRA [NUM_ML_SYMBOLS]; \
UInt32 n ## SEQ_LL_BASES [NUM_LL_SYMBOLS]; \
UInt32 n ## SEQ_ML_BASES [NUM_ML_SYMBOLS]; \
#define Z7_ZSTD_DEC_USE_BASES_CALC
#ifdef Z7_ZSTD_DEC_USE_BASES_CALC
#define FILL_LOC_BASES(n, startSum) \
{ unsigned i; UInt32 sum = startSum; \
for (i = 0; i != Z7_ARRAY_SIZE(GLOBAL_TABLE(n ## _EXTRA)); i++) \
{ const unsigned a = GLOBAL_TABLE(n ## _EXTRA)[i]; \
BASES_TABLE(n ## _BASES)[i] = sum; \
/* if (sum != GLOBAL_TABLE(n ## _BASES)[i]) exit(1); */ \
sum += (UInt32)1 << a; \
BASES_TABLE(n ## _EXTRA)[i] = (Byte)a; }}
#define FILL_LOC_BASES_ALL \
FILL_LOC_BASES (SEQ_LL, 0) \
FILL_LOC_BASES (SEQ_ML, MATCH_LEN_MIN) \
#else
#define COPY_GLOBAL_ARR(n) \
memcpy(BASES_TABLE(n), GLOBAL_TABLE(n), sizeof(GLOBAL_TABLE(n)));
#define FILL_LOC_BASES_ALL \
COPY_GLOBAL_ARR (SEQ_LL_EXTRA) \
COPY_GLOBAL_ARR (SEQ_ML_EXTRA) \
COPY_GLOBAL_ARR (SEQ_LL_BASES) \
COPY_GLOBAL_ARR (SEQ_ML_BASES) \
#endif
#endif
/// The sequence decoding baseline and number of additional bits to read/add
#if !defined(Z7_ZSTD_DEC_USE_BASES_CALC)
static const UInt32 GLOBAL_TABLE(SEQ_LL_BASES) [NUM_LL_SYMBOLS] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 18, 20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
0x2000, 0x4000, 0x8000, 0x10000
};
#endif
static const Byte GLOBAL_TABLE(SEQ_LL_EXTRA) [NUM_LL_SYMBOLS] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16
};
#if !defined(Z7_ZSTD_DEC_USE_BASES_CALC)
static const UInt32 GLOBAL_TABLE(SEQ_ML_BASES) [NUM_ML_SYMBOLS] =
{
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 37, 39, 41, 43, 47, 51, 59, 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
0x1003, 0x2003, 0x4003, 0x8003, 0x10003
};
#endif
static const Byte GLOBAL_TABLE(SEQ_ML_EXTRA) [NUM_ML_SYMBOLS] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16
};
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
static const Int16 SEQ_LL_PREDEF_DIST [NUM_LL_SYMBOLS] =
{
4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
-1,-1,-1,-1
};
static const Int16 SEQ_OFFSET_PREDEF_DIST [NUM_OFFSET_SYMBOLS_PREDEF] =
{
1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1
};
static const Int16 SEQ_ML_PREDEF_DIST [NUM_ML_SYMBOLS] =
{
1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
-1,-1,-1,-1,-1
};
#endif
// typedef int FastInt;
// typedef Int32 FastInt32;
typedef unsigned FastInt;
typedef UInt32 FastInt32;
typedef FastInt32 CFseRecord;
#define FSE_REC_LEN_OFFSET 8
#define FSE_REC_STATE_OFFSET 16
#define GET_FSE_REC_SYM(st) ((Byte)(st))
#define GET_FSE_REC_LEN(st) ((Byte)((st) >> FSE_REC_LEN_OFFSET))
#define GET_FSE_REC_STATE(st) ((st) >> FSE_REC_STATE_OFFSET)
// #define FSE_REC_SYM_MASK (0xff)
// #define GET_FSE_REC_SYM(st) (st & FSE_REC_SYM_MASK)
#define W_BASE(state, len, sym) \
(((UInt32)state << (4 + FSE_REC_STATE_OFFSET)) + \
(len << FSE_REC_LEN_OFFSET) + (sym))
#define W(state, len, sym) W_BASE(state, len, sym)
static const CFseRecord k_PredefRecords_LL[1 << 6] = {
W(0,4, 0),W(1,4, 0),W(2,5, 1),W(0,5, 3),W(0,5, 4),W(0,5, 6),W(0,5, 7),W(0,5, 9),
W(0,5,10),W(0,5,12),W(0,6,14),W(0,5,16),W(0,5,18),W(0,5,19),W(0,5,21),W(0,5,22),
W(0,5,24),W(2,5,25),W(0,5,26),W(0,6,27),W(0,6,29),W(0,6,31),W(2,4, 0),W(0,4, 1),
W(0,5, 2),W(2,5, 4),W(0,5, 5),W(2,5, 7),W(0,5, 8),W(2,5,10),W(0,5,11),W(0,6,13),
W(2,5,16),W(0,5,17),W(2,5,19),W(0,5,20),W(2,5,22),W(0,5,23),W(0,4,25),W(1,4,25),
W(2,5,26),W(0,6,28),W(0,6,30),W(3,4, 0),W(1,4, 1),W(2,5, 2),W(2,5, 3),W(2,5, 5),
W(2,5, 6),W(2,5, 8),W(2,5, 9),W(2,5,11),W(2,5,12),W(0,6,15),W(2,5,17),W(2,5,18),
W(2,5,20),W(2,5,21),W(2,5,23),W(2,5,24),W(0,6,35),W(0,6,34),W(0,6,33),W(0,6,32)
};
static const CFseRecord k_PredefRecords_OF[1 << 5] = {
W(0,5, 0),W(0,4, 6),W(0,5, 9),W(0,5,15),W(0,5,21),W(0,5, 3),W(0,4, 7),W(0,5,12),
W(0,5,18),W(0,5,23),W(0,5, 5),W(0,4, 8),W(0,5,14),W(0,5,20),W(0,5, 2),W(1,4, 7),
W(0,5,11),W(0,5,17),W(0,5,22),W(0,5, 4),W(1,4, 8),W(0,5,13),W(0,5,19),W(0,5, 1),
W(1,4, 6),W(0,5,10),W(0,5,16),W(0,5,28),W(0,5,27),W(0,5,26),W(0,5,25),W(0,5,24)
};
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
#undef W
#define W(state, len, sym) W_BASE(state, len, (sym + MATCH_LEN_MIN))
#endif
static const CFseRecord k_PredefRecords_ML[1 << 6] = {
W(0,6, 0),W(0,4, 1),W(2,5, 2),W(0,5, 3),W(0,5, 5),W(0,5, 6),W(0,5, 8),W(0,6,10),
W(0,6,13),W(0,6,16),W(0,6,19),W(0,6,22),W(0,6,25),W(0,6,28),W(0,6,31),W(0,6,33),
W(0,6,35),W(0,6,37),W(0,6,39),W(0,6,41),W(0,6,43),W(0,6,45),W(1,4, 1),W(0,4, 2),
W(2,5, 3),W(0,5, 4),W(2,5, 6),W(0,5, 7),W(0,6, 9),W(0,6,12),W(0,6,15),W(0,6,18),
W(0,6,21),W(0,6,24),W(0,6,27),W(0,6,30),W(0,6,32),W(0,6,34),W(0,6,36),W(0,6,38),
W(0,6,40),W(0,6,42),W(0,6,44),W(2,4, 1),W(3,4, 1),W(1,4, 2),W(2,5, 4),W(2,5, 5),
W(2,5, 7),W(2,5, 8),W(0,6,11),W(0,6,14),W(0,6,17),W(0,6,20),W(0,6,23),W(0,6,26),
W(0,6,29),W(0,6,52),W(0,6,51),W(0,6,50),W(0,6,49),W(0,6,48),W(0,6,47),W(0,6,46)
};
// sum of freqs[] must be correct
// (numSyms != 0)
// (accuracy >= 5)
static
Z7_NO_INLINE
// Z7_FORCE_INLINE
void FSE_Generate(CFseRecord *table,
const Int16 *const freqs, const size_t numSyms,
const unsigned accuracy, UInt32 delta)
{
size_t size = (size_t)1 << accuracy;
// max value in states[x] is ((1 << accuracy) * 2)
UInt16 states[FSE_NUM_SYMBOLS_MAX];
{
/* Symbols with "less than 1" probability get a single cell,
starting from the end of the table.
These symbols define a full state reset, reading (accuracy) bits. */
size_t threshold = size;
{
size_t s = 0;
do
if (freqs[s] == -1)
{
table[--threshold] = (CFseRecord)s;
states[s] = 1;
}
while (++s != numSyms);
}
#ifdef SHOW_STAT
if (threshold == size)
{
STAT_INC(g_Num_Threshold_0)
STAT_UPDATE(g_Num_Threshold_0sum += (unsigned)size;)
}
else
{
STAT_INC(g_Num_Threshold_1)
STAT_UPDATE(g_Num_Threshold_1sum += (unsigned)size;)
}
#endif
// { unsigned uuu; for (uuu = 0; uuu < 400; uuu++)
{
// Each (symbol) gets freqs[symbol] cells.
// Cell allocation is spread, not linear.
const size_t step = (size >> 1) + (size >> 3) + 3;
size_t pos = 0;
// const unsigned mask = size - 1;
/*
if (threshold == size)
{
size_t s = 0;
size--;
do
{
int freq = freqs[s];
if (freq <= 0)
continue;
states[s] = (UInt16)freq;
do
{
table[pos] (CFseRecord)s;
pos = (pos + step) & size; // & mask;
}
while (--freq);
}
while (++s != numSyms);
}
else
*/
{
size_t s = 0;
size--;
do
{
int freq = freqs[s];
if (freq <= 0)
continue;
states[s] = (UInt16)freq;
do
{
table[pos] = (CFseRecord)s;
// we skip position, if it's already occupied by a "less than 1" probability symbol.
// (step) is coprime to table size, so the cycle will visit each position exactly once
do
pos = (pos + step) & size; // & mask;
while (pos >= threshold);
}
while (--freq);
}
while (++s != numSyms);
}
size++;
// (pos != 0) is unexpected case that means that freqs[] are not correct.
// so it's some failure in code (for example, incorrect predefined freq[] table)
// if (pos != 0) return SZ_ERROR_FAIL;
}
// }
}
{
const CFseRecord * const limit = table + size;
delta = ((UInt32)size << FSE_REC_STATE_OFFSET) - delta;
/* State increases by symbol over time, decreasing number of bits.
Baseline increases until the bit threshold is passed, at which point it resets to 0 */
do
{
#define TABLE_ITER(a) \
{ \
const FastInt sym = (FastInt)table[a]; \
const unsigned nextState = states[sym]; \
unsigned nb; \
states[sym] = (UInt16)(nextState + 1); \
nb = accuracy - GetHighestSetBit_32_nonzero_small(nextState); \
table[a] = (CFseRecord)(sym - delta \
+ ((UInt32)nb << FSE_REC_LEN_OFFSET) \
+ ((UInt32)nextState << FSE_REC_STATE_OFFSET << nb)); \
}
TABLE_ITER(0)
TABLE_ITER(1)
table += 2;
}
while (table != limit);
}
}
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
static void Print_Predef(unsigned predefAccuracy,
const unsigned numSymsPredef,
const Int16 * const predefFreqs,
const CFseRecord *checkTable)
{
CFseRecord table[1 << 6];
unsigned i;
FSE_Generate(table, predefFreqs, numSymsPredef, predefAccuracy,
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
numSymsPredef == NUM_ML_SYMBOLS ? MATCH_LEN_MIN :
#endif
0
);
if (memcmp(table, checkTable, sizeof(UInt32) << predefAccuracy) != 0)
exit(1);
for (i = 0; i < (1u << predefAccuracy); i++)
{
const UInt32 v = table[i];
const unsigned state = (unsigned)(GET_FSE_REC_STATE(v));
if (state & 0xf)
exit(1);
if (i != 0)
{
printf(",");
if (i % 8 == 0)
printf("\n");
}
printf("W(%d,%d,%2d)",
(unsigned)(state >> 4),
(unsigned)((v >> FSE_REC_LEN_OFFSET) & 0xff),
(unsigned)GET_FSE_REC_SYM(v));
}
printf("\n\n");
}
#endif
#define GET16(dest, p) { const Byte *ptr = p; dest = GetUi16(ptr); }
#define GET32(dest, p) { const Byte *ptr = p; dest = GetUi32(ptr); }
// (1 <= numBits <= 9)
#define FORWARD_READ_BITS(destVal, numBits, mask) \
{ const CBitCtr_signed bos3 = (bitOffset) >> 3; \
if (bos3 >= 0) return SZ_ERROR_DATA; \
GET16(destVal, src + bos3) \
destVal >>= (bitOffset) & 7; \
bitOffset += (CBitCtr_signed)(numBits); \
mask = (1u << (numBits)) - 1; \
destVal &= mask; \
}
#define FORWARD_READ_1BIT(destVal) \
{ const CBitCtr_signed bos3 = (bitOffset) >> 3; \
if (bos3 >= 0) return SZ_ERROR_DATA; \
destVal = *(src + bos3); \
destVal >>= (bitOffset) & 7; \
(bitOffset)++; \
destVal &= 1; \
}
// in: (accuracyMax <= 9)
// at least 2 bytes will be processed from (in) stream.
// at return: (in->len > 0)
static
Z7_NO_INLINE
SRes FSE_DecodeHeader(CFseRecord *const table,
CInBufPair *const in,
const unsigned accuracyMax,
Byte *const accuracyRes,
unsigned numSymbolsMax)
{
unsigned accuracy;
unsigned remain1;
unsigned syms;
Int16 freqs[FSE_NUM_SYMBOLS_MAX + 3]; // +3 for overwrite (repeat)
const Byte *src = in->ptr;
CBitCtr_signed bitOffset = (CBitCtr_signed)in->len - 1;
if (bitOffset <= 0)
return SZ_ERROR_DATA;
accuracy = *src & 0xf;
accuracy += 5;
if (accuracy > accuracyMax)
return SZ_ERROR_DATA;
*accuracyRes = (Byte)accuracy;
remain1 = (1u << accuracy) + 1; // (it's remain_freqs_sum + 1)
syms = 0;
src += bitOffset; // src points to last byte
bitOffset = 4 - (bitOffset << 3);
for (;;)
{
// (2 <= remain1)
const unsigned bits = GetHighestSetBit_32_nonzero_small((unsigned)remain1);
// (1 <= bits <= accuracy)
unsigned val; // it must be unsigned or int
unsigned mask;
FORWARD_READ_BITS(val, bits, mask)
{
const unsigned val2 = remain1 + val - mask;
if (val2 > mask)
{
unsigned bit;
FORWARD_READ_1BIT(bit)
if (bit)
val = val2;
}
}
{
// (remain1 >= 2)
// (0 <= (int)val <= remain1)
val = (unsigned)((int)val - 1);
// val now is "probability" of symbol
// (probability == -1) means "less than 1" frequency.
// (-1 <= (int)val <= remain1 - 1)
freqs[syms++] = (Int16)(int)val;
if (val != 0)
{
remain1 -= (int)val < 0 ? 1u : (unsigned)val;
// remain1 -= val;
// val >>= (sizeof(val) * 8 - 2);
// remain1 -= val & 2;
// freqs[syms++] = (Int16)(int)val;
// syms++;
if (remain1 == 1)
break;
if (syms >= FSE_NUM_SYMBOLS_MAX)
return SZ_ERROR_DATA;
}
else // if (val == 0)
{
// freqs[syms++] = 0;
// syms++;
for (;;)
{
unsigned repeat;
FORWARD_READ_BITS(repeat, 2, mask)
freqs[syms ] = 0;
freqs[syms + 1] = 0;
freqs[syms + 2] = 0;
syms += repeat;
if (syms >= FSE_NUM_SYMBOLS_MAX)
return SZ_ERROR_DATA;
if (repeat != 3)
break;
}
}
}
}
if (syms > numSymbolsMax)
return SZ_ERROR_DATA;
bitOffset += 7;
bitOffset >>= 3;
if (bitOffset > 0)
return SZ_ERROR_DATA;
in->ptr = src + bitOffset;
in->len = (size_t)(1 - bitOffset);
{
// unsigned uuu; for (uuu = 0; uuu < 50; uuu++)
FSE_Generate(table, freqs, syms, accuracy,
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
numSymbolsMax == NUM_ML_SYMBOLS ? MATCH_LEN_MIN :
#endif
0
);
}
return SZ_OK;
}
// ---------- HUFFMAN ----------
#define HUF_MAX_BITS 12
#define HUF_MAX_SYMBS 256
#define HUF_DUMMY_SIZE (128 + 8 * 2) // it must multiple of 8
// #define HUF_DUMMY_SIZE 0
#define HUF_TABLE_SIZE ((2 << HUF_MAX_BITS) + HUF_DUMMY_SIZE)
#define HUF_GET_SYMBOLS(table) ((table) + (1 << HUF_MAX_BITS) + HUF_DUMMY_SIZE)
// #define HUF_GET_LENS(table) (table)
typedef struct
{
// Byte table[HUF_TABLE_SIZE];
UInt64 table64[HUF_TABLE_SIZE / sizeof(UInt64)];
}
CZstdDecHufTable;
/*
Input:
numSyms != 0
(bits) array size must be aligned for 2
if (numSyms & 1), then bits[numSyms] == 0,
Huffman tree must be correct before Huf_Build() call:
(sum (1/2^bits[i]) == 1).
&& (bits[i] <= HUF_MAX_BITS)
*/
static
Z7_FORCE_INLINE
void Huf_Build(Byte * const table,
const Byte *bits, const unsigned numSyms)
{
unsigned counts0[HUF_MAX_BITS + 1];
unsigned counts1[HUF_MAX_BITS + 1];
const Byte * const bitsEnd = bits + numSyms;
// /*
{
unsigned t;
for (t = 0; t < Z7_ARRAY_SIZE(counts0); t++) counts0[t] = 0;
for (t = 0; t < Z7_ARRAY_SIZE(counts1); t++) counts1[t] = 0;
}
// */
// memset(counts0, 0, sizeof(counts0));
// memset(counts1, 0, sizeof(counts1));
{
const Byte *bits2 = bits;
// we access additional bits[symbol] if (numSyms & 1)
do
{
counts0[bits2[0]]++;
counts1[bits2[1]]++;
}
while ((bits2 += 2) < bitsEnd);
}
{
unsigned r = 0;
unsigned i = HUF_MAX_BITS;
// Byte *lens = HUF_GET_LENS(symbols);
do
{
const unsigned num = (counts0[i] + counts1[i]) << (HUF_MAX_BITS - i);
counts0[i] = r;
if (num)
{
Byte *lens = &table[r];
r += num;
memset(lens, (int)i, num);
}
}
while (--i);
counts0[0] = 0; // for speculated loads
// no need for check:
// if (r != (UInt32)1 << HUF_MAX_BITS) exit(0);
}
{
#ifdef MY_CPU_64BIT
UInt64
#else
UInt32
#endif
v = 0;
Byte *symbols = HUF_GET_SYMBOLS(table);
do
{
const unsigned nb = *bits++;
if (nb)
{
const unsigned code = counts0[nb];
const unsigned num = (1u << HUF_MAX_BITS) >> nb;
counts0[nb] = code + num;
// memset(&symbols[code], i, num);
// /*
{
Byte *s2 = &symbols[code];
if (num <= 2)
{
s2[0] = (Byte)v;
s2[(size_t)num - 1] = (Byte)v;
}
else if (num <= 8)
{
*(UInt32 *)(void *)s2 = (UInt32)v;
*(UInt32 *)(void *)(s2 + (size_t)num - 4) = (UInt32)v;
}
else
{
#ifdef MY_CPU_64BIT
UInt64 *s = (UInt64 *)(void *)s2;
const UInt64 *lim = (UInt64 *)(void *)(s2 + num);
do
{
s[0] = v; s[1] = v; s += 2;
}
while (s != lim);
#else
UInt32 *s = (UInt32 *)(void *)s2;
const UInt32 *lim = (const UInt32 *)(const void *)(s2 + num);
do
{
s[0] = v; s[1] = v; s += 2;
s[0] = v; s[1] = v; s += 2;
}
while (s != lim);
#endif
}
}
// */
}
v +=
#ifdef MY_CPU_64BIT
0x0101010101010101;
#else
0x01010101;
#endif
}
while (bits != bitsEnd);
}
}
// how many bytes (src) was moved back from original value.
// we need (HUF_SRC_OFFSET == 3) for optimized 32-bit memory access
#define HUF_SRC_OFFSET 3
// v <<= 8 - (bitOffset & 7) + numBits;
// v >>= 32 - HUF_MAX_BITS;
#define HUF_GET_STATE(v, bitOffset, numBits) \
GET32(v, src + (HUF_SRC_OFFSET - 3) + ((CBitCtr_signed)bitOffset >> 3)) \
v >>= 32 - HUF_MAX_BITS - 8 + ((unsigned)bitOffset & 7) - numBits; \
v &= (1u << HUF_MAX_BITS) - 1; \
#ifndef Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
#if defined(MY_CPU_AMD64) && defined(_MSC_VER) && _MSC_VER == 1400 \
|| !defined(MY_CPU_X86_OR_AMD64) \
// || 1 == 1 /* for debug : to force STREAM4_PRELOAD mode */
// we need big number (>=16) of registers for PRELOAD4
#define Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD4
// #define Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD2 // for debug
#endif
#endif
// for debug: simpler and smaller code but slow:
// #define Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE
#if defined(Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE) || \
!defined(Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS)
#define HUF_DECODE(bitOffset, dest) \
{ \
UInt32 v; \
HUF_GET_STATE(v, bitOffset, 0) \
bitOffset -= table[v]; \
*(dest) = symbols[v]; \
if ((CBitCtr_signed)bitOffset < 0) return SZ_ERROR_DATA; \
}
#endif
#if !defined(Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE) || \
defined(Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD4) || \
defined(Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD2) \
#define HUF_DECODE_2_INIT(v, bitOffset) \
HUF_GET_STATE(v, bitOffset, 0)
#define HUF_DECODE_2(v, bitOffset, dest) \
{ \
unsigned numBits; \
numBits = table[v]; \
*(dest) = symbols[v]; \
HUF_GET_STATE(v, bitOffset, numBits) \
bitOffset -= (CBitCtr)numBits; \
if ((CBitCtr_signed)bitOffset < 0) return SZ_ERROR_DATA; \
}
#endif
// src == ptr - HUF_SRC_OFFSET
// we are allowed to access 3 bytes before start of input buffer
static
Z7_NO_INLINE
SRes Huf_Decompress_1stream(const Byte * const table,
const Byte *src, const size_t srcLen,
Byte *dest, const size_t destLen)
{
CBitCtr bitOffset;
if (srcLen == 0)
return SZ_ERROR_DATA;
SET_bitOffset_TO_PAD (bitOffset, src + HUF_SRC_OFFSET, srcLen)
if (destLen)
{
const Byte *symbols = HUF_GET_SYMBOLS(table);
const Byte *destLim = dest + destLen;
#ifdef Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE
{
do
{
HUF_DECODE (bitOffset, dest)
}
while (++dest != destLim);
}
#else
{
UInt32 v;
HUF_DECODE_2_INIT (v, bitOffset)
do
{
HUF_DECODE_2 (v, bitOffset, dest)
}
while (++dest != destLim);
}
#endif
}
return bitOffset == 0 ? SZ_OK : SZ_ERROR_DATA;
}
// for debug : it reduces register pressure : by array copy can be slow :
// #define Z7_ZSTD_DEC_USE_HUF_LOCAL
// src == ptr + (6 - HUF_SRC_OFFSET)
// srcLen >= 10
// we are allowed to access 3 bytes before start of input buffer
static
Z7_NO_INLINE
SRes Huf_Decompress_4stream(const Byte * const
#ifdef Z7_ZSTD_DEC_USE_HUF_LOCAL
table2,
#else
table,
#endif
const Byte *src, size_t srcLen,
Byte *dest, size_t destLen)
{
#ifdef Z7_ZSTD_DEC_USE_HUF_LOCAL
Byte table[HUF_TABLE_SIZE];
#endif
UInt32 sizes[3];
const size_t delta = (destLen + 3) / 4;
if ((sizes[0] = GetUi16(src + (0 + HUF_SRC_OFFSET - 6))) == 0) return SZ_ERROR_DATA;
if ((sizes[1] = GetUi16(src + (2 + HUF_SRC_OFFSET - 6))) == 0) return SZ_ERROR_DATA;
sizes[1] += sizes[0];
if ((sizes[2] = GetUi16(src + (4 + HUF_SRC_OFFSET - 6))) == 0) return SZ_ERROR_DATA;
sizes[2] += sizes[1];
srcLen -= 6;
if (srcLen <= sizes[2])
return SZ_ERROR_DATA;
#ifdef Z7_ZSTD_DEC_USE_HUF_LOCAL
{
// unsigned i = 0; for(; i < 1000; i++)
memcpy(table, table2, HUF_TABLE_SIZE);
}
#endif
#ifndef Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
{
CBitCtr bitOffset_0,
bitOffset_1,
bitOffset_2,
bitOffset_3;
{
SET_bitOffset_TO_PAD_and_SET_BIT_SIZE (bitOffset_0, src + HUF_SRC_OFFSET, sizes[0])
SET_bitOffset_TO_PAD_and_SET_BIT_SIZE (bitOffset_1, src + HUF_SRC_OFFSET, sizes[1])
SET_bitOffset_TO_PAD_and_SET_BIT_SIZE (bitOffset_2, src + HUF_SRC_OFFSET, sizes[2])
SET_bitOffset_TO_PAD (bitOffset_3, src + HUF_SRC_OFFSET, srcLen)
}
{
const Byte * const symbols = HUF_GET_SYMBOLS(table);
Byte *destLim = dest + destLen - delta * 3;
if (dest != destLim)
#ifdef Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD4
{
UInt32 v_0, v_1, v_2, v_3;
HUF_DECODE_2_INIT (v_0, bitOffset_0)
HUF_DECODE_2_INIT (v_1, bitOffset_1)
HUF_DECODE_2_INIT (v_2, bitOffset_2)
HUF_DECODE_2_INIT (v_3, bitOffset_3)
// #define HUF_DELTA (1 << 17) / 4
do
{
HUF_DECODE_2 (v_3, bitOffset_3, dest + delta * 3)
HUF_DECODE_2 (v_2, bitOffset_2, dest + delta * 2)
HUF_DECODE_2 (v_1, bitOffset_1, dest + delta)
HUF_DECODE_2 (v_0, bitOffset_0, dest)
}
while (++dest != destLim);
/*
{// unsigned y = 0; for (;y < 1; y++)