forked from rarten/ooz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compr_leviathan.cpp
2115 lines (1901 loc) · 81.3 KB
/
compr_leviathan.cpp
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
// This file is not GPL. It may be used for educational purposes only.
#include "stdafx.h"
#include "compr_leviathan.h"
#include "compress.h"
#include "compr_util.h"
#include "compr_entropy.h"
#include "compr_match_finder.h"
#include "match_hasher.h"
#include <algorithm>
#include <memory>
enum IsRecent {
kOFFSET = 0,
kRECENT = 1,
};
struct Levi {
enum {
kMinBytesPerRound = 384,
kMaxBytesPerRound = 4096,
kRecentOffsetCount = 7,
};
struct Token {
int recent_offset0;
int litoffs;
int litlen;
int matchlen;
int offset;
};
struct TokenArray {
Token *data;
int size, capacity;
};
struct State {
int best_bit_count;
int recent_offs[7];
int matchlen;
int litlen;
int quick_recent_matchlen_litlen;
int prev_state;
void Initialize() {
best_bit_count = 0;
recent_offs[0] = 8;
recent_offs[1] = 8;
recent_offs[2] = 8;
recent_offs[3] = 8;
recent_offs[4] = 8;
recent_offs[5] = 8;
recent_offs[6] = 8;
matchlen = 0;
litlen = 0;
prev_state = 0;
quick_recent_matchlen_litlen = 0;
}
};
struct LitStats {
int total_lits;
HistoU8 lit_histo;
HistoU8 litsub_histo;
HistoU8 litsub_histo_2nd;
HistoU8 o1_histo[16];
HistoU8 litsub3_histo[4];
HistoU8 litsubf_histo[16];
static void Update(LitStats *h, const uint8 *src, int offs, int num, int recent, int increment) {
if (!num)
return;
const uint8 *p = src + offs;
const uint8 *rp = src + offs - recent;
h->total_lits += num;
h->lit_histo.count[*p] += increment;
h->litsub_histo.count[(uint8)(*p - *rp)] += increment;
h->litsub3_histo[offs & 0x3].count[(uint8)(*p - *rp)] += increment;
h->litsubf_histo[offs & 0xF].count[(uint8)(*p - *rp)] += increment;
h->o1_histo[p[-1] >> 4].count[*p] += increment;
while (--num) {
p++, rp++, offs++;
h->lit_histo.count[*p] += increment;
h->litsub_histo_2nd.count[(uint8)(*p - *rp)] += increment;
h->litsub3_histo[offs & 0x3].count[(uint8)(*p - *rp)] += increment;
h->litsubf_histo[offs & 0xF].count[(uint8)(*p - *rp)] += increment;
h->o1_histo[p[-1] >> 4].count[*p] += increment;
}
}
};
struct Stats {
LitStats lit;
HistoU8 xhisto;
HistoU8 single_token_histo;
HistoU8 multi_token_histo[8];
char uses_multi_token_mode;
HistoU8 matchlen_histo;
HistoU8 litlen_histo;
int offs_encode_type;
HistoU8 dist_histo;
HistoU8 distlo_histo;
static void RescaleOne(HistoU8 *histo) {
for (size_t i = 0; i != 256; i++)
histo->count[i] = (histo->count[i] + 5) >> 3;
}
static void Rescale(Stats *h) {
RescaleOne(&h->lit.lit_histo);
RescaleOne(&h->lit.litsub_histo);
RescaleOne(&h->lit.litsub_histo_2nd);
for (size_t i = 0; i != 16; i++)
RescaleOne(&h->lit.o1_histo[i]);
for (size_t i = 0; i != 4; i++)
RescaleOne(&h->lit.litsub3_histo[i]);
for (size_t i = 0; i != 16; i++)
RescaleOne(&h->lit.litsubf_histo[i]);
RescaleOne(&h->xhisto);
RescaleOne(&h->dist_histo);
if (h->offs_encode_type > 1)
RescaleOne(&h->distlo_histo);
RescaleOne(&h->single_token_histo);
for (size_t i = 0; i != 4; i++) // todo: oodle bug, why not 8
RescaleOne(&h->multi_token_histo[i]);
RescaleOne(&h->matchlen_histo);
RescaleOne(&h->litlen_histo);
}
static void RescaleAddOne(HistoU8 *histo, const HistoU8 *histo2) {
for (size_t i = 0; i != 256; i++)
histo->count[i] = (histo->count[i] + histo2->count[i] + 11) >> 4;
}
static void RescaleAdd(Stats *h, const Stats *b) {
RescaleAddOne(&h->lit.lit_histo, &b->lit.lit_histo);
RescaleAddOne(&h->lit.litsub_histo, &b->lit.litsub_histo);
RescaleAddOne(&h->lit.litsub_histo_2nd, &b->lit.litsub_histo_2nd);
for (size_t i = 0; i != 16; i++)
RescaleAddOne(&h->lit.o1_histo[i], &b->lit.o1_histo[i]);
for (size_t i = 0; i != 4; i++)
RescaleAddOne(&h->lit.litsub3_histo[i], &b->lit.litsub3_histo[i]);
for (size_t i = 0; i != 16; i++)
RescaleAddOne(&h->lit.litsubf_histo[i], &b->lit.litsubf_histo[i]);
RescaleAddOne(&h->xhisto, &b->xhisto);
RescaleAddOne(&h->single_token_histo, &b->single_token_histo);
for (size_t i = 0; i != 4; i++) // todo: oodle bug, why not 8
RescaleAddOne(&h->multi_token_histo[i], &b->multi_token_histo[i]);
RescaleAddOne(&h->matchlen_histo, &b->matchlen_histo);
RescaleAddOne(&h->litlen_histo, &b->litlen_histo);
if (h->offs_encode_type == b->offs_encode_type) {
if (h->offs_encode_type > 1)
RescaleAddOne(&h->distlo_histo, &b->distlo_histo);
RescaleAddOne(&h->dist_histo, &b->dist_histo);
} else {
if (h->offs_encode_type > 1)
RescaleOne(&h->distlo_histo);
RescaleOne(&h->dist_histo);
}
}
static void ReduceIfHighOne(HistoU8 *histo) {
if (GetHistoSum(*histo) >= 9000) {
for (size_t i = 0; i != 256; i++)
histo->count[i] = (histo->count[i] + 1) >> 1;
}
}
static void ReduceIfHigh(Stats *h) {
ReduceIfHighOne(&h->lit.lit_histo);
ReduceIfHighOne(&h->lit.litsub_histo);
ReduceIfHighOne(&h->lit.litsub_histo_2nd);
for (size_t i = 0; i != 16; i++)
ReduceIfHighOne(&h->lit.o1_histo[i]);
for (size_t i = 0; i != 4; i++)
ReduceIfHighOne(&h->lit.litsub3_histo[i]);
for (size_t i = 0; i != 16; i++)
ReduceIfHighOne(&h->lit.litsubf_histo[i]);
ReduceIfHighOne(&h->xhisto);
ReduceIfHighOne(&h->dist_histo);
if (h->offs_encode_type > 1)
ReduceIfHighOne(&h->distlo_histo);
ReduceIfHighOne(&h->single_token_histo);
for (size_t i = 0; i != 4; i++) // todo: oodle bug, why not 8
ReduceIfHighOne(&h->multi_token_histo[i]);
ReduceIfHighOne(&h->matchlen_histo);
ReduceIfHighOne(&h->litlen_histo);
}
static void Update(Stats *h, const uint8 *src, int pos, const Levi::Token *tokens, int num_token) {
enum {
kIncrement = 5
};
for (int i = 0; i < num_token; i++) {
const Levi::Token *t = &tokens[i];
int litlen = t->litlen;
int recent = t->recent_offset0;
LitStats::Update(&h->lit, src, pos, litlen, recent, kIncrement);
for (int j = 0; j < litlen; j++)
h->xhisto.count[(uint8)(src[pos + j] - src[pos + j - recent])] += kIncrement;
int token_pos = pos;
pos += litlen + t->matchlen;
int length_field = litlen;
if (litlen >= 3) {
h->litlen_histo.count[std::min(litlen - 3, 255)] += kIncrement;
length_field = 3;
}
if (t->matchlen < 2) {
assert(t->matchlen >= 2);
continue;
}
uint offset = t->offset;
int recent_field;
if (t->offset <= 0) {
recent_field = -(int)offset;
} else {
recent_field = 7;
if (h->offs_encode_type == 0) {
if (offset >= 8388360) {
uint t = BSR(offset - 8322816) | 0xF0;
h->dist_histo.count[t] += kIncrement;
} else {
uint t = ((offset - 8) & 0xF) + 16 * (BSR(offset - 8 + 256) - 8);
h->dist_histo.count[t] += kIncrement;
}
} else if (h->offs_encode_type == 1) {
uint t = BSR(offset + 8) - 3;
uint u = 8 * t | (((offset + 8) >> t) ^ 8);
h->dist_histo.count[u] += kIncrement;
} else {
uint ohi = offset / h->offs_encode_type, olo = offset % h->offs_encode_type;
uint t = BSR(ohi + 8) - 3;
uint u = 8 * t | (((ohi + 8) >> t) ^ 8);
h->dist_histo.count[u] += kIncrement;
h->distlo_histo.count[olo] += kIncrement;
}
}
int matchlen_field = t->matchlen - 2;
if (t->matchlen - 9 >= 0) {
h->matchlen_histo.count[std::min(t->matchlen - 9, 255)] += kIncrement;
matchlen_field = 7;
}
int token_value = matchlen_field + (recent_field << 5) + (length_field << 3);
h->single_token_histo.count[token_value] += kIncrement;
h->multi_token_histo[token_pos & 7].count[token_value] += kIncrement;
}
}
};
struct CostModel {
int chunk_type;
uint lit_cost[16][256];
uint token_cost[8][256];
int token_cost_mask;
int offs_encode_type;
uint dist_cost[256];
uint distlo_cost[256];
uint matchlen_cost[256];
uint litlen_cost[256];
};
static void MakeCostModel(const Stats *h, CostModel *cm) {
ConvertHistoToCost(h->dist_histo, cm->dist_cost, 12);
if (h->offs_encode_type > 1)
ConvertHistoToCost(h->distlo_histo, cm->distlo_cost, 0);
ConvertHistoToCost(h->matchlen_histo, cm->matchlen_cost, 6);
ConvertHistoToCost(h->litlen_histo, cm->litlen_cost, 2);
if (h->uses_multi_token_mode) {
cm->token_cost_mask = 7;
for (size_t i = 0; i != 8; i++)
ConvertHistoToCost(h->multi_token_histo[i], cm->token_cost[i], 6);
} else {
cm->token_cost_mask = 0;
ConvertHistoToCost(h->single_token_histo, cm->token_cost[0], 6);
}
switch (cm->chunk_type) {
case 0:
ConvertHistoToCost(h->xhisto, cm->lit_cost[0], 0);
break;
case 1:
ConvertHistoToCost(h->lit.lit_histo, cm->lit_cost[0], 0);
break;
case 2:
ConvertHistoToCost(h->lit.litsub_histo, cm->lit_cost[0], 0);
ConvertHistoToCost(h->lit.litsub_histo_2nd, cm->lit_cost[1], 0);
break;
case 3:
for (size_t i = 0; i != 4; i++)
ConvertHistoToCost(h->lit.litsub3_histo[i], cm->lit_cost[i], 0);
break;
case 4:
for (size_t i = 0; i != 16; i++)
ConvertHistoToCost(h->lit.o1_histo[i], cm->lit_cost[i], 0);
break;
case 5:
for (size_t i = 0; i != 16; i++)
ConvertHistoToCost(h->lit.litsubf_histo[i], cm->lit_cost[i], 0);
break;
}
}
struct SymStatsTwo {
int chunk_type[2];
Levi::Stats stats[2];
};
static __forceinline uint BitsForLitlen(CostModel &cost_model, int cur_litlen) {
if (cur_litlen < 3)
return 0;
if (cur_litlen - 3 >= 255) {
int v = BSR(((unsigned int)(cur_litlen - 3 - 255) >> 6) + 1);
return cost_model.litlen_cost[255] + 32 * (2 * v + 7);
} else {
return cost_model.litlen_cost[cur_litlen - 3];
}
}
static uint BitsForLit(const uint8 *src, int pos, int recent, const CostModel &cm, int litidx) {
const uint8 *p = src + pos;
switch (cm.chunk_type) {
case 0: return cm.lit_cost[0][(uint8)(p[0] - p[-recent])];
case 1: return cm.lit_cost[0][p[0]];
case 2: return cm.lit_cost[(litidx != 0)][(uint8)(p[0] - p[-recent])];
case 3: return cm.lit_cost[pos & 3][(uint8)(p[0] - p[-recent])];
case 4: return cm.lit_cost[p[-1] >> 4][p[0]];
case 5: return cm.lit_cost[pos & 0xf][(uint8)(p[0] - p[-recent])];
default:return 0;
}
}
static uint BitsForLits(const uint8 *src, int offs, int num, int recent, const CostModel &cm, int litidx) {
const uint8 *p = src + offs;
uint sum = 0;
if (num == 0)
return 0;
switch (cm.chunk_type) {
case 0:
do sum += cm.lit_cost[0][(uint8)(p[0] - p[-recent])]; while (p++, --num);
break;
case 1:
do sum += cm.lit_cost[0][p[0]]; while (p++, --num);
break;
case 2:
if (litidx == 0)
sum += cm.lit_cost[0][(uint8)(p[0] - p[-recent])], p++, num--;
while (num)
sum += cm.lit_cost[1][(uint8)(p[0] - p[-recent])], p++, num--;
break;
case 3:
do sum += cm.lit_cost[offs++ & 3][(uint8)(p[0] - p[-recent])]; while (p++, --num);
break;
case 4:
do sum += cm.lit_cost[p[-1] >> 4][p[0]]; while (p++, --num);
break;
case 5:
do sum += cm.lit_cost[offs++ & 15][(uint8)(p[0] - p[-recent])]; while (p++, --num);
break;
}
return sum;
}
static __forceinline int EnsureMatchLenShortEnough(int cur_matchlen, int offset) {
if (offset < 8)
cur_matchlen = std::min(cur_matchlen, offset);
return cur_matchlen;
}
static __forceinline bool CheckMatchValidLength(uint ml, uint offs) {
return (offs < 0x100000) || ((offs < 0x200000) ? (ml >= 5) : (offs < 0x400000) ? (ml >= 6) : (ml >= 8));
}
static __forceinline int GetMatchlengthQ(const uint8 *src, int offset, const uint8 *src_end, uint32 u32_at_cur) {
return EnsureMatchLenShortEnough(::GetMatchlengthQ(src, offset, src_end, u32_at_cur), offset);
}
static __forceinline int GetMatchlengthMin2(const uint8 *src_ptr_cur, int offset, const uint8 *src_ptr_safe_end) {
return EnsureMatchLenShortEnough(::GetMatchlengthMin2(src_ptr_cur, offset, src_ptr_safe_end), offset);
}
static __forceinline int BitsForToken(const CostModel &cost_model, int cur_matchlen, int cmd_offset, int recent_field, int length_field) {
if (cur_matchlen - 9 >= 0) {
int bits_for_matchlen;
if (cur_matchlen - 9 >= 255) {
int bsr = BSR(((unsigned int)(cur_matchlen - 9 - 255) >> 6) + 1);
bits_for_matchlen = cost_model.matchlen_cost[255] + 32 * (2 * bsr + 7);
} else {
bits_for_matchlen = cost_model.matchlen_cost[cur_matchlen - 9];
}
return cost_model.token_cost[cost_model.token_cost_mask & cmd_offset][7 + (recent_field << 5) + (length_field << 3)] + bits_for_matchlen;
} else {
return cost_model.token_cost[cost_model.token_cost_mask & cmd_offset][(cur_matchlen - 2) + (recent_field << 5) + (length_field << 3)];
}
}
static int BitsForOffset(const CostModel &cost_model, uint offs) {
if (cost_model.offs_encode_type == 0) {
if (offs >= 8388360) {
unsigned t = BSR(offs - 8322816) | 0xF0;
unsigned u = t - 224;
return cost_model.dist_cost[t] + 32 * u + 12;
} else {
unsigned t = ((offs - 8) & 0xF) + 16 * (BSR(offs - 8 + 256) - 8);
unsigned u = (t >> 4) + 4;
return cost_model.dist_cost[t] + 32 * u;
}
} else if (cost_model.offs_encode_type == 1) {
unsigned t = BSR(offs + 8) - 3;
unsigned u = 8 * t | (((offs + 8) >> t) ^ 8);
return cost_model.dist_cost[u] + 32 * (u >> 3);
} else {
unsigned ohi = offs / cost_model.offs_encode_type, olo = offs % cost_model.offs_encode_type;
unsigned t = BSR(ohi + 8) - 3;
unsigned u = 8 * t | (((ohi + 8) >> t) ^ 8);
return cost_model.dist_cost[u] + 32 * (u >> 3) + cost_model.distlo_cost[olo];
}
}
static __forceinline int GetRecentOffsetIndex(const int *arr, int offset) {
return (offset == arr[0]) ? 0 :
(offset == arr[1]) ? 1 :
(offset == arr[2]) ? 2 :
(offset == arr[3]) ? 3 :
(offset == arr[4]) ? 4 :
(offset == arr[5]) ? 5 :
(offset == arr[6]) ? 6 : -1;
}
};
static __forceinline bool IsMatchLongEnough2(uint ml, uint offs) {
if (offs < 0x100000) {
if (offs < 0x4000) {
return ml >= 3;
} if (offs < 0x20000) {
return ml >= 4;
} else {
return ml >= 5;
}
} else {
if (offs < 0x400000) {
return ml >= 6;
} else {
return ml >= 8;
}
}
}
static __forceinline bool IsMatchLongEnough(uint ml, uint offs) {
switch (ml) {
case 0: return false;
case 1: return offs < 0x80;
case 2:
case 3: return offs < 0x4000;
case 4: return offs < 0x20000;
case 5: return offs < 0x100000;
case 6:
case 7: return offs < 0x400000;
default: return true;
}
}
static float GetTime_Leviathan(int platforms, int packed_size, int num_tokens, int u8_len) {
return CombineCostComponents(platforms,
200.0f + packed_size * 0.407f + num_tokens * 18.920f + u8_len * 3.716f,
200.0f + packed_size * 0.445f + num_tokens * 19.738f + u8_len * 7.407f,
200.0f + packed_size * 0.664f + num_tokens * 25.981f + u8_len * 7.029f,
200.0f + packed_size * 0.330f + num_tokens * 16.504f + u8_len * 3.000f);
}
static float GetCost_LeviathanOffsets(uint offs_encode_type, const uint32 *u32_offs, int offs_count, float speed_tradeoff, int platforms) {
uint low_histo[128] = { 0 };
HistoU8 high_histo = { 0 };
uint bits_for_data = 0;
for (int i = 0; i < offs_count; i++) {
uint32 offset = u32_offs[i];
uint ohi = offset / offs_encode_type, olo = offset % offs_encode_type;
uint t = BSR(ohi + 8) - 3;
uint u = 8 * t | (((ohi + 8) >> t) ^ 8);
bits_for_data += t;
high_histo.count[u]++;
low_histo[olo]++;
}
float cost = BITSUP(GetHistoCostApprox(high_histo, offs_count)) + BITSUP(bits_for_data);
if (offs_encode_type > 1) {
cost += CombineCostComponents1A(platforms, offs_count, 0.595f, 1.05f, 1.179f, 0.567f,
28.0f, 53.0f, 62.0f, 33.0f) * speed_tradeoff;
cost += BITSUP(GetHistoCostApprox(low_histo, 128, offs_count));
cost += GetTime_SingleHuffman(platforms, offs_count, 128) * speed_tradeoff;
}
return cost;
}
static int GetBestOffsetEncodingFast(const uint32 *u32_offs, int offs_count, float speed_tradeoff, int platforms) {
uint arr[129];
for (size_t i = 0; i < 129; i++)
arr[i] = i;
// look only at the short offsets
for (int i = 0; i < offs_count; i++) {
if (u32_offs[i] <= 128)
arr[u32_offs[i]] += 256;
}
struct sorter {
bool operator()(uint a, uint b) {
return a > b;
}
};
std::sort(arr, arr + 129, sorter());
float best_cost = GetCost_LeviathanOffsets(1, u32_offs, offs_count, speed_tradeoff, platforms);
int best_offs_encode_type = 1;
for (size_t i = 0; i != 4; i++) {
uint offs_encode_type = (uint8)arr[i];
if (offs_encode_type > 1) {
float cost = GetCost_LeviathanOffsets(offs_encode_type, u32_offs, offs_count, speed_tradeoff, platforms);
if (cost < best_cost) {
best_offs_encode_type = offs_encode_type;
best_cost = cost;
}
}
}
return best_offs_encode_type;
}
static int GetBestOffsetEncodingSlow(const uint32 *u32_offs, int offs_count, float speed_tradeoff, int platforms) {
if (offs_count < 32)
return 1;
int best_offs_encode_type = 0;
float best_cost = kInvalidCost;
for (uint offs_encode_type = 1; offs_encode_type <= 128; offs_encode_type++) {
float cost = GetCost_LeviathanOffsets(offs_encode_type, u32_offs, offs_count, speed_tradeoff, platforms);
if (cost < best_cost) {
best_offs_encode_type = offs_encode_type;
best_cost = cost;
}
}
return best_offs_encode_type;
}
static void EncodeNewOffsets(uint32 *u32_offs, int offs_count, uint8 *u8_offs_hi, uint8 *u8_offs_lo, int *bits_type1_ptr, int offs_encode_type, const uint8 *u8_offs, int *bits_type0_ptr) {
int bits_type0 = 0, bits_type1 = 0;
if (offs_encode_type == 1) {
for (int i = 0; i < offs_count; i++) {
bits_type0 += (u8_offs[i] >= 0xf0) ? u8_offs[i] - 0xe0 : (u8_offs[i] >> 4) + 4;
uint32 hi = u32_offs[i];
int v = BSR(hi + 8) - 3;
u8_offs_hi[i] = 8 * v | (((hi + 8) >> v) ^ 8);
bits_type1 += v;
}
} else {
for (int i = 0; i < offs_count; i++) {
bits_type0 += (u8_offs[i] >= 0xf0) ? u8_offs[i] - 0xe0 : (u8_offs[i] >> 4) + 4;
uint32 offs = u32_offs[i];
uint32 lo = offs % offs_encode_type;
uint32 hi = offs / offs_encode_type;
int v = BSR(hi + 8) - 3;
u8_offs_hi[i] = 8 * v | (((hi + 8) >> v) ^ 8);
u8_offs_lo[i] = lo;
bits_type1 += v;
}
}
*bits_type0_ptr = bits_type0;
*bits_type1_ptr = bits_type1;
}
static float GetTime_LeviathanOffset(int platforms, int offs_count, int offs_encode_type) {
if (offs_encode_type == 0) {
return CombineCostComponents1A(platforms, offs_count, 7.186f, 8.891f, 12.846f, 7.566f,
62.07f, 43.106f, 135.580f, 62.485f);
} else {
float r = CombineCostComponents1A(platforms, offs_count, 8.183f, 6.598f, 14.464f, 8.403f,
75.901f, 80.014f, 152.175f, 74.069f);
if (offs_encode_type > 1)
r += CombineCostComponents1A(platforms, offs_count, 0.595f, 1.05f, 1.179f, 0.567f,
28.0f, 53.0f, 62.0f, 33.0f);
return r;
}
}
int EncodeLzOffsets(uint8 *dst, uint8 *dst_end, uint8 *u8_offs, uint32 *u32_offs, int offs_count,
int opts, float speed_tradeoff, int platforms,
float *cost_ptr, int min_match_len, bool use_offset_modulo_coding,
int *offs_encode_type_ptr, int level, HistoU8 *histo_ptr, HistoU8 *histolo_ptr) {
int n = INT_MAX;
HistoU8 histobuf;
*cost_ptr = kInvalidCost;
if (min_match_len == 8) {
n = EncodeArrayU8(dst, dst_end, u8_offs, offs_count, opts, speed_tradeoff, platforms, cost_ptr, level, histo_ptr);
if (n < 0)
return -1;
*cost_ptr += CombineCostComponents1A(platforms, offs_count, 7.186f, 8.891f, 12.846f, 7.566f,
62.070f, 43.106f, 135.580f, 62.485f) * speed_tradeoff;
}
uint offs_encode_type = 0;
if (use_offset_modulo_coding) {
uint8 *temp = new uint8[offs_count * 4 + 16];
std::auto_ptr<uint8> temp_deleter(temp);
offs_encode_type = 1;
if (level >= 8) {
offs_encode_type = GetBestOffsetEncodingSlow(u32_offs, offs_count, speed_tradeoff, platforms);
} else if (level >= 4) {
offs_encode_type = GetBestOffsetEncodingFast(u32_offs, offs_count, speed_tradeoff, platforms);
}
uint8 *u8_offs_hi = temp;
uint8 *u8_offs_lo = temp + offs_count;
uint8 *tmp_dst_start = temp + offs_count * 2;
uint8 *tmp_dst_end = temp + offs_count * 4 + 16;
int bits_type1, bits_type0;
EncodeNewOffsets(u32_offs, offs_count, u8_offs_hi, u8_offs_lo, &bits_type1, offs_encode_type, u8_offs, &bits_type0);
uint8 *tmp_dst = tmp_dst_start;
*tmp_dst++ = offs_encode_type + 127;
if (histo_ptr)
memset(&histobuf, 0, sizeof(histobuf));
float cost = kInvalidCost;
int n1 = EncodeArrayU8CompactHeader(tmp_dst, tmp_dst_end, u8_offs_hi, offs_count, opts, speed_tradeoff, platforms, &cost, level,
histo_ptr ? &histobuf : NULL);
if (n1 < 0)
return -1;
tmp_dst += n1;
float cost_lo = 0.0f;
if (offs_encode_type > 1) {
cost_lo = kInvalidCost;
n1 = EncodeArrayU8CompactHeader(tmp_dst, tmp_dst_end, u8_offs_lo, offs_count, opts, speed_tradeoff, platforms, &cost_lo, level, histolo_ptr);
if (n1 < 0)
return -1;
tmp_dst += n1;
}
cost = cost + 1.0f + cost_lo + GetTime_LeviathanOffset(platforms, offs_count, offs_encode_type) * speed_tradeoff;
if (BITSUP(bits_type0) + *cost_ptr <= BITSUP(bits_type1) + cost) {
offs_encode_type = 0;
} else {
*cost_ptr = cost;
n = tmp_dst - tmp_dst_start;
memcpy(dst, tmp_dst_start, n);
memcpy(u8_offs, u8_offs_hi, offs_count);
if (histo_ptr)
*histo_ptr = histobuf;
}
}
*offs_encode_type_ptr = offs_encode_type;
return n;
}
static void EncodeSubModeForBytes(uint8 *dst, const uint8 *src, size_t len, ptrdiff_t recent0) {
for (; len >= 16; len -= 16, src += 16, dst += 16)
_mm_storeu_si128((__m128i *)dst, _mm_sub_epi8(_mm_loadu_si128((const __m128i *)src), _mm_loadu_si128((const __m128i *)&src[-recent0])));
for (; len; len--, src++, dst++)
dst[0] = src[0] - src[-recent0];
}
static void WriteLiteralsInRawMode(const uint8 *src, int src_len, int initial_copy_bytes, uint8 *dst, Levi::TokenArray *tokens) {
for (int i = 0; i < tokens->size; i++) {
int lrl = tokens->data[i].litlen;
if (lrl) {
memcpy(dst, src + tokens->data[i].litoffs, lrl);
dst += lrl;
}
}
int pos = tokens->size ? tokens->data[tokens->size - 1].matchlen + tokens->data[tokens->size - 1].litlen +
tokens->data[tokens->size - 1].litoffs : initial_copy_bytes;
if (src_len - pos > 0)
memcpy(dst, src + pos, src_len - pos);
}
static void WriteLiteralsInSubMode(const uint8 *src, int src_len, int initial_copy_bytes, uint8 *dst, Levi::TokenArray *tokens, int recent0) {
for (int i = 0; i < tokens->size; i++) {
int lrl = tokens->data[i].litlen;
if (lrl) {
EncodeSubModeForBytes(dst, src + tokens->data[i].litoffs, lrl, tokens->data[i].recent_offset0);
dst += lrl;
}
}
int pos = tokens->size ? tokens->data[tokens->size - 1].matchlen + tokens->data[tokens->size - 1].litlen +
tokens->data[tokens->size - 1].litoffs : initial_copy_bytes;
if (src_len - pos > 0)
EncodeSubModeForBytes(dst, src + pos, src_len - pos, recent0);
}
static void WriteLiteralsInSub3Mode(const uint8 *src, int src_len, int initial_copy_bytes, uint8 **dst, Levi::TokenArray *tokens, int recent0) {
for (int i = 0; i < tokens->size; i++) {
int lrl = tokens->data[i].litlen;
if (lrl) {
int pos = tokens->data[i].litoffs, rec = tokens->data[i].recent_offset0;
for (int j = 0; j < lrl; j++, pos++)
*dst[pos & 3]++ = src[pos] - src[pos - rec];
}
}
int pos = tokens->size ? tokens->data[tokens->size - 1].matchlen + tokens->data[tokens->size - 1].litlen +
tokens->data[tokens->size - 1].litoffs : initial_copy_bytes;
for (; pos < src_len; pos++)
*dst[pos & 3]++ = src[pos] - src[pos - recent0];
}
static void WriteLiteralsInSubFMode(const uint8 *src, int src_len, int initial_copy_bytes, uint8 **dst, Levi::TokenArray *tokens, int recent0) {
for (int i = 0; i < tokens->size; i++) {
int lrl = tokens->data[i].litlen;
if (lrl) {
int pos = tokens->data[i].litoffs, rec = tokens->data[i].recent_offset0;
for (int j = 0; j < lrl; j++, pos++)
*dst[pos & 0xf]++ = src[pos] - src[pos - rec];
}
}
int pos = tokens->size ? tokens->data[tokens->size - 1].matchlen + tokens->data[tokens->size - 1].litlen +
tokens->data[tokens->size - 1].litoffs : initial_copy_bytes;
for (; pos < src_len; pos++)
*dst[pos & 0xf]++ = src[pos] - src[pos - recent0];
}
static void WriteLiteralsInLamsubMode(const uint8 *src, int src_len, int initial_copy_bytes, uint8 **dst, Levi::TokenArray *tokens, int recent0) {
for (int i = 0; i < tokens->size; i++) {
int lrl = tokens->data[i].litlen;
if (lrl) {
int pos = tokens->data[i].litoffs, rec = tokens->data[i].recent_offset0;
*dst[1]++ = src[pos] - src[pos - rec];
pos++;
for (int j = 1; j < lrl; j++, pos++)
*dst[0]++ = src[pos] - src[pos - rec];
}
}
int pos = tokens->size ? tokens->data[tokens->size - 1].matchlen + tokens->data[tokens->size - 1].litlen +
tokens->data[tokens->size - 1].litoffs : initial_copy_bytes;
int num = src_len - pos;
if (num > 0) {
*dst[1]++ = src[pos] - src[pos - recent0];
pos++;
for (; --num; pos++)
*dst[0]++ = src[pos] - src[pos - recent0];
}
}
static void WriteLiteralsInO1Mode(const uint8 *src, int src_len, int initial_copy_bytes, uint8 **array_data, Levi::TokenArray *tokens) {
for (int i = 0; i < tokens->size; i++) {
int lrl = tokens->data[i].litlen;
if (lrl) {
const uint8 *p = src + tokens->data[i].litoffs;
for (int j = 0; j < lrl; j++, p++)
*array_data[p[-1] >> 4]++ = p[0];
}
}
int src_cur = tokens->size ? tokens->data[tokens->size - 1].matchlen + tokens->data[tokens->size - 1].litlen +
tokens->data[tokens->size - 1].litoffs : initial_copy_bytes;
const uint8 *p = src + src_cur;
for (; src_cur < src_len; src_cur++, p++)
*array_data[p[-1] >> 4]++ = p[0];
}
int WriteLzOffsetBits(uint8 *dst, uint8 *dst_end, uint8 *u8_offs, uint32 *u32_offs, int offs_count, int offs_encode_type, uint32 *u32_len, int u32_len_count, int flag_ignore_u32_length, size_t a10) {
if (dst_end - dst <= 16)
return -1;
BitWriter64<1> f(dst);
BitWriter64<-1> b(dst_end);
if (!flag_ignore_u32_length) {
int nb = BSR(u32_len_count + 1);
b.Write(1, nb + 1);
if (nb)
b.Write(u32_len_count + 1 - (1 << nb), nb);
}
if (offs_encode_type) {
for (int i = 0; i < offs_count; i++) {
if (b.ptr_ - f.ptr_ <= 8)
return -1;
uint nb = u8_offs[i] >> 3;
uint bits = ((1 << nb) - 1) & (u32_offs[i] / offs_encode_type + 8);
if (i & 1)
b.Write(bits, nb);
else
f.Write(bits, nb);
}
} else {
for (int i = 0; i < offs_count; i++) {
if (b.ptr_ - f.ptr_ <= 8)
return -1;
uint nb, bits = u32_offs[i];
if (u8_offs[i] < 0xf0) {
nb = (u8_offs[i] >> 4) + 4;
bits = ((bits + 248) >> 4) - (1 << nb);
} else {
nb = u8_offs[i] - 0xe0;
bits = bits - (1 << nb) - 8322816;
}
if (i & 1)
b.Write(bits, nb);
else
f.Write(bits, nb);
}
}
if (!flag_ignore_u32_length) {
for (int i = 0; i < u32_len_count; i++) {
if (b.ptr_ - f.ptr_ <= 8)
return -1;
uint32 len = u32_len[i];
int nb = BSR((len >> 6) + 1);
if (i & 1) {
b.Write(1, nb + 1);
if (nb)
b.Write((len >> 6) + 1 - (1 << nb), nb);
b.Write(len & 0x3f, 6);
} else {
f.Write(1, nb + 1);
if (nb)
f.Write((len >> 6) + 1 - (1 << nb), nb);
f.Write(len & 0x3f, 6);
}
}
}
uint8 *fp = f.GetFinalPtr();
uint8 *bp = b.GetFinalPtr();
if (bp - fp <= 8)
return -1;
memmove(fp, bp, dst_end - bp);
return dst_end - bp + fp - dst;
}
static inline uint8 PackOffset(unsigned int offs) {
if (offs >= 8388360) {
return BSR(offs - 8322816) | 0xF0;
} else {
return ((offs - 8) & 0xF) | 16 * (BSR(offs + 248) - 8);
}
}
static int Leviathan_EncodeLzArrays(LzTemp *lz_temp, float *cost_ptr, Levi::LitStats *litstats, int *chunk_type_ptr,
const uint8 *src, int src_len, uint8 *dst, uint8 *dst_end,
int unused_start_pos, LzCoder *lzcoder, int recent_offs0, Levi::TokenArray *tokens,
int initial_bytes, Levi::Stats *arrhisto, int min_offset) {
uint8 *dst_org = dst;
int litlen_total = litstats->total_lits;
int num_tokens = tokens->size;
float speed_tradeoff = lzcoder->speed_tradeoff;
int max_len = std::min(src_len / 5, 2 * num_tokens);
int max_offs = std::min(src_len / 3, num_tokens);
int opts = lzcoder->entropy_opts;
int space_usage = 5 * (max_offs + max_len) + 32;
int level = lzcoder->compression_level;
int platforms = lzcoder->platforms;
int scratch_size = (src_len >> 4) + src_len + 256;
uint8 *scratchx = (uint8*)lz_temp->scratch2.Allocate(scratch_size);
uint8 *scratchx_end = scratchx + scratch_size;
uint8 *temp = new uint8[space_usage];
std::auto_ptr<uint8> temp_deleter(temp);
for (int i = 0; i < initial_bytes; i++)
*dst++ = src[i];
// Distribute memory
uint8 *u8_offs = temp;
uint8 *u8_lrl = temp + max_offs, *u8_lrl_cur = u8_lrl;
uint8 *u8_ml = temp + max_len + max_offs, *u8_ml_cur = u8_ml;
uint32 *u32_offs = (uint32*)((uintptr_t)(u8_ml + 3) & ~3);
uint32 *u32_lrl = u32_offs + max_offs, *u32_lrl_cur = u32_lrl;
uint32 *u32_ml = u32_lrl + max_len, *u32_ml_cur = u32_ml;
int offs_count = 0;
for (int i = 0; i < num_tokens; i++) {
int offs = tokens->data[i].offset;
if (offs > 0) {
u32_offs[offs_count] = offs;
if (min_offset != 1)
u8_offs[offs_count] = PackOffset(offs);
offs_count++;
}
int lrl = tokens->data[i].litlen - 3;
int ml = tokens->data[i].matchlen - 9;
if (lrl >= 0) {
*u8_lrl_cur++ = std::min(lrl, 255);
if (lrl >= 255)
*u32_lrl_cur++ = lrl - 255;
}
if (ml >= 0) {
*--u8_ml_cur = std::min(ml, 255);
if (ml >= 255)
*--u32_ml_cur = ml - 255;
}
}
int u8_lrl_total = u8_lrl_cur - u8_lrl, u8_ml_total = u8_ml - u8_ml_cur;
int u32_len_total = (u32_ml - u32_ml_cur) + (u32_lrl_cur - u32_lrl);
int u8_len_total = u8_lrl_total + u8_ml_total;
memcpy(u8_lrl_cur, u8_ml_cur, u8_ml_total);
memcpy(u32_lrl_cur, u32_ml_cur, sizeof(uint32) * (u32_ml - u32_ml_cur));
if (arrhisto) {
CountBytesHistoU8(u8_lrl, u8_lrl_total, &arrhisto->litlen_histo);
CountBytesHistoU8(u8_lrl_cur, u8_ml_total, &arrhisto->matchlen_histo);
}
int offs_encode_type = 0;
float cost_offsets = kInvalidCost;
int n = EncodeLzOffsets(dst, dst_end, u8_offs, u32_offs, offs_count,
opts, speed_tradeoff, platforms,
&cost_offsets, min_offset, true, &offs_encode_type, level,
arrhisto ? &arrhisto->dist_histo : NULL,
arrhisto ? &arrhisto->distlo_histo : NULL);
if (n < 0)
return -1;
dst += n;
if (arrhisto)
arrhisto->offs_encode_type = offs_encode_type;
float cost_u8_len = kInvalidCost;
n = EncodeArrayU8_MaybeConcat(dst, dst_end, u8_lrl, u8_len_total, opts, speed_tradeoff,
platforms, &cost_u8_len, level, NULL, u8_lrl_total);
if (n < 0)
return -1;
dst += n;
cost_u8_len += CombineCostComponents(platforms,
u8_len_total * 0.478f + 42.933f + u32_len_total * 21.527f,
u8_len_total * 0.746f + 36.646f + u32_len_total * 32.345f,
u8_len_total * 0.815f + 115.731f + u32_len_total * 36.682f,
u8_len_total * 0.453f + 48.796f + u32_len_total * 20.770f) * speed_tradeoff;
int scratch_required = u8_len_total + num_tokens + 4 * u8_len_total + 6 * offs_count + litlen_total + 32 + 4096 +
std::max(std::max(std::max(num_tokens, litlen_total), std::max(u8_len_total, offs_count)) + 0xc000, src_len);
int scratch_available = std::min(3 * src_len + 32 + 0xd000, 0x6C000);
assert(scratch_required <= scratch_available);
float adv_cost = kInvalidCost;
if (litlen_total < 32) {
*chunk_type_ptr = 1;
if (litlen_total > 0)
WriteLiteralsInRawMode(src, src_len, initial_bytes, scratchx, tokens);
int n = EncodeArrayU8(dst, dst_end, scratchx, litlen_total, opts, speed_tradeoff, platforms, &adv_cost, level, 0);
if (n < 0)
return -1;
dst += n;
} else {
HistoU8 xhisto;
for (size_t i = 0; i != 256; i++)
xhisto.count[i] = litstats->litsub_histo.count[i] + litstats->litsub_histo_2nd.count[i];
if (arrhisto) {
if (litstats != &arrhisto->lit)
arrhisto->lit = *litstats;
arrhisto->xhisto = xhisto;
}
float multi_cost = CombineCostComponents(platforms, 3283.811f, 5371.269f, 5615.7461f, 3112.6179f) * speed_tradeoff;
float raw_cost = ((GetHistoCostApprox(litstats->lit_histo, GetHistoSum(litstats->lit_histo)) + 7) >> 3) + multi_cost;
float litsub_cost = ((GetHistoCostApprox(xhisto, GetHistoSum(xhisto)) + 7) >> 3) + multi_cost;
float lamsub_cost = ((GetHistoCostApprox(litstats->litsub_histo, GetHistoSum(litstats->litsub_histo)) + 7) >> 3) +
((GetHistoCostApprox(litstats->litsub_histo_2nd, GetHistoSum(litstats->litsub_histo_2nd)) + 7) >> 3) +
multi_cost * 2;
int litsub3_bytes = 0;
for (size_t i = 0; i != 4; i++)
litsub3_bytes += (GetHistoCostApprox(litstats->litsub3_histo[i], GetHistoSum(litstats->litsub3_histo[i])) + 7) >> 3;
float litsub3_cost = litsub3_bytes + multi_cost * 4;
int litsubf_bytes = 0;
for (size_t i = 0; i != 16; i++)
litsubf_bytes += (GetHistoCostApprox(litstats->litsubf_histo[i], GetHistoSum(litstats->litsubf_histo[i])) + 7) >> 3;
float litsubf_cost = litsubf_bytes + multi_cost * 16;
int o1_bytes = 0;
for (size_t i = 0; i != 16; i++)
o1_bytes += (GetHistoCostApprox(litstats->o1_histo[i], GetHistoSum(litstats->o1_histo[i])) + 7) >> 3;
float o1_cost = o1_bytes + multi_cost * 16;
float litsub_time_cost = CombineCostComponents1(platforms, litlen_total, 0.131f, 0.184f, 0.186f, 0.105f) * speed_tradeoff;
float lamsub_time_cost = CombineCostComponents1(platforms, litlen_total, 0.554f, 0.874f, 1.149f, 0.606f) * speed_tradeoff;
float litsub3_time_cost = CombineCostComponents1(platforms, litlen_total, 1.568f, 2.388f, 2.626f, 1.629f) * speed_tradeoff;
float litsubf_time_cost = CombineCostComponents1(platforms, litlen_total, 3.946f, 5.858f, 6.167f, 3.795f) * speed_tradeoff;
float o1_time_cost = CombineCostComponents1(platforms, litlen_total, 8.869f, 10.141f, 11.111f, 8.053f) * speed_tradeoff;
litsub_cost += litsub_time_cost;
lamsub_cost += lamsub_time_cost;
litsub3_cost += litsub3_time_cost;
litsubf_cost += litsubf_time_cost;