Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEON implementation for Adler32 #251

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Using ARMv8 CRC32 specific instruction
CRC32 affects performance for both image decompression (PNG) as also in
general browsing while accessing websites that serve content using
compression (i.e. Content-Encoding: gzip).

This first patch implements an optimized CRC32 function using the
dedicated instruction available in ARMv8.

It should be between 6x (A53: 116ms X 22ms for a 4Kx4Kx4 buffer) to
10x faster (A72: 91ms x 9ms) than the C implementation currently used by zlib.

Details:
https://bugs.chromium.org/p/chromium/issues/detail?id=709716

Change-Id: I069408ebc06c49a3c2be4ba3253319e025ee09d7
  • Loading branch information
Adenilson Cavalcanti committed Apr 25, 2017
commit 5393223b7c459e8e05f511282f9fc45f8cda5dab
40 changes: 27 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(VERSION "1.2.11")
option(ASM686 "Enable building i686 assembly implementation")
option(AMD64 "Enable building amd64 assembly implementation")
option(ARMv8 "Enable building ARM NEON intrinsics implementation")
option(ARMv8CRC "Enable building ARM CRC32 instruction")

set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
Expand Down Expand Up @@ -135,22 +136,35 @@ if(CMAKE_COMPILER_IS_GNUCC)
set(ZLIB_ASMS contrib/asm686/match.S)
elseif(AMD64)
set(ZLIB_ASMS contrib/amd64/amd64-match.S)
elseif(ARMv8)
endif()

if(ARMv8)
set(ZLIB_ARMv8 contrib/arm/neon_adler32.c)
endif()
if(ARMv8CRC)
set(ZLIB_ARMv8CRC contrib/arm/armv8_crc32.c)
endif()

if(ZLIB_ASMS)
add_definitions(-DASMV)
set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
elseif(ZLIB_ARMv8)
add_definitions(-DARMv8)
set(COMPILER ${CMAKE_C_COMPILER})
# NEON is mandatory in ARMv8.
if(${COMPILER} MATCHES "aarch64")
set_source_files_properties(${ZLIB_ARMv8} PROPERTIES LANGUAGE C COMPILE_FLAGS -march=armv8-a)
# But it was optional for ARMv7.
elseif(${COMPILER} MATCHES "arm")
set_source_files_properties(${ZLIB_ARMv8} PROPERTIES LANGUAGE C COMPILE_FLAGS -mfpu=neon)
set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
elseif(ZLIB_ARMv8 OR ZLIB_ARMv8CRC)
if(ZLIB_ARMv8)
add_definitions(-DARMv8)
set(COMPILER ${CMAKE_C_COMPILER})
# NEON is mandatory in ARMv8.
if(${COMPILER} MATCHES "aarch64")
set_source_files_properties(${ZLIB_ARMv8} PROPERTIES LANGUAGE C COMPILE_FLAGS -march=armv8-a)
# But it was optional for ARMv7.
elseif(${COMPILER} MATCHES "arm")
set_source_files_properties(${ZLIB_ARMv8} PROPERTIES LANGUAGE C COMPILE_FLAGS -mfpu=neon)
endif()
endif()

if(ZLIB_ARMv8CRC)
add_definitions(-DARMv8CRC)
set(COMPILER ${CMAKE_C_COMPILER})
set_source_files_properties(${ZLIB_ARMv8CRC} PROPERTIES LANGUAGE C COMPILE_FLAGS -march=armv8-a+crc)
endif()
endif()
endif()
Expand Down Expand Up @@ -196,8 +210,8 @@ if(MINGW)
set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
endif(MINGW)

add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_ARMv8} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_ARMv8} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_ARMv8} ${ZLIB_ARMv8CRC} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_ARMv8} ${ZLIB_ARMv8CRC} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1)

Expand Down
67 changes: 67 additions & 0 deletions contrib/arm/armv8_crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright (C) 1995-2011, 2016 Mark Adler
* Copyright (C) 2017 ARM Holdings Inc.
* Authors: Adenilson Cavalcanti <[email protected]>
* Yang Zhang <[email protected]>
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <arm_acle.h>
// Depending on the compiler flavor, size_t may be defined in
// one or the other header. See:
// http://stackoverflow.com/questions/26410466/gcc-linaro-compiler-throws-error-unknown-type-name-size-t
#include <stdint.h>
#include <stddef.h>

uint32_t armv8_crc32_little(uint32_t crc,
const unsigned char *buf,
size_t len) {
uint32_t c;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a second thought, this should handle the case of buf == Z_NULL.

const uint32_t *buf4;

c = crc;
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
c = __crc32b(c, *buf++);
len--;
}

buf4 = (const uint32_t *)(const void *)buf;

while (len >= 32) {
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
c = __crc32w(c, *buf4++);
len -= 32;
}

while (len >= 4) {
c = __crc32w(c, *buf4++);
len -= 4;
}

buf = (const unsigned char *)buf4;
if (len) {
do {
c = __crc32b(c, *buf++);
} while (--len);
}

c = ~c;
return c;
}
5 changes: 5 additions & 0 deletions crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ unsigned long ZEXPORT crc32(crc, buf, len)
const unsigned char FAR *buf;
uInt len;
{
#ifdef ARMv8CRC
# pragma message("Using ARMv8 CRC32 instruction.")
return armv8_crc32_little(crc, buf, len);
#else
return crc32_z(crc, buf, len);
#endif
}

#ifdef BYFOUR
Expand Down