Line data Source code
1 : /* HOST_WIDE_INT definitions for the GNU compiler.
2 : Copyright (C) 1998-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : Provide definitions for macros which depend on HOST_BITS_PER_INT
7 : and HOST_BITS_PER_LONG. */
8 :
9 : #ifndef GCC_HWINT_H
10 : #define GCC_HWINT_H
11 :
12 : /* This describes the machine the compiler is hosted on. */
13 : #define HOST_BITS_PER_CHAR CHAR_BIT
14 : #define HOST_BITS_PER_SHORT (CHAR_BIT * SIZEOF_SHORT)
15 : #define HOST_BITS_PER_INT (CHAR_BIT * SIZEOF_INT)
16 : #define HOST_BITS_PER_LONG (CHAR_BIT * SIZEOF_LONG)
17 : #define HOST_BITS_PER_PTR (CHAR_BIT * SIZEOF_VOID_P)
18 : #define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
19 :
20 : /* The string that should be inserted into a printf style format to
21 : indicate a "long" operand. */
22 : #ifndef HOST_LONG_FORMAT
23 : #define HOST_LONG_FORMAT "l"
24 : #endif
25 :
26 : /* The string that should be inserted into a printf style format to
27 : indicate a "long long" operand. */
28 : #ifndef HOST_LONG_LONG_FORMAT
29 : #define HOST_LONG_LONG_FORMAT "ll"
30 : #endif
31 :
32 : /* Set HOST_WIDE_INT, this should be always 64 bits.
33 : The underlying type is matched to that of int64_t and assumed
34 : to be either long or long long. */
35 :
36 : #define HOST_BITS_PER_WIDE_INT 64
37 : #if INT64_T_IS_LONG
38 : # define HOST_WIDE_INT long
39 : # define HOST_WIDE_INT_C(X) X ## L
40 : #else
41 : # if HOST_BITS_PER_LONGLONG == 64
42 : # define HOST_WIDE_INT long long
43 : # define HOST_WIDE_INT_C(X) X ## LL
44 : # else
45 : #error "Unable to find a suitable type for HOST_WIDE_INT"
46 : # endif
47 : #endif
48 :
49 : #define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
50 : #define HOST_WIDE_INT_0 HOST_WIDE_INT_C (0)
51 : #define HOST_WIDE_INT_0U HOST_WIDE_INT_UC (0)
52 : #define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
53 : #define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
54 : #define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
55 : #define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
56 :
57 : /* This is a magic identifier which allows GCC to figure out the type
58 : of HOST_WIDE_INT for %wd specifier checks. You must issue this
59 : typedef before using the __asm_fprintf__ format attribute. */
60 : typedef HOST_WIDE_INT __gcc_host_wide_int__;
61 :
62 : /* Provide C99 <inttypes.h> style format definitions for 64bits. */
63 : #ifndef HAVE_INTTYPES_H
64 : #if INT64_T_IS_LONG
65 : # define GCC_PRI64 HOST_LONG_FORMAT
66 : #else
67 : # define GCC_PRI64 HOST_LONG_LONG_FORMAT
68 : #endif
69 : #undef PRId64
70 : #define PRId64 GCC_PRI64 "d"
71 : #undef PRIi64
72 : #define PRIi64 GCC_PRI64 "i"
73 : #undef PRIo64
74 : #define PRIo64 GCC_PRI64 "o"
75 : #undef PRIu64
76 : #define PRIu64 GCC_PRI64 "u"
77 : #undef PRIx64
78 : #define PRIx64 GCC_PRI64 "x"
79 : #undef PRIX64
80 : #define PRIX64 GCC_PRI64 "X"
81 : #endif
82 :
83 : /* Various printf format strings for HOST_WIDE_INT. */
84 :
85 : #if INT64_T_IS_LONG
86 : # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
87 : # define HOST_WIDE_INT_PRINT_C "L"
88 : #else
89 : # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
90 : # define HOST_WIDE_INT_PRINT_C "LL"
91 : #endif
92 :
93 : #define HOST_WIDE_INT_PRINT_DEC "%" PRId64
94 : #define HOST_WIDE_INT_PRINT_DEC_C "%" PRId64 HOST_WIDE_INT_PRINT_C
95 : #define HOST_WIDE_INT_PRINT_UNSIGNED "%" PRIu64
96 : #define HOST_WIDE_INT_PRINT_HEX "%#" PRIx64
97 : #define HOST_WIDE_INT_PRINT_HEX_PURE "%" PRIx64
98 : #define HOST_WIDE_INT_PRINT_DOUBLE_HEX "0x%" PRIx64 "%016" PRIx64
99 : #define HOST_WIDE_INT_PRINT_PADDED_HEX "%016" PRIx64
100 :
101 : /* Similarly format modifier for printing size_t. As not all hosts support
102 : z modifier in printf, use GCC_PRISZ and cast argument to fmt_size_t.
103 : So, instead of doing fprintf ("%zu\n", sizeof (x) * y); use
104 : fprintf (HOST_SIZE_T_PRINT_UNSIGNED "\n",
105 : (fmt_size_t) (sizeof (x) * y)); */
106 : #if SIZE_MAX <= UINT_MAX
107 : # define GCC_PRISZ ""
108 : # define fmt_size_t unsigned int
109 : #elif SIZE_MAX <= ULONG_MAX
110 : # define GCC_PRISZ HOST_LONG_FORMAT
111 : # define fmt_size_t unsigned long int
112 : #else
113 : # define GCC_PRISZ HOST_LONG_LONG_FORMAT
114 : # define fmt_size_t unsigned long long int
115 : #endif
116 :
117 : #define HOST_SIZE_T_PRINT_DEC "%" GCC_PRISZ "d"
118 : #define HOST_SIZE_T_PRINT_UNSIGNED "%" GCC_PRISZ "u"
119 : #define HOST_SIZE_T_PRINT_HEX "%#" GCC_PRISZ "x"
120 : #define HOST_SIZE_T_PRINT_HEX_PURE "%" GCC_PRISZ "x"
121 :
122 : /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
123 : efficiently in hardware. (That is, the widest integer type that fits
124 : in a hardware register.) Normally this is "long" but on some hosts it
125 : should be "long long" or "__int64". This is no convenient way to
126 : autodetect this, so such systems must set a flag in config.host; see there
127 : for details. */
128 :
129 : #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
130 : # define HOST_WIDEST_FAST_INT long long
131 : # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
132 : #else
133 : # define HOST_WIDEST_FAST_INT long
134 : # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
135 : #endif
136 :
137 : /* Inline functions operating on HOST_WIDE_INT. */
138 :
139 : /* Return X with all but the lowest bit masked off. */
140 :
141 : inline unsigned HOST_WIDE_INT
142 2482237055 : least_bit_hwi (unsigned HOST_WIDE_INT x)
143 : {
144 2481855492 : return (x & -x);
145 : }
146 :
147 : /* True if X is zero or a power of two. */
148 :
149 : inline bool
150 2405923273 : pow2_or_zerop (unsigned HOST_WIDE_INT x)
151 : {
152 2415986881 : return least_bit_hwi (x) == x;
153 : }
154 :
155 : /* True if X is a power of two. */
156 :
157 : inline bool
158 424201502 : pow2p_hwi (unsigned HOST_WIDE_INT x)
159 : {
160 421186259 : return x && pow2_or_zerop (x);
161 : }
162 :
163 : #if GCC_VERSION < 3004
164 :
165 : extern int clz_hwi (unsigned HOST_WIDE_INT x);
166 : extern int ctz_hwi (unsigned HOST_WIDE_INT x);
167 : extern int ffs_hwi (unsigned HOST_WIDE_INT x);
168 :
169 : /* Return the number of set bits in X. */
170 : extern int popcount_hwi (unsigned HOST_WIDE_INT x);
171 :
172 : /* Return log2, or -1 if not exact. */
173 : extern int exact_log2 (unsigned HOST_WIDE_INT);
174 :
175 : /* Return floor of log2, with -1 for zero. */
176 : extern int floor_log2 (unsigned HOST_WIDE_INT);
177 :
178 : /* Return the smallest n such that 2**n >= X. */
179 : extern int ceil_log2 (unsigned HOST_WIDE_INT);
180 :
181 : #else /* GCC_VERSION >= 3004 */
182 :
183 : /* For convenience, define 0 -> word_size. */
184 : inline int
185 4245341563 : clz_hwi (unsigned HOST_WIDE_INT x)
186 : {
187 3672530837 : if (x == 0)
188 : return HOST_BITS_PER_WIDE_INT;
189 : # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
190 3706370473 : return __builtin_clzl (x);
191 : # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
192 : return __builtin_clzll (x);
193 : # else
194 : return __builtin_clz (x);
195 : # endif
196 : }
197 :
198 : inline int
199 7666896938 : ctz_hwi (unsigned HOST_WIDE_INT x)
200 : {
201 7509436719 : if (x == 0)
202 : return HOST_BITS_PER_WIDE_INT;
203 : # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
204 7426237597 : return __builtin_ctzl (x);
205 : # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
206 : return __builtin_ctzll (x);
207 : # else
208 : return __builtin_ctz (x);
209 : # endif
210 : }
211 :
212 : inline int
213 5888573209 : ffs_hwi (unsigned HOST_WIDE_INT x)
214 : {
215 : # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
216 5888276330 : return __builtin_ffsl (x);
217 : # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
218 : return __builtin_ffsll (x);
219 : # else
220 : return __builtin_ffs (x);
221 : # endif
222 : }
223 :
224 : inline int
225 310732378 : popcount_hwi (unsigned HOST_WIDE_INT x)
226 : {
227 : # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
228 310724764 : return __builtin_popcountl (x);
229 : # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
230 : return __builtin_popcountll (x);
231 : # else
232 : return __builtin_popcount (x);
233 : # endif
234 : }
235 :
236 : inline int
237 3357547742 : floor_log2 (unsigned HOST_WIDE_INT x)
238 : {
239 3664590584 : return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
240 : }
241 :
242 : inline int
243 63719198 : ceil_log2 (unsigned HOST_WIDE_INT x)
244 : {
245 77436014 : return x == 0 ? 0 : floor_log2 (x - 1) + 1;
246 : }
247 :
248 : inline int
249 254647424 : exact_log2 (unsigned HOST_WIDE_INT x)
250 : {
251 1032213270 : return pow2p_hwi (x) ? ctz_hwi (x) : -1;
252 : }
253 :
254 : #endif /* GCC_VERSION >= 3004 */
255 :
256 : #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
257 : (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1))
258 : #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
259 :
260 : extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
261 : extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
262 : extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
263 : extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
264 : extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
265 : extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
266 : extern unsigned HOST_WIDE_INT reflect_hwi (unsigned HOST_WIDE_INT, unsigned);
267 :
268 : /* Calculate CRC for a given initial CRC, DATA, POLYNOMIAL,
269 : CRC_SIZE in bits and DATA_SIZE in bits. */
270 : extern unsigned HOST_WIDE_INT
271 : calculate_crc (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
272 : unsigned HOST_WIDE_INT, unsigned short, unsigned short);
273 :
274 : /* Calculate reversed CRC for a given initial CRC, DATA, POLYNOMIAL,
275 : CRC_SIZE in bits and DATA_SIZE in bits. */
276 : extern unsigned HOST_WIDE_INT
277 : calculate_reversed_crc (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
278 : unsigned HOST_WIDE_INT, unsigned short,
279 : unsigned short);
280 :
281 : /* Like ctz_hwi, except 0 when x == 0. */
282 :
283 : inline int
284 101127805 : ctz_or_zero (unsigned HOST_WIDE_INT x)
285 : {
286 101127805 : return ffs_hwi (x) - 1;
287 : }
288 :
289 : /* Sign extend SRC starting from PREC. */
290 :
291 : inline HOST_WIDE_INT
292 28233388928 : sext_hwi (HOST_WIDE_INT src, unsigned int prec)
293 : {
294 28233388928 : if (prec == HOST_BITS_PER_WIDE_INT)
295 : return src;
296 : else
297 : {
298 26359668249 : gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
299 : #if defined (__GNUC__)
300 : /* Take the faster path if the implementation-defined bits it's relying
301 : on are implemented the way we expect them to be. Namely, conversion
302 : from unsigned to signed preserves bit pattern, and right shift of
303 : a signed value propagates the sign bit.
304 : We have to convert from signed to unsigned and back, because when left
305 : shifting signed values, any overflow is undefined behavior. */
306 26359668249 : int shift = HOST_BITS_PER_WIDE_INT - prec;
307 26359668249 : return ((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) src << shift)) >> shift;
308 : #else
309 : /* Fall back to the slower, well defined path otherwise. */
310 : HOST_WIDE_INT sign_mask = HOST_WIDE_INT_1 << (prec - 1);
311 : HOST_WIDE_INT value_mask = (HOST_WIDE_INT_1U << prec) - HOST_WIDE_INT_1U;
312 : return (((src & value_mask) ^ sign_mask) - sign_mask);
313 : #endif
314 : }
315 : }
316 :
317 : /* Zero extend SRC starting from PREC. */
318 : inline unsigned HOST_WIDE_INT
319 19383001392 : zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
320 : {
321 19383001392 : if (prec == HOST_BITS_PER_WIDE_INT)
322 : return src;
323 : else
324 : {
325 19380654381 : gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
326 19380654381 : return src & ((HOST_WIDE_INT_1U << prec) - 1);
327 : }
328 : }
329 :
330 : /* Compute the absolute value of X. */
331 :
332 : inline HOST_WIDE_INT
333 32002225 : abs_hwi (HOST_WIDE_INT x)
334 : {
335 32002225 : gcc_checking_assert (x != HOST_WIDE_INT_MIN);
336 32002225 : return x >= 0 ? x : -x;
337 : }
338 :
339 : /* Compute the absolute value of X as an unsigned type. */
340 :
341 : inline unsigned HOST_WIDE_INT
342 7211028949 : absu_hwi (HOST_WIDE_INT x)
343 : {
344 6629119339 : return x >= 0 ? (unsigned HOST_WIDE_INT)x : -(unsigned HOST_WIDE_INT)x;
345 : }
346 :
347 : /* Compute the sum of signed A and B and indicate in *OVERFLOW whether
348 : that operation overflowed. */
349 :
350 : inline HOST_WIDE_INT
351 13822254 : add_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b, bool *overflow)
352 : {
353 : #if GCC_VERSION < 11000
354 : unsigned HOST_WIDE_INT result = a + (unsigned HOST_WIDE_INT)b;
355 : if ((((result ^ a) & (result ^ b))
356 : >> (HOST_BITS_PER_WIDE_INT - 1)) & 1)
357 : *overflow = true;
358 : else
359 : *overflow = false;
360 : #else
361 13822254 : HOST_WIDE_INT result;
362 13822254 : *overflow = __builtin_add_overflow (a, b, &result);
363 : #endif
364 13822254 : return result;
365 : }
366 :
367 : /* Compute the product of signed A and B and indicate in *OVERFLOW whether
368 : that operation overflowed. */
369 :
370 : inline HOST_WIDE_INT
371 5088602 : mul_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b, bool *overflow)
372 : {
373 : #if GCC_VERSION < 11000
374 : unsigned HOST_WIDE_INT result = a * (unsigned HOST_WIDE_INT)b;
375 : if ((a == -1 && b == HOST_WIDE_INT_MIN)
376 : || (a != 0 && (HOST_WIDE_INT)result / a != b))
377 : *overflow = true;
378 : else
379 : *overflow = false;
380 : #else
381 5088602 : HOST_WIDE_INT result;
382 5088602 : *overflow = __builtin_mul_overflow (a, b, &result);
383 : #endif
384 5088602 : return result;
385 : }
386 :
387 : /* Compute the saturated sum of signed A and B, i.e. upon overflow clamp
388 : the result to the corresponding extremum. */
389 :
390 : inline HOST_WIDE_INT
391 8752659 : add_sat_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b)
392 : {
393 8752659 : bool overflow;
394 8752659 : HOST_WIDE_INT result = add_hwi (a, b, &overflow);
395 6381761 : if (!overflow)
396 : return result;
397 0 : return (a < 0) ? HOST_WIDE_INT_MIN : HOST_WIDE_INT_MAX;
398 : }
399 :
400 : /* Compute the saturated product of signed A and B, i.e. upon overflow clamp
401 : the result to the corresponding extremum. */
402 :
403 : inline HOST_WIDE_INT
404 19007 : mul_sat_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b)
405 : {
406 19007 : bool overflow;
407 19007 : HOST_WIDE_INT result = mul_hwi (a, b, &overflow);
408 19007 : if (!overflow)
409 : return result;
410 0 : return ((a < 0) != (b < 0)) ? HOST_WIDE_INT_MIN : HOST_WIDE_INT_MAX;
411 : }
412 :
413 : #endif /* ! GCC_HWINT_H */
|