Daily bump.
[official-gcc.git] / gcc / double-int.c
blob7ffcac25a75c59a0092b96e0dfe07819fa7ee7ff
1 /* Operations with long integers.
2 Copyright (C) 2006-2015 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h" /* For BITS_PER_UNIT and *_BIG_ENDIAN. */
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
28 static int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
29 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
30 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
31 bool);
33 #define add_double(l1,h1,l2,h2,lv,hv) \
34 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
36 static int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
37 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
39 static int mul_double_wide_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
40 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
41 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
42 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
43 bool);
45 #define mul_double(l1,h1,l2,h2,lv,hv) \
46 mul_double_wide_with_sign (l1, h1, l2, h2, lv, hv, NULL, NULL, false)
48 static int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
49 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
50 HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
51 HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
52 HOST_WIDE_INT *);
54 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
55 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
56 and SUM1. Then this yields nonzero if overflow occurred during the
57 addition.
59 Overflow occurs if A and B have the same sign, but A and SUM differ in
60 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
61 sign. */
62 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
64 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
65 We do that by representing the two-word integer in 4 words, with only
66 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
67 number. The value of the word is LOWPART + HIGHPART * BASE. */
69 #define LOWPART(x) \
70 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
71 #define HIGHPART(x) \
72 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
73 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
75 /* Unpack a two-word integer into 4 words.
76 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
77 WORDS points to the array of HOST_WIDE_INTs. */
79 static void
80 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
82 words[0] = LOWPART (low);
83 words[1] = HIGHPART (low);
84 words[2] = LOWPART (hi);
85 words[3] = HIGHPART (hi);
88 /* Pack an array of 4 words into a two-word integer.
89 WORDS points to the array of words.
90 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
92 static void
93 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
94 HOST_WIDE_INT *hi)
96 *low = words[0] + words[1] * BASE;
97 *hi = words[2] + words[3] * BASE;
100 /* Add two doubleword integers with doubleword result.
101 Return nonzero if the operation overflows according to UNSIGNED_P.
102 Each argument is given as two `HOST_WIDE_INT' pieces.
103 One argument is L1 and H1; the other, L2 and H2.
104 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
106 static int
107 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
108 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
109 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
110 bool unsigned_p)
112 unsigned HOST_WIDE_INT l;
113 HOST_WIDE_INT h;
115 l = l1 + l2;
116 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
117 + (unsigned HOST_WIDE_INT) h2
118 + (l < l1));
120 *lv = l;
121 *hv = h;
123 if (unsigned_p)
124 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
125 || (h == h1
126 && l < l1));
127 else
128 return OVERFLOW_SUM_SIGN (h1, h2, h);
131 /* Negate a doubleword integer with doubleword result.
132 Return nonzero if the operation overflows, assuming it's signed.
133 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
134 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
136 static int
137 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
138 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
140 if (l1 == 0)
142 *lv = 0;
143 *hv = - (unsigned HOST_WIDE_INT) h1;
144 return (*hv & h1) < 0;
146 else
148 *lv = -l1;
149 *hv = ~h1;
150 return 0;
154 /* Multiply two doubleword integers with quadword result.
155 Return nonzero if the operation overflows according to UNSIGNED_P.
156 Each argument is given as two `HOST_WIDE_INT' pieces.
157 One argument is L1 and H1; the other, L2 and H2.
158 The value is stored as four `HOST_WIDE_INT' pieces in *LV and *HV,
159 *LW and *HW.
160 If lw is NULL then only the low part and no overflow is computed. */
162 static int
163 mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
164 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
165 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
166 unsigned HOST_WIDE_INT *lw, HOST_WIDE_INT *hw,
167 bool unsigned_p)
169 HOST_WIDE_INT arg1[4];
170 HOST_WIDE_INT arg2[4];
171 HOST_WIDE_INT prod[4 * 2];
172 unsigned HOST_WIDE_INT carry;
173 int i, j, k;
174 unsigned HOST_WIDE_INT neglow;
175 HOST_WIDE_INT neghigh;
177 encode (arg1, l1, h1);
178 encode (arg2, l2, h2);
180 memset (prod, 0, sizeof prod);
182 for (i = 0; i < 4; i++)
184 carry = 0;
185 for (j = 0; j < 4; j++)
187 k = i + j;
188 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
189 carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j];
190 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
191 carry += prod[k];
192 prod[k] = LOWPART (carry);
193 carry = HIGHPART (carry);
195 prod[i + 4] = carry;
198 decode (prod, lv, hv);
200 /* We are not interested in the wide part nor in overflow. */
201 if (lw == NULL)
202 return 0;
204 decode (prod + 4, lw, hw);
206 /* Unsigned overflow is immediate. */
207 if (unsigned_p)
208 return (*lw | *hw) != 0;
210 /* Check for signed overflow by calculating the signed representation of the
211 top half of the result; it should agree with the low half's sign bit. */
212 if (h1 < 0)
214 neg_double (l2, h2, &neglow, &neghigh);
215 add_double (neglow, neghigh, *lw, *hw, lw, hw);
217 if (h2 < 0)
219 neg_double (l1, h1, &neglow, &neghigh);
220 add_double (neglow, neghigh, *lw, *hw, lw, hw);
222 return (*hv < 0 ? ~(*lw & *hw) : *lw | *hw) != 0;
225 /* Shift the doubleword integer in L1, H1 right by COUNT places
226 keeping only PREC bits of result. ARITH nonzero specifies
227 arithmetic shifting; otherwise use logical shift.
228 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
230 static void
231 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
232 unsigned HOST_WIDE_INT count, unsigned int prec,
233 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
234 bool arith)
236 unsigned HOST_WIDE_INT signmask;
238 signmask = (arith
239 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
240 : 0);
242 if (count >= HOST_BITS_PER_DOUBLE_INT)
244 /* Shifting by the host word size is undefined according to the
245 ANSI standard, so we must handle this as a special case. */
246 *hv = 0;
247 *lv = 0;
249 else if (count >= HOST_BITS_PER_WIDE_INT)
251 *hv = 0;
252 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
254 else
256 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
257 *lv = ((l1 >> count)
258 | ((unsigned HOST_WIDE_INT) h1
259 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
262 /* Zero / sign extend all bits that are beyond the precision. */
264 if (count >= prec)
266 *hv = signmask;
267 *lv = signmask;
269 else if ((prec - count) >= HOST_BITS_PER_DOUBLE_INT)
271 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
273 *hv &= ~(HOST_WIDE_INT_M1U << (prec - count - HOST_BITS_PER_WIDE_INT));
274 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
276 else
278 *hv = signmask;
279 *lv &= ~(HOST_WIDE_INT_M1U << (prec - count));
280 *lv |= signmask << (prec - count);
284 /* Shift the doubleword integer in L1, H1 left by COUNT places
285 keeping only PREC bits of result.
286 Shift right if COUNT is negative.
287 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
288 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
290 static void
291 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
292 unsigned HOST_WIDE_INT count, unsigned int prec,
293 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
295 unsigned HOST_WIDE_INT signmask;
297 if (count >= HOST_BITS_PER_DOUBLE_INT)
299 /* Shifting by the host word size is undefined according to the
300 ANSI standard, so we must handle this as a special case. */
301 *hv = 0;
302 *lv = 0;
304 else if (count >= HOST_BITS_PER_WIDE_INT)
306 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
307 *lv = 0;
309 else
311 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
312 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
313 *lv = l1 << count;
316 /* Sign extend all bits that are beyond the precision. */
318 signmask = -((prec > HOST_BITS_PER_WIDE_INT
319 ? ((unsigned HOST_WIDE_INT) *hv
320 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
321 : (*lv >> (prec - 1))) & 1);
323 if (prec >= HOST_BITS_PER_DOUBLE_INT)
325 else if (prec >= HOST_BITS_PER_WIDE_INT)
327 *hv &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
328 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
330 else
332 *hv = signmask;
333 *lv &= ~(HOST_WIDE_INT_M1U << prec);
334 *lv |= signmask << prec;
338 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
339 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
340 CODE is a tree code for a kind of division, one of
341 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
342 or EXACT_DIV_EXPR
343 It controls how the quotient is rounded to an integer.
344 Return nonzero if the operation overflows.
345 UNS nonzero says do unsigned division. */
347 static int
348 div_and_round_double (unsigned code, int uns,
349 /* num == numerator == dividend */
350 unsigned HOST_WIDE_INT lnum_orig,
351 HOST_WIDE_INT hnum_orig,
352 /* den == denominator == divisor */
353 unsigned HOST_WIDE_INT lden_orig,
354 HOST_WIDE_INT hden_orig,
355 unsigned HOST_WIDE_INT *lquo,
356 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
357 HOST_WIDE_INT *hrem)
359 int quo_neg = 0;
360 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
361 HOST_WIDE_INT den[4], quo[4];
362 int i, j;
363 unsigned HOST_WIDE_INT work;
364 unsigned HOST_WIDE_INT carry = 0;
365 unsigned HOST_WIDE_INT lnum = lnum_orig;
366 HOST_WIDE_INT hnum = hnum_orig;
367 unsigned HOST_WIDE_INT lden = lden_orig;
368 HOST_WIDE_INT hden = hden_orig;
369 int overflow = 0;
371 if (hden == 0 && lden == 0)
372 overflow = 1, lden = 1;
374 /* Calculate quotient sign and convert operands to unsigned. */
375 if (!uns)
377 if (hnum < 0)
379 quo_neg = ~ quo_neg;
380 /* (minimum integer) / (-1) is the only overflow case. */
381 if (neg_double (lnum, hnum, &lnum, &hnum)
382 && ((HOST_WIDE_INT) lden & hden) == -1)
383 overflow = 1;
385 if (hden < 0)
387 quo_neg = ~ quo_neg;
388 neg_double (lden, hden, &lden, &hden);
392 if (hnum == 0 && hden == 0)
393 { /* single precision */
394 *hquo = *hrem = 0;
395 /* This unsigned division rounds toward zero. */
396 *lquo = lnum / lden;
397 goto finish_up;
400 if (hnum == 0)
401 { /* trivial case: dividend < divisor */
402 /* hden != 0 already checked. */
403 *hquo = *lquo = 0;
404 *hrem = hnum;
405 *lrem = lnum;
406 goto finish_up;
409 memset (quo, 0, sizeof quo);
411 memset (num, 0, sizeof num); /* to zero 9th element */
412 memset (den, 0, sizeof den);
414 encode (num, lnum, hnum);
415 encode (den, lden, hden);
417 /* Special code for when the divisor < BASE. */
418 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
420 /* hnum != 0 already checked. */
421 for (i = 4 - 1; i >= 0; i--)
423 work = num[i] + carry * BASE;
424 quo[i] = work / lden;
425 carry = work % lden;
428 else
430 /* Full double precision division,
431 with thanks to Don Knuth's "Seminumerical Algorithms". */
432 int num_hi_sig, den_hi_sig;
433 unsigned HOST_WIDE_INT quo_est, scale;
435 /* Find the highest nonzero divisor digit. */
436 for (i = 4 - 1;; i--)
437 if (den[i] != 0)
439 den_hi_sig = i;
440 break;
443 /* Insure that the first digit of the divisor is at least BASE/2.
444 This is required by the quotient digit estimation algorithm. */
446 scale = BASE / (den[den_hi_sig] + 1);
447 if (scale > 1)
448 { /* scale divisor and dividend */
449 carry = 0;
450 for (i = 0; i <= 4 - 1; i++)
452 work = (num[i] * scale) + carry;
453 num[i] = LOWPART (work);
454 carry = HIGHPART (work);
457 num[4] = carry;
458 carry = 0;
459 for (i = 0; i <= 4 - 1; i++)
461 work = (den[i] * scale) + carry;
462 den[i] = LOWPART (work);
463 carry = HIGHPART (work);
464 if (den[i] != 0) den_hi_sig = i;
468 num_hi_sig = 4;
470 /* Main loop */
471 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
473 /* Guess the next quotient digit, quo_est, by dividing the first
474 two remaining dividend digits by the high order quotient digit.
475 quo_est is never low and is at most 2 high. */
476 unsigned HOST_WIDE_INT tmp;
478 num_hi_sig = i + den_hi_sig + 1;
479 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
480 if (num[num_hi_sig] != den[den_hi_sig])
481 quo_est = work / den[den_hi_sig];
482 else
483 quo_est = BASE - 1;
485 /* Refine quo_est so it's usually correct, and at most one high. */
486 tmp = work - quo_est * den[den_hi_sig];
487 if (tmp < BASE
488 && (den[den_hi_sig - 1] * quo_est
489 > (tmp * BASE + num[num_hi_sig - 2])))
490 quo_est--;
492 /* Try QUO_EST as the quotient digit, by multiplying the
493 divisor by QUO_EST and subtracting from the remaining dividend.
494 Keep in mind that QUO_EST is the I - 1st digit. */
496 carry = 0;
497 for (j = 0; j <= den_hi_sig; j++)
499 work = quo_est * den[j] + carry;
500 carry = HIGHPART (work);
501 work = num[i + j] - LOWPART (work);
502 num[i + j] = LOWPART (work);
503 carry += HIGHPART (work) != 0;
506 /* If quo_est was high by one, then num[i] went negative and
507 we need to correct things. */
508 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
510 quo_est--;
511 carry = 0; /* add divisor back in */
512 for (j = 0; j <= den_hi_sig; j++)
514 work = num[i + j] + den[j] + carry;
515 carry = HIGHPART (work);
516 num[i + j] = LOWPART (work);
519 num [num_hi_sig] += carry;
522 /* Store the quotient digit. */
523 quo[i] = quo_est;
527 decode (quo, lquo, hquo);
529 finish_up:
530 /* If result is negative, make it so. */
531 if (quo_neg)
532 neg_double (*lquo, *hquo, lquo, hquo);
534 /* Compute trial remainder: rem = num - (quo * den) */
535 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
536 neg_double (*lrem, *hrem, lrem, hrem);
537 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
539 switch (code)
541 case TRUNC_DIV_EXPR:
542 case TRUNC_MOD_EXPR: /* round toward zero */
543 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
544 return overflow;
546 case FLOOR_DIV_EXPR:
547 case FLOOR_MOD_EXPR: /* round toward negative infinity */
548 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
550 /* quo = quo - 1; */
551 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
552 lquo, hquo);
554 else
555 return overflow;
556 break;
558 case CEIL_DIV_EXPR:
559 case CEIL_MOD_EXPR: /* round toward positive infinity */
560 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
562 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
563 lquo, hquo);
565 else
566 return overflow;
567 break;
569 case ROUND_DIV_EXPR:
570 case ROUND_MOD_EXPR: /* round to closest integer */
572 unsigned HOST_WIDE_INT labs_rem = *lrem;
573 HOST_WIDE_INT habs_rem = *hrem;
574 unsigned HOST_WIDE_INT labs_den = lden, lnegabs_rem, ldiff;
575 HOST_WIDE_INT habs_den = hden, hnegabs_rem, hdiff;
577 /* Get absolute values. */
578 if (!uns && *hrem < 0)
579 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
580 if (!uns && hden < 0)
581 neg_double (lden, hden, &labs_den, &habs_den);
583 /* If abs(rem) >= abs(den) - abs(rem), adjust the quotient. */
584 neg_double (labs_rem, habs_rem, &lnegabs_rem, &hnegabs_rem);
585 add_double (labs_den, habs_den, lnegabs_rem, hnegabs_rem,
586 &ldiff, &hdiff);
588 if (((unsigned HOST_WIDE_INT) habs_rem
589 > (unsigned HOST_WIDE_INT) hdiff)
590 || (habs_rem == hdiff && labs_rem >= ldiff))
592 if (quo_neg)
593 /* quo = quo - 1; */
594 add_double (*lquo, *hquo,
595 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
596 else
597 /* quo = quo + 1; */
598 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
599 lquo, hquo);
601 else
602 return overflow;
604 break;
606 default:
607 gcc_unreachable ();
610 /* Compute true remainder: rem = num - (quo * den) */
611 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
612 neg_double (*lrem, *hrem, lrem, hrem);
613 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
614 return overflow;
618 /* Construct from a buffer of length LEN. BUFFER will be read according
619 to byte endianess and word endianess. Only the lower LEN bytes
620 of the result are set; the remaining high bytes are cleared. */
622 double_int
623 double_int::from_buffer (const unsigned char *buffer, int len)
625 double_int result = double_int_zero;
626 int words = len / UNITS_PER_WORD;
628 gcc_assert (len * BITS_PER_UNIT <= HOST_BITS_PER_DOUBLE_INT);
630 for (int byte = 0; byte < len; byte++)
632 int offset;
633 int bitpos = byte * BITS_PER_UNIT;
634 unsigned HOST_WIDE_INT value;
636 if (len > UNITS_PER_WORD)
638 int word = byte / UNITS_PER_WORD;
640 if (WORDS_BIG_ENDIAN)
641 word = (words - 1) - word;
643 offset = word * UNITS_PER_WORD;
645 if (BYTES_BIG_ENDIAN)
646 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
647 else
648 offset += byte % UNITS_PER_WORD;
650 else
651 offset = BYTES_BIG_ENDIAN ? (len - 1) - byte : byte;
653 value = (unsigned HOST_WIDE_INT) buffer[offset];
655 if (bitpos < HOST_BITS_PER_WIDE_INT)
656 result.low |= value << bitpos;
657 else
658 result.high |= value << (bitpos - HOST_BITS_PER_WIDE_INT);
661 return result;
665 /* Returns mask for PREC bits. */
667 double_int
668 double_int::mask (unsigned prec)
670 unsigned HOST_WIDE_INT m;
671 double_int mask;
673 if (prec > HOST_BITS_PER_WIDE_INT)
675 prec -= HOST_BITS_PER_WIDE_INT;
676 m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
677 mask.high = (HOST_WIDE_INT) m;
678 mask.low = ALL_ONES;
680 else
682 mask.high = 0;
683 mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0;
686 return mask;
689 /* Returns a maximum value for signed or unsigned integer
690 of precision PREC. */
692 double_int
693 double_int::max_value (unsigned int prec, bool uns)
695 return double_int::mask (prec - (uns ? 0 : 1));
698 /* Returns a minimum value for signed or unsigned integer
699 of precision PREC. */
701 double_int
702 double_int::min_value (unsigned int prec, bool uns)
704 if (uns)
705 return double_int_zero;
706 return double_int_one.lshift (prec - 1, prec, false);
709 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
710 outside of the precision are set to the sign bit (i.e., the PREC-th one),
711 otherwise they are set to zero.
713 This corresponds to returning the value represented by PREC lowermost bits
714 of CST, with the given signedness. */
716 double_int
717 double_int::ext (unsigned prec, bool uns) const
719 if (uns)
720 return this->zext (prec);
721 else
722 return this->sext (prec);
725 /* The same as double_int::ext with UNS = true. */
727 double_int
728 double_int::zext (unsigned prec) const
730 const double_int &cst = *this;
731 double_int mask = double_int::mask (prec);
732 double_int r;
734 r.low = cst.low & mask.low;
735 r.high = cst.high & mask.high;
737 return r;
740 /* The same as double_int::ext with UNS = false. */
742 double_int
743 double_int::sext (unsigned prec) const
745 const double_int &cst = *this;
746 double_int mask = double_int::mask (prec);
747 double_int r;
748 unsigned HOST_WIDE_INT snum;
750 if (prec <= HOST_BITS_PER_WIDE_INT)
751 snum = cst.low;
752 else
754 prec -= HOST_BITS_PER_WIDE_INT;
755 snum = (unsigned HOST_WIDE_INT) cst.high;
757 if (((snum >> (prec - 1)) & 1) == 1)
759 r.low = cst.low | ~mask.low;
760 r.high = cst.high | ~mask.high;
762 else
764 r.low = cst.low & mask.low;
765 r.high = cst.high & mask.high;
768 return r;
771 /* Returns true if CST fits in signed HOST_WIDE_INT. */
773 bool
774 double_int::fits_shwi () const
776 const double_int &cst = *this;
777 if (cst.high == 0)
778 return (HOST_WIDE_INT) cst.low >= 0;
779 else if (cst.high == -1)
780 return (HOST_WIDE_INT) cst.low < 0;
781 else
782 return false;
785 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
786 unsigned HOST_WIDE_INT if UNS is true. */
788 bool
789 double_int::fits_hwi (bool uns) const
791 if (uns)
792 return this->fits_uhwi ();
793 else
794 return this->fits_shwi ();
797 /* Returns A * B. */
799 double_int
800 double_int::operator * (double_int b) const
802 const double_int &a = *this;
803 double_int ret;
804 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
805 return ret;
808 /* Multiplies *this with B and returns a reference to *this. */
810 double_int &
811 double_int::operator *= (double_int b)
813 mul_double (low, high, b.low, b.high, &low, &high);
814 return *this;
817 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
818 *OVERFLOW is set to nonzero. */
820 double_int
821 double_int::mul_with_sign (double_int b, bool unsigned_p, bool *overflow) const
823 const double_int &a = *this;
824 double_int ret, tem;
825 *overflow = mul_double_wide_with_sign (a.low, a.high, b.low, b.high,
826 &ret.low, &ret.high,
827 &tem.low, &tem.high, unsigned_p);
828 return ret;
831 double_int
832 double_int::wide_mul_with_sign (double_int b, bool unsigned_p,
833 double_int *higher, bool *overflow) const
836 double_int lower;
837 *overflow = mul_double_wide_with_sign (low, high, b.low, b.high,
838 &lower.low, &lower.high,
839 &higher->low, &higher->high,
840 unsigned_p);
841 return lower;
844 /* Returns A + B. */
846 double_int
847 double_int::operator + (double_int b) const
849 const double_int &a = *this;
850 double_int ret;
851 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
852 return ret;
855 /* Adds B to *this and returns a reference to *this. */
857 double_int &
858 double_int::operator += (double_int b)
860 add_double (low, high, b.low, b.high, &low, &high);
861 return *this;
865 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
866 *OVERFLOW is set to nonzero. */
868 double_int
869 double_int::add_with_sign (double_int b, bool unsigned_p, bool *overflow) const
871 const double_int &a = *this;
872 double_int ret;
873 *overflow = add_double_with_sign (a.low, a.high, b.low, b.high,
874 &ret.low, &ret.high, unsigned_p);
875 return ret;
878 /* Returns A - B. */
880 double_int
881 double_int::operator - (double_int b) const
883 const double_int &a = *this;
884 double_int ret;
885 neg_double (b.low, b.high, &b.low, &b.high);
886 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
887 return ret;
890 /* Subtracts B from *this and returns a reference to *this. */
892 double_int &
893 double_int::operator -= (double_int b)
895 neg_double (b.low, b.high, &b.low, &b.high);
896 add_double (low, high, b.low, b.high, &low, &high);
897 return *this;
901 /* Returns A - B. If the operation overflows via inconsistent sign bits,
902 *OVERFLOW is set to nonzero. */
904 double_int
905 double_int::sub_with_overflow (double_int b, bool *overflow) const
907 double_int ret;
908 neg_double (b.low, b.high, &ret.low, &ret.high);
909 add_double (low, high, ret.low, ret.high, &ret.low, &ret.high);
910 *overflow = OVERFLOW_SUM_SIGN (ret.high, b.high, high);
911 return ret;
914 /* Returns -A. */
916 double_int
917 double_int::operator - () const
919 const double_int &a = *this;
920 double_int ret;
921 neg_double (a.low, a.high, &ret.low, &ret.high);
922 return ret;
925 double_int
926 double_int::neg_with_overflow (bool *overflow) const
928 double_int ret;
929 *overflow = neg_double (low, high, &ret.low, &ret.high);
930 return ret;
933 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
934 specified by CODE). CODE is enum tree_code in fact, but double_int.h
935 must be included before tree.h. The remainder after the division is
936 stored to MOD. */
938 double_int
939 double_int::divmod_with_overflow (double_int b, bool uns, unsigned code,
940 double_int *mod, bool *overflow) const
942 const double_int &a = *this;
943 double_int ret;
945 *overflow = div_and_round_double (code, uns, a.low, a.high,
946 b.low, b.high, &ret.low, &ret.high,
947 &mod->low, &mod->high);
948 return ret;
951 double_int
952 double_int::divmod (double_int b, bool uns, unsigned code,
953 double_int *mod) const
955 const double_int &a = *this;
956 double_int ret;
958 div_and_round_double (code, uns, a.low, a.high,
959 b.low, b.high, &ret.low, &ret.high,
960 &mod->low, &mod->high);
961 return ret;
964 /* The same as double_int::divmod with UNS = false. */
966 double_int
967 double_int::sdivmod (double_int b, unsigned code, double_int *mod) const
969 return this->divmod (b, false, code, mod);
972 /* The same as double_int::divmod with UNS = true. */
974 double_int
975 double_int::udivmod (double_int b, unsigned code, double_int *mod) const
977 return this->divmod (b, true, code, mod);
980 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
981 specified by CODE). CODE is enum tree_code in fact, but double_int.h
982 must be included before tree.h. */
984 double_int
985 double_int::div (double_int b, bool uns, unsigned code) const
987 double_int mod;
989 return this->divmod (b, uns, code, &mod);
992 /* The same as double_int::div with UNS = false. */
994 double_int
995 double_int::sdiv (double_int b, unsigned code) const
997 return this->div (b, false, code);
1000 /* The same as double_int::div with UNS = true. */
1002 double_int
1003 double_int::udiv (double_int b, unsigned code) const
1005 return this->div (b, true, code);
1008 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
1009 specified by CODE). CODE is enum tree_code in fact, but double_int.h
1010 must be included before tree.h. */
1012 double_int
1013 double_int::mod (double_int b, bool uns, unsigned code) const
1015 double_int mod;
1017 this->divmod (b, uns, code, &mod);
1018 return mod;
1021 /* The same as double_int::mod with UNS = false. */
1023 double_int
1024 double_int::smod (double_int b, unsigned code) const
1026 return this->mod (b, false, code);
1029 /* The same as double_int::mod with UNS = true. */
1031 double_int
1032 double_int::umod (double_int b, unsigned code) const
1034 return this->mod (b, true, code);
1037 /* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
1038 the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
1039 unchanged. */
1041 bool
1042 double_int::multiple_of (double_int factor,
1043 bool unsigned_p, double_int *multiple) const
1045 double_int remainder;
1046 double_int quotient = this->divmod (factor, unsigned_p,
1047 TRUNC_DIV_EXPR, &remainder);
1048 if (remainder.is_zero ())
1050 *multiple = quotient;
1051 return true;
1054 return false;
1057 /* Set BITPOS bit in A. */
1058 double_int
1059 double_int::set_bit (unsigned bitpos) const
1061 double_int a = *this;
1062 if (bitpos < HOST_BITS_PER_WIDE_INT)
1063 a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
1064 else
1065 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1067 return a;
1070 /* Count trailing zeros in A. */
1072 double_int::trailing_zeros () const
1074 const double_int &a = *this;
1075 unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
1076 unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
1077 if (!w)
1078 return HOST_BITS_PER_DOUBLE_INT;
1079 bits += ctz_hwi (w);
1080 return bits;
1083 /* Shift A left by COUNT places. */
1085 double_int
1086 double_int::lshift (HOST_WIDE_INT count) const
1088 double_int ret;
1090 gcc_checking_assert (count >= 0);
1092 if (count >= HOST_BITS_PER_DOUBLE_INT)
1094 /* Shifting by the host word size is undefined according to the
1095 ANSI standard, so we must handle this as a special case. */
1096 ret.high = 0;
1097 ret.low = 0;
1099 else if (count >= HOST_BITS_PER_WIDE_INT)
1101 ret.high = low << (count - HOST_BITS_PER_WIDE_INT);
1102 ret.low = 0;
1104 else
1106 ret.high = (((unsigned HOST_WIDE_INT) high << count)
1107 | (low >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
1108 ret.low = low << count;
1111 return ret;
1114 /* Shift A right by COUNT places. */
1116 double_int
1117 double_int::rshift (HOST_WIDE_INT count) const
1119 double_int ret;
1121 gcc_checking_assert (count >= 0);
1123 if (count >= HOST_BITS_PER_DOUBLE_INT)
1125 /* Shifting by the host word size is undefined according to the
1126 ANSI standard, so we must handle this as a special case. */
1127 ret.high = 0;
1128 ret.low = 0;
1130 else if (count >= HOST_BITS_PER_WIDE_INT)
1132 ret.high = 0;
1133 ret.low
1134 = (unsigned HOST_WIDE_INT) (high >> (count - HOST_BITS_PER_WIDE_INT));
1136 else
1138 ret.high = high >> count;
1139 ret.low = ((low >> count)
1140 | ((unsigned HOST_WIDE_INT) high
1141 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
1144 return ret;
1147 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
1148 right if COUNT is negative. ARITH true specifies arithmetic shifting;
1149 otherwise use logical shift. */
1151 double_int
1152 double_int::lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
1154 double_int ret;
1155 if (count > 0)
1156 lshift_double (low, high, count, prec, &ret.low, &ret.high);
1157 else
1158 rshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high, arith);
1159 return ret;
1162 /* Shift A right by COUNT places keeping only PREC bits of result. Shift
1163 left if COUNT is negative. ARITH true specifies arithmetic shifting;
1164 otherwise use logical shift. */
1166 double_int
1167 double_int::rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
1169 double_int ret;
1170 if (count > 0)
1171 rshift_double (low, high, count, prec, &ret.low, &ret.high, arith);
1172 else
1173 lshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high);
1174 return ret;
1177 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
1178 Shift right if COUNT is negative. */
1180 double_int
1181 double_int::alshift (HOST_WIDE_INT count, unsigned int prec) const
1183 double_int r;
1184 if (count > 0)
1185 lshift_double (low, high, count, prec, &r.low, &r.high);
1186 else
1187 rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, true);
1188 return r;
1191 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
1192 Shift left if COUNT is negative. */
1194 double_int
1195 double_int::arshift (HOST_WIDE_INT count, unsigned int prec) const
1197 double_int r;
1198 if (count > 0)
1199 rshift_double (low, high, count, prec, &r.low, &r.high, true);
1200 else
1201 lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
1202 return r;
1205 /* Logical shift A left by COUNT places keeping only PREC bits of result.
1206 Shift right if COUNT is negative. */
1208 double_int
1209 double_int::llshift (HOST_WIDE_INT count, unsigned int prec) const
1211 double_int r;
1212 if (count > 0)
1213 lshift_double (low, high, count, prec, &r.low, &r.high);
1214 else
1215 rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, false);
1216 return r;
1219 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1220 Shift left if COUNT is negative. */
1222 double_int
1223 double_int::lrshift (HOST_WIDE_INT count, unsigned int prec) const
1225 double_int r;
1226 if (count > 0)
1227 rshift_double (low, high, count, prec, &r.low, &r.high, false);
1228 else
1229 lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
1230 return r;
1233 /* Rotate A left by COUNT places keeping only PREC bits of result.
1234 Rotate right if COUNT is negative. */
1236 double_int
1237 double_int::lrotate (HOST_WIDE_INT count, unsigned int prec) const
1239 double_int t1, t2;
1241 count %= prec;
1242 if (count < 0)
1243 count += prec;
1245 t1 = this->llshift (count, prec);
1246 t2 = this->lrshift (prec - count, prec);
1248 return t1 | t2;
1251 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1252 Rotate right if COUNT is negative. */
1254 double_int
1255 double_int::rrotate (HOST_WIDE_INT count, unsigned int prec) const
1257 double_int t1, t2;
1259 count %= prec;
1260 if (count < 0)
1261 count += prec;
1263 t1 = this->lrshift (count, prec);
1264 t2 = this->llshift (prec - count, prec);
1266 return t1 | t2;
1269 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1270 comparison is given by UNS. */
1273 double_int::cmp (double_int b, bool uns) const
1275 if (uns)
1276 return this->ucmp (b);
1277 else
1278 return this->scmp (b);
1281 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
1282 and 1 if A > B. */
1285 double_int::ucmp (double_int b) const
1287 const double_int &a = *this;
1288 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
1289 return -1;
1290 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
1291 return 1;
1292 if (a.low < b.low)
1293 return -1;
1294 if (a.low > b.low)
1295 return 1;
1297 return 0;
1300 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1301 and 1 if A > B. */
1304 double_int::scmp (double_int b) const
1306 const double_int &a = *this;
1307 if (a.high < b.high)
1308 return -1;
1309 if (a.high > b.high)
1310 return 1;
1311 if (a.low < b.low)
1312 return -1;
1313 if (a.low > b.low)
1314 return 1;
1316 return 0;
1319 /* Compares two unsigned values A and B for less-than. */
1321 bool
1322 double_int::ult (double_int b) const
1324 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1325 return true;
1326 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1327 return false;
1328 if (low < b.low)
1329 return true;
1330 return false;
1333 /* Compares two unsigned values A and B for less-than or equal-to. */
1335 bool
1336 double_int::ule (double_int b) const
1338 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1339 return true;
1340 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1341 return false;
1342 if (low <= b.low)
1343 return true;
1344 return false;
1347 /* Compares two unsigned values A and B for greater-than. */
1349 bool
1350 double_int::ugt (double_int b) const
1352 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1353 return true;
1354 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1355 return false;
1356 if (low > b.low)
1357 return true;
1358 return false;
1361 /* Compares two signed values A and B for less-than. */
1363 bool
1364 double_int::slt (double_int b) const
1366 if (high < b.high)
1367 return true;
1368 if (high > b.high)
1369 return false;
1370 if (low < b.low)
1371 return true;
1372 return false;
1375 /* Compares two signed values A and B for less-than or equal-to. */
1377 bool
1378 double_int::sle (double_int b) const
1380 if (high < b.high)
1381 return true;
1382 if (high > b.high)
1383 return false;
1384 if (low <= b.low)
1385 return true;
1386 return false;
1389 /* Compares two signed values A and B for greater-than. */
1391 bool
1392 double_int::sgt (double_int b) const
1394 if (high > b.high)
1395 return true;
1396 if (high < b.high)
1397 return false;
1398 if (low > b.low)
1399 return true;
1400 return false;
1404 /* Compares two values A and B. Returns max value. Signedness of the
1405 comparison is given by UNS. */
1407 double_int
1408 double_int::max (double_int b, bool uns)
1410 return (this->cmp (b, uns) == 1) ? *this : b;
1413 /* Compares two signed values A and B. Returns max value. */
1415 double_int
1416 double_int::smax (double_int b)
1418 return (this->scmp (b) == 1) ? *this : b;
1421 /* Compares two unsigned values A and B. Returns max value. */
1423 double_int
1424 double_int::umax (double_int b)
1426 return (this->ucmp (b) == 1) ? *this : b;
1429 /* Compares two values A and B. Returns mix value. Signedness of the
1430 comparison is given by UNS. */
1432 double_int
1433 double_int::min (double_int b, bool uns)
1435 return (this->cmp (b, uns) == -1) ? *this : b;
1438 /* Compares two signed values A and B. Returns min value. */
1440 double_int
1441 double_int::smin (double_int b)
1443 return (this->scmp (b) == -1) ? *this : b;
1446 /* Compares two unsigned values A and B. Returns min value. */
1448 double_int
1449 double_int::umin (double_int b)
1451 return (this->ucmp (b) == -1) ? *this : b;
1454 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1456 static unsigned
1457 double_int_split_digit (double_int *cst, unsigned base)
1459 unsigned HOST_WIDE_INT resl, reml;
1460 HOST_WIDE_INT resh, remh;
1462 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
1463 &resl, &resh, &reml, &remh);
1464 cst->high = resh;
1465 cst->low = resl;
1467 return reml;
1470 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1471 otherwise it is signed. */
1473 void
1474 dump_double_int (FILE *file, double_int cst, bool uns)
1476 unsigned digits[100], n;
1477 int i;
1479 if (cst.is_zero ())
1481 fprintf (file, "0");
1482 return;
1485 if (!uns && cst.is_negative ())
1487 fprintf (file, "-");
1488 cst = -cst;
1491 for (n = 0; !cst.is_zero (); n++)
1492 digits[n] = double_int_split_digit (&cst, 10);
1493 for (i = n - 1; i >= 0; i--)
1494 fprintf (file, "%u", digits[i]);
1498 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1499 otherwise. */
1501 void
1502 mpz_set_double_int (mpz_t result, double_int val, bool uns)
1504 bool negate = false;
1505 unsigned HOST_WIDE_INT vp[2];
1507 if (!uns && val.is_negative ())
1509 negate = true;
1510 val = -val;
1513 vp[0] = val.low;
1514 vp[1] = (unsigned HOST_WIDE_INT) val.high;
1515 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
1517 if (negate)
1518 mpz_neg (result, result);
1521 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1522 values of VAL will be wrapped; otherwise, they will be set to the
1523 appropriate minimum or maximum TYPE bound. */
1525 double_int
1526 mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
1528 unsigned HOST_WIDE_INT *vp;
1529 size_t count, numb;
1530 double_int res;
1532 if (!wrap)
1534 mpz_t min, max;
1536 mpz_init (min);
1537 mpz_init (max);
1538 get_type_static_bounds (type, min, max);
1540 if (mpz_cmp (val, min) < 0)
1541 mpz_set (val, min);
1542 else if (mpz_cmp (val, max) > 0)
1543 mpz_set (val, max);
1545 mpz_clear (min);
1546 mpz_clear (max);
1549 /* Determine the number of unsigned HOST_WIDE_INT that are required
1550 for representing the value. The code to calculate count is
1551 extracted from the GMP manual, section "Integer Import and Export":
1552 http://gmplib.org/manual/Integer-Import-and-Export.html */
1553 numb = 8 * sizeof (HOST_WIDE_INT);
1554 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
1555 if (count < 2)
1556 count = 2;
1557 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof (HOST_WIDE_INT));
1559 vp[0] = 0;
1560 vp[1] = 0;
1561 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
1563 gcc_assert (wrap || count <= 2);
1565 res.low = vp[0];
1566 res.high = (HOST_WIDE_INT) vp[1];
1568 res = res.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
1569 if (mpz_sgn (val) < 0)
1570 res = -res;
1572 return res;