forked from kevinjlang/cpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fm85Compression.c
947 lines (761 loc) · 32.9 KB
/
fm85Compression.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
// Copyright 2018, Kevin Lang, Oath Research
#include "fm85Compression.h"
#include "fm85Util.h"
/*********************************/
// The following material is in a separate file because it is so big.
#include "compressionData.data"
/***************************************************************/
/***************************************************************/
U8 * makeInversePermutation (U8 * permu, int length) {
U8 * inverse = (U8 *) malloc (((size_t) length) * sizeof(U8));
assert (inverse != NULL);
int i;
for (i = 0; i < length; i++) {
inverse[permu[i]] = i;
}
for (i = 0; i < length; i++) {
assert (permu[inverse[i]] == i);
}
return inverse;
}
/***************************************************************/
/***************************************************************/
/* Given an encoding table that maps unsigned bytes to codewords
of length at most 12, this builds a size-4096 decoding table */
// The second argument is typically 256, but can be other values such as 65.
U16 * makeDecodingTable (U16 * encodingTable, int numByteValues) {
int byteValue;
U16 * decodingTable = (U16 *) malloc (((size_t) 4096) * sizeof(U16));
assert (decodingTable != NULL);
for (byteValue=0; byteValue < numByteValues; byteValue++) {
int encodingEntry = encodingTable [byteValue];
int codeValue = encodingEntry & 0xfff;
int codeLength = encodingEntry >> 12;
int decodingEntry = (codeLength << 8) | byteValue;
int garbageLength = 12 - codeLength;
int numCopies = 1 << garbageLength;
int garbageBits;
for (garbageBits = 0; garbageBits < numCopies; garbageBits++) {
int extendedCodeValue = codeValue | (garbageBits << codeLength);
decodingTable[extendedCodeValue & 0xfff] = decodingEntry;
}
}
return (decodingTable);
}
/***************************************************************/
/***************************************************************/
void validateDecodingTable (U16 * decodingTable, U16 * encodingTable) {
int decodeThis;
for (decodeThis = 0; decodeThis < 4096; decodeThis++) {
int tmpD = decodingTable[decodeThis];
int decodedByte = tmpD & 0xff;
int decodedLength = tmpD >> 8;
int tmpE = encodingTable[decodedByte];
int encodedBitpattern = tmpE & 0xfff;
int encodedLength = tmpE >> 12;
// encodedBitpattern++; // uncomment this line to test the test
// encodedLength++; // uncomment this line to test the test
assert (decodedLength == encodedLength);
assert (encodedBitpattern == (decodeThis & ((1 << decodedLength) - 1)));
}
}
/***************************************************************/
/***************************************************************/
void makeTheDecodingTables (void) {
int i;
lengthLimitedUnaryDecodingTable65 = makeDecodingTable (lengthLimitedUnaryEncodingTable65, 65);
validateDecodingTable (lengthLimitedUnaryDecodingTable65, lengthLimitedUnaryEncodingTable65);
for (i = 0; i < (16 + 6); i++) {
decodingTablesForHighEntropyByte[i] = makeDecodingTable(encodingTablesForHighEntropyByte[i], 256);
validateDecodingTable (decodingTablesForHighEntropyByte[i], encodingTablesForHighEntropyByte[i]);
}
for (i = 0; i < 16; i++) {
columnPermutationsForDecoding[i] = makeInversePermutation(columnPermutationsForEncoding[i],56);
}
// fprintf (stderr, "tables okay\n"); fflush (stderr);
}
/***************************************************************/
/***************************************************************/
static inline void writeUnary (U32 * compressedWords,
Long * nextWordIndexPtr,
U64 * bitbufPtr,
int * bufbitsPtr,
Long the_value)
{
// printf("%ld writing\n", the_value);
assert (compressedWords != NULL);
assert (nextWordIndexPtr != NULL);
assert (bitbufPtr != NULL);
assert (bufbitsPtr != NULL);
Long nextWordIndex = *nextWordIndexPtr;
U64 bitbuf = *bitbufPtr;
int bufbits = *bufbitsPtr;
assert (bufbits >= 0 && bufbits <= 31);
Long remaining = the_value;
while (remaining >= 16) {
remaining -= 16;
// Here we output 16 zeros, but we don't need to physically write them into bitbuf
// because it already contains zeros in that region.
bufbits += 16; // Record the fact that 16 bits of output have occurred.
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
}
assert (remaining >= 0 && remaining <= 15);
U64 theUnaryCode = 1ULL << remaining;
bitbuf |= theUnaryCode << bufbits;
bufbits += (1 + remaining);
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
*nextWordIndexPtr = nextWordIndex;
*bitbufPtr = bitbuf;
*bufbitsPtr = bufbits;
}
/***************************************************************/
/***************************************************************/
static inline Long readUnary (U32 * compressedWords,
Long * nextWordIndexPtr,
U64 * bitbufPtr,
int * bufbitsPtr)
{
assert (compressedWords != NULL);
assert (nextWordIndexPtr != NULL);
assert (bitbufPtr != NULL);
assert (bufbitsPtr != NULL);
Long nextWordIndex = *nextWordIndexPtr;
U64 bitbuf = *bitbufPtr;
int bufbits = *bufbitsPtr;
Long subTotal = 0;
readUnaryLoop:
MAYBE_FILL_BITBUF(compressedWords,nextWordIndex,8); // ensure 8 bits in bit buffer
// if (bufbits < 8) { // Prepare for an 8-bit peek into the bitstream.
// bitbuf |= (((U64) compressedWords[nextWordIndex++]) << bufbits);
// bufbits += 32;
// }
int peek8 = bitbuf & 0xffULL; // These 8 bits include either all or part of the Unary codeword.
int trailingZeros = byteTrailingZerosTable[peek8];
assert (trailingZeros >= 0 && trailingZeros <= 8);
if (trailingZeros == 8) { // The codeword was partial, so read some more.
subTotal += 8;
bufbits -= 8;
bitbuf >>= 8;
goto readUnaryLoop;
}
bufbits -= (1+trailingZeros);
bitbuf >>= (1+trailingZeros);
*nextWordIndexPtr = nextWordIndex;
*bitbufPtr = bitbuf;
*bufbitsPtr = bufbits;
// printf("%ld READING\n", subTotal+trailingZeros); fflush (stdout);
return (subTotal+trailingZeros);
}
/***************************************************************/
/***************************************************************/
// This returns the number of compressedWords that were actually used.
// It is the caller's responsibility to ensure that the compressedWords array is long enough.
Long lowLevelCompressBytes (U8 * byteArray, // input
Long numBytesToEncode, // input
U16 * encodingTable, // input
U32 * compressedWords) { // output
Long byteIndex = 0;
Long nextWordIndex = 0;
U64 bitbuf = 0; /* bits are packed into this first, then are flushed to compressedWords */
int bufbits = 0; /* number of bits currently in bitbuf; must be between 0 and 31 */
for (byteIndex = 0; byteIndex < numBytesToEncode; byteIndex++) {
U64 codeInfo = (U64) encodingTable[byteArray[byteIndex]];
U64 codeVal = codeInfo & 0xfff;
int codeLen = codeInfo >> 12;
bitbuf |= (codeVal << bufbits);
bufbits += codeLen;
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
}
// Pad the bitstream with 11 zero-bits so that the decompressor's 12-bit peek can't overrun its input.
bufbits += 11;
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
if (bufbits > 0) { // We are done encoding now, so we flush the bit buffer.
assert (bufbits < 32);
compressedWords[nextWordIndex++] = (U32) (bitbuf & 0xffffffff);
bitbuf = 0; bufbits = 0; // not really necessary
}
return nextWordIndex;
}
/***************************************************************/
/***************************************************************/
void lowLevelUncompressBytes (U8 * byteArray, // output
Long numBytesToDecode, // input (but refers to the output)
U16 * decodingTable, // input
U32 * compressedWords, // input
Long numCompressedWords) { // input
Long byteIndex = 0;
Long wordIndex = 0;
U64 bitbuf = 0;
int bufbits = 0;
// printf ("Y\n"); fflush (stdout);
assert (byteArray != NULL);
assert (decodingTable != NULL);
assert (compressedWords != NULL);
for (byteIndex = 0; byteIndex < numBytesToDecode; byteIndex++) {
// if (bufbits < 12) { // Prepare for a 12-bit peek into the bitstream.
// bitbuf |= (((U64) compressedWords[wordIndex++]) << bufbits);
// bufbits += 32;
// }
MAYBE_FILL_BITBUF(compressedWords,wordIndex,12); // ensure 12 bits in bit buffer
int peek12 = bitbuf & 0xfffULL; // These 12 bits will include an entire Huffman codeword.
int lookup = decodingTable[peek12];
int codeWordLength = lookup >> 8;
U8 decodedByte = lookup & 0xff;
byteArray[byteIndex] = decodedByte;
bitbuf >>= codeWordLength;
bufbits -= codeWordLength;
}
// Buffer over-run should be impossible unless there is a bug.
// However, we might as well check here.
assert (wordIndex <= numCompressedWords);
// printf ("X\n"); fflush (stdout);
return;
}
/***************************************************************/
/***************************************************************/
// Here "pairs" refers to row/column pairs that specify
// the positions of surprising values in the bit matrix.
// returns the number of compressedWords actually used
Long lowLevelCompressPairs (U32 * pairArray, // input
Long numPairsToEncode, // input
Long numBaseBits, // input
U32 * compressedWords) { // output
Long pairIndex = 0;
Long nextWordIndex = 0;
U64 bitbuf = 0;
int bufbits = 0;
Long golombLoMask = (1LL << numBaseBits) - 1;
Long predictedRowIndex = 0;
Short predictedColIndex = 0;
for (pairIndex = 0; pairIndex < numPairsToEncode; pairIndex++) {
U32 rowCol = pairArray[pairIndex];
Long rowIndex = (Long) (rowCol >> 6);
Short colIndex = (Short) (rowCol & 63);
if (rowIndex != predictedRowIndex) { predictedColIndex = 0; }
assert (rowIndex >= predictedRowIndex);
assert (colIndex >= predictedColIndex);
Long yDelta = rowIndex - predictedRowIndex;
Short xDelta = colIndex - predictedColIndex;
predictedRowIndex = rowIndex;
predictedColIndex = colIndex + 1;
U64 codeInfo = (U64) lengthLimitedUnaryEncodingTable65[xDelta];
U64 codeVal = codeInfo & 0xfff;
int codeLen = codeInfo >> 12;
bitbuf |= (codeVal << bufbits);
bufbits += codeLen;
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
Long golombLo = yDelta & golombLoMask;
Long golombHi = yDelta >> numBaseBits;
writeUnary (compressedWords, &nextWordIndex, &bitbuf, &bufbits, golombHi);
bitbuf |= golombLo << bufbits;
bufbits += numBaseBits;
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
}
// Pad the bitstream so that the decompressor's 12-bit peek can't overrun its input.
Long padding = 10LL - numBaseBits; // should be 10LL
if (padding < 0) padding = 0;
bufbits += padding;
MAYBE_FLUSH_BITBUF(compressedWords,nextWordIndex);
if (bufbits > 0) { // We are done encoding now, so we flush the bit buffer.
assert (bufbits < 32);
compressedWords[nextWordIndex++] = (U32) (bitbuf & 0xffffffff);
bitbuf = 0; bufbits = 0; // not really necessary
}
return nextWordIndex;
}
/***************************************************************/
/***************************************************************/
void lowLevelUncompressPairs (U32 * pairArray, // output
Long numPairsToDecode, // input (but refers to the output)
Long numBaseBits, // input
U32 * compressedWords, // input
Long numCompressedWords) { // input
Long pairIndex = 0;
Long wordIndex = 0;
U64 bitbuf = 0;
int bufbits = 0;
Long golombLoMask = (1LL << numBaseBits) - 1;
Long predictedRowIndex = 0;
Short predictedColIndex = 0;
// for each pair we need to read:
// xDelta (12-bit length-limited unary)
// yDeltaHi (unary)
// yDeltaLo (basebits)
for (pairIndex = 0; pairIndex < numPairsToDecode; pairIndex++) {
MAYBE_FILL_BITBUF(compressedWords,wordIndex,12); // ensure 12 bits in bit buffer
int peek12 = bitbuf & 0xfffULL;
int lookup = lengthLimitedUnaryDecodingTable65[peek12];
int codeWordLength = lookup >> 8;
Short xDelta = lookup & 0xff;
bitbuf >>= codeWordLength;
bufbits -= codeWordLength;
Long golombHi = readUnary (compressedWords, &wordIndex, &bitbuf, &bufbits);
MAYBE_FILL_BITBUF(compressedWords,wordIndex,numBaseBits); // ensure numBaseBits in bit buffer
Long golombLo = bitbuf & golombLoMask;
bitbuf >>= numBaseBits;
bufbits -= numBaseBits;
Long yDelta = (golombHi << numBaseBits) | golombLo;
// Now that we have yDelta and xDelta, we can compute the pair's row and column.
if (yDelta > 0) { predictedColIndex = 0; }
Long rowIndex = predictedRowIndex + yDelta;
Short colIndex = predictedColIndex + xDelta;
U32 rowCol = (rowIndex << 6) | colIndex;
pairArray[pairIndex] = rowCol;
predictedRowIndex = rowIndex;
predictedColIndex = colIndex + 1;
}
assert (wordIndex <= numCompressedWords); // check for buffer over-run
}
/***************************************************************/
/***************************************************************/
Long safeLengthForCompressedPairBuf (Long k, Long numPairs, Long numBaseBits) {
assert (numPairs > 0);
// Long ybits = k + numPairs; // simpler and safer UB
// The following tighter UB on ybits is based on page 198
// of the textbook "Managing Gigabytes" by Witten, Moffat, and Bell.
// Notice that if numBaseBits == 0 it coincides with (k + numPairs).
Long ybits = numPairs * (1LL + numBaseBits) + (k >> numBaseBits);
Long xbits = 12 * numPairs;
Long padding = 10LL - numBaseBits;
if (padding < 0) padding = 0;
Long bits = xbits + ybits + padding;
return (divideLongsRoundingUp(bits, 32));
}
// Explanation of padding: we write
// 1) xdelta (huffman, provides at least 1 bit, requires 12-bit lookahead)
// 2) ydeltaGolombHi (unary, provides at least 1 bit, requires 8-bit lookahead)
// 3) ydeltaGolombLo (straight B bits).
// So the 12-bit lookahead is the tight constraint, but there are at least (2 + B) bits emitted,
// so we would be safe with max (0, 10 - B) bits of padding at the end of the bitstream.
/***************************************************************/
Long safeLengthForCompressedWindowBuf (Long k) { // measured in 32-bit words
Long bits = 12 * k + 11; // 11 bits of padding, due to 12-bit lookahead, with 1 bit certainly present.
return (divideLongsRoundingUp(bits, 32));
}
/***************************************************************/
/***************************************************************/
Short determinePseudoPhase (Short lgK, Long c) {
Long k = (1LL << lgK);
// This midrange logic produces pseudo-phases. They are used to select encoding tables.
// The thresholds were chosen by hand after looking at plots of measured compression.
if (1000 * c < 2375 * k) {
if ( 4 * c < 3 * k) return ( 16 + 0 ); // midrange table
else if ( 10 * c < 11 * k) return ( 16 + 1 ); // midrange table
else if ( 100 * c < 132 * k) return ( 16 + 2 ); // midrange table
else if ( 3 * c < 5 * k) return ( 16 + 3 ); // midrange table
else if (1000 * c < 1965 * k) return ( 16 + 4 ); // midrange table
else if (1000 * c < 2275 * k) return ( 16 + 5 ); // midrange table
else return ( 6 ); // steady-state table employed before its actual phase
}
else { // This steady-state logic produces true phases. They are used to select
// encoding tables, and also column permutations for the "Sliding" flavor.
assert (lgK >= 4);
Long tmp = c >> (lgK - 4);
Long phase = tmp & 15;
assert (phase >= 0 && phase < 16);
return ((Short) phase);
}
}
/***************************************************************/
/***************************************************************/
void compressTheWindow (FM85 * target, FM85 * source) {
Long k = (1LL << source->lgK);
Long windowBufLen = safeLengthForCompressedWindowBuf (k);
U32 * windowBuf = (U32 *) malloc ((size_t) (windowBufLen * sizeof(U32)));
assert (windowBuf != NULL);
Short pseudoPhase = determinePseudoPhase (source->lgK, source->numCoupons);
target->cwLength = lowLevelCompressBytes (source->slidingWindow, k,
encodingTablesForHighEntropyByte[pseudoPhase],
windowBuf);
// At this point we free the unused portion of the compression output buffer.
// Note: realloc caused strange timing spikes for lgK = 11 and 12.
U32 * shorterBuf = (U32 *) malloc (((size_t) target->cwLength) * sizeof(U32));
if (shorterBuf == NULL) { FATAL_ERROR ("Out of Memory"); }
memcpy ((void *) shorterBuf, (void *) windowBuf, ((size_t) target->cwLength) * sizeof(U32));
free (windowBuf);
target->compressedWindow = shorterBuf;
return;
}
/***************************************************************/
/***************************************************************/
void uncompressTheWindow (FM85 * target, FM85 * source) {
Long k = (1LL << source->lgK);
U8 * window = (U8 *) malloc ((size_t) (k * sizeof(U8)));
assert (window != NULL);
// bzero ((void *) window, (size_t) k); // zeroing not needed here (unlike the Hybrid Flavor)
assert (target->slidingWindow == NULL);
target->slidingWindow = window;
Short pseudoPhase = determinePseudoPhase (source->lgK, source->numCoupons);
assert (source->compressedWindow != NULL);
lowLevelUncompressBytes (target->slidingWindow, k,
decodingTablesForHighEntropyByte[pseudoPhase],
source->compressedWindow,
source->cwLength);
return;
}
/***************************************************************/
/***************************************************************/
void compressTheSurprisingValues (FM85 * target, FM85 * source, U32 * pairs, Long numPairs) {
assert (numPairs > 0);
target->numCompressedSurprisingValues = numPairs;
Long k = (1LL << source->lgK);
Long numBaseBits = golombChooseNumberOfBaseBits (k + numPairs, numPairs);
Long pairBufLen = safeLengthForCompressedPairBuf (k, numPairs, numBaseBits);
U32 * pairBuf = (U32 *) malloc ((size_t) (pairBufLen * sizeof(U32)));
assert (pairBuf != NULL);
target->csvLength = lowLevelCompressPairs (pairs, numPairs, numBaseBits, pairBuf);
// At this point we free the unused portion of the compression output buffer.
// Note: realloc caused strange timing spikes for lgK = 11 and 12.
U32 * shorterBuf = (U32 *) malloc (((size_t) target->csvLength) * sizeof(U32));
if (shorterBuf == NULL) { FATAL_ERROR ("Out of Memory"); }
memcpy ((void *) shorterBuf, (void *) pairBuf, ((size_t) target->csvLength) * sizeof(U32));
free (pairBuf);
target->compressedSurprisingValues = shorterBuf;
}
/***************************************************************/
/***************************************************************/
// allocates and returns an array of uncompressed pairs.
// the length of this array is known to the source sketch.
U32 * uncompressTheSurprisingValues (FM85 * source) {
assert (source->isCompressed == 1);
Long k = (1LL << source->lgK);
Long numPairs = source->numCompressedSurprisingValues;
assert (numPairs > 0);
U32 * pairs = (U32 *) malloc ((size_t) numPairs * sizeof(U32));
assert (pairs != NULL);
Long numBaseBits = golombChooseNumberOfBaseBits (k + numPairs, numPairs);
lowLevelUncompressPairs(pairs, numPairs, numBaseBits,
source->compressedSurprisingValues, source->csvLength);
return (pairs);
}
/***************************************************************/
/***************************************************************/
void compressEmptyFlavor (FM85 * target, FM85 * source) {
return; // nothing to do, so just return
}
/***************************************************************/
void uncompressEmptyFlavor (FM85 * target, FM85 * source) {
return; // nothing to do, so just return
}
/***************************************************************/
/***************************************************************/
void compressSparseFlavor (FM85 * target, FM85 * source) {
assert (source->slidingWindow == NULL); // there is no window to compress
Long numPairs = 0;
U32 * pairs = u32TableUnwrappingGetItems (source->surprisingValueTable, &numPairs);
introspectiveInsertionSort(pairs, 0, numPairs-1);
compressTheSurprisingValues (target, source, pairs, numPairs);
free (pairs);
return;
}
/***************************************************************/
void uncompressSparseFlavor (FM85 * target, FM85 * source) {
assert (source->compressedWindow == NULL);
assert (source->compressedSurprisingValues != NULL);
U32 * pairs = uncompressTheSurprisingValues (source);
Long numPairs = source->numCompressedSurprisingValues;
u32Table * table = makeU32TableFromPairsArray (pairs, numPairs, source->lgK);
target->surprisingValueTable = table;
free (pairs);
return;
}
/***************************************************************/
/***************************************************************/
// The empty space that this leaves at the beginning of the output array
// will be filled in later by the caller.
U32 * trickyGetPairsFromWindow (U8 * window, Long k, Long numPairsToGet, Long emptySpace) {
Long outputLength = emptySpace + numPairsToGet;
U32 * pairs = (U32 *) malloc ((size_t) (outputLength * sizeof(U32)));
assert (pairs != NULL);
Long rowIndex = 0;
Long pairIndex = emptySpace;
for (rowIndex = 0; rowIndex < k; rowIndex++) {
U8 byte = window[rowIndex];
while (byte != 0) {
Short colIndex = byteTrailingZerosTable[byte];
// assert (colIndex < 8);
byte = byte ^ (1 << colIndex); // erase the 1
pairs[pairIndex++] = (U32) ((rowIndex << 6) | colIndex);
}
}
assert (pairIndex == outputLength);
return (pairs);
}
/***************************************************************/
// This is complicated because it effectively builds a Sparse version
// of a Pinned sketch before compressing it. Hence the name Hybrid.
void compressHybridFlavor (FM85 * target, FM85 * source) {
// Long i;
Long k = (1LL << source->lgK);
Long numPairsFromTable = 0;
U32 * pairsFromTable = u32TableUnwrappingGetItems (source->surprisingValueTable, &numPairsFromTable);
introspectiveInsertionSort(pairsFromTable, 0, numPairsFromTable-1);
assert (source->slidingWindow != NULL);
assert (source->windowOffset == 0);
Long numPairsFromArray = source->numCoupons - numPairsFromTable; // because the window offset is zero
U32 * allPairs = trickyGetPairsFromWindow (source->slidingWindow, k, numPairsFromArray, numPairsFromTable);
u32Merge (pairsFromTable, 0, numPairsFromTable,
allPairs, numPairsFromTable, numPairsFromArray,
allPairs, 0); // note the overlapping subarray trick
// for (i = 0; i < source->numCoupons-1; i++) { assert (allPairs[i] < allPairs[i+1]); }
compressTheSurprisingValues (target, source, allPairs, source->numCoupons);
if (pairsFromTable != NULL) { free (pairsFromTable); } // this fixes the bug that Alex found
free (allPairs);
return;
}
/***************************************************************/
void uncompressHybridFlavor (FM85 * target, FM85 * source) {
assert (source->compressedWindow == NULL);
assert (source->compressedSurprisingValues != NULL);
U32 * pairs = uncompressTheSurprisingValues (source);
Long numPairs = source->numCompressedSurprisingValues;
// In the hybrid flavor, some of these pairs actually
// belong in the window, so we will separate them out,
// moving the "true" pairs to the bottom of the array.
Long k = (1LL << source->lgK);
U8 * window = (U8 *) malloc ((size_t) (k * sizeof(U8)));
assert (window != NULL);
bzero ((void *) window, (size_t) k); // important: zero the memory
Long nextTruePair = 0;
Long i;
for (i = 0; i < numPairs; i++) {
U32 rowCol = pairs[i];
assert (rowCol != ALL32BITS);
Short col = (Short) (rowCol & 63);
if (col < 8) {
Long row = (Long) (rowCol >> 6);
window[row] |= (1 << col); // set the window bit
}
else {
pairs[nextTruePair++] = rowCol; // move true pair down
}
}
assert (source->windowOffset == 0);
target->windowOffset = 0;
u32Table * table = makeU32TableFromPairsArray (pairs,
nextTruePair,
source->lgK);
target->surprisingValueTable = table;
target->slidingWindow = window;
free (pairs);
return;
}
/***************************************************************/
/***************************************************************/
void compressPinnedFlavor (FM85 * target, FM85 * source) {
compressTheWindow (target, source);
Long numPairs = source->surprisingValueTable->numItems;
// if (numPairs == 0) {
// fprintf (stderr,"A"); fflush (stderr);
// }
if (numPairs > 0) {
Long chkNumPairs;
U32 * pairs = u32TableUnwrappingGetItems (source->surprisingValueTable, &chkNumPairs);
assert (chkNumPairs == numPairs);
// Here we subtract 8 from the column indices. Because they are stored in the low 6 bits
// of each rowCol pair, and because no column index is less than 8 for a "Pinned" sketch,
// I believe we can simply subtract 8 from the pairs themselves.
Long i; // shift the columns over by 8 positions before compressing (because of the window)
for (i = 0; i < numPairs; i++) {
assert ((pairs[i] & 63) >= 8);
pairs[i] -= 8;
}
introspectiveInsertionSort(pairs, 0, numPairs-1);
compressTheSurprisingValues (target, source, pairs, numPairs);
free (pairs);
}
return;
}
/***************************************************************/
void uncompressPinnedFlavor (FM85 * target, FM85 * source) {
assert (source->compressedWindow != NULL);
uncompressTheWindow (target, source);
Long numPairs = source->numCompressedSurprisingValues;
if (numPairs == 0) {
target->surprisingValueTable = u32TableMake (2, 6 + source->lgK);
// fprintf (stderr,"B"); fflush (stderr);
}
else {
assert (numPairs > 0);
assert (source->compressedSurprisingValues != NULL);
U32 * pairs = uncompressTheSurprisingValues (source);
Long i; // undo the compressor's 8-column shift
for (i = 0; i < numPairs; i++) {
assert ((pairs[i] & 63) < 56);
pairs[i] += 8;
}
u32Table * table = makeU32TableFromPairsArray (pairs, numPairs, source->lgK);
target->surprisingValueTable = table;
free (pairs);
}
return;
}
/***************************************************************/
/***************************************************************/
// Complicated by the existence of both a left fringe and a right fringe.
void compressSlidingFlavor (FM85 * target, FM85 * source) {
compressTheWindow (target, source);
Long numPairs = source->surprisingValueTable->numItems;
// if (numPairs == 0) {
// fprintf (stderr,"C"); fflush (stderr);
// }
if (numPairs > 0) {
Long chkNumPairs;
U32 * pairs = u32TableUnwrappingGetItems (source->surprisingValueTable, &chkNumPairs);
assert (chkNumPairs == numPairs);
// Here we apply a complicated transformation to the column indices, which
// changes the implied ordering of the pairs, so we must do it before sorting.
Short pseudoPhase = determinePseudoPhase (source->lgK, source->numCoupons); // NB
assert (pseudoPhase < 16);
U8 * permutation = columnPermutationsForEncoding[pseudoPhase];
Short offset = source->windowOffset;
assert (offset > 0 && offset <= 56);
Long i;
for (i = 0; i < numPairs; i++) {
U32 rowCol = pairs[i];
Long row = (Long) (rowCol >> 6);
Short col = (Short) (rowCol & 63);
// first rotate the columns into a canonical configuration: new = ((old - (offset+8)) + 64) mod 64
col = (col + 56 - offset) & 63;
assert (col >= 0 && col < 56);
// then apply the permutation
col = permutation[col];
pairs[i] = (U32) ((row << 6) | col);
}
introspectiveInsertionSort(pairs, 0, numPairs-1);
compressTheSurprisingValues (target, source, pairs, numPairs);
free (pairs);
}
return;
}
/***************************************************************/
void uncompressSlidingFlavor (FM85 * target, FM85 * source) {
assert (source->compressedWindow != NULL);
uncompressTheWindow (target, source);
Long numPairs = source->numCompressedSurprisingValues;
if (numPairs == 0) {
target->surprisingValueTable = u32TableMake (2, 6 + source->lgK);
// fprintf (stderr,"D"); fflush (stderr);
}
else {
assert (numPairs > 0);
assert (source->compressedSurprisingValues != NULL);
U32 * pairs = uncompressTheSurprisingValues (source);
Short pseudoPhase = determinePseudoPhase (source->lgK, source->numCoupons); // NB
assert (pseudoPhase < 16);
U8 * permutation = columnPermutationsForDecoding[pseudoPhase];
Short offset = source->windowOffset;
assert (offset > 0 && offset <= 56);
Long i;
for (i = 0; i < numPairs; i++) {
U32 rowCol = pairs[i];
Long row = (Long) (rowCol >> 6);
Short col = (Short) (rowCol & 63);
// first undo the permutation
col = permutation[col];
// then undo the rotation: old = (new + (offset+8)) mod 64
col = (col + (offset+8)) & 63;
pairs[i] = (U32) ((row << 6) | col);
}
u32Table * table = makeU32TableFromPairsArray (pairs, numPairs, source->lgK);
target->surprisingValueTable = table;
free (pairs);
}
return;
}
/***************************************************************/
/***************************************************************/
// Note: in the final system, compressed and uncompressed sketches will have different types
FM85 * fm85Compress (FM85 * source) {
assert (source->isCompressed == 0);
FM85 * target = (FM85 *) malloc (sizeof(FM85));
assert (target != NULL);
target->lgK = source->lgK;
target->numCoupons = source->numCoupons;
target->windowOffset = source->windowOffset;
target->firstInterestingColumn = source->firstInterestingColumn;
target->mergeFlag = source->mergeFlag;
target->kxp = source->kxp;
target->hipEstAccum = source->hipEstAccum;
target->hipErrAccum = source->hipErrAccum;
target->isCompressed = 1;
// initialize the variables that belong in a compressed sketch
target->numCompressedSurprisingValues = 0;
target->compressedSurprisingValues = (U32 *) NULL;
target->csvLength = 0;
target->compressedWindow = (U32 *) NULL;
target->cwLength = 0;
// clear the variables that don't belong in a compressed sketch
target->slidingWindow = NULL;
target->surprisingValueTable = NULL;
enum flavorType flavor = determineSketchFlavor(source);
switch (flavor) {
case EMPTY: compressEmptyFlavor (target, source); break;
case SPARSE:
compressSparseFlavor (target, source);
assert (target->compressedWindow == NULL);
assert (target->compressedSurprisingValues != NULL);
break;
case HYBRID:
compressHybridFlavor (target, source);
assert (target->compressedWindow == NULL);
assert (target->compressedSurprisingValues != NULL);
break;
case PINNED:
compressPinnedFlavor (target, source);
assert (target->compressedWindow != NULL);
// assert (target->compressedSurprisingValues != NULL);
break;
case SLIDING:
compressSlidingFlavor(target, source);
assert (target->compressedWindow != NULL);
// assert (target->compressedSurprisingValues != NULL);
break;
default: FATAL_ERROR ("Unknown sketch flavor");
}
return target;
}
/***************************************************************/
/***************************************************************/
// Note: in the final system, compressed and uncompressed sketches will have different types
FM85 * fm85Uncompress (FM85 * source) {
assert (source->isCompressed == 1);
FM85 * target = (FM85 *) malloc (sizeof(FM85));
assert (target != NULL);
target->lgK = source->lgK;
target->numCoupons = source->numCoupons;
target->windowOffset = source->windowOffset;
target->firstInterestingColumn = source->firstInterestingColumn;
target->mergeFlag = source->mergeFlag;
target->kxp = source->kxp;
target->hipEstAccum = source->hipEstAccum;
target->hipErrAccum = source->hipErrAccum;
target->isCompressed = 0;
// initialize the variables that belong in an updateable sketch
target->slidingWindow = (U8 *) NULL;
target->surprisingValueTable = (u32Table *) NULL;
// clear the variables that don't belong in an updateable sketch
target->numCompressedSurprisingValues = 0;
target->compressedSurprisingValues = (U32 *) NULL;
target->csvLength = 0;
target->compressedWindow = (U32 *) NULL;
target->cwLength = 0;
enum flavorType flavor = determineSketchFlavor(source);
switch (flavor) {
case EMPTY: uncompressEmptyFlavor (target, source); break;
case SPARSE:
assert (source->compressedWindow == NULL);
uncompressSparseFlavor (target, source);
break;
case HYBRID:
uncompressHybridFlavor (target, source);
break;
case PINNED:
assert (source->compressedWindow != NULL);
uncompressPinnedFlavor (target, source);
break;
case SLIDING: uncompressSlidingFlavor(target, source); break;
default: FATAL_ERROR ("Unknown sketch flavor");
}
return target;
}