| 1 |
// This library is part of PLINK 2, copyright (C) 2005-2024 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 "plink2_bits.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
namespace plink2 { |
| 22 |
#endif |
| 23 |
|
| 24 |
#if defined(USE_SSE2) && !defined(USE_AVX2) |
| 25 |
void Pack32bTo16bMask(const void* words_vec, uintptr_t ct_32b, void* dest) { |
| 26 |
// This is also competitive in the AVX2 case, but never quite beats the |
| 27 |
// simple loop. (We'd want to enable a similar function for Ryzen, |
| 28 |
// processing one 32-byte vector instead of two 16-byte vectors at a time in |
| 29 |
// the main loop since _mm256_packus_epi16() doesn't do what we want.) |
| 30 |
const VecW m1 = VCONST_W(kMask5555); |
| 31 |
# ifdef USE_SHUFFLE8 |
| 32 |
const VecW swap12 = vecw_setr8( |
| 33 |
0, 1, 4, 5, 2, 3, 6, 7, |
| 34 |
8, 9, 12, 13, 10, 11, 14, 15); |
| 35 |
# else |
| 36 |
const VecW m2 = VCONST_W(kMask3333); |
| 37 |
# endif |
| 38 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 39 |
const VecW m8 = VCONST_W(kMask00FF); |
| 40 |
const VecW* words_valias = S_CAST(const VecW*, words_vec); |
| 41 |
unsigned char* dest_uc = S_CAST(unsigned char*, dest); |
| 42 |
for (uintptr_t vidx = 0; vidx != ct_32b; ++vidx) { |
| 43 |
VecW vec_lo = vecw_loadu(&(words_valias[2 * vidx])) & m1; |
| 44 |
VecW vec_hi = vecw_loadu(&(words_valias[2 * vidx + 1])) & m1; |
| 45 |
# ifdef USE_SHUFFLE8 |
| 46 |
// this right-shift-3 + shuffle shortcut saves two operations. |
| 47 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 3)) & m4; |
| 48 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 3)) & m4; |
| 49 |
vec_lo = vecw_shuffle8(swap12, vec_lo); |
| 50 |
vec_hi = vecw_shuffle8(swap12, vec_hi); |
| 51 |
# else |
| 52 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 1)) & m2; |
| 53 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 1)) & m2; |
| 54 |
vec_lo = (vec_lo | vecw_srli(vec_lo, 2)) & m4; |
| 55 |
vec_hi = (vec_hi | vecw_srli(vec_hi, 2)) & m4; |
| 56 |
# endif |
| 57 |
vec_lo = vec_lo | vecw_srli(vec_lo, 4); |
| 58 |
vec_hi = vec_hi | vecw_srli(vec_hi, 4); |
| 59 |
const VecW vec_packed = vecw_gather_even(vec_lo, vec_hi, m8); |
| 60 |
vecw_storeu(&(dest_uc[vidx * 16]), vec_packed); |
| 61 |
} |
| 62 |
} |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef __x86_64__ |
| 66 |
VecW vecw_slli_variable_ct(VecW vv, uint32_t ct) { |
| 67 |
return vecw_slli(vv, ct); |
| 68 |
} |
| 69 |
#else |
| 70 |
// Using a lookup table because NEON bit shift functions can only be called |
| 71 |
// with compile-time constants |
| 72 |
// https://eigen.tuxfamily.org/bz/show_bug.cgi?id=1631 |
| 73 |
// https://github.com/VectorCamp/vectorscan/issues/21 |
| 74 |
VecW vecw_slli_variable_ct(VecW vv, uint32_t ct) { |
| 75 |
switch(ct) { |
| 76 |
default: return vv; |
| 77 |
case 1: return vecw_slli(vv, 1); |
| 78 |
case 2: return vecw_slli(vv, 2); |
| 79 |
case 3: return vecw_slli(vv, 3); |
| 80 |
case 4: return vecw_slli(vv, 4); |
| 81 |
case 5: return vecw_slli(vv, 5); |
| 82 |
case 6: return vecw_slli(vv, 6); |
| 83 |
case 7: return vecw_slli(vv, 7); |
| 84 |
} |
| 85 |
} |
| 86 |
#endif |
| 87 |
|
| 88 |
void SetAllBits(uintptr_t ct, uintptr_t* bitarr) { |
| 89 |
// leaves bits beyond the end unset |
| 90 |
// ok for ct == 0 |
| 91 |
uintptr_t quotient = ct / kBitsPerWord; |
| 92 |
uintptr_t remainder = ct % kBitsPerWord; |
| 93 |
SetAllWArr(quotient, bitarr); |
| 94 |
if (remainder) { |
| 95 |
bitarr[quotient] = (k1LU << remainder) - k1LU; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void FillBitsNz(uintptr_t start_idx, uintptr_t end_idx, uintptr_t* bitarr) { |
| 100 |
assert(end_idx > start_idx); |
| 101 |
uintptr_t maj_start = start_idx / kBitsPerWord; |
| 102 |
uintptr_t maj_end = end_idx / kBitsPerWord; |
| 103 |
uintptr_t minor; |
| 104 |
if (maj_start == maj_end) { |
| 105 |
bitarr[maj_start] |= (k1LU << (end_idx % kBitsPerWord)) - (k1LU << (start_idx % kBitsPerWord)); |
| 106 |
} else { |
| 107 |
bitarr[maj_start] |= ~((k1LU << (start_idx % kBitsPerWord)) - k1LU); |
| 108 |
SetAllWArr(maj_end - maj_start - 1, &(bitarr[maj_start + 1])); |
| 109 |
minor = end_idx % kBitsPerWord; |
| 110 |
if (minor) { |
| 111 |
bitarr[maj_end] |= (k1LU << minor) - k1LU; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void ClearBitsNz(uintptr_t start_idx, uintptr_t end_idx, uintptr_t* bitarr) { |
| 117 |
assert(end_idx > start_idx); |
| 118 |
uintptr_t maj_start = start_idx / kBitsPerWord; |
| 119 |
uintptr_t maj_end = end_idx / kBitsPerWord; |
| 120 |
uintptr_t minor; |
| 121 |
if (maj_start == maj_end) { |
| 122 |
bitarr[maj_start] &= ~((k1LU << (end_idx % kBitsPerWord)) - (k1LU << (start_idx % kBitsPerWord))); |
| 123 |
} else { |
| 124 |
bitarr[maj_start] = bzhi(bitarr[maj_start], start_idx % kBitsPerWord); |
| 125 |
ZeroWArr(maj_end - maj_start - 1, &(bitarr[maj_start + 1])); |
| 126 |
minor = end_idx % kBitsPerWord; |
| 127 |
if (minor) { |
| 128 |
bitarr[maj_end] &= ~((k1LU << minor) - k1LU); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void BitvecAnd(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec) { |
| 134 |
// main_bitvec := main_bitvec AND arg_bitvec |
| 135 |
#ifdef USE_SSE2 |
| 136 |
VecW* main_bitvvec_iter = R_CAST(VecW*, main_bitvec); |
| 137 |
const VecW* arg_bitvvec_iter = R_CAST(const VecW*, arg_bitvec); |
| 138 |
const uintptr_t full_vec_ct = word_ct / kWordsPerVec; |
| 139 |
// ok, retested this explicit unroll (Jun 2018) and it's still noticeably |
| 140 |
// faster for small cases than the simple loop. sigh. |
| 141 |
if (full_vec_ct & 1) { |
| 142 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 143 |
} |
| 144 |
if (full_vec_ct & 2) { |
| 145 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 146 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 147 |
} |
| 148 |
for (uintptr_t ulii = 3; ulii < full_vec_ct; ulii += 4) { |
| 149 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 150 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 151 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 152 |
*main_bitvvec_iter++ &= *arg_bitvvec_iter++; |
| 153 |
} |
| 154 |
# ifdef USE_AVX2 |
| 155 |
if (word_ct & 2) { |
| 156 |
const uintptr_t base_idx = full_vec_ct * kWordsPerVec; |
| 157 |
main_bitvec[base_idx] &= arg_bitvec[base_idx]; |
| 158 |
main_bitvec[base_idx + 1] &= arg_bitvec[base_idx + 1]; |
| 159 |
} |
| 160 |
# endif |
| 161 |
if (word_ct & 1) { |
| 162 |
main_bitvec[word_ct - 1] &= arg_bitvec[word_ct - 1]; |
| 163 |
} |
| 164 |
#else |
| 165 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 166 |
main_bitvec[widx] &= arg_bitvec[widx]; |
| 167 |
} |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
void BitvecInvmask(const uintptr_t* __restrict exclude_bitvec, uintptr_t word_ct, uintptr_t* __restrict main_bitvec) { |
| 172 |
// main_bitvec := main_bitvec ANDNOT exclude_bitvec |
| 173 |
// note that this is the reverse of the _mm_andnot() operand order |
| 174 |
#ifdef USE_SSE2 |
| 175 |
VecW* main_bitvvec_iter = R_CAST(VecW*, main_bitvec); |
| 176 |
const VecW* exclude_bitvvec_iter = R_CAST(const VecW*, exclude_bitvec); |
| 177 |
const uintptr_t full_vec_ct = word_ct / kWordsPerVec; |
| 178 |
if (full_vec_ct & 1) { |
| 179 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 180 |
++main_bitvvec_iter; |
| 181 |
} |
| 182 |
if (full_vec_ct & 2) { |
| 183 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 184 |
++main_bitvvec_iter; |
| 185 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 186 |
++main_bitvvec_iter; |
| 187 |
} |
| 188 |
for (uintptr_t ulii = 3; ulii < full_vec_ct; ulii += 4) { |
| 189 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 190 |
++main_bitvvec_iter; |
| 191 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 192 |
++main_bitvvec_iter; |
| 193 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 194 |
++main_bitvvec_iter; |
| 195 |
*main_bitvvec_iter = vecw_and_notfirst(*exclude_bitvvec_iter++, *main_bitvvec_iter); |
| 196 |
++main_bitvvec_iter; |
| 197 |
} |
| 198 |
# ifdef USE_AVX2 |
| 199 |
if (word_ct & 2) { |
| 200 |
const uintptr_t base_idx = full_vec_ct * kWordsPerVec; |
| 201 |
main_bitvec[base_idx] &= ~exclude_bitvec[base_idx]; |
| 202 |
main_bitvec[base_idx + 1] &= ~exclude_bitvec[base_idx + 1]; |
| 203 |
} |
| 204 |
# endif |
| 205 |
if (word_ct & 1) { |
| 206 |
main_bitvec[word_ct - 1] &= ~exclude_bitvec[word_ct - 1]; |
| 207 |
} |
| 208 |
#else |
| 209 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 210 |
main_bitvec[widx] &= ~exclude_bitvec[widx]; |
| 211 |
} |
| 212 |
#endif |
| 213 |
} |
| 214 |
|
| 215 |
void BitvecOr(const uintptr_t* __restrict arg_bitvec, uintptr_t word_ct, uintptr_t* main_bitvec) { |
| 216 |
// main_bitvec := main_bitvec OR arg_bitvec |
| 217 |
#ifdef USE_SSE2 |
| 218 |
VecW* main_bitvvec_iter = R_CAST(VecW*, main_bitvec); |
| 219 |
const VecW* arg_bitvvec_iter = R_CAST(const VecW*, arg_bitvec); |
| 220 |
const uintptr_t full_vec_ct = word_ct / kWordsPerVec; |
| 221 |
if (full_vec_ct & 1) { |
| 222 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 223 |
} |
| 224 |
if (full_vec_ct & 2) { |
| 225 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 226 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 227 |
} |
| 228 |
for (uintptr_t ulii = 3; ulii < full_vec_ct; ulii += 4) { |
| 229 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 230 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 231 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 232 |
*main_bitvvec_iter++ |= (*arg_bitvvec_iter++); |
| 233 |
} |
| 234 |
# ifdef USE_AVX2 |
| 235 |
if (word_ct & 2) { |
| 236 |
const uintptr_t base_idx = full_vec_ct * kWordsPerVec; |
| 237 |
main_bitvec[base_idx] |= arg_bitvec[base_idx]; |
| 238 |
main_bitvec[base_idx + 1] |= arg_bitvec[base_idx + 1]; |
| 239 |
} |
| 240 |
# endif |
| 241 |
if (word_ct & 1) { |
| 242 |
main_bitvec[word_ct - 1] |= arg_bitvec[word_ct - 1]; |
| 243 |
} |
| 244 |
#else |
| 245 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 246 |
main_bitvec[widx] |= arg_bitvec[widx]; |
| 247 |
} |
| 248 |
#endif |
| 249 |
} |
| 250 |
|
| 251 |
void BitvecInvert(uintptr_t word_ct, uintptr_t* main_bitvec) { |
| 252 |
#ifdef USE_SSE2 |
| 253 |
VecW* main_bitvvec_iter = R_CAST(VecW*, main_bitvec); |
| 254 |
const uintptr_t full_vec_ct = word_ct / kWordsPerVec; |
| 255 |
const VecW all1 = VCONST_W(~k0LU); |
| 256 |
if (full_vec_ct & 1) { |
| 257 |
*main_bitvvec_iter++ ^= all1; |
| 258 |
} |
| 259 |
if (full_vec_ct & 2) { |
| 260 |
*main_bitvvec_iter++ ^= all1; |
| 261 |
*main_bitvvec_iter++ ^= all1; |
| 262 |
} |
| 263 |
for (uintptr_t ulii = 3; ulii < full_vec_ct; ulii += 4) { |
| 264 |
*main_bitvvec_iter++ ^= all1; |
| 265 |
*main_bitvvec_iter++ ^= all1; |
| 266 |
*main_bitvvec_iter++ ^= all1; |
| 267 |
*main_bitvvec_iter++ ^= all1; |
| 268 |
} |
| 269 |
# ifdef USE_AVX2 |
| 270 |
if (word_ct & 2) { |
| 271 |
const uintptr_t base_idx = full_vec_ct * kWordsPerVec; |
| 272 |
main_bitvec[base_idx] ^= ~k0LU; |
| 273 |
main_bitvec[base_idx + 1] ^= ~k0LU; |
| 274 |
} |
| 275 |
# endif |
| 276 |
if (word_ct & 1) { |
| 277 |
main_bitvec[word_ct - 1] ^= ~k0LU; |
| 278 |
} |
| 279 |
#else |
| 280 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 281 |
main_bitvec[widx] ^= ~k0LU; |
| 282 |
} |
| 283 |
#endif |
| 284 |
} |
| 285 |
|
| 286 |
void BitvecXorCopy(const uintptr_t* __restrict source1_bitvec, const uintptr_t* __restrict source2_bitvec, uintptr_t word_ct, uintptr_t* target_bitvec) { |
| 287 |
#ifdef USE_SSE2 |
| 288 |
VecW* target_bitvvec = R_CAST(VecW*, target_bitvec); |
| 289 |
const VecW* source1_bitvvec = R_CAST(const VecW*, source1_bitvec); |
| 290 |
const VecW* source2_bitvvec = R_CAST(const VecW*, source2_bitvec); |
| 291 |
const uintptr_t full_vec_ct = word_ct / kWordsPerVec; |
| 292 |
for (uintptr_t ulii = 0; ulii != full_vec_ct; ++ulii) { |
| 293 |
target_bitvvec[ulii] = source1_bitvvec[ulii] ^ source2_bitvvec[ulii]; |
| 294 |
} |
| 295 |
# ifdef USE_AVX2 |
| 296 |
if (word_ct & 2) { |
| 297 |
const uintptr_t base_idx = full_vec_ct * kWordsPerVec; |
| 298 |
target_bitvec[base_idx] = source1_bitvec[base_idx] ^ source2_bitvec[base_idx]; |
| 299 |
target_bitvec[base_idx + 1] = source1_bitvec[base_idx + 1] ^ source2_bitvec[base_idx + 1]; |
| 300 |
} |
| 301 |
# endif |
| 302 |
if (word_ct & 1) { |
| 303 |
target_bitvec[word_ct - 1] = source1_bitvec[word_ct - 1] ^ source2_bitvec[word_ct - 1]; |
| 304 |
} |
| 305 |
#else |
| 306 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 307 |
target_bitvec[widx] = source1_bitvec[widx] ^ source2_bitvec[widx]; |
| 308 |
} |
| 309 |
#endif |
| 310 |
} |
| 311 |
|
| 312 |
void BitvecInvertCopy(const uintptr_t* __restrict source_bitvec, uintptr_t word_ct, uintptr_t* __restrict target_bitvec) { |
| 313 |
#ifdef USE_SSE2 |
| 314 |
const VecW* source_bitvvec_iter = R_CAST(const VecW*, source_bitvec); |
| 315 |
VecW* target_bitvvec_iter = R_CAST(VecW*, target_bitvec); |
| 316 |
const uintptr_t full_vec_ct = word_ct / kWordsPerVec; |
| 317 |
const VecW all1 = VCONST_W(~k0LU); |
| 318 |
// As of Apple clang 11, this manual unroll is no longer relevant. todo: |
| 319 |
// check Linux performance, and remove all of these unrolls if perf is good |
| 320 |
// enough without them. |
| 321 |
if (full_vec_ct & 1) { |
| 322 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 323 |
} |
| 324 |
if (full_vec_ct & 2) { |
| 325 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 326 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 327 |
} |
| 328 |
for (uintptr_t ulii = 3; ulii < full_vec_ct; ulii += 4) { |
| 329 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 330 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 331 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 332 |
*target_bitvvec_iter++ = (*source_bitvvec_iter++) ^ all1; |
| 333 |
} |
| 334 |
# ifdef USE_AVX2 |
| 335 |
if (word_ct & 2) { |
| 336 |
const uintptr_t base_idx = full_vec_ct * kWordsPerVec; |
| 337 |
target_bitvec[base_idx] = ~source_bitvec[base_idx]; |
| 338 |
target_bitvec[base_idx + 1] = ~source_bitvec[base_idx + 1]; |
| 339 |
} |
| 340 |
# endif |
| 341 |
if (word_ct & 1) { |
| 342 |
target_bitvec[word_ct - 1] = ~source_bitvec[word_ct - 1]; |
| 343 |
} |
| 344 |
#else |
| 345 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 346 |
target_bitvec[widx] = ~source_bitvec[widx]; |
| 347 |
} |
| 348 |
#endif |
| 349 |
} |
| 350 |
|
| 351 |
uintptr_t AdvTo1Bit(const uintptr_t* bitarr, uintptr_t loc) { |
| 352 |
const uintptr_t* bitarr_iter = &(bitarr[loc / kBitsPerWord]); |
| 353 |
uintptr_t ulii = (*bitarr_iter) >> (loc % kBitsPerWord); |
| 354 |
if (ulii) { |
| 355 |
return loc + ctzw(ulii); |
| 356 |
} |
| 357 |
do { |
| 358 |
ulii = *(++bitarr_iter); |
| 359 |
} while (!ulii); |
| 360 |
return S_CAST(uintptr_t, bitarr_iter - bitarr) * kBitsPerWord + ctzw(ulii); |
| 361 |
} |
| 362 |
|
| 363 |
uintptr_t AdvTo0Bit(const uintptr_t* bitarr, uintptr_t loc) { |
| 364 |
const uintptr_t* bitarr_iter = &(bitarr[loc / kBitsPerWord]); |
| 365 |
uintptr_t ulii = (~(*bitarr_iter)) >> (loc % kBitsPerWord); |
| 366 |
if (ulii) { |
| 367 |
return loc + ctzw(ulii); |
| 368 |
} |
| 369 |
do { |
| 370 |
ulii = *(++bitarr_iter); |
| 371 |
} while (ulii == ~k0LU); |
| 372 |
return S_CAST(uintptr_t, bitarr_iter - bitarr) * kBitsPerWord + ctzw(~ulii); |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
uintptr_t NextNonmissingUnsafe(const uintptr_t* genoarr, uintptr_t loc) { |
| 377 |
const uintptr_t* genoarr_iter = &(genoarr[loc / kBitsPerWordD2]); |
| 378 |
uintptr_t ulii = (~(*genoarr_iter)) >> (2 * (loc % kBitsPerWordD2)); |
| 379 |
if (ulii) { |
| 380 |
return loc + (ctzw(ulii) / 2); |
| 381 |
} |
| 382 |
do { |
| 383 |
ulii = *(++genoarr_iter); |
| 384 |
} while (ulii == ~k0LU); |
| 385 |
return S_CAST(uintptr_t, genoarr_iter - genoarr) * kBitsPerWordD2 + (ctzw(~ulii) / 2); |
| 386 |
} |
| 387 |
*/ |
| 388 |
|
| 389 |
uint32_t AdvBoundedTo1Bit(const uintptr_t* bitarr, uint32_t loc, uint32_t ceil) { |
| 390 |
// safe version. |
| 391 |
const uintptr_t* bitarr_iter = &(bitarr[loc / kBitsPerWord]); |
| 392 |
uintptr_t ulii = (*bitarr_iter) >> (loc % kBitsPerWord); |
| 393 |
if (ulii) { |
| 394 |
const uint32_t rval = loc + ctzw(ulii); |
| 395 |
return MINV(rval, ceil); |
| 396 |
} |
| 397 |
const uintptr_t* bitarr_last = &(bitarr[(ceil - 1) / kBitsPerWord]); |
| 398 |
do { |
| 399 |
if (bitarr_iter >= bitarr_last) { |
| 400 |
return ceil; |
| 401 |
} |
| 402 |
ulii = *(++bitarr_iter); |
| 403 |
} while (!ulii); |
| 404 |
const uint32_t rval = S_CAST(uintptr_t, bitarr_iter - bitarr) * kBitsPerWord + ctzw(ulii); |
| 405 |
return MINV(rval, ceil); |
| 406 |
} |
| 407 |
|
| 408 |
uintptr_t AdvBoundedTo0Bit(const uintptr_t* bitarr, uintptr_t loc, uintptr_t ceil) { |
| 409 |
assert(ceil >= 1); |
| 410 |
const uintptr_t* bitarr_ptr = &(bitarr[loc / kBitsPerWord]); |
| 411 |
uintptr_t ulii = (~(*bitarr_ptr)) >> (loc % kBitsPerWord); |
| 412 |
if (ulii) { |
| 413 |
loc += ctzw(ulii); |
| 414 |
return MINV(loc, ceil); |
| 415 |
} |
| 416 |
const uintptr_t* bitarr_last = &(bitarr[(ceil - 1) / kBitsPerWord]); |
| 417 |
do { |
| 418 |
if (bitarr_ptr >= bitarr_last) { |
| 419 |
return ceil; |
| 420 |
} |
| 421 |
ulii = *(++bitarr_ptr); |
| 422 |
} while (ulii == ~k0LU); |
| 423 |
loc = S_CAST(uintptr_t, bitarr_ptr - bitarr) * kBitsPerWord + ctzw(~ulii); |
| 424 |
return MINV(loc, ceil); |
| 425 |
} |
| 426 |
|
| 427 |
uintptr_t FindLast1BitBefore(const uintptr_t* bitarr, uintptr_t loc) { |
| 428 |
// unlike the next_{un}set family, this always returns a STRICTLY earlier |
| 429 |
// position |
| 430 |
const uintptr_t* bitarr_iter = &(bitarr[loc / kBitsPerWord]); |
| 431 |
const uint32_t remainder = loc % kBitsPerWord; |
| 432 |
uintptr_t ulii; |
| 433 |
if (remainder) { |
| 434 |
ulii = bzhi(*bitarr_iter, remainder); |
| 435 |
if (ulii) { |
| 436 |
return loc - remainder + bsrw(ulii); |
| 437 |
} |
| 438 |
} |
| 439 |
do { |
| 440 |
ulii = *(--bitarr_iter); |
| 441 |
} while (!ulii); |
| 442 |
return S_CAST(uintptr_t, bitarr_iter - bitarr) * kBitsPerWord + bsrw(ulii); |
| 443 |
} |
| 444 |
|
| 445 |
#ifndef NO_UNALIGNED |
| 446 |
uint32_t AllBytesAreX(const unsigned char* bytes, unsigned char match, uintptr_t byte_ct) { |
| 447 |
if (byte_ct < kBytesPerWord) { |
| 448 |
for (uint32_t uii = 0; uii != byte_ct; ++uii) { |
| 449 |
if (bytes[uii] != match) { |
| 450 |
return 0; |
| 451 |
} |
| 452 |
} |
| 453 |
return 1; |
| 454 |
} |
| 455 |
const uintptr_t* bytes_alias = R_CAST(const uintptr_t*, bytes); |
| 456 |
const uintptr_t word_match = S_CAST(uintptr_t, match) * kMask0101; |
| 457 |
uintptr_t word_ct_m1 = (byte_ct - 1) / kBytesPerWord; |
| 458 |
// todo: try movemask in AVX2 case |
| 459 |
for (uintptr_t widx = 0; widx != word_ct_m1; ++widx) { |
| 460 |
if (bytes_alias[widx] != word_match) { |
| 461 |
return 0; |
| 462 |
} |
| 463 |
} |
| 464 |
const uintptr_t last_word = *R_CAST(const uintptr_t*, &(bytes[byte_ct - kBytesPerWord])); |
| 465 |
if (last_word != word_match) { |
| 466 |
return 0; |
| 467 |
} |
| 468 |
return 1; |
| 469 |
} |
| 470 |
#else // NO_UNALIGNED |
| 471 |
uint32_t AllBytesAreX(const unsigned char* bytes, unsigned char match, uintptr_t byte_ct) { |
| 472 |
if (byte_ct < 2 * kBytesPerWord - 1) { |
| 473 |
// use simple loop instead of main algorithm unless byte_ct guarantees |
| 474 |
// fullword_ct >= 1 |
| 475 |
for (uint32_t uii = 0; uii != byte_ct; ++uii) { |
| 476 |
if (bytes[uii] != match) { |
| 477 |
return 0; |
| 478 |
} |
| 479 |
} |
| 480 |
return 1; |
| 481 |
} |
| 482 |
// todo: try movemask in AVX2 case |
| 483 |
const uintptr_t* bytes_alias; |
| 484 |
const uint32_t lead_byte_ct = AlignKToW(bytes, &bytes_alias); |
| 485 |
for (uint32_t uii = 0; uii != lead_byte_ct; ++uii) { |
| 486 |
if (bytes[uii] != match) { |
| 487 |
return 0; |
| 488 |
} |
| 489 |
} |
| 490 |
bytes = &(bytes[lead_byte_ct]); |
| 491 |
byte_ct -= lead_byte_ct; |
| 492 |
const uintptr_t fullword_ct = byte_ct / kBytesPerWord; |
| 493 |
const uintptr_t word_match = S_CAST(uintptr_t, match) * kMask0101; |
| 494 |
for (uintptr_t widx = 0; widx != fullword_ct; ++widx) { |
| 495 |
if (bytes_alias[widx] != word_match) { |
| 496 |
return 0; |
| 497 |
} |
| 498 |
} |
| 499 |
for (uintptr_t ulii = fullword_ct * kBytesPerWord; ulii != byte_ct; ++ulii) { |
| 500 |
if (bytes[ulii] != match) { |
| 501 |
return 0; |
| 502 |
} |
| 503 |
} |
| 504 |
return 1; |
| 505 |
} |
| 506 |
#endif |
| 507 |
|
| 508 |
#ifdef USE_AVX2 |
| 509 |
// void CopyBitarrSubsetEx(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t bit_idx_start, uint32_t output_bit_idx_end, uintptr_t* __restrict output_bitarr) { |
| 510 |
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) { |
| 511 |
const uint32_t output_bit_idx_end_lowbits = output_bit_idx_end % kBitsPerWord; |
| 512 |
uintptr_t* output_bitarr_iter = output_bitarr; |
| 513 |
uintptr_t* output_bitarr_last = &(output_bitarr[output_bit_idx_end / kBitsPerWord]); |
| 514 |
uintptr_t cur_output_word = 0; |
| 515 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 516 |
uint32_t write_idx_lowbits = 0; |
| 517 |
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != output_bit_idx_end_lowbits)) { |
| 518 |
uintptr_t cur_mask_word; |
| 519 |
// sparse subset_mask optimization |
| 520 |
// guaranteed to terminate since there's at least one more set bit |
| 521 |
do { |
| 522 |
cur_mask_word = subset_mask[++read_widx]; |
| 523 |
} while (!cur_mask_word); |
| 524 |
uintptr_t extracted_bits = raw_bitarr[read_widx]; |
| 525 |
uint32_t set_bit_ct = kBitsPerWord; |
| 526 |
if (cur_mask_word != ~k0LU) { |
| 527 |
extracted_bits = _pext_u64(extracted_bits, cur_mask_word); |
| 528 |
set_bit_ct = PopcountWord(cur_mask_word); |
| 529 |
} |
| 530 |
cur_output_word |= extracted_bits << write_idx_lowbits; |
| 531 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct; |
| 532 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 533 |
*output_bitarr_iter++ = cur_output_word; |
| 534 |
// ...and these are the bits that fell off |
| 535 |
// bugfix: unsafe to right-shift 64 |
| 536 |
if (write_idx_lowbits) { |
| 537 |
cur_output_word = extracted_bits >> (kBitsPerWord - write_idx_lowbits); |
| 538 |
} else { |
| 539 |
cur_output_word = 0; |
| 540 |
} |
| 541 |
} |
| 542 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 543 |
} |
| 544 |
if (write_idx_lowbits) { |
| 545 |
*output_bitarr_iter = cur_output_word; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
# ifdef NO_UNALIGNED |
| 550 |
void CopyBitarrSubsetToUnaligned(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t output_bit_idx_end, void* __restrict output_bitarr) { |
| 551 |
const uint32_t output_bit_idx_end_lowbits = output_bit_idx_end % kBitsPerWord; |
| 552 |
unsigned char* output_bitarr_iter = S_CAST(unsigned char*, output_bitarr); |
| 553 |
unsigned char* output_bitarr_last = &(output_bitarr_iter[(output_bit_idx_end / kBitsPerWord) * kBytesPerWord]); |
| 554 |
uintptr_t cur_output_word = 0; |
| 555 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 556 |
uint32_t write_idx_lowbits = 0; |
| 557 |
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != output_bit_idx_end_lowbits)) { |
| 558 |
uintptr_t cur_mask_word; |
| 559 |
// sparse subset_mask optimization |
| 560 |
// guaranteed to terminate since there's at least one more set bit |
| 561 |
do { |
| 562 |
cur_mask_word = subset_mask[++read_widx]; |
| 563 |
} while (!cur_mask_word); |
| 564 |
uintptr_t extracted_bits = raw_bitarr[read_widx]; |
| 565 |
uint32_t set_bit_ct = kBitsPerWord; |
| 566 |
if (cur_mask_word != ~k0LU) { |
| 567 |
extracted_bits = _pext_u64(extracted_bits, cur_mask_word); |
| 568 |
set_bit_ct = PopcountWord(cur_mask_word); |
| 569 |
} |
| 570 |
cur_output_word |= extracted_bits << write_idx_lowbits; |
| 571 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct; |
| 572 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 573 |
AppendW(cur_output_word, &output_bitarr_iter); |
| 574 |
// ...and these are the bits that fell off |
| 575 |
// bugfix: unsafe to right-shift 64 |
| 576 |
if (write_idx_lowbits) { |
| 577 |
cur_output_word = extracted_bits >> (kBitsPerWord - write_idx_lowbits); |
| 578 |
} else { |
| 579 |
cur_output_word = 0; |
| 580 |
} |
| 581 |
} |
| 582 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 583 |
} |
| 584 |
if (write_idx_lowbits) { |
| 585 |
CopyToUnalignedW(output_bitarr_iter, &cur_output_word); |
| 586 |
} |
| 587 |
} |
| 588 |
# endif |
| 589 |
|
| 590 |
uintptr_t PopcountVecsAvx2(const VecW* bit_vvec, uintptr_t vec_ct) { |
| 591 |
// See popcnt_avx2() in libpopcnt. |
| 592 |
VecW cnt = vecw_setzero(); |
| 593 |
VecW ones = vecw_setzero(); |
| 594 |
VecW twos = vecw_setzero(); |
| 595 |
VecW fours = vecw_setzero(); |
| 596 |
VecW eights = vecw_setzero(); |
| 597 |
VecW prev_sad_result = vecw_setzero(); |
| 598 |
const uintptr_t vec_ct_a16 = RoundDownPow2(vec_ct, 16); |
| 599 |
for (uintptr_t vec_idx = 0; vec_idx != vec_ct_a16; vec_idx += 16) { |
| 600 |
VecW twos_a = Csa256(bit_vvec[vec_idx + 0], bit_vvec[vec_idx + 1], &ones); |
| 601 |
VecW twos_b = Csa256(bit_vvec[vec_idx + 2], bit_vvec[vec_idx + 3], &ones); |
| 602 |
VecW fours_a = Csa256(twos_a, twos_b, &twos); |
| 603 |
|
| 604 |
twos_a = Csa256(bit_vvec[vec_idx + 4], bit_vvec[vec_idx + 5], &ones); |
| 605 |
twos_b = Csa256(bit_vvec[vec_idx + 6], bit_vvec[vec_idx + 7], &ones); |
| 606 |
VecW fours_b = Csa256(twos_a, twos_b, &twos); |
| 607 |
const VecW eights_a = Csa256(fours_a, fours_b, &fours); |
| 608 |
|
| 609 |
twos_a = Csa256(bit_vvec[vec_idx + 8], bit_vvec[vec_idx + 9], &ones); |
| 610 |
twos_b = Csa256(bit_vvec[vec_idx + 10], bit_vvec[vec_idx + 11], &ones); |
| 611 |
fours_a = Csa256(twos_a, twos_b, &twos); |
| 612 |
|
| 613 |
twos_a = Csa256(bit_vvec[vec_idx + 12], bit_vvec[vec_idx + 13], &ones); |
| 614 |
twos_b = Csa256(bit_vvec[vec_idx + 14], bit_vvec[vec_idx + 15], &ones); |
| 615 |
fours_b = Csa256(twos_a, twos_b, &twos); |
| 616 |
const VecW eights_b = Csa256(fours_a, fours_b, &fours); |
| 617 |
const VecW sixteens = Csa256(eights_a, eights_b, &eights); |
| 618 |
cnt = cnt + prev_sad_result; |
| 619 |
// work around high SAD latency |
| 620 |
prev_sad_result = PopcountVecAvx2(sixteens); |
| 621 |
} |
| 622 |
bit_vvec = &(bit_vvec[vec_ct_a16]); |
| 623 |
const uintptr_t remainder = vec_ct % 16; |
| 624 |
cnt = cnt + prev_sad_result; |
| 625 |
if (remainder < 12) { |
| 626 |
cnt = vecw_slli(cnt, 4); |
| 627 |
if (remainder) { |
| 628 |
VecW popcnt1_acc = vecw_setzero(); |
| 629 |
VecW popcnt2_acc = vecw_setzero(); |
| 630 |
const VecW lookup1 = vecw_setr8(4, 5, 5, 6, 5, 6, 6, 7, |
| 631 |
5, 6, 6, 7, 6, 7, 7, 8); |
| 632 |
const VecW lookup2 = vecw_setr8(4, 3, 3, 2, 3, 2, 2, 1, |
| 633 |
3, 2, 2, 1, 2, 1, 1, 0); |
| 634 |
|
| 635 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 636 |
for (uintptr_t vec_idx = 0; vec_idx != remainder; ++vec_idx) { |
| 637 |
const VecW vv = bit_vvec[vec_idx]; |
| 638 |
const VecW lo = vv & m4; |
| 639 |
const VecW hi = vecw_srli(vv, 4) & m4; |
| 640 |
popcnt1_acc = popcnt1_acc + vecw_shuffle8(lookup1, lo); |
| 641 |
popcnt2_acc = popcnt2_acc + vecw_shuffle8(lookup2, hi); |
| 642 |
} |
| 643 |
cnt = cnt + vecw_sad(popcnt1_acc, popcnt2_acc); |
| 644 |
} |
| 645 |
} else { |
| 646 |
VecW twos_a = Csa256(bit_vvec[0], bit_vvec[1], &ones); |
| 647 |
VecW twos_b = Csa256(bit_vvec[2], bit_vvec[3], &ones); |
| 648 |
VecW fours_a = Csa256(twos_a, twos_b, &twos); |
| 649 |
twos_a = Csa256(bit_vvec[4], bit_vvec[5], &ones); |
| 650 |
twos_b = Csa256(bit_vvec[6], bit_vvec[7], &ones); |
| 651 |
VecW fours_b = Csa256(twos_a, twos_b, &twos); |
| 652 |
const VecW eights_a = Csa256(fours_a, fours_b, &fours); |
| 653 |
twos_a = Csa256(bit_vvec[8], bit_vvec[9], &ones); |
| 654 |
twos_b = Csa256(bit_vvec[10], bit_vvec[11], &ones); |
| 655 |
fours_a = Csa256(twos_a, twos_b, &twos); |
| 656 |
twos_a = vecw_setzero(); |
| 657 |
if (remainder & 2) { |
| 658 |
twos_a = Csa256(bit_vvec[12], bit_vvec[13], &ones); |
| 659 |
} |
| 660 |
twos_b = vecw_setzero(); |
| 661 |
if (remainder & 1) { |
| 662 |
twos_b = CsaOne256(bit_vvec[remainder - 1], &ones); |
| 663 |
} |
| 664 |
fours_b = Csa256(twos_a, twos_b, &twos); |
| 665 |
const VecW eights_b = Csa256(fours_a, fours_b, &fours); |
| 666 |
const VecW sixteens = Csa256(eights_a, eights_b, &eights); |
| 667 |
cnt = cnt + PopcountVecAvx2(sixteens); |
| 668 |
cnt = vecw_slli(cnt, 4); |
| 669 |
} |
| 670 |
// Appears to be counterproductive to put multiple SAD instructions in |
| 671 |
// flight. |
| 672 |
// Compiler is smart enough that it's pointless to manually inline |
| 673 |
// PopcountVecAvx2. (Tried combining the 4 SAD calls into one, didn't help.) |
| 674 |
cnt = cnt + vecw_slli(PopcountVecAvx2(eights), 3); |
| 675 |
cnt = cnt + vecw_slli(PopcountVecAvx2(fours), 2); |
| 676 |
cnt = cnt + vecw_slli(PopcountVecAvx2(twos), 1); |
| 677 |
cnt = cnt + PopcountVecAvx2(ones); |
| 678 |
return HsumW(cnt); |
| 679 |
} |
| 680 |
|
| 681 |
uintptr_t PopcountVecsAvx2Intersect(const VecW* __restrict vvec1_iter, const VecW* __restrict vvec2_iter, uintptr_t vec_ct) { |
| 682 |
// See popcnt_avx2() in libpopcnt. vec_ct must be a multiple of 16. |
| 683 |
VecW cnt = vecw_setzero(); |
| 684 |
VecW ones = vecw_setzero(); |
| 685 |
VecW twos = vecw_setzero(); |
| 686 |
VecW fours = vecw_setzero(); |
| 687 |
VecW eights = vecw_setzero(); |
| 688 |
for (uintptr_t vec_idx = 0; vec_idx < vec_ct; vec_idx += 16) { |
| 689 |
VecW twos_a = Csa256(vvec1_iter[vec_idx + 0] & vvec2_iter[vec_idx + 0], vvec1_iter[vec_idx + 1] & vvec2_iter[vec_idx + 1], &ones); |
| 690 |
VecW twos_b = Csa256(vvec1_iter[vec_idx + 2] & vvec2_iter[vec_idx + 2], vvec1_iter[vec_idx + 3] & vvec2_iter[vec_idx + 3], &ones); |
| 691 |
VecW fours_a = Csa256(twos_a, twos_b, &twos); |
| 692 |
|
| 693 |
twos_a = Csa256(vvec1_iter[vec_idx + 4] & vvec2_iter[vec_idx + 4], vvec1_iter[vec_idx + 5] & vvec2_iter[vec_idx + 5], &ones); |
| 694 |
twos_b = Csa256(vvec1_iter[vec_idx + 6] & vvec2_iter[vec_idx + 6], vvec1_iter[vec_idx + 7] & vvec2_iter[vec_idx + 7], &ones); |
| 695 |
VecW fours_b = Csa256(twos_a, twos_b, &twos); |
| 696 |
const VecW eights_a = Csa256(fours_a, fours_b, &fours); |
| 697 |
|
| 698 |
twos_a = Csa256(vvec1_iter[vec_idx + 8] & vvec2_iter[vec_idx + 8], vvec1_iter[vec_idx + 9] & vvec2_iter[vec_idx + 9], &ones); |
| 699 |
twos_b = Csa256(vvec1_iter[vec_idx + 10] & vvec2_iter[vec_idx + 10], vvec1_iter[vec_idx + 11] & vvec2_iter[vec_idx + 11], &ones); |
| 700 |
fours_a = Csa256(twos_a, twos_b, &twos); |
| 701 |
|
| 702 |
twos_a = Csa256(vvec1_iter[vec_idx + 12] & vvec2_iter[vec_idx + 12], vvec1_iter[vec_idx + 13] & vvec2_iter[vec_idx + 13], &ones); |
| 703 |
twos_b = Csa256(vvec1_iter[vec_idx + 14] & vvec2_iter[vec_idx + 14], vvec1_iter[vec_idx + 15] & vvec2_iter[vec_idx + 15], &ones); |
| 704 |
fours_b = Csa256(twos_a, twos_b, &twos); |
| 705 |
const VecW eights_b = Csa256(fours_a, fours_b, &fours); |
| 706 |
const VecW sixteens = Csa256(eights_a, eights_b, &eights); |
| 707 |
cnt = cnt + PopcountVecAvx2(sixteens); |
| 708 |
} |
| 709 |
cnt = vecw_slli(cnt, 4); |
| 710 |
cnt = cnt + vecw_slli(PopcountVecAvx2(eights), 3); |
| 711 |
cnt = cnt + vecw_slli(PopcountVecAvx2(fours), 2); |
| 712 |
cnt = cnt + vecw_slli(PopcountVecAvx2(twos), 1); |
| 713 |
cnt = cnt + PopcountVecAvx2(ones); |
| 714 |
return HsumW(cnt); |
| 715 |
} |
| 716 |
|
| 717 |
uintptr_t PopcountWordsIntersect(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, uintptr_t word_ct) { |
| 718 |
const uintptr_t* bitvec1_end = &(bitvec1_iter[word_ct]); |
| 719 |
const uintptr_t block_ct = word_ct / (16 * kWordsPerVec); |
| 720 |
uintptr_t tot = 0; |
| 721 |
if (block_ct) { |
| 722 |
tot = PopcountVecsAvx2Intersect(R_CAST(const VecW*, bitvec1_iter), R_CAST(const VecW*, bitvec2_iter), block_ct * 16); |
| 723 |
bitvec1_iter = &(bitvec1_iter[block_ct * (16 * kWordsPerVec)]); |
| 724 |
bitvec2_iter = &(bitvec2_iter[block_ct * (16 * kWordsPerVec)]); |
| 725 |
} |
| 726 |
while (bitvec1_iter < bitvec1_end) { |
| 727 |
tot += PopcountWord((*bitvec1_iter++) & (*bitvec2_iter++)); |
| 728 |
} |
| 729 |
return tot; |
| 730 |
} |
| 731 |
|
| 732 |
uintptr_t PopcountVecsAvx2Xor(const VecW* __restrict vvec1_iter, const VecW* __restrict vvec2_iter, uintptr_t vec_ct) { |
| 733 |
// vec_ct must be a multiple of 16. |
| 734 |
VecW cnt = vecw_setzero(); |
| 735 |
VecW ones = vecw_setzero(); |
| 736 |
VecW twos = vecw_setzero(); |
| 737 |
VecW fours = vecw_setzero(); |
| 738 |
VecW eights = vecw_setzero(); |
| 739 |
for (uintptr_t vec_idx = 0; vec_idx < vec_ct; vec_idx += 16) { |
| 740 |
VecW twos_a = Csa256(vvec1_iter[vec_idx + 0] ^ vvec2_iter[vec_idx + 0], vvec1_iter[vec_idx + 1] ^ vvec2_iter[vec_idx + 1], &ones); |
| 741 |
VecW twos_b = Csa256(vvec1_iter[vec_idx + 2] ^ vvec2_iter[vec_idx + 2], vvec1_iter[vec_idx + 3] ^ vvec2_iter[vec_idx + 3], &ones); |
| 742 |
VecW fours_a = Csa256(twos_a, twos_b, &twos); |
| 743 |
|
| 744 |
twos_a = Csa256(vvec1_iter[vec_idx + 4] ^ vvec2_iter[vec_idx + 4], vvec1_iter[vec_idx + 5] ^ vvec2_iter[vec_idx + 5], &ones); |
| 745 |
twos_b = Csa256(vvec1_iter[vec_idx + 6] ^ vvec2_iter[vec_idx + 6], vvec1_iter[vec_idx + 7] ^ vvec2_iter[vec_idx + 7], &ones); |
| 746 |
VecW fours_b = Csa256(twos_a, twos_b, &twos); |
| 747 |
const VecW eights_a = Csa256(fours_a, fours_b, &fours); |
| 748 |
|
| 749 |
twos_a = Csa256(vvec1_iter[vec_idx + 8] ^ vvec2_iter[vec_idx + 8], vvec1_iter[vec_idx + 9] ^ vvec2_iter[vec_idx + 9], &ones); |
| 750 |
twos_b = Csa256(vvec1_iter[vec_idx + 10] ^ vvec2_iter[vec_idx + 10], vvec1_iter[vec_idx + 11] ^ vvec2_iter[vec_idx + 11], &ones); |
| 751 |
fours_a = Csa256(twos_a, twos_b, &twos); |
| 752 |
|
| 753 |
twos_a = Csa256(vvec1_iter[vec_idx + 12] ^ vvec2_iter[vec_idx + 12], vvec1_iter[vec_idx + 13] ^ vvec2_iter[vec_idx + 13], &ones); |
| 754 |
twos_b = Csa256(vvec1_iter[vec_idx + 14] ^ vvec2_iter[vec_idx + 14], vvec1_iter[vec_idx + 15] ^ vvec2_iter[vec_idx + 15], &ones); |
| 755 |
fours_b = Csa256(twos_a, twos_b, &twos); |
| 756 |
const VecW eights_b = Csa256(fours_a, fours_b, &fours); |
| 757 |
const VecW sixteens = Csa256(eights_a, eights_b, &eights); |
| 758 |
cnt = cnt + PopcountVecAvx2(sixteens); |
| 759 |
} |
| 760 |
cnt = vecw_slli(cnt, 4); |
| 761 |
cnt = cnt + vecw_slli(PopcountVecAvx2(eights), 3); |
| 762 |
cnt = cnt + vecw_slli(PopcountVecAvx2(fours), 2); |
| 763 |
cnt = cnt + vecw_slli(PopcountVecAvx2(twos), 1); |
| 764 |
cnt = cnt + PopcountVecAvx2(ones); |
| 765 |
return HsumW(cnt); |
| 766 |
} |
| 767 |
|
| 768 |
uintptr_t PopcountWordsXor(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, uintptr_t word_ct) { |
| 769 |
const uintptr_t* bitvec1_end = &(bitvec1_iter[word_ct]); |
| 770 |
const uintptr_t block_ct = word_ct / (16 * kWordsPerVec); |
| 771 |
uintptr_t tot = 0; |
| 772 |
if (block_ct) { |
| 773 |
tot = PopcountVecsAvx2Xor(R_CAST(const VecW*, bitvec1_iter), R_CAST(const VecW*, bitvec2_iter), block_ct * 16); |
| 774 |
bitvec1_iter = &(bitvec1_iter[block_ct * (16 * kWordsPerVec)]); |
| 775 |
bitvec2_iter = &(bitvec2_iter[block_ct * (16 * kWordsPerVec)]); |
| 776 |
} |
| 777 |
while (bitvec1_iter < bitvec1_end) { |
| 778 |
tot += PopcountWord((*bitvec1_iter++) ^ (*bitvec2_iter++)); |
| 779 |
} |
| 780 |
return tot; |
| 781 |
} |
| 782 |
|
| 783 |
/* |
| 784 |
uintptr_t PopcountVecsAvx2Intersect3(const VecW* __restrict vvec1_iter, const VecW* __restrict vvec2_iter, const VecW* __restrict vvec3_iter, uintptr_t vec_ct) { |
| 785 |
VecW cnt = vecw_setzero(); |
| 786 |
VecW ones = vecw_setzero(); |
| 787 |
VecW twos = vecw_setzero(); |
| 788 |
VecW fours = vecw_setzero(); |
| 789 |
VecW eights = vecw_setzero(); |
| 790 |
for (uintptr_t vec_idx = 0; vec_idx < vec_ct; vec_idx += 16) { |
| 791 |
VecW twos_a = Csa256(vvec1_iter[vec_idx + 0] & vvec2_iter[vec_idx + 0] & vvec3_iter[vec_idx + 0], vvec1_iter[vec_idx + 1] & vvec2_iter[vec_idx + 1] & vvec3_iter[vec_idx + 1], &ones); |
| 792 |
VecW twos_b = Csa256(vvec1_iter[vec_idx + 2] & vvec2_iter[vec_idx + 2] & vvec3_iter[vec_idx + 2], vvec1_iter[vec_idx + 3] & vvec2_iter[vec_idx + 3] & vvec3_iter[vec_idx + 3], &ones); |
| 793 |
VecW fours_a = Csa256(twos_a, twos_b, &twos); |
| 794 |
|
| 795 |
twos_a = Csa256(vvec1_iter[vec_idx + 4] & vvec2_iter[vec_idx + 4] & vvec3_iter[vec_idx + 4], vvec1_iter[vec_idx + 5] & vvec2_iter[vec_idx + 5] & vvec3_iter[vec_idx + 5], &ones); |
| 796 |
twos_b = Csa256(vvec1_iter[vec_idx + 6] & vvec2_iter[vec_idx + 6] & vvec3_iter[vec_idx + 6], vvec1_iter[vec_idx + 7] & vvec2_iter[vec_idx + 7] & vvec3_iter[vec_idx + 7], &ones); |
| 797 |
VecW fours_b = Csa256(twos_a, twos_b, &twos); |
| 798 |
const VecW eights_a = Csa256(fours_a, fours_b, &fours); |
| 799 |
|
| 800 |
twos_a = Csa256(vvec1_iter[vec_idx + 8] & vvec2_iter[vec_idx + 8] & vvec3_iter[vec_idx + 8], vvec1_iter[vec_idx + 9] & vvec2_iter[vec_idx + 9] & vvec3_iter[vec_idx + 9], &ones); |
| 801 |
twos_b = Csa256(vvec1_iter[vec_idx + 10] & vvec2_iter[vec_idx + 10] & vvec3_iter[vec_idx + 10], vvec1_iter[vec_idx + 11] & vvec2_iter[vec_idx + 11] & vvec3_iter[vec_idx + 11], &ones); |
| 802 |
fours_a = Csa256(twos_a, twos_b, &twos); |
| 803 |
|
| 804 |
twos_a = Csa256(vvec1_iter[vec_idx + 12] & vvec2_iter[vec_idx + 12] & vvec3_iter[vec_idx + 12], vvec1_iter[vec_idx + 13] & vvec2_iter[vec_idx + 13] & vvec3_iter[vec_idx + 13], &ones); |
| 805 |
twos_b = Csa256(vvec1_iter[vec_idx + 14] & vvec2_iter[vec_idx + 14] & vvec3_iter[vec_idx + 14], vvec1_iter[vec_idx + 15] & vvec2_iter[vec_idx + 15] & vvec3_iter[vec_idx + 15], &ones); |
| 806 |
fours_b = Csa256(twos_a, twos_b, &twos); |
| 807 |
const VecW eights_b = Csa256(fours_a, fours_b, &fours); |
| 808 |
const VecW sixteens = Csa256(eights_a, eights_b, &eights); |
| 809 |
cnt = cnt + PopcountVecAvx2(sixteens); |
| 810 |
} |
| 811 |
cnt = vecw_slli(cnt, 4); |
| 812 |
cnt = cnt + vecw_slli(PopcountVecAvx2(eights), 3); |
| 813 |
cnt = cnt + vecw_slli(PopcountVecAvx2(fours), 2); |
| 814 |
cnt = cnt + vecw_slli(PopcountVecAvx2(twos), 1); |
| 815 |
cnt = cnt + PopcountVecAvx2(ones); |
| 816 |
return HsumW(cnt); |
| 817 |
} |
| 818 |
|
| 819 |
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) { |
| 820 |
const uintptr_t* bitvec1_end = &(bitvec1_iter[word_ct]); |
| 821 |
const uintptr_t block_ct = word_ct / (16 * kWordsPerVec); |
| 822 |
uintptr_t tot = 0; |
| 823 |
if (block_ct) { |
| 824 |
tot = PopcountVecsAvx2Intersect3(R_CAST(const VecW*, bitvec1_iter), R_CAST(const VecW*, bitvec2_iter), R_CAST(const VecW*, bitvec3_iter), block_ct * 16); |
| 825 |
bitvec1_iter = &(bitvec1_iter[block_ct * (16 * kWordsPerVec)]); |
| 826 |
bitvec2_iter = &(bitvec2_iter[block_ct * (16 * kWordsPerVec)]); |
| 827 |
bitvec3_iter = &(bitvec3_iter[block_ct * (16 * kWordsPerVec)]); |
| 828 |
} |
| 829 |
while (bitvec1_iter < bitvec1_end) { |
| 830 |
tot += PopcountWord((*bitvec1_iter++) & (*bitvec2_iter++) & (*bitvec3_iter++)); |
| 831 |
} |
| 832 |
return tot; |
| 833 |
} |
| 834 |
*/ |
| 835 |
|
| 836 |
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) { |
| 837 |
const uint32_t expand_sizex_m1 = expand_size + read_start_bit - 1; |
| 838 |
const uint32_t leading_byte_ct = 1 + (expand_sizex_m1 % kBitsPerWord) / CHAR_BIT; |
| 839 |
uintptr_t compact_word = SubwordLoad(compact_bitarr, leading_byte_ct) >> read_start_bit; |
| 840 |
const unsigned char* compact_bitarr_biter = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 841 |
uint32_t compact_idx_lowbits = read_start_bit + CHAR_BIT * (sizeof(intptr_t) - leading_byte_ct); |
| 842 |
for (uint32_t widx = 0; widx != word_ct; ++widx) { |
| 843 |
const uintptr_t mask_word = expand_mask[widx]; |
| 844 |
uintptr_t write_word = 0; |
| 845 |
if (mask_word) { |
| 846 |
const uint32_t mask_set_ct = PopcountWord(mask_word); |
| 847 |
uint32_t next_compact_idx_lowbits = compact_idx_lowbits + mask_set_ct; |
| 848 |
if (next_compact_idx_lowbits <= kBitsPerWord) { |
| 849 |
write_word = _pdep_u64(compact_word, mask_word); |
| 850 |
if (mask_set_ct != kBitsPerWord) { |
| 851 |
compact_word = compact_word >> mask_set_ct; |
| 852 |
} else { |
| 853 |
// avoid nasal demons |
| 854 |
compact_word = 0; |
| 855 |
} |
| 856 |
} else { |
| 857 |
uintptr_t next_compact_word; |
| 858 |
CopyFromUnalignedIncrW(&next_compact_word, &compact_bitarr_biter); |
| 859 |
next_compact_idx_lowbits -= kBitsPerWord; |
| 860 |
compact_word |= next_compact_word << (kBitsPerWord - compact_idx_lowbits); |
| 861 |
write_word = _pdep_u64(compact_word, mask_word); |
| 862 |
if (next_compact_idx_lowbits != kBitsPerWord) { |
| 863 |
compact_word = next_compact_word >> next_compact_idx_lowbits; |
| 864 |
} else { |
| 865 |
compact_word = 0; |
| 866 |
} |
| 867 |
} |
| 868 |
compact_idx_lowbits = next_compact_idx_lowbits; |
| 869 |
} |
| 870 |
target[widx] = write_word; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
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) { |
| 875 |
const uint32_t expand_sizex_m1 = expand_size + read_start_bit - 1; |
| 876 |
const uint32_t leading_byte_ct = 1 + (expand_sizex_m1 % kBitsPerWord) / CHAR_BIT; |
| 877 |
uintptr_t compact_word = SubwordLoad(compact_bitarr, leading_byte_ct) >> read_start_bit; |
| 878 |
const unsigned char* compact_bitarrb = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 879 |
uint32_t compact_widx = UINT32_MAX; // deliberate overflow |
| 880 |
uint32_t compact_idx_lowbits = read_start_bit + CHAR_BIT * (sizeof(uintptr_t) - leading_byte_ct); |
| 881 |
const uint32_t subset_size_lowbits = subset_size % kBitsPerWord; |
| 882 |
uintptr_t* target_iter = target; |
| 883 |
uintptr_t* target_last = &(target[subset_size / kBitsPerWord]); |
| 884 |
uintptr_t cur_output_word = 0; |
| 885 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 886 |
uint32_t write_idx_lowbits = 0; |
| 887 |
|
| 888 |
// bugfix (5 Feb 2018): missed a case in sparse subset_mask optimization |
| 889 |
uint32_t expand_bit_ct_skip = 0; |
| 890 |
while ((target_iter != target_last) || (write_idx_lowbits != subset_size_lowbits)) { |
| 891 |
uintptr_t expand_word; |
| 892 |
uintptr_t subset_word; |
| 893 |
uint32_t expand_bit_ct; |
| 894 |
while (1) { |
| 895 |
++read_widx; |
| 896 |
expand_word = expand_mask[read_widx]; |
| 897 |
subset_word = subset_mask[read_widx]; |
| 898 |
expand_bit_ct = PopcountWord(expand_word); |
| 899 |
if (subset_word) { |
| 900 |
break; |
| 901 |
} |
| 902 |
expand_bit_ct_skip += expand_bit_ct; |
| 903 |
} |
| 904 |
uintptr_t extracted_bits = 0; |
| 905 |
const uint32_t set_bit_ct = PopcountWord(subset_word); |
| 906 |
if (expand_word & subset_word) { |
| 907 |
// lazy load |
| 908 |
compact_idx_lowbits += expand_bit_ct_skip; |
| 909 |
if (compact_idx_lowbits >= kBitsPerWord) { |
| 910 |
compact_widx += compact_idx_lowbits / kBitsPerWord; |
| 911 |
compact_idx_lowbits = compact_idx_lowbits % kBitsPerWord; |
| 912 |
CopyFromUnalignedOffsetW(&compact_word, compact_bitarrb, compact_widx); |
| 913 |
compact_word >>= compact_idx_lowbits; |
| 914 |
} else { |
| 915 |
compact_word = compact_word >> expand_bit_ct_skip; |
| 916 |
} |
| 917 |
uint32_t next_compact_idx_lowbits = compact_idx_lowbits + expand_bit_ct; |
| 918 |
uintptr_t expanded_bits; |
| 919 |
if (next_compact_idx_lowbits <= kBitsPerWord) { |
| 920 |
expanded_bits = _pdep_u64(compact_word, expand_word); |
| 921 |
if (expand_bit_ct != kBitsPerWord) { |
| 922 |
compact_word = compact_word >> expand_bit_ct; |
| 923 |
} |
| 924 |
} else { |
| 925 |
++compact_widx; |
| 926 |
uintptr_t next_compact_word; |
| 927 |
CopyFromUnalignedOffsetW(&next_compact_word, compact_bitarrb, compact_widx); |
| 928 |
next_compact_idx_lowbits -= kBitsPerWord; |
| 929 |
compact_word |= next_compact_word << (kBitsPerWord - compact_idx_lowbits); |
| 930 |
expanded_bits = _pdep_u64(compact_word, expand_word); |
| 931 |
if (next_compact_idx_lowbits != kBitsPerWord) { |
| 932 |
compact_word = next_compact_word >> next_compact_idx_lowbits; |
| 933 |
} |
| 934 |
} |
| 935 |
extracted_bits = _pext_u64(expanded_bits, subset_word); |
| 936 |
compact_idx_lowbits = next_compact_idx_lowbits; |
| 937 |
cur_output_word |= extracted_bits << write_idx_lowbits; |
| 938 |
expand_bit_ct_skip = 0; |
| 939 |
} else { |
| 940 |
expand_bit_ct_skip += expand_bit_ct; |
| 941 |
} |
| 942 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct; |
| 943 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 944 |
*target_iter++ = cur_output_word; |
| 945 |
// ...and these are the bits that fell off |
| 946 |
if (write_idx_lowbits) { |
| 947 |
cur_output_word = extracted_bits >> (kBitsPerWord - write_idx_lowbits); |
| 948 |
} else { |
| 949 |
cur_output_word = 0; |
| 950 |
} |
| 951 |
} |
| 952 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 953 |
} |
| 954 |
if (write_idx_lowbits) { |
| 955 |
*target_iter = cur_output_word; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
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) { |
| 960 |
assert(mid_popcount); |
| 961 |
const uint32_t leading_byte_ct = 1 + ((mid_popcount - 1) % kBitsPerWord) / CHAR_BIT; |
| 962 |
uintptr_t compact_read_word = SubwordLoad(compact_bitarr, leading_byte_ct); |
| 963 |
uint32_t compact_idx_lowbits = CHAR_BIT * (sizeof(intptr_t) - leading_byte_ct); |
| 964 |
const unsigned char* compact_bitarr_biter = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 965 |
const uintptr_t* mid_bitarr_iter = mid_bitarr; |
| 966 |
uint32_t mid_idx_lowbits = mid_start_bit; |
| 967 |
uintptr_t mid_read_word = (*mid_bitarr_iter) >> mid_start_bit; |
| 968 |
for (uint32_t widx = 0; widx != word_ct; ++widx) { |
| 969 |
const uintptr_t top_word = top_expand_mask[widx]; |
| 970 |
uintptr_t mid_write_word = 0; |
| 971 |
uintptr_t compact_write_word = 0; |
| 972 |
if (top_word) { |
| 973 |
const uint32_t top_set_ct = PopcountWord(top_word); |
| 974 |
uint32_t next_mid_idx_lowbits = mid_idx_lowbits + top_set_ct; |
| 975 |
if (next_mid_idx_lowbits <= kBitsPerWord) { |
| 976 |
mid_write_word = _pdep_u64(mid_read_word, top_word); |
| 977 |
if (top_set_ct != kBitsPerWord) { |
| 978 |
mid_read_word = mid_read_word >> top_set_ct; |
| 979 |
} else { |
| 980 |
// avoid nasal demons |
| 981 |
mid_read_word = 0; |
| 982 |
} |
| 983 |
} else { |
| 984 |
uintptr_t next_mid_read_word = *(++mid_bitarr_iter); |
| 985 |
next_mid_idx_lowbits -= kBitsPerWord; |
| 986 |
mid_read_word |= next_mid_read_word << (kBitsPerWord - mid_idx_lowbits); |
| 987 |
mid_write_word = _pdep_u64(mid_read_word, top_word); |
| 988 |
if (next_mid_idx_lowbits != kBitsPerWord) { |
| 989 |
mid_read_word = next_mid_read_word >> next_mid_idx_lowbits; |
| 990 |
} else { |
| 991 |
mid_read_word = 0; |
| 992 |
} |
| 993 |
} |
| 994 |
mid_idx_lowbits = next_mid_idx_lowbits; |
| 995 |
if (mid_write_word) { |
| 996 |
const uint32_t mid_set_ct = PopcountWord(mid_write_word); |
| 997 |
uint32_t next_compact_idx_lowbits = compact_idx_lowbits + mid_set_ct; |
| 998 |
if (next_compact_idx_lowbits <= kBitsPerWord) { |
| 999 |
compact_write_word = _pdep_u64(compact_read_word, mid_write_word); |
| 1000 |
if (mid_set_ct != kBitsPerWord) { |
| 1001 |
compact_read_word = compact_read_word >> mid_set_ct; |
| 1002 |
} else { |
| 1003 |
compact_read_word = 0; |
| 1004 |
} |
| 1005 |
} else { |
| 1006 |
uintptr_t next_compact_word; |
| 1007 |
CopyFromUnalignedIncrW(&next_compact_word, &compact_bitarr_biter); |
| 1008 |
next_compact_idx_lowbits -= kBitsPerWord; |
| 1009 |
compact_read_word |= next_compact_word << (kBitsPerWord - compact_idx_lowbits); |
| 1010 |
compact_write_word = _pdep_u64(compact_read_word, mid_write_word); |
| 1011 |
if (next_compact_idx_lowbits != kBitsPerWord) { |
| 1012 |
compact_read_word = next_compact_word >> next_compact_idx_lowbits; |
| 1013 |
} else { |
| 1014 |
compact_read_word = 0; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
compact_idx_lowbits = next_compact_idx_lowbits; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
mid_target[widx] = mid_write_word; |
| 1021 |
compact_target[widx] = compact_write_word; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
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) { |
| 1026 |
assert(mid_popcount); |
| 1027 |
const uint32_t leading_byte_ct = 1 + ((mid_popcount - 1) % kBitsPerWord) / CHAR_BIT; |
| 1028 |
uintptr_t compact_read_word = SubwordLoad(compact_bitarr, leading_byte_ct); |
| 1029 |
uint32_t compact_idx_lowbits = CHAR_BIT * (sizeof(intptr_t) - leading_byte_ct); |
| 1030 |
const unsigned char* compact_bitarrb = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 1031 |
const uintptr_t* mid_bitarr_iter = mid_bitarr; |
| 1032 |
const uint32_t subset_size_lowbits = subset_size % kBitsPerWord; |
| 1033 |
const uint32_t write_widx_last = subset_size / kBitsPerWord; |
| 1034 |
uintptr_t mid_read_word = (*mid_bitarr_iter) >> mid_start_bit; |
| 1035 |
uintptr_t mid_output_word = 0; |
| 1036 |
uintptr_t compact_output_word = 0; |
| 1037 |
uint32_t mid_idx_lowbits = mid_start_bit; |
| 1038 |
uint32_t compact_widx = UINT32_MAX; // deliberate overflow |
| 1039 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 1040 |
uint32_t write_idx_lowbits = 0; |
| 1041 |
uint32_t write_widx = 0; |
| 1042 |
|
| 1043 |
// bugfix (5 Feb 2018): missed a case in sparse subset_mask optimization |
| 1044 |
uint32_t mid_set_skip = 0; |
| 1045 |
while ((write_widx != write_widx_last) || (write_idx_lowbits != subset_size_lowbits)) { |
| 1046 |
uintptr_t subset_word; |
| 1047 |
uintptr_t mid_expanded_bits; |
| 1048 |
uint32_t mid_set_ct; |
| 1049 |
while (1) { |
| 1050 |
++read_widx; |
| 1051 |
uintptr_t top_word = top_expand_mask[read_widx]; |
| 1052 |
subset_word = subset_mask[read_widx]; |
| 1053 |
mid_expanded_bits = 0; |
| 1054 |
if (top_word) { |
| 1055 |
uint32_t top_set_ct = PopcountWord(top_word); |
| 1056 |
uint32_t next_mid_idx_lowbits = mid_idx_lowbits + top_set_ct; |
| 1057 |
if (next_mid_idx_lowbits <= kBitsPerWord) { |
| 1058 |
mid_expanded_bits = _pdep_u64(mid_read_word, top_word); |
| 1059 |
if (top_set_ct != kBitsPerWord) { |
| 1060 |
mid_read_word = mid_read_word >> top_set_ct; |
| 1061 |
} else { |
| 1062 |
// avoid nasal demons |
| 1063 |
mid_read_word = 0; |
| 1064 |
} |
| 1065 |
} else { |
| 1066 |
uintptr_t next_mid_read_word = *(++mid_bitarr_iter); |
| 1067 |
next_mid_idx_lowbits -= kBitsPerWord; |
| 1068 |
mid_read_word |= next_mid_read_word << (kBitsPerWord - mid_idx_lowbits); |
| 1069 |
mid_expanded_bits = _pdep_u64(mid_read_word, top_word); |
| 1070 |
if (next_mid_idx_lowbits != kBitsPerWord) { |
| 1071 |
mid_read_word = next_mid_read_word >> next_mid_idx_lowbits; |
| 1072 |
} else { |
| 1073 |
mid_read_word = 0; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
mid_idx_lowbits = next_mid_idx_lowbits; |
| 1077 |
} |
| 1078 |
mid_set_ct = PopcountWord(mid_expanded_bits); |
| 1079 |
if (subset_word) { |
| 1080 |
break; |
| 1081 |
} |
| 1082 |
mid_set_skip += mid_set_ct; |
| 1083 |
} |
| 1084 |
|
| 1085 |
uintptr_t mid_extracted_bits = 0; |
| 1086 |
uintptr_t compact_extracted_bits = 0; |
| 1087 |
uint32_t set_bit_ct = PopcountWord(subset_word); |
| 1088 |
if (mid_expanded_bits & subset_word) { |
| 1089 |
// lazy load |
| 1090 |
compact_idx_lowbits += mid_set_skip; |
| 1091 |
if (compact_idx_lowbits >= kBitsPerWord) { |
| 1092 |
compact_widx += compact_idx_lowbits / kBitsPerWord; |
| 1093 |
compact_idx_lowbits = compact_idx_lowbits % kBitsPerWord; |
| 1094 |
CopyFromUnalignedOffsetW(&compact_read_word, compact_bitarrb, compact_widx); |
| 1095 |
compact_read_word >>= compact_idx_lowbits; |
| 1096 |
} else { |
| 1097 |
compact_read_word = compact_read_word >> mid_set_skip; |
| 1098 |
} |
| 1099 |
uint32_t next_compact_idx_lowbits = compact_idx_lowbits + mid_set_ct; |
| 1100 |
uintptr_t compact_expanded_bits; |
| 1101 |
if (next_compact_idx_lowbits <= kBitsPerWord) { |
| 1102 |
compact_expanded_bits = _pdep_u64(compact_read_word, mid_expanded_bits); |
| 1103 |
if (mid_set_ct != kBitsPerWord) { |
| 1104 |
compact_read_word = compact_read_word >> mid_set_ct; |
| 1105 |
} |
| 1106 |
} else { |
| 1107 |
++compact_widx; |
| 1108 |
uintptr_t next_compact_word; |
| 1109 |
CopyFromUnalignedOffsetW(&next_compact_word, compact_bitarrb, compact_widx); |
| 1110 |
next_compact_idx_lowbits -= kBitsPerWord; |
| 1111 |
compact_read_word |= next_compact_word << (kBitsPerWord - compact_idx_lowbits); |
| 1112 |
compact_expanded_bits = _pdep_u64(compact_read_word, mid_expanded_bits); |
| 1113 |
if (next_compact_idx_lowbits != kBitsPerWord) { |
| 1114 |
compact_read_word = next_compact_word >> next_compact_idx_lowbits; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
compact_extracted_bits = _pext_u64(compact_expanded_bits, subset_word); |
| 1118 |
mid_extracted_bits = _pext_u64(mid_expanded_bits, subset_word); |
| 1119 |
compact_idx_lowbits = next_compact_idx_lowbits; |
| 1120 |
compact_output_word |= compact_extracted_bits << write_idx_lowbits; |
| 1121 |
mid_output_word |= mid_extracted_bits << write_idx_lowbits; |
| 1122 |
mid_set_skip = 0; |
| 1123 |
} else { |
| 1124 |
mid_set_skip += mid_set_ct; |
| 1125 |
} |
| 1126 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct; |
| 1127 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 1128 |
mid_target[write_widx] = mid_output_word; |
| 1129 |
compact_target[write_widx] = compact_output_word; |
| 1130 |
++write_widx; |
| 1131 |
if (write_idx_lowbits) { |
| 1132 |
mid_output_word = mid_extracted_bits >> (kBitsPerWord - write_idx_lowbits); |
| 1133 |
compact_output_word = compact_extracted_bits >> (kBitsPerWord - write_idx_lowbits); |
| 1134 |
} else { |
| 1135 |
mid_output_word = 0; |
| 1136 |
compact_output_word = 0; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 1140 |
} |
| 1141 |
if (write_idx_lowbits) { |
| 1142 |
mid_target[write_widx] = mid_output_word; |
| 1143 |
compact_target[write_widx] = compact_output_word; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
#else // !USE_AVX2 |
| 1147 |
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) { |
| 1148 |
const uint32_t output_bit_idx_end_lowbits = output_bit_idx_end % kBitsPerWord; |
| 1149 |
uintptr_t* output_bitarr_iter = output_bitarr; |
| 1150 |
uintptr_t* output_bitarr_last = &(output_bitarr[output_bit_idx_end / kBitsPerWord]); |
| 1151 |
uintptr_t cur_output_word = 0; |
| 1152 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 1153 |
uint32_t write_idx_lowbits = 0; |
| 1154 |
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != output_bit_idx_end_lowbits)) { |
| 1155 |
uintptr_t cur_mask_word; |
| 1156 |
// sparse subset_mask optimization |
| 1157 |
// guaranteed to terminate since there's at least one more set bit |
| 1158 |
do { |
| 1159 |
cur_mask_word = subset_mask[++read_widx]; |
| 1160 |
} while (!cur_mask_word); |
| 1161 |
uintptr_t cur_masked_input_word = raw_bitarr[read_widx] & cur_mask_word; |
| 1162 |
const uint32_t cur_mask_popcount = PopcountWord(cur_mask_word); |
| 1163 |
uintptr_t subsetted_input_word = 0; |
| 1164 |
while (cur_masked_input_word) { |
| 1165 |
const uintptr_t mask_word_high = (cur_mask_word | (cur_masked_input_word ^ (cur_masked_input_word - 1))) + 1; |
| 1166 |
if (!mask_word_high) { |
| 1167 |
subsetted_input_word |= cur_masked_input_word >> (kBitsPerWord - cur_mask_popcount); |
| 1168 |
break; |
| 1169 |
} |
| 1170 |
const uint32_t cur_read_end = ctzw(mask_word_high); |
| 1171 |
const uintptr_t bits_to_copy = cur_masked_input_word & (~mask_word_high); |
| 1172 |
cur_masked_input_word ^= bits_to_copy; |
| 1173 |
const uint32_t cur_write_end = PopcountWord(cur_mask_word & (~mask_word_high)); |
| 1174 |
subsetted_input_word |= bits_to_copy >> (cur_read_end - cur_write_end); |
| 1175 |
} |
| 1176 |
cur_output_word |= subsetted_input_word << write_idx_lowbits; |
| 1177 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + cur_mask_popcount; |
| 1178 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 1179 |
*output_bitarr_iter++ = cur_output_word; |
| 1180 |
// ...and these are the bits that fell off |
| 1181 |
// bugfix: unsafe to right-shift 64 |
| 1182 |
if (write_idx_lowbits) { |
| 1183 |
cur_output_word = subsetted_input_word >> (kBitsPerWord - write_idx_lowbits); |
| 1184 |
} else { |
| 1185 |
cur_output_word = 0; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 1189 |
} |
| 1190 |
if (write_idx_lowbits) { |
| 1191 |
*output_bitarr_iter = cur_output_word; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
# ifdef NO_UNALIGNED |
| 1196 |
void CopyBitarrSubsetToUnaligned(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict subset_mask, uint32_t output_bit_idx_end, void* __restrict output_bitarr) { |
| 1197 |
const uint32_t output_bit_idx_end_lowbits = output_bit_idx_end % kBitsPerWord; |
| 1198 |
unsigned char* output_bitarr_iter = S_CAST(unsigned char*, output_bitarr); |
| 1199 |
unsigned char* output_bitarr_last = &(output_bitarr_iter[(output_bit_idx_end / kBitsPerWord) * kBytesPerWord]); |
| 1200 |
uintptr_t cur_output_word = 0; |
| 1201 |
uint32_t read_widx = UINT32_MAX; // deliberate overflow |
| 1202 |
uint32_t write_idx_lowbits = 0; |
| 1203 |
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != output_bit_idx_end_lowbits)) { |
| 1204 |
uintptr_t cur_mask_word; |
| 1205 |
// sparse subset_mask optimization |
| 1206 |
// guaranteed to terminate since there's at least one more set bit |
| 1207 |
do { |
| 1208 |
cur_mask_word = subset_mask[++read_widx]; |
| 1209 |
} while (!cur_mask_word); |
| 1210 |
uintptr_t cur_masked_input_word = raw_bitarr[read_widx] & cur_mask_word; |
| 1211 |
const uint32_t cur_mask_popcount = PopcountWord(cur_mask_word); |
| 1212 |
uintptr_t subsetted_input_word = 0; |
| 1213 |
while (cur_masked_input_word) { |
| 1214 |
const uintptr_t mask_word_high = (cur_mask_word | (cur_masked_input_word ^ (cur_masked_input_word - 1))) + 1; |
| 1215 |
if (!mask_word_high) { |
| 1216 |
subsetted_input_word |= cur_masked_input_word >> (kBitsPerWord - cur_mask_popcount); |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
const uint32_t cur_read_end = ctzw(mask_word_high); |
| 1220 |
const uintptr_t bits_to_copy = cur_masked_input_word & (~mask_word_high); |
| 1221 |
cur_masked_input_word ^= bits_to_copy; |
| 1222 |
const uint32_t cur_write_end = PopcountWord(cur_mask_word & (~mask_word_high)); |
| 1223 |
subsetted_input_word |= bits_to_copy >> (cur_read_end - cur_write_end); |
| 1224 |
} |
| 1225 |
cur_output_word |= subsetted_input_word << write_idx_lowbits; |
| 1226 |
const uint32_t new_write_idx_lowbits = write_idx_lowbits + cur_mask_popcount; |
| 1227 |
if (new_write_idx_lowbits >= kBitsPerWord) { |
| 1228 |
AppendW(cur_output_word, &output_bitarr_iter); |
| 1229 |
// ...and these are the bits that fell off |
| 1230 |
// bugfix: unsafe to right-shift 64 |
| 1231 |
if (write_idx_lowbits) { |
| 1232 |
cur_output_word = subsetted_input_word >> (kBitsPerWord - write_idx_lowbits); |
| 1233 |
} else { |
| 1234 |
cur_output_word = 0; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord; |
| 1238 |
} |
| 1239 |
if (write_idx_lowbits) { |
| 1240 |
CopyToUnalignedW(output_bitarr_iter, &cur_output_word); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
# endif |
| 1244 |
|
| 1245 |
// Basic SSE2 implementation of Lauradoux/Walisch popcount. |
| 1246 |
uintptr_t PopcountVecsNoAvx2(const VecW* bit_vvec, uintptr_t vec_ct) { |
| 1247 |
// popcounts vptr[0..(vec_ct-1)]. Assumes vec_ct is a multiple of 3 (0 ok). |
| 1248 |
assert(!(vec_ct % 3)); |
| 1249 |
const VecW m0 = vecw_setzero(); |
| 1250 |
const VecW m1 = VCONST_W(kMask5555); |
| 1251 |
const VecW m2 = VCONST_W(kMask3333); |
| 1252 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1253 |
const VecW* bit_vvec_iter = bit_vvec; |
| 1254 |
VecW prev_sad_result = vecw_setzero(); |
| 1255 |
VecW acc = vecw_setzero(); |
| 1256 |
uintptr_t cur_incr = 30; |
| 1257 |
for (; ; vec_ct -= cur_incr) { |
| 1258 |
if (vec_ct < 30) { |
| 1259 |
if (!vec_ct) { |
| 1260 |
acc = acc + prev_sad_result; |
| 1261 |
return HsumW(acc); |
| 1262 |
} |
| 1263 |
cur_incr = vec_ct; |
| 1264 |
} |
| 1265 |
VecW inner_acc = vecw_setzero(); |
| 1266 |
const VecW* bit_vvec_stop = &(bit_vvec_iter[cur_incr]); |
| 1267 |
do { |
| 1268 |
VecW count1 = *bit_vvec_iter++; |
| 1269 |
VecW count2 = *bit_vvec_iter++; |
| 1270 |
VecW half1 = *bit_vvec_iter++; |
| 1271 |
VecW half2 = vecw_srli(half1, 1) & m1; |
| 1272 |
half1 = half1 & m1; |
| 1273 |
// Two bits can represent values from 0-3, so make each pair in count1 |
| 1274 |
// count2 store a partial bitcount covering themselves AND another bit |
| 1275 |
// from elsewhere. |
| 1276 |
count1 = count1 - (vecw_srli(count1, 1) & m1); |
| 1277 |
count2 = count2 - (vecw_srli(count2, 1) & m1); |
| 1278 |
count1 = count1 + half1; |
| 1279 |
count2 = count2 + half2; |
| 1280 |
// Four bits represent 0-15, so we can safely add four 0-3 partial |
| 1281 |
// bitcounts together. |
| 1282 |
count1 = (count1 & m2) + (vecw_srli(count1, 2) & m2); |
| 1283 |
count1 = count1 + (count2 & m2) + (vecw_srli(count2, 2) & m2); |
| 1284 |
// Accumulator stores sixteen 0-255 counts in parallel. |
| 1285 |
// (32 in AVX2 case, 4 in 32-bit case) |
| 1286 |
inner_acc = inner_acc + (count1 & m4) + (vecw_srli(count1, 4) & m4); |
| 1287 |
} while (bit_vvec_iter < bit_vvec_stop); |
| 1288 |
// _mm_sad_epu8() has better throughput than the previous method of |
| 1289 |
// horizontal-summing the bytes in inner_acc, by enough to compensate for |
| 1290 |
// the loop length being reduced from 30 to 15 vectors, but it has high |
| 1291 |
// latency. We work around that by waiting till the end of the next full |
| 1292 |
// loop iteration to actually use the SAD result. |
| 1293 |
acc = acc + prev_sad_result; |
| 1294 |
prev_sad_result = vecw_bytesum(inner_acc, m0); |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
static inline uintptr_t PopcountVecsNoAvx2Intersect(const VecW* __restrict vvec1_iter, const VecW* __restrict vvec2_iter, uintptr_t vec_ct) { |
| 1299 |
// popcounts vvec1 AND vvec2[0..(ct-1)]. ct is a multiple of 3. |
| 1300 |
assert(!(vec_ct % 3)); |
| 1301 |
const VecW m0 = vecw_setzero(); |
| 1302 |
const VecW m1 = VCONST_W(kMask5555); |
| 1303 |
const VecW m2 = VCONST_W(kMask3333); |
| 1304 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1305 |
VecW prev_sad_result = vecw_setzero(); |
| 1306 |
VecW acc = vecw_setzero(); |
| 1307 |
uintptr_t cur_incr = 30; |
| 1308 |
for (; ; vec_ct -= cur_incr) { |
| 1309 |
if (vec_ct < 30) { |
| 1310 |
if (!vec_ct) { |
| 1311 |
acc = acc + prev_sad_result; |
| 1312 |
return HsumW(acc); |
| 1313 |
} |
| 1314 |
cur_incr = vec_ct; |
| 1315 |
} |
| 1316 |
VecW inner_acc = vecw_setzero(); |
| 1317 |
const VecW* vvec1_stop = &(vvec1_iter[cur_incr]); |
| 1318 |
do { |
| 1319 |
VecW count1 = (*vvec1_iter++) & (*vvec2_iter++); |
| 1320 |
VecW count2 = (*vvec1_iter++) & (*vvec2_iter++); |
| 1321 |
VecW half1 = (*vvec1_iter++) & (*vvec2_iter++); |
| 1322 |
const VecW half2 = vecw_srli(half1, 1) & m1; |
| 1323 |
half1 = half1 & m1; |
| 1324 |
count1 = count1 - (vecw_srli(count1, 1) & m1); |
| 1325 |
count2 = count2 - (vecw_srli(count2, 1) & m1); |
| 1326 |
count1 = count1 + half1; |
| 1327 |
count2 = count2 + half2; |
| 1328 |
count1 = (count1 & m2) + (vecw_srli(count1, 2) & m2); |
| 1329 |
count1 = count1 + (count2 & m2) + (vecw_srli(count2, 2) & m2); |
| 1330 |
inner_acc = inner_acc + (count1 & m4) + (vecw_srli(count1, 4) & m4); |
| 1331 |
} while (vvec1_iter < vvec1_stop); |
| 1332 |
acc = acc + prev_sad_result; |
| 1333 |
prev_sad_result = vecw_bytesum(inner_acc, m0); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
uintptr_t PopcountWordsIntersect(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, uintptr_t word_ct) { |
| 1338 |
uintptr_t tot = 0; |
| 1339 |
const uintptr_t* bitvec1_end = &(bitvec1_iter[word_ct]); |
| 1340 |
const uintptr_t trivec_ct = word_ct / (3 * kWordsPerVec); |
| 1341 |
tot += PopcountVecsNoAvx2Intersect(R_CAST(const VecW*, bitvec1_iter), R_CAST(const VecW*, bitvec2_iter), trivec_ct * 3); |
| 1342 |
bitvec1_iter = &(bitvec1_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1343 |
bitvec2_iter = &(bitvec2_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1344 |
while (bitvec1_iter < bitvec1_end) { |
| 1345 |
tot += PopcountWord((*bitvec1_iter++) & (*bitvec2_iter++)); |
| 1346 |
} |
| 1347 |
return tot; |
| 1348 |
} |
| 1349 |
|
| 1350 |
static inline uintptr_t PopcountVecsNoAvx2Xor(const VecW* __restrict vvec1_iter, const VecW* __restrict vvec2_iter, uintptr_t vec_ct) { |
| 1351 |
// popcounts vvec1 XOR vvec2[0..(ct-1)]. ct is a multiple of 3. |
| 1352 |
assert(!(vec_ct % 3)); |
| 1353 |
const VecW m0 = vecw_setzero(); |
| 1354 |
const VecW m1 = VCONST_W(kMask5555); |
| 1355 |
const VecW m2 = VCONST_W(kMask3333); |
| 1356 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1357 |
VecW prev_sad_result = vecw_setzero(); |
| 1358 |
VecW acc = vecw_setzero(); |
| 1359 |
uintptr_t cur_incr = 30; |
| 1360 |
for (; ; vec_ct -= cur_incr) { |
| 1361 |
if (vec_ct < 30) { |
| 1362 |
if (!vec_ct) { |
| 1363 |
acc = acc + prev_sad_result; |
| 1364 |
return HsumW(acc); |
| 1365 |
} |
| 1366 |
cur_incr = vec_ct; |
| 1367 |
} |
| 1368 |
VecW inner_acc = vecw_setzero(); |
| 1369 |
const VecW* vvec1_stop = &(vvec1_iter[cur_incr]); |
| 1370 |
do { |
| 1371 |
VecW count1 = (*vvec1_iter++) ^ (*vvec2_iter++); |
| 1372 |
VecW count2 = (*vvec1_iter++) ^ (*vvec2_iter++); |
| 1373 |
VecW half1 = (*vvec1_iter++) ^ (*vvec2_iter++); |
| 1374 |
const VecW half2 = vecw_srli(half1, 1) & m1; |
| 1375 |
half1 = half1 & m1; |
| 1376 |
count1 = count1 - (vecw_srli(count1, 1) & m1); |
| 1377 |
count2 = count2 - (vecw_srli(count2, 1) & m1); |
| 1378 |
count1 = count1 + half1; |
| 1379 |
count2 = count2 + half2; |
| 1380 |
count1 = (count1 & m2) + (vecw_srli(count1, 2) & m2); |
| 1381 |
count1 = count1 + (count2 & m2) + (vecw_srli(count2, 2) & m2); |
| 1382 |
inner_acc = inner_acc + (count1 & m4) + (vecw_srli(count1, 4) & m4); |
| 1383 |
} while (vvec1_iter < vvec1_stop); |
| 1384 |
acc = acc + prev_sad_result; |
| 1385 |
prev_sad_result = vecw_bytesum(inner_acc, m0); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
uintptr_t PopcountWordsXor(const uintptr_t* __restrict bitvec1_iter, const uintptr_t* __restrict bitvec2_iter, uintptr_t word_ct) { |
| 1390 |
uintptr_t tot = 0; |
| 1391 |
const uintptr_t* bitvec1_end = &(bitvec1_iter[word_ct]); |
| 1392 |
const uintptr_t trivec_ct = word_ct / (3 * kWordsPerVec); |
| 1393 |
tot += PopcountVecsNoAvx2Xor(R_CAST(const VecW*, bitvec1_iter), R_CAST(const VecW*, bitvec2_iter), trivec_ct * 3); |
| 1394 |
bitvec1_iter = &(bitvec1_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1395 |
bitvec2_iter = &(bitvec2_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1396 |
while (bitvec1_iter < bitvec1_end) { |
| 1397 |
tot += PopcountWord((*bitvec1_iter++) ^ (*bitvec2_iter++)); |
| 1398 |
} |
| 1399 |
return tot; |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* |
| 1403 |
static inline uintptr_t PopcountVecsNoAvx2Intersect3(const VecW* __restrict vvec1_iter, const VecW* __restrict vvec2_iter, const VecW* __restrict vvec3_iter, uintptr_t vec_ct) { |
| 1404 |
assert(!(vec_ct % 3)); |
| 1405 |
const VecW m0 = vecw_setzero(); |
| 1406 |
const VecW m1 = VCONST_W(kMask5555); |
| 1407 |
const VecW m2 = VCONST_W(kMask3333); |
| 1408 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 1409 |
VecW prev_sad_result = vecw_setzero(); |
| 1410 |
VecW acc = vecw_setzero(); |
| 1411 |
uintptr_t cur_incr = 30; |
| 1412 |
for (; ; vec_ct -= cur_incr) { |
| 1413 |
if (vec_ct < 30) { |
| 1414 |
if (!vec_ct) { |
| 1415 |
acc = acc + prev_sad_result; |
| 1416 |
return HsumW(acc); |
| 1417 |
} |
| 1418 |
cur_incr = vec_ct; |
| 1419 |
} |
| 1420 |
VecW inner_acc = vecw_setzero(); |
| 1421 |
const VecW* vvec1_stop = &(vvec1_iter[cur_incr]); |
| 1422 |
do { |
| 1423 |
VecW count1 = (*vvec1_iter++) & (*vvec2_iter++) & (*vvec3_iter++); |
| 1424 |
VecW count2 = (*vvec1_iter++) & (*vvec2_iter++) & (*vvec3_iter++); |
| 1425 |
VecW half1 = (*vvec1_iter++) & (*vvec2_iter++) & (*vvec3_iter++); |
| 1426 |
const VecW half2 = vecw_srli(half1, 1) & m1; |
| 1427 |
half1 = half1 & m1; |
| 1428 |
count1 = count1 - (vecw_srli(count1, 1) & m1); |
| 1429 |
count2 = count2 - (vecw_srli(count2, 1) & m1); |
| 1430 |
count1 = count1 + half1; |
| 1431 |
count2 = count2 + half2; |
| 1432 |
count1 = (count1 & m2) + (vecw_srli(count1, 2) & m2); |
| 1433 |
count1 = count1 + (count2 & m2) + (vecw_srli(count2, 2) & m2); |
| 1434 |
inner_acc = inner_acc + (count1 & m4) + (vecw_srli(count1, 4) & m4); |
| 1435 |
} while (vvec1_iter < vvec1_stop); |
| 1436 |
acc = acc + prev_sad_result; |
| 1437 |
prev_sad_result = vecw_bytesum(inner_acc, m0); |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
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) { |
| 1442 |
uintptr_t tot = 0; |
| 1443 |
const uintptr_t* bitvec1_end = &(bitvec1_iter[word_ct]); |
| 1444 |
const uintptr_t trivec_ct = word_ct / (3 * kWordsPerVec); |
| 1445 |
tot += PopcountVecsNoAvx2Intersect3(R_CAST(const VecW*, bitvec1_iter), R_CAST(const VecW*, bitvec2_iter), R_CAST(const VecW*, bitvec3_iter), trivec_ct * 3); |
| 1446 |
bitvec1_iter = &(bitvec1_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1447 |
bitvec2_iter = &(bitvec2_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1448 |
bitvec3_iter = &(bitvec3_iter[trivec_ct * (3 * kWordsPerVec)]); |
| 1449 |
while (bitvec1_iter < bitvec1_end) { |
| 1450 |
tot += PopcountWord((*bitvec1_iter++) & (*bitvec2_iter++) & (*bitvec3_iter++)); |
| 1451 |
} |
| 1452 |
return tot; |
| 1453 |
} |
| 1454 |
*/ |
| 1455 |
|
| 1456 |
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) { |
| 1457 |
ZeroWArr(word_ct, target); |
| 1458 |
const unsigned char* compact_bitarrb = S_CAST(const unsigned char*, compact_bitarr); |
| 1459 |
const uint32_t expand_sizex_m1 = expand_size + read_start_bit - 1; |
| 1460 |
const uint32_t compact_widx_last = expand_sizex_m1 / kBitsPerWord; |
| 1461 |
uint32_t compact_idx_lowbits = read_start_bit; |
| 1462 |
uint32_t loop_len = kBitsPerWord; |
| 1463 |
uintptr_t write_widx = 0; |
| 1464 |
uintptr_t expand_mask_bits = expand_mask[0]; |
| 1465 |
for (uint32_t compact_widx = 0; ; ++compact_widx) { |
| 1466 |
uintptr_t compact_word; |
| 1467 |
if (compact_widx >= compact_widx_last) { |
| 1468 |
if (compact_widx > compact_widx_last) { |
| 1469 |
return; |
| 1470 |
} |
| 1471 |
loop_len = 1 + (expand_sizex_m1 % kBitsPerWord); |
| 1472 |
// avoid possible segfault |
| 1473 |
compact_word = SubwordLoad(&(compact_bitarrb[compact_widx * kBytesPerWord]), DivUp(loop_len, CHAR_BIT)); |
| 1474 |
} else { |
| 1475 |
CopyFromUnalignedOffsetW(&compact_word, compact_bitarrb, compact_widx); |
| 1476 |
} |
| 1477 |
for (; compact_idx_lowbits != loop_len; ++compact_idx_lowbits) { |
| 1478 |
const uintptr_t lowbit = BitIter1y(expand_mask, &write_widx, &expand_mask_bits); |
| 1479 |
// bugfix: can't just use (compact_word & 1) and compact_word >>= 1, |
| 1480 |
// since we may skip the first bit on the first loop iteration |
| 1481 |
if ((compact_word >> compact_idx_lowbits) & 1) { |
| 1482 |
target[write_widx] |= lowbit; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
compact_idx_lowbits = 0; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
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) { |
| 1490 |
const uint32_t expand_sizex_m1 = expand_size + read_start_bit - 1; |
| 1491 |
const uint32_t leading_byte_ct = 1 + (expand_sizex_m1 % kBitsPerWord) / CHAR_BIT; |
| 1492 |
uint32_t read_idx_lowbits = CHAR_BIT * (sizeof(intptr_t) - leading_byte_ct); |
| 1493 |
uintptr_t compact_read_word = SubwordLoad(compact_bitarr, leading_byte_ct) << read_idx_lowbits; |
| 1494 |
read_idx_lowbits += read_start_bit; |
| 1495 |
const unsigned char* compact_bitarr_biter = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 1496 |
const uint32_t subset_size_lowbits = subset_size % kBitsPerWord; |
| 1497 |
uintptr_t* target_iter = target; |
| 1498 |
uintptr_t* target_last = &(target[subset_size / kBitsPerWord]); |
| 1499 |
uintptr_t compact_write_word = 0; |
| 1500 |
uint32_t read_widx = 0; |
| 1501 |
// further improvement is probably possible (e.g. use AVX2 lazy-load), but |
| 1502 |
// I'll postpone for now |
| 1503 |
uint32_t write_idx_lowbits = 0; |
| 1504 |
while ((target_iter != target_last) || (write_idx_lowbits != subset_size_lowbits)) { |
| 1505 |
const uintptr_t subset_word = subset_mask[read_widx]; |
| 1506 |
const uintptr_t expand_word = expand_mask[read_widx]; |
| 1507 |
++read_widx; |
| 1508 |
uintptr_t tmp_compact_write_word = 0; |
| 1509 |
if (expand_word) { |
| 1510 |
const uint32_t expand_bit_ct = PopcountWord(expand_word); |
| 1511 |
uint32_t read_idx_lowbits_end = read_idx_lowbits + expand_bit_ct; |
| 1512 |
uintptr_t tmp_compact_read_word = 0; |
| 1513 |
if (read_idx_lowbits != kBitsPerWord) { |
| 1514 |
tmp_compact_read_word = compact_read_word >> read_idx_lowbits; |
| 1515 |
} |
| 1516 |
if (read_idx_lowbits_end > kBitsPerWord) { |
| 1517 |
CopyFromUnalignedIncrW(&compact_read_word, &compact_bitarr_biter); |
| 1518 |
tmp_compact_read_word |= compact_read_word << (kBitsPerWord - read_idx_lowbits); |
| 1519 |
read_idx_lowbits_end -= kBitsPerWord; |
| 1520 |
} |
| 1521 |
tmp_compact_read_word = bzhi_max(tmp_compact_read_word, expand_bit_ct); |
| 1522 |
read_idx_lowbits = read_idx_lowbits_end; |
| 1523 |
if (tmp_compact_read_word) { |
| 1524 |
uintptr_t cur_intersect = subset_word & expand_word; |
| 1525 |
while (cur_intersect) { |
| 1526 |
const uintptr_t cur_intersect_and_arg = cur_intersect - k1LU; |
| 1527 |
const uintptr_t lowmask = (cur_intersect ^ cur_intersect_and_arg) >> 1; |
| 1528 |
const uint32_t read_idx_offset = PopcountWord(expand_word & lowmask); |
| 1529 |
uintptr_t shifted_compact_read_word = tmp_compact_read_word >> read_idx_offset; |
| 1530 |
if (shifted_compact_read_word & 1) { |
| 1531 |
tmp_compact_write_word |= (k1LU << PopcountWord(subset_word & lowmask)); |
| 1532 |
if (shifted_compact_read_word == 1) { |
| 1533 |
break; |
| 1534 |
} |
| 1535 |
} |
| 1536 |
cur_intersect &= cur_intersect_and_arg; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
compact_write_word |= tmp_compact_write_word << write_idx_lowbits; |
| 1540 |
} |
| 1541 |
const uint32_t write_idx_lowbits_end = write_idx_lowbits + PopcountWord(subset_word); |
| 1542 |
if (write_idx_lowbits_end >= kBitsPerWord) { |
| 1543 |
*target_iter++ = compact_write_word; |
| 1544 |
if (write_idx_lowbits) { |
| 1545 |
compact_write_word = tmp_compact_write_word >> (kBitsPerWord - write_idx_lowbits); |
| 1546 |
} else { |
| 1547 |
compact_write_word = 0; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
write_idx_lowbits = write_idx_lowbits_end % kBitsPerWord; |
| 1551 |
} |
| 1552 |
if (write_idx_lowbits) { |
| 1553 |
*target_iter = compact_write_word; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
// compact_bitarr := phaseinfo |
| 1558 |
// mid_bitarr := phasepresent, [1 + het_ct] |
| 1559 |
// top_expand_mask := all_hets, [raw_sample_ct] |
| 1560 |
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) { |
| 1561 |
ZeroWArr(word_ct, mid_target); |
| 1562 |
ZeroWArr(word_ct, compact_target); |
| 1563 |
const unsigned char* compact_bitarrb = S_CAST(const unsigned char*, compact_bitarr); |
| 1564 |
const uint32_t mid_popcount_m1 = mid_popcount - 1; |
| 1565 |
const uint32_t compact_widx_last = mid_popcount_m1 / kBitsPerWord; |
| 1566 |
uint32_t mid_idx = mid_start_bit; |
| 1567 |
// can allow compact_idx_lowbits to be initialized to nonzero |
| 1568 |
uint32_t loop_len = kBitsPerWord; |
| 1569 |
uintptr_t write_widx = 0; |
| 1570 |
uintptr_t top_expand_mask_bits = top_expand_mask[0]; |
| 1571 |
for (uint32_t compact_widx = 0; ; ++compact_widx) { |
| 1572 |
uintptr_t compact_word; |
| 1573 |
if (compact_widx >= compact_widx_last) { |
| 1574 |
if (compact_widx > compact_widx_last) { |
| 1575 |
return; |
| 1576 |
} |
| 1577 |
loop_len = 1 + (mid_popcount_m1 % kBitsPerWord); |
| 1578 |
// avoid possible segfault |
| 1579 |
compact_word = SubwordLoad(&(compact_bitarrb[compact_widx * kBytesPerWord]), DivUp(loop_len, CHAR_BIT)); |
| 1580 |
} else { |
| 1581 |
CopyFromUnalignedOffsetW(&compact_word, compact_bitarrb, compact_widx); |
| 1582 |
} |
| 1583 |
for (uint32_t compact_idx_lowbits = 0; compact_idx_lowbits != loop_len; ++mid_idx) { |
| 1584 |
const uintptr_t lowbit = BitIter1y(top_expand_mask, &write_widx, &top_expand_mask_bits); |
| 1585 |
if (IsSet(mid_bitarr, mid_idx)) { |
| 1586 |
mid_target[write_widx] |= lowbit; |
| 1587 |
compact_target[write_widx] |= lowbit * (compact_word & 1); |
| 1588 |
compact_word >>= 1; |
| 1589 |
++compact_idx_lowbits; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
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) { |
| 1596 |
assert(mid_popcount); |
| 1597 |
const uint32_t leading_byte_ct = 1 + ((mid_popcount - 1) % kBitsPerWord) / CHAR_BIT; |
| 1598 |
uint32_t compact_idx_lowbits = CHAR_BIT * (sizeof(intptr_t) - leading_byte_ct); |
| 1599 |
uintptr_t compact_read_word = SubwordLoad(compact_bitarr, leading_byte_ct) << compact_idx_lowbits; |
| 1600 |
const unsigned char* compact_bitarr_biter = &(S_CAST(const unsigned char*, compact_bitarr)[leading_byte_ct]); |
| 1601 |
// bugfix (12 Apr 2018): need to round down here |
| 1602 |
const uint32_t subset_size_dl = subset_size / kBitsPerWord; |
| 1603 |
const uint32_t subset_size_lowbits = subset_size % kBitsPerWord; |
| 1604 |
const uintptr_t* mid_read_iter = mid_bitarr; |
| 1605 |
uintptr_t mid_read_word = *mid_read_iter++; |
| 1606 |
uintptr_t mid_write_word = 0; |
| 1607 |
uintptr_t compact_write_word = 0; |
| 1608 |
uint32_t mid_idx_lowbits = mid_start_bit; |
| 1609 |
uint32_t write_idx_lowbits = 0; |
| 1610 |
uint32_t write_widx = 0; |
| 1611 |
uint32_t read_widx = 0; |
| 1612 |
while ((write_widx != subset_size_dl) || (write_idx_lowbits != subset_size_lowbits)) { |
| 1613 |
const uintptr_t subset_word = subset_mask[read_widx]; |
| 1614 |
const uintptr_t top_word = top_expand_mask[read_widx]; |
| 1615 |
++read_widx; |
| 1616 |
uintptr_t tmp_mid_write_word = 0; |
| 1617 |
uintptr_t tmp_compact_write_word = 0; |
| 1618 |
if (top_word) { |
| 1619 |
const uint32_t top_set_ct = PopcountWord(top_word); |
| 1620 |
uint32_t mid_idx_lowbits_end = mid_idx_lowbits + top_set_ct; |
| 1621 |
uintptr_t tmp_mid_read_word = 0; |
| 1622 |
if (mid_idx_lowbits != kBitsPerWord) { |
| 1623 |
tmp_mid_read_word = mid_read_word >> mid_idx_lowbits; |
| 1624 |
} |
| 1625 |
if (mid_idx_lowbits_end > kBitsPerWord) { |
| 1626 |
// be paranoid for now re: reading an extra word off the end of |
| 1627 |
// mid_bitarr |
| 1628 |
mid_read_word = *mid_read_iter++; |
| 1629 |
tmp_mid_read_word |= mid_read_word << (kBitsPerWord - mid_idx_lowbits); |
| 1630 |
mid_idx_lowbits_end -= kBitsPerWord; |
| 1631 |
} |
| 1632 |
tmp_mid_read_word = bzhi_max(tmp_mid_read_word, top_set_ct); |
| 1633 |
mid_idx_lowbits = mid_idx_lowbits_end; |
| 1634 |
if (tmp_mid_read_word) { |
| 1635 |
const uint32_t mid_set_ct = PopcountWord(tmp_mid_read_word); |
| 1636 |
uintptr_t tmp_compact_read_word; |
| 1637 |
if (compact_idx_lowbits != kBitsPerWord) { |
| 1638 |
const uint32_t compact_idx_lowbits_end = compact_idx_lowbits + mid_set_ct; |
| 1639 |
tmp_compact_read_word = compact_read_word >> compact_idx_lowbits; |
| 1640 |
// avoid reading off end of compact_bitarr here |
| 1641 |
if (compact_idx_lowbits_end <= kBitsPerWord) { |
| 1642 |
compact_idx_lowbits = compact_idx_lowbits_end; |
| 1643 |
} else { |
| 1644 |
CopyFromUnalignedIncrW(&compact_read_word, &compact_bitarr_biter); |
| 1645 |
tmp_compact_read_word |= compact_read_word << (kBitsPerWord - compact_idx_lowbits); |
| 1646 |
compact_idx_lowbits = compact_idx_lowbits_end - kBitsPerWord; |
| 1647 |
} |
| 1648 |
} else { |
| 1649 |
// special case, can't right-shift 64 |
| 1650 |
CopyFromUnalignedIncrW(&compact_read_word, &compact_bitarr_biter); |
| 1651 |
compact_idx_lowbits = mid_set_ct; |
| 1652 |
tmp_compact_read_word = compact_read_word; |
| 1653 |
} |
| 1654 |
tmp_compact_read_word = bzhi_max(tmp_compact_read_word, mid_set_ct); |
| 1655 |
|
| 1656 |
uintptr_t cur_masked_top = subset_word & top_word; |
| 1657 |
while (cur_masked_top) { |
| 1658 |
const uintptr_t cur_masked_top_and_arg = cur_masked_top - k1LU; |
| 1659 |
const uintptr_t lowmask = (cur_masked_top ^ cur_masked_top_and_arg) >> 1; |
| 1660 |
const uint32_t read_idx_offset = PopcountWord(top_word & lowmask); |
| 1661 |
uintptr_t shifted_mid_read_word = tmp_mid_read_word >> read_idx_offset; |
| 1662 |
if (shifted_mid_read_word & 1) { |
| 1663 |
// bugfix (7 Sep 2017): forgot the "k1LU << " part of this |
| 1664 |
const uintptr_t cur_bit = k1LU << PopcountWord(subset_word & lowmask); |
| 1665 |
tmp_mid_write_word |= cur_bit; |
| 1666 |
tmp_compact_write_word += cur_bit * ((tmp_compact_read_word >> (mid_set_ct - PopcountWord(shifted_mid_read_word))) & 1); |
| 1667 |
if (shifted_mid_read_word == 1) { |
| 1668 |
break; |
| 1669 |
} |
| 1670 |
} |
| 1671 |
cur_masked_top &= cur_masked_top_and_arg; |
| 1672 |
} |
| 1673 |
} |
| 1674 |
mid_write_word |= tmp_mid_write_word << write_idx_lowbits; |
| 1675 |
compact_write_word |= tmp_compact_write_word << write_idx_lowbits; |
| 1676 |
} |
| 1677 |
const uint32_t write_idx_lowbits_end = write_idx_lowbits + PopcountWord(subset_word); |
| 1678 |
if (write_idx_lowbits_end >= kBitsPerWord) { |
| 1679 |
mid_target[write_widx] = mid_write_word; |
| 1680 |
compact_target[write_widx] = compact_write_word; |
| 1681 |
++write_widx; |
| 1682 |
if (write_idx_lowbits) { |
| 1683 |
const uint32_t rshift = kBitsPerWord - write_idx_lowbits; |
| 1684 |
mid_write_word = tmp_mid_write_word >> rshift; |
| 1685 |
compact_write_word = tmp_compact_write_word >> rshift; |
| 1686 |
} else { |
| 1687 |
mid_write_word = 0; |
| 1688 |
compact_write_word = 0; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
write_idx_lowbits = write_idx_lowbits_end % kBitsPerWord; |
| 1692 |
} |
| 1693 |
if (write_idx_lowbits) { |
| 1694 |
mid_target[write_widx] = mid_write_word; |
| 1695 |
compact_target[write_widx] = compact_write_word; |
| 1696 |
} |
| 1697 |
} |
| 1698 |
#endif |
| 1699 |
uintptr_t PopcountBytes(const void* bitarr, uintptr_t byte_ct) { |
| 1700 |
const unsigned char* bitarr_uc = S_CAST(const unsigned char*, bitarr); |
| 1701 |
const uintptr_t* bitvec; |
| 1702 |
const uint32_t lead_byte_ct = AlignKToAW(bitarr_uc, &bitvec); |
| 1703 |
uintptr_t tot = 0; |
| 1704 |
uint32_t trail_byte_ct; |
| 1705 |
// bugfix: had wrong condition here |
| 1706 |
if (byte_ct >= lead_byte_ct) { |
| 1707 |
#ifdef USE_SSE2 |
| 1708 |
const uint32_t word_rem = lead_byte_ct % kBytesPerWord; |
| 1709 |
if (word_rem) { |
| 1710 |
tot = PopcountWord(ProperSubwordLoad(bitarr_uc, word_rem)); |
| 1711 |
} |
| 1712 |
if (lead_byte_ct >= kBytesPerWord) { |
| 1713 |
tot += PopcountWord(bitvec[-1]); |
| 1714 |
# ifdef USE_AVX2 |
| 1715 |
if (lead_byte_ct >= 2 * kBytesPerWord) { |
| 1716 |
tot += PopcountWord(bitvec[-2]); |
| 1717 |
if (lead_byte_ct >= 3 * kBytesPerWord) { |
| 1718 |
tot += PopcountWord(bitvec[-3]); |
| 1719 |
} |
| 1720 |
} |
| 1721 |
# endif |
| 1722 |
} |
| 1723 |
#else |
| 1724 |
if (lead_byte_ct) { |
| 1725 |
tot = PopcountWord(ProperSubwordLoad(bitarr_uc, lead_byte_ct)); |
| 1726 |
} |
| 1727 |
#endif |
| 1728 |
byte_ct -= lead_byte_ct; |
| 1729 |
const uintptr_t word_ct = byte_ct / kBytesPerWord; |
| 1730 |
// vec-alignment required here |
| 1731 |
tot += PopcountWords(bitvec, word_ct); |
| 1732 |
bitarr_uc = DowncastKToUc(&(bitvec[word_ct])); |
| 1733 |
trail_byte_ct = byte_ct % kBytesPerWord; |
| 1734 |
} else { |
| 1735 |
// this may still be >= kBytesPerWord in USE_SSE2 case, so can't remove |
| 1736 |
// loop |
| 1737 |
trail_byte_ct = byte_ct; |
| 1738 |
} |
| 1739 |
for (uint32_t bytes_remaining = trail_byte_ct; ; ) { |
| 1740 |
uintptr_t cur_word; |
| 1741 |
if (bytes_remaining < kBytesPerWord) { |
| 1742 |
if (!bytes_remaining) { |
| 1743 |
return tot; |
| 1744 |
} |
| 1745 |
cur_word = ProperSubwordLoad(bitarr_uc, bytes_remaining); |
| 1746 |
bytes_remaining = 0; |
| 1747 |
} else { |
| 1748 |
CopyFromUnalignedIncrW(&cur_word, &bitarr_uc); |
| 1749 |
bytes_remaining -= kBytesPerWord; |
| 1750 |
} |
| 1751 |
tot += PopcountWord(cur_word); |
| 1752 |
} |
| 1753 |
} |
| 1754 |
|
| 1755 |
uintptr_t PopcountBytesMasked(const void* bitarr, const uintptr_t* mask_arr, uintptr_t byte_ct) { |
| 1756 |
// todo: try modifying PopcountWordsIntersect() to use unaligned load |
| 1757 |
// instructions; then, if there is no performance penalty, try modifying this |
| 1758 |
// main loop to call it. |
| 1759 |
const uintptr_t word_ct = byte_ct / kBytesPerWord; |
| 1760 |
#ifdef USE_SSE42 |
| 1761 |
const unsigned char* bitarr_uc = S_CAST(const unsigned char*, bitarr); |
| 1762 |
uintptr_t tot = 0; |
| 1763 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 1764 |
uintptr_t cur_word; |
| 1765 |
CopyFromUnalignedOffsetW(&cur_word, bitarr_uc, widx); |
| 1766 |
tot += PopcountWord(cur_word & mask_arr[widx]); |
| 1767 |
} |
| 1768 |
const uint32_t trail_byte_ct = byte_ct % kBytesPerWord; |
| 1769 |
if (trail_byte_ct) { |
| 1770 |
const uintptr_t cur_word = ProperSubwordLoad(&(bitarr_uc[word_ct * kBytesPerWord]), trail_byte_ct); |
| 1771 |
tot += PopcountWord(cur_word & mask_arr[word_ct]); |
| 1772 |
} |
| 1773 |
return tot; |
| 1774 |
#else |
| 1775 |
const unsigned char* bitarr_biter = S_CAST(const unsigned char*, bitarr); |
| 1776 |
const uintptr_t mainblock_word_ct = word_ct - (word_ct % (24 / kBytesPerWord)); |
| 1777 |
const unsigned char* bitarr_24b_end = &(bitarr_biter[mainblock_word_ct * kBytesPerWord]); |
| 1778 |
const uintptr_t* mask_arr_iter = mask_arr; |
| 1779 |
uintptr_t tot = 0; |
| 1780 |
while (bitarr_biter < bitarr_24b_end) { |
| 1781 |
uintptr_t loader; |
| 1782 |
CopyFromUnalignedIncrW(&loader, &bitarr_biter); |
| 1783 |
loader &= (*mask_arr_iter++); |
| 1784 |
uintptr_t ulii = loader - ((loader >> 1) & kMask5555); |
| 1785 |
CopyFromUnalignedIncrW(&loader, &bitarr_biter); |
| 1786 |
loader &= (*mask_arr_iter++); |
| 1787 |
uintptr_t uljj = loader - ((loader >> 1) & kMask5555); |
| 1788 |
CopyFromUnalignedIncrW(&loader, &bitarr_biter); |
| 1789 |
loader &= (*mask_arr_iter++); |
| 1790 |
ulii += (loader >> 1) & kMask5555; |
| 1791 |
uljj += loader & kMask5555; |
| 1792 |
ulii = (ulii & kMask3333) + ((ulii >> 2) & kMask3333); |
| 1793 |
ulii += (uljj & kMask3333) + ((uljj >> 2) & kMask3333); |
| 1794 |
uintptr_t tmp_stor = (ulii & kMask0F0F) + ((ulii >> 4) & kMask0F0F); |
| 1795 |
|
| 1796 |
# ifndef __LP64__ |
| 1797 |
CopyFromUnalignedIncrW(&loader, &bitarr_biter); |
| 1798 |
loader &= (*mask_arr_iter++); |
| 1799 |
ulii = loader - ((loader >> 1) & kMask5555); |
| 1800 |
CopyFromUnalignedIncrW(&loader, &bitarr_biter); |
| 1801 |
loader &= (*mask_arr_iter++); |
| 1802 |
uljj = loader - ((loader >> 1) & kMask5555); |
| 1803 |
CopyFromUnalignedIncrW(&loader, &bitarr_biter); |
| 1804 |
loader &= (*mask_arr_iter++); |
| 1805 |
ulii += (loader >> 1) & kMask5555; |
| 1806 |
uljj += loader & kMask5555; |
| 1807 |
ulii = (ulii & kMask3333) + ((ulii >> 2) & kMask3333); |
| 1808 |
ulii += (uljj & kMask3333) + ((uljj >> 2) & kMask3333); |
| 1809 |
tmp_stor += (ulii & kMask0F0F) + ((ulii >> 4) & kMask0F0F); |
| 1810 |
# endif |
| 1811 |
|
| 1812 |
// 32-bit case: each 8-bit slot stores a number in 0..48. Multiplying by |
| 1813 |
// 0x01010101 is equivalent to the left-shifts and adds we need to sum |
| 1814 |
// those four 8-bit numbers in the high-order slot. |
| 1815 |
// 64-bit case: each 8-bit slot stores a number in 0..24. |
| 1816 |
tot += (tmp_stor * kMask0101) >> (kBitsPerWord - 8); |
| 1817 |
} |
| 1818 |
for (uint32_t trail_byte_ct = byte_ct - (mainblock_word_ct * kBytesPerWord); ; ) { |
| 1819 |
uintptr_t cur_word; |
| 1820 |
if (trail_byte_ct < kBytesPerWord) { |
| 1821 |
if (!trail_byte_ct) { |
| 1822 |
return tot; |
| 1823 |
} |
| 1824 |
cur_word = ProperSubwordLoad(bitarr_biter, trail_byte_ct); |
| 1825 |
trail_byte_ct = 0; |
| 1826 |
} else { |
| 1827 |
CopyFromUnalignedIncrW(&cur_word, &bitarr_biter); |
| 1828 |
trail_byte_ct -= kBytesPerWord; |
| 1829 |
} |
| 1830 |
tot += PopcountWord(cur_word & (*mask_arr_iter++)); |
| 1831 |
} |
| 1832 |
#endif |
| 1833 |
} |
| 1834 |
|
| 1835 |
uintptr_t PopcountBitRange(const uintptr_t* bitvec, uintptr_t start_idx, uintptr_t end_idx) { |
| 1836 |
uintptr_t start_idxl = start_idx / kBitsPerWord; |
| 1837 |
const uintptr_t start_idxlr = start_idx & (kBitsPerWord - 1); |
| 1838 |
const uintptr_t end_idxl = end_idx / kBitsPerWord; |
| 1839 |
const uintptr_t end_idxlr = end_idx & (kBitsPerWord - 1); |
| 1840 |
uintptr_t ct = 0; |
| 1841 |
if (start_idxl == end_idxl) { |
| 1842 |
return PopcountWord(bitvec[start_idxl] & ((k1LU << end_idxlr) - (k1LU << start_idxlr))); |
| 1843 |
} |
| 1844 |
if (start_idxlr) { |
| 1845 |
ct = PopcountWord(bitvec[start_idxl++] >> start_idxlr); |
| 1846 |
} |
| 1847 |
if (end_idxl > start_idxl) { |
| 1848 |
ct += PopcountWordsNzbase(bitvec, start_idxl, end_idxl); |
| 1849 |
} |
| 1850 |
if (end_idxlr) { |
| 1851 |
ct += PopcountWord(bzhi(bitvec[end_idxl], end_idxlr)); |
| 1852 |
} |
| 1853 |
return ct; |
| 1854 |
} |
| 1855 |
|
| 1856 |
void FillCumulativePopcounts(const uintptr_t* subset_mask, uint32_t word_ct, uint32_t* cumulative_popcounts) { |
| 1857 |
assert(word_ct); |
| 1858 |
const uint32_t word_ct_m1 = word_ct - 1; |
| 1859 |
uint32_t cur_sum = 0; |
| 1860 |
for (uint32_t widx = 0; widx != word_ct_m1; ++widx) { |
| 1861 |
cumulative_popcounts[widx] = cur_sum; |
| 1862 |
cur_sum += PopcountWord(subset_mask[widx]); |
| 1863 |
} |
| 1864 |
cumulative_popcounts[word_ct_m1] = cur_sum; |
| 1865 |
} |
| 1866 |
|
| 1867 |
void FillCumulativePopcountsW(const uintptr_t* subset_mask, uintptr_t word_ct, uintptr_t* cumulative_popcounts_w) { |
| 1868 |
assert(word_ct); |
| 1869 |
const uintptr_t word_ct_m1 = word_ct - 1; |
| 1870 |
uintptr_t cur_sum = 0; |
| 1871 |
for (uintptr_t widx = 0; widx != word_ct_m1; ++widx) { |
| 1872 |
cumulative_popcounts_w[widx] = cur_sum; |
| 1873 |
cur_sum += PopcountWord(subset_mask[widx]); |
| 1874 |
} |
| 1875 |
cumulative_popcounts_w[word_ct_m1] = cur_sum; |
| 1876 |
} |
| 1877 |
|
| 1878 |
void UidxsToIdxs(const uintptr_t* subset_mask, const uint32_t* subset_cumulative_popcounts, const uintptr_t idx_list_len, uint32_t* idx_list) { |
| 1879 |
uint32_t* idx_list_end = &(idx_list[idx_list_len]); |
| 1880 |
for (uint32_t* idx_list_iter = idx_list; idx_list_iter != idx_list_end; ++idx_list_iter) { |
| 1881 |
*idx_list_iter = RawToSubsettedPos(subset_mask, subset_cumulative_popcounts, *idx_list_iter); |
| 1882 |
} |
| 1883 |
} |
| 1884 |
|
| 1885 |
void Expand1bitTo8(const void* __restrict bytearr, uint32_t input_bit_ct, uint32_t incr, void* __restrict dst) { |
| 1886 |
const unsigned char* bytearr_uc = S_CAST(const unsigned char*, bytearr); |
| 1887 |
const uint32_t input_bit_ct_plus = input_bit_ct + kBytesPerWord - 1; |
| 1888 |
#if defined(USE_SHUFFLE8) && !defined(NO_UNALIGNED) |
| 1889 |
const uint32_t input_byte_ct = input_bit_ct_plus / 8; |
| 1890 |
const uint32_t fullvec_ct = input_byte_ct / (kBytesPerVec / 8); |
| 1891 |
uint32_t byte_idx = 0; |
| 1892 |
if (fullvec_ct) { |
| 1893 |
const Vec8thUint* bytearr_alias = R_CAST(const Vec8thUint*, bytearr_uc); |
| 1894 |
# ifdef USE_AVX2 |
| 1895 |
const VecUc byte_gather = VecToUc(_mm256_setr_epi64x(0, kMask0101, 2 * kMask0101, 3 * kMask0101)); |
| 1896 |
const VecUc bit_mask = VecToUc(_mm256_set1_epi64x(0x7fbfdfeff7fbfdfeLL)); |
| 1897 |
# else |
| 1898 |
const VecUc byte_gather = VecToUc(_mm_setr_epi32(0, 0, 0x01010101, 0x01010101)); |
| 1899 |
const VecUc bit_mask = VecToUc(_mm_set1_epi64x(0x7fbfdfeff7fbfdfeLL)); |
| 1900 |
# endif |
| 1901 |
const VecUc all1 = vecuc_set1(255); |
| 1902 |
const VecUc subfrom = vecuc_set1(incr); |
| 1903 |
VecUc* dst_alias = S_CAST(VecUc*, dst); |
| 1904 |
for (uint32_t vec_idx = 0; vec_idx != fullvec_ct; ++vec_idx) { |
| 1905 |
# ifdef USE_AVX2 |
| 1906 |
VecUc vmask = VecToUc(_mm256_set1_epi32(bytearr_alias[vec_idx])); |
| 1907 |
# else |
| 1908 |
VecUc vmask = VecToUc(_mm_set1_epi16(bytearr_alias[vec_idx])); |
| 1909 |
# endif |
| 1910 |
vmask = vecuc_shuffle8(vmask, byte_gather); |
| 1911 |
vmask = vmask | bit_mask; |
| 1912 |
vmask = (vmask == all1); |
| 1913 |
const VecUc result = subfrom - vmask; |
| 1914 |
vecuc_storeu(&(dst_alias[vec_idx]), result); |
| 1915 |
} |
| 1916 |
byte_idx = fullvec_ct * (kBytesPerVec / 8); |
| 1917 |
} |
| 1918 |
const uintptr_t incr_word = incr * kMask0101; |
| 1919 |
uintptr_t* dst_w = S_CAST(uintptr_t*, dst); |
| 1920 |
for (; byte_idx != input_byte_ct; ++byte_idx) { |
| 1921 |
const uintptr_t input_byte = bytearr_uc[byte_idx]; |
| 1922 |
# ifdef USE_AVX2 |
| 1923 |
const uintptr_t input_byte_scatter = _pdep_u64(input_byte, kMask0101); |
| 1924 |
# else |
| 1925 |
const uintptr_t input_byte_scatter = (((input_byte & 0xfe) * 0x2040810204080LLU) & kMask0101) | (input_byte & 1); |
| 1926 |
# endif |
| 1927 |
dst_w[byte_idx] = incr_word + input_byte_scatter; |
| 1928 |
} |
| 1929 |
#else // NO_UNALIGNED || (!USE_SSE42) |
| 1930 |
const uintptr_t incr_word = incr * kMask0101; |
| 1931 |
uintptr_t* dst_w = S_CAST(uintptr_t*, dst); |
| 1932 |
# ifdef __LP64__ |
| 1933 |
const uint32_t input_byte_ct = input_bit_ct_plus / 8; |
| 1934 |
for (uint32_t uii = 0; uii != input_byte_ct; ++uii) { |
| 1935 |
// this operation maps binary hgfedcba to h0000000g0000000f... |
| 1936 |
// ^ ^ ^ |
| 1937 |
// | | | |
| 1938 |
// 56 48 40 |
| 1939 |
// 1. (cur_variant_include_word & 0xfe) gives us hgfedcb0; necessary to |
| 1940 |
// avoid carryover. |
| 1941 |
// 2. multiply by the number with bits 7, 14, 21, ..., 49 set, to get |
| 1942 |
// hgfedcbhgfedcbhgf... |
| 1943 |
// ^ ^ ^ |
| 1944 |
// | | | |
| 1945 |
// 56 48 40 |
| 1946 |
// 3. mask out all but bits 8, 16, 24, ..., 56 |
| 1947 |
// todo: test if this actually beats the per-character loop... |
| 1948 |
const uintptr_t input_byte = bytearr_uc[uii]; |
| 1949 |
const uintptr_t input_byte_scatter = (((input_byte & 0xfe) * 0x2040810204080LLU) & kMask0101) | (input_byte & 1); |
| 1950 |
dst_w[uii] = incr_word + input_byte_scatter; |
| 1951 |
} |
| 1952 |
# else |
| 1953 |
const uint32_t fullbyte_ct = input_bit_ct_plus / 8; |
| 1954 |
for (uint32_t uii = 0; uii != fullbyte_ct; ++uii) { |
| 1955 |
// dcba -> d0000000c0000000b0000000a |
| 1956 |
const uintptr_t input_byte = bytearr_uc[uii]; |
| 1957 |
uintptr_t input_byte_scatter = ((input_byte & 0xf) * 0x204081) & kMask0101; |
| 1958 |
dst_w[2 * uii] = incr_word + input_byte_scatter; |
| 1959 |
input_byte_scatter = ((input_byte >> 4) * 0x204081) & kMask0101; |
| 1960 |
dst_w[2 * uii + 1] = incr_word + input_byte_scatter; |
| 1961 |
} |
| 1962 |
if (input_bit_ct_plus & 4) { |
| 1963 |
uintptr_t input_byte = bytearr_uc[fullbyte_ct]; |
| 1964 |
// input_bit_ct mod 8 in 1..4, so high bits zeroed out |
| 1965 |
uintptr_t input_byte_scatter = (input_byte * 0x204081) & kMask0101; |
| 1966 |
dst_w[2 * fullbyte_ct] = incr_word + input_byte_scatter; |
| 1967 |
} |
| 1968 |
# endif |
| 1969 |
#endif |
| 1970 |
} |
| 1971 |
|
| 1972 |
void Expand1bitTo16(const void* __restrict bytearr, uint32_t input_bit_ct, uint32_t incr, void* __restrict dst) { |
| 1973 |
const unsigned char* bytearr_uc = S_CAST(const unsigned char*, bytearr); |
| 1974 |
#if defined(USE_SHUFFLE8) && (!(defined(USE_AVX2) && defined(NO_UNALIGNED))) |
| 1975 |
const uint32_t input_nybble_ct = DivUp(input_bit_ct, 4); |
| 1976 |
const uint32_t fullvec_ct = input_nybble_ct / (kBytesPerVec / 8); |
| 1977 |
uint32_t byte_idx = 0; |
| 1978 |
if (fullvec_ct) { |
| 1979 |
const Vec16thUint* bytearr_alias = R_CAST(const Vec16thUint*, bytearr_uc); |
| 1980 |
# ifdef USE_AVX2 |
| 1981 |
const VecU16 byte_gather = VecToU16(_mm256_setr_epi64x(0, 0, kMask0101, kMask0101)); |
| 1982 |
const VecU16 bit_mask = VecToU16(_mm256_set_epi32(0xff7fffbfU, 0xffdfffefU, 0xfff7fffbU, 0xfffdfffeU, 0xff7fffbfU, 0xffdfffefU, 0xfff7fffbU, 0xfffdfffeU)); |
| 1983 |
# else |
| 1984 |
const VecU16 bit_mask = VecToU16(_mm_set_epi32(0xff7fffbfU, 0xffdfffefU, 0xfff7fffbU, 0xfffdfffeU)); |
| 1985 |
# endif |
| 1986 |
const VecU16 all1 = VCONST_S(0xffff); |
| 1987 |
const VecU16 subfrom = vecu16_set1(incr); |
| 1988 |
VecU16* dst_alias = S_CAST(VecU16*, dst); |
| 1989 |
// todo: check whether this is actually any better than the non-vectorized |
| 1990 |
// loop |
| 1991 |
for (uint32_t vec_idx = 0; vec_idx != fullvec_ct; ++vec_idx) { |
| 1992 |
# ifdef USE_AVX2 |
| 1993 |
VecU16 vmask = VecToU16(_mm256_set1_epi16(bytearr_alias[vec_idx])); |
| 1994 |
vmask = vecu16_shuffle8(vmask, byte_gather); |
| 1995 |
# else |
| 1996 |
VecU16 vmask = VecToU16(_mm_set1_epi8(bytearr_alias[vec_idx])); |
| 1997 |
# endif |
| 1998 |
vmask = vmask | bit_mask; |
| 1999 |
vmask = (vmask == all1); |
| 2000 |
const VecU16 result = subfrom - vmask; |
| 2001 |
vecu16_storeu(&(dst_alias[vec_idx]), result); |
| 2002 |
} |
| 2003 |
byte_idx = fullvec_ct * (kBytesPerVec / 16); |
| 2004 |
} |
| 2005 |
const uintptr_t incr_word = incr * kMask0001; |
| 2006 |
const uint32_t fullbyte_ct = input_nybble_ct / 2; |
| 2007 |
uintptr_t* dst_w = S_CAST(uintptr_t*, dst); |
| 2008 |
for (; byte_idx != fullbyte_ct; ++byte_idx) { |
| 2009 |
const uintptr_t input_byte = bytearr_uc[byte_idx]; |
| 2010 |
const uintptr_t input_byte_scatter = input_byte * 0x200040008001LLU; |
| 2011 |
const uintptr_t write0 = input_byte_scatter & kMask0001; |
| 2012 |
const uintptr_t write1 = (input_byte_scatter >> 4) & kMask0001; |
| 2013 |
dst_w[2 * byte_idx] = incr_word + write0; |
| 2014 |
dst_w[2 * byte_idx + 1] = incr_word + write1; |
| 2015 |
} |
| 2016 |
if (input_nybble_ct % 2) { |
| 2017 |
const uintptr_t input_byte = bytearr_uc[byte_idx]; |
| 2018 |
const uintptr_t write0 = (input_byte * 0x200040008001LLU) & kMask0001; |
| 2019 |
dst_w[input_nybble_ct - 1] = incr_word + write0; |
| 2020 |
} |
| 2021 |
#else // (!USE_SHUFFLE8) || (NO_UNALIGNED && USE_AVX2) |
| 2022 |
const uintptr_t incr_word = incr * kMask0001; |
| 2023 |
uintptr_t* dst_w = S_CAST(uintptr_t*, dst); |
| 2024 |
# ifdef __LP64__ |
| 2025 |
const uint32_t input_nybble_ct = DivUp(input_bit_ct, 4); |
| 2026 |
const uint32_t fullbyte_ct = input_nybble_ct / 2; |
| 2027 |
for (uint32_t uii = 0; uii != fullbyte_ct; ++uii) { |
| 2028 |
const uintptr_t input_byte = bytearr_uc[uii]; |
| 2029 |
const uintptr_t input_byte_scatter = input_byte * 0x200040008001LLU; |
| 2030 |
const uintptr_t write0 = input_byte_scatter & kMask0001; |
| 2031 |
const uintptr_t write1 = (input_byte_scatter >> 4) & kMask0001; |
| 2032 |
dst_w[2 * uii] = incr_word + write0; |
| 2033 |
dst_w[2 * uii + 1] = incr_word + write1; |
| 2034 |
} |
| 2035 |
if (input_nybble_ct % 2) { |
| 2036 |
const uintptr_t input_byte = bytearr_uc[fullbyte_ct]; |
| 2037 |
const uintptr_t write0 = (input_byte * 0x200040008001LLU) & kMask0001; |
| 2038 |
dst_w[input_nybble_ct - 1] = incr_word + write0; |
| 2039 |
} |
| 2040 |
# else // !__LP64__ |
| 2041 |
const uint32_t fullbyte_ct = input_bit_ct / 8; |
| 2042 |
for (uint32_t uii = 0; uii != fullbyte_ct; ++uii) { |
| 2043 |
uintptr_t input_byte = bytearr_uc[uii]; |
| 2044 |
const uintptr_t input_byte_scatter = input_byte * 0x8001; |
| 2045 |
dst_w[4 * uii] = (input_byte_scatter & kMask0001) + incr_word; |
| 2046 |
dst_w[4 * uii + 1] = ((input_byte_scatter >> 2) & kMask0001) + incr_word; |
| 2047 |
dst_w[4 * uii + 2] = ((input_byte_scatter >> 4) & kMask0001) + incr_word; |
| 2048 |
dst_w[4 * uii + 3] = ((input_byte_scatter >> 6) & kMask0001) + incr_word; |
| 2049 |
} |
| 2050 |
const uint32_t remainder = input_bit_ct % 8; |
| 2051 |
if (remainder) { |
| 2052 |
uintptr_t input_byte = bytearr_uc[fullbyte_ct]; |
| 2053 |
uint16_t* dst_u16 = S_CAST(uint16_t*, dst); |
| 2054 |
uint16_t* dst_u16_last = &(dst_u16[8 * fullbyte_ct]); |
| 2055 |
for (uint32_t uii = 0; uii < remainder; ++uii) { |
| 2056 |
dst_u16_last[uii] = (input_byte & 1) + incr; |
| 2057 |
input_byte = input_byte >> 1; |
| 2058 |
} |
| 2059 |
} |
| 2060 |
# endif |
| 2061 |
#endif |
| 2062 |
} |
| 2063 |
|
| 2064 |
#ifdef USE_SSE2 |
| 2065 |
static_assert(kPglBitTransposeBatch == S_CAST(uint32_t, kBitsPerCacheline), "TransposeBitblock64() needs to be updated."); |
| 2066 |
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) { |
| 2067 |
// We need to perform the equivalent of 9-10 shuffles (assuming a full-size |
| 2068 |
// 512x512 or 1024x1024 bitblock). |
| 2069 |
// The first shuffles are performed by the ingestion loop: we write the first |
| 2070 |
// word from every row to buf0, then the second word from every row, etc., |
| 2071 |
// yielding |
| 2072 |
// (0,0) ... (0,63) (1,0) ... (1,63) (2,0) ... (511,63) |
| 2073 |
// (0,64) ... (0,127) (1,64) ... (1,127) (2,64) ... (511,127) |
| 2074 |
// ... |
| 2075 |
// (0,448) ... (0,511) (1,448) ... (1,511) (2,448) ... (511,511) |
| 2076 |
// in terms of the original bit positions when kCacheline==64. |
| 2077 |
// Since each input row has 8-16 words, this amounts to 3-4 shuffles. |
| 2078 |
// |
| 2079 |
// The second step writes |
| 2080 |
// (0,0) (0,1) ... (0,7) (1,0) (1,1) ... (1,7) ... (511,7) |
| 2081 |
// (0,8) (0,9) ... (0,15) (1,8) (1,9) ... (1,15) ... (511,15) |
| 2082 |
// ... |
| 2083 |
// (0,504) ... (0,511) (1,504) ... (1,511) ... (511,511) |
| 2084 |
// to buf1, performing the equivalent of 3 shuffles, and the third step |
| 2085 |
// finishes the transpose using movemask. |
| 2086 |
// |
| 2087 |
// buf0 and buf1 must both be 32KiB vector-aligned buffers when |
| 2088 |
// kCacheline==64, and 128KiB when kCacheline==128. |
| 2089 |
|
| 2090 |
// todo: better ARM implementation |
| 2091 |
const uint32_t buf0_row_ct = DivUp(write_row_ct, 64); |
| 2092 |
{ |
| 2093 |
uintptr_t* buf0_ul = DowncastVecWToW(buf0); |
| 2094 |
const uint32_t zfill_ct = (-read_row_ct) & (kBitsPerWord - 1); |
| 2095 |
for (uint32_t ridx = 0; ridx != buf0_row_ct; ++ridx) { |
| 2096 |
const uintptr_t* read_iter_tmp = &(read_iter[ridx]); |
| 2097 |
uintptr_t* buf0_row_start = &(buf0_ul[kPglBitTransposeBatch * ridx]); |
| 2098 |
for (uint32_t uii = 0; uii != read_row_ct; ++uii) { |
| 2099 |
buf0_row_start[uii] = *read_iter_tmp; |
| 2100 |
read_iter_tmp = &(read_iter_tmp[read_ul_stride]); |
| 2101 |
} |
| 2102 |
// This is a simple way of fulfilling the trailing-zero part of the |
| 2103 |
// function contract. |
| 2104 |
// ( buf0 rows zeroed out to 512 bytes |
| 2105 |
// -> buf1 rows zeroed out to 64 bytes |
| 2106 |
// -> output rows zeroed out to 8 bytes) |
| 2107 |
ZeroWArr(zfill_ct, &(buf0_row_start[read_row_ct])); |
| 2108 |
} |
| 2109 |
} |
| 2110 |
const uint32_t write_word_width = DivUp(read_row_ct, 64); |
| 2111 |
{ |
| 2112 |
const VecW* buf0_read_iter = buf0; |
| 2113 |
uintptr_t* write_iter0 = DowncastVecWToW(buf1); |
| 2114 |
# ifdef USE_SHUFFLE8 |
| 2115 |
const VecW gather_u16s = vecw_setr8(0, 8, 1, 9, 2, 10, 3, 11, |
| 2116 |
4, 12, 5, 13, 6, 14, 7, 15); |
| 2117 |
# ifdef USE_AVX2 |
| 2118 |
const VecW gather_u32s = vecw_setr8(0, 1, 8, 9, 2, 3, 10, 11, |
| 2119 |
4, 5, 12, 13, 6, 7, 14, 15); |
| 2120 |
# endif |
| 2121 |
# else |
| 2122 |
const VecW m8 = VCONST_W(kMask00FF); |
| 2123 |
# endif |
| 2124 |
const uint32_t buf0_row_b64width = write_word_width * 8; |
| 2125 |
for (uint32_t ridx = 0; ridx != buf0_row_ct; ++ridx) { |
| 2126 |
uintptr_t* write_iter1 = &(write_iter0[kCacheline]); |
| 2127 |
uintptr_t* write_iter2 = &(write_iter1[kCacheline]); |
| 2128 |
uintptr_t* write_iter3 = &(write_iter2[kCacheline]); |
| 2129 |
uintptr_t* write_iter4 = &(write_iter3[kCacheline]); |
| 2130 |
uintptr_t* write_iter5 = &(write_iter4[kCacheline]); |
| 2131 |
uintptr_t* write_iter6 = &(write_iter5[kCacheline]); |
| 2132 |
uintptr_t* write_iter7 = &(write_iter6[kCacheline]); |
| 2133 |
for (uint32_t b64idx = 0; b64idx != buf0_row_b64width; ++b64idx) { |
| 2134 |
# ifdef USE_AVX2 |
| 2135 |
VecW loader0 = buf0_read_iter[b64idx * 2]; |
| 2136 |
VecW loader1 = buf0_read_iter[b64idx * 2 + 1]; |
| 2137 |
// (0,0) (0,1) ... (0,7) (1,0) (1,1) ... (1,7) (2,0) ... (3,7) |
| 2138 |
// -> (0,0) (1,0) (0,1) (1,1) (0,2) .... (1,7) (2,0) (3,0) (2,1) ... |
| 2139 |
loader0 = vecw_shuffle8(loader0, gather_u16s); |
| 2140 |
loader1 = vecw_shuffle8(loader1, gather_u16s); |
| 2141 |
// -> (0,0) (1,0) (0,1) (1,1) (0,2) (1,2) (0,3) (1,3) (2,0) (3,0) ... |
| 2142 |
VecW vec_lo = vecw_permute0xd8_if_avx2(loader0); |
| 2143 |
VecW vec_hi = vecw_permute0xd8_if_avx2(loader1); |
| 2144 |
// -> (0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) ... |
| 2145 |
vec_lo = vecw_shuffle8(vec_lo, gather_u32s); |
| 2146 |
// -> (4,0) (5,0) (6,0) (7,0) (4,1) (5,1) (6,1) (7,1) (4,2) ... |
| 2147 |
vec_hi = vecw_shuffle8(vec_hi, gather_u32s); |
| 2148 |
const VecW final0145 = vecw_unpacklo32(vec_lo, vec_hi); |
| 2149 |
const VecW final2367 = vecw_unpackhi32(vec_lo, vec_hi); |
| 2150 |
write_iter0[b64idx] = vecw_extract64_0(final0145); |
| 2151 |
write_iter1[b64idx] = vecw_extract64_1(final0145); |
| 2152 |
write_iter2[b64idx] = vecw_extract64_0(final2367); |
| 2153 |
write_iter3[b64idx] = vecw_extract64_1(final2367); |
| 2154 |
write_iter4[b64idx] = vecw_extract64_2(final0145); |
| 2155 |
write_iter5[b64idx] = vecw_extract64_3(final0145); |
| 2156 |
write_iter6[b64idx] = vecw_extract64_2(final2367); |
| 2157 |
write_iter7[b64idx] = vecw_extract64_3(final2367); |
| 2158 |
# else // !USE_AVX2 |
| 2159 |
VecW loader0 = buf0_read_iter[b64idx * 4]; |
| 2160 |
VecW loader1 = buf0_read_iter[b64idx * 4 + 1]; |
| 2161 |
VecW loader2 = buf0_read_iter[b64idx * 4 + 2]; |
| 2162 |
VecW loader3 = buf0_read_iter[b64idx * 4 + 3]; |
| 2163 |
// (0,0) (0,1) ... (0,7) (1,0) (1,1) ... (1,7) |
| 2164 |
// -> (0,0) (1,0) (0,1) (1,1) (0,2) ... (1,7) |
| 2165 |
# ifdef USE_SHUFFLE8 |
| 2166 |
loader0 = vecw_shuffle8(loader0, gather_u16s); |
| 2167 |
loader1 = vecw_shuffle8(loader1, gather_u16s); |
| 2168 |
loader2 = vecw_shuffle8(loader2, gather_u16s); |
| 2169 |
loader3 = vecw_shuffle8(loader3, gather_u16s); |
| 2170 |
# else |
| 2171 |
VecW tmp_lo = vecw_unpacklo8(loader0, loader1); |
| 2172 |
VecW tmp_hi = vecw_unpackhi8(loader0, loader1); |
| 2173 |
loader0 = vecw_blendv(vecw_slli(tmp_hi, 8), tmp_lo, m8); |
| 2174 |
loader1 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 8), m8); |
| 2175 |
tmp_lo = vecw_unpacklo8(loader2, loader3); |
| 2176 |
tmp_hi = vecw_unpackhi8(loader2, loader3); |
| 2177 |
loader2 = vecw_blendv(vecw_slli(tmp_hi, 8), tmp_lo, m8); |
| 2178 |
loader3 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 8), m8); |
| 2179 |
# endif |
| 2180 |
// -> (0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) ... |
| 2181 |
const VecW lo_0123 = vecw_unpacklo16(loader0, loader1); |
| 2182 |
// -> (0,4) (1,4) (2,4) (3,4) (0,5) (1,5) (2,5) (3,5) (0,6) ... |
| 2183 |
const VecW lo_4567 = vecw_unpackhi16(loader0, loader1); |
| 2184 |
const VecW hi_0123 = vecw_unpacklo16(loader2, loader3); |
| 2185 |
const VecW hi_4567 = vecw_unpackhi16(loader2, loader3); |
| 2186 |
|
| 2187 |
VecW final01 = vecw_unpacklo32(lo_0123, hi_0123); |
| 2188 |
VecW final23 = vecw_unpackhi32(lo_0123, hi_0123); |
| 2189 |
VecW final45 = vecw_unpacklo32(lo_4567, hi_4567); |
| 2190 |
VecW final67 = vecw_unpackhi32(lo_4567, hi_4567); |
| 2191 |
write_iter0[b64idx] = vecw_extract64_0(final01); |
| 2192 |
write_iter1[b64idx] = vecw_extract64_1(final01); |
| 2193 |
write_iter2[b64idx] = vecw_extract64_0(final23); |
| 2194 |
write_iter3[b64idx] = vecw_extract64_1(final23); |
| 2195 |
write_iter4[b64idx] = vecw_extract64_0(final45); |
| 2196 |
write_iter5[b64idx] = vecw_extract64_1(final45); |
| 2197 |
write_iter6[b64idx] = vecw_extract64_0(final67); |
| 2198 |
write_iter7[b64idx] = vecw_extract64_1(final67); |
| 2199 |
# endif // !USE_AVX2 |
| 2200 |
} |
| 2201 |
buf0_read_iter = &(buf0_read_iter[kPglBitTransposeBatch / kWordsPerVec]); |
| 2202 |
write_iter0 = &(write_iter7[kCacheline]); |
| 2203 |
} |
| 2204 |
} |
| 2205 |
const VecW* buf1_read_iter = buf1; |
| 2206 |
const uint32_t write_v8ui_stride = kVec8thUintPerWord * write_ul_stride; |
| 2207 |
const uint32_t buf1_fullrow_ct = write_row_ct / 8; |
| 2208 |
const uint32_t buf1_row_vecwidth = write_word_width * (8 / kWordsPerVec); |
| 2209 |
Vec8thUint* write_iter0 = DowncastWToV8(write_iter); |
| 2210 |
for (uint32_t ridx = 0; ridx != buf1_fullrow_ct; ++ridx) { |
| 2211 |
Vec8thUint* write_iter1 = &(write_iter0[write_v8ui_stride]); |
| 2212 |
Vec8thUint* write_iter2 = &(write_iter1[write_v8ui_stride]); |
| 2213 |
Vec8thUint* write_iter3 = &(write_iter2[write_v8ui_stride]); |
| 2214 |
Vec8thUint* write_iter4 = &(write_iter3[write_v8ui_stride]); |
| 2215 |
Vec8thUint* write_iter5 = &(write_iter4[write_v8ui_stride]); |
| 2216 |
Vec8thUint* write_iter6 = &(write_iter5[write_v8ui_stride]); |
| 2217 |
Vec8thUint* write_iter7 = &(write_iter6[write_v8ui_stride]); |
| 2218 |
for (uint32_t vidx = 0; vidx != buf1_row_vecwidth; ++vidx) { |
| 2219 |
VecW loader = buf1_read_iter[vidx]; |
| 2220 |
write_iter7[vidx] = vecw_movemask(loader); |
| 2221 |
loader = vecw_slli(loader, 1); |
| 2222 |
write_iter6[vidx] = vecw_movemask(loader); |
| 2223 |
loader = vecw_slli(loader, 1); |
| 2224 |
write_iter5[vidx] = vecw_movemask(loader); |
| 2225 |
loader = vecw_slli(loader, 1); |
| 2226 |
write_iter4[vidx] = vecw_movemask(loader); |
| 2227 |
loader = vecw_slli(loader, 1); |
| 2228 |
write_iter3[vidx] = vecw_movemask(loader); |
| 2229 |
loader = vecw_slli(loader, 1); |
| 2230 |
write_iter2[vidx] = vecw_movemask(loader); |
| 2231 |
loader = vecw_slli(loader, 1); |
| 2232 |
write_iter1[vidx] = vecw_movemask(loader); |
| 2233 |
loader = vecw_slli(loader, 1); |
| 2234 |
write_iter0[vidx] = vecw_movemask(loader); |
| 2235 |
} |
| 2236 |
buf1_read_iter = &(buf1_read_iter[kCacheline / kWordsPerVec]); |
| 2237 |
write_iter0 = &(write_iter7[write_v8ui_stride]); |
| 2238 |
} |
| 2239 |
const uint32_t row_ct_rem = write_row_ct % 8; |
| 2240 |
if (!row_ct_rem) { |
| 2241 |
return; |
| 2242 |
} |
| 2243 |
const uint32_t lshift = 8 - row_ct_rem; |
| 2244 |
Vec8thUint* write_iter_last = &(write_iter0[write_v8ui_stride * (row_ct_rem - 1)]); |
| 2245 |
for (uint32_t vidx = 0; vidx != buf1_row_vecwidth; ++vidx) { |
| 2246 |
VecW loader = buf1_read_iter[vidx]; |
| 2247 |
loader = vecw_slli_variable_ct(loader, lshift); |
| 2248 |
Vec8thUint* inner_write_iter = &(write_iter_last[vidx]); |
| 2249 |
for (uint32_t uii = 0; uii != row_ct_rem; ++uii) { |
| 2250 |
*inner_write_iter = vecw_movemask(loader); |
| 2251 |
loader = vecw_slli(loader, 1); |
| 2252 |
inner_write_iter -= write_v8ui_stride; |
| 2253 |
} |
| 2254 |
} |
| 2255 |
} |
| 2256 |
#else // !USE_SSE2 |
| 2257 |
# ifdef __LP64__ |
| 2258 |
static_assert(kWordsPerVec == 1, "TransposeBitblock64() needs to be updated."); |
| 2259 |
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) { |
| 2260 |
// buf must be vector-aligned and have size 64k |
| 2261 |
const uint32_t initial_read_byte_ct = DivUp(write_row_ct, CHAR_BIT); |
| 2262 |
// fold the first 6 shuffles into the initial ingestion loop |
| 2263 |
const unsigned char* initial_read_iter = DowncastKToUc(read_iter); |
| 2264 |
const unsigned char* initial_read_end = &(initial_read_iter[initial_read_byte_ct]); |
| 2265 |
unsigned char* initial_target_iter = DowncastToUc(buf0); |
| 2266 |
const uint32_t read_byte_stride = read_ul_stride * kBytesPerWord; |
| 2267 |
const uint32_t read_batch_rem = kBitsPerCacheline - read_row_ct; |
| 2268 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 2269 |
const unsigned char* read_iter_tmp = initial_read_iter; |
| 2270 |
for (uint32_t ujj = 0; ujj != read_row_ct; ++ujj) { |
| 2271 |
*initial_target_iter++ = *read_iter_tmp; |
| 2272 |
read_iter_tmp = &(read_iter_tmp[read_byte_stride]); |
| 2273 |
} |
| 2274 |
initial_target_iter = memsetua(initial_target_iter, 0, read_batch_rem); |
| 2275 |
} |
| 2276 |
|
| 2277 |
// third-to-last shuffle, 8 bit spacing -> 4 |
| 2278 |
const VecW* source_iter = buf0; |
| 2279 |
uintptr_t* target_iter0 = buf1; |
| 2280 |
const uint32_t write_word_ct = BitCtToWordCt(read_row_ct); |
| 2281 |
const uint32_t first_inner_loop_iter_ct = 4 * write_word_ct; |
| 2282 |
uint32_t cur_write_skip = 4 * kWordsPerCacheline - first_inner_loop_iter_ct; |
| 2283 |
// coincidentally, this also needs to run DivUp(write_row_ct, CHAR_BIT) |
| 2284 |
// times |
| 2285 |
for (uint32_t uii = 0; uii != initial_read_byte_ct; ++uii) { |
| 2286 |
uintptr_t* target_iter1 = &(target_iter0[kWordsPerCacheline * 4]); |
| 2287 |
for (uint32_t ujj = 0; ujj != first_inner_loop_iter_ct; ++ujj) { |
| 2288 |
const uintptr_t source_word_lo = *source_iter++; |
| 2289 |
const uintptr_t source_word_hi = *source_iter++; |
| 2290 |
*target_iter0++ = PackTwo0F0FMask(source_word_lo, source_word_hi); |
| 2291 |
*target_iter1++ = PackTwo0F0FMask(source_word_lo >> 4, source_word_hi >> 4); |
| 2292 |
} |
| 2293 |
source_iter = &(source_iter[2 * cur_write_skip]); |
| 2294 |
target_iter0 = &(target_iter1[cur_write_skip]); |
| 2295 |
} |
| 2296 |
|
| 2297 |
// second-to-last shuffle, 4 bit spacing -> 2 |
| 2298 |
source_iter = buf1; |
| 2299 |
target_iter0 = buf0; |
| 2300 |
const uint32_t second_outer_loop_iter_ct = DivUp(write_row_ct, 4); |
| 2301 |
const uint32_t second_inner_loop_iter_ct = 2 * write_word_ct; |
| 2302 |
cur_write_skip = 2 * kWordsPerCacheline - second_inner_loop_iter_ct; |
| 2303 |
for (uint32_t uii = 0; uii != second_outer_loop_iter_ct; ++uii) { |
| 2304 |
uintptr_t* target_iter1 = &(target_iter0[kWordsPerCacheline * 2]); |
| 2305 |
for (uint32_t ujj = 0; ujj != second_inner_loop_iter_ct; ++ujj) { |
| 2306 |
const uintptr_t source_word_lo = *source_iter++; |
| 2307 |
const uintptr_t source_word_hi = *source_iter++; |
| 2308 |
*target_iter0++ = PackTwo3333Mask(source_word_lo, source_word_hi); |
| 2309 |
*target_iter1++ = PackTwo3333Mask(source_word_lo >> 2, source_word_hi >> 2); |
| 2310 |
} |
| 2311 |
source_iter = &(source_iter[2 * cur_write_skip]); |
| 2312 |
target_iter0 = &(target_iter1[cur_write_skip]); |
| 2313 |
} |
| 2314 |
// last shuffle, 2 bit spacing -> 1 |
| 2315 |
source_iter = buf0; |
| 2316 |
target_iter0 = write_iter; |
| 2317 |
const uint32_t last_loop_iter_ct = DivUp(write_row_ct, 2); |
| 2318 |
for (uint32_t uii = 0; uii != last_loop_iter_ct; ++uii) { |
| 2319 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 2320 |
for (uint32_t ujj = 0; ujj != write_word_ct; ++ujj) { |
| 2321 |
const uintptr_t source_word_lo = S_CAST(uintptr_t, *source_iter++); |
| 2322 |
const uintptr_t source_word_hi = S_CAST(uintptr_t, *source_iter++); |
| 2323 |
target_iter0[ujj] = PackTwo5555Mask(source_word_lo, source_word_hi); |
| 2324 |
target_iter1[ujj] = PackTwo5555Mask(source_word_lo >> 1, source_word_hi >> 1); |
| 2325 |
} |
| 2326 |
source_iter = &(source_iter[2 * (kWordsPerCacheline - write_word_ct)]); |
| 2327 |
target_iter0 = &(target_iter1[write_ul_stride]); |
| 2328 |
} |
| 2329 |
} |
| 2330 |
# else |
| 2331 |
static_assert(kWordsPerVec == 1, "TransposeBitblock32() needs to be updated."); |
| 2332 |
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) { |
| 2333 |
// buf must be vector-aligned and have size 64k |
| 2334 |
const uint32_t initial_read_byte_ct = DivUp(write_batch_size, CHAR_BIT); |
| 2335 |
// fold the first 6 shuffles into the initial ingestion loop |
| 2336 |
const unsigned char* initial_read_iter = DowncastKToUc(read_iter); |
| 2337 |
const unsigned char* initial_read_end = &(initial_read_iter[initial_read_byte_ct]); |
| 2338 |
unsigned char* initial_target_iter = DowncastToUc(buf0); |
| 2339 |
const uint32_t read_byte_stride = read_ul_stride * kBytesPerWord; |
| 2340 |
const uint32_t read_batch_rem = kBitsPerCacheline - read_batch_size; |
| 2341 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 2342 |
const unsigned char* read_iter_tmp = initial_read_iter; |
| 2343 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 2344 |
*initial_target_iter++ = *read_iter_tmp; |
| 2345 |
read_iter_tmp = &(read_iter_tmp[read_byte_stride]); |
| 2346 |
} |
| 2347 |
initial_target_iter = memsetua(initial_target_iter, 0, read_batch_rem); |
| 2348 |
} |
| 2349 |
|
| 2350 |
// third-to-last shuffle, 8 bit spacing -> 4 |
| 2351 |
const VecW* source_iter = buf0; |
| 2352 |
uintptr_t* target_iter0 = buf1; |
| 2353 |
const uint32_t write_word_ct = BitCtToWordCt(read_batch_size); |
| 2354 |
const uint32_t first_inner_loop_iter_ct = 4 * write_word_ct; |
| 2355 |
uint32_t cur_write_skip = 4 * kWordsPerCacheline - first_inner_loop_iter_ct; |
| 2356 |
// coincidentally, this also needs to run DivUp(write_batch_size, CHAR_BIT) |
| 2357 |
// times |
| 2358 |
for (uint32_t uii = 0; uii != initial_read_byte_ct; ++uii) { |
| 2359 |
uintptr_t* target_iter1 = &(target_iter0[kWordsPerCacheline * 4]); |
| 2360 |
for (uint32_t ujj = 0; ujj != first_inner_loop_iter_ct; ++ujj) { |
| 2361 |
const uintptr_t source_word_lo = *source_iter++; |
| 2362 |
const uintptr_t source_word_hi = *source_iter++; |
| 2363 |
*target_iter0++ = PackTwo0F0FMask(source_word_lo, source_word_hi); |
| 2364 |
*target_iter1++ = PackTwo0F0FMask(source_word_lo >> 4, source_word_hi >> 4); |
| 2365 |
} |
| 2366 |
source_iter = &(source_iter[2 * cur_write_skip]); |
| 2367 |
target_iter0 = &(target_iter1[cur_write_skip]); |
| 2368 |
} |
| 2369 |
|
| 2370 |
// second-to-last shuffle, 4 bit spacing -> 2 |
| 2371 |
source_iter = buf1; |
| 2372 |
target_iter0 = buf0; |
| 2373 |
const uint32_t second_outer_loop_iter_ct = DivUp(write_batch_size, 4); |
| 2374 |
const uint32_t second_inner_loop_iter_ct = 2 * write_word_ct; |
| 2375 |
cur_write_skip = 2 * kWordsPerCacheline - second_inner_loop_iter_ct; |
| 2376 |
for (uint32_t uii = 0; uii != second_outer_loop_iter_ct; ++uii) { |
| 2377 |
uintptr_t* target_iter1 = &(target_iter0[kWordsPerCacheline * 2]); |
| 2378 |
for (uint32_t ujj = 0; ujj != second_inner_loop_iter_ct; ++ujj) { |
| 2379 |
const uintptr_t source_word_lo = *source_iter++; |
| 2380 |
const uintptr_t source_word_hi = *source_iter++; |
| 2381 |
*target_iter0++ = PackTwo3333Mask(source_word_lo, source_word_hi); |
| 2382 |
*target_iter1++ = PackTwo3333Mask(source_word_lo >> 2, source_word_hi >> 2); |
| 2383 |
} |
| 2384 |
source_iter = &(source_iter[2 * cur_write_skip]); |
| 2385 |
target_iter0 = &(target_iter1[cur_write_skip]); |
| 2386 |
} |
| 2387 |
// last shuffle, 2 bit spacing -> 1 |
| 2388 |
source_iter = buf0; |
| 2389 |
target_iter0 = write_iter; |
| 2390 |
const uint32_t last_loop_iter_ct = DivUp(write_batch_size, 2); |
| 2391 |
for (uint32_t uii = 0; uii != last_loop_iter_ct; ++uii) { |
| 2392 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 2393 |
for (uint32_t ujj = 0; ujj != write_word_ct; ++ujj) { |
| 2394 |
const uintptr_t source_word_lo = S_CAST(uintptr_t, *source_iter++); |
| 2395 |
const uintptr_t source_word_hi = S_CAST(uintptr_t, *source_iter++); |
| 2396 |
target_iter0[ujj] = PackTwo5555Mask(source_word_lo, source_word_hi); |
| 2397 |
target_iter1[ujj] = PackTwo5555Mask(source_word_lo >> 1, source_word_hi >> 1); |
| 2398 |
} |
| 2399 |
source_iter = &(source_iter[2 * (kWordsPerCacheline - write_word_ct)]); |
| 2400 |
target_iter0 = &(target_iter1[write_ul_stride]); |
| 2401 |
} |
| 2402 |
} |
| 2403 |
# endif |
| 2404 |
#endif // !USE_SSE2 |
| 2405 |
|
| 2406 |
#ifdef USE_SSE2 |
| 2407 |
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) { |
| 2408 |
// Very similar to TransposeNypblock64() in pgenlib_internal. |
| 2409 |
// vecaligned_buf must be vector-aligned and have size 8k if kCacheline==64, |
| 2410 |
// 32k if kCacheline==128 |
| 2411 |
const uint32_t buf_row_ct = DivUp(write_batch_size, 8); |
| 2412 |
// fold the first 4-5 shuffles into the initial ingestion loop |
| 2413 |
const uint32_t* initial_read_iter = DowncastKWToU32(read_iter); |
| 2414 |
const uint32_t* initial_read_end = &(initial_read_iter[buf_row_ct]); |
| 2415 |
uint32_t* initial_target_iter = DowncastVecWToU32(vecaligned_buf); |
| 2416 |
const uint32_t read_u32_stride = read_ul_stride * (kBytesPerWord / 4); |
| 2417 |
const uint32_t read_batch_rem = kNybblesPerCacheline - read_batch_size; |
| 2418 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 2419 |
const uint32_t* read_iter_tmp = initial_read_iter; |
| 2420 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 2421 |
*initial_target_iter++ = *read_iter_tmp; |
| 2422 |
read_iter_tmp = &(read_iter_tmp[read_u32_stride]); |
| 2423 |
} |
| 2424 |
if (!read_batch_rem) { |
| 2425 |
continue; |
| 2426 |
} |
| 2427 |
memset(initial_target_iter, 0, read_batch_rem * 4); |
| 2428 |
initial_target_iter = &(initial_target_iter[read_batch_rem]); |
| 2429 |
} |
| 2430 |
|
| 2431 |
// 32 bit spacing -> 4 |
| 2432 |
const VecW* source_iter = vecaligned_buf; |
| 2433 |
const VecW m4 = VCONST_W(kMask0F0F); |
| 2434 |
const uint32_t buf_fullrow_ct = write_batch_size / 8; |
| 2435 |
const uint32_t b64width = DivUp(read_batch_size, 16); |
| 2436 |
uintptr_t* target_iter0 = write_iter; |
| 2437 |
uint32_t cur_dst_row_ct = 8; |
| 2438 |
# ifdef USE_SHUFFLE8 |
| 2439 |
const VecW gather_u16s = vecw_setr8(0, 8, 1, 9, 2, 10, 3, 11, |
| 2440 |
4, 12, 5, 13, 6, 14, 7, 15); |
| 2441 |
# else |
| 2442 |
const VecW m8 = VCONST_W(kMask00FF); |
| 2443 |
# endif |
| 2444 |
# ifdef USE_AVX2 |
| 2445 |
// movemask is slower even in AVX2 case |
| 2446 |
const VecW gather_u32s = vecw_setr8(0, 1, 8, 9, 2, 3, 10, 11, |
| 2447 |
4, 5, 12, 13, 6, 7, 14, 15); |
| 2448 |
for (uint32_t buf_row_idx = 0; ; ++buf_row_idx) { |
| 2449 |
if (buf_row_idx >= buf_fullrow_ct) { |
| 2450 |
if (buf_row_idx == buf_row_ct) { |
| 2451 |
return; |
| 2452 |
} |
| 2453 |
cur_dst_row_ct = write_batch_size % 8; |
| 2454 |
} |
| 2455 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 2456 |
uintptr_t* target_iter2 = &(target_iter1[write_ul_stride]); |
| 2457 |
uintptr_t* target_iter3 = &(target_iter2[write_ul_stride]); |
| 2458 |
uintptr_t* target_iter4 = &(target_iter3[write_ul_stride]); |
| 2459 |
uintptr_t* target_iter5 = &(target_iter4[write_ul_stride]); |
| 2460 |
uintptr_t* target_iter6 = &(target_iter5[write_ul_stride]); |
| 2461 |
uintptr_t* target_iter7 = &(target_iter6[write_ul_stride]); |
| 2462 |
for (uint32_t b64idx = 0; b64idx != b64width; ++b64idx) { |
| 2463 |
const VecW loader0 = source_iter[b64idx * 2]; |
| 2464 |
const VecW loader1 = source_iter[b64idx * 2 + 1]; |
| 2465 |
VecW even_nybbles0 = loader0 & m4; |
| 2466 |
VecW odd_nybbles0 = vecw_and_notfirst(m4, loader0); |
| 2467 |
VecW even_nybbles1 = loader1 & m4; |
| 2468 |
VecW odd_nybbles1 = vecw_and_notfirst(m4, loader1); |
| 2469 |
even_nybbles0 = even_nybbles0 | vecw_srli(even_nybbles0, 28); |
| 2470 |
odd_nybbles0 = vecw_slli(odd_nybbles0, 28) | odd_nybbles0; |
| 2471 |
even_nybbles1 = even_nybbles1 | vecw_srli(even_nybbles1, 28); |
| 2472 |
odd_nybbles1 = vecw_slli(odd_nybbles1, 28) | odd_nybbles1; |
| 2473 |
// Label the bytes in even_nybbles0 (0, 1, 2, ..., 31), and the bytes in |
| 2474 |
// even_nybbles1 (32, 33, ..., 63). We wish to generate the following |
| 2475 |
// lane-and-vector-crossing permutation: |
| 2476 |
// (0, 8, 16, 24, 32, 40, 48, 56, 1, 9, 17, 25, 33, 41, 49, 57) |
| 2477 |
// (2, 10, 18, 26, 34, 42, 50, 58, 3, 11, 19, 27, 35, 43, 51, 59) |
| 2478 |
|
| 2479 |
// first shuffle: |
| 2480 |
// (0, 8, 1, 9, 2, 10, 3, 11, _, _, _, _, _, _, _, _, |
| 2481 |
// 16, 24, 17, 25, 18, 26, 19, 27, _, _, _, _, _, _, _, _) |
| 2482 |
// |
| 2483 |
// (32, 40, 33, 41, 34, 42, 35, 43, _, _, _, _, _, _, _, _, |
| 2484 |
// 48, 56, 49, 57, 50, 58, 51, 59, _, _, _, _, _, _, _, _) |
| 2485 |
// |
| 2486 |
// _mm256_unpacklo_epi16: |
| 2487 |
// (0, 8, 32, 40, 1, 9, 33, 41, 2, 10, 34, 42, 3, 11, 35, 43, |
| 2488 |
// 16, 24, 48, 56, 17, 25, 49, 57, 18, 26, 50, 58, 19, 27, 51, 59) |
| 2489 |
// |
| 2490 |
// {0, 2, 1, 3} permute: |
| 2491 |
// (0, 8, 32, 40, 1, 9, 33, 41, 16, 24, 48, 56, 17, 25, 49, 57, |
| 2492 |
// 2, 10, 34, 42, 3, 11, 35, 43, 18, 26, 50, 58, 19, 27, 51, 59) |
| 2493 |
// |
| 2494 |
// final shuffle gives us what we want. |
| 2495 |
even_nybbles0 = vecw_shuffle8(even_nybbles0, gather_u16s); |
| 2496 |
odd_nybbles0 = vecw_shuffle8(odd_nybbles0, gather_u16s); |
| 2497 |
even_nybbles1 = vecw_shuffle8(even_nybbles1, gather_u16s); |
| 2498 |
odd_nybbles1 = vecw_shuffle8(odd_nybbles1, gather_u16s); |
| 2499 |
|
| 2500 |
VecW target_even = vecw_unpacklo16(even_nybbles0, even_nybbles1); |
| 2501 |
VecW target_odd = vecw_unpackhi16(odd_nybbles0, odd_nybbles1); |
| 2502 |
|
| 2503 |
target_even = vecw_permute0xd8_if_avx2(target_even); |
| 2504 |
target_odd = vecw_permute0xd8_if_avx2(target_odd); |
| 2505 |
|
| 2506 |
target_even = vecw_shuffle8(target_even, gather_u32s); |
| 2507 |
target_odd = vecw_shuffle8(target_odd, gather_u32s); |
| 2508 |
|
| 2509 |
// tried using _mm_stream_si64 here, that totally sucked |
| 2510 |
switch (cur_dst_row_ct) { |
| 2511 |
case 8: |
| 2512 |
target_iter7[b64idx] = vecw_extract64_3(target_odd); |
| 2513 |
// fall through |
| 2514 |
case 7: |
| 2515 |
target_iter6[b64idx] = vecw_extract64_3(target_even); |
| 2516 |
// fall through |
| 2517 |
case 6: |
| 2518 |
target_iter5[b64idx] = vecw_extract64_2(target_odd); |
| 2519 |
// fall through |
| 2520 |
case 5: |
| 2521 |
target_iter4[b64idx] = vecw_extract64_2(target_even); |
| 2522 |
// fall through |
| 2523 |
case 4: |
| 2524 |
target_iter3[b64idx] = vecw_extract64_1(target_odd); |
| 2525 |
// fall through |
| 2526 |
case 3: |
| 2527 |
target_iter2[b64idx] = vecw_extract64_1(target_even); |
| 2528 |
// fall through |
| 2529 |
case 2: |
| 2530 |
target_iter1[b64idx] = vecw_extract64_0(target_odd); |
| 2531 |
// fall through |
| 2532 |
default: |
| 2533 |
target_iter0[b64idx] = vecw_extract64_0(target_even); |
| 2534 |
} |
| 2535 |
} |
| 2536 |
source_iter = &(source_iter[(4 * kPglNybbleTransposeBatch) / kBytesPerVec]); |
| 2537 |
target_iter0 = &(target_iter7[write_ul_stride]); |
| 2538 |
} |
| 2539 |
# else // !USE_AVX2 |
| 2540 |
for (uint32_t buf_row_idx = 0; ; ++buf_row_idx) { |
| 2541 |
if (buf_row_idx >= buf_fullrow_ct) { |
| 2542 |
if (buf_row_idx == buf_row_ct) { |
| 2543 |
return; |
| 2544 |
} |
| 2545 |
cur_dst_row_ct = write_batch_size % 8; |
| 2546 |
} |
| 2547 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 2548 |
uintptr_t* target_iter2 = &(target_iter1[write_ul_stride]); |
| 2549 |
uintptr_t* target_iter3 = &(target_iter2[write_ul_stride]); |
| 2550 |
uintptr_t* target_iter4 = &(target_iter3[write_ul_stride]); |
| 2551 |
uintptr_t* target_iter5 = &(target_iter4[write_ul_stride]); |
| 2552 |
uintptr_t* target_iter6 = &(target_iter5[write_ul_stride]); |
| 2553 |
uintptr_t* target_iter7 = &(target_iter6[write_ul_stride]); |
| 2554 |
for (uint32_t b64idx = 0; b64idx != b64width; ++b64idx) { |
| 2555 |
const VecW loader0 = source_iter[b64idx * 4]; |
| 2556 |
const VecW loader1 = source_iter[b64idx * 4 + 1]; |
| 2557 |
const VecW loader2 = source_iter[b64idx * 4 + 2]; |
| 2558 |
const VecW loader3 = source_iter[b64idx * 4 + 3]; |
| 2559 |
VecW even_nybbles0 = loader0 & m4; |
| 2560 |
VecW odd_nybbles0 = vecw_and_notfirst(m4, loader0); |
| 2561 |
VecW even_nybbles1 = loader1 & m4; |
| 2562 |
VecW odd_nybbles1 = vecw_and_notfirst(m4, loader1); |
| 2563 |
VecW even_nybbles2 = loader2 & m4; |
| 2564 |
VecW odd_nybbles2 = vecw_and_notfirst(m4, loader2); |
| 2565 |
VecW even_nybbles3 = loader3 & m4; |
| 2566 |
VecW odd_nybbles3 = vecw_and_notfirst(m4, loader3); |
| 2567 |
even_nybbles0 = even_nybbles0 | vecw_srli(even_nybbles0, 28); |
| 2568 |
odd_nybbles0 = vecw_slli(odd_nybbles0, 28) | odd_nybbles0; |
| 2569 |
even_nybbles1 = even_nybbles1 | vecw_srli(even_nybbles1, 28); |
| 2570 |
odd_nybbles1 = vecw_slli(odd_nybbles1, 28) | odd_nybbles1; |
| 2571 |
even_nybbles2 = even_nybbles2 | vecw_srli(even_nybbles2, 28); |
| 2572 |
odd_nybbles2 = vecw_slli(odd_nybbles2, 28) | odd_nybbles2; |
| 2573 |
even_nybbles3 = even_nybbles3 | vecw_srli(even_nybbles3, 28); |
| 2574 |
odd_nybbles3 = vecw_slli(odd_nybbles3, 28) | odd_nybbles3; |
| 2575 |
// Label the bytes in even_nybbles0 (0, 1, 2, ..., 15), the bytes in |
| 2576 |
// even_nybbles1 (16, 17, ..., 31), ..., up to even_nybbles3 being (48, |
| 2577 |
// 49, ..., 63). We wish to generate the following vector-crossing |
| 2578 |
// permutation: |
| 2579 |
// (0, 8, 16, 24, 32, 40, 48, 56, 1, 9, 17, 25, 33, 41, 49, 57) |
| 2580 |
// (2, 10, 18, 26, 34, 42, 50, 58, 3, 11, 19, 27, 35, 43, 51, 59) |
| 2581 |
|
| 2582 |
// first shuffle: |
| 2583 |
// (0, 8, 1, 9, 2, 10, 3, 11, _, _, _, _, _, _, _, _) |
| 2584 |
// (16, 24, 17, 25, 18, 26, 19, 27, _, _, _, _, _, _, _, _) |
| 2585 |
// (32, 40, 33, 41, 34, 42, 35, 43, _, _, _, _, _, _, _, _) |
| 2586 |
// (48, 56, 49, 57, 50, 58, 51, 59, _, _, _, _, _, _, _, _) |
| 2587 |
|
| 2588 |
// _mm_unpacklo_epi16: |
| 2589 |
// (0, 8, 16, 24, 1, 9, 17, 25, 2, 10, 18, 26, 3, 11, 19, 27) |
| 2590 |
// (32, 40, 48, 56, 33, 41, 49, 57, 34, 42, 50, 58, 35, 43, 51, 59) |
| 2591 |
// |
| 2592 |
// finish with _mm_unpack{lo,hi}_epi32 |
| 2593 |
# ifdef USE_SHUFFLE8 |
| 2594 |
even_nybbles0 = vecw_shuffle8(even_nybbles0, gather_u16s); |
| 2595 |
odd_nybbles0 = vecw_shuffle8(odd_nybbles0, gather_u16s); |
| 2596 |
even_nybbles1 = vecw_shuffle8(even_nybbles1, gather_u16s); |
| 2597 |
odd_nybbles1 = vecw_shuffle8(odd_nybbles1, gather_u16s); |
| 2598 |
even_nybbles2 = vecw_shuffle8(even_nybbles2, gather_u16s); |
| 2599 |
odd_nybbles2 = vecw_shuffle8(odd_nybbles2, gather_u16s); |
| 2600 |
even_nybbles3 = vecw_shuffle8(even_nybbles3, gather_u16s); |
| 2601 |
odd_nybbles3 = vecw_shuffle8(odd_nybbles3, gather_u16s); |
| 2602 |
# else |
| 2603 |
VecW tmp_lo = vecw_unpacklo8(even_nybbles0, odd_nybbles0); |
| 2604 |
VecW tmp_hi = vecw_unpackhi8(even_nybbles0, odd_nybbles0); |
| 2605 |
even_nybbles0 = vecw_blendv(vecw_slli(tmp_hi, 8), tmp_lo, m8); |
| 2606 |
odd_nybbles0 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 8), m8); |
| 2607 |
tmp_lo = vecw_unpacklo8(even_nybbles1, odd_nybbles1); |
| 2608 |
tmp_hi = vecw_unpackhi8(even_nybbles1, odd_nybbles1); |
| 2609 |
even_nybbles1 = vecw_blendv(vecw_slli(tmp_hi, 8), tmp_lo, m8); |
| 2610 |
odd_nybbles1 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 8), m8); |
| 2611 |
tmp_lo = vecw_unpacklo8(even_nybbles2, odd_nybbles2); |
| 2612 |
tmp_hi = vecw_unpackhi8(even_nybbles2, odd_nybbles2); |
| 2613 |
even_nybbles2 = vecw_blendv(vecw_slli(tmp_hi, 8), tmp_lo, m8); |
| 2614 |
odd_nybbles2 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 8), m8); |
| 2615 |
tmp_lo = vecw_unpacklo8(even_nybbles3, odd_nybbles3); |
| 2616 |
tmp_hi = vecw_unpackhi8(even_nybbles3, odd_nybbles3); |
| 2617 |
even_nybbles3 = vecw_blendv(vecw_slli(tmp_hi, 8), tmp_lo, m8); |
| 2618 |
odd_nybbles3 = vecw_blendv(tmp_hi, vecw_srli(tmp_lo, 8), m8); |
| 2619 |
# endif |
| 2620 |
|
| 2621 |
const VecW even_lo = vecw_unpacklo16(even_nybbles0, even_nybbles1); |
| 2622 |
const VecW odd_lo = vecw_unpackhi16(odd_nybbles0, odd_nybbles1); |
| 2623 |
const VecW even_hi = vecw_unpacklo16(even_nybbles2, even_nybbles3); |
| 2624 |
const VecW odd_hi = vecw_unpackhi16(odd_nybbles2, odd_nybbles3); |
| 2625 |
|
| 2626 |
const VecW final02 = vecw_unpacklo32(even_lo, even_hi); |
| 2627 |
const VecW final13 = vecw_unpacklo32(odd_lo, odd_hi); |
| 2628 |
const VecW final46 = vecw_unpackhi32(even_lo, even_hi); |
| 2629 |
const VecW final57 = vecw_unpackhi32(odd_lo, odd_hi); |
| 2630 |
switch (cur_dst_row_ct) { |
| 2631 |
case 8: |
| 2632 |
target_iter7[b64idx] = vecw_extract64_1(final57); |
| 2633 |
// fall through |
| 2634 |
case 7: |
| 2635 |
target_iter6[b64idx] = vecw_extract64_1(final46); |
| 2636 |
// fall through |
| 2637 |
case 6: |
| 2638 |
target_iter5[b64idx] = vecw_extract64_0(final57); |
| 2639 |
// fall through |
| 2640 |
case 5: |
| 2641 |
target_iter4[b64idx] = vecw_extract64_0(final46); |
| 2642 |
// fall through |
| 2643 |
case 4: |
| 2644 |
target_iter3[b64idx] = vecw_extract64_1(final13); |
| 2645 |
// fall through |
| 2646 |
case 3: |
| 2647 |
target_iter2[b64idx] = vecw_extract64_1(final02); |
| 2648 |
// fall through |
| 2649 |
case 2: |
| 2650 |
target_iter1[b64idx] = vecw_extract64_0(final13); |
| 2651 |
// fall through |
| 2652 |
default: |
| 2653 |
target_iter0[b64idx] = vecw_extract64_0(final02); |
| 2654 |
} |
| 2655 |
} |
| 2656 |
source_iter = &(source_iter[(4 * kPglNybbleTransposeBatch) / kBytesPerVec]); |
| 2657 |
target_iter0 = &(target_iter7[write_ul_stride]); |
| 2658 |
} |
| 2659 |
# endif // !USE_AVX2 |
| 2660 |
} |
| 2661 |
#else // !USE_SSE2 |
| 2662 |
static_assert(kWordsPerVec == 1, "TransposeNybbleblock() needs to be updated."); |
| 2663 |
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) { |
| 2664 |
// Very similar to unvectorized TransposeNypblock{32,64}() in pgenlib_misc. |
| 2665 |
// vecaligned_buf must have size 8k |
| 2666 |
const uint32_t buf_row_ct = NybbleCtToByteCt(write_batch_size); |
| 2667 |
// fold the first 6 shuffles into the initial ingestion loop |
| 2668 |
const unsigned char* initial_read_iter = DowncastKToUc(read_iter); |
| 2669 |
const unsigned char* initial_read_end = &(initial_read_iter[buf_row_ct]); |
| 2670 |
unsigned char* initial_target_iter = DowncastToUc(vecaligned_buf); |
| 2671 |
const uint32_t read_byte_stride = read_ul_stride * kBytesPerWord; |
| 2672 |
const uint32_t read_batch_rem = kNybblesPerCacheline - read_batch_size; |
| 2673 |
for (; initial_read_iter != initial_read_end; ++initial_read_iter) { |
| 2674 |
const unsigned char* read_iter_tmp = initial_read_iter; |
| 2675 |
for (uint32_t ujj = 0; ujj != read_batch_size; ++ujj) { |
| 2676 |
*initial_target_iter++ = *read_iter_tmp; |
| 2677 |
read_iter_tmp = &(read_iter_tmp[read_byte_stride]); |
| 2678 |
} |
| 2679 |
initial_target_iter = memsetua(initial_target_iter, 0, read_batch_rem); |
| 2680 |
} |
| 2681 |
|
| 2682 |
// 8 bit spacing -> 4 |
| 2683 |
const VecW* source_iter = vecaligned_buf; |
| 2684 |
uintptr_t* target_iter0 = write_iter; |
| 2685 |
const uint32_t buf_fullrow_ct = write_batch_size / 2; |
| 2686 |
const uint32_t write_word_ct = NybbleCtToWordCt(read_batch_size); |
| 2687 |
for (uint32_t uii = 0; uii != buf_fullrow_ct; ++uii) { |
| 2688 |
uintptr_t* target_iter1 = &(target_iter0[write_ul_stride]); |
| 2689 |
for (uint32_t ujj = 0; ujj != write_word_ct; ++ujj) { |
| 2690 |
const uintptr_t source_word_lo = *source_iter++; |
| 2691 |
const uintptr_t source_word_hi = *source_iter++; |
| 2692 |
target_iter0[ujj] = PackTwo0F0FMask(source_word_lo, source_word_hi); |
| 2693 |
target_iter1[ujj] = PackTwo0F0FMask(source_word_lo >> 4, source_word_hi >> 4); |
| 2694 |
} |
| 2695 |
source_iter = &(source_iter[2 * (kWordsPerCacheline - write_word_ct)]); |
| 2696 |
target_iter0 = &(target_iter1[write_ul_stride]); |
| 2697 |
} |
| 2698 |
const uint32_t remainder = write_batch_size % 2; |
| 2699 |
if (!remainder) { |
| 2700 |
return; |
| 2701 |
} |
| 2702 |
for (uint32_t ujj = 0; ujj != write_word_ct; ++ujj) { |
| 2703 |
const uintptr_t source_word_lo = *source_iter++; |
| 2704 |
const uintptr_t source_word_hi = *source_iter++; |
| 2705 |
target_iter0[ujj] = PackTwo0F0FMask(source_word_lo, source_word_hi); |
| 2706 |
} |
| 2707 |
} |
| 2708 |
#endif // !USE_SSE2 |
| 2709 |
|
| 2710 |
#ifdef USE_SSE2 |
| 2711 |
# ifdef USE_AVX2 |
| 2712 |
const unsigned char kLeadMask[2 * kBytesPerVec] __attribute__ ((aligned (64))) = |
| 2713 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2714 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2715 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 2716 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; |
| 2717 |
# else |
| 2718 |
const unsigned char kLeadMask[2 * kBytesPerVec] __attribute__ ((aligned (32))) = |
| 2719 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2720 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; |
| 2721 |
# endif |
| 2722 |
|
| 2723 |
uintptr_t BytesumArr(const void* bytearr, uintptr_t byte_ct) { |
| 2724 |
uintptr_t tot = 0; |
| 2725 |
if (byte_ct < kBytesPerVec) { |
| 2726 |
const unsigned char* bytearr_uc = S_CAST(const unsigned char*, bytearr); |
| 2727 |
for (uintptr_t ulii = 0; ulii != byte_ct; ++ulii) { |
| 2728 |
tot += bytearr_uc[ulii]; |
| 2729 |
} |
| 2730 |
return tot; |
| 2731 |
} |
| 2732 |
const unsigned char* bytearr_uc_iter = S_CAST(const unsigned char*, bytearr); |
| 2733 |
const unsigned char* bytearr_uc_final = &(bytearr_uc_iter[byte_ct - kBytesPerVec]); |
| 2734 |
const VecW m0 = vecw_setzero(); |
| 2735 |
VecW acc = vecw_setzero(); |
| 2736 |
while (bytearr_uc_iter < bytearr_uc_final) { |
| 2737 |
const VecW cur_vec = vecw_loadu(bytearr_uc_iter); |
| 2738 |
acc = acc + vecw_sad(cur_vec, m0); |
| 2739 |
bytearr_uc_iter = &(bytearr_uc_iter[kBytesPerVec]); |
| 2740 |
} |
| 2741 |
VecW cur_vec = vecw_loadu(bytearr_uc_final); |
| 2742 |
const uintptr_t overlap_byte_ct = bytearr_uc_iter - bytearr_uc_final; |
| 2743 |
const VecW mask_vec = vecw_loadu(&(kLeadMask[kBytesPerVec - overlap_byte_ct])); |
| 2744 |
cur_vec = cur_vec & mask_vec; |
| 2745 |
acc = acc + vecw_sad(cur_vec, m0); |
| 2746 |
return HsumW(acc); |
| 2747 |
} |
| 2748 |
|
| 2749 |
#else // !USE_SSE2 |
| 2750 |
uintptr_t BytesumArr(const void* bytearr, uintptr_t byte_ct) { |
| 2751 |
// Assumes sum is representable by uintptr_t. |
| 2752 |
const unsigned char* bytearr_uc = S_CAST(const unsigned char*, bytearr); |
| 2753 |
const uintptr_t* bytearr_alias_iter; |
| 2754 |
uint32_t lead_byte_ct = AlignKToAW(bytearr_uc, &bytearr_alias_iter); |
| 2755 |
uintptr_t tot = 0; |
| 2756 |
if (lead_byte_ct) { |
| 2757 |
if (lead_byte_ct > byte_ct) { |
| 2758 |
lead_byte_ct = byte_ct; |
| 2759 |
} |
| 2760 |
for (uint32_t uii = 0; uii != lead_byte_ct; ++uii) { |
| 2761 |
tot += bytearr_uc[uii]; |
| 2762 |
} |
| 2763 |
byte_ct -= lead_byte_ct; |
| 2764 |
if (!byte_ct) { |
| 2765 |
return tot; |
| 2766 |
} |
| 2767 |
} |
| 2768 |
const uint32_t fullword_ct = byte_ct / kBytesPerWord; |
| 2769 |
const uint32_t wordblock_idx_trail = fullword_ct / 256; |
| 2770 |
const uint32_t wordblock_idx_end = DivUp(fullword_ct, 256); |
| 2771 |
uint32_t wordblock_len = 256; |
| 2772 |
for (uint32_t wordblock_idx = 0; ; ++wordblock_idx) { |
| 2773 |
if (wordblock_idx >= wordblock_idx_trail) { |
| 2774 |
if (wordblock_idx == wordblock_idx_end) { |
| 2775 |
byte_ct = byte_ct % kBytesPerWord; |
| 2776 |
const unsigned char* last_word_start = DowncastKToUc(bytearr_alias_iter); |
| 2777 |
for (uint32_t uii = 0; uii != byte_ct; ++uii) { |
| 2778 |
tot += last_word_start[uii]; |
| 2779 |
} |
| 2780 |
return tot; |
| 2781 |
} |
| 2782 |
wordblock_len = fullword_ct % 256; |
| 2783 |
} |
| 2784 |
const uintptr_t* bytearr_alias_stop = &(bytearr_alias_iter[wordblock_len]); |
| 2785 |
uintptr_t acc_even = 0; |
| 2786 |
uintptr_t acc_odd = 0; |
| 2787 |
do { |
| 2788 |
uintptr_t cur_word = *bytearr_alias_iter++; |
| 2789 |
acc_even += cur_word & kMask00FF; |
| 2790 |
acc_odd += (cur_word >> 8) & kMask00FF; |
| 2791 |
} while (bytearr_alias_iter < bytearr_alias_stop); |
| 2792 |
acc_even = S_CAST(Halfword, acc_even) + (acc_even >> kBitsPerWordD2); |
| 2793 |
acc_odd = S_CAST(Halfword, acc_odd) + (acc_odd >> kBitsPerWordD2); |
| 2794 |
tot += acc_even + acc_odd; |
| 2795 |
} |
| 2796 |
} |
| 2797 |
#endif // !USE_SSE2 |
| 2798 |
|
| 2799 |
uintptr_t CountByte(const void* bytearr, unsigned char ucc, uintptr_t byte_ct) { |
| 2800 |
const unsigned char* bytearr_uc_iter = S_CAST(const unsigned char*, bytearr); |
| 2801 |
#ifdef USE_SSE2 |
| 2802 |
if (byte_ct < kBytesPerVec) { |
| 2803 |
#endif |
| 2804 |
uintptr_t tot = 0; |
| 2805 |
for (uintptr_t ulii = 0; ulii != byte_ct; ++ulii) { |
| 2806 |
tot += (bytearr_uc_iter[ulii] == ucc); |
| 2807 |
} |
| 2808 |
return tot; |
| 2809 |
#ifdef USE_SSE2 |
| 2810 |
} |
| 2811 |
const VecW m0 = vecw_setzero(); |
| 2812 |
const VecUc match_vvec = vecuc_set1(ucc); |
| 2813 |
VecW acc = vecw_setzero(); |
| 2814 |
while (byte_ct > 255 * kBytesPerVec) { |
| 2815 |
VecUc inner_acc = vecuc_setzero(); |
| 2816 |
for (uint32_t uii = 0; uii != 255; ++uii) { |
| 2817 |
const VecUc cur_vvec = vecuc_loadu(bytearr_uc_iter); |
| 2818 |
bytearr_uc_iter = &(bytearr_uc_iter[kBytesPerVec]); |
| 2819 |
inner_acc = inner_acc - (cur_vvec == match_vvec); |
| 2820 |
} |
| 2821 |
acc = acc + vecw_sad(VecUcToW(inner_acc), m0); |
| 2822 |
byte_ct -= 255 * kBytesPerVec; |
| 2823 |
} |
| 2824 |
const unsigned char* bytearr_uc_final = &(bytearr_uc_iter[byte_ct - kBytesPerVec]); |
| 2825 |
VecUc inner_acc = vecuc_setzero(); |
| 2826 |
while (bytearr_uc_iter < bytearr_uc_final) { |
| 2827 |
const VecUc cur_vvec = vecuc_loadu(bytearr_uc_iter); |
| 2828 |
bytearr_uc_iter = &(bytearr_uc_iter[kBytesPerVec]); |
| 2829 |
inner_acc = inner_acc - (cur_vvec == match_vvec); |
| 2830 |
} |
| 2831 |
VecUc cur_vvec = vecuc_loadu(bytearr_uc_final); |
| 2832 |
const uintptr_t overlap_byte_ct = bytearr_uc_iter - bytearr_uc_final; |
| 2833 |
const VecUc mask_vvec = vecuc_loadu(&(kLeadMask[kBytesPerVec - overlap_byte_ct])); |
| 2834 |
cur_vvec = (cur_vvec == match_vvec) & mask_vvec; |
| 2835 |
inner_acc = inner_acc - cur_vvec; |
| 2836 |
acc = acc + vecw_sad(VecUcToW(inner_acc), m0); |
| 2837 |
return HsumW(acc); |
| 2838 |
#endif // USE_SSE2 |
| 2839 |
} |
| 2840 |
|
| 2841 |
uintptr_t CountU16(const void* u16arr, uint16_t usii, uintptr_t u16_ct) { |
| 2842 |
const unsigned char* u16arr_biter = S_CAST(const unsigned char*, u16arr); |
| 2843 |
#ifdef USE_SSE2 |
| 2844 |
if (u16_ct < (kBytesPerVec / 2)) { |
| 2845 |
#endif |
| 2846 |
// todo: benchmark this vs. zero-extend in a non-SSE2 setting |
| 2847 |
uintptr_t tot = 0; |
| 2848 |
for (uintptr_t ulii = 0; ulii != u16_ct; ++ulii) { |
| 2849 |
uint16_t cur_u16; |
| 2850 |
CopyFromUnalignedOffsetU16(&cur_u16, u16arr_biter, ulii); |
| 2851 |
tot += (cur_u16 == usii); |
| 2852 |
} |
| 2853 |
return tot; |
| 2854 |
#ifdef USE_SSE2 |
| 2855 |
} |
| 2856 |
const VecW m0 = vecw_setzero(); |
| 2857 |
const VecU16 match_vvec = vecu16_set1(usii); |
| 2858 |
VecW acc = vecw_setzero(); |
| 2859 |
// can also use larger loop and a slightly different accumulation algorithm, |
| 2860 |
// but it should make practically no difference; lets keep these loops as |
| 2861 |
// similar as possible for now. |
| 2862 |
while (u16_ct > 255 * (kBytesPerVec / 2)) { |
| 2863 |
VecU16 inner_acc = vecu16_setzero(); |
| 2864 |
for (uint32_t uii = 0; uii != 255; ++uii) { |
| 2865 |
const VecU16 cur_vvec = vecu16_loadu(u16arr_biter); |
| 2866 |
u16arr_biter = &(u16arr_biter[kBytesPerVec]); |
| 2867 |
inner_acc = inner_acc - (cur_vvec == match_vvec); |
| 2868 |
} |
| 2869 |
acc = acc + vecw_sad(VecU16ToW(inner_acc), m0); |
| 2870 |
u16_ct -= 255 * (kBytesPerVec / 2); |
| 2871 |
} |
| 2872 |
const unsigned char* u16arr_final = &(u16arr_biter[u16_ct * sizeof(int16_t) - kBytesPerVec]); |
| 2873 |
VecU16 inner_acc = vecu16_setzero(); |
| 2874 |
while (u16arr_biter < u16arr_final) { |
| 2875 |
const VecU16 cur_vvec = vecu16_loadu(u16arr_biter); |
| 2876 |
u16arr_biter = &(u16arr_biter[kBytesPerVec]); |
| 2877 |
inner_acc = inner_acc - (cur_vvec == match_vvec); |
| 2878 |
} |
| 2879 |
VecU16 cur_vvec = vecu16_loadu(u16arr_final); |
| 2880 |
const uintptr_t overlap_byte_ct = u16arr_biter - u16arr_final; |
| 2881 |
const VecU16 mask_vvec = vecu16_loadu(&(kLeadMask[kBytesPerVec - overlap_byte_ct])); |
| 2882 |
cur_vvec = (cur_vvec == match_vvec) & mask_vvec; |
| 2883 |
inner_acc = inner_acc - cur_vvec; |
| 2884 |
acc = acc + vecw_sad(VecU16ToW(inner_acc), m0); |
| 2885 |
return HsumW(acc); |
| 2886 |
#endif // USE_SSE2 |
| 2887 |
} |
| 2888 |
|
| 2889 |
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) { |
| 2890 |
if (!src_subset_size) { |
| 2891 |
return 0; |
| 2892 |
} |
| 2893 |
CopyBitarrSubset(src_subset, sample_include, sample_ct, dst_subset); |
| 2894 |
const unsigned char* src_vals_uc = S_CAST(const unsigned char*, src_vals); |
| 2895 |
unsigned char* dst_vals_uc = S_CAST(unsigned char*, dst_vals); |
| 2896 |
unsigned char* dst_vals_iter = dst_vals_uc; |
| 2897 |
uintptr_t sample_widx = 0; |
| 2898 |
uintptr_t src_subset_bits = src_subset[0]; |
| 2899 |
for (uint32_t src_idx = 0; src_idx != src_subset_size; ++src_idx) { |
| 2900 |
const uintptr_t lowbit = BitIter1y(src_subset, &sample_widx, &src_subset_bits); |
| 2901 |
if (sample_include[sample_widx] & lowbit) { |
| 2902 |
*dst_vals_iter++ = src_vals_uc[src_idx]; |
| 2903 |
} |
| 2904 |
} |
| 2905 |
return dst_vals_iter - dst_vals_uc; |
| 2906 |
} |
| 2907 |
|
| 2908 |
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) { |
| 2909 |
if (!src_subset_size) { |
| 2910 |
return 0; |
| 2911 |
} |
| 2912 |
CopyBitarrSubset(src_subset, sample_include, sample_ct, dst_subset); |
| 2913 |
const uint16_t* src_vals_u16 = S_CAST(const uint16_t*, src_vals); |
| 2914 |
uint16_t* dst_vals_u16 = S_CAST(uint16_t*, dst_vals); |
| 2915 |
uint16_t* dst_vals_iter = dst_vals_u16; |
| 2916 |
uintptr_t sample_widx = 0; |
| 2917 |
uintptr_t src_subset_bits = src_subset[0]; |
| 2918 |
for (uint32_t src_idx = 0; src_idx != src_subset_size; ++src_idx) { |
| 2919 |
const uintptr_t lowbit = BitIter1y(src_subset, &sample_widx, &src_subset_bits); |
| 2920 |
if (sample_include[sample_widx] & lowbit) { |
| 2921 |
*dst_vals_iter++ = src_vals_u16[src_idx]; |
| 2922 |
} |
| 2923 |
} |
| 2924 |
return dst_vals_iter - dst_vals_u16; |
| 2925 |
} |
| 2926 |
|
| 2927 |
// 'Unsafe' because it assumes high bits of every byte are 0. |
| 2928 |
void Reduce8to4bitInplaceUnsafe(uintptr_t entry_ct, uintptr_t* mainvec) { |
| 2929 |
#ifdef USE_SSE2 |
| 2930 |
const uintptr_t fullvec_ct = entry_ct / (kBytesPerVec * 2); |
| 2931 |
const VecW m8 = VCONST_W(kMask00FF); |
| 2932 |
VecW* vmainvec = R_CAST(VecW*, mainvec); |
| 2933 |
for (uintptr_t write_vidx = 0; write_vidx != fullvec_ct; ++write_vidx) { |
| 2934 |
VecW v0 = vmainvec[write_vidx * 2]; |
| 2935 |
VecW v1 = vmainvec[write_vidx * 2 + 1]; |
| 2936 |
v0 = v0 | vecw_srli(v0, 4); |
| 2937 |
v1 = v1 | vecw_srli(v1, 4); |
| 2938 |
vmainvec[write_vidx] = vecw_gather_even(v0, v1, m8); |
| 2939 |
} |
| 2940 |
uintptr_t write_idx = fullvec_ct * kWordsPerVec; |
| 2941 |
if (write_idx == entry_ct * 2) { |
| 2942 |
return; |
| 2943 |
} |
| 2944 |
#else |
| 2945 |
uintptr_t write_idx = 0; |
| 2946 |
#endif |
| 2947 |
// Read two words at a time and write one. |
| 2948 |
// We could instead read one word and write a Halfword at a time, but I'd |
| 2949 |
// rather not worry about the strict-aliasing issues involved there. |
| 2950 |
const uintptr_t write_idx_last = (entry_ct - 1) / (kBytesPerWord * 2); |
| 2951 |
uintptr_t write_word; |
| 2952 |
for (; ; ++write_idx) { |
| 2953 |
const uintptr_t inword0 = mainvec[2 * write_idx]; |
| 2954 |
const uintptr_t inword1 = mainvec[2 * write_idx + 1]; |
| 2955 |
write_word = PackTwo0F0F(inword0, inword1); |
| 2956 |
if (write_idx == write_idx_last) { |
| 2957 |
break; |
| 2958 |
} |
| 2959 |
mainvec[write_idx] = write_word; |
| 2960 |
} |
| 2961 |
const uint32_t remaining_entry_ct = ModNz(entry_ct, kBytesPerWord * 2); |
| 2962 |
mainvec[write_idx] = bzhi_max(write_word, remaining_entry_ct * 4); |
| 2963 |
} |
| 2964 |
|
| 2965 |
// advances forward_ct set bits; forward_ct must be positive. (stays put if |
| 2966 |
// forward_ct == 1 and current bit is set. may want to tweak this interface, |
| 2967 |
// easy to introduce off-by-one bugs...) |
| 2968 |
// In usual 64-bit case, also assumes bitvec is 16-byte aligned and the end of |
| 2969 |
// the trailing 16-byte block can be safely read from. |
| 2970 |
uintptr_t FindNth1BitFrom(const uintptr_t* bitvec, uintptr_t cur_pos, uintptr_t forward_ct) { |
| 2971 |
assert(forward_ct); |
| 2972 |
uintptr_t widx = cur_pos / kBitsPerWord; |
| 2973 |
uintptr_t ulii = cur_pos % kBitsPerWord; |
| 2974 |
const uintptr_t* bptr = &(bitvec[widx]); |
| 2975 |
uintptr_t uljj; |
| 2976 |
uintptr_t ulkk; |
| 2977 |
#ifdef __LP64__ |
| 2978 |
const VecW* vptr; |
| 2979 |
assert(IsVecAligned(bitvec)); |
| 2980 |
#endif |
| 2981 |
if (ulii) { |
| 2982 |
uljj = (*bptr) >> ulii; |
| 2983 |
ulkk = PopcountWord(uljj); |
| 2984 |
if (ulkk >= forward_ct) { |
| 2985 |
FindNth1BitFrom_finish: |
| 2986 |
return widx * kBitsPerWord + ulii + WordBitIdxToUidx(uljj, forward_ct - 1); |
| 2987 |
} |
| 2988 |
forward_ct -= ulkk; |
| 2989 |
++widx; |
| 2990 |
++bptr; |
| 2991 |
} |
| 2992 |
ulii = 0; |
| 2993 |
#ifdef __LP64__ |
| 2994 |
while (widx & (kWordsPerVec - k1LU)) { |
| 2995 |
uljj = *bptr; |
| 2996 |
ulkk = PopcountWord(uljj); |
| 2997 |
if (ulkk >= forward_ct) { |
| 2998 |
goto FindNth1BitFrom_finish; |
| 2999 |
} |
| 3000 |
forward_ct -= ulkk; |
| 3001 |
++widx; |
| 3002 |
++bptr; |
| 3003 |
} |
| 3004 |
vptr = R_CAST(const VecW*, bptr); |
| 3005 |
#ifdef USE_AVX2 |
| 3006 |
while (forward_ct > kBitsPerWord * (16 * kWordsPerVec)) { |
| 3007 |
uljj = (forward_ct - 1) / (kBitsPerWord * kWordsPerVec); |
| 3008 |
ulkk = PopcountVecsAvx2(vptr, uljj); |
| 3009 |
vptr = &(vptr[uljj]); |
| 3010 |
forward_ct -= ulkk; |
| 3011 |
} |
| 3012 |
#else |
| 3013 |
while (forward_ct > kBitsPerWord * (3 * kWordsPerVec)) { |
| 3014 |
uljj = ((forward_ct - 1) / (kBitsPerWord * (3 * kWordsPerVec))) * 3; |
| 3015 |
// yeah, yeah, this is suboptimal if we have SSE4.2 |
| 3016 |
ulkk = PopcountVecsNoAvx2(vptr, uljj); |
| 3017 |
vptr = &(vptr[uljj]); |
| 3018 |
forward_ct -= ulkk; |
| 3019 |
} |
| 3020 |
#endif |
| 3021 |
bptr = R_CAST(const uintptr_t*, vptr); |
| 3022 |
while (forward_ct > kBitsPerWord) { |
| 3023 |
forward_ct -= PopcountWord(*bptr++); |
| 3024 |
} |
| 3025 |
#else |
| 3026 |
while (forward_ct > kBitsPerWord) { |
| 3027 |
uljj = (forward_ct - 1) / kBitsPerWord; |
| 3028 |
ulkk = PopcountWords(bptr, uljj); |
| 3029 |
bptr = &(bptr[uljj]); |
| 3030 |
forward_ct -= ulkk; |
| 3031 |
} |
| 3032 |
#endif |
| 3033 |
for (; ; ++bptr) { |
| 3034 |
uljj = *bptr; |
| 3035 |
ulkk = PopcountWord(uljj); |
| 3036 |
if (ulkk >= forward_ct) { |
| 3037 |
widx = bptr - bitvec; |
| 3038 |
goto FindNth1BitFrom_finish; |
| 3039 |
} |
| 3040 |
forward_ct -= ulkk; |
| 3041 |
} |
| 3042 |
} |
| 3043 |
|
| 3044 |
void FillU32SubsetStarts(const uintptr_t* subset, uint32_t thread_ct, uint32_t start, uint64_t bit_ct, uint32_t* starts) { |
| 3045 |
assert(bit_ct); |
| 3046 |
uint32_t uidx = AdvTo1Bit(subset, start); |
| 3047 |
uint32_t prev_bit_idx = 0; |
| 3048 |
starts[0] = uidx; |
| 3049 |
for (uint32_t tidx = 1; tidx != thread_ct; ++tidx) { |
| 3050 |
const uint32_t bit_idx = (tidx * bit_ct) / thread_ct; |
| 3051 |
if (bit_idx != prev_bit_idx) { |
| 3052 |
uidx = FindNth1BitFrom(subset, uidx + 1, bit_idx - prev_bit_idx); |
| 3053 |
prev_bit_idx = bit_idx; |
| 3054 |
} |
| 3055 |
starts[tidx] = uidx; |
| 3056 |
} |
| 3057 |
} |
| 3058 |
|
| 3059 |
#ifdef __cplusplus |
| 3060 |
} // namespace plink2 |
| 3061 |
#endif |