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