This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paq8pxv.cpp
5196 lines (4787 loc) · 181 KB
/
paq8pxv.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
/* paq8pxv file compressor/archiver. Release by Kaido Orav
Copyright (C) 2008-2021 Matt Mahoney, Serge Osnach, Alexander Ratushnyak,
Bill Pettis, Przemyslaw Skibinski, Matthew Fite, wowtiger, Andrew Paterson,
Jan Ondrus, Andreas Morphis, Pavel L. Holoborodko, Kaido Orav, Simon Berger,
Neill Corlett
LICENSE
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details at
Visit <http://www.gnu.org/copyleft/gpl.html>.
To install and use in Windows:
- To install, put paq8pxv.exe or a shortcut to it on your desktop.
- To compress a file or folder, drop it on the paq8pxv icon.
- To decompress, drop a .paq8pxv file on the icon.
A .paq8pxv extension is added for compression, removed for decompression.
The output will go in the same folder as the input.
While paq8pxv is working, a command window will appear and report
progress. When it is done you can close the window by pressing
ENTER or clicking [X].
COMMAND LINE INTERFACE
- To install, put paq8pxv.exe somewhere in your PATH.
- To compress: paq8pxv [-N] file1 [file2...]
- To decompress: paq8pxv [-d] file1.paq8pxv [dir2]
- To view contents: paq8pxv -l file1.paq8pxv
The compressed output file is named by adding ".paq8pxv" extension to
the first named file (file1.paq8pxv). Each file that exists will be
added to the archive and its name will be stored without a path.
The option -N specifies a compression level ranging from -0
(fastest) to -8 (smallest). The default is -5. If there is
no option and only one file, then the program will pause when
finished until you press the ENTER key (to support drag and drop).
If file1.paq8pxv exists then it is overwritten. Level -0 only
transforms or decompresses data.
If the first named file ends in ".paq8pxv" then it is assumed to be
an archive and the files within are extracted to the same directory
as the archive unless a different directory (dir2) is specified.
The -d option forces extraction even if there is not a ".paq8pxv"
extension. If any output file already exists, then it is compared
with the archive content and the first byte that differs is reported.
No files are overwritten or deleted. If there is only one argument
(no -d or dir2) then the program will pause when finished until
you press ENTER.
For compression, if any named file is actually a directory, then all
files and subdirectories are compressed, preserving the directory
structure, except that empty directories are not stored, and file
attributes (timestamps, permissions, etc.) are not preserved.
During extraction, directories are created as needed. For example:
paq8pxv -1 c:\tmp\foo bar
compresses foo and bar (if they exist) to c:\tmp\foo.paq8pxv
paq8pxv -d c:\tmp\foo.paq8pxv
extracts foo and compares bar in the current directory. If foo and bar
are directories then their contents are extracted/compared.
There are no commands to update an existing archive or to extract
part of an archive. Files and archives larger than 2GB are not
supported (but might work on 64-bit machines, not tested).
File names with nonprintable characters are not supported (spaces
are OK).
TO COMPILE
There are 2 files: paq8pxv.cpp (C++) and wrtpre.cpp (C++).
paq8pxv.cpp recognizes the following compiler options:
-DWINDOWS (to compile in Windows)
-DUNIX (to compile in Unix, Linux, etc)
-DMT (to compile with multithreading support)
If you compile without -DWINDOWS or -DUNIX, you can still compress files,
but you cannot compress directories or create them during extraction.
You can extract directories if you manually create the empty directories
first.
Recommended compiler commands and optimizations:
MINGW g++ (x86,x64):
with multithreading:
g++ paq8pxv.cpp -DWINDOWS -DMT -msse2 -O2 -s -static -o paq8pxv.exe
without multithreading:
g++ paq8pxv.cpp -DWINDOWS -msse2 -O2 -s -static -o paq8pxv.exe
UNIX/Linux (PC x86,x64):
with multithreading:
g++ paq8pxv.cpp -DUNIX -DMT -msse2 -O2 -s -static -lpthread -o paq8pxv
without multithreading:
g++ paq8pxv.cpp -DUNIX -msse2 -O2 -s -static -lpthread -o paq8pxv
Non PC (e.g. PowerPC under MacOS X)
g++ paq8pxv.cpp -O2 -DUNIX -s -o paq8pxv
ARCHIVE FILE FORMAT
An archive has the following format.
paq8pxv -N
segment offset 8 bytes
segment size 2 bytes
compressed segment size 2 bytes
streams (0b00000xxx xxxxxxxx) 2 bytes
\0 file list size
compressed file list(
size TAB filename CR LF
size TAB filename CR LF
...)
compressed binary data
file segmentation data
stream data sizes[11]
-N is the option (-0 to -1) and mode, even if a default was used.
00LMNNNN bit M is set if fast mode,
bit L is set if quick mode,
if L or M are not set default to slow mode.
segment size is total size of file(s)
compressed segment size is compressed segmentation data in bytes
at segmnet offset after compressed binary data.
file segmentation data is full list of detected blocks:
type size info
type size info
type size
type size info
.....
info is present if block type needs extra info like in image or audio.
streams - if bit is set then stream is present. Right to left order
for stream 10 to 0. If bit is set store stream lengt to archive.
Plain file names are stored without a path. Files in compressed
directories are stored with path relative to the compressed directory
(using UNIX style forward slashes "/"). For example, given these files:
123 C:\dir1\file1.txt
456 C:\dir2\file2.txt
Then
paq8pxv archive \dir1\file1.txt \dir2
will create archive.paq8pxv
The command:
paq8pxv archive.paq8pxv C:\dir3
will create the files:
C:\dir3\file1.txt
C:\dir3\dir2\file2.txt
Decompression will fail if the first 10 bytes are not "paq8pxv -". Sizes
are stored as decimal numbers. CR, LF, TAB are ASCII codes
13, 10, 9 respectively.
ARITHMETIC CODING
The binary data is arithmetic coded as the shortest base 256 fixed point
number x = SUM_i x_i 256^-1-i such that p(<y) <= x < p(<=y), where y is the
input string, x_i is the i'th coded byte, p(<y) (and p(<=y)) means the
probability that a string is lexicographcally less than (less than
or equal to) y according to the model, _ denotes subscript, and ^ denotes
exponentiation.
The model p(y) for y is a conditional bit stream,
p(y) = PROD_j p(y_j | y_0..j-1) where y_0..j-1 denotes the first j
bits of y, and y_j is the next bit. Compression depends almost entirely
on the ability to predict the next bit accurately.
MODEL MIXING
paq8pxv uses a neural network to combine a large number of models. The
i'th model independently predicts
p1_i = p(y_j = 1 | y_0..j-1), p0_i = 1 - p1_i.
The network computes the next bit probabilty
p1 = squash(SUM_i w_i t_i), p0 = 1 - p1 (1)
where t_i = stretch(p1_i) is the i'th input, p1_i is the prediction of
the i'th model, p1 is the output prediction, stretch(p) = ln(p/(1-p)),
and squash(s) = 1/(1+exp(-s)). Note that squash() and stretch() are
inverses of each other.
After bit y_j (0 or 1) is received, the network is trained:
w_i := w_i + eta t_i (y_j - p1) (2)
where eta is an ad-hoc learning rate, t_i is the i'th input, (y_j - p1)
is the prediction error for the j'th input but, and w_i is the i'th
weight. Note that this differs from back propagation:
w_i := w_i + eta t_i (y_j - p1) p0 p1 (3)
which is a gradient descent in weight space to minimize root mean square
error. Rather, the goal in compression is to minimize coding cost,
which is -log(p0) if y = 1 or -log(p1) if y = 0. Taking
the partial derivative of cost with respect to w_i yields (2).
MODELS
Most models are context models. A function of the context (last few
bytes) is mapped by a lookup table or hash table to a state which depends
on the bit history (prior sequence of 0 and 1 bits seen in this context).
The bit history is then mapped to p1_i by a fixed or adaptive function.
There are several types of bit history states:
- Run Map. The state is (b,n) where b is the last bit seen (0 or 1) and
n is the number of consecutive times this value was seen. The initial
state is (0,0). The output is computed directly:
t_i = (2b - 1)K log(n + 1).
where K is ad-hoc, around 4 to 10. When bit y_j is seen, the state
is updated:
(b,n) := (b,n+1) if y_j = b, else (y_j,1).
- Stationary Map. The state is p, initially 1/2. The output is
t_i = stretch(p). The state is updated at ad-hoc rate K (around 0.01):
p := p + K(y_j - p)
- Nonstationary Map. This is a compromise between a stationary map, which
assumes uniform statistics, and a run map, which adapts quickly by
discarding old statistics. An 8 bit state represents (n0,n1,h), initially
(0,0,0) where:
n0 is the number of 0 bits seen "recently".
n1 is the number of 1 bits seen "recently".
n = n0 + n1.
h is the full bit history for 0 <= n <= 4,
the last bit seen (0 or 1) if 5 <= n <= 15,
0 for n >= 16.
The primaty output is t_i := stretch(sm(n0,n1,h)), where sm(.) is
a stationary map with K = 1/256, initialized to
sm(n0,n1,h) = (n1+(1/64))/(n+2/64). Four additional inputs are also
be computed to improve compression slightly:
p1_i = sm(n0,n1,h)
p0_i = 1 - p1_i
t_i := stretch(p_1)
t_i+1 := K1 (p1_i - p0_i)
t_i+2 := K2 stretch(p1) if n0 = 0, -K2 stretch(p1) if n1 = 0, else 0
t_i+3 := K3 (-p0_i if n1 = 0, p1_i if n0 = 0, else 0)
t_i+4 := K3 (-p0_i if n0 = 0, p1_i if n1 = 0, else 0)
where K1..K4 are ad-hoc constants.
h is updated as follows:
If n < 4, append y_j to h.
Else if n <= 16, set h := y_j.
Else h = 0.
The update rule is biased toward newer data in a way that allows
n0 or n1, but not both, to grow large by discarding counts of the
opposite bit. Large counts are incremented probabilistically.
Specifically, when y_j = 0 then the update rule is:
n0 := n0 + 1, n < 29
n0 + 1 with probability 2^(27-n0)/2 else n0, 29 <= n0 < 41
n0, n = 41.
n1 := n1, n1 <= 5
round(8/3 lg n1), if n1 > 5
swapping (n0,n1) when y_j = 1.
Furthermore, to allow an 8 bit representation for (n0,n1,h), states
exceeding the following values of n0 or n1 are replaced with the
state with the closest ratio n0:n1 obtained by decrementing the
smaller count: (41,0,h), (40,1,h), (12,2,h), (5,3,h), (4,4,h),
(3,5,h), (2,12,h), (1,40,h), (0,41,h). For example:
(12,2,1) 0-> (7,1,0) because there is no state (13,2,0).
- Match Model. The state is (c,b), initially (0,0), where c is 1 if
the context was previously seen, else 0, and b is the next bit in
this context. The prediction is:
t_i := (2b - 1)Kc log(m + 1)
where m is the length of the context. The update rule is c := 1,
b := y_j. A match model can be implemented efficiently by storing
input in a buffer and storing pointers into the buffer into a hash
table indexed by context. Then c is indicated by a hash table entry
and b can be retrieved from the buffer.
CONTEXTS
High compression is achieved by combining a large number of contexts.
Most (not all) contexts start on a byte boundary and end on the bit
immediately preceding the predicted bit. The contexts below are
modeled with both a run map and a nonstationary map unless indicated.
ARCHITECTURE
The context models are mixed by several of several hundred neural networks
selected by a low-order context. The outputs of these networks are
combined using a second neural network, then fed through several stages of
adaptive probability maps (APM) before arithmetic coding.
For images, only one neural network is used and its context is fixed.
An APM is a stationary map combining a context and an input probability.
The input probability is stretched and divided into 32 segments to
combine with other contexts. The output is interpolated between two
adjacent quantized values of stretch(p1).
PREPROCESSING
paq8pxv uses preprocessing transforms on certain data types to improve
compression. To improve reliability, the decoding transform is
tested during compression to ensure that the input file can be
restored. If the decoder output is not identical to the input file
due to a bug, then the transform is abandoned and the data is compressed
without a transform so that it will still decompress correctly.
The input is split into blocks with the format <type> <decoded size> <info>
where <type> is 1 byte (0 = no transform), <decoded size> is the size
of the data after decoding, which may be different than the size of <data>.
Data is stored uncompressed after compressed data ends.
The preprocessor has 3 parts:
- Detector. Splits the input into smaller blocks depending on data type.
- Coder. Input is a block to be compressed. Output is a temporary
file. The coder determines whether a transform is to be applied
based on file type, and if so, which one. A coder may use lots
of resources (memory, time) and make multiple passes through the
input file. The file type is stored (as one byte) during compression.
- Decoder. Performs the inverse transform of the coder. It uses few
resorces (fast, low memory) and runs in a single pass (stream oriented).
It takes input either from a file or the arithmetic decoder. Each call
to the decoder returns a single decoded byte.
IMPLEMENTATION
Hash tables are designed to minimize cache misses, which consume most
of the CPU time.
Most of the memory is used by the nonstationary context models.
Contexts are represented by 32 bits, possibly a hash. These are
mapped to a bit history, represented by 1 byte. The hash table is
organized into 64-byte buckets on cache line boundaries. Each bucket
contains 7 x 7 bit histories, 7 16-bit checksums, and a 2 element LRU
queue packed into one byte. Each 7 byte element represents 7 histories
for a context ending on a 3-bit boundary plus 0-2 more bits. One
element (for bits 0-1, which have 4 unused bytes) also contains a run model
consisting of the last byte seen and a count (as 1 byte each).
Run models use 4 byte hash elements consisting of a 2 byte checksum, a
repeat count (0-255) and the byte value. The count also serves as
a priority.
Stationary models are most appropriate for small contexts, so the
context is used as a direct table lookup without hashing.
The inner loops of the neural network prediction (1) and training (2)
algorithms are implemented in SIMD assembler, which computes 4 or more
elements at a time.
*/
#define VERSION "17"
#define PROGNAME "paq8pxv" VERSION // Please change this if you change the program.
#define MT // uncomment for multithreading, compression only
#define ERRMSG // uncomment to show error messages if programm quits
#define VMMSG // prints vm error messages and x86 asm to console
#ifdef WINDOWS
#ifdef MT
//#define PTHREAD //uncomment to force pthread to igore windows native threads
#endif
#endif
#ifdef UNIX
#ifdef MT
#define PTHREAD 1
#endif
#endif
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#define NDEBUG // remove for debugging (turns on Array bound checks)
#include <assert.h>
#ifdef MT
#include <vector>
#endif
#ifdef UNIX
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <memory.h>
#include <cstdio>
#include <ctype.h>
#include <sys/cdefs.h>
#include <dirent.h>
#include <errno.h>
#endif
#ifdef WINDOWS
#include <windows.h>
#endif
#include <stdint.h>
#ifdef _MSC_VER
//
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
// 8, 16, 32 bit unsigned types (adjust as appropriate)
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int U32;
typedef uint64_t U64;
typedef signed char int8_t;
// min, max functions
#if !defined(WINDOWS) || !defined (min)
inline int min(int a, int b) {return a<b?a:b;}
inline int max(int a, int b) {return a<b?b:a;}
#endif
/*
#if defined(WINDOWS) || defined(_MSC_VER)
#define atoll(S) _atoi64(S)
#endif
#ifdef _MSC_VER
#define fseeko(a,b,c) _fseeki64(a,b,c)
#define ftello(a) _ftelli64(a)
#else
#ifndef UNIX
#ifndef fseeko
#define fseeko(a,b,c) fseeko64(a,b,c)
#endif
#ifndef ftello
#define ftello(a) ftello64(a)
#endif
#endif
#endif
*/
#ifdef MT
#ifdef PTHREAD
#include "pthread.h"
#endif
#endif
#define ispowerof2(x) ((x&(x-1))==0)
#include <math.h>
// Error handler: print message if any, and exit
void quit(const char* message=0) {
#ifdef ERRMSG
printf("%s",message);
#endif
exit(1);
}
// strings are equal ignoring case?
int equals(const char* a, const char* b) {
assert(a && b);
while (*a && *b) {
int c1=*a;
if (c1>='A'&&c1<='Z') c1+='a'-'A';
int c2=*b;
if (c2>='A'&&c2<='Z') c2+='a'-'A';
if (c1!=c2) return 0;
++a;
++b;
}
return *a==*b;
}
//////////////////////////// Array ////////////////////////////
// Array<T,Align> a(n); allocates memory for n elements of T.
// The base address is aligned if the "alignment" parameter is given.
// Constructors for T are not called, the allocated memory is initialized to 0s.
// It's the caller's responsibility to populate the array with elements.
// Parameters are checked and indexing is bounds checked if assertions are on.
// Use of copy and assignment constructors are not supported.
//
// a.size(): returns the number of T elements currently in the array.
// a.resize(newsize): grows or shrinks the array.
// a.append(x): appends x to the end of the array and reserving space for more elements if needed.
// a.pop_back(): removes the last element by reducing the size by one (but does not free memory).
#ifndef NDEBUG
static void chkindex(U32 index, U32 upper_bound) {
if (index>=upper_bound) {
fprintf(stderr, "out of upper bound %d\n",index);
quit();
}
}
#endif
template <class T, const int Align=16> class Array {
private:
U64 used_size;
U64 reserved_size;
char *ptr; // Address of allocated memory (may not be aligned)
T* data; // Aligned base address of the elements, (ptr <= T)
void create(U64 requested_size);
inline U64 padding() const {return Align-1;}
inline U64 allocated_bytes() const {return (reserved_size==0)?0:reserved_size*sizeof(T)+padding();}
public:
explicit Array(U64 requested_size) {create(requested_size);}
~Array();
T& operator[](U64 i) {
#ifndef NDEBUG
chkindex(U32(i),U32(used_size));
#endif
return data[U32(i)];
}
const T& operator[](U64 i) const {
#ifndef NDEBUG
chkindex(U32(i),U32(used_size));
#endif
return data[U32(i)];
}
U64 size() const {return used_size;}
int size32() const {return int(used_size);}
void resize(U64 new_size);
void pop_back() {assert(used_size>0); --used_size; } // decrement size
void push_back(const T& x); // increment size, append x
Array(const Array&) { assert(false); } //prevent copying - this method must be public (gcc must see it but actually won't use it)
private:
Array& operator=(const Array&); //prevent assignment
};
template<class T, const int Align> void Array<T,Align>::create(U64 requested_size) {
assert((Align&(Align-1))==0);
used_size=reserved_size=requested_size;
if (requested_size==0) {
data=0;ptr=0;
return;
}
U64 bytes_to_allocate=allocated_bytes();
ptr=(char*)calloc(bytes_to_allocate,1);
if(!ptr){
printf("Requested size %d b.\n",(U32)(bytes_to_allocate));
#ifdef MT
printf("Try using less memory in your cfg file or reduce thread count.\n");
#endif
quit("Out of memory.");
}
U64 pad=padding();
data=(T*)(((uintptr_t)ptr+pad) & ~(uintptr_t)pad);
assert(ptr<=(char*)data && (char*)data<=ptr+Align);
assert(((uintptr_t)data & (Align-1))==0); //aligned as expected?
}
template<class T, const int Align> void Array<T,Align>::resize(U64 new_size) {
if (new_size<=reserved_size) {
used_size=new_size;
return;
}
char *old_ptr=ptr;
T *old_data=data;
U64 old_size=used_size;
create(new_size);
if(old_size>0) {
assert(old_ptr && old_data);
memcpy(data, old_data, sizeof(T)*old_size);
}
if(old_ptr){free(old_ptr);old_ptr=0;}
}
template<class T, const int Align> void Array<T,Align>::push_back(const T& x) {
if(used_size==reserved_size) {
U64 old_size=used_size;
U64 new_size=used_size*2+16;
resize(new_size);
used_size=old_size;
}
data[used_size++]=x;
}
template<class T, const int Align> Array<T, Align>::~Array() {
free(ptr);
used_size=reserved_size=0;
data=0;ptr=0;
}
template <class T> void alloc(T*&ptr, int c) {
ptr=(T*)calloc(c, sizeof(T));
if (!ptr) quit("Out of memory.\n");
}
template <class T> void alloc1(T*&data, int c,T*&ptr,const int align=16) {
ptr=(T*)calloc(c, sizeof(T));
if (!ptr) quit("Out of memory.\n");
data=(T*)(((uintptr_t)ptr+(align-1)) & ~(uintptr_t)(align-1));
}
/////////////////////////// String /////////////////////////////
// A tiny subset of std::string
// size() includes NUL terminator.
class String: public Array<char> {
public:
const char* c_str() const {return &(*this)[0];}
void operator=(const char* s) {
resize(strlen(s)+1);
strcpy(&(*this)[0], s);
}
void operator+=(const char* s) {
assert(s);
pop_back();
while (*s) push_back(*s++);
push_back(0);
}
String(const char* s=""): Array<char>(1) {
(*this)+=s;
}
};
/////////////////////////// File /////////////////////////////
// The main purpose of these classes is to keep temporary files in
// RAM as mush as possible. The default behaviour is to simply pass
// function calls to the operating system - except in case of temporary
// files.
// Helper function: create a temporary file
//
// On Windows when using tmpfile() the temporary file may be created
// in the root directory causing access denied error when User Account Control (UAC) is on.
// To avoid this issue with tmpfile() we simply use fopen() instead.
// We create the temporary file in the directory where the executable is launched from.
// Luckily the MS C runtime library provides two (MS specific) fopen() flags: "T"emporary and "D"elete.
FILE* tmpfile2(void){
FILE *f;
#if defined(WINDOWS)
int i;
char temppath[MAX_PATH];
char filename[MAX_PATH];
//i=GetTempPath(MAX_PATH,temppath); //store temp file in system temp path
i=GetModuleFileName(NULL,temppath,MAX_PATH); //store temp file in program folder
if ((i==0) || (i>MAX_PATH)) return NULL;
char *p=strrchr(temppath, '\\');
if (p==0) return NULL;
p++;*p=0;
if (GetTempFileName(temppath,"tmp",0,filename)==0) return NULL;
f=fopen(filename,"w+bTD");
if (f==NULL) unlink(filename);
return f;
#else
f=tmpfile(); // temporary file
if (!f) return NULL;
return f;
#endif
}
//////////////////////////// rnd ///////////////////////////////
// 32-bit pseudo random number generator
class Random{
U32 table[64];
int i;
public:
Random() {
table[0]=123456789;
table[1]=987654321;
for (int j=0; j<62; j++) table[j+2]=table[j+1]*11+table[j]*23/16;
i=0;
}
U32 operator()() {
return ++i, table[i&63]=table[(i-24)&63]^table[(i-55)&63];
}
} ;
// Buffer for file segment info
// type size info(if not -1)
class Segment {
Array<U8> b;
public:
U32 pos; //size of buffer
U64 hpos; //header pos points to segment info at archive end
//int count; //count of segments
Segment(int i=0): b(i),pos(0),hpos(0) {}
void setsize(int i) {
if (!i) return;
assert(i>0);
b.resize(i);
}
U8& operator[](U32 i) {
if (i>=b.size()) setsize(i+1);
return b[i];
}
U8 operator()(U32 i) const {
assert(i>=0);
assert(i<=b.size());
return b[i];
}
// put 8 bytes to segment buffer
void put8(U64 num){
if ((pos+8)>=b.size()) setsize(pos+8);
b[pos++]=(num>>56)&0xff;
b[pos++]=(num>>48)&0xff;
b[pos++]=(num>>40)&0xff;
b[pos++]=(num>>32)&0xff;
b[pos++]=(num>>24)&0xff;
b[pos++]=(num>>16)&0xff;
b[pos++]=(num>>8)&0xff;
b[pos++]=num&0xff;
}
void put4(U32 num){
if ((pos+4)>=b.size()) setsize(pos+4);
b[pos++]=(num>>24)&0xff;
b[pos++]=(num>>16)&0xff;
b[pos++]=(num>>8)&0xff;
b[pos++]=num&0xff;
}
void put1(U8 num){
if (pos>=b.size()) setsize(pos+1);
b[pos++]=num;
}
int size() const {
return b.size();
}
};
/////////////////////// Global context /////////////////////////
U8 level=1; // Compression level 0 no compression
// level 1 compression
int defaultType;
Segment segment; //for file segments type size info(if not -1)
int streamCount;
FILE **filestreams;
bool doFullOpt=false;
bool doBounds=false;
bool doBoundsRun=false;
bool doDebugInfo=false;
// Contain all global data usable between models
class BlockData {
public:
int y; // Last bit, 0 or 1, set by encoder
int c0; // Last 0-7 bits of the partial byte with a leading 1 bit (1-255)
U32 c4;//,c8; // Last 4,4 whole bytes, packed. Last byte is bits 0-7.
int bpos; // bits in c0 (0 to 7)
int blpos; // Relative position in block
int filetype;
int finfo;
int bposshift;
int c0shift_bpos;
struct Inputs{
int ncount; // mixer input count
Array<short,32> n; // input array
void add(int p){ n[ncount++]=p; }
} ;
Array<Inputs> mxInputs; // array of inputs
int cInputs;
BlockData():y(0), c0(1), c4(0),bpos(0),blpos(0),filetype(defaultType),finfo(-1),bposshift(0),c0shift_bpos(0),mxInputs(0),cInputs(-1) {
}
~BlockData(){ }
};
///////////////////////////// ilog //////////////////////////////
// ilog(x) = round(log2(x) * 16), 0 <= x < 256
class Ilog {
U8 t[256];
public:
int operator()(U16 x) const {return t[x];}
Ilog();
} ilog;
// Compute lookup table by numerical integration of 1/x
Ilog::Ilog() {
U32 x=14155776;
for (int i=2; i<257; ++i) {
x+=774541002/(i*2-1); // numerator is 2^29/ln 2
t[i-1]=x>>24;
}
}
///////////////////////// state table ////////////////////////
// State table:
// nex(state, 0) = next state if bit y is 0, 0 <= state < 256
// nex(state, 1) = next state if bit y is 1
// nex(state, 2) = number of zeros in bit history represented by state
// nex(state, 3) = number of ones represented
//
// States represent a bit history within some context.
// State 0 is the starting state (no bits seen).
// States 1-30 represent all possible sequences of 1-4 bits.
// States 31-252 represent a pair of counts, (n0,n1), the number
// of 0 and 1 bits respectively. If n0+n1 < 16 then there are
// two states for each pair, depending on if a 0 or 1 was the last
// bit seen.
// If n0 and n1 are too large, then there is no state to represent this
// pair, so another state with about the same ratio of n0/n1 is substituted.
// Also, when a bit is observed and the count of the opposite bit is large,
// then part of this count is discarded to favor newer data over old.
static const U8 State_table[256][4]={
{ 1, 2, 0, 0},{ 3, 5, 1, 0},{ 4, 6, 0, 1},{ 7, 10, 2, 0}, // 0-3
{ 8, 12, 1, 1},{ 9, 13, 1, 1},{ 11, 14, 0, 2},{ 15, 19, 3, 0}, // 4-7
{ 16, 23, 2, 1},{ 17, 24, 2, 1},{ 18, 25, 2, 1},{ 20, 27, 1, 2}, // 8-11
{ 21, 28, 1, 2},{ 22, 29, 1, 2},{ 26, 30, 0, 3},{ 31, 33, 4, 0}, // 12-15
{ 32, 35, 3, 1},{ 32, 35, 3, 1},{ 32, 35, 3, 1},{ 32, 35, 3, 1}, // 16-19
{ 34, 37, 2, 2},{ 34, 37, 2, 2},{ 34, 37, 2, 2},{ 34, 37, 2, 2}, // 20-23
{ 34, 37, 2, 2},{ 34, 37, 2, 2},{ 36, 39, 1, 3},{ 36, 39, 1, 3}, // 24-27
{ 36, 39, 1, 3},{ 36, 39, 1, 3},{ 38, 40, 0, 4},{ 41, 43, 5, 0}, // 28-31
{ 42, 45, 4, 1},{ 42, 45, 4, 1},{ 44, 47, 3, 2},{ 44, 47, 3, 2}, // 32-35
{ 46, 49, 2, 3},{ 46, 49, 2, 3},{ 48, 51, 1, 4},{ 48, 51, 1, 4}, // 36-39
{ 50, 52, 0, 5},{ 53, 43, 6, 0},{ 54, 57, 5, 1},{ 54, 57, 5, 1}, // 40-43
{ 56, 59, 4, 2},{ 56, 59, 4, 2},{ 58, 61, 3, 3},{ 58, 61, 3, 3}, // 44-47
{ 60, 63, 2, 4},{ 60, 63, 2, 4},{ 62, 65, 1, 5},{ 62, 65, 1, 5}, // 48-51
{ 50, 66, 0, 6},{ 67, 55, 7, 0},{ 68, 57, 6, 1},{ 68, 57, 6, 1}, // 52-55
{ 70, 73, 5, 2},{ 70, 73, 5, 2},{ 72, 75, 4, 3},{ 72, 75, 4, 3}, // 56-59
{ 74, 77, 3, 4},{ 74, 77, 3, 4},{ 76, 79, 2, 5},{ 76, 79, 2, 5}, // 60-63
{ 62, 81, 1, 6},{ 62, 81, 1, 6},{ 64, 82, 0, 7},{ 83, 69, 8, 0}, // 64-67
{ 84, 71, 7, 1},{ 84, 71, 7, 1},{ 86, 73, 6, 2},{ 86, 73, 6, 2}, // 68-71
{ 44, 59, 5, 3},{ 44, 59, 5, 3},{ 58, 61, 4, 4},{ 58, 61, 4, 4}, // 72-75
{ 60, 49, 3, 5},{ 60, 49, 3, 5},{ 76, 89, 2, 6},{ 76, 89, 2, 6}, // 76-79
{ 78, 91, 1, 7},{ 78, 91, 1, 7},{ 80, 92, 0, 8},{ 93, 69, 9, 0}, // 80-83
{ 94, 87, 8, 1},{ 94, 87, 8, 1},{ 96, 45, 7, 2},{ 96, 45, 7, 2}, // 84-87
{ 48, 99, 2, 7},{ 48, 99, 2, 7},{ 88,101, 1, 8},{ 88,101, 1, 8}, // 88-91
{ 80,102, 0, 9},{103, 69,10, 0},{104, 87, 9, 1},{104, 87, 9, 1}, // 92-95
{106, 57, 8, 2},{106, 57, 8, 2},{ 62,109, 2, 8},{ 62,109, 2, 8}, // 96-99
{ 88,111, 1, 9},{ 88,111, 1, 9},{ 80,112, 0,10},{113, 85,11, 0}, // 100-103
{114, 87,10, 1},{114, 87,10, 1},{116, 57, 9, 2},{116, 57, 9, 2}, // 104-107
{ 62,119, 2, 9},{ 62,119, 2, 9},{ 88,121, 1,10},{ 88,121, 1,10}, // 108-111
{ 90,122, 0,11},{123, 85,12, 0},{124, 97,11, 1},{124, 97,11, 1}, // 112-115
{126, 57,10, 2},{126, 57,10, 2},{ 62,129, 2,10},{ 62,129, 2,10}, // 116-119
{ 98,131, 1,11},{ 98,131, 1,11},{ 90,132, 0,12},{133, 85,13, 0}, // 120-123
{134, 97,12, 1},{134, 97,12, 1},{136, 57,11, 2},{136, 57,11, 2}, // 124-127
{ 62,139, 2,11},{ 62,139, 2,11},{ 98,141, 1,12},{ 98,141, 1,12}, // 128-131
{ 90,142, 0,13},{143, 95,14, 0},{144, 97,13, 1},{144, 97,13, 1}, // 132-135
{ 68, 57,12, 2},{ 68, 57,12, 2},{ 62, 81, 2,12},{ 62, 81, 2,12}, // 136-139
{ 98,147, 1,13},{ 98,147, 1,13},{100,148, 0,14},{149, 95,15, 0}, // 140-143
{150,107,14, 1},{150,107,14, 1},{108,151, 1,14},{108,151, 1,14}, // 144-147
{100,152, 0,15},{153, 95,16, 0},{154,107,15, 1},{108,155, 1,15}, // 148-151
{100,156, 0,16},{157, 95,17, 0},{158,107,16, 1},{108,159, 1,16}, // 152-155
{100,160, 0,17},{161,105,18, 0},{162,107,17, 1},{108,163, 1,17}, // 156-159
{110,164, 0,18},{165,105,19, 0},{166,117,18, 1},{118,167, 1,18}, // 160-163
{110,168, 0,19},{169,105,20, 0},{170,117,19, 1},{118,171, 1,19}, // 164-167
{110,172, 0,20},{173,105,21, 0},{174,117,20, 1},{118,175, 1,20}, // 168-171
{110,176, 0,21},{177,105,22, 0},{178,117,21, 1},{118,179, 1,21}, // 172-175
{110,180, 0,22},{181,115,23, 0},{182,117,22, 1},{118,183, 1,22}, // 176-179
{120,184, 0,23},{185,115,24, 0},{186,127,23, 1},{128,187, 1,23}, // 180-183
{120,188, 0,24},{189,115,25, 0},{190,127,24, 1},{128,191, 1,24}, // 184-187
{120,192, 0,25},{193,115,26, 0},{194,127,25, 1},{128,195, 1,25}, // 188-191
{120,196, 0,26},{197,115,27, 0},{198,127,26, 1},{128,199, 1,26}, // 192-195
{120,200, 0,27},{201,115,28, 0},{202,127,27, 1},{128,203, 1,27}, // 196-199
{120,204, 0,28},{205,115,29, 0},{206,127,28, 1},{128,207, 1,28}, // 200-203
{120,208, 0,29},{209,125,30, 0},{210,127,29, 1},{128,211, 1,29}, // 204-207
{130,212, 0,30},{213,125,31, 0},{214,137,30, 1},{138,215, 1,30}, // 208-211
{130,216, 0,31},{217,125,32, 0},{218,137,31, 1},{138,219, 1,31}, // 212-215
{130,220, 0,32},{221,125,33, 0},{222,137,32, 1},{138,223, 1,32}, // 216-219
{130,224, 0,33},{225,125,34, 0},{226,137,33, 1},{138,227, 1,33}, // 220-223
{130,228, 0,34},{229,125,35, 0},{230,137,34, 1},{138,231, 1,34}, // 224-227
{130,232, 0,35},{233,125,36, 0},{234,137,35, 1},{138,235, 1,35}, // 228-231
{130,236, 0,36},{237,125,37, 0},{238,137,36, 1},{138,239, 1,36}, // 232-235
{130,240, 0,37},{241,125,38, 0},{242,137,37, 1},{138,243, 1,37}, // 236-239
{130,244, 0,38},{245,135,39, 0},{246,137,38, 1},{138,247, 1,38}, // 240-243
{140,248, 0,39},{249,135,40, 0},{250, 69,39, 1},{ 80,251, 1,39}, // 244-247
{140,252, 0,40},{249,135,41, 0},{250, 69,40, 1},{ 80,251, 1,40}, // 248-251
{140,252, 0,41}}; // 252, 253-255 are reserved
#define nex(state,sel) State_table[state][sel]
#if 0 // change to #if 0 to generate this table at run time (4% slower)
static const U8 State_table[256][4]={
{ 1, 2, 0, 0},{ 3, 5, 1, 0},{ 4, 6, 0, 1},{ 7, 10, 2, 0}, // 0-3
{ 8, 12, 1, 1},{ 9, 13, 1, 1},{ 11, 14, 0, 2},{ 15, 19, 3, 0}, // 4-7
{ 16, 23, 2, 1},{ 17, 24, 2, 1},{ 18, 25, 2, 1},{ 20, 27, 1, 2}, // 8-11
{ 21, 28, 1, 2},{ 22, 29, 1, 2},{ 26, 30, 0, 3},{ 31, 33, 4, 0}, // 12-15
{ 32, 35, 3, 1},{ 32, 35, 3, 1},{ 32, 35, 3, 1},{ 32, 35, 3, 1}, // 16-19
{ 34, 37, 2, 2},{ 34, 37, 2, 2},{ 34, 37, 2, 2},{ 34, 37, 2, 2}, // 20-23
{ 34, 37, 2, 2},{ 34, 37, 2, 2},{ 36, 39, 1, 3},{ 36, 39, 1, 3}, // 24-27
{ 36, 39, 1, 3},{ 36, 39, 1, 3},{ 38, 40, 0, 4},{ 41, 43, 5, 0}, // 28-31
{ 42, 45, 4, 1},{ 42, 45, 4, 1},{ 44, 47, 3, 2},{ 44, 47, 3, 2}, // 32-35
{ 46, 49, 2, 3},{ 46, 49, 2, 3},{ 48, 51, 1, 4},{ 48, 51, 1, 4}, // 36-39
{ 50, 52, 0, 5},{ 53, 43, 6, 0},{ 54, 57, 5, 1},{ 54, 57, 5, 1}, // 40-43
{ 56, 59, 4, 2},{ 56, 59, 4, 2},{ 58, 61, 3, 3},{ 58, 61, 3, 3}, // 44-47
{ 60, 63, 2, 4},{ 60, 63, 2, 4},{ 62, 65, 1, 5},{ 62, 65, 1, 5}, // 48-51
{ 50, 66, 0, 6},{ 67, 55, 7, 0},{ 68, 57, 6, 1},{ 68, 57, 6, 1}, // 52-55
{ 70, 73, 5, 2},{ 70, 73, 5, 2},{ 72, 75, 4, 3},{ 72, 75, 4, 3}, // 56-59
{ 74, 77, 3, 4},{ 74, 77, 3, 4},{ 76, 79, 2, 5},{ 76, 79, 2, 5}, // 60-63
{ 62, 81, 1, 6},{ 62, 81, 1, 6},{ 64, 82, 0, 7},{ 83, 69, 8, 0}, // 64-67
{ 84, 71, 7, 1},{ 84, 71, 7, 1},{ 86, 73, 6, 2},{ 86, 73, 6, 2}, // 68-71
{ 44, 59, 5, 3},{ 44, 59, 5, 3},{ 58, 61, 4, 4},{ 58, 61, 4, 4}, // 72-75
{ 60, 49, 3, 5},{ 60, 49, 3, 5},{ 76, 89, 2, 6},{ 76, 89, 2, 6}, // 76-79
{ 78, 91, 1, 7},{ 78, 91, 1, 7},{ 80, 92, 0, 8},{ 93, 69, 9, 0}, // 80-83
{ 94, 87, 8, 1},{ 94, 87, 8, 1},{ 96, 45, 7, 2},{ 96, 45, 7, 2}, // 84-87
{ 48, 99, 2, 7},{ 48, 99, 2, 7},{ 88,101, 1, 8},{ 88,101, 1, 8}, // 88-91
{ 80,102, 0, 9},{103, 69,10, 0},{104, 87, 9, 1},{104, 87, 9, 1}, // 92-95
{106, 57, 8, 2},{106, 57, 8, 2},{ 62,109, 2, 8},{ 62,109, 2, 8}, // 96-99
{ 88,111, 1, 9},{ 88,111, 1, 9},{ 80,112, 0,10},{113, 85,11, 0}, // 100-103
{114, 87,10, 1},{114, 87,10, 1},{116, 57, 9, 2},{116, 57, 9, 2}, // 104-107
{ 62,119, 2, 9},{ 62,119, 2, 9},{ 88,121, 1,10},{ 88,121, 1,10}, // 108-111
{ 90,122, 0,11},{123, 85,12, 0},{124, 97,11, 1},{124, 97,11, 1}, // 112-115
{126, 57,10, 2},{126, 57,10, 2},{ 62,129, 2,10},{ 62,129, 2,10}, // 116-119
{ 98,131, 1,11},{ 98,131, 1,11},{ 90,132, 0,12},{133, 85,13, 0}, // 120-123
{134, 97,12, 1},{134, 97,12, 1},{136, 57,11, 2},{136, 57,11, 2}, // 124-127
{ 62,139, 2,11},{ 62,139, 2,11},{ 98,141, 1,12},{ 98,141, 1,12}, // 128-131
{ 90,142, 0,13},{143, 95,14, 0},{144, 97,13, 1},{144, 97,13, 1}, // 132-135
{ 68, 57,12, 2},{ 68, 57,12, 2},{ 62, 81, 2,12},{ 62, 81, 2,12}, // 136-139
{ 98,147, 1,13},{ 98,147, 1,13},{100,148, 0,14},{149, 95,15, 0}, // 140-143
{150,107,14, 1},{150,107,14, 1},{108,151, 1,14},{108,151, 1,14}, // 144-147
{100,152, 0,15},{153, 95,16, 0},{154,107,15, 1},{108,155, 1,15}, // 148-151
{100,156, 0,16},{157, 95,17, 0},{158,107,16, 1},{108,159, 1,16}, // 152-155
{100,160, 0,17},{161,105,18, 0},{162,107,17, 1},{108,163, 1,17}, // 156-159
{110,164, 0,18},{165,105,19, 0},{166,117,18, 1},{118,167, 1,18}, // 160-163
{110,168, 0,19},{169,105,20, 0},{170,117,19, 1},{118,171, 1,19}, // 164-167
{110,172, 0,20},{173,105,21, 0},{174,117,20, 1},{118,175, 1,20}, // 168-171
{110,176, 0,21},{177,105,22, 0},{178,117,21, 1},{118,179, 1,21}, // 172-175
{110,180, 0,22},{181,115,23, 0},{182,117,22, 1},{118,183, 1,22}, // 176-179
{120,184, 0,23},{185,115,24, 0},{186,127,23, 1},{128,187, 1,23}, // 180-183
{120,188, 0,24},{189,115,25, 0},{190,127,24, 1},{128,191, 1,24}, // 184-187
{120,192, 0,25},{193,115,26, 0},{194,127,25, 1},{128,195, 1,25}, // 188-191
{120,196, 0,26},{197,115,27, 0},{198,127,26, 1},{128,199, 1,26}, // 192-195
{120,200, 0,27},{201,115,28, 0},{202,127,27, 1},{128,203, 1,27}, // 196-199
{120,204, 0,28},{205,115,29, 0},{206,127,28, 1},{128,207, 1,28}, // 200-203
{120,208, 0,29},{209,125,30, 0},{210,127,29, 1},{128,211, 1,29}, // 204-207
{130,212, 0,30},{213,125,31, 0},{214,137,30, 1},{138,215, 1,30}, // 208-211
{130,216, 0,31},{217,125,32, 0},{218,137,31, 1},{138,219, 1,31}, // 212-215
{130,220, 0,32},{221,125,33, 0},{222,137,32, 1},{138,223, 1,32}, // 216-219
{130,224, 0,33},{225,125,34, 0},{226,137,33, 1},{138,227, 1,33}, // 220-223
{130,228, 0,34},{229,125,35, 0},{230,137,34, 1},{138,231, 1,34}, // 224-227
{130,232, 0,35},{233,125,36, 0},{234,137,35, 1},{138,235, 1,35}, // 228-231
{130,236, 0,36},{237,125,37, 0},{238,137,36, 1},{138,239, 1,36}, // 232-235
{130,240, 0,37},{241,125,38, 0},{242,137,37, 1},{138,243, 1,37}, // 236-239
{130,244, 0,38},{245,135,39, 0},{246,137,38, 1},{138,247, 1,38}, // 240-243
{140,248, 0,39},{249,135,40, 0},{250, 69,39, 1},{ 80,251, 1,39}, // 244-247
{140,252, 0,40},{249,135,41, 0},{250, 69,40, 1},{ 80,251, 1,40}, // 248-251
{140,252, 0,41}}; // 252, 253-255 are reserved
#define nex(state,sel) State_table[state][sel]
// The code used to generate the above table at run time (4% slower).
// To print the table, uncomment the 4 lines of print statements below.
// In this code x,y = n0,n1 is the number of 0,1 bits represented by a state.
#else
class StateTable {
int mdc;
//Array<U8> ns; // state*4 -> next state if 0, if 1, n0, n1
enum {B=5, N=64}; // sizes of b, t
int b[6]; // x -> max y, y -> max x
//static U8 t[N][N][2]; // x,y -> state number, number of states
U8 ns[1024]; // state*4 -> next state if 0, if 1, n0, n1
// int bound[6];
U8 t[N][N][2];
int num_states(int x, int y); // compute t[x][y][1]
void discount(int& x); // set new value of x after 1 or y after 0
void next_state(int& x, int& y, int b); // new (x,y) after bit b
void generate(); // compute t[x][y][1]
public:
int next(int state, int sel) {return ns[state*4+sel];}
StateTable(int s0,int s1,int s2,int s3,int s4,int s5,int s6);
void Init(int s0,int s1,int s2,int s3,int s4,int s5,int s6);
} ;
//const int StateTable::b[B]={42,41,13,6,5}; // x -> max y, y -> max x
//U8 StateTable::t[N][N][2];
int StateTable::num_states(int x, int y) {
if (x<y) return num_states(y, x);