Hash :
d193d51b
Author :
Date :
2024-06-17T22:46:08
Replace issue ids post migration to new issue tracker This change replaces anglebug.com/NNNN links. Bug: None Change-Id: I8ac3aec8d2a8a844b3d7b99fc0a6b2be8da31761 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5637912 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// bitset_utils:
// Bitset-related helper classes, such as a fast iterator to scan for set bits.
//
#ifndef COMMON_BITSETITERATOR_H_
#define COMMON_BITSETITERATOR_H_
#include <stdint.h>
#include <array>
#include "common/angleutils.h"
#include "common/debug.h"
#include "common/mathutil.h"
#include "common/platform.h"
namespace angle
{
// Given x, create 1 << x.
template <typename BitsT, typename ParamT>
constexpr BitsT Bit(ParamT x)
{
// It's undefined behavior if the shift size is equal to or larger than the width of the type.
ASSERT(static_cast<size_t>(x) < sizeof(BitsT) * 8);
return (static_cast<BitsT>(1) << static_cast<size_t>(x));
}
// Given x, create (1 << x) - 1, i.e. a mask with x bits set.
template <typename BitsT, typename ParamT>
constexpr BitsT BitMask(ParamT x)
{
if (static_cast<size_t>(x) == 0)
{
return 0;
}
return ((Bit<BitsT>(static_cast<ParamT>(static_cast<size_t>(x) - 1)) - 1) << 1) | 1;
}
template <size_t N, typename BitsT, typename ParamT = std::size_t>
class BitSetT final
{
public:
class Reference final
{
public:
~Reference() {}
Reference &operator=(bool x)
{
mParent->set(mBit, x);
return *this;
}
explicit operator bool() const { return mParent->test(mBit); }
private:
friend class BitSetT;
Reference(BitSetT *parent, ParamT bit) : mParent(parent), mBit(bit) {}
BitSetT *mParent;
ParamT mBit;
};
class Iterator final
{
public:
Iterator(const BitSetT &bits);
Iterator &operator++();
bool operator==(const Iterator &other) const;
bool operator!=(const Iterator &other) const;
ParamT operator*() const;
// These helper functions allow mutating an iterator in-flight.
// They only operate on later bits to ensure we don't iterate the same bit twice.
void resetLaterBit(std::size_t index)
{
ASSERT(index > mCurrentBit);
mBitsCopy.reset(index);
}
// bits could contain bit that earlier than mCurrentBit. Since mBitCopy can't have bits
// earlier than mCurrentBit, the & operation will mask out earlier bits anyway.
void resetLaterBits(const BitSetT &bits)
{
BitSetT maskedBits = ~Mask(mCurrentBit + 1);
maskedBits &= bits;
mBitsCopy &= ~maskedBits;
}
void setLaterBit(std::size_t index)
{
ASSERT(index > mCurrentBit);
mBitsCopy.set(index);
}
void setLaterBits(const BitSetT &bits)
{
ASSERT((BitSetT(bits) &= Mask(mCurrentBit + 1)).none());
mBitsCopy |= bits;
}
private:
std::size_t getNextBit();
BitSetT mBitsCopy;
std::size_t mCurrentBit;
};
using value_type = BitsT;
using param_type = ParamT;
constexpr BitSetT();
constexpr explicit BitSetT(BitsT value);
constexpr explicit BitSetT(std::initializer_list<ParamT> init);
constexpr bool operator==(const BitSetT &other) const;
constexpr bool operator!=(const BitSetT &other) const;
constexpr bool operator[](ParamT pos) const;
Reference operator[](ParamT pos) { return Reference(this, pos); }
constexpr bool test(ParamT pos) const;
constexpr bool all() const;
constexpr bool any() const;
constexpr bool none() const;
constexpr std::size_t count() const;
// Returns true iff there are unset bits prior
// to the most significant bit set. For example:
// 0b0000 - false
// 0b0001 - false
// 0b0011 - false
// 0b0010 - true
// 0b0101 - true
constexpr bool hasGaps() const;
constexpr static std::size_t size() { return N; }
constexpr BitSetT &operator&=(const BitSetT &other);
constexpr BitSetT &operator|=(const BitSetT &other);
constexpr BitSetT &operator^=(const BitSetT &other);
constexpr BitSetT operator~() const;
constexpr BitSetT &operator&=(BitsT value);
constexpr BitSetT &operator|=(BitsT value);
constexpr BitSetT &operator^=(BitsT value);
constexpr BitSetT operator<<(std::size_t pos) const;
constexpr BitSetT &operator<<=(std::size_t pos);
constexpr BitSetT operator>>(std::size_t pos) const;
constexpr BitSetT &operator>>=(std::size_t pos);
constexpr BitSetT &set();
constexpr BitSetT &set(ParamT pos, bool value = true);
constexpr BitSetT &reset();
constexpr BitSetT &reset(ParamT pos);
constexpr BitSetT &flip();
constexpr BitSetT &flip(ParamT pos);
constexpr unsigned long to_ulong() const { return static_cast<unsigned long>(mBits); }
constexpr BitsT bits() const { return mBits; }
Iterator begin() const { return Iterator(*this); }
Iterator end() const { return Iterator(BitSetT()); }
constexpr static BitSetT Zero() { return BitSetT(); }
constexpr ParamT first() const;
constexpr ParamT last() const;
// Produces a mask of ones up to the "x"th bit.
constexpr static BitSetT Mask(std::size_t x)
{
BitSetT result;
result.mBits = BitMask<BitsT>(static_cast<ParamT>(x));
return result;
}
private:
BitsT mBits;
};
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT>::BitSetT() : mBits(0)
{
static_assert(N > 0, "Bitset type cannot support zero bits.");
static_assert(N <= sizeof(BitsT) * 8, "Bitset type cannot support a size this large.");
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT>::BitSetT(BitsT value) : mBits(value & Mask(N).bits())
{}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT>::BitSetT(std::initializer_list<ParamT> init) : mBits(0)
{
for (ParamT element : init)
{
mBits |= Bit<BitsT>(element);
}
ASSERT(mBits == (mBits & Mask(N).bits()));
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::operator==(const BitSetT &other) const
{
return mBits == other.mBits;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::operator!=(const BitSetT &other) const
{
return mBits != other.mBits;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::operator[](ParamT pos) const
{
return test(pos);
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::test(ParamT pos) const
{
return (mBits & Bit<BitsT>(pos)) != 0;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::all() const
{
ASSERT(mBits == (mBits & Mask(N).bits()));
return mBits == Mask(N).bits();
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::any() const
{
ASSERT(mBits == (mBits & Mask(N).bits()));
return (mBits != 0);
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::none() const
{
ASSERT(mBits == (mBits & Mask(N).bits()));
return (mBits == 0);
}
template <size_t N, typename BitsT, typename ParamT>
constexpr std::size_t BitSetT<N, BitsT, ParamT>::count() const
{
return gl::BitCount(mBits);
}
template <size_t N, typename BitsT, typename ParamT>
constexpr bool BitSetT<N, BitsT, ParamT>::hasGaps() const
{
ASSERT(mBits == (mBits & Mask(N).bits()));
return (mBits != Mask(N).bits()) && ((mBits & (mBits + 1)) != 0);
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator&=(const BitSetT &other)
{
mBits &= other.mBits;
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator|=(const BitSetT &other)
{
mBits |= other.mBits;
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator^=(const BitSetT &other)
{
mBits = mBits ^ other.mBits;
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> BitSetT<N, BitsT, ParamT>::operator~() const
{
return BitSetT<N, BitsT, ParamT>(~mBits & Mask(N).bits());
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator&=(BitsT value)
{
mBits &= value;
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator|=(BitsT value)
{
mBits |= value;
ASSERT(mBits == (mBits & Mask(N).bits()));
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator^=(BitsT value)
{
mBits ^= value;
ASSERT(mBits == (mBits & Mask(N).bits()));
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> BitSetT<N, BitsT, ParamT>::operator<<(std::size_t pos) const
{
return BitSetT<N, BitsT, ParamT>((mBits << pos) & Mask(N).bits());
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator<<=(std::size_t pos)
{
mBits = mBits << pos & Mask(N).bits();
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> BitSetT<N, BitsT, ParamT>::operator>>(std::size_t pos) const
{
return BitSetT<N, BitsT, ParamT>(mBits >> pos);
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator>>=(std::size_t pos)
{
mBits = (mBits >> pos) & Mask(N).bits();
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::set()
{
ASSERT(mBits == (mBits & Mask(N).bits()));
mBits = Mask(N).bits();
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::set(ParamT pos, bool value)
{
ASSERT(static_cast<size_t>(pos) < N);
if (value)
{
mBits |= Bit<BitsT>(pos);
}
else
{
reset(pos);
}
ASSERT(mBits == (mBits & Mask(N).bits()));
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::reset()
{
ASSERT(mBits == (mBits & Mask(N).bits()));
mBits = 0;
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::reset(ParamT pos)
{
ASSERT(static_cast<size_t>(pos) < N);
ASSERT(mBits == (mBits & Mask(N).bits()));
mBits &= ~Bit<BitsT>(pos);
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::flip()
{
ASSERT(mBits == (mBits & Mask(N).bits()));
mBits ^= Mask(N).bits();
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::flip(ParamT pos)
{
ASSERT(static_cast<size_t>(pos) < N);
mBits ^= Bit<BitsT>(pos);
ASSERT(mBits == (mBits & Mask(N).bits()));
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
constexpr ParamT BitSetT<N, BitsT, ParamT>::first() const
{
ASSERT(!none());
return static_cast<ParamT>(gl::ScanForward(mBits));
}
template <size_t N, typename BitsT, typename ParamT>
constexpr ParamT BitSetT<N, BitsT, ParamT>::last() const
{
ASSERT(!none());
return static_cast<ParamT>(gl::ScanReverse(mBits));
}
template <size_t N, typename BitsT, typename ParamT>
BitSetT<N, BitsT, ParamT>::Iterator::Iterator(const BitSetT &bits) : mBitsCopy(bits), mCurrentBit(0)
{
if (bits.any())
{
mCurrentBit = getNextBit();
}
}
template <size_t N, typename BitsT, typename ParamT>
ANGLE_INLINE typename BitSetT<N, BitsT, ParamT>::Iterator &
BitSetT<N, BitsT, ParamT>::Iterator::operator++()
{
ASSERT(mBitsCopy.any());
mBitsCopy.reset(static_cast<ParamT>(mCurrentBit));
mCurrentBit = getNextBit();
return *this;
}
template <size_t N, typename BitsT, typename ParamT>
bool BitSetT<N, BitsT, ParamT>::Iterator::operator==(const Iterator &other) const
{
return mBitsCopy == other.mBitsCopy;
}
template <size_t N, typename BitsT, typename ParamT>
bool BitSetT<N, BitsT, ParamT>::Iterator::operator!=(const Iterator &other) const
{
return !(*this == other);
}
template <size_t N, typename BitsT, typename ParamT>
ParamT BitSetT<N, BitsT, ParamT>::Iterator::operator*() const
{
return static_cast<ParamT>(mCurrentBit);
}
template <size_t N, typename BitsT, typename ParamT>
std::size_t BitSetT<N, BitsT, ParamT>::Iterator::getNextBit()
{
if (mBitsCopy.none())
{
return 0;
}
return gl::ScanForward(mBitsCopy.mBits);
}
template <size_t N>
using BitSet8 = BitSetT<N, uint8_t>;
template <size_t N>
using BitSet16 = BitSetT<N, uint16_t>;
template <size_t N>
using BitSet32 = BitSetT<N, uint32_t>;
static_assert(std::is_trivially_copyable<BitSet32<32>>(), "must be memcpy-able");
template <size_t N>
using BitSet64 = BitSetT<N, uint64_t>;
template <std::size_t N>
class BitSetArray;
namespace priv
{
template <size_t N, typename T>
using EnableIfBitsFit = typename std::enable_if<N <= sizeof(T) * 8>::type;
template <size_t N, typename Enable = void>
struct GetBitSet
{
using Type = BitSetArray<N>;
};
// Prefer 64-bit bitsets on 64-bit CPUs. They seem faster than 32-bit.
#if defined(ANGLE_IS_64_BIT_CPU)
template <size_t N>
struct GetBitSet<N, EnableIfBitsFit<N, uint64_t>>
{
using Type = BitSet64<N>;
};
constexpr std::size_t kDefaultBitSetSize = 64;
using BaseBitSetType = BitSet64<kDefaultBitSetSize>;
#else
template <size_t N>
struct GetBitSet<N, EnableIfBitsFit<N, uint32_t>>
{
using Type = BitSet32<N>;
};
constexpr std::size_t kDefaultBitSetSize = 32;
using BaseBitSetType = BitSet32<kDefaultBitSetSize>;
#endif // defined(ANGLE_IS_64_BIT_CPU)
} // namespace priv
template <size_t N>
using BitSet = typename priv::GetBitSet<N>::Type;
template <std::size_t N>
class BitSetArray final
{
public:
using BaseBitSet = priv::BaseBitSetType;
using value_type = BaseBitSet::value_type;
using param_type = BaseBitSet::param_type;
constexpr BitSetArray();
constexpr explicit BitSetArray(uint64_t value);
constexpr explicit BitSetArray(std::initializer_list<param_type> init);
class Reference final
{
public:
~Reference() {}
Reference &operator=(bool x)
{
mParent.set(mPosition, x);
return *this;
}
explicit operator bool() const { return mParent.test(mPosition); }
private:
friend class BitSetArray;
Reference(BitSetArray &parent, std::size_t pos) : mParent(parent), mPosition(pos) {}
BitSetArray &mParent;
std::size_t mPosition;
};
class Iterator final
{
public:
Iterator(const BitSetArray<N> &bitSetArray, std::size_t index);
Iterator &operator++();
bool operator==(const Iterator &other) const;
bool operator!=(const Iterator &other) const;
size_t operator*() const;
// These helper functions allow mutating an iterator in-flight.
// They only operate on later bits to ensure we don't iterate the same bit twice.
void resetLaterBit(std::size_t pos)
{
ASSERT(pos > (mIndex * priv::kDefaultBitSetSize) + *mCurrentIterator);
prepareCopy();
mParentCopy.reset(pos);
updateIteratorBit(pos, false);
}
void setLaterBit(std::size_t pos)
{
ASSERT(pos > (mIndex * priv::kDefaultBitSetSize) + *mCurrentIterator);
prepareCopy();
mParentCopy.set(pos);
updateIteratorBit(pos, true);
}
void setLaterBits(const BitSetArray &bits)
{
prepareCopy();
mParentCopy |= bits;
updateIteratorBits(bits);
}
private:
ANGLE_INLINE void prepareCopy()
{
ASSERT(mParent.mBaseBitSetArray[mIndex].end() ==
mParentCopy.mBaseBitSetArray[mIndex].end());
if (mParentCopy.none())
{
mParentCopy = mParent;
mCurrentParent = &mParentCopy;
}
}
ANGLE_INLINE void updateIteratorBit(std::size_t pos, bool setBit)
{
// Get the index and offset, update current interator if within range
size_t index = pos >> kShiftForDivision;
size_t offset = pos & kDefaultBitSetSizeMinusOne;
if (index == mIndex)
{
if (setBit)
{
mCurrentIterator.setLaterBit(offset);
}
else
{
mCurrentIterator.resetLaterBit(offset);
}
}
}
ANGLE_INLINE void updateIteratorBits(const BitSetArray &bits)
{
mCurrentIterator.setLaterBits(bits.mBaseBitSetArray[mIndex]);
}
// Problem -
// We want to provide the fastest path possible for usecases that iterate though the bitset.
//
// Options -
// 1) For non-mutating iterations the const ref <mParent> is set as mCurrentParent and only
// for usecases that need to mutate the bitset while iterating we perform a copy of
// <mParent> into <mParentCopy> and modify its bits accordingly.
// 2) The alternate approach was to perform a copy all the time in the constructor
// irrespective of whether it was a mutating usecase or not.
//
// Experiment -
// BitSetIteratorPerfTest was run on a Windows machine with Intel CPU and these were the
// results -
// 1) Copy only when necessary -
// RESULT BitSetIteratorPerf.wall_time: run = 116.1067374961 ns
// RESULT BitSetIteratorPerf.trial_steps : run = 8416124 count
// RESULT BitSetIteratorPerf.total_steps : run = 16832251 count
// 2) Copy always -
// RESULT BitSetIteratorPerf.wall_time: run = 242.7446459439 ns
// RESULT BitSetIteratorPerf.trial_steps : run = 4171416 count
// RESULT BitSetIteratorPerf.total_steps : run = 8342834 count
//
// Resolution -
// We settled on the copy only when necessary path.
size_t mIndex;
const BitSetArray &mParent;
BitSetArray mParentCopy;
const BitSetArray *mCurrentParent;
typename BaseBitSet::Iterator mCurrentIterator;
};
constexpr static std::size_t size() { return N; }
Iterator begin() const { return Iterator(*this, 0); }
Iterator end() const { return Iterator(*this, kArraySize); }
constexpr unsigned long to_ulong() const
{
// TODO(anglebug.com/42264163): Handle serializing more than kDefaultBitSetSize
for (std::size_t index = 1; index < kArraySize; index++)
{
ASSERT(mBaseBitSetArray[index].none());
}
return static_cast<unsigned long>(mBaseBitSetArray[0].to_ulong());
}
// Assignment operators
constexpr BitSetArray &operator&=(const BitSetArray &other);
constexpr BitSetArray &operator|=(const BitSetArray &other);
constexpr BitSetArray &operator^=(const BitSetArray &other);
// Bitwise operators
constexpr BitSetArray<N> operator&(const angle::BitSetArray<N> &other) const;
constexpr BitSetArray<N> operator|(const angle::BitSetArray<N> &other) const;
constexpr BitSetArray<N> operator^(const angle::BitSetArray<N> &other) const;
// Relational Operators
constexpr bool operator==(const angle::BitSetArray<N> &other) const;
constexpr bool operator!=(const angle::BitSetArray<N> &other) const;
// Unary operators
constexpr BitSetArray operator~() const;
constexpr bool operator[](std::size_t pos) const;
constexpr Reference operator[](std::size_t pos)
{
ASSERT(pos < size());
return Reference(*this, pos);
}
// Setter, getters and other helper methods
constexpr BitSetArray &set();
constexpr BitSetArray &set(std::size_t pos, bool value = true);
constexpr BitSetArray &reset();
constexpr BitSetArray &reset(std::size_t pos);
constexpr bool test(std::size_t pos) const;
constexpr bool all() const;
constexpr bool any() const;
constexpr bool none() const;
constexpr std::size_t count() const;
constexpr bool intersects(const BitSetArray &other) const;
constexpr BitSetArray<N> &flip();
constexpr param_type first() const;
constexpr param_type last() const;
constexpr value_type bits(size_t index) const;
// Produces a mask of ones up to the "x"th bit.
constexpr static BitSetArray Mask(std::size_t x);
private:
static constexpr std::size_t kDefaultBitSetSizeMinusOne = priv::kDefaultBitSetSize - 1;
static constexpr std::size_t kShiftForDivision =
static_cast<std::size_t>(rx::Log2(static_cast<unsigned int>(priv::kDefaultBitSetSize)));
static constexpr std::size_t kArraySize =
((N + kDefaultBitSetSizeMinusOne) >> kShiftForDivision);
constexpr static std::size_t kLastElementCount = (N & kDefaultBitSetSizeMinusOne);
constexpr static std::size_t kLastElementMask =
priv::BaseBitSetType::Mask(kLastElementCount == 0 ? priv::kDefaultBitSetSize
: kLastElementCount)
.bits();
std::array<BaseBitSet, kArraySize> mBaseBitSetArray;
};
static_assert(std::is_trivially_copyable<BitSetArray<32>>(), "must be memcpy-able");
template <std::size_t N>
constexpr BitSetArray<N>::BitSetArray()
{
static_assert(N > priv::kDefaultBitSetSize, "BitSetArray type can't support requested size.");
reset();
}
template <std::size_t N>
constexpr BitSetArray<N>::BitSetArray(uint64_t value)
{
reset();
if (priv::kDefaultBitSetSize < 64)
{
size_t i = 0;
for (; i < kArraySize - 1; ++i)
{
value_type elemValue =
value & priv::BaseBitSetType::Mask(priv::kDefaultBitSetSize).bits();
mBaseBitSetArray[i] = priv::BaseBitSetType(elemValue);
value >>= priv::kDefaultBitSetSize;
}
value_type elemValue = value & kLastElementMask;
mBaseBitSetArray[i] = priv::BaseBitSetType(elemValue);
}
else
{
value_type elemValue = value & priv::BaseBitSetType::Mask(priv::kDefaultBitSetSize).bits();
mBaseBitSetArray[0] = priv::BaseBitSetType(elemValue);
}
}
template <std::size_t N>
constexpr BitSetArray<N>::BitSetArray(std::initializer_list<param_type> init)
{
reset();
for (param_type element : init)
{
size_t index = element >> kShiftForDivision;
size_t offset = element & kDefaultBitSetSizeMinusOne;
mBaseBitSetArray[index].set(offset, true);
}
}
template <size_t N>
BitSetArray<N>::Iterator::Iterator(const BitSetArray<N> &bitSetArray, std::size_t index)
: mIndex(index),
mParent(bitSetArray),
mCurrentParent(&mParent),
mCurrentIterator(mParent.mBaseBitSetArray[0].begin())
{
while (mIndex < mCurrentParent->kArraySize)
{
if (mCurrentParent->mBaseBitSetArray[mIndex].any())
{
break;
}
mIndex++;
}
if (mIndex < mCurrentParent->kArraySize)
{
mCurrentIterator = mCurrentParent->mBaseBitSetArray[mIndex].begin();
}
else
{
mCurrentIterator = mCurrentParent->mBaseBitSetArray[mCurrentParent->kArraySize - 1].end();
}
}
template <std::size_t N>
typename BitSetArray<N>::Iterator &BitSetArray<N>::Iterator::operator++()
{
++mCurrentIterator;
while (mCurrentIterator == mCurrentParent->mBaseBitSetArray[mIndex].end())
{
mIndex++;
if (mIndex >= mCurrentParent->kArraySize)
{
break;
}
mCurrentIterator = mCurrentParent->mBaseBitSetArray[mIndex].begin();
}
return *this;
}
template <std::size_t N>
bool BitSetArray<N>::Iterator::operator==(const BitSetArray<N>::Iterator &other) const
{
return mCurrentIterator == other.mCurrentIterator;
}
template <std::size_t N>
bool BitSetArray<N>::Iterator::operator!=(const BitSetArray<N>::Iterator &other) const
{
return mCurrentIterator != other.mCurrentIterator;
}
template <std::size_t N>
std::size_t BitSetArray<N>::Iterator::operator*() const
{
return (mIndex * priv::kDefaultBitSetSize) + *mCurrentIterator;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::operator&=(const BitSetArray<N> &other)
{
for (std::size_t index = 0; index < kArraySize; index++)
{
mBaseBitSetArray[index] &= other.mBaseBitSetArray[index];
}
return *this;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::operator|=(const BitSetArray<N> &other)
{
for (std::size_t index = 0; index < kArraySize; index++)
{
mBaseBitSetArray[index] |= other.mBaseBitSetArray[index];
}
return *this;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::operator^=(const BitSetArray<N> &other)
{
for (std::size_t index = 0; index < kArraySize; index++)
{
mBaseBitSetArray[index] ^= other.mBaseBitSetArray[index];
}
return *this;
}
template <std::size_t N>
constexpr BitSetArray<N> BitSetArray<N>::operator&(const angle::BitSetArray<N> &other) const
{
angle::BitSetArray<N> result(other);
result &= *this;
return result;
}
template <std::size_t N>
constexpr BitSetArray<N> BitSetArray<N>::operator|(const angle::BitSetArray<N> &other) const
{
angle::BitSetArray<N> result(other);
result |= *this;
return result;
}
template <std::size_t N>
constexpr BitSetArray<N> BitSetArray<N>::operator^(const angle::BitSetArray<N> &other) const
{
angle::BitSetArray<N> result(other);
result ^= *this;
return result;
}
template <std::size_t N>
constexpr bool BitSetArray<N>::operator==(const angle::BitSetArray<N> &other) const
{
for (std::size_t index = 0; index < kArraySize; index++)
{
if (mBaseBitSetArray[index] != other.mBaseBitSetArray[index])
{
return false;
}
}
return true;
}
template <std::size_t N>
constexpr bool BitSetArray<N>::operator!=(const angle::BitSetArray<N> &other) const
{
return !(*this == other);
}
template <std::size_t N>
constexpr BitSetArray<N> BitSetArray<N>::operator~() const
{
angle::BitSetArray<N> result;
for (std::size_t index = 0; index < kArraySize; index++)
{
result.mBaseBitSetArray[index] |= ~mBaseBitSetArray[index];
}
// The last element in result may need special handling
result.mBaseBitSetArray[kArraySize - 1] &= kLastElementMask;
return result;
}
template <std::size_t N>
constexpr bool BitSetArray<N>::operator[](std::size_t pos) const
{
ASSERT(pos < size());
return test(pos);
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::set()
{
for (BaseBitSet &baseBitSet : mBaseBitSetArray)
{
baseBitSet.set();
}
// The last element in mBaseBitSetArray may need special handling
mBaseBitSetArray[kArraySize - 1] &= kLastElementMask;
return *this;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::set(std::size_t pos, bool value)
{
ASSERT(pos < size());
// Get the index and offset, then set the bit
size_t index = pos >> kShiftForDivision;
size_t offset = pos & kDefaultBitSetSizeMinusOne;
mBaseBitSetArray[index].set(offset, value);
return *this;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::reset()
{
for (BaseBitSet &baseBitSet : mBaseBitSetArray)
{
baseBitSet.reset();
}
return *this;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::reset(std::size_t pos)
{
ASSERT(pos < size());
return set(pos, false);
}
template <std::size_t N>
constexpr bool BitSetArray<N>::test(std::size_t pos) const
{
ASSERT(pos < size());
// Get the index and offset, then test the bit
size_t index = pos >> kShiftForDivision;
size_t offset = pos & kDefaultBitSetSizeMinusOne;
return mBaseBitSetArray[index].test(offset);
}
template <std::size_t N>
constexpr bool BitSetArray<N>::all() const
{
constexpr priv::BaseBitSetType kLastElementBitSet = priv::BaseBitSetType(kLastElementMask);
for (std::size_t index = 0; index < kArraySize - 1; index++)
{
if (!mBaseBitSetArray[index].all())
{
return false;
}
}
// The last element in mBaseBitSetArray may need special handling
return mBaseBitSetArray[kArraySize - 1] == kLastElementBitSet;
}
template <std::size_t N>
constexpr bool BitSetArray<N>::any() const
{
for (const BaseBitSet &baseBitSet : mBaseBitSetArray)
{
if (baseBitSet.any())
{
return true;
}
}
return false;
}
template <std::size_t N>
constexpr bool BitSetArray<N>::none() const
{
for (const BaseBitSet &baseBitSet : mBaseBitSetArray)
{
if (!baseBitSet.none())
{
return false;
}
}
return true;
}
template <std::size_t N>
constexpr std::size_t BitSetArray<N>::count() const
{
size_t count = 0;
for (const BaseBitSet &baseBitSet : mBaseBitSetArray)
{
count += baseBitSet.count();
}
return count;
}
template <std::size_t N>
constexpr bool BitSetArray<N>::intersects(const BitSetArray<N> &other) const
{
for (std::size_t index = 0; index < kArraySize; index++)
{
if ((mBaseBitSetArray[index].bits() & other.mBaseBitSetArray[index].bits()) != 0)
{
return true;
}
}
return false;
}
template <std::size_t N>
constexpr BitSetArray<N> &BitSetArray<N>::flip()
{
for (BaseBitSet &baseBitSet : mBaseBitSetArray)
{
baseBitSet.flip();
}
// The last element in mBaseBitSetArray may need special handling
mBaseBitSetArray[kArraySize - 1] &= kLastElementMask;
return *this;
}
template <std::size_t N>
constexpr typename BitSetArray<N>::param_type BitSetArray<N>::first() const
{
ASSERT(any());
for (size_t arrayIndex = 0; arrayIndex < kArraySize; ++arrayIndex)
{
const BaseBitSet &baseBitSet = mBaseBitSetArray[arrayIndex];
if (baseBitSet.any())
{
return baseBitSet.first() + arrayIndex * priv::kDefaultBitSetSize;
}
}
UNREACHABLE();
return 0;
}
template <std::size_t N>
constexpr typename BitSetArray<N>::param_type BitSetArray<N>::last() const
{
ASSERT(any());
for (size_t arrayIndex = kArraySize; arrayIndex > 0; --arrayIndex)
{
const BaseBitSet &baseBitSet = mBaseBitSetArray[arrayIndex - 1];
if (baseBitSet.any())
{
return baseBitSet.last() + (arrayIndex - 1) * priv::kDefaultBitSetSize;
}
}
UNREACHABLE();
return 0;
}
template <std::size_t N>
constexpr typename BitSetArray<N>::value_type BitSetArray<N>::bits(size_t index) const
{
return mBaseBitSetArray[index].bits();
}
template <std::size_t N>
constexpr BitSetArray<N> BitSetArray<N>::Mask(std::size_t x)
{
BitSetArray result;
for (size_t arrayIndex = 0; arrayIndex < kArraySize; ++arrayIndex)
{
const size_t bitOffset = arrayIndex * priv::kDefaultBitSetSize;
if (x <= bitOffset)
{
break;
}
const size_t bitsInThisIndex = std::min(x - bitOffset, priv::kDefaultBitSetSize);
result.mBaseBitSetArray[arrayIndex] = BaseBitSet::Mask(bitsInThisIndex);
}
return result;
}
} // namespace angle
template <size_t N, typename BitsT, typename ParamT>
inline constexpr angle::BitSetT<N, BitsT, ParamT> operator&(
const angle::BitSetT<N, BitsT, ParamT> &lhs,
const angle::BitSetT<N, BitsT, ParamT> &rhs)
{
angle::BitSetT<N, BitsT, ParamT> result(lhs);
result &= rhs.bits();
return result;
}
template <size_t N, typename BitsT, typename ParamT>
inline constexpr angle::BitSetT<N, BitsT, ParamT> operator|(
const angle::BitSetT<N, BitsT, ParamT> &lhs,
const angle::BitSetT<N, BitsT, ParamT> &rhs)
{
angle::BitSetT<N, BitsT, ParamT> result(lhs);
result |= rhs.bits();
return result;
}
template <size_t N, typename BitsT, typename ParamT>
inline constexpr angle::BitSetT<N, BitsT, ParamT> operator^(
const angle::BitSetT<N, BitsT, ParamT> &lhs,
const angle::BitSetT<N, BitsT, ParamT> &rhs)
{
angle::BitSetT<N, BitsT, ParamT> result(lhs);
result ^= rhs.bits();
return result;
}
template <size_t N, typename BitsT, typename ParamT>
inline bool operator==(angle::BitSetT<N, BitsT, ParamT> &lhs, angle::BitSetT<N, BitsT, ParamT> &rhs)
{
return lhs.bits() == rhs.bits();
}
template <size_t N, typename BitsT, typename ParamT>
inline bool operator!=(angle::BitSetT<N, BitsT, ParamT> &lhs, angle::BitSetT<N, BitsT, ParamT> &rhs)
{
return !(lhs == rhs);
}
#endif // COMMON_BITSETITERATOR_H_