-
Notifications
You must be signed in to change notification settings - Fork 0
/
libzpaq.cpp
1412 lines (1317 loc) · 41.2 KB
/
libzpaq.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
/* libzpaq.cpp
Part of LIBZPAQ Version 2.00
Written by Matt Mahoney, Oct. 30, 2010
The LIBZPAQ software is placed in the public domain. It may be used
without restriction. LIBZPAQ is provided "as is" with no warranty.
*/
#include "libzpaq.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
namespace libzpaq {
// Standard library redirections
void* calloc(int a, int b) {return ::calloc(a, b);}
void free(void* p) {::free(p);}
int memcmp(const void* d, const void* s, int n) {
return ::memcmp(d, s, n);}
void* memset(void* d, int c, int n) {return ::memset(d, c, n);}
double log(double x) {return ::log(x);}
double exp(double x) {return ::exp(x);}
double pow(double x, double y) {return ::pow(x, y);}
// Read 16 bit little-endian number
int toU16(const char* p) {
return (p[0]&255)+256*(p[1]&255);
}
//////////////////////////// SHA1 ////////////////////////////
// SHA1 code, see http://en.wikipedia.org/wiki/SHA-1
// Start a new hash
void SHA1::init() {
len0=len1=0;
h[0]=0x67452301;
h[1]=0xEFCDAB89;
h[2]=0x98BADCFE;
h[3]=0x10325476;
h[4]=0xC3D2E1F0;
}
// Return old result and start a new hash
const char* SHA1::result() {
// pad and append length
const U32 s1=len1, s0=len0;
put(0x80);
while ((len0&511)!=448)
put(0);
put(s1>>24);
put(s1>>16);
put(s1>>8);
put(s1);
put(s0>>24);
put(s0>>16);
put(s0>>8);
put(s0);
// copy h to hbuf
for (int i=0; i<5; ++i) {
hbuf[4*i]=h[i]>>24;
hbuf[4*i+1]=h[i]>>16;
hbuf[4*i+2]=h[i]>>8;
hbuf[4*i+3]=h[i];
}
// return hash prior to clearing state
init();
return hbuf;
}
// Hash 1 block of 64 bytes
void SHA1::process() {
for (int i=16; i<80; ++i) {
w[i]=w[i-3]^w[i-8]^w[i-14]^w[i-16];
w[i]=w[i]<<1|w[i]>>31;
}
U32 a=h[0];
U32 b=h[1];
U32 c=h[2];
U32 d=h[3];
U32 e=h[4];
for (int i=0; i<20; ++i) {
const U32 f=(b&c)|(~b&d), k=0x5A827999;
const U32 t=(a<<5|a>>27)+f+e+k+w[i];
e=d;
d=c;
c=b<<30|b>>2;
b=a;
a=t;
}
for (int i=20; i<40; ++i) {
const U32 f=b^c^d, k=0x6ED9EBA1;
const U32 t=(a<<5|a>>27)+f+e+k+w[i];
e=d;
d=c;
c=b<<30|b>>2;
b=a;
a=t;
}
for (int i=40; i<60; ++i) {
const U32 f=(b&c)|(b&d)|(c&d), k=0x8F1BBCDC;
const U32 t=(a<<5|a>>27)+f+e+k+w[i];
e=d;
d=c;
c=b<<30|b>>2;
b=a;
a=t;
}
for (int i=60; i<80; ++i) {
const U32 f=b^c^d, k=0xCA62C1D6;
const U32 t=(a<<5|a>>27)+f+e+k+w[i];
e=d;
d=c;
c=b<<30|b>>2;
b=a;
a=t;
}
h[0]+=a;
h[1]+=b;
h[2]+=c;
h[3]+=d;
h[4]+=e;
}
//////////////////////////// Component ///////////////////////
// A Component is a context model, indirect context model, match model,
// fixed weight mixer, adaptive 2 input mixer without or with current
// partial byte as context, adaptive m input mixer (without or with),
// or SSE (without or with).
const int compsize[256]={0,2,3,2,3,4,6,6,3,5};
void Component::init() {
limit=cxt=a=b=c=0;
cm.resize(0);
ht.resize(0);
a16.resize(0);
}
////////////////////////// StateTable //////////////////////////
// How many states with count of n0 zeros, n1 ones (0...2)
int StateTable::num_states(int n0, int n1) {
const int B=6;
const int bound[B]={20,48,15,8,6,5}; // n0 -> max n1, n1 -> max n0
if (n0<n1) return num_states(n1, n0);
if (n0<0 || n1<0 || n1>=B || n0>bound[n1]) return 0;
return 1+(n1>0 && n0+n1<=17);
}
// New value of count n0 if 1 is observed (and vice versa)
void StateTable::discount(int& n0) {
n0=(n0>=1)+(n0>=2)+(n0>=3)+(n0>=4)+(n0>=5)+(n0>=7)+(n0>=8);
}
// compute next n0,n1 (0 to N) given input y (0 or 1)
void StateTable::next_state(int& n0, int& n1, int y) {
if (n0<n1)
next_state(n1, n0, 1-y);
else {
if (y) {
++n1;
discount(n0);
}
else {
++n0;
discount(n1);
}
// 20,0,0 -> 20,0
// 48,1,0 -> 48,1
// 15,2,0 -> 8,1
// 8,3,0 -> 6,2
// 8,3,1 -> 5,3
// 6,4,0 -> 5,3
// 5,5,0 -> 5,4
// 5,5,1 -> 4,5
while (!num_states(n0, n1)) {
if (n1<2) --n0;
else {
n0=(n0*(n1-1)+(n1/2))/n1;
--n1;
}
}
}
}
// Initialize next state table ns[state*4] -> next if 0, next if 1, n0, n1
StateTable::StateTable() {
// Assign states by increasing priority
const int N=50;
U8 t[N][N][2]={{{0}}}; // (n0,n1,y) -> state number
int state=0;
for (int i=0; i<N; ++i) {
for (int n1=0; n1<=i; ++n1) {
int n0=i-n1;
int n=num_states(n0, n1);
assert(n>=0 && n<=2);
if (n) {
t[n0][n1][0]=state;
t[n0][n1][1]=state+n-1;
state+=n;
}
}
}
// Generate next state table
memset(ns, 0, sizeof(ns));
for (int n0=0; n0<N; ++n0) {
for (int n1=0; n1<N; ++n1) {
for (int y=0; y<num_states(n0, n1); ++y) {
int s=t[n0][n1][y];
assert(s>=0 && s<256);
int s0=n0, s1=n1;
next_state(s0, s1, 0);
assert(s0>=0 && s0<N && s1>=0 && s1<N);
ns[s*4+0]=t[s0][s1][0];
s0=n0, s1=n1;
next_state(s0, s1, 1);
assert(s0>=0 && s0<N && s1>=0 && s1<N);
ns[s*4+1]=t[s0][s1][1];
ns[s*4+2]=n0;
ns[s*4+3]=n1;
}
}
}
}
/////////////////////////// ZPAQL //////////////////////////
// Write header to out2, return true if HCOMP/PCOMP section is present
bool ZPAQL::write(Writer* out2) {
if (header.size()<=6) return false;
assert(header[0]+256*header[1]==cend-2+hend-hbegin);
assert(cend>=7);
assert(hbegin>=cend);
assert(hend>=hbegin);
assert(out2);
if (header[6]>0) { // if any components then write COMP
for (int i=0; i<cend; ++i)
out2->put(header[i]);
}
else { // write PCOMP size only
out2->put((hend-hbegin)&255);
out2->put((hend-hbegin)>>8);
}
for (int i=hbegin; i<hend; ++i)
out2->put(header[i]);
return true;
}
// Read header from in2
int ZPAQL::read(Reader* in2) {
// Get header size and allocate
int hsize=in2->get();
hsize+=in2->get()*256;
header.resize(hsize+300);
cend=hbegin=hend=0;
header[cend++]=hsize&255;
header[cend++]=hsize>>8;
while (cend<7) header[cend++]=in2->get(); // hh hm ph pm n
// Read COMP
int n=header[cend-1];
for (int i=0; i<n; ++i) {
int type=in2->get(); // component type
if (type==-1) error("unexpected end of file");
header[cend++]=type; // component type
int size=compsize[type];
if (size<1) error("Invalid component type");
if (cend+size>header.size()-8) error("COMP list too big");
for (int j=1; j<size; ++j)
header[cend++]=in2->get();
}
if ((header[cend++]=in2->get())!=0) error("missing COMP END");
// Insert a guard gap and read HCOMP
hbegin=hend=cend+128;
while (hend<hsize+129) {
assert(hend<header.size()-8);
int op=in2->get();
if (op==-1) error("unexpected end of file");
header[hend++]=op;
}
if ((header[hend++]=in2->get())!=0) error("missing HCOMP END");
assert(cend>=7 && cend<header.size());
assert(hbegin==cend+128 && hbegin<header.size());
assert(hend>hbegin && hend<header.size());
assert(hsize==header[0]+256*header[1]);
assert(hsize==cend-2+hend-hbegin);
return cend+hend-hbegin;
}
// Free memory, but preserve output, sha1 pointers
void ZPAQL::clear() {
cend=hbegin=hend=0; // COMP and HCOMP locations
a=b=c=d=f=pc=0; // machine state
select=0;
header.resize(0);
h.resize(0);
m.resize(0);
r.resize(0);
}
// Constructor
ZPAQL::ZPAQL() {
clear();
output=0;
sha1=0;
}
// Initialize machine state as HCOMP
void ZPAQL::inith() {
assert(header.size()>6);
assert(output==0);
assert(sha1==0);
init(header[2], header[3]); // hh, hm
}
// Initialize machine state as PCOMP
void ZPAQL::initp() {
assert(header.size()>6);
init(header[4], header[5]); // ph, pm
}
// Return memory requirement in bytes
double ZPAQL::memory() {
double mem=pow(2.0,header[2]+2)+pow(2.0,header[3]) // hh hm
+pow(2.0,header[4]+2)+pow(2.0,header[5]) // ph pm
+header.size();
int cp=7; // start of comp list
for (int i=0; i<header[6]; ++i) { // n
assert(cp<cend);
double size=pow(2.0, header[cp+1]); // sizebits
switch(header[cp]) {
case CM: mem+=4*size; break;
case ICM: mem+=64*size+1024; break;
case MATCH: mem+=4*size+pow(2.0, header[cp+2]); break; // bufbits
case MIX2: mem+=2*size; break;
case MIX: mem+=4*size*header[cp+3]; break; // m
case ISSE: mem+=64*size+2048; break;
case SSE: mem+=128*size; break;
}
cp+=compsize[header[cp]];
}
return mem;
}
// Initialize machine state to run a program.
// Set select to nonzero if header matches anything in the cache
// or else add it.
void ZPAQL::init(int hbits, int mbits) {
assert(header.size()>0);
assert(cend>=7);
assert(hbegin>=cend+128);
assert(hend>=hbegin);
assert(hend<header.size()-130);
assert(header[0]+256*header[1]==cend-2+hend-hbegin);
h.resize(1, hbits);
m.resize(1, mbits);
r.resize(256);
a=b=c=d=pc=f=0;
selectModel();
}
// Run program on input by interpreting header
void ZPAQL::run0(U32 input) {
assert(cend>6);
assert(hbegin>=cend+128);
assert(hend>=hbegin);
assert(hend<header.size()-130);
assert(m.size()>0);
assert(h.size()>0);
assert(header[0]+256*header[1]==cend+hend-hbegin-2);
pc=hbegin;
a=input;
while (execute()) ;
}
// Execute one instruction, return 0 after HALT else 1
int ZPAQL::execute() {
switch(header[pc++]) {
case 0: err(); break; // ERROR
case 1: ++a; break; // A++
case 2: --a; break; // A--
case 3: a = ~a; break; // A!
case 4: a = 0; break; // A=0
case 7: a = r[header[pc++]]; break; // A=R N
case 8: swap(b); break; // B<>A
case 9: ++b; break; // B++
case 10: --b; break; // B--
case 11: b = ~b; break; // B!
case 12: b = 0; break; // B=0
case 15: b = r[header[pc++]]; break; // B=R N
case 16: swap(c); break; // C<>A
case 17: ++c; break; // C++
case 18: --c; break; // C--
case 19: c = ~c; break; // C!
case 20: c = 0; break; // C=0
case 23: c = r[header[pc++]]; break; // C=R N
case 24: swap(d); break; // D<>A
case 25: ++d; break; // D++
case 26: --d; break; // D--
case 27: d = ~d; break; // D!
case 28: d = 0; break; // D=0
case 31: d = r[header[pc++]]; break; // D=R N
case 32: swap(m(b)); break; // *B<>A
case 33: ++m(b); break; // *B++
case 34: --m(b); break; // *B--
case 35: m(b) = ~m(b); break; // *B!
case 36: m(b) = 0; break; // *B=0
case 39: if (f) pc+=((header[pc]+128)&255)-127; else ++pc; break; // JT N
case 40: swap(m(c)); break; // *C<>A
case 41: ++m(c); break; // *C++
case 42: --m(c); break; // *C--
case 43: m(c) = ~m(c); break; // *C!
case 44: m(c) = 0; break; // *C=0
case 47: if (!f) pc+=((header[pc]+128)&255)-127; else ++pc; break; // JF N
case 48: swap(h(d)); break; // *D<>A
case 49: ++h(d); break; // *D++
case 50: --h(d); break; // *D--
case 51: h(d) = ~h(d); break; // *D!
case 52: h(d) = 0; break; // *D=0
case 55: r[header[pc++]] = a; break; // R=A N
case 56: return 0 ; // HALT
case 57: if (output) output->put(a); if (sha1) sha1->put(a); break; // OUT
case 59: a = (a+m(b)+512)*773; break; // HASH
case 60: h(d) = (h(d)+a+512)*773; break; // HASHD
case 63: pc+=((header[pc]+128)&255)-127; break; // JMP N
case 64: a = a; break; // A=A
case 65: a = b; break; // A=B
case 66: a = c; break; // A=C
case 67: a = d; break; // A=D
case 68: a = m(b); break; // A=*B
case 69: a = m(c); break; // A=*C
case 70: a = h(d); break; // A=*D
case 71: a = header[pc++]; break; // A= N
case 72: b = a; break; // B=A
case 73: b = b; break; // B=B
case 74: b = c; break; // B=C
case 75: b = d; break; // B=D
case 76: b = m(b); break; // B=*B
case 77: b = m(c); break; // B=*C
case 78: b = h(d); break; // B=*D
case 79: b = header[pc++]; break; // B= N
case 80: c = a; break; // C=A
case 81: c = b; break; // C=B
case 82: c = c; break; // C=C
case 83: c = d; break; // C=D
case 84: c = m(b); break; // C=*B
case 85: c = m(c); break; // C=*C
case 86: c = h(d); break; // C=*D
case 87: c = header[pc++]; break; // C= N
case 88: d = a; break; // D=A
case 89: d = b; break; // D=B
case 90: d = c; break; // D=C
case 91: d = d; break; // D=D
case 92: d = m(b); break; // D=*B
case 93: d = m(c); break; // D=*C
case 94: d = h(d); break; // D=*D
case 95: d = header[pc++]; break; // D= N
case 96: m(b) = a; break; // *B=A
case 97: m(b) = b; break; // *B=B
case 98: m(b) = c; break; // *B=C
case 99: m(b) = d; break; // *B=D
case 100: m(b) = m(b); break; // *B=*B
case 101: m(b) = m(c); break; // *B=*C
case 102: m(b) = h(d); break; // *B=*D
case 103: m(b) = header[pc++]; break; // *B= N
case 104: m(c) = a; break; // *C=A
case 105: m(c) = b; break; // *C=B
case 106: m(c) = c; break; // *C=C
case 107: m(c) = d; break; // *C=D
case 108: m(c) = m(b); break; // *C=*B
case 109: m(c) = m(c); break; // *C=*C
case 110: m(c) = h(d); break; // *C=*D
case 111: m(c) = header[pc++]; break; // *C= N
case 112: h(d) = a; break; // *D=A
case 113: h(d) = b; break; // *D=B
case 114: h(d) = c; break; // *D=C
case 115: h(d) = d; break; // *D=D
case 116: h(d) = m(b); break; // *D=*B
case 117: h(d) = m(c); break; // *D=*C
case 118: h(d) = h(d); break; // *D=*D
case 119: h(d) = header[pc++]; break; // *D= N
case 128: a += a; break; // A+=A
case 129: a += b; break; // A+=B
case 130: a += c; break; // A+=C
case 131: a += d; break; // A+=D
case 132: a += m(b); break; // A+=*B
case 133: a += m(c); break; // A+=*C
case 134: a += h(d); break; // A+=*D
case 135: a += header[pc++]; break; // A+= N
case 136: a -= a; break; // A-=A
case 137: a -= b; break; // A-=B
case 138: a -= c; break; // A-=C
case 139: a -= d; break; // A-=D
case 140: a -= m(b); break; // A-=*B
case 141: a -= m(c); break; // A-=*C
case 142: a -= h(d); break; // A-=*D
case 143: a -= header[pc++]; break; // A-= N
case 144: a *= a; break; // A*=A
case 145: a *= b; break; // A*=B
case 146: a *= c; break; // A*=C
case 147: a *= d; break; // A*=D
case 148: a *= m(b); break; // A*=*B
case 149: a *= m(c); break; // A*=*C
case 150: a *= h(d); break; // A*=*D
case 151: a *= header[pc++]; break; // A*= N
case 152: div(a); break; // A/=A
case 153: div(b); break; // A/=B
case 154: div(c); break; // A/=C
case 155: div(d); break; // A/=D
case 156: div(m(b)); break; // A/=*B
case 157: div(m(c)); break; // A/=*C
case 158: div(h(d)); break; // A/=*D
case 159: div(header[pc++]); break; // A/= N
case 160: mod(a); break; // A%=A
case 161: mod(b); break; // A%=B
case 162: mod(c); break; // A%=C
case 163: mod(d); break; // A%=D
case 164: mod(m(b)); break; // A%=*B
case 165: mod(m(c)); break; // A%=*C
case 166: mod(h(d)); break; // A%=*D
case 167: mod(header[pc++]); break; // A%= N
case 168: a &= a; break; // A&=A
case 169: a &= b; break; // A&=B
case 170: a &= c; break; // A&=C
case 171: a &= d; break; // A&=D
case 172: a &= m(b); break; // A&=*B
case 173: a &= m(c); break; // A&=*C
case 174: a &= h(d); break; // A&=*D
case 175: a &= header[pc++]; break; // A&= N
case 176: a &= ~ a; break; // A&~A
case 177: a &= ~ b; break; // A&~B
case 178: a &= ~ c; break; // A&~C
case 179: a &= ~ d; break; // A&~D
case 180: a &= ~ m(b); break; // A&~*B
case 181: a &= ~ m(c); break; // A&~*C
case 182: a &= ~ h(d); break; // A&~*D
case 183: a &= ~ header[pc++]; break; // A&~ N
case 184: a |= a; break; // A|=A
case 185: a |= b; break; // A|=B
case 186: a |= c; break; // A|=C
case 187: a |= d; break; // A|=D
case 188: a |= m(b); break; // A|=*B
case 189: a |= m(c); break; // A|=*C
case 190: a |= h(d); break; // A|=*D
case 191: a |= header[pc++]; break; // A|= N
case 192: a ^= a; break; // A^=A
case 193: a ^= b; break; // A^=B
case 194: a ^= c; break; // A^=C
case 195: a ^= d; break; // A^=D
case 196: a ^= m(b); break; // A^=*B
case 197: a ^= m(c); break; // A^=*C
case 198: a ^= h(d); break; // A^=*D
case 199: a ^= header[pc++]; break; // A^= N
case 200: a <<= (a&31); break; // A<<=A
case 201: a <<= (b&31); break; // A<<=B
case 202: a <<= (c&31); break; // A<<=C
case 203: a <<= (d&31); break; // A<<=D
case 204: a <<= (m(b)&31); break; // A<<=*B
case 205: a <<= (m(c)&31); break; // A<<=*C
case 206: a <<= (h(d)&31); break; // A<<=*D
case 207: a <<= (header[pc++]&31); break; // A<<= N
case 208: a >>= (a&31); break; // A>>=A
case 209: a >>= (b&31); break; // A>>=B
case 210: a >>= (c&31); break; // A>>=C
case 211: a >>= (d&31); break; // A>>=D
case 212: a >>= (m(b)&31); break; // A>>=*B
case 213: a >>= (m(c)&31); break; // A>>=*C
case 214: a >>= (h(d)&31); break; // A>>=*D
case 215: a >>= (header[pc++]&31); break; // A>>= N
case 216: f = (a == a); break; // A==A
case 217: f = (a == b); break; // A==B
case 218: f = (a == c); break; // A==C
case 219: f = (a == d); break; // A==D
case 220: f = (a == U32(m(b))); break; // A==*B
case 221: f = (a == U32(m(c))); break; // A==*C
case 222: f = (a == h(d)); break; // A==*D
case 223: f = (a == U32(header[pc++])); break; // A== N
case 224: f = (a < a); break; // A<A
case 225: f = (a < b); break; // A<B
case 226: f = (a < c); break; // A<C
case 227: f = (a < d); break; // A<D
case 228: f = (a < U32(m(b))); break; // A<*B
case 229: f = (a < U32(m(c))); break; // A<*C
case 230: f = (a < h(d)); break; // A<*D
case 231: f = (a < U32(header[pc++])); break; // A< N
case 232: f = (a > a); break; // A>A
case 233: f = (a > b); break; // A>B
case 234: f = (a > c); break; // A>C
case 235: f = (a > d); break; // A>D
case 236: f = (a > U32(m(b))); break; // A>*B
case 237: f = (a > U32(m(c))); break; // A>*C
case 238: f = (a > h(d)); break; // A>*D
case 239: f = (a > U32(header[pc++])); break; // A> N
case 255: if((pc=hbegin+header[pc]+256*header[pc+1])>=hend)err();break;//LJ
default: err();
}
return 1;
}
// Print illegal instruction error message and exit
void ZPAQL::err() {
error("ZPAQL execution error");
}
// Search header for an optimization and set select>0 if found.
void ZPAQL::selectModel() {
int p=0, len, count=0;
select=0;
while (true) {
++count;
len=toU16(models+p);
if (len<1) break;
if (cend+hend-hbegin==len+2 &&
(cend<=6 || memcmp(&header[6], models+p+6, cend-6)==0)
&& memcmp(&header[hbegin], models+p+cend, hend-hbegin)==0)
select=count;
p+=len+2;
}
}
///////////////////////// Predictor /////////////////////////
// Initailize model-independent tables
Predictor::Predictor(ZPAQL& zr):
c8(1), hmap4(1), z(zr) {
assert(sizeof(U8)==1);
assert(sizeof(U16)==2);
assert(sizeof(U32)==4);
assert(sizeof(short)==2);
assert(sizeof(int)==4);
assert(sizeof(ptrdiff_t)==sizeof(char*));
// Initialize tables
for (int i=0; i<1024; ++i)
dt[i]=(1<<17)/(i*2+3)*2;
for (int i=0; i<32768; ++i)
stretcht[i]=int(log((i+0.5)/(32767.5-i))*64+0.5+100000)-100000;
for (int i=0; i<4096; ++i)
squasht[i]=int(32768.0/(1+exp((i-2048)*(-1.0/64))));
// Verify floating point math for squash() and stretch()
U32 sqsum=0, stsum=0;
for (int i=32767; i>=0; --i)
stsum=stsum*3+stretch(i);
for (int i=4095; i>=0; --i)
sqsum=sqsum*3+squash(i-2048);
assert(stsum==3887533746u);
assert(sqsum==2278286169u);
}
// Initialize the predictor with a new model in z
void Predictor::init() {
// Initialize context hash function
z.inith();
// Initialize predictions
for (int i=0; i<256; ++i) p[i]=0;
// Initialize components
for (int i=0; i<256; ++i) // clear old model
comp[i].init();
int n=z.header[6]; // hsize[0..1] hh hm ph pm n (comp)[n] END 0[128] (hcomp) END
if (n<1 || n>255) error("n must be 1..255 components");
const U8* cp=&z.header[7]; // start of component list
for (int i=0; i<n; ++i) {
assert(cp<&z.header[z.cend]);
assert(cp>&z.header[0] && cp<&z.header[z.header.size()-8]);
Component& cr=comp[i];
switch(cp[0]) {
case CONS: // c
p[i]=(cp[1]-128)*4;
break;
case CM: // sizebits limit
cr.cm.resize(1, cp[1]); // packed CM (22 bits) + CMCOUNT (10 bits)
cr.limit=cp[2]*4;
for (int j=0; j<cr.cm.size(); ++j)
cr.cm[j]=0x80000000;
break;
case ICM: // sizebits
cr.limit=1023;
cr.cm.resize(256);
cr.ht.resize(64, cp[1]);
for (int j=0; j<cr.cm.size(); ++j)
cr.cm[j]=st.cminit(j);
break;
case MATCH: // sizebits
cr.cm.resize(1, cp[1]); // index
cr.ht.resize(1, cp[2]); // buf
cr.ht(0)=1;
break;
case AVG: // j k wt
break;
case MIX2: // sizebits j k rate mask
if (cp[3]>=i) error("MIX2 k >= i");
if (cp[2]>=i) error("MIX2 j >= i");
cr.c=(1<<cp[1]); // size (number of contexts)
cr.a16.resize(1, cp[1]); // wt[size][m]
for (int j=0; j<cr.a16.size(); ++j)
cr.a16[j]=32768;
break;
case MIX: { // sizebits j m rate mask
if (cp[2]>=i) error("MIX j >= i");
if (cp[3]<1 || cp[3]>i-cp[2])
error("MIX m not in 1..i-j");
int m=cp[3]; // number of inputs
assert(m>=1);
cr.c=(1<<cp[1]); // size (number of contexts)
cr.cm.resize(m, cp[1]); // wt[size][m]
for (int j=0; j<cr.cm.size(); ++j)
cr.cm[j]=65536/m;
break;
}
case ISSE: // sizebits j
if (cp[2]>=i) error("ISSE j >= i");
cr.ht.resize(64, cp[1]);
cr.cm.resize(512);
for (int j=0; j<256; ++j) {
cr.cm[j*2]=1<<15;
cr.cm[j*2+1]=clamp512k(stretch(st.cminit(j)>>8)<<10);
}
break;
case SSE: // sizebits j start limit
if (cp[2]>=i) error("SSE j >= i");
if (cp[3]>cp[4]*4) error("SSE start > limit*4");
cr.cm.resize(32, cp[1]);
cr.limit=cp[4]*4;
for (int j=0; j<cr.cm.size(); ++j)
cr.cm[j]=squash((j&31)*64-992)<<17|cp[3];
break;
default: error("unknown component type");
}
assert(compsize[*cp]>0);
cp+=compsize[*cp];
assert(cp>=&z.header[7] && cp<&z.header[z.cend]);
}
}
// Return next bit prediction using interpreted COMP code
int Predictor::predict0() {
assert(c8>=1 && c8<=255);
// Predict next bit
int n=z.header[6];
assert(n>0 && n<=255);
const U8* cp=&z.header[7];
assert(cp[-1]==n);
for (int i=0; i<n; ++i) {
assert(cp>&z.header[0] && cp<&z.header[z.header.size()-8]);
Component& cr=comp[i];
switch(cp[0]) {
case CONS: // c
break;
case CM: // sizebits limit
cr.cxt=z.H(i)^hmap4;
p[i]=stretch(cr.cm(cr.cxt)>>17);
break;
case ICM: // sizebits
assert((hmap4&15)>0);
if (c8==1 || (c8&0xf0)==16) cr.c=find(cr.ht, cp[1]+2, z.H(i)+16*c8);
cr.cxt=cr.ht[cr.c+(hmap4&15)];
p[i]=stretch(cr.cm(cr.cxt)>>8);
break;
case MATCH: // sizebits bufbits: a=len, b=offset, c=bit, cxt=256/len,
// ht=buf, limit=8*pos+bp
assert(cr.a>=0 && cr.a<=255);
if (cr.a==0) p[i]=0;
else {
cr.c=cr.ht((cr.limit>>3)-cr.b)>>(7-(cr.limit&7))&1; // predicted bit
p[i]=stretch(cr.cxt*(cr.c*-2+1)&32767);
}
break;
case AVG: // j k wt
p[i]=(p[cp[1]]*cp[3]+p[cp[2]]*(256-cp[3]))>>8;
break;
case MIX2: { // sizebits j k rate mask
// c=size cm=wt[size][m] cxt=input
cr.cxt=((z.H(i)+(c8&cp[5]))&(cr.c-1));
assert(int(cr.cxt)>=0 && int(cr.cxt)<cr.a16.size());
int w=cr.a16[cr.cxt];
assert(w>=0 && w<65536);
p[i]=(w*p[cp[2]]+(65536-w)*p[cp[3]])>>16;
assert(p[i]>=-2048 && p[i]<2048);
}
break;
case MIX: { // sizebits j m rate mask
// c=size cm=wt[size][m] cxt=index of wt in cm
int m=cp[3];
assert(m>=1 && m<=i);
cr.cxt=z.H(i)+(c8&cp[5]);
cr.cxt=(cr.cxt&(cr.c-1))*m; // pointer to row of weights
assert(int(cr.cxt)>=0 && int(cr.cxt)<=cr.cm.size()-m);
int* wt=(int*)&cr.cm[cr.cxt];
p[i]=0;
for (int j=0; j<m; ++j)
p[i]+=(wt[j]>>8)*p[cp[2]+j];
p[i]=clamp2k(p[i]>>8);
}
break;
case ISSE: { // sizebits j -- c=hi, cxt=bh
assert((hmap4&15)>0);
if (c8==1 || (c8&0xf0)==16)
cr.c=find(cr.ht, cp[1]+2, z.H(i)+16*c8);
cr.cxt=cr.ht[cr.c+(hmap4&15)]; // bit history
int *wt=(int*)&cr.cm[cr.cxt*2];
p[i]=clamp2k((wt[0]*p[cp[2]]+wt[1]*64)>>16);
}
break;
case SSE: { // sizebits j start limit
cr.cxt=(z.H(i)+c8)*32;
int pq=p[cp[2]]+992;
if (pq<0) pq=0;
if (pq>1983) pq=1983;
int wt=pq&63;
pq>>=6;
assert(pq>=0 && pq<=30);
cr.cxt+=pq;
p[i]=stretch(((cr.cm(cr.cxt)>>10)*(64-wt)+(cr.cm(cr.cxt+1)>>10)*wt)>>13);
cr.cxt+=wt>>5;
}
break;
default:
error("component predict not implemented");
}
cp+=compsize[cp[0]];
assert(cp<&z.header[z.cend]);
assert(p[i]>=-2048 && p[i]<2048);
}
assert(cp[0]==NONE);
return squash(p[n-1]);
}
// Update model with decoded bit y (0...1)
void Predictor::update0(int y) {
assert(y==0 || y==1);
assert(c8>=1 && c8<=255);
assert(hmap4>=1 && hmap4<=511);
// Update components
const U8* cp=&z.header[7];
int n=z.header[6];
assert(n>=1 && n<=255);
assert(cp[-1]==n);
for (int i=0; i<n; ++i) {
Component& cr=comp[i];
switch(cp[0]) {
case CONS: // c
break;
case CM: // sizebits limit
train(cr, y);
break;
case ICM: { // sizebits: cxt=ht[b]=bh, ht[c][0..15]=bh row, cxt=bh
cr.ht[cr.c+(hmap4&15)]=st.next(cr.ht[cr.c+(hmap4&15)], y);
U32& pn=cr.cm(cr.cxt);
pn+=int(y*32767-(pn>>8))>>2;
}
break;
case MATCH: // sizebits bufbits:
// a=len, b=offset, c=bit, cm=index, cxt=256/len
// ht=buf, limit=8*pos+bp
{
assert(cr.a>=0 && cr.a<=255);
assert(cr.c==0 || cr.c==1);
if (cr.c!=y) cr.a=0; // mismatch?
cr.ht(cr.limit>>3)+=cr.ht(cr.limit>>3)+y;
if ((++cr.limit&7)==0) {
int pos=cr.limit>>3;
if (cr.a==0) { // look for a match
cr.b=pos-cr.cm(z.H(i));
if (cr.b&(cr.ht.size()-1))
while (cr.a<255 && cr.ht(pos-cr.a-1)==cr.ht(pos-cr.a-cr.b-1))
++cr.a;
}
else cr.a+=cr.a<255;
cr.cm(z.H(i))=pos;
if (cr.a>0) cr.cxt=2048/cr.a;
}
}
break;
case AVG: // j k wt
break;
case MIX2: { // sizebits j k rate mask
// cm=input[2],wt[size][2], cxt=weight row
assert(cr.a16.size()==cr.c);
assert(int(cr.cxt)>=0 && int(cr.cxt)<cr.a16.size());
int err=(y*32767-squash(p[i]))*cp[4]>>5;
int w=cr.a16[cr.cxt];
w+=(err*(p[cp[2]]-p[cp[3]])+(1<<12))>>13;
if (w<0) w=0;
if (w>65535) w=65535;
cr.a16[cr.cxt]=w;
}
break;
case MIX: { // sizebits j m rate mask
// cm=wt[size][m], cxt=input
int m=cp[3];
assert(m>0 && m<=i);
assert(cr.cm.size()==m*cr.c);
assert(int(cr.cxt)>=0 && int(cr.cxt)<=cr.cm.size()-m);
int err=(y*32767-squash(p[i]))*cp[4]>>4;
int* wt=(int*)&cr.cm[cr.cxt];
for (int j=0; j<m; ++j)
wt[j]=clamp512k(wt[j]+((err*p[cp[2]+j]+(1<<12))>>13));
}
break;
case ISSE: { // sizebits j -- c=hi, cxt=bh
assert(int(cr.cxt)==cr.ht[cr.c+(hmap4&15)]);
int err=y*32767-squash(p[i]);
int *wt=(int*)&cr.cm[cr.cxt*2];
wt[0]=clamp512k(wt[0]+((err*p[cp[2]]+(1<<12))>>13));
wt[1]=clamp512k(wt[1]+((err+16)>>5));
cr.ht[cr.c+(hmap4&15)]=st.next(cr.cxt, y);
}
break;
case SSE: // sizebits j start limit
train(cr, y);
break;
default:
assert(0);
}
cp+=compsize[cp[0]];
assert(cp>=&z.header[7] && cp<&z.header[z.cend]
&& cp<&z.header[z.header.size()-8]);
}
assert(cp[0]==NONE);
// Save bit y in c8, hmap4
c8+=c8+y;
if (c8>=256) {
z.run(c8-256);
hmap4=1;
c8=1;
}
else if (c8>=16 && c8<32)
hmap4=(hmap4&0xf)<<5|y<<4|1;
else
hmap4=(hmap4&0x1f0)|(((hmap4&0xf)*2+y)&0xf);
}
// Find cxt row in hash table ht. ht has rows of 16 indexed by the
// low sizebits of cxt with element 0 having the next higher 8 bits for
// collision detection. If not found after 3 adjacent tries, replace the
// row with lowest element 1 as priority. Return index of row.
int Predictor::find(Array<U8>& ht, int sizebits, U32 cxt) {
assert(ht.size()==16<<sizebits);
int chk=cxt>>sizebits&255;
int h0=(cxt*16)&(ht.size()-16);
if (ht[h0]==chk) return h0;
int h1=h0^16;
if (ht[h1]==chk) return h1;
int h2=h0^32;
if (ht[h2]==chk) return h2;
if (ht[h0+1]<=ht[h1+1] && ht[h0+1]<=ht[h2+1])
return memset(&ht[h0], 0, 16), ht[h0]=chk, h0;
else if (ht[h1+1]<ht[h2+1])
return memset(&ht[h1], 0, 16), ht[h1]=chk, h1;
else
return memset(&ht[h2], 0, 16), ht[h2]=chk, h2;
}
/////////////////////// Decoder ///////////////////////
Decoder::Decoder(ZPAQL& z):
in(0), low(1), high(0xFFFFFFFF), curr(0), pr(z) {}
// Return next bit of decoded input, which has 16 bit probability p of being 1
int Decoder::decode(int p) {
assert(p>=0 && p<65536);
assert(high>low && low>0);
if (curr<low || curr>high) error("archive corrupted");
assert(curr>=low && curr<=high);
U32 mid=low+((high-low)>>16)*p+((((high-low)&0xffff)*p)>>16); // split range
assert(high>mid && mid>=low);
int y=curr<=mid;
if (y) high=mid; else low=mid+1; // pick half
while ((high^low)<0x1000000) { // shift out identical leading bytes
high=high<<8|255;
low=low<<8;
low+=(low==0);
int c=in->get();
if (c<0) error("unexpected end of file");
curr=curr<<8|c;
}
return y;
}
// Decompress 1 byte or -1 at end of input
int Decoder::decompress() {