]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/double-int.h
gengtype.c (main): Handle double_int type.
[thirdparty/gcc.git] / gcc / double-int.h
CommitLineData
f82783bd
ZD
1/* Operations with long integers.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
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
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
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.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
20
21#ifndef DOUBLE_INT_H
22#define DOUBLE_INT_H
23
24/* A large integer is currently represented as a pair of HOST_WIDE_INTs.
25 It therefore represents a number with precision of
26 2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
27 internal representation will change, if numbers with greater precision
28 are needed, so the users should not rely on it). The representation does
29 not contain any information about signedness of the represented value, so
30 it can be used to represent both signed and unsigned numbers. For
31 operations where the results depend on signedness (division, comparisons),
32 it must be specified separately. For each such operation, there are three
33 versions of the function -- double_int_op, that takes an extra UNS argument
34 giving the signedness of the values, and double_int_sop and double_int_uop
35 that stand for its specializations for signed and unsigned values.
36
37 You may also represent with numbers in smaller precision using double_int.
38 You however need to use double_int_ext (that fills in the bits of the
39 number over the prescribed precision with zeros or with the sign bit) before
40 operations that do not perform arithmetics modulo 2^precision (comparisons,
41 division), and possibly before storing the results, if you want to keep
42 them in some canonical form). In general, the signedness of double_int_ext
43 should match the signedness of the operation.
44
45 ??? The components of double_int differ in signedness mostly for
46 historical reasons (they replace an older structure used to represent
47 numbers with precision wigher than HOST_WIDE_INT). It might be less
48 confusing to have them both signed or both unsigned. */
49
50typedef struct
51{
52 unsigned HOST_WIDE_INT low;
53 HOST_WIDE_INT high;
54} double_int;
55
56union tree_node;
57
58/* Constructors and conversions. */
59
60union tree_node *double_int_to_tree (union tree_node *, double_int);
61double_int tree_to_double_int (union tree_node *tree);
62
63/* Constructs double_int from integer CST. The bits over the precision of
64 HOST_WIDE_INT are filled with the sign bit. */
65
66static inline double_int
67shwi_to_double_int (HOST_WIDE_INT cst)
68{
69 double_int r;
70
71 r.low = (unsigned HOST_WIDE_INT) cst;
72 r.high = cst < 0 ? -1 : 0;
73
74 return r;
75}
76
77/* Some useful constants. */
78
79#define double_int_minus_one (shwi_to_double_int (-1))
80#define double_int_zero (shwi_to_double_int (0))
81#define double_int_one (shwi_to_double_int (1))
82#define double_int_two (shwi_to_double_int (2))
83#define double_int_ten (shwi_to_double_int (10))
84
85/* Constructs double_int from unsigned integer CST. The bits over the
86 precision of HOST_WIDE_INT are filled with zeros. */
87
88static inline double_int
89uhwi_to_double_int (unsigned HOST_WIDE_INT cst)
90{
91 double_int r;
92
93 r.low = cst;
94 r.high = 0;
95
96 return r;
97}
98
99/* The following operations perform arithmetics modulo 2^precision,
100 so you do not need to call double_int_ext between them, even if
101 you are representing numbers with precision less than
102 2 * HOST_BITS_PER_WIDE_INT bits. */
103
104double_int double_int_mul (double_int, double_int);
105double_int double_int_add (double_int, double_int);
106double_int double_int_neg (double_int);
107
108/* You must ensure that double_int_ext is called on the operands
109 of the following operations, if the precision of the numbers
110 is less than 2 * HOST_BITS_PER_WIDE_INT bits. */
111bool double_int_fits_in_hwi_p (double_int, bool);
112bool double_int_fits_in_shwi_p (double_int);
113bool double_int_fits_in_uhwi_p (double_int);
114HOST_WIDE_INT double_int_to_shwi (double_int);
115unsigned HOST_WIDE_INT double_int_to_uhwi (double_int);
116double_int double_int_div (double_int, double_int, bool, unsigned);
117double_int double_int_sdiv (double_int, double_int, unsigned);
118double_int double_int_udiv (double_int, double_int, unsigned);
119bool double_int_negative_p (double_int);
120int double_int_cmp (double_int, double_int, bool);
121int double_int_scmp (double_int, double_int);
122int double_int_ucmp (double_int, double_int);
123void dump_double_int (FILE *, double_int, bool);
124
125/* Zero and sign extension of numbers in smaller precisions. */
126
127double_int double_int_ext (double_int, unsigned, bool);
128double_int double_int_sext (double_int, unsigned);
129double_int double_int_zext (double_int, unsigned);
130
131#define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
132
133/* The operands of the following comparison functions must be processed
134 with double_int_ext, if their precision is less than
135 2 * HOST_BITS_PER_WIDE_INT bits. */
136
137/* Returns true if CST is zero. */
138
139static inline bool
140double_int_zero_p (double_int cst)
141{
142 return cst.low == 0 && cst.high == 0;
143}
144
145/* Returns true if CST is one. */
146
147static inline bool
148double_int_one_p (double_int cst)
149{
150 return cst.low == 1 && cst.high == 0;
151}
152
153/* Returns true if CST is minus one. */
154
155static inline bool
156double_int_minus_one_p (double_int cst)
157{
158 return (cst.low == ALL_ONES && cst.high == -1);
159}
160
161/* Returns true if CST1 == CST2. */
162
163static inline bool
164double_int_equal_p (double_int cst1, double_int cst2)
165{
166 return cst1.low == cst2.low && cst1.high == cst2.high;
167}
168
169#endif /* DOUBLE_INT_H */