1172302Spjd/* Operations with long integers.
2172302Spjd   Copyright (C) 2006 Free Software Foundation, Inc.
3172302Spjd
4172302SpjdThis file is part of GCC.
5172302Spjd
6172302SpjdGCC is free software; you can redistribute it and/or modify it
7172302Spjdunder the terms of the GNU General Public License as published by the
8172302SpjdFree Software Foundation; either version 2, or (at your option) any
9172302Spjdlater version.
10172302Spjd
11172302SpjdGCC is distributed in the hope that it will be useful, but WITHOUT
12172302SpjdANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13172302SpjdFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14172302Spjdfor more details.
15172302Spjd
16172302SpjdYou should have received a copy of the GNU General Public License
17172302Spjdalong with GCC; see the file COPYING.  If not, write to the Free
18172302SpjdSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19172302Spjd02110-1301, USA.  */
20172302Spjd
21172302Spjd#ifndef DOUBLE_INT_H
22172302Spjd#define DOUBLE_INT_H
23172302Spjd
24172302Spjd/* A large integer is currently represented as a pair of HOST_WIDE_INTs.
25172302Spjd   It therefore represents a number with precision of
26172302Spjd   2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
27172302Spjd   internal representation will change, if numbers with greater precision
28172302Spjd   are needed, so the users should not rely on it).  The representation does
29172302Spjd   not contain any information about signedness of the represented value, so
30172302Spjd   it can be used to represent both signed and unsigned numbers.  For
31172302Spjd   operations where the results depend on signedness (division, comparisons),
32172302Spjd   it must be specified separately.  For each such operation, there are three
33172302Spjd   versions of the function -- double_int_op, that takes an extra UNS argument
34172302Spjd   giving the signedness of the values, and double_int_sop and double_int_uop
35172302Spjd   that stand for its specializations for signed and unsigned values.
36172302Spjd
37172302Spjd   You may also represent with numbers in smaller precision using double_int.
38172302Spjd   You however need to use double_int_ext (that fills in the bits of the
39172302Spjd   number over the prescribed precision with zeros or with the sign bit) before
40172302Spjd   operations that do not perform arithmetics modulo 2^precision (comparisons,
41172302Spjd   division), and possibly before storing the results, if you want to keep
42172302Spjd   them in some canonical form).  In general, the signedness of double_int_ext
43172302Spjd   should match the signedness of the operation.
44172302Spjd
45172302Spjd   ??? The components of double_int differ in signedness mostly for
46172302Spjd   historical reasons (they replace an older structure used to represent
47172302Spjd   numbers with precision higher than HOST_WIDE_INT).  It might be less
48172302Spjd   confusing to have them both signed or both unsigned.  */
49172302Spjd
50172302Spjdtypedef struct
51172302Spjd{
52172302Spjd  unsigned HOST_WIDE_INT low;
53172302Spjd  HOST_WIDE_INT high;
54172302Spjd} double_int;
55172302Spjd
56172302Spjdunion tree_node;
57172302Spjd
58172302Spjd/* Constructors and conversions.  */
59172302Spjd
60172302Spjdunion tree_node *double_int_to_tree (union tree_node *, double_int);
61172302Spjddouble_int tree_to_double_int (union tree_node *tree);
62172302Spjd
63172302Spjd/* Constructs double_int from integer CST.  The bits over the precision of
64172302Spjd   HOST_WIDE_INT are filled with the sign bit.  */
65172302Spjd
66172302Spjdstatic inline double_int
67172302Spjdshwi_to_double_int (HOST_WIDE_INT cst)
68172302Spjd{
69172302Spjd  double_int r;
70172302Spjd
71172302Spjd  r.low = (unsigned HOST_WIDE_INT) cst;
72172302Spjd  r.high = cst < 0 ? -1 : 0;
73172302Spjd
74172302Spjd  return r;
75172302Spjd}
76172302Spjd
77172302Spjd/* Some useful constants.  */
78172302Spjd
79172302Spjd#define double_int_minus_one (shwi_to_double_int (-1))
80172302Spjd#define double_int_zero (shwi_to_double_int (0))
81172302Spjd#define double_int_one (shwi_to_double_int (1))
82172302Spjd#define double_int_two (shwi_to_double_int (2))
83172302Spjd#define double_int_ten (shwi_to_double_int (10))
84172302Spjd
85172302Spjd/* Constructs double_int from unsigned integer CST.  The bits over the
86172302Spjd   precision of HOST_WIDE_INT are filled with zeros.  */
87172302Spjd
88172302Spjdstatic inline double_int
89172302Spjduhwi_to_double_int (unsigned HOST_WIDE_INT cst)
90172302Spjd{
91172302Spjd  double_int r;
92172302Spjd
93172302Spjd  r.low = cst;
94172302Spjd  r.high = 0;
95172302Spjd
96172302Spjd  return r;
97172302Spjd}
98172302Spjd
99172302Spjd/* The following operations perform arithmetics modulo 2^precision,
100172302Spjd   so you do not need to call double_int_ext between them, even if
101172302Spjd   you are representing numbers with precision less than
102172302Spjd   2 * HOST_BITS_PER_WIDE_INT bits.  */
103172302Spjd
104172302Spjddouble_int double_int_mul (double_int, double_int);
105172302Spjddouble_int double_int_add (double_int, double_int);
106172302Spjddouble_int double_int_neg (double_int);
107172302Spjd
108172302Spjd/* You must ensure that double_int_ext is called on the operands
109172302Spjd   of the following operations, if the precision of the numbers
110172302Spjd   is less than 2 * HOST_BITS_PER_WIDE_INT bits.  */
111172302Spjdbool double_int_fits_in_hwi_p (double_int, bool);
112172302Spjdbool double_int_fits_in_shwi_p (double_int);
113172302Spjdbool double_int_fits_in_uhwi_p (double_int);
114172302SpjdHOST_WIDE_INT double_int_to_shwi (double_int);
115172302Spjdunsigned HOST_WIDE_INT double_int_to_uhwi (double_int);
116172302Spjddouble_int double_int_div (double_int, double_int, bool, unsigned);
117172302Spjddouble_int double_int_sdiv (double_int, double_int, unsigned);
118172302Spjddouble_int double_int_udiv (double_int, double_int, unsigned);
119172302Spjddouble_int double_int_mod (double_int, double_int, bool, unsigned);
120172302Spjddouble_int double_int_smod (double_int, double_int, unsigned);
121172302Spjddouble_int double_int_umod (double_int, double_int, unsigned);
122172302Spjddouble_int double_int_divmod (double_int, double_int, bool, unsigned, double_int *);
123172302Spjddouble_int double_int_sdivmod (double_int, double_int, unsigned, double_int *);
124172302Spjddouble_int double_int_udivmod (double_int, double_int, unsigned, double_int *);
125172302Spjdbool double_int_negative_p (double_int);
126172302Spjdint double_int_cmp (double_int, double_int, bool);
127172302Spjdint double_int_scmp (double_int, double_int);
128172302Spjdint double_int_ucmp (double_int, double_int);
129172302Spjdvoid dump_double_int (FILE *, double_int, bool);
130172302Spjd
131172302Spjd/* Zero and sign extension of numbers in smaller precisions.  */
132172302Spjd
133172302Spjddouble_int double_int_ext (double_int, unsigned, bool);
134172302Spjddouble_int double_int_sext (double_int, unsigned);
135172302Spjddouble_int double_int_zext (double_int, unsigned);
136
137#define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
138
139/* The operands of the following comparison functions must be processed
140   with double_int_ext, if their precision is less than
141   2 * HOST_BITS_PER_WIDE_INT bits.  */
142
143/* Returns true if CST is zero.  */
144
145static inline bool
146double_int_zero_p (double_int cst)
147{
148  return cst.low == 0 && cst.high == 0;
149}
150
151/* Returns true if CST is one.  */
152
153static inline bool
154double_int_one_p (double_int cst)
155{
156  return cst.low == 1 && cst.high == 0;
157}
158
159/* Returns true if CST is minus one.  */
160
161static inline bool
162double_int_minus_one_p (double_int cst)
163{
164  return (cst.low == ALL_ONES && cst.high == -1);
165}
166
167/* Returns true if CST1 == CST2.  */
168
169static inline bool
170double_int_equal_p (double_int cst1, double_int cst2)
171{
172  return cst1.low == cst2.low && cst1.high == cst2.high;
173}
174
175#endif /* DOUBLE_INT_H */
176