universal_crc

Universal_crc is a utility for generating optimized ANSI C code for CRC calculation. It is developed by Danjel McGougan <danjel.mcgougan@gmail.com> and is licensed under the GPL.

Download version 1.2 - source only
Download version 1.2 - source and win32 exe

Compile it using "gcc -O2 -o universal_crc universal_crc.c".

Help text

This is from the help text of the utility:

Usage: universal_crc <parameters>

Parameters:
  -b <bits> | --bits=<bits>
    Number of bits in the CRC register, 1-64 is supported
    Mandatory parameter

  -p <polynomial> | --poly=<polynomial>
    CRC polynomial value;
    Coefficient of x^0 is bit 0 (LSB) of this value
    Coefficient of x^1 is bit 1 of this value, etc.
    Coefficient of x^<bits> is implied 1
    Bit-reversed automatically if -r is used
    Mandatory parameter

  -i <init> | --init=<init>
    Initial value of the CRC register
    Not bit-reversed even if -r is used
    Default 0 if not specified

  -x <xor> | --xor=<xor>
    Value that is XORed to the final CRC register value
    Not bit-reversed even if -r is used
    Default 0 if not specified

  -r | --reverse
    Bit-reverse the CRC register (LSB is shifted out and MSB in)
    This also means that message bits are processed LSB first
    Default is not to reverse

  -n | --non-direct
    Shift in message bits into the CRC register and augment the
    message. This is equivalent to the direct method of not
    augmenting the message and XORing the message bits with the
    bits shifted out of the CRC register, but the initial CRC
    register value needs to be converted (if it is non-zero) for
    compatibility.
    Default is direct mode.

  -a <algorithm> | --algorithm=<algorithm>
    CRC algorithm to use:
      bit    standard bit-at-a-time algorithm (default, smallest cache footprint)
      tab    standard table-driven algorithm (256 table entries)
      tabi   table-driven algorithm, independent lookups (1024 entries)
             good for superscalar cores
             inspired by crc32 algorithm in zlib originally by Rodney Brown
      tabiw  table-driven algorithm, independent lookups, word-at-a-time
             same as tabi but reads 32 bits at a time from memory

  --crc-type=<type>
    Use <type> as the unsigned integer type to hold the CRC value

  --tab-type=<type>
    Use <type> as the unsigned integer type to hold the CRC table entries

  --test
    Generate test code

Example output

Here is some example output produced when invoking the utility like this:

universal_crc -b 32 -p 0x04c11db7 -i 0xffffffff -x 0xffffffff -r -a tabi

Performance

Here are some example performance numbers of the generated CRC code when running on an AMD Athlon 64 in 32-bit mode.

The CRC code was compiled using gcc 3.4.4 and flags "-O3 -march=athlon64".

bits  algo     cycles/byte
----  ----     -----------
 5    bit      34.00
 8    bit      33.97
13    bit      57.00
16    bit      33.00
23    bit      58.00
32    bit      34.00
49    bit      85.00
64    bit      86.00
 5    tab      6.56
 8    tab      6.56
13    tab      8.00
16    tab      8.00
23    tab      7.00
32    tab      7.00
49    tab      11.00
64    tab      11.01
 5    tabi     2.19
 8    tabi     2.19
13    tabi     2.94
16    tabi     2.47
23    tabi     3.25
32    tabi     3.25
49    tabi     10.53
64    tabi     10.53
 5    tabiw    2.01
 8    tabiw    2.01
13    tabiw    2.31
16    tabiw    2.31
23    tabiw    2.10
32    tabiw    2.10
49    tabiw    6.21
64    tabiw    6.21

Contact me

Bug reports or any kind of feedback is highly welcome! Contact me at danjel.mcgougan@gmail.com.