-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgenlib_internal.cpp
8604 lines (8308 loc) · 380 KB
/
pgenlib_internal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This library is part of PLINK 2.00, copyright (C) 2005-2018 Shaun Purcell,
// Christopher Chang.
//
// This library is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library. If not, see <http://www.gnu.org/licenses/>.
#include "pgenlib_internal.h"
#ifndef NO_MMAP
# include <sys/types.h> // fstat()
# include <sys/stat.h> // open(), fstat()
# include <sys/mman.h> // mmap()
# include <fcntl.h> // open()
# include <unistd.h> // fstat()
#endif
#ifdef __cplusplus
namespace plink2 {
#endif
#ifdef USE_AVX2
void CopyQuaterarrNonemptySubset(const uintptr_t* __restrict raw_quaterarr, const uintptr_t* __restrict subset_mask, uint32_t raw_quaterarr_entry_ct, uint32_t subset_entry_ct, uintptr_t* __restrict output_quaterarr) {
if (subset_entry_ct == raw_quaterarr_entry_ct) {
memcpy(output_quaterarr, raw_quaterarr, DivUp(subset_entry_ct, kBitsPerWordD2) * sizeof(intptr_t));
ZeroTrailingQuaters(subset_entry_ct, output_quaterarr);
return;
}
assert(subset_entry_ct);
uintptr_t cur_output_word = 0;
uintptr_t* output_quaterarr_iter = output_quaterarr;
uintptr_t* output_quaterarr_last = &(output_quaterarr[subset_entry_ct / kBitsPerWordD2]);
const uint32_t word_write_shift_end = 2 * (subset_entry_ct % kBitsPerWordD2);
uint32_t word_write_shift = 0;
uint32_t subset_mask_widx = 0;
while (1) {
const uintptr_t cur_include_word = subset_mask[subset_mask_widx];
if (cur_include_word) {
uint32_t wordhalf_idx = 0;
uint32_t cur_include_halfword = S_CAST(Halfword, cur_include_word);
while (1) {
if (cur_include_halfword) {
uintptr_t extracted_bits = raw_quaterarr[subset_mask_widx * 2 + wordhalf_idx];
uint32_t set_bit_ct = kBitsPerWord;
if (cur_include_halfword != UINT32_MAX) {
const uintptr_t pext_mask = 3 * UnpackHalfwordToWord(cur_include_halfword);
extracted_bits = _pext_u64(extracted_bits, pext_mask);
set_bit_ct = PopcountWord(pext_mask);
}
cur_output_word |= extracted_bits << word_write_shift;
word_write_shift += set_bit_ct;
if (word_write_shift >= kBitsPerWord) {
*output_quaterarr_iter++ = cur_output_word;
word_write_shift -= kBitsPerWord;
cur_output_word = 0;
if (word_write_shift) {
cur_output_word = extracted_bits >> (set_bit_ct - word_write_shift);
}
}
}
if (wordhalf_idx) {
break;
}
++wordhalf_idx;
cur_include_halfword = cur_include_word >> kBitsPerWordD2;
}
if (output_quaterarr_iter == output_quaterarr_last) {
if (word_write_shift == word_write_shift_end) {
if (word_write_shift_end) {
*output_quaterarr_last = cur_output_word;
}
return;
}
}
}
++subset_mask_widx;
}
}
// bit_idx_start assumed to be < kBitsPerWord
void Copy01Subset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genovec, uint32_t write_bit_idx_start, uint32_t bit_ct, uintptr_t* __restrict output_bitarr) {
const uint32_t bit_idx_end = bit_ct + write_bit_idx_start;
const uint32_t bit_idx_end_lowbits = bit_idx_end % kBitsPerWord;
const Halfword* raw_bitarr_alias = R_CAST(const Halfword*, raw_bitarr);
uintptr_t* output_bitarr_iter = output_bitarr;
uintptr_t* output_bitarr_last = &(output_bitarr[bit_idx_end / kBitsPerWord]);
uintptr_t cur_output_word = 0;
uint32_t read_widx = UINT32_MAX; // deliberate overflow
uint32_t write_idx_lowbits = write_bit_idx_start;
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != bit_idx_end_lowbits)) {
uintptr_t cur_mask_word;
// sparse genovec optimization
// guaranteed to terminate since there's at least one more set bit
do {
// todo: try reading two genovec words at a time. would need to be very
// careful with the possible trailing word, though.
// more important to optimize this function now that regular phased-call
// handling code is using it.
cur_mask_word = genovec[++read_widx];
cur_mask_word = cur_mask_word & (~(cur_mask_word >> 1)) & kMask5555;
} while (!cur_mask_word);
uintptr_t extracted_bits = raw_bitarr_alias[read_widx];
uint32_t set_bit_ct = kBitsPerWordD2;
if (cur_mask_word != kMask5555) {
const uintptr_t cur_mask_hw = PackWordToHalfword(cur_mask_word);
set_bit_ct = PopcountWord(cur_mask_word);
extracted_bits = _pext_u64(extracted_bits, cur_mask_hw);
}
cur_output_word |= extracted_bits << write_idx_lowbits;
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct;
if (new_write_idx_lowbits >= kBitsPerWord) {
*output_bitarr_iter++ = cur_output_word;
// ...and these are the bits that fell off
// impossible for write_idx_lowbits to be zero here
cur_output_word = extracted_bits >> (kBitsPerWord - write_idx_lowbits);
}
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord;
}
if (write_idx_lowbits) {
*output_bitarr_iter = cur_output_word;
}
}
void Copy10Subset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genovec, uint32_t bit_ct, uintptr_t* __restrict output_bitarr) {
const uint32_t bit_idx_end_lowbits = bit_ct % kBitsPerWord;
const Halfword* raw_bitarr_alias = R_CAST(const Halfword*, raw_bitarr);
uintptr_t* output_bitarr_iter = output_bitarr;
uintptr_t* output_bitarr_last = &(output_bitarr[bit_ct / kBitsPerWord]);
uintptr_t cur_output_word = 0;
uint32_t read_widx = UINT32_MAX; // deliberate overflow
uint32_t write_idx_lowbits = 0;
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != bit_idx_end_lowbits)) {
uintptr_t cur_mask_word;
// sparse genovec optimization
// guaranteed to terminate since there's at least one more set bit
do {
cur_mask_word = genovec[++read_widx];
cur_mask_word = (~cur_mask_word) & (cur_mask_word >> 1) & kMask5555;
} while (!cur_mask_word);
uintptr_t extracted_bits = raw_bitarr_alias[read_widx];
uint32_t set_bit_ct = kBitsPerWordD2;
if (cur_mask_word != kMask5555) {
const uintptr_t cur_mask_hw = PackWordToHalfword(cur_mask_word);
set_bit_ct = PopcountWord(cur_mask_word);
extracted_bits = _pext_u64(extracted_bits, cur_mask_hw);
}
cur_output_word |= extracted_bits << write_idx_lowbits;
const uint32_t new_write_idx_lowbits = write_idx_lowbits + set_bit_ct;
if (new_write_idx_lowbits >= kBitsPerWord) {
*output_bitarr_iter++ = cur_output_word;
// ...and these are the bits that fell off
// impossible for write_idx_lowbits to be zero here
cur_output_word = extracted_bits >> (kBitsPerWord - write_idx_lowbits);
}
write_idx_lowbits = new_write_idx_lowbits % kBitsPerWord;
}
if (write_idx_lowbits) {
*output_bitarr_iter = cur_output_word;
}
}
#else // !USE_AVX2
void CopyQuaterarrNonemptySubset(const uintptr_t* __restrict raw_quaterarr, const uintptr_t* __restrict subset_mask, uint32_t raw_quaterarr_entry_ct, uint32_t subset_entry_ct, uintptr_t* __restrict output_quaterarr) {
// in plink 2.0, we probably want (0-based) bit raw_quaterarr_entry_ct of
// subset_mask to be always allocated and unset. This removes a few special
// cases re: iterating past the end of arrays.
assert(subset_entry_ct);
assert(raw_quaterarr_entry_ct >= subset_entry_ct);
uintptr_t cur_output_word = 0;
uintptr_t* output_quaterarr_iter = output_quaterarr;
uintptr_t* output_quaterarr_last = &(output_quaterarr[subset_entry_ct / kBitsPerWordD2]);
const uint32_t word_write_halfshift_end = subset_entry_ct % kBitsPerWordD2;
uint32_t word_write_halfshift = 0;
// if <= 2/3-filled, use sparse copy algorithm
// (tried CopyBitarrSubset() approach, that actually worsened things)
if (subset_entry_ct * (3 * k1LU) <= raw_quaterarr_entry_ct * (2 * k1LU)) {
uint32_t subset_mask_widx = 0;
while (1) {
const uintptr_t cur_include_word = subset_mask[subset_mask_widx];
if (cur_include_word) {
uint32_t wordhalf_idx = 0;
uint32_t cur_include_halfword = S_CAST(Halfword, cur_include_word);
while (1) {
if (cur_include_halfword) {
uintptr_t raw_quaterarr_word = raw_quaterarr[subset_mask_widx * 2 + wordhalf_idx];
do {
uint32_t rqa_idx_lowbits = ctzu32(cur_include_halfword);
cur_output_word |= ((raw_quaterarr_word >> (rqa_idx_lowbits * 2)) & 3) << (word_write_halfshift * 2);
if (++word_write_halfshift == kBitsPerWordD2) {
*output_quaterarr_iter++ = cur_output_word;
word_write_halfshift = 0;
cur_output_word = 0;
}
cur_include_halfword &= cur_include_halfword - 1;
} while (cur_include_halfword);
}
if (wordhalf_idx) {
break;
}
++wordhalf_idx;
cur_include_halfword = cur_include_word >> kBitsPerWordD2;
}
if (output_quaterarr_iter == output_quaterarr_last) {
if (word_write_halfshift == word_write_halfshift_end) {
if (word_write_halfshift_end) {
*output_quaterarr_last = cur_output_word;
}
return;
}
}
}
++subset_mask_widx;
}
}
// blocked copy
const uintptr_t* raw_quaterarr_iter = raw_quaterarr;
while (1) {
const uintptr_t cur_include_word = *subset_mask++;
uint32_t wordhalf_idx = 0;
uintptr_t cur_include_halfword = S_CAST(Halfword, cur_include_word);
while (1) {
uintptr_t raw_quaterarr_word = *raw_quaterarr_iter++;
while (cur_include_halfword) {
uint32_t rqa_idx_lowbits = ctzw(cur_include_halfword);
uintptr_t halfword_invshifted = (~cur_include_halfword) >> rqa_idx_lowbits;
uintptr_t raw_quaterarr_curblock_unmasked = raw_quaterarr_word >> (rqa_idx_lowbits * 2);
uint32_t rqa_block_len = ctzw(halfword_invshifted);
uint32_t block_len_limit = kBitsPerWordD2 - word_write_halfshift;
cur_output_word |= raw_quaterarr_curblock_unmasked << (2 * word_write_halfshift);
if (rqa_block_len < block_len_limit) {
word_write_halfshift += rqa_block_len;
cur_output_word &= (k1LU << (word_write_halfshift * 2)) - k1LU;
} else {
// no need to mask, extra bits vanish off the high end
*output_quaterarr_iter++ = cur_output_word;
word_write_halfshift = rqa_block_len - block_len_limit;
if (word_write_halfshift) {
cur_output_word = (raw_quaterarr_curblock_unmasked >> (2 * block_len_limit)) & ((k1LU << (2 * word_write_halfshift)) - k1LU);
} else {
// avoid potential right-shift-[word length]
cur_output_word = 0;
}
}
cur_include_halfword &= (~(k1LU << (rqa_block_len + rqa_idx_lowbits))) + k1LU;
}
if (wordhalf_idx) {
break;
}
++wordhalf_idx;
cur_include_halfword = cur_include_word >> kBitsPerWordD2;
}
if (output_quaterarr_iter == output_quaterarr_last) {
if (word_write_halfshift == word_write_halfshift_end) {
if (word_write_halfshift_end) {
*output_quaterarr_last = cur_output_word;
}
return;
}
}
}
}
void Copy01Subset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genovec, uint32_t write_bit_idx_start, uint32_t bit_ct, uintptr_t* __restrict output_bitarr) {
const uint32_t bit_idx_end = write_bit_idx_start + bit_ct;
const uint32_t bit_idx_end_lowbits = bit_idx_end % kBitsPerWord;
const Halfword* raw_bitarr_alias = R_CAST(const Halfword*, raw_bitarr);
uintptr_t* output_bitarr_iter = output_bitarr;
uintptr_t* output_bitarr_last = &(output_bitarr[bit_idx_end / kBitsPerWord]);
uintptr_t cur_output_word = 0;
uint32_t read_widx = UINT32_MAX; // deliberate overflow
uint32_t write_idx_lowbits = write_bit_idx_start;
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != bit_idx_end_lowbits)) {
uintptr_t geno_hets;
// sparse genovec optimization
// guaranteed to terminate since there's at least one more set bit
do {
geno_hets = genovec[++read_widx];
geno_hets = geno_hets & (~(geno_hets >> 1)) & kMask5555;
} while (!geno_hets);
// screw it, just iterate over set bits
const uint32_t cur_halfword = raw_bitarr_alias[read_widx];
do {
const uint32_t sample_idx_lowbits = ctzw(geno_hets) / 2;
cur_output_word |= S_CAST(uintptr_t, (cur_halfword >> sample_idx_lowbits) & k1LU) << write_idx_lowbits;
if (++write_idx_lowbits == kBitsPerWord) {
*output_bitarr_iter++ = cur_output_word;
cur_output_word = 0;
write_idx_lowbits = 0;
}
geno_hets &= geno_hets - 1;
} while (geno_hets);
}
if (write_idx_lowbits) {
*output_bitarr_iter = cur_output_word;
}
}
void Copy10Subset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genovec, uint32_t bit_ct, uintptr_t* __restrict output_bitarr) {
const uint32_t bit_idx_end_lowbits = bit_ct % kBitsPerWord;
const Halfword* raw_bitarr_alias = R_CAST(const Halfword*, raw_bitarr);
uintptr_t* output_bitarr_iter = output_bitarr;
uintptr_t* output_bitarr_last = &(output_bitarr[bit_ct / kBitsPerWord]);
uintptr_t cur_output_word = 0;
uint32_t read_widx = UINT32_MAX; // deliberate overflow
uint32_t write_idx_lowbits = 0;
while ((output_bitarr_iter != output_bitarr_last) || (write_idx_lowbits != bit_idx_end_lowbits)) {
uintptr_t geno_2alts;
// sparse genovec optimization
// guaranteed to terminate since there's at least one more set bit
do {
geno_2alts = genovec[++read_widx];
geno_2alts = (~geno_2alts) & (geno_2alts >> 1) & kMask5555;
} while (!geno_2alts);
const uint32_t cur_halfword = raw_bitarr_alias[read_widx];
do {
const uint32_t sample_idx_lowbits = ctzw(geno_2alts) / 2;
cur_output_word |= S_CAST(uintptr_t, (cur_halfword >> sample_idx_lowbits) & k1LU) << write_idx_lowbits;
if (++write_idx_lowbits == kBitsPerWord) {
*output_bitarr_iter++ = cur_output_word;
cur_output_word = 0;
write_idx_lowbits = 0;
}
geno_2alts &= geno_2alts - 1;
} while (geno_2alts);
}
if (write_idx_lowbits) {
*output_bitarr_iter = cur_output_word;
}
}
#endif
// Harley-Seal algorithm only works for bitarrays, not quaterarrays, so don't
// add an AVX2 specialization here.
// ...unless something like the interleaved_vec strategy is used? hmm. should
// test this on basic frequency counter.
void Count2FreqVec3(const VecW* geno_vvec, uint32_t vec_ct, uint32_t* __restrict alt1_plus_bothset_ctp, uint32_t* __restrict bothset_ctp) {
assert(!(vec_ct % 3));
// Increments bothset_ct by the number of 0b11 in the current block, and
// alt1_ct by twice the number of 0b10 plus the number of 0b01.
const VecW m1 = VCONST_W(kMask5555);
const VecW m2 = VCONST_W(kMask3333);
const VecW m4 = VCONST_W(kMask0F0F);
const VecW* geno_vvec_iter = geno_vvec;
uint32_t alt1_plus_bothset_ct = 0;
uint32_t bothset_ct = 0;
while (1) {
UniVec acc_alt1_plus_bothset;
UniVec acc_bothset;
acc_alt1_plus_bothset.vw = vecw_setzero();
acc_bothset.vw = vecw_setzero();
const VecW* geno_vvec_stop;
if (vec_ct < 30) {
if (!vec_ct) {
*alt1_plus_bothset_ctp = alt1_plus_bothset_ct;
*bothset_ctp = bothset_ct;
return;
}
geno_vvec_stop = &(geno_vvec_iter[vec_ct]);
vec_ct = 0;
} else {
geno_vvec_stop = &(geno_vvec_iter[30]);
vec_ct -= 30;
}
do {
VecW cur_geno_vword1 = *geno_vvec_iter++;
// process first two vwords simultaneously to minimize linear dependence
VecW cur_geno_vword2 = *geno_vvec_iter++;
VecW cur_geno_vword_low_lshifted1 = vecw_slli(cur_geno_vword1 & m1, 1);
VecW cur_geno_vword_low_lshifted2 = vecw_slli(cur_geno_vword2 & m1, 1);
// 00 -> 00; 01 -> 01; 10 -> 10; 11 -> 01
// note that _mm_andnot_si128 flips the *first* argument before the AND
// operation.
VecW alt1_plus_bothset1 = (~cur_geno_vword_low_lshifted1) & cur_geno_vword1;
VecW alt1_plus_bothset2 = (~cur_geno_vword_low_lshifted2) & cur_geno_vword2;
VecW bothset1 = vecw_srli(cur_geno_vword_low_lshifted1 & cur_geno_vword1, 1);
VecW bothset2 = vecw_srli(cur_geno_vword_low_lshifted2 & cur_geno_vword2, 1);
cur_geno_vword1 = *geno_vvec_iter++;
alt1_plus_bothset1 = (alt1_plus_bothset1 & m2) + (vecw_srli(alt1_plus_bothset1, 2) & m2);
bothset2 = bothset1 + bothset2;
alt1_plus_bothset2 = (alt1_plus_bothset2 & m2) + (vecw_srli(alt1_plus_bothset2, 2) & m2);
cur_geno_vword_low_lshifted1 = vecw_slli(cur_geno_vword1 & m1, 1);
alt1_plus_bothset2 = alt1_plus_bothset1 + alt1_plus_bothset2;
// alt1_plus_bothset2 now contains 4-bit values from 0-8, while bothset2
// contains 2-bit values from 0-2
// (todo: check whether this is faster if we use double_bothsetx
// variables instead of bothset1/bothset2)
bothset1 = vecw_srli(cur_geno_vword_low_lshifted1 & cur_geno_vword1, 1);
alt1_plus_bothset1 = (~cur_geno_vword_low_lshifted1) & cur_geno_vword1;
bothset2 = bothset1 + bothset2;
alt1_plus_bothset1 = (alt1_plus_bothset1 & m2) + (vecw_srli(alt1_plus_bothset1, 2) & m2);
bothset2 = (bothset2 & m2) + (vecw_srli(bothset2, 2) & m2);
alt1_plus_bothset2 = alt1_plus_bothset1 + alt1_plus_bothset2;
// alt1_plus_bothset2 now contains 4-bit values from 0-12, while bothset2
// contains 4-bit values from 0-6. aggregate both into 8-bit values.
bothset2 = (bothset2 & m4) + (vecw_srli(bothset2, 4) & m4);
alt1_plus_bothset2 = (alt1_plus_bothset2 & m4) + (vecw_srli(alt1_plus_bothset2, 4) & m4);
acc_bothset.vw = acc_bothset.vw + bothset2;
acc_alt1_plus_bothset.vw = acc_alt1_plus_bothset.vw + alt1_plus_bothset2;
} while (geno_vvec_iter < geno_vvec_stop);
const VecW m8 = VCONST_W(kMask00FF);
acc_bothset.vw = (acc_bothset.vw + vecw_srli(acc_bothset.vw, 8)) & m8;
acc_alt1_plus_bothset.vw = (acc_alt1_plus_bothset.vw & m8) + (vecw_srli(acc_alt1_plus_bothset.vw, 8) & m8);
bothset_ct += UniVecHsum16(acc_bothset);
alt1_plus_bothset_ct += UniVecHsum16(acc_alt1_plus_bothset);
}
}
void Count3FreqVec6(const VecW* geno_vvec, uint32_t vec_ct, uint32_t* __restrict even_ctp, uint32_t* __restrict odd_ctp, uint32_t* __restrict bothset_ctp) {
assert(!(vec_ct % 6));
// Sets even_ct to the number of set low bits in the current block, odd_ct to
// the number of set high bits, and bothset_ct by the number of 0b11s.
// Easy to adapt this to take a subset quatervec parameter.
const VecW m1 = VCONST_W(kMask5555);
const VecW m2 = VCONST_W(kMask3333);
const VecW m4 = VCONST_W(kMask0F0F);
const VecW* geno_vvec_iter = geno_vvec;
uint32_t even_ct = 0;
uint32_t odd_ct = 0;
uint32_t bothset_ct = 0;
while (1) {
UniVec acc_even;
UniVec acc_odd;
UniVec acc_bothset;
acc_even.vw = vecw_setzero();
acc_odd.vw = vecw_setzero();
acc_bothset.vw = vecw_setzero();
const VecW* geno_vvec_stop;
if (vec_ct < 60) {
if (!vec_ct) {
*even_ctp = even_ct;
*odd_ctp = odd_ct;
*bothset_ctp = bothset_ct;
return;
}
geno_vvec_stop = &(geno_vvec_iter[vec_ct]);
vec_ct = 0;
} else {
geno_vvec_stop = &(geno_vvec_iter[60]);
vec_ct -= 60;
}
do {
// hmm, this seems to have more linear dependence than I'd want, but the
// reorderings I tried just made the code harder to read without helping,
// so I'll leave this alone
VecW cur_geno_vword = *geno_vvec_iter++;
VecW odd1 = m1 & vecw_srli(cur_geno_vword, 1);
VecW even1 = m1 & cur_geno_vword;
VecW bothset1 = odd1 & cur_geno_vword;
cur_geno_vword = *geno_vvec_iter++;
VecW cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1);
even1 = even1 + (m1 & cur_geno_vword);
odd1 = odd1 + cur_geno_vword_high;
bothset1 = bothset1 + (cur_geno_vword_high & cur_geno_vword);
cur_geno_vword = *geno_vvec_iter++;
cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1);
even1 = even1 + (m1 & cur_geno_vword);
odd1 = odd1 + cur_geno_vword_high;
bothset1 = bothset1 + (cur_geno_vword_high & cur_geno_vword);
even1 = (even1 & m2) + (vecw_srli(even1, 2) & m2);
odd1 = (odd1 & m2) + (vecw_srli(odd1, 2) & m2);
bothset1 = (bothset1 & m2) + (vecw_srli(bothset1, 2) & m2);
cur_geno_vword = *geno_vvec_iter++;
VecW odd2 = m1 & vecw_srli(cur_geno_vword, 1);
VecW even2 = m1 & cur_geno_vword;
VecW bothset2 = odd2 & cur_geno_vword;
cur_geno_vword = *geno_vvec_iter++;
cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1);
even2 = even2 + (m1 & cur_geno_vword);
odd2 = odd2 + cur_geno_vword_high;
bothset2 = bothset2 + (cur_geno_vword_high & cur_geno_vword);
cur_geno_vword = *geno_vvec_iter++;
cur_geno_vword_high = m1 & vecw_srli(cur_geno_vword, 1);
even2 = even2 + (m1 & cur_geno_vword);
odd2 = odd2 + cur_geno_vword_high;
bothset2 = bothset2 + (cur_geno_vword_high & cur_geno_vword);
even1 = even1 + (even2 & m2) + (vecw_srli(even2, 2) & m2);
odd1 = odd1 + (odd2 & m2) + (vecw_srli(odd2, 2) & m2);
bothset1 = bothset1 + (bothset2 & m2) + (vecw_srli(bothset2, 2) & m2);
// these now contain 4-bit values from 0-12
acc_even.vw = acc_even.vw + (even1 & m4) + (vecw_srli(even1, 4) & m4);
acc_odd.vw = acc_odd.vw + (odd1 & m4) + (vecw_srli(odd1, 4) & m4);
acc_bothset.vw = acc_bothset.vw + (bothset1 & m4) + (vecw_srli(bothset1, 4) & m4);
} while (geno_vvec_iter < geno_vvec_stop);
const VecW m8 = VCONST_W(kMask00FF);
acc_even.vw = (acc_even.vw & m8) + (vecw_srli(acc_even.vw, 8) & m8);
acc_odd.vw = (acc_odd.vw & m8) + (vecw_srli(acc_odd.vw, 8) & m8);
acc_bothset.vw = (acc_bothset.vw & m8) + (vecw_srli(acc_bothset.vw, 8) & m8);
even_ct += UniVecHsum16(acc_even);
odd_ct += UniVecHsum16(acc_odd);
bothset_ct += UniVecHsum16(acc_bothset);
}
}
void CountSubset3FreqVec6(const VecW* __restrict geno_vvec, const VecW* __restrict interleaved_mask_vvec, uint32_t vec_ct, uint32_t* __restrict even_ctp, uint32_t* __restrict odd_ctp, uint32_t* __restrict bothset_ctp) {
assert(!(vec_ct % 6));
// Sets even_ct to the number of set low bits in the current block, odd_ct to
// the number of set high bits, and bothset_ct by the number of 0b11s.
// Easy to adapt this to take a subset quatervec parameter.
const VecW m1 = VCONST_W(kMask5555);
const VecW m2 = VCONST_W(kMask3333);
const VecW m4 = VCONST_W(kMask0F0F);
const VecW* geno_vvec_iter = geno_vvec;
const VecW* interleaved_mask_vvec_iter = interleaved_mask_vvec;
uint32_t even_ct = 0;
uint32_t odd_ct = 0;
uint32_t bothset_ct = 0;
while (1) {
UniVec acc_even;
UniVec acc_odd;
UniVec acc_bothset;
acc_even.vw = vecw_setzero();
acc_odd.vw = vecw_setzero();
acc_bothset.vw = vecw_setzero();
const VecW* geno_vvec_stop;
if (vec_ct < 60) {
if (!vec_ct) {
*even_ctp = even_ct;
*odd_ctp = odd_ct;
*bothset_ctp = bothset_ct;
return;
}
geno_vvec_stop = &(geno_vvec_iter[vec_ct]);
vec_ct = 0;
} else {
geno_vvec_stop = &(geno_vvec_iter[60]);
vec_ct -= 60;
}
do {
VecW interleaved_mask_vword = *interleaved_mask_vvec_iter++;
VecW cur_geno_vword = *geno_vvec_iter++;
VecW cur_mask = interleaved_mask_vword & m1;
VecW odd1 = cur_mask & vecw_srli(cur_geno_vword, 1);
VecW even1 = cur_mask & cur_geno_vword;
VecW bothset1 = odd1 & cur_geno_vword;
cur_mask = vecw_srli(interleaved_mask_vword, 1) & m1;
cur_geno_vword = *geno_vvec_iter++;
VecW cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1);
even1 = even1 + (cur_mask & cur_geno_vword);
odd1 = odd1 + cur_geno_vword_high_masked;
bothset1 = bothset1 + (cur_geno_vword_high_masked & cur_geno_vword);
interleaved_mask_vword = *interleaved_mask_vvec_iter++;
cur_mask = interleaved_mask_vword & m1;
cur_geno_vword = *geno_vvec_iter++;
cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1);
even1 = even1 + (cur_mask & cur_geno_vword);
odd1 = odd1 + cur_geno_vword_high_masked;
bothset1 = bothset1 + (cur_geno_vword_high_masked & cur_geno_vword);
even1 = (even1 & m2) + (vecw_srli(even1, 2) & m2);
odd1 = (odd1 & m2) + (vecw_srli(odd1, 2) & m2);
bothset1 = (bothset1 & m2) + (vecw_srli(bothset1, 2) & m2);
cur_mask = vecw_srli(interleaved_mask_vword, 1) & m1;
cur_geno_vword = *geno_vvec_iter++;
VecW odd2 = cur_mask & vecw_srli(cur_geno_vword, 1);
VecW even2 = cur_mask & cur_geno_vword;
VecW bothset2 = odd2 & cur_geno_vword;
interleaved_mask_vword = *interleaved_mask_vvec_iter++;
cur_mask = interleaved_mask_vword & m1;
cur_geno_vword = *geno_vvec_iter++;
cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1);
even2 = even2 + (cur_mask & cur_geno_vword);
odd2 = odd2 + cur_geno_vword_high_masked;
bothset2 = bothset2 + (cur_geno_vword_high_masked & cur_geno_vword);
cur_mask = vecw_srli(interleaved_mask_vword, 1) & m1;
cur_geno_vword = *geno_vvec_iter++;
cur_geno_vword_high_masked = cur_mask & vecw_srli(cur_geno_vword, 1);
even2 = even2 + (cur_mask & cur_geno_vword);
odd2 = odd2 + cur_geno_vword_high_masked;
bothset2 = bothset2 + (cur_geno_vword_high_masked & cur_geno_vword);
even1 = even1 + (even2 & m2) + (vecw_srli(even2, 2) & m2);
odd1 = odd1 + (odd2 & m2) + (vecw_srli(odd2, 2) & m2);
bothset1 = bothset1 + (bothset2 & m2) + (vecw_srli(bothset2, 2) & m2);
// these now contain 4-bit values from 0-12
acc_even.vw = acc_even.vw + (even1 & m4) + (vecw_srli(even1, 4) & m4);
acc_odd.vw = acc_odd.vw + (odd1 & m4) + (vecw_srli(odd1, 4) & m4);
acc_bothset.vw = acc_bothset.vw + (bothset1 & m4) + (vecw_srli(bothset1, 4) & m4);
} while (geno_vvec_iter < geno_vvec_stop);
const VecW m8 = VCONST_W(kMask00FF);
acc_even.vw = (acc_even.vw & m8) + (vecw_srli(acc_even.vw, 8) & m8);
acc_odd.vw = (acc_odd.vw & m8) + (vecw_srli(acc_odd.vw, 8) & m8);
acc_bothset.vw = (acc_bothset.vw & m8) + (vecw_srli(acc_bothset.vw, 8) & m8);
even_ct += UniVecHsum16(acc_even);
odd_ct += UniVecHsum16(acc_odd);
bothset_ct += UniVecHsum16(acc_bothset);
}
}
// possible todo: AVX2
uint32_t Count01Vec6(const VecW* geno_vvec, uint32_t vec_ct) {
assert(!(vec_ct % 6));
const VecW m1 = VCONST_W(kMask5555);
const VecW m2 = VCONST_W(kMask3333);
const VecW m4 = VCONST_W(kMask0F0F);
const VecW m8 = VCONST_W(kMask00FF);
const VecW* geno_vvec_iter = geno_vvec;
uint32_t tot = 0;
while (1) {
UniVec acc;
acc.vw = vecw_setzero();
const VecW* geno_vvec_stop;
if (vec_ct < 60) {
if (!vec_ct) {
return tot;
}
geno_vvec_stop = &(geno_vvec_iter[vec_ct]);
vec_ct = 0;
} else {
geno_vvec_stop = &(geno_vvec_iter[60]);
vec_ct -= 60;
}
do {
VecW loader1 = *geno_vvec_iter++;
VecW loader2 = *geno_vvec_iter++;
VecW count1 = ((~vecw_srli(loader1, 1)) & loader1) & m1;
VecW count2 = ((~vecw_srli(loader2, 1)) & loader2) & m1;
loader1 = *geno_vvec_iter++;
loader2 = *geno_vvec_iter++;
count1 = count1 + (((~vecw_srli(loader1, 1)) & loader1) & m1);
count2 = count2 + (((~vecw_srli(loader2, 1)) & loader2) & m1);
loader1 = *geno_vvec_iter++;
loader2 = *geno_vvec_iter++;
count1 = count1 + (((~vecw_srli(loader1, 1)) & loader1) & m1);
count2 = count2 + (((~vecw_srli(loader2, 1)) & loader2) & m1);
count1 = (count1 & m2) + (vecw_srli(count1, 2) & m2);
count1 = count1 + (count2 & m2) + (vecw_srli(count2, 2) & m2);
acc.vw = acc.vw + (count1 & m4) + (vecw_srli(count1, 4) & m4);
} while (geno_vvec_iter < geno_vvec_stop);
acc.vw = (acc.vw & m8) + (vecw_srli(acc.vw, 8) & m8);
tot += UniVecHsum16(acc);
}
}
// This type of code can probably be improved; see
// https://lemire.me/blog/2018/01/09/how-fast-can-you-bit-interleave-32-bit-integers-simd-edition/ .
// (Also, it may be worthwhile to add a compilation option which generates AVX2
// instructions but mostly avoids _pdep_u64() and _pext_u64(); apparently even
// the latest AMD processors have poor implementations of those two
// operations?)
void FillInterleavedMaskVec(const uintptr_t* __restrict subset_mask, uint32_t base_vec_ct, uintptr_t* interleaved_mask_vec) {
#ifdef __LP64__
const uintptr_t* subset_mask_iter = subset_mask;
uintptr_t* interleaved_mask_vec_iter = interleaved_mask_vec;
# ifdef USE_AVX2
uintptr_t orig_word1 = 0;
uintptr_t orig_word3 = 0;
# endif
for (uint32_t vec_idx = 0; vec_idx < base_vec_ct; ++vec_idx) {
# ifdef USE_AVX2
// 0 128 1 129 2 130 ...
for (uint32_t widx = 0; widx < 4; ++widx) {
uintptr_t ww_even;
uintptr_t ww_odd;
if (!(widx % 2)) {
orig_word1 = subset_mask_iter[0];
orig_word3 = subset_mask_iter[2];
++subset_mask_iter;
ww_even = S_CAST(uint32_t, orig_word1);
ww_odd = S_CAST(uint32_t, orig_word3);
} else {
ww_even = orig_word1 >> 32;
ww_odd = orig_word3 >> 32;
}
ww_even = UnpackHalfwordToWord(ww_even);
ww_odd = UnpackHalfwordToWord(ww_odd);
*interleaved_mask_vec_iter++ = ww_even | (ww_odd << 1);
}
subset_mask_iter = &(subset_mask_iter[2]);
# else // not USE_AVX2
// 0 64 1 65 2 66 ...
const uintptr_t orig_word1 = *subset_mask_iter++;
const uintptr_t orig_word2 = *subset_mask_iter++;
for (uint32_t widx = 0; widx < 2; ++widx) {
uintptr_t ww_even;
uintptr_t ww_odd;
// todo: check if there's a better way to organize this loop
if (!widx) {
ww_even = S_CAST(uint32_t, orig_word1);
ww_odd = S_CAST(uint32_t, orig_word2);
} else {
ww_even = orig_word1 >> 32;
ww_odd = orig_word2 >> 32;
}
ww_even = UnpackHalfwordToWord(ww_even);
ww_odd = UnpackHalfwordToWord(ww_odd);
*interleaved_mask_vec_iter++ = ww_even | (ww_odd << 1);
}
# endif // not USE_AVX2
}
#else
for (uint32_t widx = 0; widx < base_vec_ct; ++widx) {
const uintptr_t orig_word = subset_mask[widx];
uintptr_t ww_even = S_CAST(uint16_t, orig_word);
uintptr_t ww_odd = orig_word >> 16;
ww_even = UnpackHalfwordToWord(ww_even);
ww_odd = UnpackHalfwordToWord(ww_odd);
interleaved_mask_vec[widx] = ww_even | (ww_odd << 1);
}
#endif
}
void GenovecAlleleCtsUnsafe(const uintptr_t* genovec, uint32_t sample_ct, uint32_t* __restrict allele_cts, uint32_t* __restrict bothset_ctp) {
// assumes trailing bits of last genovec word are zeroed out.
// sets allele_cts[0] to the number of observed ref alleles, and
// allele_cts[1] to the number of observed alt1s.
const uint32_t sample_ctl2 = QuaterCtToWordCt(sample_ct);
uint32_t word_idx = sample_ctl2 - (sample_ctl2 % (3 * kWordsPerVec));
uint32_t alt1_plus_bothset_ct;
uint32_t bothset_ct;
assert(VecIsAligned(genovec));
Count2FreqVec3(R_CAST(const VecW*, genovec), word_idx / kWordsPerVec, &alt1_plus_bothset_ct, &bothset_ct);
for (; word_idx < sample_ctl2; ++word_idx) {
const uintptr_t cur_geno_word = genovec[word_idx];
const uintptr_t cur_geno_word_low_lshifted = (cur_geno_word & kMask5555) << 1;
alt1_plus_bothset_ct += QuatersumWord((~cur_geno_word_low_lshifted) & cur_geno_word);
bothset_ct += QuatersumWord(cur_geno_word_low_lshifted & cur_geno_word);
}
const uint32_t alt1_ct = alt1_plus_bothset_ct - bothset_ct;
allele_cts[0] = (sample_ct - bothset_ct) * 2 - alt1_ct;
allele_cts[1] = alt1_ct;
*bothset_ctp = bothset_ct;
}
void GenovecCountFreqsUnsafe(const uintptr_t* genovec, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) {
// fills genocounts[0] with the number of 00s, genocounts[1] with the number
// of 01s, etc.
// assumes trailing bits of last genovec word are zeroed out.
// sample_ct == 0 ok.
const uint32_t sample_ctl2 = QuaterCtToWordCt(sample_ct);
uint32_t even_ct;
uint32_t odd_ct;
uint32_t bothset_ct;
uint32_t word_idx = sample_ctl2 - (sample_ctl2 % (6 * kWordsPerVec));
assert(VecIsAligned(genovec));
Count3FreqVec6(R_CAST(const VecW*, genovec), word_idx / kWordsPerVec, &even_ct, &odd_ct, &bothset_ct);
for (; word_idx < sample_ctl2; ++word_idx) {
const uintptr_t cur_geno_word = genovec[word_idx];
const uintptr_t cur_geno_word_high = kMask5555 & (cur_geno_word >> 1);
even_ct += Popcount01Word(cur_geno_word & kMask5555);
odd_ct += Popcount01Word(cur_geno_word_high);
bothset_ct += Popcount01Word(cur_geno_word & cur_geno_word_high);
}
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct;
genocounts[1] = even_ct - bothset_ct;
genocounts[2] = odd_ct - bothset_ct;
genocounts[3] = bothset_ct;
}
void GenovecCountSubsetFreqs(const uintptr_t* __restrict genovec, const uintptr_t* __restrict sample_include_interleaved_vec, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) {
// fills genocounts[0] with the number of 00s, genocounts[1] with the number
// of 01s, etc.
// {raw_}sample_ct == 0 ok.
const uint32_t raw_sample_ctv2 = QuaterCtToVecCt(raw_sample_ct);
uint32_t even_ct;
uint32_t odd_ct;
uint32_t bothset_ct;
#ifdef __LP64__
uint32_t vec_idx = raw_sample_ctv2 - (raw_sample_ctv2 % 6);
assert(VecIsAligned(genovec));
CountSubset3FreqVec6(R_CAST(const VecW*, genovec), R_CAST(const VecW*, sample_include_interleaved_vec), vec_idx, &even_ct, &odd_ct, &bothset_ct);
const uintptr_t* genovec_iter = &(genovec[kWordsPerVec * vec_idx]);
const uintptr_t* interleaved_mask_iter = &(sample_include_interleaved_vec[(kWordsPerVec / 2) * vec_idx]);
# ifdef USE_AVX2
uintptr_t mask_base1 = 0;
uintptr_t mask_base2 = 0;
uintptr_t mask_base3 = 0;
uintptr_t mask_base4 = 0;
for (; vec_idx < raw_sample_ctv2; ++vec_idx) {
uintptr_t mask_word1;
uintptr_t mask_word2;
uintptr_t mask_word3;
uintptr_t mask_word4;
if (!(vec_idx % 2)) {
mask_base1 = *interleaved_mask_iter++;
mask_base2 = *interleaved_mask_iter++;
mask_base3 = *interleaved_mask_iter++;
mask_base4 = *interleaved_mask_iter++;
mask_word1 = mask_base1 & kMask5555;
mask_word2 = mask_base2 & kMask5555;
mask_word3 = mask_base3 & kMask5555;
mask_word4 = mask_base4 & kMask5555;
} else {
mask_word1 = (mask_base1 >> 1) & kMask5555;
mask_word2 = (mask_base2 >> 1) & kMask5555;
mask_word3 = (mask_base3 >> 1) & kMask5555;
mask_word4 = (mask_base4 >> 1) & kMask5555;
}
uint32_t uii = 0;
while (1) {
const uintptr_t cur_geno_word1 = *genovec_iter++;
const uintptr_t cur_geno_word2 = *genovec_iter++;
const uintptr_t cur_geno_word1_high_masked = mask_word1 & (cur_geno_word1 >> 1);
const uintptr_t cur_geno_word2_high_masked = mask_word2 & (cur_geno_word2 >> 1);
even_ct += PopcountWord(((cur_geno_word1 & mask_word1) << 1) | (cur_geno_word2 & mask_word2));
odd_ct += PopcountWord((cur_geno_word1_high_masked << 1) | cur_geno_word2_high_masked);
bothset_ct += PopcountWord(((cur_geno_word1 & cur_geno_word1_high_masked) << 1) | (cur_geno_word2 & cur_geno_word2_high_masked));
if (uii) {
break;
}
++uii;
mask_word1 = mask_word3;
mask_word2 = mask_word4;
}
}
# else // not USE_AVX2
uintptr_t mask_base1 = 0;
uintptr_t mask_base2 = 0;
for (; vec_idx < raw_sample_ctv2; ++vec_idx) {
uintptr_t mask_word1;
uintptr_t mask_word2;
if (!(vec_idx % 2)) {
mask_base1 = *interleaved_mask_iter++;
mask_base2 = *interleaved_mask_iter++;
mask_word1 = mask_base1 & kMask5555;
mask_word2 = mask_base2 & kMask5555;
} else {
mask_word1 = (mask_base1 >> 1) & kMask5555;
mask_word2 = (mask_base2 >> 1) & kMask5555;
}
const uintptr_t cur_geno_word1 = *genovec_iter++;
const uintptr_t cur_geno_word2 = *genovec_iter++;
const uintptr_t cur_geno_word1_high_masked = mask_word1 & (cur_geno_word1 >> 1);
const uintptr_t cur_geno_word2_high_masked = mask_word2 & (cur_geno_word2 >> 1);
# ifdef USE_SSE42
even_ct += PopcountWord(((cur_geno_word1 & mask_word1) << 1) | (cur_geno_word2 & mask_word2));
odd_ct += PopcountWord((cur_geno_word1_high_masked << 1) | cur_geno_word2_high_masked);
bothset_ct += PopcountWord(((cur_geno_word1 & cur_geno_word1_high_masked) << 1) | (cur_geno_word2 & cur_geno_word2_high_masked));
# else
even_ct += QuatersumWord((cur_geno_word1 & mask_word1) + (cur_geno_word2 & mask_word2));
odd_ct += QuatersumWord(cur_geno_word1_high_masked + cur_geno_word2_high_masked);
bothset_ct += QuatersumWord((cur_geno_word1 & cur_geno_word1_high_masked) + (cur_geno_word2 & cur_geno_word2_high_masked));
# endif
}
# endif // not USE_AVX2
#else // not __LP64__
uint32_t word_idx = raw_sample_ctv2 - (raw_sample_ctv2 % 6);
CountSubset3FreqVec6(R_CAST(const VecW*, genovec), R_CAST(const VecW*, sample_include_interleaved_vec), word_idx, &even_ct, &odd_ct, &bothset_ct);
const uintptr_t* interleaved_mask_iter = &(sample_include_interleaved_vec[word_idx / 2]);
uintptr_t mask_base = 0;
for (; word_idx < raw_sample_ctv2; ++word_idx) {
uintptr_t mask_word;
if (!(word_idx % 2)) {
mask_base = *interleaved_mask_iter++;
mask_word = mask_base & kMask5555;
} else {
mask_word = (mask_base >> 1) & kMask5555;
}
const uintptr_t cur_geno_word = genovec[word_idx];
const uintptr_t cur_geno_word_high_masked = mask_word & (cur_geno_word >> 1);
even_ct += Popcount01Word(cur_geno_word & mask_word);
odd_ct += Popcount01Word(cur_geno_word_high_masked);
bothset_ct += Popcount01Word(cur_geno_word & cur_geno_word_high_masked);
}
#endif
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct;
genocounts[1] = even_ct - bothset_ct;
genocounts[2] = odd_ct - bothset_ct;
genocounts[3] = bothset_ct;
}
uint32_t GenovecCount01Unsafe(const uintptr_t* genovec, uint32_t sample_ct) {
const uint32_t sample_ctl2 = QuaterCtToWordCt(sample_ct);
uint32_t word_idx = sample_ctl2 - (sample_ctl2 % (6 * kWordsPerVec));
assert(VecIsAligned(genovec));
uint32_t tot = Count01Vec6(R_CAST(const VecW*, genovec), word_idx / kWordsPerVec);
for (; word_idx < sample_ctl2; ++word_idx) {
const uintptr_t cur_geno_word = genovec[word_idx];
tot += Popcount01Word(cur_geno_word & (~(cur_geno_word >> 1)) & kMask5555);
}
return tot;
}
#ifdef __arm__
# error "Unaligned accesses in SmallGenoarrCount3FreqIncr()."
#endif
void SmallGenoarrCount3FreqIncr(const uintptr_t* genoarr_iter, uint32_t byte_ct, uint32_t* even_ctp, uint32_t* odd_ctp, uint32_t* bothset_ctp) {
while (1) {
uintptr_t cur_geno_word;
if (byte_ct < kBytesPerWord) {
if (!byte_ct) {
return;
}
cur_geno_word = ProperSubwordLoad(genoarr_iter, byte_ct);
byte_ct = 0;
} else {
cur_geno_word = *genoarr_iter++;
byte_ct -= kBytesPerWord;
}
const uintptr_t cur_geno_word_high = kMask5555 & (cur_geno_word >> 1);
*even_ctp += Popcount01Word(cur_geno_word & kMask5555);
*odd_ctp += Popcount01Word(cur_geno_word_high);
*bothset_ctp += Popcount01Word(cur_geno_word & cur_geno_word_high);
}
}
void GenoarrCountFreqs(const unsigned char* genoarr, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) {
// does not read past the end of genoarr
uint32_t lead_byte_ct = (-R_CAST(uintptr_t, genoarr)) % kBytesPerVec;
uint32_t even_ct = 0;
uint32_t odd_ct = 0;
uint32_t bothset_ct = 0;
const uintptr_t* genoarr_iter;
uint32_t trail_ct;
if (sample_ct > lead_byte_ct * 4 + (6 * kQuatersPerVec)) {
const uint32_t remaining_sample_ct = sample_ct - 4 * lead_byte_ct;
// strictly speaking, this relies on undefined behavior: see e.g.
// http://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html
// Probably want to search out all instances of __arm__ and make the code
// standard-compliant, if that can be done without a speed penalty. Though
// it makes sense to wait until more is known about Apple's MacBook
// processor plans...
SmallGenoarrCount3FreqIncr(R_CAST(const uintptr_t*, genoarr), lead_byte_ct, &even_ct, &odd_ct, &bothset_ct);
genoarr_iter = R_CAST(const uintptr_t*, &(genoarr[lead_byte_ct]));
const uint32_t remaining_full_vec_ct = remaining_sample_ct / kQuatersPerVec;
uint32_t even_ct_incr;
uint32_t odd_ct_incr;
uint32_t bothset_ct_incr;
const uint32_t vec_ct = remaining_full_vec_ct - (remaining_full_vec_ct % 6);
Count3FreqVec6(R_CAST(const VecW*, genoarr_iter), vec_ct, &even_ct_incr, &odd_ct_incr, &bothset_ct_incr);
even_ct += even_ct_incr;
odd_ct += odd_ct_incr;
bothset_ct += bothset_ct_incr;
genoarr_iter = &(genoarr_iter[kWordsPerVec * vec_ct]);
trail_ct = remaining_sample_ct - (vec_ct * kQuatersPerVec);
} else {
genoarr_iter = R_CAST(const uintptr_t*, genoarr);
trail_ct = sample_ct;
}
const uint32_t trail_byte_ct = QuaterCtToByteCt(trail_ct);
SmallGenoarrCount3FreqIncr(genoarr_iter, trail_byte_ct, &even_ct, &odd_ct, &bothset_ct);
genocounts[0] = sample_ct + bothset_ct - even_ct - odd_ct;
genocounts[1] = even_ct - bothset_ct;
genocounts[2] = odd_ct - bothset_ct;
genocounts[3] = bothset_ct;
}
#ifdef __arm__
# error "Unaligned accesses in GenoarrCountSubsetFreqs()."
#endif
void GenoarrCountSubsetFreqs(const unsigned char* genoarr, const uintptr_t* __restrict sample_include_interleaved_vec, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts) {
// does not read past the end of genoarr
const uintptr_t* genoarr_iter = R_CAST(const uintptr_t*, genoarr);
const uintptr_t* interleaved_mask_iter = sample_include_interleaved_vec;
const uint32_t raw_sample_ctv2 = QuaterCtToVecCt(raw_sample_ct);
uint32_t even_ct = 0;
uint32_t odd_ct = 0;
uint32_t bothset_ct = 0;
#ifdef USE_AVX2
const uint32_t halfvec_idx_trail = (raw_sample_ct + 3) / (kBitsPerVec / 4);
uintptr_t mask_base1 = 0;
uintptr_t mask_base2 = 0;
uintptr_t mask_base3 = 0;
uintptr_t mask_base4 = 0;
for (uint32_t vec_idx = 0; vec_idx < raw_sample_ctv2; ++vec_idx) {
uintptr_t mask_word1;
uintptr_t mask_word2;
uintptr_t mask_word3;
uintptr_t mask_word4;
if (!(vec_idx % 2)) {
mask_base1 = *interleaved_mask_iter++;
mask_base2 = *interleaved_mask_iter++;
mask_base3 = *interleaved_mask_iter++;
mask_base4 = *interleaved_mask_iter++;