aboutsummaryrefslogtreecommitdiff
path: root/archival/bz/compress.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-10-14 00:43:01 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-10-14 00:43:01 +0000
commitef3aabe906a351f0bdf97199b4f38a2c6b54eaa5 (patch)
tree7ce8c73be864396eb656c2ef59ef797824a07942 /archival/bz/compress.c
parent77f1ec1b9bf100e6c10aa0856c7156e321511785 (diff)
downloadbusybox-w32-ef3aabe906a351f0bdf97199b4f38a2c6b54eaa5.tar.gz
busybox-w32-ef3aabe906a351f0bdf97199b4f38a2c6b54eaa5.tar.bz2
busybox-w32-ef3aabe906a351f0bdf97199b4f38a2c6b54eaa5.zip
bzip2: size reduction, to just below 9k.
Diffstat (limited to 'archival/bz/compress.c')
-rw-r--r--archival/bz/compress.c154
1 files changed, 80 insertions, 74 deletions
diff --git a/archival/bz/compress.c b/archival/bz/compress.c
index 54426dcc7..4bd364ee3 100644
--- a/archival/bz/compress.c
+++ b/archival/bz/compress.c
@@ -50,7 +50,7 @@ static NOINLINE
50void bsFinishWrite(EState* s) 50void bsFinishWrite(EState* s)
51{ 51{
52 while (s->bsLive > 0) { 52 while (s->bsLive > 0) {
53 s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24); 53 s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24);
54 s->numZ++; 54 s->numZ++;
55 s->bsBuff <<= 8; 55 s->bsBuff <<= 8;
56 s->bsLive -= 8; 56 s->bsLive -= 8;
@@ -60,13 +60,14 @@ void bsFinishWrite(EState* s)
60 60
61/*---------------------------------------------------*/ 61/*---------------------------------------------------*/
62static 62static
63/* Forced inlining results in +600 bytes code, 63/* Helps only on level 5, on other levels hurts. ? */
64 * 2% faster compression. Not worth it. */ 64#if CONFIG_BZIP2_FEATURE_SPEED >= 5
65/*ALWAYS_INLINE*/ 65ALWAYS_INLINE
66#endif
66void bsW(EState* s, int32_t n, uint32_t v) 67void bsW(EState* s, int32_t n, uint32_t v)
67{ 68{
68 while (s->bsLive >= 8) { 69 while (s->bsLive >= 8) {
69 s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24); 70 s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24);
70 s->numZ++; 71 s->numZ++;
71 s->bsBuff <<= 8; 72 s->bsBuff <<= 8;
72 s->bsLive -= 8; 73 s->bsLive -= 8;
@@ -78,7 +79,7 @@ void bsW(EState* s, int32_t n, uint32_t v)
78 79
79/*---------------------------------------------------*/ 80/*---------------------------------------------------*/
80static 81static
81void bsPutU32(EState* s, uint32_t u) 82void bsPutU32(EState* s, unsigned u)
82{ 83{
83 bsW(s, 8, (u >> 24) & 0xff); 84 bsW(s, 8, (u >> 24) & 0xff);
84 bsW(s, 8, (u >> 16) & 0xff); 85 bsW(s, 8, (u >> 16) & 0xff);
@@ -89,9 +90,10 @@ void bsPutU32(EState* s, uint32_t u)
89 90
90/*---------------------------------------------------*/ 91/*---------------------------------------------------*/
91static 92static
92void bsPutUChar(EState* s, UChar c) 93void bsPutU16(EState* s, unsigned u)
93{ 94{
94 bsW(s, 8, (uint32_t)c); 95 bsW(s, 8, (u >> 8) & 0xff);
96 bsW(s, 8, u & 0xff);
95} 97}
96 98
97 99
@@ -103,7 +105,7 @@ void bsPutUChar(EState* s, UChar c)
103static 105static
104void makeMaps_e(EState* s) 106void makeMaps_e(EState* s)
105{ 107{
106 int32_t i; 108 int i;
107 s->nInUse = 0; 109 s->nInUse = 0;
108 for (i = 0; i < 256; i++) { 110 for (i = 0; i < 256; i++) {
109 if (s->inUse[i]) { 111 if (s->inUse[i]) {
@@ -118,7 +120,7 @@ void makeMaps_e(EState* s)
118static NOINLINE 120static NOINLINE
119void generateMTFValues(EState* s) 121void generateMTFValues(EState* s)
120{ 122{
121 UChar yy[256]; 123 uint8_t yy[256];
122 int32_t i, j; 124 int32_t i, j;
123 int32_t zPend; 125 int32_t zPend;
124 int32_t wr; 126 int32_t wr;
@@ -128,7 +130,7 @@ void generateMTFValues(EState* s)
128 * After sorting (eg, here), 130 * After sorting (eg, here),
129 * s->arr1[0 .. s->nblock-1] holds sorted order, 131 * s->arr1[0 .. s->nblock-1] holds sorted order,
130 * and 132 * and
131 * ((UChar*)s->arr2)[0 .. s->nblock-1] 133 * ((uint8_t*)s->arr2)[0 .. s->nblock-1]
132 * holds the original block data. 134 * holds the original block data.
133 * 135 *
134 * The first thing to do is generate the MTF values, 136 * The first thing to do is generate the MTF values,
@@ -140,14 +142,14 @@ void generateMTFValues(EState* s)
140 * 142 *
141 * The final compressed bitstream is generated into the 143 * The final compressed bitstream is generated into the
142 * area starting at 144 * area starting at
143 * (UChar*) (&((UChar*)s->arr2)[s->nblock]) 145 * &((uint8_t*)s->arr2)[s->nblock]
144 * 146 *
145 * These storage aliases are set up in bzCompressInit(), 147 * These storage aliases are set up in bzCompressInit(),
146 * except for the last one, which is arranged in 148 * except for the last one, which is arranged in
147 * compressBlock(). 149 * compressBlock().
148 */ 150 */
149 uint32_t* ptr = s->ptr; 151 uint32_t* ptr = s->ptr;
150 UChar* block = s->block; 152 uint8_t* block = s->block;
151 uint16_t* mtfv = s->mtfv; 153 uint16_t* mtfv = s->mtfv;
152 154
153 makeMaps_e(s); 155 makeMaps_e(s);
@@ -159,12 +161,12 @@ void generateMTFValues(EState* s)
159 wr = 0; 161 wr = 0;
160 zPend = 0; 162 zPend = 0;
161 for (i = 0; i < s->nInUse; i++) 163 for (i = 0; i < s->nInUse; i++)
162 yy[i] = (UChar) i; 164 yy[i] = (uint8_t) i;
163 165
164 for (i = 0; i < s->nblock; i++) { 166 for (i = 0; i < s->nblock; i++) {
165 UChar ll_i; 167 uint8_t ll_i;
166 AssertD(wr <= i, "generateMTFValues(1)"); 168 AssertD(wr <= i, "generateMTFValues(1)");
167 j = ptr[i]-1; 169 j = ptr[i] - 1;
168 if (j < 0) 170 if (j < 0)
169 j += s->nblock; 171 j += s->nblock;
170 ll_i = s->unseqToSeq[block[j]]; 172 ll_i = s->unseqToSeq[block[j]];
@@ -189,15 +191,15 @@ void generateMTFValues(EState* s)
189 zPend = 0; 191 zPend = 0;
190 } 192 }
191 { 193 {
192 register UChar rtmp; 194 register uint8_t rtmp;
193 register UChar* ryy_j; 195 register uint8_t* ryy_j;
194 register UChar rll_i; 196 register uint8_t rll_i;
195 rtmp = yy[1]; 197 rtmp = yy[1];
196 yy[1] = yy[0]; 198 yy[1] = yy[0];
197 ryy_j = &(yy[1]); 199 ryy_j = &(yy[1]);
198 rll_i = ll_i; 200 rll_i = ll_i;
199 while (rll_i != rtmp) { 201 while (rll_i != rtmp) {
200 register UChar rtmp2; 202 register uint8_t rtmp2;
201 ryy_j++; 203 ryy_j++;
202 rtmp2 = rtmp; 204 rtmp2 = rtmp;
203 rtmp = *ryy_j; 205 rtmp = *ryy_j;
@@ -250,7 +252,7 @@ void sendMTFValues(EState* s)
250 int32_t nGroups, nBytes; 252 int32_t nGroups, nBytes;
251 253
252 /* 254 /*
253 * UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; 255 * uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
254 * is a global since the decoder also needs it. 256 * is a global since the decoder also needs it.
255 * 257 *
256 * int32_t code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; 258 * int32_t code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
@@ -295,7 +297,7 @@ void sendMTFValues(EState* s)
295 297
296 if (ge > gs 298 if (ge > gs
297 && nPart != nGroups && nPart != 1 299 && nPart != nGroups && nPart != 1
298 && ((nGroups-nPart) % 2 == 1) 300 && ((nGroups - nPart) % 2 == 1)
299 ) { 301 ) {
300 aFreq -= s->mtfFreq[ge]; 302 aFreq -= s->mtfFreq[ge];
301 ge--; 303 ge--;
@@ -324,7 +326,7 @@ void sendMTFValues(EState* s)
324 for (v = 0; v < alphaSize; v++) 326 for (v = 0; v < alphaSize; v++)
325 s->rfreq[t][v] = 0; 327 s->rfreq[t][v] = 0;
326 328
327#ifdef FAST_GROUP6 329#if CONFIG_BZIP2_FEATURE_SPEED >= 5
328 /* 330 /*
329 * Set up an auxiliary length table which is used to fast-track 331 * Set up an auxiliary length table which is used to fast-track
330 * the common case (nGroups == 6). 332 * the common case (nGroups == 6).
@@ -337,7 +339,6 @@ void sendMTFValues(EState* s)
337 } 339 }
338 } 340 }
339#endif 341#endif
340
341 nSelectors = 0; 342 nSelectors = 0;
342 totc = 0; 343 totc = 0;
343 gs = 0; 344 gs = 0;
@@ -355,7 +356,7 @@ void sendMTFValues(EState* s)
355 */ 356 */
356 for (t = 0; t < nGroups; t++) 357 for (t = 0; t < nGroups; t++)
357 cost[t] = 0; 358 cost[t] = 0;
358#ifdef FAST_GROUP6 359#if CONFIG_BZIP2_FEATURE_SPEED >= 5
359 if (nGroups == 6 && 50 == ge-gs+1) { 360 if (nGroups == 6 && 50 == ge-gs+1) {
360 /*--- fast track the common case ---*/ 361 /*--- fast track the common case ---*/
361 register uint32_t cost01, cost23, cost45; 362 register uint32_t cost01, cost23, cost45;
@@ -395,11 +396,11 @@ void sendMTFValues(EState* s)
395 * Find the coding table which is best for this group, 396 * Find the coding table which is best for this group,
396 * and record its identity in the selector table. 397 * and record its identity in the selector table.
397 */ 398 */
398 bc = 999999999; 399 /*bc = 999999999;*/
399 bt = -1; 400 /*bt = -1;*/
400 //bc = cost[0]; 401 bc = cost[0];
401 //bt = 0; 402 bt = 0;
402 for (t = 0; t < nGroups; t++) { 403 for (t = 1 /*0*/; t < nGroups; t++) {
403 if (cost[t] < bc) { 404 if (cost[t] < bc) {
404 bc = cost[t]; 405 bc = cost[t];
405 bt = t; 406 bt = t;
@@ -413,8 +414,8 @@ void sendMTFValues(EState* s)
413 /* 414 /*
414 * Increment the symbol frequencies for the selected table. 415 * Increment the symbol frequencies for the selected table.
415 */ 416 */
416/* ~0.5% faster compress. +800 bytes */ 417/* 1% faster compress. +800 bytes */
417#if 0 418#if CONFIG_BZIP2_FEATURE_SPEED >= 4
418 if (nGroups == 6 && 50 == ge-gs+1) { 419 if (nGroups == 6 && 50 == ge-gs+1) {
419 /*--- fast track the common case ---*/ 420 /*--- fast track the common case ---*/
420#define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++ 421#define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++
@@ -429,7 +430,7 @@ void sendMTFValues(EState* s)
429 BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44); 430 BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
430 BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49); 431 BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
431#undef BZ_ITUR 432#undef BZ_ITUR
432 gs = ge+1; 433 gs = ge + 1;
433 } else 434 } else
434#endif 435#endif
435 { 436 {
@@ -438,7 +439,7 @@ void sendMTFValues(EState* s)
438 s->rfreq[bt][mtfv[gs]]++; 439 s->rfreq[bt][mtfv[gs]]++;
439 gs++; 440 gs++;
440 } 441 }
441 /* already is: gs = ge+1; */ 442 /* already is: gs = ge + 1; */
442 } 443 }
443 } 444 }
444 445
@@ -456,7 +457,7 @@ void sendMTFValues(EState* s)
456 457
457 /*--- Compute MTF values for the selectors. ---*/ 458 /*--- Compute MTF values for the selectors. ---*/
458 { 459 {
459 UChar pos[BZ_N_GROUPS], ll_i, tmp2, tmp; 460 uint8_t pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
460 461
461 for (i = 0; i < nGroups; i++) 462 for (i = 0; i < nGroups; i++)
462 pos[i] = i; 463 pos[i] = i;
@@ -490,31 +491,34 @@ void sendMTFValues(EState* s)
490 491
491 /*--- Transmit the mapping table. ---*/ 492 /*--- Transmit the mapping table. ---*/
492 { 493 {
493 Bool inUse16[16]; 494 /* bbox: optimized a bit more than in bzip2 */
495 int inUse16 = 0;
494 for (i = 0; i < 16; i++) { 496 for (i = 0; i < 16; i++) {
495 inUse16[i] = False; 497 if (sizeof(long) <= 4) {
496 for (j = 0; j < 16; j++) 498 inUse16 = inUse16*2 +
497 if (s->inUse[i * 16 + j]) 499 ((*(uint32_t*)&(s->inUse[i * 16 + 0])
498 inUse16[i] = True; 500 | *(uint32_t*)&(s->inUse[i * 16 + 4])
501 | *(uint32_t*)&(s->inUse[i * 16 + 8])
502 | *(uint32_t*)&(s->inUse[i * 16 + 12])) != 0);
503 } else { /* Our CPU can do better */
504 inUse16 = inUse16*2 +
505 ((*(uint64_t*)&(s->inUse[i * 16 + 0])
506 | *(uint64_t*)&(s->inUse[i * 16 + 8])) != 0);
507 }
499 } 508 }
500 509
501 nBytes = s->numZ; 510 nBytes = s->numZ;
502 for (i = 0; i < 16; i++) { 511 bsW(s, 16, inUse16);
503 if (inUse16[i])
504 bsW(s, 1, 1);
505 else
506 bsW(s, 1, 0);
507 }
508 512
513 inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */
509 for (i = 0; i < 16; i++) { 514 for (i = 0; i < 16; i++) {
510 if (inUse16[i]) { 515 if (inUse16 < 0) {
511 for (j = 0; j < 16; j++) { 516 unsigned v16 = 0;
512 if (s->inUse[i * 16 + j]) 517 for (j = 0; j < 16; j++)
513 bsW(s, 1, 1); 518 v16 = v16*2 + s->inUse[i * 16 + j];
514 else 519 bsW(s, 16, v16);
515 bsW(s, 1, 0);
516 }
517 } 520 }
521 inUse16 <<= 1;
518 } 522 }
519 } 523 }
520 524
@@ -558,7 +562,7 @@ void sendMTFValues(EState* s)
558 if (nGroups == 6 && 50 == ge-gs+1) { 562 if (nGroups == 6 && 50 == ge-gs+1) {
559 /*--- fast track the common case ---*/ 563 /*--- fast track the common case ---*/
560 uint16_t mtfv_i; 564 uint16_t mtfv_i;
561 UChar* s_len_sel_selCtr = &(s->len[s->selector[selCtr]][0]); 565 uint8_t* s_len_sel_selCtr = &(s->len[s->selector[selCtr]][0]);
562 int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]); 566 int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]);
563#define BZ_ITAH(nn) \ 567#define BZ_ITAH(nn) \
564 mtfv_i = mtfv[gs+(nn)]; \ 568 mtfv_i = mtfv[gs+(nn)]; \
@@ -580,7 +584,7 @@ void sendMTFValues(EState* s)
580 { 584 {
581 /*--- slow version which correctly handles all situations ---*/ 585 /*--- slow version which correctly handles all situations ---*/
582 /* code is bit bigger, but moves multiply out of the loop */ 586 /* code is bit bigger, but moves multiply out of the loop */
583 UChar* s_len_sel_selCtr = &(s->len [s->selector[selCtr]][0]); 587 uint8_t* s_len_sel_selCtr = &(s->len [s->selector[selCtr]][0]);
584 int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]); 588 int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]);
585 while (gs <= ge) { 589 while (gs <= ge) {
586 bsW(s, 590 bsW(s,
@@ -599,7 +603,7 @@ void sendMTFValues(EState* s)
599 603
600/*---------------------------------------------------*/ 604/*---------------------------------------------------*/
601static 605static
602void BZ2_compressBlock(EState* s, Bool is_last_block) 606void BZ2_compressBlock(EState* s, int is_last_block)
603{ 607{
604 if (s->nblock > 0) { 608 if (s->nblock > 0) {
605 BZ_FINALISE_CRC(s->blockCRC); 609 BZ_FINALISE_CRC(s->blockCRC);
@@ -611,26 +615,27 @@ void BZ2_compressBlock(EState* s, Bool is_last_block)
611 BZ2_blockSort(s); 615 BZ2_blockSort(s);
612 } 616 }
613 617
614 s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]); 618 s->zbits = &((uint8_t*)s->arr2)[s->nblock];
615 619
616 /*-- If this is the first block, create the stream header. --*/ 620 /*-- If this is the first block, create the stream header. --*/
617 if (s->blockNo == 1) { 621 if (s->blockNo == 1) {
618 BZ2_bsInitWrite(s); 622 BZ2_bsInitWrite(s);
619 /*bsPutUChar(s, BZ_HDR_B);*/ 623 /*bsPutU8(s, BZ_HDR_B);*/
620 /*bsPutUChar(s, BZ_HDR_Z);*/ 624 /*bsPutU8(s, BZ_HDR_Z);*/
621 /*bsPutUChar(s, BZ_HDR_h);*/ 625 /*bsPutU8(s, BZ_HDR_h);*/
622 /*bsPutUChar(s, (UChar)(BZ_HDR_0 + s->blockSize100k));*/ 626 /*bsPutU8(s, BZ_HDR_0 + s->blockSize100k);*/
623 bsPutU32(s, BZ_HDR_BZh0 + s->blockSize100k); 627 bsPutU32(s, BZ_HDR_BZh0 + s->blockSize100k);
624 } 628 }
625 629
626 if (s->nblock > 0) { 630 if (s->nblock > 0) {
627 /*bsPutUChar(s, 0x31);*/ 631 /*bsPutU8(s, 0x31);*/
628 /*bsPutUChar(s, 0x41);*/ 632 /*bsPutU8(s, 0x41);*/
629 /*bsPutUChar(s, 0x59);*/ 633 /*bsPutU8(s, 0x59);*/
630 /*bsPutUChar(s, 0x26);*/ 634 /*bsPutU8(s, 0x26);*/
631 bsPutU32(s, 0x31415926); 635 bsPutU32(s, 0x31415926);
632 bsPutUChar(s, 0x53); 636 /*bsPutU8(s, 0x53);*/
633 bsPutUChar(s, 0x59); 637 /*bsPutU8(s, 0x59);*/
638 bsPutU16(s, 0x5359);
634 639
635 /*-- Now the block's CRC, so it is in a known place. --*/ 640 /*-- Now the block's CRC, so it is in a known place. --*/
636 bsPutU32(s, s->blockCRC); 641 bsPutU32(s, s->blockCRC);
@@ -653,13 +658,14 @@ void BZ2_compressBlock(EState* s, Bool is_last_block)
653 658
654 /*-- If this is the last block, add the stream trailer. --*/ 659 /*-- If this is the last block, add the stream trailer. --*/
655 if (is_last_block) { 660 if (is_last_block) {
656 /*bsPutUChar(s, 0x17);*/ 661 /*bsPutU8(s, 0x17);*/
657 /*bsPutUChar(s, 0x72);*/ 662 /*bsPutU8(s, 0x72);*/
658 /*bsPutUChar(s, 0x45);*/ 663 /*bsPutU8(s, 0x45);*/
659 /*bsPutUChar(s, 0x38);*/ 664 /*bsPutU8(s, 0x38);*/
660 bsPutU32(s, 0x17724538); 665 bsPutU32(s, 0x17724538);
661 bsPutUChar(s, 0x50); 666 /*bsPutU8(s, 0x50);*/
662 bsPutUChar(s, 0x90); 667 /*bsPutU8(s, 0x90);*/
668 bsPutU16(s, 0x5090);
663 bsPutU32(s, s->combinedCRC); 669 bsPutU32(s, s->combinedCRC);
664 bsFinishWrite(s); 670 bsFinishWrite(s);
665 } 671 }