1 /* HOST_WIDE_INT definitions for the GNU compiler.
2 Copyright (C) 1998-2014 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. */
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"
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"
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];
45 # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
48 /* Set HOST_WIDE_INT, this should be always 64 bits.
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. */
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
59 # if HOST_BITS_PER_LONGLONG == 64
60 # define HOST_WIDE_INT long long
61 # define HOST_WIDE_INT_C(X) X ## LL
63 #error "Unable to find a suitable type for HOST_WIDE_INT"
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"
97 #error Please add support for HOST_HALF_WIDE_INT
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)
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__
;
112 /* Various printf format strings for HOST_WIDE_INT. */
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"
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 */
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"
138 /* Provide C99 <inttypes.h> style format definitions for 64bits. */
139 #ifndef HAVE_INTTYPES_H
141 #define PRId64 HOST_WIDE_INT_PRINT "d"
143 #define PRIi64 HOST_WIDE_INT_PRINT "i"
145 #define PRIo64 HOST_WIDE_INT_PRINT "o"
147 #define PRIu64 HOST_WIDE_INT_PRINT "u"
149 #define PRIx64 HOST_WIDE_INT_PRINT "x"
151 #define PRIX64 HOST_WIDE_INT_PRINT "X"
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
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
166 # error "Your host said it wanted to use long long but that does not exist"
169 # define HOST_WIDEST_FAST_INT long
170 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
173 /* Inline functions operating on HOST_WIDE_INT. */
174 #if GCC_VERSION < 3004
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
);
180 /* Return the number of set bits in X. */
181 extern int popcount_hwi (unsigned HOST_WIDE_INT x
);
183 /* Return log2, or -1 if not exact. */
184 extern int exact_log2 (unsigned HOST_WIDE_INT
);
186 /* Return floor of log2, with -1 for zero. */
187 extern int floor_log2 (unsigned HOST_WIDE_INT
);
189 /* Return the smallest n such that 2**n >= X. */
190 extern int ceil_log2 (unsigned HOST_WIDE_INT
);
192 #else /* GCC_VERSION >= 3004 */
194 /* For convenience, define 0 -> word_size. */
196 clz_hwi (unsigned HOST_WIDE_INT x
)
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
);
205 return __builtin_clz (x
);
210 ctz_hwi (unsigned HOST_WIDE_INT x
)
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
);
219 return __builtin_ctz (x
);
224 ffs_hwi (unsigned HOST_WIDE_INT x
)
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
);
231 return __builtin_ffs (x
);
236 popcount_hwi (unsigned HOST_WIDE_INT x
)
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
);
243 return __builtin_popcount (x
);
248 floor_log2 (unsigned HOST_WIDE_INT x
)
250 return HOST_BITS_PER_WIDE_INT
- 1 - clz_hwi (x
);
254 ceil_log2 (unsigned HOST_WIDE_INT x
)
256 return floor_log2 (x
- 1) + 1;
260 exact_log2 (unsigned HOST_WIDE_INT x
)
262 return x
== (x
& -x
) && x
? ctz_hwi (x
) : -1;
265 #endif /* GCC_VERSION >= 3004 */
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))
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
);
278 /* Sign extend SRC starting from PREC. */
280 static inline HOST_WIDE_INT
281 sext_hwi (HOST_WIDE_INT src
, unsigned int prec
)
283 if (prec
== HOST_BITS_PER_WIDE_INT
)
287 gcc_checking_assert (prec
< HOST_BITS_PER_WIDE_INT
);
288 int shift
= HOST_BITS_PER_WIDE_INT
- prec
;
289 return (src
<< shift
) >> shift
;
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
)
297 if (prec
== HOST_BITS_PER_WIDE_INT
)
301 gcc_checking_assert (prec
< HOST_BITS_PER_WIDE_INT
);
302 return src
& (((unsigned HOST_WIDE_INT
) 1 << prec
) - 1);
306 #endif /* ! GCC_HWINT_H */