File: crcgen.c

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (44 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download | duplicates (29)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <inttypes.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    /* Polynomial in bit-reversed notation */
    uint64_t poly;
    uint64_t crctab[256], v;
    int i, j;

    poly = strtoumax(argv[1], NULL, 0);

    printf("/* C */\n");
    printf("static const uint64_t crc64_tab[256] = {\n");
    for (i = 0; i < 256; i++) {
	v = i;
	for (j = 0; j < 8; j++)
	    v = (v >> 1) ^ ((v & 1) ? poly : 0);
	crctab[i] = v;
    }

    for (i = 0; i < 256; i += 2) {
	printf("    /* %02x */ UINT64_C(0x%016"PRIx64"), "
	       "UINT64_C(0x%016"PRIx64")%s\n",
	       i, crctab[i], crctab[i+1], (i == 254) ? "" : ",");
    }
    printf("};\n\n");

    printf("# perl\n");
    printf("@crc64_tab = (\n");
    for (i = 0; i < 256; i += 2) {
	printf("    [0x%08"PRIx32", 0x%08"PRIx32"], "
	       "[0x%08"PRIx32", 0x%08"PRIx32"]%-1s    # %02x\n",
	       (uint32_t)(crctab[i] >> 32),
	       (uint32_t)(crctab[i]),
	       (uint32_t)(crctab[i+1] >> 32),
	       (uint32_t)(crctab[i+1]),
	       (i == 254) ? "" : ",",
	       i);
    }
    printf(");\n");

    return 0;
}