001-neon-implementation-of-adler32.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. From d2f06cd65d7ac39c6dd6761eef162abc946b155b Mon Sep 17 00:00:00 2001
  2. From: Adenilson Cavalcanti <adenilson.cavalcanti@arm.com>
  3. Date: Tue, 11 Apr 2017 17:13:02 -0700
  4. Subject: [PATCH] NEON implementation for Adler32
  5. The checksum is calculated in the uncompressed PNG data
  6. and can be made much faster by using SIMD.
  7. Tests in ARMv8 yielded an improvement of about 3x
  8. (e.g. walltime was 350ms x 125ms for a 4096x4096 bytes
  9. executed 30 times). That results in at least 18% improvement
  10. in image decoding in Chromium.
  11. Further details at:
  12. https://bugs.chromium.org/p/chromium/issues/detail?id=688601
  13. ---
  14. CMakeLists.txt | 29 +++++++---
  15. adler32.c | 5 ++
  16. contrib/README.contrib | 3 +
  17. contrib/arm/neon_adler32.c | 137 +++++++++++++++++++++++++++++++++++++++++++++
  18. 4 files changed, 166 insertions(+), 8 deletions(-)
  19. create mode 100644 contrib/arm/neon_adler32.c
  20. diff --git a/CMakeLists.txt b/CMakeLists.txt
  21. index 0fe939df..8e75f664 100644
  22. --- a/CMakeLists.txt
  23. +++ b/CMakeLists.txt
  24. @@ -7,6 +7,7 @@ set(VERSION "1.2.11")
  25. option(ASM686 "Enable building i686 assembly implementation")
  26. option(AMD64 "Enable building amd64 assembly implementation")
  27. +option(ARMv8 "Enable building ARM NEON intrinsics implementation")
  28. set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
  29. set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
  30. @@ -132,14 +133,26 @@ endif()
  31. if(CMAKE_COMPILER_IS_GNUCC)
  32. if(ASM686)
  33. set(ZLIB_ASMS contrib/asm686/match.S)
  34. - elseif (AMD64)
  35. + elseif(AMD64)
  36. set(ZLIB_ASMS contrib/amd64/amd64-match.S)
  37. - endif ()
  38. + elseif(ARMv8)
  39. + set(ZLIB_ARMv8 contrib/arm/neon_adler32.c)
  40. + endif()
  41. - if(ZLIB_ASMS)
  42. - add_definitions(-DASMV)
  43. - set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
  44. - endif()
  45. + if(ZLIB_ASMS)
  46. + add_definitions(-DASMV)
  47. + set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
  48. + elseif(ZLIB_ARMv8)
  49. + add_definitions(-DARMv8)
  50. + set(COMPILER ${CMAKE_C_COMPILER})
  51. + # NEON is mandatory in ARMv8.
  52. + if(${COMPILER} MATCHES "aarch64")
  53. + set_source_files_properties(${ZLIB_ARMv8} PROPERTIES LANGUAGE C COMPILE_FLAGS -march=armv8-a)
  54. + # But it was optional for ARMv7.
  55. + elseif(${COMPILER} MATCHES "arm")
  56. + set_source_files_properties(${ZLIB_ARMv8} PROPERTIES LANGUAGE C COMPILE_FLAGS -mfpu=neon)
  57. + endif()
  58. + endif()
  59. endif()
  60. if(MSVC)
  61. @@ -183,8 +196,8 @@ if(MINGW)
  62. set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
  63. endif(MINGW)
  64. -add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
  65. -add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
  66. +add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_ARMv8} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
  67. +add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_ARMv8} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
  68. set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
  69. set_target_properties(zlib PROPERTIES SOVERSION 1)
  70. diff --git a/adler32.c b/adler32.c
  71. index d0be4380..45ebaa4b 100644
  72. --- a/adler32.c
  73. +++ b/adler32.c
  74. @@ -136,7 +136,12 @@ uLong ZEXPORT adler32(adler, buf, len)
  75. const Bytef *buf;
  76. uInt len;
  77. {
  78. +#ifdef ARMv8
  79. +# pragma message("Using NEON-ized Adler32.")
  80. + return NEON_adler32(adler, buf, len);
  81. +#else
  82. return adler32_z(adler, buf, len);
  83. +#endif
  84. }
  85. /* ========================================================================= */
  86. diff --git a/contrib/README.contrib b/contrib/README.contrib
  87. index a411d5c3..3fd1d202 100644
  88. --- a/contrib/README.contrib
  89. +++ b/contrib/README.contrib
  90. @@ -12,6 +12,9 @@ amd64/ by Mikhail Teterin <mi@ALDAN.algebra.com>
  91. asm code for AMD64
  92. See patch at http://www.freebsd.org/cgi/query-pr.cgi?pr=bin/96393
  93. +arm/ by Adenilson Cavalcanti <cavalcantii@chromium.org>
  94. + ARM optimizations (NEON and ARMv8 code).
  95. +
  96. asm686/ by Brian Raiter <breadbox@muppetlabs.com>
  97. asm code for Pentium and PPro/PII, using the AT&T (GNU as) syntax
  98. See http://www.muppetlabs.com/~breadbox/software/assembly.html
  99. diff --git a/contrib/arm/neon_adler32.c b/contrib/arm/neon_adler32.c
  100. new file mode 100644
  101. index 00000000..f173a74f
  102. --- /dev/null
  103. +++ b/contrib/arm/neon_adler32.c
  104. @@ -0,0 +1,137 @@
  105. +/* Copyright (C) 1995-2011, 2016 Mark Adler
  106. + * Copyright (C) 2017 ARM Holdings Inc.
  107. + * Authors: Adenilson Cavalcanti <adenilson.cavalcanti@arm.com>
  108. + * Simon Hosie <simon.hosie@arm.com>
  109. + * This software is provided 'as-is', without any express or implied
  110. + * warranty. In no event will the authors be held liable for any damages
  111. + * arising from the use of this software.
  112. + * Permission is granted to anyone to use this software for any purpose,
  113. + * including commercial applications, and to alter it and redistribute it
  114. + * freely, subject to the following restrictions:
  115. + * 1. The origin of this software must not be misrepresented; you must not
  116. + * claim that you wrote the original software. If you use this software
  117. + * in a product, an acknowledgment in the product documentation would be
  118. + * appreciated but is not required.
  119. + * 2. Altered source versions must be plainly marked as such, and must not be
  120. + * misrepresented as being the original software.
  121. + * 3. This notice may not be removed or altered from any source distribution.
  122. + */
  123. +
  124. +#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
  125. +#include <arm_neon.h>
  126. +
  127. +static void NEON_accum32(uint32_t *s, const unsigned char *buf,
  128. + unsigned int len)
  129. +{
  130. + static const uint8_t taps[32] = {
  131. + 32, 31, 30, 29, 28, 27, 26, 25,
  132. + 24, 23, 22, 21, 20, 19, 18, 17,
  133. + 16, 15, 14, 13, 12, 11, 10, 9,
  134. + 8, 7, 6, 5, 4, 3, 2, 1 };
  135. +
  136. + uint32x2_t adacc2, s2acc2, as;
  137. + uint8x16_t t0 = vld1q_u8(taps), t1 = vld1q_u8(taps + 16);
  138. +
  139. + uint32x4_t adacc = vdupq_n_u32(0), s2acc = vdupq_n_u32(0);
  140. + adacc = vsetq_lane_u32(s[0], adacc, 0);
  141. + s2acc = vsetq_lane_u32(s[1], s2acc, 0);
  142. +
  143. + while (len >= 2) {
  144. + uint8x16_t d0 = vld1q_u8(buf), d1 = vld1q_u8(buf + 16);
  145. + uint16x8_t adler, sum2;
  146. + s2acc = vaddq_u32(s2acc, vshlq_n_u32(adacc, 5));
  147. + adler = vpaddlq_u8( d0);
  148. + adler = vpadalq_u8(adler, d1);
  149. + sum2 = vmull_u8( vget_low_u8(t0), vget_low_u8(d0));
  150. + sum2 = vmlal_u8(sum2, vget_high_u8(t0), vget_high_u8(d0));
  151. + sum2 = vmlal_u8(sum2, vget_low_u8(t1), vget_low_u8(d1));
  152. + sum2 = vmlal_u8(sum2, vget_high_u8(t1), vget_high_u8(d1));
  153. + adacc = vpadalq_u16(adacc, adler);
  154. + s2acc = vpadalq_u16(s2acc, sum2);
  155. + len -= 2;
  156. + buf += 32;
  157. + }
  158. +
  159. + while (len > 0) {
  160. + uint8x16_t d0 = vld1q_u8(buf);
  161. + uint16x8_t adler, sum2;
  162. + s2acc = vaddq_u32(s2acc, vshlq_n_u32(adacc, 4));
  163. + adler = vpaddlq_u8(d0);
  164. + sum2 = vmull_u8( vget_low_u8(t1), vget_low_u8(d0));
  165. + sum2 = vmlal_u8(sum2, vget_high_u8(t1), vget_high_u8(d0));
  166. + adacc = vpadalq_u16(adacc, adler);
  167. + s2acc = vpadalq_u16(s2acc, sum2);
  168. + buf += 16;
  169. + len--;
  170. + }
  171. +
  172. + adacc2 = vpadd_u32(vget_low_u32(adacc), vget_high_u32(adacc));
  173. + s2acc2 = vpadd_u32(vget_low_u32(s2acc), vget_high_u32(s2acc));
  174. + as = vpadd_u32(adacc2, s2acc2);
  175. + s[0] = vget_lane_u32(as, 0);
  176. + s[1] = vget_lane_u32(as, 1);
  177. +}
  178. +
  179. +static void NEON_handle_tail(uint32_t *pair, const unsigned char *buf,
  180. + unsigned int len)
  181. +{
  182. + /* Oldie K&R code integration. */
  183. + unsigned int i;
  184. + for (i = 0; i < len; ++i) {
  185. + pair[0] += buf[i];
  186. + pair[1] += pair[0];
  187. + }
  188. +}
  189. +
  190. +extern unsigned long NEON_adler32(unsigned long adler, const unsigned char *buf,
  191. + const unsigned int len)
  192. +{
  193. + /* initial Adler-32 value (deferred check for len == 1 speed) */
  194. + if (!buf)
  195. + return 1L;
  196. +
  197. + /* The largest prime smaller than 65536. */
  198. + const uint32_t M_BASE = 65521;
  199. + /* This is the threshold where doing accumulation may overflow. */
  200. + const int M_NMAX = 5552;
  201. +
  202. + unsigned long sum2;
  203. + uint32_t pair[2];
  204. + int n = M_NMAX;
  205. + unsigned int done = 0;
  206. + /* Oldie K&R code integration. */
  207. + unsigned int i;
  208. +
  209. + /* Split Adler-32 into component sums, it can be supplied by
  210. + * the caller sites (e.g. in a PNG file).
  211. + */
  212. + sum2 = (adler >> 16) & 0xffff;
  213. + adler &= 0xffff;
  214. + pair[0] = adler;
  215. + pair[1] = sum2;
  216. +
  217. + for (i = 0; i < len; i += n) {
  218. + if ((i + n) > len)
  219. + n = len - i;
  220. +
  221. + if (n < 16)
  222. + break;
  223. +
  224. + NEON_accum32(pair, buf + i, n / 16);
  225. + pair[0] %= M_BASE;
  226. + pair[1] %= M_BASE;
  227. +
  228. + done += (n / 16) * 16;
  229. + }
  230. +
  231. + /* Handle the tail elements. */
  232. + if (done < len) {
  233. + NEON_handle_tail(pair, (buf + done), len - done);
  234. + pair[0] %= M_BASE;
  235. + pair[1] %= M_BASE;
  236. + }
  237. +
  238. + /* D = B * 65536 + A, see: https://en.wikipedia.org/wiki/Adler-32. */
  239. + return (pair[1] << 16) | pair[0];
  240. +}
  241. +#endif