Coverage Report

Created: 2026-03-10 08:46

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