cgen-bitset.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* CGEN generic opcode support.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. This file is part of libopcodes.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. It is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* Functions for manipulating CGEN_BITSET. */
  16. #include "libiberty.h"
  17. #include "cgen/bitset.h"
  18. #include <string.h>
  19. /* Create a bit mask. */
  20. CGEN_BITSET *
  21. cgen_bitset_create (unsigned bit_count)
  22. {
  23. CGEN_BITSET * mask = xmalloc (sizeof (* mask));
  24. cgen_bitset_init (mask, bit_count);
  25. return mask;
  26. }
  27. /* Initialize an existing bit mask. */
  28. void
  29. cgen_bitset_init (CGEN_BITSET * mask, unsigned bit_count)
  30. {
  31. if (! mask)
  32. return;
  33. mask->length = (bit_count / 8) + 1;
  34. mask->bits = xmalloc (mask->length);
  35. cgen_bitset_clear (mask);
  36. }
  37. /* Clear the bits of a bit mask. */
  38. void
  39. cgen_bitset_clear (CGEN_BITSET * mask)
  40. {
  41. unsigned i;
  42. if (! mask)
  43. return;
  44. for (i = 0; i < mask->length; ++i)
  45. mask->bits[i] = 0;
  46. }
  47. /* Add a bit to a bit mask. */
  48. void
  49. cgen_bitset_add (CGEN_BITSET * mask, unsigned bit_num)
  50. {
  51. int byte_ix, bit_ix;
  52. int bit_mask;
  53. if (! mask)
  54. return;
  55. byte_ix = bit_num / 8;
  56. bit_ix = bit_num % 8;
  57. bit_mask = 1 << (7 - bit_ix);
  58. mask->bits[byte_ix] |= bit_mask;
  59. }
  60. /* Set a bit mask. */
  61. void
  62. cgen_bitset_set (CGEN_BITSET * mask, unsigned bit_num)
  63. {
  64. if (! mask)
  65. return;
  66. cgen_bitset_clear (mask);
  67. cgen_bitset_add (mask, bit_num);
  68. }
  69. /* Test for a bit in a bit mask.
  70. Returns 1 if the bit is found */
  71. int
  72. cgen_bitset_contains (CGEN_BITSET * mask, unsigned bit_num)
  73. {
  74. int byte_ix, bit_ix;
  75. int bit_mask;
  76. if (! mask)
  77. return 1; /* No bit restrictions. */
  78. byte_ix = bit_num / 8;
  79. bit_ix = 7 - (bit_num % 8);
  80. bit_mask = 1 << bit_ix;
  81. return (mask->bits[byte_ix] & bit_mask) >> bit_ix;
  82. }
  83. /* Compare two bit masks for equality.
  84. Returns 0 if they are equal. */
  85. int
  86. cgen_bitset_compare (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
  87. {
  88. if (mask1 == mask2)
  89. return 0;
  90. if (! mask1 || ! mask2)
  91. return 1;
  92. if (mask1->length != mask2->length)
  93. return 1;
  94. return memcmp (mask1->bits, mask2->bits, mask1->length);
  95. }
  96. /* Test two bit masks for common bits.
  97. Returns 1 if a common bit is found. */
  98. int
  99. cgen_bitset_intersect_p (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
  100. {
  101. unsigned i, limit;
  102. if (mask1 == mask2)
  103. return 1;
  104. if (! mask1 || ! mask2)
  105. return 0;
  106. limit = mask1->length < mask2->length ? mask1->length : mask2->length;
  107. for (i = 0; i < limit; ++i)
  108. if ((mask1->bits[i] & mask2->bits[i]))
  109. return 1;
  110. return 0;
  111. }
  112. /* Make a copy of a bit mask. */
  113. CGEN_BITSET *
  114. cgen_bitset_copy (CGEN_BITSET * mask)
  115. {
  116. CGEN_BITSET* newmask;
  117. if (! mask)
  118. return NULL;
  119. newmask = cgen_bitset_create ((mask->length * 8) - 1);
  120. memcpy (newmask->bits, mask->bits, mask->length);
  121. return newmask;
  122. }
  123. /* Combine two bit masks. */
  124. void
  125. cgen_bitset_union (CGEN_BITSET * mask1, CGEN_BITSET * mask2,
  126. CGEN_BITSET * result)
  127. {
  128. unsigned i;
  129. if (! mask1 || ! mask2 || ! result
  130. || mask1->length != mask2->length
  131. || mask1->length != result->length)
  132. return;
  133. for (i = 0; i < result->length; ++i)
  134. result->bits[i] = mask1->bits[i] | mask2->bits[i];
  135. }