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