GCC Middle and Back End API Reference
hwint.h
Go to the documentation of this file.
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. */
60typedef 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
141inline unsigned HOST_WIDE_INT
142least_bit_hwi (unsigned HOST_WIDE_INT x)
143{
144 return (x & -x);
145}
146
147/* True if X is zero or a power of two. */
148
149inline bool
150pow2_or_zerop (unsigned HOST_WIDE_INT x)
151{
152 return least_bit_hwi (x) == x;
153}
154
155/* True if X is a power of two. */
156
157inline bool
158pow2p_hwi (unsigned HOST_WIDE_INT x)
159{
160 return x && pow2_or_zerop (x);
161}
162
163#if GCC_VERSION < 3004
164
165extern int clz_hwi (unsigned HOST_WIDE_INT x);
166extern int ctz_hwi (unsigned HOST_WIDE_INT x);
167extern int ffs_hwi (unsigned HOST_WIDE_INT x);
168
169/* Return the number of set bits in X. */
170extern int popcount_hwi (unsigned HOST_WIDE_INT x);
171
172/* Return log2, or -1 if not exact. */
173extern int exact_log2 (unsigned HOST_WIDE_INT);
174
175/* Return floor of log2, with -1 for zero. */
176extern int floor_log2 (unsigned HOST_WIDE_INT);
177
178/* Return the smallest n such that 2**n >= X. */
179extern int ceil_log2 (unsigned HOST_WIDE_INT);
180
181#else /* GCC_VERSION >= 3004 */
182
183/* For convenience, define 0 -> word_size. */
184inline int
185clz_hwi (unsigned HOST_WIDE_INT x)
186{
187 if (x == 0)
189# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
190 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
198inline int
199ctz_hwi (unsigned HOST_WIDE_INT x)
200{
201 if (x == 0)
203# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
204 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
212inline int
213ffs_hwi (unsigned HOST_WIDE_INT x)
214{
215# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
216 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
224inline int
225popcount_hwi (unsigned HOST_WIDE_INT x)
226{
227# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
228 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
236inline int
237floor_log2 (unsigned HOST_WIDE_INT x)
238{
239 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
240}
241
242inline int
243ceil_log2 (unsigned HOST_WIDE_INT x)
244{
245 return x == 0 ? 0 : floor_log2 (x - 1) + 1;
246}
247
248inline int
249exact_log2 (unsigned HOST_WIDE_INT x)
250{
251 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
260extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
261extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
262extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
263extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
264extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
265extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
266extern 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. */
270extern unsigned HOST_WIDE_INT
271calculate_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. */
276extern unsigned HOST_WIDE_INT
277calculate_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
283inline int
284ctz_or_zero (unsigned HOST_WIDE_INT x)
285{
286 return ffs_hwi (x) - 1;
287}
288
289/* Sign extend SRC starting from PREC. */
290
291inline HOST_WIDE_INT
292sext_hwi (HOST_WIDE_INT src, unsigned int prec)
293{
294 if (prec == HOST_BITS_PER_WIDE_INT)
295 return src;
296 else
297 {
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 int shift = HOST_BITS_PER_WIDE_INT - prec;
307 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. */
318inline unsigned HOST_WIDE_INT
319zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
320{
321 if (prec == HOST_BITS_PER_WIDE_INT)
322 return src;
323 else
324 {
326 return src & ((HOST_WIDE_INT_1U << prec) - 1);
327 }
328}
329
330/* Compute the absolute value of X. */
331
332inline HOST_WIDE_INT
333abs_hwi (HOST_WIDE_INT x)
334{
336 return x >= 0 ? x : -x;
337}
338
339/* Compute the absolute value of X as an unsigned type. */
340
341inline unsigned HOST_WIDE_INT
342absu_hwi (HOST_WIDE_INT x)
343{
344 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
350inline HOST_WIDE_INT
351add_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 HOST_WIDE_INT result;
362 *overflow = __builtin_add_overflow (a, b, &result);
363#endif
364 return result;
365}
366
367/* Compute the product of signed A and B and indicate in *OVERFLOW whether
368 that operation overflowed. */
369
370inline HOST_WIDE_INT
371mul_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 HOST_WIDE_INT result;
382 *overflow = __builtin_mul_overflow (a, b, &result);
383#endif
384 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
390inline HOST_WIDE_INT
391add_sat_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b)
392{
393 bool overflow;
394 HOST_WIDE_INT result = add_hwi (a, b, &overflow);
395 if (!overflow)
396 return result;
397 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
403inline HOST_WIDE_INT
404mul_sat_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b)
405{
406 bool overflow;
407 HOST_WIDE_INT result = mul_hwi (a, b, &overflow);
408 if (!overflow)
409 return result;
410 return ((a < 0) != (b < 0)) ? HOST_WIDE_INT_MIN : HOST_WIDE_INT_MAX;
411}
412
413#endif /* ! GCC_HWINT_H */
unsigned HOST_WIDE_INT calculate_crc(unsigned HOST_WIDE_INT crc, unsigned HOST_WIDE_INT data, unsigned HOST_WIDE_INT polynomial, unsigned short crc_bits, unsigned short data_bits)
Definition hwint.cc:232
int ffs_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.cc:110
HOST_WIDE_INT gcd(HOST_WIDE_INT a, HOST_WIDE_INT b)
Definition hwint.cc:139
HOST_WIDE_INT mul_hwi(HOST_WIDE_INT x, HOST_WIDE_INT y)
Definition hwint.cc:172
HOST_WIDE_INT pos_mul_hwi(HOST_WIDE_INT x, HOST_WIDE_INT y)
Definition hwint.cc:160
unsigned HOST_WIDE_INT calculate_reversed_crc(unsigned HOST_WIDE_INT crc, unsigned HOST_WIDE_INT data, unsigned HOST_WIDE_INT polynomial, unsigned short crc_bits, unsigned short data_bits)
Definition hwint.cc:264
int exact_log2(unsigned HOST_WIDE_INT x)
Definition hwint.cc:82
int floor_log2(unsigned HOST_WIDE_INT x)
Definition hwint.cc:43
unsigned HOST_WIDE_INT reflect_hwi(unsigned HOST_WIDE_INT value, unsigned bitwidth)
Definition hwint.cc:202
int ceil_log2(unsigned HOST_WIDE_INT x)
Definition hwint.cc:73
int ctz_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.cc:93
HOST_WIDE_INT least_common_multiple(HOST_WIDE_INT a, HOST_WIDE_INT b)
Definition hwint.cc:194
int clz_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.cc:101
int popcount_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.cc:118
HOST_WIDE_INT abs_hwi(HOST_WIDE_INT)
Definition hwint.h:333
bool pow2p_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.h:158
HOST_WIDE_INT add_sat_hwi(HOST_WIDE_INT a, HOST_WIDE_INT b)
Definition hwint.h:391
#define HOST_WIDE_INT_1U
Definition hwint.h:53
int ctz_or_zero(unsigned HOST_WIDE_INT x)
Definition hwint.h:284
bool pow2_or_zerop(unsigned HOST_WIDE_INT x)
Definition hwint.h:150
unsigned HOST_WIDE_INT absu_hwi(HOST_WIDE_INT)
Definition hwint.h:342
HOST_WIDE_INT sext_hwi(HOST_WIDE_INT src, unsigned int prec)
Definition hwint.h:292
unsigned HOST_WIDE_INT least_bit_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.h:142
#define HOST_WIDE_INT_MIN
Definition hwint.h:256
#define HOST_WIDE_INT_1
Definition hwint.h:52
#define HOST_WIDE_INT_MAX
Definition hwint.h:258
HOST_WIDE_INT __gcc_host_wide_int__
Definition hwint.h:60
#define HOST_BITS_PER_WIDE_INT
Definition hwint.h:36
HOST_WIDE_INT mul_sat_hwi(HOST_WIDE_INT a, HOST_WIDE_INT b)
Definition hwint.h:404
unsigned HOST_WIDE_INT zext_hwi(unsigned HOST_WIDE_INT src, unsigned int prec)
Definition hwint.h:319
HOST_WIDE_INT add_hwi(HOST_WIDE_INT a, HOST_WIDE_INT b, bool *overflow)
Definition hwint.h:351
Ca const poly_int< N, Cb > & b
Definition poly-int.h:771
Ca & a
Definition poly-int.h:770
#define gcc_checking_assert(EXPR)
Definition system.h:779