diff options
Diffstat (limited to 'gcc/double-int.h')
-rw-r--r-- | gcc/double-int.h | 675 |
1 files changed, 597 insertions, 78 deletions
diff --git a/gcc/double-int.h b/gcc/double-int.h index 4c4c1b3ecbd..9bbf644603c 100644 --- a/gcc/double-int.h +++ b/gcc/double-int.h @@ -50,10 +50,132 @@ along with GCC; see the file COPYING3. If not see numbers with precision higher than HOST_WIDE_INT). It might be less confusing to have them both signed or both unsigned. */ -typedef struct +typedef struct double_int { +public: + /* Normally, we would define constructors to create instances. + Two things prevent us from doing so. + First, defining a constructor makes the class non-POD in C++03, + and we certainly want double_int to be a POD. + Second, the GCC conding conventions prefer explicit conversion, + and explicit conversion operators are not available until C++11. */ + + static double_int from_unsigned (unsigned HOST_WIDE_INT cst); + static double_int from_signed (HOST_WIDE_INT cst); + + /* No copy assignment operator or destructor to keep the type a POD. */ + + /* There are some special value-creation static member functions. */ + + static double_int mask (unsigned prec); + static double_int max_value (unsigned int prec, bool uns); + static double_int min_value (unsigned int prec, bool uns); + + /* The following functions are mutating operations. */ + + double_int &operator ++ (); // prefix + double_int &operator -- (); // prefix + double_int &operator *= (double_int); + double_int &operator += (double_int); + double_int &operator -= (double_int); + + /* The following functions are non-mutating operations. */ + + /* Conversion functions. */ + + HOST_WIDE_INT to_signed () const; + unsigned HOST_WIDE_INT to_unsigned () const; + + /* Conversion query functions. */ + + bool fits_unsigned () const; + bool fits_signed () const; + bool fits (bool uns) const; + + /* Attribute query functions. */ + + int trailing_zeros () const; + int popcount () const; + + /* Arithmetic query operations. */ + + bool multiple_of (double_int, bool, double_int *) const; + + /* Arithmetic operation functions. */ + + double_int set_bit (unsigned) const; + double_int mul_with_sign (double_int, bool, int *) const; + + double_int operator * (double_int b) const; + double_int operator + (double_int b) const; + double_int operator - (double_int b) const; + double_int operator - () const; + double_int operator ~ () const; + double_int operator & (double_int b) const; + double_int operator | (double_int b) const; + double_int operator ^ (double_int b) const; + double_int and_not (double_int b) const; + + double_int lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const; + double_int rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const; + double_int alshift (HOST_WIDE_INT count, unsigned int prec) const; + double_int arshift (HOST_WIDE_INT count, unsigned int prec) const; + double_int llshift (HOST_WIDE_INT count, unsigned int prec) const; + double_int lrshift (HOST_WIDE_INT count, unsigned int prec) const; + double_int lrotate (HOST_WIDE_INT count, unsigned int prec) const; + double_int rrotate (HOST_WIDE_INT count, unsigned int prec) const; + + /* You must ensure that double_int::ext is called on the operands + of the following operations, if the precision of the numbers + is less than HOST_BITS_PER_DOUBLE_INT bits. */ + double_int div (double_int, bool, unsigned) const; + double_int sdiv (double_int, unsigned) const; + double_int udiv (double_int, unsigned) const; + double_int mod (double_int, bool, unsigned) const; + double_int smod (double_int, unsigned) const; + double_int umod (double_int, unsigned) const; + double_int divmod (double_int, bool, unsigned, double_int *) const; + double_int sdivmod (double_int, unsigned, double_int *) const; + double_int udivmod (double_int, unsigned, double_int *) const; + + /* Precision control functions. */ + + double_int ext (unsigned prec, bool uns) const; + double_int zext (unsigned prec) const; + double_int sext (unsigned prec) const; + + /* Comparative functions. */ + + bool is_zero () const; + bool is_one () const; + bool is_minus_one () const; + bool is_negative () const; + + int cmp (double_int b, bool uns) const; + int ucmp (double_int b) const; + int scmp (double_int b) const; + + bool ult (double_int b) const; + bool ugt (double_int b) const; + bool slt (double_int b) const; + bool sgt (double_int b) const; + + double_int max (double_int b, bool uns); + double_int smax (double_int b); + double_int umax (double_int b); + + double_int min (double_int b, bool uns); + double_int smin (double_int b); + double_int umin (double_int b); + + bool operator == (double_int cst2) const; + bool operator != (double_int cst2) const; + + /* Please migrate away from using these member variables publically. */ + unsigned HOST_WIDE_INT low; HOST_WIDE_INT high; + } double_int; #define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT) @@ -63,66 +185,148 @@ typedef struct /* Constructs double_int from integer CST. The bits over the precision of HOST_WIDE_INT are filled with the sign bit. */ -static inline double_int -shwi_to_double_int (HOST_WIDE_INT cst) +inline +double_int double_int::from_signed (HOST_WIDE_INT cst) { double_int r; - r.low = (unsigned HOST_WIDE_INT) cst; r.high = cst < 0 ? -1 : 0; - return r; } +/* FIXME(crowl): Remove after converting callers. */ +static inline double_int +shwi_to_double_int (HOST_WIDE_INT cst) +{ + return double_int::from_signed (cst); +} + /* Some useful constants. */ +/* FIXME(crowl): Maybe remove after converting callers? + The problem is that a named constant would not be as optimizable, + while the functional syntax is more verbose. */ -#define double_int_minus_one (shwi_to_double_int (-1)) -#define double_int_zero (shwi_to_double_int (0)) -#define double_int_one (shwi_to_double_int (1)) -#define double_int_two (shwi_to_double_int (2)) -#define double_int_ten (shwi_to_double_int (10)) +#define double_int_minus_one (double_int::from_signed (-1)) +#define double_int_zero (double_int::from_signed (0)) +#define double_int_one (double_int::from_signed (1)) +#define double_int_two (double_int::from_signed (2)) +#define double_int_ten (double_int::from_signed (10)) /* Constructs double_int from unsigned integer CST. The bits over the precision of HOST_WIDE_INT are filled with zeros. */ -static inline double_int -uhwi_to_double_int (unsigned HOST_WIDE_INT cst) +inline +double_int double_int::from_unsigned (unsigned HOST_WIDE_INT cst) { double_int r; - r.low = cst; r.high = 0; - return r; } +/* FIXME(crowl): Remove after converting callers. */ +static inline double_int +uhwi_to_double_int (unsigned HOST_WIDE_INT cst) +{ + return double_int::from_unsigned (cst); +} + +inline double_int & +double_int::operator ++ () +{ + *this += double_int_one; + return *this; +} + +inline double_int & +double_int::operator -- () +{ + *this -= double_int_one; + return *this; +} + +inline double_int & +double_int::operator *= (double_int b) +{ + *this = *this * b; + return *this; +} + +inline double_int & +double_int::operator += (double_int b) +{ + *this = *this + b; + return *this; +} + +inline double_int & +double_int::operator -= (double_int b) +{ + *this = *this - b; + return *this; +} + /* Returns value of CST as a signed number. CST must satisfy - double_int_fits_in_shwi_p. */ + double_int::fits_signed. */ +inline HOST_WIDE_INT +double_int::to_signed () const +{ + return (HOST_WIDE_INT) low; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline HOST_WIDE_INT double_int_to_shwi (double_int cst) { - return (HOST_WIDE_INT) cst.low; + return cst.to_signed (); } /* Returns value of CST as an unsigned number. CST must satisfy - double_int_fits_in_uhwi_p. */ + double_int::fits_unsigned. */ + +inline unsigned HOST_WIDE_INT +double_int::to_unsigned () const +{ + return low; +} +/* FIXME(crowl): Remove after converting callers. */ static inline unsigned HOST_WIDE_INT double_int_to_uhwi (double_int cst) { - return cst.low; + return cst.to_unsigned (); } -bool double_int_fits_in_hwi_p (double_int, bool); -bool double_int_fits_in_shwi_p (double_int); - /* Returns true if CST fits in unsigned HOST_WIDE_INT. */ +inline bool +double_int::fits_unsigned () const +{ + return high == 0; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline bool double_int_fits_in_uhwi_p (double_int cst) { - return cst.high == 0; + return cst.fits_unsigned (); +} + +/* Returns true if CST fits in signed HOST_WIDE_INT. */ + +/* FIXME(crowl): Remove after converting callers. */ +inline bool +double_int_fits_in_shwi_p (double_int cst) +{ + return cst.fits_signed (); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline bool +double_int_fits_in_hwi_p (double_int cst, bool uns) +{ + return cst.fits (uns); } /* The following operations perform arithmetics modulo 2^precision, @@ -130,88 +334,258 @@ double_int_fits_in_uhwi_p (double_int cst) you are representing numbers with precision less than HOST_BITS_PER_DOUBLE_INT bits. */ -double_int double_int_mul (double_int, double_int); -double_int double_int_mul_with_sign (double_int, double_int, bool, int *); -double_int double_int_add (double_int, double_int); -double_int double_int_sub (double_int, double_int); -double_int double_int_neg (double_int); +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_mul (double_int a, double_int b) +{ + return a * b; +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_mul_with_sign (double_int a, double_int b, + bool unsigned_p, int *overflow) +{ + return a.mul_with_sign (b, unsigned_p, overflow); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_add (double_int a, double_int b) +{ + return a + b; +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_sub (double_int a, double_int b) +{ + return a - b; +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_neg (double_int a) +{ + return -a; +} /* You must ensure that double_int_ext is called on the operands of the following operations, if the precision of the numbers is less than HOST_BITS_PER_DOUBLE_INT bits. */ -double_int double_int_div (double_int, double_int, bool, unsigned); -double_int double_int_sdiv (double_int, double_int, unsigned); -double_int double_int_udiv (double_int, double_int, unsigned); -double_int double_int_mod (double_int, double_int, bool, unsigned); -double_int double_int_smod (double_int, double_int, unsigned); -double_int double_int_umod (double_int, double_int, unsigned); -double_int double_int_divmod (double_int, double_int, bool, unsigned, double_int *); -double_int double_int_sdivmod (double_int, double_int, unsigned, double_int *); -double_int double_int_udivmod (double_int, double_int, unsigned, double_int *); -bool double_int_multiple_of (double_int, double_int, bool, double_int *); +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_div (double_int a, double_int b, bool uns, unsigned code) +{ + return a.div (b, uns, code); +} -double_int double_int_setbit (double_int, unsigned); -int double_int_ctz (double_int); +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_sdiv (double_int a, double_int b, unsigned code) +{ + return a.sdiv (b, code); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_udiv (double_int a, double_int b, unsigned code) +{ + return a.udiv (b, code); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_mod (double_int a, double_int b, bool uns, unsigned code) +{ + return a.mod (b, uns, code); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_smod (double_int a, double_int b, unsigned code) +{ + return a.smod (b, code); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_umod (double_int a, double_int b, unsigned code) +{ + return a.umod (b, code); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_divmod (double_int a, double_int b, bool uns, + unsigned code, double_int *mod) +{ + return a.divmod (b, uns, code, mod); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_sdivmod (double_int a, double_int b, unsigned code, double_int *mod) +{ + return a.sdivmod (b, code, mod); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_udivmod (double_int a, double_int b, unsigned code, double_int *mod) +{ + return a.udivmod (b, code, mod); +} + +/***/ + +/* FIXME(crowl): Remove after converting callers. */ +inline bool +double_int_multiple_of (double_int product, double_int factor, + bool unsigned_p, double_int *multiple) +{ + return product.multiple_of (factor, unsigned_p, multiple); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_setbit (double_int a, unsigned bitpos) +{ + return a.set_bit (bitpos); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline int +double_int_ctz (double_int a) +{ + return a.trailing_zeros (); +} /* Logical operations. */ /* Returns ~A. */ +inline double_int +double_int::operator ~ () const +{ + double_int result; + result.low = ~low; + result.high = ~high; + return result; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline double_int double_int_not (double_int a) { - a.low = ~a.low; - a.high = ~a.high; - return a; + return ~a; } /* Returns A | B. */ +inline double_int +double_int::operator | (double_int b) const +{ + double_int result; + result.low = low | b.low; + result.high = high | b.high; + return result; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline double_int double_int_ior (double_int a, double_int b) { - a.low |= b.low; - a.high |= b.high; - return a; + return a | b; } /* Returns A & B. */ +inline double_int +double_int::operator & (double_int b) const +{ + double_int result; + result.low = low & b.low; + result.high = high & b.high; + return result; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline double_int double_int_and (double_int a, double_int b) { - a.low &= b.low; - a.high &= b.high; - return a; + return a & b; } /* Returns A & ~B. */ +inline double_int +double_int::and_not (double_int b) const +{ + double_int result; + result.low = low & ~b.low; + result.high = high & ~b.high; + return result; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline double_int double_int_and_not (double_int a, double_int b) { - a.low &= ~b.low; - a.high &= ~b.high; - return a; + return a.and_not (b); } /* Returns A ^ B. */ +inline double_int +double_int::operator ^ (double_int b) const +{ + double_int result; + result.low = low ^ b.low; + result.high = high ^ b.high; + return result; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline double_int double_int_xor (double_int a, double_int b) { - a.low ^= b.low; - a.high ^= b.high; - return a; + return a ^ b; } /* Shift operations. */ -double_int double_int_lshift (double_int, HOST_WIDE_INT, unsigned int, bool); -double_int double_int_rshift (double_int, HOST_WIDE_INT, unsigned int, bool); -double_int double_int_lrotate (double_int, HOST_WIDE_INT, unsigned int); -double_int double_int_rrotate (double_int, HOST_WIDE_INT, unsigned int); + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_lshift (double_int a, HOST_WIDE_INT count, unsigned int prec, + bool arith) +{ + return a.lshift (count, prec, arith); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_rshift (double_int a, HOST_WIDE_INT count, unsigned int prec, + bool arith) +{ + return a.rshift (count, prec, arith); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_lrotate (double_int a, HOST_WIDE_INT count, unsigned int prec) +{ + return a.lrotate (count, prec); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_rrotate (double_int a, HOST_WIDE_INT count, unsigned int prec) +{ + return a.rrotate (count, prec); +} /* Returns true if CST is negative. Of course, CST is considered to be signed. */ @@ -222,29 +596,115 @@ double_int_negative_p (double_int cst) return cst.high < 0; } -int double_int_cmp (double_int, double_int, bool); -int double_int_scmp (double_int, double_int); -int double_int_ucmp (double_int, double_int); +/* FIXME(crowl): Remove after converting callers. */ +inline int +double_int_cmp (double_int a, double_int b, bool uns) +{ + return a.cmp (b, uns); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline int +double_int_scmp (double_int a, double_int b) +{ + return a.scmp (b); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline int +double_int_ucmp (double_int a, double_int b) +{ + return a.ucmp (b); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_max (double_int a, double_int b, bool uns) +{ + return a.max (b, uns); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_smax (double_int a, double_int b) +{ + return a.smax (b); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_umax (double_int a, double_int b) +{ + return a.umax (b); +} -double_int double_int_max (double_int, double_int, bool); -double_int double_int_smax (double_int, double_int); -double_int double_int_umax (double_int, double_int); -double_int double_int_min (double_int, double_int, bool); -double_int double_int_smin (double_int, double_int); -double_int double_int_umin (double_int, double_int); +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_min (double_int a, double_int b, bool uns) +{ + return a.min (b, uns); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_smin (double_int a, double_int b) +{ + return a.smin (b); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_umin (double_int a, double_int b) +{ + return a.umin (b); +} void dump_double_int (FILE *, double_int, bool); /* Zero and sign extension of numbers in smaller precisions. */ -double_int double_int_ext (double_int, unsigned, bool); -double_int double_int_sext (double_int, unsigned); -double_int double_int_zext (double_int, unsigned); -double_int double_int_mask (unsigned); +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_ext (double_int a, unsigned prec, bool uns) +{ + return a.ext (prec, uns); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_sext (double_int a, unsigned prec) +{ + return a.sext (prec); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_zext (double_int a, unsigned prec) +{ + return a.zext (prec); +} -double_int double_int_max_value (unsigned int, bool); -double_int double_int_min_value (unsigned int, bool); +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_mask (unsigned prec) +{ + return double_int::mask (prec); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_max_value (unsigned int prec, bool uns) +{ + return double_int::max_value (prec, uns); +} + +/* FIXME(crowl): Remove after converting callers. */ +inline double_int +double_int_min_value (unsigned int prec, bool uns) +{ + return double_int::min_value (prec, uns); +} #define ALL_ONES (~((unsigned HOST_WIDE_INT) 0)) @@ -254,69 +714,128 @@ double_int double_int_min_value (unsigned int, bool); /* Returns true if CST is zero. */ +inline bool +double_int::is_zero () const +{ + return low == 0 && high == 0; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline bool double_int_zero_p (double_int cst) { - return cst.low == 0 && cst.high == 0; + return cst.is_zero (); } /* Returns true if CST is one. */ +inline bool +double_int::is_one () const +{ + return low == 1 && high == 0; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline bool double_int_one_p (double_int cst) { - return cst.low == 1 && cst.high == 0; + return cst.is_one (); } /* Returns true if CST is minus one. */ +inline bool +double_int::is_minus_one () const +{ + return low == ALL_ONES && high == -1; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline bool double_int_minus_one_p (double_int cst) { - return (cst.low == ALL_ONES && cst.high == -1); + return cst.is_minus_one (); +} + +/* Returns true if CST is negative. */ + +inline bool +double_int::is_negative () const +{ + return high < 0; } /* Returns true if CST1 == CST2. */ +inline bool +double_int::operator == (double_int cst2) const +{ + return low == cst2.low && high == cst2.high; +} + +/* FIXME(crowl): Remove after converting callers. */ static inline bool double_int_equal_p (double_int cst1, double_int cst2) { - return cst1.low == cst2.low && cst1.high == cst2.high; + return cst1 == cst2; +} + +/* Returns true if CST1 != CST2. */ + +inline bool +double_int::operator != (double_int cst2) const +{ + return low != cst2.low || high != cst2.high; } /* Return number of set bits of CST. */ +inline int +double_int::popcount () const +{ + return popcount_hwi (high) + popcount_hwi (low); +} + +/* FIXME(crowl): Remove after converting callers. */ static inline int double_int_popcount (double_int cst) { - return popcount_hwi (cst.high) + popcount_hwi (cst.low); + return cst.popcount (); } /* Legacy interface with decomposed high/low parts. */ +/* FIXME(crowl): Remove after converting callers. */ extern int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool); +/* FIXME(crowl): Remove after converting callers. */ #define add_double(l1,h1,l2,h2,lv,hv) \ add_double_with_sign (l1, h1, l2, h2, lv, hv, false) +/* FIXME(crowl): Remove after converting callers. */ extern int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *); +/* FIXME(crowl): Remove after converting callers. */ extern int mul_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool); +/* FIXME(crowl): Remove after converting callers. */ extern int mul_double_wide_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool); +/* FIXME(crowl): Remove after converting callers. */ #define mul_double(l1,h1,l2,h2,lv,hv) \ mul_double_with_sign (l1, h1, l2, h2, lv, hv, false) +/* FIXME(crowl): Remove after converting callers. */ extern void lshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT, unsigned int, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool); +/* FIXME(crowl): Remove after converting callers. */ extern int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT, HOST_WIDE_INT, unsigned HOST_WIDE_INT *, |