From: Paul Eggert Date: Wed, 19 Feb 2025 04:09:10 +0000 (-0800) Subject: cksum: minor crctab generation cleanups X-Git-Tag: v9.7~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ac924f31910168512dc93eceb55d0e98e1f0a97;p=thirdparty%2Fcoreutils.git cksum: minor crctab generation cleanups * src/cksum.c [CRCTAB]: Include only config.h and stdio.h, to simplify the crctab-generating code. [!CRCTAB]: Do not include stdint.h or stdio.h, as cksum.h does it now. (BIT, r, crc_remainder, main) [CRCTAB]: Use unsigned int, not uint_fast32_t or uint32_t, as this is good enough for GNU where unsigned int is guaranteed to be at least 32 bits, and this way we needn’t worry about mismatches between %08x formats and uint_fast32_t. (main) [CRCTAB]: Prefer more-local decls. Do not output unnecessary directives to include stdint.h or stdio.h. No need for ‘return EXIT_SUCCESS;’ nowadays. * src/crctab.c: Regenerate. --- diff --git a/src/cksum.c b/src/cksum.c index 17999120de..3381125bdb 100644 --- a/src/cksum.c +++ b/src/cksum.c @@ -34,20 +34,11 @@ #include -#include -#include -#include -#include -#include "system.h" - -#ifdef USE_VMULL_CRC32 -# include -# include -#endif - #ifdef CRCTAB -# define BIT(x) ((uint_fast32_t) 1 << (x)) +# include + +# define BIT(x) (1u << (x)) # define SBIT BIT (31) /* The generating polynomial is @@ -61,7 +52,7 @@ | BIT (11) | BIT (10) | BIT (8) | BIT (7) | BIT (5) \ | BIT (4) | BIT (2) | BIT (1) | BIT (0)) -static uint_fast32_t r[8]; +static unsigned int r[8]; static void fill_r (void) @@ -71,10 +62,10 @@ fill_r (void) r[i] = (r[i - 1] << 1) ^ ((r[i - 1] & SBIT) ? GEN : 0); } -static uint_fast32_t +static unsigned int crc_remainder (int m) { - uint_fast32_t rem = 0; + unsigned int rem = 0; for (int i = 0; i < 8; i++) if (BIT (i) & m) @@ -86,15 +77,12 @@ crc_remainder (int m) int main (void) { - int i; - static uint_fast32_t crctab[8][256]; + static unsigned int crctab[8][256]; fill_r (); - for (i = 0; i < 256; i++) - { - crctab[0][i] = crc_remainder (i); - } + for (int i = 0; i < 256; i++) + crctab[0][i] = crc_remainder (i); /* CRC(0x11 0x22 0x33 0x44) is equal to @@ -103,28 +91,26 @@ main (void) We precompute the CRC values for the offset values into separate CRC tables. We can then use them to speed up CRC calculation by processing multiple bytes at the time. */ - for (i = 0; i < 256; i++) + for (int i = 0; i < 256; i++) { - uint32_t crc = 0; + unsigned int crc = 0; - crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ (i & 0xFF)) & 0xFF]; - for (idx_t offset = 1; offset < 8; offset++) + crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ i) & 0xFF]; + for (int offset = 1; offset < 8; offset++) { - crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ 0x00) & 0xFF]; - crctab[offset][i] = crc; + crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ 0) & 0xFF]; + crctab[offset][i] = crc & 0xFFFFFFFF; } } printf ("#include \n"); - printf ("#include \n"); - printf ("#include \n"); printf ("#include \"cksum.h\"\n"); printf ("\n"); printf ("uint_fast32_t const crctab[8][256] = {\n"); for (int y = 0; y < 8; y++) { printf ("{\n 0x%08x", crctab[y][0]); - for (i = 0; i < 51; i++) + for (int i = 0; i < 51; i++) { printf (",\n 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x", crctab[y][i * 5 + 1], crctab[y][i * 5 + 2], @@ -134,12 +120,20 @@ main (void) printf ("\n},\n"); } printf ("};\n"); - return EXIT_SUCCESS; } #else /* !CRCTAB */ # include "cksum.h" +# include +# include +# include "system.h" + +# ifdef USE_VMULL_CRC32 +# include +# include +# endif + # include "crc.h" /* Number of bytes to read at once. */ diff --git a/src/crctab.c b/src/crctab.c index bf5d884bf7..b8a4d88863 100644 --- a/src/crctab.c +++ b/src/crctab.c @@ -1,6 +1,4 @@ #include -#include -#include #include "cksum.h" uint_fast32_t const crctab[8][256] = {