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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*-
* Copyright (c) 2014-2018 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#ifdef HAVE_X86INTRIN_H
#if !defined(_MSC_VER) && !defined(_lint)
#include <x86intrin.h>
#endif
#endif
/* 16B alignment */
#define WT_ALIGNED_16(p) (((uintptr_t)(p) & 0x0f) == 0)
#define WT_VECTOR_SIZE 16 /* chunk size */
#if defined(HAVE_ARM_NEON_INTRIN_H)
#include <arm_neon.h>
/*
* _mm_movemask_epi8_neon --
* Creates a 16-bit mask from the most significant bits of the 16 signed
* or unsigned 8-bit integers.
*/
static inline uint16_t
_mm_movemask_epi8_neon(const uint8x16_t data)
{
uint64x1_t p;
p = vset_lane_u64(0x8040201008040201, p, 0);
uint8x16_t powers = vcombine_u8(p, p);
uint8x16_t zero8x16 = vdupq_n_s8(0);
int8x16_t input = vcltq_s8((int8x16_t)data, (int8x16_t)zero8x16);
uint64x2_t mask = vpaddlq_u32(
vpaddlq_u16(vpaddlq_u8(vandq_u8((uint8x16_t)input, powers))));
uint16_t output;
output =
((vgetq_lane_u8(mask, 8) << 8) | (vgetq_lane_u8(mask, 0) << 0));
return (output);
}
#endif
/*
* __wt_lex_compare --
* Lexicographic comparison routine.
*
* Returns:
* < 0 if user_item is lexicographically < tree_item
* = 0 if user_item is lexicographically = tree_item
* > 0 if user_item is lexicographically > tree_item
*
* We use the names "user" and "tree" so it's clear in the btree code which
* the application is looking at when we call its comparison function.
*/
static inline int
__wt_lex_compare(const WT_ITEM *user_item, const WT_ITEM *tree_item)
{
size_t len, usz, tsz;
const uint8_t *userp, *treep;
usz = user_item->size;
tsz = tree_item->size;
len = WT_MIN(usz, tsz);
userp = user_item->data;
treep = tree_item->data;
#ifdef HAVE_X86INTRIN_H
/* Use vector instructions if we'll execute at least 2 of them. */
if (len >= WT_VECTOR_SIZE * 2) {
size_t remain;
__m128i res_eq, u, t;
remain = len % WT_VECTOR_SIZE;
len -= remain;
if (WT_ALIGNED_16(userp) && WT_ALIGNED_16(treep))
for (; len > 0;
len -= WT_VECTOR_SIZE,
userp += WT_VECTOR_SIZE, treep += WT_VECTOR_SIZE) {
u = _mm_load_si128((const __m128i *)userp);
t = _mm_load_si128((const __m128i *)treep);
res_eq = _mm_cmpeq_epi8(u, t);
if (_mm_movemask_epi8(res_eq) != 65535)
break;
}
else
for (; len > 0;
len -= WT_VECTOR_SIZE,
userp += WT_VECTOR_SIZE, treep += WT_VECTOR_SIZE) {
u = _mm_loadu_si128((const __m128i *)userp);
t = _mm_loadu_si128((const __m128i *)treep);
res_eq = _mm_cmpeq_epi8(u, t);
if (_mm_movemask_epi8(res_eq) != 65535)
break;
}
len += remain;
}
#elif defined(HAVE_ARM_NEON_INTRIN_H)
/* Use vector instructions if we'll execute at least 1 of them. */
if (len >= WT_VECTOR_SIZE) {
size_t remain;
uint8x16_t res_eq, u, t;
remain = len % WT_VECTOR_SIZE;
len -= remain;
for (; len > 0;
len -= WT_VECTOR_SIZE,
userp += WT_VECTOR_SIZE, treep += WT_VECTOR_SIZE) {
u = vld1q_u8(userp);
t = vld1q_u8(treep);
res_eq = vceqq_u8(u, t);
if (_mm_movemask_epi8_neon(res_eq) != 65535)
break;
}
len += remain;
}
#endif
/*
* Use the non-vectorized version for the remaining bytes and for the
* small key sizes.
*/
for (; len > 0; --len, ++userp, ++treep)
if (*userp != *treep)
return (*userp < *treep ? -1 : 1);
/* Contents are equal up to the smallest length. */
return ((usz == tsz) ? 0 : (usz < tsz) ? -1 : 1);
}
/*
* __wt_compare --
* The same as __wt_lex_compare, but using the application's collator
* function when configured.
*/
static inline int
__wt_compare(WT_SESSION_IMPL *session, WT_COLLATOR *collator,
const WT_ITEM *user_item, const WT_ITEM *tree_item, int *cmpp)
{
if (collator == NULL) {
*cmpp = __wt_lex_compare(user_item, tree_item);
return (0);
}
return (collator->compare(
collator, &session->iface, user_item, tree_item, cmpp));
}
/*
* __wt_lex_compare_skip --
* Lexicographic comparison routine, skipping leading bytes.
*
* Returns:
* < 0 if user_item is lexicographically < tree_item
* = 0 if user_item is lexicographically = tree_item
* > 0 if user_item is lexicographically > tree_item
*
* We use the names "user" and "tree" so it's clear in the btree code which
* the application is looking at when we call its comparison function.
*/
static inline int
__wt_lex_compare_skip(
const WT_ITEM *user_item, const WT_ITEM *tree_item, size_t *matchp)
{
size_t len, usz, tsz;
const uint8_t *userp, *treep;
usz = user_item->size;
tsz = tree_item->size;
len = WT_MIN(usz, tsz) - *matchp;
userp = (const uint8_t *)user_item->data + *matchp;
treep = (const uint8_t *)tree_item->data + *matchp;
#ifdef HAVE_X86INTRIN_H
/* Use vector instructions if we'll execute at least 2 of them. */
if (len >= WT_VECTOR_SIZE * 2) {
size_t remain;
__m128i res_eq, u, t;
remain = len % WT_VECTOR_SIZE;
len -= remain;
if (WT_ALIGNED_16(userp) && WT_ALIGNED_16(treep))
for (; len > 0;
len -= WT_VECTOR_SIZE,
userp += WT_VECTOR_SIZE, treep += WT_VECTOR_SIZE,
*matchp += WT_VECTOR_SIZE) {
u = _mm_load_si128((const __m128i *)userp);
t = _mm_load_si128((const __m128i *)treep);
res_eq = _mm_cmpeq_epi8(u, t);
if (_mm_movemask_epi8(res_eq) != 65535)
break;
}
else
for (; len > 0;
len -= WT_VECTOR_SIZE,
userp += WT_VECTOR_SIZE, treep += WT_VECTOR_SIZE,
*matchp += WT_VECTOR_SIZE) {
u = _mm_loadu_si128((const __m128i *)userp);
t = _mm_loadu_si128((const __m128i *)treep);
res_eq = _mm_cmpeq_epi8(u, t);
if (_mm_movemask_epi8(res_eq) != 65535)
break;
}
len += remain;
}
#elif defined(HAVE_ARM_NEON_INTRIN_H)
/* Use vector instructions if we'll execute at least 1 of them. */
if (len >= WT_VECTOR_SIZE) {
size_t remain;
uint8x16_t res_eq, u, t;
remain = len % WT_VECTOR_SIZE;
len -= remain;
if (WT_ALIGNED_16(userp) && WT_ALIGNED_16(treep))
for (; len > 0;
len -= WT_VECTOR_SIZE,
userp += WT_VECTOR_SIZE, treep += WT_VECTOR_SIZE,
*matchp += WT_VECTOR_SIZE) {
u = vld1q_u8(userp);
t = vld1q_u8(treep);
res_eq = vceqq_u8(u, t);
if (_mm_movemask_epi8_neon(res_eq) != 65535)
break;
}
len += remain;
}
#endif
/*
* Use the non-vectorized version for the remaining bytes and for the
* small key sizes.
*/
for (; len > 0; --len, ++userp, ++treep, ++*matchp)
if (*userp != *treep)
return (*userp < *treep ? -1 : 1);
/* Contents are equal up to the smallest length. */
return ((usz == tsz) ? 0 : (usz < tsz) ? -1 : 1);
}
/*
* __wt_compare_skip --
* The same as __wt_lex_compare_skip, but using the application's collator
* function when configured.
*/
static inline int
__wt_compare_skip(WT_SESSION_IMPL *session, WT_COLLATOR *collator,
const WT_ITEM *user_item, const WT_ITEM *tree_item, int *cmpp,
size_t *matchp)
{
if (collator == NULL) {
*cmpp = __wt_lex_compare_skip(user_item, tree_item, matchp);
return (0);
}
return (collator->compare(
collator, &session->iface, user_item, tree_item, cmpp));
}
/*
* __wt_lex_compare_short --
* Lexicographic comparison routine for short keys.
*
* Returns:
* < 0 if user_item is lexicographically < tree_item
* = 0 if user_item is lexicographically = tree_item
* > 0 if user_item is lexicographically > tree_item
*
* We use the names "user" and "tree" so it's clear in the btree code which
* the application is looking at when we call its comparison function.
*/
static inline int
__wt_lex_compare_short(const WT_ITEM *user_item, const WT_ITEM *tree_item)
{
size_t len, usz, tsz;
const uint8_t *userp, *treep;
usz = user_item->size;
tsz = tree_item->size;
len = WT_MIN(usz, tsz);
userp = user_item->data;
treep = tree_item->data;
/*
* The maximum packed uint64_t is 9B, catch row-store objects using
* packed record numbers as keys.
*
* Don't use a #define to compress this case statement: gcc7 complains
* about implicit fallthrough and doesn't support explicit fallthrough
* comments in macros.
*/
#define WT_COMPARE_SHORT_MAXLEN 9
switch (len) {
case 9:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 8:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 7:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 6:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 5:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 4:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 3:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 2:
if (*userp != *treep)
break;
++userp;
++treep;
/* FALLTHROUGH */
case 1:
if (*userp != *treep)
break;
/* Contents are equal up to the smallest length. */
return ((usz == tsz) ? 0 : (usz < tsz) ? -1 : 1);
}
return (*userp < *treep ? -1 : 1);
}
|