1 /* HOST_WIDE_INT definitions for the GNU compiler.
2    Copyright (C) 1998-2016 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 #define HOST_BITS_PER_PTR   (CHAR_BIT * SIZEOF_VOID_P)
18 
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
24 
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
30 
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
44 
45 #ifdef HAVE_LONG_LONG
46 # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
47 #endif
48 
49 /* Set HOST_WIDE_INT, this should be always 64 bits.
50    The underlying type is matched to that of int64_t and assumed
51    to be either long or long long.  */
52 
53 #define HOST_BITS_PER_WIDE_INT 64
54 #if INT64_T_IS_LONG
55 #   define HOST_WIDE_INT long
56 #   define HOST_WIDE_INT_C(X) X ## L
57 #else
58 # if HOST_BITS_PER_LONGLONG == 64
59 #   define HOST_WIDE_INT long long
60 #   define HOST_WIDE_INT_C(X) X ## LL
61 # else
62    #error "Unable to find a suitable type for HOST_WIDE_INT"
63 # endif
64 #endif
65 
66 #define HOST_WIDE_INT_UC(X) HOST_WIDE_INT_C (X ## U)
67 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C (1)
68 #define HOST_WIDE_INT_1U HOST_WIDE_INT_UC (1)
69 #define HOST_WIDE_INT_M1 HOST_WIDE_INT_C (-1)
70 #define HOST_WIDE_INT_M1U HOST_WIDE_INT_UC (-1)
71 
72 /* This is a magic identifier which allows GCC to figure out the type
73    of HOST_WIDE_INT for %wd specifier checks.  You must issue this
74    typedef before using the __asm_fprintf__ format attribute.  */
75 typedef HOST_WIDE_INT __gcc_host_wide_int__;
76 
77 /* Provide C99 <inttypes.h> style format definitions for 64bits.  */
78 #ifndef HAVE_INTTYPES_H
79 #if INT64_T_IS_LONG
80 # define GCC_PRI64 HOST_LONG_FORMAT
81 #else
82 # define GCC_PRI64 HOST_LONG_LONG_FORMAT
83 #endif
84 #undef PRId64
85 #define PRId64 GCC_PRI64 "d"
86 #undef PRIi64
87 #define PRIi64 GCC_PRI64 "i"
88 #undef PRIo64
89 #define PRIo64 GCC_PRI64 "o"
90 #undef PRIu64
91 #define PRIu64 GCC_PRI64 "u"
92 #undef PRIx64
93 #define PRIx64 GCC_PRI64 "x"
94 #undef PRIX64
95 #define PRIX64 GCC_PRI64 "X"
96 #endif
97 
98 /* Various printf format strings for HOST_WIDE_INT.  */
99 
100 #if INT64_T_IS_LONG
101 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
102 # define HOST_WIDE_INT_PRINT_C "L"
103 #else
104 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
105 # define HOST_WIDE_INT_PRINT_C "LL"
106 #endif
107 
108 #define HOST_WIDE_INT_PRINT_DEC "%" PRId64
109 #define HOST_WIDE_INT_PRINT_DEC_C "%" PRId64 HOST_WIDE_INT_PRINT_C
110 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" PRIu64
111 #define HOST_WIDE_INT_PRINT_HEX "%#" PRIx64
112 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" PRIx64
113 #define HOST_WIDE_INT_PRINT_DOUBLE_HEX "0x%" PRIx64 "%016" PRIx64
114 #define HOST_WIDE_INT_PRINT_PADDED_HEX "%016" PRIx64
115 
116 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
117    efficiently in hardware.  (That is, the widest integer type that fits
118    in a hardware register.)  Normally this is "long" but on some hosts it
119    should be "long long" or "__int64".  This is no convenient way to
120    autodetect this, so such systems must set a flag in config.host; see there
121    for details.  */
122 
123 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
124 #  ifdef HAVE_LONG_LONG
125 #    define HOST_WIDEST_FAST_INT long long
126 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
127 #  else
128 #    error "Your host said it wanted to use long long but that does not exist"
129 #  endif
130 #else
131 #  define HOST_WIDEST_FAST_INT long
132 #  define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
133 #endif
134 
135 /* Inline functions operating on HOST_WIDE_INT.  */
136 #if GCC_VERSION < 3004
137 
138 extern int clz_hwi (unsigned HOST_WIDE_INT x);
139 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
140 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
141 
142 /* Return the number of set bits in X.  */
143 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
144 
145 /* Return log2, or -1 if not exact.  */
146 extern int exact_log2                  (unsigned HOST_WIDE_INT);
147 
148 /* Return floor of log2, with -1 for zero.  */
149 extern int floor_log2                  (unsigned HOST_WIDE_INT);
150 
151 /* Return the smallest n such that 2**n >= X.  */
152 extern int ceil_log2			(unsigned HOST_WIDE_INT);
153 
154 #else /* GCC_VERSION >= 3004 */
155 
156 /* For convenience, define 0 -> word_size.  */
157 static inline int
clz_hwi(unsigned HOST_WIDE_INT x)158 clz_hwi (unsigned HOST_WIDE_INT x)
159 {
160   if (x == 0)
161     return HOST_BITS_PER_WIDE_INT;
162 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
163   return __builtin_clzl (x);
164 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
165   return __builtin_clzll (x);
166 # else
167   return __builtin_clz (x);
168 # endif
169 }
170 
171 static inline int
ctz_hwi(unsigned HOST_WIDE_INT x)172 ctz_hwi (unsigned HOST_WIDE_INT x)
173 {
174   if (x == 0)
175     return HOST_BITS_PER_WIDE_INT;
176 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
177   return __builtin_ctzl (x);
178 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
179   return __builtin_ctzll (x);
180 # else
181   return __builtin_ctz (x);
182 # endif
183 }
184 
185 static inline int
ffs_hwi(unsigned HOST_WIDE_INT x)186 ffs_hwi (unsigned HOST_WIDE_INT x)
187 {
188 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
189   return __builtin_ffsl (x);
190 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
191   return __builtin_ffsll (x);
192 # else
193   return __builtin_ffs (x);
194 # endif
195 }
196 
197 static inline int
popcount_hwi(unsigned HOST_WIDE_INT x)198 popcount_hwi (unsigned HOST_WIDE_INT x)
199 {
200 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
201   return __builtin_popcountl (x);
202 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
203   return __builtin_popcountll (x);
204 # else
205   return __builtin_popcount (x);
206 # endif
207 }
208 
209 static inline int
floor_log2(unsigned HOST_WIDE_INT x)210 floor_log2 (unsigned HOST_WIDE_INT x)
211 {
212   return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
213 }
214 
215 static inline int
ceil_log2(unsigned HOST_WIDE_INT x)216 ceil_log2 (unsigned HOST_WIDE_INT x)
217 {
218   return floor_log2 (x - 1) + 1;
219 }
220 
221 static inline int
exact_log2(unsigned HOST_WIDE_INT x)222 exact_log2 (unsigned HOST_WIDE_INT x)
223 {
224   return x == (x & -x) && x ? ctz_hwi (x) : -1;
225 }
226 
227 #endif /* GCC_VERSION >= 3004 */
228 
229 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
230   ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
231 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
232 
233 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
234 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
235 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
236 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
237 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
238 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
239 
240 /* Sign extend SRC starting from PREC.  */
241 
242 static inline HOST_WIDE_INT
sext_hwi(HOST_WIDE_INT src,unsigned int prec)243 sext_hwi (HOST_WIDE_INT src, unsigned int prec)
244 {
245   if (prec == HOST_BITS_PER_WIDE_INT)
246     return src;
247   else
248 #if defined (__GNUC__)
249     {
250       /* Take the faster path if the implementation-defined bits it's relying
251 	 on are implemented the way we expect them to be.  Namely, conversion
252 	 from unsigned to signed preserves bit pattern, and right shift of
253 	 a signed value propagates the sign bit.
254 	 We have to convert from signed to unsigned and back, because when left
255 	 shifting signed values, any overflow is undefined behavior.  */
256       gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
257       int shift = HOST_BITS_PER_WIDE_INT - prec;
258       return ((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) src << shift)) >> shift;
259     }
260 #else
261     {
262       /* Fall back to the slower, well defined path otherwise.  */
263       gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
264       HOST_WIDE_INT sign_mask = HOST_WIDE_INT_1 << (prec - 1);
265       HOST_WIDE_INT value_mask = (HOST_WIDE_INT_1U << prec) - HOST_WIDE_INT_1U;
266       return (((src & value_mask) ^ sign_mask) - sign_mask);
267     }
268 #endif
269 }
270 
271 /* Zero extend SRC starting from PREC.  */
272 static inline unsigned HOST_WIDE_INT
zext_hwi(unsigned HOST_WIDE_INT src,unsigned int prec)273 zext_hwi (unsigned HOST_WIDE_INT src, unsigned int prec)
274 {
275   if (prec == HOST_BITS_PER_WIDE_INT)
276     return src;
277   else
278     {
279       gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
280       return src & (((unsigned HOST_WIDE_INT) 1 << prec) - 1);
281     }
282 }
283 
284 /* Compute the absolute value of X.  */
285 
286 inline HOST_WIDE_INT
abs_hwi(HOST_WIDE_INT x)287 abs_hwi (HOST_WIDE_INT x)
288 {
289   gcc_checking_assert (x != HOST_WIDE_INT_MIN);
290   return x >= 0 ? x : -x;
291 }
292 
293 /* Compute the absolute value of X as an unsigned type.  */
294 
295 inline unsigned HOST_WIDE_INT
absu_hwi(HOST_WIDE_INT x)296 absu_hwi (HOST_WIDE_INT x)
297 {
298   return x >= 0 ? (unsigned HOST_WIDE_INT)x : -(unsigned HOST_WIDE_INT)x;
299 }
300 
301 #endif /* ! GCC_HWINT_H */
302