xref: /PHP-7.4/Zend/zend_bitset.h (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache JIT                                                     |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Dmitry Stogov <dmitry@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef _ZEND_BITSET_H_
20 #define _ZEND_BITSET_H_
21 
22 typedef zend_ulong *zend_bitset;
23 
24 #define ZEND_BITSET_ELM_SIZE sizeof(zend_ulong)
25 
26 #if SIZEOF_ZEND_LONG == 4
27 # define ZEND_BITSET_ELM_NUM(n)		((n) >> 5)
28 # define ZEND_BITSET_BIT_NUM(n)		((zend_ulong)(n) & Z_UL(0x1f))
29 #elif SIZEOF_ZEND_LONG == 8
30 # define ZEND_BITSET_ELM_NUM(n)		((n) >> 6)
31 # define ZEND_BITSET_BIT_NUM(n)		((zend_ulong)(n) & Z_UL(0x3f))
32 #else
33 # define ZEND_BITSET_ELM_NUM(n)		((n) / (sizeof(zend_long) * 8))
34 # define ZEND_BITSET_BIT_NUM(n)		((n) % (sizeof(zend_long) * 8))
35 #endif
36 
37 #define ZEND_BITSET_ALLOCA(n, use_heap) \
38 	(zend_bitset)do_alloca((n) * ZEND_BITSET_ELM_SIZE, use_heap)
39 
40 /* Number of trailing zero bits (0x01 -> 0; 0x40 -> 6; 0x00 -> LEN) */
zend_ulong_ntz(zend_ulong num)41 static zend_always_inline int zend_ulong_ntz(zend_ulong num)
42 {
43 #if (defined(__GNUC__) || __has_builtin(__builtin_ctzl)) \
44 	&& SIZEOF_ZEND_LONG == SIZEOF_LONG && defined(PHP_HAVE_BUILTIN_CTZL)
45 	return __builtin_ctzl(num);
46 #elif (defined(__GNUC__) || __has_builtin(__builtin_ctzll)) && defined(PHP_HAVE_BUILTIN_CTZLL)
47 	return __builtin_ctzll(num);
48 #elif defined(_WIN32)
49 	unsigned long index;
50 
51 #if defined(_WIN64)
52 	if (!BitScanForward64(&index, num)) {
53 #else
54 	if (!BitScanForward(&index, num)) {
55 #endif
56 		/* undefined behavior */
57 		return SIZEOF_ZEND_LONG * 8;
58 	}
59 
60 	return (int) index;
61 #else
62 	int n;
63 
64 	if (num == Z_UL(0)) return SIZEOF_ZEND_LONG * 8;
65 
66 	n = 1;
67 #if SIZEOF_ZEND_LONG == 8
68 	if ((num & 0xffffffff) == 0) {n += 32; num = num >> Z_UL(32);}
69 #endif
70 	if ((num & 0x0000ffff) == 0) {n += 16; num = num >> 16;}
71 	if ((num & 0x000000ff) == 0) {n +=  8; num = num >>  8;}
72 	if ((num & 0x0000000f) == 0) {n +=  4; num = num >>  4;}
73 	if ((num & 0x00000003) == 0) {n +=  2; num = num >>  2;}
74 	return n - (num & 1);
75 #endif
76 }
77 
78 /* Returns the number of zend_ulong words needed to store a bitset that is N
79    bits long.  */
80 static inline uint32_t zend_bitset_len(uint32_t n)
81 {
82 	return (n + ((sizeof(zend_long) * 8) - 1)) / (sizeof(zend_long) * 8);
83 }
84 
85 static inline zend_bool zend_bitset_in(zend_bitset set, uint32_t n)
86 {
87 	return ZEND_BIT_TEST(set, n);
88 }
89 
90 static inline void zend_bitset_incl(zend_bitset set, uint32_t n)
91 {
92 	set[ZEND_BITSET_ELM_NUM(n)] |= Z_UL(1) << ZEND_BITSET_BIT_NUM(n);
93 }
94 
95 static inline void zend_bitset_excl(zend_bitset set, uint32_t n)
96 {
97 	set[ZEND_BITSET_ELM_NUM(n)] &= ~(Z_UL(1) << ZEND_BITSET_BIT_NUM(n));
98 }
99 
100 static inline void zend_bitset_clear(zend_bitset set, uint32_t len)
101 {
102 	memset(set, 0, len * ZEND_BITSET_ELM_SIZE);
103 }
104 
105 static inline int zend_bitset_empty(zend_bitset set, uint32_t len)
106 {
107 	uint32_t i;
108 	for (i = 0; i < len; i++) {
109 		if (set[i]) {
110 			return 0;
111 		}
112 	}
113 	return 1;
114 }
115 
116 static inline void zend_bitset_fill(zend_bitset set, uint32_t len)
117 {
118 	memset(set, 0xff, len * ZEND_BITSET_ELM_SIZE);
119 }
120 
121 static inline zend_bool zend_bitset_equal(zend_bitset set1, zend_bitset set2, uint32_t len)
122 {
123     return memcmp(set1, set2, len * ZEND_BITSET_ELM_SIZE) == 0;
124 }
125 
126 static inline void zend_bitset_copy(zend_bitset set1, zend_bitset set2, uint32_t len)
127 {
128     memcpy(set1, set2, len * ZEND_BITSET_ELM_SIZE);
129 }
130 
131 static inline void zend_bitset_intersection(zend_bitset set1, zend_bitset set2, uint32_t len)
132 {
133     uint32_t i;
134 
135     for (i = 0; i < len; i++) {
136 		set1[i] &= set2[i];
137 	}
138 }
139 
140 static inline void zend_bitset_union(zend_bitset set1, zend_bitset set2, uint32_t len)
141 {
142 	uint32_t i;
143 
144 	for (i = 0; i < len; i++) {
145 		set1[i] |= set2[i];
146 	}
147 }
148 
149 static inline void zend_bitset_difference(zend_bitset set1, zend_bitset set2, uint32_t len)
150 {
151 	uint32_t i;
152 
153 	for (i = 0; i < len; i++) {
154 		set1[i] = set1[i] & ~set2[i];
155 	}
156 }
157 
158 static inline void zend_bitset_union_with_intersection(zend_bitset set1, zend_bitset set2, zend_bitset set3, zend_bitset set4, uint32_t len)
159 {
160 	uint32_t i;
161 
162 	for (i = 0; i < len; i++) {
163 		set1[i] = set2[i] | (set3[i] & set4[i]);
164 	}
165 }
166 
167 static inline void zend_bitset_union_with_difference(zend_bitset set1, zend_bitset set2, zend_bitset set3, zend_bitset set4, uint32_t len)
168 {
169 	uint32_t i;
170 
171 	for (i = 0; i < len; i++) {
172 		set1[i] = set2[i] | (set3[i] & ~set4[i]);
173 	}
174 }
175 
176 static inline zend_bool zend_bitset_subset(zend_bitset set1, zend_bitset set2, uint32_t len)
177 {
178 	uint32_t i;
179 
180 	for (i = 0; i < len; i++) {
181 		if (set1[i] & ~set2[i]) {
182 			return 0;
183 		}
184 	}
185 	return 1;
186 }
187 
188 static inline int zend_bitset_first(zend_bitset set, uint32_t len)
189 {
190 	uint32_t i;
191 
192 	for (i = 0; i < len; i++) {
193 		if (set[i]) {
194 			return ZEND_BITSET_ELM_SIZE * 8 * i + zend_ulong_ntz(set[i]);
195 		}
196 	}
197 	return -1; /* empty set */
198 }
199 
200 static inline int zend_bitset_last(zend_bitset set, uint32_t len)
201 {
202 	uint32_t i = len;
203 
204 	while (i > 0) {
205 		i--;
206 		if (set[i]) {
207 			int j = ZEND_BITSET_ELM_SIZE * 8 * i - 1;
208 			zend_ulong x = set[i];
209 			while (x != Z_UL(0)) {
210 				x = x >> Z_UL(1);
211 				j++;
212 			}
213 			return j;
214 		}
215 	}
216 	return -1; /* empty set */
217 }
218 
219 #define ZEND_BITSET_FOREACH(set, len, bit) do { \
220 	zend_bitset _set = (set); \
221 	uint32_t _i, _len = (len); \
222 	for (_i = 0; _i < _len; _i++) { \
223 		zend_ulong _x = _set[_i]; \
224 		if (_x) { \
225 			(bit) = ZEND_BITSET_ELM_SIZE * 8 * _i; \
226 			for (; _x != 0; _x >>= Z_UL(1), (bit)++) { \
227 				if (!(_x & Z_UL(1))) continue;
228 
229 #define ZEND_BITSET_REVERSE_FOREACH(set, len, bit) do { \
230 	zend_bitset _set = (set); \
231 	uint32_t _i = (len); \
232 	zend_ulong _test = Z_UL(1) << (ZEND_BITSET_ELM_SIZE * 8 - 1); \
233 	while (_i-- > 0) { \
234 		zend_ulong _x = _set[_i]; \
235 		if (_x) { \
236 			(bit) = ZEND_BITSET_ELM_SIZE * 8 * (_i + 1) - 1; \
237 			for (; _x != 0; _x <<= Z_UL(1), (bit)--) { \
238 				if (!(_x & _test)) continue; \
239 
240 #define ZEND_BITSET_FOREACH_END() \
241 			} \
242 		} \
243 	} \
244 } while (0)
245 
246 static inline int zend_bitset_pop_first(zend_bitset set, uint32_t len) {
247 	int i = zend_bitset_first(set, len);
248 	if (i >= 0) {
249 		zend_bitset_excl(set, i);
250 	}
251 	return i;
252 }
253 
254 #endif /* _ZEND_BITSET_H_ */
255