Debian Codegraph @

include/pgenlib_ffi_support.cc

raw
1 // This library is part of PLINK 2.0, copyright (C) 2005-2022 Shaun Purcell,
2 // Christopher Chang.
3 //
4 // This library is free software: you can redistribute it and/or modify it
5 // under the terms of the GNU Lesser General Public License as published by the
6 // Free Software Foundation; either version 3 of the License, or (at your
7 // option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 // for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this library. If not, see <http://www.gnu.org/licenses/>.
16
17 #include "pgenlib_ffi_support.h"
18
19 #ifdef __cplusplus
20 namespace plink2 {
21 #endif
22
23 void GenoarrToBytesMinus9(const uintptr_t* genoarr, uint32_t sample_ct, int8_t* genobytes) {
24 const uint32_t word_ct_m1 = (sample_ct - 1) / kBytesPerWord;
25 const Quarterword* read_alias = R_CAST(const Quarterword*, genoarr);
26 unsigned char* genobytes_uc = DowncastToUc(genobytes);
27 for (uint32_t widx = 0; ; ++widx) {
28 uintptr_t qw = Unpack0303(read_alias[widx]);
29 // now each byte is in {0, 1, 2, 3}. Convert the 3s to -9s in a branchless
30 // manner.
31 // (-9) - 3 = -12, which is represented as 244 in a uint8_t
32 const uintptr_t geno_missing = qw & (qw >> 1) & kMask0101;
33 qw += geno_missing * 244;
34 if (widx == word_ct_m1) {
35 SubwordStore(qw, ModNz(sample_ct, kBytesPerWord), &(genobytes_uc[widx * kBytesPerWord]));
36 return;
37 }
38 CopyToUnalignedOffsetW(genobytes_uc, &qw, widx);
39 }
40 }
41
42 static const int32_t kGenoInt32Quads[1024] ALIGNV16 = QUAD_TABLE256(0, 1, 2, -9);
43
44 void GenoarrToInt32sMinus9(const uintptr_t* genoarr, uint32_t sample_ct, int32_t* geno_int32) {
45 GenoarrLookup256x4bx4(genoarr, kGenoInt32Quads, sample_ct, geno_int32);
46 }
47
48 // todo: use GenoarrLookup16x8bx2()
49 static const int64_t kGenoToInt64[4] = {0, 1, 2, -9};
50
51 void GenoarrToInt64sMinus9(const uintptr_t* genoarr, uint32_t sample_ct, int64_t* geno_int64) {
52 const uint32_t word_ct_m1 = (sample_ct - 1) / kBitsPerWordD2;
53 int64_t* write_iter = geno_int64;
54 uint32_t subgroup_len = kBitsPerWordD2;
55 for (uint32_t widx = 0; ; ++widx) {
56 if (widx >= word_ct_m1) {
57 if (widx > word_ct_m1) {
58 return;
59 }
60 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
61 }
62 uintptr_t geno_word = genoarr[widx];
63 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
64 *write_iter++ = kGenoToInt64[geno_word & 3];
65 geno_word >>= 2;
66 }
67 }
68 }
69
70 // missing = -9
71 const double kGenoDoublePairs[32] ALIGNV16 = PAIR_TABLE16(0.0, 1.0, 2.0, -9.0);
72
73 const uint64_t kGenoToIntcodeDPairs[32] ALIGNV16 = PAIR_TABLE16(0, 0x100000000LLU, 0x100000001LLU, 0xfffffff7fffffff7LLU);
74
75 void GenoarrPhasedToAlleleCodes(const uint64_t* genoarr_to_intcode_dpair_table, const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, uint32_t sample_ct, uint32_t phasepresent_ct, unsigned char* phasebytes, int32_t* allele_codes) {
76 // phasebytes can be nullptr, phasepresent cannot
77 GenoarrToAlleleCodes(genoarr_to_intcode_dpair_table, genoarr, sample_ct, allele_codes);
78 // should be safe to assume allele_codes are 8-byte aligned?
79 uint64_t* allele_codes_alias64 = R_CAST(uint64_t*, allele_codes);
80 uintptr_t sample_uidx_base = 0;
81 uintptr_t cur_bits = phasepresent[0];
82 if (!phasebytes) {
83 for (uint32_t phased_idx = 0; phased_idx != phasepresent_ct; ++phased_idx) {
84 const uintptr_t sample_uidx = BitIter1(phasepresent, &sample_uidx_base, &cur_bits);
85 if (IsSet(phaseinfo, sample_uidx)) {
86 // 1|0
87 allele_codes_alias64[sample_uidx] = 1;
88 }
89 }
90 return;
91 }
92 // 0 and 2 = homozygous, automatically phased; otherwise patch in from
93 // phaseinfo if phasepresent_ct is nonzero
94 // so, start off by extracting low bit from each pair and flipping it
95 const uint32_t word_ct_m1 = (sample_ct - 1) / kBytesPerWord;
96 const Quarterword* read_alias = R_CAST(const Quarterword*, genoarr);
97 uintptr_t* write_walias = R_CAST(uintptr_t*, phasebytes);
98 for (uint32_t widx = 0; ; ++widx) {
99 uintptr_t qw = Unpack0303(read_alias[widx]);
100 qw = (~qw) & kMask0101;
101 if (widx == word_ct_m1) {
102 SubwordStore(qw, ModNz(sample_ct, kBytesPerWord), &(write_walias[widx]));
103 break;
104 }
105 write_walias[widx] = qw;
106 }
107 for (uint32_t phased_idx = 0; phased_idx != phasepresent_ct; ++phased_idx) {
108 const uintptr_t sample_uidx = BitIter1(phasepresent, &sample_uidx_base, &cur_bits);
109 phasebytes[sample_uidx] = 1;
110 if (IsSet(phaseinfo, sample_uidx)) {
111 allele_codes_alias64[sample_uidx] = 1;
112 }
113 }
114 }
115
116 void GenoarrMPToAlleleCodes(const uint64_t* geno_to_intcode_dpair_table, const PgenVariant* pgv, uint32_t sample_ct, unsigned char* phasebytes, int32_t* allele_codes) {
117 // phasebytes can be nullptr, phasepresent cannot
118 const uintptr_t* genoarr = pgv->genovec;
119 const uintptr_t* phasepresent = pgv->phasepresent;
120 const uintptr_t* phaseinfo = pgv->phaseinfo;
121 const uint32_t phasepresent_ct = pgv->phasepresent_ct;
122 const uint32_t patch_01_ct = pgv->patch_01_ct;
123 const uint32_t patch_10_ct = pgv->patch_10_ct;
124 if ((!patch_01_ct) && (!patch_10_ct)) {
125 GenoarrPhasedToAlleleCodes(geno_to_intcode_dpair_table, genoarr, phasepresent, phaseinfo, sample_ct, phasepresent_ct, phasebytes, allele_codes);
126 return;
127 }
128 GenoarrToAlleleCodes(geno_to_intcode_dpair_table, genoarr, sample_ct, allele_codes);
129 // See e.g. PglMultiallelicSparseToDense().
130 if (patch_01_ct) {
131 const uintptr_t* patch_01_set = pgv->patch_01_set;
132 const AlleleCode* patch_01_vals = pgv->patch_01_vals;
133 uintptr_t sample_idx_base = 0;
134 uintptr_t cur_bits = patch_01_set[0];
135 int32_t* allele_codes1 = &(allele_codes[1]);
136 for (uint32_t uii = 0; uii != patch_01_ct; ++uii) {
137 const uintptr_t sample_idx = BitIter1(patch_01_set, &sample_idx_base, &cur_bits);
138 allele_codes1[2 * sample_idx] = patch_01_vals[uii];
139 }
140 }
141 if (phasebytes) {
142 // Initialize 0/2 phasebytes before processing patch_10.
143 const uint32_t word_ct_m1 = (sample_ct - 1) / kBytesPerWord;
144 const Quarterword* read_alias = R_CAST(const Quarterword*, genoarr);
145 for (uint32_t widx = 0; ; ++widx) {
146 uintptr_t qw = Unpack0303(read_alias[widx]);
147 qw = (~qw) & kMask0101;
148 if (widx == word_ct_m1) {
149 SubwordStore(qw, ModNz(sample_ct, kBytesPerWord), &(phasebytes[widx * kBytesPerWord]));
150 break;
151 }
152 CopyToUnalignedOffsetW(phasebytes, &qw, widx);
153 }
154 }
155 if (patch_10_ct) {
156 const uintptr_t* patch_10_set = pgv->patch_10_set;
157 const AlleleCode* patch_10_vals = pgv->patch_10_vals;
158 uintptr_t sample_idx_base = 0;
159 uintptr_t cur_bits = patch_10_set[0];
160 if (!phasebytes) {
161 for (uint32_t uii = 0; uii != patch_10_ct; ++uii) {
162 const uintptr_t sample_idx = BitIter1(patch_10_set, &sample_idx_base, &cur_bits);
163 allele_codes[2 * sample_idx] = patch_10_vals[2 * uii];
164 allele_codes[2 * sample_idx + 1] = patch_10_vals[2 * uii + 1];
165 }
166 } else {
167 for (uint32_t uii = 0; uii != patch_10_ct; ++uii) {
168 const uintptr_t sample_idx = BitIter1(patch_10_set, &sample_idx_base, &cur_bits);
169 const AlleleCode ac0 = patch_10_vals[2 * uii];
170 const AlleleCode ac1 = patch_10_vals[2 * uii + 1];
171 allele_codes[2 * sample_idx] = ac0;
172 allele_codes[2 * sample_idx + 1] = ac1;
173 if (ac0 != ac1) {
174 phasebytes[sample_idx] = 0;
175 // When phasepresent bit is set, we'll fix this up later.
176 }
177 }
178 }
179 }
180 uintptr_t sample_uidx_base = 0;
181 uintptr_t cur_bits = phasepresent[0];
182 if (!phasebytes) {
183 for (uint32_t phased_idx = 0; phased_idx != phasepresent_ct; ++phased_idx) {
184 const uintptr_t sample_uidx = BitIter1(phasepresent, &sample_uidx_base, &cur_bits);
185 if (IsSet(phaseinfo, sample_uidx)) {
186 const int32_t tmp_code = allele_codes[2 * sample_uidx];
187 allele_codes[2 * sample_uidx] = allele_codes[2 * sample_uidx + 1];
188 allele_codes[2 * sample_uidx + 1] = tmp_code;
189 }
190 }
191 return;
192 }
193 for (uint32_t phased_idx = 0; phased_idx != phasepresent_ct; ++phased_idx) {
194 const uintptr_t sample_uidx = BitIter1(phasepresent, &sample_uidx_base, &cur_bits);
195 phasebytes[sample_uidx] = 1;
196 if (IsSet(phaseinfo, sample_uidx)) {
197 const int32_t tmp_code = allele_codes[2 * sample_uidx];
198 allele_codes[2 * sample_uidx] = allele_codes[2 * sample_uidx + 1];
199 allele_codes[2 * sample_uidx + 1] = tmp_code;
200 }
201 }
202 }
203
204 // missing = -9
205 // may want a double-lookup function for this
206 static const int32_t kGenoToHap0Code[6] = {0, 0, 1, -9, 0, 1};
207 static const int32_t kGenoToHap1Code[6] = {0, 1, 1, -9, 0, 0};
208
209 // todo: write version of this which fills phasebytes
210 void GenoarrPhasedToHapCodes(const uintptr_t* genoarr, const uintptr_t* phaseinfo, uint32_t variant_batch_size, int32_t* hap0_codes_iter, int32_t* hap1_codes_iter) {
211 // assumes genoarr and phaseinfo have already been transposed
212 const uint32_t word_ct_m1 = (variant_batch_size - 1) / kBitsPerWordD2;
213 const Halfword* phaseinfo_alias = R_CAST(const Halfword*, phaseinfo);
214 uint32_t subgroup_len = kBitsPerWordD2;
215 for (uint32_t widx = 0; ; ++widx) {
216 if (widx >= word_ct_m1) {
217 if (widx > word_ct_m1) {
218 return;
219 }
220 subgroup_len = ModNz(variant_batch_size, kBitsPerWordD2);
221 }
222 uintptr_t geno_word = genoarr[widx];
223 uintptr_t phaseinfo_hw = phaseinfo_alias[widx];
224 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
225 const uintptr_t cur_pgeno_code = (geno_word & 3) + 4 * (phaseinfo_hw & 1);
226 *hap0_codes_iter++ = kGenoToHap0Code[cur_pgeno_code];
227 *hap1_codes_iter++ = kGenoToHap1Code[cur_pgeno_code];
228 geno_word >>= 2;
229 phaseinfo_hw >>= 1;
230 }
231 }
232 }
233
234 // todo: use GenoarrLookup256x4bx4()
235 static const float kGenoToFloat[4] = {0.0, 1.0, 2.0, -9.0};
236
237 void Dosage16ToFloatsMinus9(const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, float* geno_float) {
238 const uint32_t word_ct_m1 = (sample_ct - 1) / kBitsPerWordD2;
239 float* write_iter = geno_float;
240 uint32_t subgroup_len = kBitsPerWordD2;
241 for (uint32_t widx = 0; ; ++widx) {
242 if (widx >= word_ct_m1) {
243 if (widx > word_ct_m1) {
244 break;
245 }
246 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
247 }
248 uintptr_t geno_word = genoarr[widx];
249 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
250 *write_iter++ = kGenoToFloat[geno_word & 3];
251 geno_word >>= 2;
252 }
253 }
254 if (dosage_ct) {
255 const uint16_t* dosage_main_iter = dosage_main;
256 uintptr_t sample_uidx_base = 0;
257 uintptr_t cur_bits = dosage_present[0];
258 for (uint32_t dosage_idx = 0; dosage_idx != dosage_ct; ++dosage_idx) {
259 const uintptr_t sample_uidx = BitIter1(dosage_present, &sample_uidx_base, &cur_bits);
260 // multiply by 2^{-14}
261 geno_float[sample_uidx] = S_CAST(float, *dosage_main_iter++) * S_CAST(float, 0.00006103515625);
262 }
263 }
264 }
265
266 void Dosage16ToDoubles(const double* geno_double_pair_table, const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, double* geno_double) {
267 GenoarrLookup16x8bx2(genoarr, geno_double_pair_table, sample_ct, geno_double);
268 if (dosage_ct) {
269 const uint16_t* dosage_main_iter = dosage_main;
270 uintptr_t sample_uidx_base = 0;
271 uintptr_t cur_bits = dosage_present[0];
272 for (uint32_t dosage_idx = 0; dosage_idx != dosage_ct; ++dosage_idx) {
273 const uintptr_t sample_uidx = BitIter1(dosage_present, &sample_uidx_base, &cur_bits);
274 geno_double[sample_uidx] = S_CAST(double, *dosage_main_iter++) * 0.00006103515625;
275 }
276 }
277 }
278
279 BoolErr Dosage16ToDoublesMeanimpute(const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, double* geno_double) {
280 STD_ARRAY_DECL(uint32_t, 4, genocounts);
281 double geno_double_pair_buf[32];
282 if (!dosage_ct) {
283 GenoarrCountFreqsUnsafe(genoarr, sample_ct, genocounts);
284 const double* geno_double_pair_table = kGenoDoublePairs;
285 if (genocounts[3]) {
286 const uint32_t denom = sample_ct - genocounts[3];
287 if (!denom) {
288 return 1;
289 }
290 const uint32_t numer = genocounts[1] + 2 * genocounts[2];
291 const double missing_val = u63tod(numer) / u31tod(denom);
292 geno_double_pair_buf[0] = 0.0;
293 geno_double_pair_buf[2] = 1.0;
294 geno_double_pair_buf[4] = 2.0;
295 geno_double_pair_buf[6] = missing_val;
296 InitLookup16x8bx2(geno_double_pair_buf);
297 geno_double_pair_table = geno_double_pair_buf;
298 }
299 GenoarrLookup16x8bx2(genoarr, geno_double_pair_table, sample_ct, geno_double);
300 return 0;
301 }
302 // In the generic case, it may be faster to check for the existence of a
303 // missing value before calling GenoarrCountInvsubsetFreqs2() (since if there
304 // are no missing values, we don't need to count at all). However, we assume
305 // the caller is using this function over Dosage16ToDoubles() for a reason.
306 GenoarrCountInvsubsetFreqs2(genoarr, dosage_present, sample_ct, sample_ct - dosage_ct, genocounts);
307 const double* geno_double_pair_table = kGenoDoublePairs;
308 if (genocounts[3]) {
309 uint64_t denom = sample_ct - genocounts[3];
310 if (!denom) {
311 return 1;
312 }
313 denom *= 16384LLU;
314 uint64_t numer = 0;
315 for (uint32_t dosage_idx = 0; dosage_idx != dosage_ct; ++dosage_idx) {
316 numer += dosage_main[dosage_idx];
317 }
318 numer += 16384LLU * (genocounts[1] + 2 * genocounts[2]);
319 const double missing_val = u63tod(numer) / u63tod(denom);
320 geno_double_pair_buf[0] = 0.0;
321 geno_double_pair_buf[2] = 1.0;
322 geno_double_pair_buf[4] = 2.0;
323 geno_double_pair_buf[6] = missing_val;
324 InitLookup16x8bx2(geno_double_pair_buf);
325 geno_double_pair_table = geno_double_pair_buf;
326 }
327 GenoarrLookup16x8bx2(genoarr, geno_double_pair_table, sample_ct, geno_double);
328 uintptr_t sample_uidx_base = 0;
329 uintptr_t cur_bits = dosage_present[0];
330 for (uint32_t dosage_idx = 0; dosage_idx != dosage_ct; ++dosage_idx) {
331 const uintptr_t sample_uidx = BitIter1(dosage_present, &sample_uidx_base, &cur_bits);
332 geno_double[sample_uidx] = S_CAST(double, dosage_main[dosage_idx]) * 0.00006103515625;
333 }
334 return 0;
335 }
336
337 double LinearCombinationMeanimpute(const double* weights, const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct) {
338 const uint32_t word_ct = DivUp(sample_ct, kBitsPerWordD2);
339 double result = 0.0;
340 double result2 = 0.0;
341 double miss_weight = 0.0;
342 if (!dosage_ct) {
343 for (uint32_t widx = 0; widx != word_ct; ++widx) {
344 const uintptr_t geno_word = genoarr[widx];
345 if (!geno_word) {
346 continue;
347 }
348 const double* cur_weights = &(weights[widx * kBitsPerWordD2]);
349 uintptr_t geno_word1 = geno_word & kMask5555;
350 uintptr_t geno_word2 = (geno_word >> 1) & kMask5555;
351 uintptr_t geno_missing_word = geno_word1 & geno_word2;
352 geno_word1 ^= geno_missing_word;
353 while (geno_word1) {
354 const uint32_t sample_idx_lowbits = ctzw(geno_word1) / 2;
355 result += cur_weights[sample_idx_lowbits];
356 geno_word1 &= geno_word1 - 1;
357 }
358 geno_word2 ^= geno_missing_word;
359 while (geno_word2) {
360 const uint32_t sample_idx_lowbits = ctzw(geno_word2) / 2;
361 result2 += cur_weights[sample_idx_lowbits];
362 geno_word2 &= geno_word2 - 1;
363 }
364 while (geno_missing_word) {
365 const uint32_t sample_idx_lowbits = ctzw(geno_missing_word) / 2;
366 miss_weight += cur_weights[sample_idx_lowbits];
367 geno_missing_word &= geno_missing_word - 1;
368 }
369 }
370 result += 2 * result2;
371 if (miss_weight != 0.0) {
372 // bugfix (29 Oct 2019): previous mean-imputation formula was based on
373 // *weighted* MAF, which was obviously nonsense when negative weights
374 // were present.
375 STD_ARRAY_DECL(uint32_t, 4, genocounts);
376 GenoarrCountFreqsUnsafe(genoarr, sample_ct, genocounts);
377 const double numer = u63tod(genocounts[1] + 2 * genocounts[2]);
378 const double denom = u31tod(sample_ct - genocounts[3]);
379 result += miss_weight * (numer / denom);
380 }
381 return result;
382 }
383 const Halfword* dosage_present_hws = R_CAST(const Halfword*, dosage_present);
384 uint32_t onealt_ct = 0;
385 uint32_t twoalt_ct = 0;
386 uint32_t missing_ct = 0;
387 for (uint32_t widx = 0; widx != word_ct; ++widx) {
388 const uintptr_t geno_word = genoarr[widx];
389 if (geno_word) {
390 const double* cur_weights = &(weights[widx * kBitsPerWordD2]);
391 uintptr_t geno_word1 = geno_word & kMask5555;
392 uintptr_t geno_word2 = (geno_word >> 1) & kMask5555;
393 uintptr_t geno_missing_word = geno_word1 & geno_word2;
394 const uintptr_t mask_word = ~(geno_missing_word | UnpackHalfwordToWord(dosage_present_hws[widx]));
395 geno_word1 &= mask_word;
396 while (geno_word1) {
397 const uint32_t sample_idx_lowbits = ctzw(geno_word1) / 2;
398 result += cur_weights[sample_idx_lowbits];
399 // probably sparse enough that this is faster than popcount?
400 ++onealt_ct;
401 geno_word1 &= geno_word1 - 1;
402 }
403 geno_word2 &= mask_word;
404 while (geno_word2) {
405 const uint32_t sample_idx_lowbits = ctzw(geno_word2) / 2;
406 result2 += cur_weights[sample_idx_lowbits];
407 ++twoalt_ct;
408 geno_word2 &= geno_word2 - 1;
409 }
410 while (geno_missing_word) {
411 const uint32_t sample_idx_lowbits = ctzw(geno_missing_word) / 2;
412 miss_weight += cur_weights[sample_idx_lowbits];
413 ++missing_ct;
414 geno_missing_word &= geno_missing_word - 1;
415 }
416 }
417 }
418 result += result2 * 2;
419 const uint16_t* dosage_main_iter = dosage_main;
420 double resultx = 0.0;
421 uintptr_t sample_uidx_base = 0;
422 uintptr_t cur_bits = dosage_present[0];
423 if (miss_weight == 0.0) {
424 for (uint32_t dosage_idx = 0; dosage_idx != dosage_ct; ++dosage_idx) {
425 const uintptr_t sample_uidx = BitIter1(dosage_present, &sample_uidx_base, &cur_bits);
426 resultx += S_CAST(double, *dosage_main_iter++) * weights[sample_uidx];
427 }
428 result += 0.00006103515625 * resultx;
429 return result;
430 }
431 // Also need to track dosage-sum for mean-imputation.
432 uint64_t dosage_sum = 0;
433 for (uint32_t dosage_idx = 0; dosage_idx != dosage_ct; ++dosage_idx) {
434 const uintptr_t sample_uidx = BitIter1(dosage_present, &sample_uidx_base, &cur_bits);
435 const uint32_t cur_dosage = *dosage_main_iter++;
436 dosage_sum += cur_dosage;
437 resultx += u31tod(cur_dosage) * weights[sample_uidx];
438 }
439 result += 0.00006103515625 * resultx;
440 const double numer = u63tod(16384 * S_CAST(uint64_t, onealt_ct + 2 * twoalt_ct) + dosage_sum);
441 const double denom = 16384 * u31tod(sample_ct - missing_ct);
442 result += miss_weight * (numer / denom);
443 return result;
444 }
445
446 void BytesToBitsUnsafe(const uint8_t* boolbytes, uint32_t sample_ct, uintptr_t* bitarr) {
447 const uint32_t ull_ct_m1 = (sample_ct - 1) / 8;
448 const unsigned char* boolbytes_uc = DowncastKToUc(boolbytes);
449 unsigned char* write_alias = DowncastToUc(bitarr);
450 for (uint32_t ullidx = 0; ; ++ullidx) {
451 uint64_t cur_ull;
452 if (ullidx >= ull_ct_m1) {
453 if (ullidx > ull_ct_m1) {
454 return;
455 }
456 cur_ull = SubU64Load(&(boolbytes_uc[ullidx * sizeof(int64_t)]), ModNz(sample_ct, 8));
457 } else {
458 CopyFromUnalignedOffsetU64(&cur_ull, boolbytes_uc, ullidx);
459 }
460 // assuming boolbytes is 0/1-valued, this multiply-and-shift maps binary
461 // h0000000g0000000f... to binary hgfedcba.
462 // ^ ^ ^
463 // | | |
464 // 56 48 40
465 // (the constant has bits 0, 7, 14, 21, 28, 35, 42, and 49 set)
466 // (can also use _pext_u64() in AVX2 case)
467 write_alias[ullidx] = S_CAST(unsigned char, (cur_ull * 0x2040810204081LLU) >> 49);
468 }
469 }
470
471 void BytesToGenoarrUnsafe(const int8_t* genobytes, uint32_t sample_ct, uintptr_t* genoarr) {
472 const uint32_t word_ct_m1 = (sample_ct - 1) / kBytesPerWord;
473 const unsigned char* genobytes_uc = DowncastKToUc(genobytes);
474 Quarterword* write_alias = R_CAST(Quarterword*, genoarr);
475 for (uint32_t widx = 0; ; ++widx) {
476 uintptr_t ww;
477 if (widx >= word_ct_m1) {
478 if (widx > word_ct_m1) {
479 return;
480 }
481 ww = SubwordLoad(&(genobytes_uc[widx * kBytesPerWord]), ModNz(sample_ct, kBytesPerWord));
482 } else {
483 CopyFromUnalignedOffsetW(&ww, genobytes_uc, widx);
484 }
485 write_alias[widx] = Pack0303Mask(ww);
486 }
487 }
488
489 void AlleleCodesToGenoarrUnsafe(const int32_t* allele_codes, const unsigned char* phasepresent_bytes, uint32_t sample_ct, uintptr_t* genoarr, uintptr_t* phasepresent, uintptr_t* phaseinfo) {
490 // - If phasepresent_bytes is nullptr, phasepresent is not updated. In this
491 // case, phaseinfo is updated iff it's not nullptr. It's okay for both
492 // phasepresent and phaseinfo to be nullptr here.
493 // - Otherwise, phasepresent and phaseinfo are always updated; neither can be
494 // nullptr.
495 // - Trailing bits of phasepresent/phaseinfo may not be zeroed out.
496 const uint32_t word_ct_m1 = (sample_ct - 1) / kBitsPerWordD2;
497 uint32_t subgroup_len = kBitsPerWordD2;
498 const uint32_t* read_alias = R_CAST(const uint32_t*, allele_codes);
499 Halfword* phaseinfo_alias = R_CAST(Halfword*, phaseinfo);
500 if (!phasepresent_bytes) {
501 for (uint32_t widx = 0; ; ++widx) {
502 if (widx >= word_ct_m1) {
503 if (widx > word_ct_m1) {
504 return;
505 }
506 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
507 }
508 uintptr_t geno_write_word = 0;
509 if (!phaseinfo) {
510 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
511 // 0,0 -> 0
512 // 0,1 or 1,0 -> 1
513 // 1,1 -> 2
514 // -9,-9 -> 3
515 // undefined behavior on e.g. 0,2
516 const uint32_t first_code = *read_alias++;
517 const uint32_t second_code = *read_alias++;
518 uintptr_t cur_geno;
519 if (first_code <= 1) {
520 cur_geno = first_code + second_code;
521 } else {
522 // todo: test whether branchless is better
523 // (in practice, this will usually be predictable?)
524 cur_geno = 3;
525 }
526 geno_write_word |= (cur_geno << (uii * 2));
527 }
528 } else {
529 Halfword phaseinfo_write_hw = 0;
530 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
531 // set phaseinfo_write_hw bit iff 1,0
532 const uint32_t first_code = *read_alias++;
533 const uint32_t second_code = *read_alias++;
534 uintptr_t cur_geno;
535 if (first_code <= 1) {
536 cur_geno = first_code + second_code;
537 phaseinfo_write_hw |= (cur_geno & first_code) << uii;
538 } else {
539 // todo: test whether branchless is better
540 // (in practice, this will usually be predictable?)
541 cur_geno = 3;
542 }
543 geno_write_word |= (cur_geno << (uii * 2));
544 }
545 phaseinfo_alias[widx] = phaseinfo_write_hw;
546 }
547 genoarr[widx] = geno_write_word;
548 }
549 }
550 const unsigned char* phasepresent_bytes_iter = phasepresent_bytes;
551 Halfword* phasepresent_alias = R_CAST(Halfword*, phasepresent);
552 for (uint32_t widx = 0; ; ++widx) {
553 if (widx >= word_ct_m1) {
554 if (widx > word_ct_m1) {
555 return;
556 }
557 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
558 }
559 uintptr_t geno_write_word = 0;
560 Halfword phasepresent_write_hw = 0;
561 Halfword phaseinfo_write_hw = 0;
562 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
563 const uint32_t first_code = *read_alias++;
564 const uint32_t second_code = *read_alias++;
565 uintptr_t cur_geno;
566 if (first_code <= 1) {
567 cur_geno = first_code + second_code;
568 const uint32_t cur_phasepresent = cur_geno & phasepresent_bytes_iter[uii];
569 phasepresent_write_hw |= cur_phasepresent << uii;
570 phaseinfo_write_hw |= (cur_phasepresent & first_code) << uii;
571 } else {
572 cur_geno = 3;
573 }
574 geno_write_word |= (cur_geno << (uii * 2));
575 }
576 phasepresent_bytes_iter = &(phasepresent_bytes_iter[subgroup_len]);
577 phasepresent_alias[widx] = phasepresent_write_hw;
578 phaseinfo_alias[widx] = phaseinfo_write_hw;
579 genoarr[widx] = geno_write_word;
580 }
581 }
582
583 // Does not clear trailing bits of genovec, phasepresent, or phaseinfo.
584 // Returns max(2, 1 + max allele code) if allele_codes is valid, -1 if invalid.
585 int32_t ConvertMultiAlleleCodesUnsafe(const int32_t* allele_codes, const unsigned char* phasepresent_bytes, uint32_t sample_ct, uintptr_t* genoarr, uintptr_t* patch_01_set, AlleleCode* patch_01_vals, uintptr_t* patch_10_set, AlleleCode* patch_10_vals, uint32_t* patch_01_ctp, uint32_t* patch_10_ctp, uintptr_t* phasepresent, uintptr_t* phaseinfo) {
586 const uint32_t sample_ctl = DivUp(sample_ct, kBitsPerWord);
587 const uint32_t word_ct_m1 = (sample_ct - 1) / kBitsPerWordD2;
588 uint32_t subgroup_len = kBitsPerWordD2;
589 const uint32_t* read_alias = R_CAST(const uint32_t*, allele_codes);
590 if (phasepresent_bytes) {
591 BytesToBitsUnsafe(phasepresent_bytes, sample_ct, phasepresent);
592 }
593 Halfword* phasepresent_alias = R_CAST(Halfword*, phasepresent);
594 Halfword* phaseinfo_alias = R_CAST(Halfword*, phaseinfo);
595 // todo: try scanning allele_codes for its maximum value upfront, instead of
596 // checking in inner loops; then mirror PglMultiallelicDenseToSparse() in
597 // valid multiallelic case, and call AlleleCodesToGenoarrUnsafe() otherwise.
598 ZeroWArr(sample_ctl, patch_01_set);
599 ZeroWArr(sample_ctl, patch_10_set);
600 uint32_t max_allele_code = 1;
601 Halfword* patch_01_set_alias = R_CAST(Halfword*, patch_01_set);
602 Halfword* patch_10_set_alias = R_CAST(Halfword*, patch_10_set);
603 AlleleCode* patch_01_iter = patch_01_vals;
604 AlleleCode* patch_10_iter = patch_10_vals;
605 for (uint32_t widx = 0; ; ++widx) {
606 if (widx >= word_ct_m1) {
607 if (widx > word_ct_m1) {
608 if (max_allele_code >= kPglMaxAlleleCt) {
609 return -1;
610 }
611 *patch_01_ctp = patch_01_iter - patch_01_vals;
612 *patch_10_ctp = (patch_10_iter - patch_10_vals) >> 1;
613 return S_CAST(int32_t, max_allele_code + 1);
614 }
615 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
616 }
617 uintptr_t geno_write_word = 0;
618 Halfword phaseinfo_write_hw = 0;
619 Halfword het_2_hw = 0;
620 for (uint32_t uii = 0; uii != subgroup_len; ++uii) {
621 // 0,0 -> 0
622 // 0,x or x,0 -> 1
623 // x,y -> 2
624 // -9,-9 -> 3
625 const uint32_t first_code = *read_alias++;
626 const uint32_t second_code = *read_alias++;
627 uintptr_t cur_geno = 0;
628 if (first_code == 0) {
629 if (second_code != 0) {
630 cur_geno = 1;
631 if (second_code > 1) {
632 if (second_code > max_allele_code) {
633 max_allele_code = second_code;
634 }
635 patch_01_set_alias[widx] |= 1U << uii;
636 // If second_code is actually out-of-range, harmlessly truncate
637 // here, and error out before function return.
638 // (this code is correct without the static-cast, but may as well
639 // be explicit about where truncation could happen.)
640 *patch_01_iter++ = S_CAST(AlleleCode, second_code);
641 }
642 }
643 } else if (first_code == 0xfffffff7U) {
644 if (second_code != 0xfffffff7U) {
645 return -1;
646 }
647 cur_geno = 3;
648 } else {
649 // first_code >= 1
650 if (second_code == 0) {
651 cur_geno = 1;
652 phaseinfo_write_hw |= 1U << uii;
653 if (first_code > 1) {
654 if (first_code > max_allele_code) {
655 max_allele_code = first_code;
656 }
657 patch_01_set_alias[widx] |= 1U << uii;
658 *patch_01_iter++ = S_CAST(AlleleCode, first_code);
659 }
660 } else {
661 cur_geno = 2;
662 if (first_code <= second_code) {
663 if (second_code > 1) {
664 if (second_code > max_allele_code) {
665 max_allele_code = second_code;
666 }
667 patch_10_set_alias[widx] |= 1U << uii;
668 *patch_10_iter++ = S_CAST(AlleleCode, first_code);
669 *patch_10_iter++ = S_CAST(AlleleCode, second_code);
670 if (first_code != second_code) {
671 het_2_hw |= 1U << uii;
672 }
673 }
674 } else {
675 // first_code > second_code
676 if (first_code > max_allele_code) {
677 max_allele_code = first_code;
678 }
679 phaseinfo_write_hw |= 1U << uii;
680 patch_10_set_alias[widx] |= 1U << uii;
681 het_2_hw |= 1U << uii;
682 *patch_10_iter++ = S_CAST(AlleleCode, second_code);
683 *patch_10_iter++ = S_CAST(AlleleCode, first_code);
684 }
685 }
686 }
687 geno_write_word |= (cur_geno << (uii * 2));
688 }
689 genoarr[widx] = geno_write_word;
690 if (phasepresent_bytes) {
691 const uintptr_t het_1_word = geno_write_word & (~(geno_write_word >> 1)) & kMask5555;
692 Halfword het_hw = het_2_hw | PackWordToHalfword(het_1_word);
693 phasepresent_alias[widx] &= het_hw;
694 }
695 if (phaseinfo_alias) {
696 phaseinfo_alias[widx] = phaseinfo_write_hw;
697 }
698 }
699 }
700
701 static inline uint32_t BiallelicDosage16Halfdist(uint32_t dosage_int) {
702 const uint32_t dosage_int_rem = dosage_int & 16383;
703 return abs_i32(S_CAST(int32_t, dosage_int_rem) - 8192);
704 }
705
706 void FloatsToDosage16(const float* floatarr, uint32_t sample_ct, uint32_t hard_call_halfdist, uintptr_t* genoarr, uintptr_t* dosage_present, uint16_t* dosage_main, uint32_t* dosage_ct_ptr) {
707 const uint32_t word_ct_m1 = (sample_ct - 1) / kBitsPerWordD2;
708 const float* read_iter = floatarr;
709 Halfword* dosage_present_alias = R_CAST(Halfword*, dosage_present);
710 uint16_t* dosage_main_iter = dosage_main;
711 uint32_t subgroup_len = kBitsPerWordD2;
712 for (uint32_t widx = 0; ; ++widx) {
713 if (widx >= word_ct_m1) {
714 if (widx > word_ct_m1) {
715 if (widx % 2) {
716 dosage_present_alias[widx] = 0;
717 }
718 break;
719 }
720 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
721 }
722 uintptr_t geno_word = 0;
723 uint32_t dosage_present_hw = 0;
724 for (uint32_t sample_idx_lowbits = 0; sample_idx_lowbits != subgroup_len; ++sample_idx_lowbits) {
725 // 0..2 -> 0..32768
726 const float fxx = (*read_iter++) * 16384 + 0.5;
727 uintptr_t cur_geno = 3;
728 if ((fxx >= 0.0) && (fxx < 32769)) {
729 uint32_t dosage_int = S_CAST(int32_t, fxx);
730 const uint32_t cur_halfdist = BiallelicDosage16Halfdist(dosage_int);
731 if (cur_halfdist >= hard_call_halfdist) {
732 cur_geno = (dosage_int + (8192 * k1LU)) / 16384;
733 }
734 if (cur_halfdist != 8192) {
735 dosage_present_hw |= 1U << sample_idx_lowbits;
736 *dosage_main_iter++ = dosage_int;
737 }
738 }
739 geno_word |= cur_geno << (2 * sample_idx_lowbits);
740 }
741 genoarr[widx] = geno_word;
742 dosage_present_alias[widx] = dosage_present_hw;
743 }
744 *dosage_ct_ptr = dosage_main_iter - dosage_main;
745 }
746
747 void DoublesToDosage16(const double* doublearr, uint32_t sample_ct, uint32_t hard_call_halfdist, uintptr_t* genoarr, uintptr_t* dosage_present, uint16_t* dosage_main, uint32_t* dosage_ct_ptr) {
748 const uint32_t word_ct_m1 = (sample_ct - 1) / kBitsPerWordD2;
749 const double* read_iter = doublearr;
750 Halfword* dosage_present_alias = R_CAST(Halfword*, dosage_present);
751 uint16_t* dosage_main_iter = dosage_main;
752 uint32_t subgroup_len = kBitsPerWordD2;
753 for (uint32_t widx = 0; ; ++widx) {
754 if (widx >= word_ct_m1) {
755 if (widx > word_ct_m1) {
756 if (widx % 2) {
757 dosage_present_alias[widx] = 0;
758 }
759 break;
760 }
761 subgroup_len = ModNz(sample_ct, kBitsPerWordD2);
762 }
763 uintptr_t geno_word = 0;
764 uint32_t dosage_present_hw = 0;
765 for (uint32_t sample_idx_lowbits = 0; sample_idx_lowbits != subgroup_len; ++sample_idx_lowbits) {
766 // 0..2 -> 0..32768
767 const double dxx = (*read_iter++) * 16384 + 0.5;
768 uintptr_t cur_geno = 3;
769 if ((dxx >= 0.0) && (dxx < 32769)) {
770 uint32_t dosage_int = S_CAST(int32_t, dxx);
771 const uint32_t cur_halfdist = BiallelicDosage16Halfdist(dosage_int);
772 if (cur_halfdist >= hard_call_halfdist) {
773 cur_geno = (dosage_int + (8192 * k1LU)) / 16384;
774 }
775 if (cur_halfdist != 8192) {
776 dosage_present_hw |= 1U << sample_idx_lowbits;
777 *dosage_main_iter++ = dosage_int;
778 }
779 }
780 geno_word |= cur_geno << (2 * sample_idx_lowbits);
781 }
782 genoarr[widx] = geno_word;
783 dosage_present_alias[widx] = dosage_present_hw;
784 }
785 *dosage_ct_ptr = dosage_main_iter - dosage_main;
786 }
787
788 #ifdef __cplusplus
789 } // namespace plink2
790 #endif