]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/hwint.h
2014-05-23 Richard Biener <rguenther@suse.de>
[thirdparty/gcc.git] / gcc / hwint.h
1 /* HOST_WIDE_INT definitions for the GNU compiler.
2 Copyright (C) 1998-2014 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
48 /* Set HOST_WIDE_INT, this should be always 64 bits.
49
50 With a sane ABI, 'long' is the largest efficient host integer type.
51 Thus, we use that unless we have to use 'long long'
52 because we're on a 32-bit host. */
53
54 #define HOST_BITS_PER_WIDE_INT 64
55 #if HOST_BITS_PER_LONG == 64
56 # define HOST_WIDE_INT long
57 # define HOST_WIDE_INT_C(X) X ## L
58 #else
59 # if HOST_BITS_PER_LONGLONG == 64
60 # define HOST_WIDE_INT long long
61 # define HOST_WIDE_INT_C(X) X ## LL
62 # else
63 #error "Unable to find a suitable type for HOST_WIDE_INT"
64 # endif
65 #endif
66
67 /* Print support for half a host wide int. */
68 #define HOST_BITS_PER_HALF_WIDE_INT 32
69 #if HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_LONG
70 # define HOST_HALF_WIDE_INT long
71 # define HOST_HALF_WIDE_INT_PRINT HOST_LONG_FORMAT
72 # define HOST_HALF_WIDE_INT_PRINT_C "L"
73 # define HOST_HALF_WIDE_INT_PRINT_DEC "%" HOST_HALF_WIDE_INT_PRINT "d"
74 # define HOST_HALF_WIDE_INT_PRINT_DEC_C HOST_HALF_WIDE_INT_PRINT_DEC HOST_HALF_WIDE_INT_PRINT_C
75 # define HOST_HALF_WIDE_INT_PRINT_UNSIGNED "%" HOST_HALF_WIDE_INT_PRINT "u"
76 # define HOST_HALF_WIDE_INT_PRINT_HEX "%#" HOST_HALF_WIDE_INT_PRINT "x"
77 # define HOST_HALF_WIDE_INT_PRINT_HEX_PURE "%" HOST_HALF_WIDE_INT_PRINT "x"
78 #elif HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_INT
79 # define HOST_HALF_WIDE_INT int
80 # define HOST_HALF_WIDE_INT_PRINT ""
81 # define HOST_HALF_WIDE_INT_PRINT_C ""
82 # define HOST_HALF_WIDE_INT_PRINT_DEC "%" HOST_HALF_WIDE_INT_PRINT "d"
83 # define HOST_HALF_WIDE_INT_PRINT_DEC_C HOST_HALF_WIDE_INT_PRINT_DEC HOST_HALF_WIDE_INT_PRINT_C
84 # define HOST_HALF_WIDE_INT_PRINT_UNSIGNED "%" HOST_HALF_WIDE_INT_PRINT "u"
85 # define HOST_HALF_WIDE_INT_PRINT_HEX "%#" HOST_HALF_WIDE_INT_PRINT "x"
86 # define HOST_HALF_WIDE_INT_PRINT_HEX_PURE "%" HOST_HALF_WIDE_INT_PRINT "x"
87 #elif HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_SHORT
88 # define HOST_HALF_WIDE_INT short
89 # define HOST_HALF_WIDE_INT_PRINT ""
90 # define HOST_HALF_WIDE_INT_PRINT_C ""
91 # define HOST_HALF_WIDE_INT_PRINT_DEC "%" HOST_HALF_WIDE_INT_PRINT "d"
92 # define HOST_HALF_WIDE_INT_PRINT_DEC_C HOST_HALF_WIDE_INT_PRINT_DEC HOST_HALF_WIDE_INT_PRINT_C
93 # define HOST_HALF_WIDE_INT_PRINT_UNSIGNED "%" HOST_HALF_WIDE_INT_PRINT "u"
94 # define HOST_HALF_WIDE_INT_PRINT_HEX "%#" HOST_HALF_WIDE_INT_PRINT "x"
95 # define HOST_HALF_WIDE_INT_PRINT_HEX_PURE "%" HOST_HALF_WIDE_INT_PRINT "x"
96 #else
97 #error Please add support for HOST_HALF_WIDE_INT
98 #endif
99
100
101 #define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
102 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
103 #define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
104 #define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
105 #define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
106
107 /* This is a magic identifier which allows GCC to figure out the type
108 of HOST_WIDE_INT for %wd specifier checks. You must issue this
109 typedef before using the __asm_fprintf__ format attribute. */
110 typedef HOST_WIDE_INT __gcc_host_wide_int__;
111
112 /* Various printf format strings for HOST_WIDE_INT. */
113
114 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
115 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
116 # define HOST_WIDE_INT_PRINT_C "L"
117 /* HOST_BITS_PER_WIDE_INT is 64 bits. */
118 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
119 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
120 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
121 "%016" HOST_LONG_FORMAT "x"
122 #else
123 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
124 # define HOST_WIDE_INT_PRINT_C "LL"
125 /* HOST_BITS_PER_WIDE_INT is 64 bits. */
126 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
127 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
128 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
129 "%016" HOST_LONG_LONG_FORMAT "x"
130 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
131
132 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
133 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
134 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
135 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
136 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
137
138 /* Provide C99 <inttypes.h> style format definitions for 64bits. */
139 #ifndef HAVE_INTTYPES_H
140 #undef PRId64
141 #define PRId64 HOST_WIDE_INT_PRINT "d"
142 #undef PRIi64
143 #define PRIi64 HOST_WIDE_INT_PRINT "i"
144 #undef PRIo64
145 #define PRIo64 HOST_WIDE_INT_PRINT "o"
146 #undef PRIu64
147 #define PRIu64 HOST_WIDE_INT_PRINT "u"
148 #undef PRIx64
149 #define PRIx64 HOST_WIDE_INT_PRINT "x"
150 #undef PRIX64
151 #define PRIX64 HOST_WIDE_INT_PRINT "X"
152 #endif
153
154 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
155 efficiently in hardware. (That is, the widest integer type that fits
156 in a hardware register.) Normally this is "long" but on some hosts it
157 should be "long long" or "__int64". This is no convenient way to
158 autodetect this, so such systems must set a flag in config.host; see there
159 for details. */
160
161 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
162 # ifdef HAVE_LONG_LONG
163 # define HOST_WIDEST_FAST_INT long long
164 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
165 # else
166 # error "Your host said it wanted to use long long but that does not exist"
167 # endif
168 #else
169 # define HOST_WIDEST_FAST_INT long
170 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
171 #endif
172
173 /* Inline functions operating on HOST_WIDE_INT. */
174 #if GCC_VERSION < 3004
175
176 extern int clz_hwi (unsigned HOST_WIDE_INT x);
177 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
178 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
179
180 /* Return the number of set bits in X. */
181 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
182
183 /* Return log2, or -1 if not exact. */
184 extern int exact_log2 (unsigned HOST_WIDE_INT);
185
186 /* Return floor of log2, with -1 for zero. */
187 extern int floor_log2 (unsigned HOST_WIDE_INT);
188
189 /* Return the smallest n such that 2**n >= X. */
190 extern int ceil_log2 (unsigned HOST_WIDE_INT);
191
192 #else /* GCC_VERSION >= 3004 */
193
194 /* For convenience, define 0 -> word_size. */
195 static inline int
196 clz_hwi (unsigned HOST_WIDE_INT x)
197 {
198 if (x == 0)
199 return HOST_BITS_PER_WIDE_INT;
200 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
201 return __builtin_clzl (x);
202 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
203 return __builtin_clzll (x);
204 # else
205 return __builtin_clz (x);
206 # endif
207 }
208
209 static inline int
210 ctz_hwi (unsigned HOST_WIDE_INT x)
211 {
212 if (x == 0)
213 return HOST_BITS_PER_WIDE_INT;
214 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
215 return __builtin_ctzl (x);
216 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
217 return __builtin_ctzll (x);
218 # else
219 return __builtin_ctz (x);
220 # endif
221 }
222
223 static inline int
224 ffs_hwi (unsigned HOST_WIDE_INT x)
225 {
226 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
227 return __builtin_ffsl (x);
228 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
229 return __builtin_ffsll (x);
230 # else
231 return __builtin_ffs (x);
232 # endif
233 }
234
235 static inline int
236 popcount_hwi (unsigned HOST_WIDE_INT x)
237 {
238 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
239 return __builtin_popcountl (x);
240 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
241 return __builtin_popcountll (x);
242 # else
243 return __builtin_popcount (x);
244 # endif
245 }
246
247 static inline int
248 floor_log2 (unsigned HOST_WIDE_INT x)
249 {
250 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
251 }
252
253 static inline int
254 ceil_log2 (unsigned HOST_WIDE_INT x)
255 {
256 return floor_log2 (x - 1) + 1;
257 }
258
259 static inline int
260 exact_log2 (unsigned HOST_WIDE_INT x)
261 {
262 return x == (x & -x) && x ? ctz_hwi (x) : -1;
263 }
264
265 #endif /* GCC_VERSION >= 3004 */
266
267 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
268 ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
269 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
270
271 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
272 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
273 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
274 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
275 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
276 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
277
278 /* Sign extend SRC starting from PREC. */
279
280 static inline HOST_WIDE_INT
281 sext_hwi (HOST_WIDE_INT src, unsigned int prec)
282 {
283 if (prec == HOST_BITS_PER_WIDE_INT)
284 return src;
285 else
286 {
287 gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
288 int shift = HOST_BITS_PER_WIDE_INT - prec;
289 return (src << shift) >> shift;
290 }
291 }
292
293 /* Zero extend SRC starting from PREC. */
294 static inline unsigned HOST_WIDE_INT
295 zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
296 {
297 if (prec == HOST_BITS_PER_WIDE_INT)
298 return src;
299 else
300 {
301 gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
302 return src & (((unsigned HOST_WIDE_INT) 1 << prec) - 1);
303 }
304 }
305
306 #endif /* ! GCC_HWINT_H */