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
|
#include "bkgraph.h"
static bk_GraphNode *_bkgraph_grow(bk_Graph *f) {
if (f->free) {
f->length++;
f->free--;
} else {
f->length = f->length + 1;
f->free = (f->length >> 1) & 0xFFFFFF;
RESIZE(f->entries, f->length + f->free);
}
return &(f->entries[f->length - 1]);
}
static uint32_t dfs_insert_cells(bk_Block *b, bk_Graph *f, uint32_t *order) {
if (!b || b->_visitstate == VISIT_GRAY) return 0;
if (b->_visitstate == VISIT_BLACK) return b->_height;
b->_visitstate = VISIT_GRAY;
uint32_t height = 0;
for (uint32_t j = 0; j < b->length; j++) {
bk_Cell *cell = &(b->cells[j]);
if (bk_cellIsPointer(cell) && cell->p) {
uint32_t thatHeight = dfs_insert_cells(cell->p, f, order);
if (thatHeight + 1 > height) height = thatHeight + 1;
}
}
bk_GraphNode *e = _bkgraph_grow(f);
e->alias = 0;
e->block = b;
*order += 1;
e->order = *order;
e->height = b->_height = height;
b->_visitstate = VISIT_BLACK;
return height;
}
static int _by_height(const void *_a, const void *_b) {
const bk_GraphNode *a = _a;
const bk_GraphNode *b = _b;
return a->height == b->height ? a->order - b->order : b->height - a->height;
}
static int _by_order(const void *_a, const void *_b) {
const bk_GraphNode *a = _a;
const bk_GraphNode *b = _b;
return a->block && b->block && a->block->_visitstate != b->block->_visitstate // Visited first
? b->block->_visitstate - a->block->_visitstate
: a->block && b->block && a->block->_depth != b->block->_depth // By depth
? a->block->_depth - b->block->_depth
: b->order - a->order; // By order
}
bk_Graph *bk_newGraphFromRootBlock(bk_Block *b) {
bk_Graph *forest;
NEW(forest);
uint32_t tsOrder = 0;
dfs_insert_cells(b, forest, &tsOrder);
qsort(forest->entries, forest->length, sizeof(bk_GraphNode), _by_height);
for (uint32_t j = 0; j < forest->length; j++) {
forest->entries[j].block->_index = j;
forest->entries[j].alias = j;
}
return forest;
}
void bk_delete_Graph(bk_Graph *f) {
if (!f || !f->entries) return;
for (uint32_t j = 0; j < f->length; j++) {
bk_Block *b = f->entries[j].block;
if (b && b->cells) FREE(b->cells);
FREE(b);
}
FREE(f->entries);
FREE(f);
}
static uint32_t gethash(bk_Block *b) {
uint32_t h = 5381;
for (uint32_t j = 0; j < b->length; j++) {
h = ((h << 5) + h) + b->cells[j].t;
h = ((h << 5) + h);
switch (b->cells[j].t) {
case b8:
case b16:
case b32:
h += b->cells[j].z;
break;
case p16:
case p32:
case sp16:
case sp32:
if (b->cells[j].p) { h += b->cells[j].p->_index; }
break;
default:
break;
}
}
return h;
}
static bool compareblock(bk_Block *a, bk_Block *b) {
if (!a && !b) return true;
if (!a || !b) return false;
if (a->length != b->length) return false;
for (uint32_t j = 0; j < a->length; j++) {
if (a->cells[j].t != b->cells[j].t) return false;
switch (a->cells[j].t) {
case b8:
case b16:
case b32:
if (a->cells[j].z != b->cells[j].z) return false;
break;
case p16:
case p32:
case sp16:
case sp32:
if (a->cells[j].p != b->cells[j].p) return false;
break;
default:
break;
}
}
return true;
}
static bool compareEntry(bk_GraphNode *a, bk_GraphNode *b) {
if (a->hash != b->hash) return false;
return compareblock(a->block, b->block);
}
static void replaceptr(bk_Graph *f, bk_Block *b) {
for (uint32_t j = 0; j < b->length; j++) {
switch (b->cells[j].t) {
case p16:
case p32:
case sp16:
case sp32:
if (b->cells[j].p) {
uint32_t index = b->cells[j].p->_index;
while (f->entries[index].alias != index) {
index = f->entries[index].alias;
}
b->cells[j].p = f->entries[index].block;
}
break;
default:
break;
}
}
}
void bk_minimizeGraph(bk_Graph *f) {
uint32_t rear = (uint32_t)(f->length - 1);
while (rear > 0) {
uint32_t front = rear;
while (f->entries[front].height == f->entries[rear].height && front > 0) {
front--;
}
front++;
for (uint32_t j = front; j <= rear; j++) {
f->entries[j].hash = gethash(f->entries[j].block);
}
for (uint32_t j = front; j <= rear; j++) {
bk_GraphNode *a = &(f->entries[j]);
if (a->alias == j) {
for (uint32_t k = j + 1; k <= rear; k++) {
bk_GraphNode *b = &(f->entries[k]);
if (b->alias == k && compareEntry(a, b)) { b->alias = j; }
}
}
}
// replace pointers with aliased
for (uint32_t j = 0; j < front; j++) {
replaceptr(f, f->entries[j].block);
}
rear = front - 1;
}
}
static size_t otfcc_bkblock_size(bk_Block *b) {
size_t size = 0;
for (uint32_t j = 0; j < b->length; j++)
switch (b->cells[j].t) {
case b8:
size += 1;
break;
case b16:
case p16:
case sp16:
size += 2;
break;
case b32:
case p32:
case sp32:
size += 4;
break;
default:
break;
}
return size;
}
static uint32_t getoffset(size_t *offsets, bk_Block *ref, bk_Block *target, uint8_t bits) {
size_t offref = offsets[ref->_index];
size_t offtgt = offsets[target->_index];
/*
if (offtgt < offref || (offtgt - offref) >> bits) {
fprintf(stderr, "[otfcc-fea] Warning : Unable to fit offset %d into %d bits.\n",
(int32_t)(offtgt - offref), bits);
}
*/
return (uint32_t)(offtgt - offref);
}
static int64_t getoffset_untangle(size_t *offsets, bk_Block *ref, bk_Block *target) {
size_t offref = offsets[ref->_index];
size_t offtgt = offsets[target->_index];
return (int64_t)(offtgt - offref);
}
static void escalate_sppointers(bk_Block *b, bk_Graph *f, uint32_t *order, uint32_t depth) {
if (!b) return;
for (uint32_t j = 0; j < b->length; j++) {
bk_Cell *cell = &(b->cells[j]);
if (bk_cellIsPointer(cell) && cell->p && cell->t >= sp16) {
escalate_sppointers(cell->p, f, order, depth);
}
}
b->_depth = depth;
*order += 1;
f->entries[b->_index].order = *order;
}
static void dfs_attract_cells(bk_Block *b, bk_Graph *f, uint32_t *order, uint32_t depth) {
if (!b) return;
if (b->_visitstate != VISIT_WHITE) {
if (b->_depth < depth) { b->_depth = depth; }
return;
}
b->_visitstate = VISIT_GRAY;
for (uint32_t j = b->length; j-- > 0;) {
bk_Cell *cell = &(b->cells[j]);
if (bk_cellIsPointer(cell) && cell->p) { dfs_attract_cells(cell->p, f, order, depth + 1); }
}
*order += 1;
f->entries[b->_index].order = *order;
escalate_sppointers(b, f, order, depth);
b->_visitstate = VISIT_BLACK;
}
static void attract_bkgraph(bk_Graph *f) {
// Clear the visit state of all blocks
for (uint32_t j = 0; j < f->length; j++) {
f->entries[j].block->_visitstate = VISIT_WHITE;
f->entries[j].order = 0;
f->entries[j].block->_index = j;
f->entries[j].block->_depth = 0;
}
uint32_t order = 0;
dfs_attract_cells(f->entries[0].block, f, &order, 0);
qsort(f->entries, f->length, sizeof(bk_GraphNode), _by_order);
for (uint32_t j = 0; j < f->length; j++) {
f->entries[j].block->_index = j;
}
}
static bool try_untabgle_block(bk_Graph *f, bk_Block *b, size_t *offsets, uint16_t passes) {
bool didCopy = false;
for (uint32_t j = 0; j < b->length; j++) {
switch (b->cells[j].t) {
case p16:
case sp16:
if (b->cells[j].p) {
int64_t offset = getoffset_untangle(offsets, b, b->cells[j].p);
if (offset < 0 || offset > 0xFFFF) {
bk_GraphNode *e = _bkgraph_grow(f);
e->order = 0;
e->alias = 0;
e->block = bk_new_Block(bkcopy, b->cells[j].p, bkover);
b->cells[j].t = sp16;
b->cells[j].p = e->block;
didCopy = true;
}
}
break;
default:
break;
}
}
return didCopy;
}
static bool try_untangle(bk_Graph *f, uint16_t passes) {
size_t *offsets;
NEW(offsets, f->length + 1);
offsets[0] = 0;
for (uint32_t j = 0; j < f->length; j++) {
if (f->entries[j].block->_visitstate == VISIT_BLACK) {
offsets[j + 1] = offsets[j] + otfcc_bkblock_size(f->entries[j].block);
} else {
offsets[j + 1] = offsets[j];
}
}
uint32_t totalBlocks = f->length;
bool didUntangle = false;
for (uint32_t j = 0; j < totalBlocks; j++) {
if (f->entries[j].block->_visitstate == VISIT_BLACK) {
bool didCopy = try_untabgle_block(f, f->entries[j].block, offsets, passes);
didUntangle = didUntangle || didCopy;
}
}
FREE(offsets);
return didUntangle;
}
static void otfcc_build_bkblock(caryll_Buffer *buf, bk_Block *b, size_t *offsets) {
for (uint32_t j = 0; j < b->length; j++) {
switch (b->cells[j].t) {
case b8:
bufwrite8(buf, b->cells[j].z);
break;
case b16:
bufwrite16b(buf, b->cells[j].z);
break;
case b32:
bufwrite32b(buf, b->cells[j].z);
break;
case p16:
case sp16:
if (b->cells[j].p) {
bufwrite16b(buf, getoffset(offsets, b, b->cells[j].p, 16));
} else {
bufwrite16b(buf, 0);
}
break;
case p32:
case sp32:
if (b->cells[j].p) {
bufwrite32b(buf, getoffset(offsets, b, b->cells[j].p, 32));
} else {
bufwrite32b(buf, 0);
}
break;
default:
break;
}
}
}
caryll_Buffer *bk_build_Graph(bk_Graph *f) {
caryll_Buffer *buf = bufnew();
size_t *offsets;
NEW(offsets, f->length + 1);
offsets[0] = 0;
for (uint32_t j = 0; j < f->length; j++) {
if (f->entries[j].block->_visitstate == VISIT_BLACK) {
offsets[j + 1] = offsets[j] + otfcc_bkblock_size(f->entries[j].block);
} else {
offsets[j + 1] = offsets[j];
}
}
for (uint32_t j = 0; j < f->length; j++) {
if (f->entries[j].block->_visitstate == VISIT_BLACK) {
otfcc_build_bkblock(buf, f->entries[j].block, offsets);
}
}
FREE(offsets);
return buf;
}
size_t bk_estimateSizeOfGraph(bk_Graph *f) {
size_t *offsets;
NEW(offsets, f->length + 1);
offsets[0] = 0;
for (uint32_t j = 0; j < f->length; j++) {
if (f->entries[j].block->_visitstate == VISIT_BLACK) {
offsets[j + 1] = offsets[j] + otfcc_bkblock_size(f->entries[j].block);
} else {
offsets[j + 1] = offsets[j];
}
}
size_t estimatedSize = offsets[f->length];
FREE(offsets);
return estimatedSize;
}
void bk_untangleGraph(/*BORROW*/ bk_Graph *f) {
uint16_t passes = 0;
bool tangled = false;
attract_bkgraph(f);
do {
tangled = try_untangle(f, passes);
if (tangled) { attract_bkgraph(f); }
passes++;
} while (tangled && passes < 16);
}
caryll_Buffer *bk_build_Block(/*MOVE*/ bk_Block *root) {
bk_Graph *f = bk_newGraphFromRootBlock(root);
bk_minimizeGraph(f);
bk_untangleGraph(f);
caryll_Buffer *buf = bk_build_Graph(f);
bk_delete_Graph(f);
return buf;
}
caryll_Buffer *bk_build_Block_noMinimize(/*MOVE*/ bk_Block *root) {
bk_Graph *f = bk_newGraphFromRootBlock(root);
bk_untangleGraph(f);
caryll_Buffer *buf = bk_build_Graph(f);
bk_delete_Graph(f);
return buf;
}
|