OSDN Git Service

Backported from mainline
[pf3gnuchains/gcc-fork.git] / gcc / hwint.h
1 /* HOST_WIDE_INT definitions for the GNU compiler.
2    Copyright (C) 1998, 2002, 2004, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
7    Provide definitions for macros which depend on HOST_BITS_PER_INT
8    and HOST_BITS_PER_LONG.  */
9
10 #ifndef GCC_HWINT_H
11 #define GCC_HWINT_H
12
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)
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 #ifdef HAVE___INT64
49 # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
50 #endif
51
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.
55
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.  */
59
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
76
77 /* Various printf format strings for HOST_WIDE_INT.  */
78
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 # define HOST_WIDE_INT_1 1L
83   /* 'long' might be 32 or 64 bits, and the number of leading zeroes
84      must be tweaked accordingly.  */
85 # if HOST_BITS_PER_WIDE_INT == 64
86 #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
87      "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
88 # else
89 #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
90      "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
91 # endif
92 #else
93 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
94 # define HOST_WIDE_INT_PRINT_C "LL"
95 # define HOST_WIDE_INT_1 1LL
96   /* We can assume that 'long long' is at least 64 bits.  */
97 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
98     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
99 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
100
101 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
102 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
103 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
104 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
105 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
106
107 /* Set HOST_WIDEST_INT.  This is a 64-bit type unless the compiler
108    in use has no 64-bit type at all; in that case it's 32 bits.  */
109
110 #if HOST_BITS_PER_WIDE_INT >= 64 \
111     || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
112 # define HOST_WIDEST_INT                      HOST_WIDE_INT
113 # define HOST_BITS_PER_WIDEST_INT             HOST_BITS_PER_WIDE_INT
114 # define HOST_WIDEST_INT_PRINT                HOST_WIDE_INT_PRINT
115 # define HOST_WIDEST_INT_PRINT_DEC            HOST_WIDE_INT_PRINT_DEC
116 # define HOST_WIDEST_INT_PRINT_DEC_C          HOST_WIDE_INT_PRINT_DEC_C
117 # define HOST_WIDEST_INT_PRINT_UNSIGNED       HOST_WIDE_INT_PRINT_UNSIGNED
118 # define HOST_WIDEST_INT_PRINT_HEX            HOST_WIDE_INT_PRINT_HEX
119 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     HOST_WIDE_INT_PRINT_DOUBLE_HEX
120 #else
121 # if HOST_BITS_PER_LONGLONG >= 64
122 #  define HOST_BITS_PER_WIDEST_INT            HOST_BITS_PER_LONGLONG
123 #  define HOST_WIDEST_INT                     long long
124 # else
125 #  if HOST_BITS_PER___INT64 >= 64
126 #   define HOST_BITS_PER_WIDEST_INT           HOST_BITS_PER___INT64
127 #   define HOST_WIDEST_INT                    __int64
128 #  else
129     #error "This line should be impossible to reach"
130 #  endif
131 # endif
132 # define HOST_WIDEST_INT_PRINT                HOST_LONG_LONG_FORMAT
133 # define HOST_WIDEST_INT_PRINT_DEC            "%" HOST_LONG_LONG_FORMAT "d"
134 # define HOST_WIDEST_INT_PRINT_DEC_C          "%" HOST_LONG_LONG_FORMAT "dLL"
135 # define HOST_WIDEST_INT_PRINT_UNSIGNED       "%" HOST_LONG_LONG_FORMAT "u"
136 # define HOST_WIDEST_INT_PRINT_HEX            "%#" HOST_LONG_LONG_FORMAT "x"
137 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     \
138     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
139 #endif
140
141 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
142    efficiently in hardware.  (That is, the widest integer type that fits
143    in a hardware register.)  Normally this is "long" but on some hosts it
144    should be "long long" or "__int64".  This is no convenient way to
145    autodetect this, so such systems must set a flag in config.host; see there
146    for details.  */
147
148 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
149 #  ifdef HAVE_LONG_LONG
150 #    define HOST_WIDEST_FAST_INT long long
151 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
152 #  elif defined (HAVE___INT64)
153 #    define HOST_WIDEST_FAST_INT __int64
154 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
155 #  else
156 #    error "Your host said it wanted to use long long or __int64 but neither"
157 #    error "exist"
158 #  endif
159 #else
160 #  define HOST_WIDEST_FAST_INT long
161 #  define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
162 #endif
163
164 /* Inline functions operating on HOST_WIDE_INT.  */
165 #if GCC_VERSION < 3004
166
167 extern int clz_hwi (unsigned HOST_WIDE_INT x);
168 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
169 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
170
171 /* Return log2, or -1 if not exact.  */
172 extern int exact_log2                  (unsigned HOST_WIDE_INT);
173
174 /* Return floor of log2, with -1 for zero.  */
175 extern int floor_log2                  (unsigned HOST_WIDE_INT);
176
177 #else /* GCC_VERSION >= 3004 */
178
179 /* For convenience, define 0 -> word_size.  */
180 static inline int
181 clz_hwi (unsigned HOST_WIDE_INT x)
182 {
183   if (x == 0)
184     return HOST_BITS_PER_WIDE_INT;
185 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
186   return __builtin_clzl (x);
187 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
188   return __builtin_clzll (x);
189 # else
190   return __builtin_clz (x);
191 # endif
192 }
193
194 static inline int
195 ctz_hwi (unsigned HOST_WIDE_INT x)
196 {
197   if (x == 0)
198     return HOST_BITS_PER_WIDE_INT;
199 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
200   return __builtin_ctzl (x);
201 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
202   return __builtin_ctzll (x);
203 # else
204   return __builtin_ctz (x);
205 # endif
206 }
207
208 static inline int
209 ffs_hwi (unsigned HOST_WIDE_INT x)
210 {
211 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
212   return __builtin_ffsl (x);
213 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
214   return __builtin_ffsll (x);
215 # else
216   return __builtin_ffs (x);
217 # endif
218 }
219
220 static inline int
221 floor_log2 (unsigned HOST_WIDE_INT x)
222 {
223   return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
224 }
225
226 static inline int
227 exact_log2 (unsigned HOST_WIDE_INT x)
228 {
229   return x == (x & -x) && x ? ctz_hwi (x) : -1;
230 }
231
232 #endif /* GCC_VERSION >= 3004 */
233
234 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
235   ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
236 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
237
238 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
239 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
240 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
241 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
242 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
243 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
244
245 #endif /* ! GCC_HWINT_H */