]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/double-int.h
wait.cc (wait): Rename.
[thirdparty/gcc.git] / gcc / double-int.h
CommitLineData
f82783bd 1/* Operations with long integers.
2bd1333d 2 Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
b8698a0f 3
f82783bd 4This file is part of GCC.
b8698a0f 5
f82783bd
ZD
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
9dcd6f09 8Free Software Foundation; either version 3, or (at your option) any
f82783bd 9later version.
b8698a0f 10
f82783bd
ZD
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b8698a0f 15
f82783bd 16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
f82783bd
ZD
19
20#ifndef DOUBLE_INT_H
21#define DOUBLE_INT_H
22
34917a10 23#ifndef GENERATOR_FILE
e4fd22c6 24#include <gmp.h>
34917a10 25#endif
e4fd22c6
BM
26#include "coretypes.h"
27
f82783bd
ZD
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
66a4ad37 51 numbers with precision higher than HOST_WIDE_INT). It might be less
f82783bd
ZD
52 confusing to have them both signed or both unsigned. */
53
54typedef struct
55{
56 unsigned HOST_WIDE_INT low;
57 HOST_WIDE_INT high;
58} double_int;
59
2bd1333d
AS
60#define HOST_BITS_PER_DOUBLE_INT (2 * HOST_BITS_PER_WIDE_INT)
61
f82783bd
ZD
62/* Constructors and conversions. */
63
f82783bd
ZD
64/* Constructs double_int from integer CST. The bits over the precision of
65 HOST_WIDE_INT are filled with the sign bit. */
66
67static inline double_int
68shwi_to_double_int (HOST_WIDE_INT cst)
69{
70 double_int r;
b8698a0f 71
f82783bd
ZD
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
89static inline double_int
90uhwi_to_double_int (unsigned HOST_WIDE_INT cst)
91{
92 double_int r;
b8698a0f 93
f82783bd
ZD
94 r.low = cst;
95 r.high = 0;
96
97 return r;
98}
99
100/* The following operations perform arithmetics modulo 2^precision,
101 so you do not need to call double_int_ext between them, even if
102 you are representing numbers with precision less than
103 2 * HOST_BITS_PER_WIDE_INT bits. */
104
105double_int double_int_mul (double_int, double_int);
106double_int double_int_add (double_int, double_int);
107double_int double_int_neg (double_int);
108
109/* You must ensure that double_int_ext is called on the operands
110 of the following operations, if the precision of the numbers
111 is less than 2 * HOST_BITS_PER_WIDE_INT bits. */
112bool double_int_fits_in_hwi_p (double_int, bool);
113bool double_int_fits_in_shwi_p (double_int);
114bool double_int_fits_in_uhwi_p (double_int);
115HOST_WIDE_INT double_int_to_shwi (double_int);
116unsigned HOST_WIDE_INT double_int_to_uhwi (double_int);
117double_int double_int_div (double_int, double_int, bool, unsigned);
118double_int double_int_sdiv (double_int, double_int, unsigned);
119double_int double_int_udiv (double_int, double_int, unsigned);
f414f2f3
ZD
120double_int double_int_mod (double_int, double_int, bool, unsigned);
121double_int double_int_smod (double_int, double_int, unsigned);
122double_int double_int_umod (double_int, double_int, unsigned);
123double_int double_int_divmod (double_int, double_int, bool, unsigned, double_int *);
124double_int double_int_sdivmod (double_int, double_int, unsigned, double_int *);
125double_int double_int_udivmod (double_int, double_int, unsigned, double_int *);
54fb1ae0 126double_int double_int_setbit (double_int, unsigned);
2bd1333d
AS
127
128/* Logical operations. */
129static inline double_int
130double_int_not (double_int a)
131{
132 a.low = ~a.low;
133 a.high = ~ a.high;
134 return a;
135}
136
137/* Shift operations. */
138double_int double_int_lshift (double_int, HOST_WIDE_INT, unsigned int, bool);
139double_int double_int_rshift (double_int, HOST_WIDE_INT, unsigned int, bool);
140
141/* Returns true if CST is negative. Of course, CST is considered to
142 be signed. */
143
144static inline bool
145double_int_negative_p (double_int cst)
146{
147 return cst.high < 0;
148}
149
f82783bd
ZD
150int double_int_cmp (double_int, double_int, bool);
151int double_int_scmp (double_int, double_int);
152int double_int_ucmp (double_int, double_int);
153void dump_double_int (FILE *, double_int, bool);
154
155/* Zero and sign extension of numbers in smaller precisions. */
156
157double_int double_int_ext (double_int, unsigned, bool);
158double_int double_int_sext (double_int, unsigned);
159double_int double_int_zext (double_int, unsigned);
b3ce5b6e 160double_int double_int_mask (unsigned);
f82783bd
ZD
161
162#define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
163
164/* The operands of the following comparison functions must be processed
165 with double_int_ext, if their precision is less than
166 2 * HOST_BITS_PER_WIDE_INT bits. */
167
168/* Returns true if CST is zero. */
169
170static inline bool
171double_int_zero_p (double_int cst)
172{
173 return cst.low == 0 && cst.high == 0;
174}
175
176/* Returns true if CST is one. */
177
178static inline bool
179double_int_one_p (double_int cst)
180{
181 return cst.low == 1 && cst.high == 0;
182}
183
184/* Returns true if CST is minus one. */
185
186static inline bool
187double_int_minus_one_p (double_int cst)
188{
189 return (cst.low == ALL_ONES && cst.high == -1);
190}
191
192/* Returns true if CST1 == CST2. */
193
194static inline bool
195double_int_equal_p (double_int cst1, double_int cst2)
196{
197 return cst1.low == cst2.low && cst1.high == cst2.high;
198}
199
330db1e3
RG
200
201/* Legacy interface with decomposed high/low parts. */
202
203extern tree force_fit_type_double (tree, unsigned HOST_WIDE_INT, HOST_WIDE_INT,
204 int, bool);
205extern int fit_double_type (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
206 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
207 const_tree);
208extern int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
209 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
210 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
211 bool);
212#define add_double(l1,h1,l2,h2,lv,hv) \
213 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
214extern int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
215 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
216extern int mul_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
217 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
218 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
219 bool);
220#define mul_double(l1,h1,l2,h2,lv,hv) \
221 mul_double_with_sign (l1, h1, l2, h2, lv, hv, false)
222extern void lshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
223 HOST_WIDE_INT, unsigned int,
224 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool);
225extern void rshift_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
226 HOST_WIDE_INT, unsigned int,
227 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *, bool);
228extern void lrotate_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
229 HOST_WIDE_INT, unsigned int,
230 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
231extern void rrotate_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
232 HOST_WIDE_INT, unsigned int,
233 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
234extern int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
235 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
236 HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
237 HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
238 HOST_WIDE_INT *);
239
240
34917a10 241#ifndef GENERATOR_FILE
e4fd22c6
BM
242/* Conversion to and from GMP integer representations. */
243
244void mpz_set_double_int (mpz_t, double_int, bool);
22ea9ec0 245double_int mpz_get_double_int (const_tree, mpz_t, bool);
34917a10 246#endif
e4fd22c6 247
f82783bd 248#endif /* DOUBLE_INT_H */