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
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 BITS_PER_UNIT and *_BIG_ENDIAN. */
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
*,
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
*,
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
*,
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
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
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. */
70 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
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. */
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. */
93 decode (HOST_WIDE_INT
*words
, unsigned HOST_WIDE_INT
*low
,
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. */
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
,
112 unsigned HOST_WIDE_INT l
;
116 h
= (HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
) h1
117 + (unsigned HOST_WIDE_INT
) h2
124 return ((unsigned HOST_WIDE_INT
) h
< (unsigned HOST_WIDE_INT
) h1
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. */
137 neg_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
138 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
143 *hv
= - (unsigned HOST_WIDE_INT
) h1
;
144 return (*hv
& h1
) < 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,
160 If lw is NULL then only the low part and no overflow is computed. */
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
,
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
;
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
++)
185 for (j
= 0; j
< 4; 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. */
192 prod
[k
] = LOWPART (carry
);
193 carry
= HIGHPART (carry
);
198 decode (prod
, lv
, hv
);
200 /* We are not interested in the wide part nor in overflow. */
204 decode (prod
+ 4, lw
, hw
);
206 /* Unsigned overflow is immediate. */
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. */
214 neg_double (l2
, h2
, &neglow
, &neghigh
);
215 add_double (neglow
, neghigh
, *lw
, *hw
, lw
, hw
);
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. */
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
,
236 unsigned HOST_WIDE_INT signmask
;
239 ? -((unsigned HOST_WIDE_INT
) h1
>> (HOST_BITS_PER_WIDE_INT
- 1))
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. */
249 else if (count
>= HOST_BITS_PER_WIDE_INT
)
252 *lv
= (unsigned HOST_WIDE_INT
) h1
>> (count
- HOST_BITS_PER_WIDE_INT
);
256 *hv
= (unsigned HOST_WIDE_INT
) h1
>> 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. */
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
);
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. */
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. */
304 else if (count
>= HOST_BITS_PER_WIDE_INT
)
306 *hv
= l1
<< (count
- HOST_BITS_PER_WIDE_INT
);
311 *hv
= (((unsigned HOST_WIDE_INT
) h1
<< count
)
312 | (l1
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
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
);
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
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. */
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
,
360 HOST_WIDE_INT num
[4 + 1]; /* extra element for scaling. */
361 HOST_WIDE_INT den
[4], quo
[4];
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
;
371 if (hden
== 0 && lden
== 0)
372 overflow
= 1, lden
= 1;
374 /* Calculate quotient sign and convert operands to unsigned. */
380 /* (minimum integer) / (-1) is the only overflow case. */
381 if (neg_double (lnum
, hnum
, &lnum
, &hnum
)
382 && ((HOST_WIDE_INT
) lden
& hden
) == -1)
388 neg_double (lden
, hden
, &lden
, &hden
);
392 if (hnum
== 0 && hden
== 0)
393 { /* single precision */
395 /* This unsigned division rounds toward zero. */
401 { /* trivial case: dividend < divisor */
402 /* hden != 0 already checked. */
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
;
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
--)
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);
448 { /* scale divisor and dividend */
450 for (i
= 0; i
<= 4 - 1; i
++)
452 work
= (num
[i
] * scale
) + carry
;
453 num
[i
] = LOWPART (work
);
454 carry
= HIGHPART (work
);
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
;
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
];
485 /* Refine quo_est so it's usually correct, and at most one high. */
486 tmp
= work
- quo_est
* den
[den_hi_sig
];
488 && (den
[den_hi_sig
- 1] * quo_est
489 > (tmp
* BASE
+ num
[num_hi_sig
- 2])))
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. */
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
)
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. */
527 decode (quo
, lquo
, hquo
);
530 /* If result is negative, make it so. */
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
);
542 case TRUNC_MOD_EXPR
: /* round toward zero */
543 case EXACT_DIV_EXPR
: /* for this one, it shouldn't matter */
547 case FLOOR_MOD_EXPR
: /* round toward negative infinity */
548 if (quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio < 0 && rem != 0 */
551 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1,
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,
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
,
588 if (((unsigned HOST_WIDE_INT
) habs_rem
589 > (unsigned HOST_WIDE_INT
) hdiff
)
590 || (habs_rem
== hdiff
&& labs_rem
>= ldiff
))
594 add_double (*lquo
, *hquo
,
595 (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1, lquo
, hquo
);
598 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
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
);
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. */
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
++)
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
);
648 offset
+= byte
% UNITS_PER_WORD
;
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
;
658 result
.high
|= value
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
665 /* Returns mask for PREC bits. */
668 double_int::mask (unsigned prec
)
670 unsigned HOST_WIDE_INT m
;
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
;
683 mask
.low
= prec
? ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1 : 0;
689 /* Returns a maximum value for signed or unsigned integer
690 of precision PREC. */
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. */
702 double_int::min_value (unsigned int prec
, bool 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. */
717 double_int::ext (unsigned prec
, bool uns
) const
720 return this->zext (prec
);
722 return this->sext (prec
);
725 /* The same as double_int::ext with UNS = true. */
728 double_int::zext (unsigned prec
) const
730 const double_int
&cst
= *this;
731 double_int mask
= double_int::mask (prec
);
734 r
.low
= cst
.low
& mask
.low
;
735 r
.high
= cst
.high
& mask
.high
;
740 /* The same as double_int::ext with UNS = false. */
743 double_int::sext (unsigned prec
) const
745 const double_int
&cst
= *this;
746 double_int mask
= double_int::mask (prec
);
748 unsigned HOST_WIDE_INT snum
;
750 if (prec
<= HOST_BITS_PER_WIDE_INT
)
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
;
764 r
.low
= cst
.low
& mask
.low
;
765 r
.high
= cst
.high
& mask
.high
;
771 /* Returns true if CST fits in signed HOST_WIDE_INT. */
774 double_int::fits_shwi () const
776 const double_int
&cst
= *this;
778 return (HOST_WIDE_INT
) cst
.low
>= 0;
779 else if (cst
.high
== -1)
780 return (HOST_WIDE_INT
) cst
.low
< 0;
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. */
789 double_int::fits_hwi (bool uns
) const
792 return this->fits_uhwi ();
794 return this->fits_shwi ();
800 double_int::operator * (double_int b
) const
802 const double_int
&a
= *this;
804 mul_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
808 /* Multiplies *this with B and returns a reference to *this. */
811 double_int::operator *= (double_int b
)
813 mul_double (low
, high
, b
.low
, b
.high
, &low
, &high
);
817 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
818 *OVERFLOW is set to nonzero. */
821 double_int::mul_with_sign (double_int b
, bool unsigned_p
, bool *overflow
) const
823 const double_int
&a
= *this;
825 *overflow
= mul_double_wide_with_sign (a
.low
, a
.high
, b
.low
, b
.high
,
827 &tem
.low
, &tem
.high
, unsigned_p
);
832 double_int::wide_mul_with_sign (double_int b
, bool unsigned_p
,
833 double_int
*higher
, bool *overflow
) const
837 *overflow
= mul_double_wide_with_sign (low
, high
, b
.low
, b
.high
,
838 &lower
.low
, &lower
.high
,
839 &higher
->low
, &higher
->high
,
847 double_int::operator + (double_int b
) const
849 const double_int
&a
= *this;
851 add_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
855 /* Adds B to *this and returns a reference to *this. */
858 double_int::operator += (double_int b
)
860 add_double (low
, high
, b
.low
, b
.high
, &low
, &high
);
865 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
866 *OVERFLOW is set to nonzero. */
869 double_int::add_with_sign (double_int b
, bool unsigned_p
, bool *overflow
) const
871 const double_int
&a
= *this;
873 *overflow
= add_double_with_sign (a
.low
, a
.high
, b
.low
, b
.high
,
874 &ret
.low
, &ret
.high
, unsigned_p
);
881 double_int::operator - (double_int b
) const
883 const double_int
&a
= *this;
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
);
890 /* Subtracts B from *this and returns a reference to *this. */
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
);
901 /* Returns A - B. If the operation overflows via inconsistent sign bits,
902 *OVERFLOW is set to nonzero. */
905 double_int::sub_with_overflow (double_int b
, bool *overflow
) const
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
);
917 double_int::operator - () const
919 const double_int
&a
= *this;
921 neg_double (a
.low
, a
.high
, &ret
.low
, &ret
.high
);
926 double_int::neg_with_overflow (bool *overflow
) const
929 *overflow
= neg_double (low
, high
, &ret
.low
, &ret
.high
);
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
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;
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
);
952 double_int::divmod (double_int b
, bool uns
, unsigned code
,
953 double_int
*mod
) const
955 const double_int
&a
= *this;
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
);
964 /* The same as double_int::divmod with UNS = false. */
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. */
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. */
985 double_int::div (double_int b
, bool uns
, unsigned code
) const
989 return this->divmod (b
, uns
, code
, &mod
);
992 /* The same as double_int::div with UNS = false. */
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. */
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. */
1013 double_int::mod (double_int b
, bool uns
, unsigned code
) const
1017 this->divmod (b
, uns
, code
, &mod
);
1021 /* The same as double_int::mod with UNS = false. */
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. */
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
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
;
1057 /* Set BITPOS bit in A. */
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
;
1065 a
.high
|= (HOST_WIDE_INT
) 1 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
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
;
1078 return HOST_BITS_PER_DOUBLE_INT
;
1079 bits
+= ctz_hwi (w
);
1083 /* Shift A left by COUNT places. */
1086 double_int::lshift (HOST_WIDE_INT count
) const
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. */
1099 else if (count
>= HOST_BITS_PER_WIDE_INT
)
1101 ret
.high
= low
<< (count
- HOST_BITS_PER_WIDE_INT
);
1106 ret
.high
= (((unsigned HOST_WIDE_INT
) high
<< count
)
1107 | (low
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
1108 ret
.low
= low
<< count
;
1114 /* Shift A right by COUNT places. */
1117 double_int::rshift (HOST_WIDE_INT count
) const
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. */
1130 else if (count
>= HOST_BITS_PER_WIDE_INT
)
1134 = (unsigned HOST_WIDE_INT
) (high
>> (count
- HOST_BITS_PER_WIDE_INT
));
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));
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. */
1152 double_int::lshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const
1156 lshift_double (low
, high
, count
, prec
, &ret
.low
, &ret
.high
);
1158 rshift_double (low
, high
, absu_hwi (count
), prec
, &ret
.low
, &ret
.high
, arith
);
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. */
1167 double_int::rshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const
1171 rshift_double (low
, high
, count
, prec
, &ret
.low
, &ret
.high
, arith
);
1173 lshift_double (low
, high
, absu_hwi (count
), prec
, &ret
.low
, &ret
.high
);
1177 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
1178 Shift right if COUNT is negative. */
1181 double_int::alshift (HOST_WIDE_INT count
, unsigned int prec
) const
1185 lshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
);
1187 rshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
, true);
1191 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
1192 Shift left if COUNT is negative. */
1195 double_int::arshift (HOST_WIDE_INT count
, unsigned int prec
) const
1199 rshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
, true);
1201 lshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
);
1205 /* Logical shift A left by COUNT places keeping only PREC bits of result.
1206 Shift right if COUNT is negative. */
1209 double_int::llshift (HOST_WIDE_INT count
, unsigned int prec
) const
1213 lshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
);
1215 rshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
, false);
1219 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1220 Shift left if COUNT is negative. */
1223 double_int::lrshift (HOST_WIDE_INT count
, unsigned int prec
) const
1227 rshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
, false);
1229 lshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
);
1233 /* Rotate A left by COUNT places keeping only PREC bits of result.
1234 Rotate right if COUNT is negative. */
1237 double_int::lrotate (HOST_WIDE_INT count
, unsigned int prec
) const
1245 t1
= this->llshift (count
, prec
);
1246 t2
= this->lrshift (prec
- count
, prec
);
1251 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1252 Rotate right if COUNT is negative. */
1255 double_int::rrotate (HOST_WIDE_INT count
, unsigned int prec
) const
1263 t1
= this->lrshift (count
, prec
);
1264 t2
= this->llshift (prec
- count
, prec
);
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
1276 return this->ucmp (b
);
1278 return this->scmp (b
);
1281 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 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
)
1290 if ((unsigned HOST_WIDE_INT
) a
.high
> (unsigned HOST_WIDE_INT
) b
.high
)
1300 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1304 double_int::scmp (double_int b
) const
1306 const double_int
&a
= *this;
1307 if (a
.high
< b
.high
)
1309 if (a
.high
> b
.high
)
1319 /* Compares two unsigned values A and B for less-than. */
1322 double_int::ult (double_int b
) const
1324 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1326 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1333 /* Compares two unsigned values A and B for less-than or equal-to. */
1336 double_int::ule (double_int b
) const
1338 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1340 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1347 /* Compares two unsigned values A and B for greater-than. */
1350 double_int::ugt (double_int b
) const
1352 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1354 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1361 /* Compares two signed values A and B for less-than. */
1364 double_int::slt (double_int b
) const
1375 /* Compares two signed values A and B for less-than or equal-to. */
1378 double_int::sle (double_int b
) const
1389 /* Compares two signed values A and B for greater-than. */
1392 double_int::sgt (double_int b
) const
1404 /* Compares two values A and B. Returns max value. Signedness of the
1405 comparison is given by UNS. */
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. */
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. */
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. */
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. */
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. */
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. */
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
);
1470 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1471 otherwise it is signed. */
1474 dump_double_int (FILE *file
, double_int cst
, bool uns
)
1476 unsigned digits
[100], n
;
1481 fprintf (file
, "0");
1485 if (!uns
&& cst
.is_negative ())
1487 fprintf (file
, "-");
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
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 ())
1514 vp
[1] = (unsigned HOST_WIDE_INT
) val
.high
;
1515 mpz_import (result
, 2, -1, sizeof (HOST_WIDE_INT
), 0, 0, vp
);
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. */
1526 mpz_get_double_int (const_tree type
, mpz_t val
, bool wrap
)
1528 unsigned HOST_WIDE_INT
*vp
;
1538 get_type_static_bounds (type
, min
, max
);
1540 if (mpz_cmp (val
, min
) < 0)
1542 else if (mpz_cmp (val
, max
) > 0)
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
;
1557 vp
= (unsigned HOST_WIDE_INT
*) alloca (count
* sizeof (HOST_WIDE_INT
));
1561 mpz_export (vp
, &count
, -1, sizeof (HOST_WIDE_INT
), 0, 0, val
);
1563 gcc_assert (wrap
|| count
<= 2);
1566 res
.high
= (HOST_WIDE_INT
) vp
[1];
1568 res
= res
.ext (TYPE_PRECISION (type
), TYPE_UNSIGNED (type
));
1569 if (mpz_sgn (val
) < 0)