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
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 /* Returns mask for PREC bits. */
28 static inline double_int
29 double_int_mask (unsigned prec
)
31 unsigned HOST_WIDE_INT m
;
34 if (prec
> HOST_BITS_PER_WIDE_INT
)
36 prec
-= HOST_BITS_PER_WIDE_INT
;
37 m
= ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1;
38 mask
.high
= (HOST_WIDE_INT
) m
;
44 mask
.low
= ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1;
50 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
51 outside of the precision are set to the sign bit (i.e., the PREC-th one),
52 otherwise they are set to zero.
54 This corresponds to returning the value represented by PREC lowermost bits
55 of CST, with the given signedness. */
58 double_int_ext (double_int cst
, unsigned prec
, bool uns
)
61 return double_int_zext (cst
, prec
);
63 return double_int_sext (cst
, prec
);
66 /* The same as double_int_ext with UNS = true. */
69 double_int_zext (double_int cst
, unsigned prec
)
71 double_int mask
= double_int_mask (prec
);
74 r
.low
= cst
.low
& mask
.low
;
75 r
.high
= cst
.high
& mask
.high
;
80 /* The same as double_int_ext with UNS = false. */
83 double_int_sext (double_int cst
, unsigned prec
)
85 double_int mask
= double_int_mask (prec
);
87 unsigned HOST_WIDE_INT snum
;
89 if (prec
<= HOST_BITS_PER_WIDE_INT
)
93 prec
-= HOST_BITS_PER_WIDE_INT
;
94 snum
= (unsigned HOST_WIDE_INT
) cst
.high
;
96 if (((snum
>> (prec
- 1)) & 1) == 1)
98 r
.low
= cst
.low
| ~mask
.low
;
99 r
.high
= cst
.high
| ~mask
.high
;
103 r
.low
= cst
.low
& mask
.low
;
104 r
.high
= cst
.high
& mask
.high
;
110 /* Constructs long integer from tree CST. The extra bits over the precision of
111 the number are filled with sign bit if CST is signed, and with zeros if it
115 tree_to_double_int (tree cst
)
117 /* We do not need to call double_int_restrict here to ensure the semantics as
118 described, as this is the default one for trees. */
119 return TREE_INT_CST (cst
);
122 /* Returns true if CST fits in unsigned HOST_WIDE_INT. */
125 double_int_fits_in_uhwi_p (double_int cst
)
127 return cst
.high
== 0;
130 /* Returns true if CST fits in signed HOST_WIDE_INT. */
133 double_int_fits_in_shwi_p (double_int cst
)
136 return (HOST_WIDE_INT
) cst
.low
>= 0;
137 else if (cst
.high
== -1)
138 return (HOST_WIDE_INT
) cst
.low
< 0;
143 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
144 unsigned HOST_WIDE_INT if UNS is true. */
147 double_int_fits_in_hwi_p (double_int cst
, bool uns
)
150 return double_int_fits_in_uhwi_p (cst
);
152 return double_int_fits_in_shwi_p (cst
);
155 /* Returns value of CST as a signed number. CST must satisfy
156 double_int_fits_in_shwi_p. */
159 double_int_to_shwi (double_int cst
)
161 return (HOST_WIDE_INT
) cst
.low
;
164 /* Returns value of CST as an unsigned number. CST must satisfy
165 double_int_fits_in_uhwi_p. */
167 unsigned HOST_WIDE_INT
168 double_int_to_uhwi (double_int cst
)
176 double_int_mul (double_int a
, double_int b
)
179 mul_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
186 double_int_add (double_int a
, double_int b
)
189 add_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
196 double_int_neg (double_int a
)
199 neg_double (a
.low
, a
.high
, &ret
.low
, &ret
.high
);
203 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
204 specified by CODE). CODE is enum tree_code in fact, but double_int.h
205 must be included before tree.h. The remainder after the division is
209 double_int_divmod (double_int a
, double_int b
, bool uns
, unsigned code
,
214 div_and_round_double (code
, uns
, a
.low
, a
.high
, b
.low
, b
.high
,
215 &ret
.low
, &ret
.high
, &mod
->low
, &mod
->high
);
219 /* The same as double_int_divmod with UNS = false. */
222 double_int_sdivmod (double_int a
, double_int b
, unsigned code
, double_int
*mod
)
224 return double_int_divmod (a
, b
, false, code
, mod
);
227 /* The same as double_int_divmod with UNS = true. */
230 double_int_udivmod (double_int a
, double_int b
, unsigned code
, double_int
*mod
)
232 return double_int_divmod (a
, b
, true, code
, mod
);
235 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
236 specified by CODE). CODE is enum tree_code in fact, but double_int.h
237 must be included before tree.h. */
240 double_int_div (double_int a
, double_int b
, bool uns
, unsigned code
)
244 return double_int_divmod (a
, b
, uns
, code
, &mod
);
247 /* The same as double_int_div with UNS = false. */
250 double_int_sdiv (double_int a
, double_int b
, unsigned code
)
252 return double_int_div (a
, b
, false, code
);
255 /* The same as double_int_div with UNS = true. */
258 double_int_udiv (double_int a
, double_int b
, unsigned code
)
260 return double_int_div (a
, b
, true, code
);
263 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
264 specified by CODE). CODE is enum tree_code in fact, but double_int.h
265 must be included before tree.h. */
268 double_int_mod (double_int a
, double_int b
, bool uns
, unsigned code
)
272 double_int_divmod (a
, b
, uns
, code
, &mod
);
276 /* The same as double_int_mod with UNS = false. */
279 double_int_smod (double_int a
, double_int b
, unsigned code
)
281 return double_int_mod (a
, b
, false, code
);
284 /* The same as double_int_mod with UNS = true. */
287 double_int_umod (double_int a
, double_int b
, unsigned code
)
289 return double_int_mod (a
, b
, true, code
);
292 /* Constructs tree in type TYPE from with value given by CST. */
295 double_int_to_tree (tree type
, double_int cst
)
297 cst
= double_int_ext (cst
, TYPE_PRECISION (type
), TYPE_UNSIGNED (type
));
299 return build_int_cst_wide (type
, cst
.low
, cst
.high
);
302 /* Returns true if CST is negative. Of course, CST is considered to
306 double_int_negative_p (double_int cst
)
311 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
312 comparison is given by UNS. */
315 double_int_cmp (double_int a
, double_int b
, bool uns
)
318 return double_int_ucmp (a
, b
);
320 return double_int_scmp (a
, b
);
323 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
327 double_int_ucmp (double_int a
, double_int b
)
329 if ((unsigned HOST_WIDE_INT
) a
.high
< (unsigned HOST_WIDE_INT
) b
.high
)
331 if ((unsigned HOST_WIDE_INT
) a
.high
> (unsigned HOST_WIDE_INT
) b
.high
)
341 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
345 double_int_scmp (double_int a
, double_int b
)
351 if ((HOST_WIDE_INT
) a
.low
< (HOST_WIDE_INT
) b
.low
)
353 if ((HOST_WIDE_INT
) a
.low
> (HOST_WIDE_INT
) b
.low
)
359 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
362 double_int_split_digit (double_int
*cst
, unsigned base
)
364 unsigned HOST_WIDE_INT resl
, reml
;
365 HOST_WIDE_INT resh
, remh
;
367 div_and_round_double (FLOOR_DIV_EXPR
, true, cst
->low
, cst
->high
, base
, 0,
368 &resl
, &resh
, &reml
, &remh
);
375 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
376 otherwise it is signed. */
379 dump_double_int (FILE *file
, double_int cst
, bool uns
)
381 unsigned digits
[100], n
;
384 if (double_int_zero_p (cst
))
390 if (!uns
&& double_int_negative_p (cst
))
393 cst
= double_int_neg (cst
);
396 for (n
= 0; !double_int_zero_p (cst
); n
++)
397 digits
[n
] = double_int_split_digit (&cst
, 10);
398 for (i
= n
- 1; i
>= 0; i
--)
399 fprintf (file
, "%u", digits
[i
]);