1 /* HOST_WIDE_INT definitions for the GNU compiler.
2 Copyright (C) 1998, 2002, 2004, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 Provide definitions for macros which depend on HOST_BITS_PER_INT
8 and HOST_BITS_PER_LONG. */
13 /* This describes the machine the compiler is hosted on. */
14 #define HOST_BITS_PER_CHAR CHAR_BIT
15 #define HOST_BITS_PER_SHORT (CHAR_BIT * SIZEOF_SHORT)
16 #define HOST_BITS_PER_INT (CHAR_BIT * SIZEOF_INT)
17 #define HOST_BITS_PER_LONG (CHAR_BIT * SIZEOF_LONG)
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"
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"
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];
46 # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
49 # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
52 /* Set HOST_WIDE_INT. This should be the widest efficient host
53 integer type. It can be 32 or 64 bits, except that if we are
54 targeting a machine with 64-bit size_t then it has to be 64 bits.
56 With a sane ABI, 'long' is the largest efficient host integer type.
57 Thus, we use that unless we have to use 'long long' or '__int64'
58 because we're targeting a 64-bit machine from a 32-bit host. */
60 #if HOST_BITS_PER_LONG >= 64 || !defined NEED_64BIT_HOST_WIDE_INT
61 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
62 # define HOST_WIDE_INT long
64 # if HOST_BITS_PER_LONGLONG >= 64
65 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONGLONG
66 # define HOST_WIDE_INT long long
68 # if HOST_BITS_PER___INT64 >= 64
69 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
70 # define HOST_WIDE_INT __int64
72 #error "Unable to find a suitable type for HOST_WIDE_INT"
77 /* This is a magic identifier which allows GCC to figure out the type
78 of HOST_WIDE_INT for %wd specifier checks. You must issue this
79 typedef before using the __asm_fprintf__ format attribute. */
80 typedef HOST_WIDE_INT __gcc_host_wide_int__
;
82 /* Various printf format strings for HOST_WIDE_INT. */
84 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
85 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
86 # define HOST_WIDE_INT_PRINT_C "L"
87 # define HOST_WIDE_INT_1 1L
88 /* 'long' might be 32 or 64 bits, and the number of leading zeroes
89 must be tweaked accordingly. */
90 # if HOST_BITS_PER_WIDE_INT == 64
91 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
92 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
94 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
95 "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
98 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
99 # define HOST_WIDE_INT_PRINT_C "LL"
100 # define HOST_WIDE_INT_1 1LL
101 /* We can assume that 'long long' is at least 64 bits. */
102 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
103 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
104 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
106 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
107 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
108 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
109 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
110 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
112 /* Set HOST_WIDEST_INT. This is a 64-bit type unless the compiler
113 in use has no 64-bit type at all; in that case it's 32 bits. */
115 #if HOST_BITS_PER_WIDE_INT >= 64 \
116 || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
117 # define HOST_WIDEST_INT HOST_WIDE_INT
118 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_WIDE_INT
119 # define HOST_WIDEST_INT_PRINT HOST_WIDE_INT_PRINT
120 # define HOST_WIDEST_INT_PRINT_DEC HOST_WIDE_INT_PRINT_DEC
121 # define HOST_WIDEST_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC_C
122 # define HOST_WIDEST_INT_PRINT_UNSIGNED HOST_WIDE_INT_PRINT_UNSIGNED
123 # define HOST_WIDEST_INT_PRINT_HEX HOST_WIDE_INT_PRINT_HEX
124 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX HOST_WIDE_INT_PRINT_DOUBLE_HEX
126 # if HOST_BITS_PER_LONGLONG >= 64
127 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONGLONG
128 # define HOST_WIDEST_INT long long
130 # if HOST_BITS_PER___INT64 >= 64
131 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER___INT64
132 # define HOST_WIDEST_INT __int64
134 #error "This line should be impossible to reach"
137 # define HOST_WIDEST_INT_PRINT HOST_LONG_LONG_FORMAT
138 # define HOST_WIDEST_INT_PRINT_DEC "%" HOST_LONG_LONG_FORMAT "d"
139 # define HOST_WIDEST_INT_PRINT_DEC_C "%" HOST_LONG_LONG_FORMAT "dLL"
140 # define HOST_WIDEST_INT_PRINT_UNSIGNED "%" HOST_LONG_LONG_FORMAT "u"
141 # define HOST_WIDEST_INT_PRINT_HEX "%#" HOST_LONG_LONG_FORMAT "x"
142 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX \
143 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
146 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
147 efficiently in hardware. (That is, the widest integer type that fits
148 in a hardware register.) Normally this is "long" but on some hosts it
149 should be "long long" or "__int64". This is no convenient way to
150 autodetect this, so such systems must set a flag in config.host; see there
153 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
154 # ifdef HAVE_LONG_LONG
155 # define HOST_WIDEST_FAST_INT long long
156 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
157 # elif defined (HAVE___INT64)
158 # define HOST_WIDEST_FAST_INT __int64
159 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
161 # error "Your host said it wanted to use long long or __int64 but neither"
165 # define HOST_WIDEST_FAST_INT long
166 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
169 /* Inline functions operating on HOST_WIDE_INT. */
170 #if GCC_VERSION < 3004
172 extern int clz_hwi (unsigned HOST_WIDE_INT x
);
173 extern int ctz_hwi (unsigned HOST_WIDE_INT x
);
174 extern int ffs_hwi (unsigned HOST_WIDE_INT x
);
176 /* Return log2, or -1 if not exact. */
177 extern int exact_log2 (unsigned HOST_WIDE_INT
);
179 /* Return floor of log2, with -1 for zero. */
180 extern int floor_log2 (unsigned HOST_WIDE_INT
);
182 #else /* GCC_VERSION >= 3004 */
184 /* For convenience, define 0 -> word_size. */
186 clz_hwi (unsigned HOST_WIDE_INT x
)
189 return HOST_BITS_PER_WIDE_INT
;
190 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
191 return __builtin_clzl (x
);
192 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
193 return __builtin_clzll (x
);
195 return __builtin_clz (x
);
200 ctz_hwi (unsigned HOST_WIDE_INT x
)
203 return HOST_BITS_PER_WIDE_INT
;
204 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
205 return __builtin_ctzl (x
);
206 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
207 return __builtin_ctzll (x
);
209 return __builtin_ctz (x
);
214 ffs_hwi (unsigned HOST_WIDE_INT x
)
216 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
217 return __builtin_ffsl (x
);
218 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
219 return __builtin_ffsll (x
);
221 return __builtin_ffs (x
);
226 floor_log2 (unsigned HOST_WIDE_INT x
)
228 return HOST_BITS_PER_WIDE_INT
- 1 - clz_hwi (x
);
232 exact_log2 (unsigned HOST_WIDE_INT x
)
234 return x
== (x
& -x
) && x
? ctz_hwi (x
) : -1;
237 #endif /* GCC_VERSION >= 3004 */
239 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
240 ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
241 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
243 extern HOST_WIDE_INT
abs_hwi (HOST_WIDE_INT
);
244 extern unsigned HOST_WIDE_INT
absu_hwi (HOST_WIDE_INT
);
245 extern HOST_WIDE_INT
gcd (HOST_WIDE_INT
, HOST_WIDE_INT
);
246 extern HOST_WIDE_INT
pos_mul_hwi (HOST_WIDE_INT
, HOST_WIDE_INT
);
247 extern HOST_WIDE_INT
mul_hwi (HOST_WIDE_INT
, HOST_WIDE_INT
);
248 extern HOST_WIDE_INT
least_common_multiple (HOST_WIDE_INT
, HOST_WIDE_INT
);
250 #endif /* ! GCC_HWINT_H */