Support C++11 thread_local destructors.
[official-gcc.git] / gcc / hwint.h
blobca471486c63dfc7c00facda3c7642f4202597365
1 /* HOST_WIDE_INT definitions for the GNU compiler.
2 Copyright (C) 1998, 2002, 2004, 2008, 2009, 2010, 2012
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. */
10 #ifndef GCC_HWINT_H
11 #define GCC_HWINT_H
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"
23 #endif
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"
29 #endif
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];
42 # endif
43 #endif
45 #ifdef HAVE_LONG_LONG
46 # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
47 #endif
48 #ifdef HAVE___INT64
49 # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
50 #endif
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
63 # define HOST_WIDE_INT_C(X) X ## L
64 #else
65 # if HOST_BITS_PER_LONGLONG >= 64
66 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONGLONG
67 # define HOST_WIDE_INT long long
68 # define HOST_WIDE_INT_C(X) X ## LL
69 # else
70 # if HOST_BITS_PER___INT64 >= 64
71 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
72 # define HOST_WIDE_INT __int64
73 # define HOST_WIDE_INT_C(X) X ## i64
74 # else
75 #error "Unable to find a suitable type for HOST_WIDE_INT"
76 # endif
77 # endif
78 #endif
80 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C(1)
82 /* This is a magic identifier which allows GCC to figure out the type
83 of HOST_WIDE_INT for %wd specifier checks. You must issue this
84 typedef before using the __asm_fprintf__ format attribute. */
85 typedef HOST_WIDE_INT __gcc_host_wide_int__;
87 /* Various printf format strings for HOST_WIDE_INT. */
89 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
90 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
91 # define HOST_WIDE_INT_PRINT_C "L"
92 /* 'long' might be 32 or 64 bits, and the number of leading zeroes
93 must be tweaked accordingly. */
94 # if HOST_BITS_PER_WIDE_INT == 64
95 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
96 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
97 # else
98 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
99 "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
100 # endif
101 #else
102 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
103 # define HOST_WIDE_INT_PRINT_C "LL"
104 /* We can assume that 'long long' is at least 64 bits. */
105 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
106 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
107 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
109 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
110 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
111 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
112 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
113 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
115 /* Set HOST_WIDEST_INT. This is a 64-bit type unless the compiler
116 in use has no 64-bit type at all; in that case it's 32 bits. */
118 #if HOST_BITS_PER_WIDE_INT >= 64 \
119 || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
120 # define HOST_WIDEST_INT HOST_WIDE_INT
121 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_WIDE_INT
122 # define HOST_WIDEST_INT_PRINT HOST_WIDE_INT_PRINT
123 # define HOST_WIDEST_INT_PRINT_DEC HOST_WIDE_INT_PRINT_DEC
124 # define HOST_WIDEST_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC_C
125 # define HOST_WIDEST_INT_PRINT_UNSIGNED HOST_WIDE_INT_PRINT_UNSIGNED
126 # define HOST_WIDEST_INT_PRINT_HEX HOST_WIDE_INT_PRINT_HEX
127 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX HOST_WIDE_INT_PRINT_DOUBLE_HEX
128 # define HOST_WIDEST_INT_C(X) HOST_WIDE_INT(X)
129 #else
130 # if HOST_BITS_PER_LONGLONG >= 64
131 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONGLONG
132 # define HOST_WIDEST_INT long long
133 # define HOST_WIDEST_INT_C(X) X ## LL
134 # else
135 # if HOST_BITS_PER___INT64 >= 64
136 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER___INT64
137 # define HOST_WIDEST_INT __int64
138 # define HOST_WIDEST_INT_C(X) X ## i64
139 # else
140 #error "This line should be impossible to reach"
141 # endif
142 # endif
143 # define HOST_WIDEST_INT_PRINT HOST_LONG_LONG_FORMAT
144 # define HOST_WIDEST_INT_PRINT_DEC "%" HOST_LONG_LONG_FORMAT "d"
145 # define HOST_WIDEST_INT_PRINT_DEC_C "%" HOST_LONG_LONG_FORMAT "dLL"
146 # define HOST_WIDEST_INT_PRINT_UNSIGNED "%" HOST_LONG_LONG_FORMAT "u"
147 # define HOST_WIDEST_INT_PRINT_HEX "%#" HOST_LONG_LONG_FORMAT "x"
148 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX \
149 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
150 #endif
152 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
153 efficiently in hardware. (That is, the widest integer type that fits
154 in a hardware register.) Normally this is "long" but on some hosts it
155 should be "long long" or "__int64". This is no convenient way to
156 autodetect this, so such systems must set a flag in config.host; see there
157 for details. */
159 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
160 # ifdef HAVE_LONG_LONG
161 # define HOST_WIDEST_FAST_INT long long
162 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
163 # elif defined (HAVE___INT64)
164 # define HOST_WIDEST_FAST_INT __int64
165 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
166 # else
167 # error "Your host said it wanted to use long long or __int64 but neither"
168 # error "exist"
169 # endif
170 #else
171 # define HOST_WIDEST_FAST_INT long
172 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
173 #endif
175 /* Inline functions operating on HOST_WIDE_INT. */
176 #if GCC_VERSION < 3004
178 extern int clz_hwi (unsigned HOST_WIDE_INT x);
179 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
180 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
182 /* Return the number of set bits in X. */
183 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
185 /* Return log2, or -1 if not exact. */
186 extern int exact_log2 (unsigned HOST_WIDE_INT);
188 /* Return floor of log2, with -1 for zero. */
189 extern int floor_log2 (unsigned HOST_WIDE_INT);
191 /* Return the smallest n such that 2**n >= X. */
192 extern int ceil_log2 (unsigned HOST_WIDE_INT);
194 #else /* GCC_VERSION >= 3004 */
196 /* For convenience, define 0 -> word_size. */
197 static inline int
198 clz_hwi (unsigned HOST_WIDE_INT x)
200 if (x == 0)
201 return HOST_BITS_PER_WIDE_INT;
202 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
203 return __builtin_clzl (x);
204 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
205 return __builtin_clzll (x);
206 # else
207 return __builtin_clz (x);
208 # endif
211 static inline int
212 ctz_hwi (unsigned HOST_WIDE_INT x)
214 if (x == 0)
215 return HOST_BITS_PER_WIDE_INT;
216 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
217 return __builtin_ctzl (x);
218 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
219 return __builtin_ctzll (x);
220 # else
221 return __builtin_ctz (x);
222 # endif
225 static inline int
226 ffs_hwi (unsigned HOST_WIDE_INT x)
228 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
229 return __builtin_ffsl (x);
230 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
231 return __builtin_ffsll (x);
232 # else
233 return __builtin_ffs (x);
234 # endif
237 static inline int
238 popcount_hwi (unsigned HOST_WIDE_INT x)
240 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
241 return __builtin_popcountl (x);
242 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
243 return __builtin_popcountll (x);
244 # else
245 return __builtin_popcount (x);
246 # endif
249 static inline int
250 floor_log2 (unsigned HOST_WIDE_INT x)
252 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
255 static inline int
256 ceil_log2 (unsigned HOST_WIDE_INT x)
258 return floor_log2 (x - 1) + 1;
261 static inline int
262 exact_log2 (unsigned HOST_WIDE_INT x)
264 return x == (x & -x) && x ? ctz_hwi (x) : -1;
267 #endif /* GCC_VERSION >= 3004 */
269 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
270 ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
271 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
273 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
274 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
275 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
276 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
277 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
278 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
280 #endif /* ! GCC_HWINT_H */