double-int.h 12.3 KB
Newer Older
1
/* Operations with long integers.
2
   Copyright (C) 2006-2013 Free Software Foundation, Inc.
H.J. Lu committed
3

4
This file is part of GCC.
H.J. Lu committed
5

6 7
GCC is free software; you can redistribute it and/or modify it
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.
H.J. Lu committed
10

11 12 13 14
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
H.J. Lu committed
15

16
You should have received a copy of the GNU General Public License
17 18
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#ifndef DOUBLE_INT_H
#define DOUBLE_INT_H

/* A large integer is currently represented as a pair of HOST_WIDE_INTs.
   It therefore represents a number with precision of
   2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
   internal representation will change, if numbers with greater precision
   are needed, so the users should not rely on it).  The representation does
   not contain any information about signedness of the represented value, so
   it can be used to represent both signed and unsigned numbers.  For
   operations where the results depend on signedness (division, comparisons),
   it must be specified separately.  For each such operation, there are three
   versions of the function -- double_int_op, that takes an extra UNS argument
   giving the signedness of the values, and double_int_sop and double_int_uop
   that stand for its specializations for signed and unsigned values.

   You may also represent with numbers in smaller precision using double_int.
   You however need to use double_int_ext (that fills in the bits of the
   number over the prescribed precision with zeros or with the sign bit) before
   operations that do not perform arithmetics modulo 2^precision (comparisons,
   division), and possibly before storing the results, if you want to keep
   them in some canonical form).  In general, the signedness of double_int_ext
   should match the signedness of the operation.

   ??? The components of double_int differ in signedness mostly for
   historical reasons (they replace an older structure used to represent
46
   numbers with precision higher than HOST_WIDE_INT).  It might be less
47 48
   confusing to have them both signed or both unsigned.  */

49
struct double_int
50
{
51 52 53 54 55 56 57
  /* 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.  */

58 59
  static double_int from_uhwi (unsigned HOST_WIDE_INT cst);
  static double_int from_shwi (HOST_WIDE_INT cst);
60
  static double_int from_pair (HOST_WIDE_INT high, unsigned HOST_WIDE_INT low);
61

62 63 64 65
  /* Construct from a fuffer of length LEN.  BUFFER will be read according
     to byte endianess and word endianess.  */
  static double_int from_buffer (const unsigned char *buffer, int len);

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  /* 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);
81 82 83
  double_int &operator &= (double_int);
  double_int &operator ^= (double_int);
  double_int &operator |= (double_int);
84 85 86 87 88

  /* The following functions are non-mutating operations.  */

  /* Conversion functions.  */

89 90
  HOST_WIDE_INT to_shwi () const;
  unsigned HOST_WIDE_INT to_uhwi () const;
91 92 93

  /* Conversion query functions.  */

94 95 96
  bool fits_uhwi () const;
  bool fits_shwi () const;
  bool fits_hwi (bool uns) const;
97 98 99 100 101 102 103 104 105 106 107 108

  /* 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.  */

109 110 111 112
  /* The following operations perform arithmetics modulo 2^precision, so you
     do not need to call .ext between them, even if you are representing
     numbers with precision less than HOST_BITS_PER_DOUBLE_INT bits.  */

113
  double_int set_bit (unsigned) const;
114
  double_int mul_with_sign (double_int, bool unsigned_p, bool *overflow) const;
115 116
  double_int wide_mul_with_sign (double_int, bool unsigned_p,
				 double_int *higher, bool *overflow) const;
117
  double_int add_with_sign (double_int, bool unsigned_p, bool *overflow) const;
118 119
  double_int sub_with_overflow (double_int, bool *overflow) const;
  double_int neg_with_overflow (bool *overflow) const;
120

121 122 123
  double_int operator * (double_int) const;
  double_int operator + (double_int) const;
  double_int operator - (double_int) const;
124 125
  double_int operator - () const;
  double_int operator ~ () const;
126 127 128 129
  double_int operator & (double_int) const;
  double_int operator | (double_int) const;
  double_int operator ^ (double_int) const;
  double_int and_not (double_int) const;
130

131
  double_int lshift (HOST_WIDE_INT count) const;
132
  double_int lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const;
133
  double_int rshift (HOST_WIDE_INT count) const;
134 135 136 137 138 139 140 141 142 143 144
  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.  */
145

146 147 148 149 150 151
  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;
152 153
  double_int divmod_with_overflow (double_int, bool, unsigned,
				   double_int *, bool *) const;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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;
176
  bool ule (double_int b) const;
177 178
  bool ugt (double_int b) const;
  bool slt (double_int b) const;
179
  bool sle (double_int b) const;
180 181 182 183 184 185 186 187 188 189 190 191 192
  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;

Ondřej Bílka committed
193
  /* Please migrate away from using these member variables publicly.  */
194

195 196
  unsigned HOST_WIDE_INT low;
  HOST_WIDE_INT high;
197

198
};
199

200 201
#define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)

202 203 204 205 206
/* Constructors and conversions.  */

/* Constructs double_int from integer CST.  The bits over the precision of
   HOST_WIDE_INT are filled with the sign bit.  */

207 208
inline double_int
double_int::from_shwi (HOST_WIDE_INT cst)
209 210 211 212 213 214 215 216
{
  double_int r;
  r.low = (unsigned HOST_WIDE_INT) cst;
  r.high = cst < 0 ? -1 : 0;
  return r;
}

/* Some useful constants.  */
217 218 219
/* 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.  */
220

221 222 223 224 225
#define double_int_minus_one (double_int::from_shwi (-1))
#define double_int_zero (double_int::from_shwi (0))
#define double_int_one (double_int::from_shwi (1))
#define double_int_two (double_int::from_shwi (2))
#define double_int_ten (double_int::from_shwi (10))
226 227 228 229

/* Constructs double_int from unsigned integer CST.  The bits over the
   precision of HOST_WIDE_INT are filled with zeros.  */

230 231
inline double_int
double_int::from_uhwi (unsigned HOST_WIDE_INT cst)
232 233 234 235 236 237 238
{
  double_int r;
  r.low = cst;
  r.high = 0;
  return r;
}

239 240
inline double_int
double_int::from_pair (HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
241
{
242 243 244 245
  double_int r;
  r.low = low;
  r.high = high;
  return r;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

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 &
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
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;
}

283
/* Returns value of CST as a signed number.  CST must satisfy
284
   double_int::fits_signed.  */
285

286
inline HOST_WIDE_INT
287
double_int::to_shwi () const
288 289 290 291
{
  return (HOST_WIDE_INT) low;
}

292
/* Returns value of CST as an unsigned number.  CST must satisfy
293 294 295
   double_int::fits_unsigned.  */

inline unsigned HOST_WIDE_INT
296
double_int::to_uhwi () const
297 298 299
{
  return low;
}
300 301 302

/* Returns true if CST fits in unsigned HOST_WIDE_INT.  */

303
inline bool
304
double_int::fits_uhwi () const
305 306 307 308
{
  return high == 0;
}

309
/* Logical operations.  */
310 311 312

/* Returns ~A.  */

313 314 315 316 317 318 319 320 321
inline double_int
double_int::operator ~ () const
{
  double_int result;
  result.low = ~low;
  result.high = ~high;
  return result;
}

322 323
/* Returns A | B.  */

324 325 326 327 328 329 330 331 332
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;
}

333 334
/* Returns A & B.  */

335 336 337 338 339 340 341 342 343
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;
}

344 345
/* Returns A & ~B.  */

346 347 348 349 350 351 352 353 354
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;
}

355 356
/* Returns A ^ B.  */

357 358 359 360 361 362 363 364 365
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;
}

366 367 368 369 370 371
void dump_double_int (FILE *, double_int, bool);

#define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))

/* The operands of the following comparison functions must be processed
   with double_int_ext, if their precision is less than
372
   HOST_BITS_PER_DOUBLE_INT bits.  */
373 374 375

/* Returns true if CST is zero.  */

376 377 378 379 380 381
inline bool
double_int::is_zero () const
{
  return low == 0 && high == 0;
}

382 383
/* Returns true if CST is one.  */

384 385 386 387 388 389
inline bool
double_int::is_one () const
{
  return low == 1 && high == 0;
}

390 391
/* Returns true if CST is minus one.  */

392 393 394 395 396 397 398 399 400 401 402 403
inline bool
double_int::is_minus_one () const
{
  return low == ALL_ONES && high == -1;
}

/* Returns true if CST is negative.  */

inline bool
double_int::is_negative () const
{
  return high < 0;
404 405 406 407
}

/* Returns true if CST1 == CST2.  */

408 409 410 411 412 413 414 415 416 417 418 419
inline bool
double_int::operator == (double_int cst2) const
{
  return low == cst2.low && high == cst2.high;
}

/* Returns true if CST1 != CST2.  */

inline bool
double_int::operator != (double_int cst2) const
{
  return low != cst2.low || high != cst2.high;
420 421
}

422 423
/* Return number of set bits of CST.  */

424 425 426 427 428 429
inline int
double_int::popcount () const
{
  return popcount_hwi (high) + popcount_hwi (low);
}

430

431
#ifndef GENERATOR_FILE
432 433 434
/* Conversion to and from GMP integer representations.  */

void mpz_set_double_int (mpz_t, double_int, bool);
435
double_int mpz_get_double_int (const_tree, mpz_t, bool);
436
#endif
437

438
#endif /* DOUBLE_INT_H */