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
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"
23 #include "tm.h" /* For SHIFT_COUNT_TRUNCATED. */
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
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
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. */
42 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
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. */
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. */
65 decode (HOST_WIDE_INT
*words
, unsigned HOST_WIDE_INT
*low
,
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. */
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
,
84 unsigned HOST_WIDE_INT l
;
88 h
= (HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
) h1
89 + (unsigned HOST_WIDE_INT
) h2
96 return ((unsigned HOST_WIDE_INT
) h
< (unsigned HOST_WIDE_INT
) h1
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
)
116 return (*hv
& h1
) < 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
,
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
,
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
,
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
;
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
++)
169 for (j
= 0; j
< 4; 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. */
176 prod
[k
] = LOWPART (carry
);
177 carry
= HIGHPART (carry
);
182 decode (prod
, lv
, hv
);
183 decode (prod
+ 4, lw
, hw
);
185 /* Unsigned overflow is immediate. */
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. */
193 neg_double (l2
, h2
, &neglow
, &neghigh
);
194 add_double (neglow
, neghigh
, *lw
, *hw
, lw
, hw
);
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. */
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
,
215 unsigned HOST_WIDE_INT signmask
;
218 ? -((unsigned HOST_WIDE_INT
) h1
>> (HOST_BITS_PER_WIDE_INT
- 1))
221 if (SHIFT_COUNT_TRUNCATED
)
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. */
231 else if (count
>= HOST_BITS_PER_WIDE_INT
)
234 *lv
= (unsigned HOST_WIDE_INT
) h1
>> (count
- HOST_BITS_PER_WIDE_INT
);
238 *hv
= (unsigned HOST_WIDE_INT
) h1
>> 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. */
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
);
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. */
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
;
281 rshift_double (l1
, h1
, absu_hwi (count
), prec
, lv
, hv
, arith
);
285 if (SHIFT_COUNT_TRUNCATED
)
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. */
295 else if (count
>= HOST_BITS_PER_WIDE_INT
)
297 *hv
= l1
<< (count
- HOST_BITS_PER_WIDE_INT
);
302 *hv
= (((unsigned HOST_WIDE_INT
) h1
<< count
)
303 | (l1
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
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
);
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
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
,
351 HOST_WIDE_INT num
[4 + 1]; /* extra element for scaling. */
352 HOST_WIDE_INT den
[4], quo
[4];
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
;
362 if (hden
== 0 && lden
== 0)
363 overflow
= 1, lden
= 1;
365 /* Calculate quotient sign and convert operands to unsigned. */
371 /* (minimum integer) / (-1) is the only overflow case. */
372 if (neg_double (lnum
, hnum
, &lnum
, &hnum
)
373 && ((HOST_WIDE_INT
) lden
& hden
) == -1)
379 neg_double (lden
, hden
, &lden
, &hden
);
383 if (hnum
== 0 && hden
== 0)
384 { /* single precision */
386 /* This unsigned division rounds toward zero. */
392 { /* trivial case: dividend < divisor */
393 /* hden != 0 already checked. */
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
;
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
--)
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);
439 { /* scale divisor and dividend */
441 for (i
= 0; i
<= 4 - 1; i
++)
443 work
= (num
[i
] * scale
) + carry
;
444 num
[i
] = LOWPART (work
);
445 carry
= HIGHPART (work
);
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
;
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
];
476 /* Refine quo_est so it's usually correct, and at most one high. */
477 tmp
= work
- quo_est
* den
[den_hi_sig
];
479 && (den
[den_hi_sig
- 1] * quo_est
480 > (tmp
* BASE
+ num
[num_hi_sig
- 2])))
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. */
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
)
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. */
518 decode (quo
, lquo
, hquo
);
521 /* If result is negative, make it so. */
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
);
533 case TRUNC_MOD_EXPR
: /* round toward zero */
534 case EXACT_DIV_EXPR
: /* for this one, it shouldn't matter */
538 case FLOOR_MOD_EXPR
: /* round toward negative infinity */
539 if (quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio < 0 && rem != 0 */
542 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1,
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,
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. */
570 neg_double (*lrem
, *hrem
, &labs_rem
, &habs_rem
);
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
, <wice
, &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
)))
586 add_double (*lquo
, *hquo
,
587 (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1, lquo
, hquo
);
590 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
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
);
609 /* Returns mask for PREC bits. */
612 double_int::mask (unsigned prec
)
614 unsigned HOST_WIDE_INT m
;
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
;
627 mask
.low
= prec
? ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1 : 0;
633 /* Returns a maximum value for signed or unsigned integer
634 of precision PREC. */
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. */
646 double_int::min_value (unsigned int prec
, bool 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. */
661 double_int::ext (unsigned prec
, bool uns
) const
664 return this->zext (prec
);
666 return this->sext (prec
);
669 /* The same as double_int::ext with UNS = true. */
672 double_int::zext (unsigned prec
) const
674 const double_int
&cst
= *this;
675 double_int mask
= double_int::mask (prec
);
678 r
.low
= cst
.low
& mask
.low
;
679 r
.high
= cst
.high
& mask
.high
;
684 /* The same as double_int::ext with UNS = false. */
687 double_int::sext (unsigned prec
) const
689 const double_int
&cst
= *this;
690 double_int mask
= double_int::mask (prec
);
692 unsigned HOST_WIDE_INT snum
;
694 if (prec
<= HOST_BITS_PER_WIDE_INT
)
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
;
708 r
.low
= cst
.low
& mask
.low
;
709 r
.high
= cst
.high
& mask
.high
;
715 /* Returns true if CST fits in signed HOST_WIDE_INT. */
718 double_int::fits_shwi () const
720 const double_int
&cst
= *this;
722 return (HOST_WIDE_INT
) cst
.low
>= 0;
723 else if (cst
.high
== -1)
724 return (HOST_WIDE_INT
) cst
.low
< 0;
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. */
733 double_int::fits_hwi (bool uns
) const
736 return this->fits_uhwi ();
738 return this->fits_shwi ();
744 double_int::operator * (double_int b
) const
746 const double_int
&a
= *this;
748 mul_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
752 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
753 *OVERFLOW is set to nonzero. */
756 double_int::mul_with_sign (double_int b
, bool unsigned_p
, bool *overflow
) const
758 const double_int
&a
= *this;
760 *overflow
= mul_double_with_sign (a
.low
, a
.high
, b
.low
, b
.high
,
761 &ret
.low
, &ret
.high
, unsigned_p
);
768 double_int::operator + (double_int b
) const
770 const double_int
&a
= *this;
772 add_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
776 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
777 *OVERFLOW is set to nonzero. */
780 double_int::add_with_sign (double_int b
, bool unsigned_p
, bool *overflow
) const
782 const double_int
&a
= *this;
784 *overflow
= add_double_with_sign (a
.low
, a
.high
, b
.low
, b
.high
,
785 &ret
.low
, &ret
.high
, unsigned_p
);
792 double_int::operator - (double_int b
) const
794 const double_int
&a
= *this;
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
);
804 double_int::operator - () const
806 const double_int
&a
= *this;
808 neg_double (a
.low
, a
.high
, &ret
.low
, &ret
.high
);
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
818 double_int::divmod (double_int b
, bool uns
, unsigned code
,
819 double_int
*mod
) const
821 const double_int
&a
= *this;
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
);
830 /* The same as double_int::divmod with UNS = false. */
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. */
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. */
851 double_int::div (double_int b
, bool uns
, unsigned code
) const
855 return this->divmod (b
, uns
, code
, &mod
);
858 /* The same as double_int::div with UNS = false. */
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. */
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. */
879 double_int::mod (double_int b
, bool uns
, unsigned code
) const
883 this->divmod (b
, uns
, code
, &mod
);
887 /* The same as double_int::mod with UNS = false. */
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. */
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
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
;
923 /* Set BITPOS bit in A. */
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
;
931 a
.high
|= (HOST_WIDE_INT
) 1 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
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
;
944 return HOST_BITS_PER_DOUBLE_INT
;
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. */
954 double_int::lshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const
956 const double_int
&a
= *this;
958 lshift_double (a
.low
, a
.high
, count
, prec
, &ret
.low
, &ret
.high
, arith
);
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. */
967 double_int::rshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const
969 const double_int
&a
= *this;
971 lshift_double (a
.low
, a
.high
, -count
, prec
, &ret
.low
, &ret
.high
, arith
);
975 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
976 Shift right if COUNT is negative. */
979 double_int::alshift (HOST_WIDE_INT count
, unsigned int prec
) const
982 lshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
, true);
986 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
987 Shift left if COUNT is negative. */
990 double_int::arshift (HOST_WIDE_INT count
, unsigned int prec
) const
993 lshift_double (low
, high
, -count
, prec
, &r
.low
, &r
.high
, true);
997 /* Logical shift A left by COUNT places keeping only PREC bits of result.
998 Shift right if COUNT is negative. */
1001 double_int::llshift (HOST_WIDE_INT count
, unsigned int prec
) const
1004 lshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
, false);
1008 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1009 Shift left if COUNT is negative. */
1012 double_int::lrshift (HOST_WIDE_INT count
, unsigned int prec
) const
1015 lshift_double (low
, high
, -count
, prec
, &r
.low
, &r
.high
, false);
1019 /* Rotate A left by COUNT places keeping only PREC bits of result.
1020 Rotate right if COUNT is negative. */
1023 double_int::lrotate (HOST_WIDE_INT count
, unsigned int prec
) const
1031 t1
= this->lshift (count
, prec
, false);
1032 t2
= this->rshift (prec
- count
, prec
, false);
1037 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1038 Rotate right if COUNT is negative. */
1041 double_int::rrotate (HOST_WIDE_INT count
, unsigned int prec
) const
1049 t1
= this->rshift (count
, prec
, false);
1050 t2
= this->lshift (prec
- count
, prec
, false);
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
1062 return this->ucmp (b
);
1064 return this->scmp (b
);
1067 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 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
)
1076 if ((unsigned HOST_WIDE_INT
) a
.high
> (unsigned HOST_WIDE_INT
) b
.high
)
1086 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1090 double_int::scmp (double_int b
) const
1092 const double_int
&a
= *this;
1093 if (a
.high
< b
.high
)
1095 if (a
.high
> b
.high
)
1105 /* Compares two unsigned values A and B for less-than. */
1108 double_int::ult (double_int b
) const
1110 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1112 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1119 /* Compares two unsigned values A and B for less-than or equal-to. */
1122 double_int::ule (double_int b
) const
1124 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1126 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1133 /* Compares two unsigned values A and B for greater-than. */
1136 double_int::ugt (double_int b
) const
1138 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1140 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1147 /* Compares two signed values A and B for less-than. */
1150 double_int::slt (double_int b
) const
1161 /* Compares two signed values A and B for less-than or equal-to. */
1164 double_int::sle (double_int b
) const
1175 /* Compares two signed values A and B for greater-than. */
1178 double_int::sgt (double_int b
) const
1190 /* Compares two values A and B. Returns max value. Signedness of the
1191 comparison is given by UNS. */
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. */
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. */
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. */
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. */
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. */
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. */
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
);
1256 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1257 otherwise it is signed. */
1260 dump_double_int (FILE *file
, double_int cst
, bool uns
)
1262 unsigned digits
[100], n
;
1267 fprintf (file
, "0");
1271 if (!uns
&& cst
.is_negative ())
1273 fprintf (file
, "-");
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
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 ())
1300 vp
[1] = (unsigned HOST_WIDE_INT
) val
.high
;
1301 mpz_import (result
, 2, -1, sizeof (HOST_WIDE_INT
), 0, 0, vp
);
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. */
1312 mpz_get_double_int (const_tree type
, mpz_t val
, bool wrap
)
1314 unsigned HOST_WIDE_INT
*vp
;
1324 get_type_static_bounds (type
, min
, max
);
1326 if (mpz_cmp (val
, min
) < 0)
1328 else if (mpz_cmp (val
, max
) > 0)
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
;
1343 vp
= (unsigned HOST_WIDE_INT
*) alloca (count
* sizeof(HOST_WIDE_INT
));
1347 mpz_export (vp
, &count
, -1, sizeof (HOST_WIDE_INT
), 0, 0, val
);
1349 gcc_assert (wrap
|| count
<= 2);
1352 res
.high
= (HOST_WIDE_INT
) vp
[1];
1354 res
= res
.ext (TYPE_PRECISION (type
), TYPE_UNSIGNED (type
));
1355 if (mpz_sgn (val
) < 0)