| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 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 |
|
| 18 |
#include "pgenlib_misc.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
namespace plink2 { |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifndef NDEBUG |
| 25 |
char* g_pgl_errbuf = nullptr; |
| 26 |
char* g_pgl_errbuf_write_iter = nullptr; |
| 27 |
char* g_pgl_errbuf_end = nullptr; |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifdef USE_AVX2 |
| 31 |
void CopyNyparrNonemptySubset(const uintptr_t* __restrict raw_nyparr, const uintptr_t* __restrict subset_mask, uint32_t raw_nyparr_entry_ct, uint32_t subset_entry_ct, uintptr_t* __restrict output_nyparr) { |
| 32 |
if (subset_entry_ct == raw_nyparr_entry_ct) { |
| 33 |
memcpy(output_nyparr, raw_nyparr, DivUp(subset_entry_ct, kBitsPerWordD2) * sizeof(intptr_t)); |
| 34 |
ZeroTrailingNyps(subset_entry_ct, output_nyparr); |
| 35 |
return; |
| 36 |
} |
| 37 |
assert(subset_entry_ct); |
| 38 |
uintptr_t cur_output_word = 0; |
| 39 |
|
| 40 |
uintptr_t* output_nyparr_iter = output_nyparr; |
| 41 |
|
| 42 |
uintptr_t* output_nyparr_last = &(output_nyparr[subset_entry_ct / kBitsPerWordD2]); |
| 43 |
const uint32_t word_write_shift_end = 2 * (subset_entry_ct % kBitsPerWordD2); |
| 44 |
uint32_t word_write_shift = 0; |
| 45 |
for (uint32_t subset_mask_widx = 0; ; ++subset_mask_widx) { |
| 46 |
const uintptr_t cur_include_word = subset_mask[subset_mask_widx]; |
| 47 |
if (cur_include_word) { |
| 48 |
uint32_t cur_include_halfword = S_CAST(Halfword, cur_include_word); |
| 49 |
for (uint32_t wordhalf_idx = 0; ; ++wordhalf_idx) { |
| 50 |
if (cur_include_halfword) { |
| 51 |
uintptr_t extracted_bits = raw_nyparr[subset_mask_widx * 2 + wordhalf_idx]; |
| 52 |
uint32_t set_bit_ct = kBitsPerWord; |
| 53 |
if (cur_include_halfword != UINT32_MAX) { |
| 54 |
const uintptr_t pext_mask = 3 * UnpackHalfwordToWord(cur_include_halfword); |
| 55 |
extracted_bits = _pext_u64(extracted_bits, pext_mask); |
| 56 |
set_bit_ct = PopcountWord(pext_mask); |
| 57 |
} |
| 58 |
cur_output_word |= extracted_bits << word_write_shift; |
| 59 |
word_write_shift += set_bit_ct; |
| 60 |
if (word_write_shift >= kBitsPerWord) { |
| 61 |
*output_nyparr_iter++ = cur_output_word; |
| 62 |
word_write_shift -= kBitsPerWord; |
| 63 |
cur_output_word = 0; |
| 64 |
if (word_write_shift) { |
| 65 |
cur_output_word = extracted_bits >> (set_bit_ct - word_write_shift); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
if (wordhalf_idx) { |
| 70 |
break; |
| 71 |
} |
| 72 |
cur_include_halfword = cur_include_word >> kBitsPerWordD2; |
| 73 |
} |
| 74 |
if (output_nyparr_iter == output_nyparr_last) { |
| 75 |
if (word_write_shift == word_write_shift_end) { |
| 76 |
if (word_write_shift_end) { |
| 77 |
*output_nyparr_last = cur_output_word; |
| 78 |
} |
| 79 |
return; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// bit_idx_start assumed to be < kBitsPerWord |
| 87 |
void CopyGenomatchSubset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genoarr, uintptr_t match_word, uint32_t write_bit_idx_start, uint32_t bit_ct, void* __restrict output) { |
| 88 |
const uint32_t bit_idx_end = bit_ct + write_bit_idx_start; |
| 89 |
const uint32_t bit_idx_end_lowbits = bit_idx_end % kBitsPerWord; |
| 90 |
const Halfword* raw_bitarr_alias = DowncastKWToHW(raw_bitarr); |
| 91 |
unsigned char* output_biter = S_CAST(unsigned char*, output); |
| 92 |
unsigned char* output_last = &(output_biter[(bit_idx_end / kBitsPerWord) * kBytesPerWord]); |
| 93 |
uintptr_t cur_output_word = 0; |
| 94 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 95 |
uint32_t write_idx_lowbits = write_bit_idx_start; |
| 96 |
while ((output_biter != output_last) || (write_idx_lowbits != bit_idx_end_lowbits)) { |
| 97 |
uintptr_t cur_mask_word; |
| 98 |
// sparse genoarr optimization |
| 99 |
// guaranteed to terminate since there's at least one more set bit |
| 100 |
do { |
| 101 |
// todo: try reading two genoarr words at a time. would need to be very |
| 102 |
// careful with the possible trailing word, though. |
| 103 |
// more important to optimize this function now that regular phased-call |
| 104 |
// handling code is using it. |
| 105 |
cur_mask_word = genoarr[++read_widx] ^ match_word; |
| 106 |
cur_mask_word = (~(cur_mask_word | (cur_mask_word >> 1))) & kMask5555; |
| 107 |
} while (!cur_mask_word); |
| 108 |
uintptr_t extracted_bits = raw_bitarr_alias[read_widx]; |
| 109 |
uint32_t set_bit_ct = kBitsPerWordD2; |
| 110 |
if (cur_mask_word != kMask5555) { |
| 111 |
const uintptr_t cur_mask_hw = PackWordToHalfword(cur_mask_word); |
| 112 |
set_bit_ct = PopcountWord(cur_mask_word); |
| 113 |
extracted_bits = _pext_u64(extracted_bits, cur_mask_hw); |
| 114 |
} |
| 115 |
cur_output_word |= extracted_bits << write_idx_lowbits; |
| 116 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct; |
| 117 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 118 |
AppendW(cur_output_word, &output_biter); |
| 119 |
// ...and these are the bits that fell off |
| 120 |
// impossible for write_idx_lowbits to be zero here |
| 121 |
cur_output_word = extracted_bits >> (kBitsPerWord - write_idx_lowbits); |
| 122 |
} |
| 123 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 124 |
} |
| 125 |
if (write_idx_lowbits) { |
| 126 |
CopyToUnalignedW(output_biter, &cur_output_word); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Variant of ExpandBytearr() which is based off a target 2-bit value instead |
| 131 |
// of single expand_mask bits. expand_size must be the number of instances of |
| 132 |
// the target value in genovec. |
| 133 |
void ExpandBytearrFromGenoarr(const void* __restrict compact_bitarr, const uintptr_t* __restrict genoarr, uintptr_t match_word, uint32_t genoword_ct, uint32_t expand_size, uint32_t read_start_bit, uintptr_t* __restrict target) { |
| 134 |
const uint32_t expand_sizex_m1 = expand_size + read_start_bit - 1; |
| 135 |
const uint32_t leading_byte_ct = 1 + (expand_sizex_m1 % kBitsPerWord) / CHAR_BIT; |
| 136 |
const uint32_t genoword_ct_m1 = genoword_ct - 1; |
| 137 |
uintptr_t compact_word = SubwordLoad(compact_bitarr, leading_byte_ct) >> read_start_bit; |
| 138 |
const unsigned char* compact_bitarr_biter = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 139 |
uint32_t compact_idx_lowbits = read_start_bit + CHAR_BIT * (sizeof(intptr_t) - leading_byte_ct); |
| 140 |
for (uint32_t widx = 0; ; widx += 2) { |
| 141 |
uintptr_t mask_word; |
| 142 |
if (widx >= genoword_ct_m1) { |
| 143 |
if (widx > genoword_ct_m1) { |
| 144 |
return; |
| 145 |
} |
| 146 |
mask_word = 0; |
| 147 |
} else { |
| 148 |
const uintptr_t geno_word1 = genoarr[widx + 1] ^ match_word; |
| 149 |
mask_word = PackWordToHalfwordMask5555(~(geno_word1 | (geno_word1 >> 1))); |
| 150 |
mask_word = mask_word << 32; |
| 151 |
} |
| 152 |
const uintptr_t geno_word0 = genoarr[widx] ^ match_word; |
| 153 |
mask_word |= PackWordToHalfwordMask5555(~(geno_word0 | (geno_word0 >> 1))); |
| 154 |
uintptr_t write_word = 0; |
| 155 |
if (mask_word) { |
| 156 |
const uint32_t mask_set_ct = PopcountWord(mask_word); |
| 157 |
uint32_t next_compact_idx_lowbits = compact_idx_lowbits + mask_set_ct; |
| 158 |
if (next_compact_idx_lowbits <= kBitsPerWord) { |
| 159 |
write_word = _pdep_u64(compact_word, mask_word); |
| 160 |
if (mask_set_ct != kBitsPerWord) { |
| 161 |
compact_word = compact_word >> mask_set_ct; |
| 162 |
} else { |
| 163 |
// avoid nasal demons |
| 164 |
compact_word = 0; |
| 165 |
} |
| 166 |
} else { |
| 167 |
uintptr_t next_compact_word; |
| 168 |
CopyFromUnalignedIncrW(&next_compact_word, &compact_bitarr_biter); |
| 169 |
next_compact_idx_lowbits -= kBitsPerWord; |
| 170 |
compact_word |= next_compact_word << (kBitsPerWord - compact_idx_lowbits); |
| 171 |
write_word = _pdep_u64(compact_word, mask_word); |
| 172 |
if (next_compact_idx_lowbits != kBitsPerWord) { |
| 173 |
compact_word = next_compact_word >> next_compact_idx_lowbits; |
| 174 |
} else { |
| 175 |
compact_word = 0; |
| 176 |
} |
| 177 |
} |
| 178 |
compact_idx_lowbits = next_compact_idx_lowbits; |
| 179 |
} |
| 180 |
target[widx / 2] = write_word; |
| 181 |
} |
| 182 |
} |
| 183 |
#else // !USE_AVX2 |
| 184 |
void CopyNyparrNonemptySubset(const uintptr_t* __restrict raw_nyparr, const uintptr_t* __restrict subset_mask, uint32_t raw_nyparr_entry_ct, uint32_t subset_entry_ct, uintptr_t* __restrict output_nyparr) { |
| 185 |
if (subset_entry_ct == raw_nyparr_entry_ct) { |
| 186 |
// subset_mask may be nullptr in this case |
| 187 |
memcpy(output_nyparr, raw_nyparr, DivUp(subset_entry_ct, kBitsPerWordD2) * sizeof(intptr_t)); |
| 188 |
ZeroTrailingNyps(subset_entry_ct, output_nyparr); |
| 189 |
return; |
| 190 |
} |
| 191 |
assert(subset_entry_ct); |
| 192 |
assert(raw_nyparr_entry_ct >= subset_entry_ct); |
| 193 |
uintptr_t cur_output_word = 0; |
| 194 |
|
| 195 |
uintptr_t* output_nyparr_iter = output_nyparr; |
| 196 |
|
| 197 |
uintptr_t* output_nyparr_last = &(output_nyparr[subset_entry_ct / kBitsPerWordD2]); |
| 198 |
const uint32_t word_write_halfshift_end = subset_entry_ct % kBitsPerWordD2; |
| 199 |
uint32_t word_write_halfshift = 0; |
| 200 |
// if <= 2/3-filled, use sparse copy algorithm |
| 201 |
// (tried CopyBitarrSubset() approach, that actually worsened things) |
| 202 |
if (subset_entry_ct * (3 * k1LU) <= raw_nyparr_entry_ct * (2 * k1LU)) { |
| 203 |
for (uint32_t subset_mask_widx = 0; ; ++subset_mask_widx) { |
| 204 |
const uintptr_t cur_include_word = subset_mask[subset_mask_widx]; |
| 205 |
if (cur_include_word) { |
| 206 |
uint32_t cur_include_halfword = S_CAST(Halfword, cur_include_word); |
| 207 |
for (uint32_t wordhalf_idx = 0; ; ++wordhalf_idx) { |
| 208 |
if (cur_include_halfword) { |
| 209 |
uintptr_t raw_nyparr_word = raw_nyparr[subset_mask_widx * 2 + wordhalf_idx]; |
| 210 |
do { |
| 211 |
uint32_t rqa_idx_lowbits = ctzu32(cur_include_halfword); |
| 212 |
cur_output_word |= ((raw_nyparr_word >> (rqa_idx_lowbits * 2)) & 3) << (word_write_halfshift * 2); |
| 213 |
if (++word_write_halfshift == kBitsPerWordD2) { |
| 214 |
*output_nyparr_iter++ = cur_output_word; |
| 215 |
word_write_halfshift = 0; |
| 216 |
cur_output_word = 0; |
| 217 |
} |
| 218 |
cur_include_halfword &= cur_include_halfword - 1; |
| 219 |
} while (cur_include_halfword); |
| 220 |
} |
| 221 |
if (wordhalf_idx) { |
| 222 |
break; |
| 223 |
} |
| 224 |
cur_include_halfword = cur_include_word >> kBitsPerWordD2; |
| 225 |
} |
| 226 |
if (output_nyparr_iter == output_nyparr_last) { |
| 227 |
if (word_write_halfshift == word_write_halfshift_end) { |
| 228 |
if (word_write_halfshift_end) { |
| 229 |
*output_nyparr_last = cur_output_word; |
| 230 |
} |
| 231 |
return; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
// blocked copy |
| 238 |
const uintptr_t* raw_nyparr_iter = raw_nyparr; |
| 239 |
for (; ; ++subset_mask) { |
| 240 |
const uintptr_t cur_include_word = *subset_mask; |
| 241 |
uintptr_t cur_include_halfword = S_CAST(Halfword, cur_include_word); |
| 242 |
for (uint32_t wordhalf_idx = 0; ; ++wordhalf_idx) { |
| 243 |
uintptr_t raw_nyparr_word = *raw_nyparr_iter++; |
| 244 |
while (cur_include_halfword) { |
| 245 |
const uint32_t rqa_idx_lowbits = ctzw(cur_include_halfword); |
| 246 |
|
| 247 |
// TAOCP, 7.1.3, (43). |
| 248 |
const uintptr_t bottom_block_remover = (cur_include_halfword | (cur_include_halfword - 1)) + 1; |
| 249 |
|
| 250 |
const uintptr_t raw_nyparr_curblock_unmasked = raw_nyparr_word >> (rqa_idx_lowbits * 2); |
| 251 |
const uint32_t rqa_block_len = ctzw(bottom_block_remover) - rqa_idx_lowbits; |
| 252 |
const uint32_t block_len_limit = kBitsPerWordD2 - word_write_halfshift; |
| 253 |
cur_output_word |= raw_nyparr_curblock_unmasked << (2 * word_write_halfshift); |
| 254 |
if (rqa_block_len < block_len_limit) { |
| 255 |
word_write_halfshift += rqa_block_len; |
| 256 |
cur_output_word = bzhi(cur_output_word, word_write_halfshift * 2); |
| 257 |
} else { |
| 258 |
// no need to mask, extra bits vanish off the high end |
| 259 |
*output_nyparr_iter++ = cur_output_word; |
| 260 |
word_write_halfshift = rqa_block_len - block_len_limit; |
| 261 |
if (word_write_halfshift) { |
| 262 |
cur_output_word = bzhi(raw_nyparr_curblock_unmasked >> (2 * block_len_limit), 2 * word_write_halfshift); |
| 263 |
} else { |
| 264 |
// avoid potential right-shift-[word length] |
| 265 |
cur_output_word = 0; |
| 266 |
} |
| 267 |
} |
| 268 |
cur_include_halfword &= bottom_block_remover; |
| 269 |
} |
| 270 |
if (wordhalf_idx) { |
| 271 |
break; |
| 272 |
} |
| 273 |
cur_include_halfword = cur_include_word >> kBitsPerWordD2; |
| 274 |
} |
| 275 |
if (output_nyparr_iter == output_nyparr_last) { |
| 276 |
if (word_write_halfshift == word_write_halfshift_end) { |
| 277 |
if (word_write_halfshift_end) { |
| 278 |
*output_nyparr_last = cur_output_word; |
| 279 |
} |
| 280 |
return; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void CopyGenomatchSubset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genovec, uintptr_t match_word, uint32_t write_bit_idx_start, uint32_t bit_ct, void* __restrict output) { |
| 287 |
const uint32_t bit_idx_end = write_bit_idx_start + bit_ct; |
| 288 |
const uint32_t bit_idx_end_lowbits = bit_idx_end % kBitsPerWord; |
| 289 |
const Halfword* raw_bitarr_alias = DowncastKWToHW(raw_bitarr); |
| 290 |
unsigned char* output_biter = S_CAST(unsigned char*, output); |
| 291 |
unsigned char* output_last = &(output_biter[(bit_idx_end / kBitsPerWord) * kBytesPerWord]); |
| 292 |
uintptr_t cur_output_word = 0; |
| 293 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 294 |
uint32_t write_idx_lowbits = write_bit_idx_start; |
| 295 |
while ((output_biter != output_last) || (write_idx_lowbits != bit_idx_end_lowbits)) { |
| 296 |
uintptr_t geno_word; |
| 297 |
// sparse genovec optimization |
| 298 |
// guaranteed to terminate since there's at least one more set bit |
| 299 |
do { |
| 300 |
geno_word = genovec[++read_widx] ^ match_word; |
| 301 |
geno_word = (~(geno_word | (geno_word >> 1))) & kMask5555; |
| 302 |
} while (!geno_word); |
| 303 |
// screw it, just iterate over set bits |
| 304 |
const uint32_t cur_halfword = raw_bitarr_alias[read_widx]; |
| 305 |
do { |
| 306 |
const uint32_t sample_idx_lowbits = ctzw(geno_word) / 2; |
| 307 |
cur_output_word |= S_CAST(uintptr_t, (cur_halfword >> sample_idx_lowbits) & k1LU) << write_idx_lowbits; |
| 308 |
if (++write_idx_lowbits == kBitsPerWord) { |
| 309 |
AppendW(cur_output_word, &output_biter); |
| 310 |
cur_output_word = 0; |
| 311 |
write_idx_lowbits = 0; |
| 312 |
} |
| 313 |
geno_word &= geno_word - 1; |
| 314 |
} while (geno_word); |
| 315 |
} |
| 316 |
if (write_idx_lowbits) { |
| 317 |
CopyToUnalignedW(output_biter, &cur_output_word); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
void ExpandBytearrFromGenoarr(const void* __restrict compact_bitarr, const uintptr_t* __restrict genoarr, uintptr_t match_word, uint32_t genoword_ct, uint32_t expand_size, uint32_t read_start_bit, uintptr_t* __restrict target) { |
| 322 |
Halfword* target_alias = DowncastWToHW(target); |
| 323 |
ZeroHwArr(RoundUpPow2(genoword_ct, 2), target_alias); |
| 324 |
const unsigned char* compact_bitarr_alias = S_CAST(const unsigned char*, compact_bitarr); |
| 325 |
const uint32_t expand_sizex_m1 = expand_size + read_start_bit - 1; |
| 326 |
const uint32_t compact_widx_last = expand_sizex_m1 / kBitsPerWord; |
| 327 |
uint32_t compact_idx_lowbits = read_start_bit; |
| 328 |
uint32_t loop_len = kBitsPerWord; |
| 329 |
uintptr_t write_hwidx = 0; |
| 330 |
uintptr_t genomatch_bits = genoarr[0] ^ match_word; |
| 331 |
genomatch_bits = (~(genomatch_bits | (genomatch_bits >> 1))) & kMask5555; |
| 332 |
for (uint32_t compact_widx = 0; ; ++compact_widx) { |
| 333 |
uintptr_t compact_word; |
| 334 |
if (compact_widx >= compact_widx_last) { |
| 335 |
if (compact_widx > compact_widx_last) { |
| 336 |
return; |
| 337 |
} |
| 338 |
loop_len = 1 + (expand_sizex_m1 % kBitsPerWord); |
| 339 |
// avoid possible segfault |
| 340 |
compact_word = SubwordLoad(&(compact_bitarr_alias[compact_widx * kBytesPerWord]), DivUp(loop_len, CHAR_BIT)); |
| 341 |
} else { |
| 342 |
CopyFromUnalignedOffsetW(&compact_word, compact_bitarr_alias, compact_widx); |
| 343 |
} |
| 344 |
for (; compact_idx_lowbits != loop_len; ++compact_idx_lowbits) { |
| 345 |
while (!genomatch_bits) { |
| 346 |
genomatch_bits = genoarr[++write_hwidx] ^ match_word; |
| 347 |
genomatch_bits = (~(genomatch_bits | (genomatch_bits >> 1))) & kMask5555; |
| 348 |
} |
| 349 |
if (compact_word & (k1LU << compact_idx_lowbits)) { |
| 350 |
const uint32_t lowbit_idx = ctzw(genomatch_bits); |
| 351 |
target_alias[write_hwidx] |= 1U << (lowbit_idx / 2); |
| 352 |
} |
| 353 |
genomatch_bits &= genomatch_bits - 1; |
| 354 |
} |
| 355 |
compact_idx_lowbits = 0; |
| 356 |
} |
| 357 |
} |
| 358 |
#endif |
| 359 |
|
| 360 |
// Harley-Seal algorithm only works for bitarrays, not nyparrays, so don't |
| 361 |
// add an AVX2 specialization here. |
| 362 |
// ...unless something like the interleaved_vec strategy is used? hmm. should |
| 363 |
// test this on basic frequency counter. |
| 364 |
/* |
| 365 |
void Count2FreqVec3(const VecW* geno_vvec, uint32_t vec_ct, uint32_t* __restrict alt1_plus_bothset_ctp, uint32_t* __restrict bothset_ctp) { |
| 366 |
assert(!(vec_ct % 3)); |
| 367 |
// Increments bothset_ct by the number of 0b11 in the current block, and |
| 368 |
// alt1_ct by twice the number of 0b10 plus the number of 0b01. |
| 369 |
const VecW m1 = VCONST_W(kMask5555); |
| 370 |
const VecW m2 = VCONST_W(kMask3333); |
| 371 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 372 |
const VecW* geno_vvec_iter = geno_vvec; |
| 373 |
uint32_t alt1_plus_bothset_ct = 0; |
| 374 |
uint32_t bothset_ct = 0; |
| 375 |
|
| 376 |
while (1) { |
| 377 |
UniVec acc_alt1_plus_bothset; |
| 378 |
UniVec acc_bothset; |
| 379 |
acc_alt1_plus_bothset.vw = vecw_setzero(); |
| 380 |
acc_bothset.vw = vecw_setzero(); |
| 381 |
const VecW* geno_vvec_stop; |
| 382 |
if (vec_ct < 30) { |
| 383 |
if (!vec_ct) { |
| 384 |
*alt1_plus_bothset_ctp = alt1_plus_bothset_ct; |
| 385 |
*bothset_ctp = bothset_ct; |
| 386 |
return; |
| 387 |
} |
| 388 |
geno_vvec_stop = &(geno_vvec_iter[vec_ct]); |
| 389 |
vec_ct = 0; |
| 390 |
} else { |
| 391 |
geno_vvec_stop = &(geno_vvec_iter[30]); |
| 392 |
vec_ct -= 30; |
| 393 |
} |
| 394 |
do { |
| 395 |
VecW cur_geno_vword1 = *geno_vvec_iter++; |
| 396 |
// process first two vwords simultaneously to minimize linear dependence |
| 397 |
VecW cur_geno_vword2 = *geno_vvec_iter++; |
| 398 |
VecW cur_geno_vword_low_lshifted1 = vecw_slli(cur_geno_vword1 & m1, 1); |
| 399 |
VecW cur_geno_vword_low_lshifted2 = vecw_slli(cur_geno_vword2 & m1, 1); |
| 400 |
|
| 401 |
// 00 -> 00; 01 -> 01; 10 -> 10; 11 -> 01 |
| 402 |
// note that _mm_andnot_si128 flips the *first* argument before the AND |
| 403 |
// operation. |
| 404 |
VecW alt1_plus_bothset1 = vecw_and_notfirst(cur_geno_vword_low_lshifted1, cur_geno_vword1); |
| 405 |
VecW alt1_plus_bothset2 = vecw_and_notfirst(cur_geno_vword_low_lshifted2, cur_geno_vword2); |
| 406 |
|
| 407 |
VecW bothset1 = vecw_srli(cur_geno_vword_low_lshifted1 & cur_geno_vword1, 1); |
| 408 |
VecW bothset2 = vecw_srli(cur_geno_vword_low_lshifted2 & cur_geno_vword2, 1); |
| 409 |
|
| 410 |
cur_geno_vword1 = *geno_vvec_iter++; |
| 411 |
alt1_plus_bothset1 = (alt1_plus_bothset1 & m2) + (vecw_srli(alt1_plus_bothset1, 2) & m2); |
| 412 |
bothset2 = bothset1 + bothset2; |
| 413 |
alt1_plus_bothset2 = (alt1_plus_bothset2 & m2) + (vecw_srli(alt1_plus_bothset2, 2) & m2); |
| 414 |
cur_geno_vword_low_lshifted1 = vecw_slli(cur_geno_vword1 & m1, 1); |
| 415 |
|
| 416 |
alt1_plus_bothset2 = alt1_plus_bothset1 + alt1_plus_bothset2; |
| 417 |
// alt1_plus_bothset2 now contains 4-bit values from 0-8, while bothset2 |
| 418 |
// contains 2-bit values from 0-2 |
| 419 |
// (todo: check whether this is faster if we use double_bothsetx |
| 420 |
// variables instead of bothset1/bothset2) |
| 421 |
bothset1 = vecw_srli(cur_geno_vword_low_lshifted1 & cur_geno_vword1, 1); |
| 422 |
alt1_plus_bothset1 = vecw_and_notfirst(cur_geno_vword_low_lshifted1, cur_geno_vword1); |
| 423 |
bothset2 = bothset1 + bothset2; |
| 424 |
alt1_plus_bothset1 = (alt1_plus_bothset1 & m2) + (vecw_srli(alt1_plus_bothset1, 2) & m2); |
| 425 |
|
| 426 |
bothset2 = (bothset2 & m2) + (vecw_srli(bothset2, 2) & m2); |
| 427 |
alt1_plus_bothset2 = alt1_plus_bothset1 + alt1_plus_bothset2; |
| 428 |
// alt1_plus_bothset2 now contains 4-bit values from 0-12, while bothset2 |
| 429 |
// contains 4-bit values from 0-6. aggregate both into 8-bit values. |
| 430 |
bothset2 = (bothset2 & m4) + (vecw_srli(bothset2, 4) & m4); |
| 431 |
alt1_plus_bothset2 = (alt1_plus_bothset2 & m4) + (vecw_srli(alt1_plus_bothset2, 4) & m4); |
| 432 |
|
| 433 |
acc_bothset.vw = acc_bothset.vw + bothset2; |
| 434 |
acc_alt1_plus_bothset.vw = acc_alt1_plus_bothset.vw + alt1_plus_bothset2; |
| 435 |
} while (geno_vvec_iter < geno_vvec_stop); |
| 436 |
const VecW m8 = VCONST_W(kMask00FF); |
| 437 |
acc_bothset.vw = (acc_bothset.vw + vecw_srli(acc_bothset.vw, 8)) & m8; |
| 438 |
acc_alt1_plus_bothset.vw = (acc_alt1_plus_bothset.vw & m8) + (vecw_srli(acc_alt1_plus_bothset.vw, 8) & m8); |
| 439 |
bothset_ct += UniVecHsum16(acc_bothset); |
| 440 |
alt1_plus_bothset_ct += UniVecHsum16(acc_alt1_plus_bothset); |
| 441 |
} |
| 442 |
} |
| 443 |
*/ |
| 444 |
|
| 445 |
// todo: benchmark against general-purpose counter |
| 446 |
void Count12Vec6(const VecW* geno_vvec, uint32_t vec_ct, uint32_t* __restrict raw_01_ctp, uint32_t* __restrict raw_both_ctp) { |
| 447 |
assert(!(vec_ct % 6)); |
| 448 |
const VecW m1 = VCONST_W(kMask5555); |
| 449 |
const VecW m2 = VCONST_W(kMask3333); |
| 450 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 451 |
const VecW* geno_vvec_iter = geno_vvec; |
| 452 |
VecW acc_01 = vecw_setzero(); |
| 453 |
VecW acc_both = vecw_setzero(); |
| 454 |
uintptr_t cur_incr = 60; |
| 455 |
for (; ; vec_ct -= cur_incr) { |
| 456 |
if (vec_ct < 60) { |
| 457 |
if (!vec_ct) { |
| 458 |
*raw_01_ctp = HsumW(acc_01); |
| 459 |
*raw_both_ctp = HsumW(acc_both); |
| 460 |
return; |
| 461 |
} |
| 462 |
cur_incr = vec_ct; |
| 463 |
} |
| 464 |
VecW inner_acc_01 = vecw_setzero(); |
| 465 |
VecW inner_acc_both = vecw_setzero(); |
| 466 |
const VecW* geno_vvec_stop = &(geno_vvec_iter[cur_incr]); |
| 467 |
do { |
| 468 |
VecW cur_vvec = *geno_vvec_iter++; |
| 469 |
VecW vvec_rshift = vecw_srli(cur_vvec, 1); |
| 470 |
VecW nyp_both = m1 & (cur_vvec ^ vvec_rshift); |
| 471 |
VecW nyp_01 = nyp_both & cur_vvec; |
| 472 |
|
| 473 |
cur_vvec = *geno_vvec_iter++; |
| 474 |
vvec_rshift = vecw_srli(cur_vvec, 1); |
| 475 |
VecW vvec_both = m1 & (cur_vvec ^ vvec_rshift); |
| 476 |
nyp_01 = nyp_01 + (vvec_both & cur_vvec); |
| 477 |
nyp_both = nyp_both + vvec_both; |
| 478 |
|
| 479 |
cur_vvec = *geno_vvec_iter++; |
| 480 |
vvec_rshift = vecw_srli(cur_vvec, 1); |
| 481 |
vvec_both = m1 & (cur_vvec ^ vvec_rshift); |
| 482 |
nyp_01 = nyp_01 + (vvec_both & cur_vvec); |
| 483 |
nyp_both = nyp_both + vvec_both; |
| 484 |
|
| 485 |
VecW nybble_01 = (nyp_01 & m2) + (vecw_srli(nyp_01, 2) & m2); |
| 486 |
VecW nybble_both = (nyp_both & m2) + (vecw_srli(nyp_both, 2) & m2); |
| 487 |
|
| 488 |
cur_vvec = *geno_vvec_iter++; |
| 489 |
vvec_rshift = vecw_srli(cur_vvec, 1); |
| 490 |
nyp_both = m1 & (cur_vvec ^ vvec_rshift); |
| 491 |
nyp_01 = nyp_both & cur_vvec; |
| 492 |
|
| 493 |
cur_vvec = *geno_vvec_iter++; |
| 494 |
vvec_rshift = vecw_srli(cur_vvec, 1); |
| 495 |
vvec_both = m1 & (cur_vvec ^ vvec_rshift); |
| 496 |
nyp_01 = nyp_01 + (vvec_both & cur_vvec); |
| 497 |
nyp_both = nyp_both + vvec_both; |
| 498 |
|
| 499 |
cur_vvec = *geno_vvec_iter++; |
| 500 |
vvec_rshift = vecw_srli(cur_vvec, 1); |
| 501 |
vvec_both = m1 & (cur_vvec ^ vvec_rshift); |
| 502 |
nyp_01 = nyp_01 + (vvec_both & cur_vvec); |
| 503 |
nyp_both = nyp_both + vvec_both; |
| 504 |
|
| 505 |
nybble_01 = nybble_01 + (nyp_01 & m2) + (vecw_srli(nyp_01, 2) & m2); |
| 506 |
nybble_both = nybble_both + (nyp_both & m2) + (vecw_srli(nyp_both, 2) & m2); |
| 507 |
|
| 508 |
inner_acc_01 = inner_acc_01 + (nybble_01 & m4) + (vecw_srli(nybble_01, 4) & m4); |
| 509 |
inner_acc_both = inner_acc_both + (nybble_both & m4) + (vecw_srli(nybble_both, 4) & m4); |
| 510 |
} while (geno_vvec_iter < geno_vvec_stop); |
| 511 |
const VecW m0 = vecw_setzero(); |
| 512 |
acc_01 = acc_01 + vecw_bytesum(inner_acc_01, m0); |
| 513 |
acc_both = acc_both + vecw_bytesum(inner_acc_both, m0); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
void GenovecCount12Unsafe(const uintptr_t* genovec, uint32_t sample_ct, uint32_t* __restrict raw_01_ctp, uint32_t* __restrict raw_10_ctp) { |
| 518 |
// assumes trailing bits of last genovec word are zeroed out. |
| 519 |
// sample_ct == 0 ok. |
| 520 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 521 |
uint32_t raw_01_ct; |
| 522 |
uint32_t raw_both_ct; |
| 523 |
uint32_t word_idx = sample_ctl2 - (sample_ctl2 % (6 * kWordsPerVec)); |
| 524 |
assert(IsVecAligned(genovec)); |
| 525 |
Count12Vec6(R_CAST(const VecW*, genovec), word_idx / kWordsPerVec, &raw_01_ct, &raw_both_ct); |
| 526 |
for (; word_idx != sample_ctl2; ++word_idx) { |
| 527 |
const uintptr_t cur_geno_word = genovec[word_idx]; |
| 528 |
const uintptr_t cur_rshift = cur_geno_word >> 1; |
| 529 |
const uintptr_t cur_both = kMask5555 & (cur_geno_word ^ cur_rshift); |
| 530 |
raw_01_ct += Popcount01Word(cur_both & cur_geno_word); |
| 531 |
raw_both_ct += Popcount01Word(cur_both); |
| 532 |
} |
| 533 |
*raw_01_ctp = raw_01_ct; |
| 534 |
*raw_10_ctp = raw_both_ct - raw_01_ct; |
| 535 |
} |
| 536 |
|
| 537 |
void Count3FreqVec6(const void* genoarr, uint32_t vec_ct, uint32_t* __restrict even_ctp, uint32_t* __restrict odd_ctp, uint32_t* __restrict bothset_ctp) { |
| 538 |
assert(!(vec_ct % 6)); |
| 539 |
// Sets even_ct to the number of set low bits in the current block, odd_ct to |
| 540 |
// the number of set high bits, and bothset_ct by the number of 0b11s. |
| 541 |
const VecW m1 = VCONST_W(kMask5555); |
| 542 |
const VecW m2 = VCONST_W(kMask3333); |
| 543 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 544 |
const unsigned char* geno_vvec_biter = S_CAST(const unsigned char*, genoarr); |
| 545 |
VecW acc_even = vecw_setzero(); |
| 546 |
VecW acc_odd = vecw_setzero(); |
| 547 |
VecW acc_bothset = vecw_setzero(); |
| 548 |
uintptr_t cur_incr = 60; |
| 549 |
for (; ; vec_ct -= cur_incr) { |
| 550 |
if (vec_ct < 60) { |
| 551 |
if (!vec_ct) { |
| 552 |
*even_ctp = HsumW(acc_even); |
| 553 |
*odd_ctp = HsumW(acc_odd); |
| 554 |
*bothset_ctp = HsumW(acc_bothset); |
| 555 |
return; |
| 556 |
} |
| 557 |
cur_incr = vec_ct; |
| 558 |
} |
| 559 |
VecW inner_acc_even = vecw_setzero(); |
| 560 |
VecW inner_acc_odd = vecw_setzero(); |
| 561 |
VecW inner_acc_bothset = vecw_setzero(); |
| 562 |
const unsigned char* geno_vvec_stop = &(geno_vvec_biter[cur_incr * kBytesPerVec]); |
| 563 |
do { |
| 564 |
// hmm, this seems to have more linear dependence than I'd want, but the |
| 565 |
// reorderings I tried just made the code harder to read without helping, |
| 566 |
// so I'll leave this alone |
| 567 |
VecW cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 568 |
geno_vvec_biter += kBytesPerVec; |
| 569 |
VecW odd1 = m1 & vecw_srli(cur_geno_vword, 1); |
| 570 |
VecW even1 = m1 & cur_geno_vword; |
| 571 |
VecW bothset1 = odd1 & cur_geno_vword; |
| 572 |
|
| 573 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 574 |
geno_vvec_biter += kBytesPerVec; |
| 575 |
VecW cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1); |
| 576 |
even1 = even1 + (m1 & cur_geno_vword); |
| 577 |
odd1 = odd1 + cur_geno_vword_high; |
| 578 |
bothset1 = bothset1 + (cur_geno_vword_high & cur_geno_vword); |
| 579 |
|
| 580 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 581 |
geno_vvec_biter += kBytesPerVec; |
| 582 |
cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1); |
| 583 |
even1 = even1 + (m1 & cur_geno_vword); |
| 584 |
odd1 = odd1 + cur_geno_vword_high; |
| 585 |
bothset1 = bothset1 + (cur_geno_vword_high & cur_geno_vword); |
| 586 |
|
| 587 |
even1 = (even1 & m2) + (vecw_srli(even1, 2) & m2); |
| 588 |
odd1 = (odd1 & m2) + (vecw_srli(odd1, 2) & m2); |
| 589 |
bothset1 = (bothset1 & m2) + (vecw_srli(bothset1, 2) & m2); |
| 590 |
|
| 591 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 592 |
geno_vvec_biter += kBytesPerVec; |
| 593 |
VecW odd2 = m1 & vecw_srli(cur_geno_vword, 1); |
| 594 |
VecW even2 = m1 & cur_geno_vword; |
| 595 |
VecW bothset2 = odd2 & cur_geno_vword; |
| 596 |
|
| 597 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 598 |
geno_vvec_biter += kBytesPerVec; |
| 599 |
cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1); |
| 600 |
even2 = even2 + (m1 & cur_geno_vword); |
| 601 |
odd2 = odd2 + cur_geno_vword_high; |
| 602 |
bothset2 = bothset2 + (cur_geno_vword_high & cur_geno_vword); |
| 603 |
|
| 604 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 605 |
geno_vvec_biter += kBytesPerVec; |
| 606 |
cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1); |
| 607 |
even2 = even2 + (m1 & cur_geno_vword); |
| 608 |
odd2 = odd2 + cur_geno_vword_high; |
| 609 |
bothset2 = bothset2 + (cur_geno_vword_high & cur_geno_vword); |
| 610 |
|
| 611 |
even1 = even1 + (even2 & m2) + (vecw_srli(even2, 2) & m2); |
| 612 |
odd1 = odd1 + (odd2 & m2) + (vecw_srli(odd2, 2) & m2); |
| 613 |
bothset1 = bothset1 + (bothset2 & m2) + (vecw_srli(bothset2, 2) & m2); |
| 614 |
// these now contain 4-bit values from 0-12 |
| 615 |
|
| 616 |
inner_acc_even = inner_acc_even + (even1 & m4) + (vecw_srli(even1, 4) & m4); |
| 617 |
inner_acc_odd = inner_acc_odd + (odd1 & m4) + (vecw_srli(odd1, 4) & m4); |
| 618 |
inner_acc_bothset = inner_acc_bothset + (bothset1 & m4) + (vecw_srli(bothset1, 4) & m4); |
| 619 |
} while (geno_vvec_biter < geno_vvec_stop); |
| 620 |
const VecW m0 = vecw_setzero(); |
| 621 |
acc_even = acc_even + vecw_bytesum(inner_acc_even, m0); |
| 622 |
acc_odd = acc_odd + vecw_bytesum(inner_acc_odd, m0); |
| 623 |
acc_bothset = acc_bothset + vecw_bytesum(inner_acc_bothset, m0); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
void FillInterleavedMaskVec(const uintptr_t* __restrict subset_mask, uint32_t base_vec_ct, uintptr_t* interleaved_mask_vec) { |
| 628 |
#ifdef USE_SSE2 |
| 629 |
// This is a cousin of github.com/KWillets/simd_interleave , which was |
| 630 |
// written in response to |
| 631 |
// https://lemire.me/blog/2018/01/09/how-fast-can-you-bit-interleave-32-bit-integers-simd-edition/ |
| 632 |
// AVX2 implementation takes ~40% less time than before, and SSE4.2 takes |
| 633 |
// ~65% less. This also avoids the Ryzen-screwing _pdep_u64()/_pext_u64() |
| 634 |
// operations. |
| 635 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 636 |
# ifdef USE_SHUFFLE8 |
| 637 |
const VecW lookup0 = vecw_setr8( |
| 638 |
0, 1, 4, 5, 16, 17, 20, 21, |
| 639 |
64, 65, 68, 69, 80, 81, 84, 85); |
| 640 |
const VecW lookup1 = vecw_slli(lookup0, 1); |
| 641 |
# else |
| 642 |
const VecW m1 = VCONST_W(kMask5555); |
| 643 |
const VecW m2 = VCONST_W(kMask3333); |
| 644 |
# endif |
| 645 |
const VecW* subset_mask_valias = R_CAST(const VecW*, subset_mask); |
| 646 |
VecW* interleaved_mask_valias = R_CAST(VecW*, interleaved_mask_vec); |
| 647 |
|
| 648 |
for (uint32_t vidx = 0; vidx != base_vec_ct; ++vidx) { |
| 649 |
// I'll assume the compiler can handle this register allocation job. |
| 650 |
VecW vec_lo; |
| 651 |
VecW vec_hi; |
| 652 |
vecw_lo_and_hi_nybbles(subset_mask_valias[vidx], m4, &vec_lo, &vec_hi); |
| 653 |
# ifdef USE_SHUFFLE8 |
| 654 |
vec_lo = vecw_shuffle8(lookup0, vec_lo); |
| 655 |
vec_hi = vecw_shuffle8(lookup1, vec_hi); |
| 656 |
# else |
| 657 |
vec_lo = (vec_lo | vecw_slli(vec_lo, 2)) & m2; |
| 658 |
vec_hi = (vec_hi | vecw_slli(vec_hi, 2)) & m2; |
| 659 |
vec_lo = (vec_lo | vecw_slli(vec_lo, 1)) & m1; |
| 660 |
vec_hi = (vec_hi | vecw_slli(vec_hi, 1)) & m1; |
| 661 |
vec_hi = vecw_slli(vec_hi, 1); |
| 662 |
# endif |
| 663 |
interleaved_mask_valias[vidx] = vec_lo | vec_hi; |
| 664 |
} |
| 665 |
#else // !USE_SSE2 |
| 666 |
for (uint32_t widx = 0; widx != base_vec_ct; ++widx) { |
| 667 |
const uintptr_t orig_word = subset_mask[widx]; |
| 668 |
uintptr_t ww_even = S_CAST(Halfword, orig_word); |
| 669 |
uintptr_t ww_odd = orig_word >> kBitsPerWordD2; |
| 670 |
ww_even = UnpackHalfwordToWord(ww_even); |
| 671 |
ww_odd = UnpackHalfwordToWord(ww_odd); |
| 672 |
interleaved_mask_vec[widx] = ww_even | (ww_odd << 1); |
| 673 |
} |
| 674 |
#endif |
| 675 |
} |
| 676 |
|
| 677 |
/* |
| 678 |
void GenovecAlleleCtsUnsafe(const uintptr_t* genovec, uint32_t sample_ct, uint32_t* __restrict allele_cts, uint32_t* __restrict bothset_ctp) { |
| 679 |
// assumes trailing bits of last genovec word are zeroed out. |
| 680 |
// sets allele_cts[0] to the number of observed ref alleles, and |
| 681 |
// allele_cts[1] to the number of observed alt1s. |
| 682 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 683 |
uint32_t word_idx = sample_ctl2 - (sample_ctl2 % (3 * kWordsPerVec)); |
| 684 |
uint32_t alt1_plus_bothset_ct; |
| 685 |
uint32_t bothset_ct; |
| 686 |
assert(IsVecAligned(genovec)); |
| 687 |
Count2FreqVec3(R_CAST(const VecW*, genovec), word_idx / kWordsPerVec, &alt1_plus_bothset_ct, &bothset_ct); |
| 688 |
for (; word_idx != sample_ctl2; ++word_idx) { |
| 689 |
const uintptr_t cur_geno_word = genovec[word_idx]; |
| 690 |
const uintptr_t cur_geno_word_low_lshifted = (cur_geno_word & kMask5555) << 1; |
| 691 |
alt1_plus_bothset_ct += NypsumWord((~cur_geno_word_low_lshifted) & cur_geno_word); |
| 692 |
bothset_ct += NypsumWord(cur_geno_word_low_lshifted & cur_geno_word); |
| 693 |
} |
| 694 |
const uint32_t alt1_ct = alt1_plus_bothset_ct - bothset_ct; |
| 695 |
allele_cts[0] = (sample_ct - bothset_ct) * 2 - alt1_ct; |
| 696 |
allele_cts[1] = alt1_ct; |
| 697 |
*bothset_ctp = bothset_ct; |
| 698 |
} |
| 699 |
*/ |
| 700 |
|
| 701 |
void GenoarrCountFreqsUnsafe(const uintptr_t* genoarr, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) { |
| 702 |
// fills genocounts[0] with the number of 00s, genocounts[1] with the number |
| 703 |
// of 01s, etc. |
| 704 |
// assumes trailing bits of last genoarr word are zeroed out. |
| 705 |
// sample_ct == 0 ok. |
| 706 |
// no longer requires vector-alignment. |
| 707 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 708 |
uint32_t even_ct; |
| 709 |
uint32_t odd_ct; |
| 710 |
uint32_t bothset_ct; |
| 711 |
uint32_t word_idx = sample_ctl2 - (sample_ctl2 % (6 * kWordsPerVec)); |
| 712 |
Count3FreqVec6(genoarr, word_idx / kWordsPerVec, &even_ct, &odd_ct, &bothset_ct); |
| 713 |
for (; word_idx != sample_ctl2; ++word_idx) { |
| 714 |
const uintptr_t cur_geno_word = genoarr[word_idx]; |
| 715 |
const uintptr_t cur_geno_word_high = kMask5555 & (cur_geno_word >> 1); |
| 716 |
even_ct += Popcount01Word(cur_geno_word & kMask5555); |
| 717 |
odd_ct += Popcount01Word(cur_geno_word_high); |
| 718 |
bothset_ct += Popcount01Word(cur_geno_word & cur_geno_word_high); |
| 719 |
} |
| 720 |
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct; |
| 721 |
genocounts[1] = even_ct - bothset_ct; |
| 722 |
genocounts[2] = odd_ct - bothset_ct; |
| 723 |
genocounts[3] = bothset_ct; |
| 724 |
} |
| 725 |
|
| 726 |
uintptr_t MostCommonGenoUnsafe(const uintptr_t* genoarr, uint32_t sample_ct) { |
| 727 |
STD_ARRAY_DECL(uint32_t, 4, genocounts); |
| 728 |
GenoarrCountFreqsUnsafe(genoarr, sample_ct, genocounts); |
| 729 |
uint32_t most_common_geno_ct = genocounts[0]; |
| 730 |
if (most_common_geno_ct * 2 >= sample_ct) { |
| 731 |
return 0; |
| 732 |
} |
| 733 |
uintptr_t most_common_geno = 0; |
| 734 |
for (uintptr_t cur_geno = 1; cur_geno != 4; ++cur_geno) { |
| 735 |
if (most_common_geno_ct < genocounts[cur_geno]) { |
| 736 |
most_common_geno = cur_geno; |
| 737 |
most_common_geno_ct = genocounts[cur_geno]; |
| 738 |
} |
| 739 |
} |
| 740 |
return most_common_geno; |
| 741 |
} |
| 742 |
|
| 743 |
// genoarr now allowed to be unaligned. |
| 744 |
void CountSubset3FreqVec6(const void* __restrict genoarr, const VecW* __restrict interleaved_mask_vvec, uint32_t vec_ct, uint32_t* __restrict even_ctp, uint32_t* __restrict odd_ctp, uint32_t* __restrict bothset_ctp) { |
| 745 |
assert(!(vec_ct % 6)); |
| 746 |
// Sets even_ct to the number of set low bits in the current block after |
| 747 |
// subsetting, odd_ct to the number of set high bits, and bothset_ct by the |
| 748 |
// number of 0b11s. |
| 749 |
const VecW m1 = VCONST_W(kMask5555); |
| 750 |
const VecW m2 = VCONST_W(kMask3333); |
| 751 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 752 |
const unsigned char* geno_vvec_biter = S_CAST(const unsigned char*, genoarr); |
| 753 |
const VecW* interleaved_mask_vvec_iter = interleaved_mask_vvec; |
| 754 |
VecW acc_even = vecw_setzero(); |
| 755 |
VecW acc_odd = vecw_setzero(); |
| 756 |
VecW acc_bothset = vecw_setzero(); |
| 757 |
uintptr_t cur_incr = 60; |
| 758 |
for (; ; vec_ct -= cur_incr) { |
| 759 |
if (vec_ct < 60) { |
| 760 |
if (!vec_ct) { |
| 761 |
*even_ctp = HsumW(acc_even); |
| 762 |
*odd_ctp = HsumW(acc_odd); |
| 763 |
*bothset_ctp = HsumW(acc_bothset); |
| 764 |
return; |
| 765 |
} |
| 766 |
cur_incr = vec_ct; |
| 767 |
} |
| 768 |
VecW inner_acc_even = vecw_setzero(); |
| 769 |
VecW inner_acc_odd = vecw_setzero(); |
| 770 |
VecW inner_acc_bothset = vecw_setzero(); |
| 771 |
const unsigned char* geno_vvec_stop = &(geno_vvec_biter[cur_incr * kBytesPerVec]); |
| 772 |
do { |
| 773 |
VecW interleaved_mask_vword = *interleaved_mask_vvec_iter++; |
| 774 |
VecW cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 775 |
geno_vvec_biter += kBytesPerVec; |
| 776 |
VecW cur_mask = interleaved_mask_vword & m1; |
| 777 |
VecW odd1 = cur_mask & vecw_srli(cur_geno_vword, 1); |
| 778 |
VecW even1 = cur_mask & cur_geno_vword; |
| 779 |
VecW bothset1 = odd1 & cur_geno_vword; |
| 780 |
|
| 781 |
cur_mask = vecw_srli(interleaved_mask_vword, 1) & m1; |
| 782 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 783 |
geno_vvec_biter += kBytesPerVec; |
| 784 |
VecW cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1); |
| 785 |
even1 = even1 + (cur_mask & cur_geno_vword); |
| 786 |
odd1 = odd1 + cur_geno_vword_high_masked; |
| 787 |
bothset1 = bothset1 + (cur_geno_vword_high_masked & cur_geno_vword); |
| 788 |
|
| 789 |
interleaved_mask_vword = *interleaved_mask_vvec_iter++; |
| 790 |
cur_mask = interleaved_mask_vword & m1; |
| 791 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 792 |
geno_vvec_biter += kBytesPerVec; |
| 793 |
cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1); |
| 794 |
even1 = even1 + (cur_mask & cur_geno_vword); |
| 795 |
odd1 = odd1 + cur_geno_vword_high_masked; |
| 796 |
bothset1 = bothset1 + (cur_geno_vword_high_masked & cur_geno_vword); |
| 797 |
|
| 798 |
even1 = (even1 & m2) + (vecw_srli(even1, 2) & m2); |
| 799 |
odd1 = (odd1 & m2) + (vecw_srli(odd1, 2) & m2); |
| 800 |
bothset1 = (bothset1 & m2) + (vecw_srli(bothset1, 2) & m2); |
| 801 |
|
| 802 |
cur_mask = vecw_srli(interleaved_mask_vword, 1) & m1; |
| 803 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 804 |
geno_vvec_biter += kBytesPerVec; |
| 805 |
VecW odd2 = cur_mask & vecw_srli(cur_geno_vword, 1); |
| 806 |
VecW even2 = cur_mask & cur_geno_vword; |
| 807 |
VecW bothset2 = odd2 & cur_geno_vword; |
| 808 |
|
| 809 |
interleaved_mask_vword = *interleaved_mask_vvec_iter++; |
| 810 |
cur_mask = interleaved_mask_vword & m1; |
| 811 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 812 |
geno_vvec_biter += kBytesPerVec; |
| 813 |
cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1); |
| 814 |
even2 = even2 + (cur_mask & cur_geno_vword); |
| 815 |
odd2 = odd2 + cur_geno_vword_high_masked; |
| 816 |
bothset2 = bothset2 + (cur_geno_vword_high_masked & cur_geno_vword); |
| 817 |
|
| 818 |
cur_mask = vecw_srli(interleaved_mask_vword, 1) & m1; |
| 819 |
cur_geno_vword = vecw_loadu(geno_vvec_biter); |
| 820 |
geno_vvec_biter += kBytesPerVec; |
| 821 |
cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1); |
| 822 |
even2 = even2 + (cur_mask & cur_geno_vword); |
| 823 |
odd2 = odd2 + cur_geno_vword_high_masked; |
| 824 |
bothset2 = bothset2 + (cur_geno_vword_high_masked & cur_geno_vword); |
| 825 |
|
| 826 |
even1 = even1 + (even2 & m2) + (vecw_srli(even2, 2) & m2); |
| 827 |
odd1 = odd1 + (odd2 & m2) + (vecw_srli(odd2, 2) & m2); |
| 828 |
bothset1 = bothset1 + (bothset2 & m2) + (vecw_srli(bothset2, 2) & m2); |
| 829 |
// these now contain 4-bit values from 0-12 |
| 830 |
|
| 831 |
inner_acc_even = inner_acc_even + (even1 & m4) + (vecw_srli(even1, 4) & m4); |
| 832 |
inner_acc_odd = inner_acc_odd + (odd1 & m4) + (vecw_srli(odd1, 4) & m4); |
| 833 |
inner_acc_bothset = inner_acc_bothset + (bothset1 & m4) + (vecw_srli(bothset1, 4) & m4); |
| 834 |
} while (geno_vvec_biter < geno_vvec_stop); |
| 835 |
const VecW m0 = vecw_setzero(); |
| 836 |
acc_even = acc_even + vecw_bytesum(inner_acc_even, m0); |
| 837 |
acc_odd = acc_odd + vecw_bytesum(inner_acc_odd, m0); |
| 838 |
acc_bothset = acc_bothset + vecw_bytesum(inner_acc_bothset, m0); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
void GenoarrCountSubsetFreqs(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict sample_include_interleaved_vec, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) { |
| 843 |
// fills genocounts[0] with the number of 00s, genocounts[1] with the number |
| 844 |
// of 01s, etc. |
| 845 |
// {raw_}sample_ct == 0 ok. |
| 846 |
const uint32_t raw_sample_ctv2 = NypCtToVecCt(raw_sample_ct); |
| 847 |
uint32_t even_ct; |
| 848 |
uint32_t odd_ct; |
| 849 |
uint32_t bothset_ct; |
| 850 |
#ifdef USE_SSE2 |
| 851 |
uint32_t vec_idx = raw_sample_ctv2 - (raw_sample_ctv2 % 6); |
| 852 |
CountSubset3FreqVec6(genoarr, R_CAST(const VecW*, sample_include_interleaved_vec), vec_idx, &even_ct, &odd_ct, &bothset_ct); |
| 853 |
const uintptr_t* genoarr_iter = &(genoarr[kWordsPerVec * vec_idx]); |
| 854 |
const uintptr_t* interleaved_mask_iter = &(sample_include_interleaved_vec[(kWordsPerVec / 2) * vec_idx]); |
| 855 |
# ifdef USE_AVX2 |
| 856 |
uintptr_t mask_base1 = 0; |
| 857 |
uintptr_t mask_base2 = 0; |
| 858 |
uintptr_t mask_base3 = 0; |
| 859 |
uintptr_t mask_base4 = 0; |
| 860 |
for (; vec_idx != raw_sample_ctv2; ++vec_idx) { |
| 861 |
uintptr_t mask_word1; |
| 862 |
uintptr_t mask_word2; |
| 863 |
uintptr_t mask_word3; |
| 864 |
uintptr_t mask_word4; |
| 865 |
if (!(vec_idx % 2)) { |
| 866 |
mask_base1 = *interleaved_mask_iter++; |
| 867 |
mask_base2 = *interleaved_mask_iter++; |
| 868 |
mask_base3 = *interleaved_mask_iter++; |
| 869 |
mask_base4 = *interleaved_mask_iter++; |
| 870 |
mask_word1 = mask_base1 & kMask5555; |
| 871 |
mask_word2 = mask_base2 & kMask5555; |
| 872 |
mask_word3 = mask_base3 & kMask5555; |
| 873 |
mask_word4 = mask_base4 & kMask5555; |
| 874 |
} else { |
| 875 |
mask_word1 = (mask_base1 >> 1) & kMask5555; |
| 876 |
mask_word2 = (mask_base2 >> 1) & kMask5555; |
| 877 |
mask_word3 = (mask_base3 >> 1) & kMask5555; |
| 878 |
mask_word4 = (mask_base4 >> 1) & kMask5555; |
| 879 |
} |
| 880 |
for (uint32_t vechalf_idx = 0; ; ++vechalf_idx) { |
| 881 |
const uintptr_t cur_geno_word1 = *genoarr_iter++; |
| 882 |
const uintptr_t cur_geno_word2 = *genoarr_iter++; |
| 883 |
const uintptr_t cur_geno_word1_high_masked = mask_word1 & (cur_geno_word1 >> 1); |
| 884 |
const uintptr_t cur_geno_word2_high_masked = mask_word2 & (cur_geno_word2 >> 1); |
| 885 |
even_ct += PopcountWord(((cur_geno_word1 & mask_word1) << 1) | (cur_geno_word2 & mask_word2)); |
| 886 |
odd_ct += PopcountWord((cur_geno_word1_high_masked << 1) | cur_geno_word2_high_masked); |
| 887 |
bothset_ct += PopcountWord(((cur_geno_word1 & cur_geno_word1_high_masked) << 1) | (cur_geno_word2 & cur_geno_word2_high_masked)); |
| 888 |
if (vechalf_idx) { |
| 889 |
break; |
| 890 |
} |
| 891 |
mask_word1 = mask_word3; |
| 892 |
mask_word2 = mask_word4; |
| 893 |
} |
| 894 |
} |
| 895 |
# else // not USE_AVX2 |
| 896 |
uintptr_t mask_base1 = 0; |
| 897 |
uintptr_t mask_base2 = 0; |
| 898 |
for (; vec_idx != raw_sample_ctv2; ++vec_idx) { |
| 899 |
uintptr_t mask_word1; |
| 900 |
uintptr_t mask_word2; |
| 901 |
if (!(vec_idx % 2)) { |
| 902 |
mask_base1 = *interleaved_mask_iter++; |
| 903 |
mask_base2 = *interleaved_mask_iter++; |
| 904 |
mask_word1 = mask_base1 & kMask5555; |
| 905 |
mask_word2 = mask_base2 & kMask5555; |
| 906 |
} else { |
| 907 |
mask_word1 = (mask_base1 >> 1) & kMask5555; |
| 908 |
mask_word2 = (mask_base2 >> 1) & kMask5555; |
| 909 |
} |
| 910 |
const uintptr_t cur_geno_word1 = *genoarr_iter++; |
| 911 |
const uintptr_t cur_geno_word2 = *genoarr_iter++; |
| 912 |
const uintptr_t cur_geno_word1_high_masked = mask_word1 & (cur_geno_word1 >> 1); |
| 913 |
const uintptr_t cur_geno_word2_high_masked = mask_word2 & (cur_geno_word2 >> 1); |
| 914 |
# ifdef USE_SSE42 |
| 915 |
even_ct += PopcountWord(((cur_geno_word1 & mask_word1) << 1) | (cur_geno_word2 & mask_word2)); |
| 916 |
odd_ct += PopcountWord((cur_geno_word1_high_masked << 1) | cur_geno_word2_high_masked); |
| 917 |
bothset_ct += PopcountWord(((cur_geno_word1 & cur_geno_word1_high_masked) << 1) | (cur_geno_word2 & cur_geno_word2_high_masked)); |
| 918 |
# else |
| 919 |
even_ct += NypsumWord((cur_geno_word1 & mask_word1) + (cur_geno_word2 & mask_word2)); |
| 920 |
odd_ct += NypsumWord(cur_geno_word1_high_masked + cur_geno_word2_high_masked); |
| 921 |
bothset_ct += NypsumWord((cur_geno_word1 & cur_geno_word1_high_masked) + (cur_geno_word2 & cur_geno_word2_high_masked)); |
| 922 |
# endif |
| 923 |
} |
| 924 |
# endif // not USE_AVX2 |
| 925 |
#else // not USE_SSE2 |
| 926 |
uint32_t word_idx = raw_sample_ctv2 - (raw_sample_ctv2 % 6); |
| 927 |
CountSubset3FreqVec6(genoarr, R_CAST(const VecW*, sample_include_interleaved_vec), word_idx, &even_ct, &odd_ct, &bothset_ct); |
| 928 |
const uintptr_t* interleaved_mask_iter = &(sample_include_interleaved_vec[word_idx / 2]); |
| 929 |
uintptr_t mask_base = 0; |
| 930 |
for (; word_idx != raw_sample_ctv2; ++word_idx) { |
| 931 |
uintptr_t mask_word; |
| 932 |
if (!(word_idx % 2)) { |
| 933 |
mask_base = *interleaved_mask_iter++; |
| 934 |
mask_word = mask_base & kMask5555; |
| 935 |
} else { |
| 936 |
mask_word = (mask_base >> 1) & kMask5555; |
| 937 |
} |
| 938 |
const uintptr_t cur_geno_word = genoarr[word_idx]; |
| 939 |
const uintptr_t cur_geno_word_high_masked = mask_word & (cur_geno_word >> 1); |
| 940 |
even_ct += Popcount01Word(cur_geno_word & mask_word); |
| 941 |
odd_ct += Popcount01Word(cur_geno_word_high_masked); |
| 942 |
bothset_ct += Popcount01Word(cur_geno_word & cur_geno_word_high_masked); |
| 943 |
} |
| 944 |
#endif |
| 945 |
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct; |
| 946 |
genocounts[1] = even_ct - bothset_ct; |
| 947 |
genocounts[2] = odd_ct - bothset_ct; |
| 948 |
genocounts[3] = bothset_ct; |
| 949 |
} |
| 950 |
|
| 951 |
void GenoarrCountSubsetFreqs2(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict sample_include, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) { |
| 952 |
// slower GenoarrCountSubsetFreqs() which does not require |
| 953 |
// sample_include_interleaved_vec to be precomputed. |
| 954 |
// {raw_}sample_ct == 0 ok. |
| 955 |
const uint32_t raw_sample_ctl2 = NypCtToWordCt(raw_sample_ct); |
| 956 |
const uint32_t fullword_ct = raw_sample_ctl2 / 2; |
| 957 |
uint32_t even_ct = 0; |
| 958 |
uint32_t odd_ct = 0; |
| 959 |
uint32_t bothset_ct = 0; |
| 960 |
for (uint32_t widx = 0; widx != fullword_ct; ++widx) { |
| 961 |
// possible todo: try vectorizing this loop in SSE4.2+ high-sample-ct case |
| 962 |
// with shuffle-based dynamic unpacking of sample_include? |
| 963 |
const uintptr_t mask_word = sample_include[widx]; |
| 964 |
if (mask_word) { |
| 965 |
uintptr_t geno_word = genoarr[2 * widx]; |
| 966 |
uintptr_t geno_even = PackWordToHalfwordMask5555(geno_word); |
| 967 |
uintptr_t geno_odd = PackWordToHalfwordMaskAAAA(geno_word); |
| 968 |
geno_word = genoarr[2 * widx + 1]; |
| 969 |
geno_even |= S_CAST(uintptr_t, PackWordToHalfwordMask5555(geno_word)) << kBitsPerWordD2; |
| 970 |
geno_odd |= S_CAST(uintptr_t, PackWordToHalfwordMaskAAAA(geno_word)) << kBitsPerWordD2; |
| 971 |
const uintptr_t geno_even_masked = geno_even & mask_word; |
| 972 |
even_ct += PopcountWord(geno_even_masked); |
| 973 |
odd_ct += PopcountWord(geno_odd & mask_word); |
| 974 |
bothset_ct += PopcountWord(geno_odd & geno_even_masked); |
| 975 |
} |
| 976 |
} |
| 977 |
if (raw_sample_ctl2 % 2) { |
| 978 |
const uintptr_t mask_hw = sample_include[fullword_ct]; |
| 979 |
if (mask_hw) { |
| 980 |
const uintptr_t geno_word = genoarr[2 * fullword_ct]; |
| 981 |
// todo: benchmark main loop unpack vs. pack |
| 982 |
const uintptr_t mask_word = UnpackHalfwordToWord(mask_hw); |
| 983 |
const uintptr_t geno_word_shifted = geno_word >> 1; |
| 984 |
const uintptr_t geno_word_masked = geno_word & mask_word; |
| 985 |
even_ct += Popcount01Word(geno_word_masked); |
| 986 |
odd_ct += Popcount01Word(geno_word_shifted & mask_word); |
| 987 |
bothset_ct += Popcount01Word(geno_word_masked & geno_word_shifted); |
| 988 |
} |
| 989 |
} |
| 990 |
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct; |
| 991 |
genocounts[1] = even_ct - bothset_ct; |
| 992 |
genocounts[2] = odd_ct - bothset_ct; |
| 993 |
genocounts[3] = bothset_ct; |
| 994 |
} |
| 995 |
|
| 996 |
void GenoarrCountInvsubsetFreqs2(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict sample_exclude, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) { |
| 997 |
// {raw_}sample_ct == 0 ok. |
| 998 |
// ugh, 'fullword' is overloaded. probable todo: keep halfword/fullword, |
| 999 |
// switch more common use case to bodyword/trailword. |
| 1000 |
const uint32_t bodyword_ct = raw_sample_ct / kBitsPerWord; |
| 1001 |
uint32_t even_ct = 0; |
| 1002 |
uint32_t odd_ct = 0; |
| 1003 |
uint32_t bothset_ct = 0; |
| 1004 |
for (uint32_t widx = 0; widx != bodyword_ct; ++widx) { |
| 1005 |
// possible todo: try vectorizing this loop in SSE4.2+ high-sample-ct case |
| 1006 |
// with shuffle-based dynamic unpacking of sample_exclude? |
| 1007 |
const uintptr_t mask_word = ~sample_exclude[widx]; |
| 1008 |
if (mask_word) { |
| 1009 |
uintptr_t geno_word = genoarr[2 * widx]; |
| 1010 |
uintptr_t geno_even = PackWordToHalfwordMask5555(geno_word); |
| 1011 |
uintptr_t geno_odd = PackWordToHalfwordMaskAAAA(geno_word); |
| 1012 |
geno_word = genoarr[2 * widx + 1]; |
| 1013 |
geno_even |= S_CAST(uintptr_t, PackWordToHalfwordMask5555(geno_word)) << kBitsPerWordD2; |
| 1014 |
geno_odd |= S_CAST(uintptr_t, PackWordToHalfwordMaskAAAA(geno_word)) << kBitsPerWordD2; |
| 1015 |
const uintptr_t geno_even_masked = geno_even & mask_word; |
| 1016 |
even_ct += PopcountWord(geno_even_masked); |
| 1017 |
odd_ct += PopcountWord(geno_odd & mask_word); |
| 1018 |
bothset_ct += PopcountWord(geno_odd & geno_even_masked); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
const uint32_t remainder = raw_sample_ct % kBitsPerWord; |
| 1022 |
if (remainder) { |
| 1023 |
const uintptr_t mask_word = bzhi(~sample_exclude[bodyword_ct], remainder); |
| 1024 |
if (mask_word) { |
| 1025 |
uintptr_t geno_word = genoarr[2 * bodyword_ct]; |
| 1026 |
uintptr_t geno_even = PackWordToHalfwordMask5555(geno_word); |
| 1027 |
uintptr_t geno_odd = PackWordToHalfwordMaskAAAA(geno_word); |
| 1028 |
if (remainder > kBitsPerWordD2) { |
| 1029 |
geno_word = genoarr[2 * bodyword_ct + 1]; |
| 1030 |
geno_even |= S_CAST(uintptr_t, PackWordToHalfwordMask5555(geno_word)) << kBitsPerWordD2; |
| 1031 |
geno_odd |= S_CAST(uintptr_t, PackWordToHalfwordMaskAAAA(geno_word)) << kBitsPerWordD2; |
| 1032 |
} |
| 1033 |
const uintptr_t geno_even_masked = geno_even & mask_word; |
| 1034 |
even_ct += PopcountWord(geno_even_masked); |
| 1035 |
odd_ct += PopcountWord(geno_odd & mask_word); |
| 1036 |
bothset_ct += PopcountWord(geno_odd & geno_even_masked); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct; |
| 1040 |
genocounts[1] = even_ct - bothset_ct; |
| 1041 |
genocounts[2] = odd_ct - bothset_ct; |
| 1042 |
genocounts[3] = bothset_ct; |
| 1043 |
} |
| 1044 |
|
| 1045 |
void GenoarrCountSubsetIntersectFreqs(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict subset1, const uintptr_t* __restrict subset2, uint32_t raw_sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) { |
| 1046 |
// {raw_}sample_ct == 0 ok. |
| 1047 |
const uint32_t raw_sample_ctl2 = NypCtToWordCt(raw_sample_ct); |
| 1048 |
const uint32_t fullword_ct = raw_sample_ctl2 / 2; |
| 1049 |
uint32_t subset_intersect_ct = 0; |
| 1050 |
uint32_t even_ct = 0; |
| 1051 |
uint32_t odd_ct = 0; |
| 1052 |
uint32_t bothset_ct = 0; |
| 1053 |
for (uint32_t widx = 0; widx != fullword_ct; ++widx) { |
| 1054 |
// hmm, there may be little point to vectorizing this |
| 1055 |
const uintptr_t mask_word = subset1[widx] & subset2[widx]; |
| 1056 |
if (mask_word) { |
| 1057 |
uintptr_t geno_word = genoarr[2 * widx]; |
| 1058 |
uintptr_t geno_even = PackWordToHalfwordMask5555(geno_word); |
| 1059 |
uintptr_t geno_odd = PackWordToHalfwordMaskAAAA(geno_word); |
| 1060 |
geno_word = genoarr[2 * widx + 1]; |
| 1061 |
geno_even |= S_CAST(uintptr_t, PackWordToHalfwordMask5555(geno_word)) << kBitsPerWordD2; |
| 1062 |
geno_odd |= S_CAST(uintptr_t, PackWordToHalfwordMaskAAAA(geno_word)) << kBitsPerWordD2; |
| 1063 |
const uintptr_t geno_even_masked = geno_even & mask_word; |
| 1064 |
subset_intersect_ct += PopcountWord(mask_word); |
| 1065 |
even_ct += PopcountWord(geno_even_masked); |
| 1066 |
odd_ct += PopcountWord(geno_odd & mask_word); |
| 1067 |
bothset_ct += PopcountWord(geno_odd & geno_even_masked); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
if (raw_sample_ctl2 % 2) { |
| 1071 |
const uintptr_t mask_hw = subset1[fullword_ct] & subset2[fullword_ct]; |
| 1072 |
if (mask_hw) { |
| 1073 |
const uintptr_t geno_word = genoarr[fullword_ct * 2]; |
| 1074 |
const uintptr_t mask_word = UnpackHalfwordToWord(mask_hw); |
| 1075 |
const uintptr_t geno_word_shifted = geno_word >> 1; |
| 1076 |
const uintptr_t geno_word_masked = geno_word & mask_word; |
| 1077 |
subset_intersect_ct += Popcount01Word(mask_word); |
| 1078 |
even_ct += Popcount01Word(geno_word_masked); |
| 1079 |
odd_ct += Popcount01Word(geno_word_shifted & mask_word); |
| 1080 |
bothset_ct += Popcount01Word(geno_word_masked & geno_word_shifted); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
genocounts[0] = subset_intersect_ct + bothset_ct - even_ct - odd_ct; |
| 1084 |
genocounts[1] = even_ct - bothset_ct; |
| 1085 |
genocounts[2] = odd_ct - bothset_ct; |
| 1086 |
genocounts[3] = bothset_ct; |
| 1087 |
} |
| 1088 |
|
| 1089 |
void GenovecInvertUnsafe(uint32_t sample_ct, uintptr_t* genovec) { |
| 1090 |
// flips 0 to 2 and vice versa. |
| 1091 |
// "unsafe" because trailing bits are not zeroed out. |
| 1092 |
const uint32_t vec_ct = NypCtToVecCt(sample_ct); |
| 1093 |
assert(IsVecAligned(genovec)); |
| 1094 |
const VecW not_m1 = VCONST_W(kMaskAAAA); |
| 1095 |
VecW* vptr = R_CAST(VecW*, genovec); |
| 1096 |
for (uint32_t vidx = 0; vidx != vec_ct; ++vidx) { |
| 1097 |
VecW cur_vec = vptr[vidx]; |
| 1098 |
// flip high bit iff low bit is unset |
| 1099 |
vptr[vidx] = cur_vec ^ vecw_and_notfirst(vecw_slli(cur_vec, 1), not_m1); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
void DifflistCountSubsetFreqs(const uintptr_t* __restrict sample_include, const uintptr_t* __restrict raregeno, const uint32_t* __restrict difflist_sample_ids, uint32_t common_geno, uint32_t difflist_len, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) { |
| 1104 |
STD_ARRAY_REF_FILL0(4, genocounts); |
| 1105 |
for (uint32_t difflist_idx = 0; difflist_idx != difflist_len; ++difflist_idx) { |
| 1106 |
const uint32_t raw_sample_idx = difflist_sample_ids[difflist_idx]; |
| 1107 |
if (IsSet(sample_include, raw_sample_idx)) { |
| 1108 |
genocounts[GetNyparrEntry(raregeno, difflist_idx)] += 1; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
genocounts[common_geno] = sample_ct - genocounts[0] - genocounts[1] - genocounts[2] - genocounts[3]; |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
#ifdef USE_SSE2 |
| 1116 |
static_assert(kPglNypTransposeBatch == S_CAST(uint32_t, kNypsPerCacheline), "TransposeNypblock64() needs to be updated."); |
| 1117 |
# ifdef CACHELINE64 |
| 1118 |
void TransposeNypblock64(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* __restrict write_iter, unsigned char* __restrict buf0, unsigned char* __restrict buf1) { |
| 1119 |
// buf0 and buf1 must each be vector-aligned and have size 16k |
| 1120 |
// Tried using previous AVX2 small-buffer approach, but that was a bit |
| 1121 |
// worse... though maybe it should be revisited? |
| 1122 |
|
| 1123 |
// Each input row has 256 nyps, across 8 words. |
| 1124 |
// First word of each row goes into first buf0 row, etc. |
| 1125 |
const uint32_t buf0_row_ct = DivUp(write_batch_size, 32); |
| 1126 |
{ |
| 1127 |
// Fold the first 3 shuffles into the ingestion loop. |
| 1128 |
// Can fold 2 additional shuffles here by ingesting uint16s instead. That |
| 1129 |
// removes the need for the second loop, halving the workspace requirement. |
| 1130 |
// Benchmark results of that approach are slightly but consistently worse, |
| 1131 |
// though. |
| 1132 |
const uintptr_t* initial_read_iter = read_iter; |
| 1133 |
const uintptr_t* initial_read_end = &(initial_read_iter[buf0_row_ct]); |
| 1134 |
uintptr_t* initial_target_iter = R_CAST(uintptr_t*, buf0); |
| 1135 |
const uint32_t read_batch_rem = kNypsPerCacheline - read_batch_size; |
| 1136 |
// Tried fancier vector-based ingestion, didn't help. |
| 1137 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 1138 |
const uintptr_t* read_iter_tmp = initial_read_iter; |
| 1139 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 1140 |
*initial_target_iter++ = *read_iter_tmp; |
| 1141 |
read_iter_tmp = &(read_iter_tmp[read_ul_stride]); |
| 1142 |
} |
| 1143 |
ZeroWArr(read_batch_rem, initial_target_iter); |
| 1144 |
initial_target_iter = &(initial_target_iter[read_batch_rem]); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
// shuffle from 64 -> 16 |
| 1149 |
const uint32_t buf1_row_ct = DivUp(write_batch_size, 8); |
| 1150 |
{ |
| 1151 |
// full buf0 row is 256 * 8 bytes |
| 1152 |
// full buf1 row is 512 bytes |
| 1153 |
const VecW* buf0_read_iter = R_CAST(const VecW*, buf0); |
| 1154 |
__m128i* write_iter0 = R_CAST(__m128i*, buf1); |
| 1155 |
const uint32_t buf0_row_clwidth = DivUp(read_batch_size, 8); |
| 1156 |
# ifdef USE_SHUFFLE8 |
| 1157 |
const VecW gather_u32s = vecw_setr8(0, 1, 8, 9, 2, 3, 10, 11, |
| 1158 |
4, 5, 12, 13, 6, 7, 14, 15); |
| 1159 |
# else |
| 1160 |
const VecW m16 = VCONST_W(kMask0000FFFF); |
| 1161 |
# endif |
| 1162 |
for (uint32_t bidx = 0; bidx != buf0_row_ct; ++bidx) { |
| 1163 |
__m128i* write_iter1 = &(write_iter0[32]); |
| 1164 |
__m128i* write_iter2 = &(write_iter1[32]); |
| 1165 |
__m128i* write_iter3 = &(write_iter2[32]); |
| 1166 |
for (uint32_t clidx = 0; clidx != buf0_row_clwidth; ++clidx) { |
| 1167 |
# ifdef USE_AVX2 |
| 1168 |
VecW loader0 = buf0_read_iter[clidx * 2]; |
| 1169 |
VecW loader1 = buf0_read_iter[clidx * 2 + 1]; |
| 1170 |
loader0 = vecw_shuffle8(loader0, gather_u32s); |
| 1171 |
loader1 = vecw_shuffle8(loader1, gather_u32s); |
| 1172 |
// (0,0) (0,1) (1,0) ... (7,1) (0,2) ... (7,3) (8,0) ... (31,3) |
| 1173 |
// (0,4) ... (31,7) |
| 1174 |
// -> (0,0) ... (7,1) (0,2) ... (7,3) (8,0) ... (15,3) (0,4) ... (15,7) |
| 1175 |
// (16,0) ... (31,3) (16,4) ... |
| 1176 |
loader0 = vecw_permute0xd8_if_avx2(loader0); |
| 1177 |
loader1 = vecw_permute0xd8_if_avx2(loader1); |
| 1178 |
// -> (0,0) ... (7,1) (0,2) ... (7,3) (0,4) ... (7,7) (8,0) ... (15,7) |
| 1179 |
// (16,0) ... (23,7) (24,0) ... (31,7) |
| 1180 |
const __m256i vec_lo = _mm256_shuffle_epi32(WToVec(loader0), 0xd8); |
| 1181 |
const __m256i vec_hi = _mm256_shuffle_epi32(WToVec(loader1), 0xd8); |
| 1182 |
const __m256i final0145 = _mm256_unpacklo_epi64(vec_lo, vec_hi); |
| 1183 |
const __m256i final2367 = _mm256_unpackhi_epi64(vec_lo, vec_hi); |
| 1184 |
// GCC doesn't support _mm256_storeu_si128i as of this writing. |
| 1185 |
write_iter0[clidx] = _mm256_castsi256_si128(final0145); |
| 1186 |
write_iter1[clidx] = _mm256_castsi256_si128(final2367); |
| 1187 |
write_iter2[clidx] = _mm256_extracti128_si256(final0145, 1); |
| 1188 |
write_iter3[clidx] = _mm256_extracti128_si256(final2367, 1); |
| 1189 |
# else |
| 1190 |
VecW loader0 = buf0_read_iter[clidx * 4]; |
| 1191 |
VecW loader1 = buf0_read_iter[clidx * 4 + 1]; |
| 1192 |
VecW loader2 = buf0_read_iter[clidx * 4 + 2]; |
| 1193 |
VecW loader3 = buf0_read_iter[clidx * 4 + 3]; |
| 1194 |
# ifdef USE_SHUFFLE8 |
| 1195 |
loader0 = vecw_shuffle8(loader0, gather_u32s); |
| 1196 |
loader1 = vecw_shuffle8(loader1, gather_u32s); |
| 1197 |
loader2 = vecw_shuffle8(loader2, gather_u32s); |
| 1198 |
loader3 = vecw_shuffle8(loader3, gather_u32s); |
| 1199 |
# else |
| 1200 |
VecW tmp_lo = vecw_unpacklo16(loader0, loader1); |
| 1201 |
VecW tmp_hi = vecw_unpackhi16(loader0, loader1); |
| 1202 |
loader0 = vecw_blendv(vecw_slli(tmp_hi, 16), tmp_lo, m16); |
| 1203 |
loader1 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 16), m16); |
| 1204 |
tmp_lo = vecw_unpacklo16(loader2, loader3); |
| 1205 |
tmp_hi = vecw_unpackhi16(loader2, loader3); |
| 1206 |
loader2 = vecw_blendv(vecw_slli(tmp_hi, 16), tmp_lo, m16); |
| 1207 |
loader3 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 16), m16); |
| 1208 |
# endif |
| 1209 |
// (0,0) (0,1) (1,0) ... (7,1) (0,2) ... (7,3) (8,0) ... (31,3) |
| 1210 |
// + (0,4) ... (31,7) |
| 1211 |
// -> (0,0) ... (7,3) (0,4) ... (7,7) (8,0) ... (15,7) |
| 1212 |
const VecW lo_0_15 = vecw_unpacklo32(loader0, loader1); |
| 1213 |
const VecW lo_16_31 = vecw_unpackhi32(loader0, loader1); |
| 1214 |
const VecW hi_0_15 = vecw_unpacklo32(loader2, loader3); |
| 1215 |
const VecW hi_16_31 = vecw_unpackhi32(loader2, loader3); |
| 1216 |
write_iter0[clidx] = WToVec(vecw_unpacklo64(lo_0_15, hi_0_15)); |
| 1217 |
write_iter1[clidx] = WToVec(vecw_unpackhi64(lo_0_15, hi_0_15)); |
| 1218 |
write_iter2[clidx] = WToVec(vecw_unpacklo64(lo_16_31, hi_16_31)); |
| 1219 |
write_iter3[clidx] = WToVec(vecw_unpackhi64(lo_16_31, hi_16_31)); |
| 1220 |
# endif |
| 1221 |
} |
| 1222 |
buf0_read_iter = &(buf0_read_iter[2048 / kBytesPerVec]); |
| 1223 |
write_iter0 = &(write_iter3[32]); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
// movemask from 16 -> 2 |
| 1228 |
const VecW* source_iter = R_CAST(VecW*, buf1); |
| 1229 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1230 |
|
| 1231 |
// Take advantage of current function contract. |
| 1232 |
const uint32_t buf1_fullrow_ct = (write_batch_size + 3) / 8; |
| 1233 |
|
| 1234 |
const uint32_t write_v8ui_stride = kVec8thUintPerWord * write_ul_stride; |
| 1235 |
const uint32_t vec_ct = DivUp(read_batch_size, (kBytesPerVec / 2)); |
| 1236 |
Vec8thUint* target_iter0 = DowncastWToV8(write_iter); |
| 1237 |
for (uint32_t uii = 0; uii != buf1_fullrow_ct; ++uii) { |
| 1238 |
Vec8thUint* target_iter1 = &(target_iter0[write_v8ui_stride]); |
| 1239 |
Vec8thUint* target_iter2 = &(target_iter1[write_v8ui_stride]); |
| 1240 |
Vec8thUint* target_iter3 = &(target_iter2[write_v8ui_stride]); |
| 1241 |
Vec8thUint* target_iter4 = &(target_iter3[write_v8ui_stride]); |
| 1242 |
Vec8thUint* target_iter5 = &(target_iter4[write_v8ui_stride]); |
| 1243 |
Vec8thUint* target_iter6 = &(target_iter5[write_v8ui_stride]); |
| 1244 |
Vec8thUint* target_iter7 = &(target_iter6[write_v8ui_stride]); |
| 1245 |
for (uint32_t vidx = 0; vidx != vec_ct; ++vidx) { |
| 1246 |
const VecW loader = source_iter[vidx]; |
| 1247 |
// Using goal bit-coordinates, where '_' indicates irrelevant content, we |
| 1248 |
// set target_0123 to |
| 1249 |
// _ (0, 0) _ (1, 0) _ (2, 0) _ (3, 0) _ (0, 1) _ (1, 1) _ (2, 1) ... |
| 1250 |
// and target_4567 to |
| 1251 |
// _ (4, 0) _ (5, 0) _ (6, 0) _ (7, 0) _ (4, 1) _ (5, 1) _ (6, 1) ... |
| 1252 |
// This is perfectly arranged for movemask. |
| 1253 |
// todo: better ARM implementation |
| 1254 |
VecW target_4567 = vecw_blendv(loader, vecw_srli(loader, 7), m8); |
| 1255 |
target_iter7[vidx] = vecw_movemask(target_4567); |
| 1256 |
target_4567 = vecw_slli(target_4567, 2); |
| 1257 |
target_iter6[vidx] = vecw_movemask(target_4567); |
| 1258 |
target_4567 = vecw_slli(target_4567, 2); |
| 1259 |
target_iter5[vidx] = vecw_movemask(target_4567); |
| 1260 |
target_4567 = vecw_slli(target_4567, 2); |
| 1261 |
target_iter4[vidx] = vecw_movemask(target_4567); |
| 1262 |
VecW target_0123 = vecw_blendv(vecw_slli(loader, 8), vecw_slli(loader, 1), m8); |
| 1263 |
target_iter3[vidx] = vecw_movemask(target_0123); |
| 1264 |
target_0123 = vecw_slli(target_0123, 2); |
| 1265 |
target_iter2[vidx] = vecw_movemask(target_0123); |
| 1266 |
target_0123 = vecw_slli(target_0123, 2); |
| 1267 |
target_iter1[vidx] = vecw_movemask(target_0123); |
| 1268 |
target_0123 = vecw_slli(target_0123, 2); |
| 1269 |
target_iter0[vidx] = vecw_movemask(target_0123); |
| 1270 |
} |
| 1271 |
source_iter = &(source_iter[(2 * kPglNypTransposeBatch) / kBytesPerVec]); |
| 1272 |
target_iter0 = &(target_iter7[write_v8ui_stride]); |
| 1273 |
} |
| 1274 |
if (buf1_fullrow_ct == buf1_row_ct) { |
| 1275 |
return; |
| 1276 |
} |
| 1277 |
Vec8thUint* target_iter1 = &(target_iter0[write_v8ui_stride]); |
| 1278 |
Vec8thUint* target_iter2 = &(target_iter1[write_v8ui_stride]); |
| 1279 |
Vec8thUint* target_iter3 = &(target_iter2[write_v8ui_stride]); |
| 1280 |
for (uint32_t vidx = 0; vidx != vec_ct; ++vidx) { |
| 1281 |
const VecW loader = source_iter[vidx]; |
| 1282 |
VecW target_0123 = vecw_blendv(vecw_slli(loader, 8), vecw_slli(loader, 1), m8); |
| 1283 |
target_iter3[vidx] = vecw_movemask(target_0123); |
| 1284 |
target_0123 = vecw_slli(target_0123, 2); |
| 1285 |
target_iter2[vidx] = vecw_movemask(target_0123); |
| 1286 |
target_0123 = vecw_slli(target_0123, 2); |
| 1287 |
target_iter1[vidx] = vecw_movemask(target_0123); |
| 1288 |
target_0123 = vecw_slli(target_0123, 2); |
| 1289 |
target_iter0[vidx] = vecw_movemask(target_0123); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
# else |
| 1293 |
# ifndef CACHELINE128 |
| 1294 |
# error "CACHELINE64 or CACHELINE128 expected." |
| 1295 |
# endif |
| 1296 |
// assumes USE_SHUFFLE8, !USE_AVX2 |
| 1297 |
void TransposeNypblock64(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* __restrict write_iter, unsigned char* __restrict buf0, unsigned char* __restrict buf1) { |
| 1298 |
// buf0 and buf1 must each be vector-aligned and have size 64k |
| 1299 |
// Each input row has 512 nyps, across 16 words. |
| 1300 |
// First word of each row goes into first buf0 row, etc. |
| 1301 |
const uint32_t buf0_row_ct = DivUp(write_batch_size, 32); |
| 1302 |
{ |
| 1303 |
// Fold the first 4 shuffles into the ingestion loop. |
| 1304 |
const uintptr_t* initial_read_iter = read_iter; |
| 1305 |
const uintptr_t* initial_read_end = &(initial_read_iter[buf0_row_ct]); |
| 1306 |
uintptr_t* initial_target_iter = R_CAST(uintptr_t*, buf0); |
| 1307 |
const uint32_t read_batch_rem = kNypsPerCacheline - read_batch_size; |
| 1308 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 1309 |
const uintptr_t* read_iter_tmp = initial_read_iter; |
| 1310 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 1311 |
*initial_target_iter++ = *read_iter_tmp; |
| 1312 |
read_iter_tmp = &(read_iter_tmp[read_ul_stride]); |
| 1313 |
} |
| 1314 |
ZeroWArr(read_batch_rem, initial_target_iter); |
| 1315 |
initial_target_iter = &(initial_target_iter[read_batch_rem]); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
// First buf0 row now corresponds to a 512x32 nyp matrix (512 * 8 bytes) that |
| 1320 |
// we wish to transpose. We split this into eight 512x4 matrices. |
| 1321 |
// (ARMv8 doesn't have efficient movemask, so this should be better than four |
| 1322 |
// 512x8 matrices.) |
| 1323 |
// This is nearly identical to the middle step in TransposeBitblock64(). |
| 1324 |
{ |
| 1325 |
const VecW* buf0_read_iter = R_CAST(const VecW*, buf0); |
| 1326 |
uintptr_t* write_iter0 = R_CAST(uintptr_t*, buf1); |
| 1327 |
const VecW gather_u16s = vecw_setr8(0, 8, 1, 9, 2, 10, 3, 11, |
| 1328 |
4, 12, 5, 13, 6, 14, 7, 15); |
| 1329 |
const uint32_t buf0_row_b64width = DivUp(read_batch_size, 8); |
| 1330 |
for (uint32_t ridx = 0; ridx != buf0_row_ct; ++ridx) { |
| 1331 |
uintptr_t* write_iter1 = &(write_iter0[64]); |
| 1332 |
uintptr_t* write_iter2 = &(write_iter1[64]); |
| 1333 |
uintptr_t* write_iter3 = &(write_iter2[64]); |
| 1334 |
uintptr_t* write_iter4 = &(write_iter3[64]); |
| 1335 |
uintptr_t* write_iter5 = &(write_iter4[64]); |
| 1336 |
uintptr_t* write_iter6 = &(write_iter5[64]); |
| 1337 |
uintptr_t* write_iter7 = &(write_iter6[64]); |
| 1338 |
for (uint32_t b64idx = 0; b64idx != buf0_row_b64width; ++b64idx) { |
| 1339 |
VecW loader0 = buf0_read_iter[b64idx * 4]; |
| 1340 |
VecW loader1 = buf0_read_iter[b64idx * 4 + 1]; |
| 1341 |
VecW loader2 = buf0_read_iter[b64idx * 4 + 2]; |
| 1342 |
VecW loader3 = buf0_read_iter[b64idx * 4 + 3]; |
| 1343 |
loader0 = vecw_shuffle8(loader0, gather_u16s); |
| 1344 |
loader1 = vecw_shuffle8(loader1, gather_u16s); |
| 1345 |
loader2 = vecw_shuffle8(loader2, gather_u16s); |
| 1346 |
loader3 = vecw_shuffle8(loader3, gather_u16s); |
| 1347 |
const VecW lo_0123 = vecw_unpacklo16(loader0, loader1); |
| 1348 |
const VecW lo_4567 = vecw_unpackhi16(loader0, loader1); |
| 1349 |
const VecW hi_0123 = vecw_unpacklo16(loader2, loader3); |
| 1350 |
const VecW hi_4567 = vecw_unpackhi16(loader2, loader3); |
| 1351 |
|
| 1352 |
const VecW final01 = vecw_unpacklo32(lo_0123, hi_0123); |
| 1353 |
const VecW final23 = vecw_unpackhi32(lo_0123, hi_0123); |
| 1354 |
const VecW final45 = vecw_unpacklo32(lo_4567, hi_4567); |
| 1355 |
const VecW final67 = vecw_unpackhi32(lo_4567, hi_4567); |
| 1356 |
write_iter0[b64idx] = vecw_extract64_0(final01); |
| 1357 |
write_iter1[b64idx] = vecw_extract64_1(final01); |
| 1358 |
write_iter2[b64idx] = vecw_extract64_0(final23); |
| 1359 |
write_iter3[b64idx] = vecw_extract64_1(final23); |
| 1360 |
write_iter4[b64idx] = vecw_extract64_0(final45); |
| 1361 |
write_iter5[b64idx] = vecw_extract64_1(final45); |
| 1362 |
write_iter6[b64idx] = vecw_extract64_0(final67); |
| 1363 |
write_iter7[b64idx] = vecw_extract64_1(final67); |
| 1364 |
} |
| 1365 |
buf0_read_iter = &(buf0_read_iter[256]); |
| 1366 |
write_iter0 = &(write_iter7[64]); |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
// 8 -> 2 |
| 1371 |
// This is similar to the main TransposeNybbleblock() loop. |
| 1372 |
const VecW* source_iter = R_CAST(VecW*, buf1); |
| 1373 |
const VecW m2 = VCONST_W(kMask3333); |
| 1374 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1375 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1376 |
const VecW gather_even = vecw_setr8(0, 2, 4, 6, 8, 10, 12, 14, |
| 1377 |
-1, -1, -1, -1, -1, -1, -1, -1); |
| 1378 |
// Take advantage of current function contract. |
| 1379 |
const uint32_t buf1_row_ct = (write_batch_size + 3) / 4; |
| 1380 |
|
| 1381 |
const uint32_t fourword_ct = DivUp(read_batch_size, 32); |
| 1382 |
uintptr_t* target_iter0 = write_iter; |
| 1383 |
for (uint32_t ridx = 0; ridx != buf1_row_ct; ++ridx) { |
| 1384 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 1385 |
uintptr_t* target_iter2 = &(target_iter1[write_ul_stride]); |
| 1386 |
uintptr_t* target_iter3 = &(target_iter2[write_ul_stride]); |
| 1387 |
for (uint32_t dvidx = 0; dvidx != fourword_ct; ++dvidx) { |
| 1388 |
const VecW loader0 = source_iter[dvidx * 2]; |
| 1389 |
const VecW loader1 = source_iter[dvidx * 2 + 1]; |
| 1390 |
|
| 1391 |
VecW even_nyps0 = loader0 & m2; |
| 1392 |
VecW even_nyps1 = loader1 & m2; |
| 1393 |
VecW odd_nyps0 = vecw_srli(loader0, 2) & m2; |
| 1394 |
VecW odd_nyps1 = vecw_srli(loader1, 2) & m2; |
| 1395 |
even_nyps0 = even_nyps0 | vecw_srli(even_nyps0, 6); |
| 1396 |
even_nyps1 = even_nyps1 | vecw_srli(even_nyps1, 6); |
| 1397 |
odd_nyps0 = odd_nyps0 | vecw_srli(odd_nyps0, 6); |
| 1398 |
odd_nyps1 = odd_nyps1 | vecw_srli(odd_nyps1, 6); |
| 1399 |
// Low four bits of even_nyps{0,1}[0], [2], ..., [14] are destined for |
| 1400 |
// target_iter0; high four bits of those bytes are destined for |
| 1401 |
// target_iter2. |
| 1402 |
const VecW even_nyps = vecw_gather_even(even_nyps0, even_nyps1, m8); |
| 1403 |
const VecW odd_nyps = vecw_gather_even(odd_nyps0, odd_nyps1, m8); |
| 1404 |
|
| 1405 |
VecW mod0_nyps = even_nyps & m4; |
| 1406 |
VecW mod1_nyps = odd_nyps & m4; |
| 1407 |
VecW mod2_nyps = vecw_srli(even_nyps, 4) & m4; |
| 1408 |
VecW mod3_nyps = vecw_srli(odd_nyps, 4) & m4; |
| 1409 |
mod0_nyps = mod0_nyps | vecw_srli(mod0_nyps, 4); |
| 1410 |
mod1_nyps = mod1_nyps | vecw_srli(mod1_nyps, 4); |
| 1411 |
mod2_nyps = mod2_nyps | vecw_srli(mod2_nyps, 4); |
| 1412 |
mod3_nyps = mod3_nyps | vecw_srli(mod3_nyps, 4); |
| 1413 |
mod0_nyps = vecw_shuffle8(mod0_nyps, gather_even); |
| 1414 |
mod1_nyps = vecw_shuffle8(mod1_nyps, gather_even); |
| 1415 |
mod2_nyps = vecw_shuffle8(mod2_nyps, gather_even); |
| 1416 |
mod3_nyps = vecw_shuffle8(mod3_nyps, gather_even); |
| 1417 |
target_iter0[dvidx] = vecw_extract64_0(mod0_nyps); |
| 1418 |
target_iter1[dvidx] = vecw_extract64_0(mod1_nyps); |
| 1419 |
target_iter2[dvidx] = vecw_extract64_0(mod2_nyps); |
| 1420 |
target_iter3[dvidx] = vecw_extract64_0(mod3_nyps); |
| 1421 |
} |
| 1422 |
source_iter = &(source_iter[32]); |
| 1423 |
target_iter0 = &(target_iter3[write_ul_stride]); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
# endif |
| 1427 |
#else // !USE_SSE2 |
| 1428 |
# ifdef __LP64__ |
| 1429 |
static_assert(kWordsPerVec == 1, "TransposeNypblock64() needs to be updated."); |
| 1430 |
void TransposeNypblock64(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* __restrict write_iter, unsigned char* __restrict buf0, unsigned char* __restrict buf1) { |
| 1431 |
// buf0 and buf1 must each be vector-aligned and have size 16k |
| 1432 |
// defining them as unsigned char* might prevent a strict-aliasing issue? |
| 1433 |
// (might need to go through greater contortions to actually be safe?) |
| 1434 |
const uint32_t buf_row_ct = NypCtToByteCt(write_batch_size); |
| 1435 |
// fold the first 6 shuffles into the initial ingestion loop |
| 1436 |
const unsigned char* initial_read_iter = DowncastKToUc(read_iter); |
| 1437 |
const unsigned char* initial_read_end = &(initial_read_iter[buf_row_ct]); |
| 1438 |
unsigned char* initial_target_iter = buf0; |
| 1439 |
const uint32_t read_byte_stride = read_ul_stride * kBytesPerWord; |
| 1440 |
const uint32_t read_batch_rem = kNypsPerCacheline - read_batch_size; |
| 1441 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 1442 |
const unsigned char* read_iter_tmp = initial_read_iter; |
| 1443 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 1444 |
*initial_target_iter++ = *read_iter_tmp; |
| 1445 |
read_iter_tmp = &(read_iter_tmp[read_byte_stride]); |
| 1446 |
} |
| 1447 |
initial_target_iter = memsetua(initial_target_iter, 0, read_batch_rem); |
| 1448 |
} |
| 1449 |
|
| 1450 |
// second-to-last shuffle, 8 bit spacing -> 4 |
| 1451 |
const uintptr_t* source_iter = R_CAST(uintptr_t*, buf0); |
| 1452 |
uintptr_t* target_iter0 = R_CAST(uintptr_t*, buf1); |
| 1453 |
const uint32_t write_word_ct = NypCtToWordCt(read_batch_size); |
| 1454 |
const uint32_t penult_inner_loop_iter_ct = 2 * write_word_ct; |
| 1455 |
const uint32_t cur_write_skip = 2 * kWordsPerCacheline - penult_inner_loop_iter_ct; |
| 1456 |
for (uint32_t uii = 0; uii != buf_row_ct; ++uii) { |
| 1457 |
uintptr_t* target_iter1 = &(target_iter0[kWordsPerCacheline * 2]); |
| 1458 |
for (uint32_t ujj = 0; ujj != penult_inner_loop_iter_ct; ++ujj) { |
| 1459 |
const uintptr_t source_word_lo = *source_iter++; |
| 1460 |
const uintptr_t source_word_hi = *source_iter++; |
| 1461 |
*target_iter0++ = PackTwo0F0FMask(source_word_lo, source_word_hi); |
| 1462 |
*target_iter1++ = PackTwo0F0FMask(source_word_lo >> 4, source_word_hi >> 4); |
| 1463 |
} |
| 1464 |
source_iter = &(source_iter[2 * cur_write_skip]); |
| 1465 |
target_iter0 = &(target_iter1[cur_write_skip]); |
| 1466 |
} |
| 1467 |
|
| 1468 |
// last shuffle, 4 bit spacing -> 2 |
| 1469 |
source_iter = R_CAST(uintptr_t*, buf1); |
| 1470 |
target_iter0 = write_iter; |
| 1471 |
const uint32_t last_loop_iter_ct = DivUp(write_batch_size, 2); |
| 1472 |
for (uint32_t uii = 0; uii != last_loop_iter_ct; ++uii) { |
| 1473 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 1474 |
for (uint32_t ujj = 0; ujj != write_word_ct; ++ujj) { |
| 1475 |
const uintptr_t source_word_lo = *source_iter++; |
| 1476 |
const uintptr_t source_word_hi = *source_iter++; |
| 1477 |
target_iter0[ujj] = PackTwo3333Mask(source_word_lo, source_word_hi); |
| 1478 |
target_iter1[ujj] = PackTwo3333Mask(source_word_lo >> 2, source_word_hi >> 2); |
| 1479 |
} |
| 1480 |
source_iter = &(source_iter[2 * (kWordsPerCacheline - write_word_ct)]); |
| 1481 |
target_iter0 = &(target_iter1[write_ul_stride]); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
# else |
| 1485 |
static_assert(kWordsPerVec == 1, "TransposeNypblock32() needs to be updated."); |
| 1486 |
void TransposeNypblock32(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* __restrict write_iter, unsigned char* __restrict buf0, unsigned char* __restrict buf1) { |
| 1487 |
// buf0 and buf1 must each be vector-aligned and have size 16k |
| 1488 |
// defining them as unsigned char* might prevent a strict-aliasing issue? |
| 1489 |
// (might need to go through greater contortions to actually be safe?) |
| 1490 |
const uint32_t buf_row_ct = NypCtToByteCt(write_batch_size); |
| 1491 |
// fold the first 6 shuffles into the initial ingestion loop |
| 1492 |
const unsigned char* initial_read_iter = DowncastKToUc(read_iter); |
| 1493 |
const unsigned char* initial_read_end = &(initial_read_iter[buf_row_ct]); |
| 1494 |
unsigned char* initial_target_iter = buf0; |
| 1495 |
const uint32_t read_byte_stride = read_ul_stride * kBytesPerWord; |
| 1496 |
const uint32_t read_batch_rem = kNypsPerCacheline - read_batch_size; |
| 1497 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 1498 |
const unsigned char* read_iter_tmp = initial_read_iter; |
| 1499 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 1500 |
*initial_target_iter++ = *read_iter_tmp; |
| 1501 |
read_iter_tmp = &(read_iter_tmp[read_byte_stride]); |
| 1502 |
} |
| 1503 |
initial_target_iter = memsetua(initial_target_iter, 0, read_batch_rem); |
| 1504 |
} |
| 1505 |
|
| 1506 |
// second-to-last shuffle, 8 bit spacing -> 4 |
| 1507 |
const uintptr_t* source_iter = R_CAST(uintptr_t*, buf0); |
| 1508 |
uintptr_t* target_iter0 = R_CAST(uintptr_t*, buf1); |
| 1509 |
const uint32_t write_word_ct = NypCtToWordCt(read_batch_size); |
| 1510 |
const uint32_t penult_inner_loop_iter_ct = 2 * write_word_ct; |
| 1511 |
const uint32_t cur_write_skip = 2 * kWordsPerCacheline - penult_inner_loop_iter_ct; |
| 1512 |
for (uint32_t uii = 0; uii != buf_row_ct; ++uii) { |
| 1513 |
uintptr_t* target_iter1 = &(target_iter0[kWordsPerCacheline * 2]); |
| 1514 |
for (uint32_t ujj = 0; ujj != penult_inner_loop_iter_ct; ++ujj) { |
| 1515 |
const uintptr_t source_word_lo = *source_iter++; |
| 1516 |
const uintptr_t source_word_hi = *source_iter++; |
| 1517 |
*target_iter0++ = PackTwo0F0FMask(source_word_lo, source_word_hi); |
| 1518 |
*target_iter1++ = PackTwo0F0FMask(source_word_lo >> 4, source_word_hi >> 4); |
| 1519 |
} |
| 1520 |
source_iter = &(source_iter[2 * cur_write_skip]); |
| 1521 |
target_iter0 = &(target_iter1[cur_write_skip]); |
| 1522 |
} |
| 1523 |
|
| 1524 |
// last shuffle, 4 bit spacing -> 2 |
| 1525 |
source_iter = R_CAST(uintptr_t*, buf1); |
| 1526 |
target_iter0 = write_iter; |
| 1527 |
const uint32_t last_loop_iter_ct = DivUp(write_batch_size, 2); |
| 1528 |
for (uint32_t uii = 0; uii != last_loop_iter_ct; ++uii) { |
| 1529 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 1530 |
for (uint32_t ujj = 0; ujj != write_word_ct; ++ujj) { |
| 1531 |
const uintptr_t source_word_lo = *source_iter++; |
| 1532 |
const uintptr_t source_word_hi = *source_iter++; |
| 1533 |
target_iter0[ujj] = PackTwo3333Mask(source_word_lo, source_word_hi); |
| 1534 |
target_iter1[ujj] = PackTwo3333Mask(source_word_lo >> 2, source_word_hi >> 2); |
| 1535 |
} |
| 1536 |
source_iter = &(source_iter[2 * (kWordsPerCacheline - write_word_ct)]); |
| 1537 |
target_iter0 = &(target_iter1[write_ul_stride]); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
# endif // !__LP64__ |
| 1541 |
#endif // !USE_SSE2 |
| 1542 |
|
| 1543 |
void BiallelicDosage16Invert(uint32_t dosage_ct, uint16_t* dosage_main) { |
| 1544 |
// replace each x with (32768 - x). |
| 1545 |
// compiler is smart enough to vectorize this. |
| 1546 |
for (uint32_t uii = 0; uii != dosage_ct; ++uii) { |
| 1547 |
dosage_main[uii] = 32768 - dosage_main[uii]; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
|
| 1551 |
void BiallelicDphase16Invert(uint32_t dphase_ct, int16_t* dphase_delta) { |
| 1552 |
for (uint32_t uii = 0; uii != dphase_ct; ++uii) { |
| 1553 |
dphase_delta[uii] = -dphase_delta[uii]; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
#if defined(USE_SSE2) && !defined(USE_AVX2) |
| 1558 |
void PackWordsToHalfwordsInvmatch(const uintptr_t* __restrict genoarr, uintptr_t inv_match_word, uint32_t inword_ct, uintptr_t* __restrict dst) { |
| 1559 |
// In shuffle8 case, this takes ~30% less time than a |
| 1560 |
// PackWordToHalfwordMask5555 loop. |
| 1561 |
const uint32_t out_fullvec_ct = inword_ct / (kWordsPerVec * 2); |
| 1562 |
const VecW xor_vec = vecw_set1(inv_match_word); |
| 1563 |
const VecW m1 = VCONST_W(kMask5555); |
| 1564 |
# ifdef USE_SHUFFLE8 |
| 1565 |
const VecW swap12 = vecw_setr8( |
| 1566 |
0, 1, 4, 5, 2, 3, 6, 7, |
| 1567 |
8, 9, 12, 13, 10, 11, 14, 15); |
| 1568 |
# else |
| 1569 |
const VecW m2 = VCONST_W(kMask3333); |
| 1570 |
# endif |
| 1571 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1572 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1573 |
for (uintptr_t vidx = 0; vidx != out_fullvec_ct; ++vidx) { |
| 1574 |
VecW vec_lo = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx])) ^ xor_vec; |
| 1575 |
VecW vec_hi = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx + kWordsPerVec])) ^ xor_vec; |
| 1576 |
vec_lo = vec_lo & vecw_srli(vec_lo, 1) & m1; |
| 1577 |
vec_hi = vec_hi & vecw_srli(vec_hi, 1) & m1; |
| 1578 |
# ifdef USE_SHUFFLE8 |
| 1579 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 3)) & m4; |
| 1580 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 3)) & m4; |
| 1581 |
vec_lo = vecw_shuffle8(swap12, vec_lo); |
| 1582 |
vec_hi = vecw_shuffle8(swap12, vec_hi); |
| 1583 |
# else |
| 1584 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 1)) & m2; |
| 1585 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 1)) & m2; |
| 1586 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 2)) & m4; |
| 1587 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 2)) & m4; |
| 1588 |
# endif |
| 1589 |
vec_lo = vec_lo | vecw_srli(vec_lo, 4); |
| 1590 |
vec_hi = vec_hi | vecw_srli(vec_hi, 4); |
| 1591 |
const VecW vec_packed = vecw_gather_even(vec_lo, vec_hi, m8); |
| 1592 |
vecw_storeu(&(dst[kWordsPerVec * vidx]), vec_packed); |
| 1593 |
} |
| 1594 |
Halfword* dst_alias = DowncastWToHW(dst); |
| 1595 |
uint32_t widx = RoundDownPow2(inword_ct, kWordsPerVec * 2); |
| 1596 |
for (; widx != inword_ct; ++widx) { |
| 1597 |
const uintptr_t cur_word = genoarr[widx] ^ inv_match_word; |
| 1598 |
const Halfword hw = PackWordToHalfwordMask5555(cur_word & (cur_word >> 1)); |
| 1599 |
dst_alias[widx] = hw; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
void PackWordsToHalfwordsMismatch(const uintptr_t* __restrict genoarr, uintptr_t mismatch_word, uint32_t inword_ct, uintptr_t* __restrict dst) { |
| 1604 |
const uint32_t out_fullvec_ct = inword_ct / (kWordsPerVec * 2); |
| 1605 |
const VecW xor_vec = vecw_set1(mismatch_word); |
| 1606 |
const VecW m1 = VCONST_W(kMask5555); |
| 1607 |
# ifdef USE_SHUFFLE8 |
| 1608 |
const VecW swap12 = vecw_setr8( |
| 1609 |
0, 1, 4, 5, 2, 3, 6, 7, |
| 1610 |
8, 9, 12, 13, 10, 11, 14, 15); |
| 1611 |
# else |
| 1612 |
const VecW m2 = VCONST_W(kMask3333); |
| 1613 |
# endif |
| 1614 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1615 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1616 |
for (uintptr_t vidx = 0; vidx != out_fullvec_ct; ++vidx) { |
| 1617 |
VecW vec_lo = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx])) ^ xor_vec; |
| 1618 |
VecW vec_hi = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx + kWordsPerVec])) ^ xor_vec; |
| 1619 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 1)) & m1; |
| 1620 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 1)) & m1; |
| 1621 |
# ifdef USE_SHUFFLE8 |
| 1622 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 3)) & m4; |
| 1623 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 3)) & m4; |
| 1624 |
vec_lo = vecw_shuffle8(swap12, vec_lo); |
| 1625 |
vec_hi = vecw_shuffle8(swap12, vec_hi); |
| 1626 |
# else |
| 1627 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 1)) & m2; |
| 1628 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 1)) & m2; |
| 1629 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 2)) & m4; |
| 1630 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 2)) & m4; |
| 1631 |
# endif |
| 1632 |
vec_lo = vec_lo | vecw_srli(vec_lo, 4); |
| 1633 |
vec_hi = vec_hi | vecw_srli(vec_hi, 4); |
| 1634 |
const VecW vec_packed = vecw_gather_even(vec_lo, vec_hi, m8); |
| 1635 |
vecw_storeu(&(dst[kWordsPerVec * vidx]), vec_packed); |
| 1636 |
} |
| 1637 |
Halfword* dst_alias = DowncastWToHW(dst); |
| 1638 |
uint32_t widx = RoundDownPow2(inword_ct, kWordsPerVec * 2); |
| 1639 |
for (; widx != inword_ct; ++widx) { |
| 1640 |
const uintptr_t cur_word = genoarr[widx] ^ mismatch_word; |
| 1641 |
const Halfword hw = PackWordToHalfwordMask5555(cur_word | (cur_word >> 1)); |
| 1642 |
dst_alias[widx] = hw; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
void MaskWordsToHalfwordsInvmatch(const uintptr_t* __restrict genoarr, uintptr_t inv_match_word, uint32_t inword_ct, uintptr_t* src, uintptr_t* dst) { |
| 1647 |
const uint32_t out_fullvec_ct = inword_ct / (kWordsPerVec * 2); |
| 1648 |
const VecW xor_vec = vecw_set1(inv_match_word); |
| 1649 |
const VecW m1 = VCONST_W(kMask5555); |
| 1650 |
# ifdef USE_SHUFFLE8 |
| 1651 |
const VecW swap12 = vecw_setr8( |
| 1652 |
0, 1, 4, 5, 2, 3, 6, 7, |
| 1653 |
8, 9, 12, 13, 10, 11, 14, 15); |
| 1654 |
# else |
| 1655 |
const VecW m2 = VCONST_W(kMask3333); |
| 1656 |
# endif |
| 1657 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1658 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1659 |
for (uintptr_t vidx = 0; vidx != out_fullvec_ct; ++vidx) { |
| 1660 |
VecW vec_lo = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx])) ^ xor_vec; |
| 1661 |
VecW vec_hi = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx + kWordsPerVec])) ^ xor_vec; |
| 1662 |
VecW src_vec = vecw_loadu(&(src[kWordsPerVec * vidx])); |
| 1663 |
vec_lo = vec_lo & vecw_srli(vec_lo, 1) & m1; |
| 1664 |
vec_hi = vec_hi & vecw_srli(vec_hi, 1) & m1; |
| 1665 |
# ifdef USE_SHUFFLE8 |
| 1666 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 3)) & m4; |
| 1667 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 3)) & m4; |
| 1668 |
vec_lo = vecw_shuffle8(swap12, vec_lo); |
| 1669 |
vec_hi = vecw_shuffle8(swap12, vec_hi); |
| 1670 |
# else |
| 1671 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 1)) & m2; |
| 1672 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 1)) & m2; |
| 1673 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 2)) & m4; |
| 1674 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 2)) & m4; |
| 1675 |
# endif |
| 1676 |
vec_lo = vec_lo | vecw_srli(vec_lo, 4); |
| 1677 |
vec_hi = vec_hi | vecw_srli(vec_hi, 4); |
| 1678 |
const VecW vec_packed = vecw_gather_even(vec_lo, vec_hi, m8); |
| 1679 |
vecw_storeu(&(dst[kWordsPerVec * vidx]), src_vec & vec_packed); |
| 1680 |
} |
| 1681 |
Halfword* src_alias = DowncastWToHW(src); |
| 1682 |
Halfword* dst_alias = DowncastWToHW(dst); |
| 1683 |
uint32_t widx = RoundDownPow2(inword_ct, kWordsPerVec * 2); |
| 1684 |
for (; widx != inword_ct; ++widx) { |
| 1685 |
const uintptr_t cur_word = genoarr[widx] ^ inv_match_word; |
| 1686 |
const Halfword hw = PackWordToHalfwordMask5555(cur_word & (cur_word >> 1)); |
| 1687 |
dst_alias[widx] = src_alias[widx] & hw; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
#else |
| 1691 |
void PackWordsToHalfwordsInvmatch(const uintptr_t* __restrict genoarr, uintptr_t inv_match_word, uint32_t inword_ct, uintptr_t* __restrict dst) { |
| 1692 |
Halfword* dst_alias = DowncastWToHW(dst); |
| 1693 |
for (uint32_t widx = 0; widx != inword_ct; ++widx) { |
| 1694 |
const uintptr_t cur_word = genoarr[widx] ^ inv_match_word; |
| 1695 |
const Halfword hw = PackWordToHalfwordMask5555(cur_word & (cur_word >> 1)); |
| 1696 |
dst_alias[widx] = hw; |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
void PackWordsToHalfwordsMismatch(const uintptr_t* __restrict genoarr, uintptr_t mismatch_word, uint32_t inword_ct, uintptr_t* __restrict dst) { |
| 1701 |
Halfword* dst_alias = DowncastWToHW(dst); |
| 1702 |
for (uint32_t widx = 0; widx != inword_ct; ++widx) { |
| 1703 |
const uintptr_t cur_word = genoarr[widx] ^ mismatch_word; |
| 1704 |
const Halfword hw = PackWordToHalfwordMask5555(cur_word | (cur_word >> 1)); |
| 1705 |
dst_alias[widx] = hw; |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
void MaskWordsToHalfwordsInvmatch(const uintptr_t* __restrict genoarr, uintptr_t inv_match_word, uint32_t inword_ct, uintptr_t* src, uintptr_t* dst) { |
| 1710 |
Halfword* src_alias = DowncastWToHW(src); |
| 1711 |
Halfword* dst_alias = DowncastWToHW(dst); |
| 1712 |
for (uint32_t widx = 0; widx != inword_ct; ++widx) { |
| 1713 |
const uintptr_t cur_word = genoarr[widx] ^ inv_match_word; |
| 1714 |
const Halfword hw = PackWordToHalfwordMask5555(cur_word & (cur_word >> 1)); |
| 1715 |
dst_alias[widx] = src_alias[widx] & hw; |
| 1716 |
} |
| 1717 |
} |
| 1718 |
#endif |
| 1719 |
|
| 1720 |
void SparseToMissingness(const uintptr_t* __restrict raregeno, const uint32_t* difflist_sample_ids, uint32_t sample_ct, uint32_t difflist_common_geno, uint32_t difflist_len, uintptr_t* __restrict missingness) { |
| 1721 |
if (difflist_common_geno != 3) { |
| 1722 |
const uint32_t sample_ctl = BitCtToWordCt(sample_ct); |
| 1723 |
ZeroWArr(sample_ctl, missingness); |
| 1724 |
if (!difflist_len) { |
| 1725 |
return; |
| 1726 |
} |
| 1727 |
const uint32_t raregeno_word_ct = NypCtToWordCt(difflist_len); |
| 1728 |
for (uint32_t widx = 0; widx != raregeno_word_ct; ++widx) { |
| 1729 |
const uintptr_t raregeno_word = raregeno[widx]; |
| 1730 |
uintptr_t raregeno_11 = raregeno_word & (raregeno_word >> 1) & kMask5555; |
| 1731 |
if (raregeno_11) { |
| 1732 |
const uint32_t* cur_difflist_sample_ids = &(difflist_sample_ids[widx * kBitsPerWordD2]); |
| 1733 |
do { |
| 1734 |
const uint32_t sample_idx_lowbits = ctzw(raregeno_11) / 2; |
| 1735 |
const uint32_t cur_sample_id = cur_difflist_sample_ids[sample_idx_lowbits]; |
| 1736 |
SetBit(cur_sample_id, missingness); |
| 1737 |
raregeno_11 &= raregeno_11 - 1; |
| 1738 |
} while (raregeno_11); |
| 1739 |
} |
| 1740 |
} |
| 1741 |
} else { |
| 1742 |
SetAllBits(sample_ct, missingness); |
| 1743 |
// Don't need to look at raregeno, all cases are nonmissing. |
| 1744 |
for (uint32_t uii = 0; uii != difflist_len; ++uii) { |
| 1745 |
const uint32_t cur_sample_id = difflist_sample_ids[uii]; |
| 1746 |
ClearBit(cur_sample_id, missingness); |
| 1747 |
} |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
#if defined(USE_SSE2) && !defined(USE_AVX2) |
| 1752 |
void SplitHomRef2hetUnsafeW(const uintptr_t* __restrict genoarr, uint32_t inword_ct, uintptr_t* __restrict hom_buf, uintptr_t* __restrict ref2het_buf) { |
| 1753 |
const uint32_t out_fullvec_ct = inword_ct / (kWordsPerVec * 2); |
| 1754 |
// In shuffle8 case, this takes ~55% less time than unvectorized loop. |
| 1755 |
// homozygous: geno = 0 or 2 |
| 1756 |
// ref2het: geno = 0 or 1 |
| 1757 |
const VecW m1 = VCONST_W(kMask5555); |
| 1758 |
# ifdef USE_SHUFFLE8 |
| 1759 |
const VecW swap12 = vecw_setr8( |
| 1760 |
0, 1, 4, 5, 2, 3, 6, 7, |
| 1761 |
8, 9, 12, 13, 10, 11, 14, 15); |
| 1762 |
# else |
| 1763 |
const VecW m2 = VCONST_W(kMask3333); |
| 1764 |
# endif |
| 1765 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1766 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1767 |
for (uintptr_t vidx = 0; vidx != out_fullvec_ct; ++vidx) { |
| 1768 |
const VecW vec_lo = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx])); |
| 1769 |
const VecW vec_hi = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx + kWordsPerVec])); |
| 1770 |
VecW hom_lo = vecw_and_notfirst(vec_lo, m1); |
| 1771 |
VecW r2h_lo = vecw_and_notfirst(vecw_srli(vec_lo, 1), m1); |
| 1772 |
VecW hom_hi = vecw_and_notfirst(vec_hi, m1); |
| 1773 |
VecW r2h_hi = vecw_and_notfirst(vecw_srli(vec_hi, 1), m1); |
| 1774 |
# ifdef USE_SHUFFLE8 |
| 1775 |
hom_lo = (hom_lo | vecw_srli(hom_lo, 3)) & m4; |
| 1776 |
r2h_lo = (r2h_lo | vecw_srli(r2h_lo, 3)) & m4; |
| 1777 |
hom_hi = (hom_hi | vecw_srli(hom_hi, 3)) & m4; |
| 1778 |
r2h_hi = (r2h_hi | vecw_srli(r2h_hi, 3)) & m4; |
| 1779 |
hom_lo = vecw_shuffle8(swap12, hom_lo); |
| 1780 |
r2h_lo = vecw_shuffle8(swap12, r2h_lo); |
| 1781 |
hom_hi = vecw_shuffle8(swap12, hom_hi); |
| 1782 |
r2h_hi = vecw_shuffle8(swap12, r2h_hi); |
| 1783 |
# else |
| 1784 |
hom_lo = (hom_lo | vecw_srli(hom_lo, 1)) & m2; |
| 1785 |
r2h_lo = (r2h_lo | vecw_srli(r2h_lo, 1)) & m2; |
| 1786 |
hom_hi = (hom_hi | vecw_srli(hom_hi, 1)) & m2; |
| 1787 |
r2h_hi = (r2h_hi | vecw_srli(r2h_hi, 1)) & m2; |
| 1788 |
hom_lo = (hom_lo | vecw_srli(hom_lo, 2)) & m4; |
| 1789 |
r2h_lo = (r2h_lo | vecw_srli(r2h_lo, 2)) & m4; |
| 1790 |
hom_hi = (hom_hi | vecw_srli(hom_hi, 2)) & m4; |
| 1791 |
r2h_hi = (r2h_hi | vecw_srli(r2h_hi, 2)) & m4; |
| 1792 |
# endif |
| 1793 |
hom_lo = hom_lo | vecw_srli(hom_lo, 4); |
| 1794 |
r2h_lo = r2h_lo | vecw_srli(r2h_lo, 4); |
| 1795 |
hom_hi = hom_hi | vecw_srli(hom_hi, 4); |
| 1796 |
r2h_hi = r2h_hi | vecw_srli(r2h_hi, 4); |
| 1797 |
const VecW hom_packed = vecw_gather_even(hom_lo, hom_hi, m8); |
| 1798 |
const VecW r2h_packed = vecw_gather_even(r2h_lo, r2h_hi, m8); |
| 1799 |
vecw_storeu(&(hom_buf[kWordsPerVec * vidx]), hom_packed); |
| 1800 |
vecw_storeu(&(ref2het_buf[kWordsPerVec * vidx]), r2h_packed); |
| 1801 |
} |
| 1802 |
Halfword* hom_alias = DowncastWToHW(hom_buf); |
| 1803 |
Halfword* r2h_alias = DowncastWToHW(ref2het_buf); |
| 1804 |
uint32_t widx = RoundDownPow2(inword_ct, kWordsPerVec * 2); |
| 1805 |
for (; widx != inword_ct; ++widx) { |
| 1806 |
const uintptr_t inv_geno_word = ~genoarr[widx]; |
| 1807 |
hom_alias[widx] = PackWordToHalfwordMask5555(inv_geno_word); |
| 1808 |
r2h_alias[widx] = PackWordToHalfwordMaskAAAA(inv_geno_word); |
| 1809 |
} |
| 1810 |
} |
| 1811 |
#else |
| 1812 |
void SplitHomRef2hetUnsafeW(const uintptr_t* genoarr, uint32_t inword_ct, uintptr_t* __restrict hom_buf, uintptr_t* __restrict ref2het_buf) { |
| 1813 |
Halfword* hom_alias = DowncastWToHW(hom_buf); |
| 1814 |
Halfword* ref2het_alias = DowncastWToHW(ref2het_buf); |
| 1815 |
for (uint32_t widx = 0; widx != inword_ct; ++widx) { |
| 1816 |
const uintptr_t inv_geno_word = ~genoarr[widx]; |
| 1817 |
hom_alias[widx] = PackWordToHalfwordMask5555(inv_geno_word); |
| 1818 |
ref2het_alias[widx] = PackWordToHalfwordMaskAAAA(inv_geno_word); |
| 1819 |
} |
| 1820 |
} |
| 1821 |
#endif |
| 1822 |
|
| 1823 |
void SplitHomRef2het(const uintptr_t* genoarr, uint32_t sample_ct, uintptr_t* __restrict hom_buf, uintptr_t* __restrict ref2het_buf) { |
| 1824 |
const uint32_t full_outword_ct = sample_ct / kBitsPerWord; |
| 1825 |
SplitHomRef2hetUnsafeW(genoarr, full_outword_ct * 2, hom_buf, ref2het_buf); |
| 1826 |
const uint32_t remainder = sample_ct % kBitsPerWord; |
| 1827 |
if (remainder) { |
| 1828 |
uintptr_t geno_word = genoarr[full_outword_ct * 2]; |
| 1829 |
uintptr_t hom_word = PackWordToHalfwordMask5555(~geno_word); |
| 1830 |
uintptr_t ref2het_word = PackWordToHalfwordMaskAAAA(~geno_word); |
| 1831 |
if (remainder > kBitsPerWordD2) { |
| 1832 |
geno_word = genoarr[full_outword_ct * 2 + 1]; |
| 1833 |
hom_word |= S_CAST(uintptr_t, PackWordToHalfwordMask5555(~geno_word)) << kBitsPerWordD2; |
| 1834 |
ref2het_word |= S_CAST(uintptr_t, PackWordToHalfwordMaskAAAA(~geno_word)) << kBitsPerWordD2; |
| 1835 |
} |
| 1836 |
const uintptr_t cur_mask = (k1LU << remainder) - 1; |
| 1837 |
hom_buf[full_outword_ct] = hom_word & cur_mask; |
| 1838 |
ref2het_buf[full_outword_ct] = ref2het_word & cur_mask; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
BoolErr HapsplitMustPhased(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, uint32_t sample_ct, uint32_t phase_exists, uintptr_t* hap_arr, uintptr_t* nm_arr) { |
| 1843 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 1844 |
uintptr_t detect_unphased = 0; |
| 1845 |
if (!phase_exists) { |
| 1846 |
// error out if het encountered |
| 1847 |
for (uint32_t widx = 0; widx != sample_ctl2; ++widx) { |
| 1848 |
const uintptr_t geno_word = genoarr[widx]; |
| 1849 |
const uintptr_t nm_word = 3 * (kMask5555 & (~(geno_word & (geno_word >> 1)))); |
| 1850 |
const uintptr_t geno_nm = geno_word & nm_word; |
| 1851 |
// geno_nm is now {00, 01, 10, 00}. In particular, if we ever see 01, we |
| 1852 |
// error out. |
| 1853 |
const uintptr_t geno_nm_hi = (geno_nm >> 1) & kMask5555; |
| 1854 |
nm_arr[widx] = nm_word; |
| 1855 |
hap_arr[widx] = geno_nm | geno_nm_hi; |
| 1856 |
detect_unphased |= geno_nm; |
| 1857 |
} |
| 1858 |
detect_unphased &= kMask5555; |
| 1859 |
} else { |
| 1860 |
// error out if het encountered, and not covered by phasepresent |
| 1861 |
const Halfword* phasepresent_alias = DowncastKWToHW(phasepresent); |
| 1862 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 1863 |
for (uint32_t widx = 0; widx != sample_ctl2; ++widx) { |
| 1864 |
const uintptr_t geno_word = genoarr[widx]; |
| 1865 |
const uintptr_t geno_rshift = geno_word >> 1; |
| 1866 |
const uintptr_t nm_word = 3 * (kMask5555 & (~(geno_word & geno_rshift))); |
| 1867 |
const uintptr_t geno_nm = geno_word & nm_word; |
| 1868 |
const uintptr_t geno_nm_hi = (geno_nm >> 1) & kMask5555; |
| 1869 |
const uintptr_t het_word = geno_nm & kMask5555; |
| 1870 |
const uintptr_t phasepresent_word = UnpackHalfwordToWord(phasepresent_alias[widx]); |
| 1871 |
const uintptr_t phaseinfo_word = phasepresent_word & UnpackHalfwordToWord(phaseinfo_alias[widx]); |
| 1872 |
nm_arr[widx] = nm_word; |
| 1873 |
hap_arr[widx] = geno_nm + geno_nm_hi + phaseinfo_word; |
| 1874 |
detect_unphased |= het_word & (~phasepresent_word); |
| 1875 |
} |
| 1876 |
} |
| 1877 |
const uint32_t trailing_nyp_ct = sample_ct % kBitsPerWordD2; |
| 1878 |
if (trailing_nyp_ct) { |
| 1879 |
const uint32_t trailing_bit_ct = trailing_nyp_ct * 2; |
| 1880 |
const uint32_t last_word_idx = sample_ctl2 - 1; |
| 1881 |
nm_arr[last_word_idx] = bzhi(nm_arr[last_word_idx], trailing_bit_ct); |
| 1882 |
hap_arr[last_word_idx] = bzhi(hap_arr[last_word_idx], trailing_bit_ct); |
| 1883 |
} |
| 1884 |
return (detect_unphased != 0); |
| 1885 |
} |
| 1886 |
|
| 1887 |
#if defined(USE_SSE2) && !defined(USE_AVX2) |
| 1888 |
void HapsplitHaploid(const uintptr_t* __restrict genoarr, uint32_t sample_ct, uintptr_t* __restrict hap_arr, uintptr_t* __restrict nm_arr) { |
| 1889 |
// In shuffle8 case, this takes ~40% less time than unvectorized loop. |
| 1890 |
const uint32_t sample_ctl = BitCtToWordCt(sample_ct); |
| 1891 |
const uint32_t sample_ctl2_is_odd = NypCtToWordCt(sample_ct) & 1; |
| 1892 |
const uint32_t wordpair_ct = sample_ctl - sample_ctl2_is_odd; |
| 1893 |
const uint32_t out_fullvec_ct = wordpair_ct / 2; |
| 1894 |
const VecW m1 = VCONST_W(kMask5555); |
| 1895 |
# ifdef USE_SHUFFLE8 |
| 1896 |
const VecW swap12 = vecw_setr8( |
| 1897 |
0, 1, 4, 5, 2, 3, 6, 7, |
| 1898 |
8, 9, 12, 13, 10, 11, 14, 15); |
| 1899 |
# else |
| 1900 |
const VecW m2 = VCONST_W(kMask3333); |
| 1901 |
# endif |
| 1902 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1903 |
const VecW m8 = VCONST_W(kMask00FF); |
| 1904 |
for (uintptr_t vidx = 0; vidx != out_fullvec_ct; ++vidx) { |
| 1905 |
const VecW vec_lo = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx])); |
| 1906 |
const VecW vec_hi = vecw_loadu(&(genoarr[2 * kWordsPerVec * vidx + kWordsPerVec])); |
| 1907 |
VecW nm_lo = vecw_and_notfirst(vec_lo, m1); |
| 1908 |
VecW nm_hi = vecw_and_notfirst(vec_hi, m1); |
| 1909 |
VecW hap_lo = nm_lo & vecw_srli(vec_lo, 1); |
| 1910 |
VecW hap_hi = nm_hi & vecw_srli(vec_hi, 1); |
| 1911 |
# ifdef USE_SHUFFLE8 |
| 1912 |
nm_lo = (nm_lo | vecw_srli(nm_lo, 3)) & m4; |
| 1913 |
nm_hi = (nm_hi | vecw_srli(nm_hi, 3)) & m4; |
| 1914 |
hap_lo = (hap_lo | vecw_srli(hap_lo, 3)) & m4; |
| 1915 |
hap_hi = (hap_hi | vecw_srli(hap_hi, 3)) & m4; |
| 1916 |
nm_lo = vecw_shuffle8(swap12, nm_lo); |
| 1917 |
nm_hi = vecw_shuffle8(swap12, nm_hi); |
| 1918 |
hap_lo = vecw_shuffle8(swap12, hap_lo); |
| 1919 |
hap_hi = vecw_shuffle8(swap12, hap_hi); |
| 1920 |
# else |
| 1921 |
nm_lo = (nm_lo | vecw_srli(nm_lo, 1)) & m2; |
| 1922 |
nm_hi = (nm_hi | vecw_srli(nm_hi, 1)) & m2; |
| 1923 |
hap_lo = (hap_lo | vecw_srli(hap_lo, 1)) & m2; |
| 1924 |
hap_hi = (hap_hi | vecw_srli(hap_hi, 1)) & m2; |
| 1925 |
nm_lo = (nm_lo | vecw_srli(nm_lo, 2)) & m4; |
| 1926 |
nm_hi = (nm_hi | vecw_srli(nm_hi, 2)) & m4; |
| 1927 |
hap_lo = (hap_lo | vecw_srli(hap_lo, 2)) & m4; |
| 1928 |
hap_hi = (hap_hi | vecw_srli(hap_hi, 2)) & m4; |
| 1929 |
# endif |
| 1930 |
nm_lo = nm_lo | vecw_srli(nm_lo, 4); |
| 1931 |
nm_hi = nm_hi | vecw_srli(nm_hi, 4); |
| 1932 |
hap_lo = hap_lo | vecw_srli(hap_lo, 4); |
| 1933 |
hap_hi = hap_hi | vecw_srli(hap_hi, 4); |
| 1934 |
const VecW nm_packed = vecw_gather_even(nm_lo, nm_hi, m8); |
| 1935 |
const VecW hap_packed = vecw_gather_even(hap_lo, hap_hi, m8); |
| 1936 |
vecw_storeu(&(nm_arr[kWordsPerVec * vidx]), nm_packed); |
| 1937 |
vecw_storeu(&(hap_arr[kWordsPerVec * vidx]), hap_packed); |
| 1938 |
} |
| 1939 |
if (wordpair_ct % 2) { |
| 1940 |
const uint32_t widx = wordpair_ct - 1; |
| 1941 |
const uintptr_t geno_word0 = genoarr[widx * 2]; |
| 1942 |
const uintptr_t geno_word1 = genoarr[widx * 2 + 1]; |
| 1943 |
const uintptr_t nm_word0 = ~geno_word0; |
| 1944 |
const uintptr_t nm_word1 = ~geno_word1; |
| 1945 |
const uintptr_t hap_word0 = nm_word0 & (geno_word0 >> 1); |
| 1946 |
const uintptr_t hap_word1 = nm_word1 & (geno_word1 >> 1); |
| 1947 |
nm_arr[widx] = PackTwo5555Mask(nm_word0, nm_word1); |
| 1948 |
hap_arr[widx] = PackTwo5555Mask(hap_word0, hap_word1); |
| 1949 |
} |
| 1950 |
if (sample_ctl2_is_odd) { |
| 1951 |
const uintptr_t geno_word0 = genoarr[wordpair_ct * 2]; |
| 1952 |
const uintptr_t nm_word0 = ~geno_word0; |
| 1953 |
const uintptr_t hap_word0 = nm_word0 & (geno_word0 >> 1); |
| 1954 |
nm_arr[wordpair_ct] = PackWordToHalfwordMask5555(nm_word0); |
| 1955 |
hap_arr[wordpair_ct] = PackWordToHalfwordMask5555(hap_word0); |
| 1956 |
} |
| 1957 |
const uint32_t trailing_bit_ct = sample_ct % kBitsPerWord; |
| 1958 |
if (trailing_bit_ct) { |
| 1959 |
const uint32_t last_word_idx = sample_ctl - 1; |
| 1960 |
nm_arr[last_word_idx] = bzhi(nm_arr[last_word_idx], trailing_bit_ct); |
| 1961 |
hap_arr[last_word_idx] = bzhi(hap_arr[last_word_idx], trailing_bit_ct); |
| 1962 |
} |
| 1963 |
} |
| 1964 |
#else |
| 1965 |
void HapsplitHaploid(const uintptr_t* __restrict genoarr, uint32_t sample_ct, uintptr_t* __restrict hap_arr, uintptr_t* __restrict nm_arr) { |
| 1966 |
const uint32_t sample_ctl = BitCtToWordCt(sample_ct); |
| 1967 |
const uint32_t sample_ctl2_is_odd = NypCtToWordCt(sample_ct) & 1; |
| 1968 |
const uint32_t wordpair_ct = sample_ctl - sample_ctl2_is_odd; |
| 1969 |
for (uint32_t widx = 0; widx != wordpair_ct; ++widx) { |
| 1970 |
const uintptr_t geno_word0 = genoarr[widx * 2]; |
| 1971 |
const uintptr_t geno_word1 = genoarr[widx * 2 + 1]; |
| 1972 |
const uintptr_t nm_word0 = ~geno_word0; |
| 1973 |
const uintptr_t nm_word1 = ~geno_word1; |
| 1974 |
const uintptr_t hap_word0 = nm_word0 & (geno_word0 >> 1); |
| 1975 |
const uintptr_t hap_word1 = nm_word1 & (geno_word1 >> 1); |
| 1976 |
nm_arr[widx] = PackTwo5555Mask(nm_word0, nm_word1); |
| 1977 |
hap_arr[widx] = PackTwo5555Mask(hap_word0, hap_word1); |
| 1978 |
} |
| 1979 |
if (sample_ctl2_is_odd) { |
| 1980 |
const uintptr_t geno_word0 = genoarr[wordpair_ct * 2]; |
| 1981 |
const uintptr_t nm_word0 = ~geno_word0; |
| 1982 |
const uintptr_t hap_word0 = nm_word0 & (geno_word0 >> 1); |
| 1983 |
nm_arr[wordpair_ct] = PackWordToHalfwordMask5555(nm_word0); |
| 1984 |
hap_arr[wordpair_ct] = PackWordToHalfwordMask5555(hap_word0); |
| 1985 |
} |
| 1986 |
const uint32_t trailing_bit_ct = sample_ct % kBitsPerWord; |
| 1987 |
if (trailing_bit_ct) { |
| 1988 |
const uint32_t last_word_idx = sample_ctl - 1; |
| 1989 |
nm_arr[last_word_idx] = bzhi(nm_arr[last_word_idx], trailing_bit_ct); |
| 1990 |
hap_arr[last_word_idx] = bzhi(hap_arr[last_word_idx], trailing_bit_ct); |
| 1991 |
} |
| 1992 |
} |
| 1993 |
#endif |
| 1994 |
|
| 1995 |
#ifdef USE_SHUFFLE8 |
| 1996 |
// ~70% less time than per-byte lookup. |
| 1997 |
void GenoarrLookup256x1bx4(const uintptr_t* genoarr, const void* table256x1bx4, uint32_t sample_ct, void* __restrict result) { |
| 1998 |
const uint32_t* table_alias = S_CAST(const uint32_t*, table256x1bx4); |
| 1999 |
const unsigned char* genoarr_alias = DowncastKToUc(genoarr); |
| 2000 |
unsigned char* resultb = S_CAST(unsigned char*, result); |
| 2001 |
const uint32_t full_byte_ct = sample_ct / 4; |
| 2002 |
if (full_byte_ct >= kBytesPerVec) { |
| 2003 |
const uint32_t last_genoarr_offset = full_byte_ct - kBytesPerVec; |
| 2004 |
const VecW lookup_even = vecw_loadu(&(table_alias[256])); |
| 2005 |
const VecW lookup_odd = vecw_loadu(&(table_alias[256 + kInt32PerVec])); |
| 2006 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 2007 |
for (uint32_t genoarr_offset = 0; ; genoarr_offset += kBytesPerVec) { |
| 2008 |
if (genoarr_offset >= last_genoarr_offset) { |
| 2009 |
if (genoarr_offset == full_byte_ct) { |
| 2010 |
break; |
| 2011 |
} |
| 2012 |
genoarr_offset = last_genoarr_offset; |
| 2013 |
} |
| 2014 |
VecW cur_vec = vecw_loadu(&(genoarr_alias[genoarr_offset])); |
| 2015 |
VecW vec_lo; |
| 2016 |
VecW vec_hi; |
| 2017 |
vecw_lo_and_hi_nybbles(cur_vec, m4, &vec_lo, &vec_hi); |
| 2018 |
VecW result_lo_even = vecw_shuffle8(lookup_even, vec_lo); |
| 2019 |
VecW result_hi_even = vecw_shuffle8(lookup_even, vec_hi); |
| 2020 |
VecW result_lo_odd = vecw_shuffle8(lookup_odd, vec_lo); |
| 2021 |
VecW result_hi_odd = vecw_shuffle8(lookup_odd, vec_hi); |
| 2022 |
// In AVX2 case, result_lo_even has (0, 2, 4, ..., 62), and result_lo_odd |
| 2023 |
// has (1, 3, 5, ..., 63). |
| 2024 |
// even -> (0, 2, 4, ..., 14, 32, ..., 46, 16, ..., 30, 48, ..., 62) |
| 2025 |
// odd -> (1, 3, 5, ..., 15, 33, ..., 47, 17, ..., 31, 49, ..., 63) |
| 2026 |
// Then unpacklo yields (0, 1, 2, ..., 31). |
| 2027 |
result_lo_even = vecw_permute0xd8_if_avx2(result_lo_even); |
| 2028 |
result_lo_odd = vecw_permute0xd8_if_avx2(result_lo_odd); |
| 2029 |
result_hi_even = vecw_permute0xd8_if_avx2(result_hi_even); |
| 2030 |
result_hi_odd = vecw_permute0xd8_if_avx2(result_hi_odd); |
| 2031 |
const VecW result0 = vecw_unpacklo8(result_lo_even, result_lo_odd); |
| 2032 |
const VecW result1 = vecw_unpackhi8(result_lo_even, result_lo_odd); |
| 2033 |
const VecW result2 = vecw_unpacklo8(result_hi_even, result_hi_odd); |
| 2034 |
const VecW result3 = vecw_unpackhi8(result_hi_even, result_hi_odd); |
| 2035 |
unsigned char* cur_resultb = &(resultb[genoarr_offset * 4]); |
| 2036 |
vecw_storeu(cur_resultb, result0); |
| 2037 |
vecw_storeu(&(cur_resultb[kBytesPerVec]), result1); |
| 2038 |
vecw_storeu(&(cur_resultb[2 * kBytesPerVec]), result2); |
| 2039 |
vecw_storeu(&(cur_resultb[3 * kBytesPerVec]), result3); |
| 2040 |
} |
| 2041 |
} else { |
| 2042 |
for (uint32_t byte_idx = 0; byte_idx != full_byte_ct; ++byte_idx) { |
| 2043 |
CopyToUnalignedOffsetU32(resultb, &(table_alias[genoarr_alias[byte_idx]]), byte_idx); |
| 2044 |
} |
| 2045 |
} |
| 2046 |
const uint32_t remainder = sample_ct % 4; |
| 2047 |
if (remainder) { |
| 2048 |
unsigned char* result_last = &(resultb[full_byte_ct * 4]); |
| 2049 |
uintptr_t geno_byte = genoarr_alias[full_byte_ct]; |
| 2050 |
for (uint32_t uii = 0; uii != remainder; ++uii) { |
| 2051 |
result_last[uii] = table_alias[geno_byte & 3]; |
| 2052 |
geno_byte >>= 2; |
| 2053 |
} |
| 2054 |
} |
| 2055 |
} |
| 2056 |
#else |
| 2057 |
void GenoarrLookup256x1bx4(const uintptr_t* genoarr, const void* table256x1bx4, uint32_t sample_ct, void* __restrict result) { |
| 2058 |
const uint32_t* table_alias = S_CAST(const uint32_t*, table256x1bx4); |
| 2059 |
const unsigned char* genoarr_alias = DowncastKToUc(genoarr); |
| 2060 |
unsigned char* resultb = S_CAST(unsigned char*, result); |
| 2061 |
const uint32_t full_byte_ct = sample_ct / 4; |
| 2062 |
for (uint32_t byte_idx = 0; byte_idx != full_byte_ct; ++byte_idx) { |
| 2063 |
CopyToUnalignedOffsetU32(resultb, &(table_alias[genoarr_alias[byte_idx]]), byte_idx); |
| 2064 |
} |
| 2065 |
const uint32_t remainder = sample_ct % 4; |
| 2066 |
if (remainder) { |
| 2067 |
unsigned char* result_last = &(resultb[full_byte_ct * 4]); |
| 2068 |
uintptr_t geno_byte = genoarr_alias[full_byte_ct]; |
| 2069 |
for (uint32_t uii = 0; uii != remainder; ++uii) { |
| 2070 |
result_last[uii] = table_alias[geno_byte & 3]; |
| 2071 |
geno_byte >>= 2; |
| 2072 |
} |
| 2073 |
} |
| 2074 |
} |
| 2075 |
#endif |
| 2076 |
|
| 2077 |
#ifndef NO_UNALIGNED |
| 2078 |
void GenoarrLookup16x4bx2(const uintptr_t* genoarr, const void* table16x4bx2, uint32_t sample_ct, void* __restrict result) { |
| 2079 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table16x4bx2); |
| 2080 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2081 |
const uint32_t sample_ctl2m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2082 |
uint32_t loop_len = kBitsPerWordD4; |
| 2083 |
uintptr_t geno_word = 0; |
| 2084 |
for (uint32_t widx = 0; ; ++widx) { |
| 2085 |
if (widx >= sample_ctl2m1) { |
| 2086 |
if (widx > sample_ctl2m1) { |
| 2087 |
if (sample_ct % 2) { |
| 2088 |
// not a regular CopyToUnaligned, we're only copying the bottom half |
| 2089 |
// of the element |
| 2090 |
memcpy(result_biter, &(table_alias[geno_word & 3]), 4); |
| 2091 |
} |
| 2092 |
return; |
| 2093 |
} |
| 2094 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2095 |
} |
| 2096 |
geno_word = genoarr[widx]; |
| 2097 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2098 |
const uintptr_t cur_2geno = geno_word & 15; |
| 2099 |
AppendU64(table_alias[cur_2geno], &result_biter); |
| 2100 |
geno_word >>= 4; |
| 2101 |
} |
| 2102 |
} |
| 2103 |
} |
| 2104 |
#else |
| 2105 |
// plink2_glm_logistic does not guarantee 8 byte table alignment. |
| 2106 |
void GenoarrLookup16x4bx2(const uintptr_t* genoarr, const void* table16x4bx2, uint32_t sample_ct, void* __restrict result) { |
| 2107 |
const uint32_t* table_u32 = S_CAST(const uint32_t*, table16x4bx2); |
| 2108 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2109 |
const uint32_t sample_ctl2m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2110 |
uint32_t loop_len = kBitsPerWordD4; |
| 2111 |
uintptr_t geno_word = 0; |
| 2112 |
for (uint32_t widx = 0; ; ++widx) { |
| 2113 |
if (widx >= sample_ctl2m1) { |
| 2114 |
if (widx > sample_ctl2m1) { |
| 2115 |
if (sample_ct % 2) { |
| 2116 |
// not a regular CopyToUnaligned, we're only copying the bottom half |
| 2117 |
// of the element |
| 2118 |
memcpy(result_biter, &(table_u32[(geno_word & 3) * 2]), 4); |
| 2119 |
} |
| 2120 |
return; |
| 2121 |
} |
| 2122 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2123 |
} |
| 2124 |
geno_word = genoarr[widx]; |
| 2125 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2126 |
const uintptr_t cur_2geno = geno_word & 15; |
| 2127 |
memcpy(result_biter, &(table_u32[cur_2geno * 2]), 8); |
| 2128 |
result_biter += 8; |
| 2129 |
geno_word >>= 4; |
| 2130 |
} |
| 2131 |
} |
| 2132 |
} |
| 2133 |
#endif |
| 2134 |
|
| 2135 |
// this might be important for genovec -> AlleleCode expansion |
| 2136 |
void GenoarrLookup256x2bx4(const uintptr_t* genoarr, const void* table256x2bx4, uint32_t sample_ct, void* __restrict result) { |
| 2137 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table256x2bx4); |
| 2138 |
const unsigned char* genoarr_alias = DowncastKToUc(genoarr); |
| 2139 |
unsigned char* resultb = S_CAST(unsigned char*, result); |
| 2140 |
const uint32_t full_byte_ct = sample_ct / 4; |
| 2141 |
for (uint32_t byte_idx = 0; byte_idx != full_byte_ct; ++byte_idx) { |
| 2142 |
CopyToUnalignedOffsetU64(resultb, &(table_alias[genoarr_alias[byte_idx]]), byte_idx); |
| 2143 |
} |
| 2144 |
const uint32_t remainder = sample_ct % 4; |
| 2145 |
if (remainder) { |
| 2146 |
unsigned char* result_last = &(resultb[full_byte_ct * sizeof(int64_t)]); |
| 2147 |
uintptr_t geno_byte = genoarr_alias[full_byte_ct]; |
| 2148 |
for (uint32_t uii = 0; uii != remainder; ++uii) { |
| 2149 |
CopyToUnalignedOffsetU16(result_last, DowncastKU64ToU16(&(table_alias[geno_byte & 3])), uii); |
| 2150 |
geno_byte >>= 2; |
| 2151 |
} |
| 2152 |
} |
| 2153 |
} |
| 2154 |
|
| 2155 |
#ifdef USE_SSE2 |
| 2156 |
void GenoarrLookup4x16b(const uintptr_t* genoarr, const void* table4x16b, uint32_t sample_ct, void* result) { |
| 2157 |
const __m128i* table_alias = S_CAST(const __m128i*, table4x16b); |
| 2158 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2159 |
const uint32_t sample_ctl2m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2160 |
uint32_t loop_len = kBitsPerWordD2; |
| 2161 |
uintptr_t geno_word = 0; |
| 2162 |
for (uint32_t widx = 0; ; ++widx) { |
| 2163 |
if (widx >= sample_ctl2m1) { |
| 2164 |
if (widx > sample_ctl2m1) { |
| 2165 |
return; |
| 2166 |
} |
| 2167 |
loop_len = ModNz(sample_ct, kBitsPerWordD2); |
| 2168 |
} |
| 2169 |
geno_word = genoarr[widx]; |
| 2170 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2171 |
_mm_storeu_si128(R_CAST(__m128i*, result_biter), table_alias[geno_word & 3]); |
| 2172 |
result_biter += 16; |
| 2173 |
geno_word >>= 2; |
| 2174 |
} |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
void GenoarrLookup16x8bx2(const uintptr_t* genoarr, const void* table16x8bx2, uint32_t sample_ct, void* __restrict result) { |
| 2179 |
const __m128i* table_alias = S_CAST(const __m128i*, table16x8bx2); |
| 2180 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2181 |
const uint32_t sample_ctl2m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2182 |
uint32_t loop_len = kBitsPerWordD4; |
| 2183 |
uintptr_t geno_word = 0; |
| 2184 |
for (uint32_t widx = 0; ; ++widx) { |
| 2185 |
if (widx >= sample_ctl2m1) { |
| 2186 |
if (widx > sample_ctl2m1) { |
| 2187 |
if (sample_ct % 2) { |
| 2188 |
memcpy(result_biter, &(table_alias[geno_word & 3]), 8); |
| 2189 |
} |
| 2190 |
return; |
| 2191 |
} |
| 2192 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2193 |
} |
| 2194 |
geno_word = genoarr[widx]; |
| 2195 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2196 |
const uintptr_t cur_2geno = geno_word & 15; |
| 2197 |
_mm_storeu_si128(R_CAST(__m128i*, result_biter), table_alias[cur_2geno]); |
| 2198 |
result_biter += 16; |
| 2199 |
geno_word >>= 4; |
| 2200 |
} |
| 2201 |
} |
| 2202 |
} |
| 2203 |
|
| 2204 |
void GenoarrLookup256x4bx4(const uintptr_t* genoarr, const void* table256x4bx4, uint32_t sample_ct, void* __restrict result) { |
| 2205 |
const __m128i* table_alias = S_CAST(const __m128i*, table256x4bx4); |
| 2206 |
const unsigned char* genoarr_alias = DowncastKToUc(genoarr); |
| 2207 |
unsigned char* resultb = S_CAST(unsigned char*, result); |
| 2208 |
const uint32_t full_byte_ct = sample_ct / 4; |
| 2209 |
for (uint32_t byte_idx = 0; byte_idx != full_byte_ct; ++byte_idx) { |
| 2210 |
_mm_storeu_si128(R_CAST(__m128i*, &(resultb[byte_idx * 16])), table_alias[genoarr_alias[byte_idx]]); |
| 2211 |
} |
| 2212 |
const uint32_t remainder = sample_ct % 4; |
| 2213 |
if (remainder) { |
| 2214 |
unsigned char* result_last = &(resultb[full_byte_ct * 16]); |
| 2215 |
uintptr_t geno_byte = genoarr_alias[full_byte_ct]; |
| 2216 |
for (uint32_t uii = 0; uii != remainder; ++uii) { |
| 2217 |
CopyToUnalignedOffsetU32(result_last, R_CAST(const uint32_t*, &(table_alias[geno_byte & 3])), uii); |
| 2218 |
geno_byte >>= 2; |
| 2219 |
} |
| 2220 |
} |
| 2221 |
} |
| 2222 |
#else // !USE_SSE2 |
| 2223 |
void GenoarrLookup4x16b(const uintptr_t* genoarr, const void* table4x16b, uint32_t sample_ct, void* result) { |
| 2224 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table4x16b); |
| 2225 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2226 |
const uint32_t sample_ctl2m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2227 |
uint32_t loop_len = kBitsPerWordD2; |
| 2228 |
uintptr_t geno_word = 0; |
| 2229 |
for (uint32_t widx = 0; ; ++widx) { |
| 2230 |
if (widx >= sample_ctl2m1) { |
| 2231 |
if (widx > sample_ctl2m1) { |
| 2232 |
return; |
| 2233 |
} |
| 2234 |
loop_len = ModNz(sample_ct, kBitsPerWordD2); |
| 2235 |
} |
| 2236 |
geno_word = genoarr[widx]; |
| 2237 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2238 |
memcpy(result_biter, &(table_alias[(geno_word & 3) * 2]), 16); |
| 2239 |
result_biter += 16; |
| 2240 |
geno_word >>= 2; |
| 2241 |
} |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
void GenoarrLookup16x8bx2(const uintptr_t* genoarr, const void* table16x8bx2, uint32_t sample_ct, void* __restrict result) { |
| 2246 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table16x8bx2); |
| 2247 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2248 |
const uint32_t sample_ctl2m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2249 |
uint32_t loop_len = kBitsPerWordD4; |
| 2250 |
uintptr_t geno_word = 0; |
| 2251 |
for (uint32_t widx = 0; ; ++widx) { |
| 2252 |
if (widx >= sample_ctl2m1) { |
| 2253 |
if (widx > sample_ctl2m1) { |
| 2254 |
if (sample_ct % 2) { |
| 2255 |
CopyToUnalignedU64(result_biter, &(table_alias[(geno_word & 3) * 2])); |
| 2256 |
} |
| 2257 |
return; |
| 2258 |
} |
| 2259 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2260 |
} |
| 2261 |
geno_word = genoarr[widx]; |
| 2262 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2263 |
const uintptr_t cur_2geno = geno_word & 15; |
| 2264 |
memcpy(result_biter, &(table_alias[cur_2geno * 2]), 16); |
| 2265 |
result_biter += 16; |
| 2266 |
geno_word >>= 4; |
| 2267 |
} |
| 2268 |
} |
| 2269 |
} |
| 2270 |
|
| 2271 |
void GenoarrLookup256x4bx4(const uintptr_t* genoarr, const void* table256x4bx4, uint32_t sample_ct, void* __restrict result) { |
| 2272 |
const uint32_t* table_alias = S_CAST(const uint32_t*, table256x4bx4); |
| 2273 |
const unsigned char* genoarr_alias = DowncastKToUc(genoarr); |
| 2274 |
unsigned char* resultb = S_CAST(unsigned char*, result); |
| 2275 |
const uint32_t full_byte_ct = sample_ct / 4; |
| 2276 |
for (uint32_t byte_idx = 0; byte_idx != full_byte_ct; ++byte_idx) { |
| 2277 |
memcpy(&(resultb[byte_idx * 16]), &(table_alias[genoarr_alias[byte_idx] * 4]), 16); |
| 2278 |
} |
| 2279 |
const uint32_t remainder = sample_ct % 4; |
| 2280 |
if (remainder) { |
| 2281 |
unsigned char* result_last = &(resultb[full_byte_ct * 16]); |
| 2282 |
uintptr_t geno_byte = genoarr_alias[full_byte_ct]; |
| 2283 |
for (uint32_t uii = 0; uii != remainder; ++uii) { |
| 2284 |
CopyToUnalignedOffsetU32(result_last, &(table_alias[(geno_byte & 3) * 4]), uii); |
| 2285 |
geno_byte >>= 2; |
| 2286 |
} |
| 2287 |
} |
| 2288 |
} |
| 2289 |
#endif |
| 2290 |
|
| 2291 |
void InitLookup16x4bx2(void* table16x4bx2) { |
| 2292 |
uint32_t* table_iter = S_CAST(uint32_t*, table16x4bx2); |
| 2293 |
uint32_t vals[4]; |
| 2294 |
vals[0] = table_iter[0]; |
| 2295 |
table_iter[1] = vals[0]; |
| 2296 |
vals[1] = table_iter[2]; |
| 2297 |
table_iter[3] = vals[0]; |
| 2298 |
vals[2] = table_iter[4]; |
| 2299 |
table_iter[5] = vals[0]; |
| 2300 |
vals[3] = table_iter[6]; |
| 2301 |
table_iter[7] = vals[0]; |
| 2302 |
table_iter = &(table_iter[8]); |
| 2303 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2304 |
const uint32_t cur_high = vals[high_idx]; |
| 2305 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2306 |
*table_iter++ = vals[low_idx]; |
| 2307 |
*table_iter++ = cur_high; |
| 2308 |
} |
| 2309 |
} |
| 2310 |
} |
| 2311 |
|
| 2312 |
void InitLookup16x8bx2(void* table16x8bx2) { |
| 2313 |
uint64_t* table_iter = S_CAST(uint64_t*, table16x8bx2); |
| 2314 |
uint64_t vals[4]; |
| 2315 |
vals[0] = table_iter[0]; |
| 2316 |
table_iter[1] = vals[0]; |
| 2317 |
vals[1] = table_iter[2]; |
| 2318 |
table_iter[3] = vals[0]; |
| 2319 |
vals[2] = table_iter[4]; |
| 2320 |
table_iter[5] = vals[0]; |
| 2321 |
vals[3] = table_iter[6]; |
| 2322 |
table_iter[7] = vals[0]; |
| 2323 |
table_iter = &(table_iter[8]); |
| 2324 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2325 |
// bugfix (20 Jun 2018): cur_high needs to be a uint64_t, not a uint32_t |
| 2326 |
const uint64_t cur_high = vals[high_idx]; |
| 2327 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2328 |
*table_iter++ = vals[low_idx]; |
| 2329 |
*table_iter++ = cur_high; |
| 2330 |
} |
| 2331 |
} |
| 2332 |
} |
| 2333 |
|
| 2334 |
void InitLookup256x1bx4(void* table256x1bx4) { |
| 2335 |
unsigned char* table_iter = S_CAST(unsigned char*, table256x1bx4); |
| 2336 |
unsigned char vals[4]; |
| 2337 |
vals[0] = table_iter[0]; |
| 2338 |
vals[1] = table_iter[4]; |
| 2339 |
vals[2] = table_iter[8]; |
| 2340 |
vals[3] = table_iter[12]; |
| 2341 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2342 |
const uint32_t cur_high = vals[high_idx]; |
| 2343 |
for (uint32_t second_idx = 0; second_idx != 4; ++second_idx) { |
| 2344 |
const uint32_t cur_second = vals[second_idx]; |
| 2345 |
for (uint32_t third_idx = 0; third_idx != 4; ++third_idx) { |
| 2346 |
const uint32_t cur_third = vals[third_idx]; |
| 2347 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2348 |
*table_iter++ = vals[low_idx]; |
| 2349 |
*table_iter++ = cur_third; |
| 2350 |
*table_iter++ = cur_second; |
| 2351 |
*table_iter++ = cur_high; |
| 2352 |
} |
| 2353 |
} |
| 2354 |
} |
| 2355 |
} |
| 2356 |
#ifdef USE_SHUFFLE8 |
| 2357 |
// first vector: 0, 1, 2, 3 |
| 2358 |
// second vector: 0, 4, 8, 12 |
| 2359 |
uint32_t* final_u32s = R_CAST(uint32_t*, table_iter); |
| 2360 |
uint32_t val0123; |
| 2361 |
memcpy(&val0123, vals, 4); |
| 2362 |
for (uint32_t uii = 0; uii != kInt32PerVec; ++uii) { |
| 2363 |
final_u32s[uii] = val0123; |
| 2364 |
} |
| 2365 |
final_u32s[kInt32PerVec] = S_CAST(uint32_t, vals[0]) * 0x1010101; |
| 2366 |
final_u32s[kInt32PerVec + 1] = S_CAST(uint32_t, vals[1]) * 0x1010101; |
| 2367 |
final_u32s[kInt32PerVec + 2] = S_CAST(uint32_t, vals[2]) * 0x1010101; |
| 2368 |
final_u32s[kInt32PerVec + 3] = S_CAST(uint32_t, vals[3]) * 0x1010101; |
| 2369 |
# ifdef USE_AVX2 |
| 2370 |
memcpy(&(final_u32s[12]), &(final_u32s[8]), 16); |
| 2371 |
# endif |
| 2372 |
#endif |
| 2373 |
} |
| 2374 |
|
| 2375 |
void InitLookup256x2bx4(void* table256x2bx4) { |
| 2376 |
uint16_t* table_iter = S_CAST(uint16_t*, table256x2bx4); |
| 2377 |
uint16_t vals[4]; |
| 2378 |
vals[0] = table_iter[0]; |
| 2379 |
vals[1] = table_iter[4]; |
| 2380 |
vals[2] = table_iter[8]; |
| 2381 |
vals[3] = table_iter[12]; |
| 2382 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2383 |
const uint32_t cur_high = vals[high_idx]; |
| 2384 |
for (uint32_t second_idx = 0; second_idx != 4; ++second_idx) { |
| 2385 |
const uint32_t cur_second = vals[second_idx]; |
| 2386 |
for (uint32_t third_idx = 0; third_idx != 4; ++third_idx) { |
| 2387 |
const uint32_t cur_third = vals[third_idx]; |
| 2388 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2389 |
*table_iter++ = vals[low_idx]; |
| 2390 |
*table_iter++ = cur_third; |
| 2391 |
*table_iter++ = cur_second; |
| 2392 |
*table_iter++ = cur_high; |
| 2393 |
} |
| 2394 |
} |
| 2395 |
} |
| 2396 |
} |
| 2397 |
} |
| 2398 |
|
| 2399 |
void InitLookup256x4bx4(void* table256x4bx4) { |
| 2400 |
uint32_t* table_iter = S_CAST(uint32_t*, table256x4bx4); |
| 2401 |
uint32_t vals[4]; |
| 2402 |
vals[0] = table_iter[0]; |
| 2403 |
vals[1] = table_iter[4]; |
| 2404 |
vals[2] = table_iter[8]; |
| 2405 |
vals[3] = table_iter[12]; |
| 2406 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2407 |
const uint32_t cur_high = vals[high_idx]; |
| 2408 |
for (uint32_t second_idx = 0; second_idx != 4; ++second_idx) { |
| 2409 |
const uint32_t cur_second = vals[second_idx]; |
| 2410 |
for (uint32_t third_idx = 0; third_idx != 4; ++third_idx) { |
| 2411 |
const uint32_t cur_third = vals[third_idx]; |
| 2412 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2413 |
*table_iter++ = vals[low_idx]; |
| 2414 |
*table_iter++ = cur_third; |
| 2415 |
*table_iter++ = cur_second; |
| 2416 |
*table_iter++ = cur_high; |
| 2417 |
} |
| 2418 |
} |
| 2419 |
} |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
void PhaseLookup4b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const void* table56x4bx2, uint32_t sample_ct, void* __restrict result) { |
| 2424 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table56x4bx2); |
| 2425 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2426 |
const Halfword* phasepresent_alias = DowncastKWToHW(phasepresent); |
| 2427 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 2428 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2429 |
uint32_t loop_len = kBitsPerWordD4; |
| 2430 |
uintptr_t geno_word = 0; |
| 2431 |
uintptr_t phasepresent_hw_shifted = 0; |
| 2432 |
uintptr_t phaseinfo_hw_shifted = 0; |
| 2433 |
for (uint32_t widx = 0; ; ++widx) { |
| 2434 |
if (widx >= sample_ctl2_m1) { |
| 2435 |
if (widx > sample_ctl2_m1) { |
| 2436 |
if (sample_ct % 2) { |
| 2437 |
uintptr_t cur_idx = (geno_word & 3); |
| 2438 |
// assume trailing bits of phasepresent/phaseinfo clear |
| 2439 |
// phaseinfo_hw_shifted not necessarily updated, so need if-statement |
| 2440 |
// bugfix (25 Jun 2018): must only consider active bit of |
| 2441 |
// phasepresent_hw_shifted, not the already-processed ones |
| 2442 |
if (phasepresent_hw_shifted & 16) { |
| 2443 |
cur_idx ^= 16 | (phaseinfo_hw_shifted & 2); |
| 2444 |
} |
| 2445 |
memcpy(result_biter, &(table_alias[cur_idx]), 4); |
| 2446 |
} |
| 2447 |
return; |
| 2448 |
} |
| 2449 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2450 |
} |
| 2451 |
geno_word = genoarr[widx]; |
| 2452 |
phasepresent_hw_shifted = phasepresent_alias[widx]; |
| 2453 |
if (!phasepresent_hw_shifted) { |
| 2454 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2455 |
AppendU64(table_alias[geno_word & 15], &result_biter); |
| 2456 |
geno_word >>= 4; |
| 2457 |
} |
| 2458 |
} else { |
| 2459 |
phasepresent_hw_shifted = phasepresent_hw_shifted << 4; |
| 2460 |
phaseinfo_hw_shifted = phaseinfo_alias[widx]; |
| 2461 |
|
| 2462 |
// note that this must be on a separate line (or we have to static_cast) |
| 2463 |
phaseinfo_hw_shifted = phaseinfo_hw_shifted << 1; |
| 2464 |
|
| 2465 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2466 |
const uintptr_t cur_idx = ((geno_word & 15) | (phasepresent_hw_shifted & 48)) ^ (phaseinfo_hw_shifted & 6); |
| 2467 |
AppendU64(table_alias[cur_idx], &result_biter); |
| 2468 |
geno_word >>= 4; |
| 2469 |
phasepresent_hw_shifted >>= 2; |
| 2470 |
phaseinfo_hw_shifted >>= 2; |
| 2471 |
} |
| 2472 |
} |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
void InitPhaseLookup4b(void* table56x4bx2) { |
| 2477 |
uint32_t* table_iter = S_CAST(uint32_t*, table56x4bx2); |
| 2478 |
uint32_t vals[4]; |
| 2479 |
vals[0] = table_iter[0]; |
| 2480 |
table_iter[1] = vals[0]; |
| 2481 |
vals[1] = table_iter[2]; |
| 2482 |
table_iter[3] = vals[0]; |
| 2483 |
vals[2] = table_iter[4]; |
| 2484 |
table_iter[5] = vals[0]; |
| 2485 |
vals[3] = table_iter[6]; |
| 2486 |
table_iter[7] = vals[0]; |
| 2487 |
table_iter = &(table_iter[8]); |
| 2488 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2489 |
const uint32_t cur_high = vals[high_idx]; |
| 2490 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2491 |
*table_iter++ = vals[low_idx]; |
| 2492 |
*table_iter++ = cur_high; |
| 2493 |
} |
| 2494 |
} |
| 2495 |
// [16][0]..[31][1]: bit 4 is set |
| 2496 |
// low bits must be 01 or 11 |
| 2497 |
const uint32_t val_phaseinfo0 = table_iter[2]; |
| 2498 |
table_iter[3] = vals[0]; |
| 2499 |
const uint32_t val_phaseinfo1 = table_iter[6]; |
| 2500 |
table_iter[7] = vals[0]; |
| 2501 |
table_iter = &(table_iter[8]); |
| 2502 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2503 |
const uint32_t cur_high = vals[high_idx]; |
| 2504 |
table_iter[2] = val_phaseinfo0; |
| 2505 |
table_iter[3] = cur_high; |
| 2506 |
table_iter[6] = val_phaseinfo1; |
| 2507 |
table_iter[7] = cur_high; |
| 2508 |
table_iter = &(table_iter[8]); |
| 2509 |
} |
| 2510 |
// [32][0]..[39][1]: bit 5 set, bit 4 unset |
| 2511 |
// high bits must be 00 or 01 |
| 2512 |
for (uint32_t high_idx = 0; high_idx != 2; ++high_idx) { |
| 2513 |
const uint32_t cur_high = high_idx? val_phaseinfo0 : val_phaseinfo1; |
| 2514 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2515 |
*table_iter++ = vals[low_idx]; |
| 2516 |
*table_iter++ = cur_high; |
| 2517 |
} |
| 2518 |
} |
| 2519 |
table_iter = &(table_iter[16]); |
| 2520 |
// [48][0]..[55][1]: bits 4 and 5 set |
| 2521 |
for (uint32_t high_idx = 0; high_idx != 2; ++high_idx) { |
| 2522 |
const uint32_t cur_high = high_idx? val_phaseinfo0 : val_phaseinfo1; |
| 2523 |
table_iter[2] = val_phaseinfo0; |
| 2524 |
table_iter[3] = cur_high; |
| 2525 |
table_iter[6] = val_phaseinfo1; |
| 2526 |
table_iter[7] = cur_high; |
| 2527 |
table_iter = &(table_iter[8]); |
| 2528 |
} |
| 2529 |
} |
| 2530 |
|
| 2531 |
#ifdef USE_SSE2 |
| 2532 |
void PhaseLookup8b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const void* table56x8bx2, uint32_t sample_ct, void* __restrict result) { |
| 2533 |
const __m128i* table_alias = S_CAST(const __m128i*, table56x8bx2); |
| 2534 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2535 |
const Halfword* phasepresent_alias = DowncastKWToHW(phasepresent); |
| 2536 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 2537 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2538 |
uint32_t loop_len = kBitsPerWordD4; |
| 2539 |
uintptr_t geno_word = 0; |
| 2540 |
uintptr_t phasepresent_hw_shifted = 0; |
| 2541 |
uintptr_t phaseinfo_hw_shifted = 0; |
| 2542 |
for (uint32_t widx = 0; ; ++widx) { |
| 2543 |
if (widx >= sample_ctl2_m1) { |
| 2544 |
if (widx > sample_ctl2_m1) { |
| 2545 |
if (sample_ct % 2) { |
| 2546 |
uintptr_t cur_idx = (geno_word & 3); |
| 2547 |
if (phasepresent_hw_shifted & 16) { |
| 2548 |
cur_idx ^= 16 | (phaseinfo_hw_shifted & 2); |
| 2549 |
} |
| 2550 |
memcpy(result_biter, &(table_alias[cur_idx]), 8); |
| 2551 |
} |
| 2552 |
return; |
| 2553 |
} |
| 2554 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2555 |
} |
| 2556 |
geno_word = genoarr[widx]; |
| 2557 |
phasepresent_hw_shifted = phasepresent_alias[widx]; |
| 2558 |
if (!phasepresent_hw_shifted) { |
| 2559 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2560 |
_mm_storeu_si128(R_CAST(__m128i*, result_biter), table_alias[geno_word & 15]); |
| 2561 |
result_biter += 16; |
| 2562 |
geno_word >>= 4; |
| 2563 |
} |
| 2564 |
} else { |
| 2565 |
phasepresent_hw_shifted = phasepresent_hw_shifted << 4; |
| 2566 |
phaseinfo_hw_shifted = phaseinfo_alias[widx]; |
| 2567 |
phaseinfo_hw_shifted = phaseinfo_hw_shifted << 1; |
| 2568 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2569 |
const uintptr_t cur_idx = ((geno_word & 15) | (phasepresent_hw_shifted & 48)) ^ (phaseinfo_hw_shifted & 6); |
| 2570 |
_mm_storeu_si128(R_CAST(__m128i*, result_biter), table_alias[cur_idx]); |
| 2571 |
result_biter += 16; |
| 2572 |
geno_word >>= 4; |
| 2573 |
phasepresent_hw_shifted >>= 2; |
| 2574 |
phaseinfo_hw_shifted >>= 2; |
| 2575 |
} |
| 2576 |
} |
| 2577 |
} |
| 2578 |
} |
| 2579 |
#else // !USE_SSE2 |
| 2580 |
void PhaseLookup8b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const void* table56x8bx2, uint32_t sample_ct, void* __restrict result) { |
| 2581 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table56x8bx2); |
| 2582 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2583 |
const Halfword* phasepresent_alias = DowncastKWToHW(phasepresent); |
| 2584 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 2585 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2586 |
uint32_t loop_len = kBitsPerWordD4; |
| 2587 |
uintptr_t geno_word = 0; |
| 2588 |
uintptr_t phasepresent_hw_shifted = 0; |
| 2589 |
uintptr_t phaseinfo_hw_shifted = 0; |
| 2590 |
for (uint32_t widx = 0; ; ++widx) { |
| 2591 |
if (widx >= sample_ctl2_m1) { |
| 2592 |
if (widx > sample_ctl2_m1) { |
| 2593 |
if (sample_ct % 2) { |
| 2594 |
uintptr_t cur_idx = (geno_word & 3); |
| 2595 |
if (phasepresent_hw_shifted & 16) { |
| 2596 |
cur_idx ^= 16 | (phaseinfo_hw_shifted & 2); |
| 2597 |
} |
| 2598 |
CopyToUnalignedU64(result_biter, &(table_alias[cur_idx * 2])); |
| 2599 |
} |
| 2600 |
return; |
| 2601 |
} |
| 2602 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2603 |
} |
| 2604 |
geno_word = genoarr[widx]; |
| 2605 |
phasepresent_hw_shifted = phasepresent_alias[widx]; |
| 2606 |
if (!phasepresent_hw_shifted) { |
| 2607 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2608 |
memcpy(result_biter, &(table_alias[(geno_word & 15) * 2]), 16); |
| 2609 |
result_biter = &(result_biter[16]); |
| 2610 |
geno_word >>= 4; |
| 2611 |
} |
| 2612 |
} else { |
| 2613 |
phasepresent_hw_shifted = phasepresent_hw_shifted << 4; |
| 2614 |
phaseinfo_hw_shifted = phaseinfo_alias[widx]; |
| 2615 |
phaseinfo_hw_shifted = phaseinfo_hw_shifted << 1; |
| 2616 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2617 |
const uintptr_t cur_idx = ((geno_word & 15) | (phasepresent_hw_shifted & 48)) ^ (phaseinfo_hw_shifted & 6); |
| 2618 |
memcpy(result_biter, &(table_alias[cur_idx * 2]), 16); |
| 2619 |
// bugfix (12 Jun 2023): forgot this |
| 2620 |
result_biter = &(result_biter[16]); |
| 2621 |
|
| 2622 |
geno_word >>= 4; |
| 2623 |
phasepresent_hw_shifted >>= 2; |
| 2624 |
phaseinfo_hw_shifted >>= 2; |
| 2625 |
} |
| 2626 |
} |
| 2627 |
} |
| 2628 |
} |
| 2629 |
#endif |
| 2630 |
|
| 2631 |
void InitPhaseLookup8b(void* table56x8bx2) { |
| 2632 |
uint64_t* table_iter = S_CAST(uint64_t*, table56x8bx2); |
| 2633 |
uint64_t vals[4]; |
| 2634 |
vals[0] = table_iter[0]; |
| 2635 |
table_iter[1] = vals[0]; |
| 2636 |
vals[1] = table_iter[2]; |
| 2637 |
table_iter[3] = vals[0]; |
| 2638 |
vals[2] = table_iter[4]; |
| 2639 |
table_iter[5] = vals[0]; |
| 2640 |
vals[3] = table_iter[6]; |
| 2641 |
table_iter[7] = vals[0]; |
| 2642 |
table_iter = &(table_iter[8]); |
| 2643 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2644 |
const uint64_t cur_high = vals[high_idx]; |
| 2645 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2646 |
*table_iter++ = vals[low_idx]; |
| 2647 |
*table_iter++ = cur_high; |
| 2648 |
} |
| 2649 |
} |
| 2650 |
// [16][0]..[31][1]: bit 4 is set |
| 2651 |
// low bits must be 01 or 11 |
| 2652 |
const uint64_t val_phaseinfo0 = table_iter[2]; |
| 2653 |
table_iter[3] = vals[0]; |
| 2654 |
const uint64_t val_phaseinfo1 = table_iter[6]; |
| 2655 |
table_iter[7] = vals[0]; |
| 2656 |
table_iter = &(table_iter[8]); |
| 2657 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2658 |
const uint64_t cur_high = vals[high_idx]; |
| 2659 |
table_iter[2] = val_phaseinfo0; |
| 2660 |
table_iter[3] = cur_high; |
| 2661 |
table_iter[6] = val_phaseinfo1; |
| 2662 |
table_iter[7] = cur_high; |
| 2663 |
table_iter = &(table_iter[8]); |
| 2664 |
} |
| 2665 |
// [32][0]..[39][1]: bit 5 set, bit 4 unset |
| 2666 |
// high bits must be 00 or 01 |
| 2667 |
for (uint32_t high_idx = 0; high_idx != 2; ++high_idx) { |
| 2668 |
const uint64_t cur_high = high_idx? val_phaseinfo0 : val_phaseinfo1; |
| 2669 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2670 |
*table_iter++ = vals[low_idx]; |
| 2671 |
*table_iter++ = cur_high; |
| 2672 |
} |
| 2673 |
} |
| 2674 |
table_iter = &(table_iter[16]); |
| 2675 |
// [48][0]..[55][1]: bits 4 and 5 set |
| 2676 |
for (uint32_t high_idx = 0; high_idx != 2; ++high_idx) { |
| 2677 |
const uint64_t cur_high = high_idx? val_phaseinfo0 : val_phaseinfo1; |
| 2678 |
table_iter[2] = val_phaseinfo0; |
| 2679 |
table_iter[3] = cur_high; |
| 2680 |
table_iter[6] = val_phaseinfo1; |
| 2681 |
table_iter[7] = cur_high; |
| 2682 |
table_iter = &(table_iter[8]); |
| 2683 |
} |
| 2684 |
} |
| 2685 |
|
| 2686 |
// bits 0..3: two genotypes |
| 2687 |
// bits 4..5: two (phasepresent | sex_male) bits |
| 2688 |
// bits 1,3: unpacked phaseinfo xor |
| 2689 |
void PhaseXNohhLookup4b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const uintptr_t* sex_male, const void* table64x4bx2, uint32_t sample_ct, void* result) { |
| 2690 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table64x4bx2); |
| 2691 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2692 |
const Halfword* phasepresent_alias = DowncastKWToHW(phasepresent); |
| 2693 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 2694 |
const Halfword* sex_male_alias = DowncastKWToHW(sex_male); |
| 2695 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2696 |
uint32_t loop_len = kBitsPerWordD4; |
| 2697 |
uintptr_t geno_word_xored = 0; |
| 2698 |
uintptr_t male_or_phasepresent_hw_shifted = 0; |
| 2699 |
for (uint32_t widx = 0; ; ++widx) { |
| 2700 |
if (widx >= sample_ctl2_m1) { |
| 2701 |
if (widx > sample_ctl2_m1) { |
| 2702 |
if (sample_ct % 2) { |
| 2703 |
uintptr_t cur_idx = (geno_word_xored & 3) | (male_or_phasepresent_hw_shifted & 16); |
| 2704 |
memcpy(result_biter, &(table_alias[cur_idx]), 4); |
| 2705 |
} |
| 2706 |
return; |
| 2707 |
} |
| 2708 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2709 |
} |
| 2710 |
geno_word_xored = genoarr[widx]; |
| 2711 |
male_or_phasepresent_hw_shifted = sex_male_alias[widx]; |
| 2712 |
const uintptr_t phasepresent_hw = phasepresent_alias[widx]; |
| 2713 |
male_or_phasepresent_hw_shifted |= phasepresent_hw; |
| 2714 |
male_or_phasepresent_hw_shifted <<= 4; |
| 2715 |
if (!phasepresent_hw) { |
| 2716 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2717 |
AppendU64(table_alias[(geno_word_xored & 15) | (male_or_phasepresent_hw_shifted & 48)], &result_biter); |
| 2718 |
geno_word_xored >>= 4; |
| 2719 |
male_or_phasepresent_hw_shifted >>= 2; |
| 2720 |
} |
| 2721 |
} else { |
| 2722 |
geno_word_xored ^= UnpackHalfwordToWordShift1(phaseinfo_alias[widx]); |
| 2723 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2724 |
const uintptr_t cur_idx = (geno_word_xored & 15) | (male_or_phasepresent_hw_shifted & 48); |
| 2725 |
AppendU64(table_alias[cur_idx], &result_biter); |
| 2726 |
geno_word_xored >>= 4; |
| 2727 |
male_or_phasepresent_hw_shifted >>= 2; |
| 2728 |
} |
| 2729 |
} |
| 2730 |
} |
| 2731 |
} |
| 2732 |
|
| 2733 |
void InitPhaseXNohhLookup4b(void* table64x4bx2) { |
| 2734 |
uint32_t* table_iter = S_CAST(uint32_t*, table64x4bx2); |
| 2735 |
uint32_t vals[4]; |
| 2736 |
vals[0] = table_iter[0]; |
| 2737 |
table_iter[1] = vals[0]; |
| 2738 |
vals[1] = table_iter[2]; |
| 2739 |
table_iter[3] = vals[0]; |
| 2740 |
vals[2] = table_iter[4]; |
| 2741 |
table_iter[5] = vals[0]; |
| 2742 |
vals[3] = table_iter[6]; |
| 2743 |
table_iter[7] = vals[0]; |
| 2744 |
table_iter = &(table_iter[8]); |
| 2745 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2746 |
const uint32_t cur_high = vals[high_idx]; |
| 2747 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2748 |
*table_iter++ = vals[low_idx]; |
| 2749 |
*table_iter++ = cur_high; |
| 2750 |
} |
| 2751 |
} |
| 2752 |
// [16][0]..[31][1]: bit 4 is set |
| 2753 |
uint32_t male_or_phasepresent_vals[4]; |
| 2754 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2755 |
male_or_phasepresent_vals[low_idx] = *table_iter++; |
| 2756 |
*table_iter++ = vals[0]; |
| 2757 |
} |
| 2758 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2759 |
const uint32_t cur_high = vals[high_idx]; |
| 2760 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2761 |
*table_iter++ = male_or_phasepresent_vals[low_idx]; |
| 2762 |
*table_iter++ = cur_high; |
| 2763 |
} |
| 2764 |
} |
| 2765 |
// [32][0]..[47][1]: bit 5 set, bit 4 unset |
| 2766 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2767 |
const uint32_t cur_high = male_or_phasepresent_vals[high_idx]; |
| 2768 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2769 |
*table_iter++ = vals[low_idx]; |
| 2770 |
*table_iter++ = cur_high; |
| 2771 |
} |
| 2772 |
} |
| 2773 |
// [48][0]..[63][1]: bits 4 and 5 set |
| 2774 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2775 |
const uint32_t cur_high = male_or_phasepresent_vals[high_idx]; |
| 2776 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2777 |
*table_iter++ = male_or_phasepresent_vals[low_idx]; |
| 2778 |
*table_iter++ = cur_high; |
| 2779 |
} |
| 2780 |
} |
| 2781 |
} |
| 2782 |
|
| 2783 |
void GenoarrSexLookup4b(const uintptr_t* genoarr, const uintptr_t* sex_male, const void* table64x4bx2, uint32_t sample_ct, void* result) { |
| 2784 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table64x4bx2); |
| 2785 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2786 |
const Halfword* sex_male_alias = DowncastKWToHW(sex_male); |
| 2787 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2788 |
uint32_t loop_len = kBitsPerWordD4; |
| 2789 |
uintptr_t geno_word = 0; |
| 2790 |
uintptr_t male_hw_shifted = 0; |
| 2791 |
for (uint32_t widx = 0; ; ++widx) { |
| 2792 |
if (widx >= sample_ctl2_m1) { |
| 2793 |
if (widx > sample_ctl2_m1) { |
| 2794 |
if (sample_ct % 2) { |
| 2795 |
uintptr_t cur_idx = (geno_word & 3) | (male_hw_shifted & 16); |
| 2796 |
memcpy(result_biter, &(table_alias[cur_idx]), 4); |
| 2797 |
} |
| 2798 |
return; |
| 2799 |
} |
| 2800 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2801 |
} |
| 2802 |
geno_word = genoarr[widx]; |
| 2803 |
male_hw_shifted = sex_male_alias[widx]; |
| 2804 |
male_hw_shifted <<= 4; |
| 2805 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2806 |
AppendU64(table_alias[(geno_word & 15) | (male_hw_shifted & 48)], &result_biter); |
| 2807 |
geno_word >>= 4; |
| 2808 |
male_hw_shifted >>= 2; |
| 2809 |
} |
| 2810 |
} |
| 2811 |
} |
| 2812 |
|
| 2813 |
void InitPhaseXNohhLookup8b(void* table64x8bx2) { |
| 2814 |
uint64_t* table_iter = S_CAST(uint64_t*, table64x8bx2); |
| 2815 |
uint64_t vals[4]; |
| 2816 |
vals[0] = table_iter[0]; |
| 2817 |
table_iter[1] = vals[0]; |
| 2818 |
vals[1] = table_iter[2]; |
| 2819 |
table_iter[3] = vals[0]; |
| 2820 |
vals[2] = table_iter[4]; |
| 2821 |
table_iter[5] = vals[0]; |
| 2822 |
vals[3] = table_iter[6]; |
| 2823 |
table_iter[7] = vals[0]; |
| 2824 |
table_iter = &(table_iter[8]); |
| 2825 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2826 |
const uint64_t cur_high = vals[high_idx]; |
| 2827 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2828 |
*table_iter++ = vals[low_idx]; |
| 2829 |
*table_iter++ = cur_high; |
| 2830 |
} |
| 2831 |
} |
| 2832 |
// [16][0]..[31][1]: bit 4 is set |
| 2833 |
uint64_t male_or_phasepresent_vals[4]; |
| 2834 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2835 |
male_or_phasepresent_vals[low_idx] = *table_iter++; |
| 2836 |
*table_iter++ = vals[0]; |
| 2837 |
} |
| 2838 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2839 |
const uint64_t cur_high = vals[high_idx]; |
| 2840 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2841 |
*table_iter++ = male_or_phasepresent_vals[low_idx]; |
| 2842 |
*table_iter++ = cur_high; |
| 2843 |
} |
| 2844 |
} |
| 2845 |
// [32][0]..[47][1]: bit 5 set, bit 4 unset |
| 2846 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2847 |
const uint64_t cur_high = male_or_phasepresent_vals[high_idx]; |
| 2848 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2849 |
*table_iter++ = vals[low_idx]; |
| 2850 |
*table_iter++ = cur_high; |
| 2851 |
} |
| 2852 |
} |
| 2853 |
// [48][0]..[63][1]: bits 4 and 5 set |
| 2854 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 2855 |
const uint64_t cur_high = male_or_phasepresent_vals[high_idx]; |
| 2856 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2857 |
*table_iter++ = male_or_phasepresent_vals[low_idx]; |
| 2858 |
*table_iter++ = cur_high; |
| 2859 |
} |
| 2860 |
} |
| 2861 |
} |
| 2862 |
|
| 2863 |
#ifdef USE_SSE2 |
| 2864 |
void GenoarrSexLookup8b(const uintptr_t* genoarr, const uintptr_t* sex_male, const void* table64x8bx2, uint32_t sample_ct, void* result) { |
| 2865 |
const __m128i* table_alias = S_CAST(const __m128i*, table64x8bx2); |
| 2866 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2867 |
const Halfword* sex_male_alias = DowncastKWToHW(sex_male); |
| 2868 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2869 |
uint32_t loop_len = kBitsPerWordD4; |
| 2870 |
uintptr_t geno_word = 0; |
| 2871 |
uintptr_t male_hw_shifted = 0; |
| 2872 |
for (uint32_t widx = 0; ; ++widx) { |
| 2873 |
if (widx >= sample_ctl2_m1) { |
| 2874 |
if (widx > sample_ctl2_m1) { |
| 2875 |
if (sample_ct % 2) { |
| 2876 |
uintptr_t cur_idx = (geno_word & 3) | (male_hw_shifted & 16); |
| 2877 |
memcpy(result_biter, &(table_alias[cur_idx]), 8); |
| 2878 |
} |
| 2879 |
return; |
| 2880 |
} |
| 2881 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2882 |
} |
| 2883 |
geno_word = genoarr[widx]; |
| 2884 |
male_hw_shifted = sex_male_alias[widx]; |
| 2885 |
male_hw_shifted <<= 4; |
| 2886 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2887 |
_mm_storeu_si128(R_CAST(__m128i*, result_biter), table_alias[(geno_word & 15) | (male_hw_shifted & 48)]); |
| 2888 |
result_biter += 16; |
| 2889 |
geno_word >>= 4; |
| 2890 |
male_hw_shifted >>= 2; |
| 2891 |
} |
| 2892 |
} |
| 2893 |
} |
| 2894 |
#else // !USE_SSE2 |
| 2895 |
void GenoarrSexLookup8b(const uintptr_t* genoarr, const uintptr_t* sex_male, const void* table64x8bx2, uint32_t sample_ct, void* result) { |
| 2896 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table64x8bx2); |
| 2897 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2898 |
const Halfword* sex_male_alias = DowncastKWToHW(sex_male); |
| 2899 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2900 |
uint32_t loop_len = kBitsPerWordD4; |
| 2901 |
uintptr_t geno_word = 0; |
| 2902 |
uintptr_t male_hw_shifted = 0; |
| 2903 |
for (uint32_t widx = 0; ; ++widx) { |
| 2904 |
if (widx >= sample_ctl2_m1) { |
| 2905 |
if (widx > sample_ctl2_m1) { |
| 2906 |
if (sample_ct % 2) { |
| 2907 |
const uintptr_t cur_idx = (geno_word & 3) | (male_hw_shifted & 16); |
| 2908 |
memcpy(result_biter, &(table_alias[cur_idx * 2]), 8); |
| 2909 |
} |
| 2910 |
return; |
| 2911 |
} |
| 2912 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2913 |
} |
| 2914 |
geno_word = genoarr[widx]; |
| 2915 |
male_hw_shifted = sex_male_alias[widx]; |
| 2916 |
male_hw_shifted <<= 4; |
| 2917 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2918 |
memcpy(result_biter, &(table_alias[((geno_word & 15) | (male_hw_shifted & 48)) * 2]), 16); |
| 2919 |
result_biter = &(result_biter[16]); |
| 2920 |
geno_word >>= 4; |
| 2921 |
male_hw_shifted >>= 2; |
| 2922 |
} |
| 2923 |
} |
| 2924 |
} |
| 2925 |
#endif |
| 2926 |
|
| 2927 |
void VcfPhaseLookup4b(const uintptr_t* genoarr, const uintptr_t* cur_phased, const uintptr_t* phaseinfo, const void* table246x4bx2, uint32_t sample_ct, void* __restrict result) { |
| 2928 |
const uint64_t* table_alias = S_CAST(const uint64_t*, table246x4bx2); |
| 2929 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 2930 |
const Halfword* cur_phased_alias = DowncastKWToHW(cur_phased); |
| 2931 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 2932 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 2933 |
uint32_t loop_len = kBitsPerWordD4; |
| 2934 |
uintptr_t geno_word = 0; |
| 2935 |
uintptr_t cur_phased_hw_shifted = 0; |
| 2936 |
uintptr_t phaseinfo_hw_shifted = 0; |
| 2937 |
for (uint32_t widx = 0; ; ++widx) { |
| 2938 |
if (widx >= sample_ctl2_m1) { |
| 2939 |
if (widx > sample_ctl2_m1) { |
| 2940 |
if (sample_ct % 2) { |
| 2941 |
uintptr_t cur_idx = (geno_word & 3) | (cur_phased_hw_shifted & 16) | (phaseinfo_hw_shifted & 64); |
| 2942 |
memcpy(result_biter, &(table_alias[cur_idx]), 4); |
| 2943 |
} |
| 2944 |
return; |
| 2945 |
} |
| 2946 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 2947 |
} |
| 2948 |
geno_word = genoarr[widx]; |
| 2949 |
cur_phased_hw_shifted = cur_phased_alias[widx]; |
| 2950 |
if (!cur_phased_hw_shifted) { |
| 2951 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2952 |
AppendU64(table_alias[geno_word & 15], &result_biter); |
| 2953 |
geno_word >>= 4; |
| 2954 |
} |
| 2955 |
} else { |
| 2956 |
cur_phased_hw_shifted = cur_phased_hw_shifted << 4; |
| 2957 |
phaseinfo_hw_shifted = phaseinfo_alias[widx]; |
| 2958 |
|
| 2959 |
// note that this must be on a separate line (or we have to static_cast) |
| 2960 |
phaseinfo_hw_shifted = phaseinfo_hw_shifted << 6; |
| 2961 |
|
| 2962 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 2963 |
const uintptr_t cur_idx = (geno_word & 15) | (cur_phased_hw_shifted & 48) | (phaseinfo_hw_shifted & 192); |
| 2964 |
AppendU64(table_alias[cur_idx], &result_biter); |
| 2965 |
geno_word >>= 4; |
| 2966 |
cur_phased_hw_shifted >>= 2; |
| 2967 |
phaseinfo_hw_shifted >>= 2; |
| 2968 |
} |
| 2969 |
} |
| 2970 |
} |
| 2971 |
} |
| 2972 |
|
| 2973 |
void InitVcfPhaseLookup4b(void* table246x4bx2) { |
| 2974 |
uint32_t* table_iter = S_CAST(uint32_t*, table246x4bx2); |
| 2975 |
uint32_t unphased_vals[4]; |
| 2976 |
unphased_vals[0] = table_iter[0]; |
| 2977 |
table_iter[1] = unphased_vals[0]; |
| 2978 |
unphased_vals[1] = table_iter[2]; |
| 2979 |
table_iter[3] = unphased_vals[0]; |
| 2980 |
unphased_vals[2] = table_iter[4]; |
| 2981 |
table_iter[5] = unphased_vals[0]; |
| 2982 |
unphased_vals[3] = table_iter[6]; |
| 2983 |
table_iter[7] = unphased_vals[0]; |
| 2984 |
table_iter = &(table_iter[8]); |
| 2985 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 2986 |
const uint32_t cur_high = unphased_vals[high_idx]; |
| 2987 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 2988 |
*table_iter++ = unphased_vals[low_idx]; |
| 2989 |
*table_iter++ = cur_high; |
| 2990 |
} |
| 2991 |
} |
| 2992 |
// [16][0]..[31][1]: first entry is phased and unflipped, second is unphased |
| 2993 |
uint32_t phased_unflipped_vals[4]; |
| 2994 |
phased_unflipped_vals[0] = table_iter[0]; |
| 2995 |
table_iter[1] = unphased_vals[0]; |
| 2996 |
phased_unflipped_vals[1] = table_iter[2]; |
| 2997 |
table_iter[3] = unphased_vals[0]; |
| 2998 |
phased_unflipped_vals[2] = table_iter[4]; |
| 2999 |
table_iter[5] = unphased_vals[0]; |
| 3000 |
phased_unflipped_vals[3] = table_iter[6]; |
| 3001 |
table_iter[7] = unphased_vals[0]; |
| 3002 |
table_iter = &(table_iter[8]); |
| 3003 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 3004 |
const uint32_t cur_high = unphased_vals[high_idx]; |
| 3005 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3006 |
*table_iter++ = phased_unflipped_vals[low_idx]; |
| 3007 |
*table_iter++ = cur_high; |
| 3008 |
} |
| 3009 |
} |
| 3010 |
// [32][0]..[63][1]: second entry is phased and unflipped |
| 3011 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3012 |
const uint32_t cur_high = phased_unflipped_vals[high_idx]; |
| 3013 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3014 |
*table_iter++ = unphased_vals[low_idx]; |
| 3015 |
*table_iter++ = cur_high; |
| 3016 |
} |
| 3017 |
} |
| 3018 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3019 |
const uint32_t cur_high = phased_unflipped_vals[high_idx]; |
| 3020 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3021 |
*table_iter++ = phased_unflipped_vals[low_idx]; |
| 3022 |
*table_iter++ = cur_high; |
| 3023 |
} |
| 3024 |
} |
| 3025 |
// [64][0]..[79][1] should be impossible |
| 3026 |
table_iter = &(table_iter[32]); |
| 3027 |
// [80][0]..[95][1]: first entry is phased and flipped, second is unphased |
| 3028 |
// genotype must be 01 |
| 3029 |
const uint32_t phased_flipped_01 = table_iter[2]; |
| 3030 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3031 |
table_iter[2] = phased_flipped_01; |
| 3032 |
table_iter[3] = unphased_vals[high_idx]; |
| 3033 |
table_iter = &(table_iter[8]); |
| 3034 |
} |
| 3035 |
// [96][0]..[111][1] should be impossible |
| 3036 |
table_iter = &(table_iter[32]); |
| 3037 |
// [112][0]..[127][1]: first entry phased-flipped, second phased-unflipped |
| 3038 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3039 |
table_iter[2] = phased_flipped_01; |
| 3040 |
table_iter[3] = phased_unflipped_vals[high_idx]; |
| 3041 |
table_iter = &(table_iter[8]); |
| 3042 |
} |
| 3043 |
// [128][0]..[163][1] should be impossible |
| 3044 |
table_iter = &(table_iter[72]); |
| 3045 |
// [164][0]..[167][1]: second entry phased-flipped, first entry unphased |
| 3046 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3047 |
*table_iter++ = unphased_vals[low_idx]; |
| 3048 |
*table_iter++ = phased_flipped_01; |
| 3049 |
} |
| 3050 |
// [168][0]..[179][1] should be impossible |
| 3051 |
table_iter = &(table_iter[24]); |
| 3052 |
// [180][0]..[183][1]: second entry phased-flipped, first phased-unflipped |
| 3053 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3054 |
*table_iter++ = phased_unflipped_vals[low_idx]; |
| 3055 |
*table_iter++ = phased_flipped_01; |
| 3056 |
} |
| 3057 |
// [184][0]..[244][1] should be impossible |
| 3058 |
// [245][0]..[245][1]: both phased-flipped |
| 3059 |
table_iter[122] = phased_flipped_01; |
| 3060 |
table_iter[123] = phased_flipped_01; |
| 3061 |
} |
| 3062 |
|
| 3063 |
void VcfPhaseLookup2b(const uintptr_t* genoarr, const uintptr_t* cur_phased, const uintptr_t* phaseinfo, const void* table246x2bx2, uint32_t sample_ct, void* __restrict result) { |
| 3064 |
const uint32_t* table_alias = S_CAST(const uint32_t*, table246x2bx2); |
| 3065 |
const uint32_t sample_ctl2_m1 = (sample_ct - 1) / kBitsPerWordD2; |
| 3066 |
const Halfword* cur_phased_alias = DowncastKWToHW(cur_phased); |
| 3067 |
const Halfword* phaseinfo_alias = DowncastKWToHW(phaseinfo); |
| 3068 |
unsigned char* result_biter = S_CAST(unsigned char*, result); |
| 3069 |
uint32_t loop_len = kBitsPerWordD4; |
| 3070 |
uintptr_t geno_word = 0; |
| 3071 |
uintptr_t cur_phased_hw_shifted = 0; |
| 3072 |
uintptr_t phaseinfo_hw_shifted = 0; |
| 3073 |
for (uint32_t widx = 0; ; ++widx) { |
| 3074 |
if (widx >= sample_ctl2_m1) { |
| 3075 |
if (widx > sample_ctl2_m1) { |
| 3076 |
if (sample_ct % 2) { |
| 3077 |
uintptr_t cur_idx = (geno_word & 3) | (cur_phased_hw_shifted & 16) | (phaseinfo_hw_shifted & 64); |
| 3078 |
memcpy(result_biter, &(table_alias[cur_idx]), 2); |
| 3079 |
} |
| 3080 |
return; |
| 3081 |
} |
| 3082 |
loop_len = ModNz(sample_ct, kBitsPerWordD2) / 2; |
| 3083 |
} |
| 3084 |
geno_word = genoarr[widx]; |
| 3085 |
cur_phased_hw_shifted = cur_phased_alias[widx]; |
| 3086 |
if (!cur_phased_hw_shifted) { |
| 3087 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 3088 |
AppendU32(table_alias[geno_word & 15], &result_biter); |
| 3089 |
geno_word >>= 4; |
| 3090 |
} |
| 3091 |
} else { |
| 3092 |
cur_phased_hw_shifted = cur_phased_hw_shifted << 4; |
| 3093 |
phaseinfo_hw_shifted = phaseinfo_alias[widx]; |
| 3094 |
|
| 3095 |
// note that this must be on a separate line (or we have to static_cast) |
| 3096 |
phaseinfo_hw_shifted = phaseinfo_hw_shifted << 6; |
| 3097 |
|
| 3098 |
for (uint32_t uii = 0; uii != loop_len; ++uii) { |
| 3099 |
const uintptr_t cur_idx = (geno_word & 15) | (cur_phased_hw_shifted & 48) | (phaseinfo_hw_shifted & 192); |
| 3100 |
AppendU32(table_alias[cur_idx], &result_biter); |
| 3101 |
geno_word >>= 4; |
| 3102 |
cur_phased_hw_shifted >>= 2; |
| 3103 |
phaseinfo_hw_shifted >>= 2; |
| 3104 |
} |
| 3105 |
} |
| 3106 |
} |
| 3107 |
} |
| 3108 |
|
| 3109 |
void InitVcfPhaseLookup2b(void* table246x2bx2) { |
| 3110 |
uint16_t* table_iter = S_CAST(uint16_t*, table246x2bx2); |
| 3111 |
uint16_t unphased_vals[4]; |
| 3112 |
unphased_vals[0] = table_iter[0]; |
| 3113 |
table_iter[1] = unphased_vals[0]; |
| 3114 |
unphased_vals[1] = table_iter[2]; |
| 3115 |
table_iter[3] = unphased_vals[0]; |
| 3116 |
unphased_vals[2] = table_iter[4]; |
| 3117 |
table_iter[5] = unphased_vals[0]; |
| 3118 |
unphased_vals[3] = table_iter[6]; |
| 3119 |
table_iter[7] = unphased_vals[0]; |
| 3120 |
table_iter = &(table_iter[8]); |
| 3121 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 3122 |
const uint32_t cur_high = unphased_vals[high_idx]; |
| 3123 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3124 |
*table_iter++ = unphased_vals[low_idx]; |
| 3125 |
*table_iter++ = cur_high; |
| 3126 |
} |
| 3127 |
} |
| 3128 |
// [16][0]..[31][1]: first entry is phased and unflipped, second is unphased |
| 3129 |
uint16_t phased_unflipped_vals[4]; |
| 3130 |
phased_unflipped_vals[0] = table_iter[0]; |
| 3131 |
table_iter[1] = unphased_vals[0]; |
| 3132 |
phased_unflipped_vals[1] = table_iter[2]; |
| 3133 |
table_iter[3] = unphased_vals[0]; |
| 3134 |
phased_unflipped_vals[2] = table_iter[4]; |
| 3135 |
table_iter[5] = unphased_vals[0]; |
| 3136 |
phased_unflipped_vals[3] = table_iter[6]; |
| 3137 |
table_iter[7] = unphased_vals[0]; |
| 3138 |
table_iter = &(table_iter[8]); |
| 3139 |
for (uint32_t high_idx = 1; high_idx != 4; ++high_idx) { |
| 3140 |
const uint32_t cur_high = unphased_vals[high_idx]; |
| 3141 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3142 |
*table_iter++ = phased_unflipped_vals[low_idx]; |
| 3143 |
*table_iter++ = cur_high; |
| 3144 |
} |
| 3145 |
} |
| 3146 |
// [32][0]..[63][1]: second entry is phased and unflipped |
| 3147 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3148 |
const uint32_t cur_high = phased_unflipped_vals[high_idx]; |
| 3149 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3150 |
*table_iter++ = unphased_vals[low_idx]; |
| 3151 |
*table_iter++ = cur_high; |
| 3152 |
} |
| 3153 |
} |
| 3154 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3155 |
const uint32_t cur_high = phased_unflipped_vals[high_idx]; |
| 3156 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3157 |
*table_iter++ = phased_unflipped_vals[low_idx]; |
| 3158 |
*table_iter++ = cur_high; |
| 3159 |
} |
| 3160 |
} |
| 3161 |
// [64][0]..[79][1] should be impossible |
| 3162 |
table_iter = &(table_iter[32]); |
| 3163 |
// [80][0]..[95][1]: first entry is phased and flipped, second is unphased |
| 3164 |
// genotype must be 01 |
| 3165 |
const uint32_t phased_flipped_01 = table_iter[2]; |
| 3166 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3167 |
table_iter[2] = phased_flipped_01; |
| 3168 |
table_iter[3] = unphased_vals[high_idx]; |
| 3169 |
table_iter = &(table_iter[8]); |
| 3170 |
} |
| 3171 |
// [96][0]..[111][1] should be impossible |
| 3172 |
table_iter = &(table_iter[32]); |
| 3173 |
// [112][0]..[127][1]: first entry phased-flipped, second phased-unflipped |
| 3174 |
for (uint32_t high_idx = 0; high_idx != 4; ++high_idx) { |
| 3175 |
table_iter[2] = phased_flipped_01; |
| 3176 |
table_iter[3] = phased_unflipped_vals[high_idx]; |
| 3177 |
table_iter = &(table_iter[8]); |
| 3178 |
} |
| 3179 |
// [128][0]..[163][1] should be impossible |
| 3180 |
table_iter = &(table_iter[72]); |
| 3181 |
// [164][0]..[167][1]: second entry phased-flipped, first entry unphased |
| 3182 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3183 |
*table_iter++ = unphased_vals[low_idx]; |
| 3184 |
*table_iter++ = phased_flipped_01; |
| 3185 |
} |
| 3186 |
// [168][0]..[179][1] should be impossible |
| 3187 |
table_iter = &(table_iter[24]); |
| 3188 |
// [180][0]..[183][1]: second entry phased-flipped, first phased-unflipped |
| 3189 |
for (uint32_t low_idx = 0; low_idx != 4; ++low_idx) { |
| 3190 |
*table_iter++ = phased_unflipped_vals[low_idx]; |
| 3191 |
*table_iter++ = phased_flipped_01; |
| 3192 |
} |
| 3193 |
// [184][0]..[244][1] should be impossible |
| 3194 |
// [245][0]..[245][1]: both phased-flipped |
| 3195 |
table_iter[122] = phased_flipped_01; |
| 3196 |
table_iter[123] = phased_flipped_01; |
| 3197 |
} |
| 3198 |
|
| 3199 |
|
| 3200 |
void ClearGenoarrMissing1bit8Unsafe(const uintptr_t* __restrict genoarr, uint32_t* subset_sizep, uintptr_t* __restrict subset, void* __restrict sparse_vals) { |
| 3201 |
const uint32_t orig_subset_size = *subset_sizep; |
| 3202 |
Halfword* subset_alias = DowncastWToHW(subset); |
| 3203 |
uint32_t read_idx = 0; |
| 3204 |
// deliberate overflow |
| 3205 |
for (uint32_t read_widx = UINT32_MAX; ; ) { |
| 3206 |
uint32_t subset_bits; |
| 3207 |
do { |
| 3208 |
subset_bits = subset_alias[++read_widx]; |
| 3209 |
} while (!subset_bits); |
| 3210 |
uintptr_t detect_11 = genoarr[read_widx]; |
| 3211 |
detect_11 = detect_11 & (detect_11 >> 1) & kMask5555; |
| 3212 |
if (detect_11) { |
| 3213 |
uint32_t detect_11_hw = PackWordToHalfword(detect_11); |
| 3214 |
const uint32_t joint_u32 = subset_bits & detect_11_hw; |
| 3215 |
if (joint_u32) { |
| 3216 |
uintptr_t lowbit = joint_u32 & (-joint_u32); |
| 3217 |
uint32_t write_idx = read_idx + PopcountWord(subset_bits & (lowbit - 1)); |
| 3218 |
read_idx = write_idx + 1; |
| 3219 |
uint32_t subset_bits_write = subset_bits ^ lowbit; |
| 3220 |
unsigned char* sparse_vals_uc = S_CAST(unsigned char*, sparse_vals); |
| 3221 |
subset_bits &= -(2 * lowbit); |
| 3222 |
for (; read_idx != orig_subset_size; ++read_idx) { |
| 3223 |
#ifdef USE_AVX2 |
| 3224 |
if (!subset_bits) { |
| 3225 |
subset_alias[read_widx] = subset_bits_write; |
| 3226 |
do { |
| 3227 |
subset_bits = subset_alias[++read_widx]; |
| 3228 |
} while (!subset_bits); |
| 3229 |
subset_bits_write = subset_bits; |
| 3230 |
detect_11 = genoarr[read_widx]; |
| 3231 |
detect_11 = detect_11 & (detect_11 >> 1); |
| 3232 |
detect_11_hw = PackWordToHalfwordMask5555(detect_11); |
| 3233 |
} |
| 3234 |
lowbit = subset_bits & (-subset_bits); |
| 3235 |
subset_bits ^= lowbit; |
| 3236 |
if (lowbit & detect_11_hw) { |
| 3237 |
subset_bits_write ^= lowbit; |
| 3238 |
continue; |
| 3239 |
} |
| 3240 |
#else |
| 3241 |
if (!subset_bits) { |
| 3242 |
subset_alias[read_widx] = subset_bits_write; |
| 3243 |
do { |
| 3244 |
subset_bits = subset_alias[++read_widx]; |
| 3245 |
} while (!subset_bits); |
| 3246 |
subset_bits_write = subset_bits; |
| 3247 |
detect_11 = genoarr[read_widx]; |
| 3248 |
detect_11 = detect_11 & (detect_11 >> 1); |
| 3249 |
} |
| 3250 |
lowbit = subset_bits & (-subset_bits); |
| 3251 |
subset_bits ^= lowbit; |
| 3252 |
if ((lowbit * lowbit) & detect_11) { |
| 3253 |
subset_bits_write ^= lowbit; |
| 3254 |
continue; |
| 3255 |
} |
| 3256 |
#endif |
| 3257 |
sparse_vals_uc[write_idx++] = sparse_vals_uc[read_idx]; |
| 3258 |
} |
| 3259 |
subset_alias[read_widx] = subset_bits_write; |
| 3260 |
*subset_sizep = write_idx; |
| 3261 |
return; |
| 3262 |
} |
| 3263 |
} |
| 3264 |
read_idx += PopcountWord(subset_bits); |
| 3265 |
if (read_idx == orig_subset_size) { |
| 3266 |
return; |
| 3267 |
} |
| 3268 |
} |
| 3269 |
} |
| 3270 |
|
| 3271 |
void ClearGenoarrMissing1bit16Unsafe(const uintptr_t* __restrict genoarr, uint32_t* subset_sizep, uintptr_t* __restrict subset, void* __restrict sparse_vals) { |
| 3272 |
const uint32_t orig_subset_size = *subset_sizep; |
| 3273 |
Halfword* subset_alias = DowncastWToHW(subset); |
| 3274 |
uint32_t read_idx = 0; |
| 3275 |
// deliberate overflow |
| 3276 |
for (uint32_t read_widx = UINT32_MAX; ; ) { |
| 3277 |
uint32_t subset_bits; |
| 3278 |
do { |
| 3279 |
subset_bits = subset_alias[++read_widx]; |
| 3280 |
} while (!subset_bits); |
| 3281 |
uintptr_t detect_11 = genoarr[read_widx]; |
| 3282 |
detect_11 = detect_11 & (detect_11 >> 1) & kMask5555; |
| 3283 |
if (detect_11) { |
| 3284 |
uint32_t detect_11_hw = PackWordToHalfword(detect_11); |
| 3285 |
const uint32_t joint_u32 = subset_bits & detect_11_hw; |
| 3286 |
if (joint_u32) { |
| 3287 |
uintptr_t lowbit = joint_u32 & (-joint_u32); |
| 3288 |
uint32_t write_idx = read_idx + PopcountWord(subset_bits & (lowbit - 1)); |
| 3289 |
read_idx = write_idx + 1; |
| 3290 |
uint32_t subset_bits_write = subset_bits ^ lowbit; |
| 3291 |
uint16_t* sparse_vals_u16 = S_CAST(uint16_t*, sparse_vals); |
| 3292 |
subset_bits &= -(2 * lowbit); |
| 3293 |
for (; read_idx != orig_subset_size; ++read_idx) { |
| 3294 |
#ifdef USE_AVX2 |
| 3295 |
if (!subset_bits) { |
| 3296 |
subset_alias[read_widx] = subset_bits_write; |
| 3297 |
do { |
| 3298 |
subset_bits = subset_alias[++read_widx]; |
| 3299 |
} while (!subset_bits); |
| 3300 |
subset_bits_write = subset_bits; |
| 3301 |
detect_11 = genoarr[read_widx]; |
| 3302 |
detect_11 = detect_11 & (detect_11 >> 1); |
| 3303 |
detect_11_hw = PackWordToHalfwordMask5555(detect_11); |
| 3304 |
} |
| 3305 |
lowbit = subset_bits & (-subset_bits); |
| 3306 |
subset_bits ^= lowbit; |
| 3307 |
if (lowbit & detect_11_hw) { |
| 3308 |
subset_bits_write ^= lowbit; |
| 3309 |
continue; |
| 3310 |
} |
| 3311 |
#else |
| 3312 |
if (!subset_bits) { |
| 3313 |
subset_alias[read_widx] = subset_bits_write; |
| 3314 |
do { |
| 3315 |
subset_bits = subset_alias[++read_widx]; |
| 3316 |
} while (!subset_bits); |
| 3317 |
subset_bits_write = subset_bits; |
| 3318 |
detect_11 = genoarr[read_widx]; |
| 3319 |
detect_11 = detect_11 & (detect_11 >> 1); |
| 3320 |
} |
| 3321 |
lowbit = subset_bits & (-subset_bits); |
| 3322 |
subset_bits ^= lowbit; |
| 3323 |
if ((lowbit * lowbit) & detect_11) { |
| 3324 |
subset_bits_write ^= lowbit; |
| 3325 |
continue; |
| 3326 |
} |
| 3327 |
#endif |
| 3328 |
sparse_vals_u16[write_idx++] = sparse_vals_u16[read_idx]; |
| 3329 |
} |
| 3330 |
subset_alias[read_widx] = subset_bits_write; |
| 3331 |
*subset_sizep = write_idx; |
| 3332 |
return; |
| 3333 |
} |
| 3334 |
} |
| 3335 |
read_idx += PopcountWord(subset_bits); |
| 3336 |
if (read_idx == orig_subset_size) { |
| 3337 |
return; |
| 3338 |
} |
| 3339 |
} |
| 3340 |
} |
| 3341 |
|
| 3342 |
double u127prod_diff_d(uint64_t plus_term0, uint64_t plus_term1, uint64_t minus_term0, uint64_t minus_term1) { |
| 3343 |
uint64_t plus_hi; |
| 3344 |
const uint64_t plus_lo = multiply64to128(plus_term0, plus_term1, &plus_hi); |
| 3345 |
uint64_t minus_hi; |
| 3346 |
const uint64_t minus_lo = multiply64to128(minus_term0, minus_term1, &minus_hi); |
| 3347 |
const uint64_t result_lo = plus_lo - minus_lo; |
| 3348 |
const uint64_t result_hi = plus_hi - minus_hi - (plus_lo < minus_lo); |
| 3349 |
return u127tod(result_hi, result_lo); |
| 3350 |
} |
| 3351 |
|
| 3352 |
double i127prod_diff_d(uint64_t plus_term0, uint64_t plus_term1, uint64_t minus_term0, uint64_t minus_term1) { |
| 3353 |
uint64_t plus_hi; |
| 3354 |
const uint64_t plus_lo = multiply64to128(plus_term0, plus_term1, &plus_hi); |
| 3355 |
uint64_t minus_hi; |
| 3356 |
const uint64_t minus_lo = multiply64to128(minus_term0, minus_term1, &minus_hi); |
| 3357 |
const uint64_t result_lo = plus_lo - minus_lo; |
| 3358 |
const uint64_t result_hi = plus_hi - minus_hi - (plus_lo < minus_lo); |
| 3359 |
if (!(result_hi & (1LLU << 63))) { |
| 3360 |
return u127tod(result_hi, result_lo); |
| 3361 |
} else { |
| 3362 |
const uint64_t minus_result_lo = -result_lo; |
| 3363 |
const uint64_t minus_result_hi = (-result_hi) - (result_lo != 0); |
| 3364 |
return -u127tod(minus_result_hi, minus_result_lo); |
| 3365 |
} |
| 3366 |
} |
| 3367 |
|
| 3368 |
double MultiallelicDiploidMinimac3R2(const uint64_t* __restrict sums, const uint64_t* __restrict hap_ssqs_x2, uint32_t nm_sample_ct, uint32_t allele_ct, uint32_t extra_phased_het_ct) { |
| 3369 |
// sums[k] == sum_i [left_dosage_{ik} + right_dosage_{ik}] |
| 3370 |
// hap_ssqs_x2[k] == |
| 3371 |
// 2 * sum_i [(left_dosage_{ik})^2 + (right_dosage_{ik})^2] |
| 3372 |
// This may be odd, since it's computed as |
| 3373 |
// (left + right)^2 + (left - right)^2 |
| 3374 |
// and the parities of the two integers can be different. |
| 3375 |
// For phased hardcalls, it is fine for the hap_ssqs_x2[k] values to |
| 3376 |
// correspond to unphased hardcalls iff extra_phased_het_ct is the number of |
| 3377 |
// phased-hets that weren't accounted for in hap_ssqs_x2[]; this makes it |
| 3378 |
// straightforward for GetMultiallelicCountsAndDosage16s to stick to the |
| 3379 |
// custom internal multiallelic-count functions. |
| 3380 |
if (!nm_sample_ct) { |
| 3381 |
return (0.0 / 0.0); |
| 3382 |
} |
| 3383 |
// Allelic phased-dosages are on a (k-1)-dimensional simplex; embed this in |
| 3384 |
// R^k as the (1, 0, ..., 0), (0, 1, ..., 0), ..., (0, 0, ..., 1) polytope. |
| 3385 |
// Define |
| 3386 |
// m_k := (1/2n) * sum_i [left_dosage_{ik} + right_dosage_{ik}] |
| 3387 |
// Minimac3-r2 is defined as empirical phased-dosage variance divided by |
| 3388 |
// expected-under-allele-frequencies variance. |
| 3389 |
// Expected sum-of-squared-Euclidean-distance with perfect imputation is |
| 3390 |
// 2n("1"^2 - sum_k ((m_k)^2)) |
| 3391 |
// and observed sum-of-squared-distance is |
| 3392 |
// sum_k (sum_i [(left_dosage_{ik})^2 + (right_dosage_{ik})^2] - |
| 3393 |
// 2n((m_k)^2)) |
| 3394 |
|
| 3395 |
// ssq_sum_x2 can be as large as 2^31 * nm_sample_ct; meansq_sum can cancel |
| 3396 |
// as little as (1 / allele_ct) of it |
| 3397 |
if (nm_sample_ct < 92682) { |
| 3398 |
uint64_t ssq_sum_x2 = extra_phased_het_ct * 0x20000000LLU; |
| 3399 |
uint64_t meansq_sum = 0; |
| 3400 |
for (uint32_t allele_idx = 0; allele_idx != allele_ct; ++allele_idx) { |
| 3401 |
const uint64_t cur_allele_dosage = sums[allele_idx]; |
| 3402 |
ssq_sum_x2 += hap_ssqs_x2[allele_idx]; |
| 3403 |
// cur_allele_dosage == 2n * m_k |
| 3404 |
// -> meansq_sum becomes 2n * sum_k [2n((m_k)^2)] |
| 3405 |
meansq_sum += cur_allele_dosage * cur_allele_dosage; |
| 3406 |
} |
| 3407 |
const uint64_t observed_variance_times_2n = ssq_sum_x2 * nm_sample_ct - meansq_sum; |
| 3408 |
// "1"^2 -> 16384^2 in our units. So 2n * 2n * "1"^2 is equal to |
| 3409 |
// n * n * 16384^2 * 4. |
| 3410 |
const uint64_t expected_variance_times_2n = nm_sample_ct * 0x40000000LLU * nm_sample_ct - meansq_sum; |
| 3411 |
// mach_r2 == 1 test cases: |
| 3412 |
// - AA, AB, BB: 1, 4, 4 |
| 3413 |
// sums[0] = 6 * 2^14 |
| 3414 |
// sums[1] = 12 * 2^14 |
| 3415 |
// ssqs[0] = 8 * 2^28 |
| 3416 |
// ssqs[1] = 20 * 2^28 |
| 3417 |
// ssq_sum = (8 + 20) * 2^28 |
| 3418 |
// meansq_sum = (6 * 6 + 12 * 12) * 2^28 |
| 3419 |
// observed_variance = 28 * 9 * 2^28 - 180 * 2^28 |
| 3420 |
// expected_variance = (9 * 9 * 4 * 2^28 - 180 * 2^28) / 2 |
| 3421 |
// - AA, AB, BB, AC, BC, CC: 1, 4, 4, 6, 12, 9 |
| 3422 |
// sums[0] = 12 * 2^14 |
| 3423 |
// sums[1] = 24 * 2^14 |
| 3424 |
// sums[2] = 36 * 2^14 |
| 3425 |
// ssqs[0] = 14 * 2^28 |
| 3426 |
// ssqs[1] = 32 * 2^28 |
| 3427 |
// ssqs[2] = 54 * 2^28 |
| 3428 |
// ssq_sum = (14 + 32 + 54) * 2^28 |
| 3429 |
// meansq_sum = (12 * 12 + 24 * 24 + 36 * 36) * 2^28 |
| 3430 |
// observed_variance = 100 * 36 * 2^28 - 56 * 36 * 2^28 |
| 3431 |
// expected_variance = (36 * 36 * 4 * 2^28 - 56 * 36 * 2^28) / 2 |
| 3432 |
return S_CAST(double, observed_variance_times_2n) / S_CAST(double, expected_variance_times_2n); |
| 3433 |
} |
| 3434 |
uint64_t ssq_sum_x2 = extra_phased_het_ct * 0x20000000LLU; |
| 3435 |
uint64_t meansq_sum_lo = 0; |
| 3436 |
uint64_t meansq_sum_hi = 0; |
| 3437 |
for (uint32_t allele_idx = 0; allele_idx != allele_ct; ++allele_idx) { |
| 3438 |
const uint64_t cur_allele_dosage = sums[allele_idx]; |
| 3439 |
const uint64_t cur_ssq_x2 = hap_ssqs_x2[allele_idx]; |
| 3440 |
ssq_sum_x2 += cur_ssq_x2; |
| 3441 |
uint64_t incr_hi; |
| 3442 |
uint64_t incr_lo = multiply64to128(cur_allele_dosage, cur_allele_dosage, &incr_hi); |
| 3443 |
meansq_sum_lo += incr_lo; |
| 3444 |
meansq_sum_hi += incr_hi + (meansq_sum_lo < incr_lo); |
| 3445 |
} |
| 3446 |
uint64_t prod_hi; |
| 3447 |
uint64_t prod_lo = multiply64to128(ssq_sum_x2, nm_sample_ct, &prod_hi); |
| 3448 |
const uint64_t observed_variance_times_2n_lo = prod_lo - meansq_sum_lo; |
| 3449 |
const uint64_t observed_variance_times_2n_hi = prod_hi - meansq_sum_hi - (prod_lo < meansq_sum_lo); |
| 3450 |
const double observed_variance_times_2n = u127tod(observed_variance_times_2n_hi, observed_variance_times_2n_lo); |
| 3451 |
|
| 3452 |
prod_lo = multiply64to128(nm_sample_ct * 0x40000000LLU, nm_sample_ct, &prod_hi); |
| 3453 |
const uint64_t expected_variance_times_2n_lo = prod_lo - meansq_sum_lo; |
| 3454 |
const uint64_t expected_variance_times_2n_hi = prod_hi - meansq_sum_hi - (prod_lo < meansq_sum_lo); |
| 3455 |
const double expected_variance_times_2n = u127tod(expected_variance_times_2n_hi, expected_variance_times_2n_lo); |
| 3456 |
return observed_variance_times_2n / expected_variance_times_2n; |
| 3457 |
} |
| 3458 |
|
| 3459 |
void PgrDifflistToGenovecUnsafe(const uintptr_t* __restrict raregeno, const uint32_t* difflist_sample_ids, uintptr_t difflist_common_geno, uint32_t sample_ct, uint32_t difflist_len, uintptr_t* __restrict genovec) { |
| 3460 |
// Ok for trailing bits of raregeno to be nonzero. Does not zero out |
| 3461 |
// trailing bits of genovec. |
| 3462 |
const uint32_t vec_ct = NypCtToVecCt(sample_ct); |
| 3463 |
// could just memset up to word boundary; this should be a bit more |
| 3464 |
// vector-instruction-friendly, though |
| 3465 |
vecset(genovec, difflist_common_geno * kMask5555, vec_ct); |
| 3466 |
const uintptr_t* raregeno_incr = raregeno; |
| 3467 |
uint32_t difflist_idx = 0; |
| 3468 |
uint32_t difflist_idx_stop = kBitsPerWordD2; |
| 3469 |
if (!difflist_common_geno) { |
| 3470 |
// faster inner loop since there's no existing value to mask out |
| 3471 |
// todo: check if this should just be deleted since the code bloat causes |
| 3472 |
// too many more cache misses |
| 3473 |
for (; ; difflist_idx_stop += kBitsPerWordD2) { |
| 3474 |
if (difflist_idx_stop > difflist_len) { |
| 3475 |
if (difflist_idx == difflist_len) { |
| 3476 |
return; |
| 3477 |
} |
| 3478 |
difflist_idx_stop = difflist_len; |
| 3479 |
} |
| 3480 |
uintptr_t raregeno_word = *raregeno_incr++; |
| 3481 |
for (; difflist_idx != difflist_idx_stop; ++difflist_idx) { |
| 3482 |
const uint32_t cur_sample_idx = difflist_sample_ids[difflist_idx]; |
| 3483 |
genovec[cur_sample_idx / kBitsPerWordD2] |= (raregeno_word & 3) << (2 * (cur_sample_idx % kBitsPerWordD2)); |
| 3484 |
raregeno_word >>= 2; |
| 3485 |
} |
| 3486 |
} |
| 3487 |
} |
| 3488 |
for (; ; difflist_idx_stop += kBitsPerWordD2) { |
| 3489 |
if (difflist_idx_stop > difflist_len) { |
| 3490 |
if (difflist_idx == difflist_len) { |
| 3491 |
return; |
| 3492 |
} |
| 3493 |
difflist_idx_stop = difflist_len; |
| 3494 |
} |
| 3495 |
uintptr_t raregeno_word = *raregeno_incr++; |
| 3496 |
for (; difflist_idx != difflist_idx_stop; ++difflist_idx) { |
| 3497 |
const uint32_t cur_sample_idx = difflist_sample_ids[difflist_idx]; |
| 3498 |
AssignNyparrEntry(cur_sample_idx, raregeno_word & 3, genovec); |
| 3499 |
raregeno_word >>= 2; |
| 3500 |
} |
| 3501 |
} |
| 3502 |
} |
| 3503 |
|
| 3504 |
const uint16_t kHcToAlleleCodes[1024] = QUAD_TABLE256(0, 0x100, 0x101, 0xffff); |
| 3505 |
|
| 3506 |
static_assert(sizeof(AlleleCode) == 1, "PglMultiallelicSparseToDenseMiss() needs to be updated."); |
| 3507 |
void PglMultiallelicSparseToDenseMiss(const PgenVariant* pgvp, uint32_t sample_ct, AlleleCode* __restrict wide_codes) { |
| 3508 |
GenoarrLookup256x2bx4(pgvp->genovec, kHcToAlleleCodes, sample_ct, wide_codes); |
| 3509 |
const uint32_t patch_01_ct = pgvp->patch_01_ct; |
| 3510 |
if (patch_01_ct) { |
| 3511 |
const uintptr_t* patch_01_set = pgvp->patch_01_set; |
| 3512 |
uintptr_t sample_idx_base = 0; |
| 3513 |
uintptr_t cur_bits = patch_01_set[0]; |
| 3514 |
const AlleleCode* patch_01_vals = pgvp->patch_01_vals; |
| 3515 |
AlleleCode* wide_codes1 = &(wide_codes[1]); |
| 3516 |
for (uint32_t uii = 0; uii != patch_01_ct; ++uii) { |
| 3517 |
const uintptr_t sample_idx = BitIter1(patch_01_set, &sample_idx_base, &cur_bits); |
| 3518 |
wide_codes1[2 * sample_idx] = patch_01_vals[uii]; |
| 3519 |
} |
| 3520 |
} |
| 3521 |
const uint32_t patch_10_ct = pgvp->patch_10_ct; |
| 3522 |
if (patch_10_ct) { |
| 3523 |
const uintptr_t* patch_10_set = pgvp->patch_10_set; |
| 3524 |
uintptr_t sample_idx_base = 0; |
| 3525 |
uintptr_t cur_bits = patch_10_set[0]; |
| 3526 |
const DoubleAlleleCode* patch_10_vals_alias = R_CAST(const DoubleAlleleCode*, pgvp->patch_10_vals); |
| 3527 |
DoubleAlleleCode* wide_codes_alias = R_CAST(DoubleAlleleCode*, wide_codes); |
| 3528 |
for (uint32_t uii = 0; uii != patch_10_ct; ++uii) { |
| 3529 |
const uintptr_t sample_idx = BitIter1(patch_10_set, &sample_idx_base, &cur_bits); |
| 3530 |
wide_codes_alias[sample_idx] = patch_10_vals_alias[uii]; |
| 3531 |
} |
| 3532 |
} |
| 3533 |
} |
| 3534 |
|
| 3535 |
uintptr_t PglComputeMaxAlleleCt(const uintptr_t* allele_idx_offsets, uint32_t variant_ct) { |
| 3536 |
if ((!allele_idx_offsets) || (allele_idx_offsets[variant_ct] == 2 * variant_ct)) { |
| 3537 |
return 2; |
| 3538 |
} |
| 3539 |
// todo: try vectorizing this |
| 3540 |
uintptr_t max_allele_ct = 2; |
| 3541 |
uintptr_t prev_offset = allele_idx_offsets[0]; |
| 3542 |
const uintptr_t* shifted_offsets = &(allele_idx_offsets[1]); |
| 3543 |
for (uintptr_t uii = 0; uii != variant_ct; ++uii) { |
| 3544 |
const uintptr_t cur_offset = shifted_offsets[uii]; |
| 3545 |
const uintptr_t cur_allele_ct = cur_offset - prev_offset; |
| 3546 |
if (cur_allele_ct > max_allele_ct) { |
| 3547 |
max_allele_ct = cur_allele_ct; |
| 3548 |
} |
| 3549 |
prev_offset = cur_offset; |
| 3550 |
} |
| 3551 |
return max_allele_ct; |
| 3552 |
} |
| 3553 |
|
| 3554 |
// Ok for nybble_vvec to be unaligned. |
| 3555 |
uint32_t CountNybbleVec(const unsigned char* nybble_vvec_biter, uintptr_t nybble_word, uint32_t vec_ct) { |
| 3556 |
const VecW m0 = vecw_setzero(); |
| 3557 |
const VecW alld15 = VCONST_W(kMask1111); |
| 3558 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 3559 |
const VecW xor_vvec = vecw_set1(nybble_word); |
| 3560 |
VecW prev_sad_result = vecw_setzero(); |
| 3561 |
VecW acc = vecw_setzero(); |
| 3562 |
uintptr_t cur_incr = 15; |
| 3563 |
for (; ; vec_ct -= cur_incr) { |
| 3564 |
if (vec_ct < 15) { |
| 3565 |
if (!vec_ct) { |
| 3566 |
acc = acc + prev_sad_result; |
| 3567 |
return HsumW(acc); |
| 3568 |
} |
| 3569 |
cur_incr = vec_ct; |
| 3570 |
} |
| 3571 |
VecW inner_acc = vecw_setzero(); |
| 3572 |
const unsigned char* nybble_vvec_stop = &(nybble_vvec_biter[cur_incr * kBytesPerVec]); |
| 3573 |
do { |
| 3574 |
VecW loader = vecw_loadu(nybble_vvec_biter) ^ xor_vvec; |
| 3575 |
nybble_vvec_biter += kBytesPerVec; |
| 3576 |
// DetectAllZeroNybbles() followed by right-shift-3 is the same number of |
| 3577 |
// operations, can see if that's any faster in practice |
| 3578 |
loader = vecw_srli(loader, 1) | loader; |
| 3579 |
loader = vecw_srli(loader, 2) | loader; |
| 3580 |
inner_acc = inner_acc + vecw_and_notfirst(loader, alld15); |
| 3581 |
} while (nybble_vvec_biter < nybble_vvec_stop); |
| 3582 |
inner_acc = (inner_acc & m4) + (vecw_srli(inner_acc, 4) & m4); |
| 3583 |
acc = acc + prev_sad_result; |
| 3584 |
prev_sad_result = vecw_bytesum(inner_acc, m0); |
| 3585 |
} |
| 3586 |
} |
| 3587 |
|
| 3588 |
uint32_t CountNybble(const void* nybblearr, uintptr_t nybble_word, uintptr_t nybble_ct) { |
| 3589 |
const unsigned char* nybblearr_uc = S_CAST(const unsigned char*, nybblearr); |
| 3590 |
const uint32_t fullword_ct = nybble_ct / kBitsPerWordD4; |
| 3591 |
uint32_t tot = CountNybbleVec(nybblearr_uc, nybble_word, fullword_ct / kWordsPerVec); |
| 3592 |
#ifdef __LP64__ |
| 3593 |
for (uint32_t word_idx = RoundDownPow2(fullword_ct, kWordsPerVec); word_idx != fullword_ct; ++word_idx) { |
| 3594 |
uintptr_t cur_word; |
| 3595 |
CopyFromUnalignedOffsetW(&cur_word, nybblearr_uc, word_idx); |
| 3596 |
cur_word ^= nybble_word; |
| 3597 |
cur_word = cur_word | (cur_word >> 1); |
| 3598 |
cur_word = cur_word | (cur_word >> 2); |
| 3599 |
tot += Popcount0001Word((~cur_word) & kMask1111); |
| 3600 |
} |
| 3601 |
#endif |
| 3602 |
const uint32_t trailing_nybble_ct = nybble_ct % kBitsPerWordD4; |
| 3603 |
if (trailing_nybble_ct) { |
| 3604 |
const uint32_t trailing_byte_ct = DivUp(trailing_nybble_ct, (CHAR_BIT / 4)); |
| 3605 |
uintptr_t cur_word = SubwordLoad(&(nybblearr_uc[fullword_ct * kBytesPerWord]), trailing_byte_ct) ^ nybble_word; |
| 3606 |
cur_word = cur_word | (cur_word >> 1); |
| 3607 |
cur_word = cur_word | (cur_word >> 2); |
| 3608 |
cur_word = bzhi((~cur_word) & kMask1111, trailing_nybble_ct * 4); |
| 3609 |
#if defined(USE_SSE42) || !defined(__LP64__) |
| 3610 |
tot += Popcount0001Word(cur_word); |
| 3611 |
#else |
| 3612 |
// minor optimization, can't overflow |
| 3613 |
tot += (cur_word * kMask1111) >> 60; |
| 3614 |
#endif |
| 3615 |
} |
| 3616 |
return tot; |
| 3617 |
} |
| 3618 |
|
| 3619 |
uint64_t PglHeaderBaseEndOffset(uint32_t variant_ct, uintptr_t vrec_len_byte_ct, uint32_t phase_or_dosage_present, uint32_t explicit_nonref_flags) { |
| 3620 |
const uint32_t vblock_ct = DivUp(variant_ct, kPglVblockSize); |
| 3621 |
uint64_t offset = 12 + vblock_ct * sizeof(int64_t) + variant_ct * vrec_len_byte_ct; |
| 3622 |
if (phase_or_dosage_present) { |
| 3623 |
// 8-bit vrtypes |
| 3624 |
offset += variant_ct; |
| 3625 |
} else { |
| 3626 |
// 4-bit vrtypes |
| 3627 |
offset += DivUp(variant_ct, 2); |
| 3628 |
} |
| 3629 |
if (explicit_nonref_flags) { |
| 3630 |
offset += DivUp(variant_ct, CHAR_BIT); |
| 3631 |
} |
| 3632 |
return offset; |
| 3633 |
} |
| 3634 |
|
| 3635 |
#ifdef __cplusplus |
| 3636 |
} // namespace plink2 |
| 3637 |
#endif |