]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/hwint.h
config.gcc: Remove need_64bit_hwint.
[thirdparty/gcc.git] / gcc / hwint.h
CommitLineData
cce4a958 1/* HOST_WIDE_INT definitions for the GNU compiler.
23a5b65a 2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
cce4a958 3
1322177d 4 This file is part of GCC.
cce4a958
KG
5
6 Provide definitions for macros which depend on HOST_BITS_PER_INT
19eb1ad7 7 and HOST_BITS_PER_LONG. */
cce4a958 8
88657302
RH
9#ifndef GCC_HWINT_H
10#define GCC_HWINT_H
cce4a958 11
75e93faa
ZW
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
edb89024
DR
18/* The string that should be inserted into a printf style format to
19 indicate a "long" operand. */
b8698a0f 20#ifndef HOST_LONG_FORMAT
edb89024
DR
21#define HOST_LONG_FORMAT "l"
22#endif
23
be6601c3
MM
24/* The string that should be inserted into a printf style format to
25 indicate a "long long" operand. */
b8698a0f 26#ifndef HOST_LONG_LONG_FORMAT
be6601c3
MM
27#define HOST_LONG_LONG_FORMAT "ll"
28#endif
29
4977bab6
ZW
30/* If HAVE_LONG_LONG and SIZEOF_LONG_LONG aren't defined, but
31 GCC_VERSION >= 3000, assume this is the second or later stage of a
32 bootstrap, we do have long long, and it's 64 bits. (This is
33 required by C99; we do have some ports that violate that assumption
34 but they're all cross-compile-only.) Just in case, force a
35 constraint violation if that assumption is incorrect. */
36#if !defined HAVE_LONG_LONG
37# if GCC_VERSION >= 3000
38# define HAVE_LONG_LONG 1
39# define SIZEOF_LONG_LONG 8
c3284718 40extern char sizeof_long_long_must_be_8[sizeof (long long) == 8 ? 1 : -1];
4977bab6
ZW
41# endif
42#endif
43
75e93faa
ZW
44#ifdef HAVE_LONG_LONG
45# define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
4977bab6 46#endif
964b104a 47#ifdef HAVE___INT64
4977bab6 48# define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
6d08665a 49#endif
cce4a958 50
4977bab6
ZW
51/* Set HOST_WIDE_INT. This should be the widest efficient host
52 integer type. It can be 32 or 64 bits, except that if we are
53 targeting a machine with 64-bit size_t then it has to be 64 bits.
cce4a958 54
4977bab6
ZW
55 With a sane ABI, 'long' is the largest efficient host integer type.
56 Thus, we use that unless we have to use 'long long' or '__int64'
57 because we're targeting a 64-bit machine from a 32-bit host. */
4abe9f62 58
54da09ee 59#if HOST_BITS_PER_LONG >= 64
4977bab6
ZW
60# define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
61# define HOST_WIDE_INT long
fd2d9121 62# define HOST_WIDE_INT_C(X) X ## L
4977bab6
ZW
63#else
64# if HOST_BITS_PER_LONGLONG >= 64
4abe9f62
AO
65# define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONGLONG
66# define HOST_WIDE_INT long long
fd2d9121 67# define HOST_WIDE_INT_C(X) X ## LL
cce4a958 68# else
4977bab6
ZW
69# if HOST_BITS_PER___INT64 >= 64
70# define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
71# define HOST_WIDE_INT __int64
fd2d9121 72# define HOST_WIDE_INT_C(X) X ## i64
cce4a958 73# else
4977bab6 74 #error "Unable to find a suitable type for HOST_WIDE_INT"
cce4a958
KG
75# endif
76# endif
4977bab6 77#endif
cce4a958 78
5e0919f1
KZ
79/* Print support for half a host wide int. */
80#define HOST_BITS_PER_HALF_WIDE_INT (HOST_BITS_PER_WIDE_INT / 2)
81#if HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_LONG
82# define HOST_HALF_WIDE_INT long
83# define HOST_HALF_WIDE_INT_PRINT HOST_LONG_FORMAT
84# define HOST_HALF_WIDE_INT_PRINT_C "L"
85# define HOST_HALF_WIDE_INT_PRINT_DEC "%" HOST_HALF_WIDE_INT_PRINT "d"
86# define HOST_HALF_WIDE_INT_PRINT_DEC_C HOST_HALF_WIDE_INT_PRINT_DEC HOST_HALF_WIDE_INT_PRINT_C
87# define HOST_HALF_WIDE_INT_PRINT_UNSIGNED "%" HOST_HALF_WIDE_INT_PRINT "u"
88# define HOST_HALF_WIDE_INT_PRINT_HEX "%#" HOST_HALF_WIDE_INT_PRINT "x"
89# define HOST_HALF_WIDE_INT_PRINT_HEX_PURE "%" HOST_HALF_WIDE_INT_PRINT "x"
90#elif HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_INT
91# define HOST_HALF_WIDE_INT int
92# define HOST_HALF_WIDE_INT_PRINT ""
93# define HOST_HALF_WIDE_INT_PRINT_C ""
94# define HOST_HALF_WIDE_INT_PRINT_DEC "%" HOST_HALF_WIDE_INT_PRINT "d"
95# define HOST_HALF_WIDE_INT_PRINT_DEC_C HOST_HALF_WIDE_INT_PRINT_DEC HOST_HALF_WIDE_INT_PRINT_C
96# define HOST_HALF_WIDE_INT_PRINT_UNSIGNED "%" HOST_HALF_WIDE_INT_PRINT "u"
97# define HOST_HALF_WIDE_INT_PRINT_HEX "%#" HOST_HALF_WIDE_INT_PRINT "x"
98# define HOST_HALF_WIDE_INT_PRINT_HEX_PURE "%" HOST_HALF_WIDE_INT_PRINT "x"
99#elif HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_SHORT
100# define HOST_HALF_WIDE_INT short
101# define HOST_HALF_WIDE_INT_PRINT ""
102# define HOST_HALF_WIDE_INT_PRINT_C ""
103# define HOST_HALF_WIDE_INT_PRINT_DEC "%" HOST_HALF_WIDE_INT_PRINT "d"
104# define HOST_HALF_WIDE_INT_PRINT_DEC_C HOST_HALF_WIDE_INT_PRINT_DEC HOST_HALF_WIDE_INT_PRINT_C
105# define HOST_HALF_WIDE_INT_PRINT_UNSIGNED "%" HOST_HALF_WIDE_INT_PRINT "u"
106# define HOST_HALF_WIDE_INT_PRINT_HEX "%#" HOST_HALF_WIDE_INT_PRINT "x"
107# define HOST_HALF_WIDE_INT_PRINT_HEX_PURE "%" HOST_HALF_WIDE_INT_PRINT "x"
108#else
109#error Please add support for HOST_HALF_WIDE_INT
110#endif
111
112
c3284718
RS
113#define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
114#define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
115#define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
116#define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
117#define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
fd2d9121 118
be7a421e
SB
119/* This is a magic identifier which allows GCC to figure out the type
120 of HOST_WIDE_INT for %wd specifier checks. You must issue this
121 typedef before using the __asm_fprintf__ format attribute. */
122typedef HOST_WIDE_INT __gcc_host_wide_int__;
123
4977bab6
ZW
124/* Various printf format strings for HOST_WIDE_INT. */
125
126#if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
edb89024 127# define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
85f015e1 128# define HOST_WIDE_INT_PRINT_C "L"
4977bab6
ZW
129 /* 'long' might be 32 or 64 bits, and the number of leading zeroes
130 must be tweaked accordingly. */
131# if HOST_BITS_PER_WIDE_INT == 64
edb89024 132# define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
34a47f6f 133 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
5e0919f1
KZ
134# define HOST_WIDE_INT_PRINT_PADDED_HEX \
135 "%016" HOST_LONG_FORMAT "x"
cce4a958 136# else
edb89024 137# define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
34a47f6f 138 "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
5e0919f1
KZ
139# define HOST_WIDE_INT_PRINT_PADDED_HEX \
140 "%08" HOST_LONG_FORMAT "x"
cce4a958 141# endif
4977bab6 142#else
6194fd98 143# define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
85f015e1 144# define HOST_WIDE_INT_PRINT_C "LL"
4977bab6 145 /* We can assume that 'long long' is at least 64 bits. */
be6601c3 146# define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
34a47f6f 147 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
5e0919f1
KZ
148# define HOST_WIDE_INT_PRINT_PADDED_HEX \
149 "%016" HOST_LONG_LONG_FORMAT "x"
85f015e1
KG
150#endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
151
152#define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
153#define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
154#define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
35c59d9c 155#define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
dde8b360 156#define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
cce4a958 157
44f9a8e4
ZW
158/* Set HOST_WIDEST_INT. This is a 64-bit type unless the compiler
159 in use has no 64-bit type at all; in that case it's 32 bits. */
4977bab6 160
44f9a8e4
ZW
161#if HOST_BITS_PER_WIDE_INT >= 64 \
162 || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
4977bab6
ZW
163# define HOST_WIDEST_INT HOST_WIDE_INT
164# define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_WIDE_INT
e2d87023 165# define HOST_WIDEST_INT_PRINT HOST_WIDE_INT_PRINT
4977bab6
ZW
166# define HOST_WIDEST_INT_PRINT_DEC HOST_WIDE_INT_PRINT_DEC
167# define HOST_WIDEST_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC_C
4977bab6 168# define HOST_WIDEST_INT_PRINT_UNSIGNED HOST_WIDE_INT_PRINT_UNSIGNED
4977bab6
ZW
169# define HOST_WIDEST_INT_PRINT_HEX HOST_WIDE_INT_PRINT_HEX
170# define HOST_WIDEST_INT_PRINT_DOUBLE_HEX HOST_WIDE_INT_PRINT_DOUBLE_HEX
c3284718 171# define HOST_WIDEST_INT_C(X) HOST_WIDE_INT (X)
4977bab6
ZW
172#else
173# if HOST_BITS_PER_LONGLONG >= 64
174# define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONGLONG
175# define HOST_WIDEST_INT long long
fd2d9121 176# define HOST_WIDEST_INT_C(X) X ## LL
cce4a958 177# else
4977bab6
ZW
178# if HOST_BITS_PER___INT64 >= 64
179# define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER___INT64
180# define HOST_WIDEST_INT __int64
fd2d9121 181# define HOST_WIDEST_INT_C(X) X ## i64
cce4a958 182# else
44f9a8e4 183 #error "This line should be impossible to reach"
cce4a958
KG
184# endif
185# endif
e2d87023 186# define HOST_WIDEST_INT_PRINT HOST_LONG_LONG_FORMAT
be6601c3
MM
187# define HOST_WIDEST_INT_PRINT_DEC "%" HOST_LONG_LONG_FORMAT "d"
188# define HOST_WIDEST_INT_PRINT_DEC_C "%" HOST_LONG_LONG_FORMAT "dLL"
189# define HOST_WIDEST_INT_PRINT_UNSIGNED "%" HOST_LONG_LONG_FORMAT "u"
35c59d9c 190# define HOST_WIDEST_INT_PRINT_HEX "%#" HOST_LONG_LONG_FORMAT "x"
be6601c3 191# define HOST_WIDEST_INT_PRINT_DOUBLE_HEX \
34a47f6f 192 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
4977bab6 193#endif
75e93faa 194
99fa8911
AP
195/* Define HOST_WIDEST_FAST_INT to the widest integer type supported
196 efficiently in hardware. (That is, the widest integer type that fits
197 in a hardware register.) Normally this is "long" but on some hosts it
198 should be "long long" or "__int64". This is no convenient way to
9f5ed61a 199 autodetect this, so such systems must set a flag in config.host; see there
99fa8911
AP
200 for details. */
201
202#ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
203# ifdef HAVE_LONG_LONG
204# define HOST_WIDEST_FAST_INT long long
205# define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
206# elif defined (HAVE___INT64)
207# define HOST_WIDEST_FAST_INT __int64
208# define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
209# else
fa10beec 210# error "Your host said it wanted to use long long or __int64 but neither"
99fa8911
AP
211# error "exist"
212# endif
213#else
214# define HOST_WIDEST_FAST_INT long
215# define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
216#endif
217
c59ffc41
JM
218/* Inline functions operating on HOST_WIDE_INT. */
219#if GCC_VERSION < 3004
220
221extern int clz_hwi (unsigned HOST_WIDE_INT x);
222extern int ctz_hwi (unsigned HOST_WIDE_INT x);
223extern int ffs_hwi (unsigned HOST_WIDE_INT x);
224
440b6d59
TV
225/* Return the number of set bits in X. */
226extern int popcount_hwi (unsigned HOST_WIDE_INT x);
227
c59ffc41
JM
228/* Return log2, or -1 if not exact. */
229extern int exact_log2 (unsigned HOST_WIDE_INT);
230
231/* Return floor of log2, with -1 for zero. */
232extern int floor_log2 (unsigned HOST_WIDE_INT);
233
46d33ae9
SB
234/* Return the smallest n such that 2**n >= X. */
235extern int ceil_log2 (unsigned HOST_WIDE_INT);
236
c59ffc41
JM
237#else /* GCC_VERSION >= 3004 */
238
239/* For convenience, define 0 -> word_size. */
240static inline int
241clz_hwi (unsigned HOST_WIDE_INT x)
242{
243 if (x == 0)
244 return HOST_BITS_PER_WIDE_INT;
245# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
246 return __builtin_clzl (x);
247# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
248 return __builtin_clzll (x);
249# else
250 return __builtin_clz (x);
251# endif
252}
253
254static inline int
255ctz_hwi (unsigned HOST_WIDE_INT x)
256{
257 if (x == 0)
258 return HOST_BITS_PER_WIDE_INT;
259# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
260 return __builtin_ctzl (x);
261# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
262 return __builtin_ctzll (x);
263# else
264 return __builtin_ctz (x);
265# endif
266}
267
268static inline int
269ffs_hwi (unsigned HOST_WIDE_INT x)
270{
271# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
272 return __builtin_ffsl (x);
273# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
274 return __builtin_ffsll (x);
275# else
276 return __builtin_ffs (x);
277# endif
278}
279
440b6d59
TV
280static inline int
281popcount_hwi (unsigned HOST_WIDE_INT x)
282{
283# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
284 return __builtin_popcountl (x);
285# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
286 return __builtin_popcountll (x);
287# else
288 return __builtin_popcount (x);
289# endif
290}
291
c59ffc41
JM
292static inline int
293floor_log2 (unsigned HOST_WIDE_INT x)
294{
295 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
296}
297
46d33ae9
SB
298static inline int
299ceil_log2 (unsigned HOST_WIDE_INT x)
300{
301 return floor_log2 (x - 1) + 1;
302}
303
c59ffc41
JM
304static inline int
305exact_log2 (unsigned HOST_WIDE_INT x)
306{
307 return x == (x & -x) && x ? ctz_hwi (x) : -1;
308}
309
310#endif /* GCC_VERSION >= 3004 */
311
3c67fd9c
SP
312#define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
313 ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
314#define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
315
316extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
4c9cf7af 317extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
3c67fd9c
SP
318extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
319extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
320extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
321extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
b305e3da 322
5e0919f1
KZ
323/* Sign extend SRC starting from PREC. */
324
5e0919f1
KZ
325static inline HOST_WIDE_INT
326sext_hwi (HOST_WIDE_INT src, unsigned int prec)
327{
328 if (prec == HOST_BITS_PER_WIDE_INT)
329 return src;
330 else
331 {
67b5215c 332 gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
5e0919f1
KZ
333 int shift = HOST_BITS_PER_WIDE_INT - prec;
334 return (src << shift) >> shift;
335 }
336}
5e0919f1
KZ
337
338/* Zero extend SRC starting from PREC. */
5e0919f1
KZ
339static inline unsigned HOST_WIDE_INT
340zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
341{
342 if (prec == HOST_BITS_PER_WIDE_INT)
343 return src;
344 else
67b5215c
RS
345 {
346 gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
e13b3dfd 347 return src & (((unsigned HOST_WIDE_INT) 1 << prec) - 1);
67b5215c 348 }
5e0919f1 349}
5e0919f1 350
88657302 351#endif /* ! GCC_HWINT_H */