fix pr48988
[official-gcc.git] / gcc / hwint.h
blob1eadd45da73ace7e9ef1745cb4efe664dff23b11
1 /* HOST_WIDE_INT definitions for the GNU compiler.
2 Copyright (C) 1998, 2002, 2004, 2008, 2009, 2010
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 #else
64 # if HOST_BITS_PER_LONGLONG >= 64
65 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONGLONG
66 # define HOST_WIDE_INT long long
67 # else
68 # if HOST_BITS_PER___INT64 >= 64
69 # define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
70 # define HOST_WIDE_INT __int64
71 # else
72 #error "Unable to find a suitable type for HOST_WIDE_INT"
73 # endif
74 # endif
75 #endif
77 /* Various printf format strings for HOST_WIDE_INT. */
79 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
80 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
81 # define HOST_WIDE_INT_PRINT_C "L"
82 /* 'long' might be 32 or 64 bits, and the number of leading zeroes
83 must be tweaked accordingly. */
84 # if HOST_BITS_PER_WIDE_INT == 64
85 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
86 "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
87 # else
88 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
89 "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
90 # endif
91 #else
92 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
93 # define HOST_WIDE_INT_PRINT_C "LL"
94 /* We can assume that 'long long' is at least 64 bits. */
95 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
96 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
97 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
99 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
100 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
101 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
102 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
104 /* Set HOST_WIDEST_INT. This is a 64-bit type unless the compiler
105 in use has no 64-bit type at all; in that case it's 32 bits. */
107 #if HOST_BITS_PER_WIDE_INT >= 64 \
108 || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
109 # define HOST_WIDEST_INT HOST_WIDE_INT
110 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_WIDE_INT
111 # define HOST_WIDEST_INT_PRINT HOST_WIDE_INT_PRINT
112 # define HOST_WIDEST_INT_PRINT_DEC HOST_WIDE_INT_PRINT_DEC
113 # define HOST_WIDEST_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC_C
114 # define HOST_WIDEST_INT_PRINT_UNSIGNED HOST_WIDE_INT_PRINT_UNSIGNED
115 # define HOST_WIDEST_INT_PRINT_HEX HOST_WIDE_INT_PRINT_HEX
116 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX HOST_WIDE_INT_PRINT_DOUBLE_HEX
117 #else
118 # if HOST_BITS_PER_LONGLONG >= 64
119 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONGLONG
120 # define HOST_WIDEST_INT long long
121 # else
122 # if HOST_BITS_PER___INT64 >= 64
123 # define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER___INT64
124 # define HOST_WIDEST_INT __int64
125 # else
126 #error "This line should be impossible to reach"
127 # endif
128 # endif
129 # define HOST_WIDEST_INT_PRINT HOST_LONG_LONG_FORMAT
130 # define HOST_WIDEST_INT_PRINT_DEC "%" HOST_LONG_LONG_FORMAT "d"
131 # define HOST_WIDEST_INT_PRINT_DEC_C "%" HOST_LONG_LONG_FORMAT "dLL"
132 # define HOST_WIDEST_INT_PRINT_UNSIGNED "%" HOST_LONG_LONG_FORMAT "u"
133 # define HOST_WIDEST_INT_PRINT_HEX "%#" HOST_LONG_LONG_FORMAT "x"
134 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX \
135 "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
136 #endif
138 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
139 efficiently in hardware. (That is, the widest integer type that fits
140 in a hardware register.) Normally this is "long" but on some hosts it
141 should be "long long" or "__int64". This is no convenient way to
142 autodetect this, so such systems must set a flag in config.host; see there
143 for details. */
145 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
146 # ifdef HAVE_LONG_LONG
147 # define HOST_WIDEST_FAST_INT long long
148 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
149 # elif defined (HAVE___INT64)
150 # define HOST_WIDEST_FAST_INT __int64
151 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
152 # else
153 # error "Your host said it wanted to use long long or __int64 but neither"
154 # error "exist"
155 # endif
156 #else
157 # define HOST_WIDEST_FAST_INT long
158 # define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
159 #endif
161 /* Inline functions operating on HOST_WIDE_INT. */
162 #if GCC_VERSION < 3004
164 extern int clz_hwi (unsigned HOST_WIDE_INT x);
165 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
166 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
168 /* Return log2, or -1 if not exact. */
169 extern int exact_log2 (unsigned HOST_WIDE_INT);
171 /* Return floor of log2, with -1 for zero. */
172 extern int floor_log2 (unsigned HOST_WIDE_INT);
174 #else /* GCC_VERSION >= 3004 */
176 /* For convenience, define 0 -> word_size. */
177 static inline int
178 clz_hwi (unsigned HOST_WIDE_INT x)
180 if (x == 0)
181 return HOST_BITS_PER_WIDE_INT;
182 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
183 return __builtin_clzl (x);
184 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
185 return __builtin_clzll (x);
186 # else
187 return __builtin_clz (x);
188 # endif
191 static inline int
192 ctz_hwi (unsigned HOST_WIDE_INT x)
194 if (x == 0)
195 return HOST_BITS_PER_WIDE_INT;
196 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
197 return __builtin_ctzl (x);
198 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
199 return __builtin_ctzll (x);
200 # else
201 return __builtin_ctz (x);
202 # endif
205 static inline int
206 ffs_hwi (unsigned HOST_WIDE_INT x)
208 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
209 return __builtin_ffsl (x);
210 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
211 return __builtin_ffsll (x);
212 # else
213 return __builtin_ffs (x);
214 # endif
217 static inline int
218 floor_log2 (unsigned HOST_WIDE_INT x)
220 return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
223 static inline int
224 exact_log2 (unsigned HOST_WIDE_INT x)
226 return x == (x & -x) && x ? ctz_hwi (x) : -1;
229 #endif /* GCC_VERSION >= 3004 */
231 /* Compute the greatest common divisor of two numbers using
232 Euclid's algorithm. */
234 static inline int
235 gcd (int a, int b)
237 int x, y, z;
239 x = abs (a);
240 y = abs (b);
242 while (x > 0)
244 z = y % x;
245 y = x;
246 x = z;
249 return y;
252 /* Compute the least common multiple of two numbers A and B . */
254 static inline int
255 least_common_multiple (int a, int b)
257 return (abs (a) * abs (b) / gcd (a, b));
260 #endif /* ! GCC_HWINT_H */