1 |
684 |
jeremybenn |
/* Operations with long integers.
|
2 |
|
|
Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GCC.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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/>. */
|
19 |
|
|
|
20 |
|
|
#ifndef DOUBLE_INT_H
|
21 |
|
|
#define DOUBLE_INT_H
|
22 |
|
|
|
23 |
|
|
#ifndef GENERATOR_FILE
|
24 |
|
|
#include <gmp.h>
|
25 |
|
|
#endif
|
26 |
|
|
#include "coretypes.h"
|
27 |
|
|
|
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.
|
40 |
|
|
|
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.
|
48 |
|
|
|
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. */
|
53 |
|
|
|
54 |
|
|
typedef struct
|
55 |
|
|
{
|
56 |
|
|
unsigned HOST_WIDE_INT low;
|
57 |
|
|
HOST_WIDE_INT high;
|
58 |
|
|
} double_int;
|
59 |
|
|
|
60 |
|
|
#define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
|
61 |
|
|
|
62 |
|
|
/* Constructors and conversions. */
|
63 |
|
|
|
64 |
|
|
/* Constructs double_int from integer CST. The bits over the precision of
|
65 |
|
|
HOST_WIDE_INT are filled with the sign bit. */
|
66 |
|
|
|
67 |
|
|
static inline double_int
|
68 |
|
|
shwi_to_double_int (HOST_WIDE_INT cst)
|
69 |
|
|
{
|
70 |
|
|
double_int r;
|
71 |
|
|
|
72 |
|
|
r.low = (unsigned HOST_WIDE_INT) cst;
|
73 |
|
|
r.high = cst < 0 ? -1 : 0;
|
74 |
|
|
|
75 |
|
|
return r;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
/* Some useful constants. */
|
79 |
|
|
|
80 |
|
|
#define double_int_minus_one (shwi_to_double_int (-1))
|
81 |
|
|
#define double_int_zero (shwi_to_double_int (0))
|
82 |
|
|
#define double_int_one (shwi_to_double_int (1))
|
83 |
|
|
#define double_int_two (shwi_to_double_int (2))
|
84 |
|
|
#define double_int_ten (shwi_to_double_int (10))
|
85 |
|
|
|
86 |
|
|
/* Constructs double_int from unsigned integer CST. The bits over the
|
87 |
|
|
precision of HOST_WIDE_INT are filled with zeros. */
|
88 |
|
|
|
89 |
|
|
static inline double_int
|
90 |
|
|
uhwi_to_double_int (unsigned HOST_WIDE_INT cst)
|
91 |
|
|
{
|
92 |
|
|
double_int r;
|
93 |
|
|
|
94 |
|
|
r.low = cst;
|
95 |
|
|
r.high = 0;
|
96 |
|
|
|
97 |
|
|
return r;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/* Returns value of CST as a signed number. CST must satisfy
|
101 |
|
|
double_int_fits_in_shwi_p. */
|
102 |
|
|
|
103 |
|
|
static inline HOST_WIDE_INT
|
104 |
|
|
double_int_to_shwi (double_int cst)
|
105 |
|
|
{
|
106 |
|
|
return (HOST_WIDE_INT) cst.low;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/* Returns value of CST as an unsigned number. CST must satisfy
|
110 |
|
|
double_int_fits_in_uhwi_p. */
|
111 |
|
|
|
112 |
|
|
static inline unsigned HOST_WIDE_INT
|
113 |
|
|
double_int_to_uhwi (double_int cst)
|
114 |
|
|
{
|
115 |
|
|
return cst.low;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
bool double_int_fits_in_hwi_p (double_int, bool);
|
119 |
|
|
bool double_int_fits_in_shwi_p (double_int);
|
120 |
|
|
|
121 |
|
|
/* Returns true if CST fits in unsigned HOST_WIDE_INT. */
|
122 |
|
|
|
123 |
|
|
static inline bool
|
124 |
|
|
double_int_fits_in_uhwi_p (double_int cst)
|
125 |
|
|
{
|
126 |
|
|
return cst.high == 0;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/* The following operations perform arithmetics modulo 2^precision,
|
130 |
|
|
so you do not need to call double_int_ext between them, even if
|
131 |
|
|
you are representing numbers with precision less than
|
132 |
|
|
2 * HOST_BITS_PER_WIDE_INT bits. */
|
133 |
|
|
|
134 |
|
|
double_int double_int_mul (double_int, double_int);
|
135 |
|
|
double_int double_int_mul_with_sign (double_int, double_int, bool, int *);
|
136 |
|
|
double_int double_int_add (double_int, double_int);
|
137 |
|
|
double_int double_int_sub (double_int, double_int);
|
138 |
|
|
double_int double_int_neg (double_int);
|
139 |
|
|
|
140 |
|
|
/* You must ensure that double_int_ext is called on the operands
|
141 |
|
|
of the following operations, if the precision of the numbers
|
142 |
|
|
is less than 2 * HOST_BITS_PER_WIDE_INT bits. */
|
143 |
|
|
double_int double_int_div (double_int, double_int, bool, unsigned);
|
144 |
|
|
double_int double_int_sdiv (double_int, double_int, unsigned);
|
145 |
|
|
double_int double_int_udiv (double_int, double_int, unsigned);
|
146 |
|
|
double_int double_int_mod (double_int, double_int, bool, unsigned);
|
147 |
|
|
double_int double_int_smod (double_int, double_int, unsigned);
|
148 |
|
|
double_int double_int_umod (double_int, double_int, unsigned);
|
149 |
|
|
double_int double_int_divmod (double_int, double_int, bool, unsigned, double_int *);
|
150 |
|
|
double_int double_int_sdivmod (double_int, double_int, unsigned, double_int *);
|
151 |
|
|
double_int double_int_udivmod (double_int, double_int, unsigned, double_int *);
|
152 |
|
|
|
153 |
|
|
double_int double_int_setbit (double_int, unsigned);
|
154 |
|
|
int double_int_ctz (double_int);
|
155 |
|
|
|
156 |
|
|
/* Logical operations. */
|
157 |
|
|
|
158 |
|
|
/* Returns ~A. */
|
159 |
|
|
|
160 |
|
|
static inline double_int
|
161 |
|
|
double_int_not (double_int a)
|
162 |
|
|
{
|
163 |
|
|
a.low = ~a.low;
|
164 |
|
|
a.high = ~a.high;
|
165 |
|
|
return a;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/* Returns A | B. */
|
169 |
|
|
|
170 |
|
|
static inline double_int
|
171 |
|
|
double_int_ior (double_int a, double_int b)
|
172 |
|
|
{
|
173 |
|
|
a.low |= b.low;
|
174 |
|
|
a.high |= b.high;
|
175 |
|
|
return a;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
/* Returns A & B. */
|
179 |
|
|
|
180 |
|
|
static inline double_int
|
181 |
|
|
double_int_and (double_int a, double_int b)
|
182 |
|
|
{
|
183 |
|
|
a.low &= b.low;
|
184 |
|
|
a.high &= b.high;
|
185 |
|
|
return a;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
/* Returns A & ~B. */
|
189 |
|
|
|
190 |
|
|
static inline double_int
|
191 |
|
|
double_int_and_not (double_int a, double_int b)
|
192 |
|
|
{
|
193 |
|
|
a.low &= ~b.low;
|
194 |
|
|
a.high &= ~b.high;
|
195 |
|
|
return a;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/* Returns A ^ B. */
|
199 |
|
|
|
200 |
|
|
static inline double_int
|
201 |
|
|
double_int_xor (double_int a, double_int b)
|
202 |
|
|
{
|
203 |
|
|
a.low ^= b.low;
|
204 |
|
|
a.high ^= b.high;
|
205 |
|
|
return a;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/* Shift operations. */
|
210 |
|
|
double_int double_int_lshift (double_int, HOST_WIDE_INT, unsigned int, bool);
|
211 |
|
|
double_int double_int_rshift (double_int, HOST_WIDE_INT, unsigned int, bool);
|
212 |
|
|
double_int double_int_lrotate (double_int, HOST_WIDE_INT, unsigned int);
|
213 |
|
|
double_int double_int_rrotate (double_int, HOST_WIDE_INT, unsigned int);
|
214 |
|
|
|
215 |
|
|
/* Returns true if CST is negative. Of course, CST is considered to
|
216 |
|
|
be signed. */
|
217 |
|
|
|
218 |
|
|
static inline bool
|
219 |
|
|
double_int_negative_p (double_int cst)
|
220 |
|
|
{
|
221 |
|
|
return cst.high < 0;
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
int double_int_cmp (double_int, double_int, bool);
|
225 |
|
|
int double_int_scmp (double_int, double_int);
|
226 |
|
|
int double_int_ucmp (double_int, double_int);
|
227 |
|
|
|
228 |
|
|
double_int double_int_max (double_int, double_int, bool);
|
229 |
|
|
double_int double_int_smax (double_int, double_int);
|
230 |
|
|
double_int double_int_umax (double_int, double_int);
|
231 |
|
|
|
232 |
|
|
double_int double_int_min (double_int, double_int, bool);
|
233 |
|
|
double_int double_int_smin (double_int, double_int);
|
234 |
|
|
double_int double_int_umin (double_int, double_int);
|
235 |
|
|
|
236 |
|
|
void dump_double_int (FILE *, double_int, bool);
|
237 |
|
|
|
238 |
|
|
/* Zero and sign extension of numbers in smaller precisions. */
|
239 |
|
|
|
240 |
|
|
double_int double_int_ext (double_int, unsigned, bool);
|
241 |
|
|
double_int double_int_sext (double_int, unsigned);
|
242 |
|
|
double_int double_int_zext (double_int, unsigned);
|
243 |
|
|
double_int double_int_mask (unsigned);
|
244 |
|
|
|
245 |
|
|
#define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
|
246 |
|
|
|
247 |
|
|
/* The operands of the following comparison functions must be processed
|
248 |
|
|
with double_int_ext, if their precision is less than
|
249 |
|
|
2 * HOST_BITS_PER_WIDE_INT bits. */
|
250 |
|
|
|
251 |
|
|
/* Returns true if CST is zero. */
|
252 |
|
|
|
253 |
|
|
static inline bool
|
254 |
|
|
double_int_zero_p (double_int cst)
|
255 |
|
|
{
|
256 |
|
|
return cst.low == 0 && cst.high == 0;
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
/* Returns true if CST is one. */
|
260 |
|
|
|
261 |
|
|
static inline bool
|
262 |
|
|
double_int_one_p (double_int cst)
|
263 |
|
|
{
|
264 |
|
|
return cst.low == 1 && cst.high == 0;
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
/* Returns true if CST is minus one. */
|
268 |
|
|
|
269 |
|
|
static inline bool
|
270 |
|
|
double_int_minus_one_p (double_int cst)
|
271 |
|
|
{
|
272 |
|
|
return (cst.low == ALL_ONES && cst.high == -1);
|
273 |
|
|
}
|
274 |
|
|
|
275 |
|
|
/* Returns true if CST1 == CST2. */
|
276 |
|
|
|
277 |
|
|
static inline bool
|
278 |
|
|
double_int_equal_p (double_int cst1, double_int cst2)
|
279 |
|
|
{
|
280 |
|
|
return cst1.low == cst2.low && cst1.high == cst2.high;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
/* Legacy interface with decomposed high/low parts. */
|
285 |
|
|
|
286 |
|
|
extern int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
287 |
|
|
unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
288 |
|
|
unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
|
289 |
|
|
bool);
|
290 |
|
|
#define add_double(l1,h1,l2,h2,lv,hv) \
|
291 |
|
|
add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
|
292 |
|
|
extern int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
293 |
|
|
unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
|
294 |
|
|
extern int mul_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
295 |
|
|
unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
296 |
|
|
unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
|
297 |
|
|
bool);
|
298 |
|
|
#define mul_double(l1,h1,l2,h2,lv,hv) \
|
299 |
|
|
mul_double_with_sign (l1, h1, l2, h2, lv, hv, false)
|
300 |
|
|
extern void lshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
301 |
|
|
HOST_WIDE_INT, unsigned int,
|
302 |
|
|
unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool);
|
303 |
|
|
extern void rshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
|
304 |
|
|
HOST_WIDE_INT, unsigned int,
|
305 |
|
|
unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool);
|
306 |
|
|
extern int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
|
307 |
|
|
HOST_WIDE_INT, unsigned HOST_WIDE_INT,
|
308 |
|
|
HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
|
309 |
|
|
HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
|
310 |
|
|
HOST_WIDE_INT *);
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
#ifndef GENERATOR_FILE
|
314 |
|
|
/* Conversion to and from GMP integer representations. */
|
315 |
|
|
|
316 |
|
|
void mpz_set_double_int (mpz_t, double_int, bool);
|
317 |
|
|
double_int mpz_get_double_int (const_tree, mpz_t, bool);
|
318 |
|
|
#endif
|
319 |
|
|
|
320 |
|
|
#endif /* DOUBLE_INT_H */
|