chunkcopy.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* chunkcopy.h -- fast chunk copy and set operations
  2. * Copyright (C) 2017 ARM, Inc.
  3. * Copyright 2017 The Chromium Authors. All rights reserved.
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the Chromium source repository LICENSE file.
  6. */
  7. #ifndef CHUNKCOPY_H
  8. #define CHUNKCOPY_H
  9. #include <stdint.h>
  10. #include "zutil.h"
  11. #define Z_STATIC_ASSERT(name, assert) typedef char name[(assert) ? 1 : -1]
  12. #if __STDC_VERSION__ >= 199901L
  13. #define Z_RESTRICT restrict
  14. #else
  15. #define Z_RESTRICT
  16. #endif
  17. #if defined(__clang__) || defined(__GNUC__) || defined(__llvm__)
  18. #define Z_BUILTIN_MEMCPY __builtin_memcpy
  19. #else
  20. #define Z_BUILTIN_MEMCPY zmemcpy
  21. #endif
  22. #if defined(INFLATE_CHUNK_SIMD_NEON)
  23. #include <arm_neon.h>
  24. typedef uint8x16_t z_vec128i_t;
  25. #elif defined(INFLATE_CHUNK_SIMD_SSE2)
  26. #include <emmintrin.h>
  27. typedef __m128i z_vec128i_t;
  28. #else
  29. #error chunkcopy.h inflate chunk SIMD is not defined for your build target
  30. #endif
  31. /*
  32. * chunk copy type: the z_vec128i_t type size should be exactly 128-bits
  33. * and equal to CHUNKCOPY_CHUNK_SIZE.
  34. */
  35. #define CHUNKCOPY_CHUNK_SIZE sizeof(z_vec128i_t)
  36. Z_STATIC_ASSERT(vector_128_bits_wide,
  37. CHUNKCOPY_CHUNK_SIZE == sizeof(int8_t) * 16);
  38. /*
  39. * Ask the compiler to perform a wide, unaligned load with a machine
  40. * instruction appropriate for the z_vec128i_t type.
  41. */
  42. static inline z_vec128i_t loadchunk(
  43. const unsigned char FAR* s) {
  44. z_vec128i_t v;
  45. Z_BUILTIN_MEMCPY(&v, s, sizeof(v));
  46. return v;
  47. }
  48. /*
  49. * Ask the compiler to perform a wide, unaligned store with a machine
  50. * instruction appropriate for the z_vec128i_t type.
  51. */
  52. static inline void storechunk(
  53. unsigned char FAR* d,
  54. const z_vec128i_t v) {
  55. Z_BUILTIN_MEMCPY(d, &v, sizeof(v));
  56. }
  57. /*
  58. * Perform a memcpy-like operation, assuming that length is non-zero and that
  59. * it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE bytes of output even if
  60. * the length is shorter than this.
  61. *
  62. * It also guarantees that it will properly unroll the data if the distance
  63. * between `out` and `from` is at least CHUNKCOPY_CHUNK_SIZE, which we rely on
  64. * in chunkcopy_relaxed().
  65. *
  66. * Aside from better memory bus utilisation, this means that short copies
  67. * (CHUNKCOPY_CHUNK_SIZE bytes or fewer) will fall straight through the loop
  68. * without iteration, which will hopefully make the branch prediction more
  69. * reliable.
  70. */
  71. static inline unsigned char FAR* chunkcopy_core(
  72. unsigned char FAR* out,
  73. const unsigned char FAR* from,
  74. unsigned len) {
  75. const int bump = (--len % CHUNKCOPY_CHUNK_SIZE) + 1;
  76. storechunk(out, loadchunk(from));
  77. out += bump;
  78. from += bump;
  79. len /= CHUNKCOPY_CHUNK_SIZE;
  80. while (len-- > 0) {
  81. storechunk(out, loadchunk(from));
  82. out += CHUNKCOPY_CHUNK_SIZE;
  83. from += CHUNKCOPY_CHUNK_SIZE;
  84. }
  85. return out;
  86. }
  87. /*
  88. * Like chunkcopy_core(), but avoid writing beyond of legal output.
  89. *
  90. * Accepts an additional pointer to the end of safe output. A generic safe
  91. * copy would use (out + len), but it's normally the case that the end of the
  92. * output buffer is beyond the end of the current copy, and this can still be
  93. * exploited.
  94. */
  95. static inline unsigned char FAR* chunkcopy_core_safe(
  96. unsigned char FAR* out,
  97. const unsigned char FAR* from,
  98. unsigned len,
  99. unsigned char FAR* limit) {
  100. Assert(out + len <= limit, "chunk copy exceeds safety limit");
  101. if ((limit - out) < (ptrdiff_t)CHUNKCOPY_CHUNK_SIZE) {
  102. const unsigned char FAR* Z_RESTRICT rfrom = from;
  103. Assert((uintptr_t)out - (uintptr_t)from >= len,
  104. "invalid restrict in chunkcopy_core_safe");
  105. Assert((uintptr_t)from - (uintptr_t)out >= len,
  106. "invalid restrict in chunkcopy_core_safe");
  107. if (len & 8) {
  108. Z_BUILTIN_MEMCPY(out, rfrom, 8);
  109. out += 8;
  110. rfrom += 8;
  111. }
  112. if (len & 4) {
  113. Z_BUILTIN_MEMCPY(out, rfrom, 4);
  114. out += 4;
  115. rfrom += 4;
  116. }
  117. if (len & 2) {
  118. Z_BUILTIN_MEMCPY(out, rfrom, 2);
  119. out += 2;
  120. rfrom += 2;
  121. }
  122. if (len & 1) {
  123. *out++ = *rfrom++;
  124. }
  125. return out;
  126. }
  127. return chunkcopy_core(out, from, len);
  128. }
  129. /*
  130. * Perform short copies until distance can be rewritten as being at least
  131. * CHUNKCOPY_CHUNK_SIZE.
  132. *
  133. * Assumes it's OK to overwrite at least the first 2*CHUNKCOPY_CHUNK_SIZE
  134. * bytes of output even if the copy is shorter than this. This assumption
  135. * holds within zlib inflate_fast(), which starts every iteration with at
  136. * least 258 bytes of output space available (258 being the maximum length
  137. * output from a single token; see inffast.c).
  138. */
  139. static inline unsigned char FAR* chunkunroll_relaxed(
  140. unsigned char FAR* out,
  141. unsigned FAR* dist,
  142. unsigned FAR* len) {
  143. const unsigned char FAR* from = out - *dist;
  144. while (*dist < *len && *dist < CHUNKCOPY_CHUNK_SIZE) {
  145. storechunk(out, loadchunk(from));
  146. out += *dist;
  147. *len -= *dist;
  148. *dist += *dist;
  149. }
  150. return out;
  151. }
  152. #if defined(INFLATE_CHUNK_SIMD_NEON)
  153. /*
  154. * v_load64_dup(): load *src as an unaligned 64-bit int and duplicate it in
  155. * every 64-bit component of the 128-bit result (64-bit int splat).
  156. */
  157. static inline z_vec128i_t v_load64_dup(const void* src) {
  158. return vcombine_u8(vld1_u8(src), vld1_u8(src));
  159. }
  160. /*
  161. * v_load32_dup(): load *src as an unaligned 32-bit int and duplicate it in
  162. * every 32-bit component of the 128-bit result (32-bit int splat).
  163. */
  164. static inline z_vec128i_t v_load32_dup(const void* src) {
  165. int32_t i32;
  166. Z_BUILTIN_MEMCPY(&i32, src, sizeof(i32));
  167. return vreinterpretq_u8_s32(vdupq_n_s32(i32));
  168. }
  169. /*
  170. * v_load16_dup(): load *src as an unaligned 16-bit int and duplicate it in
  171. * every 16-bit component of the 128-bit result (16-bit int splat).
  172. */
  173. static inline z_vec128i_t v_load16_dup(const void* src) {
  174. int16_t i16;
  175. Z_BUILTIN_MEMCPY(&i16, src, sizeof(i16));
  176. return vreinterpretq_u8_s16(vdupq_n_s16(i16));
  177. }
  178. /*
  179. * v_load8_dup(): load the 8-bit int *src and duplicate it in every 8-bit
  180. * component of the 128-bit result (8-bit int splat).
  181. */
  182. static inline z_vec128i_t v_load8_dup(const void* src) {
  183. return vld1q_dup_u8((const uint8_t*)src);
  184. }
  185. /*
  186. * v_store_128(): store the 128-bit vec in a memory destination (that might
  187. * not be 16-byte aligned) void* out.
  188. */
  189. static inline void v_store_128(void* out, const z_vec128i_t vec) {
  190. vst1q_u8(out, vec);
  191. }
  192. #elif defined(INFLATE_CHUNK_SIMD_SSE2)
  193. /*
  194. * v_load64_dup(): load *src as an unaligned 64-bit int and duplicate it in
  195. * every 64-bit component of the 128-bit result (64-bit int splat).
  196. */
  197. static inline z_vec128i_t v_load64_dup(const void* src) {
  198. int64_t i64;
  199. Z_BUILTIN_MEMCPY(&i64, src, sizeof(i64));
  200. return _mm_set1_epi64x(i64);
  201. }
  202. /*
  203. * v_load32_dup(): load *src as an unaligned 32-bit int and duplicate it in
  204. * every 32-bit component of the 128-bit result (32-bit int splat).
  205. */
  206. static inline z_vec128i_t v_load32_dup(const void* src) {
  207. int32_t i32;
  208. Z_BUILTIN_MEMCPY(&i32, src, sizeof(i32));
  209. return _mm_set1_epi32(i32);
  210. }
  211. /*
  212. * v_load16_dup(): load *src as an unaligned 16-bit int and duplicate it in
  213. * every 16-bit component of the 128-bit result (16-bit int splat).
  214. */
  215. static inline z_vec128i_t v_load16_dup(const void* src) {
  216. int16_t i16;
  217. Z_BUILTIN_MEMCPY(&i16, src, sizeof(i16));
  218. return _mm_set1_epi16(i16);
  219. }
  220. /*
  221. * v_load8_dup(): load the 8-bit int *src and duplicate it in every 8-bit
  222. * component of the 128-bit result (8-bit int splat).
  223. */
  224. static inline z_vec128i_t v_load8_dup(const void* src) {
  225. return _mm_set1_epi8(*(const char*)src);
  226. }
  227. /*
  228. * v_store_128(): store the 128-bit vec in a memory destination (that might
  229. * not be 16-byte aligned) void* out.
  230. */
  231. static inline void v_store_128(void* out, const z_vec128i_t vec) {
  232. _mm_storeu_si128((__m128i*)out, vec);
  233. }
  234. #endif
  235. /*
  236. * Perform an overlapping copy which behaves as a memset() operation, but
  237. * supporting periods other than one, and assume that length is non-zero and
  238. * that it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE*3 bytes of output
  239. * even if the length is shorter than this.
  240. */
  241. static inline unsigned char FAR* chunkset_core(
  242. unsigned char FAR* out,
  243. unsigned period,
  244. unsigned len) {
  245. z_vec128i_t v;
  246. const int bump = ((len - 1) % sizeof(v)) + 1;
  247. switch (period) {
  248. case 1:
  249. v = v_load8_dup(out - 1);
  250. v_store_128(out, v);
  251. out += bump;
  252. len -= bump;
  253. while (len > 0) {
  254. v_store_128(out, v);
  255. out += sizeof(v);
  256. len -= sizeof(v);
  257. }
  258. return out;
  259. case 2:
  260. v = v_load16_dup(out - 2);
  261. v_store_128(out, v);
  262. out += bump;
  263. len -= bump;
  264. if (len > 0) {
  265. v = v_load16_dup(out - 2);
  266. do {
  267. v_store_128(out, v);
  268. out += sizeof(v);
  269. len -= sizeof(v);
  270. } while (len > 0);
  271. }
  272. return out;
  273. case 4:
  274. v = v_load32_dup(out - 4);
  275. v_store_128(out, v);
  276. out += bump;
  277. len -= bump;
  278. if (len > 0) {
  279. v = v_load32_dup(out - 4);
  280. do {
  281. v_store_128(out, v);
  282. out += sizeof(v);
  283. len -= sizeof(v);
  284. } while (len > 0);
  285. }
  286. return out;
  287. case 8:
  288. v = v_load64_dup(out - 8);
  289. v_store_128(out, v);
  290. out += bump;
  291. len -= bump;
  292. if (len > 0) {
  293. v = v_load64_dup(out - 8);
  294. do {
  295. v_store_128(out, v);
  296. out += sizeof(v);
  297. len -= sizeof(v);
  298. } while (len > 0);
  299. }
  300. return out;
  301. }
  302. out = chunkunroll_relaxed(out, &period, &len);
  303. return chunkcopy_core(out, out - period, len);
  304. }
  305. /*
  306. * Perform a memcpy-like operation, but assume that length is non-zero and that
  307. * it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE bytes of output even if
  308. * the length is shorter than this.
  309. *
  310. * Unlike chunkcopy_core() above, no guarantee is made regarding the behaviour
  311. * of overlapping buffers, regardless of the distance between the pointers.
  312. * This is reflected in the `restrict`-qualified pointers, allowing the
  313. * compiler to re-order loads and stores.
  314. */
  315. static inline unsigned char FAR* chunkcopy_relaxed(
  316. unsigned char FAR* Z_RESTRICT out,
  317. const unsigned char FAR* Z_RESTRICT from,
  318. unsigned len) {
  319. Assert((uintptr_t)out - (uintptr_t)from >= len,
  320. "invalid restrict in chunkcopy_relaxed");
  321. Assert((uintptr_t)from - (uintptr_t)out >= len,
  322. "invalid restrict in chunkcopy_relaxed");
  323. return chunkcopy_core(out, from, len);
  324. }
  325. /*
  326. * Like chunkcopy_relaxed(), but avoid writing beyond of legal output.
  327. *
  328. * Unlike chunkcopy_core_safe() above, no guarantee is made regarding the
  329. * behaviour of overlapping buffers, regardless of the distance between the
  330. * pointers. This is reflected in the `restrict`-qualified pointers, allowing
  331. * the compiler to re-order loads and stores.
  332. *
  333. * Accepts an additional pointer to the end of safe output. A generic safe
  334. * copy would use (out + len), but it's normally the case that the end of the
  335. * output buffer is beyond the end of the current copy, and this can still be
  336. * exploited.
  337. */
  338. static inline unsigned char FAR* chunkcopy_safe(
  339. unsigned char FAR* out,
  340. const unsigned char FAR* Z_RESTRICT from,
  341. unsigned len,
  342. unsigned char FAR* limit) {
  343. Assert(out + len <= limit, "chunk copy exceeds safety limit");
  344. Assert((uintptr_t)out - (uintptr_t)from >= len,
  345. "invalid restrict in chunkcopy_safe");
  346. Assert((uintptr_t)from - (uintptr_t)out >= len,
  347. "invalid restrict in chunkcopy_safe");
  348. return chunkcopy_core_safe(out, from, len, limit);
  349. }
  350. /*
  351. * Perform chunky copy within the same buffer, where the source and destination
  352. * may potentially overlap.
  353. *
  354. * Assumes that len > 0 on entry, and that it's safe to write at least
  355. * CHUNKCOPY_CHUNK_SIZE*3 bytes to the output.
  356. */
  357. static inline unsigned char FAR* chunkcopy_lapped_relaxed(
  358. unsigned char FAR* out,
  359. unsigned dist,
  360. unsigned len) {
  361. if (dist < len && dist < CHUNKCOPY_CHUNK_SIZE) {
  362. return chunkset_core(out, dist, len);
  363. }
  364. return chunkcopy_core(out, out - dist, len);
  365. }
  366. /*
  367. * Behave like chunkcopy_lapped_relaxed(), but avoid writing beyond of legal
  368. * output.
  369. *
  370. * Accepts an additional pointer to the end of safe output. A generic safe
  371. * copy would use (out + len), but it's normally the case that the end of the
  372. * output buffer is beyond the end of the current copy, and this can still be
  373. * exploited.
  374. */
  375. static inline unsigned char FAR* chunkcopy_lapped_safe(
  376. unsigned char FAR* out,
  377. unsigned dist,
  378. unsigned len,
  379. unsigned char FAR* limit) {
  380. Assert(out + len <= limit, "chunk copy exceeds safety limit");
  381. if ((limit - out) < (ptrdiff_t)(3 * CHUNKCOPY_CHUNK_SIZE)) {
  382. /* TODO(cavalcantii): try harder to optimise this */
  383. while (len-- > 0) {
  384. *out = *(out - dist);
  385. out++;
  386. }
  387. return out;
  388. }
  389. return chunkcopy_lapped_relaxed(out, dist, len);
  390. }
  391. /* TODO(cavalcanti): see crbug.com/1110083. */
  392. static inline unsigned char FAR* chunkcopy_safe_ugly(unsigned char FAR* out,
  393. unsigned dist,
  394. unsigned len,
  395. unsigned char FAR* limit) {
  396. #if defined(__GNUC__) && !defined(__clang__)
  397. /* Speed is the same as using chunkcopy_safe
  398. w/ GCC on ARM (tested gcc 6.3 and 7.5) and avoids
  399. undefined behavior.
  400. */
  401. return chunkcopy_core_safe(out, out - dist, len, limit);
  402. #elif defined(__clang__) && defined(ARMV8_OS_ANDROID) && !defined(__aarch64__)
  403. /* Seems to perform better on 32bit (i.e. Android). */
  404. return chunkcopy_core_safe(out, out - dist, len, limit);
  405. #else
  406. /* Seems to perform better on 64bit. */
  407. return chunkcopy_lapped_safe(out, dist, len, limit);
  408. #endif
  409. }
  410. /*
  411. * The chunk-copy code above deals with writing the decoded DEFLATE data to
  412. * the output with SIMD methods to increase decode speed. Reading the input
  413. * to the DEFLATE decoder with a wide, SIMD method can also increase decode
  414. * speed. This option is supported on little endian machines, and reads the
  415. * input data in 64-bit (8 byte) chunks.
  416. */
  417. #ifdef INFLATE_CHUNK_READ_64LE
  418. /*
  419. * Buffer the input in a uint64_t (8 bytes) in the wide input reading case.
  420. */
  421. typedef uint64_t inflate_holder_t;
  422. /*
  423. * Ask the compiler to perform a wide, unaligned load of a uint64_t using a
  424. * machine instruction appropriate for the uint64_t type.
  425. */
  426. static inline inflate_holder_t read64le(const unsigned char FAR *in) {
  427. inflate_holder_t input;
  428. Z_BUILTIN_MEMCPY(&input, in, sizeof(input));
  429. return input;
  430. }
  431. #else
  432. /*
  433. * Otherwise, buffer the input bits using zlib's default input buffer type.
  434. */
  435. typedef unsigned long inflate_holder_t;
  436. #endif /* INFLATE_CHUNK_READ_64LE */
  437. #undef Z_STATIC_ASSERT
  438. #undef Z_RESTRICT
  439. #undef Z_BUILTIN_MEMCPY
  440. #endif /* CHUNKCOPY_H */