Merged r158229 through r158464 into branch.
[official-gcc.git] / gcc / double-int.h
blob682034859b92bb22783e89ac4111887380763216
1 /* Operations with long integers.
2 Copyright (C) 2006, 2007, 2008, 2010 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
26 #include "coretypes.h"
28 /* A large integer is currently represented as a pair of HOST_WIDE_INTs.
29 It therefore represents a number with precision of
30 2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
31 internal representation will change, if numbers with greater precision
32 are needed, so the users should not rely on it). The representation does
33 not contain any information about signedness of the represented value, so
34 it can be used to represent both signed and unsigned numbers. For
35 operations where the results depend on signedness (division, comparisons),
36 it must be specified separately. For each such operation, there are three
37 versions of the function -- double_int_op, that takes an extra UNS argument
38 giving the signedness of the values, and double_int_sop and double_int_uop
39 that stand for its specializations for signed and unsigned values.
41 You may also represent with numbers in smaller precision using double_int.
42 You however need to use double_int_ext (that fills in the bits of the
43 number over the prescribed precision with zeros or with the sign bit) before
44 operations that do not perform arithmetics modulo 2^precision (comparisons,
45 division), and possibly before storing the results, if you want to keep
46 them in some canonical form). In general, the signedness of double_int_ext
47 should match the signedness of the operation.
49 ??? The components of double_int differ in signedness mostly for
50 historical reasons (they replace an older structure used to represent
51 numbers with precision higher than HOST_WIDE_INT). It might be less
52 confusing to have them both signed or both unsigned. */
54 typedef struct
56 unsigned HOST_WIDE_INT low;
57 HOST_WIDE_INT high;
58 } double_int;
60 #define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
62 /* Constructors and conversions. */
64 tree double_int_to_tree (tree, double_int);
65 bool double_int_fits_to_tree_p (const_tree, double_int);
67 /* Constructs double_int from tree CST. */
69 #define tree_to_double_int(cst) (TREE_INT_CST (cst))
71 /* Constructs double_int from integer CST. The bits over the precision of
72 HOST_WIDE_INT are filled with the sign bit. */
74 static inline double_int
75 shwi_to_double_int (HOST_WIDE_INT cst)
77 double_int r;
79 r.low = (unsigned HOST_WIDE_INT) cst;
80 r.high = cst < 0 ? -1 : 0;
82 return r;
85 /* Some useful constants. */
87 #define double_int_minus_one (shwi_to_double_int (-1))
88 #define double_int_zero (shwi_to_double_int (0))
89 #define double_int_one (shwi_to_double_int (1))
90 #define double_int_two (shwi_to_double_int (2))
91 #define double_int_ten (shwi_to_double_int (10))
93 /* Constructs double_int from unsigned integer CST. The bits over the
94 precision of HOST_WIDE_INT are filled with zeros. */
96 static inline double_int
97 uhwi_to_double_int (unsigned HOST_WIDE_INT cst)
99 double_int r;
101 r.low = cst;
102 r.high = 0;
104 return r;
107 /* The following operations perform arithmetics modulo 2^precision,
108 so you do not need to call double_int_ext between them, even if
109 you are representing numbers with precision less than
110 2 * HOST_BITS_PER_WIDE_INT bits. */
112 double_int double_int_mul (double_int, double_int);
113 double_int double_int_add (double_int, double_int);
114 double_int double_int_neg (double_int);
116 /* You must ensure that double_int_ext is called on the operands
117 of the following operations, if the precision of the numbers
118 is less than 2 * HOST_BITS_PER_WIDE_INT bits. */
119 bool double_int_fits_in_hwi_p (double_int, bool);
120 bool double_int_fits_in_shwi_p (double_int);
121 bool double_int_fits_in_uhwi_p (double_int);
122 HOST_WIDE_INT double_int_to_shwi (double_int);
123 unsigned HOST_WIDE_INT double_int_to_uhwi (double_int);
124 double_int double_int_div (double_int, double_int, bool, unsigned);
125 double_int double_int_sdiv (double_int, double_int, unsigned);
126 double_int double_int_udiv (double_int, double_int, unsigned);
127 double_int double_int_mod (double_int, double_int, bool, unsigned);
128 double_int double_int_smod (double_int, double_int, unsigned);
129 double_int double_int_umod (double_int, double_int, unsigned);
130 double_int double_int_divmod (double_int, double_int, bool, unsigned, double_int *);
131 double_int double_int_sdivmod (double_int, double_int, unsigned, double_int *);
132 double_int double_int_udivmod (double_int, double_int, unsigned, double_int *);
134 /* Logical operations. */
135 static inline double_int
136 double_int_not (double_int a)
138 a.low = ~a.low;
139 a.high = ~ a.high;
140 return a;
143 /* Shift operations. */
144 double_int double_int_lshift (double_int, HOST_WIDE_INT, unsigned int, bool);
145 double_int double_int_rshift (double_int, HOST_WIDE_INT, unsigned int, bool);
147 /* Returns true if CST is negative. Of course, CST is considered to
148 be signed. */
150 static inline bool
151 double_int_negative_p (double_int cst)
153 return cst.high < 0;
156 int double_int_cmp (double_int, double_int, bool);
157 int double_int_scmp (double_int, double_int);
158 int double_int_ucmp (double_int, double_int);
159 void dump_double_int (FILE *, double_int, bool);
161 /* Zero and sign extension of numbers in smaller precisions. */
163 double_int double_int_ext (double_int, unsigned, bool);
164 double_int double_int_sext (double_int, unsigned);
165 double_int double_int_zext (double_int, unsigned);
166 double_int double_int_mask (unsigned);
168 #define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
170 /* The operands of the following comparison functions must be processed
171 with double_int_ext, if their precision is less than
172 2 * HOST_BITS_PER_WIDE_INT bits. */
174 /* Returns true if CST is zero. */
176 static inline bool
177 double_int_zero_p (double_int cst)
179 return cst.low == 0 && cst.high == 0;
182 /* Returns true if CST is one. */
184 static inline bool
185 double_int_one_p (double_int cst)
187 return cst.low == 1 && cst.high == 0;
190 /* Returns true if CST is minus one. */
192 static inline bool
193 double_int_minus_one_p (double_int cst)
195 return (cst.low == ALL_ONES && cst.high == -1);
198 /* Returns true if CST1 == CST2. */
200 static inline bool
201 double_int_equal_p (double_int cst1, double_int cst2)
203 return cst1.low == cst2.low && cst1.high == cst2.high;
207 /* Legacy interface with decomposed high/low parts. */
209 extern tree force_fit_type_double (tree, unsigned HOST_WIDE_INT, HOST_WIDE_INT,
210 int, bool);
211 extern int fit_double_type (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
212 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
213 const_tree);
214 extern int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
215 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
216 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
217 bool);
218 #define add_double(l1,h1,l2,h2,lv,hv) \
219 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
220 extern int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
221 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
222 extern int mul_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
223 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
224 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
225 bool);
226 #define mul_double(l1,h1,l2,h2,lv,hv) \
227 mul_double_with_sign (l1, h1, l2, h2, lv, hv, false)
228 extern void lshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
229 HOST_WIDE_INT, unsigned int,
230 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool);
231 extern void rshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
232 HOST_WIDE_INT, unsigned int,
233 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool);
234 extern void lrotate_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
235 HOST_WIDE_INT, unsigned int,
236 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
237 extern void rrotate_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
238 HOST_WIDE_INT, unsigned int,
239 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
240 extern int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
241 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
242 HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
243 HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
244 HOST_WIDE_INT *);
247 #ifndef GENERATOR_FILE
248 /* Conversion to and from GMP integer representations. */
250 void mpz_set_double_int (mpz_t, double_int, bool);
251 double_int mpz_get_double_int (const_tree, mpz_t, bool);
252 #endif
254 #endif /* DOUBLE_INT_H */