From bdbcee59690b87483f17071743de0e1233bd236b Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Thu, 25 Sep 2025 16:30:53 +0100 Subject: [PATCH] Synchronise ARMv8 and Loongarch CRC32 implementations --- arch/arm/arm_functions.h | 6 ++++++ arch/arm/crc32_armv8.c | 24 +++++++++++++++++------- arch/loongarch/crc32_la.c | 7 +------ functable.c | 2 ++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/arch/arm/arm_functions.h b/arch/arm/arm_functions.h index b7b24f88..8a89c7c4 100644 --- a/arch/arm/arm_functions.h +++ b/arch/arm/arm_functions.h @@ -22,6 +22,8 @@ void inflate_fast_neon(PREFIX3(stream) *strm, uint32_t start); #ifdef ARM_CRC32 uint32_t crc32_armv8(uint32_t crc, const uint8_t *buf, size_t len); +void crc32_fold_copy_armv8(crc32_fold *crc, uint8_t *dst, const uint8_t *src, size_t len); +void crc32_fold_armv8(crc32_fold *crc, const uint8_t *src, size_t len, uint32_t init_crc); #endif #ifdef ARM_SIMD @@ -62,6 +64,10 @@ void slide_hash_armv6(deflate_state *s); # if (defined(ARM_CRC32) && defined(__ARM_FEATURE_CRC32)) # undef native_crc32 # define native_crc32 crc32_armv8 +# undef native_crc32_fold +# define native_crc32_fold crc32_fold_armv8 +# undef native_crc32_fold_copy +# define native_crc32_fold_copy crc32_fold_copy_armv8 # endif #endif diff --git a/arch/arm/crc32_armv8.c b/arch/arm/crc32_armv8.c index c3561d51..3fa36a12 100644 --- a/arch/arm/crc32_armv8.c +++ b/arch/arm/crc32_armv8.c @@ -2,12 +2,12 @@ * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler * Copyright (C) 2016 Yang Zhang * For conditions of distribution and use, see copyright notice in zlib.h - * -*/ + */ #if defined(ARM_CRC32) #include "acle_intrins.h" #include "zbuild.h" +#include "crc32.h" Z_INTERNAL Z_TARGET_CRC uint32_t crc32_armv8(uint32_t crc, const uint8_t *buf, size_t len) { Z_REGISTER uint32_t c; @@ -52,25 +52,35 @@ Z_INTERNAL Z_TARGET_CRC uint32_t crc32_armv8(uint32_t crc, const uint8_t *buf, s buf += sizeof(uint64_t); } - if (len >= sizeof(uint32_t)) { + if (len & sizeof(uint32_t)) { buf4 = *((uint32_t*)buf); c = __crc32w(c, buf4); - len -= sizeof(uint32_t); buf += sizeof(uint32_t); } - if (len >= sizeof(uint16_t)) { + if (len & sizeof(uint16_t)) { buf2 = *((uint16_t*)buf); c = __crc32h(c, buf2); - len -= sizeof(uint16_t); buf += sizeof(uint16_t); } - if (len) { + if (len & sizeof(uint8_t)) { c = __crc32b(c, *buf); } c = ~c; return c; } + +/* Note: Based on generic crc32_fold_* implementation with functable call replaced by direct call. */ +Z_INTERNAL Z_TARGET_CRC void crc32_fold_copy_armv8(crc32_fold *crc, uint8_t *dst, const uint8_t *src, size_t len) { + crc->value = crc32_armv8(crc->value, src, len); + memcpy(dst, src, len); +} + +Z_INTERNAL Z_TARGET_CRC void crc32_fold_armv8(crc32_fold *crc, const uint8_t *src, size_t len, uint32_t init_crc) { + Z_UNUSED(init_crc); + crc->value = crc32_armv8(crc->value, src, len); +} + #endif diff --git a/arch/loongarch/crc32_la.c b/arch/loongarch/crc32_la.c index 034bf7f1..38c63eea 100644 --- a/arch/loongarch/crc32_la.c +++ b/arch/loongarch/crc32_la.c @@ -5,15 +5,11 @@ #if defined(LOONGARCH_CRC) #include "zbuild.h" -#include "zmemory.h" -#include "zutil.h" #include "crc32.h" #include - #include -Z_INTERNAL uint32_t crc32_loongarch64(uint32_t crc, const uint8_t *buf, - size_t len) { +Z_INTERNAL uint32_t crc32_loongarch64(uint32_t crc, const uint8_t *buf, size_t len) { Z_REGISTER uint32_t c; Z_REGISTER uint16_t buf2; Z_REGISTER uint32_t buf4; @@ -76,7 +72,6 @@ Z_INTERNAL uint32_t crc32_loongarch64(uint32_t crc, const uint8_t *buf, return c; } - /* Note: Based on generic crc32_fold_* implementation with functable call replaced by direct call. */ Z_INTERNAL void crc32_fold_copy_loongarch64(crc32_fold *crc, uint8_t *dst, const uint8_t *src, size_t len) { crc->value = crc32_loongarch64(crc->value, src, len); diff --git a/functable.c b/functable.c index dd6400df..f09544d1 100644 --- a/functable.c +++ b/functable.c @@ -206,6 +206,8 @@ static void init_functable(void) { #ifdef ARM_CRC32 if (cf.arm.has_crc32) { ft.crc32 = &crc32_armv8; + ft.crc32_fold = &crc32_fold_armv8; + ft.crc32_fold_copy = &crc32_fold_copy_armv8; } #endif -- 2.47.3