wide-int-bitmask.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Operation with 128 bit bitmask.
  2. Copyright (C) 2013-2019 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. struct wide_int_bitmask
  18. {
  19. inline wide_int_bitmask ();
  20. inline wide_int_bitmask (uint64_t l);
  21. inline wide_int_bitmask (uint64_t l, uint64_t h);
  22. inline wide_int_bitmask &operator &= (wide_int_bitmask);
  23. inline wide_int_bitmask &operator |= (wide_int_bitmask);
  24. inline wide_int_bitmask operator ~ () const;
  25. inline wide_int_bitmask operator & (wide_int_bitmask) const;
  26. inline wide_int_bitmask operator | (wide_int_bitmask) const;
  27. inline wide_int_bitmask operator >> (int);
  28. inline wide_int_bitmask operator << (int);
  29. inline bool operator == (wide_int_bitmask) const;
  30. inline bool operator != (wide_int_bitmask) const;
  31. uint64_t low, high;
  32. };
  33. inline
  34. wide_int_bitmask::wide_int_bitmask ()
  35. : low (0), high (0)
  36. {
  37. }
  38. inline
  39. wide_int_bitmask::wide_int_bitmask (uint64_t l)
  40. : low (l), high (0)
  41. {
  42. }
  43. inline
  44. wide_int_bitmask::wide_int_bitmask (uint64_t l, uint64_t h)
  45. : low (l), high (h)
  46. {
  47. }
  48. inline wide_int_bitmask &
  49. wide_int_bitmask::operator &= (wide_int_bitmask b)
  50. {
  51. low &= b.low;
  52. high &= b.high;
  53. return *this;
  54. }
  55. inline wide_int_bitmask &
  56. wide_int_bitmask::operator |= (wide_int_bitmask b)
  57. {
  58. low |= b.low;
  59. high |= b.high;
  60. return *this;
  61. }
  62. inline wide_int_bitmask
  63. wide_int_bitmask::operator ~ () const
  64. {
  65. wide_int_bitmask ret (~low, ~high);
  66. return ret;
  67. }
  68. inline wide_int_bitmask
  69. wide_int_bitmask::operator | (wide_int_bitmask b) const
  70. {
  71. wide_int_bitmask ret (low | b.low, high | b.high);
  72. return ret;
  73. }
  74. inline wide_int_bitmask
  75. wide_int_bitmask::operator & (wide_int_bitmask b) const
  76. {
  77. wide_int_bitmask ret (low & b.low, high & b.high);
  78. return ret;
  79. }
  80. inline wide_int_bitmask
  81. wide_int_bitmask::operator << (int amount)
  82. {
  83. wide_int_bitmask ret;
  84. if (amount >= 64)
  85. {
  86. ret.low = 0;
  87. ret.high = low << (amount - 64);
  88. }
  89. else if (amount == 0)
  90. ret = *this;
  91. else
  92. {
  93. ret.low = low << amount;
  94. ret.high = (low >> (64 - amount)) | (high << amount);
  95. }
  96. return ret;
  97. }
  98. inline wide_int_bitmask
  99. wide_int_bitmask::operator >> (int amount)
  100. {
  101. wide_int_bitmask ret;
  102. if (amount >= 64)
  103. {
  104. ret.low = high >> (amount - 64);
  105. ret.high = 0;
  106. }
  107. else if (amount == 0)
  108. ret = *this;
  109. else
  110. {
  111. ret.low = (high << (64 - amount)) | (low >> amount);
  112. ret.high = high >> amount;
  113. }
  114. return ret;
  115. }
  116. inline bool
  117. wide_int_bitmask::operator == (wide_int_bitmask b) const
  118. {
  119. return low == b.low && high == b.high;
  120. }
  121. inline bool
  122. wide_int_bitmask::operator != (wide_int_bitmask b) const
  123. {
  124. return low != b.low || high != b.high;
  125. }
  126. #endif /* ! GCC_WIDE_INT_BITMASK_H */