gcc/ChangeLog
[official-gcc.git] / gcc / hwint.h
blobdbf023975cb3a1e1b2c6a63745948cdb863a058b
1 /* HOST_WIDE_INT definitions for the GNU compiler.
2 Copyright (C) 1998-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 Provide definitions for macros which depend on HOST_BITS_PER_INT
7 and HOST_BITS_PER_LONG. */
9 #ifndef GCC_HWINT_H
10 #define GCC_HWINT_H
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)
18 /* The string that should be inserted into a printf style format to
19 indicate a "long" operand. */
20 #ifndef HOST_LONG_FORMAT
21 #define HOST_LONG_FORMAT "l"
22 #endif
24 /* The string that should be inserted into a printf style format to
25 indicate a "long long" operand. */
26 #ifndef HOST_LONG_LONG_FORMAT
27 #define HOST_LONG_LONG_FORMAT "ll"
28 #endif
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
40 extern char sizeof_long_long_must_be_8[sizeof(long long) == 8 ? 1 : -1];
41 # endif
42 #endif
44 #ifdef HAVE_LONG_LONG
45 # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
46 #endif
47 #ifdef HAVE___INT64
48 # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
49 #endif
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.
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. */
59 #if HOST_BITS_PER_LONG >= 64 || !defined NEED_64BIT_HOST_WIDE_INT
60 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
61 # define HOST_WIDE_INT long
62 # define HOST_WIDE_INT_C(X) X ## L
63 #else
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
67 # define HOST_WIDE_INT_C(X) X ## LL
68 # else
69 # if HOST_BITS_PER___INT64 >= 64
70 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
71 # define HOST_WIDE_INT __int64
72 # define HOST_WIDE_INT_C(X) X ## i64
73 # else
74 #error "Unable to find a suitable type for HOST_WIDE_INT"
75 # endif
76 # endif
77 #endif
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
113 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C(1)
115 /* This is a magic identifier which allows GCC to figure out the type
116 of HOST_WIDE_INT for %wd specifier checks. You must issue this
117 typedef before using the __asm_fprintf__ format attribute. */
118 typedef HOST_WIDE_INT __gcc_host_wide_int__;
120 /* Various printf format strings for HOST_WIDE_INT. */
122 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
123 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
124 # define HOST_WIDE_INT_PRINT_C "L"
125 /* 'long' might be 32 or 64 bits, and the number of leading zeroes
126 must be tweaked accordingly. */
127 # if HOST_BITS_PER_WIDE_INT == 64
128 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
129 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
130 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
131 "%016" HOST_LONG_FORMAT "x"
132 # else
133 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
134 "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
135 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
136 "%08" HOST_LONG_FORMAT "x"
137 # endif
138 #else
139 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
140 # define HOST_WIDE_INT_PRINT_C "LL"
141 /* We can assume that 'long long' is at least 64 bits. */
142 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
143 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
144 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
145 "%016" HOST_LONG_LONG_FORMAT "x"
146 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
148 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
149 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
150 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
151 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
152 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
154 /* Set HOST_WIDEST_INT. This is a 64-bit type unless the compiler
155 in use has no 64-bit type at all; in that case it's 32 bits. */
157 #if HOST_BITS_PER_WIDE_INT >= 64 \
158 || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
159 # define HOST_WIDEST_INT HOST_WIDE_INT
160 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_WIDE_INT
161 # define HOST_WIDEST_INT_PRINT HOST_WIDE_INT_PRINT
162 # define HOST_WIDEST_INT_PRINT_DEC HOST_WIDE_INT_PRINT_DEC
163 # define HOST_WIDEST_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC_C
164 # define HOST_WIDEST_INT_PRINT_UNSIGNED HOST_WIDE_INT_PRINT_UNSIGNED
165 # define HOST_WIDEST_INT_PRINT_HEX HOST_WIDE_INT_PRINT_HEX
166 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX HOST_WIDE_INT_PRINT_DOUBLE_HEX
167 # define HOST_WIDEST_INT_C(X) HOST_WIDE_INT(X)
168 #else
169 # if HOST_BITS_PER_LONGLONG >= 64
170 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONGLONG
171 # define HOST_WIDEST_INT long long
172 # define HOST_WIDEST_INT_C(X) X ## LL
173 # else
174 # if HOST_BITS_PER___INT64 >= 64
175 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER___INT64
176 # define HOST_WIDEST_INT __int64
177 # define HOST_WIDEST_INT_C(X) X ## i64
178 # else
179 #error "This line should be impossible to reach"
180 # endif
181 # endif
182 # define HOST_WIDEST_INT_PRINT HOST_LONG_LONG_FORMAT
183 # define HOST_WIDEST_INT_PRINT_DEC "%" HOST_LONG_LONG_FORMAT "d"
184 # define HOST_WIDEST_INT_PRINT_DEC_C "%" HOST_LONG_LONG_FORMAT "dLL"
185 # define HOST_WIDEST_INT_PRINT_UNSIGNED "%" HOST_LONG_LONG_FORMAT "u"
186 # define HOST_WIDEST_INT_PRINT_HEX "%#" HOST_LONG_LONG_FORMAT "x"
187 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX \
188 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
189 #endif
191 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
192 efficiently in hardware. (That is, the widest integer type that fits
193 in a hardware register.) Normally this is "long" but on some hosts it
194 should be "long long" or "__int64". This is no convenient way to
195 autodetect this, so such systems must set a flag in config.host; see there
196 for details. */
198 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
199 # ifdef HAVE_LONG_LONG
200 # define HOST_WIDEST_FAST_INT long long
201 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
202 # elif defined (HAVE___INT64)
203 # define HOST_WIDEST_FAST_INT __int64
204 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
205 # else
206 # error "Your host said it wanted to use long long or __int64 but neither"
207 # error "exist"
208 # endif
209 #else
210 # define HOST_WIDEST_FAST_INT long
211 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
212 #endif
214 /* Inline functions operating on HOST_WIDE_INT. */
215 #if GCC_VERSION < 3004
217 extern int clz_hwi (unsigned HOST_WIDE_INT x);
218 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
219 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
221 /* Return the number of set bits in X. */
222 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
224 /* Return log2, or -1 if not exact. */
225 extern int exact_log2 (unsigned HOST_WIDE_INT);
227 /* Return floor of log2, with -1 for zero. */
228 extern int floor_log2 (unsigned HOST_WIDE_INT);
230 /* Return the smallest n such that 2**n >= X. */
231 extern int ceil_log2 (unsigned HOST_WIDE_INT);
233 #else /* GCC_VERSION >= 3004 */
235 /* For convenience, define 0 -> word_size. */
236 static inline int
237 clz_hwi (unsigned HOST_WIDE_INT x)
239 if (x == 0)
240 return HOST_BITS_PER_WIDE_INT;
241 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
242 return __builtin_clzl (x);
243 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
244 return __builtin_clzll (x);
245 # else
246 return __builtin_clz (x);
247 # endif
250 static inline int
251 ctz_hwi (unsigned HOST_WIDE_INT x)
253 if (x == 0)
254 return HOST_BITS_PER_WIDE_INT;
255 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
256 return __builtin_ctzl (x);
257 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
258 return __builtin_ctzll (x);
259 # else
260 return __builtin_ctz (x);
261 # endif
264 static inline int
265 ffs_hwi (unsigned HOST_WIDE_INT x)
267 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
268 return __builtin_ffsl (x);
269 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
270 return __builtin_ffsll (x);
271 # else
272 return __builtin_ffs (x);
273 # endif
276 static inline int
277 popcount_hwi (unsigned HOST_WIDE_INT x)
279 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
280 return __builtin_popcountl (x);
281 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
282 return __builtin_popcountll (x);
283 # else
284 return __builtin_popcount (x);
285 # endif
288 static inline int
289 floor_log2 (unsigned HOST_WIDE_INT x)
291 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
294 static inline int
295 ceil_log2 (unsigned HOST_WIDE_INT x)
297 return floor_log2 (x - 1) + 1;
300 static inline int
301 exact_log2 (unsigned HOST_WIDE_INT x)
303 return x == (x & -x) && x ? ctz_hwi (x) : -1;
306 #endif /* GCC_VERSION >= 3004 */
308 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
309 ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
310 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
312 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
313 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
314 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
315 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
316 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
317 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
319 /* Sign extend SRC starting from PREC. */
321 #ifdef ENABLE_CHECKING
322 extern HOST_WIDE_INT sext_hwi (HOST_WIDE_INT, unsigned int);
323 #else
324 static inline HOST_WIDE_INT
325 sext_hwi (HOST_WIDE_INT src, unsigned int prec)
327 if (prec == HOST_BITS_PER_WIDE_INT)
328 return src;
329 else
331 int shift = HOST_BITS_PER_WIDE_INT - prec;
332 return (src << shift) >> shift;
335 #endif
337 /* Zero extend SRC starting from PREC. */
338 #ifdef ENABLE_CHECKING
339 extern unsigned HOST_WIDE_INT zext_hwi (unsigned HOST_WIDE_INT, unsigned int);
340 #else
341 static inline unsigned HOST_WIDE_INT
342 zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
344 if (prec == HOST_BITS_PER_WIDE_INT)
345 return src;
346 else
347 return src & (((HOST_WIDE_INT)1 << prec) - 1);
349 #endif
351 #endif /* ! GCC_HWINT_H */