sc_crc: Fix CPU detection and input type (#113)

sc_crc: Fix CPU detection and input type
This commit is contained in:
Ozan Tezcan 2023-04-11 04:22:52 +03:00 committed by GitHub
parent b62219bb61
commit 92b5455a2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 39 additions and 32 deletions

View File

@ -9,7 +9,7 @@ on:
jobs:
archs:
# The host should always be linux
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
name: Build on aarch64
steps:
- uses: actions/checkout@v2.1.0

View File

@ -9,7 +9,7 @@ on:
jobs:
archs:
# The host should always be linux
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
name: Build on armv6
steps:
- uses: actions/checkout@v2.1.0

View File

@ -9,7 +9,7 @@ on:
jobs:
archs:
# The host should always be linux
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
name: Build on armv7
steps:
- uses: actions/checkout@v2.1.0

View File

@ -9,7 +9,7 @@ on:
jobs:
archs:
# The host should always be linux
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
name: Build on ppc64le
steps:
- uses: actions/checkout@v2.1.0

View File

@ -9,7 +9,7 @@ on:
jobs:
archs:
# The host should always be linux
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
name: Build on s390x
steps:
- uses: actions/checkout@v2.1.0

View File

@ -41,17 +41,19 @@ if (SC_BUILD_TEST)
add_executable(${PROJECT_NAME}_test crc32_test.c sc_crc32.c)
# detect x86
check_c_compiler_flag(-msse4.2 HAVE_CRC32_HARDWARE)
if (${HAVE_CRC32_HARDWARE})
if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
message(STATUS "CPU have -msse4.2, defined HAVE_CRC32C")
target_compile_options(${PROJECT_NAME}_test PRIVATE -msse4.2)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
check_c_compiler_flag(-msse4.2 HAVE_CRC32_HARDWARE)
if (${HAVE_CRC32_HARDWARE})
if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
message(STATUS "CPU have -msse4.2, defined HAVE_CRC32C")
target_compile_options(${PROJECT_NAME}_test PRIVATE -msse4.2)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
endif ()
endif ()
endif ()
# detect aarch64
if (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64|aarch64")
message(STATUS "CPU = aarch64, defined HAVE_CRC32C, -march=armv8.1-a")
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
target_compile_options(${PROJECT_NAME}_test PRIVATE -march=armv8.1-a)

View File

@ -3,7 +3,7 @@
- Same code from : https://stackoverflow.com/a/17646775
- Fixed some alignment issues, replaced asm code with compiler intrinsics
- Added aarch64 hardware support
- Requires architecture and endianness detection, CMAKE example is :
- Requires architecture and endianness detection, CMAKE example is:
```cmake
## Cmake
@ -12,16 +12,21 @@ include(CheckCCompilerFlag)
include (TestBigEndian)
# Detect x86 and sse4.2 support
check_c_compiler_flag(-msse4.2 HAVE_CRC32_HARDWARE)
if (${HAVE_CRC32_HARDWARE})
message(STATUS "CPU have -msse4.2, defined HAVE_CRC32C")
target_compile_options(${PROJECT_NAME}_test PRIVATE -msse4.2)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
message(STATUS "System is x86_64")
check_c_compiler_flag(-msse4.2 HAVE_CRC32_HARDWARE)
if (${HAVE_CRC32_HARDWARE})
if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
message(STATUS "CPU have -msse4.2, defined HAVE_CRC32C")
target_compile_options(${PROJECT_NAME}_test PRIVATE -msse4.2)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
endif ()
endif ()
endif ()
# Detect aarch64 and set march=armv8.1-a. armv7 doesn't have CRC32c instruction
# so, armv7 will be unsupported with this flag.
if (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64|aarch64")
message(STATUS "CPU = aarch64, defined HAVE_CRC32C, -march=armv8.1-a")
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
target_compile_options(${PROJECT_NAME}_test PRIVATE -march=armv8.1-a)
@ -46,7 +51,7 @@ endif ()
int main(int argc, char *argv[])
{
uint32_t crc;
const uint8_t buf[100] = {0};
const char buf[100] = {0};
sc_crc32_init();

View File

@ -5,7 +5,7 @@
int main(void)
{
uint32_t crc;
const uint8_t buf[100] = {0};
const char buf[100] = {0};
sc_crc32_init();

View File

@ -8,18 +8,17 @@ int main(int argc, char *argv[])
(void) argv;
uint32_t crc1, crc2, crc3;
uint8_t buf[128] = {1, 1, 2, 3};
uint8_t buf2[4096 * 8] = {2, 5, 6, 5};
char buf[128] = {1, 1, 2, 3};
char buf2[4096 * 8] = {2, 5, 6, 5};
sc_crc32_init();
// pre-computed values
assert(sc_crc32(0, (uint8_t *) "", 1) == 1383945041);
assert(sc_crc32(0, (uint8_t *) "1", 2) == 2727214374);
assert(sc_crc32(0, (uint8_t *) "\0\0\0\0\0\0\0\0\0\0", 10) ==
3822973035);
assert(sc_crc32(0, (uint8_t *) "test", 5) == 2440484327);
assert(sc_crc32(0, (uint8_t *) "testtest", 9) == 443192409);
assert(sc_crc32(0, "", 1) == 1383945041);
assert(sc_crc32(0, "1", 2) == 2727214374);
assert(sc_crc32(0, "\0\0\0\0\0\0\0\0\0\0", 10) == 3822973035);
assert(sc_crc32(0, "test", 5) == 2440484327);
assert(sc_crc32(0, "testtest", 9) == 443192409);
crc1 = sc_crc32(0, buf, 100);
crc2 = sc_crc32(crc1, buf + 100, 28);

View File

@ -170,7 +170,7 @@ static void crc32_init_hw(void)
crc32_zeros(crc32c_short, CRC32_SHORT);
}
uint32_t crc32_hw(uint32_t crc, const uint8_t *buf, uint32_t len)
uint32_t crc32_hw(uint32_t crc, const void *buf, size_t len)
{
const unsigned char *next = buf;
const unsigned char *end;
@ -420,7 +420,7 @@ static uint32_t crc32_sw_be(uint32_t crc, const void *buf, size_t len)
#endif // HAVE_BIG_ENDIAN
#endif
uint32_t sc_crc32(uint32_t crc, const uint8_t *buf, uint32_t len)
uint32_t sc_crc32(uint32_t crc, const void *buf, size_t len)
{
#ifdef HAVE_CRC32C
return crc32_hw(crc, buf, len);

View File

@ -32,6 +32,7 @@
#ifndef SC_CRC32_H
#define SC_CRC32_H
#include <stddef.h>
#include <stdint.h>
#define SC_CRC32_VERSION "2.0.0"
@ -48,6 +49,6 @@ void sc_crc32_init(void);
* @param len len
* @return crc value
*/
uint32_t sc_crc32(uint32_t crc, const uint8_t *buf, uint32_t len);
uint32_t sc_crc32(uint32_t crc, const void *buf, size_t len);
#endif