Add files that I missed when importing NaCl changes earlier
[gcc/nacl-gcc.git] / gcc / double-int.h
blob6c1e52bc0e1a0218d9b43e956deb927c0b0071cf
1 /* Operations with long integers.
2 Copyright (C) 2006, 2007 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 /* A large integer is currently represented as a pair of HOST_WIDE_INTs.
24 It therefore represents a number with precision of
25 2 * HOST_BITS_PER_WIDE_INT bits (it is however possible that the
26 internal representation will change, if numbers with greater precision
27 are needed, so the users should not rely on it). The representation does
28 not contain any information about signedness of the represented value, so
29 it can be used to represent both signed and unsigned numbers. For
30 operations where the results depend on signedness (division, comparisons),
31 it must be specified separately. For each such operation, there are three
32 versions of the function -- double_int_op, that takes an extra UNS argument
33 giving the signedness of the values, and double_int_sop and double_int_uop
34 that stand for its specializations for signed and unsigned values.
36 You may also represent with numbers in smaller precision using double_int.
37 You however need to use double_int_ext (that fills in the bits of the
38 number over the prescribed precision with zeros or with the sign bit) before
39 operations that do not perform arithmetics modulo 2^precision (comparisons,
40 division), and possibly before storing the results, if you want to keep
41 them in some canonical form). In general, the signedness of double_int_ext
42 should match the signedness of the operation.
44 ??? The components of double_int differ in signedness mostly for
45 historical reasons (they replace an older structure used to represent
46 numbers with precision higher than HOST_WIDE_INT). It might be less
47 confusing to have them both signed or both unsigned. */
49 typedef struct
51 unsigned HOST_WIDE_INT low;
52 HOST_WIDE_INT high;
53 } double_int;
55 union tree_node;
57 /* Constructors and conversions. */
59 union tree_node *double_int_to_tree (union tree_node *, double_int);
60 double_int tree_to_double_int (union tree_node *tree);
62 /* Constructs double_int from integer CST. The bits over the precision of
63 HOST_WIDE_INT are filled with the sign bit. */
65 static inline double_int
66 shwi_to_double_int (HOST_WIDE_INT cst)
68 double_int r;
70 r.low = (unsigned HOST_WIDE_INT) cst;
71 r.high = cst < 0 ? -1 : 0;
73 return r;
76 /* Some useful constants. */
78 #define double_int_minus_one (shwi_to_double_int (-1))
79 #define double_int_zero (shwi_to_double_int (0))
80 #define double_int_one (shwi_to_double_int (1))
81 #define double_int_two (shwi_to_double_int (2))
82 #define double_int_ten (shwi_to_double_int (10))
84 /* Constructs double_int from unsigned integer CST. The bits over the
85 precision of HOST_WIDE_INT are filled with zeros. */
87 static inline double_int
88 uhwi_to_double_int (unsigned HOST_WIDE_INT cst)
90 double_int r;
92 r.low = cst;
93 r.high = 0;
95 return r;
98 /* The following operations perform arithmetics modulo 2^precision,
99 so you do not need to call double_int_ext between them, even if
100 you are representing numbers with precision less than
101 2 * HOST_BITS_PER_WIDE_INT bits. */
103 double_int double_int_mul (double_int, double_int);
104 double_int double_int_add (double_int, double_int);
105 double_int double_int_neg (double_int);
107 /* You must ensure that double_int_ext is called on the operands
108 of the following operations, if the precision of the numbers
109 is less than 2 * HOST_BITS_PER_WIDE_INT bits. */
110 bool double_int_fits_in_hwi_p (double_int, bool);
111 bool double_int_fits_in_shwi_p (double_int);
112 bool double_int_fits_in_uhwi_p (double_int);
113 HOST_WIDE_INT double_int_to_shwi (double_int);
114 unsigned HOST_WIDE_INT double_int_to_uhwi (double_int);
115 double_int double_int_div (double_int, double_int, bool, unsigned);
116 double_int double_int_sdiv (double_int, double_int, unsigned);
117 double_int double_int_udiv (double_int, double_int, unsigned);
118 double_int double_int_mod (double_int, double_int, bool, unsigned);
119 double_int double_int_smod (double_int, double_int, unsigned);
120 double_int double_int_umod (double_int, double_int, unsigned);
121 double_int double_int_divmod (double_int, double_int, bool, unsigned, double_int *);
122 double_int double_int_sdivmod (double_int, double_int, unsigned, double_int *);
123 double_int double_int_udivmod (double_int, double_int, unsigned, double_int *);
124 bool double_int_negative_p (double_int);
125 int double_int_cmp (double_int, double_int, bool);
126 int double_int_scmp (double_int, double_int);
127 int double_int_ucmp (double_int, double_int);
128 void dump_double_int (FILE *, double_int, bool);
130 /* Zero and sign extension of numbers in smaller precisions. */
132 double_int double_int_ext (double_int, unsigned, bool);
133 double_int double_int_sext (double_int, unsigned);
134 double_int double_int_zext (double_int, unsigned);
136 #define ALL_ONES (~((unsigned HOST_WIDE_INT) 0))
138 /* The operands of the following comparison functions must be processed
139 with double_int_ext, if their precision is less than
140 2 * HOST_BITS_PER_WIDE_INT bits. */
142 /* Returns true if CST is zero. */
144 static inline bool
145 double_int_zero_p (double_int cst)
147 return cst.low == 0 && cst.high == 0;
150 /* Returns true if CST is one. */
152 static inline bool
153 double_int_one_p (double_int cst)
155 return cst.low == 1 && cst.high == 0;
158 /* Returns true if CST is minus one. */
160 static inline bool
161 double_int_minus_one_p (double_int cst)
163 return (cst.low == ALL_ONES && cst.high == -1);
166 /* Returns true if CST1 == CST2. */
168 static inline bool
169 double_int_equal_p (double_int cst1, double_int cst2)
171 return cst1.low == cst2.low && cst1.high == cst2.high;
174 #endif /* DOUBLE_INT_H */