-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkwgc.c
1245 lines (1165 loc) · 46.9 KB
/
kwgc.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2020-2024 Andy Kurnia.
#include <arpa/inet.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
typedef enum {
BuildLayout_Legacy,
BuildLayout_Magpie,
BuildLayout_MagpieMerged,
BuildLayout_Experimental,
BuildLayout_Wolges,
} BuildLayout;
// endian helpers
bool is_big_endian(void) {
return htons(1) == 1;
}
void swap_bytes_32(uint8_t *p, size_t size_in_bytes) {
uint8_t t;
for (size_t i = 3; i < size_in_bytes; i += 4) {
t = p[i], p[i] = p[i - 3], p[i - 3] = t;
t = p[i - 1], p[i - 1] = p[i - 2], p[i - 2] = t;
}
}
// time helpers
bool time_goes_to_stderr = false;
struct timeval now(void) {
struct timeval tv;
if (!gettimeofday(&tv, NULL)) return tv;
return (struct timeval){
.tv_sec = 0,
.tv_usec = 0,
};
}
int fprint_dur_us(FILE *fp, struct timeval tv_end, struct timeval tv_start) {
int64_t dur_sec = tv_end.tv_sec - tv_start.tv_sec;
int32_t dur_usec = tv_end.tv_usec - tv_start.tv_usec;
if (dur_usec < 0) {
dur_usec += 1000000;
--dur_sec;
}
return fprintf(fp, "%" PRId64 ".%06d", dur_sec, dur_usec);
}
// malloc helpers
static inline void *not_null_or_die(void *ptr) {
if (!ptr) { perror("not_null_or_die"); abort(); }
return ptr;
}
static inline void *malloc_or_die(size_t size) {
return not_null_or_die(malloc(size));
}
static inline void *realloc_or_die(void *ptr, size_t size) {
return not_null_or_die(realloc(ptr, size));
}
// generic vec types
#define VEC_ELT_NAME Bool
#define VEC_ELT_T bool
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
#define VEC_ELT_NAME U64
#define VEC_ELT_T uint64_t
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
#define VEC_ELT_NAME Byte
#define VEC_ELT_T uint8_t
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
#define VEC_ELT_NAME U32
#define VEC_ELT_T uint32_t
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
#define VEC_ELT_NAME Char
#define VEC_ELT_T char
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
// tile-label-specific
#include "tiles.c"
// parser
typedef struct {
uint32_t ofs; // assume no overflow.
uint32_t len;
} OfsLen;
#define VEC_ELT_NAME OfsLen
#define VEC_ELT_T OfsLen
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
// qc = qsort comparator
int qc_chr_cmp(const void *a, const void *b) {
return memcmp(a, b, 1);
}
uint8_t *qc_ref_tiles_bytes; // temp global, do not free().
int qc_tiles_slices(const void *a, const void *b) {
OfsLen aol = *(OfsLen *)a;
OfsLen bol = *(OfsLen *)b;
uint32_t min_len = aol.len < bol.len ? aol.len : bol.len;
int mc = memcmp(qc_ref_tiles_bytes + aol.ofs, qc_ref_tiles_bytes + bol.ofs, min_len);
if (mc) return mc;
if (min_len < bol.len) return -1;
if (min_len < aol.len) return 1;
return 0;
}
static inline bool eql_tiles_slices(OfsLen *a, OfsLen *b) {
return a->len == b->len && !memcmp(qc_ref_tiles_bytes + a->ofs, qc_ref_tiles_bytes + b->ofs, a->len);
}
// append-only list of words.
typedef struct {
VecOfsLen tiles_slices;
VecByte tiles_bytes;
} Wordlist;
static inline Wordlist wordlist_new(void) {
return (Wordlist){
.tiles_slices = vecOfsLen_new(),
.tiles_bytes = vecByte_new(),
};
}
static inline void wordlist_free(Wordlist self[static 1]) {
vecByte_free(&self->tiles_bytes);
vecOfsLen_free(&self->tiles_slices);
}
static inline void wordlist_sort(Wordlist self[static 1]) {
qc_ref_tiles_bytes = self->tiles_bytes.ptr;
qsort(self->tiles_slices.ptr, self->tiles_slices.len, sizeof(OfsLen), qc_tiles_slices);
}
static inline void wordlist_dedup(Wordlist self[static 1]) {
size_t r = 1;
while (r < self->tiles_slices.len && !eql_tiles_slices(&self->tiles_slices.ptr[r], &self->tiles_slices.ptr[r - 1])) ++r;
if (r < self->tiles_slices.len) {
// [r] == [r-1]
size_t w = r;
while (++r < self->tiles_slices.len) {
if (!eql_tiles_slices(&self->tiles_slices.ptr[r], &self->tiles_slices.ptr[w - 1])) {
memcpy(&self->tiles_slices.ptr[w], &self->tiles_slices.ptr[r], sizeof(OfsLen));
++w;
}
}
self->tiles_slices.len = w;
}
}
// kwg builder
// unconfirmed entries.
typedef struct {
uint32_t arc_index; // refers to states. should only need 22 bits.
uint8_t tile;
bool accepts;
} KwgcTransition;
#define VEC_ELT_NAME KwgcTransition
#define VEC_ELT_T KwgcTransition
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
typedef struct {
VecKwgcTransition transitions; // Vec<kwgc_transition>
VecU32 indexes; // Vec<uint32_t>
} KwgcTransitionStack;
static inline KwgcTransitionStack kwgc_transition_stack_new(void) {
return (KwgcTransitionStack){
.transitions = vecKwgcTransition_new(),
.indexes = vecU32_new(),
};
}
static inline void kwgc_transition_stack_free(KwgcTransitionStack self[static 1]) {
vecU32_free(&self->indexes);
vecKwgcTransition_free(&self->transitions);
}
static inline void kwgc_transition_stack_push(KwgcTransitionStack self[static 1], uint8_t tile) {
vecKwgcTransition_push(&self->transitions, &(KwgcTransition){
.arc_index = 0, // filled up later
.tile = tile,
.accepts = false,
});
uint32_t len = self->transitions.len;
vecU32_push(&self->indexes, &len);
}
typedef struct {
uint32_t arc_index; // refers to states.
uint32_t next_index; // refers to states.
uint8_t tile;
bool accepts;
} KwgcState;
#define VEC_ELT_NAME KwgcState
#define VEC_ELT_T KwgcState
#include "generic_vec.c"
#undef VEC_ELT_T
#undef VEC_ELT_NAME
static inline void do_hash(uint64_t *hash, uint8_t data) {
*hash = (*hash * 3467) ^ (data ^ 0xff);
}
static inline uint64_t kwgc_state_hash(KwgcState self[static 1]) {
uint64_t hash = 0;
do_hash(&hash, self->tile);
do_hash(&hash, self->accepts);
do_hash(&hash, ((uint8_t *)&self->arc_index)[0]);
do_hash(&hash, ((uint8_t *)&self->arc_index)[1]);
do_hash(&hash, ((uint8_t *)&self->arc_index)[2]);
do_hash(&hash, ((uint8_t *)&self->arc_index)[3]);
do_hash(&hash, ((uint8_t *)&self->next_index)[0]);
do_hash(&hash, ((uint8_t *)&self->next_index)[1]);
do_hash(&hash, ((uint8_t *)&self->next_index)[2]);
do_hash(&hash, ((uint8_t *)&self->next_index)[3]);
return hash;
}
static inline bool kwgc_state_eql(KwgcState a[static 1], KwgcState b[static 1]) {
#ifndef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
return a->tile == b->tile &&
a->accepts == b->accepts &&
a->arc_index == b->arc_index &&
a->next_index == b->next_index;
#ifndef __clang__
#pragma GCC diagnostic pop
#endif
}
#define KHM_K_NAME KwgcState
#define KHM_K_T KwgcState
#define KHM_K_HASHFUNC kwgc_state_hash
#define KHM_K_EQLFUNC kwgc_state_eql
#define KHM_V_NAME U32
#define KHM_V_T uint32_t
#include "generic_khm.c"
#undef KHM_V_T
#undef KHM_V_NAME
#undef KHM_K_EQLFUNC
#undef KHM_K_HASHFUNC
#undef KHM_K_T
#undef KHM_K_NAME
// for each i > 0, states[i].arc_index < i and states[i].next_index < i.
// this ensures states is already a topologically sorted DAG.
typedef struct {
VecKwgcState states;
KhmKwgcStateU32 states_finder;
} KwgcStateMaker;
static inline KwgcStateMaker kwgc_state_maker_new(void) {
return (KwgcStateMaker){
.states = vecKwgcState_new(),
.states_finder = khmKwgcStateU32_new(),
};
}
static inline void kwgc_state_maker_free(KwgcStateMaker self[static 1]) {
khmKwgcStateU32_free(&self->states_finder);
vecKwgcState_free(&self->states);
}
static inline uint32_t kwgc_state_maker_make_state(KwgcStateMaker self[static 1], VecKwgcTransition node_transitions[static 1], size_t target_len) {
uint32_t ret = 0;
for (size_t i = node_transitions->len; i-- > target_len; ) {
KwgcTransition *node_transition = &node_transitions->ptr[i];
KwgcState state = {
.arc_index = node_transition->arc_index,
.next_index = ret,
.tile = node_transition->tile,
.accepts = node_transition->accepts,
};
uint32_t *existing_state_index = khmKwgcStateU32_get(&self->states_finder, &state);
if (existing_state_index) {
ret = *existing_state_index;
} else {
ret = (uint32_t)self->states.len;
vecKwgcState_push(&self->states, &state);
khmKwgcStateU32_set(&self->states_finder, &state, &ret);
}
}
return ret;
}
static inline void kwgc_transition_stack_pop(KwgcTransitionStack self[static 1], KwgcStateMaker state_maker[static 1]) {
size_t start_of_batch = (size_t)self->indexes.ptr[--self->indexes.len];
uint32_t new_arc_index = kwgc_state_maker_make_state(state_maker, &self->transitions, start_of_batch);
self->transitions.ptr[start_of_batch - 1].arc_index = new_arc_index;
self->transitions.len = start_of_batch;
}
static inline uint32_t kwgc_state_maker_make_dawg(KwgcStateMaker self[static 1], Wordlist sorted_machine_words[static 1], uint32_t dawg_start_state, bool is_gaddag_phase) {
KwgcTransitionStack transition_stack = kwgc_transition_stack_new();
for (size_t machine_word_index = 0; machine_word_index < sorted_machine_words->tiles_slices.len; ++machine_word_index) {
OfsLen *this_word = &sorted_machine_words->tiles_slices.ptr[machine_word_index];
uint32_t prefix_len = 0;
if (machine_word_index > 0) {
OfsLen *prev_word = &sorted_machine_words->tiles_slices.ptr[machine_word_index - 1];
uint32_t prev_word_len = transition_stack.indexes.len; // this can be one less than prev_word->len for gaddag.
uint32_t min_word_len = prev_word_len < this_word->len ? prev_word_len : this_word->len;
while (prefix_len < min_word_len &&
sorted_machine_words->tiles_bytes.ptr[prev_word->ofs + prefix_len] ==
sorted_machine_words->tiles_bytes.ptr[this_word->ofs + prefix_len])
++prefix_len;
for (uint32_t i = prefix_len; i < prev_word_len; ++i) {
kwgc_transition_stack_pop(&transition_stack, self);
}
}
for (uint32_t i = prefix_len; i < this_word->len; ++i) {
kwgc_transition_stack_push(&transition_stack, sorted_machine_words->tiles_bytes.ptr[this_word->ofs + i]);
}
if (is_gaddag_phase && !sorted_machine_words->tiles_bytes.ptr[this_word->ofs + this_word->len - 1]) {
--transition_stack.indexes.len;
// gaddag["AC@"] points to dawg["CA"]
uint32_t p = dawg_start_state;
for (uint32_t i = this_word->len - 1; i > 0; --i) {
uint8_t sought_tile = sorted_machine_words->tiles_bytes.ptr[this_word->ofs + i - 1];
while (true) {
KwgcState *pstate = &self->states.ptr[p];
if (pstate->tile == sought_tile) {
p = pstate->arc_index;
break;
}
p = pstate->next_index;
}
}
transition_stack.transitions.ptr[transition_stack.transitions.len - 1].arc_index = p;
} else {
transition_stack.transitions.ptr[transition_stack.transitions.len - 1].accepts = true;
}
}
while (transition_stack.indexes.len) kwgc_transition_stack_pop(&transition_stack, self);
uint32_t ret = kwgc_state_maker_make_state(self, &transition_stack.transitions, 0);
kwgc_transition_stack_free(&transition_stack);
return ret;
}
typedef struct {
KwgcState *states;
uint32_t states_len;
uint32_t *head_indexes;
uint32_t *to_end_lens; // using uint8_t costs runtime.
uint32_t *destination;
uint32_t num_written;
} KwgcStatesDefragger;
void kwgc_states_defragger_defrag_legacy(KwgcStatesDefragger self[static 1], uint32_t p) {
p = self->head_indexes[p];
uint32_t *dp = self->destination + p;
if (*dp) return;
uint32_t num = self->to_end_lens[p];
// temp value to break self-cycles.
*dp = (uint32_t)~0;
uint32_t write_p = p;
while (true) {
uint32_t a = self->states[p].arc_index;
if (a) kwgc_states_defragger_defrag_legacy(self, a);
p = self->states[p].next_index;
if (!p) break;
}
uint32_t initial_num_written = self->num_written;
*dp = 0;
for (uint32_t ofs = 0; ofs < num; ++ofs) {
// prefer earlier index, so dawg part does not point to gaddag part.
dp = self->destination + write_p;
if (*dp) break;
*dp = initial_num_written + ofs;
write_p = self->states[write_p].next_index;
}
// Always += num even if some nodes are necessarily duplicated due to sharing by different prev_nodes.
self->num_written += num;
}
void kwgc_states_defragger_defrag_magpie(KwgcStatesDefragger self[static 1], uint32_t p) {
uint32_t *dp = self->destination + p;
if (*dp) return;
*dp = self->num_written;
// non-legacy mode reserves the space first.
uint32_t num = self->to_end_lens[p];
self->num_written += num;
while (true) {
uint32_t a = self->states[p].arc_index;
if (a) kwgc_states_defragger_defrag_magpie(self, a);
p = self->states[p].next_index;
if (!p) break;
}
}
void kwgc_states_defragger_defrag_magpie_merged(KwgcStatesDefragger self[static 1], uint32_t p) {
p = self->head_indexes[p];
uint32_t *dp = self->destination + p;
if (*dp) return;
uint32_t initial_num_written = self->num_written;
// temp value to break self-cycles.
*dp = (uint32_t)~0;
// non-legacy mode reserves the space first.
uint32_t num = self->to_end_lens[p];
self->num_written += num;
uint32_t write_p = p;
while (true) {
uint32_t a = self->states[p].arc_index;
if (a) kwgc_states_defragger_defrag_magpie_merged(self, a);
p = self->states[p].next_index;
if (!p) break;
}
*dp = 0;
for (uint32_t ofs = 0; ofs < num; ++ofs) {
// prefer earlier index, so dawg part does not point to gaddag part.
dp = self->destination + write_p;
if (*dp) break;
*dp = initial_num_written + ofs;
write_p = self->states[write_p].next_index;
}
// non-legacy mode already reserves the space.
}
// Each block has 16 entries (hardcoded).
// 16 entries of u32 make 64 bytes, which is a common cache line size.
// 0 <= block_len[i] <= 16, from (i << 4) the first block_len[i] are occupied.
// If block_len[i] < 16, blocks_with_len[block_len[i]] stack includes i.
typedef struct {
VecByte block_len;
VecU32 blocks_with_len[16];
} KwgcStatesDefraggerExperimentalParams;
static inline void kwgc_states_defragger_defrag_cache_friendly(KwgcStatesDefragger self[static 1], KwgcStatesDefraggerExperimentalParams params[static 1], uint32_t p) {
p = self->head_indexes[p];
uint32_t *dp = self->destination + p;
if (*dp) return;
// temp value to break self-cycles.
*dp = (uint32_t)~0;
// non-legacy mode reserves the space first.
uint32_t num = self->to_end_lens[p];
// choose a cache-friendly page to place these.
uint32_t num_blocks = params->block_len.len;
uint32_t initial_num_written = 0;
if (num > 16) {
uint8_t tmp_u8 = 0;
// always even-align for 128 byte cache line machines.
if ((num_blocks & 1) == 1) {
vecU32_push(¶ms->blocks_with_len[0], &num_blocks);
vecByte_push(¶ms->block_len, &tmp_u8);
++num_blocks;
}
initial_num_written = num_blocks << 4;
uint32_t inner_num = num;
tmp_u8 = 16;
while (inner_num > 16) {
vecByte_push(¶ms->block_len, &tmp_u8);
inner_num -= 16;
}
// this can be between 1 to 16.
if (inner_num < 16) {
uint32_t tmp_u32 = params->block_len.len;
vecU32_push(¶ms->blocks_with_len[inner_num], &tmp_u32);
}
tmp_u8 = inner_num;
vecByte_push(¶ms->block_len, &tmp_u8);
} else {
// 1 <= num <= 16
for (uint8_t required_gap = 16 - (uint8_t)num; ; --required_gap) { // 0 <= required_gap <= 15
// if found, use it
if (params->blocks_with_len[required_gap].len) {
uint32_t place = params->blocks_with_len[required_gap].ptr[--params->blocks_with_len[required_gap].len];
// use | instead of + because it cannot overflow
initial_num_written = (place << 4) | required_gap;
// repurpose this variable.
required_gap += num; // 1 <= required_gap <= 16
if (required_gap < 16) vecU32_push(¶ms->blocks_with_len[required_gap], &place);
params->block_len.ptr[place] = required_gap;
break;
}
// if 0, add new row.
if (!required_gap) {
initial_num_written = num_blocks << 4;
if (num < 16) vecU32_push(¶ms->blocks_with_len[num], &num_blocks);
uint8_t tmp_u8 = num;
vecByte_push(¶ms->block_len, &tmp_u8);
break;
}
}
}
uint32_t write_p = p;
while (true) {
uint32_t a = self->states[p].arc_index;
if (a) kwgc_states_defragger_defrag_cache_friendly(self, params, a);
p = self->states[p].next_index;
if (!p) break;
}
*dp = 0;
for (uint32_t ofs = 0; ofs < num; ++ofs) {
// prefer earlier index, so dawg part does not point to gaddag part.
dp = self->destination + write_p;
if (*dp) break;
*dp = initial_num_written + ofs;
write_p = self->states[write_p].next_index;
}
// non-legacy mode already reserves the space.
}
uint32_t *qc_ref_num_ways; // temp global, do not free().
uint32_t *qc_ref_to_end_lens; // temp global, do not free().
int qc_build_experimental(const void *a, const void *b) {
uint32_t pa = *(uint32_t *)a;
uint32_t pb = *(uint32_t *)b;
uint32_t num_ways_a = qc_ref_num_ways[pa];
uint32_t num_ways_b = qc_ref_num_ways[pb];
if (num_ways_b < num_ways_a) return -1;
if (num_ways_b > num_ways_a) return 1;
uint32_t to_end_lens_a = qc_ref_to_end_lens[pa];
uint32_t to_end_lens_b = qc_ref_to_end_lens[pb];
if (to_end_lens_b < to_end_lens_a) return -1;
if (to_end_lens_b > to_end_lens_a) return 1;
if (pa < pb) return -1;
if (pa > pb) return 1;
return 0;
}
void kwgc_states_defragger_build_experimental(KwgcStatesDefragger self[static 1], uint32_t num_ways[static 1], uint32_t top_indexes[static 1]) {
uint32_t states_len_minus_one = self->states_len - 1;
uint32_t *idxs = malloc_or_die(states_len_minus_one * sizeof(uint32_t));
for (uint32_t p = 0; p < states_len_minus_one; ++p) idxs[p] = p + 1;
qc_ref_num_ways = num_ways;
qc_ref_to_end_lens = self->to_end_lens;
qsort(idxs, states_len_minus_one, sizeof(uint32_t), qc_build_experimental);
KwgcStatesDefraggerExperimentalParams params = {
.block_len = vecByte_new(),
.blocks_with_len = {
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
},
};
{
// num_written is either 1 or 2, both are < 16.
uint8_t num_written_as_byte = (uint8_t)self->num_written;
vecByte_push(¶ms.block_len, &num_written_as_byte);
}
{
uint32_t zero = 0;
vecU32_push(¶ms.blocks_with_len[self->num_written], &zero);
}
for (uint32_t i = 0; i < states_len_minus_one; ++i) {
kwgc_states_defragger_defrag_cache_friendly(self, ¶ms, top_indexes[idxs[i]]);
}
self->num_written = ((params.block_len.len - 1) << 4) + params.block_len.ptr[params.block_len.len - 1];
for (uint32_t i = 16; i-- > 0; ) vecU32_free(¶ms.blocks_with_len[i]);
vecByte_free(¶ms.block_len);
free(idxs);
}
bool *qc_ref_used_in_dawg; // temp global, do not free().
int qc_build_wolges(const void *a, const void *b) {
uint32_t pa = *(uint32_t *)a;
uint32_t pb = *(uint32_t *)b;
bool used_in_dawg_a = qc_ref_used_in_dawg[pa];
bool used_in_dawg_b = qc_ref_used_in_dawg[pb];
if (used_in_dawg_b < used_in_dawg_a) return -1;
if (used_in_dawg_b > used_in_dawg_a) return 1;
uint32_t num_ways_a = qc_ref_num_ways[pa];
uint32_t num_ways_b = qc_ref_num_ways[pb];
if (num_ways_b < num_ways_a) return -1;
if (num_ways_b > num_ways_a) return 1;
uint32_t to_end_lens_a = qc_ref_to_end_lens[pa];
uint32_t to_end_lens_b = qc_ref_to_end_lens[pb];
if (to_end_lens_b < to_end_lens_a) return -1;
if (to_end_lens_b > to_end_lens_a) return 1;
if (pa < pb) return -1;
if (pa > pb) return 1;
return 0;
}
void kwgc_states_defragger_build_wolges(KwgcStatesDefragger self[static 1], uint32_t num_ways[static 1], bool is_gaddag, uint32_t dawg_start_state) {
uint32_t states_len_minus_one = self->states_len - 1;
uint32_t *idxs = malloc_or_die(states_len_minus_one * sizeof(uint32_t));
for (uint32_t p = 0; p < states_len_minus_one; ++p) idxs[p] = p + 1;
qc_ref_num_ways = num_ways;
qc_ref_to_end_lens = self->to_end_lens;
if (is_gaddag) {
// Check which nodes are used in dawg.
bool *used_in_dawg = malloc_or_die(self->states_len * sizeof(bool));
used_in_dawg[0] = false;
uint32_t p = 1;
for (; p <= dawg_start_state; ++p) used_in_dawg[p] = true;
for (; p < self->states_len; ++p) used_in_dawg[p] = used_in_dawg[self->states[p].next_index];
qc_ref_used_in_dawg = used_in_dawg;
qsort(idxs, states_len_minus_one, sizeof(uint32_t), qc_build_wolges);
free(used_in_dawg);
} else {
// All nodes are dawg nodes.
qsort(idxs, states_len_minus_one, sizeof(uint32_t), qc_build_experimental);
}
KwgcStatesDefraggerExperimentalParams params = {
.block_len = vecByte_new(),
.blocks_with_len = {
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
vecU32_new(), vecU32_new(), vecU32_new(), vecU32_new(),
},
};
{
// num_written is either 1 or 2, both are < 16.
uint8_t num_written_as_byte = (uint8_t)self->num_written;
vecByte_push(¶ms.block_len, &num_written_as_byte);
}
{
uint32_t zero = 0;
vecU32_push(¶ms.blocks_with_len[self->num_written], &zero);
}
for (uint32_t i = 0; i < states_len_minus_one; ++i) {
kwgc_states_defragger_defrag_cache_friendly(self, ¶ms, idxs[i]);
}
self->num_written = ((params.block_len.len - 1) << 4) + params.block_len.ptr[params.block_len.len - 1];
for (uint32_t i = 16; i-- > 0; ) vecU32_free(¶ms.blocks_with_len[i]);
vecByte_free(¶ms.block_len);
free(idxs);
}
static inline void kwgc_write_node(uint8_t *pout, uint32_t defragged_arc_index, bool is_end, bool accepts, uint8_t tile) {
pout[0] = defragged_arc_index;
pout[1] = defragged_arc_index >> 8;
pout[2] = ((defragged_arc_index >> 16) & 0x3f) | (uint8_t)(is_end << 6) | (uint8_t)(accepts << 7);
pout[3] = tile;
}
// ret must initially be empty.
void kwgc_build(VecU32 *ret, Wordlist sorted_machine_words[static 1], bool is_gaddag, BuildLayout build_layout) {
KwgcStateMaker state_maker = kwgc_state_maker_new();
// The sink state always exists.
vecKwgcState_push(&state_maker.states, &(KwgcState){
.arc_index = 0,
.next_index = 0,
.tile = 0,
.accepts = false,
});
uint32_t gaddag_start_state = 0;
khmKwgcStateU32_set(&state_maker.states_finder, state_maker.states.ptr, &gaddag_start_state);
uint32_t dawg_start_state = kwgc_state_maker_make_dawg(&state_maker, sorted_machine_words, 0, false);
if (is_gaddag) {
OfsLen cur_ofs_len = { .ofs = 0, .len = 0 };
Wordlist gaddag_wl = wordlist_new();
for (size_t machine_word_index = 0; machine_word_index < sorted_machine_words->tiles_slices.len; ++machine_word_index) {
OfsLen *this_word = &sorted_machine_words->tiles_slices.ptr[machine_word_index];
uint32_t prefix_len = 0;
if (machine_word_index > 0) {
OfsLen *prev_word = &sorted_machine_words->tiles_slices.ptr[machine_word_index - 1];
uint32_t max_prefix_len = prev_word->len - 1; // - 1 because CAR -> CARE means we still need to emit RAC@.
if (this_word->len < max_prefix_len) max_prefix_len = this_word->len;
while (prefix_len < max_prefix_len &&
sorted_machine_words->tiles_bytes.ptr[prev_word->ofs + prefix_len] ==
sorted_machine_words->tiles_bytes.ptr[this_word->ofs + prefix_len])
++prefix_len;
}
// CARE = ERAC, RAC@, AC@, C@
for (size_t i = this_word->len; i > 0; --i) {
vecByte_push(&gaddag_wl.tiles_bytes, &sorted_machine_words->tiles_bytes.ptr[this_word->ofs + i - 1]);
}
cur_ofs_len.len = this_word->len;
vecOfsLen_push(&gaddag_wl.tiles_slices, &cur_ofs_len);
cur_ofs_len.ofs += cur_ofs_len.len;
uint8_t zero = 0;
for (size_t j = this_word->len - 1; j > prefix_len; --j) {
for (size_t i = j; i > 0; --i) {
vecByte_push(&gaddag_wl.tiles_bytes, &sorted_machine_words->tiles_bytes.ptr[this_word->ofs + i - 1]);
}
vecByte_push(&gaddag_wl.tiles_bytes, &zero);
cur_ofs_len.len = j + 1;
vecOfsLen_push(&gaddag_wl.tiles_slices, &cur_ofs_len);
cur_ofs_len.ofs += cur_ofs_len.len;
}
}
wordlist_sort(&gaddag_wl);
gaddag_start_state = kwgc_state_maker_make_dawg(&state_maker, &gaddag_wl, dawg_start_state, true);
wordlist_free(&gaddag_wl);
}
uint32_t *head_indexes = NULL;
switch (build_layout) {
case BuildLayout_Magpie:
break;
case BuildLayout_Legacy:
case BuildLayout_MagpieMerged:
case BuildLayout_Experimental:
case BuildLayout_Wolges:
head_indexes = malloc_or_die(state_maker.states.len * sizeof(uint32_t));
for (uint32_t p = 0; p < state_maker.states.len; ++p) head_indexes[p] = p;
// point to immediate prev.
for (uint32_t p = state_maker.states.len - 1; p > 0; --p) {
head_indexes[state_maker.states.ptr[p].next_index] = p;
}
// head_indexes[0] is garbage, does not matter.
// adjust to point to prev heads instead.
for (uint32_t p = state_maker.states.len - 1; p > 0; --p) {
head_indexes[p] = head_indexes[head_indexes[p]];
}
}
uint32_t *to_end_lens = malloc_or_die(state_maker.states.len * sizeof(uint32_t));
for (uint32_t p = 0; p < state_maker.states.len; ++p) {
to_end_lens[p] = 1;
uint32_t next = state_maker.states.ptr[p].next_index;
if (next) to_end_lens[p] += to_end_lens[next];
}
uint32_t *destination = malloc_or_die(state_maker.states.len * sizeof(uint32_t));
memset(destination, 0, state_maker.states.len * sizeof(uint32_t));
uint32_t *num_ways = NULL;
switch (build_layout) {
case BuildLayout_Experimental:
case BuildLayout_Wolges:
num_ways = malloc_or_die(state_maker.states.len * sizeof(uint32_t));
memset(num_ways, 0, state_maker.states.len * sizeof(uint32_t));
num_ways[dawg_start_state] = 1;
if (is_gaddag) num_ways[gaddag_start_state] = 1;
for (uint32_t p = state_maker.states.len - 1; p > 0; --p) {
uint32_t this_num_ways = num_ways[p];
// saturating add using cmov.
uint32_t *pp_dest = num_ways + state_maker.states.ptr[p].next_index;
if ((*pp_dest += this_num_ways) < this_num_ways) *pp_dest = (uint32_t)~0;
pp_dest = num_ways + state_maker.states.ptr[p].arc_index;
if ((*pp_dest += this_num_ways) < this_num_ways) *pp_dest = (uint32_t)~0;
}
break;
case BuildLayout_Legacy:
case BuildLayout_Magpie:
case BuildLayout_MagpieMerged:
break;
}
uint32_t *top_indexes = NULL;
switch (build_layout) {
case BuildLayout_Experimental:
top_indexes = malloc_or_die(state_maker.states.len * sizeof(uint32_t));
memset(top_indexes, 0, state_maker.states.len * sizeof(uint32_t));
for (uint32_t p = 1; p < state_maker.states.len; ++p) {
uint32_t *pp_dest = top_indexes + state_maker.states.ptr[p].arc_index;
*pp_dest = p | (uint32_t)-!!*pp_dest;
}
// [p] = 0 (no parent), parent_index, or !0 if > 1 parents.
// if not unique, set [p] = p.
for (uint32_t p = 0; p < state_maker.states.len; ++p) {
uint32_t *pp_dest = top_indexes + p;
if (*pp_dest == 0 || *pp_dest == (uint32_t)~0) *pp_dest = p;
}
// adjust to point to prev tops's heads instead.
for (uint32_t p = state_maker.states.len - 1; p > 0; --p) {
top_indexes[p] = head_indexes[top_indexes[top_indexes[p]]];
}
break;
case BuildLayout_Legacy:
case BuildLayout_Magpie:
case BuildLayout_MagpieMerged:
case BuildLayout_Wolges:
break;
}
KwgcStatesDefragger states_defragger = {
.states = (KwgcState *)state_maker.states.ptr,
.states_len = state_maker.states.len,
.head_indexes = head_indexes,
.to_end_lens = to_end_lens,
.destination = destination,
.num_written = is_gaddag ? 2 : 1,
};
destination[0] = (uint32_t)~0; // useful for empty lexicon.
switch (build_layout) {
case BuildLayout_Legacy:
kwgc_states_defragger_defrag_legacy(&states_defragger, dawg_start_state);
if (is_gaddag) kwgc_states_defragger_defrag_legacy(&states_defragger, gaddag_start_state);
break;
case BuildLayout_Magpie:
kwgc_states_defragger_defrag_magpie(&states_defragger, dawg_start_state);
if (is_gaddag) kwgc_states_defragger_defrag_magpie(&states_defragger, gaddag_start_state);
break;
case BuildLayout_MagpieMerged:
kwgc_states_defragger_defrag_magpie_merged(&states_defragger, dawg_start_state);
if (is_gaddag) kwgc_states_defragger_defrag_magpie_merged(&states_defragger, gaddag_start_state);
break;
case BuildLayout_Experimental:
kwgc_states_defragger_build_experimental(&states_defragger, num_ways, top_indexes);
break;
case BuildLayout_Wolges:
kwgc_states_defragger_build_wolges(&states_defragger, num_ways, is_gaddag, dawg_start_state);
break;
}
destination[0] = 0; // useful for empty lexicon.
if (states_defragger.num_written > 0x400000) {
// the format can only have 0x400000 elements, each has 4 bytes
fprintf(stderr, "this format cannot have %u nodes\n", states_defragger.num_written);
free(destination);
free(to_end_lens);
free(head_indexes);
kwgc_state_maker_free(&state_maker);
return;
}
vecU32_ensure_cap_exact(ret, ret->len = states_defragger.num_written);
memset(ret->ptr, 0, ret->len * sizeof(uint32_t)); // initialize gaps to 0 for determinism.
kwgc_write_node((uint8_t *)ret->ptr, destination[dawg_start_state], true, false, 0);
if (is_gaddag) kwgc_write_node((uint8_t *)(ret->ptr + 1), destination[gaddag_start_state], true, false, 0);
for (uint32_t outer_p = 1; outer_p < state_maker.states.len; ++outer_p) {
uint32_t dp = destination[outer_p];
if (dp) {
for (uint32_t p = outer_p; ; ++dp) {
KwgcState *state = states_defragger.states + p;
kwgc_write_node((uint8_t *)(ret->ptr + dp), destination[state->arc_index], !state->next_index, state->accepts, state->tile);
if (!state->next_index) break;
p = state->next_index;
}
}
}
free(top_indexes);
free(num_ways);
free(destination);
free(to_end_lens);
free(head_indexes);
kwgc_state_maker_free(&state_maker);
}
// commands
bool do_lang_kwg(char **argv, ParsedTile tileset_parse(uint8_t *), BuildLayout build_layout, int mode) {
// assume argc >= 4. mode in [0 (dawgonly), 1 (gaddawg), 2 (alpha)].
bool errored = false;
bool defer_fclose = false;
bool defer_free_file_content = false;
bool defer_free_wl = false;
bool defer_free_ret = false;
FILE *f = fopen(argv[2], "rb"); if (!f) { perror("fopen"); goto errored; } defer_fclose = true;
if (fseek(f, 0L, SEEK_END)) { perror("fseek"); goto errored; }
off_t file_size_signed = ftello(f); if (file_size_signed < 0) { perror("ftello"); goto errored; }
size_t file_size = (size_t)file_size_signed;
uint8_t *file_content = malloc_or_die(file_size + 1); defer_free_file_content = true;
rewind(f);
if (fread(file_content, 1, file_size, f) != file_size) { perror("fread"); goto errored; }
defer_fclose = false; if (fclose(f)) { perror("fclose"); goto errored; }
file_content[file_size++] = '\n'; // sentinel
OfsLen cur_ofs_len = { .ofs = 0, .len = 0 };
Wordlist wl = wordlist_new(); defer_free_wl = true;
for (size_t i = 0; i < file_size; ) {
ParsedTile parsed_tile = tileset_parse(file_content + i);
if (parsed_tile.len && parsed_tile.index > 0) { // ignore blank
vecByte_push(&wl.tiles_bytes, &parsed_tile.index);
i += parsed_tile.len;
++cur_ofs_len.len;
} else if (file_content[i] <= ' ') {
while (file_content[i] != '\n') ++i;
++i; // skip the newline
if (cur_ofs_len.len > 0) {
if (mode == 2) qsort(wl.tiles_bytes.ptr + cur_ofs_len.ofs, cur_ofs_len.len, sizeof(uint8_t), qc_chr_cmp);
vecOfsLen_push(&wl.tiles_slices, &cur_ofs_len);
cur_ofs_len.ofs += cur_ofs_len.len;
cur_ofs_len.len = 0;
}
} else {
fprintf(stderr, "bad tile at offset %zu\n", i);
goto errored;
}
}
defer_free_file_content = false; free(file_content);
wordlist_sort(&wl);
wordlist_dedup(&wl);
VecU32 ret = vecU32_new(); defer_free_ret = true;
kwgc_build(&ret, &wl, mode == 1, build_layout);
if (!ret.len) goto errored;
f = fopen(argv[3], "wb"); if (!f) { perror("fopen"); goto errored; } defer_fclose = true;
if (fwrite(ret.ptr, sizeof(uint32_t), ret.len, f) != ret.len) { perror("fwrite"); goto errored; }
defer_fclose = false; if (fclose(f)) { perror("fclose"); goto errored; }
goto cleanup;
errored: errored = true;
cleanup:
if (defer_free_ret) vecU32_free(&ret);
if (defer_free_wl) wordlist_free(&wl);
if (defer_free_file_content) free(file_content);
if (defer_fclose) { if (fclose(f)) { perror("fclose"); errored = true; } }
return !errored;
}
bool do_lang_klv2(char **argv, ParsedTile tileset_parse(uint8_t *), BuildLayout build_layout) {
bool errored = false;
bool defer_fclose = false;
bool defer_free_file_content = false;
bool defer_free_wl = false;
bool defer_free_ret = false;
bool defer_free_out = false;
FILE *f = fopen(argv[2], "rb"); if (!f) { perror("fopen"); goto errored; } defer_fclose = true;
if (fseek(f, 0L, SEEK_END)) { perror("fseek"); goto errored; }
off_t file_size_signed = ftello(f); if (file_size_signed < 0) { perror("ftello"); goto errored; }
size_t file_size = (size_t)file_size_signed;
uint8_t *file_content = malloc_or_die(file_size + 1); defer_free_file_content = true;
rewind(f);
if (fread(file_content, 1, file_size, f) != file_size) { perror("fread"); goto errored; }
defer_fclose = false; if (fclose(f)) { perror("fclose"); goto errored; }
file_content[file_size++] = '\n'; // sentinel
OfsLen cur_ofs_len = { .ofs = 0, .len = 0 };
Wordlist wl = wordlist_new(); defer_free_wl = true;
bool this_is_big_endian = is_big_endian();
for (size_t i = 0; i < file_size; ) {
ParsedTile parsed_tile = tileset_parse(file_content + i);
if (parsed_tile.len) { // allow blank
vecByte_push(&wl.tiles_bytes, &parsed_tile.index);
i += parsed_tile.len;
++cur_ofs_len.len;
} else if (file_content[i] == ',') {
size_t orig_i = i;
while (file_content[i] != '\n') ++i;
file_content[i] = '\0'; // prevent sscanf from crashing
float val;
if (sscanf((char *)(file_content + orig_i + 1), "%f", &val) != 1) {
fprintf(stderr, "bad value at offset %zu\n", orig_i + 1);
goto errored;
}
++i; // skip the newline
if (cur_ofs_len.len > 0) {
qsort(wl.tiles_bytes.ptr + cur_ofs_len.ofs, cur_ofs_len.len, sizeof(uint8_t), qc_chr_cmp);
if (this_is_big_endian) {
vecByte_push(&wl.tiles_bytes, ((uint8_t *)&val) + 3);
vecByte_push(&wl.tiles_bytes, ((uint8_t *)&val) + 2);
vecByte_push(&wl.tiles_bytes, ((uint8_t *)&val) + 1);
vecByte_push(&wl.tiles_bytes, (uint8_t *)&val);
} else {
vecByte_push(&wl.tiles_bytes, (uint8_t *)&val);
vecByte_push(&wl.tiles_bytes, ((uint8_t *)&val) + 1);
vecByte_push(&wl.tiles_bytes, ((uint8_t *)&val) + 2);
vecByte_push(&wl.tiles_bytes, ((uint8_t *)&val) + 3);
}
vecOfsLen_push(&wl.tiles_slices, &cur_ofs_len);
cur_ofs_len.ofs += cur_ofs_len.len + 4; // sizeof(float)
cur_ofs_len.len = 0;
}
} else if (file_content[i] <= ' ' && !cur_ofs_len.len) {
while (file_content[i] != '\n') ++i;
++i; // skip the newline
} else {
fprintf(stderr, "bad tile at offset %zu\n", i);
goto errored;
}
}
defer_free_file_content = false; free(file_content);
wordlist_sort(&wl);
wordlist_dedup(&wl);
VecU32 ret = vecU32_new(); defer_free_ret = true;
kwgc_build(&ret, &wl, false, build_layout);
if (!ret.len) goto errored;
size_t out_len = ret.len + wl.tiles_slices.len + 2;
uint8_t *out = malloc_or_die(out_len * sizeof(uint32_t)); defer_free_out = true;
uint8_t *pout = out;
*pout++ = ret.len;
*pout++ = ret.len >> 8;
*pout++ = ret.len >> 16;
*pout++ = ret.len >> 24;
memcpy(pout, ret.ptr, ret.len * sizeof(uint32_t));
pout += ret.len * sizeof(uint32_t);
*pout++ = wl.tiles_slices.len;
*pout++ = wl.tiles_slices.len >> 8;
*pout++ = wl.tiles_slices.len >> 16;
*pout++ = wl.tiles_slices.len >> 24;
for (size_t i = 0; i < wl.tiles_slices.len; ++i) {
OfsLen *this_word = &wl.tiles_slices.ptr[i];