| 1 |
#ifndef __PLINK2_BITS_H__ |
| 2 |
#define __PLINK2_BITS_H__ |
| 3 |
|
| 4 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This library is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU Lesser General Public License as published by the |
| 9 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 10 |
// option) any later version. |
| 11 |
// |
| 12 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 15 |
// for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
// Bitarray support. (Inline single-word operations are in plink2_base.h.) |
| 22 |
|
| 23 |
#include "plink2_base.h" |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
namespace plink2 { |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(USE_SSE2) && !defined(USE_AVX2) |
| 30 |
// may also want a version which doesn't always apply kMask5555 |
| 31 |
void Pack32bTo16bMask(const void* words_vec, uintptr_t ct_32b, void* dest); |
| 32 |
|
| 33 |
HEADER_INLINE void PackWordsToHalfwordsMask(const uintptr_t* words_vec, uintptr_t word_ct, void* dest) { |
| 34 |
uintptr_t widx = 0; |
| 35 |
if (word_ct >= (32 / kBytesPerWord)) { |
| 36 |
const uintptr_t ct_32b = word_ct / (32 / kBytesPerWord); |
| 37 |
Pack32bTo16bMask(words_vec, ct_32b, dest); |
| 38 |
widx = ct_32b * (32 / kBytesPerWord); |
| 39 |
} |
| 40 |
unsigned char* dest_uc = S_CAST(unsigned char*, dest); |
| 41 |
for (; widx != word_ct; ++widx) { |
| 42 |
const Halfword hw = PackWordToHalfwordMask5555(words_vec[widx]); |
| 43 |
CopyToUnalignedOffsetHW(dest_uc, &hw, widx); |
| 44 |
} |
| 45 |
} |
| 46 |
#else |
| 47 |
HEADER_INLINE void PackWordsToHalfwordsMask(const uintptr_t* words_vec, uintptr_t word_ct, void* dest) { |
| 48 |
unsigned char* dest_uc = S_CAST(unsigned char*, dest); |
| 49 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 50 |
const Halfword hw = PackWordToHalfwordMask5555(words_vec[widx]); |
| 51 |
CopyToUnalignedOffsetHW(dest_uc, &hw, widx); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#endif |
| 56 |
|
| 57 |
// ok for ct == 0 |
| 58 |
void SetAllBits(uintptr_t ct, uintptr_t* bitarr); |
| 59 |
|
| 60 |
// "Nz" added to names to make it obvious these require positive len |
| 61 |
void FillBitsNz(uintptr_t start_idx, uintptr_t end_idx, uintptr_t* bitarr); |
| 62 |
void ClearBitsNz(uintptr_t start_idx, uintptr_t end_idx, uintptr_t* bitarr); |
| 63 |
|
| 64 |
void BitvecAnd(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec); |
| 65 |
|
| 66 |
void BitvecInvmask(const uintptr_t* __restrict exclude_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec); |
| 67 |
|
| 68 |
void BitvecOr(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* main_bitvec); |
| 69 |
|
| 70 |
void BitvecInvert(uintptr_t word_ct, uintptr_t* main_bitvec); |
| 71 |
|
| 72 |
void BitvecXorCopy(const uintptr_t* __restrict source1_bitvec, const uintptr_t* __restrict source2_bitvec, uintptr_t word_ct, uintptr_t* target_bitvec); |
| 73 |
|
| 74 |
void BitvecInvertCopy(const uintptr_t* __restrict source_bitvec, uintptr_t word_ct, uintptr_t* __restrict target_bitvec); |
| 75 |
|
| 76 |
// These ensure the trailing bits are zeroed out. |
| 77 |
// 'AlignedBitarr' instead of Bitvec since this takes bit_ct instead of word_ct |
| 78 |
// as the size argument, and zeroes trailing bits. |
| 79 |
HEADER_INLINE void AlignedBitarrInvert(uintptr_t bit_ct, uintptr_t* main_bitvec) { |
| 80 |
const uintptr_t fullword_ct = bit_ct / kBitsPerWord; |
| 81 |
BitvecInvert(fullword_ct, main_bitvec); |
| 82 |
const uint32_t trail_ct = bit_ct % kBitsPerWord; |
| 83 |
if (trail_ct) { |
| 84 |
main_bitvec[fullword_ct] = bzhi(~main_bitvec[fullword_ct], trail_ct); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
HEADER_INLINE void AlignedBitarrInvertCopy(const uintptr_t* __restrict source_bitvec, uintptr_t bit_ct, uintptr_t* __restrict target_bitvec) { |
| 89 |
const uintptr_t fullword_ct = bit_ct / kBitsPerWord; |
| 90 |
BitvecInvertCopy(source_bitvec, fullword_ct, target_bitvec); |
| 91 |
const uint32_t trail_ct = bit_ct % kBitsPerWord; |
| 92 |
if (trail_ct) { |
| 93 |
target_bitvec[fullword_ct] = bzhi(~source_bitvec[fullword_ct], trail_ct); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Functions with "adv" in the name generally take an index or char-pointer as |
| 98 |
// an argument and return its new value, while "mov" functions take a |
| 99 |
// pointer-to-index or pointer-to-char-pointer and move it. |
| 100 |
|
| 101 |
// These return the current index if the corresponding bit satisfies the |
| 102 |
// condition. |
| 103 |
uintptr_t AdvTo1Bit(const uintptr_t* bitarr, uintptr_t loc); |
| 104 |
|
| 105 |
uintptr_t AdvTo0Bit(const uintptr_t* bitarr, uintptr_t loc); |
| 106 |
|
| 107 |
// uintptr_t NextNonmissingUnsafe(const uintptr_t* genoarr, uintptr_t loc); |
| 108 |
|
| 109 |
uint32_t AdvBoundedTo1Bit(const uintptr_t* bitarr, uint32_t loc, uint32_t ceil); |
| 110 |
|
| 111 |
uintptr_t AdvBoundedTo0Bit(const uintptr_t* bitarr, uintptr_t loc, uintptr_t ceil); |
| 112 |
|
| 113 |
uintptr_t FindLast1BitBefore(const uintptr_t* bitarr, uintptr_t loc); |
| 114 |
|
| 115 |
// possible todo: check if movemask-based solution is better in AVX2 case |
| 116 |
HEADER_INLINE uint32_t AllWordsAreZero(const uintptr_t* word_arr, uintptr_t word_ct) { |
| 117 |
while (word_ct--) { |
| 118 |
if (*word_arr++) { |
| 119 |
return 0; |
| 120 |
} |
| 121 |
} |
| 122 |
return 1; |
| 123 |
} |
| 124 |
|
| 125 |
HEADER_INLINE uint32_t AllBitsAreOne(const uintptr_t* bitarr, uintptr_t bit_ct) { |
| 126 |
const uintptr_t fullword_ct = bit_ct / kBitsPerWord; |
| 127 |
for (uintptr_t widx = 0; widx != fullword_ct; ++widx) { |
| 128 |
if (~(bitarr[widx])) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
} |
| 132 |
const uint32_t trailing_bit_ct = bit_ct % kBitsPerWord; |
| 133 |
return (!trailing_bit_ct) || ((~(bitarr[fullword_ct])) << (kBitsPerWord - trailing_bit_ct)); |
| 134 |
} |
| 135 |
|
| 136 |
uint32_t AllBytesAreX(const unsigned char* bytes, unsigned char match, uintptr_t byte_ct); |
| 137 |
|
| 138 |
// Updated PopcountWords() code is based on |
| 139 |
// https://github.com/kimwalisch/libpopcnt . libpopcnt license text follows. |
| 140 |
|
| 141 |
/* |
| 142 |
* libpopcnt.h - C/C++ library for counting the number of 1 bits (bit |
| 143 |
* population count) in an array as quickly as possible using |
| 144 |
* specialized CPU instructions i.e. POPCNT, AVX2, AVX512, NEON. |
| 145 |
* |
| 146 |
* Copyright (c) 2016 - 2017, Kim Walisch |
| 147 |
* Copyright (c) 2016 - 2017, Wojciech Mula |
| 148 |
* |
| 149 |
* All rights reserved. |
| 150 |
* |
| 151 |
* Redistribution and use in source and binary forms, with or without |
| 152 |
* modification, are permitted provided that the following conditions are met: |
| 153 |
* |
| 154 |
* 1. Redistributions of source code must retain the above copyright notice, this |
| 155 |
* list of conditions and the following disclaimer. |
| 156 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 157 |
* this list of conditions and the following disclaimer in the documentation |
| 158 |
* and/or other materials provided with the distribution. |
| 159 |
* |
| 160 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 161 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 162 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 163 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 164 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 165 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 166 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 167 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 168 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 169 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 170 |
*/ |
| 171 |
|
| 172 |
#ifdef USE_AVX2 |
| 173 |
// 'Csa' = carry, save, add |
| 174 |
// If bb, cc, and *lp are bitvectors, this returns the carry bitvector and sets |
| 175 |
// *lp to contain the low-order bits of the sums. I.e. for each position: |
| 176 |
// if none of bb, cc, and *lp are set, *lp bit is zero and carry bit is zero |
| 177 |
// if exactly 1 is set, *lp bit becomes one and carry bit is zero |
| 178 |
// if exactly 2 are set, *lp bit becomes zero and carry bit is one |
| 179 |
// if all 3 are set, *lp bit becomes one and carry bit is one |
| 180 |
HEADER_INLINE VecW Csa256(VecW bb, VecW cc, VecW* lp) { |
| 181 |
const VecW aa = *lp; |
| 182 |
const VecW uu = aa ^ bb; |
| 183 |
*lp = uu ^ cc; |
| 184 |
return (aa & bb) | (uu & cc); |
| 185 |
} |
| 186 |
|
| 187 |
HEADER_INLINE VecW CsaOne256(VecW bb, VecW* lp) { |
| 188 |
const VecW aa = *lp; |
| 189 |
*lp = aa ^ bb; |
| 190 |
return aa & bb; |
| 191 |
} |
| 192 |
|
| 193 |
HEADER_INLINE VecW PopcountVecAvx2(VecW vv) { |
| 194 |
const VecW lookup1 = vecw_setr8(4, 5, 5, 6, 5, 6, 6, 7, |
| 195 |
5, 6, 6, 7, 6, 7, 7, 8); |
| 196 |
const VecW lookup2 = vecw_setr8(4, 3, 3, 2, 3, 2, 2, 1, |
| 197 |
3, 2, 2, 1, 2, 1, 1, 0); |
| 198 |
|
| 199 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 200 |
const VecW lo = vv & m4; |
| 201 |
const VecW hi = vecw_srli(vv, 4) & m4; |
| 202 |
const VecW popcnt1 = vecw_shuffle8(lookup1, lo); |
| 203 |
const VecW popcnt2 = vecw_shuffle8(lookup2, hi); |
| 204 |
return vecw_sad(popcnt1, popcnt2); |
| 205 |
} |
| 206 |
|
| 207 |
HEADER_INLINE uintptr_t HsumW(VecW vv) { |
| 208 |
UniVec vu; |
| 209 |
vu.vw = vv; |
| 210 |
return vu.w[0] + vu.w[1] + vu.w[2] + vu.w[3]; |
| 211 |
// _mm256_extract_epi64() only worth it if we don't need to extract all the |
| 212 |
// values. |
| 213 |
// (also, I wouldn't be surprised if the compiler recognized the pattern |
| 214 |
// above) |
| 215 |
} |
| 216 |
|
| 217 |
// This no longer has any restrictions on vec_ct, though it isn't worth the |
| 218 |
// overhead for vec_ct < 16. |
| 219 |
uintptr_t PopcountVecsAvx2(const VecW* bit_vvec, uintptr_t vec_ct); |
| 220 |
|
| 221 |
HEADER_INLINE uintptr_t PopcountWords(const uintptr_t* bitvec, uintptr_t word_ct) { |
| 222 |
// Efficiently popcounts bitvec[0..(word_ct - 1)]. In the 64-bit case, |
| 223 |
// bitvec[] must be 16-byte aligned. |
| 224 |
// The PopcountWordsNzbase() wrapper takes care of starting from a later |
| 225 |
// index. |
| 226 |
uintptr_t tot = 0; |
| 227 |
if (word_ct >= 76) { |
| 228 |
assert(IsVecAligned(bitvec)); |
| 229 |
const uintptr_t remainder = word_ct % kWordsPerVec; |
| 230 |
const uintptr_t main_block_word_ct = word_ct - remainder; |
| 231 |
tot = PopcountVecsAvx2(R_CAST(const VecW*, bitvec), main_block_word_ct / kWordsPerVec); |
| 232 |
bitvec = &(bitvec[main_block_word_ct]); |
| 233 |
word_ct = remainder; |
| 234 |
} |
| 235 |
// note that recent clang versions automatically expand this to a |
| 236 |
// full-service routine; takes ~50% longer than PopcountVecsAvx2 on >1kb |
| 237 |
// arrays, but way better than the naive loop |
| 238 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 239 |
tot += PopcountWord(bitvec[widx]); |
| 240 |
} |
| 241 |
return tot; |
| 242 |
} |
| 243 |
#else // !USE_AVX2 |
| 244 |
# ifdef USE_SSE2 |
| 245 |
HEADER_INLINE uintptr_t HsumW(VecW vv) { |
| 246 |
UniVec vu; |
| 247 |
vu.vw = vv; |
| 248 |
return vu.w[0] + vu.w[1]; |
| 249 |
} |
| 250 |
# else |
| 251 |
HEADER_INLINE uintptr_t HsumW(VecW vv) { |
| 252 |
return vv; |
| 253 |
} |
| 254 |
# endif |
| 255 |
|
| 256 |
// assumes vec_ct is a multiple of 3 |
| 257 |
uintptr_t PopcountVecsNoAvx2(const VecW* bit_vvec, uintptr_t vec_ct); |
| 258 |
|
| 259 |
HEADER_INLINE uintptr_t PopcountWords(const uintptr_t* bitvec, uintptr_t word_ct) { |
| 260 |
uintptr_t tot = 0; |
| 261 |
#ifndef USE_SSE42 |
| 262 |
if (word_ct >= (3 * kWordsPerVec)) { |
| 263 |
// This has an asymptotic ~10% advantage in the SSE4.2 case, but word_ct |
| 264 |
// needs to be in the hundreds before the initial comparison even starts to |
| 265 |
// pay for itself. |
| 266 |
assert(IsVecAligned(bitvec)); |
| 267 |
const uintptr_t remainder = word_ct % (3 * kWordsPerVec); |
| 268 |
const uintptr_t main_block_word_ct = word_ct - remainder; |
| 269 |
tot = PopcountVecsNoAvx2(R_CAST(const VecW*, bitvec), main_block_word_ct / kWordsPerVec); |
| 270 |
word_ct = remainder; |
| 271 |
bitvec = &(bitvec[main_block_word_ct]); |
| 272 |
} |
| 273 |
#endif |
| 274 |
for (uintptr_t trailing_word_idx = 0; trailing_word_idx != word_ct; ++trailing_word_idx) { |
| 275 |
tot += PopcountWord(bitvec[trailing_word_idx]); |
| 276 |
} |
| 277 |
return tot; |
| 278 |
} |
| 279 |
#endif // !USE_AVX2 |
| 280 |
|
| 281 |
uintptr_t PopcountWordsIntersect(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, uintptr_t word_ct); |
| 282 |
|
| 283 |
uintptr_t PopcountWordsXor(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, uintptr_t word_ct); |
| 284 |
|
| 285 |
// uintptr_t PopcountWordsIntersect3(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, const uintptr_t* __restrict bitvec3_iter, uintptr_t word_ct); |
| 286 |
|
| 287 |
#ifdef __LP64__ |
| 288 |
HEADER_INLINE uintptr_t PopcountWordsNzbase(const uintptr_t* bitvec, uintptr_t start_idx, uintptr_t end_idx) { |
| 289 |
uintptr_t prefix_ct = 0; |
| 290 |
# ifdef USE_AVX2 |
| 291 |
while (start_idx & 3) { |
| 292 |
if (end_idx == start_idx) { |
| 293 |
return prefix_ct; |
| 294 |
} |
| 295 |
prefix_ct += PopcountWord(bitvec[start_idx++]); |
| 296 |
} |
| 297 |
# else |
| 298 |
if (start_idx & 1) { |
| 299 |
if (end_idx == start_idx) { |
| 300 |
return 0; |
| 301 |
} |
| 302 |
prefix_ct = PopcountWord(bitvec[start_idx++]); |
| 303 |
} |
| 304 |
# endif // USE_AVX2 |
| 305 |
return prefix_ct + PopcountWords(&(bitvec[start_idx]), end_idx - start_idx); |
| 306 |
} |
| 307 |
#else |
| 308 |
HEADER_INLINE uintptr_t PopcountWordsNzbase(const uintptr_t* bitvec, uintptr_t start_idx, uintptr_t end_idx) { |
| 309 |
return PopcountWords(&(bitvec[start_idx]), end_idx - start_idx); |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
// start_idx == end_idx ok |
| 314 |
uintptr_t PopcountBitRange(const uintptr_t* bitvec, uintptr_t start_idx, uintptr_t end_idx); |
| 315 |
|
| 316 |
// requires positive word_ct |
| 317 |
// stay agnostic a bit longer re: word_ct := DIV_UP(entry_ct, kBitsPerWord) |
| 318 |
// vs. word_ct := 1 + (entry_ct / kBitsPerWord) |
| 319 |
// (this is a source of bugs, though; interface should probably be changed to |
| 320 |
// use entry_ct once multiallelic/dosage implementation is done) |
| 321 |
void FillCumulativePopcounts(const uintptr_t* subset_mask, uint32_t word_ct, uint32_t* cumulative_popcounts); |
| 322 |
|
| 323 |
void FillCumulativePopcountsW(const uintptr_t* subset_mask, uintptr_t word_ct, uintptr_t* cumulative_popcounts_w); |
| 324 |
|
| 325 |
// If idx_list is a list of valid unfiltered indexes, this converts them |
| 326 |
// in-place to corresponding filtered indexes. |
| 327 |
void UidxsToIdxs(const uintptr_t* subset_mask, const uint32_t* subset_cumulative_popcounts, const uintptr_t idx_list_len, uint32_t* idx_list); |
| 328 |
|
| 329 |
// These functions do not overread, but may write extra bytes up to the word |
| 330 |
// boundary. |
| 331 |
void Expand1bitTo8(const void* __restrict bytearr, uint32_t input_bit_ct, uint32_t incr, void* __restrict dst); |
| 332 |
|
| 333 |
void Expand1bitTo16(const void* __restrict bytearr, uint32_t input_bit_ct, uint32_t incr, void* __restrict dst); |
| 334 |
|
| 335 |
|
| 336 |
// might rename this to IsSet01 (guaranteeing 0/1 return value), and change |
| 337 |
// IsSet() to bitarr[idx / kBitsPerWord] & (k1LU << (idx % kBitsPerWord)) since |
| 338 |
// I'd expect that to play better with out-of-order execution. but need to |
| 339 |
// benchmark first. |
| 340 |
HEADER_INLINE uintptr_t IsSet(const uintptr_t* bitarr, uintptr_t idx) { |
| 341 |
return (bitarr[idx / kBitsPerWord] >> (idx % kBitsPerWord)) & 1; |
| 342 |
} |
| 343 |
|
| 344 |
HEADER_INLINE uintptr_t IsSetUnaligned(const void* bitarr, uintptr_t idx) { |
| 345 |
const unsigned char* bitarr_uc = S_CAST(const unsigned char*, bitarr); |
| 346 |
return (bitarr_uc[idx / CHAR_BIT] >> (idx % CHAR_BIT)) & 1; |
| 347 |
} |
| 348 |
|
| 349 |
HEADER_INLINE void SetBit(uintptr_t idx, uintptr_t* bitarr) { |
| 350 |
bitarr[idx / kBitsPerWord] |= k1LU << (idx % kBitsPerWord); |
| 351 |
} |
| 352 |
|
| 353 |
HEADER_INLINE void ClearBit(uintptr_t idx, uintptr_t* bitarr) { |
| 354 |
bitarr[idx / kBitsPerWord] &= ~(k1LU << (idx % kBitsPerWord)); |
| 355 |
} |
| 356 |
|
| 357 |
HEADER_INLINE void AssignBit(uintptr_t idx, uintptr_t newbit, uintptr_t* bitarr) { |
| 358 |
const uintptr_t inv_mask = k1LU << (idx % kBitsPerWord); |
| 359 |
uintptr_t* cur_word_ptr = &(bitarr[idx / kBitsPerWord]); |
| 360 |
*cur_word_ptr = ((*cur_word_ptr) & (~inv_mask)) | (inv_mask * newbit); |
| 361 |
} |
| 362 |
|
| 363 |
/* |
| 364 |
HEADER_INLINE uintptr_t BitInnerIter1(uintptr_t uidx_base, uintptr_t* cur_bitsp, uintptr_t* cur_uidx_stopp) { |
| 365 |
const uintptr_t cur_bits = *cur_bitsp; |
| 366 |
const uint32_t uidx_start_lowbits = ctzw(*cur_bitsp); |
| 367 |
// Key idea is to iterate over sub-blocks of set bits in a single word, in |
| 368 |
// essentially the same manner as non-AVX2 CopyBitarrSubset() was doing. |
| 369 |
// This particular expression 'finds' the end of the current sub-block. |
| 370 |
const uintptr_t cur_bits_lfill_p1 = (cur_bits | (cur_bits - 1)) + 1; |
| 371 |
*cur_bitsp = cur_bits & cur_bits_lfill_p1; |
| 372 |
uint32_t uidx_stop_lowbits = kBitsPerWord; |
| 373 |
if (cur_bits_lfill_p1) { |
| 374 |
uidx_stop_lowbits = ctzw(cur_bits_lfill_p1); |
| 375 |
} |
| 376 |
*cur_uidx_stopp = uidx_base + uidx_stop_lowbits; |
| 377 |
return uidx_base + uidx_start_lowbits; |
| 378 |
} |
| 379 |
*/ |
| 380 |
|
| 381 |
HEADER_INLINE uintptr_t BitIter1(const uintptr_t* __restrict bitarr, uintptr_t* __restrict uidx_basep, uintptr_t* __restrict cur_bitsp) { |
| 382 |
uintptr_t cur_bits = *cur_bitsp; |
| 383 |
if (!cur_bits) { |
| 384 |
uintptr_t widx = (*uidx_basep) / kBitsPerWord; |
| 385 |
do { |
| 386 |
cur_bits = bitarr[++widx]; |
| 387 |
} while (!cur_bits); |
| 388 |
*uidx_basep = widx * kBitsPerWord; |
| 389 |
} |
| 390 |
*cur_bitsp = cur_bits & (cur_bits - 1); |
| 391 |
return (*uidx_basep) + ctzw(cur_bits); |
| 392 |
} |
| 393 |
|
| 394 |
// Returns lowbit index instead of the full index. |
| 395 |
HEADER_INLINE uint32_t BitIter1x(const uintptr_t* __restrict bitarr, uintptr_t* __restrict widxp, uintptr_t* __restrict cur_bitsp) { |
| 396 |
uintptr_t cur_bits = *cur_bitsp; |
| 397 |
while (!cur_bits) { |
| 398 |
cur_bits = bitarr[++(*widxp)]; |
| 399 |
} |
| 400 |
*cur_bitsp = cur_bits & (cur_bits - 1); |
| 401 |
return ctzw(cur_bits); |
| 402 |
} |
| 403 |
|
| 404 |
// Returns isolated lowbit. |
| 405 |
HEADER_INLINE uintptr_t BitIter1y(const uintptr_t* __restrict bitarr, uintptr_t* __restrict widxp, uintptr_t* __restrict cur_bitsp) { |
| 406 |
uintptr_t cur_bits = *cur_bitsp; |
| 407 |
while (!cur_bits) { |
| 408 |
cur_bits = bitarr[++(*widxp)]; |
| 409 |
} |
| 410 |
const uintptr_t shifted_bit = cur_bits & (-cur_bits); |
| 411 |
*cur_bitsp = cur_bits ^ shifted_bit; |
| 412 |
return shifted_bit; |
| 413 |
} |
| 414 |
|
| 415 |
HEADER_INLINE void BitIter1Start(const uintptr_t* __restrict bitarr, uintptr_t restart_uidx, uintptr_t* __restrict uidx_basep, uintptr_t* __restrict cur_bitsp) { |
| 416 |
const uintptr_t widx = restart_uidx / kBitsPerWord; |
| 417 |
*cur_bitsp = bitarr[widx] & ((~k0LU) << (restart_uidx % kBitsPerWord)); |
| 418 |
*uidx_basep = widx * kBitsPerWord; |
| 419 |
} |
| 420 |
|
| 421 |
HEADER_INLINE uintptr_t BitIter1NoAdv(const uintptr_t* __restrict bitarr, uintptr_t* __restrict uidx_basep, uintptr_t* __restrict cur_bitsp) { |
| 422 |
uintptr_t cur_bits = *cur_bitsp; |
| 423 |
if (!cur_bits) { |
| 424 |
uintptr_t widx = (*uidx_basep) / kBitsPerWord; |
| 425 |
do { |
| 426 |
cur_bits = bitarr[++widx]; |
| 427 |
} while (!cur_bits); |
| 428 |
*uidx_basep = widx * kBitsPerWord; |
| 429 |
*cur_bitsp = cur_bits; |
| 430 |
} |
| 431 |
return (*uidx_basep) + ctzw(cur_bits); |
| 432 |
} |
| 433 |
|
| 434 |
HEADER_INLINE uintptr_t BitIter0(const uintptr_t* __restrict bitarr, uintptr_t* __restrict uidx_basep, uintptr_t* __restrict cur_inv_bitsp) { |
| 435 |
uintptr_t cur_inv_bits = *cur_inv_bitsp; |
| 436 |
if (!cur_inv_bits) { |
| 437 |
uintptr_t widx = (*uidx_basep) / kBitsPerWord; |
| 438 |
do { |
| 439 |
cur_inv_bits = ~bitarr[++widx]; |
| 440 |
} while (!cur_inv_bits); |
| 441 |
*uidx_basep = widx * kBitsPerWord; |
| 442 |
} |
| 443 |
*cur_inv_bitsp = cur_inv_bits & (cur_inv_bits - 1); |
| 444 |
return (*uidx_basep) + ctzw(cur_inv_bits); |
| 445 |
} |
| 446 |
|
| 447 |
HEADER_INLINE void BitIter0Start(const uintptr_t* __restrict bitarr, uintptr_t restart_uidx, uintptr_t* __restrict uidx_basep, uintptr_t* __restrict cur_inv_bitsp) { |
| 448 |
const uintptr_t widx = restart_uidx / kBitsPerWord; |
| 449 |
*cur_inv_bitsp = (~bitarr[widx]) & ((~k0LU) << (restart_uidx % kBitsPerWord)); |
| 450 |
*uidx_basep = widx * kBitsPerWord; |
| 451 |
} |
| 452 |
|
| 453 |
HEADER_INLINE uintptr_t BitIter0NoAdv(const uintptr_t* __restrict bitarr, uintptr_t* __restrict uidx_basep, uintptr_t* __restrict cur_inv_bitsp) { |
| 454 |
uintptr_t cur_inv_bits = *cur_inv_bitsp; |
| 455 |
if (!cur_inv_bits) { |
| 456 |
uintptr_t widx = (*uidx_basep) / kBitsPerWord; |
| 457 |
do { |
| 458 |
cur_inv_bits = ~bitarr[++widx]; |
| 459 |
} while (!cur_inv_bits); |
| 460 |
*uidx_basep = widx * kBitsPerWord; |
| 461 |
*cur_inv_bitsp = cur_inv_bits; |
| 462 |
} |
| 463 |
return (*uidx_basep) + ctzw(cur_inv_bits); |
| 464 |
} |
| 465 |
|
| 466 |
// todo: test this against extracting a nonmissing bitarr first |
| 467 |
/* |
| 468 |
HEADER_INLINE void NextNonmissingUnsafeCk32(const uintptr_t* __restrict genoarr, uint32_t* __restrict loc_ptr) { |
| 469 |
if (GetNyparrEntry(genoarr, *loc_ptr) == 3) { |
| 470 |
*loc_ptr = NextNonmissingUnsafe(genoarr, *loc_ptr); |
| 471 |
} |
| 472 |
} |
| 473 |
*/ |
| 474 |
|
| 475 |
// Equivalent to PopcountBitRange(subset_mask, 0, raw_idx). |
| 476 |
HEADER_INLINE uint32_t RawToSubsettedPos(const uintptr_t* subset_mask, const uint32_t* subset_cumulative_popcounts, uintptr_t raw_idx) { |
| 477 |
// this should be much better than keeping a uidx_to_idx array! |
| 478 |
// (update: there are more compact indexes, but postpone for now, this is |
| 479 |
// is nice and simple and gets us most of what we need.) |
| 480 |
const uintptr_t raw_widx = raw_idx / kBitsPerWord; |
| 481 |
return subset_cumulative_popcounts[raw_widx] + PopcountWord(bzhi(subset_mask[raw_widx], raw_idx % kBitsPerWord)); |
| 482 |
} |
| 483 |
|
| 484 |
HEADER_INLINE uintptr_t RawToSubsettedPosW(const uintptr_t* subset_mask, const uintptr_t* subset_cumulative_popcounts, uintptr_t raw_idx) { |
| 485 |
const uintptr_t raw_widx = raw_idx / kBitsPerWord; |
| 486 |
return subset_cumulative_popcounts[raw_widx] + PopcountWord(bzhi(subset_mask[raw_widx], raw_idx % kBitsPerWord)); |
| 487 |
} |
| 488 |
|
| 489 |
HEADER_INLINE void ZeroTrailingBits(uintptr_t bit_ct, uintptr_t* bitarr) { |
| 490 |
const uint32_t trail_ct = bit_ct % kBitsPerWord; |
| 491 |
if (trail_ct) { |
| 492 |
bitarr[bit_ct / kBitsPerWord] = bzhi(bitarr[bit_ct / kBitsPerWord], trail_ct); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
#ifdef USE_SSE2 |
| 497 |
HEADER_INLINE void ZeroTrailingWords(uint32_t word_ct, uintptr_t* bitvec) { |
| 498 |
const uint32_t remainder = word_ct % kWordsPerVec; |
| 499 |
if (remainder) { |
| 500 |
ZeroWArr(kWordsPerVec - remainder, &(bitvec[word_ct])); |
| 501 |
} |
| 502 |
} |
| 503 |
#else |
| 504 |
HEADER_INLINE void ZeroTrailingWords(__maybe_unused uint32_t word_ct, __maybe_unused uintptr_t* bitvec) { |
| 505 |
} |
| 506 |
#endif |
| 507 |
|
| 508 |
HEADER_INLINE void CopyBitarr(const uintptr_t* __restrict src, uintptr_t bit_ct, uintptr_t* __restrict dst) { |
| 509 |
memcpy(dst, src, BitCtToWordCt(bit_ct) * kBytesPerWord); |
| 510 |
} |
| 511 |
|
| 512 |
// output_bit_idx_end is practically always subset_size |
| 513 |
// if not, it currently must correspond to PopcountWords(subset_mask, word_ct) |
| 514 |
// for some word_ct |
| 515 |
void CopyBitarrSubset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t output_bit_idx_end, uintptr_t* __restrict output_bitarr); |
| 516 |
|
| 517 |
#ifndef NO_UNALIGNED |
| 518 |
HEADER_INLINE void CopyBitarrSubsetToUnaligned(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t output_bit_idx_end, void* __restrict output_bitarr) { |
| 519 |
CopyBitarrSubset(raw_bitarr, subset_mask, output_bit_idx_end, S_CAST(uintptr_t*, output_bitarr)); |
| 520 |
} |
| 521 |
#else |
| 522 |
void CopyBitarrSubsetToUnaligned(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t output_bit_idx_end, void* __restrict output_bitarr); |
| 523 |
#endif |
| 524 |
|
| 525 |
// expand_size + read_start_bit must be positive. |
| 526 |
void ExpandBytearr(const void* __restrict compact_bitarr, const uintptr_t* __restrict expand_mask, uint32_t word_ct, uint32_t expand_size, uint32_t read_start_bit, uintptr_t* __restrict target); |
| 527 |
|
| 528 |
// equivalent to calling ExpandBytearr() followed by CopyBitarrSubset() |
| 529 |
void ExpandThenSubsetBytearr(const void* __restrict compact_bitarr, const uintptr_t* __restrict expand_mask, const uintptr_t* __restrict subset_mask, uint32_t expand_size, uint32_t subset_size, uint32_t read_start_bit, uintptr_t* __restrict target); |
| 530 |
|
| 531 |
// mid_popcount must be positive |
| 532 |
void ExpandBytearrNested(const void* __restrict compact_bitarr, const uintptr_t* __restrict mid_bitarr, const uintptr_t* __restrict top_expand_mask, uint32_t word_ct, uint32_t mid_popcount, uint32_t mid_start_bit, uintptr_t* __restrict mid_target, uintptr_t* __restrict compact_target); |
| 533 |
|
| 534 |
// mid_popcount must be positive |
| 535 |
// if mid_start_bit == 1, mid_popcount should not include that bit |
| 536 |
void ExpandThenSubsetBytearrNested(const void* __restrict compact_bitarr, const uintptr_t* __restrict mid_bitarr, const uintptr_t* __restrict top_expand_mask, const uintptr_t* __restrict subset_mask, uint32_t subset_size, uint32_t mid_popcount, uint32_t mid_start_bit, uintptr_t* __restrict mid_target, uintptr_t* __restrict compact_target); |
| 537 |
|
| 538 |
// these don't read past the end of bitarr |
| 539 |
uintptr_t PopcountBytes(const void* bitarr, uintptr_t byte_ct); |
| 540 |
uintptr_t PopcountBytesMasked(const void* bitarr, const uintptr_t* mask_arr, uintptr_t byte_ct); |
| 541 |
|
| 542 |
|
| 543 |
// TransposeNypblock(), which is more plink-specific, is in pgenlib_misc |
| 544 |
CONSTI32(kPglBitTransposeBatch, kBitsPerCacheline); |
| 545 |
CONSTI32(kPglBitTransposeWords, kWordsPerCacheline); |
| 546 |
// * Up to 512x512 (CACHELINE64) or 1024x1024 (CACHELINE128) |
| 547 |
// * vecaligned_buf must have size 64k (CACHELINE64) or 256k (CACHELINE128) |
| 548 |
// * write_iter must be allocated up to at least |
| 549 |
// RoundUpPow2(write_batch_size, 2) rows |
| 550 |
// * We use pointers with different types to read from and write to buf0/buf1, |
| 551 |
// so defining the base type as unsigned char* is theoretically necessary to |
| 552 |
// avoid breaking strict-aliasing rules, while the restrict qualifiers should |
| 553 |
// tell the compiler it doesn't need to be paranoid about writes to one of |
| 554 |
// the buffers screwing with reads from the other. |
| 555 |
CONSTI32(kPglBitTransposeBufbytes, (kPglBitTransposeBatch * kPglBitTransposeBatch) / (CHAR_BIT / 2)); |
| 556 |
#ifdef __LP64__ |
| 557 |
void TransposeBitblock64(const uintptr_t* read_iter, uintptr_t read_ul_stride, uintptr_t write_ul_stride, uint32_t read_row_ct, uint32_t write_row_ct, uintptr_t* write_iter, VecW* __restrict buf0, VecW* __restrict buf1); |
| 558 |
|
| 559 |
HEADER_INLINE void TransposeBitblock(const uintptr_t* read_iter, uintptr_t read_ul_stride, uintptr_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* write_iter, VecW* vecaligned_buf) { |
| 560 |
TransposeBitblock64(read_iter, read_ul_stride, write_ul_stride, read_batch_size, write_batch_size, write_iter, vecaligned_buf, &(vecaligned_buf[kPglBitTransposeBufbytes / (2 * kBytesPerVec)])); |
| 561 |
} |
| 562 |
|
| 563 |
#else // !__LP64__ |
| 564 |
void TransposeBitblock32(const uintptr_t* read_iter, uintptr_t read_ul_stride, uintptr_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* write_iter, VecW* __restrict buf0, VecW* __restrict buf1); |
| 565 |
|
| 566 |
// If this ever needs to be called on an input byte array, read_iter could be |
| 567 |
// changed to const void*; in that case, read_ul_stride should be changed to a |
| 568 |
// byte count. |
| 569 |
HEADER_INLINE void TransposeBitblock(const uintptr_t* read_iter, uintptr_t read_ul_stride, uintptr_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* write_iter, VecW* vecaligned_buf) { |
| 570 |
TransposeBitblock32(read_iter, read_ul_stride, write_ul_stride, read_batch_size, write_batch_size, write_iter, vecaligned_buf, &(vecaligned_buf[kPglBitTransposeBufbytes / (2 * kBytesPerVec)])); |
| 571 |
} |
| 572 |
#endif |
| 573 |
|
| 574 |
CONSTI32(kPglBitTransposeBufwords, kPglBitTransposeBufbytes / kBytesPerWord); |
| 575 |
CONSTI32(kPglBitTransposeBufvecs, kPglBitTransposeBufbytes / kBytesPerVec); |
| 576 |
|
| 577 |
CONSTI32(kPglNybbleTransposeBatch, kNybblesPerCacheline); |
| 578 |
CONSTI32(kPglNybbleTransposeWords, kWordsPerCacheline); |
| 579 |
|
| 580 |
CONSTI32(kPglNybbleTransposeBufbytes, (kPglNybbleTransposeBatch * kPglNybbleTransposeBatch) / 2); |
| 581 |
|
| 582 |
// * Up to 128x128 (CACHELINE64) or 256x256 (CACHELINE128) |
| 583 |
// * vecaligned_buf must have size 8k (CACHELINE64) or 32k (CACHELINE128) |
| 584 |
// * Now ok for write_iter to not be padded when write_batch_size odd |
| 585 |
void TransposeNybbleblock(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, VecW* vecaligned_buf); |
| 586 |
|
| 587 |
#ifdef USE_SSE2 |
| 588 |
# ifdef USE_AVX2 |
| 589 |
extern const unsigned char kLeadMask[2 * kBytesPerVec] __attribute__ ((aligned (64))); |
| 590 |
# else |
| 591 |
extern const unsigned char kLeadMask[2 * kBytesPerVec] __attribute__ ((aligned (32))); |
| 592 |
# endif |
| 593 |
#endif |
| 594 |
|
| 595 |
uintptr_t BytesumArr(const void* bytearr, uintptr_t byte_ct); |
| 596 |
|
| 597 |
uintptr_t CountByte(const void* bytearr, unsigned char ucc, uintptr_t byte_ct); |
| 598 |
|
| 599 |
uintptr_t CountU16(const void* u16arr, uint16_t usii, uintptr_t u16_ct); |
| 600 |
|
| 601 |
|
| 602 |
// Applies sample_include to {src_subset, src_vals}. |
| 603 |
uint32_t Copy1bit8Subset(const uintptr_t* __restrict src_subset, const void* __restrict src_vals, const uintptr_t* __restrict sample_include, uint32_t src_subset_size, uint32_t sample_ct, uintptr_t* __restrict dst_subset, void* __restrict dst_vals); |
| 604 |
|
| 605 |
uint32_t Copy1bit16Subset(const uintptr_t* __restrict src_subset, const void* __restrict src_vals, const uintptr_t* __restrict sample_include, uint32_t src_subset_size, uint32_t sample_ct, uintptr_t* __restrict dst_subset, void* __restrict dst_vals); |
| 606 |
|
| 607 |
// more verbose than (val + 3) / 4, but may as well make semantic meaning |
| 608 |
// obvious; any explicit DivUp(val, 4) expressions should have a different |
| 609 |
// meaning |
| 610 |
// (not needed for bitct -> bytect, DivUp(val, CHAR_BIT) is clear enough) |
| 611 |
HEADER_CINLINE uintptr_t NypCtToByteCt(uintptr_t val) { |
| 612 |
return DivUp(val, 4); |
| 613 |
} |
| 614 |
|
| 615 |
HEADER_CINLINE uintptr_t NypCtToVecCt(uintptr_t val) { |
| 616 |
return DivUp(val, kNypsPerVec); |
| 617 |
} |
| 618 |
|
| 619 |
HEADER_CINLINE uintptr_t NypCtToWordCt(uintptr_t val) { |
| 620 |
return DivUp(val, kBitsPerWordD2); |
| 621 |
} |
| 622 |
|
| 623 |
HEADER_CINLINE uintptr_t NypCtToAlignedWordCt(uintptr_t val) { |
| 624 |
return kWordsPerVec * NypCtToVecCt(val); |
| 625 |
} |
| 626 |
|
| 627 |
HEADER_CINLINE uintptr_t NypCtToCachelineCt(uintptr_t val) { |
| 628 |
return DivUp(val, kNypsPerCacheline); |
| 629 |
} |
| 630 |
|
| 631 |
HEADER_INLINE uintptr_t GetNyparrEntry(const uintptr_t* nyparr, uint32_t idx) { |
| 632 |
return (nyparr[idx / kBitsPerWordD2] >> (2 * (idx % kBitsPerWordD2))) & 3; |
| 633 |
} |
| 634 |
|
| 635 |
// todo: check if this optimizes newval=0 out |
| 636 |
HEADER_INLINE void AssignNyparrEntry(uint32_t idx, uintptr_t newval, uintptr_t* nyparr) { |
| 637 |
const uint32_t bit_shift_ct = 2 * (idx % kBitsPerWordD2); |
| 638 |
uintptr_t* wordp = &(nyparr[idx / kBitsPerWordD2]); |
| 639 |
*wordp = ((*wordp) & (~((3 * k1LU) << bit_shift_ct))) | (newval << bit_shift_ct); |
| 640 |
} |
| 641 |
|
| 642 |
HEADER_INLINE void ClearNyparrEntry(uint32_t idx, uintptr_t* nyparr) { |
| 643 |
nyparr[idx / kBitsPerWordD2] &= ~((3 * k1LU) << (idx % kBitsPerWordD2)); |
| 644 |
} |
| 645 |
|
| 646 |
HEADER_CINLINE uintptr_t NybbleCtToVecCt(uintptr_t val) { |
| 647 |
return DivUp(val, kNybblesPerVec); |
| 648 |
} |
| 649 |
|
| 650 |
HEADER_CINLINE uintptr_t NybbleCtToAlignedWordCt(uintptr_t val) { |
| 651 |
return kWordsPerVec * NybbleCtToVecCt(val); |
| 652 |
} |
| 653 |
|
| 654 |
HEADER_CINLINE uintptr_t NybbleCtToCachelineCt(uintptr_t val) { |
| 655 |
return DivUp(val, kNybblesPerCacheline); |
| 656 |
} |
| 657 |
|
| 658 |
HEADER_INLINE void AssignNybblearrEntry(uint32_t idx, uintptr_t newval, uintptr_t* nybblearr) { |
| 659 |
const uint32_t bit_shift_ct = 4 * (idx % kBitsPerWordD4); |
| 660 |
uintptr_t* wordp = &(nybblearr[idx / kBitsPerWordD4]); |
| 661 |
*wordp = ((*wordp) & (~((15 * k1LU) << bit_shift_ct))) | (newval << bit_shift_ct); |
| 662 |
} |
| 663 |
|
| 664 |
// 'Unsafe' because it assumes high bits of every byte are 0 and entry_ct is |
| 665 |
// positive. |
| 666 |
void Reduce8to4bitInplaceUnsafe(uintptr_t entry_ct, uintptr_t* mainvec); |
| 667 |
|
| 668 |
// forward_ct must be positive. Stays put if forward_ct == 1 and current bit |
| 669 |
// is set. |
| 670 |
// In usual 64-bit case, also assumes bitvec is vector aligned. |
| 671 |
uintptr_t FindNth1BitFrom(const uintptr_t* bitvec, uintptr_t cur_pos, uintptr_t forward_ct); |
| 672 |
|
| 673 |
// This function assumes (bit_ct * (thread_ct - 1)) < 2^64. |
| 674 |
// bit_ct must be positive, but can be smaller than thread_ct |
| 675 |
void FillU32SubsetStarts(const uintptr_t* subset, uint32_t thread_ct, uint32_t start, uint64_t bit_ct, uint32_t* starts); |
| 676 |
|
| 677 |
#ifdef __cplusplus |
| 678 |
} // namespace plink2 |
| 679 |
#endif |
| 680 |
|
| 681 |
#endif // __PLINK2_BITS_H__ |