2012-09-20 Chen Wei-Ren <chenwj@iis.sinica.edu.tw>
[official-gcc.git] / gcc / double-int.c
blobf3d5e8b3dde97067a0af9ce41a854115a133f5b3
1 /* Operations with long integers.
2 Copyright (C) 2006, 2007, 2009, 2010, 2012 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 SHIFT_COUNT_TRUNCATED. */
24 #include "tree.h"
26 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
27 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
28 and SUM1. Then this yields nonzero if overflow occurred during the
29 addition.
31 Overflow occurs if A and B have the same sign, but A and SUM differ in
32 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
33 sign. */
34 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
36 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
37 We do that by representing the two-word integer in 4 words, with only
38 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
39 number. The value of the word is LOWPART + HIGHPART * BASE. */
41 #define LOWPART(x) \
42 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
43 #define HIGHPART(x) \
44 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
45 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
47 /* Unpack a two-word integer into 4 words.
48 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
49 WORDS points to the array of HOST_WIDE_INTs. */
51 static void
52 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
54 words[0] = LOWPART (low);
55 words[1] = HIGHPART (low);
56 words[2] = LOWPART (hi);
57 words[3] = HIGHPART (hi);
60 /* Pack an array of 4 words into a two-word integer.
61 WORDS points to the array of words.
62 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
64 static void
65 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
66 HOST_WIDE_INT *hi)
68 *low = words[0] + words[1] * BASE;
69 *hi = words[2] + words[3] * BASE;
72 /* Add two doubleword integers with doubleword result.
73 Return nonzero if the operation overflows according to UNSIGNED_P.
74 Each argument is given as two `HOST_WIDE_INT' pieces.
75 One argument is L1 and H1; the other, L2 and H2.
76 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
78 int
79 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
80 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
81 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
82 bool unsigned_p)
84 unsigned HOST_WIDE_INT l;
85 HOST_WIDE_INT h;
87 l = l1 + l2;
88 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
89 + (unsigned HOST_WIDE_INT) h2
90 + (l < l1));
92 *lv = l;
93 *hv = h;
95 if (unsigned_p)
96 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
97 || (h == h1
98 && l < l1));
99 else
100 return OVERFLOW_SUM_SIGN (h1, h2, h);
103 /* Negate a doubleword integer with doubleword result.
104 Return nonzero if the operation overflows, assuming it's signed.
105 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
106 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
109 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
110 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
112 if (l1 == 0)
114 *lv = 0;
115 *hv = - h1;
116 return (*hv & h1) < 0;
118 else
120 *lv = -l1;
121 *hv = ~h1;
122 return 0;
126 /* Multiply two doubleword integers with doubleword result.
127 Return nonzero if the operation overflows according to UNSIGNED_P.
128 Each argument is given as two `HOST_WIDE_INT' pieces.
129 One argument is L1 and H1; the other, L2 and H2.
130 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
133 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
134 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
135 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
136 bool unsigned_p)
138 unsigned HOST_WIDE_INT toplow;
139 HOST_WIDE_INT tophigh;
141 return mul_double_wide_with_sign (l1, h1, l2, h2,
142 lv, hv, &toplow, &tophigh,
143 unsigned_p);
147 mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
148 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
149 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
150 unsigned HOST_WIDE_INT *lw, HOST_WIDE_INT *hw,
151 bool unsigned_p)
153 HOST_WIDE_INT arg1[4];
154 HOST_WIDE_INT arg2[4];
155 HOST_WIDE_INT prod[4 * 2];
156 unsigned HOST_WIDE_INT carry;
157 int i, j, k;
158 unsigned HOST_WIDE_INT neglow;
159 HOST_WIDE_INT neghigh;
161 encode (arg1, l1, h1);
162 encode (arg2, l2, h2);
164 memset (prod, 0, sizeof prod);
166 for (i = 0; i < 4; i++)
168 carry = 0;
169 for (j = 0; j < 4; j++)
171 k = i + j;
172 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
173 carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j];
174 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
175 carry += prod[k];
176 prod[k] = LOWPART (carry);
177 carry = HIGHPART (carry);
179 prod[i + 4] = carry;
182 decode (prod, lv, hv);
183 decode (prod + 4, lw, hw);
185 /* Unsigned overflow is immediate. */
186 if (unsigned_p)
187 return (*lw | *hw) != 0;
189 /* Check for signed overflow by calculating the signed representation of the
190 top half of the result; it should agree with the low half's sign bit. */
191 if (h1 < 0)
193 neg_double (l2, h2, &neglow, &neghigh);
194 add_double (neglow, neghigh, *lw, *hw, lw, hw);
196 if (h2 < 0)
198 neg_double (l1, h1, &neglow, &neghigh);
199 add_double (neglow, neghigh, *lw, *hw, lw, hw);
201 return (*hv < 0 ? ~(*lw & *hw) : *lw | *hw) != 0;
204 /* Shift the doubleword integer in L1, H1 right by COUNT places
205 keeping only PREC bits of result. ARITH nonzero specifies
206 arithmetic shifting; otherwise use logical shift.
207 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
209 static void
210 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
211 unsigned HOST_WIDE_INT count, unsigned int prec,
212 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
213 bool arith)
215 unsigned HOST_WIDE_INT signmask;
217 signmask = (arith
218 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
219 : 0);
221 if (SHIFT_COUNT_TRUNCATED)
222 count %= prec;
224 if (count >= HOST_BITS_PER_DOUBLE_INT)
226 /* Shifting by the host word size is undefined according to the
227 ANSI standard, so we must handle this as a special case. */
228 *hv = 0;
229 *lv = 0;
231 else if (count >= HOST_BITS_PER_WIDE_INT)
233 *hv = 0;
234 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
236 else
238 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
239 *lv = ((l1 >> count)
240 | ((unsigned HOST_WIDE_INT) h1
241 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
244 /* Zero / sign extend all bits that are beyond the precision. */
246 if (count >= prec)
248 *hv = signmask;
249 *lv = signmask;
251 else if ((prec - count) >= HOST_BITS_PER_DOUBLE_INT)
253 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
255 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
256 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
258 else
260 *hv = signmask;
261 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
262 *lv |= signmask << (prec - count);
266 /* Shift the doubleword integer in L1, H1 left by COUNT places
267 keeping only PREC bits of result.
268 Shift right if COUNT is negative.
269 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
270 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
272 void
273 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
274 HOST_WIDE_INT count, unsigned int prec,
275 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, bool arith)
277 unsigned HOST_WIDE_INT signmask;
279 if (count < 0)
281 rshift_double (l1, h1, absu_hwi (count), prec, lv, hv, arith);
282 return;
285 if (SHIFT_COUNT_TRUNCATED)
286 count %= prec;
288 if (count >= HOST_BITS_PER_DOUBLE_INT)
290 /* Shifting by the host word size is undefined according to the
291 ANSI standard, so we must handle this as a special case. */
292 *hv = 0;
293 *lv = 0;
295 else if (count >= HOST_BITS_PER_WIDE_INT)
297 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
298 *lv = 0;
300 else
302 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
303 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
304 *lv = l1 << count;
307 /* Sign extend all bits that are beyond the precision. */
309 signmask = -((prec > HOST_BITS_PER_WIDE_INT
310 ? ((unsigned HOST_WIDE_INT) *hv
311 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
312 : (*lv >> (prec - 1))) & 1);
314 if (prec >= HOST_BITS_PER_DOUBLE_INT)
316 else if (prec >= HOST_BITS_PER_WIDE_INT)
318 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
319 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
321 else
323 *hv = signmask;
324 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
325 *lv |= signmask << prec;
329 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
330 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
331 CODE is a tree code for a kind of division, one of
332 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
333 or EXACT_DIV_EXPR
334 It controls how the quotient is rounded to an integer.
335 Return nonzero if the operation overflows.
336 UNS nonzero says do unsigned division. */
339 div_and_round_double (unsigned code, int uns,
340 /* num == numerator == dividend */
341 unsigned HOST_WIDE_INT lnum_orig,
342 HOST_WIDE_INT hnum_orig,
343 /* den == denominator == divisor */
344 unsigned HOST_WIDE_INT lden_orig,
345 HOST_WIDE_INT hden_orig,
346 unsigned HOST_WIDE_INT *lquo,
347 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
348 HOST_WIDE_INT *hrem)
350 int quo_neg = 0;
351 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
352 HOST_WIDE_INT den[4], quo[4];
353 int i, j;
354 unsigned HOST_WIDE_INT work;
355 unsigned HOST_WIDE_INT carry = 0;
356 unsigned HOST_WIDE_INT lnum = lnum_orig;
357 HOST_WIDE_INT hnum = hnum_orig;
358 unsigned HOST_WIDE_INT lden = lden_orig;
359 HOST_WIDE_INT hden = hden_orig;
360 int overflow = 0;
362 if (hden == 0 && lden == 0)
363 overflow = 1, lden = 1;
365 /* Calculate quotient sign and convert operands to unsigned. */
366 if (!uns)
368 if (hnum < 0)
370 quo_neg = ~ quo_neg;
371 /* (minimum integer) / (-1) is the only overflow case. */
372 if (neg_double (lnum, hnum, &lnum, &hnum)
373 && ((HOST_WIDE_INT) lden & hden) == -1)
374 overflow = 1;
376 if (hden < 0)
378 quo_neg = ~ quo_neg;
379 neg_double (lden, hden, &lden, &hden);
383 if (hnum == 0 && hden == 0)
384 { /* single precision */
385 *hquo = *hrem = 0;
386 /* This unsigned division rounds toward zero. */
387 *lquo = lnum / lden;
388 goto finish_up;
391 if (hnum == 0)
392 { /* trivial case: dividend < divisor */
393 /* hden != 0 already checked. */
394 *hquo = *lquo = 0;
395 *hrem = hnum;
396 *lrem = lnum;
397 goto finish_up;
400 memset (quo, 0, sizeof quo);
402 memset (num, 0, sizeof num); /* to zero 9th element */
403 memset (den, 0, sizeof den);
405 encode (num, lnum, hnum);
406 encode (den, lden, hden);
408 /* Special code for when the divisor < BASE. */
409 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
411 /* hnum != 0 already checked. */
412 for (i = 4 - 1; i >= 0; i--)
414 work = num[i] + carry * BASE;
415 quo[i] = work / lden;
416 carry = work % lden;
419 else
421 /* Full double precision division,
422 with thanks to Don Knuth's "Seminumerical Algorithms". */
423 int num_hi_sig, den_hi_sig;
424 unsigned HOST_WIDE_INT quo_est, scale;
426 /* Find the highest nonzero divisor digit. */
427 for (i = 4 - 1;; i--)
428 if (den[i] != 0)
430 den_hi_sig = i;
431 break;
434 /* Insure that the first digit of the divisor is at least BASE/2.
435 This is required by the quotient digit estimation algorithm. */
437 scale = BASE / (den[den_hi_sig] + 1);
438 if (scale > 1)
439 { /* scale divisor and dividend */
440 carry = 0;
441 for (i = 0; i <= 4 - 1; i++)
443 work = (num[i] * scale) + carry;
444 num[i] = LOWPART (work);
445 carry = HIGHPART (work);
448 num[4] = carry;
449 carry = 0;
450 for (i = 0; i <= 4 - 1; i++)
452 work = (den[i] * scale) + carry;
453 den[i] = LOWPART (work);
454 carry = HIGHPART (work);
455 if (den[i] != 0) den_hi_sig = i;
459 num_hi_sig = 4;
461 /* Main loop */
462 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
464 /* Guess the next quotient digit, quo_est, by dividing the first
465 two remaining dividend digits by the high order quotient digit.
466 quo_est is never low and is at most 2 high. */
467 unsigned HOST_WIDE_INT tmp;
469 num_hi_sig = i + den_hi_sig + 1;
470 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
471 if (num[num_hi_sig] != den[den_hi_sig])
472 quo_est = work / den[den_hi_sig];
473 else
474 quo_est = BASE - 1;
476 /* Refine quo_est so it's usually correct, and at most one high. */
477 tmp = work - quo_est * den[den_hi_sig];
478 if (tmp < BASE
479 && (den[den_hi_sig - 1] * quo_est
480 > (tmp * BASE + num[num_hi_sig - 2])))
481 quo_est--;
483 /* Try QUO_EST as the quotient digit, by multiplying the
484 divisor by QUO_EST and subtracting from the remaining dividend.
485 Keep in mind that QUO_EST is the I - 1st digit. */
487 carry = 0;
488 for (j = 0; j <= den_hi_sig; j++)
490 work = quo_est * den[j] + carry;
491 carry = HIGHPART (work);
492 work = num[i + j] - LOWPART (work);
493 num[i + j] = LOWPART (work);
494 carry += HIGHPART (work) != 0;
497 /* If quo_est was high by one, then num[i] went negative and
498 we need to correct things. */
499 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
501 quo_est--;
502 carry = 0; /* add divisor back in */
503 for (j = 0; j <= den_hi_sig; j++)
505 work = num[i + j] + den[j] + carry;
506 carry = HIGHPART (work);
507 num[i + j] = LOWPART (work);
510 num [num_hi_sig] += carry;
513 /* Store the quotient digit. */
514 quo[i] = quo_est;
518 decode (quo, lquo, hquo);
520 finish_up:
521 /* If result is negative, make it so. */
522 if (quo_neg)
523 neg_double (*lquo, *hquo, lquo, hquo);
525 /* Compute trial remainder: rem = num - (quo * den) */
526 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
527 neg_double (*lrem, *hrem, lrem, hrem);
528 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
530 switch (code)
532 case TRUNC_DIV_EXPR:
533 case TRUNC_MOD_EXPR: /* round toward zero */
534 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
535 return overflow;
537 case FLOOR_DIV_EXPR:
538 case FLOOR_MOD_EXPR: /* round toward negative infinity */
539 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
541 /* quo = quo - 1; */
542 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
543 lquo, hquo);
545 else
546 return overflow;
547 break;
549 case CEIL_DIV_EXPR:
550 case CEIL_MOD_EXPR: /* round toward positive infinity */
551 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
553 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
554 lquo, hquo);
556 else
557 return overflow;
558 break;
560 case ROUND_DIV_EXPR:
561 case ROUND_MOD_EXPR: /* round to closest integer */
563 unsigned HOST_WIDE_INT labs_rem = *lrem;
564 HOST_WIDE_INT habs_rem = *hrem;
565 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
566 HOST_WIDE_INT habs_den = hden, htwice;
568 /* Get absolute values. */
569 if (*hrem < 0)
570 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
571 if (hden < 0)
572 neg_double (lden, hden, &labs_den, &habs_den);
574 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
575 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
576 labs_rem, habs_rem, &ltwice, &htwice);
578 if (((unsigned HOST_WIDE_INT) habs_den
579 < (unsigned HOST_WIDE_INT) htwice)
580 || (((unsigned HOST_WIDE_INT) habs_den
581 == (unsigned HOST_WIDE_INT) htwice)
582 && (labs_den <= ltwice)))
584 if (*hquo < 0)
585 /* quo = quo - 1; */
586 add_double (*lquo, *hquo,
587 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
588 else
589 /* quo = quo + 1; */
590 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
591 lquo, hquo);
593 else
594 return overflow;
596 break;
598 default:
599 gcc_unreachable ();
602 /* Compute true remainder: rem = num - (quo * den) */
603 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
604 neg_double (*lrem, *hrem, lrem, hrem);
605 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
606 return overflow;
609 /* Returns mask for PREC bits. */
611 double_int
612 double_int::mask (unsigned prec)
614 unsigned HOST_WIDE_INT m;
615 double_int mask;
617 if (prec > HOST_BITS_PER_WIDE_INT)
619 prec -= HOST_BITS_PER_WIDE_INT;
620 m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
621 mask.high = (HOST_WIDE_INT) m;
622 mask.low = ALL_ONES;
624 else
626 mask.high = 0;
627 mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0;
630 return mask;
633 /* Returns a maximum value for signed or unsigned integer
634 of precision PREC. */
636 double_int
637 double_int::max_value (unsigned int prec, bool uns)
639 return double_int::mask (prec - (uns ? 0 : 1));
642 /* Returns a minimum value for signed or unsigned integer
643 of precision PREC. */
645 double_int
646 double_int::min_value (unsigned int prec, bool uns)
648 if (uns)
649 return double_int_zero;
650 return double_int_one.lshift (prec - 1, prec, false);
653 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
654 outside of the precision are set to the sign bit (i.e., the PREC-th one),
655 otherwise they are set to zero.
657 This corresponds to returning the value represented by PREC lowermost bits
658 of CST, with the given signedness. */
660 double_int
661 double_int::ext (unsigned prec, bool uns) const
663 if (uns)
664 return this->zext (prec);
665 else
666 return this->sext (prec);
669 /* The same as double_int::ext with UNS = true. */
671 double_int
672 double_int::zext (unsigned prec) const
674 const double_int &cst = *this;
675 double_int mask = double_int::mask (prec);
676 double_int r;
678 r.low = cst.low & mask.low;
679 r.high = cst.high & mask.high;
681 return r;
684 /* The same as double_int::ext with UNS = false. */
686 double_int
687 double_int::sext (unsigned prec) const
689 const double_int &cst = *this;
690 double_int mask = double_int::mask (prec);
691 double_int r;
692 unsigned HOST_WIDE_INT snum;
694 if (prec <= HOST_BITS_PER_WIDE_INT)
695 snum = cst.low;
696 else
698 prec -= HOST_BITS_PER_WIDE_INT;
699 snum = (unsigned HOST_WIDE_INT) cst.high;
701 if (((snum >> (prec - 1)) & 1) == 1)
703 r.low = cst.low | ~mask.low;
704 r.high = cst.high | ~mask.high;
706 else
708 r.low = cst.low & mask.low;
709 r.high = cst.high & mask.high;
712 return r;
715 /* Returns true if CST fits in signed HOST_WIDE_INT. */
717 bool
718 double_int::fits_shwi () const
720 const double_int &cst = *this;
721 if (cst.high == 0)
722 return (HOST_WIDE_INT) cst.low >= 0;
723 else if (cst.high == -1)
724 return (HOST_WIDE_INT) cst.low < 0;
725 else
726 return false;
729 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
730 unsigned HOST_WIDE_INT if UNS is true. */
732 bool
733 double_int::fits_hwi (bool uns) const
735 if (uns)
736 return this->fits_uhwi ();
737 else
738 return this->fits_shwi ();
741 /* Returns A * B. */
743 double_int
744 double_int::operator * (double_int b) const
746 const double_int &a = *this;
747 double_int ret;
748 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
749 return ret;
752 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
753 *OVERFLOW is set to nonzero. */
755 double_int
756 double_int::mul_with_sign (double_int b, bool unsigned_p, bool *overflow) const
758 const double_int &a = *this;
759 double_int ret;
760 *overflow = mul_double_with_sign (a.low, a.high, b.low, b.high,
761 &ret.low, &ret.high, unsigned_p);
762 return ret;
765 /* Returns A + B. */
767 double_int
768 double_int::operator + (double_int b) const
770 const double_int &a = *this;
771 double_int ret;
772 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
773 return ret;
776 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
777 *OVERFLOW is set to nonzero. */
779 double_int
780 double_int::add_with_sign (double_int b, bool unsigned_p, bool *overflow) const
782 const double_int &a = *this;
783 double_int ret;
784 *overflow = add_double_with_sign (a.low, a.high, b.low, b.high,
785 &ret.low, &ret.high, unsigned_p);
786 return ret;
789 /* Returns A - B. */
791 double_int
792 double_int::operator - (double_int b) const
794 const double_int &a = *this;
795 double_int ret;
796 neg_double (b.low, b.high, &b.low, &b.high);
797 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
798 return ret;
801 /* Returns -A. */
803 double_int
804 double_int::operator - () const
806 const double_int &a = *this;
807 double_int ret;
808 neg_double (a.low, a.high, &ret.low, &ret.high);
809 return ret;
812 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
813 specified by CODE). CODE is enum tree_code in fact, but double_int.h
814 must be included before tree.h. The remainder after the division is
815 stored to MOD. */
817 double_int
818 double_int::divmod (double_int b, bool uns, unsigned code,
819 double_int *mod) const
821 const double_int &a = *this;
822 double_int ret;
824 div_and_round_double (code, uns, a.low, a.high,
825 b.low, b.high, &ret.low, &ret.high,
826 &mod->low, &mod->high);
827 return ret;
830 /* The same as double_int::divmod with UNS = false. */
832 double_int
833 double_int::sdivmod (double_int b, unsigned code, double_int *mod) const
835 return this->divmod (b, false, code, mod);
838 /* The same as double_int::divmod with UNS = true. */
840 double_int
841 double_int::udivmod (double_int b, unsigned code, double_int *mod) const
843 return this->divmod (b, true, code, mod);
846 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
847 specified by CODE). CODE is enum tree_code in fact, but double_int.h
848 must be included before tree.h. */
850 double_int
851 double_int::div (double_int b, bool uns, unsigned code) const
853 double_int mod;
855 return this->divmod (b, uns, code, &mod);
858 /* The same as double_int::div with UNS = false. */
860 double_int
861 double_int::sdiv (double_int b, unsigned code) const
863 return this->div (b, false, code);
866 /* The same as double_int::div with UNS = true. */
868 double_int
869 double_int::udiv (double_int b, unsigned code) const
871 return this->div (b, true, code);
874 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
875 specified by CODE). CODE is enum tree_code in fact, but double_int.h
876 must be included before tree.h. */
878 double_int
879 double_int::mod (double_int b, bool uns, unsigned code) const
881 double_int mod;
883 this->divmod (b, uns, code, &mod);
884 return mod;
887 /* The same as double_int::mod with UNS = false. */
889 double_int
890 double_int::smod (double_int b, unsigned code) const
892 return this->mod (b, false, code);
895 /* The same as double_int::mod with UNS = true. */
897 double_int
898 double_int::umod (double_int b, unsigned code) const
900 return this->mod (b, true, code);
903 /* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
904 the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
905 unchanged. */
907 bool
908 double_int::multiple_of (double_int factor,
909 bool unsigned_p, double_int *multiple) const
911 double_int remainder;
912 double_int quotient = this->divmod (factor, unsigned_p,
913 TRUNC_DIV_EXPR, &remainder);
914 if (remainder.is_zero ())
916 *multiple = quotient;
917 return true;
920 return false;
923 /* Set BITPOS bit in A. */
924 double_int
925 double_int::set_bit (unsigned bitpos) const
927 double_int a = *this;
928 if (bitpos < HOST_BITS_PER_WIDE_INT)
929 a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
930 else
931 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
933 return a;
936 /* Count trailing zeros in A. */
938 double_int::trailing_zeros () const
940 const double_int &a = *this;
941 unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
942 unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
943 if (!w)
944 return HOST_BITS_PER_DOUBLE_INT;
945 bits += ctz_hwi (w);
946 return bits;
949 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
950 right if COUNT is negative. ARITH true specifies arithmetic shifting;
951 otherwise use logical shift. */
953 double_int
954 double_int::lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
956 const double_int &a = *this;
957 double_int ret;
958 lshift_double (a.low, a.high, count, prec, &ret.low, &ret.high, arith);
959 return ret;
962 /* Shift A right by COUNT places keeping only PREC bits of result. Shift
963 left if COUNT is negative. ARITH true specifies arithmetic shifting;
964 otherwise use logical shift. */
966 double_int
967 double_int::rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
969 const double_int &a = *this;
970 double_int ret;
971 lshift_double (a.low, a.high, -count, prec, &ret.low, &ret.high, arith);
972 return ret;
975 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
976 Shift right if COUNT is negative. */
978 double_int
979 double_int::alshift (HOST_WIDE_INT count, unsigned int prec) const
981 double_int r;
982 lshift_double (low, high, count, prec, &r.low, &r.high, true);
983 return r;
986 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
987 Shift left if COUNT is negative. */
989 double_int
990 double_int::arshift (HOST_WIDE_INT count, unsigned int prec) const
992 double_int r;
993 lshift_double (low, high, -count, prec, &r.low, &r.high, true);
994 return r;
997 /* Logical shift A left by COUNT places keeping only PREC bits of result.
998 Shift right if COUNT is negative. */
1000 double_int
1001 double_int::llshift (HOST_WIDE_INT count, unsigned int prec) const
1003 double_int r;
1004 lshift_double (low, high, count, prec, &r.low, &r.high, false);
1005 return r;
1008 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1009 Shift left if COUNT is negative. */
1011 double_int
1012 double_int::lrshift (HOST_WIDE_INT count, unsigned int prec) const
1014 double_int r;
1015 lshift_double (low, high, -count, prec, &r.low, &r.high, false);
1016 return r;
1019 /* Rotate A left by COUNT places keeping only PREC bits of result.
1020 Rotate right if COUNT is negative. */
1022 double_int
1023 double_int::lrotate (HOST_WIDE_INT count, unsigned int prec) const
1025 double_int t1, t2;
1027 count %= prec;
1028 if (count < 0)
1029 count += prec;
1031 t1 = this->lshift (count, prec, false);
1032 t2 = this->rshift (prec - count, prec, false);
1034 return t1 | t2;
1037 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1038 Rotate right if COUNT is negative. */
1040 double_int
1041 double_int::rrotate (HOST_WIDE_INT count, unsigned int prec) const
1043 double_int t1, t2;
1045 count %= prec;
1046 if (count < 0)
1047 count += prec;
1049 t1 = this->rshift (count, prec, false);
1050 t2 = this->lshift (prec - count, prec, false);
1052 return t1 | t2;
1055 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1056 comparison is given by UNS. */
1059 double_int::cmp (double_int b, bool uns) const
1061 if (uns)
1062 return this->ucmp (b);
1063 else
1064 return this->scmp (b);
1067 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
1068 and 1 if A > B. */
1071 double_int::ucmp (double_int b) const
1073 const double_int &a = *this;
1074 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
1075 return -1;
1076 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
1077 return 1;
1078 if (a.low < b.low)
1079 return -1;
1080 if (a.low > b.low)
1081 return 1;
1083 return 0;
1086 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1087 and 1 if A > B. */
1090 double_int::scmp (double_int b) const
1092 const double_int &a = *this;
1093 if (a.high < b.high)
1094 return -1;
1095 if (a.high > b.high)
1096 return 1;
1097 if (a.low < b.low)
1098 return -1;
1099 if (a.low > b.low)
1100 return 1;
1102 return 0;
1105 /* Compares two unsigned values A and B for less-than. */
1107 bool
1108 double_int::ult (double_int b) const
1110 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1111 return true;
1112 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1113 return false;
1114 if (low < b.low)
1115 return true;
1116 return false;
1119 /* Compares two unsigned values A and B for less-than or equal-to. */
1121 bool
1122 double_int::ule (double_int b) const
1124 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1125 return true;
1126 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1127 return false;
1128 if (low <= b.low)
1129 return true;
1130 return false;
1133 /* Compares two unsigned values A and B for greater-than. */
1135 bool
1136 double_int::ugt (double_int b) const
1138 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1139 return true;
1140 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1141 return false;
1142 if (low > b.low)
1143 return true;
1144 return false;
1147 /* Compares two signed values A and B for less-than. */
1149 bool
1150 double_int::slt (double_int b) const
1152 if (high < b.high)
1153 return true;
1154 if (high > b.high)
1155 return false;
1156 if (low < b.low)
1157 return true;
1158 return false;
1161 /* Compares two signed values A and B for less-than or equal-to. */
1163 bool
1164 double_int::sle (double_int b) const
1166 if (high < b.high)
1167 return true;
1168 if (high > b.high)
1169 return false;
1170 if (low <= b.low)
1171 return true;
1172 return false;
1175 /* Compares two signed values A and B for greater-than. */
1177 bool
1178 double_int::sgt (double_int b) const
1180 if (high > b.high)
1181 return true;
1182 if (high < b.high)
1183 return false;
1184 if (low > b.low)
1185 return true;
1186 return false;
1190 /* Compares two values A and B. Returns max value. Signedness of the
1191 comparison is given by UNS. */
1193 double_int
1194 double_int::max (double_int b, bool uns)
1196 return (this->cmp (b, uns) == 1) ? *this : b;
1199 /* Compares two signed values A and B. Returns max value. */
1201 double_int
1202 double_int::smax (double_int b)
1204 return (this->scmp (b) == 1) ? *this : b;
1207 /* Compares two unsigned values A and B. Returns max value. */
1209 double_int
1210 double_int::umax (double_int b)
1212 return (this->ucmp (b) == 1) ? *this : b;
1215 /* Compares two values A and B. Returns mix value. Signedness of the
1216 comparison is given by UNS. */
1218 double_int
1219 double_int::min (double_int b, bool uns)
1221 return (this->cmp (b, uns) == -1) ? *this : b;
1224 /* Compares two signed values A and B. Returns min value. */
1226 double_int
1227 double_int::smin (double_int b)
1229 return (this->scmp (b) == -1) ? *this : b;
1232 /* Compares two unsigned values A and B. Returns min value. */
1234 double_int
1235 double_int::umin (double_int b)
1237 return (this->ucmp (b) == -1) ? *this : b;
1240 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1242 static unsigned
1243 double_int_split_digit (double_int *cst, unsigned base)
1245 unsigned HOST_WIDE_INT resl, reml;
1246 HOST_WIDE_INT resh, remh;
1248 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
1249 &resl, &resh, &reml, &remh);
1250 cst->high = resh;
1251 cst->low = resl;
1253 return reml;
1256 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1257 otherwise it is signed. */
1259 void
1260 dump_double_int (FILE *file, double_int cst, bool uns)
1262 unsigned digits[100], n;
1263 int i;
1265 if (cst.is_zero ())
1267 fprintf (file, "0");
1268 return;
1271 if (!uns && cst.is_negative ())
1273 fprintf (file, "-");
1274 cst = -cst;
1277 for (n = 0; !cst.is_zero (); n++)
1278 digits[n] = double_int_split_digit (&cst, 10);
1279 for (i = n - 1; i >= 0; i--)
1280 fprintf (file, "%u", digits[i]);
1284 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1285 otherwise. */
1287 void
1288 mpz_set_double_int (mpz_t result, double_int val, bool uns)
1290 bool negate = false;
1291 unsigned HOST_WIDE_INT vp[2];
1293 if (!uns && val.is_negative ())
1295 negate = true;
1296 val = -val;
1299 vp[0] = val.low;
1300 vp[1] = (unsigned HOST_WIDE_INT) val.high;
1301 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
1303 if (negate)
1304 mpz_neg (result, result);
1307 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1308 values of VAL will be wrapped; otherwise, they will be set to the
1309 appropriate minimum or maximum TYPE bound. */
1311 double_int
1312 mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
1314 unsigned HOST_WIDE_INT *vp;
1315 size_t count, numb;
1316 double_int res;
1318 if (!wrap)
1320 mpz_t min, max;
1322 mpz_init (min);
1323 mpz_init (max);
1324 get_type_static_bounds (type, min, max);
1326 if (mpz_cmp (val, min) < 0)
1327 mpz_set (val, min);
1328 else if (mpz_cmp (val, max) > 0)
1329 mpz_set (val, max);
1331 mpz_clear (min);
1332 mpz_clear (max);
1335 /* Determine the number of unsigned HOST_WIDE_INT that are required
1336 for representing the value. The code to calculate count is
1337 extracted from the GMP manual, section "Integer Import and Export":
1338 http://gmplib.org/manual/Integer-Import-and-Export.html */
1339 numb = 8*sizeof(HOST_WIDE_INT);
1340 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
1341 if (count < 2)
1342 count = 2;
1343 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof(HOST_WIDE_INT));
1345 vp[0] = 0;
1346 vp[1] = 0;
1347 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
1349 gcc_assert (wrap || count <= 2);
1351 res.low = vp[0];
1352 res.high = (HOST_WIDE_INT) vp[1];
1354 res = res.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
1355 if (mpz_sgn (val) < 0)
1356 res = -res;
1358 return res;