/
BitArray.cs
1186 lines (1028 loc) · 51.8 KB
/
BitArray.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers.Binary;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
namespace System.Collections
{
// A vector of bits. Use this to store bits efficiently, without having to do bit
// shifting yourself.
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class BitArray : ICollection, ICloneable
{
private int[] m_array; // Do not rename (binary serialization)
private int m_length; // Do not rename (binary serialization)
private int _version; // Do not rename (binary serialization)
private const int _ShrinkThreshold = 256;
/*=========================================================================
** Allocates space to hold length bit values. All of the values in the bit
** array are set to false.
**
** Exceptions: ArgumentException if length < 0.
=========================================================================*/
public BitArray(int length)
: this(length, false)
{
}
/*=========================================================================
** Allocates space to hold length bit values. All of the values in the bit
** array are set to defaultValue.
**
** Exceptions: ArgumentOutOfRangeException if length < 0.
=========================================================================*/
public BitArray(int length, bool defaultValue)
{
ArgumentOutOfRangeException.ThrowIfNegative(length);
m_array = new int[GetInt32ArrayLengthFromBitLength(length)];
m_length = length;
if (defaultValue)
{
Array.Fill(m_array, -1);
// clear high bit values in the last int
Div32Rem(length, out int extraBits);
if (extraBits > 0)
{
m_array[^1] = (1 << extraBits) - 1;
}
}
_version = 0;
}
/*=========================================================================
** Allocates space to hold the bit values in bytes. bytes[0] represents
** bits 0 - 7, bytes[1] represents bits 8 - 15, etc. The LSB of each byte
** represents the lowest index value; bytes[0] & 1 represents bit 0,
** bytes[0] & 2 represents bit 1, bytes[0] & 4 represents bit 2, etc.
**
** Exceptions: ArgumentException if bytes == null.
=========================================================================*/
public BitArray(byte[] bytes)
{
ArgumentNullException.ThrowIfNull(bytes);
// this value is chosen to prevent overflow when computing m_length.
// m_length is of type int32 and is exposed as a property, so
// type of m_length can't be changed to accommodate.
if (bytes.Length > int.MaxValue / BitsPerByte)
{
throw new ArgumentException(SR.Format(SR.Argument_ArrayTooLarge, BitsPerByte), nameof(bytes));
}
m_array = new int[GetInt32ArrayLengthFromByteLength(bytes.Length)];
m_length = bytes.Length * BitsPerByte;
uint totalCount = (uint)bytes.Length / 4;
ReadOnlySpan<byte> byteSpan = bytes;
for (int i = 0; i < totalCount; i++)
{
m_array[i] = BinaryPrimitives.ReadInt32LittleEndian(byteSpan);
byteSpan = byteSpan.Slice(4);
}
Debug.Assert(byteSpan.Length >= 0 && byteSpan.Length < 4);
int last = 0;
switch (byteSpan.Length)
{
case 3:
last = byteSpan[2] << 16;
goto case 2;
// fall through
case 2:
last |= byteSpan[1] << 8;
goto case 1;
// fall through
case 1:
m_array[totalCount] = last | byteSpan[0];
break;
}
_version = 0;
}
public unsafe BitArray(bool[] values)
{
ArgumentNullException.ThrowIfNull(values);
m_array = new int[GetInt32ArrayLengthFromBitLength(values.Length)];
m_length = values.Length;
uint i = 0;
if (values.Length < Vector256<byte>.Count)
{
goto LessThan32;
}
// Comparing with 1s would get rid of the final negation, however this would not work for some CLR bools
// (true for any non-zero values, false for 0) - any values between 2-255 will be interpreted as false.
// Instead, We compare with zeroes (== false) then negate the result to ensure compatibility.
ref byte value = ref Unsafe.As<bool, byte>(ref MemoryMarshal.GetArrayDataReference<bool>(values));
if (Vector512.IsHardwareAccelerated)
{
for (; i <= (uint)values.Length - Vector512<byte>.Count; i += (uint)Vector512<byte>.Count)
{
Vector512<byte> vector = Vector512.LoadUnsafe(ref value, i);
Vector512<byte> isFalse = Vector512.Equals(vector, Vector512<byte>.Zero);
ulong result = isFalse.ExtractMostSignificantBits();
m_array[i / 32u] = (int)(~result & 0x00000000FFFFFFFF);
m_array[(i / 32u) + 1] = (int)((~result >> 32) & 0x00000000FFFFFFFF);
}
}
else if (Vector256.IsHardwareAccelerated)
{
for (; i <= (uint)values.Length - Vector256<byte>.Count; i += (uint)Vector256<byte>.Count)
{
Vector256<byte> vector = Vector256.LoadUnsafe(ref value, i);
Vector256<byte> isFalse = Vector256.Equals(vector, Vector256<byte>.Zero);
uint result = isFalse.ExtractMostSignificantBits();
m_array[i / 32u] = (int)(~result);
}
}
else if (Vector128.IsHardwareAccelerated)
{
for (; i <= (uint)values.Length - Vector128<byte>.Count * 2u; i += (uint)Vector128<byte>.Count * 2u)
{
Vector128<byte> lowerVector = Vector128.LoadUnsafe(ref value, i);
Vector128<byte> lowerIsFalse = Vector128.Equals(lowerVector, Vector128<byte>.Zero);
uint lowerResult = lowerIsFalse.ExtractMostSignificantBits();
Vector128<byte> upperVector = Vector128.LoadUnsafe(ref value, i + (uint)Vector128<byte>.Count);
Vector128<byte> upperIsFalse = Vector128.Equals(upperVector, Vector128<byte>.Zero);
uint upperResult = upperIsFalse.ExtractMostSignificantBits();
m_array[i / 32u] = (int)(~((upperResult << 16) | lowerResult));
}
}
LessThan32:
for (; i < (uint)values.Length; i++)
{
if (values[i])
{
int elementIndex = Div32Rem((int)i, out int extraBits);
m_array[elementIndex] |= 1 << extraBits;
}
}
_version = 0;
}
/*=========================================================================
** Allocates space to hold the bit values in values. values[0] represents
** bits 0 - 31, values[1] represents bits 32 - 63, etc. The LSB of each
** integer represents the lowest index value; values[0] & 1 represents bit
** 0, values[0] & 2 represents bit 1, values[0] & 4 represents bit 2, etc.
**
** Exceptions: ArgumentException if values == null.
=========================================================================*/
public BitArray(int[] values)
{
ArgumentNullException.ThrowIfNull(values);
// this value is chosen to prevent overflow when computing m_length
if (values.Length > int.MaxValue / BitsPerInt32)
{
throw new ArgumentException(SR.Format(SR.Argument_ArrayTooLarge, BitsPerInt32), nameof(values));
}
m_array = new int[values.Length];
Array.Copy(values, m_array, values.Length);
m_length = values.Length * BitsPerInt32;
_version = 0;
}
/*=========================================================================
** Allocates a new BitArray with the same length and bit values as bits.
**
** Exceptions: ArgumentException if bits == null.
=========================================================================*/
public BitArray(BitArray bits)
{
ArgumentNullException.ThrowIfNull(bits);
int arrayLength = GetInt32ArrayLengthFromBitLength(bits.m_length);
m_array = new int[arrayLength];
Debug.Assert(bits.m_array.Length <= arrayLength);
Array.Copy(bits.m_array, m_array, arrayLength);
m_length = bits.m_length;
_version = bits._version;
}
public bool this[int index]
{
get => Get(index);
set => Set(index, value);
}
/*=========================================================================
** Returns the bit value at position index.
**
** Exceptions: ArgumentOutOfRangeException if index < 0 or
** index >= GetLength().
=========================================================================*/
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Get(int index)
{
if ((uint)index >= (uint)m_length)
ThrowArgumentOutOfRangeException(index);
return (m_array[index >> 5] & (1 << index)) != 0;
}
/*=========================================================================
** Sets the bit value at position index to value.
**
** Exceptions: ArgumentOutOfRangeException if index < 0 or
** index >= GetLength().
=========================================================================*/
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(int index, bool value)
{
if ((uint)index >= (uint)m_length)
ThrowArgumentOutOfRangeException(index);
int bitMask = 1 << index;
ref int segment = ref m_array[index >> 5];
if (value)
{
segment |= bitMask;
}
else
{
segment &= ~bitMask;
}
_version++;
}
/*=========================================================================
** Sets all the bit values to value.
=========================================================================*/
public void SetAll(bool value)
{
int arrayLength = GetInt32ArrayLengthFromBitLength(Length);
Span<int> span = m_array.AsSpan(0, arrayLength);
if (value)
{
span.Fill(-1);
// clear high bit values in the last int
Div32Rem(m_length, out int extraBits);
if (extraBits > 0)
{
span[^1] &= (1 << extraBits) - 1;
}
}
else
{
span.Clear();
}
_version++;
}
/*=========================================================================
** Returns a reference to the current instance ANDed with value.
**
** Exceptions: ArgumentException if value == null or
** value.Length != this.Length.
=========================================================================*/
public unsafe BitArray And(BitArray value)
{
ArgumentNullException.ThrowIfNull(value);
// This method uses unsafe code to manipulate data in the BitArrays. To avoid issues with
// buggy code concurrently mutating these instances in a way that could cause memory corruption,
// we snapshot the arrays from both and then operate only on those snapshots, while also validating
// that the count we iterate to is within the bounds of both arrays. We don't care about such code
// corrupting the BitArray data in a way that produces incorrect answers, since BitArray is not meant
// to be thread-safe; we only care about avoiding buffer overruns.
int[] thisArray = m_array;
int[] valueArray = value.m_array;
int count = GetInt32ArrayLengthFromBitLength(Length);
if (Length != value.Length || (uint)count > (uint)thisArray.Length || (uint)count > (uint)valueArray.Length)
throw new ArgumentException(SR.Arg_ArrayLengthsDiffer);
// Unroll loop for count less than Vector256 size.
switch (count)
{
case 7: thisArray[6] &= valueArray[6]; goto case 6;
case 6: thisArray[5] &= valueArray[5]; goto case 5;
case 5: thisArray[4] &= valueArray[4]; goto case 4;
case 4: thisArray[3] &= valueArray[3]; goto case 3;
case 3: thisArray[2] &= valueArray[2]; goto case 2;
case 2: thisArray[1] &= valueArray[1]; goto case 1;
case 1: thisArray[0] &= valueArray[0]; goto Done;
case 0: goto Done;
}
uint i = 0;
ref int left = ref MemoryMarshal.GetArrayDataReference<int>(thisArray);
ref int right = ref MemoryMarshal.GetArrayDataReference<int>(valueArray);
if (Vector512.IsHardwareAccelerated && (uint)count >= Vector512<int>.Count)
{
for (; i < (uint)count - (Vector512<int>.Count - 1u); i += (uint)Vector512<int>.Count)
{
Vector512<int> result = Vector512.LoadUnsafe(ref left, i) & Vector512.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
else if (Vector256.IsHardwareAccelerated && (uint)count >= Vector256<int>.Count)
{
for (; i < (uint)count - (Vector256<int>.Count - 1u); i += (uint)Vector256<int>.Count)
{
Vector256<int> result = Vector256.LoadUnsafe(ref left, i) & Vector256.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
else if (Vector128.IsHardwareAccelerated && (uint)count >= Vector128<int>.Count)
{
for (; i < (uint)count - (Vector128<int>.Count - 1u); i += (uint)Vector128<int>.Count)
{
Vector128<int> result = Vector128.LoadUnsafe(ref left, i) & Vector128.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
for (; i < (uint)count; i++)
thisArray[i] &= valueArray[i];
Done:
_version++;
return this;
}
/*=========================================================================
** Returns a reference to the current instance ORed with value.
**
** Exceptions: ArgumentException if value == null or
** value.Length != this.Length.
=========================================================================*/
public unsafe BitArray Or(BitArray value)
{
ArgumentNullException.ThrowIfNull(value);
// This method uses unsafe code to manipulate data in the BitArrays. To avoid issues with
// buggy code concurrently mutating these instances in a way that could cause memory corruption,
// we snapshot the arrays from both and then operate only on those snapshots, while also validating
// that the count we iterate to is within the bounds of both arrays. We don't care about such code
// corrupting the BitArray data in a way that produces incorrect answers, since BitArray is not meant
// to be thread-safe; we only care about avoiding buffer overruns.
int[] thisArray = m_array;
int[] valueArray = value.m_array;
int count = GetInt32ArrayLengthFromBitLength(Length);
if (Length != value.Length || (uint)count > (uint)thisArray.Length || (uint)count > (uint)valueArray.Length)
throw new ArgumentException(SR.Arg_ArrayLengthsDiffer);
// Unroll loop for count less than Vector256 size.
switch (count)
{
case 7: thisArray[6] |= valueArray[6]; goto case 6;
case 6: thisArray[5] |= valueArray[5]; goto case 5;
case 5: thisArray[4] |= valueArray[4]; goto case 4;
case 4: thisArray[3] |= valueArray[3]; goto case 3;
case 3: thisArray[2] |= valueArray[2]; goto case 2;
case 2: thisArray[1] |= valueArray[1]; goto case 1;
case 1: thisArray[0] |= valueArray[0]; goto Done;
case 0: goto Done;
}
uint i = 0;
ref int left = ref MemoryMarshal.GetArrayDataReference<int>(thisArray);
ref int right = ref MemoryMarshal.GetArrayDataReference<int>(valueArray);
if (Vector512.IsHardwareAccelerated && (uint)count >= Vector512<int>.Count)
{
for (; i < (uint)count - (Vector512<int>.Count - 1u); i += (uint)Vector512<int>.Count)
{
Vector512<int> result = Vector512.LoadUnsafe(ref left, i) | Vector512.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
else if (Vector256.IsHardwareAccelerated && (uint)count >= Vector256<int>.Count)
{
for (; i < (uint)count - (Vector256<int>.Count - 1u); i += (uint)Vector256<int>.Count)
{
Vector256<int> result = Vector256.LoadUnsafe(ref left, i) | Vector256.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
else if (Vector128.IsHardwareAccelerated && (uint)count >= Vector128<int>.Count)
{
for (; i < (uint)count - (Vector128<int>.Count - 1u); i += (uint)Vector128<int>.Count)
{
Vector128<int> result = Vector128.LoadUnsafe(ref left, i) | Vector128.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
for (; i < (uint)count; i++)
thisArray[i] |= valueArray[i];
Done:
_version++;
return this;
}
/*=========================================================================
** Returns a reference to the current instance XORed with value.
**
** Exceptions: ArgumentException if value == null or
** value.Length != this.Length.
=========================================================================*/
public unsafe BitArray Xor(BitArray value)
{
ArgumentNullException.ThrowIfNull(value);
// This method uses unsafe code to manipulate data in the BitArrays. To avoid issues with
// buggy code concurrently mutating these instances in a way that could cause memory corruption,
// we snapshot the arrays from both and then operate only on those snapshots, while also validating
// that the count we iterate to is within the bounds of both arrays. We don't care about such code
// corrupting the BitArray data in a way that produces incorrect answers, since BitArray is not meant
// to be thread-safe; we only care about avoiding buffer overruns.
int[] thisArray = m_array;
int[] valueArray = value.m_array;
int count = GetInt32ArrayLengthFromBitLength(Length);
if (Length != value.Length || (uint)count > (uint)thisArray.Length || (uint)count > (uint)valueArray.Length)
throw new ArgumentException(SR.Arg_ArrayLengthsDiffer);
// Unroll loop for count less than Vector256 size.
switch (count)
{
case 7: thisArray[6] ^= valueArray[6]; goto case 6;
case 6: thisArray[5] ^= valueArray[5]; goto case 5;
case 5: thisArray[4] ^= valueArray[4]; goto case 4;
case 4: thisArray[3] ^= valueArray[3]; goto case 3;
case 3: thisArray[2] ^= valueArray[2]; goto case 2;
case 2: thisArray[1] ^= valueArray[1]; goto case 1;
case 1: thisArray[0] ^= valueArray[0]; goto Done;
case 0: goto Done;
}
uint i = 0;
ref int left = ref MemoryMarshal.GetArrayDataReference<int>(thisArray);
ref int right = ref MemoryMarshal.GetArrayDataReference<int>(valueArray);
if (Vector512.IsHardwareAccelerated && (uint)count >= Vector512<int>.Count)
{
for (; i < (uint)count - (Vector512<int>.Count - 1u); i += (uint)Vector512<int>.Count)
{
Vector512<int> result = Vector512.LoadUnsafe(ref left, i) ^ Vector512.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
else if (Vector256.IsHardwareAccelerated && (uint)count >= Vector256<int>.Count)
{
for (; i < (uint)count - (Vector256<int>.Count - 1u); i += (uint)Vector256<int>.Count)
{
Vector256<int> result = Vector256.LoadUnsafe(ref left, i) ^ Vector256.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
else if (Vector128.IsHardwareAccelerated && (uint)count >= Vector128<int>.Count)
{
for (; i < (uint)count - (Vector128<int>.Count - 1u); i += (uint)Vector128<int>.Count)
{
Vector128<int> result = Vector128.LoadUnsafe(ref left, i) ^ Vector128.LoadUnsafe(ref right, i);
result.StoreUnsafe(ref left, i);
}
}
for (; i < (uint)count; i++)
thisArray[i] ^= valueArray[i];
Done:
_version++;
return this;
}
/*=========================================================================
** Inverts all the bit values. On/true bit values are converted to
** off/false. Off/false bit values are turned on/true. The current instance
** is updated and returned.
=========================================================================*/
public unsafe BitArray Not()
{
// This method uses unsafe code to manipulate data in the BitArray. To avoid issues with
// buggy code concurrently mutating this instance in a way that could cause memory corruption,
// we snapshot the array then operate only on this snapshot. We don't care about such code
// corrupting the BitArray data in a way that produces incorrect answers, since BitArray is not meant
// to be thread-safe; we only care about avoiding buffer overruns.
int[] thisArray = m_array;
int count = GetInt32ArrayLengthFromBitLength(Length);
// Unroll loop for count less than Vector256 size.
switch (count)
{
case 7: thisArray[6] = ~thisArray[6]; goto case 6;
case 6: thisArray[5] = ~thisArray[5]; goto case 5;
case 5: thisArray[4] = ~thisArray[4]; goto case 4;
case 4: thisArray[3] = ~thisArray[3]; goto case 3;
case 3: thisArray[2] = ~thisArray[2]; goto case 2;
case 2: thisArray[1] = ~thisArray[1]; goto case 1;
case 1: thisArray[0] = ~thisArray[0]; goto Done;
case 0: goto Done;
}
uint i = 0;
ref int value = ref MemoryMarshal.GetArrayDataReference<int>(thisArray);
if (Vector512.IsHardwareAccelerated && (uint)count >= Vector512<int>.Count)
{
for (; i < (uint)count - (Vector512<int>.Count - 1u); i += (uint)Vector512<int>.Count)
{
Vector512<int> result = ~Vector512.LoadUnsafe(ref value, i);
result.StoreUnsafe(ref value, i);
}
}
else if (Vector256.IsHardwareAccelerated && (uint)count >= Vector256<int>.Count)
{
for (; i < (uint)count - (Vector256<int>.Count - 1u); i += (uint)Vector256<int>.Count)
{
Vector256<int> result = ~Vector256.LoadUnsafe(ref value, i);
result.StoreUnsafe(ref value, i);
}
}
else if (Vector128.IsHardwareAccelerated && (uint)count >= Vector128<int>.Count)
{
for (; i < (uint)count - (Vector128<int>.Count - 1u); i += (uint)Vector128<int>.Count)
{
Vector128<int> result = ~Vector128.LoadUnsafe(ref value, i);
result.StoreUnsafe(ref value, i);
}
}
for (; i < (uint)count; i++)
thisArray[i] = ~thisArray[i];
Done:
_version++;
return this;
}
/*=========================================================================
** Shift all the bit values to right on count bits. The current instance is
** updated and returned.
*
** Exceptions: ArgumentOutOfRangeException if count < 0
=========================================================================*/
public BitArray RightShift(int count)
{
if (count <= 0)
{
ArgumentOutOfRangeException.ThrowIfNegative(count);
_version++;
return this;
}
int toIndex = 0;
int ints = GetInt32ArrayLengthFromBitLength(m_length);
if (count < m_length)
{
// We can not use Math.DivRem without taking a dependency on System.Runtime.Extensions
int fromIndex = Div32Rem(count, out int shiftCount);
Div32Rem(m_length, out int extraBits);
if (shiftCount == 0)
{
unchecked
{
// Cannot use `(1u << extraBits) - 1u` as the mask
// because for extraBits == 0, we need the mask to be 111...111, not 0.
// In that case, we are shifting a uint by 32, which could be considered undefined.
// The result of a shift operation is undefined ... if the right operand
// is greater than or equal to the width in bits of the promoted left operand,
// https://docs.microsoft.com/en-us/cpp/c-language/bitwise-shift-operators?view=vs-2017
// However, the compiler protects us from undefined behaviour by constraining the
// right operand to between 0 and width - 1 (inclusive), i.e. right_operand = (right_operand % width).
uint mask = uint.MaxValue >> (BitsPerInt32 - extraBits);
m_array[ints - 1] &= (int)mask;
}
Array.Copy(m_array, fromIndex, m_array, 0, ints - fromIndex);
toIndex = ints - fromIndex;
}
else
{
int lastIndex = ints - 1;
unchecked
{
while (fromIndex < lastIndex)
{
uint right = (uint)m_array[fromIndex] >> shiftCount;
int left = m_array[++fromIndex] << (BitsPerInt32 - shiftCount);
m_array[toIndex++] = left | (int)right;
}
uint mask = uint.MaxValue >> (BitsPerInt32 - extraBits);
mask &= (uint)m_array[fromIndex];
m_array[toIndex++] = (int)(mask >> shiftCount);
}
}
}
m_array.AsSpan(toIndex, ints - toIndex).Clear();
_version++;
return this;
}
/*=========================================================================
** Shift all the bit values to left on count bits. The current instance is
** updated and returned.
*
** Exceptions: ArgumentOutOfRangeException if count < 0
=========================================================================*/
public BitArray LeftShift(int count)
{
if (count <= 0)
{
ArgumentOutOfRangeException.ThrowIfNegative(count);
_version++;
return this;
}
int lengthToClear;
if (count < m_length)
{
int lastIndex = (m_length - 1) >> BitShiftPerInt32; // Divide by 32.
// We can not use Math.DivRem without taking a dependency on System.Runtime.Extensions
lengthToClear = Div32Rem(count, out int shiftCount);
if (shiftCount == 0)
{
Array.Copy(m_array, 0, m_array, lengthToClear, lastIndex + 1 - lengthToClear);
}
else
{
int fromindex = lastIndex - lengthToClear;
unchecked
{
while (fromindex > 0)
{
int left = m_array[fromindex] << shiftCount;
uint right = (uint)m_array[--fromindex] >> (BitsPerInt32 - shiftCount);
m_array[lastIndex] = left | (int)right;
lastIndex--;
}
m_array[lastIndex] = m_array[fromindex] << shiftCount;
}
}
}
else
{
lengthToClear = GetInt32ArrayLengthFromBitLength(m_length); // Clear all
}
m_array.AsSpan(0, lengthToClear).Clear();
_version++;
return this;
}
public int Length
{
get
{
return m_length;
}
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
int newints = GetInt32ArrayLengthFromBitLength(value);
if (newints > m_array.Length || newints + _ShrinkThreshold < m_array.Length)
{
// grow or shrink (if wasting more than _ShrinkThreshold ints)
Array.Resize(ref m_array, newints);
}
if (value > m_length)
{
// clear high bit values in the last int
int last = (m_length - 1) >> BitShiftPerInt32;
Div32Rem(m_length, out int bits);
if (bits > 0)
{
m_array[last] &= (1 << bits) - 1;
}
// clear remaining int values
m_array.AsSpan(last + 1, newints - last - 1).Clear();
}
m_length = value;
_version++;
}
}
public unsafe void CopyTo(Array array, int index)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(index);
if (array.Rank != 1)
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
if (array is int[] intArray)
{
Div32Rem(m_length, out int extraBits);
if (extraBits == 0)
{
// we have perfect bit alignment, no need to sanitize, just copy
Array.Copy(m_array, 0, intArray, index, m_array.Length);
}
else
{
int last = (m_length - 1) >> BitShiftPerInt32;
// do not copy the last int, as it is not completely used
Array.Copy(m_array, 0, intArray, index, last);
// the last int needs to be masked
intArray[index + last] = m_array[last] & unchecked((1 << extraBits) - 1);
}
}
else if (array is byte[] byteArray)
{
int arrayLength = GetByteArrayLengthFromBitLength(m_length);
if ((array.Length - index) < arrayLength)
{
throw new ArgumentException(SR.Argument_InvalidOffLen);
}
// equivalent to m_length % BitsPerByte, since BitsPerByte is a power of 2
uint extraBits = (uint)m_length & (BitsPerByte - 1);
if (extraBits > 0)
{
// last byte is not aligned, we will directly copy one less byte
arrayLength -= 1;
}
Span<byte> span = byteArray.AsSpan(index);
int quotient = Div4Rem(arrayLength, out int remainder);
for (int i = 0; i < quotient; i++)
{
BinaryPrimitives.WriteInt32LittleEndian(span, m_array[i]);
span = span.Slice(4);
}
if (extraBits > 0)
{
Debug.Assert(span.Length > 0);
Debug.Assert(m_array.Length > quotient);
// mask the final byte
span[remainder] = (byte)((m_array[quotient] >> (remainder * 8)) & ((1 << (int)extraBits) - 1));
}
switch (remainder)
{
case 3:
span[2] = (byte)(m_array[quotient] >> 16);
goto case 2;
// fall through
case 2:
span[1] = (byte)(m_array[quotient] >> 8);
goto case 1;
// fall through
case 1:
span[0] = (byte)m_array[quotient];
break;
}
}
else if (array is bool[] boolArray)
{
if (array.Length - index < m_length)
{
throw new ArgumentException(SR.Argument_InvalidOffLen);
}
uint i = 0;
if (m_length < BitsPerInt32)
goto LessThan32;
// The mask used when shuffling a single int into Vector128/256/512.
// On little endian machines, the lower 8 bits of int belong in the first byte, next lower 8 in the second and so on.
// We place the bytes that contain the bits to its respective byte so that we can mask out only the relevant bits later.
Vector128<byte> lowerShuffleMask_CopyToBoolArray = Vector128.Create(0, 0x01010101_01010101).AsByte();
Vector128<byte> upperShuffleMask_CopyToBoolArray = Vector128.Create(0x02020202_02020202, 0x03030303_03030303).AsByte();
if (Avx512F.IsSupported && (uint)m_length >= Vector512<byte>.Count)
{
Vector256<byte> upperShuffleMask_CopyToBoolArray256 = Vector256.Create(0x04040404_04040404, 0x05050505_05050505,
0x06060606_06060606, 0x07070707_07070707).AsByte();
Vector256<byte> lowerShuffleMask_CopyToBoolArray256 = Vector256.Create(lowerShuffleMask_CopyToBoolArray, upperShuffleMask_CopyToBoolArray);
Vector512<byte> shuffleMask = Vector512.Create(lowerShuffleMask_CopyToBoolArray256, upperShuffleMask_CopyToBoolArray256);
Vector512<byte> bitMask = Vector512.Create(0x80402010_08040201).AsByte();
Vector512<byte> ones = Vector512.Create((byte)1);
fixed (bool* destination = &boolArray[index])
{
for (; (i + Vector512<byte>.Count) <= (uint)m_length; i += (uint)Vector512<byte>.Count)
{
ulong bits = (ulong)(uint)m_array[i / (uint)BitsPerInt32] + ((ulong)m_array[(i / (uint)BitsPerInt32) + 1] << BitsPerInt32);
Vector512<ulong> scalar = Vector512.Create(bits);
Vector512<byte> shuffled = Avx512BW.Shuffle(scalar.AsByte(), shuffleMask);
Vector512<byte> extracted = Avx512F.And(shuffled, bitMask);
// The extracted bits can be anywhere between 0 and 255, so we normalise the value to either 0 or 1
// to ensure compatibility with "C# bool" (0 for false, 1 for true, rest undefined)
Vector512<byte> normalized = Avx512BW.Min(extracted, ones);
Avx512F.Store((byte*)destination + i, normalized);
}
}
}
else if (Avx2.IsSupported && (uint)m_length >= Vector256<byte>.Count)
{
Vector256<byte> shuffleMask = Vector256.Create(lowerShuffleMask_CopyToBoolArray, upperShuffleMask_CopyToBoolArray);
Vector256<byte> bitMask = Vector256.Create(0x80402010_08040201).AsByte();
//Internal.Console.WriteLine(bitMask);
Vector256<byte> ones = Vector256.Create((byte)1);
fixed (bool* destination = &boolArray[index])
{
for (; (i + Vector256<byte>.Count) <= (uint)m_length; i += (uint)Vector256<byte>.Count)
{
int bits = m_array[i / (uint)BitsPerInt32];
Vector256<int> scalar = Vector256.Create(bits);
Vector256<byte> shuffled = Avx2.Shuffle(scalar.AsByte(), shuffleMask);
Vector256<byte> extracted = Avx2.And(shuffled, bitMask);
// The extracted bits can be anywhere between 0 and 255, so we normalise the value to either 0 or 1
// to ensure compatibility with "C# bool" (0 for false, 1 for true, rest undefined)
Vector256<byte> normalized = Avx2.Min(extracted, ones);
Avx.Store((byte*)destination + i, normalized);
}
}
}
else if (Ssse3.IsSupported && ((uint)m_length >= Vector512<byte>.Count * 2u))
{
Vector128<byte> lowerShuffleMask = lowerShuffleMask_CopyToBoolArray;
Vector128<byte> upperShuffleMask = upperShuffleMask_CopyToBoolArray;
Vector128<byte> ones = Vector128.Create((byte)1);
Vector128<byte> bitMask128 = BitConverter.IsLittleEndian ?
Vector128.Create(0x80402010_08040201).AsByte() :
Vector128.Create(0x01020408_10204080).AsByte();
fixed (bool* destination = &boolArray[index])
{
for (; (i + Vector128<byte>.Count * 2u) <= (uint)m_length; i += (uint)Vector128<byte>.Count * 2u)
{
int bits = m_array[i / (uint)BitsPerInt32];
Vector128<int> scalar = Vector128.CreateScalarUnsafe(bits);
Vector128<byte> shuffledLower = Ssse3.Shuffle(scalar.AsByte(), lowerShuffleMask);
Vector128<byte> extractedLower = Sse2.And(shuffledLower, bitMask128);
Vector128<byte> normalizedLower = Sse2.Min(extractedLower, ones);
Sse2.Store((byte*)destination + i, normalizedLower);
Vector128<byte> shuffledHigher = Ssse3.Shuffle(scalar.AsByte(), upperShuffleMask);
Vector128<byte> extractedHigher = Sse2.And(shuffledHigher, bitMask128);
Vector128<byte> normalizedHigher = Sse2.Min(extractedHigher, ones);
Sse2.Store((byte*)destination + i + Vector128<byte>.Count, normalizedHigher);
}
}
}
else if (AdvSimd.Arm64.IsSupported)
{
Vector128<byte> ones = Vector128.Create((byte)1);
Vector128<byte> bitMask128 = BitConverter.IsLittleEndian ?
Vector128.Create(0x80402010_08040201).AsByte() :
Vector128.Create(0x01020408_10204080).AsByte();
fixed (bool* destination = &boolArray[index])
{
for (; (i + Vector128<byte>.Count * 2u) <= (uint)m_length; i += (uint)Vector128<byte>.Count * 2u)
{
int bits = m_array[i / (uint)BitsPerInt32];
// Same logic as SSSE3 path, except we do not have Shuffle instruction.
// (TableVectorLookup could be an alternative - dotnet/runtime#1277)
// Instead we use chained ZIP1/2 instructions:
// (A0 is the byte containing LSB, A3 is the byte containing MSB)
// bits (on Big endian) - A3 A2 A1 A0
// bits (Little endian) / Byte reversal - A0 A1 A2 A3
// v1 = Vector128.Create - A0 A1 A2 A3 A0 A1 A2 A3 A0 A1 A2 A3 A0 A1 A2 A3
// v2 = ZipLow(v1, v1) - A0 A0 A1 A1 A2 A2 A3 A3 A0 A0 A1 A1 A2 A2 A3 A3
// v3 = ZipLow(v2, v2) - A0 A0 A0 A0 A1 A1 A1 A1 A2 A2 A2 A2 A3 A3 A3 A3
// shuffledLower = ZipLow(v3, v3) - A0 A0 A0 A0 A0 A0 A0 A0 A1 A1 A1 A1 A1 A1 A1 A1
// shuffledHigher = ZipHigh(v3, v3) - A2 A2 A2 A2 A2 A2 A2 A2 A3 A3 A3 A3 A3 A3 A3 A3
if (!BitConverter.IsLittleEndian)
{
bits = BinaryPrimitives.ReverseEndianness(bits);
}
Vector128<byte> vector = Vector128.Create(bits).AsByte();
vector = AdvSimd.Arm64.ZipLow(vector, vector);
vector = AdvSimd.Arm64.ZipLow(vector, vector);
Vector128<byte> shuffledLower = AdvSimd.Arm64.ZipLow(vector, vector);
Vector128<byte> extractedLower = AdvSimd.And(shuffledLower, bitMask128);
Vector128<byte> normalizedLower = AdvSimd.Min(extractedLower, ones);
Vector128<byte> shuffledHigher = AdvSimd.Arm64.ZipHigh(vector, vector);
Vector128<byte> extractedHigher = AdvSimd.And(shuffledHigher, bitMask128);
Vector128<byte> normalizedHigher = AdvSimd.Min(extractedHigher, ones);
AdvSimd.Arm64.StorePair((byte*)destination + i, normalizedLower, normalizedHigher);
}
}
}
LessThan32:
for (; i < (uint)m_length; i++)
{
int elementIndex = Div32Rem((int)i, out int extraBits);
boolArray[(uint)index + i] = ((m_array[elementIndex] >> extraBits) & 0x00000001) != 0;
}
}
else
{
throw new ArgumentException(SR.Arg_BitArrayTypeUnsupported, nameof(array));
}
}
/// <summary>
/// Determines whether all bits in the <see cref="BitArray"/> are set to <c>true</c>.
/// </summary>
/// <returns><c>true</c> if every bit in the <see cref="BitArray"/> is set to <c>true</c>, or if <see cref="BitArray"/> is empty; otherwise, <c>false</c>.</returns>
public bool HasAllSet()
{
Div32Rem(m_length, out int extraBits);
int intCount = GetInt32ArrayLengthFromBitLength(m_length);
if (extraBits != 0)
{
intCount--;
}
const int AllSetBits = -1; // 0xFF_FF_FF_FF
if (m_array.AsSpan(0, intCount).ContainsAnyExcept(AllSetBits))
{
return false;
}
if (extraBits == 0)
{
return true;
}
Debug.Assert(GetInt32ArrayLengthFromBitLength(m_length) > 0);
Debug.Assert(intCount == GetInt32ArrayLengthFromBitLength(m_length) - 1);