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
|
import * as assert from 'assert';
import * as frontend from 'llparse-frontend';
import { Compilation } from '../compilation';
import { Node } from './base';
const MAX_CHAR = 0xff;
const TABLE_GROUP = 16;
// _mm_cmpestri takes 8 ranges
const SSE_RANGES_LEN = 16;
// _mm_cmpestri takes 128bit input
const SSE_RANGES_PAD = 16;
const MAX_SSE_CALLS = 2;
const SSE_ALIGNMENT = 16;
interface ITable {
readonly name: string;
readonly declaration: ReadonlyArray<string>;
}
export class TableLookup extends Node<frontend.node.TableLookup> {
public doBuild(out: string[]): void {
const ctx = this.compilation;
const table = this.buildTable();
for (const line of table.declaration) {
out.push(line);
}
this.prologue(out);
const transform = ctx.unwrapTransform(this.ref.transform!);
// Try to vectorize nodes matching characters and looping to themselves
// NOTE: `switch` below triggers when there is not enough characters in the
// stream for vectorized processing.
this.buildSSE(out);
const current = transform.build(ctx, `*${ctx.posArg()}`);
out.push(`switch (${table.name}[(uint8_t) ${current}]) {`);
for (const [ index, edge ] of this.ref.edges.entries()) {
out.push(` case ${index + 1}: {`);
const tmp: string[] = [];
const edge = this.ref.edges[index];
this.tailTo(tmp, {
noAdvance: edge.noAdvance,
node: edge.node,
value: undefined,
});
ctx.indent(out, tmp, ' ');
out.push(' }');
}
out.push(` default: {`);
const tmp: string[] = [];
this.tailTo(tmp, this.ref.otherwise!);
ctx.indent(out, tmp, ' ');
out.push(' }');
out.push('}');
}
private buildSSE(out: string[]): boolean {
const ctx = this.compilation;
// Transformation is not supported atm
if (this.ref.transform && this.ref.transform.ref.name !== 'id') {
return false;
}
if (this.ref.edges.length !== 1) {
return false;
}
const edge = this.ref.edges[0];
if (edge.node.ref !== this.ref) {
return false;
}
// NOTE: keys are sorted
let ranges: number[] = [];
let first: number | undefined;
let last: number | undefined;
for (const key of edge.keys) {
if (first === undefined) {
first = key;
}
if (last === undefined) {
last = key;
}
if (key - last > 1) {
ranges.push(first, last);
first = key;
}
last = key;
}
if (first !== undefined && last !== undefined) {
ranges.push(first, last);
}
if (ranges.length === 0) {
return false;
}
// Way too many calls would be required
if (ranges.length > MAX_SSE_CALLS * SSE_RANGES_LEN) {
return false;
}
out.push('#ifdef __SSE4_2__');
out.push(`if (${ctx.endPosArg()} - ${ctx.posArg()} >= 16) {`);
out.push(' __m128i ranges;');
out.push(' __m128i input;');
out.push(' int avail;');
out.push(' int match_len;');
out.push('');
out.push(' /* Load input */');
out.push(` input = _mm_loadu_si128((__m128i const*) ${ctx.posArg()});`);
for (let off = 0; off < ranges.length; off += SSE_RANGES_LEN) {
const subRanges = ranges.slice(off, off + SSE_RANGES_LEN);
let paddedRanges = subRanges.slice();
while (paddedRanges.length < SSE_RANGES_PAD) {
paddedRanges.push(0);
}
const blob = ctx.blob(Buffer.from(paddedRanges), SSE_ALIGNMENT);
out.push(` ranges = _mm_loadu_si128((__m128i const*) ${blob});`);
out.push('');
out.push(' /* Find first character that does not match `ranges` */');
out.push(` match_len = _mm_cmpestri(ranges, ${subRanges.length},`);
out.push(' input, 16,');
out.push(' _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES |');
out.push(' _SIDD_NEGATIVE_POLARITY);');
out.push('');
out.push(' if (match_len != 0) {');
out.push(` ${ctx.posArg()} += match_len;`);
const tmp: string[] = [];
assert.strictEqual(edge.noAdvance, false);
this.tailTo(tmp, {
noAdvance: true,
node: edge.node,
});
ctx.indent(out, tmp, ' ');
out.push(' }');
}
{
const tmp: string[] = [];
this.tailTo(tmp, this.ref.otherwise!);
ctx.indent(out, tmp, ' ');
}
out.push('}');
out.push('#endif /* __SSE4_2__ */');
return true;
}
private buildTable(): ITable {
const table: number[] = new Array(MAX_CHAR + 1).fill(0);
for (const [ index, edge ] of this.ref.edges.entries()) {
edge.keys.forEach((key) => {
assert.strictEqual(table[key], 0);
table[key] = index + 1;
});
}
const lines = [
'static uint8_t lookup_table[] = {',
];
for (let i = 0; i < table.length; i += TABLE_GROUP) {
let line = ` ${table.slice(i, i + TABLE_GROUP).join(', ')}`;
if (i + TABLE_GROUP < table.length) {
line += ',';
}
lines.push(line);
}
lines.push('};');
return {
name: 'lookup_table',
declaration: lines,
};
}
}
|