2021-02-05 20:46:59 +03:00
..
2021-01-31 02:52:06 +03:00
2020-12-27 16:02:35 +03:00
2020-12-27 16:02:35 +03:00
2021-02-05 20:46:59 +03:00
2021-02-05 20:46:59 +03:00
2021-02-05 20:46:59 +03:00

CRC32c function

  • Same code from : https://stackoverflow.com/a/17646775

  • Fixed some alignment issues, replaced asm code with compiler intrinsics

  • Compile time switch to hardware version if supported
    (crc32c instruction on x64), fallback to software version if not available

  • See CmakeLists.txt, it checks "-msse4.2" flag. Stackoverflow answer has
    runtime dispatch between hardware and software versions if you'd like that.

  • This is Crc32c algorithm, not Crc32

## Cmake

# Needs HAVE_CRC32C definition to enable CPU instruction usage.

## Only use hardware version in 64 bit architectures.
include(CheckCCompilerFlag)

if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
    check_c_compiler_flag(-msse4.2 HAVE_CRC32_HARDWARE)
    if (${HAVE_CRC32_HARDWARE})
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2 -DHAVE_CRC32C")
    endif ()
endif()

#include "sc_crc32.h"

#include <stdio.h>

int main(int argc, char *argv[])
{
    uint32_t crc;
    const uint8_t buf[100] = {0};

    sc_crc32_init();

    // Partial calculation example
    crc = sc_crc32(0, buf, 10);
    crc = sc_crc32(crc, buf + 10, sizeof(buf) - 10);
    printf("crc : %u \n", crc);

    // Calculate at once
    crc = sc_crc32(0, buf, sizeof(buf));
    printf("crc : %u \n", crc);

    return 0;
}