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. */
53 typedef struct double_int
56 /* Normally, we would define constructors to create instances.
57 Two things prevent us from doing so.
58 First, defining a constructor makes the class non-POD in C++03,
59 and we certainly want double_int to be a POD.
60 Second, the GCC conding conventions prefer explicit conversion,
61 and explicit conversion operators are not available until C++11. */
63 static double_int
from_uhwi (unsigned HOST_WIDE_INT cst
);
64 static double_int
from_shwi (HOST_WIDE_INT cst
);
66 /* No copy assignment operator or destructor to keep the type a POD. */
68 /* There are some special value-creation static member functions. */
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
);
74 /* The following functions are mutating operations. */
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
);
82 /* The following functions are non-mutating operations. */
84 /* Conversion functions. */
86 HOST_WIDE_INT
to_shwi () const;
87 unsigned HOST_WIDE_INT
to_uhwi () const;
89 /* Conversion query functions. */
91 bool fits_uhwi () const;
92 bool fits_shwi () const;
93 bool fits_hwi (bool uns
) const;
95 /* Attribute query functions. */
97 int trailing_zeros () const;
98 int popcount () const;
100 /* Arithmetic query operations. */
102 bool multiple_of (double_int
, bool, double_int
*) const;
104 /* Arithmetic operation functions. */
106 double_int
set_bit (unsigned) const;
107 double_int
mul_with_sign (double_int
, bool, int *) const;
109 double_int
operator * (double_int b
) const;
110 double_int
operator + (double_int b
) const;
111 double_int
operator - (double_int b
) const;
112 double_int
operator - () const;
113 double_int
operator ~ () const;
114 double_int
operator & (double_int b
) const;
115 double_int
operator | (double_int b
) const;
116 double_int
operator ^ (double_int b
) const;
117 double_int
and_not (double_int b
) const;
119 double_int
lshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const;
120 double_int
rshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const;
121 double_int
alshift (HOST_WIDE_INT count
, unsigned int prec
) const;
122 double_int
arshift (HOST_WIDE_INT count
, unsigned int prec
) const;
123 double_int
llshift (HOST_WIDE_INT count
, unsigned int prec
) const;
124 double_int
lrshift (HOST_WIDE_INT count
, unsigned int prec
) const;
125 double_int
lrotate (HOST_WIDE_INT count
, unsigned int prec
) const;
126 double_int
rrotate (HOST_WIDE_INT count
, unsigned int prec
) const;
128 /* You must ensure that double_int::ext is called on the operands
129 of the following operations, if the precision of the numbers
130 is less than HOST_BITS_PER_DOUBLE_INT bits. */
131 double_int
div (double_int
, bool, unsigned) const;
132 double_int
sdiv (double_int
, unsigned) const;
133 double_int
udiv (double_int
, unsigned) const;
134 double_int
mod (double_int
, bool, unsigned) const;
135 double_int
smod (double_int
, unsigned) const;
136 double_int
umod (double_int
, unsigned) const;
137 double_int
divmod (double_int
, bool, unsigned, double_int
*) const;
138 double_int
sdivmod (double_int
, unsigned, double_int
*) const;
139 double_int
udivmod (double_int
, unsigned, double_int
*) const;
141 /* Precision control functions. */
143 double_int
ext (unsigned prec
, bool uns
) const;
144 double_int
zext (unsigned prec
) const;
145 double_int
sext (unsigned prec
) const;
147 /* Comparative functions. */
149 bool is_zero () const;
150 bool is_one () const;
151 bool is_minus_one () const;
152 bool is_negative () const;
154 int cmp (double_int b
, bool uns
) const;
155 int ucmp (double_int b
) const;
156 int scmp (double_int b
) const;
158 bool ult (double_int b
) const;
159 bool ugt (double_int b
) const;
160 bool slt (double_int b
) const;
161 bool sgt (double_int b
) const;
163 double_int
max (double_int b
, bool uns
);
164 double_int
smax (double_int b
);
165 double_int
umax (double_int b
);
167 double_int
min (double_int b
, bool uns
);
168 double_int
smin (double_int b
);
169 double_int
umin (double_int b
);
171 bool operator == (double_int cst2
) const;
172 bool operator != (double_int cst2
) const;
174 /* Please migrate away from using these member variables publically. */
176 unsigned HOST_WIDE_INT low
;
181 #define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
183 /* Constructors and conversions. */
185 /* Constructs double_int from integer CST. The bits over the precision of
186 HOST_WIDE_INT are filled with the sign bit. */
189 double_int
double_int::from_shwi (HOST_WIDE_INT cst
)
192 r
.low
= (unsigned HOST_WIDE_INT
) cst
;
193 r
.high
= cst
< 0 ? -1 : 0;
197 /* FIXME(crowl): Remove after converting callers. */
198 static inline double_int
199 shwi_to_double_int (HOST_WIDE_INT cst
)
201 return double_int::from_shwi (cst
);
204 /* Some useful constants. */
205 /* FIXME(crowl): Maybe remove after converting callers?
206 The problem is that a named constant would not be as optimizable,
207 while the functional syntax is more verbose. */
209 #define double_int_minus_one (double_int::from_shwi (-1))
210 #define double_int_zero (double_int::from_shwi (0))
211 #define double_int_one (double_int::from_shwi (1))
212 #define double_int_two (double_int::from_shwi (2))
213 #define double_int_ten (double_int::from_shwi (10))
215 /* Constructs double_int from unsigned integer CST. The bits over the
216 precision of HOST_WIDE_INT are filled with zeros. */
219 double_int
double_int::from_uhwi (unsigned HOST_WIDE_INT cst
)
227 /* FIXME(crowl): Remove after converting callers. */
228 static inline double_int
229 uhwi_to_double_int (unsigned HOST_WIDE_INT cst
)
231 return double_int::from_uhwi (cst
);
235 double_int::operator ++ ()
237 *this += double_int_one
;
242 double_int::operator -- ()
244 *this -= double_int_one
;
249 double_int::operator *= (double_int b
)
256 double_int::operator += (double_int b
)
263 double_int::operator -= (double_int b
)
269 /* Returns value of CST as a signed number. CST must satisfy
270 double_int::fits_signed. */
273 double_int::to_shwi () const
275 return (HOST_WIDE_INT
) low
;
278 /* FIXME(crowl): Remove after converting callers. */
279 static inline HOST_WIDE_INT
280 double_int_to_shwi (double_int cst
)
282 return cst
.to_shwi ();
285 /* Returns value of CST as an unsigned number. CST must satisfy
286 double_int::fits_unsigned. */
288 inline unsigned HOST_WIDE_INT
289 double_int::to_uhwi () const
294 /* FIXME(crowl): Remove after converting callers. */
295 static inline unsigned HOST_WIDE_INT
296 double_int_to_uhwi (double_int cst
)
298 return cst
.to_uhwi ();
301 /* Returns true if CST fits in unsigned HOST_WIDE_INT. */
304 double_int::fits_uhwi () const
309 /* FIXME(crowl): Remove after converting callers. */
311 double_int_fits_in_uhwi_p (double_int cst
)
313 return cst
.fits_uhwi ();
316 /* Returns true if CST fits in signed HOST_WIDE_INT. */
318 /* FIXME(crowl): Remove after converting callers. */
320 double_int_fits_in_shwi_p (double_int cst
)
322 return cst
.fits_shwi ();
325 /* FIXME(crowl): Remove after converting callers. */
327 double_int_fits_in_hwi_p (double_int cst
, bool uns
)
329 return cst
.fits_hwi (uns
);
332 /* The following operations perform arithmetics modulo 2^precision,
333 so you do not need to call double_int_ext between them, even if
334 you are representing numbers with precision less than
335 HOST_BITS_PER_DOUBLE_INT bits. */
337 /* FIXME(crowl): Remove after converting callers. */
339 double_int_mul (double_int a
, double_int b
)
344 /* FIXME(crowl): Remove after converting callers. */
346 double_int_mul_with_sign (double_int a
, double_int b
,
347 bool unsigned_p
, int *overflow
)
349 return a
.mul_with_sign (b
, unsigned_p
, overflow
);
352 /* FIXME(crowl): Remove after converting callers. */
354 double_int_add (double_int a
, double_int b
)
359 /* FIXME(crowl): Remove after converting callers. */
361 double_int_sub (double_int a
, double_int b
)
366 /* FIXME(crowl): Remove after converting callers. */
368 double_int_neg (double_int a
)
373 /* You must ensure that double_int_ext is called on the operands
374 of the following operations, if the precision of the numbers
375 is less than HOST_BITS_PER_DOUBLE_INT bits. */
377 /* FIXME(crowl): Remove after converting callers. */
379 double_int_div (double_int a
, double_int b
, bool uns
, unsigned code
)
381 return a
.div (b
, uns
, code
);
384 /* FIXME(crowl): Remove after converting callers. */
386 double_int_sdiv (double_int a
, double_int b
, unsigned code
)
388 return a
.sdiv (b
, code
);
391 /* FIXME(crowl): Remove after converting callers. */
393 double_int_udiv (double_int a
, double_int b
, unsigned code
)
395 return a
.udiv (b
, code
);
398 /* FIXME(crowl): Remove after converting callers. */
400 double_int_mod (double_int a
, double_int b
, bool uns
, unsigned code
)
402 return a
.mod (b
, uns
, code
);
405 /* FIXME(crowl): Remove after converting callers. */
407 double_int_smod (double_int a
, double_int b
, unsigned code
)
409 return a
.smod (b
, code
);
412 /* FIXME(crowl): Remove after converting callers. */
414 double_int_umod (double_int a
, double_int b
, unsigned code
)
416 return a
.umod (b
, code
);
419 /* FIXME(crowl): Remove after converting callers. */
421 double_int_divmod (double_int a
, double_int b
, bool uns
,
422 unsigned code
, double_int
*mod
)
424 return a
.divmod (b
, uns
, code
, mod
);
427 /* FIXME(crowl): Remove after converting callers. */
429 double_int_sdivmod (double_int a
, double_int b
, unsigned code
, double_int
*mod
)
431 return a
.sdivmod (b
, code
, mod
);
434 /* FIXME(crowl): Remove after converting callers. */
436 double_int_udivmod (double_int a
, double_int b
, unsigned code
, double_int
*mod
)
438 return a
.udivmod (b
, code
, mod
);
443 /* FIXME(crowl): Remove after converting callers. */
445 double_int_multiple_of (double_int product
, double_int factor
,
446 bool unsigned_p
, double_int
*multiple
)
448 return product
.multiple_of (factor
, unsigned_p
, multiple
);
451 /* FIXME(crowl): Remove after converting callers. */
453 double_int_setbit (double_int a
, unsigned bitpos
)
455 return a
.set_bit (bitpos
);
458 /* FIXME(crowl): Remove after converting callers. */
460 double_int_ctz (double_int a
)
462 return a
.trailing_zeros ();
465 /* Logical operations. */
470 double_int::operator ~ () const
478 /* FIXME(crowl): Remove after converting callers. */
479 static inline double_int
480 double_int_not (double_int a
)
488 double_int::operator | (double_int b
) const
491 result
.low
= low
| b
.low
;
492 result
.high
= high
| b
.high
;
496 /* FIXME(crowl): Remove after converting callers. */
497 static inline double_int
498 double_int_ior (double_int a
, double_int b
)
506 double_int::operator & (double_int b
) const
509 result
.low
= low
& b
.low
;
510 result
.high
= high
& b
.high
;
514 /* FIXME(crowl): Remove after converting callers. */
515 static inline double_int
516 double_int_and (double_int a
, double_int b
)
521 /* Returns A & ~B. */
524 double_int::and_not (double_int b
) const
527 result
.low
= low
& ~b
.low
;
528 result
.high
= high
& ~b
.high
;
532 /* FIXME(crowl): Remove after converting callers. */
533 static inline double_int
534 double_int_and_not (double_int a
, double_int b
)
536 return a
.and_not (b
);
542 double_int::operator ^ (double_int b
) const
545 result
.low
= low
^ b
.low
;
546 result
.high
= high
^ b
.high
;
550 /* FIXME(crowl): Remove after converting callers. */
551 static inline double_int
552 double_int_xor (double_int a
, double_int b
)
558 /* Shift operations. */
560 /* FIXME(crowl): Remove after converting callers. */
562 double_int_lshift (double_int a
, HOST_WIDE_INT count
, unsigned int prec
,
565 return a
.lshift (count
, prec
, arith
);
568 /* FIXME(crowl): Remove after converting callers. */
570 double_int_rshift (double_int a
, HOST_WIDE_INT count
, unsigned int prec
,
573 return a
.rshift (count
, prec
, arith
);
576 /* FIXME(crowl): Remove after converting callers. */
578 double_int_lrotate (double_int a
, HOST_WIDE_INT count
, unsigned int prec
)
580 return a
.lrotate (count
, prec
);
583 /* FIXME(crowl): Remove after converting callers. */
585 double_int_rrotate (double_int a
, HOST_WIDE_INT count
, unsigned int prec
)
587 return a
.rrotate (count
, prec
);
590 /* Returns true if CST is negative. Of course, CST is considered to
594 double_int_negative_p (double_int cst
)
599 /* FIXME(crowl): Remove after converting callers. */
601 double_int_cmp (double_int a
, double_int b
, bool uns
)
603 return a
.cmp (b
, uns
);
606 /* FIXME(crowl): Remove after converting callers. */
608 double_int_scmp (double_int a
, double_int b
)
613 /* FIXME(crowl): Remove after converting callers. */
615 double_int_ucmp (double_int a
, double_int b
)
620 /* FIXME(crowl): Remove after converting callers. */
622 double_int_max (double_int a
, double_int b
, bool uns
)
624 return a
.max (b
, uns
);
627 /* FIXME(crowl): Remove after converting callers. */
629 double_int_smax (double_int a
, double_int b
)
634 /* FIXME(crowl): Remove after converting callers. */
636 double_int_umax (double_int a
, double_int b
)
642 /* FIXME(crowl): Remove after converting callers. */
644 double_int_min (double_int a
, double_int b
, bool uns
)
646 return a
.min (b
, uns
);
649 /* FIXME(crowl): Remove after converting callers. */
651 double_int_smin (double_int a
, double_int b
)
656 /* FIXME(crowl): Remove after converting callers. */
658 double_int_umin (double_int a
, double_int b
)
663 void dump_double_int (FILE *, double_int
, bool);
665 /* Zero and sign extension of numbers in smaller precisions. */
667 /* FIXME(crowl): Remove after converting callers. */
669 double_int_ext (double_int a
, unsigned prec
, bool uns
)
671 return a
.ext (prec
, uns
);
674 /* FIXME(crowl): Remove after converting callers. */
676 double_int_sext (double_int a
, unsigned prec
)
678 return a
.sext (prec
);
681 /* FIXME(crowl): Remove after converting callers. */
683 double_int_zext (double_int a
, unsigned prec
)
685 return a
.zext (prec
);
688 /* FIXME(crowl): Remove after converting callers. */
690 double_int_mask (unsigned prec
)
692 return double_int::mask (prec
);
695 /* FIXME(crowl): Remove after converting callers. */
697 double_int_max_value (unsigned int prec
, bool uns
)
699 return double_int::max_value (prec
, uns
);
702 /* FIXME(crowl): Remove after converting callers. */
704 double_int_min_value (unsigned int prec
, bool uns
)
706 return double_int::min_value (prec
, uns
);
709 #define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
711 /* The operands of the following comparison functions must be processed
712 with double_int_ext, if their precision is less than
713 HOST_BITS_PER_DOUBLE_INT bits. */
715 /* Returns true if CST is zero. */
718 double_int::is_zero () const
720 return low
== 0 && high
== 0;
723 /* FIXME(crowl): Remove after converting callers. */
725 double_int_zero_p (double_int cst
)
727 return cst
.is_zero ();
730 /* Returns true if CST is one. */
733 double_int::is_one () const
735 return low
== 1 && high
== 0;
738 /* FIXME(crowl): Remove after converting callers. */
740 double_int_one_p (double_int cst
)
742 return cst
.is_one ();
745 /* Returns true if CST is minus one. */
748 double_int::is_minus_one () const
750 return low
== ALL_ONES
&& high
== -1;
753 /* FIXME(crowl): Remove after converting callers. */
755 double_int_minus_one_p (double_int cst
)
757 return cst
.is_minus_one ();
760 /* Returns true if CST is negative. */
763 double_int::is_negative () const
768 /* Returns true if CST1 == CST2. */
771 double_int::operator == (double_int cst2
) const
773 return low
== cst2
.low
&& high
== cst2
.high
;
776 /* FIXME(crowl): Remove after converting callers. */
778 double_int_equal_p (double_int cst1
, double_int cst2
)
783 /* Returns true if CST1 != CST2. */
786 double_int::operator != (double_int cst2
) const
788 return low
!= cst2
.low
|| high
!= cst2
.high
;
791 /* Return number of set bits of CST. */
794 double_int::popcount () const
796 return popcount_hwi (high
) + popcount_hwi (low
);
799 /* FIXME(crowl): Remove after converting callers. */
801 double_int_popcount (double_int cst
)
803 return cst
.popcount ();
807 /* Legacy interface with decomposed high/low parts. */
809 /* FIXME(crowl): Remove after converting callers. */
810 extern int add_double_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
811 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
812 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
814 /* FIXME(crowl): Remove after converting callers. */
815 #define add_double(l1,h1,l2,h2,lv,hv) \
816 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
817 /* FIXME(crowl): Remove after converting callers. */
818 extern int neg_double (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
819 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*);
820 /* FIXME(crowl): Remove after converting callers. */
821 extern int mul_double_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
822 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
823 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
825 /* FIXME(crowl): Remove after converting callers. */
826 extern int mul_double_wide_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
827 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
828 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
829 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
831 /* FIXME(crowl): Remove after converting callers. */
832 #define mul_double(l1,h1,l2,h2,lv,hv) \
833 mul_double_with_sign (l1, h1, l2, h2, lv, hv, false)
834 /* FIXME(crowl): Remove after converting callers. */
835 extern void lshift_double (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
836 HOST_WIDE_INT
, unsigned int,
837 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*, bool);
838 /* FIXME(crowl): Remove after converting callers. */
839 extern int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT
,
840 HOST_WIDE_INT
, unsigned HOST_WIDE_INT
,
841 HOST_WIDE_INT
, unsigned HOST_WIDE_INT
*,
842 HOST_WIDE_INT
*, unsigned HOST_WIDE_INT
*,
846 #ifndef GENERATOR_FILE
847 /* Conversion to and from GMP integer representations. */
849 void mpz_set_double_int (mpz_t
, double_int
, bool);
850 double_int
mpz_get_double_int (const_tree
, mpz_t
, bool);
853 #endif /* DOUBLE_INT_H */