Rate This Document
Findability
Accuracy
Completeness
Readability

API Reference

This document provides the definitions for the optimization macros and functions newly added to the Kunpeng LZ4 library, which is optimized based on open-source LZ4. These macros and functions take effect only on the AArch64 platform and fall back to the original implementation on other platforms.

Macro and Function Description

Table 1 Kunpeng optimization macros and functions lists the optimization macros and functions.

Table 1 Kunpeng optimization macros and functions

NameTypeDescription
KZL_MEMCPY_2Macro2-byte NEON vectorized memory copy. Uses __builtin_memcpy on AArch64 and falls back to memcpy on other platforms.
KZL_MEMCPY_4Macro4-byte NEON vectorized memory copy. Uses __builtin_memcpy on AArch64 and falls back to memcpy on other platforms.
KZL_MEMCPY_8Macro8-byte NEON vectorized memory copy. Uses vst1_u8/vld1_u8 on AArch64 and falls back to memcpy on other platforms.
KZL_MEMCPY_16Macro16-byte NEON vectorized memory copy. Uses vst1q_u8/vld1q_u8 on AArch64 and falls back to memcpy on other platforms.
KZL_MEMCPY_32Macro32-byte NEON vectorized memory copy. Uses vst1q_u8/vld1q_u8 twice on AArch64 and falls back to LZ4_memcpy twice on other platforms.
KZL_LittleEndianfastHash5Function5-byte fast hash calculation in little-endian mode, using pre-computed constants to optimize multiplication and shift operations.
KZL_BigEndianfastHash5Function5-byte fast hash calculation in big-endian mode, using pre-computed constants to optimize multiplication and shift operations.
LZ4_hashPositionIgnoreBufferLengthFunctionHash position calculation that ignores the buffer length limit, forcing the use of a 5-byte hash (LZ4_hash5) to improve matching accuracy.
skipTriggerFunctionAdaptive skip threshold calculation that dynamically selects the skip step size based on the input data size (4 steps for small data, 2 steps for large data).
LZ4_wildCopy16FunctionFast memory copy with a 16-byte step size, used for literal copy on the compression side.
LZ4_wildCopy64FunctionFast memory copy with a 64-byte step size, used for matched data copy on the decompression side.
KZL_FORCE_INLINEMacroForced inline macro to ensure critical optimization functions are compiled as inline.

Macro Definitions

KZL_MEMCPY Series

Macro Function

Replaces small-block memory copies in LZ4 compression and decompression with NEON SIMD instructions on the AArch64 platform to improve memory copy throughput, and falls back to the standard memcpy or __builtin_memcpy on non-AArch64 platforms or compilers that do not support GCC 4+.

Macro Definition

#if defined(__GNUC__) && (__GNUC__ >= 4)
#    define KZL_MEMCPY_2(dst, src, size) __builtin_memcpy(dst, src, size)
#    define KZL_MEMCPY_4(dst, src, size) __builtin_memcpy(dst, src, size)
#    define KZL_MEMCPY_8(dst, src, size) vst1_u8((dst), vld1_u8(src))
#    define KZL_MEMCPY_16(dst, src, size) vst1q_u8((dst), vld1q_u8(src))
#    define KZL_MEMCPY_32(dst, src, size) vst1q_u8((dst), vld1q_u8(src)); \
                                          vst1q_u8(((dst)+16), vld1q_u8(((src)+16)))
#endif

Parameters

ParameterDescriptionValue RangeInput/Output
dstPointer to the destination address.Valid pointerOutput
srcPointer to the source address.Valid pointerInput
sizeNumber of bytes to be copied.2, 4, 8, 16, 32 (corresponding to the macro name)Input

Platform Behavior

PlatformKZL_MEMCPY_2/4KZL_MEMCPY_8KZL_MEMCPY_16KZL_MEMCPY_32
AArch64 (GCC 4+)__builtin_memcpyvst1_u8/vld1_u8vst1q_u8/vld1q_u8Two vst1q_u8/vld1q_u8
Other platformsmemcpymemcpymemcpyTwo LZ4_memcpy

KZL_FORCE_INLINE

Macro Function

Forces the compiler to inline critical optimization functions, eliminating function call overhead.

Macro Definition

#ifndef KZL_FORCE_INLINE
#  ifdef _MSC_VER
#    define KZL_FORCE_INLINE static __forceinline
#  else
#    if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#      ifdef __GNUC__
#        define KZL_FORCE_INLINE static inline __attribute__((always_inline))
#      else
#        define KZL_FORCE_INLINE static inline
#      endif
#    else
#      define KZL_FORCE_INLINE static
#    endif
#  endif
#endif

Function Definitions

KZL_LittleEndianfastHash5

Function Usage

Calculates a 5-byte fast hash in little-endian mode, using pre-computed constants G_KZLPRIME5BYTES and SEQUENCE_MOVE to optimize multiplication and shift operations, thereby improving hash calculation performance.

Function Syntax

KZL_FORCE_INLINE uint32_t KZL_LittleEndianfastHash5(uint64_t sequence, uint8_t HashLogUsage)
{
    return (uint32_t)(((sequence << SEQUENCE_MOVE) * G_KZLPRIME5BYTES) >> (HASH_SIZE - HashLogUsage));
}

Parameters

ParameterDescriptionValue RangeInput/Output
sequence64-bit sequence value read from the input data.uint64_tInput
HashLogUsageLogarithm of the hash table size.Depending on tableType (byU16: LZ4_HASHLOG + 1; others: LZ4_HASHLOG)Input

Return Value

Returns the hash index value (uint32_t), which is used to find the matching position in the hash table.

Related Constants

ConstantValueDescription
SEQUENCE_MOVE24Number of bits for left-shifting the sequence.
G_KZLPRIME5BYTES889523592379ULL5-byte hash multiplication constant.
HASH_SIZE64Baseline number of bits for right-shifting.

KZL_BigEndianfastHash5

Function Usage

Calculates a 5-byte fast hash in big-endian mode, using pre-computed constants G_KZLPRIME8BYTES and SEQUENCE_MOVE to optimize multiplication and shift operations.

Function Syntax

KZL_FORCE_INLINE uint32_t KZL_BigEndianfastHash5(uint64_t sequence, uint8_t HashLogUsage)
{
    return (uint32_t)(((sequence >> SEQUENCE_MOVE) * G_KZLPRIME8BYTES) >> (HASH_SIZE - HashLogUsage));
}

Parameters

ParameterDescriptionValue RangeInput/Output
sequence64-bit sequence value read from the input data.uint64_tInput
HashLogUsageLogarithm of the hash table size.Depending on tableTypeInput

Return Value

Returns the hash index value (uint32_t).

Related Constants

ConstantValueDescription
SEQUENCE_MOVE24Number of bits for right-shifting the sequence.
G_KZLPRIME8BYTES11400714785074694791ULL8-byte hash multiplication constant.
HASH_SIZE64Baseline number of bits for right-shifting.

LZ4_hashPositionIgnoreBufferLength

Function Usage

Calculates the hash position by ignoring the buffer length limit. Unlike the original LZ4_hashPosition, this function forces the use of a 5-byte hash (LZ4_hash5) in all scenarios (including those where sizeof(reg_t)==4) to improve matching accuracy on the AArch64 platform.

Function Syntax

LZ4_FORCE_INLINE U32 LZ4_hashPositionIgnoreBufferLength(const void* const p, tableType_t const tableType)
{
    return LZ4_hash5(LZ4_read_ARCH(p), tableType);
}

Parameters

ParameterDescriptionValue RangeInput/Output
pPointer to the input data position.Valid pointerInput
tableTypeHash table type.byU16, byU32, byPtrInput

Return Value

Returns the hash index value (U32).

Application Scenario

In the Kunpeng-optimized version, all invocations of LZ4_hashPosition are replaced with LZ4_hashPositionIgnoreBufferLength to ensure that a 5-byte hash is always used for better matching quality.

skipTrigger

Function Usage

Calculates an adaptive skip threshold. It dynamically selects the skip step during compression based on the input data size: using a larger step size (4) for small data (< 64 KB + 11 bytes) to reduce ineffective match attempts, and a smaller step size (2) for large data to accelerate match lookup.

Function Syntax

static void skipTrigger(int srcSize, uint8_t *skipStep)
{
    if (skipStep == NULL || srcSize <= 0) {
        return;
    }

    if (srcSize >= G_64KLIMIT) {
        *skipStep = 2;
    } else {
        *skipStep = 4;
    }
}

Parameters

ParameterDescriptionValue RangeInput/Output
srcSizeInput data size.Non-negative integerInput
skipStepPointer to the output step.Valid pointerOutput

Return Value

None. The result is output via the skipStep pointer.

Output Value Description

Input ConditionOutput ValueDescription
srcSize >= G_64KLIMIT (65547)2Indicates the large data scenario, where a smaller skip step is used to accelerate match lookup.
srcSize < G_64KLIMIT4Indicates the small data scenario, where a larger skip step is used to reduce ineffective attempts.
skipStep == NULL or srcSize <= 0UnchangedIndicates the safety check scenario, where the function returns directly.

Related Constants

ConstantValueDescription
G_64KLIMIT65536 + 1164 KB boundary threshold to differentiate between large and small data scenarios.

LZ4_wildCopy16

Function Usage

Performs a fast memory copy with a 16-byte step, used for the fast copy of literal data on the compression side. Compared to the original LZ4_wildCopy8 (8-byte step), it copies 16 bytes per operation to improve copy throughput.

Function Syntax

LZ4_FORCE_INLINE
void LZ4_wildCopy16(void* dstPtr, const void* srcPtr, void* dstEnd)
{
    BYTE* d = (BYTE*)dstPtr;
    const BYTE* s = (const BYTE*)srcPtr;
    BYTE* const e = (BYTE*)dstEnd;

    do { LZ4_memcpy(d,s,16); d+=16; s+=16; } while (d<e);
}

Parameters

ParameterDescriptionValue RangeInput/Output
dstPtrPointer to the destination address.Valid pointerInput/Output
srcPtrPointer to the source address.Valid pointerInput
dstEndPointer to the end destination address.Valid pointer, dstEnd >= dstPtrInput

Return Value

None

LZ4_wildCopy64

Function Usage

Performs a fast memory copy with a 64-byte step, used for the fast copy of large-block matched data on the decompression side. Compared to the original LZ4_wildCopy32 (32-byte step), it copies 64 bytes per operation (via four 16-byte copies) to significantly improve decompression throughput.

Function Syntax

LZ4_FORCE_INLINE void
LZ4_wildCopy64(void* dstPtr, const void* srcPtr, void* dstEnd)
{
    BYTE* d = (BYTE*)dstPtr;
    const BYTE* s = (const BYTE*)srcPtr;
    BYTE* const e = (BYTE*)dstEnd;

    do {
        LZ4_memcpy(d,s,16);
        LZ4_memcpy(d+16,s+16,16);
        LZ4_memcpy(d+32,s+32,16);
        LZ4_memcpy(d+48,s+48,16);

        d+=64; s+=64;
    } while (d<e);
}

Parameters

ParameterDescriptionValue RangeInput/Output
dstPtrPointer to the destination address.Valid pointerInput/Output
srcPtrPointer to the source address.Valid pointerInput
dstEndPointer to the end destination address.Valid pointer, dstEnd >= dstPtrInput

Return Value

None

Compilation Optimization Options

The patch in this repository introduces the following compilation optimization options to lib/Makefile.

OptionDescription
-march=nativeGenerates optimal instructions for the current CPU architecture (automatically enabling NEON/SVE extensions on the Kunpeng platform).
-fltoEnables link-time optimization (LTO) to perform cross-module inlining and dead code elimination.
-fno-pltDisables procedure linkage table (PLT) jumps to reduce the overhead of dynamic link function calls.