]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/double-int.h
re PR tree-optimization/57190 (verify_ssa failed: SSA_NAME_OCCURS_IN_ABNORMAL_PHI...
[thirdparty/gcc.git] / gcc / double-int.h
CommitLineData
f82783bd 1/* Operations with long integers.
d1e082c2 2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
b8698a0f 3
f82783bd 4This file is part of GCC.
b8698a0f 5
f82783bd
ZD
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
9dcd6f09 8Free Software Foundation; either version 3, or (at your option) any
f82783bd 9later version.
b8698a0f 10
f82783bd
ZD
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b8698a0f 15
f82783bd 16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
f82783bd
ZD
19
20#ifndef DOUBLE_INT_H
21#define DOUBLE_INT_H
22
23/* A large integer is currently represented as a pair of HOST_WIDE_INTs.
24 It therefore represents a number with precision of
25 2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
26 internal representation will change, if numbers with greater precision
27 are needed, so the users should not rely on it). The representation does
28 not contain any information about signedness of the represented value, so
29 it can be used to represent both signed and unsigned numbers. For
30 operations where the results depend on signedness (division, comparisons),
31 it must be specified separately. For each such operation, there are three
32 versions of the function -- double_int_op, that takes an extra UNS argument
33 giving the signedness of the values, and double_int_sop and double_int_uop
34 that stand for its specializations for signed and unsigned values.
35
36 You may also represent with numbers in smaller precision using double_int.
37 You however need to use double_int_ext (that fills in the bits of the
38 number over the prescribed precision with zeros or with the sign bit) before
39 operations that do not perform arithmetics modulo 2^precision (comparisons,
40 division), and possibly before storing the results, if you want to keep
41 them in some canonical form). In general, the signedness of double_int_ext
42 should match the signedness of the operation.
43
44 ??? The components of double_int differ in signedness mostly for
45 historical reasons (they replace an older structure used to represent
66a4ad37 46 numbers with precision higher than HOST_WIDE_INT). It might be less
f82783bd
ZD
47 confusing to have them both signed or both unsigned. */
48
27bcd47c 49struct double_int
f82783bd 50{
0823efed
DN
51 /* Normally, we would define constructors to create instances.
52 Two things prevent us from doing so.
53 First, defining a constructor makes the class non-POD in C++03,
54 and we certainly want double_int to be a POD.
55 Second, the GCC conding conventions prefer explicit conversion,
56 and explicit conversion operators are not available until C++11. */
57
6d67b4c7
RG
58 static double_int from_uhwi (unsigned HOST_WIDE_INT cst);
59 static double_int from_shwi (HOST_WIDE_INT cst);
9be0ac8c 60 static double_int from_pair (HOST_WIDE_INT high, unsigned HOST_WIDE_INT low);
0823efed 61
cc06c01d
GJL
62 /* Construct from a fuffer of length LEN. BUFFER will be read according
63 to byte endianess and word endianess. */
64 static double_int from_buffer (const unsigned char *buffer, int len);
65
0823efed
DN
66 /* No copy assignment operator or destructor to keep the type a POD. */
67
68 /* There are some special value-creation static member functions. */
69
70 static double_int mask (unsigned prec);
71 static double_int max_value (unsigned int prec, bool uns);
72 static double_int min_value (unsigned int prec, bool uns);
73
74 /* The following functions are mutating operations. */
75
76 double_int &operator ++ (); // prefix
77 double_int &operator -- (); // prefix
78 double_int &operator *= (double_int);
79 double_int &operator += (double_int);
80 double_int &operator -= (double_int);
27bcd47c
LC
81 double_int &operator &= (double_int);
82 double_int &operator ^= (double_int);
83 double_int &operator |= (double_int);
0823efed
DN
84
85 /* The following functions are non-mutating operations. */
86
87 /* Conversion functions. */
88
6d67b4c7
RG
89 HOST_WIDE_INT to_shwi () const;
90 unsigned HOST_WIDE_INT to_uhwi () const;
0823efed
DN
91
92 /* Conversion query functions. */
93
6d67b4c7
RG
94 bool fits_uhwi () const;
95 bool fits_shwi () const;
96 bool fits_hwi (bool uns) const;
0823efed
DN
97
98 /* Attribute query functions. */
99
100 int trailing_zeros () const;
101 int popcount () const;
102
103 /* Arithmetic query operations. */
104
105 bool multiple_of (double_int, bool, double_int *) const;
106
107 /* Arithmetic operation functions. */
108
9be0ac8c
LC
109 /* The following operations perform arithmetics modulo 2^precision, so you
110 do not need to call .ext between them, even if you are representing
111 numbers with precision less than HOST_BITS_PER_DOUBLE_INT bits. */
112
0823efed 113 double_int set_bit (unsigned) const;
27bcd47c 114 double_int mul_with_sign (double_int, bool unsigned_p, bool *overflow) const;
9be0ac8c
LC
115 double_int wide_mul_with_sign (double_int, bool unsigned_p,
116 double_int *higher, bool *overflow) const;
27bcd47c 117 double_int add_with_sign (double_int, bool unsigned_p, bool *overflow) const;
9be0ac8c
LC
118 double_int sub_with_overflow (double_int, bool *overflow) const;
119 double_int neg_with_overflow (bool *overflow) const;
0823efed 120
27bcd47c
LC
121 double_int operator * (double_int) const;
122 double_int operator + (double_int) const;
123 double_int operator - (double_int) const;
0823efed
DN
124 double_int operator - () const;
125 double_int operator ~ () const;
27bcd47c
LC
126 double_int operator & (double_int) const;
127 double_int operator | (double_int) const;
128 double_int operator ^ (double_int) const;
129 double_int and_not (double_int) const;
0823efed 130
07bfc9ec 131 double_int lshift (HOST_WIDE_INT count) const;
0823efed
DN
132 double_int lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const;
133 double_int rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const;
134 double_int alshift (HOST_WIDE_INT count, unsigned int prec) const;
135 double_int arshift (HOST_WIDE_INT count, unsigned int prec) const;
136 double_int llshift (HOST_WIDE_INT count, unsigned int prec) const;
137 double_int lrshift (HOST_WIDE_INT count, unsigned int prec) const;
138 double_int lrotate (HOST_WIDE_INT count, unsigned int prec) const;
139 double_int rrotate (HOST_WIDE_INT count, unsigned int prec) const;
140
141 /* You must ensure that double_int::ext is called on the operands
142 of the following operations, if the precision of the numbers
143 is less than HOST_BITS_PER_DOUBLE_INT bits. */
9be0ac8c 144
0823efed
DN
145 double_int div (double_int, bool, unsigned) const;
146 double_int sdiv (double_int, unsigned) const;
147 double_int udiv (double_int, unsigned) const;
148 double_int mod (double_int, bool, unsigned) const;
149 double_int smod (double_int, unsigned) const;
150 double_int umod (double_int, unsigned) const;
9be0ac8c
LC
151 double_int divmod_with_overflow (double_int, bool, unsigned,
152 double_int *, bool *) const;
0823efed
DN
153 double_int divmod (double_int, bool, unsigned, double_int *) const;
154 double_int sdivmod (double_int, unsigned, double_int *) const;
155 double_int udivmod (double_int, unsigned, double_int *) const;
156
157 /* Precision control functions. */
158
159 double_int ext (unsigned prec, bool uns) const;
160 double_int zext (unsigned prec) const;
161 double_int sext (unsigned prec) const;
162
163 /* Comparative functions. */
164
165 bool is_zero () const;
166 bool is_one () const;
167 bool is_minus_one () const;
168 bool is_negative () const;
169
170 int cmp (double_int b, bool uns) const;
171 int ucmp (double_int b) const;
172 int scmp (double_int b) const;
173
174 bool ult (double_int b) const;
27bcd47c 175 bool ule (double_int b) const;
0823efed
DN
176 bool ugt (double_int b) const;
177 bool slt (double_int b) const;
27bcd47c 178 bool sle (double_int b) const;
0823efed
DN
179 bool sgt (double_int b) const;
180
181 double_int max (double_int b, bool uns);
182 double_int smax (double_int b);
183 double_int umax (double_int b);
184
185 double_int min (double_int b, bool uns);
186 double_int smin (double_int b);
187 double_int umin (double_int b);
188
189 bool operator == (double_int cst2) const;
190 bool operator != (double_int cst2) const;
191
192 /* Please migrate away from using these member variables publically. */
193
f82783bd
ZD
194 unsigned HOST_WIDE_INT low;
195 HOST_WIDE_INT high;
0823efed 196
27bcd47c 197};
f82783bd 198
2bd1333d
AS
199#define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
200
f82783bd
ZD
201/* Constructors and conversions. */
202
f82783bd
ZD
203/* Constructs double_int from integer CST. The bits over the precision of
204 HOST_WIDE_INT are filled with the sign bit. */
205
27bcd47c
LC
206inline double_int
207double_int::from_shwi (HOST_WIDE_INT cst)
f82783bd
ZD
208{
209 double_int r;
f82783bd
ZD
210 r.low = (unsigned HOST_WIDE_INT) cst;
211 r.high = cst < 0 ? -1 : 0;
f82783bd
ZD
212 return r;
213}
214
215/* Some useful constants. */
0823efed
DN
216/* FIXME(crowl): Maybe remove after converting callers?
217 The problem is that a named constant would not be as optimizable,
218 while the functional syntax is more verbose. */
f82783bd 219
6d67b4c7
RG
220#define double_int_minus_one (double_int::from_shwi (-1))
221#define double_int_zero (double_int::from_shwi (0))
222#define double_int_one (double_int::from_shwi (1))
223#define double_int_two (double_int::from_shwi (2))
224#define double_int_ten (double_int::from_shwi (10))
f82783bd
ZD
225
226/* Constructs double_int from unsigned integer CST. The bits over the
227 precision of HOST_WIDE_INT are filled with zeros. */
228
27bcd47c
LC
229inline double_int
230double_int::from_uhwi (unsigned HOST_WIDE_INT cst)
f82783bd
ZD
231{
232 double_int r;
f82783bd
ZD
233 r.low = cst;
234 r.high = 0;
f82783bd
ZD
235 return r;
236}
237
9be0ac8c
LC
238inline double_int
239double_int::from_pair (HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
0823efed 240{
9be0ac8c
LC
241 double_int r;
242 r.low = low;
243 r.high = high;
244 return r;
0823efed
DN
245}
246
247inline double_int &
248double_int::operator ++ ()
249{
250 *this += double_int_one;
251 return *this;
252}
253
254inline double_int &
255double_int::operator -- ()
256{
257 *this -= double_int_one;
258 return *this;
259}
260
27bcd47c
LC
261inline double_int &
262double_int::operator &= (double_int b)
263{
264 *this = *this & b;
265 return *this;
266}
267
268inline double_int &
269double_int::operator ^= (double_int b)
270{
271 *this = *this ^ b;
272 return *this;
273}
274
275inline double_int &
276double_int::operator |= (double_int b)
277{
278 *this = *this | b;
279 return *this;
280}
281
fd7de64c 282/* Returns value of CST as a signed number. CST must satisfy
0823efed 283 double_int::fits_signed. */
fd7de64c 284
0823efed 285inline HOST_WIDE_INT
6d67b4c7 286double_int::to_shwi () const
0823efed
DN
287{
288 return (HOST_WIDE_INT) low;
289}
290
fd7de64c 291/* Returns value of CST as an unsigned number. CST must satisfy
0823efed
DN
292 double_int::fits_unsigned. */
293
294inline unsigned HOST_WIDE_INT
6d67b4c7 295double_int::to_uhwi () const
0823efed
DN
296{
297 return low;
298}
fd7de64c 299
fd7de64c
AS
300/* Returns true if CST fits in unsigned HOST_WIDE_INT. */
301
0823efed 302inline bool
6d67b4c7 303double_int::fits_uhwi () const
0823efed
DN
304{
305 return high == 0;
306}
307
2bd1333d 308/* Logical operations. */
1961ffb8
AS
309
310/* Returns ~A. */
311
0823efed
DN
312inline double_int
313double_int::operator ~ () const
314{
315 double_int result;
316 result.low = ~low;
317 result.high = ~high;
318 return result;
319}
320
1961ffb8
AS
321/* Returns A | B. */
322
0823efed
DN
323inline double_int
324double_int::operator | (double_int b) const
325{
326 double_int result;
327 result.low = low | b.low;
328 result.high = high | b.high;
329 return result;
330}
331
e233a3b2
AS
332/* Returns A & B. */
333
0823efed
DN
334inline double_int
335double_int::operator & (double_int b) const
336{
337 double_int result;
338 result.low = low & b.low;
339 result.high = high & b.high;
340 return result;
341}
342
950f7f45
RG
343/* Returns A & ~B. */
344
0823efed
DN
345inline double_int
346double_int::and_not (double_int b) const
347{
348 double_int result;
349 result.low = low & ~b.low;
350 result.high = high & ~b.high;
351 return result;
352}
353
fd7de64c
AS
354/* Returns A ^ B. */
355
0823efed
DN
356inline double_int
357double_int::operator ^ (double_int b) const
358{
359 double_int result;
360 result.low = low ^ b.low;
361 result.high = high ^ b.high;
362 return result;
363}
364
f82783bd
ZD
365void dump_double_int (FILE *, double_int, bool);
366
f82783bd
ZD
367#define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
368
369/* The operands of the following comparison functions must be processed
370 with double_int_ext, if their precision is less than
49ab6098 371 HOST_BITS_PER_DOUBLE_INT bits. */
f82783bd
ZD
372
373/* Returns true if CST is zero. */
374
0823efed
DN
375inline bool
376double_int::is_zero () const
377{
378 return low == 0 && high == 0;
379}
380
f82783bd
ZD
381/* Returns true if CST is one. */
382
0823efed
DN
383inline bool
384double_int::is_one () const
385{
386 return low == 1 && high == 0;
387}
388
f82783bd
ZD
389/* Returns true if CST is minus one. */
390
0823efed
DN
391inline bool
392double_int::is_minus_one () const
393{
394 return low == ALL_ONES && high == -1;
395}
396
0823efed
DN
397/* Returns true if CST is negative. */
398
399inline bool
400double_int::is_negative () const
401{
402 return high < 0;
f82783bd
ZD
403}
404
405/* Returns true if CST1 == CST2. */
406
0823efed
DN
407inline bool
408double_int::operator == (double_int cst2) const
409{
410 return low == cst2.low && high == cst2.high;
411}
412
0823efed
DN
413/* Returns true if CST1 != CST2. */
414
415inline bool
416double_int::operator != (double_int cst2) const
417{
418 return low != cst2.low || high != cst2.high;
f82783bd
ZD
419}
420
440b6d59
TV
421/* Return number of set bits of CST. */
422
0823efed
DN
423inline int
424double_int::popcount () const
425{
426 return popcount_hwi (high) + popcount_hwi (low);
427}
428
330db1e3 429
34917a10 430#ifndef GENERATOR_FILE
e4fd22c6
BM
431/* Conversion to and from GMP integer representations. */
432
433void mpz_set_double_int (mpz_t, double_int, bool);
22ea9ec0 434double_int mpz_get_double_int (const_tree, mpz_t, bool);
34917a10 435#endif
e4fd22c6 436
f82783bd 437#endif /* DOUBLE_INT_H */