]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/hwint.h
2014-05-28 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 #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)
72
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__;
77
78 /* Various printf format strings for HOST_WIDE_INT. */
79
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"
88 #else
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 */
97
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"
103
104 /* Provide C99 <inttypes.h> style format definitions for 64bits. */
105 #ifndef HAVE_INTTYPES_H
106 #undef PRId64
107 #define PRId64 HOST_WIDE_INT_PRINT "d"
108 #undef PRIi64
109 #define PRIi64 HOST_WIDE_INT_PRINT "i"
110 #undef PRIo64
111 #define PRIo64 HOST_WIDE_INT_PRINT "o"
112 #undef PRIu64
113 #define PRIu64 HOST_WIDE_INT_PRINT "u"
114 #undef PRIx64
115 #define PRIx64 HOST_WIDE_INT_PRINT "x"
116 #undef PRIX64
117 #define PRIX64 HOST_WIDE_INT_PRINT "X"
118 #endif
119
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
125 for details. */
126
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
131 # else
132 # error "Your host said it wanted to use long long but that does not exist"
133 # endif
134 #else
135 # define HOST_WIDEST_FAST_INT long
136 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
137 #endif
138
139 /* Inline functions operating on HOST_WIDE_INT. */
140 #if GCC_VERSION < 3004
141
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);
145
146 /* Return the number of set bits in X. */
147 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
148
149 /* Return log2, or -1 if not exact. */
150 extern int exact_log2 (unsigned HOST_WIDE_INT);
151
152 /* Return floor of log2, with -1 for zero. */
153 extern int floor_log2 (unsigned HOST_WIDE_INT);
154
155 /* Return the smallest n such that 2**n >= X. */
156 extern int ceil_log2 (unsigned HOST_WIDE_INT);
157
158 #else /* GCC_VERSION >= 3004 */
159
160 /* For convenience, define 0 -> word_size. */
161 static inline int
162 clz_hwi (unsigned HOST_WIDE_INT x)
163 {
164 if (x == 0)
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);
170 # else
171 return __builtin_clz (x);
172 # endif
173 }
174
175 static inline int
176 ctz_hwi (unsigned HOST_WIDE_INT x)
177 {
178 if (x == 0)
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);
184 # else
185 return __builtin_ctz (x);
186 # endif
187 }
188
189 static inline int
190 ffs_hwi (unsigned HOST_WIDE_INT x)
191 {
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);
196 # else
197 return __builtin_ffs (x);
198 # endif
199 }
200
201 static inline int
202 popcount_hwi (unsigned HOST_WIDE_INT x)
203 {
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);
208 # else
209 return __builtin_popcount (x);
210 # endif
211 }
212
213 static inline int
214 floor_log2 (unsigned HOST_WIDE_INT x)
215 {
216 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
217 }
218
219 static inline int
220 ceil_log2 (unsigned HOST_WIDE_INT x)
221 {
222 return floor_log2 (x - 1) + 1;
223 }
224
225 static inline int
226 exact_log2 (unsigned HOST_WIDE_INT x)
227 {
228 return x == (x & -x) && x ? ctz_hwi (x) : -1;
229 }
230
231 #endif /* GCC_VERSION >= 3004 */
232
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))
236
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);
243
244 /* Sign extend SRC starting from PREC. */
245
246 static inline HOST_WIDE_INT
247 sext_hwi (HOST_WIDE_INT src, unsigned int prec)
248 {
249 if (prec == HOST_BITS_PER_WIDE_INT)
250 return src;
251 else
252 {
253 gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
254 int shift = HOST_BITS_PER_WIDE_INT - prec;
255 return (src << shift) >> shift;
256 }
257 }
258
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)
262 {
263 if (prec == HOST_BITS_PER_WIDE_INT)
264 return src;
265 else
266 {
267 gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
268 return src & (((unsigned HOST_WIDE_INT) 1 << prec) - 1);
269 }
270 }
271
272 #endif /* ! GCC_HWINT_H */