-
Notifications
You must be signed in to change notification settings - Fork 5
/
r8x16b_avx2.c
1618 lines (1334 loc) · 40.8 KB
/
r8x16b_avx2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <x86intrin.h>
// Optimal at 16 unrolling for both AVX2 and AVX512
#define NX 8
/*-------------------------------------------------------------------------- */
/* rans_byte.h from https://github.com/rygorous/ryg_rans */
// Simple byte-aligned rANS encoder/decoder - public domain - Fabian 'ryg' Giesen 2014
//
// Not intended to be "industrial strength"; just meant to illustrate the general
// idea.
#ifndef RANS_BYTE_HEADER
#define RANS_BYTE_HEADER
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#ifdef assert
#define RansAssert assert
#else
#define RansAssert(x)
#endif
// READ ME FIRST:
//
// This is designed like a typical arithmetic coder API, but there's three
// twists you absolutely should be aware of before you start hacking:
//
// 1. You need to encode data in *reverse* - last symbol first. rANS works
// like a stack: last in, first out.
// 2. Likewise, the encoder outputs bytes *in reverse* - that is, you give
// it a pointer to the *end* of your buffer (exclusive), and it will
// slowly move towards the beginning as more bytes are emitted.
// 3. Unlike basically any other entropy coder implementation you might
// have used, you can interleave data from multiple independent rANS
// encoders into the same bytestream without any extra signaling;
// you can also just write some bytes by yourself in the middle if
// you want to. This is in addition to the usual arithmetic encoder
// property of being able to switch models on the fly. Writing raw
// bytes can be useful when you have some data that you know is
// incompressible, and is cheaper than going through the rANS encode
// function. Using multiple rANS coders on the same byte stream wastes
// a few bytes compared to using just one, but execution of two
// independent encoders can happen in parallel on superscalar and
// Out-of-Order CPUs, so this can be *much* faster in tight decoding
// loops.
//
// This is why all the rANS functions take the write pointer as an
// argument instead of just storing it in some context struct.
// --------------------------------------------------------------------------
// L ('l' in the paper) is the lower bound of our normalization interval.
// Between this and our byte-aligned emission, we use 31 (not 32!) bits.
// This is done intentionally because exact reciprocals for 31-bit uints
// fit in 32-bit uints: this permits some optimizations during encoding.
#define RANS_BYTE_L (1u << 15) // lower bound of our normalization interval
// State for a rANS encoder. Yep, that's all there is to it.
typedef uint32_t RansState;
// Initialize a rANS encoder.
static inline void RansEncInit(RansState* r)
{
*r = RANS_BYTE_L;
}
// Renormalize the encoder. Internal function.
static inline RansState RansEncRenorm(RansState x, uint8_t** pptr, uint32_t freq, uint32_t scale_bits)
{
uint32_t x_max = ((RANS_BYTE_L >> scale_bits) << 8) * freq; // this turns into a shift.
if (x >= x_max) {
uint8_t* ptr = *pptr;
do {
*--ptr = (uint8_t) (x & 0xff);
x >>= 8;
} while (x >= x_max);
*pptr = ptr;
}
return x;
}
// Encodes a single symbol with range start "start" and frequency "freq".
// All frequencies are assumed to sum to "1 << scale_bits", and the
// resulting bytes get written to ptr (which is updated).
//
// NOTE: With rANS, you need to encode symbols in *reverse order*, i.e. from
// beginning to end! Likewise, the output bytestream is written *backwards*:
// ptr starts pointing at the end of the output buffer and keeps decrementing.
static inline void RansEncPut(RansState* r, uint8_t** pptr, uint32_t start, uint32_t freq, uint32_t scale_bits)
{
// renormalize
RansState x = RansEncRenorm(*r, pptr, freq, scale_bits);
// x = C(s,x)
*r = ((x / freq) << scale_bits) + (x % freq) + start;
}
// Flushes the rANS encoder.
static inline void RansEncFlush(RansState* r, uint8_t** pptr)
{
uint32_t x = *r;
uint8_t* ptr = *pptr;
ptr -= 4;
ptr[0] = (uint8_t) (x >> 0);
ptr[1] = (uint8_t) (x >> 8);
ptr[2] = (uint8_t) (x >> 16);
ptr[3] = (uint8_t) (x >> 24);
*pptr = ptr;
}
// Initializes a rANS decoder.
// Unlike the encoder, the decoder works forwards as you'd expect.
static inline void RansDecInit(RansState* r, uint8_t** pptr)
{
uint32_t x;
uint8_t* ptr = *pptr;
x = ptr[0] << 0;
x |= ptr[1] << 8;
x |= ptr[2] << 16;
x |= ptr[3] << 24;
ptr += 4;
*pptr = ptr;
*r = x;
}
// Returns the current cumulative frequency (map it to a symbol yourself!)
static inline uint32_t RansDecGet(RansState* r, uint32_t scale_bits)
{
return *r & ((1u << scale_bits) - 1);
}
// Advances in the bit stream by "popping" a single symbol with range start
// "start" and frequency "freq". All frequencies are assumed to sum to "1 << scale_bits",
// and the resulting bytes get written to ptr (which is updated).
static inline void RansDecAdvance(RansState* r, uint8_t** pptr, uint32_t start, uint32_t freq, uint32_t scale_bits)
{
uint32_t mask = (1u << scale_bits) - 1;
// s, x = D(x)
uint32_t x = *r;
x = freq * (x >> scale_bits) + (x & mask) - start;
// renormalize
if (x < RANS_BYTE_L) {
uint8_t* ptr = *pptr;
do x = (x << 8) | *ptr++; while (x < RANS_BYTE_L);
*pptr = ptr;
}
*r = x;
}
// --------------------------------------------------------------------------
// That's all you need for a full encoder; below here are some utility
// functions with extra convenience or optimizations.
// Encoder symbol description
// This (admittedly odd) selection of parameters was chosen to make
// RansEncPutSymbol as cheap as possible.
typedef struct {
uint32_t x_max; // (Exclusive) upper bound of pre-normalization interval
uint32_t rcp_freq; // Fixed-point reciprocal frequency
uint32_t bias; // Bias
uint16_t cmpl_freq; // Complement of frequency: (1 << scale_bits) - freq
uint16_t rcp_shift; // Reciprocal shift
// FIXME: temporary
uint16_t scale_bits;
uint16_t freq;
uint16_t start;
} RansEncSymbol;
// Decoder symbols are straightforward.
typedef struct {
uint16_t start; // Start of range.
uint16_t freq; // Symbol frequency.
} RansDecSymbol;
// Initializes an encoder symbol to start "start" and frequency "freq"
static inline void RansEncSymbolInit(RansEncSymbol* s, uint32_t start, uint32_t freq, uint32_t scale_bits)
{
RansAssert(scale_bits <= 16);
RansAssert(start <= (1u << scale_bits));
RansAssert(freq <= (1u << scale_bits) - start);
// Say M := 1 << scale_bits.
//
// The original encoder does:
// x_new = (x/freq)*M + start + (x%freq)
//
// The fast encoder does (schematically):
// q = mul_hi(x, rcp_freq) >> rcp_shift (division)
// r = x - q*freq (remainder)
// x_new = q*M + bias + r (new x)
// plugging in r into x_new yields:
// x_new = bias + x + q*(M - freq)
// =: bias + x + q*cmpl_freq (*)
//
// and we can just precompute cmpl_freq. Now we just need to
// set up our parameters such that the original encoder and
// the fast encoder agree.
// FIXME: temporary
s->scale_bits = scale_bits;
s->freq = freq;
s->start = start;
s->x_max = ((RANS_BYTE_L >> scale_bits) << 16) * freq;
s->cmpl_freq = (uint16_t) ((1 << scale_bits) - freq);
if (freq < 2) {
// freq=0 symbols are never valid to encode, so it doesn't matter what
// we set our values to.
//
// freq=1 is tricky, since the reciprocal of 1 is 1; unfortunately,
// our fixed-point reciprocal approximation can only multiply by values
// smaller than 1.
//
// So we use the "next best thing": rcp_freq=0xffffffff, rcp_shift=0.
// This gives:
// q = mul_hi(x, rcp_freq) >> rcp_shift
// = mul_hi(x, (1<<32) - 1)) >> 0
// = floor(x - x/(2^32))
// = x - 1 if 1 <= x < 2^32
// and we know that x>0 (x=0 is never in a valid normalization interval).
//
// So we now need to choose the other parameters such that
// x_new = x*M + start
// plug it in:
// x*M + start (desired result)
// = bias + x + q*cmpl_freq (*)
// = bias + x + (x - 1)*(M - 1) (plug in q=x-1, cmpl_freq)
// = bias + 1 + (x - 1)*M
// = x*M + (bias + 1 - M)
//
// so we have start = bias + 1 - M, or equivalently
// bias = start + M - 1.
s->rcp_freq = ~0u;
s->rcp_shift = 0;
s->bias = start + (1 << scale_bits) - 1;
} else {
// Alverson, "Integer Division using reciprocals"
// shift=ceil(log2(freq))
uint32_t shift = 0;
while (freq > (1u << shift))
shift++;
s->rcp_freq = (uint32_t) (((1ull << (shift + 31)) + freq-1) / freq);
s->rcp_shift = shift - 1;
// With these values, 'q' is the correct quotient, so we
// have bias=start.
s->bias = start;
}
s->rcp_shift += 32; // Avoid the extra >>32 in RansEncPutSymbol
}
// Initialize a decoder symbol to start "start" and frequency "freq"
static inline void RansDecSymbolInit(RansDecSymbol* s, uint32_t start, uint32_t freq)
{
RansAssert(start <= (1 << 16));
RansAssert(freq <= (1 << 16) - start);
s->start = (uint16_t) start;
s->freq = (uint16_t) freq;
}
// Encodes a given symbol. This is faster than straight RansEnc since we can do
// multiplications instead of a divide.
//
// See RansEncSymbolInit for a description of how this works.
static inline void RansEncPutSymbol(RansState* r, uint8_t** pptr, RansEncSymbol const* sym)
{
RansAssert(sym->x_max != 0); // can't encode symbol with freq=0
// renormalize
uint32_t x = *r;
uint32_t x_max = sym->x_max;
if (x >= x_max) {
uint16_t* ptr = *(uint16_t **)pptr;
*--ptr = (uint16_t) (x & 0xffff);
x >>= 16;
*pptr = (uint8_t *)ptr;
}
// x = C(s,x)
// NOTE: written this way so we get a 32-bit "multiply high" when
// available. If you're on a 64-bit platform with cheap multiplies
// (e.g. x64), just bake the +32 into rcp_shift.
//uint32_t q = (uint32_t) (((uint64_t)x * sym->rcp_freq) >> 32) >> sym->rcp_shift;
// Slow method, but robust
// *r = ((x / sym->freq) << sym->scale_bits) + (x % sym->freq) + sym->start;
// return;
// The extra >>32 has already been added to RansEncSymbolInit
uint32_t q = (uint32_t) (((uint64_t)x * sym->rcp_freq) >> sym->rcp_shift);
*r = x + sym->bias + q * sym->cmpl_freq;
// assert(((x / sym->freq) << sym->scale_bits) + (x % sym->freq) + sym->start == *r);
}
// Equivalent to RansDecAdvance that takes a symbol.
static inline void RansDecAdvanceSymbol(RansState* r, uint8_t** pptr, RansDecSymbol const* sym, uint32_t scale_bits)
{
RansDecAdvance(r, pptr, sym->start, sym->freq, scale_bits);
}
// Advances in the bit stream by "popping" a single symbol with range start
// "start" and frequency "freq". All frequencies are assumed to sum to "1 << scale_bits".
// No renormalization or output happens.
static inline void RansDecAdvanceStep(RansState* r, uint32_t start, uint32_t freq, uint32_t scale_bits)
{
uint32_t mask = (1u << scale_bits) - 1;
// s, x = D(x)
uint32_t x = *r;
*r = freq * (x >> scale_bits) + (x & mask) - start;
}
// Equivalent to RansDecAdvanceStep that takes a symbol.
static inline void RansDecAdvanceSymbolStep(RansState* r, RansDecSymbol const* sym, uint32_t scale_bits)
{
RansDecAdvanceStep(r, sym->start, sym->freq, scale_bits);
}
// Renormalize.
static inline void RansDecRenorm(RansState* r, uint8_t** pptr)
{
// renormalize
uint32_t x = *r;
#ifndef USE_ASM
// Better on Atom?
//
// clang 464, gcc 485
if (x >= RANS_BYTE_L) return;
uint16_t* ptr = *(uint16_t **)pptr;
x = (x << 16) | *ptr++;
*pptr = (uint8_t *)ptr;
// clang 335, gcc 687
// uint32_t c = x < RANS_BYTE_L;
// uint16_t* ptr = *(uint16_t **)pptr;
// uint32_t y = (x << 16) | *ptr;
// x = c ? y : x;
// ptr += c;
// *pptr = (uint8_t *)ptr;
#else
// clang 596, gcc 650
uint16_t *ptr = *(uint16_t **)pptr;
__asm__ ("movzwl (%0), %%eax\n\t"
"mov %1, %%edx\n\t"
"shl $0x10, %%edx\n\t"
"or %%eax, %%edx\n\t"
"xor %%eax, %%eax\n\t"
"cmp $0x8000,%1\n\t"
"cmovb %%edx, %1\n\t"
"lea 2(%0), %%rax\n\t"
"cmovb %%rax, %0\n\t"
: "=r" (ptr), "=r" (x)
: "0" (ptr), "1" (x)
: "eax", "edx"
);
*pptr = (uint8_t *)ptr;
*pptr = (uint8_t *)ptr;
#endif
*r = x;
}
#endif // RANS_BYTE_HEADER
/*-------------------------------------------------------------------------- */
/*
* Example wrapper to use the rans_byte.h functions included above.
*
* This demonstrates how to use, and unroll, an order-0 and order-1 frequency
* model.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/time.h>
#include "r16N.h"
#define TF_SHIFT 12
#define TOTFREQ (1<<TF_SHIFT)
// 9 is considerably faster in the O1sfb variant due to reduced table size.
#define TF_SHIFT_O1 9
#define TOTFREQ_O1 (1<<TF_SHIFT_O1)
/*-----------------------------------------------------------------------------
* Memory to memory compression functions.
*
* These are original versions without any manual loop unrolling. They
* are easier to understand, but can be up to 2x slower.
*/
#define MAGIC 8
#ifdef UNUSED
static void hist1(unsigned char *in, unsigned int in_size, int F0[256]) {
int i;
for (i = 0; i < in_size; i++)
F0[in[i]]++;
}
static void hist4p(unsigned char *in, unsigned int in_size, int *F0) {
int F1[256+MAGIC] = {0}, F2[256+MAGIC] = {0}, F3[256+MAGIC] = {0};
int i;
unsigned char *in_end4 = in + (in_size & ~3);
unsigned char *in_end = in + in_size;
while (in < in_end4) {
F0[*in++]++;
F1[*in++]++;
F2[*in++]++;
F3[*in++]++;
}
while (in < in_end)
F0[*in++]++;
for (i = 0; i < 256; i++)
F0[i] += F1[i] + F2[i] + F3[i];
}
#endif
static void hist8(unsigned char *in, unsigned int in_size, int F0[256]) {
int F1[256+MAGIC] = {0}, F2[256+MAGIC] = {0}, F3[256+MAGIC] = {0};
int F4[256+MAGIC] = {0}, F5[256+MAGIC] = {0}, F6[256+MAGIC] = {0}, F7[256+MAGIC] = {0};
int i, i8 = in_size & ~7;
for (i = 0; i < i8; i+=8) {
F0[in[i+0]]++;
F1[in[i+1]]++;
F2[in[i+2]]++;
F3[in[i+3]]++;
F4[in[i+4]]++;
F5[in[i+5]]++;
F6[in[i+6]]++;
F7[in[i+7]]++;
}
while (i < in_size)
F0[in[i++]]++;
for (i = 0; i < 256; i++)
F0[i] += F1[i] + F2[i] + F3[i] + F4[i] + F5[i] + F6[i] + F7[i];
}
unsigned int rans_compress_bound_4x16(unsigned int size, int order, int *tab) {
int tabsz = order == 0
? 257*3 + 4 + NX*4 + NX*4
: 257*257*3 + 4 + NX*4 + NX*4;
if (tab) *tab = tabsz;
return 1.05*size + NX*4 + tabsz;
}
unsigned char *rans_compress_O0_4x16(unsigned char *in, unsigned int in_size,
unsigned char *out, unsigned int *out_size) {
unsigned char *cp, *out_end;
RansEncSymbol syms[256];
RansState ransN[NX];
uint8_t* ptr;
int F[256+MAGIC] = {0}, i, j, tab_size, rle, x, fsum = 0;
int m = 0, M = 0;
int tabsz;
int bound = rans_compress_bound_4x16(in_size,0,&tabsz), z;
if (!out) {
*out_size = bound;
out = malloc(*out_size);
}
if (!out || bound > *out_size) {
return NULL;
}
ptr = out_end = out + bound;
// Compute statistics
hist8(in, in_size, F);
double p = (double)TOTFREQ/(double)in_size;
// Normalise so T[i] == TOTFREQ
for (m = M = j = 0; j < 256; j++) {
if (!F[j])
continue;
if (m < F[j])
m = F[j], M = j;
if ((F[j] = F[j]*p+0.499) == 0)
F[j] = 1;
fsum += F[j];
}
fsum++; // not needed, but can't remove without removing assert x<TOTFREQ (in old code)
int adjust = TOTFREQ - fsum;
if (adjust > 0) {
F[M] += adjust;
} else if (adjust < 0) {
if (F[M] > -adjust) {
F[M] += adjust;
} else {
adjust += F[M]-1;
F[M] = 1;
for (j = 0; adjust && j < 256; j++) {
if (F[j] < 2) continue;
int d = F[j] > -adjust;
int m = d ? adjust : 1-F[j];
F[j] += m;
adjust -= m;
}
}
}
//printf("F[%d]=%d\n", M, F[M]);
assert(F[M]>0);
// Encode statistics.
cp = out+4;
for (x = rle = j = 0; j < 256; j++) {
if (F[j]) {
// j
if (rle) {
rle--;
} else {
*cp++ = j;
if (!rle && j && F[j-1]) {
for(rle=j+1; rle<256 && F[rle]; rle++)
;
rle -= j+1;
*cp++ = rle;
}
//fprintf(stderr, "%d: %d %d\n", j, rle, N[j]);
}
// F[j]
if (F[j]<128) {
*cp++ = F[j];
} else {
*cp++ = 128 | (F[j]>>8);
*cp++ = F[j]&0xff;
}
RansEncSymbolInit(&syms[j], x, F[j], TF_SHIFT);
x += F[j];
}
}
*cp++ = 0;
//write(2, out+4, cp-(out+4));
tab_size = cp-out;
for (z = 0; z < NX; z++)
RansEncInit(&ransN[z]);
z = i = in_size&(NX-1);
while (z-- > 0)
RansEncPutSymbol(&ransN[z], &ptr, &syms[in[in_size-(i-z)]]);
for (i=(in_size &~(NX-1)); i>0; i-=NX) {
for (z = NX-1; z >= 0; z--) {
RansEncSymbol *s = &syms[in[i-(NX-z)]];
RansEncPutSymbol(&ransN[z], &ptr, s);
}
}
for (z = NX-1; z >= 0; z--)
RansEncFlush(&ransN[z], &ptr);
// Finalise block size and return it
*out_size = (out_end - ptr) + tab_size;
cp = out;
*cp++ = (in_size>> 0) & 0xff;
*cp++ = (in_size>> 8) & 0xff;
*cp++ = (in_size>>16) & 0xff;
*cp++ = (in_size>>24) & 0xff;
memmove(out + tab_size, ptr, out_end-ptr);
return out;
}
typedef struct {
unsigned char R[TOTFREQ];
} ari_decoder;
unsigned char *rans_uncompress_O0_4x16(unsigned char *in, unsigned int in_size,
unsigned char *out, unsigned int *out_size) {
/* Load in the static tables */
unsigned char *cp = in + 4;
int i, j, x, y, out_sz, rle;
//uint16_t sfreq[TOTFREQ+32];
//uint16_t sbase[TOTFREQ+32]; // faster to use 32-bit on clang
uint8_t ssym [TOTFREQ+64]; // faster to use 16-bit on clang
uint32_t s3[TOTFREQ] __attribute__((aligned(32))); // For TF_SHIFT <= 12
out_sz = ((in[0])<<0) | ((in[1])<<8) | ((in[2])<<16) | ((in[3])<<24);
if (!out) {
out = malloc(out_sz);
*out_size = out_sz;
}
if (!out || out_sz > *out_size)
return NULL;
// Precompute reverse lookup of frequency.
rle = x = y = 0;
j = *cp++;
do {
int F, C;
if ((F = *cp++) >= 128) {
F &= ~128;
F = ((F & 127) << 8) | *cp++;
}
C = x;
for (y = 0; y < F; y++) {
ssym [y + C] = j;
//sfreq[y + C] = F;
//sbase[y + C] = y;
s3[y+C] = (((uint32_t)F)<<(TF_SHIFT+8))|(y<<8)|j;
}
x += F;
if (!rle && j+1 == *cp) {
j = *cp++;
rle = *cp++;
} else if (rle) {
rle--;
j++;
} else {
j = *cp++;
}
} while(j);
assert(x < TOTFREQ);
int z;
RansState R[NX] __attribute__((aligned(32)));
for (z = 0; z < NX; z++)
RansDecInit(&R[z], &cp);
uint16_t *sp = (uint16_t *)cp;
int out_end = (out_sz&~(NX-1));
const uint32_t mask = (1u << TF_SHIFT)-1;
#ifdef USE_AVX2
__m256i maskv = _mm256_set1_epi32(mask); // set mask in all lanes
__m256i Rv = _mm256_load_si256((__m256i *)R);
#endif
for (i=0; i < out_end; i+=NX) {
#ifdef USE_AVX2
//for (z = 0; z < NX; z++)
// V[z] = sp[idx[z]];
// 688/674
//__m256i Vv = _mm256_i32gather_epi32((int *)sp, indices, sizeof(*sp));
__m256i Vv = _mm256_cvtepu16_epi32(_mm_loadu_si128((__m128i *)sp));
//Vv = _mm256_and_si256(Vv, _mm256_set1_epi32(0xffff));
// m[z] = R[z] & mask;
__m256i masked = _mm256_and_si256(Rv, maskv);
// S[z] = s3[m[z]];
__m256i Sv = _mm256_i32gather_epi32(s3, masked, sizeof(*s3));
// f[z] = S[z]>>(TF_SHIFT+8);
__m256i fv = _mm256_srli_epi32(Sv, TF_SHIFT+8);
// b[z] = (S[z]>>8) & mask;
__m256i bv = _mm256_srli_epi32(Sv, 8);
bv = _mm256_and_si256(bv, maskv);
// s[z] = S[z] & 0xff;
__m256i sv = _mm256_and_si256(Sv, _mm256_set1_epi32(0xff));
// R[z] = f[z] * (R[z] >> TF_SHIFT) + b[z];
Rv = _mm256_srli_epi32(Rv, TF_SHIFT);
Rv = _mm256_mullo_epi32(Rv, fv);
Rv = _mm256_add_epi32(Rv, bv);
// Tricky one: out[i+z] = s[z];
// ---a---b ---c---d ---e---f ---g---h
// packs_epi32 -a-b-c-d -a-b-c-d -e-f-g-h -e-f-g-h
// permute4x64 -a-b-c-d -e-f-g-h -a-b-c-d -e-f-g-h
// packs_epi16 abcdefgh abcdefgh abcdefgh abcdefgh
#if 1
//sv = _mm256_and_si256(sv, _mm256_set1_epi32(0xff)); // 1 byte only
sv = _mm256_packus_epi32(sv, sv);
sv = _mm256_permute4x64_epi64(sv, 0xd8);
sv = _mm256_packus_epi16(sv, sv);
*(uint64_t *)&out[i] = _mm256_extract_epi64(sv, 0);
#else
out[i+0] = _mm256_extract_epi32(sv, 0);
out[i+1] = _mm256_extract_epi32(sv, 1);
out[i+2] = _mm256_extract_epi32(sv, 2);
out[i+3] = _mm256_extract_epi32(sv, 3);
out[i+4] = _mm256_extract_epi32(sv, 4);
out[i+5] = _mm256_extract_epi32(sv, 5);
out[i+6] = _mm256_extract_epi32(sv, 6);
out[i+7] = _mm256_extract_epi32(sv, 7);
#endif
#include "permute.h"
// y = (R[z] << 16) | V[z];
__m256i Yv = _mm256_slli_epi32(Rv, 16);
//Yv = _mm256_or_si256(Yv, Vv);
// c = R[z] < RANS_BYTE_L;
__m256i renorm_mask = _mm256_xor_si256(Rv, _mm256_set1_epi32(0x80000000));
renorm_mask = _mm256_cmpgt_epi32(_mm256_set1_epi32(RANS_BYTE_L-0x80000000),
renorm_mask);
// Shift to correct locations in Vv for blending
unsigned int imask = _mm256_movemask_ps((__m256)renorm_mask);
__m256i idx = _mm256_load_si256((const __m256i*)permute[imask]);
Vv = _mm256_permutevar8x32_epi32(Vv, idx);
//Vv = _mm256_and_si256(Vv, renorm_mask); (blend does the AND anyway)
Yv = _mm256_or_si256(Yv, Vv);
//// idx[z] = c ? idx[z]+1 : idx[z];
//__m256i indices_p1 = _mm256_add_epi32(indices, _mm256_set1_epi32(1));
//indices = _mm256_blendv_epi8(indices, indices_p1, renorm_mask);
sp += _mm_popcnt_u32(imask);
// R[z] = c ? Y[z] : R[z];
Rv = _mm256_blendv_epi8(Rv, Yv, renorm_mask);
#else
#pragma omp simd aligned(R,s3 : 32)
for (z = 0; z < NX; z++) {
uint32_t S = s3[R[z] & mask];
uint16_t V = *sp;
uint16_t f = S>>(TF_SHIFT+8), b = (S>>8) & mask;
R[z] = f * (R[z] >> TF_SHIFT) + b;
uint32_t y = (R[z] << 16) | V;
int c = R[z] < RANS_BYTE_L;
sp = c ? sp+1 : sp;
R[z] = c ? y : R[z];
out[i+z] = S;
}
#endif
}
#ifdef USE_AVX2
_mm256_store_si256((__m256i *)R, Rv);
#endif
//#pragma omp simd
// for (z = 0; z < NX; z++) {
// uint32_t m = R[z] & mask;
// R[z] = sfreq[m] * (R[z] >> TF_SHIFT) + sbase[m];
// out[i+z] = ssym[m];
// uint32_t c = R[z] < RANS_BYTE_L; // NX16=>166MB/s
// uint32_t y = (R[z] << 16) | *spN[z];
// spN[z] += c ? 1 : 0;
// R[z] = c ? y : R[z];
//
// }
// }
for (z = out_sz & (NX-1); z-- > 0; )
out[out_end + z] = ssym[R[z] & mask];
*out_size = out_sz;
return out;
}
#ifdef UNUSED
static void hist1_1(unsigned char *in, unsigned int in_size,
int F0[256][256], int T0[256]) {
unsigned int last_i, i;
unsigned char c;
for (last_i=i=0; i<in_size; i++) {
F0[last_i][c = in[i]]++;
T0[last_i]++;
last_i = c;
}
}
#endif
static void hist1_4(unsigned char *in, unsigned int in_size,
int F0[256][256], int *T0) {
int T1[256+MAGIC] = {0}, T2[256+MAGIC] = {0}, T3[256+MAGIC] = {0};
unsigned int idiv4 = in_size/4;
int i;
unsigned char c0, c1, c2, c3;
unsigned char *in0 = in + 0;
unsigned char *in1 = in + idiv4;
unsigned char *in2 = in + idiv4*2;
unsigned char *in3 = in + idiv4*3;
unsigned char last_0 = 0, last_1 = in1[-1], last_2 = in2[-1], last_3 = in3[-1];
//unsigned char last_0 = 0, last_1 = 0, last_2 = 0, last_3 = 0;
unsigned char *in0_end = in1;
while (in0 < in0_end) {
F0[last_0][c0 = *in0++]++;
T0[last_0]++;
last_0 = c0;
F0[last_1][c1 = *in1++]++;
T1[last_1]++;
last_1 = c1;
F0[last_2][c2 = *in2++]++;
T2[last_2]++;
last_2 = c2;
F0[last_3][c3 = *in3++]++;
T3[last_3]++;
last_3 = c3;
}
while (in3 < in + in_size) {
F0[last_3][c3 = *in3++]++;
T3[last_3]++;
last_3 = c3;
}
for (i = 0; i < 256; i++) {
T0[i]+=T1[i]+T2[i]+T3[i];
}
}
unsigned char *rans_compress_O1_4x16(unsigned char *in, unsigned int in_size,
unsigned char *out, unsigned int *out_size) {
unsigned char *cp, *out_end;
unsigned int tab_size, rle_i, rle_j;
RansEncSymbol syms[256][256];
int bound = rans_compress_bound_4x16(in_size,1, NULL);
if (!out) {
*out_size = bound;
out = malloc(*out_size);
}
if (!out || bound > *out_size)
return NULL;
out_end = out + bound;
cp = out+4;
int F[256][256] = {{0}}, T[256+MAGIC] = {0}, i, j;
//memset(F, 0, 256*256*sizeof(int));
//memset(T, 0, 256*sizeof(int));
hist1_4(in, in_size, F, T);
F[0][in[1*(in_size>>2)]]++;
F[0][in[2*(in_size>>2)]]++;
F[0][in[3*(in_size>>2)]]++;
T[0]+=3;
// Normalise so T[i] == TOTFREQ
for (rle_i = i = 0; i < 256; i++) {
int t2, m, M;
unsigned int x;
if (T[i] == 0)
continue;
//uint64_t p = (TOTFREQ * TOTFREQ) / t;
double p = ((double)TOTFREQ_O1)/T[i];
for (t2 = m = M = j = 0; j < 256; j++) {
if (!F[i][j])
continue;
if (m < F[i][j])
m = F[i][j], M = j;
if ((F[i][j] *= p) <= 0)
F[i][j] = 1;
t2 += F[i][j];
}
//t2++;
int adjust = TOTFREQ_O1-t2;
if (adjust > 0) {
// Boost most common
F[i][M] += adjust;
} else if (adjust < 0) {
// Reduce highest and distribute remainder
if (F[i][M] > -adjust) {
F[i][M] += adjust;
} else {
adjust += F[i][M]-1;
F[i][M] = 1;
for (j = 0; adjust && j < 256; j++) {
if (F[i][j] < 2) continue;
int d = F[i][j] > -adjust;
int m = d ? adjust : 1-F[i][j];
F[i][j] += m;
adjust -= m;
}
}
}
// Store frequency table
// i
if (rle_i) {
rle_i--;
} else {
*cp++ = i;
// FIXME: could use order-0 statistics to observe which alphabet
// symbols are present and base RLE on that ordering instead.
if (i && T[i-1]) {
for(rle_i=i+1; rle_i<256 && T[rle_i]; rle_i++)
;
rle_i -= i+1;
*cp++ = rle_i;
}
}
int *F_i_ = F[i];
x = 0;
rle_j = 0;
for (j = 0; j < 256; j++) {
if (F_i_[j]) {
//fprintf(stderr, "F[%d][%d]=%d, x=%d\n", i, j, F_i_[j], x);
// j
if (rle_j) {
rle_j--;
} else {
*cp++ = j;
if (!rle_j && j && F_i_[j-1]) {
for(rle_j=j+1; rle_j<256 && F_i_[rle_j]; rle_j++)
;
rle_j -= j+1;
*cp++ = rle_j;
}
}
// F_i_[j]
if (F_i_[j]<128) {
*cp++ = F_i_[j];
} else {
*cp++ = 128 | (F_i_[j]>>8);
*cp++ = F_i_[j]&0xff;
}
RansEncSymbolInit(&syms[i][j], x, F_i_[j], TF_SHIFT_O1);
x += F_i_[j];
}
}
*cp++ = 0;
}
*cp++ = 0;
//write(2, out+4, cp-(out+4));
tab_size = cp - out;
assert(tab_size < 257*257*3);
RansState rans0, rans1, rans2, rans3;
RansEncInit(&rans0);
RansEncInit(&rans1);
RansEncInit(&rans2);
RansEncInit(&rans3);
uint8_t* ptr = out_end;
int isz4 = in_size>>2;