1 /* Operations with long integers.
2 Copyright (C) 2006 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 2, or (at your option) any
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
27 /* Returns mask for PREC bits. */
29 static inline double_int
30 double_int_mask (unsigned prec
)
32 unsigned HOST_WIDE_INT m
;
35 if (prec
> HOST_BITS_PER_WIDE_INT
)
37 prec
-= HOST_BITS_PER_WIDE_INT
;
38 m
= ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1;
39 mask
.high
= (HOST_WIDE_INT
) m
;
45 mask
.low
= ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1;
51 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
52 outside of the precision are set to the sign bit (i.e., the PREC-th one),
53 otherwise they are set to zero.
55 This corresponds to returning the value represented by PREC lowermost bits
56 of CST, with the given signedness. */
59 double_int_ext (double_int cst
, unsigned prec
, bool uns
)
62 return double_int_zext (cst
, prec
);
64 return double_int_sext (cst
, prec
);
67 /* The same as double_int_ext with UNS = true. */
70 double_int_zext (double_int cst
, unsigned prec
)
72 double_int mask
= double_int_mask (prec
);
75 r
.low
= cst
.low
& mask
.low
;
76 r
.high
= cst
.high
& mask
.high
;
81 /* The same as double_int_ext with UNS = false. */
84 double_int_sext (double_int cst
, unsigned prec
)
86 double_int mask
= double_int_mask (prec
);
88 unsigned HOST_WIDE_INT snum
;
90 if (prec
<= HOST_BITS_PER_WIDE_INT
)
94 prec
-= HOST_BITS_PER_WIDE_INT
;
95 snum
= (unsigned HOST_WIDE_INT
) cst
.high
;
97 if (((snum
>> (prec
- 1)) & 1) == 1)
99 r
.low
= cst
.low
| ~mask
.low
;
100 r
.high
= cst
.high
| ~mask
.high
;
104 r
.low
= cst
.low
& mask
.low
;
105 r
.high
= cst
.high
& mask
.high
;
111 /* Constructs long integer from tree CST. The extra bits over the precision of
112 the number are filled with sign bit if CST is signed, and with zeros if it
116 tree_to_double_int (tree cst
)
118 /* We do not need to call double_int_restrict here to ensure the semantics as
119 described, as this is the default one for trees. */
120 return TREE_INT_CST (cst
);
123 /* Returns true if CST fits in unsigned HOST_WIDE_INT. */
126 double_int_fits_in_uhwi_p (double_int cst
)
128 return cst
.high
== 0;
131 /* Returns true if CST fits in signed HOST_WIDE_INT. */
134 double_int_fits_in_shwi_p (double_int cst
)
137 return (HOST_WIDE_INT
) cst
.low
>= 0;
138 else if (cst
.high
== -1)
139 return (HOST_WIDE_INT
) cst
.low
< 0;
144 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
145 unsigned HOST_WIDE_INT if UNS is true. */
148 double_int_fits_in_hwi_p (double_int cst
, bool uns
)
151 return double_int_fits_in_uhwi_p (cst
);
153 return double_int_fits_in_shwi_p (cst
);
156 /* Returns value of CST as a signed number. CST must satisfy
157 double_int_fits_in_shwi_p. */
160 double_int_to_shwi (double_int cst
)
162 return (HOST_WIDE_INT
) cst
.low
;
165 /* Returns value of CST as an unsigned number. CST must satisfy
166 double_int_fits_in_uhwi_p. */
168 unsigned HOST_WIDE_INT
169 double_int_to_uhwi (double_int cst
)
177 double_int_mul (double_int a
, double_int b
)
180 mul_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
187 double_int_add (double_int a
, double_int b
)
190 add_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
197 double_int_neg (double_int a
)
200 neg_double (a
.low
, a
.high
, &ret
.low
, &ret
.high
);
204 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
205 specified by CODE). CODE is enum tree_code in fact, but double_int.h
206 must be included before tree.h. */
209 double_int_div (double_int a
, double_int b
, bool uns
, unsigned code
)
211 unsigned HOST_WIDE_INT rem_lo
;
212 HOST_WIDE_INT rem_hi
;
215 div_and_round_double (code
, uns
, a
.low
, a
.high
, b
.low
, b
.high
,
216 &ret
.low
, &ret
.high
, &rem_lo
, &rem_hi
);
220 /* The same as double_int_div with UNS = false. */
223 double_int_sdiv (double_int a
, double_int b
, unsigned code
)
225 return double_int_div (a
, b
, false, code
);
228 /* The same as double_int_div with UNS = true. */
231 double_int_udiv (double_int a
, double_int b
, unsigned code
)
233 return double_int_div (a
, b
, true, code
);
236 /* Constructs tree in type TYPE from with value given by CST. */
239 double_int_to_tree (tree type
, double_int cst
)
241 cst
= double_int_ext (cst
, TYPE_PRECISION (type
), TYPE_UNSIGNED (type
));
243 return build_int_cst_wide (type
, cst
.low
, cst
.high
);
246 /* Returns true if CST is negative. Of course, CST is considered to
250 double_int_negative_p (double_int cst
)
255 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
256 comparison is given by UNS. */
259 double_int_cmp (double_int a
, double_int b
, bool uns
)
262 return double_int_ucmp (a
, b
);
264 return double_int_scmp (a
, b
);
267 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
271 double_int_ucmp (double_int a
, double_int b
)
273 if ((unsigned HOST_WIDE_INT
) a
.high
< (unsigned HOST_WIDE_INT
) b
.high
)
275 if ((unsigned HOST_WIDE_INT
) a
.high
> (unsigned HOST_WIDE_INT
) b
.high
)
285 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
289 double_int_scmp (double_int a
, double_int b
)
295 if ((HOST_WIDE_INT
) a
.low
< (HOST_WIDE_INT
) b
.low
)
297 if ((HOST_WIDE_INT
) a
.low
> (HOST_WIDE_INT
) b
.low
)
303 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
306 double_int_split_digit (double_int
*cst
, unsigned base
)
308 unsigned HOST_WIDE_INT resl
, reml
;
309 HOST_WIDE_INT resh
, remh
;
311 div_and_round_double (FLOOR_DIV_EXPR
, true, cst
->low
, cst
->high
, base
, 0,
312 &resl
, &resh
, &reml
, &remh
);
319 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
320 otherwise it is signed. */
323 dump_double_int (FILE *file
, double_int cst
, bool uns
)
325 unsigned digits
[100], n
;
328 if (double_int_zero_p (cst
))
334 if (!uns
&& double_int_negative_p (cst
))
337 cst
= double_int_neg (cst
);
340 for (n
= 0; !double_int_zero_p (cst
); n
++)
341 digits
[n
] = double_int_split_digit (&cst
, 10);
342 for (i
= n
- 1; i
>= 0; i
--)
343 fprintf (file
, "%u", digits
[i
]);