Debian Codegraph @

plink_common.h

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #ifndef __PLINK_COMMON_H__
2 #define __PLINK_COMMON_H__
3
4 // This file is part of PLINK 1.9, copyright (C) 2005-2024 Shaun Purcell,
5 // Christopher Chang.
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 // Resources needed across all plink modules.
22
23 #define _FILE_OFFSET_BITS 64
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <stdint.h>
30 #ifndef __STDC_FORMAT_MACROS
31 # define __STDC_FORMAT_MACROS 1
32 #endif
33 #include <inttypes.h>
34
35 // avoid compiler warning
36 #ifndef NDEBUG
37 #define NDEBUG
38 #endif
39 #include <assert.h>
40
41 // Uncomment this to build this without CBLAS/CLAPACK.
42 // #define NOLAPACK
43
44 // Uncomment this to prevent all unstable features from being accessible from
45 // the command line.
46 // #define STABLE_BUILD
47
48 #define SPECIES_HUMAN 0
49 #define SPECIES_COW 1
50 #define SPECIES_DOG 2
51 #define SPECIES_HORSE 3
52 #define SPECIES_MOUSE 4
53 #define SPECIES_RICE 5
54 #define SPECIES_SHEEP 6
55 #define SPECIES_UNKNOWN 7
56 #define SPECIES_DEFAULT SPECIES_HUMAN
57
58 #define PROG_NAME_STR "plink"
59 #define PROG_NAME_CAPS "PLINK"
60
61 #ifdef _WIN32
62 // needed for MEMORYSTATUSEX
63 #ifndef _WIN64
64 #define WINVER 0x0500
65 #else
66 #define __LP64__
67 #endif
68 #ifndef WIN32_LEAN_AND_MEAN
69 #define WIN32_LEAN_AND_MEAN
70 #endif
71 #include <windows.h>
72 #else // Unix
73 #include <sys/stat.h>
74 #endif
75
76 #ifndef HAVE_NULLPTR
77 #ifndef __cplusplus
78 #define nullptr NULL
79 #else
80 #if __cplusplus <= 199711L
81 #ifndef nullptr
82 #define nullptr NULL
83 #endif
84 #endif
85 #endif
86 #endif
87
88 #ifdef _WIN32
89 #define fseeko fseeko64
90 #define ftello ftello64
91 #include <process.h>
92 # undef PRId64
93 # undef PRIu64
94 # define PRId64 "I64d"
95 # define PRIu64 "I64u"
96 #define pthread_t HANDLE
97 #define THREAD_RET_TYPE unsigned __stdcall
98 #define THREAD_RETURN return 0
99 #define EOLN_STR "\r\n"
100 #define FOPEN_RB "rb"
101 #define FOPEN_WB "wb"
102 #ifdef _WIN64
103 #define getc_unlocked _fgetc_nolock
104 #define putc_unlocked _fputc_nolock
105 #else
106 #define getc_unlocked getc
107 #define putc_unlocked putc
108 #endif
109 #if __cplusplus < 201103L
110 #define uint64_t unsigned long long
111 #define int64_t long long
112 #endif
113 #else
114 #include <pthread.h>
115 #define THREAD_RET_TYPE void*
116 #define THREAD_RETURN return nullptr
117 #ifdef __cplusplus
118 #ifndef PRId64
119 #define PRId64 "lld"
120 #endif
121 #endif
122 #define EOLN_STR "\n"
123 #define FOPEN_RB "r"
124 #define FOPEN_WB "w"
125 #ifndef __APPLE__
126 // argh
127 // not sure what the right threshold actually is, but this works for now
128 // (may break on gcc <3.0? but that shouldn't matter anymore)
129 // tried defining GCC_VERSION, but that didn't always work
130 #if (__GNUC__ <= 4) && (__GNUC_MINOR__ < 8)
131 #define uint64_t unsigned long long
132 #define int64_t long long
133 #endif
134 #endif
135 #endif
136
137 #ifdef _WIN64
138 #define __LP64__
139 #define CTZLU __builtin_ctzll
140 #define CLZLU __builtin_clzll
141 #else
142 #define CTZLU __builtin_ctzl
143 #define CLZLU __builtin_clzl
144 #ifndef __LP64__
145 // attempt to patch GCC 6 build failure
146 #if (__GNUC__ <= 4) && (__GNUC_MINOR__ < 8)
147 #ifndef uintptr_t
148 #define uintptr_t unsigned long
149 #endif
150 #ifndef intptr_t
151 #define intptr_t long
152 #endif
153 #endif
154 #endif
155 #endif
156
157 #ifdef __cplusplus
158 #include <algorithm>
159 # ifdef _WIN32
160 // Windows C++11 <algorithm> resets these values :(
161 # undef PRIu64
162 # undef PRId64
163 # define PRIu64 "I64u"
164 # define PRId64 "I64d"
165 # undef PRIuPTR
166 # undef PRIdPTR
167 # ifdef __LP64__
168 # define PRIuPTR PRIu64
169 # define PRIdPTR PRId64
170 # else
171 # if __cplusplus < 201103L
172 # define PRIuPTR "lu"
173 # define PRIdPTR "ld"
174 # else
175 # define PRIuPTR "u"
176 # define PRIdPTR "d"
177 # endif
178 # endif
179 # endif
180 #define HEADER_INLINE inline
181 #else
182 #define HEADER_INLINE static inline
183 #endif
184
185 // It would be useful to disable compilation on big-endian platforms, but I
186 // don't see a decent portable way to do this (see e.g. discussion at
187 // http://esr.ibiblio.org/?p=5095 ).
188
189 #ifdef __LP64__
190 #ifdef __SSE2__
191 #include <emmintrin.h>
192 #else
193 #define SIMDE_ENABLE_NATIVE_ALIASES
194 #include "x86/sse2.h"
195 #endif
196
197 #define VECFTYPE __m128
198 #define VECITYPE __m128i
199 #define VECDTYPE __m128d
200
201 // useful because of its bitwise complement: ~ZEROLU is a word with all 1
202 // bits, while ~0 is always 32 1 bits.
203 #define ZEROLU 0LLU
204
205 // mainly useful for bitshifts: (ONELU << 32) works in 64-bit builds, while
206 // (1 << 32) is undefined. also used to cast some numbers/expressions to
207 // uintptr_t (e.g. multiplying an int constant by ONELU widens it to 64 bits
208 // only in 64-bit builds; note that 1LU fails on Win64 while 1LLU doesn't do
209 // the right thing for 32-bit builds).
210 #define ONELU 1LLU
211
212 #ifdef _WIN32 // i.e. Win64
213
214 #ifndef PRIuPTR
215 #define PRIuPTR PRIu64
216 #endif
217 #ifndef PRIdPTR
218 #define PRIdPTR PRId64
219 #endif
220 #define PRIxPTR2 "016I64x"
221
222 #else // not _WIN32
223
224 #ifndef PRIuPTR
225 #define PRIuPTR "lu"
226 #endif
227 #ifndef PRIdPTR
228 #define PRIdPTR "ld"
229 #endif
230 #define PRIxPTR2 "016lx"
231
232 #endif // Win64
233
234 #define VEC_BYTES 16
235
236 #else // not __LP64__
237
238 #define ZEROLU 0LU
239 #define ONELU 1LU
240 # if (__GNUC__ <= 4) && (__GNUC_MINOR__ < 8) && (__cplusplus < 201103L)
241 # undef PRIuPTR
242 # undef PRIdPTR
243 # define PRIuPTR "lu"
244 # define PRIdPTR "ld"
245 # endif
246 #define PRIxPTR2 "08lx"
247
248 // todo: update code so this still works when reduced to 4
249 #define VEC_BYTES 8
250
251 #endif // __LP64__
252
253 // use constexpr for these as soon as compiler support is available on all
254 // platforms
255 #define FIVEMASK ((~ZEROLU) / 3)
256 #define AAAAMASK (FIVEMASK * 2)
257
258 #define VEC_BYTES_M1 (VEC_BYTES - 1)
259 #define VEC_BITS (VEC_BYTES * 8)
260 #define VEC_BITS_M1 (VEC_BITS - 1)
261
262 #ifdef DYNAMIC_ZLIB
263 #include <zlib.h>
264 #if !defined(ZLIB_VERNUM) || ZLIB_VERNUM < 0x1240
265 #error "zlib version 1.2.4 or later required."
266 #endif
267 #else
268 #include "../zlib-1.3.1/zlib.h"
269 #endif
270 #include "SFMT.h"
271
272 // 64MB of non-workspace memory guaranteed for now.
273 // Currently also serves as the maximum allele length.
274 #define NON_BIGSTACK_MIN 67108864
275
276 #define PI 3.1415926535897932
277 #define RECIP_2_32 0.00000000023283064365386962890625
278 #define RECIP_2_53 0.00000000000000011102230246251565404236316680908203125
279 // floating point comparison-to-nonzero tolerance, currently 2^{-30}
280 #define EPSILON 0.000000000931322574615478515625
281 // less tolerant versions (2^{-35}, 2^{-44}) for some exact calculations
282 #define SMALLISH_EPSILON 0.00000000002910383045673370361328125
283 #define SMALL_EPSILON 0.00000000000005684341886080801486968994140625
284 // at least sqrt(SMALL_EPSILON)
285 #define BIG_EPSILON 0.000000476837158203125
286 // 53-bit double precision limit
287 #define DOUBLE_PREC_LIMIT 0.00000000000000011102230246251565404236316680908203125
288 #define TWO_63 9223372036854775808.0
289 #define SQRT_HALF 0.70710678118654746
290
291 // 2^{-83} bias to give exact tests maximum ability to determine tiny p-values.
292 // (~2^{-53} is necessary to take advantage of denormalized small numbers, then
293 // allow tail sum to be up to 2^30.)
294 #define EXACT_TEST_BIAS 0.00000000000000000000000010339757656912845935892608650874535669572651386260986328125
295
296 // occasionally used as an infinity substitute that avoids the 32-bit Windows
297 // performance penalty
298 // can import from limits.h, we don't bother to include that for now
299 #ifndef DBL_MAX
300 #define DBL_MAX 1.7976931348623157e308
301 #endif
302
303 // not quite the same as FLT_MAX since it's a double-precision constant
304 #define FLT_MAXD 3.4028234663852886e38
305
306 #define RET_SUCCESS 0
307 #define RET_NOMEM 1
308 #define RET_OPEN_FAIL 2
309 #define RET_INVALID_FORMAT 3
310 #define RET_CALC_NOT_YET_SUPPORTED 4
311 #define RET_INVALID_CMDLINE 5
312 #define RET_WRITE_FAIL 6
313 #define RET_READ_FAIL 7
314 #define RET_THREAD_CREATE_FAIL 8
315 #define RET_ALLELE_MISMATCH 9
316 #define RET_NULL_CALC 10
317 #define RET_ALL_SAMPLES_EXCLUDED 11
318 #define RET_ALL_MARKERS_EXCLUDED 12
319 #define RET_NETWORK 13
320 #define RET_DEGENERATE_DATA 14
321 #define LOAD_PHENO_LAST_COL 127
322
323 // for 2.0 -> 1.9 backports
324 #define RET_MALFORMED_INPUT RET_INVALID_FORMAT
325
326 #define MISC_AFFECTION_01 1LLU
327 #define MISC_NONFOUNDERS 2LLU
328 #define MISC_MAF_SUCC 4LLU
329 #define MISC_FREQ_COUNTS 8LLU
330 #define MISC_FREQ_CC 0x10LLU
331 #define MISC_FREQX 0x20LLU
332 #define MISC_KEEP_ALLELE_ORDER 0x40LLU
333 #define MISC_SET_HH_MISSING 0x80LLU
334 #define MISC_SET_MIXED_MT_MISSING 0x100LLU
335 #define MISC_KEEP_AUTOCONV 0x200LLU
336 #define MISC_LOAD_CLUSTER_KEEP_NA 0x400LLU
337 #define MISC_WRITE_CLUSTER_OMIT_UNASSIGNED 0x800LLU
338 #define MISC_ALLOW_EXTRA_CHROMS 0x1000LLU
339 #define MISC_MAKE_FOUNDERS_REQUIRE_2_MISSING 0x2000LLU
340 #define MISC_MAKE_FOUNDERS_FIRST 0x4000LLU
341 #define MISC_LASSO_REPORT_ZEROES 0x8000LLU
342 #define MISC_LASSO_SELECT_COVARS 0x10000LLU
343 #define MISC_DOUBLE_ID 0x20000LLU
344 #define MISC_BIALLELIC_ONLY 0x40000LLU
345 #define MISC_BIALLELIC_ONLY_STRICT 0x80000LLU
346 #define MISC_BIALLELIC_ONLY_LIST 0x100000LLU
347 #define MISC_VCF_FILTER 0x200000LLU
348 #define MISC_GPLINK 0x400000LLU
349 #define MISC_SNPS_ONLY_JUST_ACGT 0x800000LLU
350 #define MISC_IMPUTE_SEX 0x1000000LLU
351 #define MISC_OXFORD_SNPID_CHR 0x2000000LLU
352 #define MISC_EXTRACT_RANGE 0x4000000LLU
353 #define MISC_EXCLUDE_RANGE 0x8000000LLU
354 #define MISC_MERGEX 0x10000000LLU
355 #define MISC_SET_ME_MISSING 0x20000000LLU
356 #define MISC_SEXCHECK_YCOUNT 0x40000000LLU
357 #define MISC_SEXCHECK_YONLY 0x80000000LLU
358 #define MISC_FAMILY_CLUSTERS 0x100000000LLU
359 #define MISC_FILL_MISSING_A2 0x200000000LLU
360 #define MISC_HET_SMALL_SAMPLE 0x400000000LLU
361 #define MISC_FST_CC 0x800000000LLU
362 #define MISC_SPLIT_MERGE_NOFAIL 0x1000000000LLU
363 #define MISC_REAL_REF_ALLELES 0x2000000000LLU
364 #define MISC_RPLUGIN_DEBUG 0x4000000000LLU
365 #define MISC_MISSING_GZ 0x8000000000LLU
366 #define MISC_FREQ_GZ 0x10000000000LLU
367 #define MISC_HET_GZ 0x20000000000LLU
368 #define MISC_ALLOW_NO_SAMPLES 0x40000000000LLU
369 #define MISC_ALLOW_NO_VARS 0x80000000000LLU
370 #define MISC_VCF_REQUIRE_GT 0x100000000000LLU
371 #define MISC_AC_FOUNDERS 0x200000000000LLU
372
373 // assume for now that .bed must always be accompanied by both .bim and .fam
374 #define FILTER_ALL_REQ 1LLU
375 #define FILTER_BIM_REQ 2LLU
376 #define FILTER_FAM_REQ 4LLU
377
378 // ok with --dosage + --map, but not --dosage by itself
379 #define FILTER_DOSAGEMAP 8LLU
380
381 #define FILTER_NODOSAGE 0x10LLU
382 #define FILTER_NOCNV 0x20LLU
383 #define FILTER_EXCLUDE_MARKERNAME_SNP 0x40LLU
384 #define FILTER_BINARY_CASES 0x80LLU
385 #define FILTER_BINARY_CONTROLS 0x100LLU
386 #define FILTER_BINARY_FEMALES 0x200LLU
387 #define FILTER_BINARY_MALES 0x400LLU
388 #define FILTER_BINARY_FOUNDERS 0x800LLU
389 #define FILTER_BINARY_NONFOUNDERS 0x1000LLU
390 #define FILTER_MAKE_FOUNDERS 0x2000LLU
391 #define FILTER_PRUNE 0x4000LLU
392 #define FILTER_SNPS_ONLY 0x8000LLU
393 #define FILTER_TAIL_PHENO 0x10000LLU
394 #define FILTER_ZERO_CMS 0x20000LLU
395
396 #define CALC_RELATIONSHIP 1LLU
397 #define CALC_IBC 2LLU
398 #define CALC_DISTANCE 4LLU
399 #define CALC_PLINK1_DISTANCE_MATRIX 8LLU
400 #define CALC_PLINK1_IBS_MATRIX 0x10LLU
401 #define CALC_GDISTANCE_MASK 0x1cLLU
402 #define CALC_GROUPDIST 0x20LLU
403 #define CALC_REGRESS_DISTANCE 0x40LLU
404 #define CALC_UNRELATED_HERITABILITY 0x80LLU
405 #define CALC_FREQ 0x100LLU
406 #define CALC_REL_CUTOFF 0x200LLU
407 #define CALC_WRITE_SNPLIST 0x400LLU
408 #define CALC_LIST_23_INDELS 0x800LLU
409 #define CALC_GENOME 0x1000LLU
410 #define CALC_REGRESS_REL 0x2000LLU
411 #define CALC_LD_PRUNE 0x4000LLU
412 #define CALC_DFAM 0x8000LLU
413 #define CALC_TUCC 0x10000LLU
414 #define CALC_MAKE_BED 0x20000LLU
415 #define CALC_RECODE 0x40000LLU
416 #define CALC_MERGE 0x80000LLU
417 #define CALC_WRITE_COVAR 0x100000LLU
418 #define CALC_WRITE_CLUSTER 0x200000LLU
419 #define CALC_MODEL 0x400000LLU
420 #define CALC_HARDY 0x800000LLU
421 #define CALC_GXE 0x1000000LLU
422 #define CALC_IBS_TEST 0x2000000LLU
423 #define CALC_CLUSTER 0x4000000LLU
424 #define CALC_HOMOZYG 0x8000000LLU
425 #define CALC_NEIGHBOR 0x10000000LLU
426 #define CALC_GLM 0x20000000LLU
427 #define CALC_MISSING_REPORT 0x40000000LLU
428 #define CALC_CMH 0x80000000LLU
429 #define CALC_HOMOG 0x100000000LLU
430 #define CALC_LASSO 0x200000000LLU
431 #define CALC_LASSO_LAMBDA 0x400000000LLU
432 #define CALC_WRITE_SET 0x800000000LLU
433 #define CALC_LD 0x1000000000LLU
434 #define CALC_EPI 0x2000000000LLU
435 #define CALC_TESTMISS 0x4000000000LLU
436 #define CALC_TESTMISHAP 0x8000000000LLU
437 #define CALC_SEXCHECK 0x10000000000LLU
438 #define CALC_CLUMP 0x20000000000LLU
439 #define CALC_PCA 0x40000000000LLU
440 #define CALC_BLOCKS 0x80000000000LLU
441 #define CALC_SCORE 0x100000000000LLU
442 #define CALC_MENDEL 0x200000000000LLU
443 #define CALC_HET 0x400000000000LLU
444 #define CALC_FLIPSCAN 0x800000000000LLU
445 #define CALC_TDT 0x1000000000000LLU
446 #define CALC_MAKE_PERM_PHENO 0x2000000000000LLU
447 #define CALC_QFAM 0x4000000000000LLU
448 #define CALC_FST 0x8000000000000LLU
449 #define CALC_SHOW_TAGS 0x10000000000000LLU
450 #define CALC_MAKE_BIM 0x20000000000000LLU
451 #define CALC_MAKE_FAM 0x40000000000000LLU
452 #define CALC_WRITE_VAR_RANGES 0x80000000000000LLU
453 #define CALC_DUPVAR 0x100000000000000LLU
454 #define CALC_RPLUGIN 0x200000000000000LLU
455 #define CALC_ONLY_BIM (CALC_WRITE_SET | CALC_WRITE_SNPLIST | CALC_WRITE_VAR_RANGES | CALC_LIST_23_INDELS | CALC_MAKE_BIM | CALC_DUPVAR)
456 #define CALC_ONLY_FAM (CALC_MAKE_PERM_PHENO | CALC_WRITE_COVAR | CALC_MAKE_FAM)
457 // only room for 6 more basic commands before we need to switch from a single
458 // uint64_t to uintptr_t*/is_set()/etc.
459
460 // necessary to patch heterozygous haploids/female Y chromosome genotypes
461 // during loading?
462 #define XMHH_EXISTS 1
463 #define Y_FIX_NEEDED 2
464 #define NXMHH_EXISTS 4
465
466 #define ALLELE_RECODE 1
467 #define ALLELE_RECODE_MULTICHAR 2
468 #define ALLELE_RECODE_ACGT 4
469
470 // 0 = non-explicit error
471 #define VCF_HALF_CALL_ERROR 1
472 #define VCF_HALF_CALL_MISSING 2
473 #define VCF_HALF_CALL_HAPLOID 3
474 #define VCF_HALF_CALL_REFERENCE 4
475
476 #define M23_MALE 1
477 #define M23_FEMALE 2
478 #define M23_FORCE_MISSING_SEX 4
479 #define M23_SEX 7
480
481 #define MARKER_CMS_OPTIONAL 1
482 #define MARKER_CMS_FORCED 2
483
484 #define UNSORTED_CHROM 1
485 #define UNSORTED_BP 2
486 #define UNSORTED_CM 4
487 #define UNSORTED_SPLIT_CHROM 8
488
489 #define ALLOW_NO_SEX 1
490 #define MUST_HAVE_SEX 2
491
492 #define LGEN_REFERENCE 1
493 #define LGEN_ALLELE_COUNT 2
494
495 #define PHENO_ALL 1
496 #define PHENO_MERGE 2
497
498 #define FAM_COL_1 1
499 #define FAM_COL_34 2
500 #define FAM_COL_5 4
501 #define FAM_COL_6 8
502 #define FAM_COL_13456 15
503
504 #define COVAR_KEEP_PHENO_ON_MISSING_COV 1
505 #define COVAR_NAME 2
506 #define COVAR_NUMBER 4
507 #define COVAR_NO_CONST 8
508 #define COVAR_ALLOW_NONE 0x10
509
510 #define DISTANCE_SQ 1
511 #define DISTANCE_SQ0 2
512 #define DISTANCE_TRI 3
513 #define DISTANCE_SHAPEMASK 3
514 #define DISTANCE_GZ 4
515 #define DISTANCE_BIN 8
516 #define DISTANCE_BIN4 0x10
517 #define DISTANCE_IBS 0x20
518 #define DISTANCE_1_MINUS_IBS 0x40
519 #define DISTANCE_ALCT 0x80
520 #define DISTANCE_TYPEMASK 0xe0
521 #define DISTANCE_FLAT_MISSING 0x100
522 #define DISTANCE_CLUSTER 0x200
523 #define DISTANCE_WTS_NOHEADER 0x400
524
525 #define RECODE_01 1
526 #define RECODE_12 2
527 #define RECODE_TAB 4
528 #define RECODE_DELIMX 8
529 #define RECODE_23 0x10
530 #define RECODE_A 0x20
531 #define RECODE_A_TRANSPOSE 0x40
532 #define RECODE_AD 0x80
533 #define RECODE_BEAGLE 0x100
534 #define RECODE_BEAGLE_NOMAP 0x200
535 #define RECODE_BIMBAM 0x400
536 #define RECODE_BIMBAM_1CHR 0x800
537 #define RECODE_COMPOUND 0x1000
538 #define RECODE_FASTPHASE 0x2000
539 #define RECODE_FASTPHASE_1CHR 0x4000
540 #define RECODE_HV 0x8000
541 #define RECODE_HV_1CHR 0x10000
542 #define RECODE_LGEN 0x20000
543 #define RECODE_LGEN_REF 0x40000
544 #define RECODE_LIST 0x80000
545 #define RECODE_OXFORD 0x100000
546 #define RECODE_RLIST 0x200000
547 #define RECODE_STRUCTURE 0x400000
548 #define RECODE_TRANSPOSE 0x800000
549 #define RECODE_PED 0x1000000
550 #define RECODE_VCF 0x2000000
551 #define RECODE_TYPEMASK 0x3fffff0
552 #define RECODE_FID 0x4000000
553 #define RECODE_IID 0x8000000
554 #define RECODE_INCLUDE_ALT 0x10000000
555 #define RECODE_BGZ 0x20000000
556 #define RECODE_GEN_GZ 0x40000000
557 #define RECODE_OMIT_NONMALE_Y 0x80000000U
558
559 #define GENOME_OUTPUT_GZ 1
560 #define GENOME_REL_CHECK 2
561 #define GENOME_OUTPUT_FULL 4
562 #define GENOME_IBD_UNBOUNDED 8
563 #define GENOME_NUDGE 0x10
564 // separate flag to ensure behavior is unchanged under --unbounded
565 #define GENOME_FILTER_PI_HAT 0x20
566
567 #define WRITE_COVAR_PHENO 1
568 #define WRITE_COVAR_NO_PARENTS 2
569 #define WRITE_COVAR_NO_SEX 4
570 #define WRITE_COVAR_FEMALE_2 8
571 #define WRITE_COVAR_DUMMY 0x10
572 #define WRITE_COVAR_DUMMY_NO_ROUND 0x20
573
574 #define MERGE_MODE_MASK 7
575 #define MERGE_EQUAL_POS 8
576 #define MERGE_BINARY 16
577 #define MERGE_LIST 32
578
579 #define SAMPLE_SORT_NONE 1
580 #define SAMPLE_SORT_NATURAL 2
581 #define SAMPLE_SORT_ASCII 4
582 #define SAMPLE_SORT_FILE 8
583
584 #define HWE_MIDP 1
585 #define HWE_THRESH_MIDP 2
586 #define HWE_THRESH_ALL 4
587 #define HWE_GZ 8
588
589 #define MENDEL_FILTER 1
590 #define MENDEL_FILTER_VAR_FIRST 2
591 #define MENDEL_DUOS 4
592 #define MENDEL_MULTIGEN 8
593 #define MENDEL_SUMMARIES_ONLY 0x10
594
595 #define DUMMY_MISSING_GENO 1
596 #define DUMMY_MISSING_PHENO 2
597 #define DUMMY_SCALAR_PHENO 4
598 #define DUMMY_ACGT 8
599 #define DUMMY_1234 0x10
600 #define DUMMY_12 0x20
601
602 #define SIMULATE_QT 1
603 #define SIMULATE_TAGS 2
604 #define SIMULATE_HAPS 4
605 #define SIMULATE_ACGT 8
606 #define SIMULATE_1234 0x10
607 #define SIMULATE_12 0x20
608
609 #define MODEL_ASSOC 1
610 #define MODEL_FISHER 2
611 #define MODEL_FISHER_MIDP 4
612 #define MODEL_PERM 8
613 #define MODEL_MPERM 0x10
614 #define MODEL_GENEDROP 0x20
615 #define MODEL_PERM_COUNT 0x40
616 #define MODEL_ASSOC_COUNTS 0x80
617 #define MODEL_ASSOC_FDEPR 0x100
618 #define MODEL_DMASK 0x1a6
619 #define MODEL_QT_MEANS 0x200
620 #define MODEL_PDOM 0x400
621 #define MODEL_PREC 0x800
622 #define MODEL_PGEN 0x1000
623 #define MODEL_PTREND 0x2000
624 #define MODEL_TRENDONLY 0x4000
625 #define MODEL_PMASK (MODEL_PDOM | MODEL_PREC | MODEL_PGEN | MODEL_PTREND | MODEL_TRENDONLY)
626 #define MODEL_LIN 0x8000
627 #define MODEL_QMASK (MODEL_QT_MEANS | MODEL_LIN)
628 #define MODEL_SET_TEST 0x10000
629
630 #define GLM_LOGISTIC 1
631 #define GLM_PERM 2
632 #define GLM_MPERM 4
633 #define GLM_GENEDROP 8
634 #define GLM_PERM_COUNT 0x10
635 #define GLM_GENOTYPIC 0x20
636 #define GLM_HETHOM 0x40
637 #define GLM_DOMINANT 0x80
638 #define GLM_RECESSIVE 0x100
639 #define GLM_NO_SNP 0x200
640 #define GLM_HIDE_COVAR 0x400
641 #define GLM_SEX 0x800
642 #define GLM_NO_X_SEX 0x1000
643 #define GLM_INTERACTION 0x2000
644 #define GLM_STANDARD_BETA 0x4000
645 #define GLM_BETA 0x8000
646 #define GLM_TEST_ALL 0x10000
647 #define GLM_CONDITION_DOMINANT 0x20000
648 #define GLM_CONDITION_RECESSIVE 0x40000
649 #define GLM_SET_TEST 0x80000
650 #define GLM_NO_SNP_EXCL 0x831ea
651 #define GLM_INTERCEPT 0x100000
652
653 #define MPERM_DUMP_BEST 1
654 #define MPERM_DUMP_ALL 2
655
656 // (2^31 - 1000001) / 2
657 #define APERM_MAX 1073241823
658
659 #define ADJUST_GC 2
660 #define ADJUST_LOG10 4
661 #define ADJUST_QQ 8
662 #define ADJUST_LAMBDA 16
663
664 #define DUPVAR_REF 1
665 #define DUPVAR_IDS_ONLY 2
666 #define DUPVAR_SUPPRESS_FIRST 4
667
668 #define CNV_MAKE_MAP 1
669 #define CNV_MAKE_MAP_LONG 2
670 #define CNV_CHECK_NO_OVERLAP 4
671 #define CNV_DEL 8
672 #define CNV_DUP 0x10
673 #define CNV_WRITE_FREQ 0x20
674 #define CNV_UNIQUE 0x40
675 #define CNV_DROP_NO_SEGMENT 0x80
676 #define CNV_SAMPLE_PERM 0x100
677 #define CNV_ENRICHMENT_TEST 0x200
678 #define CNV_TEST 0x400
679 #define CNV_TEST_FORCE_1SIDED 0x800
680 #define CNV_TEST_FORCE_2SIDED 0x1000
681 #define CNV_TEST_REGION 0x2000
682 #define CNV_TRACK 0x4000
683 #define CNV_SEGLIST 0x8000
684 #define CNV_REPORT_REGIONS 0x10000
685 #define CNV_VERBOSE_REPORT_REGIONS 0x20000
686 #define CNV_WRITE 0x40000
687 #define CNV_EXCLUDE_OFF_BY_1 0x80000
688
689 #define CNV_INTERSECT 1
690 #define CNV_EXCLUDE 2
691 #define CNV_COUNT 4
692
693 #define CNV_OVERLAP 1
694 #define CNV_OVERLAP_REGION 2
695 #define CNV_OVERLAP_UNION 3
696 #define CNV_DISRUPT 4
697
698 #define CNV_FREQ_EXCLUDE_ABOVE 1
699 #define CNV_FREQ_EXCLUDE_BELOW 2
700 #define CNV_FREQ_EXCLUDE_EXACT 4
701 #define CNV_FREQ_INCLUDE_EXACT 8
702 #define CNV_FREQ_FILTER 15
703 #define CNV_FREQ_OVERLAP 16
704 #define CNV_FREQ_METHOD2 32
705
706 #define SEGMENT_GROUP 1
707
708 // default jackknife iterations
709 #define ITERS_DEFAULT 100000
710 #define MAX_PCS_DEFAULT 20
711
712 #define BIGSTACK_MIN_MB 64
713 #define BIGSTACK_DEFAULT_MB 2048
714
715 #ifdef __LP64__
716 #define BITCT 64
717
718 // unions generally shouldn't be used for reinterpret_cast's job (memcpy is
719 // the right C-compatible way), but vectors are an exception to this rule.
720 typedef union {
721 VECFTYPE vf;
722 VECITYPE vi;
723 VECDTYPE vd;
724 uintptr_t u8[VEC_BITS / BITCT];
725 double d8[VEC_BYTES / sizeof(double)];
726 float f4[VEC_BYTES / sizeof(float)];
727 uint32_t u4[VEC_BYTES / sizeof(int32_t)];
728 } __univec;
729 #else
730 #define BITCT 32
731 #endif
732
733 #define BITCT2 (BITCT / 2)
734 #define BYTECT (BITCT / 8)
735 #define BYTECT4 (BITCT / 32)
736 #define VEC_WORDS (VEC_BITS / BITCT)
737 #define VEC_INT32 (VEC_BYTES / 4)
738
739 // assumed number of bytes per cache line, for alignment
740 #define CACHELINE 64
741
742 #define CACHELINE_BIT (CACHELINE * 8)
743 #define CACHELINE_INT32 (CACHELINE / 4)
744 #define CACHELINE_INT64 (CACHELINE / 8)
745 #define CACHELINE_WORD (CACHELINE / BYTECT)
746 #define CACHELINE_DBL (CACHELINE / 8)
747
748 // alignment must be a power of 2
749 HEADER_INLINE uintptr_t round_up_pow2(uintptr_t val, uintptr_t alignment) {
750 uintptr_t alignment_m1 = alignment - 1;
751 assert(!(alignment & alignment_m1));
752 return (val + alignment_m1) & (~alignment_m1);
753 }
754
755 #define BITCT_TO_VECCT(val) (((val) + (VEC_BITS - 1)) / VEC_BITS)
756 #define BITCT_TO_WORDCT(val) (((val) + (BITCT - 1)) / BITCT)
757 #define BITCT_TO_ALIGNED_WORDCT(val) (VEC_WORDS * BITCT_TO_VECCT(val))
758
759 #define QUATERCT_TO_VECCT(val) (((val) + ((VEC_BITS / 2) - 1)) / (VEC_BITS / 2))
760 #define QUATERCT_TO_WORDCT(val) (((val) + (BITCT2 - 1)) / BITCT2)
761 #define QUATERCT_TO_ALIGNED_WORDCT(val) (VEC_WORDS * QUATERCT_TO_VECCT(val))
762
763 // todo: get rid of (BITCT_TO_WORDCT(x) == QUATERCT_TO_VECCT(x)) and similar
764 // assumptions, in preparation for AVX2
765
766 #ifdef __LP64__
767 #define round_up_pow2_ull round_up_pow2
768 #else
769 HEADER_INLINE uint64_t round_up_pow2_ull(uint64_t val, uint64_t alignment) {
770 uint64_t alignment_m1 = alignment - 1;
771 assert(!(alignment & alignment_m1));
772 return (val + alignment_m1) & (~alignment_m1);
773 }
774 #endif
775
776 // 32-bit instead of word-length bitwise not here, when val can be assumed to
777 // be 32-bit.
778 // (note that the sizeof operator "returns" an uintptr_t, not a uint32_t; hence
779 // the lack of sizeof in the CACHELINE_INT32, etc. definitions.)
780 HEADER_INLINE uint32_t round_up_pow2_ui(uint32_t val, uint32_t alignment) {
781 uint32_t alignment_m1 = alignment - 1;
782 assert(!(alignment & alignment_m1));
783 return (val + alignment_m1) & (~alignment_m1);
784 }
785
786 #define MAXV(aa, bb) (((bb) > (aa))? (bb) : (aa))
787 #define MINV(aa, bb) (((aa) > (bb))? (bb) : (aa))
788
789 #ifdef _WIN32
790 // if MAX_THREADS > 65, single WaitForMultipleObjects calls must be converted
791 // into loops
792 #define MAX_THREADS 64
793 #define MAX_THREADS_P1 65
794 #else
795 // shouldn't be larger than MODEL_BLOCKSIZE for now
796 #define MAX_THREADS 512
797 #define MAX_THREADS_P1 513
798 #endif
799
800 // defined as a macro since type of idx can vary; might want a debug
801 // compilation mode which performs type-checking, though
802 #define EXTRACT_2BIT_GENO(ulptr, idx) (((ulptr)[(idx) / BITCT2] >> (2 * ((idx) % BITCT2))) & 3)
803
804 // generic maximum line length. .ped/.vcf/etc. lines can of course be longer
805 #define MAXLINELEN 131072
806
807 // must be at least 2 * MAXLINELEN + 2 to support generic token loader.
808 #define TEXTBUF_SIZE (2 * MAXLINELEN + 256)
809
810 // Maximum length of chromosome, variant, FID, IID, cluster, and set IDs (not
811 // including terminating null, that's what _P1 is for). This value supports up
812 // to 8 IDs per line (maximum so far is 5, for e.g. --hom).
813 #define MAX_ID_SLEN 16000
814
815 #define MAX_ID_BLEN (MAX_ID_SLEN + 1)
816 #define MAX_ID_SLEN_STR "16000"
817
818 // Maximum size of "dynamically" allocated line load buffer. (This is the
819 // limit that applies to .vcf and similar files.) Inconvenient to go higher
820 // since fgets() takes a int32_t size argument.
821 #define MAXLINEBUFLEN 0x7fffffc0
822
823 // Default --perm-batch-size value in most contexts. It may actually be better
824 // to *avoid* a power of two due to the need for transpositions involving this
825 // stride; see e.g. http://danluu.com/3c-conflict/ ; try 448 instead? This
826 // should be tested during PLINK 2.0 development.
827 #define DEFAULT_PERM_BATCH_SIZE 512
828
829 // note that this is NOT foolproof: see e.g.
830 // http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html . (This
831 // is why I haven't bothered with OS-based #ifdefs here.) But it should be
832 // good enough in practice.
833 #define FNAMESIZE 4096
834
835 // allow extensions like .model.trend.fisher.set.score.adjusted
836 #define MAX_POST_EXT 39
837
838 // number of types of jackknife values to precompute (x^2, y^2, x, y, xy)
839 #define JACKKNIFE_VALS_DIST 5
840 #define JACKKNIFE_VALS_GROUPDIST 3
841
842 #ifdef __LP64__
843 // number of snp-major .bed lines to read at once for distance calc if
844 // exponent is nonzero.
845 #define MULTIPLEX_DIST_EXP 64
846 // number of snp-major .bed lines to read at once for relationship calc
847 #define MULTIPLEX_REL 60
848 #else
849 // N.B. 32-bit version not as carefully tested or optimized, but I'll try to
850 // make sure it works properly
851 #define MULTIPLEX_DIST_EXP 28
852 #define MULTIPLEX_REL 30
853 #endif
854
855 // used to size a few tables
856 #define EXPECTED_MISSING_FREQ 0.05
857
858 // load markers in blocks to enable multithreading and, for quantitative
859 // phenotypes, PERMORY-style LD exploitation
860 #define MODEL_BLOCKSIZE 1024
861 #define MODEL_BLOCKKEEP 64
862
863 // string hash table constants, currently only relevant for merge operations
864 // and annotate()
865 // (dynamic sizing used for main marker name lookup)
866
867 // last prime before 2^19
868 // size chosen to be likely to fit in L3 cache
869 #define HASHSIZE 524287
870 #define HASHSIZE_S 524287
871
872 #ifdef __LP64__
873 #define HASHMEM 4194304
874 #else
875 #define HASHMEM 2097152
876 #endif
877
878 typedef struct {
879 uint32_t min;
880 uint32_t max;
881 double alpha;
882 double beta;
883 double init_interval;
884 double interval_slope;
885 } Aperm_info;
886
887 // Generic text I/O buffer: any function which reads from/writes to a text file
888 // or the console may clobber it. Sized to fit two MAXLINELEN-length lines
889 // plus a bit extra.
890 extern char g_textbuf[];
891
892 extern const char g_one_char_strs[];
893 extern const char* g_missing_geno_ptr;
894 extern const char* g_output_missing_geno_ptr;
895 extern char g_missing_geno_char2;
896
897 HEADER_INLINE const char* cond_replace(const char* ss, const char* match_str, const char* replace_str) {
898 return (ss != match_str)? ss : replace_str;
899 }
900
901 uint32_t aligned_malloc(uintptr_t size, uintptr_t** aligned_pp);
902
903 void aligned_free(uintptr_t* aligned_pp);
904
905 HEADER_INLINE void aligned_free_cond(uintptr_t* aligned_ptr) {
906 if (aligned_ptr) {
907 aligned_free(aligned_ptr);
908 }
909 }
910
911 HEADER_INLINE void aligned_free_null(uintptr_t** aligned_pp) {
912 aligned_free(*aligned_pp);
913 *aligned_pp = nullptr;
914 }
915
916 HEADER_INLINE void aligned_free_cond_null(uintptr_t** aligned_pp) {
917 if (*aligned_pp) {
918 aligned_free(*aligned_pp);
919 *aligned_pp = nullptr;
920 }
921 }
922
923 extern uintptr_t g_failed_alloc_attempt_size;
924
925 extern sfmt_t g_sfmt;
926
927 // file-scope string constants don't always have the g_ prefix, but multi-file
928 // constants are always tagged.
929 extern const char g_errstr_fopen[];
930 extern const char g_cmdline_format_str[];
931
932 extern FILE* g_logfile;
933
934 #define LOGBUFLEN (MAXLINELEN * 2)
935 // mostly-safe sprintf buffer. warning: do NOT put allele codes or
936 // arbitrary-length lists in here.
937 extern char g_logbuf[];
938
939 extern uint32_t g_debug_on;
940 extern uint32_t g_log_failed;
941
942 // should remove this global: multithreaded functions should use a file-local
943 // thread_ct which will occasionally be smaller due to job size.
944 extern uint32_t g_thread_ct;
945
946 typedef struct ll_str_struct {
947 struct ll_str_struct* next;
948 char ss[];
949 } Ll_str;
950
951 typedef struct ll_ctstr_entry_struct {
952 struct ll_ctstr_entry_struct* next;
953 uint32_t ct;
954 char ss[];
955 } Ll_ctstr_entry;
956
957 typedef struct two_col_params_struct {
958 uint32_t colx;
959 uint32_t colid;
960 uint32_t skip;
961 char skipchar;
962 char fname[];
963 } Two_col_params;
964
965 typedef struct range_list_struct {
966 char* names;
967 unsigned char* starts_range;
968 uint32_t name_ct;
969 uint32_t name_max_len;
970 } Range_list;
971
972 // Pushes a copy of ss (allocated via malloc) onto ll_stack.
973 uint32_t push_ll_str(const char* ss, Ll_str** ll_stack_ptr);
974
975 // warning: do NOT include allele codes (unless they're guaranteed to be SNPs)
976 // in log strings; they can overflow the buffer.
977 void logstr(const char* ss);
978
979 void logprint(const char* ss);
980
981 void logerrprint(const char* ss);
982
983 void logprintb();
984
985 void logerrprintb();
986
987 #define LOGPRINTF(...) snprintf(g_logbuf, LOGBUFLEN, __VA_ARGS__); logprintb();
988
989 #define LOGERRPRINTF(...) snprintf(g_logbuf, LOGBUFLEN, __VA_ARGS__); logerrprintb();
990
991 // input for wordwrap/LOGPRINTFWW should have no intermediate '\n's. If
992 // suffix_len is 0, there should be a terminating \n.
993 // void wordwrap(uint32_t suffix_len, char* ss);
994
995 void wordwrapb(uint32_t suffix_len);
996
997 #define LOGPREPRINTFWW(...) snprintf(g_logbuf, LOGBUFLEN, __VA_ARGS__); wordwrapb(0);
998
999 #define LOGPRINTFWW(...) snprintf(g_logbuf, LOGBUFLEN, __VA_ARGS__); wordwrapb(0); logprintb();
1000
1001 #define LOGERRPRINTFWW(...) snprintf(g_logbuf, LOGBUFLEN, __VA_ARGS__); wordwrapb(0); logerrprintb();
1002
1003 // 5 = length of "done." suffix, which is commonly used
1004 #define LOGPRINTFWW5(...) snprintf(g_logbuf, LOGBUFLEN, __VA_ARGS__); wordwrapb(5); logprintb();
1005
1006 #ifdef STABLE_BUILD
1007 #define UNSTABLE(val) sptr = strcpya(&(g_logbuf[9]), val); goto main_unstable_disabled
1008 #else
1009 #define UNSTABLE(val)
1010 #endif
1011
1012 int32_t fopen_checked(const char* fname, const char* mode, FILE** target_ptr);
1013
1014 HEADER_INLINE int32_t putc_checked(int32_t ii, FILE* outfile) {
1015 putc_unlocked(ii, outfile);
1016 return ferror(outfile);
1017 }
1018
1019 HEADER_INLINE int32_t fputs_checked(const char* ss, FILE* outfile) {
1020 fputs(ss, outfile);
1021 return ferror(outfile);
1022 }
1023
1024 // This must be used for all fwrite() calls where len could be >= 2^31, since
1025 // OS X raw fwrite() doesn't work in that case.
1026 int32_t fwrite_checked(const void* buf, size_t len, FILE* outfile);
1027
1028 HEADER_INLINE int32_t fread_checked(char* buf, uintptr_t len, FILE* infile, uintptr_t* bytes_read_ptr) {
1029 *bytes_read_ptr = fread(buf, 1, len, infile);
1030 return ferror(infile);
1031 }
1032
1033 HEADER_INLINE void fclose_cond(FILE* fptr) {
1034 if (fptr) {
1035 fclose(fptr);
1036 }
1037 }
1038
1039 HEADER_INLINE int32_t fclose_null(FILE** fptr_ptr) {
1040 int32_t ii = ferror(*fptr_ptr);
1041 int32_t jj = fclose(*fptr_ptr);
1042 *fptr_ptr = nullptr;
1043 return ii || jj;
1044 }
1045
1046 // Also sets 128k read buffer. Can return RET_OPEN_FAIL or RET_NOMEM.
1047 int32_t gzopen_read_checked(const char* fname, gzFile* gzf_ptr);
1048
1049 // pigz interface should be used for writing .gz files.
1050
1051 HEADER_INLINE int32_t gzclose_null(gzFile* gzf_ptr) {
1052 int32_t ii = gzclose(*gzf_ptr);
1053 *gzf_ptr = nullptr;
1054 return (ii != Z_OK);
1055 }
1056
1057 HEADER_INLINE void gzclose_cond(gzFile gz_infile) {
1058 if (gz_infile) {
1059 gzclose(gz_infile);
1060 }
1061 }
1062
1063 HEADER_INLINE int32_t flexwrite_checked(const void* buf, size_t len, uint32_t output_gz, FILE* outfile, gzFile gz_outfile) {
1064 if (!output_gz) {
1065 return fwrite_checked(buf, len, outfile);
1066 } else {
1067 return (!gzwrite(gz_outfile, buf, len));
1068 }
1069 }
1070
1071 HEADER_INLINE int32_t flexputc_checked(int32_t ii, uint32_t output_gz, FILE* outfile, gzFile gz_outfile) {
1072 if (!output_gz) {
1073 putc(ii, outfile);
1074 return ferror(outfile);
1075 } else {
1076 return (gzputc(gz_outfile, ii) == -1);
1077 }
1078 }
1079
1080 HEADER_INLINE int32_t flexputs_checked(const char* ss, uint32_t output_gz, FILE* outfile, gzFile gz_outfile) {
1081 if (!output_gz) {
1082 return fputs_checked(ss, outfile);
1083 } else {
1084 return (gzputs(gz_outfile, ss) == -1);
1085 }
1086 }
1087
1088 HEADER_INLINE int32_t flexclose_null(uint32_t output_gz, FILE** fptr_ptr, gzFile* gzf_ptr) {
1089 if (!output_gz) {
1090 return fclose_null(fptr_ptr);
1091 } else {
1092 return gzclose_null(gzf_ptr);
1093 }
1094 }
1095
1096 // manually managed, very large double-ended stack
1097 extern unsigned char* g_bigstack_base;
1098 extern unsigned char* g_bigstack_end;
1099
1100 HEADER_INLINE uintptr_t bigstack_left() {
1101 return (((uintptr_t)g_bigstack_end) - ((uintptr_t)g_bigstack_base));
1102 }
1103
1104 // Basic 64-byte-aligned allocation at bottom of stack.
1105 unsigned char* bigstack_alloc(uintptr_t size);
1106
1107
1108 // Typesafe, return-0-iff-success interfaces. (See also bigstack_calloc_...
1109 // further below.)
1110 HEADER_INLINE int32_t bigstack_alloc_c(uintptr_t ct, char** cp_ptr) {
1111 *cp_ptr = (char*)bigstack_alloc(ct);
1112 return !(*cp_ptr);
1113 }
1114
1115 HEADER_INLINE int32_t bigstack_alloc_d(uintptr_t ct, double** dp_ptr) {
1116 *dp_ptr = (double*)bigstack_alloc(ct * sizeof(double));
1117 return !(*dp_ptr);
1118 }
1119
1120 HEADER_INLINE int32_t bigstack_alloc_f(uintptr_t ct, float** fp_ptr) {
1121 *fp_ptr = (float*)bigstack_alloc(ct * sizeof(float));
1122 return !(*fp_ptr);
1123 }
1124
1125 HEADER_INLINE int32_t bigstack_alloc_i(uintptr_t ct, int32_t** ip_ptr) {
1126 *ip_ptr = (int32_t*)bigstack_alloc(ct * sizeof(int32_t));
1127 return !(*ip_ptr);
1128 }
1129
1130 HEADER_INLINE int32_t bigstack_alloc_uc(uintptr_t ct, unsigned char** ucp_ptr) {
1131 *ucp_ptr = bigstack_alloc(ct);
1132 return !(*ucp_ptr);
1133 }
1134
1135 HEADER_INLINE int32_t bigstack_alloc_ui(uintptr_t ct, uint32_t** uip_ptr) {
1136 *uip_ptr = (uint32_t*)bigstack_alloc(ct * sizeof(int32_t));
1137 return !(*uip_ptr);
1138 }
1139
1140 HEADER_INLINE int32_t bigstack_alloc_ul(uintptr_t ct, uintptr_t** ulp_ptr) {
1141 *ulp_ptr = (uintptr_t*)bigstack_alloc(ct * sizeof(intptr_t));
1142 return !(*ulp_ptr);
1143 }
1144
1145 HEADER_INLINE int32_t bigstack_alloc_ll(uintptr_t ct, int64_t** llp_ptr) {
1146 *llp_ptr = (int64_t*)bigstack_alloc(ct * sizeof(int64_t));
1147 return !(*llp_ptr);
1148 }
1149
1150 HEADER_INLINE int32_t bigstack_alloc_ull(uintptr_t ct, uint64_t** ullp_ptr) {
1151 *ullp_ptr = (uint64_t*)bigstack_alloc(ct * sizeof(int64_t));
1152 return !(*ullp_ptr);
1153 }
1154
1155 HEADER_INLINE void bigstack_reset(const void* new_base) {
1156 g_bigstack_base = (unsigned char*)new_base;
1157 }
1158
1159 HEADER_INLINE void bigstack_end_reset(const void* new_end) {
1160 g_bigstack_end = (unsigned char*)new_end;
1161 }
1162
1163 HEADER_INLINE void bigstack_double_reset(const void* new_base, const void* new_end) {
1164 bigstack_reset(new_base);
1165 bigstack_end_reset(new_end);
1166 }
1167
1168 void bigstack_shrink_top(const void* rebase, uintptr_t new_size);
1169
1170 #define END_ALLOC_CHUNK 16
1171 #define END_ALLOC_CHUNK_M1 (END_ALLOC_CHUNK - 1)
1172
1173 HEADER_INLINE void bigstack_end_set(const void* unaligned_end) {
1174 g_bigstack_end = (unsigned char*)(((uintptr_t)unaligned_end) & (~(END_ALLOC_CHUNK_M1 * ONELU)));
1175 }
1176
1177 // assumes size is divisible by END_ALLOC_CHUNK
1178 // (no value in directly calling this with a constant size parameter: the
1179 // compiler will properly optimize a bigstack_end_alloc() call)
1180 unsigned char* bigstack_end_alloc_presized(uintptr_t size);
1181
1182 HEADER_INLINE unsigned char* bigstack_end_alloc(uintptr_t size) {
1183 // multiplication by ONELU is one way to widen an int to word-size.
1184 size = round_up_pow2(size, END_ALLOC_CHUNK);
1185 return bigstack_end_alloc_presized(size);
1186 }
1187
1188 #define bigstack_end_aligned_alloc bigstack_end_alloc
1189
1190 HEADER_INLINE int32_t bigstack_end_alloc_c(uintptr_t ct, char** cp_ptr) {
1191 *cp_ptr = (char*)bigstack_end_alloc(ct);
1192 return !(*cp_ptr);
1193 }
1194
1195 HEADER_INLINE int32_t bigstack_end_alloc_d(uintptr_t ct, double** dp_ptr) {
1196 *dp_ptr = (double*)bigstack_end_alloc(ct * sizeof(double));
1197 return !(*dp_ptr);
1198 }
1199
1200 HEADER_INLINE int32_t bigstack_end_alloc_f(uintptr_t ct, float** fp_ptr) {
1201 *fp_ptr = (float*)bigstack_end_alloc(ct * sizeof(float));
1202 return !(*fp_ptr);
1203 }
1204
1205 HEADER_INLINE int32_t bigstack_end_alloc_i(uintptr_t ct, int32_t** ip_ptr) {
1206 *ip_ptr = (int32_t*)bigstack_end_alloc(ct * sizeof(int32_t));
1207 return !(*ip_ptr);
1208 }
1209
1210 HEADER_INLINE int32_t bigstack_end_alloc_uc(uintptr_t ct, unsigned char** ucp_ptr) {
1211 *ucp_ptr = bigstack_end_alloc(ct);
1212 return !(*ucp_ptr);
1213 }
1214
1215 HEADER_INLINE int32_t bigstack_end_alloc_ui(uintptr_t ct, uint32_t** uip_ptr) {
1216 *uip_ptr = (uint32_t*)bigstack_end_alloc(ct * sizeof(int32_t));
1217 return !(*uip_ptr);
1218 }
1219
1220 HEADER_INLINE int32_t bigstack_end_alloc_ul(uintptr_t ct, uintptr_t** ulp_ptr) {
1221 *ulp_ptr = (uintptr_t*)bigstack_end_alloc(ct * sizeof(intptr_t));
1222 return !(*ulp_ptr);
1223 }
1224
1225 HEADER_INLINE int32_t bigstack_end_alloc_ll(uintptr_t ct, int64_t** llp_ptr) {
1226 *llp_ptr = (int64_t*)bigstack_end_alloc(ct * sizeof(int64_t));
1227 return !(*llp_ptr);
1228 }
1229
1230 HEADER_INLINE int32_t bigstack_end_alloc_ull(uintptr_t ct, uint64_t** ullp_ptr) {
1231 *ullp_ptr = (uint64_t*)bigstack_end_alloc(ct * sizeof(int64_t));
1232 return !(*ullp_ptr);
1233 }
1234
1235 HEADER_INLINE int32_t bigstack_end_alloc_llstr(uintptr_t str_bytes, Ll_str** llstrp_ptr) {
1236 *llstrp_ptr = (Ll_str*)bigstack_end_alloc(str_bytes + sizeof(Ll_str));
1237 return !(*llstrp_ptr);
1238 }
1239
1240
1241 HEADER_INLINE int32_t is_letter(unsigned char ucc) {
1242 return (((ucc & 192) == 64) && (((ucc - 1) & 31) < 26));
1243 }
1244
1245 // if we need the digit value, better to use (unsigned char)cc - '0'...
1246 HEADER_INLINE int32_t is_digit(unsigned char ucc) {
1247 return (ucc <= '9') && (ucc >= '0');
1248 }
1249
1250 HEADER_INLINE int32_t is_not_digit(unsigned char ucc) {
1251 return (ucc > '9') || (ucc < '0');
1252 }
1253
1254 HEADER_INLINE int32_t is_not_nzdigit(unsigned char ucc) {
1255 return (ucc > '9') || (ucc <= '0');
1256 }
1257
1258 // may as well treat all chars < 32, except tab, as eoln...
1259 // kns = "known non-space" (where tab counts as a space)
1260 /*
1261 HEADER_INLINE int32_t is_eoln_kns(unsigned char ucc) {
1262 return (ucc < 32);
1263 }
1264 */
1265
1266 HEADER_INLINE int32_t is_space_or_eoln(unsigned char ucc) {
1267 return (ucc <= 32);
1268 }
1269
1270 // could assert ucc is not a space/tab
1271 #define is_eoln_kns is_space_or_eoln
1272
1273 HEADER_INLINE int32_t is_eoln_char(unsigned char ucc) {
1274 return (ucc < 32) && (ucc != 9);
1275 }
1276
1277 HEADER_INLINE int32_t is_eoln_or_comment_kns(unsigned char ucc) {
1278 return (ucc < 32) || (ucc == '#');
1279 }
1280
1281 HEADER_INLINE int32_t no_more_tokens_kns(const char* sptr) {
1282 return ((!sptr) || is_eoln_kns(*sptr));
1283 }
1284
1285 HEADER_INLINE char* skip_initial_spaces(char* sptr) {
1286 while ((*sptr == ' ') || (*sptr == '\t')) {
1287 sptr++;
1288 }
1289 return sptr;
1290 }
1291
1292 /*
1293 HEADER_INLINE int32_t is_space_or_eoln(unsigned char cc) {
1294 // ' ', \t, \n, \0, \r
1295 #ifdef __LP64__
1296 return (ucc <= 32) && (0x100002601LLU & (1LLU << ucc));
1297 #else
1298 return ((ucc <= 32) && ((ucc == ' ') || (0x2601LU & (ONELU << ucc))));
1299 #endif
1300 }
1301 */
1302
1303 // Returns whether uppercased ss matches nonempty fixed_str. Assumes fixed_str
1304 // contains nothing but letters and a null terminator.
1305 uint32_t match_upper(const char* ss, const char* fixed_str);
1306
1307 uint32_t match_upper_counted(const char* ss, const char* fixed_str, uint32_t ct);
1308
1309 // Reads an integer in [1, cap]. Assumes first character is nonspace. Has the
1310 // overflow detection atoi() lacks.
1311 #ifdef __LP64__
1312 uint32_t scan_posint_capped(const char* ss, uint64_t cap, uint32_t* valp);
1313
1314 uint32_t scan_uint_capped(const char* ss, uint64_t cap, uint32_t* valp);
1315
1316 uint32_t scan_int_abs_bounded(const char* ss, uint64_t bound, int32_t* valp);
1317 #else // not __LP64__
1318 // Need to be more careful in 32-bit case due to overflow.
1319 // A funny-looking div_10/mod_10 interface is used since the cap will usually
1320 // be a constant, and we want the integer division/modulus to occur at compile
1321 // time.
1322 uint32_t scan_posint_capped32(const char* ss, uint32_t cap_div_10, uint32_t cap_mod_10, uint32_t* valp);
1323
1324 uint32_t scan_uint_capped32(const char* ss, uint32_t cap_div_10, uint32_t cap_mod_10, uint32_t* valp);
1325
1326 uint32_t scan_int_abs_bounded32(const char* ss, uint32_t bound_div_10, uint32_t bound_mod_10, int32_t* valp);
1327
1328 #define scan_posint_capped(aa, bb, cc) scan_posint_capped32((aa), (bb) / 10, (bb) % 10, (cc))
1329
1330 #define scan_uint_capped(aa, bb, cc) scan_uint_capped32((aa), (bb) / 10, (bb) % 10, (cc))
1331
1332 #define scan_int_abs_bounded(aa, bb, cc) scan_int_abs_bounded32((aa), (bb) / 10, (bb) % 10, (cc))
1333 #endif
1334
1335 // intentionally rejects -2^31 for now
1336 HEADER_INLINE uint32_t scan_int32(const char* ss, int32_t* valp) {
1337 return scan_int_abs_bounded(ss, 0x7fffffff, valp);
1338 }
1339
1340 // default cap = 0x7ffffffe
1341 HEADER_INLINE uint32_t scan_posint_defcap(const char* ss, uint32_t* valp) {
1342 return scan_posint_capped(ss, 0x7ffffffe, valp);
1343 }
1344
1345 HEADER_INLINE uint32_t scan_uint_defcap(const char* ss, uint32_t* valp) {
1346 return scan_uint_capped(ss, 0x7ffffffe, valp);
1347 }
1348
1349 HEADER_INLINE uint32_t scan_int_abs_defcap(const char* ss, int32_t* valp) {
1350 return scan_int_abs_bounded(ss, 0x7ffffffe, valp);
1351 }
1352
1353 HEADER_INLINE uint32_t scan_uint_icap(const char* ss, uint32_t* valp) {
1354 return scan_uint_capped(ss, 0x7fffffff, valp);
1355 }
1356
1357 uint32_t scan_posintptr(const char* ss, uintptr_t* valp);
1358
1359 HEADER_INLINE uint32_t scan_double(const char* ss, double* valp) {
1360 char* ss2;
1361 *valp = strtod(ss, &ss2);
1362 return (ss == ss2);
1363 }
1364
1365 HEADER_INLINE uint32_t scan_float(const char* ss, float* valp) {
1366 char* ss2;
1367 *valp = strtof(ss, &ss2);
1368 return (ss == ss2);
1369 }
1370
1371 // More restrictive parsing of command-line parameters.
1372 HEADER_INLINE uint32_t scan_doublex(const char* ss, double* valp) {
1373 char* ss2;
1374 *valp = strtod(ss, &ss2);
1375 return (ss == ss2) || (!is_space_or_eoln(ss2[0]));
1376 }
1377
1378 uint32_t scan_posint_cappedx(const char* ss, uint64_t cap, uint32_t* valp);
1379
1380 uint32_t scan_uint_cappedx(const char* ss, uint64_t cap, uint32_t* valp);
1381
1382 uint32_t scan_int_abs_boundedx(const char* ss, uint64_t bound, int32_t* valp);
1383
1384 HEADER_INLINE uint32_t scan_int32x(const char* ss, int32_t* valp) {
1385 return scan_int_abs_boundedx(ss, 0x7fffffff, valp);
1386 }
1387
1388 HEADER_INLINE uint32_t scan_posint_defcapx(const char* ss, uint32_t* valp) {
1389 return scan_posint_cappedx(ss, 0x7ffffffe, valp);
1390 }
1391
1392 HEADER_INLINE uint32_t scan_uint_defcapx(const char* ss, uint32_t* valp) {
1393 return scan_uint_cappedx(ss, 0x7ffffffe, valp);
1394 }
1395
1396 uint32_t scan_posintptrx(const char* ss, uintptr_t* valp);
1397
1398 // __restrict isn't very important for newer x86 processors since loads/stores
1399 // tend to be automatically reordered, but may as well use it properly in
1400 // plink_common.
1401 uint32_t scan_two_doubles(char* ss, double* __restrict val1p, double* __restrict val2p);
1402
1403 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);
1404
1405 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);
1406
1407 HEADER_INLINE char* memseta(char* target, unsigned char val, uintptr_t ct) {
1408 memset(target, val, ct);
1409 return &(target[ct]);
1410 }
1411
1412 HEADER_INLINE char* memcpya(char* __restrict target, const void* __restrict source, uintptr_t ct) {
1413 memcpy(target, source, ct);
1414 return &(target[ct]);
1415 }
1416
1417 HEADER_INLINE char* memcpyb(char* __restrict target, const void* __restrict source, uint32_t ct) {
1418 // Same as memcpya, except the return value is one byte earlier. Generally
1419 // used when source is a null-terminated string of known length and we want
1420 // to copy the null, but sometimes we need to append later.
1421 memcpy(target, source, ct);
1422 return &(target[ct - 1]);
1423 }
1424
1425 HEADER_INLINE char* memcpyax(char* __restrict target, const void* __restrict source, uint32_t ct, char extra_char) {
1426 memcpy(target, source, ct);
1427 target[ct] = extra_char;
1428 return &(target[ct + 1]);
1429 }
1430
1431 HEADER_INLINE void memcpyx(char* __restrict target, const void* __restrict source, uint32_t ct, char extra_char) {
1432 memcpy(target, source, ct);
1433 target[ct] = extra_char;
1434 }
1435
1436 HEADER_INLINE void memcpyl3(char* __restrict target, const void* __restrict source) {
1437 // when it's safe to clobber the fourth character, this is faster
1438 *((uint32_t*)target) = *((const uint32_t*)source);
1439 }
1440
1441 HEADER_INLINE char* memcpyl3a(char* __restrict target, const void* __restrict source) {
1442 memcpyl3(target, source);
1443 return &(target[3]);
1444 }
1445
1446 // note that, unlike stpcpy(), this does not copy the null terminator
1447 HEADER_INLINE char* strcpya(char* __restrict target, const void* __restrict source) {
1448 uintptr_t slen = strlen((char*)source);
1449 memcpy(target, source, slen);
1450 return &(target[slen]);
1451 }
1452
1453 HEADER_INLINE char* strcpyax(char* __restrict target, const void* __restrict source, char extra_char) {
1454 uintptr_t slen = strlen((char*)source);
1455 memcpy(target, source, slen);
1456 target[slen] = extra_char;
1457 return &(target[slen + 1]);
1458 }
1459
1460 HEADER_INLINE void append_binary_eoln(char** target_ptr) {
1461 #ifdef _WIN32
1462 (*target_ptr)[0] = '\r';
1463 (*target_ptr)[1] = '\n';
1464 *target_ptr += 2;
1465 #else
1466 **target_ptr = '\n';
1467 *target_ptr += 1;
1468 #endif
1469 }
1470
1471 HEADER_INLINE void fputs_w4(const char* ss, FILE* outfile) {
1472 // for efficient handling of width-4 allele columns; don't want to call
1473 // strlen() since that's redundant with fputs
1474 if (!ss[1]) {
1475 fputs(" ", outfile);
1476 putc(ss[0], outfile);
1477 } else {
1478 if (!ss[2]) {
1479 putc(' ', outfile);
1480 putc(' ', outfile);
1481 } else if (!ss[3]) {
1482 putc(' ', outfile);
1483 }
1484 fputs(ss, outfile);
1485 }
1486 }
1487
1488 int32_t gzputs_w4(gzFile gz_outfile, const char* ss);
1489
1490 int32_t get_next_noncomment(FILE* fptr, char** lptr_ptr, uintptr_t* line_idx_ptr);
1491
1492 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);
1493
1494 // assumes we are currently in a token -- UNSAFE OTHERWISE
1495 HEADER_INLINE char* token_endnn(char* sptr) {
1496 while (!is_space_or_eoln(*(++sptr)));
1497 return sptr;
1498 }
1499
1500 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);
1501
1502 uint32_t intlen(int32_t num);
1503
1504 // safer than token_endnn(), since it handles length zero
1505 // "se" = stops at space or eoln character
1506 HEADER_INLINE uintptr_t strlen_se(const char* ss) {
1507 const char* ss2 = ss;
1508 while (!is_space_or_eoln(*ss2)) {
1509 ss2++;
1510 }
1511 return (uintptr_t)(ss2 - ss);
1512 }
1513
1514 int32_t strcmp_se(const char* s_read, const char* s_const, uint32_t s_const_len);
1515
1516 char* next_token(char* sptr);
1517
1518 char* next_token_mult(char* sptr, uint32_t ct);
1519
1520 HEADER_INLINE char* next_token_multz(char* sptr, uint32_t ct) {
1521 // tried replacing this with ternary operator, but that actually seemed to
1522 // slow things down a bit under gcc 4.2.1 (tail call optimization issue?).
1523 // todo: recheck this under newer gcc/clang.
1524 if (ct) {
1525 return next_token_mult(sptr, ct);
1526 } else {
1527 return sptr;
1528 }
1529 }
1530
1531 uint32_t count_tokens(const char* bufptr);
1532
1533 HEADER_INLINE char* fw_strcpyn(uint32_t min_width, uint32_t source_len, const char* source, char* dest) {
1534 // right-justified strcpy with known source length
1535 if (source_len < min_width) {
1536 memcpy(memseta(dest, 32, min_width - source_len), source, source_len);
1537 return &(dest[min_width]);
1538 } else {
1539 return memcpya(dest, source, source_len);
1540 }
1541 }
1542
1543 HEADER_INLINE char* fw_strcpy(uint32_t min_width, const char* source, char* dest) {
1544 return fw_strcpyn(min_width, strlen(source), source, dest);
1545 }
1546
1547 uint32_t count_and_measure_multistr(const char* multistr, uintptr_t* max_slen_ptr);
1548
1549 char* uint32toa(uint32_t uii, char* start);
1550
1551 char* int32toa(int32_t ii, char* start);
1552
1553 // Write exactly four digits (padding with zeroes if necessary); useful for
1554 // e.g. floating point encoders. uii must not be >= 10^4.
1555 char* uitoa_z4(uint32_t uii, char* start);
1556
1557 char* int64toa(int64_t llii, char* start);
1558
1559 // Minimum field width 4 (padding with spaces on left).
1560 char* uint32toa_w4(uint32_t uii, char* start);
1561
1562 char* uint32toa_w6(uint32_t uii, char* start);
1563
1564 char* uint32toa_w7(uint32_t uii, char* start);
1565
1566 char* uint32toa_w8(uint32_t uii, char* start);
1567
1568 char* uint32toa_w10(uint32_t uii, char* start);
1569
1570 // These limited-precision converters are usually several times as fast as
1571 // grisu2's descendants; and let's not even speak of sprintf. (I'm guessing
1572 // that the algorithm cannot be made round-trip-safe without throwing away its
1573 // performance advantage, since we currently multiply by numbers like 1.0e256
1574 // which don't have an exact representation. But these functions are very,
1575 // very good at what they do.)
1576 char* dtoa_e(double dxx, char* start);
1577
1578 char* ftoa_e(float dxx, char* start);
1579
1580 char* dtoa_f_p2(double dxx, char* start);
1581
1582 char* dtoa_f_p3(double dxx, char* start);
1583
1584 char* dtoa_f_w9p6(double dxx, char* start);
1585
1586 char* dtoa_f_w7p4(double dxx, char* start);
1587
1588 HEADER_INLINE void trailing_zeroes_to_spaces(char* start) {
1589 // removes trailing zeroes
1590 start--;
1591 while (*start == '0') {
1592 *start-- = ' ';
1593 }
1594 if (*start == '.') {
1595 *start = ' ';
1596 }
1597 }
1598
1599 HEADER_INLINE char* clip_trailing_zeroes(char* start) {
1600 char cc;
1601 do {
1602 cc = *(--start);
1603 } while (cc == '0');
1604 return &(start[(cc != '.')]);
1605 }
1606
1607 char* dtoa_f_w9p6_spaced(double dxx, char* start);
1608
1609 char* dtoa_f_w9p6_clipped(double dxx, char* start);
1610
1611 char* dtoa_g(double dxx, char* start);
1612
1613 char* ftoa_g(float dxx, char* start);
1614
1615 HEADER_INLINE char* width_force(uint32_t min_width, char* startp, char* endp) {
1616 uintptr_t diff = (endp - startp);
1617 if (diff >= min_width) {
1618 return endp;
1619 } else {
1620 diff = min_width - diff;
1621 do {
1622 --endp;
1623 endp[diff] = *endp;
1624 } while (endp > startp);
1625 memset(startp, 32, diff);
1626 return &(startp[min_width]);
1627 }
1628 }
1629
1630 // assumes min_width >= 5.
1631 char* dtoa_g_wxp2(double dxx, uint32_t min_width, char* start);
1632
1633 // assumes min_width >= 5.
1634 char* dtoa_g_wxp3(double dxx, uint32_t min_width, char* start);
1635
1636 // only requires min_width to be positive; less than 5 is ok
1637 char* dtoa_g_wxp4(double dxx, uint32_t min_width, char* start);
1638
1639 // only requires min_width to be positive; less than 8 is ok
1640 char* dtoa_g_wxp8(double dxx, uint32_t min_width, char* start);
1641
1642 HEADER_INLINE char* uint32toa_x(uint32_t uii, char extra_char, char* start) {
1643 char* penult = uint32toa(uii, start);
1644 *penult = extra_char;
1645 return &(penult[1]);
1646 }
1647
1648 HEADER_INLINE char* int32toa_x(int32_t ii, char extra_char, char* start) {
1649 char* penult = int32toa(ii, start);
1650 *penult = extra_char;
1651 return &(penult[1]);
1652 }
1653
1654 HEADER_INLINE char* uint32toa_w4x(uint32_t uii, char extra_char, char* start) {
1655 char* penult = uint32toa_w4(uii, start);
1656 *penult = extra_char;
1657 return &(penult[1]);
1658 }
1659
1660 HEADER_INLINE char* uint32toa_w6x(uint32_t uii, char extra_char, char* start) {
1661 char* penult = uint32toa_w6(uii, start);
1662 *penult = extra_char;
1663 return &(penult[1]);
1664 }
1665
1666 HEADER_INLINE char* uint32toa_w7x(uint32_t uii, char extra_char, char* start) {
1667 char* penult = uint32toa_w7(uii, start);
1668 *penult = extra_char;
1669 return &(penult[1]);
1670 }
1671
1672 HEADER_INLINE char* uint32toa_w8x(uint32_t uii, char extra_char, char* start) {
1673 char* penult = uint32toa_w8(uii, start);
1674 *penult = extra_char;
1675 return &(penult[1]);
1676 }
1677
1678 HEADER_INLINE char* uint32toa_w10x(uint32_t uii, char extra_char, char* start) {
1679 char* penult = uint32toa_w10(uii, start);
1680 *penult = extra_char;
1681 return &(penult[1]);
1682 }
1683
1684 HEADER_INLINE char* dtoa_ex(double dxx, char extra_char, char* start) {
1685 char* penult = dtoa_e(dxx, start);
1686 *penult = extra_char;
1687 return &(penult[1]);
1688 }
1689
1690 HEADER_INLINE char* ftoa_ex(float fxx, char extra_char, char* start) {
1691 char* penult = ftoa_e(fxx, start);
1692 *penult = extra_char;
1693 return &(penult[1]);
1694 }
1695
1696 HEADER_INLINE char* dtoa_f_w9p6x(double dxx, char extra_char, char* start) {
1697 char* penult = dtoa_f_w9p6(dxx, start);
1698 *penult = extra_char;
1699 return &(penult[1]);
1700 }
1701
1702 HEADER_INLINE char* dtoa_f_w7p4x(double dxx, char extra_char, char* start) {
1703 char* penult = dtoa_f_w7p4(dxx, start);
1704 *penult = extra_char;
1705 return &(penult[1]);
1706 }
1707
1708 HEADER_INLINE char* dtoa_gx(double dxx, char extra_char, char* start) {
1709 char* penult = dtoa_g(dxx, start);
1710 *penult = extra_char;
1711 return &(penult[1]);
1712 }
1713
1714 /*
1715 HEADER_INLINE char* ftoa_gx(float dxx, char extra_char, char* start) {
1716 char* penult = ftoa_g(dxx, start);
1717 *penult = extra_char;
1718 return &(penult[1]);
1719 }
1720 */
1721
1722 HEADER_INLINE char* dtoa_g_wxp3x(double dxx, uint32_t min_width, char extra_char, char* start) {
1723 char* penult = dtoa_g_wxp3(dxx, min_width, start);
1724 *penult = extra_char;
1725 return &(penult[1]);
1726 }
1727
1728 HEADER_INLINE char* dtoa_g_wxp4x(double dxx, uint32_t min_width, char extra_char, char* start) {
1729 char* penult = dtoa_g_wxp4(dxx, min_width, start);
1730 *penult = extra_char;
1731 return &(penult[1]);
1732 }
1733
1734 HEADER_INLINE char* dtoa_g_wxp8x(double dxx, uint32_t min_width, char extra_char, char* start) {
1735 char* penult = dtoa_g_wxp8(dxx, min_width, start);
1736 *penult = extra_char;
1737 return &(penult[1]);
1738 }
1739
1740 char* chrom_print_human(uint32_t num, char* buf);
1741
1742 void magic_num(uint32_t divisor, uint64_t* multp, uint32_t* __restrict pre_shiftp, uint32_t* __restrict post_shiftp, uint32_t* __restrict incrp);
1743
1744 HEADER_INLINE uintptr_t tri_coord_no_diag(uintptr_t small_coord, uintptr_t big_coord) {
1745 // small_coord and big_coord are 0-based indices, small_coord < big_coord
1746 return ((big_coord * (big_coord - 1)) / 2) + small_coord;
1747 }
1748
1749 HEADER_INLINE uint32_t tri_coord_no_diag_32(uint32_t small_coord, uint32_t big_coord) {
1750 return ((big_coord * (big_coord - 1)) / 2) + small_coord;
1751 }
1752
1753 // let the compiler worry about the second argument's bit width here
1754 #define SET_BIT(idx, arr) ((arr)[(idx) / BITCT] |= ONELU << ((idx) % BITCT))
1755
1756 #define SET_BIT_DBL(idx, arr) ((arr)[(idx) / BITCT2] |= ONELU << (2 * ((idx) % BITCT2)))
1757
1758 // useful for coercing int32_t loc to unsigned
1759 HEADER_INLINE void set_bit(uint32_t loc, uintptr_t* bitarr) {
1760 bitarr[loc / BITCT] |= (ONELU << (loc % BITCT));
1761 }
1762
1763 HEADER_INLINE void set_bit_ul(uintptr_t loc, uintptr_t* bitarr) {
1764 bitarr[loc / BITCT] |= (ONELU << (loc % BITCT));
1765 }
1766
1767 // requires positive len
1768 void fill_bits(uintptr_t loc_start, uintptr_t len, uintptr_t* bitarr);
1769
1770 // requires positive len
1771 void clear_bits(uintptr_t loc_start, uintptr_t len, uintptr_t* bitarr);
1772
1773 #define CLEAR_BIT(idx, arr) ((arr)[(idx) / BITCT] &= ~(ONELU << ((idx) % BITCT)))
1774
1775 #define CLEAR_BIT_DBL(idx, arr) ((arr)[(idx) / BITCT2] &= ~(ONELU << (2 * ((idx) % BITCT2))))
1776
1777 HEADER_INLINE void clear_bit(uint32_t loc, uintptr_t* bitarr) {
1778 bitarr[loc / BITCT] &= ~(ONELU << (loc % BITCT));
1779 }
1780
1781 HEADER_INLINE void clear_bit_ul(uintptr_t loc, uintptr_t* bitarr) {
1782 bitarr[loc / BITCT] &= ~(ONELU << (loc % BITCT));
1783 }
1784
1785 #define IS_SET(arr, idx) (((arr)[(idx) / BITCT] >> ((idx) % BITCT)) & 1)
1786
1787 #define IS_SET_DBL(arr, idx) (((arr)[(idx) / BITCT2] >> (2 * ((idx) % BITCT2))) & 1)
1788
1789 // use this instead of IS_SET() for signed 32-bit integers
1790 HEADER_INLINE uint32_t is_set(const uintptr_t* bitarr, uint32_t loc) {
1791 return (bitarr[loc / BITCT] >> (loc % BITCT)) & 1;
1792 }
1793
1794 HEADER_INLINE uint32_t is_set_ul(const uintptr_t* bitarr, uintptr_t loc) {
1795 return (bitarr[loc / BITCT] >> (loc % BITCT)) & 1;
1796 }
1797
1798 #define IS_NONNULL_AND_SET(arr, idx) ((arr) && IS_SET(arr, idx))
1799
1800 uint32_t next_unset_unsafe(const uintptr_t* bitarr, uint32_t loc);
1801
1802 HEADER_INLINE void next_unset_unsafe_ck(const uintptr_t* __restrict bitarr, uint32_t* __restrict loc_ptr) {
1803 if (IS_SET(bitarr, *loc_ptr)) {
1804 *loc_ptr = next_unset_unsafe(bitarr, *loc_ptr);
1805 }
1806 }
1807
1808 #ifdef __LP64__
1809 uintptr_t next_unset_ul_unsafe(const uintptr_t* bitarr, uintptr_t loc);
1810 #else
1811 HEADER_INLINE uintptr_t next_unset_ul_unsafe(const uintptr_t* bitarr, uintptr_t loc) {
1812 return (uintptr_t)next_unset_unsafe(bitarr, loc);
1813 }
1814 #endif
1815
1816 HEADER_INLINE void next_unset_ul_unsafe_ck(const uintptr_t* __restrict bitarr, uintptr_t* __restrict loc_ptr) {
1817 if (IS_SET(bitarr, *loc_ptr)) {
1818 *loc_ptr = next_unset_ul_unsafe(bitarr, *loc_ptr);
1819 }
1820 }
1821
1822 uint32_t next_unset(const uintptr_t* bitarr, uint32_t loc, uint32_t ceil);
1823
1824 HEADER_INLINE void next_unset_ck(const uintptr_t* __restrict bitarr, uint32_t ceil, uint32_t* __restrict loc_ptr) {
1825 if (IS_SET(bitarr, *loc_ptr)) {
1826 *loc_ptr = next_unset(bitarr, *loc_ptr, ceil);
1827 }
1828 }
1829
1830 #ifdef __LP64__
1831 uintptr_t next_unset_ul(const uintptr_t* bitarr, uintptr_t loc, uintptr_t ceil);
1832 #else
1833 HEADER_INLINE uintptr_t next_unset_ul(const uintptr_t* bitarr, uintptr_t loc, uintptr_t ceil) {
1834 return (uintptr_t)next_unset(bitarr, loc, ceil);
1835 }
1836 #endif
1837
1838 HEADER_INLINE void next_unset_ul_ck(const uintptr_t* __restrict bitarr, uintptr_t ceil, uintptr_t* __restrict loc_ptr) {
1839 if (IS_SET(bitarr, *loc_ptr)) {
1840 *loc_ptr = next_unset_ul(bitarr, *loc_ptr, ceil);
1841 }
1842 }
1843
1844 uint32_t next_set_unsafe(const uintptr_t* bitarr, uint32_t loc);
1845
1846 HEADER_INLINE void next_set_unsafe_ck(const uintptr_t* __restrict bitarr, uint32_t* __restrict loc_ptr) {
1847 if (!IS_SET(bitarr, *loc_ptr)) {
1848 *loc_ptr = next_set_unsafe(bitarr, *loc_ptr);
1849 }
1850 }
1851
1852 #ifdef __LP64__
1853 uintptr_t next_set_ul_unsafe(const uintptr_t* bitarr, uintptr_t loc);
1854 #else
1855 HEADER_INLINE uintptr_t next_set_ul_unsafe(const uintptr_t* bitarr, uintptr_t loc) {
1856 return (uintptr_t)next_set_unsafe(bitarr, loc);
1857 }
1858 #endif
1859
1860 HEADER_INLINE void next_set_ul_unsafe_ck(const uintptr_t* __restrict bitarr, uintptr_t* __restrict loc_ptr) {
1861 if (!IS_SET(bitarr, *loc_ptr)) {
1862 *loc_ptr = next_set_ul_unsafe(bitarr, *loc_ptr);
1863 }
1864 }
1865
1866 uint32_t next_set(const uintptr_t* bitarr, uint32_t loc, uint32_t ceil);
1867
1868 HEADER_INLINE void next_set_ck(const uintptr_t* __restrict bitarr, uint32_t ceil, uint32_t* __restrict loc_ptr) {
1869 if (!IS_SET(bitarr, *loc_ptr)) {
1870 *loc_ptr = next_set(bitarr, *loc_ptr, ceil);
1871 }
1872 }
1873
1874 #ifdef __LP64__
1875 uintptr_t next_set_ul(const uintptr_t* bitarr, uintptr_t loc, uintptr_t ceil);
1876 #else
1877 HEADER_INLINE uintptr_t next_set_ul(const uintptr_t* bitarr, uintptr_t loc, uintptr_t ceil) {
1878 return (uintptr_t)next_set(bitarr, loc, ceil);
1879 }
1880 #endif
1881
1882 HEADER_INLINE void next_set_ul_ck(const uintptr_t* __restrict bitarr, uintptr_t ceil, uintptr_t* loc_ptr) {
1883 if (!IS_SET(bitarr, *loc_ptr)) {
1884 *loc_ptr = next_set_ul(bitarr, *loc_ptr, ceil);
1885 }
1886 }
1887
1888 int32_t last_set_bit(const uintptr_t* bitarr, uint32_t word_ct);
1889
1890 // note different interface from last_set_bit()
1891 // int32_t last_clear_bit(uintptr_t* bitarr, uint32_t ceil);
1892
1893 // unlike the next_[un]set family, this always returns a STRICTLY earlier
1894 // position
1895 uint32_t prev_unset_unsafe(const uintptr_t* bitarr, uint32_t loc);
1896
1897 // uint32_t prev_unset(uintptr_t* bitarr, uint32_t loc, uint32_t floor);
1898
1899 HEADER_INLINE void prev_unset_unsafe_ck(const uintptr_t* bitarr, uint32_t* loc_ptr) {
1900 *loc_ptr -= 1;
1901 if (IS_SET(bitarr, *loc_ptr)) {
1902 *loc_ptr = prev_unset_unsafe(bitarr, *loc_ptr);
1903 }
1904 }
1905
1906 // These functions seem to optimize better than memset(arr, 0, x) under OS X
1907 // <10.9's gcc, and they should be equivalent for later versions (looks like
1908 // memcpy/memset were redone in gcc 4.3).
1909 HEADER_INLINE void fill_ulong_zero(size_t size, uintptr_t* ularr) {
1910 size_t ulii;
1911 for (ulii = 0; ulii < size; ulii++) {
1912 *ularr++ = 0;
1913 }
1914 }
1915
1916 #ifdef __LP64__
1917 HEADER_INLINE void fill_ull_zero(size_t size, uint64_t* ullarr) {
1918 fill_ulong_zero(size, (uintptr_t*)ullarr);
1919 }
1920
1921 // double v indicates that size is a vector count, not a word count.
1922 HEADER_INLINE void fill_vvec_zero(size_t size, VECITYPE* vvec) {
1923 size_t ulii;
1924 for (ulii = 0; ulii < size; ulii++) {
1925 *vvec++ = _mm_setzero_si128();
1926 }
1927 }
1928 #else
1929 HEADER_INLINE void fill_ull_zero(size_t size, uint64_t* ullarr) {
1930 fill_ulong_zero(size * 2, (uintptr_t*)ullarr);
1931 }
1932 #endif
1933
1934 HEADER_INLINE void fill_ulong_one(size_t size, uintptr_t* ularr) {
1935 size_t ulii;
1936 for (ulii = 0; ulii < size; ulii++) {
1937 *ularr++ = ~ZEROLU;
1938 }
1939 }
1940
1941 #ifdef __LP64__
1942 HEADER_INLINE void fill_ull_one(size_t size, uint64_t* ullarr) {
1943 fill_ulong_one(size, (uintptr_t*)ullarr);
1944 }
1945 #else
1946 HEADER_INLINE void fill_ull_one(size_t size, uint64_t* ullarr) {
1947 fill_ulong_one(size * 2, (uintptr_t*)ullarr);
1948 }
1949 #endif
1950
1951 HEADER_INLINE void fill_int_zero(size_t size, int32_t* iarr) {
1952 size_t ulii;
1953 for (ulii = 0; ulii < size; ulii++) {
1954 *iarr++ = 0;
1955 }
1956 }
1957
1958 HEADER_INLINE void fill_int_one(size_t size, int32_t* iarr) {
1959 size_t ulii;
1960 for (ulii = 0; ulii < size; ulii++) {
1961 *iarr++ = -1;
1962 }
1963 }
1964
1965 HEADER_INLINE void fill_uint_zero(size_t size, uint32_t* uiarr) {
1966 size_t ulii;
1967 for (ulii = 0; ulii < size; ulii++) {
1968 *uiarr++ = 0;
1969 }
1970 }
1971
1972 HEADER_INLINE void fill_uint_one(size_t size, uint32_t* uiarr) {
1973 size_t ulii;
1974 for (ulii = 0; ulii < size; ulii++) {
1975 *uiarr++ = ~0U;
1976 }
1977 }
1978
1979 HEADER_INLINE void fill_float_zero(size_t size, float* farr) {
1980 size_t ulii;
1981 for (ulii = 0; ulii < size; ulii++) {
1982 *farr++ = 0.0;
1983 }
1984 }
1985
1986 HEADER_INLINE void fill_double_zero(size_t size, double* darr) {
1987 size_t ulii;
1988 for (ulii = 0; ulii < size; ulii++) {
1989 *darr++ = 0.0;
1990 }
1991 }
1992
1993
1994 int32_t bigstack_calloc_uc(uintptr_t ct, unsigned char** ucp_ptr);
1995
1996 int32_t bigstack_calloc_d(uintptr_t ct, double** dp_ptr);
1997
1998 int32_t bigstack_calloc_f(uintptr_t ct, float** fp_ptr);
1999
2000 int32_t bigstack_calloc_ui(uintptr_t ct, uint32_t** uip_ptr);
2001
2002 int32_t bigstack_calloc_ul(uintptr_t ct, uintptr_t** ulp_ptr);
2003
2004 int32_t bigstack_calloc_ull(uintptr_t ct, uint64_t** ullp_ptr);
2005
2006 HEADER_INLINE int32_t bigstack_calloc_c(uintptr_t ct, char** cp_ptr) {
2007 return bigstack_calloc_uc(ct, (unsigned char**)cp_ptr);
2008 }
2009
2010 HEADER_INLINE int32_t bigstack_calloc_i(uintptr_t ct, int32_t** ip_ptr) {
2011 return bigstack_calloc_ui(ct, (uint32_t**)ip_ptr);
2012 }
2013
2014 HEADER_INLINE int32_t bigstack_calloc_ll(uintptr_t ct, int64_t** llp_ptr) {
2015 return bigstack_calloc_ull(ct, (uint64_t**)llp_ptr);
2016 }
2017
2018 int32_t bigstack_end_calloc_uc(uintptr_t ct, unsigned char** ucp_ptr);
2019
2020 int32_t bigstack_end_calloc_d(uintptr_t ct, double** dp_ptr);
2021
2022 int32_t bigstack_end_calloc_f(uintptr_t ct, float** fp_ptr);
2023
2024 int32_t bigstack_end_calloc_ui(uintptr_t ct, uint32_t** uip_ptr);
2025
2026 int32_t bigstack_end_calloc_ul(uintptr_t ct, uintptr_t** ulp_ptr);
2027
2028 int32_t bigstack_end_calloc_ull(uintptr_t ct, uint64_t** ullp_ptr);
2029
2030 HEADER_INLINE int32_t bigstack_end_calloc_c(uintptr_t ct, char** cp_ptr) {
2031 return bigstack_end_calloc_uc(ct, (unsigned char**)cp_ptr);
2032 }
2033
2034 HEADER_INLINE int32_t bigstack_end_calloc_i(uintptr_t ct, int32_t** ip_ptr) {
2035 return bigstack_end_calloc_ui(ct, (uint32_t**)ip_ptr);
2036 }
2037
2038 HEADER_INLINE int32_t bigstack_end_calloc_ll(uintptr_t ct, int64_t** llp_ptr) {
2039 return bigstack_end_calloc_ull(ct, (uint64_t**)llp_ptr);
2040 }
2041
2042
2043 uint32_t murmurhash3_32(const void* key, uint32_t len);
2044
2045 HEADER_INLINE uint32_t hashval2(const char* idstr, uint32_t idlen) {
2046 return murmurhash3_32(idstr, idlen) % HASHSIZE;
2047 }
2048
2049 uintptr_t geqprime(uintptr_t floor);
2050
2051 HEADER_INLINE uint32_t get_id_htable_size(uintptr_t item_ct) {
2052 if (item_ct < 32761) {
2053 return 65521;
2054 } else {
2055 return geqprime(item_ct * 2 + 1);
2056 }
2057 }
2058
2059 int32_t populate_id_htable(uintptr_t unfiltered_ct, const uintptr_t* exclude_arr, uintptr_t item_ct, const char* item_ids, uintptr_t max_id_len, uint32_t store_dups, uint32_t id_htable_size, uint32_t* id_htable);
2060
2061 HEADER_INLINE int32_t alloc_and_populate_id_htable(uintptr_t unfiltered_ct, const uintptr_t* exclude_arr, uintptr_t item_ct, const char* item_ids, uintptr_t max_id_len, uint32_t allow_dups, uint32_t* id_htable_size_ptr, uint32_t** id_htable_ptr) {
2062 uint32_t id_htable_size = get_id_htable_size(item_ct);
2063 if (bigstack_alloc_ui(id_htable_size, id_htable_ptr)) {
2064 return RET_NOMEM;
2065 }
2066 *id_htable_size_ptr = id_htable_size;
2067 return populate_id_htable(unfiltered_ct, exclude_arr, item_ct, item_ids, max_id_len, allow_dups, id_htable_size, *id_htable_ptr);
2068 }
2069
2070 uint32_t id_htable_find(const char* id_buf, uintptr_t cur_id_len, const uint32_t* id_htable, uint32_t id_htable_size, const char* item_ids, uintptr_t max_id_len);
2071
2072 void fill_idx_to_uidx(const uintptr_t* exclude_arr, uintptr_t unfiltered_item_ct, uintptr_t item_ct, uint32_t* idx_to_uidx);
2073
2074 void fill_idx_to_uidx_incl(const uintptr_t* include_arr, uintptr_t unfiltered_item_ct, uintptr_t item_ct, uint32_t* idx_to_uidx);
2075
2076 void fill_uidx_to_idx(const uintptr_t* exclude_arr, uint32_t unfiltered_item_ct, uint32_t item_ct, uint32_t* uidx_to_idx);
2077
2078 void fill_uidx_to_idx_incl(const uintptr_t* include_arr, uint32_t unfiltered_item_ct, uint32_t item_ct, uint32_t* uidx_to_idx);
2079
2080 void fill_midx_to_idx(const uintptr_t* exclude_arr_orig, const uintptr_t* exclude_arr, uint32_t item_ct, uint32_t* midx_to_idx);
2081
2082
2083 // "quaterarr" refers to a packed group of base-4 (2-bit) elements, analogous
2084 // to "bitarr". (Based on "quaternary", not "quarter".) "quatervec"
2085 // indicates that vector-alignment is also required.
2086 void fill_quatervec_55(uint32_t ct, uintptr_t* quatervec);
2087
2088 // Used to unpack e.g. unfiltered sex_male to a filtered quaterarr usable as a
2089 // raw input bitmask.
2090 // Assumes output_quaterarr is sized to a multiple of 16 bytes.
2091 void quaterarr_collapse_init(const uintptr_t* __restrict unfiltered_bitarr, uint32_t unfiltered_ct, const uintptr_t* __restrict filter_bitarr, uint32_t filtered_ct, uintptr_t* __restrict output_quaterarr);
2092
2093 void quaterarr_collapse_init_exclude(const uintptr_t* __restrict unfiltered_bitarr, uint32_t unfiltered_ct, const uintptr_t* __restrict filter_exclude_bitarr, uint32_t filtered_ct, uintptr_t* __restrict output_quaterarr);
2094
2095 uint32_t alloc_collapsed_haploid_filters(const uintptr_t* __restrict sample_bitarr, const uintptr_t* __restrict sex_male, uint32_t unfiltered_sample_ct, uint32_t sample_ct, uint32_t hh_exists, uint32_t is_include, uintptr_t** sample_include_quatervec_ptr, uintptr_t** sample_male_include_quatervec_ptr);
2096
2097 HEADER_INLINE void free_cond(void* memptr) {
2098 if (memptr) {
2099 free(memptr);
2100 }
2101 }
2102
2103 HEADER_INLINE uint32_t realnum(double dd) {
2104 return (dd == dd) && (dd != INFINITY) && (dd != -INFINITY);
2105 }
2106
2107 HEADER_INLINE double get_maf(double allele_freq) {
2108 return (allele_freq <= 0.5)? allele_freq : (1.0 - allele_freq);
2109 }
2110
2111 HEADER_INLINE int32_t filename_exists(const char* __restrict fname_append, char* fname, char* fname_end) {
2112 #ifdef _WIN32
2113 DWORD file_attr;
2114 strcpy(fname_end, fname_append);
2115 file_attr = GetFileAttributes(fname);
2116 return (file_attr != 0xffffffffU);
2117 #else
2118 struct stat st;
2119 strcpy(fname_end, fname_append);
2120 return (stat(fname, &st) == 0);
2121 #endif
2122 }
2123
2124 void sample_delim_convert(uintptr_t unfiltered_sample_ct, const uintptr_t* sample_exclude, uint32_t sample_ct, uintptr_t max_sample_id_len, char oldc, char newc, char* sample_ids);
2125
2126 void get_set_wrange_align(const uintptr_t* __restrict bitarr, uintptr_t word_ct, uintptr_t* __restrict firstw_ptr, uintptr_t* __restrict wlen_ptr);
2127
2128 // for hash tables where maximum ID string length is not known in advance.
2129 uint32_t unklen_id_htable_find(const char* cur_id, const char* const* item_ids, const uint32_t* id_htable, uint32_t hashval, uint32_t id_htable_size);
2130
2131 // okay, time to provide O(c log c)-time instead of O(c^2)-time initialization
2132 // (c = # of chromosomes/contigs).
2133 #define MAX_POSSIBLE_CHROM 65280
2134
2135 // get_id_htable_size(MAX_POSSIBLE_CHROM) (use constexpr once sufficient
2136 // compiler support is available)
2137 #define CHROM_NAME_HTABLE_SIZE 130579
2138
2139 // assumes MAX_POSSIBLE_CHROM is a multiple of 64, otherwise add round-up
2140 #define CHROM_MASK_WORDS (MAX_POSSIBLE_CHROM / BITCT)
2141
2142 // (note that n+1, n+2, n+3, and n+4 are reserved for X/Y/XY/MT)
2143 #define MAX_CHROM_TEXTNUM 95
2144
2145 #define MAX_CHROM_TEXTNUM_SLEN 2
2146
2147 #define X_OFFSET 0
2148 #define Y_OFFSET 1
2149 #define XY_OFFSET 2
2150 #define MT_OFFSET 3
2151 #define XYMT_OFFSET_CT 4
2152
2153 #define CHROM_X (MAX_POSSIBLE_CHROM + X_OFFSET)
2154 #define CHROM_Y (MAX_POSSIBLE_CHROM + Y_OFFSET)
2155 #define CHROM_XY (MAX_POSSIBLE_CHROM + XY_OFFSET)
2156 #define CHROM_MT (MAX_POSSIBLE_CHROM + MT_OFFSET)
2157
2158 #ifdef __LP64__
2159 // dog requires 42 bits, and other species require less
2160 #define CHROM_MASK_INITIAL_WORDS 1
2161 #else
2162 #define CHROM_MASK_INITIAL_WORDS 2
2163 #endif
2164
2165 typedef struct {
2166 // Main dynamic block intended to be allocated as a single aligned block of
2167 // memory on the heap freeable with vecaligned_free(), with chrom_mask at the
2168 // base.
2169
2170 uintptr_t* chrom_mask; // which chromosomes aren't known to be absent?
2171 // This is a misnomer--it includes X and excludes MT. Underlying concept is
2172 // "are some calls guaranteed to be homozygous (assuming >= 1 male)", which
2173 // is no longer true for MT since heteroplasmy is a thing. (Well, the real
2174 // goal with MT is to enable dosage-based analysis, but until all pipelines
2175 // have adapted, diploid data handling loses slightly less information than
2176 // haploid.)
2177 uintptr_t* haploid_mask;
2178
2179 // order of chromosomes in input files
2180 // currently tolerates out-of-order chromosomes, as long as all variants for
2181 // any given chromosome are together
2182 uint32_t* chrom_file_order;
2183
2184 // if the second chromosome in the dataset is chr5, chrom_file_order[1] == 5,
2185 // the raw variant indexes for chr5 are in [chrom_fo_vidx_start[1],
2186 // chrom_fo_vidx_start[2]). and chrom_idx_to_foidx[5] == 1.
2187 uint32_t* chrom_fo_vidx_start;
2188 uint32_t* chrom_idx_to_foidx;
2189
2190 // --allow-extra-chr support
2191 char** nonstd_names;
2192 uint32_t* nonstd_id_htable;
2193 // end main dynamic block
2194
2195 uint32_t chrom_ct; // number of distinct chromosomes/contigs
2196 uint32_t species;
2197
2198 int32_t xymt_codes[XYMT_OFFSET_CT]; // x, y, xy, mt
2199 uint32_t max_code;
2200
2201 uint32_t autosome_ct;
2202
2203 // yet more --allow-extra-chr support
2204 uint32_t zero_extra_chroms;
2205 uint32_t name_ct;
2206 Ll_str* incl_excl_name_stack;
2207 uint32_t is_include_stack;
2208 uint32_t output_encoding;
2209 } Chrom_info;
2210
2211 extern const char* g_species_singular;
2212 extern const char* g_species_plural;
2213
2214 int32_t init_chrom_info(Chrom_info* chrom_info_ptr);
2215
2216 void init_species(uint32_t species_code, Chrom_info* chrom_info_ptr);
2217
2218 void init_default_chrom_mask(Chrom_info* chrom_info_ptr);
2219
2220 HEADER_INLINE int32_t init_chrom_info_human(Chrom_info* chrom_info_ptr) {
2221 // convenience wrapper
2222 if (init_chrom_info(chrom_info_ptr)) {
2223 return RET_NOMEM;
2224 }
2225 init_species(SPECIES_HUMAN, chrom_info_ptr);
2226 init_default_chrom_mask(chrom_info_ptr);
2227 return 0;
2228 }
2229
2230 void forget_extra_chrom_names(uint32_t reinitialize, Chrom_info* chrom_info_ptr);
2231
2232 // in the usual case where the number of chromosomes/contigs is much less than
2233 // MAX_POSSIBLE_CHROM, this reduces chrom_info's memory consumption and
2234 // improves locality.
2235 int32_t finalize_chrom_info(Chrom_info* chrom_info_ptr);
2236
2237 void cleanup_chrom_info(Chrom_info* chrom_info_ptr);
2238
2239 HEADER_INLINE const char* species_str(uintptr_t ct) {
2240 return (ct == ONELU)? g_species_singular : g_species_plural;
2241 }
2242
2243 #define CHR_OUTPUT_PREFIX 1
2244 #define CHR_OUTPUT_M 2
2245 #define CHR_OUTPUT_MT 4
2246 #define CHR_OUTPUT_0M 8
2247
2248 HEADER_INLINE uint32_t are_all_words_zero(const uintptr_t* word_arr, uintptr_t word_ct) {
2249 while (word_ct--) {
2250 if (*word_arr++) {
2251 return 0;
2252 }
2253 }
2254 return 1;
2255 }
2256
2257 char* chrom_name_write(const Chrom_info* chrom_info_ptr, uint32_t chrom_idx, char* buf);
2258
2259 char* chrom_name_buf5w4write(const Chrom_info* chrom_info_ptr, uint32_t chrom_idx, uint32_t* chrom_name_len_ptr, char* buf5);
2260
2261 uint32_t get_max_chrom_slen(const Chrom_info* chrom_info_ptr);
2262
2263 uint32_t haploid_chrom_present(const Chrom_info* chrom_info_ptr);
2264
2265 // does not require null-termination
2266 // only handles 1-99, X, Y, XY, MT, and "chr" prefix
2267 int32_t get_chrom_code_raw(const char* sptr);
2268
2269 // now requires null-termination
2270 // now returns -1 when --allow-extra-chr may be ok, and -2 on total fail
2271 int32_t get_chrom_code(const char* chrom_name, const Chrom_info* chrom_info_ptr, uint32_t name_slen);
2272
2273 // when the chromosome name isn't null-terminated, but we want to preserve the
2274 // character there
2275 // requires chrom_name[name_slen] to be mutable
2276 int32_t get_chrom_code_counted(const Chrom_info* chrom_info_ptr, uint32_t name_slen, char* chrom_name);
2277
2278 // when it's okay to just replace the terminating space/tab with a \0
2279 HEADER_INLINE int32_t get_chrom_code_destructive(const Chrom_info* chrom_info_ptr, char* chrom_name) {
2280 char* chrom_token_end = token_endnn(chrom_name);
2281 *chrom_token_end = '\0';
2282 return get_chrom_code(chrom_name, chrom_info_ptr, (uintptr_t)(chrom_token_end - chrom_name));
2283 }
2284
2285 uint32_t get_variant_chrom_fo_idx(const Chrom_info* chrom_info_ptr, uintptr_t variant_uidx);
2286
2287 HEADER_INLINE uint32_t get_variant_chrom(const Chrom_info* chrom_info_ptr, uintptr_t variant_uidx) {
2288 return chrom_info_ptr->chrom_file_order[get_variant_chrom_fo_idx(chrom_info_ptr, variant_uidx)];
2289 }
2290
2291
2292 // these assume the chromosome is present in the dataset
2293 HEADER_INLINE uint32_t get_chrom_start_vidx(const Chrom_info* chrom_info_ptr, uint32_t chrom_idx) {
2294 return chrom_info_ptr->chrom_fo_vidx_start[chrom_info_ptr->chrom_idx_to_foidx[chrom_idx]];
2295 }
2296
2297 HEADER_INLINE uint32_t get_chrom_end_vidx(const Chrom_info* chrom_info_ptr, uint32_t chrom_idx) {
2298 return chrom_info_ptr->chrom_fo_vidx_start[chrom_info_ptr->chrom_idx_to_foidx[chrom_idx] + 1];
2299 }
2300
2301 // now assumes chrom_name is null-terminated
2302 int32_t try_to_add_chrom_name(const char* chrom_name, const char* file_descrip, uintptr_t line_idx, uint32_t name_slen, uint32_t allow_extra_chroms, int32_t* chrom_idx_ptr, Chrom_info* chrom_info_ptr);
2303
2304 HEADER_INLINE int32_t get_or_add_chrom_code(const char* chrom_name, const char* file_descrip, uintptr_t line_idx, uint32_t name_slen, uint32_t allow_extra_chroms, Chrom_info* chrom_info_ptr, int32_t* chrom_idx_ptr) {
2305 *chrom_idx_ptr = get_chrom_code(chrom_name, chrom_info_ptr, name_slen);
2306 if (*chrom_idx_ptr >= 0) {
2307 return 0;
2308 }
2309 return try_to_add_chrom_name(chrom_name, file_descrip, line_idx, name_slen, allow_extra_chroms, chrom_idx_ptr, chrom_info_ptr);
2310 }
2311
2312 HEADER_INLINE int32_t get_or_add_chrom_code_destructive(const char* file_descrip, uintptr_t line_idx, uint32_t allow_extra_chroms, char* chrom_name, char* chrom_name_end, Chrom_info* chrom_info_ptr, int32_t* chrom_idx_ptr) {
2313 *chrom_name_end = '\0';
2314 return get_or_add_chrom_code(chrom_name, file_descrip, line_idx, (uintptr_t)(chrom_name_end - chrom_name), allow_extra_chroms, chrom_info_ptr, chrom_idx_ptr);
2315 }
2316
2317 // newval does not need to be null-terminated
2318 // assumes *allele_ptr is not initialized
2319 // make last parameter const char** later
2320 uint32_t allele_set(const char* newval, uint32_t allele_slen, char** allele_ptr);
2321
2322 // *allele_ptr must be initialized; frees *allele_ptr if necessary
2323 uint32_t allele_reset(const char* newval, uint32_t allele_slen, char** allele_ptr);
2324
2325 void cleanup_allele_storage(uint32_t max_allele_slen, uintptr_t allele_storage_entry_ct, char** allele_storage);
2326
2327 // needed by fixed --merge-equal-pos implementation, which takes more liberties
2328 // with allele_storage[]
2329 void cleanup_allele_storage2(uintptr_t allele_storage_entry_ct, char** allele_storage);
2330
2331 // no need for this; code is simpler if we just create a copy of marker_exclude
2332 // with all non-autosomal loci removed
2333 /*
2334 HEADER_INLINE uintptr_t next_autosomal_unsafe(uintptr_t* marker_exclude, uintptr_t marker_uidx, Chrom_info* chrom_info_ptr, uint32_t* chrom_end_ptr, uint32_t* chrom_fo_idx_ptr) {
2335 // assumes we are at an autosomal marker if marker_uidx < *chrom_end_ptr
2336 next_unset_ul_unsafe_ck(marker_exclude, &marker_uidx);
2337 if (marker_uidx < (*chrom_end_ptr)) {
2338 return marker_uidx;
2339 }
2340 uintptr_t* haploid_mask = chrom_info_ptr->haploid_mask;
2341 uint32_t chrom_idx;
2342 while (1) {
2343 do {
2344 *chrom_fo_idx_ptr += 1;
2345 *chrom_end_ptr = chrom_info_ptr->chrom_file_order_marker_idx[(*chrom_fo_idx_ptr) + 1];
2346 } while (marker_uidx >= (*chrom_end_ptr));
2347 chrom_idx = chrom_info_ptr->chrom_file_order[*chrom_fo_idx_ptr];
2348 if (!IS_SET(haploid_mask, chrom_idx)) {
2349 return marker_uidx;
2350 }
2351 marker_uidx = next_unset_ul_unsafe(marker_exclude, *chrom_end_ptr);
2352 }
2353 }
2354 */
2355
2356 void refresh_chrom_info(const Chrom_info* chrom_info_ptr, uintptr_t marker_uidx, uint32_t* __restrict chrom_end_ptr, uint32_t* __restrict chrom_fo_idx_ptr, uint32_t* __restrict is_x_ptr, uint32_t* __restrict is_y_ptr, uint32_t* __restrict is_mt_ptr, uint32_t* __restrict is_haploid_ptr);
2357
2358 int32_t single_chrom_start(const Chrom_info* chrom_info_ptr, const uintptr_t* marker_exclude, uint32_t unfiltered_marker_ct);
2359
2360 double get_dmedian(const double* sorted_arr, uintptr_t len);
2361
2362 double destructive_get_dmedian(uintptr_t len, double* unsorted_arr);
2363
2364 int32_t strcmp_casted(const void* s1, const void* s2);
2365
2366 int32_t strcmp_natural(const void* s1, const void* s2);
2367
2368 int32_t strcmp_deref(const void* s1, const void* s2);
2369
2370 int32_t strcmp_natural_deref(const void* s1, const void* s2);
2371
2372 int32_t get_uidx_from_unsorted(const char* idstr, const uintptr_t* exclude_arr, uint32_t id_ct, const char* unsorted_ids, uintptr_t max_id_len);
2373
2374 // sorted_ids contents not changed, but not worth the trouble of returning a
2375 // const char*
2376 char* scan_for_duplicate_ids(char* sorted_ids, uintptr_t id_ct, uintptr_t max_id_len);
2377
2378 char* scan_for_duplicate_or_overlap_ids(char* sorted_ids, uintptr_t id_ct, uintptr_t max_id_len, const char* sorted_nonoverlap_ids, uintptr_t nonoverlap_id_ct, uintptr_t max_nonoverlap_id_len);
2379
2380 int32_t eval_affection(const char* bufptr, double missing_phenod);
2381
2382 uint32_t triangle_divide(int64_t cur_prod, int32_t modif);
2383
2384 void triangle_fill(uint32_t ct, uint32_t pieces, uint32_t parallel_idx, uint32_t parallel_tot, uint32_t start, uint32_t align, uint32_t* target_arr);
2385
2386 int32_t relationship_req(uint64_t calculation_type);
2387
2388 int32_t distance_req(const char* read_dists_fname, uint64_t calculation_type);
2389
2390 int32_t double_cmp(const void* aa, const void* bb);
2391
2392 int32_t double_cmp_decr(const void* aa, const void* bb);
2393
2394 int32_t double_cmp_deref(const void* aa, const void* bb);
2395
2396 int32_t char_cmp_deref(const void* aa, const void* bb);
2397
2398 // For use with qsort_ext(), when we need to use original-index as a secondary
2399 // sort key
2400 int32_t double_cmp_deref_tiebreak(const void* aa, const void* bb);
2401
2402 int32_t intcmp(const void* aa, const void* bb);
2403
2404 int32_t uintcmp(const void* aa, const void* bb);
2405
2406 #ifndef __cplusplus
2407 int32_t intcmp2(const void* aa, const void* bb);
2408 #endif
2409
2410 int32_t intcmp3_decr(const void* aa, const void* bb);
2411
2412 #ifndef __cplusplus
2413 int32_t llcmp(const void* aa, const void* bb);
2414 #endif
2415
2416 void qsort_ext2(char* main_arr, uintptr_t arr_length, uintptr_t item_length, int(* comparator_deref)(const void*, const void*), char* secondary_arr, uintptr_t secondary_item_len, char* proxy_arr, uintptr_t proxy_len);
2417
2418 int32_t qsort_ext(char* main_arr, uintptr_t arr_length, uintptr_t item_length, int(* comparator_deref)(const void*, const void*), char* secondary_arr, intptr_t secondary_item_len);
2419
2420 int32_t sort_item_ids_noalloc(uintptr_t unfiltered_ct, const uintptr_t* exclude_arr, uintptr_t item_ct, const char* __restrict item_ids, uintptr_t max_id_len, uint32_t allow_dups, uint32_t collapse_idxs, int(* comparator_deref)(const void*, const void*), char* __restrict sorted_ids, uint32_t* id_map);
2421
2422 int32_t sort_item_ids(uintptr_t unfiltered_ct, const uintptr_t* exclude_arr, uintptr_t exclude_ct, const char* __restrict item_ids, uintptr_t max_id_len, uint32_t allow_dups, uint32_t collapse_idxs, int(* comparator_deref)(const void*, const void*), char** sorted_ids_ptr, uint32_t** id_map_ptr);
2423
2424 uint32_t uint32arr_greater_than(const uint32_t* sorted_uint32_arr, uint32_t arr_length, uint32_t uii);
2425
2426 uint32_t int32arr_greater_than(const int32_t* sorted_int32_arr, uint32_t arr_length, int32_t ii);
2427
2428 uintptr_t uint64arr_greater_than(const uint64_t* sorted_uint64_arr, uintptr_t arr_length, uint64_t ullii);
2429
2430 uintptr_t doublearr_greater_than(const double* sorted_dbl_arr, uintptr_t arr_length, double dxx);
2431
2432 uintptr_t nonincr_doublearr_leq_stride(const double* nonincr_dbl_arr, uintptr_t arr_length, uintptr_t stride, double dxx);
2433
2434 int32_t bsearch_str(const char* id_buf, uintptr_t cur_id_len, const char* lptr, uintptr_t max_id_len, uintptr_t end_idx);
2435
2436 HEADER_INLINE int32_t bsearch_str_nl(const char* id_buf, const char* lptr, uintptr_t max_id_len, intptr_t end_idx) {
2437 return bsearch_str(id_buf, strlen(id_buf), lptr, max_id_len, end_idx);
2438 }
2439
2440 int32_t bsearch_str_natural(const char* id_buf, const char* lptr, uintptr_t max_id_len, uintptr_t end_idx);
2441
2442 uintptr_t bsearch_str_lb(const char* id_buf, uintptr_t cur_id_len, const char* lptr, uintptr_t max_id_len, uintptr_t end_idx);
2443
2444 uint32_t bsearch_read_fam_indiv(char* __restrict read_ptr, const char* __restrict lptr, uintptr_t max_id_len, uintptr_t filter_line_ct, char** read_pp_new, int32_t* retval_ptr, char* __restrict id_buf);
2445
2446 void bsearch_fam(const char* __restrict fam_id, const char* __restrict lptr, uintptr_t max_id_len, uint32_t filter_line_ct, uint32_t* __restrict first_idx_ptr, uint32_t* __restrict last_idx_ptr, char* __restrict id_buf);
2447
2448 // These ensure the trailing bits are zeroed out.
2449 void bitarr_invert(uintptr_t bit_ct, uintptr_t* bitarr);
2450
2451 void bitarr_invert_copy(const uintptr_t* input_bitarr, uintptr_t bit_ct, uintptr_t* output_bitarr);
2452
2453
2454 // "bitvec" indicates that word count is used instead of vector count.
2455 void bitvec_and(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec);
2456
2457 void bitvec_andnot(const uintptr_t* __restrict exclude_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec);
2458
2459 void bitvec_andnot_reversed_args(const uintptr_t* __restrict include_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec);
2460
2461 void bitvec_or(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* main_bitvec);
2462
2463 void bitvec_ornot(const uintptr_t* __restrict inverted_or_bitvec, uintptr_t word_ct, uintptr_t* main_bitvec);
2464
2465 void bitvec_xor(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec);
2466
2467 HEADER_INLINE uint32_t popcount2_long(uintptr_t val) {
2468 #ifdef __LP64__
2469 val = (val & 0x3333333333333333LLU) + ((val >> 2) & 0x3333333333333333LLU);
2470 return (((val + (val >> 4)) & 0x0f0f0f0f0f0f0f0fLLU) * 0x0101010101010101LLU) >> 56;
2471 #else
2472 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
2473 return (((val + (val >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
2474 #endif
2475 }
2476
2477 HEADER_INLINE uint32_t popcount_long(uintptr_t val) {
2478 // the simple version, good enough for all non-time-critical stuff
2479 return popcount2_long(val - ((val >> 1) & FIVEMASK));
2480 }
2481
2482 uint32_t is_monomorphic_a2(const uintptr_t* geno_arr, uint32_t sample_ct);
2483
2484 uint32_t is_monomorphic(const uintptr_t* geno_arr, uint32_t sample_ct);
2485
2486 // same as is_monomorphic, except it also flags the all-heterozygote case
2487 uint32_t less_than_two_genotypes(const uintptr_t* geno_arr, uint32_t sample_ct);
2488
2489 // uint32_t has_three_genotypes(uintptr_t* lptr, uint32_t sample_ct);
2490
2491 uintptr_t popcount_longs(const uintptr_t* lptr, uintptr_t word_ct);
2492
2493 #ifdef __LP64__
2494 HEADER_INLINE uintptr_t popcount_longs_nzbase(const uintptr_t* lptr, uintptr_t start_idx, uintptr_t end_idx) {
2495 uintptr_t prefix_ct = 0;
2496 if (start_idx & 1) {
2497 if (end_idx == start_idx) {
2498 return 0;
2499 }
2500 prefix_ct = popcount_long(lptr[start_idx++]);
2501 }
2502 return prefix_ct + popcount_longs(&(lptr[start_idx]), end_idx - start_idx);
2503 }
2504 #else
2505 HEADER_INLINE uintptr_t popcount_longs_nzbase(const uintptr_t* lptr, uintptr_t start_idx, uintptr_t end_idx) {
2506 return popcount_longs(&(lptr[start_idx]), end_idx - start_idx);
2507 }
2508 #endif
2509
2510 uintptr_t popcount2_longs(const uintptr_t* lptr, uintptr_t word_ct);
2511
2512 #define popcount01_longs popcount2_longs
2513
2514 uintptr_t popcount_bit_idx(const uintptr_t* lptr, uintptr_t start_idx, uintptr_t end_idx);
2515
2516 uint32_t chrom_window_max(const uint32_t* marker_pos, const uintptr_t* marker_exclude, const Chrom_info* chrom_info_ptr, uint32_t chrom_idx, uint32_t ct_max, uint32_t bp_max, uint32_t cur_window_max);
2517
2518 uint32_t window_back(const uint32_t* __restrict marker_pos, const double* __restrict marker_cms, const uintptr_t* marker_exclude, uint32_t marker_uidx_min, uint32_t marker_uidx_start, uint32_t count_max, uint32_t bp_max, double cm_max, uint32_t* __restrict window_trail_ct_ptr);
2519
2520 uint32_t window_forward(const uint32_t* __restrict marker_pos, const double* __restrict marker_cms, const uintptr_t* marker_exclude, uint32_t marker_uidx_start, uint32_t marker_uidx_last, uint32_t count_max, uint32_t bp_max, double cm_max, uint32_t* __restrict window_lead_ct_ptr);
2521
2522 uintptr_t jump_forward_unset_unsafe(const uintptr_t* bitarr, uintptr_t cur_pos, uintptr_t forward_ct);
2523
2524 HEADER_INLINE uintptr_t popcount_chars(const uintptr_t* lptr, uintptr_t start_idx, uintptr_t end_idx) {
2525 return popcount_bit_idx(lptr, start_idx * 8, end_idx * 8);
2526 }
2527
2528 // end_idx is in word, not bit, units
2529 uintptr_t popcount_longs_exclude(const uintptr_t* __restrict lptr, const uintptr_t* __restrict exclude_arr, uintptr_t end_idx);
2530
2531 uintptr_t popcount_longs_intersect(const uintptr_t* __restrict lptr1, const uintptr_t* __restrict lptr2, uintptr_t word_ct);
2532
2533 void vertical_bitct_subtract(const uintptr_t* bitarr, uint32_t item_ct, uint32_t* sum_arr);
2534
2535 #ifdef __LP64__
2536 void count_2freq_dbl_960b(const VECITYPE* geno_vvec, const VECITYPE* geno_vvec_end, const VECITYPE* __restrict mask1vp, const VECITYPE* __restrict mask2vp, uint32_t* __restrict ct1abp, uint32_t* __restrict ct1cp, uint32_t* __restrict ct2abp, uint32_t* __restrict ct2cp);
2537
2538 void count_3freq_1920b(const VECITYPE* geno_vvec, const VECITYPE* geno_vvec_end, const VECITYPE* __restrict maskvp, uint32_t* __restrict ctap, uint32_t* __restrict ctbp, uint32_t* __restrict ctcp);
2539 #else
2540 void count_2freq_dbl_24b(const uintptr_t* __restrict geno_vec, const uintptr_t* __restrict mask1p, const uintptr_t* __restrict mask2p, uint32_t* __restrict ct1abp, uint32_t* __restrict ct1cp, uint32_t* __restrict ct2abp, uint32_t* __restrict ct2cp);
2541
2542 void count_3freq_48b(const uintptr_t* __restrict geno_vec, const uintptr_t* __restrict maskp, uint32_t* __restrict ctap, uint32_t* __restrict ctbp, uint32_t* __restrict ctcp);
2543 #endif
2544
2545 void genovec_set_freq(const uintptr_t* __restrict geno_vec, const uintptr_t* __restrict include_quatervec, uintptr_t sample_ctl2, uint32_t* __restrict set_ctp, uint32_t* __restrict missing_ctp);
2546
2547 void genovec_set_freq_x(const uintptr_t* __restrict geno_vec, const uintptr_t* __restrict include_quatervec, const uintptr_t* __restrict male_quatervec, uintptr_t sample_ctl2, uint32_t* __restrict set_ctp, uint32_t* __restrict missing_ctp);
2548
2549 void genovec_set_freq_y(const uintptr_t* __restrict geno_vec, const uintptr_t* __restrict include_quatervec, const uintptr_t* __restrict nonmale_quatervec, uintptr_t sample_ctl2, uint32_t* __restrict set_ctp, uint32_t* __restrict missing_ctp);
2550
2551 void genovec_3freq(const uintptr_t* __restrict geno_vec, const uintptr_t* __restrict include_quatervec, uintptr_t sample_ctl2, uint32_t* __restrict missing_ctp, uint32_t* __restrict het_ctp, uint32_t* __restrict homset_ctp);
2552
2553 uintptr_t count_01(const uintptr_t* quatervec, uintptr_t word_ct);
2554
2555 HEADER_INLINE void zero_trailing_bits(uintptr_t unfiltered_ct, uintptr_t* bitarr) {
2556 uintptr_t trail_ct = unfiltered_ct & (BITCT - 1);
2557 if (trail_ct) {
2558 bitarr[unfiltered_ct / BITCT] &= (ONELU << trail_ct) - ONELU;
2559 }
2560 }
2561
2562 void fill_all_bits(uintptr_t ct, uintptr_t* bitarr);
2563
2564 uint32_t numeric_range_list_to_bitarr(const Range_list* range_list_ptr, uint32_t item_ct, uint32_t offset, uint32_t ignore_overflow, uintptr_t* bitarr);
2565
2566 int32_t string_range_list_to_bitarr(char* header_line, uint32_t item_ct, uint32_t fixed_len, const Range_list* range_list_ptr, const char* __restrict sorted_ids, const uint32_t* __restrict id_map, const char* __restrict range_list_flag, const char* __restrict file_descrip, uintptr_t* bitarr, int32_t* __restrict seen_idxs);
2567
2568 int32_t string_range_list_to_bitarr_alloc(char* header_line, uint32_t item_ct, uint32_t fixed_len, const Range_list* range_list_ptr, const char* __restrict range_list_flag, const char* __restrict file_descrip, uintptr_t** bitarr_ptr);
2569
2570 int32_t string_range_list_to_bitarr2(const char* __restrict sorted_ids, const uint32_t* id_map, uintptr_t item_ct, uintptr_t max_id_len, const Range_list* __restrict range_list_ptr, const char* __restrict range_list_flag, uintptr_t* bitarr_excl);
2571
2572 HEADER_INLINE uint32_t count_chrom_markers(const Chrom_info* chrom_info_ptr, const uintptr_t* marker_exclude, uint32_t chrom_idx) {
2573 if (!is_set(chrom_info_ptr->chrom_mask, chrom_idx)) {
2574 return 0;
2575 }
2576 const uint32_t chrom_fo_idx = chrom_info_ptr->chrom_idx_to_foidx[chrom_idx];
2577 const uint32_t min_idx = chrom_info_ptr->chrom_fo_vidx_start[chrom_fo_idx];
2578 const uint32_t max_idx = chrom_info_ptr->chrom_fo_vidx_start[chrom_fo_idx + 1];
2579 return (max_idx - min_idx) - ((uint32_t)popcount_bit_idx(marker_exclude, min_idx, max_idx));
2580 }
2581
2582 uint32_t count_non_autosomal_markers(const Chrom_info* chrom_info_ptr, const uintptr_t* marker_exclude, uint32_t count_x, uint32_t count_mt);
2583
2584 int32_t conditional_allocate_non_autosomal_markers(const Chrom_info* chrom_info_ptr, uintptr_t unfiltered_marker_ct, const uintptr_t* marker_exclude_orig, uint32_t marker_ct, uint32_t count_x, uint32_t count_mt, const char* calc_descrip, uintptr_t** marker_exclude_ptr, uint32_t* newly_excluded_ct_ptr);
2585
2586 uint32_t get_max_chrom_size(const Chrom_info* chrom_info_ptr, const uintptr_t* marker_exclude, uint32_t* last_chrom_fo_idx_ptr);
2587
2588 void count_genders(const uintptr_t* __restrict sex_nm, const uintptr_t* __restrict sex_male, const uintptr_t* __restrict sample_exclude, uintptr_t unfiltered_sample_ct, uint32_t* __restrict male_ct_ptr, uint32_t* __restrict female_ct_ptr, uint32_t* __restrict unk_ct_ptr);
2589
2590 void reverse_loadbuf(uintptr_t unfiltered_sample_ct, unsigned char* loadbuf);
2591
2592 // deprecated, try to just use copy_quaterarr_nonempty_subset()
2593 void copy_quaterarr_nonempty_subset_excl(const uintptr_t* __restrict raw_quaterarr, const uintptr_t* __restrict subset_excl, uint32_t raw_quaterarr_size, uint32_t subset_size, uintptr_t* __restrict output_quaterarr);
2594
2595 HEADER_INLINE uint32_t load_raw(uintptr_t unfiltered_sample_ct4, FILE* bedfile, uintptr_t* rawbuf) {
2596 // only use this if all accesses to the data involve
2597 // 1. some sort of mask, or
2598 // 2. explicit iteration from 0..(unfiltered_sample_ct-1).
2599 // otherwise improper trailing bits might cause a segfault, when we should
2600 // be ignoring them or just issuing a warning.
2601 return (fread(rawbuf, 1, unfiltered_sample_ct4, bedfile) < unfiltered_sample_ct4);
2602 }
2603
2604 HEADER_INLINE uintptr_t get_final_mask(uint32_t sample_ct) {
2605 uint32_t uii = sample_ct % BITCT2;
2606 if (uii) {
2607 return (ONELU << (2 * uii)) - ONELU;
2608 } else {
2609 return ~ZEROLU;
2610 }
2611 }
2612
2613 HEADER_INLINE uint32_t load_raw2(uintptr_t unfiltered_sample_ct4, uintptr_t unfiltered_sample_ctl2m1, uintptr_t final_mask, FILE* bedfile, uintptr_t* rawbuf) {
2614 if (fread(rawbuf, 1, unfiltered_sample_ct4, bedfile) < unfiltered_sample_ct4) {
2615 return 1;
2616 }
2617 rawbuf[unfiltered_sample_ctl2m1] &= final_mask;
2618 return 0;
2619 }
2620
2621 uint32_t load_and_collapse(uint32_t unfiltered_sample_ct, uint32_t sample_ct, const uintptr_t* __restrict sample_exclude, uintptr_t final_mask, uint32_t do_reverse, FILE* bedfile, uintptr_t* __restrict rawbuf, uintptr_t* __restrict mainbuf);
2622
2623 // was "collapse_copy_quaterarr_incl", but this should be better way to think
2624 // about it
2625 void copy_quaterarr_nonempty_subset(const uintptr_t* __restrict raw_quaterarr, const uintptr_t* __restrict subset_mask, uint32_t raw_quaterarr_size, uint32_t subset_size, uintptr_t* __restrict output_quaterarr);
2626
2627 /*
2628 // in-place version of copy_quaterarr_subset (usually destroying original
2629 // data).
2630 // this doesn't seem to provide a meaningful advantage over
2631 // copy_quaterarr_subset in practice, and the latter is more versatile without
2632 // requiring much more memory.
2633 void inplace_quaterarr_proper_subset(const uintptr_t* __restrict subset_mask, uint32_t orig_quaterarr_size, uint32_t subset_size, uintptr_t* __restrict main_quaterarr);
2634
2635 HEADER_INLINE void inplace_quaterarr_subset(const uintptr_t* __restrict subset_mask, uint32_t orig_quaterarr_size, uint32_t subset_size, uintptr_t* __restrict main_quaterarr) {
2636 if (orig_quaterarr_size == subset_size) {
2637 return;
2638 }
2639 inplace_quaterarr_proper_subset(subset_mask, orig_quaterarr_size, subset_size, main_quaterarr);
2640 }
2641 */
2642
2643 uint32_t load_and_collapse_incl(uint32_t unfiltered_sample_ct, uint32_t sample_ct, const uintptr_t* __restrict sample_include, uintptr_t final_mask, uint32_t do_reverse, FILE* bedfile, uintptr_t* __restrict rawbuf, uintptr_t* __restrict mainbuf);
2644
2645 // uint32_t load_and_collapse_incl_inplace(const uintptr_t* __restrict sample_include, uint32_t unfiltered_sample_ct, uint32_t sample_ct, uintptr_t final_mask, uint32_t do_reverse, FILE* bedfile, uintptr_t* __restrict mainbuf);
2646
2647 uint32_t load_and_split(uint32_t unfiltered_sample_ct, const uintptr_t* __restrict pheno_nm, const uintptr_t* __restrict pheno_c, FILE* bedfile, uintptr_t* __restrict rawbuf, uintptr_t* __restrict casebuf, uintptr_t* __restrict ctrlbuf);
2648
2649 void init_quaterarr_from_bitarr(const uintptr_t* __restrict bitarr, uintptr_t unfiltered_sample_ct, uintptr_t* __restrict new_quaterarr);
2650
2651 void init_quaterarr_from_inverted_bitarr(const uintptr_t* __restrict inverted_bitarr, uintptr_t unfiltered_sample_ct, uintptr_t* __restrict new_quaterarr);
2652
2653 void quatervec_01_init_invert(const uintptr_t* __restrict source_quatervec, uintptr_t entry_ct, uintptr_t* __restrict target_quatervec);
2654
2655 // target_vec := source_vec ANDNOT exclude_vec
2656 // may write an extra word
2657 void bitvec_andnot_copy(const uintptr_t* __restrict source_vec, const uintptr_t* __restrict exclude_vec, uintptr_t word_ct, uintptr_t* __restrict target_vec);
2658
2659 void apply_bitarr_mask_to_quaterarr_01(const uintptr_t* __restrict mask_bitarr, uintptr_t unfiltered_sample_ct, uintptr_t* main_quaterarr);
2660
2661 void apply_bitarr_excl_to_quaterarr_01(const uintptr_t* __restrict excl_bitarr, uintptr_t unfiltered_sample_ct, uintptr_t* __restrict main_quaterarr);
2662
2663 // excludes (excl_bitarr_1 & excl_bitarr_2). (union can be excluded by calling
2664 // apply_excl_to_quaterarr_01() twice.)
2665 void apply_excl_intersect_to_quaterarr_01(const uintptr_t* __restrict excl_bitarr_1, const uintptr_t* __restrict excl_bitarr_2, uintptr_t unfiltered_sample_ct, uintptr_t* __restrict main_quaterarr);
2666
2667 // initializes output_quatervec bits to 01 iff input_quatervec bits are 01,
2668 // everything else zeroed out
2669 void quatervec_copy_only_01(const uintptr_t* __restrict input_quatervec, uintptr_t unfiltered_sample_ct, uintptr_t* __restrict output_quatervec);
2670
2671 void quatervec_01_invert(uintptr_t unfiltered_sample_ct, uintptr_t* main_quatervec);
2672
2673 void vec_datamask(uintptr_t unfiltered_sample_ct, uint32_t matchval, uintptr_t* data_ptr, uintptr_t* mask_ptr, uintptr_t* result_ptr);
2674
2675 // void vec_rotate_plink1_to_plink2(uintptr_t* lptr, uint32_t word_ct);
2676
2677 void rotate_plink1_to_a2ct_and_copy(uintptr_t* loadbuf, uintptr_t* writebuf, uintptr_t word_ct);
2678
2679 void extract_collapsed_missing_bitfield(uintptr_t* lptr, uintptr_t unfiltered_sample_ct, uintptr_t* sample_include_quaterarr, uintptr_t sample_ct, uintptr_t* missing_bitfield);
2680
2681 void hh_reset(unsigned char* loadbuf, uintptr_t* sample_include_quaterarr, uintptr_t unfiltered_sample_ct);
2682
2683 void hh_reset_y(unsigned char* loadbuf, uintptr_t* sample_include_quaterarr, uintptr_t* sample_male_include_quaterarr, uintptr_t unfiltered_sample_ct);
2684
2685 HEADER_INLINE void haploid_fix(uint32_t hh_exists, uintptr_t* sample_include_quaterarr, uintptr_t* sample_male_include_quaterarr, uintptr_t sample_ct, uint32_t is_x, uint32_t is_y, unsigned char* loadbuf) {
2686 if (is_x) {
2687 if (hh_exists & XMHH_EXISTS) {
2688 hh_reset(loadbuf, sample_male_include_quaterarr, sample_ct);
2689 }
2690 } else if (is_y) {
2691 if (hh_exists & Y_FIX_NEEDED) {
2692 hh_reset_y(loadbuf, sample_include_quaterarr, sample_male_include_quaterarr, sample_ct);
2693 }
2694 } else if (hh_exists & NXMHH_EXISTS) {
2695 hh_reset(loadbuf, sample_include_quaterarr, sample_ct);
2696 }
2697 }
2698
2699 uint32_t alloc_raw_haploid_filters(uint32_t unfiltered_sample_ct, uint32_t hh_exists, uint32_t is_include, uintptr_t* sample_bitarr, uintptr_t* sex_male, uintptr_t** sample_raw_include_quatervec_ptr, uintptr_t** sample_raw_male_quatervec_ptr);
2700
2701 void haploid_fix_multiple(uintptr_t* marker_exclude, uintptr_t marker_uidx_start, uintptr_t marker_ct, Chrom_info* chrom_info_ptr, uint32_t hh_exists, uint32_t set_hh_missing, uint32_t set_mixed_mt_missing, uintptr_t* sample_raw_include2, uintptr_t* sample_raw_male_include2, uintptr_t unfiltered_sample_ct, uintptr_t byte_ct_per_marker, unsigned char* loadbuf);
2702
2703 void force_missing(unsigned char* loadbuf, uintptr_t* force_missing_include2, uintptr_t unfiltered_sample_ct);
2704
2705 HEADER_INLINE char sexchar(uintptr_t* sex_nm, uintptr_t* sex_male, uintptr_t sample_uidx) {
2706 if (is_set(sex_nm, sample_uidx)) {
2707 return '2' - is_set(sex_male, sample_uidx);
2708 } else {
2709 return '0';
2710 }
2711 }
2712
2713 int32_t open_and_size_string_list(char* fname, FILE** infile_ptr, uintptr_t* list_len_ptr, uintptr_t* max_str_len_ptr);
2714
2715 int32_t load_string_list(FILE** infile_ptr, uintptr_t max_str_len, char* str_list);
2716
2717 int32_t open_and_skip_first_lines(FILE** infile_ptr, char* fname, char* loadbuf, uintptr_t loadbuf_size, uint32_t lines_to_skip);
2718
2719 int32_t load_to_first_token(FILE* infile, uintptr_t loadbuf_size, char comment_char, const char* file_descrip, char* loadbuf, char** bufptr_ptr, uintptr_t* line_idx_ptr);
2720
2721 int32_t open_and_load_to_first_token(FILE** infile_ptr, char* fname, uintptr_t loadbuf_size, char comment_char, const char* file_descrip, char* loadbuf, char** bufptr_ptr, uintptr_t* line_idx_ptr);
2722
2723 int32_t scan_max_strlen(char* fname, uint32_t colnum, uint32_t colnum2, uint32_t headerskip, char skipchar, uintptr_t* max_str_len_ptr, uintptr_t* max_str2_len_ptr);
2724
2725 int32_t scan_max_fam_indiv_strlen(char* fname, uint32_t colnum, uintptr_t* max_sample_id_len_ptr);
2726
2727 // void inplace_collapse_uint32(uint32_t* item_arr, uint32_t unfiltered_ct, uintptr_t* exclude_arr, uint32_t filtered_ct);
2728
2729 void inplace_collapse_uint32_incl(uint32_t* item_arr, uint32_t unfiltered_ct, uintptr_t* incl_arr, uint32_t filtered_ct);
2730
2731 char* alloc_and_init_collapsed_arr(char* item_arr, uintptr_t item_len, uintptr_t unfiltered_ct, uintptr_t* exclude_arr, uintptr_t filtered_ct, uint32_t read_only);
2732
2733 char* alloc_and_init_collapsed_arr_incl(char* item_arr, uintptr_t item_len, uintptr_t unfiltered_ct, uintptr_t* include_arr, uintptr_t filtered_ct, uint32_t read_only);
2734
2735 void inplace_delta_collapse_arr(char* item_arr, uintptr_t item_len, uintptr_t filtered_ct_orig, uintptr_t filtered_ct_new, uintptr_t* exclude_orig, uintptr_t* exclude_new);
2736
2737 void inplace_delta_collapse_bitfield(uintptr_t* read_ptr, uint32_t filtered_ct_new, uintptr_t* exclude_orig, uintptr_t* exclude_new);
2738
2739 // deprecated, migrate to copy_bitarr_subset()
2740 void copy_bitarr_subset_excl(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_excl, uint32_t raw_bitarr_size, uint32_t subset_size, uintptr_t* __restrict output_bitarr);
2741
2742 void copy_bitarr_subset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t raw_bitarr_size, uint32_t subset_size, uintptr_t* __restrict output_bitarr);
2743
2744 void uncollapse_copy_flip_include_arr(uintptr_t* collapsed_include_arr, uintptr_t unfiltered_ct, uintptr_t* exclude_arr, uintptr_t* output_exclude_arr);
2745
2746 void copy_when_nonmissing(uintptr_t* loadbuf, char* source, uintptr_t elem_size, uintptr_t unfiltered_sample_ct, uintptr_t missing_ct, char* dest);
2747
2748 uint32_t collapse_duplicate_ids(char* sorted_ids, uintptr_t id_ct, uintptr_t max_id_len, uint32_t* id_starts);
2749
2750 HEADER_INLINE double rand_unif(void) {
2751 return (sfmt_genrand_uint32(&g_sfmt) + 0.5) * RECIP_2_32;
2752 }
2753
2754 void range_list_init(Range_list* range_list_ptr);
2755
2756 void free_range_list(Range_list* range_list_ptr);
2757
2758 double normdist(double zz);
2759
2760 double rand_normal(double* secondval_ptr);
2761
2762 void init_sfmt64_from_sfmt32(sfmt_t* sfmt32, sfmt_t* sfmt64);
2763
2764 HEADER_INLINE void precompute_mods(uintptr_t sample_ct, uint32_t* precomputed_mods) {
2765 // sets precomputed_mods[n] = 2^32 mod (n-2)
2766 uintptr_t sample_idx;
2767 for (sample_idx = 2; sample_idx <= sample_ct; sample_idx++) {
2768 *precomputed_mods++ = (uint32_t)(0x100000000LLU % sample_idx);
2769 }
2770 }
2771
2772 void generate_perm1_interleaved(uint32_t tot_ct, uint32_t set_ct, uintptr_t perm_idx, uintptr_t perm_ct, uintptr_t* perm_buf);
2773
2774 uint32_t cubic_real_roots(double coef_a, double coef_b, double coef_c, double* solutions);
2775
2776 void join_threads(pthread_t* threads, uint32_t ctp1);
2777
2778 #ifdef _WIN32
2779 int32_t spawn_threads(pthread_t* threads, unsigned (__stdcall *start_routine)(void*), uintptr_t ct);
2780 #else
2781 int32_t spawn_threads(pthread_t* threads, void* (*start_routine)(void*), uintptr_t ct);
2782 #endif
2783
2784 extern uintptr_t g_thread_spawn_ct;
2785 extern uint32_t g_is_last_thread_block;
2786
2787 #ifdef _WIN32
2788 extern HANDLE g_thread_start_next_event[];
2789 extern HANDLE g_thread_cur_block_done_events[];
2790
2791 HEADER_INLINE void THREAD_BLOCK_FINISH(uintptr_t tidx) {
2792 SetEvent(g_thread_cur_block_done_events[tidx - 1]);
2793 WaitForSingleObject(g_thread_start_next_event[tidx - 1], INFINITE);
2794 }
2795
2796 void join_threads2(pthread_t* threads, uint32_t ctp1, uint32_t is_last_block);
2797
2798 int32_t spawn_threads2(pthread_t* threads, unsigned (__stdcall *start_routine)(void*), uintptr_t ct, uint32_t is_last_block);
2799 #else
2800 void THREAD_BLOCK_FINISH(uintptr_t tidx);
2801
2802 void join_threads2(pthread_t* threads, uint32_t ctp1, uint32_t is_last_block);
2803
2804 int32_t spawn_threads2(pthread_t* threads, void* (*start_routine)(void*), uintptr_t ct, uint32_t is_last_block);
2805 #endif
2806
2807 extern sfmt_t** g_sfmtp_arr;
2808
2809 uint32_t bigstack_init_sfmtp(uint32_t thread_ct);
2810
2811 #endif // __PLINK_COMMON_H__