xref: /dflybsd-src/contrib/gcc-4.7/gcc/hwint.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* HOST_WIDE_INT definitions for the GNU compiler.
2*e4b17023SJohn Marino    Copyright (C) 1998, 2002, 2004, 2008, 2009, 2010
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino    This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino    Provide definitions for macros which depend on HOST_BITS_PER_INT
8*e4b17023SJohn Marino    and HOST_BITS_PER_LONG.  */
9*e4b17023SJohn Marino 
10*e4b17023SJohn Marino #ifndef GCC_HWINT_H
11*e4b17023SJohn Marino #define GCC_HWINT_H
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino /* This describes the machine the compiler is hosted on.  */
14*e4b17023SJohn Marino #define HOST_BITS_PER_CHAR  CHAR_BIT
15*e4b17023SJohn Marino #define HOST_BITS_PER_SHORT (CHAR_BIT * SIZEOF_SHORT)
16*e4b17023SJohn Marino #define HOST_BITS_PER_INT   (CHAR_BIT * SIZEOF_INT)
17*e4b17023SJohn Marino #define HOST_BITS_PER_LONG  (CHAR_BIT * SIZEOF_LONG)
18*e4b17023SJohn Marino 
19*e4b17023SJohn Marino /* The string that should be inserted into a printf style format to
20*e4b17023SJohn Marino    indicate a "long" operand.  */
21*e4b17023SJohn Marino #ifndef HOST_LONG_FORMAT
22*e4b17023SJohn Marino #define HOST_LONG_FORMAT "l"
23*e4b17023SJohn Marino #endif
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino /* The string that should be inserted into a printf style format to
26*e4b17023SJohn Marino    indicate a "long long" operand.  */
27*e4b17023SJohn Marino #ifndef HOST_LONG_LONG_FORMAT
28*e4b17023SJohn Marino #define HOST_LONG_LONG_FORMAT "ll"
29*e4b17023SJohn Marino #endif
30*e4b17023SJohn Marino 
31*e4b17023SJohn Marino /* If HAVE_LONG_LONG and SIZEOF_LONG_LONG aren't defined, but
32*e4b17023SJohn Marino    GCC_VERSION >= 3000, assume this is the second or later stage of a
33*e4b17023SJohn Marino    bootstrap, we do have long long, and it's 64 bits.  (This is
34*e4b17023SJohn Marino    required by C99; we do have some ports that violate that assumption
35*e4b17023SJohn Marino    but they're all cross-compile-only.)  Just in case, force a
36*e4b17023SJohn Marino    constraint violation if that assumption is incorrect.  */
37*e4b17023SJohn Marino #if !defined HAVE_LONG_LONG
38*e4b17023SJohn Marino # if GCC_VERSION >= 3000
39*e4b17023SJohn Marino #  define HAVE_LONG_LONG 1
40*e4b17023SJohn Marino #  define SIZEOF_LONG_LONG 8
41*e4b17023SJohn Marino extern char sizeof_long_long_must_be_8[sizeof(long long) == 8 ? 1 : -1];
42*e4b17023SJohn Marino # endif
43*e4b17023SJohn Marino #endif
44*e4b17023SJohn Marino 
45*e4b17023SJohn Marino #ifdef HAVE_LONG_LONG
46*e4b17023SJohn Marino # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
47*e4b17023SJohn Marino #endif
48*e4b17023SJohn Marino #ifdef HAVE___INT64
49*e4b17023SJohn Marino # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
50*e4b17023SJohn Marino #endif
51*e4b17023SJohn Marino 
52*e4b17023SJohn Marino /* Set HOST_WIDE_INT.  This should be the widest efficient host
53*e4b17023SJohn Marino    integer type.  It can be 32 or 64 bits, except that if we are
54*e4b17023SJohn Marino    targeting a machine with 64-bit size_t then it has to be 64 bits.
55*e4b17023SJohn Marino 
56*e4b17023SJohn Marino    With a sane ABI, 'long' is the largest efficient host integer type.
57*e4b17023SJohn Marino    Thus, we use that unless we have to use 'long long' or '__int64'
58*e4b17023SJohn Marino    because we're targeting a 64-bit machine from a 32-bit host.  */
59*e4b17023SJohn Marino 
60*e4b17023SJohn Marino #if HOST_BITS_PER_LONG >= 64 || !defined NEED_64BIT_HOST_WIDE_INT
61*e4b17023SJohn Marino #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
62*e4b17023SJohn Marino #   define HOST_WIDE_INT long
63*e4b17023SJohn Marino #else
64*e4b17023SJohn Marino # if HOST_BITS_PER_LONGLONG >= 64
65*e4b17023SJohn Marino #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONGLONG
66*e4b17023SJohn Marino #   define HOST_WIDE_INT long long
67*e4b17023SJohn Marino # else
68*e4b17023SJohn Marino #  if HOST_BITS_PER___INT64 >= 64
69*e4b17023SJohn Marino #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
70*e4b17023SJohn Marino #   define HOST_WIDE_INT __int64
71*e4b17023SJohn Marino #  else
72*e4b17023SJohn Marino     #error "Unable to find a suitable type for HOST_WIDE_INT"
73*e4b17023SJohn Marino #  endif
74*e4b17023SJohn Marino # endif
75*e4b17023SJohn Marino #endif
76*e4b17023SJohn Marino 
77*e4b17023SJohn Marino /* Various printf format strings for HOST_WIDE_INT.  */
78*e4b17023SJohn Marino 
79*e4b17023SJohn Marino #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
80*e4b17023SJohn Marino # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
81*e4b17023SJohn Marino # define HOST_WIDE_INT_PRINT_C "L"
82*e4b17023SJohn Marino # define HOST_WIDE_INT_1 1L
83*e4b17023SJohn Marino   /* 'long' might be 32 or 64 bits, and the number of leading zeroes
84*e4b17023SJohn Marino      must be tweaked accordingly.  */
85*e4b17023SJohn Marino # if HOST_BITS_PER_WIDE_INT == 64
86*e4b17023SJohn Marino #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
87*e4b17023SJohn Marino      "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
88*e4b17023SJohn Marino # else
89*e4b17023SJohn Marino #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
90*e4b17023SJohn Marino      "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
91*e4b17023SJohn Marino # endif
92*e4b17023SJohn Marino #else
93*e4b17023SJohn Marino # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
94*e4b17023SJohn Marino # define HOST_WIDE_INT_PRINT_C "LL"
95*e4b17023SJohn Marino # define HOST_WIDE_INT_1 1LL
96*e4b17023SJohn Marino   /* We can assume that 'long long' is at least 64 bits.  */
97*e4b17023SJohn Marino # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
98*e4b17023SJohn Marino     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
99*e4b17023SJohn Marino #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
100*e4b17023SJohn Marino 
101*e4b17023SJohn Marino #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
102*e4b17023SJohn Marino #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
103*e4b17023SJohn Marino #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
104*e4b17023SJohn Marino #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
105*e4b17023SJohn Marino #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
106*e4b17023SJohn Marino 
107*e4b17023SJohn Marino /* Set HOST_WIDEST_INT.  This is a 64-bit type unless the compiler
108*e4b17023SJohn Marino    in use has no 64-bit type at all; in that case it's 32 bits.  */
109*e4b17023SJohn Marino 
110*e4b17023SJohn Marino #if HOST_BITS_PER_WIDE_INT >= 64 \
111*e4b17023SJohn Marino     || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
112*e4b17023SJohn Marino # define HOST_WIDEST_INT		      HOST_WIDE_INT
113*e4b17023SJohn Marino # define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER_WIDE_INT
114*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT                HOST_WIDE_INT_PRINT
115*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_DEC	      HOST_WIDE_INT_PRINT_DEC
116*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_DEC_C	      HOST_WIDE_INT_PRINT_DEC_C
117*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_UNSIGNED	      HOST_WIDE_INT_PRINT_UNSIGNED
118*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_HEX	      HOST_WIDE_INT_PRINT_HEX
119*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     HOST_WIDE_INT_PRINT_DOUBLE_HEX
120*e4b17023SJohn Marino #else
121*e4b17023SJohn Marino # if HOST_BITS_PER_LONGLONG >= 64
122*e4b17023SJohn Marino #  define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER_LONGLONG
123*e4b17023SJohn Marino #  define HOST_WIDEST_INT		      long long
124*e4b17023SJohn Marino # else
125*e4b17023SJohn Marino #  if HOST_BITS_PER___INT64 >= 64
126*e4b17023SJohn Marino #   define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER___INT64
127*e4b17023SJohn Marino #   define HOST_WIDEST_INT		      __int64
128*e4b17023SJohn Marino #  else
129*e4b17023SJohn Marino     #error "This line should be impossible to reach"
130*e4b17023SJohn Marino #  endif
131*e4b17023SJohn Marino # endif
132*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT                HOST_LONG_LONG_FORMAT
133*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_DEC	      "%" HOST_LONG_LONG_FORMAT "d"
134*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_DEC_C	      "%" HOST_LONG_LONG_FORMAT "dLL"
135*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_UNSIGNED	      "%" HOST_LONG_LONG_FORMAT "u"
136*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_HEX	      "%#" HOST_LONG_LONG_FORMAT "x"
137*e4b17023SJohn Marino # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     \
138*e4b17023SJohn Marino     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
139*e4b17023SJohn Marino #endif
140*e4b17023SJohn Marino 
141*e4b17023SJohn Marino /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
142*e4b17023SJohn Marino    efficiently in hardware.  (That is, the widest integer type that fits
143*e4b17023SJohn Marino    in a hardware register.)  Normally this is "long" but on some hosts it
144*e4b17023SJohn Marino    should be "long long" or "__int64".  This is no convenient way to
145*e4b17023SJohn Marino    autodetect this, so such systems must set a flag in config.host; see there
146*e4b17023SJohn Marino    for details.  */
147*e4b17023SJohn Marino 
148*e4b17023SJohn Marino #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
149*e4b17023SJohn Marino #  ifdef HAVE_LONG_LONG
150*e4b17023SJohn Marino #    define HOST_WIDEST_FAST_INT long long
151*e4b17023SJohn Marino #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
152*e4b17023SJohn Marino #  elif defined (HAVE___INT64)
153*e4b17023SJohn Marino #    define HOST_WIDEST_FAST_INT __int64
154*e4b17023SJohn Marino #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
155*e4b17023SJohn Marino #  else
156*e4b17023SJohn Marino #    error "Your host said it wanted to use long long or __int64 but neither"
157*e4b17023SJohn Marino #    error "exist"
158*e4b17023SJohn Marino #  endif
159*e4b17023SJohn Marino #else
160*e4b17023SJohn Marino #  define HOST_WIDEST_FAST_INT long
161*e4b17023SJohn Marino #  define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
162*e4b17023SJohn Marino #endif
163*e4b17023SJohn Marino 
164*e4b17023SJohn Marino /* Inline functions operating on HOST_WIDE_INT.  */
165*e4b17023SJohn Marino #if GCC_VERSION < 3004
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino extern int clz_hwi (unsigned HOST_WIDE_INT x);
168*e4b17023SJohn Marino extern int ctz_hwi (unsigned HOST_WIDE_INT x);
169*e4b17023SJohn Marino extern int ffs_hwi (unsigned HOST_WIDE_INT x);
170*e4b17023SJohn Marino 
171*e4b17023SJohn Marino /* Return log2, or -1 if not exact.  */
172*e4b17023SJohn Marino extern int exact_log2                  (unsigned HOST_WIDE_INT);
173*e4b17023SJohn Marino 
174*e4b17023SJohn Marino /* Return floor of log2, with -1 for zero.  */
175*e4b17023SJohn Marino extern int floor_log2                  (unsigned HOST_WIDE_INT);
176*e4b17023SJohn Marino 
177*e4b17023SJohn Marino #else /* GCC_VERSION >= 3004 */
178*e4b17023SJohn Marino 
179*e4b17023SJohn Marino /* For convenience, define 0 -> word_size.  */
180*e4b17023SJohn Marino static inline int
clz_hwi(unsigned HOST_WIDE_INT x)181*e4b17023SJohn Marino clz_hwi (unsigned HOST_WIDE_INT x)
182*e4b17023SJohn Marino {
183*e4b17023SJohn Marino   if (x == 0)
184*e4b17023SJohn Marino     return HOST_BITS_PER_WIDE_INT;
185*e4b17023SJohn Marino # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
186*e4b17023SJohn Marino   return __builtin_clzl (x);
187*e4b17023SJohn Marino # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
188*e4b17023SJohn Marino   return __builtin_clzll (x);
189*e4b17023SJohn Marino # else
190*e4b17023SJohn Marino   return __builtin_clz (x);
191*e4b17023SJohn Marino # endif
192*e4b17023SJohn Marino }
193*e4b17023SJohn Marino 
194*e4b17023SJohn Marino static inline int
ctz_hwi(unsigned HOST_WIDE_INT x)195*e4b17023SJohn Marino ctz_hwi (unsigned HOST_WIDE_INT x)
196*e4b17023SJohn Marino {
197*e4b17023SJohn Marino   if (x == 0)
198*e4b17023SJohn Marino     return HOST_BITS_PER_WIDE_INT;
199*e4b17023SJohn Marino # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
200*e4b17023SJohn Marino   return __builtin_ctzl (x);
201*e4b17023SJohn Marino # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
202*e4b17023SJohn Marino   return __builtin_ctzll (x);
203*e4b17023SJohn Marino # else
204*e4b17023SJohn Marino   return __builtin_ctz (x);
205*e4b17023SJohn Marino # endif
206*e4b17023SJohn Marino }
207*e4b17023SJohn Marino 
208*e4b17023SJohn Marino static inline int
ffs_hwi(unsigned HOST_WIDE_INT x)209*e4b17023SJohn Marino ffs_hwi (unsigned HOST_WIDE_INT x)
210*e4b17023SJohn Marino {
211*e4b17023SJohn Marino # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
212*e4b17023SJohn Marino   return __builtin_ffsl (x);
213*e4b17023SJohn Marino # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
214*e4b17023SJohn Marino   return __builtin_ffsll (x);
215*e4b17023SJohn Marino # else
216*e4b17023SJohn Marino   return __builtin_ffs (x);
217*e4b17023SJohn Marino # endif
218*e4b17023SJohn Marino }
219*e4b17023SJohn Marino 
220*e4b17023SJohn Marino static inline int
floor_log2(unsigned HOST_WIDE_INT x)221*e4b17023SJohn Marino floor_log2 (unsigned HOST_WIDE_INT x)
222*e4b17023SJohn Marino {
223*e4b17023SJohn Marino   return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
224*e4b17023SJohn Marino }
225*e4b17023SJohn Marino 
226*e4b17023SJohn Marino static inline int
exact_log2(unsigned HOST_WIDE_INT x)227*e4b17023SJohn Marino exact_log2 (unsigned HOST_WIDE_INT x)
228*e4b17023SJohn Marino {
229*e4b17023SJohn Marino   return x == (x & -x) && x ? ctz_hwi (x) : -1;
230*e4b17023SJohn Marino }
231*e4b17023SJohn Marino 
232*e4b17023SJohn Marino #endif /* GCC_VERSION >= 3004 */
233*e4b17023SJohn Marino 
234*e4b17023SJohn Marino #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
235*e4b17023SJohn Marino   ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
236*e4b17023SJohn Marino #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
237*e4b17023SJohn Marino 
238*e4b17023SJohn Marino extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
239*e4b17023SJohn Marino extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
240*e4b17023SJohn Marino extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
241*e4b17023SJohn Marino extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
242*e4b17023SJohn Marino extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
243*e4b17023SJohn Marino extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
244*e4b17023SJohn Marino 
245*e4b17023SJohn Marino #endif /* ! GCC_HWINT_H */
246