1 /* Operations with long integers.
2 Copyright (C) 2006, 2007, 2008, 2010, 2012 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #ifndef GENERATOR_FILE
27 /* A large integer is currently represented as a pair of HOST_WIDE_INTs.
28 It therefore represents a number with precision of
29 2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
30 internal representation will change, if numbers with greater precision
31 are needed, so the users should not rely on it). The representation does
32 not contain any information about signedness of the represented value, so
33 it can be used to represent both signed and unsigned numbers. For
34 operations where the results depend on signedness (division, comparisons),
35 it must be specified separately. For each such operation, there are three
36 versions of the function -- double_int_op, that takes an extra UNS argument
37 giving the signedness of the values, and double_int_sop and double_int_uop
38 that stand for its specializations for signed and unsigned values.
40 You may also represent with numbers in smaller precision using double_int.
41 You however need to use double_int_ext (that fills in the bits of the
42 number over the prescribed precision with zeros or with the sign bit) before
43 operations that do not perform arithmetics modulo 2^precision (comparisons,
44 division), and possibly before storing the results, if you want to keep
45 them in some canonical form). In general, the signedness of double_int_ext
46 should match the signedness of the operation.
48 ??? The components of double_int differ in signedness mostly for
49 historical reasons (they replace an older structure used to represent
50 numbers with precision higher than HOST_WIDE_INT). It might be less
51 confusing to have them both signed or both unsigned. */
55 /* Normally, we would define constructors to create instances.
56 Two things prevent us from doing so.
57 First, defining a constructor makes the class non-POD in C++03,
58 and we certainly want double_int to be a POD.
59 Second, the GCC conding conventions prefer explicit conversion,
60 and explicit conversion operators are not available until C++11. */
62 static double_int
from_uhwi (unsigned HOST_WIDE_INT cst
);
63 static double_int
from_shwi (HOST_WIDE_INT cst
);
65 /* No copy assignment operator or destructor to keep the type a POD. */
67 /* There are some special value-creation static member functions. */
69 static double_int
mask (unsigned prec
);
70 static double_int
max_value (unsigned int prec
, bool uns
);
71 static double_int
min_value (unsigned int prec
, bool uns
);
73 /* The following functions are mutating operations. */
75 double_int
&operator ++ (); // prefix
76 double_int
&operator -- (); // prefix
77 double_int
&operator *= (double_int
);
78 double_int
&operator += (double_int
);
79 double_int
&operator -= (double_int
);
80 double_int
&operator &= (double_int
);
81 double_int
&operator ^= (double_int
);
82 double_int
&operator |= (double_int
);
84 /* The following functions are non-mutating operations. */
86 /* Conversion functions. */
88 HOST_WIDE_INT
to_shwi () const;
89 unsigned HOST_WIDE_INT
to_uhwi () const;
91 /* Conversion query functions. */
93 bool fits_uhwi () const;
94 bool fits_shwi () const;
95 bool fits_hwi (bool uns
) const;
97 /* Attribute query functions. */
99 int trailing_zeros () const;
100 int popcount () const;
102 /* Arithmetic query operations. */
104 bool multiple_of (double_int
, bool, double_int
*) const;
106 /* Arithmetic operation functions. */
108 double_int
set_bit (unsigned) const;
109 double_int
mul_with_sign (double_int
, bool unsigned_p
, bool *overflow
) const;
110 double_int
add_with_sign (double_int
, bool unsigned_p
, bool *overflow
) const;
112 double_int
operator * (double_int
) const;
113 double_int
operator + (double_int
) const;
114 double_int
operator - (double_int
) const;
115 double_int
operator - () const;
116 double_int
operator ~ () const;
117 double_int
operator & (double_int
) const;
118 double_int
operator | (double_int
) const;
119 double_int
operator ^ (double_int
) const;
120 double_int
and_not (double_int
) const;
122 double_int
lshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const;
123 double_int
rshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const;
124 double_int
alshift (HOST_WIDE_INT count
, unsigned int prec
) const;
125 double_int
arshift (HOST_WIDE_INT count
, unsigned int prec
) const;
126 double_int
llshift (HOST_WIDE_INT count
, unsigned int prec
) const;
127 double_int
lrshift (HOST_WIDE_INT count
, unsigned int prec
) const;
128 double_int
lrotate (HOST_WIDE_INT count
, unsigned int prec
) const;
129 double_int
rrotate (HOST_WIDE_INT count
, unsigned int prec
) const;
131 /* You must ensure that double_int::ext is called on the operands
132 of the following operations, if the precision of the numbers
133 is less than HOST_BITS_PER_DOUBLE_INT bits. */
134 double_int
div (double_int
, bool, unsigned) const;
135 double_int
sdiv (double_int
, unsigned) const;
136 double_int
udiv (double_int
, unsigned) const;
137 double_int
mod (double_int
, bool, unsigned) const;
138 double_int
smod (double_int
, unsigned) const;
139 double_int
umod (double_int
, unsigned) const;
140 double_int
divmod (double_int
, bool, unsigned, double_int
*) const;
141 double_int
sdivmod (double_int
, unsigned, double_int
*) const;
142 double_int
udivmod (double_int
, unsigned, double_int
*) const;
144 /* Precision control functions. */
146 double_int
ext (unsigned prec
, bool uns
) const;
147 double_int
zext (unsigned prec
) const;
148 double_int
sext (unsigned prec
) const;
150 /* Comparative functions. */
152 bool is_zero () const;
153 bool is_one () const;
154 bool is_minus_one () const;
155 bool is_negative () const;
157 int cmp (double_int b
, bool uns
) const;
158 int ucmp (double_int b
) const;
159 int scmp (double_int b
) const;
161 bool ult (double_int b
) const;
162 bool ule (double_int b
) const;
163 bool ugt (double_int b
) const;
164 bool slt (double_int b
) const;
165 bool sle (double_int b
) const;
166 bool sgt (double_int b
) const;
168 double_int
max (double_int b
, bool uns
);
169 double_int
smax (double_int b
);
170 double_int
umax (double_int b
);
172 double_int
min (double_int b
, bool uns
);
173 double_int
smin (double_int b
);
174 double_int
umin (double_int b
);
176 bool operator == (double_int cst2
) const;
177 bool operator != (double_int cst2
) const;
179 /* Please migrate away from using these member variables publically. */
181 unsigned HOST_WIDE_INT low
;
186 #define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
188 /* Constructors and conversions. */
190 /* Constructs double_int from integer CST. The bits over the precision of
191 HOST_WIDE_INT are filled with the sign bit. */
194 double_int::from_shwi (HOST_WIDE_INT cst
)
197 r
.low
= (unsigned HOST_WIDE_INT
) cst
;
198 r
.high
= cst
< 0 ? -1 : 0;
202 /* FIXME(crowl): Remove after converting callers. */
203 static inline double_int
204 shwi_to_double_int (HOST_WIDE_INT cst
)
206 return double_int::from_shwi (cst
);
209 /* Some useful constants. */
210 /* FIXME(crowl): Maybe remove after converting callers?
211 The problem is that a named constant would not be as optimizable,
212 while the functional syntax is more verbose. */
214 #define double_int_minus_one (double_int::from_shwi (-1))
215 #define double_int_zero (double_int::from_shwi (0))
216 #define double_int_one (double_int::from_shwi (1))
217 #define double_int_two (double_int::from_shwi (2))
218 #define double_int_ten (double_int::from_shwi (10))
220 /* Constructs double_int from unsigned integer CST. The bits over the
221 precision of HOST_WIDE_INT are filled with zeros. */
224 double_int::from_uhwi (unsigned HOST_WIDE_INT cst
)
232 /* FIXME(crowl): Remove after converting callers. */
233 static inline double_int
234 uhwi_to_double_int (unsigned HOST_WIDE_INT cst
)
236 return double_int::from_uhwi (cst
);
240 double_int::operator ++ ()
242 *this += double_int_one
;
247 double_int::operator -- ()
249 *this -= double_int_one
;
254 double_int::operator *= (double_int b
)
261 double_int::operator += (double_int b
)
268 double_int::operator -= (double_int b
)
275 double_int::operator &= (double_int b
)
282 double_int::operator ^= (double_int b
)
289 double_int::operator |= (double_int b
)
295 /* Returns value of CST as a signed number. CST must satisfy
296 double_int::fits_signed. */
299 double_int::to_shwi () const
301 return (HOST_WIDE_INT
) low
;
304 /* FIXME(crowl): Remove after converting callers. */
305 static inline HOST_WIDE_INT
306 double_int_to_shwi (double_int cst
)
308 return cst
.to_shwi ();
311 /* Returns value of CST as an unsigned number. CST must satisfy
312 double_int::fits_unsigned. */
314 inline unsigned HOST_WIDE_INT
315 double_int::to_uhwi () const
320 /* FIXME(crowl): Remove after converting callers. */
321 static inline unsigned HOST_WIDE_INT
322 double_int_to_uhwi (double_int cst
)
324 return cst
.to_uhwi ();
327 /* Returns true if CST fits in unsigned HOST_WIDE_INT. */
330 double_int::fits_uhwi () const
335 /* FIXME(crowl): Remove after converting callers. */
337 double_int_fits_in_uhwi_p (double_int cst
)
339 return cst
.fits_uhwi ();
342 /* Returns true if CST fits in signed HOST_WIDE_INT. */
344 /* FIXME(crowl): Remove after converting callers. */
346 double_int_fits_in_shwi_p (double_int cst
)
348 return cst
.fits_shwi ();
351 /* FIXME(crowl): Remove after converting callers. */
353 double_int_fits_in_hwi_p (double_int cst
, bool uns
)
355 return cst
.fits_hwi (uns
);
358 /* The following operations perform arithmetics modulo 2^precision,
359 so you do not need to call double_int_ext between them, even if
360 you are representing numbers with precision less than
361 HOST_BITS_PER_DOUBLE_INT bits. */
363 /* FIXME(crowl): Remove after converting callers. */
365 double_int_mul (double_int a
, double_int b
)
370 /* FIXME(crowl): Remove after converting callers. */
372 double_int_mul_with_sign (double_int a
, double_int b
,
373 bool unsigned_p
, int *overflow
)
376 return a
.mul_with_sign (b
, unsigned_p
, &ovf
);
380 /* FIXME(crowl): Remove after converting callers. */
382 double_int_add (double_int a
, double_int b
)
387 /* FIXME(crowl): Remove after converting callers. */
389 double_int_sub (double_int a
, double_int b
)
394 /* FIXME(crowl): Remove after converting callers. */
396 double_int_neg (double_int a
)
401 /* You must ensure that double_int_ext is called on the operands
402 of the following operations, if the precision of the numbers
403 is less than HOST_BITS_PER_DOUBLE_INT bits. */
405 /* FIXME(crowl): Remove after converting callers. */
407 double_int_div (double_int a
, double_int b
, bool uns
, unsigned code
)
409 return a
.div (b
, uns
, code
);
412 /* FIXME(crowl): Remove after converting callers. */
414 double_int_sdiv (double_int a
, double_int b
, unsigned code
)
416 return a
.sdiv (b
, code
);
419 /* FIXME(crowl): Remove after converting callers. */
421 double_int_udiv (double_int a
, double_int b
, unsigned code
)
423 return a
.udiv (b
, code
);
426 /* FIXME(crowl): Remove after converting callers. */
428 double_int_mod (double_int a
, double_int b
, bool uns
, unsigned code
)
430 return a
.mod (b
, uns
, code
);
433 /* FIXME(crowl): Remove after converting callers. */
435 double_int_smod (double_int a
, double_int b
, unsigned code
)
437 return a
.smod (b
, code
);
440 /* FIXME(crowl): Remove after converting callers. */
442 double_int_umod (double_int a
, double_int b
, unsigned code
)
444 return a
.umod (b
, code
);
447 /* FIXME(crowl): Remove after converting callers. */
449 double_int_divmod (double_int a
, double_int b
, bool uns
,
450 unsigned code
, double_int
*mod
)
452 return a
.divmod (b
, uns
, code
, mod
);
455 /* FIXME(crowl): Remove after converting callers. */
457 double_int_sdivmod (double_int a
, double_int b
, unsigned code
, double_int
*mod
)
459 return a
.sdivmod (b
, code
, mod
);
462 /* FIXME(crowl): Remove after converting callers. */
464 double_int_udivmod (double_int a
, double_int b
, unsigned code
, double_int
*mod
)
466 return a
.udivmod (b
, code
, mod
);
471 /* FIXME(crowl): Remove after converting callers. */
473 double_int_multiple_of (double_int product
, double_int factor
,
474 bool unsigned_p
, double_int
*multiple
)
476 return product
.multiple_of (factor
, unsigned_p
, multiple
);
479 /* FIXME(crowl): Remove after converting callers. */
481 double_int_setbit (double_int a
, unsigned bitpos
)
483 return a
.set_bit (bitpos
);
486 /* FIXME(crowl): Remove after converting callers. */
488 double_int_ctz (double_int a
)
490 return a
.trailing_zeros ();
493 /* Logical operations. */
498 double_int::operator ~ () const
506 /* FIXME(crowl): Remove after converting callers. */
507 static inline double_int
508 double_int_not (double_int a
)
516 double_int::operator | (double_int b
) const
519 result
.low
= low
| b
.low
;
520 result
.high
= high
| b
.high
;
524 /* FIXME(crowl): Remove after converting callers. */
525 static inline double_int
526 double_int_ior (double_int a
, double_int b
)
534 double_int::operator & (double_int b
) const
537 result
.low
= low
& b
.low
;
538 result
.high
= high
& b
.high
;
542 /* FIXME(crowl): Remove after converting callers. */
543 static inline double_int
544 double_int_and (double_int a
, double_int b
)
549 /* Returns A & ~B. */
552 double_int::and_not (double_int b
) const
555 result
.low
= low
& ~b
.low
;
556 result
.high
= high
& ~b
.high
;
560 /* FIXME(crowl): Remove after converting callers. */
561 static inline double_int
562 double_int_and_not (double_int a
, double_int b
)
564 return a
.and_not (b
);
570 double_int::operator ^ (double_int b
) const
573 result
.low
= low
^ b
.low
;
574 result
.high
= high
^ b
.high
;
578 /* FIXME(crowl): Remove after converting callers. */
579 static inline double_int
580 double_int_xor (double_int a
, double_int b
)
586 /* Shift operations. */
588 /* FIXME(crowl): Remove after converting callers. */
590 double_int_lshift (double_int a
, HOST_WIDE_INT count
, unsigned int prec
,
593 return a
.lshift (count
, prec
, arith
);
596 /* FIXME(crowl): Remove after converting callers. */
598 double_int_rshift (double_int a
, HOST_WIDE_INT count
, unsigned int prec
,
601 return a
.rshift (count
, prec
, arith
);
604 /* FIXME(crowl): Remove after converting callers. */
606 double_int_lrotate (double_int a
, HOST_WIDE_INT count
, unsigned int prec
)
608 return a
.lrotate (count
, prec
);
611 /* FIXME(crowl): Remove after converting callers. */
613 double_int_rrotate (double_int a
, HOST_WIDE_INT count
, unsigned int prec
)
615 return a
.rrotate (count
, prec
);
618 /* Returns true if CST is negative. Of course, CST is considered to
622 double_int_negative_p (double_int cst
)
627 /* FIXME(crowl): Remove after converting callers. */
629 double_int_cmp (double_int a
, double_int b
, bool uns
)
631 return a
.cmp (b
, uns
);
634 /* FIXME(crowl): Remove after converting callers. */
636 double_int_scmp (double_int a
, double_int b
)
641 /* FIXME(crowl): Remove after converting callers. */
643 double_int_ucmp (double_int a
, double_int b
)
648 /* FIXME(crowl): Remove after converting callers. */
650 double_int_max (double_int a
, double_int b
, bool uns
)
652 return a
.max (b
, uns
);
655 /* FIXME(crowl): Remove after converting callers. */
657 double_int_smax (double_int a
, double_int b
)
662 /* FIXME(crowl): Remove after converting callers. */
664 double_int_umax (double_int a
, double_int b
)
670 /* FIXME(crowl): Remove after converting callers. */
672 double_int_min (double_int a
, double_int b
, bool uns
)
674 return a
.min (b
, uns
);
677 /* FIXME(crowl): Remove after converting callers. */
679 double_int_smin (double_int a
, double_int b
)
684 /* FIXME(crowl): Remove after converting callers. */
686 double_int_umin (double_int a
, double_int b
)
691 void dump_double_int (FILE *, double_int
, bool);
693 /* Zero and sign extension of numbers in smaller precisions. */
695 /* FIXME(crowl): Remove after converting callers. */
697 double_int_ext (double_int a
, unsigned prec
, bool uns
)
699 return a
.ext (prec
, uns
);
702 /* FIXME(crowl): Remove after converting callers. */
704 double_int_sext (double_int a
, unsigned prec
)
706 return a
.sext (prec
);
709 /* FIXME(crowl): Remove after converting callers. */
711 double_int_zext (double_int a
, unsigned prec
)
713 return a
.zext (prec
);
716 /* FIXME(crowl): Remove after converting callers. */
718 double_int_mask (unsigned prec
)
720 return double_int::mask (prec
);
723 /* FIXME(crowl): Remove after converting callers. */
725 double_int_max_value (unsigned int prec
, bool uns
)
727 return double_int::max_value (prec
, uns
);
730 /* FIXME(crowl): Remove after converting callers. */
732 double_int_min_value (unsigned int prec
, bool uns
)
734 return double_int::min_value (prec
, uns
);
737 #define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
739 /* The operands of the following comparison functions must be processed
740 with double_int_ext, if their precision is less than
741 HOST_BITS_PER_DOUBLE_INT bits. */
743 /* Returns true if CST is zero. */
746 double_int::is_zero () const
748 return low
== 0 && high
== 0;
751 /* FIXME(crowl): Remove after converting callers. */
753 double_int_zero_p (double_int cst
)
755 return cst
.is_zero ();
758 /* Returns true if CST is one. */
761 double_int::is_one () const
763 return low
== 1 && high
== 0;
766 /* FIXME(crowl): Remove after converting callers. */
768 double_int_one_p (double_int cst
)
770 return cst
.is_one ();
773 /* Returns true if CST is minus one. */
776 double_int::is_minus_one () const
778 return low
== ALL_ONES
&& high
== -1;
781 /* FIXME(crowl): Remove after converting callers. */
783 double_int_minus_one_p (double_int cst
)
785 return cst
.is_minus_one ();
788 /* Returns true if CST is negative. */
791 double_int::is_negative () const
796 /* Returns true if CST1 == CST2. */
799 double_int::operator == (double_int cst2
) const
801 return low
== cst2
.low
&& high
== cst2
.high
;
804 /* FIXME(crowl): Remove after converting callers. */
806 double_int_equal_p (double_int cst1
, double_int cst2
)
811 /* Returns true if CST1 != CST2. */
814 double_int::operator != (double_int cst2
) const
816 return low
!= cst2
.low
|| high
!= cst2
.high
;
819 /* Return number of set bits of CST. */
822 double_int::popcount () const
824 return popcount_hwi (high
) + popcount_hwi (low
);
827 /* FIXME(crowl): Remove after converting callers. */
829 double_int_popcount (double_int cst
)
831 return cst
.popcount ();
835 /* Legacy interface with decomposed high/low parts. */
837 /* FIXME(crowl): Remove after converting callers. */
838 extern int add_double_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
839 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
840 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
842 /* FIXME(crowl): Remove after converting callers. */
843 #define add_double(l1,h1,l2,h2,lv,hv) \
844 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
845 /* FIXME(crowl): Remove after converting callers. */
846 extern int neg_double (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
847 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*);
848 /* FIXME(crowl): Remove after converting callers. */
849 extern int mul_double_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
850 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
851 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
853 /* FIXME(crowl): Remove after converting callers. */
854 extern int mul_double_wide_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
855 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
856 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
857 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
859 /* FIXME(crowl): Remove after converting callers. */
860 #define mul_double(l1,h1,l2,h2,lv,hv) \
861 mul_double_with_sign (l1, h1, l2, h2, lv, hv, false)
862 /* FIXME(crowl): Remove after converting callers. */
863 extern void lshift_double (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
864 HOST_WIDE_INT
, unsigned int,
865 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*, bool);
866 /* FIXME(crowl): Remove after converting callers. */
867 extern int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT
,
868 HOST_WIDE_INT
, unsigned HOST_WIDE_INT
,
869 HOST_WIDE_INT
, unsigned HOST_WIDE_INT
*,
870 HOST_WIDE_INT
*, unsigned HOST_WIDE_INT
*,
874 #ifndef GENERATOR_FILE
875 /* Conversion to and from GMP integer representations. */
877 void mpz_set_double_int (mpz_t
, double_int
, bool);
878 double_int
mpz_get_double_int (const_tree
, mpz_t
, bool);
881 #endif /* DOUBLE_INT_H */