Repository navigation
Expand file tree
/
Copy pathscanner.c
More file actions
118 lines (102 loc) · 4.07 KB
/
Copy pathscanner.c
File metadata and controls
118 lines (102 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* scanner.c -- finding the empty stored block marker in compressed bytes
* For conditions of distribution and use, see LICENSE.md
*/
#include "scanner.h"
#include <string.h>
/* Trailing zero counts for the marker scanners, the compiler's own where it has one. */
#if defined(_MSC_VER) && !defined(__clang__)
# include <intrin.h>
static inline uint32_t gzng_ctz32(uint32_t v) {
unsigned long i;
_BitScanForward(&i, v);
return (uint32_t)i;
}
static inline uint32_t gzng_ctz64(uint64_t v) {
# if defined(_M_X64) || defined(_M_ARM64)
unsigned long i;
_BitScanForward64(&i, v);
return (uint32_t)i;
# else
uint32_t lo = (uint32_t)v;
return lo != 0 ? gzng_ctz32(lo) : 32 + gzng_ctz32((uint32_t)(v >> 32));
# endif
}
#else
static inline uint32_t gzng_ctz32(uint32_t v) {
return (uint32_t)__builtin_ctz(v);
}
static inline uint32_t gzng_ctz64(uint64_t v) {
return (uint32_t)__builtin_ctzll(v);
}
#endif
/* The check behind the zero filter, the marker's remaining bytes, and for the pair scan the
empty stored block behind them. pair is a constant at every call, so each caller gets its own
specialized scan. */
static inline int32_t scan_hit(const uint8_t *q, int32_t pair) {
if (q[1] != 0 || q[2] != 0xff || q[3] != 0xff)
return 0;
return !pair || scan_empty_block(q + 4);
}
/* The scalar scan, also the tail behind the vector ones. Pointer to the first hit starting in
[start, end), or NULL. end + 3 must be readable, end + 8 for the pair scan. */
static inline const uint8_t *scan_scalar(const uint8_t *start, const uint8_t *end, int32_t pair) {
while (start < end && (start = (const uint8_t *)memchr(start, 0, (size_t)(end - start)))) {
if (scan_hit(start, pair))
return start;
start++;
}
return NULL;
}
/* Pointer to the first hit starting in [start, end), or NULL, readable bounds as in scan_scalar.
The vector versions filter for the zero byte the way libc memchr does, one load and one
reduction per 16 bytes, but stay inline so the roughly one hit per 256 bytes that compressed
data produces costs a few cycles instead of a call boundary. Candidates get the full byte
check, real markers are kilobytes apart. */
#if !defined(GZBLOCK_NO_SIMD) && (defined(__aarch64__) || defined(_M_ARM64))
# include <arm_neon.h>
static inline const uint8_t *scan(const uint8_t *start, const uint8_t *end, int32_t pair) {
while (end - start >= 16) {
uint8x16_t v = vld1q_u8(start);
if (vminvq_u8(v) == 0) {
/* four mask bits per lane, the usual movemask substitute */
uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(vceqzq_u8(v)), 4)), 0);
do {
unsigned i = gzng_ctz64(mask) >> 2;
const uint8_t *q = start + i;
if (scan_hit(q, pair))
return q;
mask &= ~(0xfull << (i * 4));
} while (mask != 0);
}
start += 16;
}
return scan_scalar(start, end, pair);
}
#elif !defined(GZBLOCK_NO_SIMD) && (defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2))
# include <emmintrin.h>
static inline const uint8_t *scan(const uint8_t *start, const uint8_t *end, int32_t pair) {
const __m128i zero = _mm_setzero_si128();
while (end - start >= 16) {
unsigned mask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((const __m128i *)start), zero));
while (mask != 0) {
unsigned i = gzng_ctz32(mask);
const uint8_t *q = start + i;
if (scan_hit(q, pair))
return q;
mask &= mask - 1;
}
start += 16;
}
return scan_scalar(start, end, pair);
}
#else
static inline const uint8_t *scan(const uint8_t *start, const uint8_t *end, int32_t pair) {
return scan_scalar(start, end, pair);
}
#endif
const uint8_t *scan_marker(const uint8_t *start, const uint8_t *end) {
return scan(start, end, 0);
}
const uint8_t *scan_marker_pair(const uint8_t *start, const uint8_t *end) {
return scan(start, end, 1);
}