| 1 |
#include "bkgraph.h" |
| 2 |
|
| 3 |
static bk_GraphNode *_bkgraph_grow(bk_Graph *f) { |
| 4 |
if (f->free) { |
| 5 |
f->length++; |
| 6 |
f->free--; |
| 7 |
} else { |
| 8 |
f->length = f->length + 1; |
| 9 |
f->free = (f->length >> 1) & 0xFFFFFF; |
| 10 |
RESIZE(f->entries, f->length + f->free); |
| 11 |
} |
| 12 |
return &(f->entries[f->length - 1]); |
| 13 |
} |
| 14 |
|
| 15 |
static uint32_t dfs_insert_cells(bk_Block *b, bk_Graph *f, uint32_t *order) { |
| 16 |
if (!b || b->_visitstate == VISIT_GRAY) return 0; |
| 17 |
if (b->_visitstate == VISIT_BLACK) return b->_height; |
| 18 |
b->_visitstate = VISIT_GRAY; |
| 19 |
uint32_t height = 0; |
| 20 |
for (uint32_t j = 0; j < b->length; j++) { |
| 21 |
bk_Cell *cell = &(b->cells[j]); |
| 22 |
if (bk_cellIsPointer(cell) && cell->p) { |
| 23 |
uint32_t thatHeight = dfs_insert_cells(cell->p, f, order); |
| 24 |
if (thatHeight + 1 > height) height = thatHeight + 1; |
| 25 |
} |
| 26 |
} |
| 27 |
bk_GraphNode *e = _bkgraph_grow(f); |
| 28 |
e->alias = 0; |
| 29 |
e->block = b; |
| 30 |
*order += 1; |
| 31 |
e->order = *order; |
| 32 |
e->height = b->_height = height; |
| 33 |
b->_visitstate = VISIT_BLACK; |
| 34 |
return height; |
| 35 |
} |
| 36 |
|
| 37 |
static int _by_height(const void *_a, const void *_b) { |
| 38 |
const bk_GraphNode *a = _a; |
| 39 |
const bk_GraphNode *b = _b; |
| 40 |
return a->height == b->height ? a->order - b->order : b->height - a->height; |
| 41 |
} |
| 42 |
|
| 43 |
static int _by_order(const void *_a, const void *_b) { |
| 44 |
const bk_GraphNode *a = _a; |
| 45 |
const bk_GraphNode *b = _b; |
| 46 |
return a->block && b->block && a->block->_visitstate != b->block->_visitstate // Visited first |
| 47 |
? b->block->_visitstate - a->block->_visitstate |
| 48 |
: a->block && b->block && a->block->_depth != b->block->_depth // By depth |
| 49 |
? a->block->_depth - b->block->_depth |
| 50 |
: b->order - a->order; // By order |
| 51 |
} |
| 52 |
|
| 53 |
bk_Graph *bk_newGraphFromRootBlock(bk_Block *b) { |
| 54 |
bk_Graph *forest; |
| 55 |
NEW(forest); |
| 56 |
uint32_t tsOrder = 0; |
| 57 |
dfs_insert_cells(b, forest, &tsOrder); |
| 58 |
qsort(forest->entries, forest->length, sizeof(bk_GraphNode), _by_height); |
| 59 |
for (uint32_t j = 0; j < forest->length; j++) { |
| 60 |
forest->entries[j].block->_index = j; |
| 61 |
forest->entries[j].alias = j; |
| 62 |
} |
| 63 |
return forest; |
| 64 |
} |
| 65 |
|
| 66 |
void bk_delete_Graph(bk_Graph *f) { |
| 67 |
if (!f || !f->entries) return; |
| 68 |
for (uint32_t j = 0; j < f->length; j++) { |
| 69 |
bk_Block *b = f->entries[j].block; |
| 70 |
if (b && b->cells) FREE(b->cells); |
| 71 |
FREE(b); |
| 72 |
} |
| 73 |
FREE(f->entries); |
| 74 |
FREE(f); |
| 75 |
} |
| 76 |
|
| 77 |
static uint32_t gethash(bk_Block *b) { |
| 78 |
uint32_t h = 5381; |
| 79 |
for (uint32_t j = 0; j < b->length; j++) { |
| 80 |
h = ((h << 5) + h) + b->cells[j].t; |
| 81 |
h = ((h << 5) + h); |
| 82 |
switch (b->cells[j].t) { |
| 83 |
case b8: |
| 84 |
case b16: |
| 85 |
case b32: |
| 86 |
h += b->cells[j].z; |
| 87 |
break; |
| 88 |
case p16: |
| 89 |
case p32: |
| 90 |
case sp16: |
| 91 |
case sp32: |
| 92 |
if (b->cells[j].p) { h += b->cells[j].p->_index; } |
| 93 |
break; |
| 94 |
default: |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
return h; |
| 99 |
} |
| 100 |
|
| 101 |
static bool compareblock(bk_Block *a, bk_Block *b) { |
| 102 |
if (!a && !b) return true; |
| 103 |
if (!a || !b) return false; |
| 104 |
if (a->length != b->length) return false; |
| 105 |
for (uint32_t j = 0; j < a->length; j++) { |
| 106 |
if (a->cells[j].t != b->cells[j].t) return false; |
| 107 |
switch (a->cells[j].t) { |
| 108 |
case b8: |
| 109 |
case b16: |
| 110 |
case b32: |
| 111 |
if (a->cells[j].z != b->cells[j].z) return false; |
| 112 |
break; |
| 113 |
case p16: |
| 114 |
case p32: |
| 115 |
case sp16: |
| 116 |
case sp32: |
| 117 |
if (a->cells[j].p != b->cells[j].p) return false; |
| 118 |
break; |
| 119 |
default: |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
static bool compareEntry(bk_GraphNode *a, bk_GraphNode *b) { |
| 126 |
if (a->hash != b->hash) return false; |
| 127 |
return compareblock(a->block, b->block); |
| 128 |
} |
| 129 |
|
| 130 |
static void replaceptr(bk_Graph *f, bk_Block *b) { |
| 131 |
for (uint32_t j = 0; j < b->length; j++) { |
| 132 |
switch (b->cells[j].t) { |
| 133 |
case p16: |
| 134 |
case p32: |
| 135 |
case sp16: |
| 136 |
case sp32: |
| 137 |
if (b->cells[j].p) { |
| 138 |
uint32_t index = b->cells[j].p->_index; |
| 139 |
while (f->entries[index].alias != index) { |
| 140 |
index = f->entries[index].alias; |
| 141 |
} |
| 142 |
b->cells[j].p = f->entries[index].block; |
| 143 |
} |
| 144 |
break; |
| 145 |
default: |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void bk_minimizeGraph(bk_Graph *f) { |
| 152 |
uint32_t rear = (uint32_t)(f->length - 1); |
| 153 |
while (rear > 0) { |
| 154 |
uint32_t front = rear; |
| 155 |
while (f->entries[front].height == f->entries[rear].height && front > 0) { |
| 156 |
front--; |
| 157 |
} |
| 158 |
front++; |
| 159 |
for (uint32_t j = front; j <= rear; j++) { |
| 160 |
f->entries[j].hash = gethash(f->entries[j].block); |
| 161 |
} |
| 162 |
for (uint32_t j = front; j <= rear; j++) { |
| 163 |
bk_GraphNode *a = &(f->entries[j]); |
| 164 |
if (a->alias == j) { |
| 165 |
for (uint32_t k = j + 1; k <= rear; k++) { |
| 166 |
bk_GraphNode *b = &(f->entries[k]); |
| 167 |
if (b->alias == k && compareEntry(a, b)) { b->alias = j; } |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
// replace pointers with aliased |
| 172 |
for (uint32_t j = 0; j < front; j++) { |
| 173 |
replaceptr(f, f->entries[j].block); |
| 174 |
} |
| 175 |
rear = front - 1; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static size_t otfcc_bkblock_size(bk_Block *b) { |
| 180 |
size_t size = 0; |
| 181 |
for (uint32_t j = 0; j < b->length; j++) |
| 182 |
switch (b->cells[j].t) { |
| 183 |
case b8: |
| 184 |
size += 1; |
| 185 |
break; |
| 186 |
case b16: |
| 187 |
case p16: |
| 188 |
case sp16: |
| 189 |
size += 2; |
| 190 |
break; |
| 191 |
case b32: |
| 192 |
case p32: |
| 193 |
case sp32: |
| 194 |
size += 4; |
| 195 |
break; |
| 196 |
default: |
| 197 |
break; |
| 198 |
} |
| 199 |
return size; |
| 200 |
} |
| 201 |
|
| 202 |
static uint32_t getoffset(size_t *offsets, bk_Block *ref, bk_Block *target, uint8_t bits) { |
| 203 |
size_t offref = offsets[ref->_index]; |
| 204 |
size_t offtgt = offsets[target->_index]; |
| 205 |
/* |
| 206 |
if (offtgt < offref || (offtgt - offref) >> bits) { |
| 207 |
fprintf(stderr, "[otfcc-fea] Warning : Unable to fit offset %d into %d bits.\n", |
| 208 |
(int32_t)(offtgt - offref), bits); |
| 209 |
} |
| 210 |
*/ |
| 211 |
return (uint32_t)(offtgt - offref); |
| 212 |
} |
| 213 |
static int64_t getoffset_untangle(size_t *offsets, bk_Block *ref, bk_Block *target) { |
| 214 |
size_t offref = offsets[ref->_index]; |
| 215 |
size_t offtgt = offsets[target->_index]; |
| 216 |
return (int64_t)(offtgt - offref); |
| 217 |
} |
| 218 |
static void escalate_sppointers(bk_Block *b, bk_Graph *f, uint32_t *order, uint32_t depth) { |
| 219 |
if (!b) return; |
| 220 |
for (uint32_t j = 0; j < b->length; j++) { |
| 221 |
bk_Cell *cell = &(b->cells[j]); |
| 222 |
if (bk_cellIsPointer(cell) && cell->p && cell->t >= sp16) { |
| 223 |
escalate_sppointers(cell->p, f, order, depth); |
| 224 |
} |
| 225 |
} |
| 226 |
b->_depth = depth; |
| 227 |
*order += 1; |
| 228 |
f->entries[b->_index].order = *order; |
| 229 |
} |
| 230 |
static void dfs_attract_cells(bk_Block *b, bk_Graph *f, uint32_t *order, uint32_t depth) { |
| 231 |
if (!b) return; |
| 232 |
if (b->_visitstate != VISIT_WHITE) { |
| 233 |
if (b->_depth < depth) { b->_depth = depth; } |
| 234 |
return; |
| 235 |
} |
| 236 |
b->_visitstate = VISIT_GRAY; |
| 237 |
for (uint32_t j = b->length; j-- > 0;) { |
| 238 |
bk_Cell *cell = &(b->cells[j]); |
| 239 |
if (bk_cellIsPointer(cell) && cell->p) { dfs_attract_cells(cell->p, f, order, depth + 1); } |
| 240 |
} |
| 241 |
*order += 1; |
| 242 |
f->entries[b->_index].order = *order; |
| 243 |
escalate_sppointers(b, f, order, depth); |
| 244 |
b->_visitstate = VISIT_BLACK; |
| 245 |
} |
| 246 |
|
| 247 |
static void attract_bkgraph(bk_Graph *f) { |
| 248 |
// Clear the visit state of all blocks |
| 249 |
for (uint32_t j = 0; j < f->length; j++) { |
| 250 |
f->entries[j].block->_visitstate = VISIT_WHITE; |
| 251 |
f->entries[j].order = 0; |
| 252 |
f->entries[j].block->_index = j; |
| 253 |
f->entries[j].block->_depth = 0; |
| 254 |
} |
| 255 |
uint32_t order = 0; |
| 256 |
dfs_attract_cells(f->entries[0].block, f, &order, 0); |
| 257 |
qsort(f->entries, f->length, sizeof(bk_GraphNode), _by_order); |
| 258 |
for (uint32_t j = 0; j < f->length; j++) { |
| 259 |
f->entries[j].block->_index = j; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
static bool try_untabgle_block(bk_Graph *f, bk_Block *b, size_t *offsets, uint16_t passes) { |
| 264 |
bool didCopy = false; |
| 265 |
for (uint32_t j = 0; j < b->length; j++) { |
| 266 |
switch (b->cells[j].t) { |
| 267 |
case p16: |
| 268 |
case sp16: |
| 269 |
if (b->cells[j].p) { |
| 270 |
int64_t offset = getoffset_untangle(offsets, b, b->cells[j].p); |
| 271 |
if (offset < 0 || offset > 0xFFFF) { |
| 272 |
bk_GraphNode *e = _bkgraph_grow(f); |
| 273 |
e->order = 0; |
| 274 |
e->alias = 0; |
| 275 |
e->block = bk_new_Block(bkcopy, b->cells[j].p, bkover); |
| 276 |
b->cells[j].t = sp16; |
| 277 |
b->cells[j].p = e->block; |
| 278 |
didCopy = true; |
| 279 |
} |
| 280 |
} |
| 281 |
break; |
| 282 |
default: |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
return didCopy; |
| 287 |
} |
| 288 |
|
| 289 |
static bool try_untangle(bk_Graph *f, uint16_t passes) { |
| 290 |
size_t *offsets; |
| 291 |
NEW(offsets, f->length + 1); |
| 292 |
offsets[0] = 0; |
| 293 |
for (uint32_t j = 0; j < f->length; j++) { |
| 294 |
if (f->entries[j].block->_visitstate == VISIT_BLACK) { |
| 295 |
offsets[j + 1] = offsets[j] + otfcc_bkblock_size(f->entries[j].block); |
| 296 |
} else { |
| 297 |
offsets[j + 1] = offsets[j]; |
| 298 |
} |
| 299 |
} |
| 300 |
uint32_t totalBlocks = f->length; |
| 301 |
bool didUntangle = false; |
| 302 |
for (uint32_t j = 0; j < totalBlocks; j++) { |
| 303 |
if (f->entries[j].block->_visitstate == VISIT_BLACK) { |
| 304 |
bool didCopy = try_untabgle_block(f, f->entries[j].block, offsets, passes); |
| 305 |
didUntangle = didUntangle || didCopy; |
| 306 |
} |
| 307 |
} |
| 308 |
FREE(offsets); |
| 309 |
return didUntangle; |
| 310 |
} |
| 311 |
|
| 312 |
static void otfcc_build_bkblock(caryll_Buffer *buf, bk_Block *b, size_t *offsets) { |
| 313 |
for (uint32_t j = 0; j < b->length; j++) { |
| 314 |
switch (b->cells[j].t) { |
| 315 |
case b8: |
| 316 |
bufwrite8(buf, b->cells[j].z); |
| 317 |
break; |
| 318 |
case b16: |
| 319 |
bufwrite16b(buf, b->cells[j].z); |
| 320 |
break; |
| 321 |
case b32: |
| 322 |
bufwrite32b(buf, b->cells[j].z); |
| 323 |
break; |
| 324 |
case p16: |
| 325 |
case sp16: |
| 326 |
if (b->cells[j].p) { |
| 327 |
bufwrite16b(buf, getoffset(offsets, b, b->cells[j].p, 16)); |
| 328 |
} else { |
| 329 |
bufwrite16b(buf, 0); |
| 330 |
} |
| 331 |
break; |
| 332 |
case p32: |
| 333 |
case sp32: |
| 334 |
if (b->cells[j].p) { |
| 335 |
bufwrite32b(buf, getoffset(offsets, b, b->cells[j].p, 32)); |
| 336 |
} else { |
| 337 |
bufwrite32b(buf, 0); |
| 338 |
} |
| 339 |
break; |
| 340 |
default: |
| 341 |
break; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
caryll_Buffer *bk_build_Graph(bk_Graph *f) { |
| 347 |
caryll_Buffer *buf = bufnew(); |
| 348 |
size_t *offsets; |
| 349 |
NEW(offsets, f->length + 1); |
| 350 |
|
| 351 |
offsets[0] = 0; |
| 352 |
for (uint32_t j = 0; j < f->length; j++) { |
| 353 |
if (f->entries[j].block->_visitstate == VISIT_BLACK) { |
| 354 |
offsets[j + 1] = offsets[j] + otfcc_bkblock_size(f->entries[j].block); |
| 355 |
} else { |
| 356 |
offsets[j + 1] = offsets[j]; |
| 357 |
} |
| 358 |
} |
| 359 |
for (uint32_t j = 0; j < f->length; j++) { |
| 360 |
if (f->entries[j].block->_visitstate == VISIT_BLACK) { |
| 361 |
otfcc_build_bkblock(buf, f->entries[j].block, offsets); |
| 362 |
} |
| 363 |
} |
| 364 |
FREE(offsets); |
| 365 |
return buf; |
| 366 |
} |
| 367 |
|
| 368 |
size_t bk_estimateSizeOfGraph(bk_Graph *f) { |
| 369 |
size_t *offsets; |
| 370 |
NEW(offsets, f->length + 1); |
| 371 |
|
| 372 |
offsets[0] = 0; |
| 373 |
for (uint32_t j = 0; j < f->length; j++) { |
| 374 |
if (f->entries[j].block->_visitstate == VISIT_BLACK) { |
| 375 |
offsets[j + 1] = offsets[j] + otfcc_bkblock_size(f->entries[j].block); |
| 376 |
} else { |
| 377 |
offsets[j + 1] = offsets[j]; |
| 378 |
} |
| 379 |
} |
| 380 |
size_t estimatedSize = offsets[f->length]; |
| 381 |
FREE(offsets); |
| 382 |
return estimatedSize; |
| 383 |
} |
| 384 |
|
| 385 |
void bk_untangleGraph(/*BORROW*/ bk_Graph *f) { |
| 386 |
uint16_t passes = 0; |
| 387 |
bool tangled = false; |
| 388 |
attract_bkgraph(f); |
| 389 |
do { |
| 390 |
tangled = try_untangle(f, passes); |
| 391 |
if (tangled) { attract_bkgraph(f); } |
| 392 |
passes++; |
| 393 |
} while (tangled && passes < 16); |
| 394 |
} |
| 395 |
|
| 396 |
caryll_Buffer *bk_build_Block(/*MOVE*/ bk_Block *root) { |
| 397 |
bk_Graph *f = bk_newGraphFromRootBlock(root); |
| 398 |
bk_minimizeGraph(f); |
| 399 |
bk_untangleGraph(f); |
| 400 |
caryll_Buffer *buf = bk_build_Graph(f); |
| 401 |
bk_delete_Graph(f); |
| 402 |
return buf; |
| 403 |
} |
| 404 |
caryll_Buffer *bk_build_Block_noMinimize(/*MOVE*/ bk_Block *root) { |
| 405 |
bk_Graph *f = bk_newGraphFromRootBlock(root); |
| 406 |
bk_untangleGraph(f); |
| 407 |
caryll_Buffer *buf = bk_build_Graph(f); |
| 408 |
bk_delete_Graph(f); |
| 409 |
return buf; |
| 410 |
} |