-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopmtpt.c
550 lines (510 loc) · 16.7 KB
/
popmtpt.c
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*_
* Copyright (c) 2019-2020 Hirochika Asai <[email protected]>
* All rights reserved.
*/
#include "palmtrie.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <immintrin.h>
/* 64-bit popcnt intrinsic. To use popcnt instruction in x86-64, the "-mpopcnt"
option must be specified in CFLAGS. */
#define popcnt(v) __builtin_popcountll(v)
#define PALMTRIE_POPMTPT_NR_NODES (1 << 23)
#define _STACK_DEPTH 64
/*
* Check if the node is compressible or not
* Return value:
* -1: Not compressible
* 0: Compressible NULL
* 1: Compressible non-NULL
*/
static struct palmtrie_mtpt_node_data *
_compressible_leaf(struct palmtrie_mtpt_node_data *n)
{
int i;
uint64_t cn;
uint64_t cl;
uint64_t tn;
uint64_t tl;
struct palmtrie_mtpt_node_data *pn;
pn = NULL;
cn = 0;
cl = 0;
for ( i = 0; i < (1 << PALMTRIE_MTPT_STRIDE); i++ ) {
if ( NULL != n->children[i] ) {
if ( n->bit > n->children[i]->bit ) {
cn++;
} else {
pn = n->children[i];
cl++;
}
}
}
tn = 0;
tl = 0;
for ( i = 0; i < (1 << PALMTRIE_MTPT_STRIDE) - 1; i++ ) {
if ( NULL != n->ternaries[i] ) {
if ( n->bit > n->ternaries[i]->bit ) {
tn++;
} else {
pn = n->ternaries[i];
tl++;
}
}
}
if ( cn || tn ) {
return NULL;
}
if ( cl == 1 && tl == 0 ) {
return pn;
}
if ( cl == 0 && tl == 1 ) {
return pn;
}
return NULL;
}
/*
* Traverse the trie to compile the optimized trie
*/
static int
_traverse_node(struct palmtrie_popmtpt *t, struct palmtrie_popmtpt_node *pn,
struct palmtrie_mtpt_node_data *n)
{
int i;
int pos;
uint64_t bitmap[(1 << PALMTRIE_MTPT_STRIDE) >> 6];
struct palmtrie_popmtpt_node *c;
struct palmtrie_mtpt_node_data *cl;
if ( NULL == n ) {
return -1;
}
pn->bit = n->bit;
#if PALMTRIE_PRIORITY_SKIP
pn->u.inode.max_priority = n->max_priority;
#endif
/* Binary */
bitmap[0] = 0;
for ( i = 0; i < ((1 << PALMTRIE_MTPT_STRIDE) >> 6); i++ ) {
bitmap[i] = 0;
}
for ( i = 0; i < (1 << PALMTRIE_MTPT_STRIDE); i++ ) {
if ( NULL != n->children[i] ) {
if ( n->bit > n->children[i]->bit
&& !_compressible_leaf(n->children[i]) ) {
/* Node */
bitmap[i >> 6] |= (1ULL << (i & 0x3f));
} else {
/* Leaf */
bitmap[i >> 6] |= (1ULL << (i & 0x3f));
}
}
}
c = &t->nodes.ptr[t->nodes.used];
#if PALMTRIE_MTPT_STRIDE == 8 && PALMTRIE_STRIDE_OPT
pn->u.inode.cbase = t->nodes.used;
int cbase = 0;
for ( i = 0; i < ((1 << PALMTRIE_MTPT_STRIDE) >> 6); i++ ) {
pn->u.inode.bitmap_c[i] = bitmap[i];
pn->u.inode.children[i] = cbase;
cbase += popcnt(bitmap[i]);
t->nodes.used += popcnt(bitmap[i]);
if ( t->nodes.used > t->nodes.nr ) {
fprintf(stderr, "Too many nodes\n");
return -1;
}
}
#elif PALMTRIE_MTPT_STRIDE >= 6
for ( i = 0; i < ((1 << PALMTRIE_MTPT_STRIDE) >> 6); i++ ) {
pn->u.inode.children[i] = t->nodes.used;
pn->u.inode.bitmap_c[i] = bitmap[i];
t->nodes.used += popcnt(bitmap[i]);
if ( t->nodes.used > t->nodes.nr ) {
fprintf(stderr, "Too many nodes\n");
return -1;
}
}
#else
pn->u.inode.children[0] = t->nodes.used;
pn->u.inode.bitmap_c[0] = bitmap[0];
t->nodes.used += popcnt(bitmap[0]);
if ( t->nodes.used > t->nodes.nr ) {
fprintf(stderr, "Too many nodes\n");
return -1;
}
#endif
pos = 0;
for ( i = 0; i < (1 << PALMTRIE_MTPT_STRIDE); i++ ) {
if ( NULL != n->children[i] ) {
if ( n->bit > n->children[i]->bit ) {
cl = _compressible_leaf(n->children[i]);
if ( cl ) {
/* Leaf */
c[pos].bit = -PALMTRIE_MTPT_STRIDE - 1;
c[pos].u.leaf.addr = cl->addr;
c[pos].u.leaf.mask = cl->mask;
c[pos].u.leaf.data = cl->data;
c[pos].u.leaf.priority = cl->priority;
pos++;
} else {
/* Traverse */
_traverse_node(t, &c[pos], n->children[i]);
pos++;
}
} else {
/* Leaf */
c[pos].bit = -PALMTRIE_MTPT_STRIDE - 1;
c[pos].u.leaf.addr = n->children[i]->addr;
c[pos].u.leaf.mask = n->children[i]->mask;
c[pos].u.leaf.data = n->children[i]->data;
c[pos].u.leaf.priority = n->children[i]->priority;
pos++;
}
}
}
/* Ternary */
bitmap[0] = 0;
for ( i = 0; i < ((1 << PALMTRIE_MTPT_STRIDE) >> 6); i++ ) {
bitmap[i] = 0;
}
for ( i = 0; i < (1 << PALMTRIE_MTPT_STRIDE) - 1; i++ ) {
if ( NULL != n->ternaries[i] ) {
if ( n->bit > n->ternaries[i]->bit
&& !_compressible_leaf(n->ternaries[i]) ) {
bitmap[i >> 6] |= (1ULL << (i & 0x3f));
} else {
/* Leaf */
bitmap[i >> 6] |= (1ULL << (i & 0x3f));
}
}
}
c = &t->nodes.ptr[t->nodes.used];
#if PALMTRIE_MTPT_STRIDE == 8 && PALMTRIE_STRIDE_OPT
int tbase = 0;
pn->u.inode.tbase = t->nodes.used;
for ( i = 0; i < ((1 << PALMTRIE_MTPT_STRIDE) >> 6); i++ ) {
pn->u.inode.bitmap_t[i] = bitmap[i];
pn->u.inode.ternaries[i] = tbase;
tbase += popcnt(bitmap[i]);
t->nodes.used += popcnt(bitmap[i]);
if ( t->nodes.used > t->nodes.nr ) {
fprintf(stderr, "Too many nodes\n");
return -1;
}
}
#elif PALMTRIE_MTPT_STRIDE >= 6
for ( i = 0; i < ((1 << PALMTRIE_MTPT_STRIDE) >> 6); i++ ) {
pn->u.inode.ternaries[i] = t->nodes.used;
pn->u.inode.bitmap_t[i] = bitmap[i];
t->nodes.used += popcnt(bitmap[i]);
if ( t->nodes.used > t->nodes.nr ) {
fprintf(stderr, "Too many nodes\n");
return -1;
}
}
#else
pn->u.inode.ternaries[0] = t->nodes.used;
pn->u.inode.bitmap_t[0] = bitmap[0];
t->nodes.used += popcnt(bitmap[0]);
if ( t->nodes.used > t->nodes.nr ) {
fprintf(stderr, "Too many nodes\n");
return -1;
}
#endif
pos = 0;
for ( i = 0; i < (1 << PALMTRIE_MTPT_STRIDE) - 1; i++ ) {
if ( NULL != n->ternaries[i] ) {
if ( n->bit > n->ternaries[i]->bit ) {
cl = _compressible_leaf(n->ternaries[i]);
if ( cl ) {
c[pos].bit = -PALMTRIE_MTPT_STRIDE - 1;
c[pos].u.leaf.addr = cl->addr;
c[pos].u.leaf.mask = cl->mask;
c[pos].u.leaf.data = cl->data;
c[pos].u.leaf.priority = cl->priority;
pos++;
} else {
/* Traverse */
_traverse_node(t, &c[pos], n->ternaries[i]);
pos++;
}
} else {
/* Terminate */
c[pos].bit = -PALMTRIE_MTPT_STRIDE - 1;
c[pos].u.leaf.addr = n->ternaries[i]->addr;
c[pos].u.leaf.mask = n->ternaries[i]->mask;
c[pos].u.leaf.data = n->ternaries[i]->data;
c[pos].u.leaf.priority = n->ternaries[i]->priority;
pos++;
}
}
}
return 0;
}
/*
* Convert the multiway trie to the optimized trie
*/
static int
_convert(struct palmtrie_popmtpt *popmtpt)
{
struct palmtrie_popmtpt_node *c;
int ret;
popmtpt->nodes.nr = PALMTRIE_POPMTPT_NR_NODES;
popmtpt->nodes.ptr = malloc(sizeof(struct palmtrie_popmtpt_node)
* popmtpt->nodes.nr);
if ( NULL == popmtpt->nodes.ptr ) {
fprintf(stderr, "Memory allocation error\n");
return -1;
}
popmtpt->nodes.used = 0;
c = &popmtpt->nodes.ptr[popmtpt->nodes.used];
popmtpt->root = popmtpt->nodes.used;
popmtpt->nodes.used++;
ret = _traverse_node(popmtpt, c, popmtpt->mtpt.root);
if ( ret < 0 ) {
return -1;
}
return 0;
}
/*
* Lookup an entry corresponding to the specified address
*/
static struct palmtrie_popmtpt_node *
_lookup(struct palmtrie_popmtpt *t, struct palmtrie_popmtpt_node *node,
addr_t addr, struct palmtrie_popmtpt_node *res)
{
int sidx;
int idx;
int i;
struct palmtrie_popmtpt_node *c;
int nr;
int tmp;
int pidx;
struct palmtrie_popmtpt_node **ptrs;
ptrs = alloca(sizeof(struct palmtrie_popmtpt_node *) * _STACK_DEPTH);
if ( __builtin_expect(!!(NULL == node), 0) ) {
return res;
}
nr = 0;
ptrs[nr++] = node;
while ( nr > 0 ) {
nr--;
node = ptrs[nr];
if ( node->bit < -PALMTRIE_MTPT_STRIDE ) {
/* Leaf */
if ( node->u.leaf.priority > res->u.leaf.priority &&
ADDR_MASK_CMP2(addr, node->u.leaf.mask, node->u.leaf.addr) ) {
res = node;
}
continue;
}
#if PALMTRIE_PRIORITY_SKIP
if ( res->u.leaf.priority >= node->u.inode.max_priority ) {
continue;
}
#endif
#if PALMTRIE_MTPT_STRIDE == 8 && PALMTRIE_STRIDE_OPT
uint32_t base;
sidx = EXTRACTN(addr, node->bit, PALMTRIE_MTPT_STRIDE);
idx = (sidx >> 1) | (1 << (PALMTRIE_MTPT_STRIDE - 1));
if ( node->u.inode.bitmap_t[0] ) {
for ( i = 7; i >= 2; i-- ) {
tmp = (idx >> i) - 1;
if ( (1ULL << (tmp & 0x3f))
& node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
base = node->u.inode.tbase
+ node->u.inode.ternaries[tmp >> 6];
c = &t->nodes.ptr[base];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
}
}
tmp = (idx >> 1) - 1;
if ( node->u.inode.bitmap_t[tmp >> 6] &&
(1ULL << (tmp & 0x3f)) & node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
base = node->u.inode.tbase + node->u.inode.ternaries[tmp >> 6];
c = &t->nodes.ptr[base];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
tmp = (idx >> 0) - 1;
if ( node->u.inode.bitmap_t[tmp >> 6] &&
(1ULL << (tmp & 0x3f)) & node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
base = node->u.inode.tbase + node->u.inode.ternaries[tmp >> 6];
c = &t->nodes.ptr[base];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
idx = sidx;
if ( (1ULL << (idx & 0x3f)) & node->u.inode.bitmap_c[idx >> 6] ) {
/* There's a child node */
pidx = popcnt(((1ULL << (idx & 0x3f)) - 1)
& node->u.inode.bitmap_c[idx >> 6]);
base = node->u.inode.cbase + node->u.inode.children[idx >> 6];
c = &t->nodes.ptr[base];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
#else
/* Sort by priority (roughly) */
sidx = EXTRACTN(addr, node->bit, PALMTRIE_MTPT_STRIDE);
#if PALMTRIE_EXACTMATCH_FIRST
idx = sidx;
if ( (1ULL << (idx & 0x3f)) & node->u.inode.bitmap_c[idx >> 6] ) {
/* There's a child node */
pidx = popcnt(((1ULL << (idx & 0x3f)) - 1)
& node->u.inode.bitmap_c[idx >> 6]);
c = &t->nodes.ptr[node->u.inode.children[idx >> 6]];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
#endif
idx = (sidx >> 1) | (1 << (PALMTRIE_MTPT_STRIDE - 1));
#if PALMTRIE_MTPT_STRIDE <= 6
#define TERNARY_CONDITION_BEGIN if ( node->u.inode.bitmap_t[0] ) {
#define TERNARY_CONDITION_END }
#elif PALMTRIE_MTPT_STRIDE == 7
#define TERNARY_CONDITION_BEGIN \
if ( node->u.inode.bitmap_t[0] || node->u.inode.bitmap_t[1] ) {
#define TERNARY_CONDITION_END }
#elif PALMTRIE_MTPT_STRIDE == 8
#define TERNARY_CONDITION_BEGIN \
if ( node->u.inode.bitmap_t[0] || node->u.inode.bitmap_t[1] \
|| node->u.inode.bitmap_t[2] || node->u.inode.bitmap_t[3] ) {
#define TERNARY_CONDITION_END }
#else
#define TERNARY_CONDITION_BEGIN
#define TERNARY_CONDITION_END
#endif
#if PALMTRIE_MTPT_STRIDE == 8
if ( node->u.inode.bitmap_t[0]) {
for ( i = 7; i >= 2; i-- ) {
tmp = (idx >> i) - 1;
if ( (1ULL << (tmp & 0x3f))
& node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
c = &t->nodes.ptr[node->u.inode.ternaries[tmp >> 6]];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
}
}
tmp = (idx >> 1) - 1;
if ( node->u.inode.bitmap_t[tmp >> 6] &&
(1ULL << (tmp & 0x3f)) & node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
c = &t->nodes.ptr[node->u.inode.ternaries[tmp >> 6]];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
tmp = (idx >> 0) - 1;
if ( node->u.inode.bitmap_t[tmp >> 6] &&
(1ULL << (tmp & 0x3f)) & node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
c = &t->nodes.ptr[node->u.inode.ternaries[tmp >> 6]];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
#else
TERNARY_CONDITION_BEGIN
for ( i = PALMTRIE_MTPT_STRIDE - 1; i >= 0; i-- ) {
tmp = (idx >> i) - 1;
if ( (1ULL << (tmp & 0x3f)) & node->u.inode.bitmap_t[tmp >> 6] ) {
pidx = popcnt(((1ULL << (tmp & 0x3f)) - 1)
& node->u.inode.bitmap_t[tmp >> 6]);
c = &t->nodes.ptr[node->u.inode.ternaries[tmp >> 6]];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
}
TERNARY_CONDITION_END
#endif
#if !PALMTRIE_EXACTMATCH_FIRST
idx = sidx;
if ( (1ULL << (idx & 0x3f)) & node->u.inode.bitmap_c[idx >> 6] ) {
/* There's a child node */
pidx = popcnt(((1ULL << (idx & 0x3f)) - 1)
& node->u.inode.bitmap_c[idx >> 6]);
c = &t->nodes.ptr[node->u.inode.children[idx >> 6]];
__builtin_prefetch(&c[pidx], 0, 3);
ptrs[nr] = &c[pidx];
nr++;
}
#endif
#endif
if ( __builtin_expect(!!(nr >= _STACK_DEPTH), 0) ) {
fprintf(stderr, "Fatal error: Stack overflow\n");
}
}
return res;
}
void *
palmtrie_popmtpt_lookup(struct palmtrie_popmtpt *t, addr_t addr)
{
struct palmtrie_popmtpt_node *n;
struct palmtrie_popmtpt_node sentinel;
sentinel.u.leaf.data = NULL;
sentinel.u.leaf.priority = -1;
n = &t->nodes.ptr[t->root];
return _lookup(t, n, addr, &sentinel)->u.leaf.data;
}
/*
* Add an entry to the trie
*/
int
palmtrie_popmtpt_add(struct palmtrie_popmtpt *mtpt, addr_t addr, addr_t mask,
int priority, void *data)
{
int ret;
ret = palmtrie_mtpt_add(&mtpt->mtpt, addr, mask, priority, data);
if ( ret < 0 ){
return -1;
}
return 0;
}
/*
* Compile the optimized trie
*/
int
palmtrie_popmtpt_commit(struct palmtrie_popmtpt *mtpt)
{
int ret;
if ( NULL != mtpt->nodes.ptr ) {
free(mtpt->nodes.ptr);
mtpt->nodes.used = 0;
mtpt->nodes.nr = 0;
mtpt->root = 0;
}
ret = _convert(mtpt);
if ( ret < 0 ) {
return -1;
}
return 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/