From 1fa2b732b7b6c1539995fa40605d9bcf807ac357 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Fri, 12 Dec 2025 04:58:15 -0500 Subject: alf: Cleanup and commentary. --- src/crunch.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/crunch.c') diff --git a/src/crunch.c b/src/crunch.c index f572192..2755248 100644 --- a/src/crunch.c +++ b/src/crunch.c @@ -30,6 +30,7 @@ unsigned int input_len, output_len, out_bitpos; short tokens[MAX_TOKENS][256]; int token_bits; +int shiftamt; /* precalculated MAX_BITS - token_bits */ int max_token; int curr_token = INIT_TOKEN; int in_pos; @@ -121,6 +122,9 @@ void init_table(void) { token_bits = INITIAL_BITS; max_token = 1 << INITIAL_BITS; curr_token = INIT_TOKEN; + + /* precalculated for store_token() */ + shiftamt = MAX_BITS - token_bits; } void check_output_len(void) { @@ -136,9 +140,9 @@ void inc_output_len(void) { output_buf[output_len] = 0; } -#ifdef ALF_ENDIAN_OK +#if !defined(APPEND_BIT) && defined ALF_ENDIAN_OK -/* This is 20% faster, but it requires knowing the endianness of +/* This is 25% 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; @@ -154,11 +158,17 @@ void store_token(int tok) { putchar('\n'); } - tok <<= (MAX_BITS - token_bits); - ui2bytes.ui = tok << (12 - out_bitpos); + /* align token so, no matter what its size (9 thru 12), + its top bit is at bit 23 of a 32-bit int. */ + ui2bytes.ui = tok << ((12 - out_bitpos) + shiftamt); + + /* always append 12 bits (it's quicker to do that than have + conditionals to decide whether the last byte is needed) */ output_buf[output_len] |= ui2bytes.bytes[HIBYTE]; output_buf[output_len + 1] = ui2bytes.bytes[MIDBYTE]; output_buf[output_len + 2] = ui2bytes.bytes[LOBYTE]; + + /* update based on actual token size */ out_bitpos += token_bits; output_len += out_bitpos / 8; out_bitpos %= 8; @@ -227,11 +237,12 @@ void make_token(short tok, u8 chr) { dump_tokens(); } store_token(TOK_RESET); /* stored at the *old* token size! */ - token_bits = INITIAL_BITS; init_table(); return; /* since we're starting over, *don't* make a token */ } else { token_bits++; + /* precalculated for store_token() */ + shiftamt--; if(opt_verbose > 1) { printf("token_bits increased to %d\n", token_bits); } -- cgit v1.2.3