wide-int-bitmask.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Operation with 128 bit bitmask.
  2. Copyright (C) 2013-2020 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_WIDE_INT_BITMASK_H
  16. #define GCC_WIDE_INT_BITMASK_H
  17. class wide_int_bitmask
  18. {
  19. public:
  20. inline wide_int_bitmask ();
  21. inline wide_int_bitmask (uint64_t l);
  22. inline wide_int_bitmask (uint64_t l, uint64_t h);
  23. inline wide_int_bitmask &operator &= (wide_int_bitmask);
  24. inline wide_int_bitmask &operator |= (wide_int_bitmask);
  25. inline wide_int_bitmask operator ~ () const;
  26. inline wide_int_bitmask operator & (wide_int_bitmask) const;
  27. inline wide_int_bitmask operator | (wide_int_bitmask) const;
  28. inline wide_int_bitmask operator >> (int);
  29. inline wide_int_bitmask operator << (int);
  30. inline bool operator == (wide_int_bitmask) const;
  31. inline bool operator != (wide_int_bitmask) const;
  32. uint64_t low, high;
  33. };
  34. inline
  35. wide_int_bitmask::wide_int_bitmask ()
  36. : low (0), high (0)
  37. {
  38. }
  39. inline
  40. wide_int_bitmask::wide_int_bitmask (uint64_t l)
  41. : low (l), high (0)
  42. {
  43. }
  44. inline
  45. wide_int_bitmask::wide_int_bitmask (uint64_t l, uint64_t h)
  46. : low (l), high (h)
  47. {
  48. }
  49. inline wide_int_bitmask &
  50. wide_int_bitmask::operator &= (wide_int_bitmask b)
  51. {
  52. low &= b.low;
  53. high &= b.high;
  54. return *this;
  55. }
  56. inline wide_int_bitmask &
  57. wide_int_bitmask::operator |= (wide_int_bitmask b)
  58. {
  59. low |= b.low;
  60. high |= b.high;
  61. return *this;
  62. }
  63. inline wide_int_bitmask
  64. wide_int_bitmask::operator ~ () const
  65. {
  66. wide_int_bitmask ret (~low, ~high);
  67. return ret;
  68. }
  69. inline wide_int_bitmask
  70. wide_int_bitmask::operator | (wide_int_bitmask b) const
  71. {
  72. wide_int_bitmask ret (low | b.low, high | b.high);
  73. return ret;
  74. }
  75. inline wide_int_bitmask
  76. wide_int_bitmask::operator & (wide_int_bitmask b) const
  77. {
  78. wide_int_bitmask ret (low & b.low, high & b.high);
  79. return ret;
  80. }
  81. inline wide_int_bitmask
  82. wide_int_bitmask::operator << (int amount)
  83. {
  84. wide_int_bitmask ret;
  85. if (amount >= 64)
  86. {
  87. ret.low = 0;
  88. ret.high = low << (amount - 64);
  89. }
  90. else if (amount == 0)
  91. ret = *this;
  92. else
  93. {
  94. ret.low = low << amount;
  95. ret.high = (low >> (64 - amount)) | (high << amount);
  96. }
  97. return ret;
  98. }
  99. inline wide_int_bitmask
  100. wide_int_bitmask::operator >> (int amount)
  101. {
  102. wide_int_bitmask ret;
  103. if (amount >= 64)
  104. {
  105. ret.low = high >> (amount - 64);
  106. ret.high = 0;
  107. }
  108. else if (amount == 0)
  109. ret = *this;
  110. else
  111. {
  112. ret.low = (high << (64 - amount)) | (low >> amount);
  113. ret.high = high >> amount;
  114. }
  115. return ret;
  116. }
  117. inline bool
  118. wide_int_bitmask::operator == (wide_int_bitmask b) const
  119. {
  120. return low == b.low && high == b.high;
  121. }
  122. inline bool
  123. wide_int_bitmask::operator != (wide_int_bitmask b) const
  124. {
  125. return low != b.low || high != b.high;
  126. }
  127. #endif /* ! GCC_WIDE_INT_BITMASK_H */