gfortran.h (gfc_expr): Remove from_H, add "representation" struct.
[official-gcc.git] / gcc / double-int.c
blob8cbd3abc653e9b183c173ccf7810e9fca92b1906
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
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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
27 /* Returns mask for PREC bits. */
29 double_int
30 double_int_mask (unsigned prec)
32 unsigned HOST_WIDE_INT m;
33 double_int mask;
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;
40 mask.low = ALL_ONES;
42 else
44 mask.high = 0;
45 mask.low = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
48 return mask;
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. */
58 double_int
59 double_int_ext (double_int cst, unsigned prec, bool uns)
61 if (uns)
62 return double_int_zext (cst, prec);
63 else
64 return double_int_sext (cst, prec);
67 /* The same as double_int_ext with UNS = true. */
69 double_int
70 double_int_zext (double_int cst, unsigned prec)
72 double_int mask = double_int_mask (prec);
73 double_int r;
75 r.low = cst.low & mask.low;
76 r.high = cst.high & mask.high;
78 return r;
81 /* The same as double_int_ext with UNS = false. */
83 double_int
84 double_int_sext (double_int cst, unsigned prec)
86 double_int mask = double_int_mask (prec);
87 double_int r;
88 unsigned HOST_WIDE_INT snum;
90 if (prec <= HOST_BITS_PER_WIDE_INT)
91 snum = cst.low;
92 else
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;
102 else
104 r.low = cst.low & mask.low;
105 r.high = cst.high & mask.high;
108 return r;
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
113 is unsigned. */
115 double_int
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. */
125 bool
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. */
133 bool
134 double_int_fits_in_shwi_p (double_int cst)
136 if (cst.high == 0)
137 return (HOST_WIDE_INT) cst.low >= 0;
138 else if (cst.high == -1)
139 return (HOST_WIDE_INT) cst.low < 0;
140 else
141 return false;
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. */
147 bool
148 double_int_fits_in_hwi_p (double_int cst, bool uns)
150 if (uns)
151 return double_int_fits_in_uhwi_p (cst);
152 else
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. */
159 HOST_WIDE_INT
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)
171 return cst.low;
174 /* Returns A * B. */
176 double_int
177 double_int_mul (double_int a, double_int b)
179 double_int ret;
180 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
181 return ret;
184 /* Returns A + B. */
186 double_int
187 double_int_add (double_int a, double_int b)
189 double_int ret;
190 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
191 return ret;
194 /* Returns -A. */
196 double_int
197 double_int_neg (double_int a)
199 double_int ret;
200 neg_double (a.low, a.high, &ret.low, &ret.high);
201 return ret;
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. The remainder after the division is
207 stored to MOD. */
209 double_int
210 double_int_divmod (double_int a, double_int b, bool uns, unsigned code,
211 double_int *mod)
213 double_int ret;
215 div_and_round_double (code, uns, a.low, a.high, b.low, b.high,
216 &ret.low, &ret.high, &mod->low, &mod->high);
217 return ret;
220 /* The same as double_int_divmod with UNS = false. */
222 double_int
223 double_int_sdivmod (double_int a, double_int b, unsigned code, double_int *mod)
225 return double_int_divmod (a, b, false, code, mod);
228 /* The same as double_int_divmod with UNS = true. */
230 double_int
231 double_int_udivmod (double_int a, double_int b, unsigned code, double_int *mod)
233 return double_int_divmod (a, b, true, code, mod);
236 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
237 specified by CODE). CODE is enum tree_code in fact, but double_int.h
238 must be included before tree.h. */
240 double_int
241 double_int_div (double_int a, double_int b, bool uns, unsigned code)
243 double_int mod;
245 return double_int_divmod (a, b, uns, code, &mod);
248 /* The same as double_int_div with UNS = false. */
250 double_int
251 double_int_sdiv (double_int a, double_int b, unsigned code)
253 return double_int_div (a, b, false, code);
256 /* The same as double_int_div with UNS = true. */
258 double_int
259 double_int_udiv (double_int a, double_int b, unsigned code)
261 return double_int_div (a, b, true, code);
264 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
265 specified by CODE). CODE is enum tree_code in fact, but double_int.h
266 must be included before tree.h. */
268 double_int
269 double_int_mod (double_int a, double_int b, bool uns, unsigned code)
271 double_int mod;
273 double_int_divmod (a, b, uns, code, &mod);
274 return mod;
277 /* The same as double_int_mod with UNS = false. */
279 double_int
280 double_int_smod (double_int a, double_int b, unsigned code)
282 return double_int_mod (a, b, false, code);
285 /* The same as double_int_mod with UNS = true. */
287 double_int
288 double_int_umod (double_int a, double_int b, unsigned code)
290 return double_int_mod (a, b, true, code);
293 /* Constructs tree in type TYPE from with value given by CST. Signedness of CST
294 is assumed to be the same as the signedness of TYPE. */
296 tree
297 double_int_to_tree (tree type, double_int cst)
299 cst = double_int_ext (cst, TYPE_PRECISION (type), TYPE_UNSIGNED (type));
301 return build_int_cst_wide (type, cst.low, cst.high);
304 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
305 to be the same as the signedness of TYPE. */
307 bool
308 double_int_fits_to_tree_p (tree type, double_int cst)
310 double_int ext = double_int_ext (cst,
311 TYPE_PRECISION (type),
312 TYPE_UNSIGNED (type));
314 return double_int_equal_p (cst, ext);
317 /* Returns true if CST is negative. Of course, CST is considered to
318 be signed. */
320 bool
321 double_int_negative_p (double_int cst)
323 return cst.high < 0;
326 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
327 comparison is given by UNS. */
330 double_int_cmp (double_int a, double_int b, bool uns)
332 if (uns)
333 return double_int_ucmp (a, b);
334 else
335 return double_int_scmp (a, b);
338 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
339 and 1 if A > B. */
342 double_int_ucmp (double_int a, double_int b)
344 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
345 return -1;
346 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
347 return 1;
348 if (a.low < b.low)
349 return -1;
350 if (a.low > b.low)
351 return 1;
353 return 0;
356 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
357 and 1 if A > B. */
360 double_int_scmp (double_int a, double_int b)
362 if (a.high < b.high)
363 return -1;
364 if (a.high > b.high)
365 return 1;
366 if ((HOST_WIDE_INT) a.low < (HOST_WIDE_INT) b.low)
367 return -1;
368 if ((HOST_WIDE_INT) a.low > (HOST_WIDE_INT) b.low)
369 return 1;
371 return 0;
374 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
376 static unsigned
377 double_int_split_digit (double_int *cst, unsigned base)
379 unsigned HOST_WIDE_INT resl, reml;
380 HOST_WIDE_INT resh, remh;
382 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
383 &resl, &resh, &reml, &remh);
384 cst->high = resh;
385 cst->low = resl;
387 return reml;
390 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
391 otherwise it is signed. */
393 void
394 dump_double_int (FILE *file, double_int cst, bool uns)
396 unsigned digits[100], n;
397 int i;
399 if (double_int_zero_p (cst))
401 fprintf (file, "0");
402 return;
405 if (!uns && double_int_negative_p (cst))
407 fprintf (file, "-");
408 cst = double_int_neg (cst);
411 for (n = 0; !double_int_zero_p (cst); n++)
412 digits[n] = double_int_split_digit (&cst, 10);
413 for (i = n - 1; i >= 0; i--)
414 fprintf (file, "%u", digits[i]);
418 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
419 otherwise. */
421 void
422 mpz_set_double_int (mpz_t result, double_int val, bool uns)
424 bool negate = false;
425 unsigned HOST_WIDE_INT vp[2];
427 if (!uns && double_int_negative_p (val))
429 negate = true;
430 val = double_int_neg (val);
433 vp[0] = val.low;
434 vp[1] = (unsigned HOST_WIDE_INT) val.high;
435 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
437 if (negate)
438 mpz_neg (result, result);
441 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
442 values of VAL will be wrapped; otherwise, they will be set to the
443 appropriate minimum or maximum TYPE bound. */
445 double_int
446 mpz_get_double_int (tree type, mpz_t val, bool wrap)
448 unsigned HOST_WIDE_INT *vp;
449 size_t count, numb;
450 double_int res;
452 if (!wrap)
454 mpz_t min, max;
456 mpz_init (min);
457 mpz_init (max);
458 get_type_static_bounds (type, min, max);
460 if (mpz_cmp (val, min) < 0)
461 mpz_set (val, min);
462 else if (mpz_cmp (val, max) > 0)
463 mpz_set (val, max);
465 mpz_clear (min);
466 mpz_clear (max);
469 /* Determine the number of unsigned HOST_WIDE_INT that are required
470 for representing the value. The code to calculate count is
471 extracted from the GMP manual, section "Integer Import and Export":
472 http://gmplib.org/manual/Integer-Import-and-Export.html */
473 numb = 8*sizeof(HOST_WIDE_INT);
474 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
475 if (count < 2)
476 count = 2;
477 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof(HOST_WIDE_INT));
479 vp[0] = 0;
480 vp[1] = 0;
481 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
483 gcc_assert (wrap || count <= 2);
485 res.low = vp[0];
486 res.high = (HOST_WIDE_INT) vp[1];
488 res = double_int_ext (res, TYPE_PRECISION (type), TYPE_UNSIGNED (type));
489 if (mpz_sgn (val) < 0)
490 res = double_int_neg (res);
492 return res;