re PR bootstrap/54281 (Fails to bootstrap with --disable-nls)
[official-gcc.git] / gcc / double-int.h
blob3d9aa2caa9deff76750e0e0b06da7d475c5d3768
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
9 later version.
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
14 for more details.
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/>. */
20 #ifndef DOUBLE_INT_H
21 #define DOUBLE_INT_H
23 #ifndef GENERATOR_FILE
24 #include <gmp.h>
25 #endif
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
55 public:
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;
177 HOST_WIDE_INT high;
179 } double_int;
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. */
188 inline
189 double_int double_int::from_shwi (HOST_WIDE_INT cst)
191 double_int r;
192 r.low = (unsigned HOST_WIDE_INT) cst;
193 r.high = cst < 0 ? -1 : 0;
194 return r;
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. */
218 inline
219 double_int double_int::from_uhwi (unsigned HOST_WIDE_INT cst)
221 double_int r;
222 r.low = cst;
223 r.high = 0;
224 return r;
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);
234 inline double_int &
235 double_int::operator ++ ()
237 *this += double_int_one;
238 return *this;
241 inline double_int &
242 double_int::operator -- ()
244 *this -= double_int_one;
245 return *this;
248 inline double_int &
249 double_int::operator *= (double_int b)
251 *this = *this * b;
252 return *this;
255 inline double_int &
256 double_int::operator += (double_int b)
258 *this = *this + b;
259 return *this;
262 inline double_int &
263 double_int::operator -= (double_int b)
265 *this = *this - b;
266 return *this;
269 /* Returns value of CST as a signed number. CST must satisfy
270 double_int::fits_signed. */
272 inline HOST_WIDE_INT
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
291 return low;
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. */
303 inline bool
304 double_int::fits_uhwi () const
306 return high == 0;
309 /* FIXME(crowl): Remove after converting callers. */
310 static inline bool
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. */
319 inline bool
320 double_int_fits_in_shwi_p (double_int cst)
322 return cst.fits_shwi ();
325 /* FIXME(crowl): Remove after converting callers. */
326 inline bool
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. */
338 inline double_int
339 double_int_mul (double_int a, double_int b)
341 return a * b;
344 /* FIXME(crowl): Remove after converting callers. */
345 inline double_int
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. */
353 inline double_int
354 double_int_add (double_int a, double_int b)
356 return a + b;
359 /* FIXME(crowl): Remove after converting callers. */
360 inline double_int
361 double_int_sub (double_int a, double_int b)
363 return a - b;
366 /* FIXME(crowl): Remove after converting callers. */
367 inline double_int
368 double_int_neg (double_int a)
370 return -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. */
378 inline double_int
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. */
385 inline double_int
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. */
392 inline double_int
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. */
399 inline double_int
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. */
406 inline double_int
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. */
413 inline double_int
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. */
420 inline double_int
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. */
428 inline double_int
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. */
435 inline double_int
436 double_int_udivmod (double_int a, double_int b, unsigned code, double_int *mod)
438 return a.udivmod (b, code, mod);
441 /***/
443 /* FIXME(crowl): Remove after converting callers. */
444 inline bool
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. */
452 inline double_int
453 double_int_setbit (double_int a, unsigned bitpos)
455 return a.set_bit (bitpos);
458 /* FIXME(crowl): Remove after converting callers. */
459 inline int
460 double_int_ctz (double_int a)
462 return a.trailing_zeros ();
465 /* Logical operations. */
467 /* Returns ~A. */
469 inline double_int
470 double_int::operator ~ () const
472 double_int result;
473 result.low = ~low;
474 result.high = ~high;
475 return result;
478 /* FIXME(crowl): Remove after converting callers. */
479 static inline double_int
480 double_int_not (double_int a)
482 return ~a;
485 /* Returns A | B. */
487 inline double_int
488 double_int::operator | (double_int b) const
490 double_int result;
491 result.low = low | b.low;
492 result.high = high | b.high;
493 return result;
496 /* FIXME(crowl): Remove after converting callers. */
497 static inline double_int
498 double_int_ior (double_int a, double_int b)
500 return a | b;
503 /* Returns A & B. */
505 inline double_int
506 double_int::operator & (double_int b) const
508 double_int result;
509 result.low = low & b.low;
510 result.high = high & b.high;
511 return result;
514 /* FIXME(crowl): Remove after converting callers. */
515 static inline double_int
516 double_int_and (double_int a, double_int b)
518 return a & b;
521 /* Returns A & ~B. */
523 inline double_int
524 double_int::and_not (double_int b) const
526 double_int result;
527 result.low = low & ~b.low;
528 result.high = high & ~b.high;
529 return result;
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);
539 /* Returns A ^ B. */
541 inline double_int
542 double_int::operator ^ (double_int b) const
544 double_int result;
545 result.low = low ^ b.low;
546 result.high = high ^ b.high;
547 return result;
550 /* FIXME(crowl): Remove after converting callers. */
551 static inline double_int
552 double_int_xor (double_int a, double_int b)
554 return a ^ b;
558 /* Shift operations. */
560 /* FIXME(crowl): Remove after converting callers. */
561 inline double_int
562 double_int_lshift (double_int a, HOST_WIDE_INT count, unsigned int prec,
563 bool arith)
565 return a.lshift (count, prec, arith);
568 /* FIXME(crowl): Remove after converting callers. */
569 inline double_int
570 double_int_rshift (double_int a, HOST_WIDE_INT count, unsigned int prec,
571 bool arith)
573 return a.rshift (count, prec, arith);
576 /* FIXME(crowl): Remove after converting callers. */
577 inline double_int
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. */
584 inline double_int
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
591 be signed. */
593 static inline bool
594 double_int_negative_p (double_int cst)
596 return cst.high < 0;
599 /* FIXME(crowl): Remove after converting callers. */
600 inline int
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. */
607 inline int
608 double_int_scmp (double_int a, double_int b)
610 return a.scmp (b);
613 /* FIXME(crowl): Remove after converting callers. */
614 inline int
615 double_int_ucmp (double_int a, double_int b)
617 return a.ucmp (b);
620 /* FIXME(crowl): Remove after converting callers. */
621 inline double_int
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. */
628 inline double_int
629 double_int_smax (double_int a, double_int b)
631 return a.smax (b);
634 /* FIXME(crowl): Remove after converting callers. */
635 inline double_int
636 double_int_umax (double_int a, double_int b)
638 return a.umax (b);
642 /* FIXME(crowl): Remove after converting callers. */
643 inline double_int
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. */
650 inline double_int
651 double_int_smin (double_int a, double_int b)
653 return a.smin (b);
656 /* FIXME(crowl): Remove after converting callers. */
657 inline double_int
658 double_int_umin (double_int a, double_int b)
660 return a.umin (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. */
668 inline double_int
669 double_int_ext (double_int a, unsigned prec, bool uns)
671 return a.ext (prec, uns);
674 /* FIXME(crowl): Remove after converting callers. */
675 inline double_int
676 double_int_sext (double_int a, unsigned prec)
678 return a.sext (prec);
681 /* FIXME(crowl): Remove after converting callers. */
682 inline double_int
683 double_int_zext (double_int a, unsigned prec)
685 return a.zext (prec);
688 /* FIXME(crowl): Remove after converting callers. */
689 inline double_int
690 double_int_mask (unsigned prec)
692 return double_int::mask (prec);
695 /* FIXME(crowl): Remove after converting callers. */
696 inline double_int
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. */
703 inline double_int
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. */
717 inline bool
718 double_int::is_zero () const
720 return low == 0 && high == 0;
723 /* FIXME(crowl): Remove after converting callers. */
724 static inline bool
725 double_int_zero_p (double_int cst)
727 return cst.is_zero ();
730 /* Returns true if CST is one. */
732 inline bool
733 double_int::is_one () const
735 return low == 1 && high == 0;
738 /* FIXME(crowl): Remove after converting callers. */
739 static inline bool
740 double_int_one_p (double_int cst)
742 return cst.is_one ();
745 /* Returns true if CST is minus one. */
747 inline bool
748 double_int::is_minus_one () const
750 return low == ALL_ONES && high == -1;
753 /* FIXME(crowl): Remove after converting callers. */
754 static inline bool
755 double_int_minus_one_p (double_int cst)
757 return cst.is_minus_one ();
760 /* Returns true if CST is negative. */
762 inline bool
763 double_int::is_negative () const
765 return high < 0;
768 /* Returns true if CST1 == CST2. */
770 inline bool
771 double_int::operator == (double_int cst2) const
773 return low == cst2.low && high == cst2.high;
776 /* FIXME(crowl): Remove after converting callers. */
777 static inline bool
778 double_int_equal_p (double_int cst1, double_int cst2)
780 return cst1 == cst2;
783 /* Returns true if CST1 != CST2. */
785 inline bool
786 double_int::operator != (double_int cst2) const
788 return low != cst2.low || high != cst2.high;
791 /* Return number of set bits of CST. */
793 inline int
794 double_int::popcount () const
796 return popcount_hwi (high) + popcount_hwi (low);
799 /* FIXME(crowl): Remove after converting callers. */
800 static inline int
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 *,
813 bool);
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 *,
824 bool);
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 *,
830 bool);
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 *,
843 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);
851 #endif
853 #endif /* DOUBLE_INT_H */