aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-09-06 17:56:57 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-09-06 17:59:45 +0200
commitd327c6b190900dcb0cb7cda9008eb4f9893a92d8 (patch)
tree48d6cbbe27e260b01001326637f9f9e8905c906a
parent81a708393d851b6b35c9bc3efe0e76ae696c07cc (diff)
downloadbusybox-w32-d327c6b190900dcb0cb7cda9008eb4f9893a92d8.tar.gz
busybox-w32-d327c6b190900dcb0cb7cda9008eb4f9893a92d8.tar.bz2
busybox-w32-d327c6b190900dcb0cb7cda9008eb4f9893a92d8.zip
gzip: code shrink
Converted a few 16-bit variables and small arrays to 32-bit. Stopped pulling desc->FOO members into temporary local variables in gen_bitlen(): on register-starved arches, this is a loss, temporaries go into stack slots. Sprinkled a few "const" on pointer arguments. function old new delta pack_gzip 742 745 +3 gen_codes 101 97 -4 build_tree 886 833 -53 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/2 up/down: 3/-57) Total: -54 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/gzip.c88
1 files changed, 44 insertions, 44 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index 3966a06b4..d9c730f13 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -507,7 +507,7 @@ static ALWAYS_INLINE void flush_outbuf_if_32bit_optimized(void)
507 * pointer, then initialize the crc shift register contents instead. 507 * pointer, then initialize the crc shift register contents instead.
508 * Return the current crc in either case. 508 * Return the current crc in either case.
509 */ 509 */
510static void updcrc(uch * s, unsigned n) 510static void updcrc(uch *s, unsigned n)
511{ 511{
512 G1.crc = crc32_block_endian0(G1.crc, s, n, global_crc32_table /*G1.crc_32_tab*/); 512 G1.crc = crc32_block_endian0(G1.crc, s, n, global_crc32_table /*G1.crc_32_tab*/);
513} 513}
@@ -610,7 +610,7 @@ static void bi_windup(void)
610 * Copy a stored block to the zip file, storing first the length and its 610 * Copy a stored block to the zip file, storing first the length and its
611 * one's complement if requested. 611 * one's complement if requested.
612 */ 612 */
613static void copy_block(char *buf, unsigned len, int header) 613static void copy_block(const char *buf, unsigned len, int header)
614{ 614{
615 bi_windup(); /* align on byte boundary */ 615 bi_windup(); /* align on byte boundary */
616 616
@@ -1010,7 +1010,8 @@ struct globals2 {
1010 tree_desc d_desc; 1010 tree_desc d_desc;
1011 tree_desc bl_desc; 1011 tree_desc bl_desc;
1012 1012
1013 ush bl_count[MAX_BITS + 1]; 1013 /* was "ush", but "unsigned" results in smaller code */
1014 unsigned bl_count[MAX_BITS + 1];
1014 1015
1015/* The lengths of the bit length codes are sent in order of decreasing 1016/* The lengths of the bit length codes are sent in order of decreasing
1016 * probability, to avoid transmitting the lengths for unused bit length codes. 1017 * probability, to avoid transmitting the lengths for unused bit length codes.
@@ -1121,7 +1122,7 @@ static void init_block(void)
1121 (tree[n].Freq < tree[m].Freq \ 1122 (tree[n].Freq < tree[m].Freq \
1122 || (tree[n].Freq == tree[m].Freq && G2.depth[n] <= G2.depth[m])) 1123 || (tree[n].Freq == tree[m].Freq && G2.depth[n] <= G2.depth[m]))
1123 1124
1124static void pqdownheap(ct_data * tree, int k) 1125static void pqdownheap(const ct_data *tree, int k)
1125{ 1126{
1126 int v = G2.heap[k]; 1127 int v = G2.heap[k];
1127 int j = k << 1; /* left son of k */ 1128 int j = k << 1; /* left son of k */
@@ -1155,22 +1156,15 @@ static void pqdownheap(ct_data * tree, int k)
1155 * The length opt_len is updated; static_len is also updated if stree is 1156 * The length opt_len is updated; static_len is also updated if stree is
1156 * not null. 1157 * not null.
1157 */ 1158 */
1158static void gen_bitlen(tree_desc * desc) 1159static void gen_bitlen(const tree_desc *desc)
1159{ 1160{
1160 ct_data *tree = desc->dyn_tree; 1161#define tree desc->dyn_tree
1161 const uint8_t *extra = desc->extra_bits; 1162 int h; /* heap index */
1162 int base = desc->extra_base; 1163 int n, m; /* iterate over the tree elements */
1163 int max_code = desc->max_code; 1164 int bits; /* bit length */
1164 int max_length = desc->max_length; 1165 int overflow; /* number of elements with bit length too large */
1165 ct_data *stree = desc->static_tree; 1166
1166 int h; /* heap index */ 1167 for (bits = 0; bits < ARRAY_SIZE(G2.bl_count); bits++)
1167 int n, m; /* iterate over the tree elements */
1168 int bits; /* bit length */
1169 int xbits; /* extra bits */
1170 ush f; /* frequency */
1171 int overflow = 0; /* number of elements with bit length too large */
1172
1173 for (bits = 0; bits <= MAX_BITS; bits++)
1174 G2.bl_count[bits] = 0; 1168 G2.bl_count[bits] = 0;
1175 1169
1176 /* In a first pass, compute the optimal bit lengths (which may 1170 /* In a first pass, compute the optimal bit lengths (which may
@@ -1178,28 +1172,32 @@ static void gen_bitlen(tree_desc * desc)
1178 */ 1172 */
1179 tree[G2.heap[G2.heap_max]].Len = 0; /* root of the heap */ 1173 tree[G2.heap[G2.heap_max]].Len = 0; /* root of the heap */
1180 1174
1175 overflow = 0;
1181 for (h = G2.heap_max + 1; h < HEAP_SIZE; h++) { 1176 for (h = G2.heap_max + 1; h < HEAP_SIZE; h++) {
1177 ulg f; /* frequency */
1178 int xbits; /* extra bits */
1179
1182 n = G2.heap[h]; 1180 n = G2.heap[h];
1183 bits = tree[tree[n].Dad].Len + 1; 1181 bits = tree[tree[n].Dad].Len + 1;
1184 if (bits > max_length) { 1182 if (bits > desc->max_length) {
1185 bits = max_length; 1183 bits = desc->max_length;
1186 overflow++; 1184 overflow++;
1187 } 1185 }
1188 tree[n].Len = (ush) bits; 1186 tree[n].Len = (ush) bits;
1189 /* We overwrite tree[n].Dad which is no longer needed */ 1187 /* We overwrite tree[n].Dad which is no longer needed */
1190 1188
1191 if (n > max_code) 1189 if (n > desc->max_code)
1192 continue; /* not a leaf node */ 1190 continue; /* not a leaf node */
1193 1191
1194 G2.bl_count[bits]++; 1192 G2.bl_count[bits]++;
1195 xbits = 0; 1193 xbits = 0;
1196 if (n >= base) 1194 if (n >= desc->extra_base)
1197 xbits = extra[n - base]; 1195 xbits = desc->extra_bits[n - desc->extra_base];
1198 f = tree[n].Freq; 1196 f = tree[n].Freq;
1199 G2.opt_len += (ulg) f *(bits + xbits); 1197 G2.opt_len += f * (bits + xbits);
1200 1198
1201 if (stree) 1199 if (desc->static_tree)
1202 G2.static_len += (ulg) f * (stree[n].Len + xbits); 1200 G2.static_len += f * (desc->static_tree[n].Len + xbits);
1203 } 1201 }
1204 if (overflow == 0) 1202 if (overflow == 0)
1205 return; 1203 return;
@@ -1209,14 +1207,14 @@ static void gen_bitlen(tree_desc * desc)
1209 1207
1210 /* Find the first bit length which could increase: */ 1208 /* Find the first bit length which could increase: */
1211 do { 1209 do {
1212 bits = max_length - 1; 1210 bits = desc->max_length - 1;
1213 while (G2.bl_count[bits] == 0) 1211 while (G2.bl_count[bits] == 0)
1214 bits--; 1212 bits--;
1215 G2.bl_count[bits]--; /* move one leaf down the tree */ 1213 G2.bl_count[bits]--; /* move one leaf down the tree */
1216 G2.bl_count[bits + 1] += 2; /* move one overflow item as its brother */ 1214 G2.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
1217 G2.bl_count[max_length]--; 1215 G2.bl_count[desc->max_length]--;
1218 /* The brother of the overflow item also moves one step up, 1216 /* The brother of the overflow item also moves one step up,
1219 * but this does not affect bl_count[max_length] 1217 * but this does not affect bl_count[desc->max_length]
1220 */ 1218 */
1221 overflow -= 2; 1219 overflow -= 2;
1222 } while (overflow > 0); 1220 } while (overflow > 0);
@@ -1226,11 +1224,11 @@ static void gen_bitlen(tree_desc * desc)
1226 * lengths instead of fixing only the wrong ones. This idea is taken 1224 * lengths instead of fixing only the wrong ones. This idea is taken
1227 * from 'ar' written by Haruhiko Okumura.) 1225 * from 'ar' written by Haruhiko Okumura.)
1228 */ 1226 */
1229 for (bits = max_length; bits != 0; bits--) { 1227 for (bits = desc->max_length; bits != 0; bits--) {
1230 n = G2.bl_count[bits]; 1228 n = G2.bl_count[bits];
1231 while (n != 0) { 1229 while (n != 0) {
1232 m = G2.heap[--h]; 1230 m = G2.heap[--h];
1233 if (m > max_code) 1231 if (m > desc->max_code)
1234 continue; 1232 continue;
1235 if (tree[m].Len != (unsigned) bits) { 1233 if (tree[m].Len != (unsigned) bits) {
1236 Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits)); 1234 Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits));
@@ -1240,6 +1238,7 @@ static void gen_bitlen(tree_desc * desc)
1240 n--; 1238 n--;
1241 } 1239 }
1242 } 1240 }
1241#undef tree
1243} 1242}
1244 1243
1245/* =========================================================================== 1244/* ===========================================================================
@@ -1250,12 +1249,13 @@ static void gen_bitlen(tree_desc * desc)
1250 * OUT assertion: the field code is set for all tree elements of non 1249 * OUT assertion: the field code is set for all tree elements of non
1251 * zero code length. 1250 * zero code length.
1252 */ 1251 */
1253static void gen_codes(ct_data * tree, int max_code) 1252static void gen_codes(ct_data *tree, int max_code)
1254{ 1253{
1255 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */ 1254 /* next_code[] and code used to be "ush", but "unsigned" results in smaller code */
1256 ush code = 0; /* running code value */ 1255 unsigned next_code[MAX_BITS + 1]; /* next code value for each bit length */
1257 int bits; /* bit index */ 1256 unsigned code = 0; /* running code value */
1258 int n; /* code index */ 1257 int bits; /* bit index */
1258 int n; /* code index */
1259 1259
1260 /* The distribution counts are first used to generate the code values 1260 /* The distribution counts are first used to generate the code values
1261 * without bit reversal. 1261 * without bit reversal.
@@ -1307,7 +1307,7 @@ do { \
1307 pqdownheap(tree, SMALLEST); \ 1307 pqdownheap(tree, SMALLEST); \
1308} while (0) 1308} while (0)
1309 1309
1310static void build_tree(tree_desc * desc) 1310static void build_tree(tree_desc *desc)
1311{ 1311{
1312 ct_data *tree = desc->dyn_tree; 1312 ct_data *tree = desc->dyn_tree;
1313 ct_data *stree = desc->static_tree; 1313 ct_data *stree = desc->static_tree;
@@ -1385,10 +1385,10 @@ static void build_tree(tree_desc * desc)
1385 /* At this point, the fields freq and dad are set. We can now 1385 /* At this point, the fields freq and dad are set. We can now
1386 * generate the bit lengths. 1386 * generate the bit lengths.
1387 */ 1387 */
1388 gen_bitlen((tree_desc *) desc); 1388 gen_bitlen(desc);
1389 1389
1390 /* The field len is now set, we can generate the bit codes */ 1390 /* The field len is now set, we can generate the bit codes */
1391 gen_codes((ct_data *) tree, max_code); 1391 gen_codes(tree, max_code);
1392} 1392}
1393 1393
1394/* =========================================================================== 1394/* ===========================================================================
@@ -1397,7 +1397,7 @@ static void build_tree(tree_desc * desc)
1397 * counts. (The contribution of the bit length codes will be added later 1397 * counts. (The contribution of the bit length codes will be added later
1398 * during the construction of bl_tree.) 1398 * during the construction of bl_tree.)
1399 */ 1399 */
1400static void scan_tree(ct_data * tree, int max_code) 1400static void scan_tree(ct_data *tree, int max_code)
1401{ 1401{
1402 int n; /* iterates over all tree elements */ 1402 int n; /* iterates over all tree elements */
1403 int prevlen = -1; /* last emitted length */ 1403 int prevlen = -1; /* last emitted length */
@@ -1449,7 +1449,7 @@ static void scan_tree(ct_data * tree, int max_code)
1449 * Send a literal or distance tree in compressed form, using the codes in 1449 * Send a literal or distance tree in compressed form, using the codes in
1450 * bl_tree. 1450 * bl_tree.
1451 */ 1451 */
1452static void send_tree(ct_data * tree, int max_code) 1452static void send_tree(const ct_data *tree, int max_code)
1453{ 1453{
1454 int n; /* iterates over all tree elements */ 1454 int n; /* iterates over all tree elements */
1455 int prevlen = -1; /* last emitted length */ 1455 int prevlen = -1; /* last emitted length */
@@ -1625,7 +1625,7 @@ static int ct_tally(int dist, int lc)
1625/* =========================================================================== 1625/* ===========================================================================
1626 * Send the block data compressed using the given Huffman trees 1626 * Send the block data compressed using the given Huffman trees
1627 */ 1627 */
1628static void compress_block(ct_data * ltree, ct_data * dtree) 1628static void compress_block(const ct_data *ltree, const ct_data *dtree)
1629{ 1629{
1630 unsigned dist; /* distance of matched string */ 1630 unsigned dist; /* distance of matched string */
1631 int lc; /* match length or unmatched char (if dist == 0) */ 1631 int lc; /* match length or unmatched char (if dist == 0) */
@@ -1675,7 +1675,7 @@ static void compress_block(ct_data * ltree, ct_data * dtree)
1675 * trees or store, and output the encoded block to the zip file. This function 1675 * trees or store, and output the encoded block to the zip file. This function
1676 * returns the total compressed length for the file so far. 1676 * returns the total compressed length for the file so far.
1677 */ 1677 */
1678static void flush_block(char *buf, ulg stored_len, int eof) 1678static void flush_block(const char *buf, ulg stored_len, int eof)
1679{ 1679{
1680 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 1680 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
1681 int max_blindex; /* index of last bit length code of non zero freq */ 1681 int max_blindex; /* index of last bit length code of non zero freq */