1 /* Functions to support fixed-length bitmaps.
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 /* Implementation of bounded (fixed length) bitmaps.
25 This provides a drop-in replacement for bitmaps that have outgrown the
26 storage capacity of a single integer.
28 Sets are stored as a fixed length array of uint64_t elements. The length of
29 this array is given as a template parameter. */
31 /* Use recusive templated functions to define constexpr operations. */
33 struct bbitmap_operators
35 /* Return a result that maps binary operator OP to elements [0, M) of
36 X and Y, and takes the remaining elements from REST. */
37 template<typename Result
, typename Operator
, typename Arg
, typename
...Rest
>
38 static constexpr Result
binary(Operator op
, const Arg
&x
, const Arg
&y
,
41 return bbitmap_operators
<M
- 1>::template binary
<Result
, Operator
, Arg
>
42 (op
, x
, y
, op (x
.val
[M
- 1], y
.val
[M
- 1]), rest
...);
45 /* Return a result that contains the bitwise inverse of elements [0, M) of X,
46 and takes the remaining elements from REST. */
47 template<typename Result
, typename Arg
, typename
...Rest
>
48 static constexpr Result
bit_not(const Arg
&x
, Rest
...rest
)
50 return bbitmap_operators
<M
- 1>::template bit_not
<Result
, Arg
>
51 (x
, ~(x
.val
[M
- 1]), rest
...);
54 /* Return true if any element [0, M) of X is nonzero. */
55 template<typename Arg
>
56 static constexpr bool non_zero(const Arg
&x
)
58 return (bool) x
.val
[M
- 1]
59 || bbitmap_operators
<M
- 1>::template non_zero
<Arg
> (x
);
62 /* Return true if elements [0, M) of X are all equal to the corresponding
64 template<typename Arg
>
65 static constexpr bool equal(const Arg
&x
, const Arg
&y
)
67 return x
.val
[M
- 1] == y
.val
[M
- 1]
68 && bbitmap_operators
<M
- 1>::template equal
<Arg
> (x
, y
);
71 /* If bit index INDEX selects a bit in the first M elements, return a
72 Result with that bit set and the other bits of the leading M elements
73 clear. Clear the leading M elements otherwise. Take the remaining
74 elements of the Result from REST. */
75 template<typename Result
, typename
...Rest
>
76 static constexpr Result
from_index(int index
, Rest
...rest
)
78 return bbitmap_operators
<M
- 1>::template from_index
<Result
>
80 uint64_t ((index
- (M
- 1) * 64) == (index
& 63)) << (index
& 63),
85 /* These functions form the base for the recursive functions above. They
86 return either bitmap containing the elements passed in REST, or a default
89 struct bbitmap_operators
<0>
91 template<typename Result
, typename Operator
, typename Arg
, typename
...Rest
>
92 static constexpr Result
binary(Operator
, const Arg
, const Arg
,
95 return Result
{ rest
... };
98 template<typename Result
, typename Arg
, typename
...Rest
>
99 static constexpr Result
bit_not(const Arg
, Rest
...rest
)
101 return Result
{ rest
... };
104 template<typename Arg
>
105 static constexpr bool non_zero(const Arg
)
110 template<typename Arg
>
111 static constexpr bool equal(const Arg
, const Arg
)
116 template<typename Result
, typename
...Rest
>
117 static constexpr Result
from_index(int, Rest
...rest
)
119 return Result
{ rest
... };
124 constexpr T
bbitmap_element_or(T x
, T y
) { return x
| y
;}
127 constexpr T
bbitmap_element_and(T x
, T y
) { return x
& y
;}
130 constexpr T
bbitmap_element_xor(T x
, T y
) { return x
^ y
;}
135 class GTY((user
)) bbitmap
140 template<typename
... Rest
>
141 constexpr bbitmap(Rest
...rest
) : val
{(uint64_t) rest
...} {}
143 constexpr bbitmap
<N
> operator|(const bbitmap
<N
> other
) const
145 return bbitmap_operators
<N
>::template binary
<bbitmap
<N
>>
146 (bbitmap_element_or
<uint64_t>, *this, other
);
149 bbitmap
<N
> operator|=(const bbitmap
<N
> other
)
151 for (int i
= 0; i
< N
; i
++)
152 val
[i
] |= other
.val
[i
];
157 constexpr bbitmap
<N
> operator&(const bbitmap
<N
> other
) const
159 return bbitmap_operators
<N
>::template binary
<bbitmap
<N
>>
160 (bbitmap_element_and
<uint64_t>, *this, other
);
163 bbitmap
<N
> operator&=(const bbitmap
<N
> other
)
165 for (int i
= 0; i
< N
; i
++)
166 val
[i
] &= other
.val
[i
];
171 constexpr bbitmap
<N
> operator^(const bbitmap
<N
> other
) const
173 return bbitmap_operators
<N
>::template binary
<bbitmap
<N
>>
174 (bbitmap_element_xor
<uint64_t>, *this, other
);
177 bbitmap
<N
> operator^=(const bbitmap
<N
> other
)
179 for (int i
= 0; i
< N
; i
++)
180 val
[i
] ^= other
.val
[i
];
185 constexpr bbitmap
<N
> operator~() const
187 return bbitmap_operators
<N
>::template bit_not
<bbitmap
<N
>>(*this);
190 constexpr bool operator!() const
192 return !(bbitmap_operators
<N
>::template non_zero
<bbitmap
<N
>>(*this));
195 constexpr explicit operator bool() const
197 return bbitmap_operators
<N
>::template non_zero
<bbitmap
<N
>>(*this);
200 constexpr bool operator==(const bbitmap
<N
> other
) const
202 return bbitmap_operators
<N
>::template equal
<bbitmap
<N
>>(*this, other
);
205 constexpr bool operator!=(const bbitmap
<N
> other
) const
207 return !(bbitmap_operators
<N
>::template equal
<bbitmap
<N
>>(*this, other
));
210 /* Return a bitmap with bit INDEX set and all other bits clear. */
212 static constexpr bbitmap
<N
> from_index (int index
)
214 return bbitmap_operators
<N
>::template from_index
<bbitmap
<N
>> (index
);
220 gt_ggc_mx (bbitmap
<N
> *)
226 gt_pch_nx (bbitmap
<N
> *)
232 gt_pch_nx (bbitmap
<N
> *, gt_pointer_operator
, void *)