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 #define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
68 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
69 #define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
70 #define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
71 #define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
73 /* This is a magic identifier which allows GCC to figure out the type
74 of HOST_WIDE_INT for %wd specifier checks. You must issue this
75 typedef before using the __asm_fprintf__ format attribute. */
76 typedef HOST_WIDE_INT __gcc_host_wide_int__
;
78 /* Various printf format strings for HOST_WIDE_INT. */
80 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
81 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
82 # define HOST_WIDE_INT_PRINT_C "L"
83 /* HOST_BITS_PER_WIDE_INT is 64 bits. */
84 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
85 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
86 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
87 "%016" HOST_LONG_FORMAT "x"
89 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
90 # define HOST_WIDE_INT_PRINT_C "LL"
91 /* HOST_BITS_PER_WIDE_INT is 64 bits. */
92 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
93 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
94 # define HOST_WIDE_INT_PRINT_PADDED_HEX \
95 "%016" HOST_LONG_LONG_FORMAT "x"
96 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
98 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
99 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
100 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
101 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
102 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
104 /* Provide C99 <inttypes.h> style format definitions for 64bits. */
105 #ifndef HAVE_INTTYPES_H
107 #define PRId64 HOST_WIDE_INT_PRINT "d"
109 #define PRIi64 HOST_WIDE_INT_PRINT "i"
111 #define PRIo64 HOST_WIDE_INT_PRINT "o"
113 #define PRIu64 HOST_WIDE_INT_PRINT "u"
115 #define PRIx64 HOST_WIDE_INT_PRINT "x"
117 #define PRIX64 HOST_WIDE_INT_PRINT "X"
120 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
121 efficiently in hardware. (That is, the widest integer type that fits
122 in a hardware register.) Normally this is "long" but on some hosts it
123 should be "long long" or "__int64". This is no convenient way to
124 autodetect this, so such systems must set a flag in config.host; see there
127 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
128 # ifdef HAVE_LONG_LONG
129 # define HOST_WIDEST_FAST_INT long long
130 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
132 # error "Your host said it wanted to use long long but that does not exist"
135 # define HOST_WIDEST_FAST_INT long
136 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
139 /* Inline functions operating on HOST_WIDE_INT. */
140 #if GCC_VERSION < 3004
142 extern int clz_hwi (unsigned HOST_WIDE_INT x
);
143 extern int ctz_hwi (unsigned HOST_WIDE_INT x
);
144 extern int ffs_hwi (unsigned HOST_WIDE_INT x
);
146 /* Return the number of set bits in X. */
147 extern int popcount_hwi (unsigned HOST_WIDE_INT x
);
149 /* Return log2, or -1 if not exact. */
150 extern int exact_log2 (unsigned HOST_WIDE_INT
);
152 /* Return floor of log2, with -1 for zero. */
153 extern int floor_log2 (unsigned HOST_WIDE_INT
);
155 /* Return the smallest n such that 2**n >= X. */
156 extern int ceil_log2 (unsigned HOST_WIDE_INT
);
158 #else /* GCC_VERSION >= 3004 */
160 /* For convenience, define 0 -> word_size. */
162 clz_hwi (unsigned HOST_WIDE_INT x
)
165 return HOST_BITS_PER_WIDE_INT
;
166 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
167 return __builtin_clzl (x
);
168 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
169 return __builtin_clzll (x
);
171 return __builtin_clz (x
);
176 ctz_hwi (unsigned HOST_WIDE_INT x
)
179 return HOST_BITS_PER_WIDE_INT
;
180 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
181 return __builtin_ctzl (x
);
182 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
183 return __builtin_ctzll (x
);
185 return __builtin_ctz (x
);
190 ffs_hwi (unsigned HOST_WIDE_INT x
)
192 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
193 return __builtin_ffsl (x
);
194 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
195 return __builtin_ffsll (x
);
197 return __builtin_ffs (x
);
202 popcount_hwi (unsigned HOST_WIDE_INT x
)
204 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
205 return __builtin_popcountl (x
);
206 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
207 return __builtin_popcountll (x
);
209 return __builtin_popcount (x
);
214 floor_log2 (unsigned HOST_WIDE_INT x
)
216 return HOST_BITS_PER_WIDE_INT
- 1 - clz_hwi (x
);
220 ceil_log2 (unsigned HOST_WIDE_INT x
)
222 return floor_log2 (x
- 1) + 1;
226 exact_log2 (unsigned HOST_WIDE_INT x
)
228 return x
== (x
& -x
) && x
? ctz_hwi (x
) : -1;
231 #endif /* GCC_VERSION >= 3004 */
233 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
234 ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
235 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
237 extern HOST_WIDE_INT
abs_hwi (HOST_WIDE_INT
);
238 extern unsigned HOST_WIDE_INT
absu_hwi (HOST_WIDE_INT
);
239 extern HOST_WIDE_INT
gcd (HOST_WIDE_INT
, HOST_WIDE_INT
);
240 extern HOST_WIDE_INT
pos_mul_hwi (HOST_WIDE_INT
, HOST_WIDE_INT
);
241 extern HOST_WIDE_INT
mul_hwi (HOST_WIDE_INT
, HOST_WIDE_INT
);
242 extern HOST_WIDE_INT
least_common_multiple (HOST_WIDE_INT
, HOST_WIDE_INT
);
244 /* Sign extend SRC starting from PREC. */
246 static inline HOST_WIDE_INT
247 sext_hwi (HOST_WIDE_INT src
, unsigned int prec
)
249 if (prec
== HOST_BITS_PER_WIDE_INT
)
253 gcc_checking_assert (prec
< HOST_BITS_PER_WIDE_INT
);
254 int shift
= HOST_BITS_PER_WIDE_INT
- prec
;
255 return (src
<< shift
) >> shift
;
259 /* Zero extend SRC starting from PREC. */
260 static inline unsigned HOST_WIDE_INT
261 zext_hwi (unsigned HOST_WIDE_INT src
, unsigned int prec
)
263 if (prec
== HOST_BITS_PER_WIDE_INT
)
267 gcc_checking_assert (prec
< HOST_BITS_PER_WIDE_INT
);
268 return src
& (((unsigned HOST_WIDE_INT
) 1 << prec
) - 1);
272 #endif /* ! GCC_HWINT_H */