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-2013 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 
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
23 
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
29 
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
43 
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
50 
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.
54 
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. */
58 
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
78 
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 
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)
118 
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. */
124 /* Various printf format strings for HOST_WIDE_INT. */
125 
126 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
127 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
128 # define HOST_WIDE_INT_PRINT_C "L"
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
132 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
133  "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
134 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
135  "%016" HOST_LONG_FORMAT "x"
136 # else
137 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
138  "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
139 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
140  "%08" HOST_LONG_FORMAT "x"
141 # endif
142 #else
143 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
144 # define HOST_WIDE_INT_PRINT_C "LL"
145  /* We can assume that 'long long' is at least 64 bits. */
146 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
147  "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
148 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
149  "%016" HOST_LONG_LONG_FORMAT "x"
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"
155 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
156 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
157 
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. */
160 
161 #if HOST_BITS_PER_WIDE_INT >= 64 \
162  || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
163 # define HOST_WIDEST_INT HOST_WIDE_INT
164 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_WIDE_INT
165 # define HOST_WIDEST_INT_PRINT HOST_WIDE_INT_PRINT
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
168 # define HOST_WIDEST_INT_PRINT_UNSIGNED HOST_WIDE_INT_PRINT_UNSIGNED
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
171 # define HOST_WIDEST_INT_C(X) HOST_WIDE_INT (X)
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
176 # define HOST_WIDEST_INT_C(X) X ## LL
177 # else
178 # if HOST_BITS_PER___INT64 >= 64
179 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER___INT64
180 # define HOST_WIDEST_INT __int64
181 # define HOST_WIDEST_INT_C(X) X ## i64
182 # else
183  #error "This line should be impossible to reach"
184 # endif
185 # endif
186 # define HOST_WIDEST_INT_PRINT HOST_LONG_LONG_FORMAT
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"
190 # define HOST_WIDEST_INT_PRINT_HEX "%#" HOST_LONG_LONG_FORMAT "x"
191 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX \
192  "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
193 #endif
194 
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
199  autodetect this, so such systems must set a flag in config.host; see there
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
210 # error "Your host said it wanted to use long long or __int64 but neither"
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 
218 /* Inline functions operating on HOST_WIDE_INT. */
219 #if GCC_VERSION < 3004
220 
221 extern int clz_hwi (unsigned HOST_WIDE_INT x);
222 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
223 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
224 
225 /* Return the number of set bits in X. */
226 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
228 /* Return log2, or -1 if not exact. */
229 extern int exact_log2 (unsigned HOST_WIDE_INT);
230 
231 /* Return floor of log2, with -1 for zero. */
232 extern int floor_log2 (unsigned HOST_WIDE_INT);
233 
234 /* Return the smallest n such that 2**n >= X. */
235 extern int ceil_log2 (unsigned HOST_WIDE_INT);
236 
237 #else /* GCC_VERSION >= 3004 */
238 
239 /* For convenience, define 0 -> word_size. */
240 static inline int
241 clz_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 
254 static inline int
255 ctz_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 
268 static inline int
269 ffs_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 
280 static inline int
281 popcount_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 
292 static inline int
293 floor_log2 (unsigned HOST_WIDE_INT x)
294 {
295  return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
296 }
297 
298 static inline int
299 ceil_log2 (unsigned HOST_WIDE_INT x)
300 {
301  return floor_log2 (x - 1) + 1;
302 }
303 
304 static inline int
305 exact_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 
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 
317 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
322 
323 /* Sign extend SRC starting from PREC. */
324 
325 static inline HOST_WIDE_INT
326 sext_hwi (HOST_WIDE_INT src, unsigned int prec)
327 {
328  if (prec == HOST_BITS_PER_WIDE_INT)
329  return src;
330  else
331  {
332  gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
334  return (src << shift) >> shift;
335  }
336 }
337 
338 /* Zero extend SRC starting from PREC. */
339 static inline unsigned HOST_WIDE_INT
340 zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
341 {
342  if (prec == HOST_BITS_PER_WIDE_INT)
343  return src;
344  else
345  {
346  gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
347  return src & (((HOST_WIDE_INT) 1 << prec) - 1);
348  }
349 }
350 
351 #endif /* ! GCC_HWINT_H */