diff options
| author | B. Watson <urchlay@slackware.uk> | 2025-12-11 17:14:44 -0500 |
|---|---|---|
| committer | B. Watson <urchlay@slackware.uk> | 2025-12-11 17:15:26 -0500 |
| commit | dec390744fe3466cede66d3b1a0209ca0c6a3df8 (patch) | |
| tree | 94c84c358476ad4d7fe152082ffbee5a497670e9 /src/crunch.c | |
| parent | a2366b1568bf782ac70c1feca26a097e83dcb7a7 (diff) | |
| download | alftools-dec390744fe3466cede66d3b1a0209ca0c6a3df8.tar.gz | |
crunch.c: Speed up by 20%, if endianness can be determined at compile time.
Diffstat (limited to 'src/crunch.c')
| -rw-r--r-- | src/crunch.c | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/crunch.c b/src/crunch.c index cb1ce5f..82cc095 100644 --- a/src/crunch.c +++ b/src/crunch.c @@ -9,6 +9,7 @@ #include "u816.h" #include "crunch.h" #include "self.h" +#include "bytorder.h" #define INITIAL_BITS 9 #define MAX_BITS 12 @@ -122,14 +123,52 @@ void init_table(void) { curr_token = INIT_TOKEN; } -void inc_output_len(void) { - if(++output_len == MAX_INPUT_SIZE) { +void check_output_len(void) { + if(output_len >= MAX_INPUT_SIZE) { fprintf(stderr, "%s: fatal: compressed file would be >16MB.\n", self); exit(1); } +} + +void inc_output_len(void) { + output_len++; + check_output_len(); output_buf[output_len] = 0; } +#ifdef ALF_ENDIAN_OK + +/* This is 20% faster, but it requires knowing the endianness of + the platform at compile time. See bytorder.h for gory details. */ +union { unsigned int ui; u8 bytes[4]; } ui2bytes; + +void store_token(int tok) { + if(opt_verbose > 1) { + printf("<%d >%d:%d #%d", in_pos, output_len, out_bitpos, tok); + if(tok == TOK_RESET) + fputs(" RESET", stdout); + else if(tok == TOK_END) + fputs(" END", stdout); + else if(tok < 256) + printf(" %s", fmt_chr(tok)); + putchar('\n'); + } + + tok <<= (MAX_BITS - token_bits); + ui2bytes.ui = tok << (12 - out_bitpos); + output_buf[output_len] |= ui2bytes.bytes[HIBYTE]; + output_buf[output_len + 1] = ui2bytes.bytes[MIDBYTE]; + output_buf[output_len + 2] = ui2bytes.bytes[LOBYTE]; + out_bitpos += token_bits; + output_len += out_bitpos / 8; + out_bitpos %= 8; + + check_output_len(); +} + +#else + +/* Slower but portable. */ void append_bit(int bit) { output_buf[output_len] |= (bit << (7 - out_bitpos)); out_bitpos++; @@ -157,6 +196,7 @@ void store_token(int tok) { append_bit(tok & mask ? 1 : 0); } } +#endif short match_token(void) { short t, nt; @@ -211,6 +251,7 @@ void crunch(void) { init_table(); out_bitpos = 0; in_pos = 0; + output_buf[output_len] = 0; /* just in case */ /* 0-byte input files don't get a TOK_RESET */ if(!input_len) { |
