xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/hwint.h (revision fdd524d4ccd2bb0c6f67401e938dabf773eb0372)
1 /* HOST_WIDE_INT definitions for the GNU compiler.
2    Copyright (C) 1998-2013 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 #ifdef HAVE___INT64
48 # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
49 #endif
50 
51 /* Set HOST_WIDE_INT.  This should be the widest efficient host
52    integer type.  It can be 32 or 64 bits, except that if we are
53    targeting a machine with 64-bit size_t then it has to be 64 bits.
54 
55    With a sane ABI, 'long' is the largest efficient host integer type.
56    Thus, we use that unless we have to use 'long long' or '__int64'
57    because we're targeting a 64-bit machine from a 32-bit host.  */
58 
59 #if HOST_BITS_PER_LONG >= 64 || !defined NEED_64BIT_HOST_WIDE_INT
60 #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
61 #   define HOST_WIDE_INT long
62 #   define HOST_WIDE_INT_C(X) X ## L
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 #   define HOST_WIDE_INT_C(X) X ## LL
68 # else
69 #  if HOST_BITS_PER___INT64 >= 64
70 #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
71 #   define HOST_WIDE_INT __int64
72 #   define HOST_WIDE_INT_C(X) X ## i64
73 #  else
74     #error "Unable to find a suitable type for HOST_WIDE_INT"
75 #  endif
76 # endif
77 #endif
78 
79 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C(1)
80 
81 /* This is a magic identifier which allows GCC to figure out the type
82    of HOST_WIDE_INT for %wd specifier checks.  You must issue this
83    typedef before using the __asm_fprintf__ format attribute.  */
84 typedef HOST_WIDE_INT __gcc_host_wide_int__;
85 
86 /* Various printf format strings for HOST_WIDE_INT.  */
87 
88 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
89 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
90 # define HOST_WIDE_INT_PRINT_C "L"
91 # define HOST_WIDE_INT_CONSTANT(x) x ## L
92   /* 'long' might be 32 or 64 bits, and the number of leading zeroes
93      must be tweaked accordingly.  */
94 # if HOST_BITS_PER_WIDE_INT == 64
95 #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
96      "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
97 # else
98 #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
99      "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
100 # endif
101 #else
102 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
103 # define HOST_WIDE_INT_PRINT_C "LL"
104 # define HOST_WIDE_INT_CONSTANT(x) x ## LL
105   /* We can assume that 'long long' is at least 64 bits.  */
106 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
107     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
108 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
109 
110 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
111 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
112 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
113 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
114 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
115 
116 /* Set HOST_WIDEST_INT.  This is a 64-bit type unless the compiler
117    in use has no 64-bit type at all; in that case it's 32 bits.  */
118 
119 #if HOST_BITS_PER_WIDE_INT >= 64 \
120     || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
121 # define HOST_WIDEST_INT		      HOST_WIDE_INT
122 # define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER_WIDE_INT
123 # define HOST_WIDEST_INT_PRINT                HOST_WIDE_INT_PRINT
124 # define HOST_WIDEST_INT_PRINT_DEC	      HOST_WIDE_INT_PRINT_DEC
125 # define HOST_WIDEST_INT_PRINT_DEC_C	      HOST_WIDE_INT_PRINT_DEC_C
126 # define HOST_WIDEST_INT_PRINT_UNSIGNED	      HOST_WIDE_INT_PRINT_UNSIGNED
127 # define HOST_WIDEST_INT_PRINT_HEX	      HOST_WIDE_INT_PRINT_HEX
128 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     HOST_WIDE_INT_PRINT_DOUBLE_HEX
129 # define HOST_WIDEST_INT_C(X)		      HOST_WIDE_INT(X)
130 #else
131 # if HOST_BITS_PER_LONGLONG >= 64
132 #  define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER_LONGLONG
133 #  define HOST_WIDEST_INT		      long long
134 #  define HOST_WIDEST_INT_C(X)		      X ## LL
135 # else
136 #  if HOST_BITS_PER___INT64 >= 64
137 #   define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER___INT64
138 #   define HOST_WIDEST_INT		      __int64
139 #   define HOST_WIDEST_INT_C(X)		      X ## i64
140 #  else
141     #error "This line should be impossible to reach"
142 #  endif
143 # endif
144 # define HOST_WIDEST_INT_PRINT                HOST_LONG_LONG_FORMAT
145 # define HOST_WIDEST_INT_PRINT_DEC	      "%" HOST_LONG_LONG_FORMAT "d"
146 # define HOST_WIDEST_INT_PRINT_DEC_C	      "%" HOST_LONG_LONG_FORMAT "dLL"
147 # define HOST_WIDEST_INT_PRINT_UNSIGNED	      "%" HOST_LONG_LONG_FORMAT "u"
148 # define HOST_WIDEST_INT_PRINT_HEX	      "%#" HOST_LONG_LONG_FORMAT "x"
149 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     \
150     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
151 #endif
152 
153 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
154    efficiently in hardware.  (That is, the widest integer type that fits
155    in a hardware register.)  Normally this is "long" but on some hosts it
156    should be "long long" or "__int64".  This is no convenient way to
157    autodetect this, so such systems must set a flag in config.host; see there
158    for details.  */
159 
160 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
161 #  ifdef HAVE_LONG_LONG
162 #    define HOST_WIDEST_FAST_INT long long
163 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
164 #  elif defined (HAVE___INT64)
165 #    define HOST_WIDEST_FAST_INT __int64
166 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
167 #  else
168 #    error "Your host said it wanted to use long long or __int64 but neither"
169 #    error "exist"
170 #  endif
171 #else
172 #  define HOST_WIDEST_FAST_INT long
173 #  define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
174 #endif
175 
176 /* Inline functions operating on HOST_WIDE_INT.  */
177 #if GCC_VERSION < 3004
178 
179 extern int clz_hwi (unsigned HOST_WIDE_INT x);
180 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
181 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
182 
183 /* Return the number of set bits in X.  */
184 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
185 
186 /* Return log2, or -1 if not exact.  */
187 extern int exact_log2                  (unsigned HOST_WIDE_INT);
188 
189 /* Return floor of log2, with -1 for zero.  */
190 extern int floor_log2                  (unsigned HOST_WIDE_INT);
191 
192 /* Return the smallest n such that 2**n >= X.  */
193 extern int ceil_log2			(unsigned HOST_WIDE_INT);
194 
195 #else /* GCC_VERSION >= 3004 */
196 
197 /* For convenience, define 0 -> word_size.  */
198 static inline int
199 clz_hwi (unsigned HOST_WIDE_INT x)
200 {
201   if (x == 0)
202     return HOST_BITS_PER_WIDE_INT;
203 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
204   return __builtin_clzl (x);
205 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
206   return __builtin_clzll (x);
207 # else
208   return __builtin_clz (x);
209 # endif
210 }
211 
212 static inline int
213 ctz_hwi (unsigned HOST_WIDE_INT x)
214 {
215   if (x == 0)
216     return HOST_BITS_PER_WIDE_INT;
217 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
218   return __builtin_ctzl (x);
219 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
220   return __builtin_ctzll (x);
221 # else
222   return __builtin_ctz (x);
223 # endif
224 }
225 
226 static inline int
227 ffs_hwi (unsigned HOST_WIDE_INT x)
228 {
229 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
230   return __builtin_ffsl (x);
231 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
232   return __builtin_ffsll (x);
233 # else
234   return __builtin_ffs (x);
235 # endif
236 }
237 
238 static inline int
239 popcount_hwi (unsigned HOST_WIDE_INT x)
240 {
241 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
242   return __builtin_popcountl (x);
243 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
244   return __builtin_popcountll (x);
245 # else
246   return __builtin_popcount (x);
247 # endif
248 }
249 
250 static inline int
251 floor_log2 (unsigned HOST_WIDE_INT x)
252 {
253   return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
254 }
255 
256 static inline int
257 ceil_log2 (unsigned HOST_WIDE_INT x)
258 {
259   return floor_log2 (x - 1) + 1;
260 }
261 
262 static inline int
263 exact_log2 (unsigned HOST_WIDE_INT x)
264 {
265   return x == (x & -x) && x ? ctz_hwi (x) : -1;
266 }
267 
268 #endif /* GCC_VERSION >= 3004 */
269 
270 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
271   ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
272 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
273 
274 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
275 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
276 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
277 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
278 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
279 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
280 
281 #endif /* ! GCC_HWINT_H */
282