forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plink_common.c
10786 lines (10309 loc) · 351 KB
/
plink_common.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
// This file is part of PLINK 1.90, copyright (C) 2005-2016 Shaun Purcell,
// Christopher Chang.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "plink_common.h"
// #include "pigz.h"
// no leading \n since this is used in LOGPRINTFWW expressions
const char g_errstr_fopen[] = "Error: Failed to open %s.\n";
const char g_cmdline_format_str[] = "\n " PROG_NAME_STR " [input flag(s)...] {command flag(s)...} {other flag(s)...}\n " PROG_NAME_STR " --help {flag name(s)...}\n\n";
char g_textbuf[TEXTBUF_SIZE];
// note that \xxx character constants are interpreted in octal.
// technically no need to represent 0-31, but 64 extra bytes of data is
// probably cheaper than the code to subtract 32 everywhere.
const char g_one_char_strs[] = "\0\0\1\0\2\0\3\0\4\0\5\0\6\0\7\0\10\0\11\0\12\0\13\0\14\0\15\0\16\0\17\0\20\0\21\0\22\0\23\0\24\0\25\0\26\0\27\0\30\0\31\0\32\0\33\0\34\0\35\0\36\0\37\0\40\0\41\0\42\0\43\0\44\0\45\0\46\0\47\0\50\0\51\0\52\0\53\0\54\0\55\0\56\0\57\0\60\0\61\0\62\0\63\0\64\0\65\0\66\0\67\0\70\0\71\0\72\0\73\0\74\0\75\0\76\0\77\0\100\0\101\0\102\0\103\0\104\0\105\0\106\0\107\0\110\0\111\0\112\0\113\0\114\0\115\0\116\0\117\0\120\0\121\0\122\0\123\0\124\0\125\0\126\0\127\0\130\0\131\0\132\0\133\0\134\0\135\0\136\0\137\0\140\0\141\0\142\0\143\0\144\0\145\0\146\0\147\0\150\0\151\0\152\0\153\0\154\0\155\0\156\0\157\0\160\0\161\0\162\0\163\0\164\0\165\0\166\0\167\0\170\0\171\0\172\0\173\0\174\0\175\0\176\0\177\0\200\0\201\0\202\0\203\0\204\0\205\0\206\0\207\0\210\0\211\0\212\0\213\0\214\0\215\0\216\0\217\0\220\0\221\0\222\0\223\0\224\0\225\0\226\0\227\0\230\0\231\0\232\0\233\0\234\0\235\0\236\0\237\0\240\0\241\0\242\0\243\0\244\0\245\0\246\0\247\0\250\0\251\0\252\0\253\0\254\0\255\0\256\0\257\0\260\0\261\0\262\0\263\0\264\0\265\0\266\0\267\0\270\0\271\0\272\0\273\0\274\0\275\0\276\0\277\0\300\0\301\0\302\0\303\0\304\0\305\0\306\0\307\0\310\0\311\0\312\0\313\0\314\0\315\0\316\0\317\0\320\0\321\0\322\0\323\0\324\0\325\0\326\0\327\0\330\0\331\0\332\0\333\0\334\0\335\0\336\0\337\0\340\0\341\0\342\0\343\0\344\0\345\0\346\0\347\0\350\0\351\0\352\0\353\0\354\0\355\0\356\0\357\0\360\0\361\0\362\0\363\0\364\0\365\0\366\0\367\0\370\0\371\0\372\0\373\0\374\0\375\0\376\0\377";
const char* g_missing_geno_ptr = &(g_one_char_strs[96]);
const char* g_output_missing_geno_ptr = &(g_one_char_strs[96]);
uintptr_t g_failed_alloc_attempt_size = 0;
sfmt_t g_sfmt;
FILE* g_logfile = nullptr;
char g_logbuf[MAXLINELEN * 2];
uint32_t g_debug_on = 0;
uint32_t g_log_failed = 0;
uint32_t g_thread_ct;
uint32_t aligned_malloc(uintptr_t size, uintptr_t** aligned_pp) {
#if defined __LP64__ && !defined __APPLE__
// Avoid random segfaults on 64-bit machines which have 8-byte- instead of
// 16-byte-aligned malloc(). (Slightly different code is needed if malloc()
// does not even guarantee 8-byte alignment.)
uintptr_t* malloc_ptr = (uintptr_t*)malloc(size + VEC_BYTES);
if (!malloc_ptr) {
g_failed_alloc_attempt_size = size + VEC_BYTES;
return 1;
}
*aligned_pp = (uintptr_t*)((((uintptr_t)malloc_ptr) + VEC_BYTES) & (~(VEC_BYTES_M1 * ONELU)));
(*aligned_pp)[-1] = (uintptr_t)malloc_ptr;
#else
// no SSE2 concerns here
*aligned_pp = (uintptr_t*)malloc(size);
if (!(*aligned_pp)) {
g_failed_alloc_attempt_size = size;
return 1;
}
#endif
return 0;
}
void aligned_free(uintptr_t* aligned_pp) {
#if defined __LP64__ && !defined __APPLE__
free((uintptr_t*)(aligned_pp[-1]));
#else
free(aligned_pp);
#endif
}
uint32_t push_ll_str(const char* ss, Ll_str** ll_stack_ptr) {
uintptr_t str_bytes = strlen(ss) + 1;
Ll_str* new_ll_str = (Ll_str*)malloc(sizeof(Ll_str) + str_bytes);
if (!new_ll_str) {
g_failed_alloc_attempt_size = sizeof(Ll_str) + str_bytes;
return 1;
}
new_ll_str->next = *ll_stack_ptr;
memcpy(new_ll_str->ss, ss, str_bytes);
*ll_stack_ptr = new_ll_str;
return 0;
}
void logstr(const char* ss) {
if (!g_debug_on) {
fputs(ss, g_logfile);
if (ferror(g_logfile)) {
putc_unlocked('\n', stdout);
fflush(stdout);
fprintf(stderr, "Warning: Logging failure on:\n%s\nFurther logging will not be attempted in this run.\n", ss);
g_log_failed = 1;
}
} else {
if (g_log_failed) {
fflush(stdout);
fputs(ss, stderr);
} else {
fputs(ss, g_logfile);
if (ferror(g_logfile)) {
putc_unlocked('\n', stdout);
fflush(stdout);
fprintf(stderr, "Error: Debug logging failure. Dumping to stderr:\n%s", ss);
g_log_failed = 1;
} else {
fflush(g_logfile);
}
}
}
}
void logprint(const char* ss) {
logstr(ss);
fputs(ss, stdout);
}
void logerrprint(const char* ss) {
logstr(ss);
fflush(stdout);
fputs(ss, stderr);
}
void logprintb() {
logstr(g_logbuf);
fputs(g_logbuf, stdout);
}
void logerrprintb() {
logstr(g_logbuf);
fflush(stdout);
fputs(g_logbuf, stderr);
}
void wordwrap(uint32_t suffix_len, char* ss) {
// Input: A null-terminated string with no intermediate newlines. If
// suffix_len is zero, there should be a terminating \n; otherwise,
// the last character should be a space.
// Effect: Spaces are replaced with newlines in a manner that plays well with
// 80 column terminal windows. (Multi-space blocks are never
// collapsed.)
char* token_start = ss;
char* line_end = &(ss[79]);
char* token_end;
while (1) {
while (*token_start == ' ') {
token_start++;
}
if (token_start > line_end) {
do {
*line_end = '\n';
line_end = &(line_end[80]);
} while (token_start > line_end);
}
token_end = strchr(token_start, ' ');
if (!token_end) {
if (&(token_start[79]) == line_end) {
return;
}
token_end = strchr(token_start, '\0');
if (!suffix_len) {
if (token_end <= &(line_end[1])) {
// okay if end-of-string is one past the end, because function
// assumes last character is \n in suffix_len == 0 case
assert(token_end[-1] == '\n');
return;
}
} else {
if (&(token_end[suffix_len]) <= line_end) {
return;
}
// because of terminal space assumption, token_start actually points
// to the end of the string
assert(token_start[-1] == ' ');
}
token_start[-1] = '\n';
return;
}
if (token_end > line_end) {
if (&(token_start[79]) != line_end) {
token_start[-1] = '\n';
line_end = &(token_start[79]);
if (token_end > line_end) {
// single really long token, can't do anything beyond putting it on
// its own line
*token_end = '\n';
line_end = &(token_end[80]);
}
} else {
// single really long token, *and* previous token was either
// nonexistent or long
*token_end = '\n';
line_end = &(token_end[80]);
}
}
token_start = &(token_end[1]);
}
}
void wordwrapb(uint32_t suffix_len) {
wordwrap(suffix_len, g_logbuf);
}
int32_t fopen_checked(const char* fname, const char* mode, FILE** target_ptr) {
*target_ptr = fopen(fname, mode);
if (!(*target_ptr)) {
LOGERRPRINTFWW(g_errstr_fopen, fname);
return -1;
}
return 0;
}
int32_t fwrite_checked(const void* buf, size_t len, FILE* outfile) {
while (len > 0x7ffff000) {
// OS X can't perform 2GB+ writes
// typical disk block size is 4kb, so 0x7ffff000 is the largest sensible
// write size
fwrite(buf, 1, 0x7ffff000, outfile);
buf = &(((unsigned char*)buf)[0x7ffff000]);
len -= 0x7ffff000;
}
fwrite(buf, 1, len, outfile);
return ferror(outfile);
}
int32_t gzopen_read_checked(const char* fname, gzFile* gzf_ptr) {
*gzf_ptr = gzopen(fname, FOPEN_RB);
if (!(*gzf_ptr)) {
LOGERRPRINTFWW(g_errstr_fopen, fname);
return RET_OPEN_FAIL;
}
if (gzbuffer(*gzf_ptr, 131072)) {
return RET_NOMEM;
}
return 0;
}
// manually managed, very large stack
unsigned char* g_bigstack_base;
unsigned char* g_bigstack_end;
unsigned char* bigstack_alloc(uintptr_t size) {
unsigned char* alloc_ptr;
size = round_up_pow2(size, CACHELINE);
if (bigstack_left() < size) {
g_failed_alloc_attempt_size = size;
return nullptr;
}
alloc_ptr = g_bigstack_base;
g_bigstack_base += size;
return alloc_ptr;
}
void bigstack_shrink_top(const void* rebase, uintptr_t new_size) {
uintptr_t freed_bytes = ((uintptr_t)(g_bigstack_base - ((unsigned char*)rebase))) - round_up_pow2(new_size, CACHELINE);
g_bigstack_base -= freed_bytes;
}
unsigned char* bigstack_end_alloc_presized(uintptr_t size) {
assert(!(size & END_ALLOC_CHUNK_M1));
uintptr_t cur_bigstack_left = bigstack_left();
if (size > cur_bigstack_left) {
g_failed_alloc_attempt_size = size;
return nullptr;
} else {
g_bigstack_end -= size;
return g_bigstack_end;
}
}
uint32_t match_upper(const char* ss, const char* fixed_str) {
char cc = *fixed_str++;
do {
if ((((unsigned char)(*ss++)) & 0xdf) != ((unsigned char)cc)) {
return 0;
}
cc = *fixed_str++;
} while (cc);
return !(*ss);
}
uint32_t match_upper_counted(const char* ss, const char* fixed_str, uint32_t ct) {
do {
if ((((unsigned char)(*ss++)) & 0xdf) != ((unsigned char)(*fixed_str++))) {
return 0;
}
} while (--ct);
return 1;
}
#ifdef __LP64__
static inline uint32_t scan_uint_capped_finish(const char* ss, uint64_t cap, uint32_t* valp) {
uint64_t val = *valp;
while (1) {
// a little bit of unrolling seems to help
const uint64_t cur_digit = (uint64_t)((unsigned char)(*ss++)) - 48;
if (cur_digit >= 10) {
break;
}
// val = val * 10 + cur_digit;
const uint64_t cur_digit2 = (uint64_t)((unsigned char)(*ss++)) - 48;
if (cur_digit2 >= 10) {
val = val * 10 + cur_digit;
if (val > cap) {
return 1;
}
break;
}
val = val * 100 + cur_digit * 10 + cur_digit2;
if (val > cap) {
return 1;
}
}
*valp = val;
return 0;
}
uint32_t scan_posint_capped(const char* ss, uint64_t cap, uint32_t* valp) {
// '0' has ascii code 48
*valp = (uint32_t)((unsigned char)(*ss++)) - 48;
if (*valp >= 10) {
// permit leading '+' (ascii 43), but not '++' or '+-'
if (*valp != 0xfffffffbU) {
return 1;
}
*valp = (uint32_t)((unsigned char)(*ss++)) - 48;
if (*valp >= 10) {
return 1;
}
}
while (!(*valp)) {
*valp = (uint32_t)((unsigned char)(*ss++)) - 48;
if ((*valp) >= 10) {
return 1;
}
}
return scan_uint_capped_finish(ss, cap, valp);
}
uint32_t scan_uint_capped(const char* ss, uint64_t cap, uint32_t* valp) {
// Reads an integer in [0, cap]. Assumes first character is nonspace.
uint32_t val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
if (val != 0xfffffffbU) {
// '-' has ascii code 45, so unsigned 45 - 48 = 0xfffffffdU
if ((val != 0xfffffffdU) || (*ss != '0')) {
return 1;
}
// accept "-0", "-00", etc.
while (*(++ss) == '0');
*valp = 0;
return ((uint32_t)((unsigned char)(*ss)) - 48) < 10;
}
// accept leading '+'
val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
*valp = val;
return scan_uint_capped_finish(ss, cap, valp);
}
uint32_t scan_int_abs_bounded(const char* ss, uint64_t bound, int32_t* valp) {
// Reads an integer in [-bound, bound]. Assumes first character is nonspace.
*valp = (uint32_t)((unsigned char)(*ss++)) - 48;
int32_t sign = 1;
if (((uint32_t)*valp) >= 10) {
if (*valp == -3) {
sign = -1;
} else if (*valp != -5) {
return 1;
}
*valp = (uint32_t)((unsigned char)(*ss++)) - 48;
if (((uint32_t)*valp) >= 10) {
return 1;
}
}
if (scan_uint_capped_finish(ss, bound, (uint32_t*)valp)) {
return 1;
}
*valp *= sign;
return 0;
}
#else // not __LP64__
uint32_t scan_posint_capped32(const char* ss, uint32_t cap_div_10, uint32_t cap_mod_10, uint32_t* valp) {
// '0' has ascii code 48
uint32_t val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
if (val != 0xfffffffbU) {
return 1;
}
val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
while (!val) {
val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
while (1) {
const uint32_t cur_digit = (uint32_t)((unsigned char)(*ss++)) - 48;
if (cur_digit >= 10) {
*valp = val;
return 0;
}
// avoid integer overflow in middle of computation
if ((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
uint32_t scan_uint_capped32(const char* ss, uint32_t cap_div_10, uint32_t cap_mod_10, uint32_t* valp) {
// Reads an integer in [0, cap]. Assumes first character is nonspace.
uint32_t val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
if (val != 0xfffffffbU) {
if ((val != 0xfffffffdU) || (*ss != '0')) {
return 1;
}
while (*(++ss) == '0');
*valp = 0;
return ((uint32_t)((unsigned char)(*ss)) - 48) < 10;
}
val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
while (1) {
const uint32_t cur_digit = (uint32_t)((unsigned char)(*ss++)) - 48;
if (cur_digit >= 10) {
*valp = val;
return 0;
}
if ((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
uint32_t scan_int_abs_bounded32(const char* ss, uint32_t bound_div_10, uint32_t bound_mod_10, int32_t* valp) {
// Reads an integer in [-bound, bound]. Assumes first character is nonspace.
uint32_t val = (uint32_t)((unsigned char)(*ss++)) - 48;
int32_t sign = 1;
if (val >= 10) {
if (val == 0xfffffffdU) {
sign = -1;
} else if (val != 0xfffffffbU) {
return 1;
}
val = (uint32_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
while (1) {
const uint32_t cur_digit = (uint32_t)((unsigned char)(*ss++)) - 48;
if (cur_digit >= 10) {
*valp = sign * ((int32_t)val);
return 0;
}
if ((val >= bound_div_10) && ((val > bound_div_10) || (cur_digit > bound_mod_10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
#endif
uint32_t scan_posintptr(const char* ss, uintptr_t* valp) {
// Reads an integer in [1, 2^BITCT - 1]. Assumes first character is
// nonspace.
uintptr_t val = (uintptr_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
#ifdef __LP64__
if (val != 0xfffffffffffffffbLLU) {
return 1;
}
#else
if (val != 0xfffffffbU) {
return 1;
}
#endif
val = (uintptr_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
while (!val) {
val = (uintptr_t)((unsigned char)(*ss++)) - 48;
if (val >= 10) {
return 1;
}
}
// limit is 20 digits, we've already read one
#ifdef __LP64__
const char* ss_limit = &(ss[20]);
#else
const char* ss_limit = &(ss[10]);
#endif
while (1) {
const uintptr_t cur_digit = (uintptr_t)((unsigned char)(*ss++)) - 48;
if (cur_digit >= 10) {
*valp = val;
return 0;
}
const uintptr_t cur_digit2 = (uintptr_t)((unsigned char)(*ss++)) - 48;
if (ss == ss_limit) {
if ((cur_digit2 < 10) || ((val >= (~ZEROLU) / 10) && ((val > (~ZEROLU) / 10) || (cur_digit > (~ZEROLU) % 10)))) {
return 1;
}
*valp = val * 10 + cur_digit;
return 0;
}
if (cur_digit2 >= 10) {
*valp = val * 10 + cur_digit;
return 0;
}
val = val * 100 + cur_digit * 10 + cur_digit2;
}
}
/*
uint32_t scan_uintptr(char* ss, uintptr_t* valp) {
// [0, 2^BITCT - 1].
uintptr_t val = (uint32_t)((unsigned char)*ss) - 48;
uintptr_t cur_digit;
if (val < 10) {
while (1) {
scan_uintptr_main_loop:
cur_digit = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (cur_digit >= 10) {
*valp = val;
return 0;
}
if ((val >= (~ZEROLU) / 10) && ((val > (~ZEROLU) / 10) || (cur_digit > (~ZEROLU) % 10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
ss++;
if (val != 0xfffffffdU) {
if (val == 0xfffffffbU) {
val = (uint32_t)((unsigned char)(*ss)) - 48;
if (val < 10) {
goto scan_uintptr_main_loop;
}
}
return 1;
}
if (*ss != '0') {
return 1;
}
while (*(++ss) == '0');
*valp = 0;
return ((uint32_t)((unsigned char)(*ss)) - 48) < 10;
}
*/
uint32_t scan_two_doubles(char* ss, double* __restrict val1p, double* __restrict val2p) {
char* ss2;
*val1p = strtod(ss, &ss2);
if (ss == ss2) {
return 1;
}
ss = skip_initial_spaces(ss2);
*val2p = strtod(ss, &ss2);
return (ss == ss2)? 1 : 0;
}
int32_t scan_token_ct_len(uintptr_t half_bufsize, FILE* infile, char* buf, uintptr_t* __restrict token_ct_ptr, uintptr_t* __restrict max_token_len_ptr) {
// buf must be of size >= (2 * half_bufsize + 2)
// max_token_len includes trailing null
uintptr_t full_bufsize = half_bufsize * 2;
uintptr_t curtoklen = 0;
uintptr_t token_ct = *token_ct_ptr;
uintptr_t max_token_len = *max_token_len_ptr;
char* midbuf = &(buf[half_bufsize]);
char* bufptr;
char* bufptr2;
char* buf_end;
uintptr_t bufsize;
while (1) {
if (fread_checked(midbuf, half_bufsize, infile, &bufsize)) {
return RET_READ_FAIL;
}
if (!bufsize) {
if (curtoklen) {
// corner case
if (curtoklen >= max_token_len) {
max_token_len = curtoklen + 1;
}
token_ct++;
}
break;
}
buf_end = &(midbuf[bufsize]);
*buf_end = ' ';
buf_end[1] = '0';
bufptr = &(buf[half_bufsize - curtoklen]);
bufptr2 = midbuf;
if (curtoklen) {
goto scan_token_ct_len_tok_start;
}
while (1) {
while (*bufptr <= ' ') {
bufptr++;
}
if (bufptr >= buf_end) {
curtoklen = 0;
break;
}
bufptr2 = &(bufptr[1]);
scan_token_ct_len_tok_start:
while (*bufptr2 > ' ') {
bufptr2++;
}
curtoklen = (uintptr_t)(bufptr2 - bufptr);
if ((bufptr2 == buf_end) && (buf_end == &(buf[full_bufsize]))) {
if (curtoklen >= half_bufsize) {
return RET_INVALID_FORMAT;
}
break;
}
if (curtoklen >= max_token_len) {
if (curtoklen >= half_bufsize) {
return RET_INVALID_FORMAT;
}
max_token_len = curtoklen + 1;
}
token_ct++;
bufptr = &(bufptr2[1]);
}
}
if (!feof(infile)) {
return RET_READ_FAIL;
}
*max_token_len_ptr = max_token_len;
*token_ct_ptr = token_ct;
return 0;
}
int32_t read_tokens(uintptr_t half_bufsize, uintptr_t token_ct, uintptr_t max_token_len, FILE* infile, char* __restrict buf, char* __restrict token_name_buf) {
// buf must be of size >= (2 * half_bufsize + 2).
// max_token_len includes trailing null
uintptr_t full_bufsize = half_bufsize * 2;
uintptr_t curtoklen = 0;
uintptr_t token_idx = 0;
char* midbuf = &(buf[half_bufsize]);
char* bufptr = midbuf;
char* bufptr2;
char* bufptr3;
char* buf_end;
uintptr_t bufsize;
while (1) {
if (fread_checked(midbuf, half_bufsize, infile, &bufsize)) {
return RET_READ_FAIL;
}
if (!bufsize) {
if (curtoklen) {
if (token_idx + 1 == token_ct) {
memcpyx(&(token_name_buf[token_idx * max_token_len]), bufptr, curtoklen, '\0');
return 0;
}
}
// something very strange has to happen to get here
return RET_READ_FAIL;
}
buf_end = &(midbuf[bufsize]);
*buf_end = ' ';
buf_end[1] = '0';
bufptr2 = midbuf;
if (curtoklen) {
goto read_tokens_tok_start;
}
while (1) {
while (*bufptr <= ' ') {
bufptr++;
}
if (bufptr >= buf_end) {
curtoklen = 0;
bufptr = midbuf;
break;
}
bufptr2 = &(bufptr[1]);
read_tokens_tok_start:
while (*bufptr2 > ' ') {
bufptr2++;
}
curtoklen = (uintptr_t)(bufptr2 - bufptr);
if ((bufptr2 == buf_end) && (buf_end == &(buf[full_bufsize]))) {
bufptr3 = &(buf[half_bufsize - curtoklen]);
memcpy(bufptr3, bufptr, curtoklen);
bufptr = bufptr3;
break;
}
memcpyx(&(token_name_buf[token_idx * max_token_len]), bufptr, curtoklen, '\0');
if (++token_idx == token_ct) {
return 0;
}
bufptr = &(bufptr2[1]);
}
}
}
int32_t gzputs_w4(gzFile gz_outfile, const char* ss) {
if (!ss[1]) {
if (gzputs(gz_outfile, " ") == -1) {
return -1;
}
return gzputc(gz_outfile, ss[0]);
}
if (!ss[2]) {
if (gzputs(gz_outfile, " ") == -1) {
return -1;
}
} else if (!ss[3]) {
if (gzputc(gz_outfile, ' ') == -1) {
return -1;
}
}
return gzputs(gz_outfile, ss);
}
int32_t get_next_noncomment(FILE* fptr, char** lptr_ptr, uintptr_t* line_idx_ptr) {
char* lptr;
do {
if (!fgets(g_textbuf, MAXLINELEN, fptr)) {
return -1;
}
*line_idx_ptr += 1;
lptr = skip_initial_spaces(g_textbuf);
} while (is_eoln_or_comment_kns(*lptr));
*lptr_ptr = lptr;
return 0;
}
int32_t get_next_noncomment_excl(const uintptr_t* __restrict marker_exclude, FILE* fptr, char** lptr_ptr, uintptr_t* __restrict line_idx_ptr, uintptr_t* __restrict marker_uidx_ptr) {
while (!get_next_noncomment(fptr, lptr_ptr, line_idx_ptr)) {
if (!is_set_ul(marker_exclude, *marker_uidx_ptr)) {
return 0;
}
*marker_uidx_ptr += 1;
}
return -1;
}
void get_top_two_ui(const uint32_t* __restrict uint_arr, uintptr_t uia_size, uintptr_t* __restrict top_idx_ptr, uintptr_t* __restrict second_idx_ptr) {
assert(uia_size > 1);
uintptr_t top_idx = (uint_arr[1] > uint_arr[0])? 1 : 0;
uintptr_t second_idx = 1 ^ top_idx;
uint32_t top_val = uint_arr[top_idx];
uint32_t second_val = uint_arr[second_idx];
uintptr_t cur_idx;
uintptr_t cur_val;
for (cur_idx = 2; cur_idx < uia_size; ++cur_idx) {
cur_val = uint_arr[cur_idx];
if (cur_val > second_val) {
if (cur_val > top_val) {
second_val = top_val;
second_idx = top_idx;
top_val = cur_val;
top_idx = cur_idx;
} else {
second_val = cur_val;
second_idx = cur_idx;
}
}
}
*top_idx_ptr = top_idx;
*second_idx_ptr = second_idx;
}
uint32_t intlen(int32_t num) {
int32_t retval = 1;
uint32_t absnum;
if (num < 0) {
absnum = -num;
retval++;
} else {
absnum = num;
}
while (absnum > 99) {
// division by a constant is faster for unsigned ints
absnum /= 100;
retval += 2;
}
if (absnum > 9) {
retval++;
}
return retval;
}
int32_t strcmp_se(const char* s_read, const char* s_const, uint32_t s_const_len) {
return memcmp(s_read, s_const, s_const_len) || (!is_space_or_eoln(s_read[s_const_len]));
}
char* next_token(char* sptr) {
if (!sptr) {
return nullptr;
}
unsigned char ucc = *sptr;
while (ucc > 32) {
ucc = *(++sptr);
}
while ((ucc == ' ') || (ucc == '\t')) {
ucc = *(++sptr);
}
return (ucc > 32)? sptr : nullptr;
}
char* next_token_mult(char* sptr, uint32_t ct) {
assert(ct);
if (!sptr) {
return nullptr;
}
unsigned char ucc = *sptr;
do {
while (ucc > 32) {
ucc = *(++sptr);
}
while ((ucc == ' ') || (ucc == '\t')) {
ucc = *(++sptr);
}
if (ucc <= 32) {
return nullptr;
}
} while (--ct);
return sptr;
}
uint32_t count_tokens(const char* bufptr) {
uint32_t token_ct = 0;
while ((*bufptr == ' ') || (*bufptr == '\t')) {
bufptr++;
}
while (!is_eoln_kns(*bufptr)) {
token_ct++;
while (!is_space_or_eoln(*(++bufptr)));
while ((*bufptr == ' ') || (*bufptr == '\t')) {
bufptr++;
}
}
return token_ct;
}
uint32_t count_and_measure_multistr(const char* multistr, uintptr_t* max_slen_ptr) {
// max_slen includes null terminator
// assumes multistr is nonempty
uint32_t ct = 0;
uintptr_t max_slen = *max_slen_ptr;
uintptr_t slen;
do {
slen = strlen(multistr) + 1;
if (slen > max_slen) {
max_slen = slen;
}
multistr = &(multistr[slen]);
ct++;
} while (*multistr);
*max_slen_ptr = max_slen;
return ct;
}
// number-to-string encoders
static const char digit2_table[200] = {
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4',
'0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
'1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
'1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
'2', '0', '2', '1', '2', '2', '2', '3', '2', '4',
'2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
'3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
'3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
'4', '0', '4', '1', '4', '2', '4', '3', '4', '4',
'4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
'5', '0', '5', '1', '5', '2', '5', '3', '5', '4',
'5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
'6', '0', '6', '1', '6', '2', '6', '3', '6', '4',
'6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
'7', '0', '7', '1', '7', '2', '7', '3', '7', '4',
'7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
'8', '0', '8', '1', '8', '2', '8', '3', '8', '4',
'8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
'9', '0', '9', '1', '9', '2', '9', '3', '9', '4',
'9', '5', '9', '6', '9', '7', '9', '8', '9', '9'};
char* uint32toa(uint32_t uii, char* start) {
// Memory-efficient fast integer writer. (You can do a bit better sometimes
// by using a larger lookup table, but on average I doubt that pays off.)
// Returns a pointer to the end of the integer (not null-terminated).
uint32_t quotient;
if (uii < 1000) {
if (uii < 10) {
*start++ = '0' + uii;
return start;
}
if (uii < 100) {
goto uint32toa_2;
}
quotient = uii / 100;
*start++ = '0' + quotient;
} else {
if (uii < 10000000) {
if (uii >= 100000) {
if (uii < 1000000) {
goto uint32toa_6;
}
quotient = uii / 1000000;
*start++ = '0' + quotient;
goto uint32toa_6b;
}
if (uii < 10000) {
goto uint32toa_4;
}
quotient = uii / 10000;
*start++ = '0' + quotient;
} else {
if (uii >= 100000000) {
quotient = uii / 100000000;
if (uii >= 1000000000) {
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
} else {
*start++ = '0' + quotient;
}
uii -= 100000000 * quotient;
}
quotient = uii / 1000000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
uint32toa_6b:
uii -= 1000000 * quotient;
uint32toa_6:
quotient = uii / 10000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
}
uii -= 10000 * quotient;
uint32toa_4:
// could make a uitoa_z4() call here, but that's slightly slower
quotient = uii / 100;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
}
uii -= 100 * quotient;
uint32toa_2:
return memcpya(start, &(digit2_table[uii * 2]), 2);
}
char* int32toa(int32_t ii, char* start) {
uint32_t uii = ii;
if (ii < 0) {
// -INT_MIN is undefined, but negating the unsigned int equivalent works
*start++ = '-';
uii = -uii;
}
return uint32toa(uii, start);
}
char* uitoa_z4(uint32_t uii, char* start) {
uint32_t quotient = uii / 100;
assert(quotient < 100);
uii -= 100 * quotient;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
return memcpya(start, &(digit2_table[uii * 2]), 2);
}
char* uitoa_z6(uint32_t uii, char* start) {
uint32_t quotient = uii / 10000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
return uitoa_z4(uii - 10000 * quotient, start);
}
char* uitoa_z8(uint32_t uii, char* start) {
uint32_t quotient = uii / 1000000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
return uitoa_z6(uii - 1000000 * quotient, start);
}
char* int64toa(int64_t llii, char* start) {
uint64_t ullii = llii;
uint64_t top_digits;
uint32_t bottom_eight;
uint32_t middle_eight;
if (llii < 0) {
*start++ = '-';
ullii = -ullii;
}