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. */
27 #include "double-int.h"
36 static int add_double_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
37 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
38 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
41 #define add_double(l1,h1,l2,h2,lv,hv) \
42 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
44 static int neg_double (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
45 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*);
47 static int mul_double_wide_with_sign (unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
48 unsigned HOST_WIDE_INT
, HOST_WIDE_INT
,
49 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
50 unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*,
53 #define mul_double(l1,h1,l2,h2,lv,hv) \
54 mul_double_wide_with_sign (l1, h1, l2, h2, lv, hv, NULL, NULL, false)
56 static int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT
,
57 HOST_WIDE_INT
, unsigned HOST_WIDE_INT
,
58 HOST_WIDE_INT
, unsigned HOST_WIDE_INT
*,
59 HOST_WIDE_INT
*, unsigned HOST_WIDE_INT
*,
62 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
63 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
64 and SUM1. Then this yields nonzero if overflow occurred during the
67 Overflow occurs if A and B have the same sign, but A and SUM differ in
68 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
70 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
72 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
73 We do that by representing the two-word integer in 4 words, with only
74 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
75 number. The value of the word is LOWPART + HIGHPART * BASE. */
78 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
80 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
81 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
83 /* Unpack a two-word integer into 4 words.
84 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
85 WORDS points to the array of HOST_WIDE_INTs. */
88 encode (HOST_WIDE_INT
*words
, unsigned HOST_WIDE_INT low
, HOST_WIDE_INT hi
)
90 words
[0] = LOWPART (low
);
91 words
[1] = HIGHPART (low
);
92 words
[2] = LOWPART (hi
);
93 words
[3] = HIGHPART (hi
);
96 /* Pack an array of 4 words into a two-word integer.
97 WORDS points to the array of words.
98 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
101 decode (HOST_WIDE_INT
*words
, unsigned HOST_WIDE_INT
*low
,
104 *low
= words
[0] + words
[1] * BASE
;
105 *hi
= words
[2] + words
[3] * BASE
;
108 /* Add two doubleword integers with doubleword result.
109 Return nonzero if the operation overflows according to UNSIGNED_P.
110 Each argument is given as two `HOST_WIDE_INT' pieces.
111 One argument is L1 and H1; the other, L2 and H2.
112 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
115 add_double_with_sign (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
116 unsigned HOST_WIDE_INT l2
, HOST_WIDE_INT h2
,
117 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
,
120 unsigned HOST_WIDE_INT l
;
124 h
= (HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
) h1
125 + (unsigned HOST_WIDE_INT
) h2
132 return ((unsigned HOST_WIDE_INT
) h
< (unsigned HOST_WIDE_INT
) h1
136 return OVERFLOW_SUM_SIGN (h1
, h2
, h
);
139 /* Negate a doubleword integer with doubleword result.
140 Return nonzero if the operation overflows, assuming it's signed.
141 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
142 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
145 neg_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
146 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
151 *hv
= - (unsigned HOST_WIDE_INT
) h1
;
152 return (*hv
& h1
) < 0;
162 /* Multiply two doubleword integers with quadword result.
163 Return nonzero if the operation overflows according to UNSIGNED_P.
164 Each argument is given as two `HOST_WIDE_INT' pieces.
165 One argument is L1 and H1; the other, L2 and H2.
166 The value is stored as four `HOST_WIDE_INT' pieces in *LV and *HV,
168 If lw is NULL then only the low part and no overflow is computed. */
171 mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
172 unsigned HOST_WIDE_INT l2
, HOST_WIDE_INT h2
,
173 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
,
174 unsigned HOST_WIDE_INT
*lw
, HOST_WIDE_INT
*hw
,
177 HOST_WIDE_INT arg1
[4];
178 HOST_WIDE_INT arg2
[4];
179 HOST_WIDE_INT prod
[4 * 2];
180 unsigned HOST_WIDE_INT carry
;
182 unsigned HOST_WIDE_INT neglow
;
183 HOST_WIDE_INT neghigh
;
185 encode (arg1
, l1
, h1
);
186 encode (arg2
, l2
, h2
);
188 memset (prod
, 0, sizeof prod
);
190 for (i
= 0; i
< 4; i
++)
193 for (j
= 0; j
< 4; j
++)
196 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
197 carry
+= (unsigned HOST_WIDE_INT
) arg1
[i
] * arg2
[j
];
198 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
200 prod
[k
] = LOWPART (carry
);
201 carry
= HIGHPART (carry
);
206 decode (prod
, lv
, hv
);
208 /* We are not interested in the wide part nor in overflow. */
212 decode (prod
+ 4, lw
, hw
);
214 /* Unsigned overflow is immediate. */
216 return (*lw
| *hw
) != 0;
218 /* Check for signed overflow by calculating the signed representation of the
219 top half of the result; it should agree with the low half's sign bit. */
222 neg_double (l2
, h2
, &neglow
, &neghigh
);
223 add_double (neglow
, neghigh
, *lw
, *hw
, lw
, hw
);
227 neg_double (l1
, h1
, &neglow
, &neghigh
);
228 add_double (neglow
, neghigh
, *lw
, *hw
, lw
, hw
);
230 return (*hv
< 0 ? ~(*lw
& *hw
) : *lw
| *hw
) != 0;
233 /* Shift the doubleword integer in L1, H1 right by COUNT places
234 keeping only PREC bits of result. ARITH nonzero specifies
235 arithmetic shifting; otherwise use logical shift.
236 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
239 rshift_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
240 unsigned HOST_WIDE_INT count
, unsigned int prec
,
241 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
,
244 unsigned HOST_WIDE_INT signmask
;
247 ? -((unsigned HOST_WIDE_INT
) h1
>> (HOST_BITS_PER_WIDE_INT
- 1))
250 if (count
>= HOST_BITS_PER_DOUBLE_INT
)
252 /* Shifting by the host word size is undefined according to the
253 ANSI standard, so we must handle this as a special case. */
257 else if (count
>= HOST_BITS_PER_WIDE_INT
)
260 *lv
= (unsigned HOST_WIDE_INT
) h1
>> (count
- HOST_BITS_PER_WIDE_INT
);
264 *hv
= (unsigned HOST_WIDE_INT
) h1
>> count
;
266 | ((unsigned HOST_WIDE_INT
) h1
267 << (HOST_BITS_PER_WIDE_INT
- count
- 1) << 1));
270 /* Zero / sign extend all bits that are beyond the precision. */
277 else if ((prec
- count
) >= HOST_BITS_PER_DOUBLE_INT
)
279 else if ((prec
- count
) >= HOST_BITS_PER_WIDE_INT
)
281 *hv
&= ~(HOST_WIDE_INT_M1U
<< (prec
- count
- HOST_BITS_PER_WIDE_INT
));
282 *hv
|= signmask
<< (prec
- count
- HOST_BITS_PER_WIDE_INT
);
287 *lv
&= ~(HOST_WIDE_INT_M1U
<< (prec
- count
));
288 *lv
|= signmask
<< (prec
- count
);
292 /* Shift the doubleword integer in L1, H1 left by COUNT places
293 keeping only PREC bits of result.
294 Shift right if COUNT is negative.
295 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
296 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
299 lshift_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
300 unsigned HOST_WIDE_INT count
, unsigned int prec
,
301 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
303 unsigned HOST_WIDE_INT signmask
;
305 if (count
>= HOST_BITS_PER_DOUBLE_INT
)
307 /* Shifting by the host word size is undefined according to the
308 ANSI standard, so we must handle this as a special case. */
312 else if (count
>= HOST_BITS_PER_WIDE_INT
)
314 *hv
= l1
<< (count
- HOST_BITS_PER_WIDE_INT
);
319 *hv
= (((unsigned HOST_WIDE_INT
) h1
<< count
)
320 | (l1
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
324 /* Sign extend all bits that are beyond the precision. */
326 signmask
= -((prec
> HOST_BITS_PER_WIDE_INT
327 ? ((unsigned HOST_WIDE_INT
) *hv
328 >> (prec
- HOST_BITS_PER_WIDE_INT
- 1))
329 : (*lv
>> (prec
- 1))) & 1);
331 if (prec
>= HOST_BITS_PER_DOUBLE_INT
)
333 else if (prec
>= HOST_BITS_PER_WIDE_INT
)
335 *hv
&= ~(HOST_WIDE_INT_M1U
<< (prec
- HOST_BITS_PER_WIDE_INT
));
336 *hv
|= signmask
<< (prec
- HOST_BITS_PER_WIDE_INT
);
341 *lv
&= ~(HOST_WIDE_INT_M1U
<< prec
);
342 *lv
|= signmask
<< prec
;
346 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
347 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
348 CODE is a tree code for a kind of division, one of
349 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
351 It controls how the quotient is rounded to an integer.
352 Return nonzero if the operation overflows.
353 UNS nonzero says do unsigned division. */
356 div_and_round_double (unsigned code
, int uns
,
357 /* num == numerator == dividend */
358 unsigned HOST_WIDE_INT lnum_orig
,
359 HOST_WIDE_INT hnum_orig
,
360 /* den == denominator == divisor */
361 unsigned HOST_WIDE_INT lden_orig
,
362 HOST_WIDE_INT hden_orig
,
363 unsigned HOST_WIDE_INT
*lquo
,
364 HOST_WIDE_INT
*hquo
, unsigned HOST_WIDE_INT
*lrem
,
368 HOST_WIDE_INT num
[4 + 1]; /* extra element for scaling. */
369 HOST_WIDE_INT den
[4], quo
[4];
371 unsigned HOST_WIDE_INT work
;
372 unsigned HOST_WIDE_INT carry
= 0;
373 unsigned HOST_WIDE_INT lnum
= lnum_orig
;
374 HOST_WIDE_INT hnum
= hnum_orig
;
375 unsigned HOST_WIDE_INT lden
= lden_orig
;
376 HOST_WIDE_INT hden
= hden_orig
;
379 if (hden
== 0 && lden
== 0)
380 overflow
= 1, lden
= 1;
382 /* Calculate quotient sign and convert operands to unsigned. */
388 /* (minimum integer) / (-1) is the only overflow case. */
389 if (neg_double (lnum
, hnum
, &lnum
, &hnum
)
390 && ((HOST_WIDE_INT
) lden
& hden
) == -1)
396 neg_double (lden
, hden
, &lden
, &hden
);
400 if (hnum
== 0 && hden
== 0)
401 { /* single precision */
403 /* This unsigned division rounds toward zero. */
409 { /* trivial case: dividend < divisor */
410 /* hden != 0 already checked. */
417 memset (quo
, 0, sizeof quo
);
419 memset (num
, 0, sizeof num
); /* to zero 9th element */
420 memset (den
, 0, sizeof den
);
422 encode (num
, lnum
, hnum
);
423 encode (den
, lden
, hden
);
425 /* Special code for when the divisor < BASE. */
426 if (hden
== 0 && lden
< (unsigned HOST_WIDE_INT
) BASE
)
428 /* hnum != 0 already checked. */
429 for (i
= 4 - 1; i
>= 0; i
--)
431 work
= num
[i
] + carry
* BASE
;
432 quo
[i
] = work
/ lden
;
438 /* Full double precision division,
439 with thanks to Don Knuth's "Seminumerical Algorithms". */
440 int num_hi_sig
, den_hi_sig
;
441 unsigned HOST_WIDE_INT quo_est
, scale
;
443 /* Find the highest nonzero divisor digit. */
444 for (i
= 4 - 1;; i
--)
451 /* Insure that the first digit of the divisor is at least BASE/2.
452 This is required by the quotient digit estimation algorithm. */
454 scale
= BASE
/ (den
[den_hi_sig
] + 1);
456 { /* scale divisor and dividend */
458 for (i
= 0; i
<= 4 - 1; i
++)
460 work
= (num
[i
] * scale
) + carry
;
461 num
[i
] = LOWPART (work
);
462 carry
= HIGHPART (work
);
467 for (i
= 0; i
<= 4 - 1; i
++)
469 work
= (den
[i
] * scale
) + carry
;
470 den
[i
] = LOWPART (work
);
471 carry
= HIGHPART (work
);
472 if (den
[i
] != 0) den_hi_sig
= i
;
479 for (i
= num_hi_sig
- den_hi_sig
- 1; i
>= 0; i
--)
481 /* Guess the next quotient digit, quo_est, by dividing the first
482 two remaining dividend digits by the high order quotient digit.
483 quo_est is never low and is at most 2 high. */
484 unsigned HOST_WIDE_INT tmp
;
486 num_hi_sig
= i
+ den_hi_sig
+ 1;
487 work
= num
[num_hi_sig
] * BASE
+ num
[num_hi_sig
- 1];
488 if (num
[num_hi_sig
] != den
[den_hi_sig
])
489 quo_est
= work
/ den
[den_hi_sig
];
493 /* Refine quo_est so it's usually correct, and at most one high. */
494 tmp
= work
- quo_est
* den
[den_hi_sig
];
496 && (den
[den_hi_sig
- 1] * quo_est
497 > (tmp
* BASE
+ num
[num_hi_sig
- 2])))
500 /* Try QUO_EST as the quotient digit, by multiplying the
501 divisor by QUO_EST and subtracting from the remaining dividend.
502 Keep in mind that QUO_EST is the I - 1st digit. */
505 for (j
= 0; j
<= den_hi_sig
; j
++)
507 work
= quo_est
* den
[j
] + carry
;
508 carry
= HIGHPART (work
);
509 work
= num
[i
+ j
] - LOWPART (work
);
510 num
[i
+ j
] = LOWPART (work
);
511 carry
+= HIGHPART (work
) != 0;
514 /* If quo_est was high by one, then num[i] went negative and
515 we need to correct things. */
516 if (num
[num_hi_sig
] < (HOST_WIDE_INT
) carry
)
519 carry
= 0; /* add divisor back in */
520 for (j
= 0; j
<= den_hi_sig
; j
++)
522 work
= num
[i
+ j
] + den
[j
] + carry
;
523 carry
= HIGHPART (work
);
524 num
[i
+ j
] = LOWPART (work
);
527 num
[num_hi_sig
] += carry
;
530 /* Store the quotient digit. */
535 decode (quo
, lquo
, hquo
);
538 /* If result is negative, make it so. */
540 neg_double (*lquo
, *hquo
, lquo
, hquo
);
542 /* Compute trial remainder: rem = num - (quo * den) */
543 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
544 neg_double (*lrem
, *hrem
, lrem
, hrem
);
545 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
550 case TRUNC_MOD_EXPR
: /* round toward zero */
551 case EXACT_DIV_EXPR
: /* for this one, it shouldn't matter */
555 case FLOOR_MOD_EXPR
: /* round toward negative infinity */
556 if (quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio < 0 && rem != 0 */
559 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1,
567 case CEIL_MOD_EXPR
: /* round toward positive infinity */
568 if (!quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio > 0 && rem != 0 */
570 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
578 case ROUND_MOD_EXPR
: /* round to closest integer */
580 unsigned HOST_WIDE_INT labs_rem
= *lrem
;
581 HOST_WIDE_INT habs_rem
= *hrem
;
582 unsigned HOST_WIDE_INT labs_den
= lden
, lnegabs_rem
, ldiff
;
583 HOST_WIDE_INT habs_den
= hden
, hnegabs_rem
, hdiff
;
585 /* Get absolute values. */
586 if (!uns
&& *hrem
< 0)
587 neg_double (*lrem
, *hrem
, &labs_rem
, &habs_rem
);
588 if (!uns
&& hden
< 0)
589 neg_double (lden
, hden
, &labs_den
, &habs_den
);
591 /* If abs(rem) >= abs(den) - abs(rem), adjust the quotient. */
592 neg_double (labs_rem
, habs_rem
, &lnegabs_rem
, &hnegabs_rem
);
593 add_double (labs_den
, habs_den
, lnegabs_rem
, hnegabs_rem
,
596 if (((unsigned HOST_WIDE_INT
) habs_rem
597 > (unsigned HOST_WIDE_INT
) hdiff
)
598 || (habs_rem
== hdiff
&& labs_rem
>= ldiff
))
602 add_double (*lquo
, *hquo
,
603 (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1, lquo
, hquo
);
606 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
618 /* Compute true remainder: rem = num - (quo * den) */
619 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
620 neg_double (*lrem
, *hrem
, lrem
, hrem
);
621 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
626 /* Construct from a buffer of length LEN. BUFFER will be read according
627 to byte endianess and word endianess. Only the lower LEN bytes
628 of the result are set; the remaining high bytes are cleared. */
631 double_int::from_buffer (const unsigned char *buffer
, int len
)
633 double_int result
= double_int_zero
;
634 int words
= len
/ UNITS_PER_WORD
;
636 gcc_assert (len
* BITS_PER_UNIT
<= HOST_BITS_PER_DOUBLE_INT
);
638 for (int byte
= 0; byte
< len
; byte
++)
641 int bitpos
= byte
* BITS_PER_UNIT
;
642 unsigned HOST_WIDE_INT value
;
644 if (len
> UNITS_PER_WORD
)
646 int word
= byte
/ UNITS_PER_WORD
;
648 if (WORDS_BIG_ENDIAN
)
649 word
= (words
- 1) - word
;
651 offset
= word
* UNITS_PER_WORD
;
653 if (BYTES_BIG_ENDIAN
)
654 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
656 offset
+= byte
% UNITS_PER_WORD
;
659 offset
= BYTES_BIG_ENDIAN
? (len
- 1) - byte
: byte
;
661 value
= (unsigned HOST_WIDE_INT
) buffer
[offset
];
663 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
664 result
.low
|= value
<< bitpos
;
666 result
.high
|= value
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
673 /* Returns mask for PREC bits. */
676 double_int::mask (unsigned prec
)
678 unsigned HOST_WIDE_INT m
;
681 if (prec
> HOST_BITS_PER_WIDE_INT
)
683 prec
-= HOST_BITS_PER_WIDE_INT
;
684 m
= ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1;
685 mask
.high
= (HOST_WIDE_INT
) m
;
691 mask
.low
= prec
? ((unsigned HOST_WIDE_INT
) 2 << (prec
- 1)) - 1 : 0;
697 /* Returns a maximum value for signed or unsigned integer
698 of precision PREC. */
701 double_int::max_value (unsigned int prec
, bool uns
)
703 return double_int::mask (prec
- (uns
? 0 : 1));
706 /* Returns a minimum value for signed or unsigned integer
707 of precision PREC. */
710 double_int::min_value (unsigned int prec
, bool uns
)
713 return double_int_zero
;
714 return double_int_one
.lshift (prec
- 1, prec
, false);
717 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
718 outside of the precision are set to the sign bit (i.e., the PREC-th one),
719 otherwise they are set to zero.
721 This corresponds to returning the value represented by PREC lowermost bits
722 of CST, with the given signedness. */
725 double_int::ext (unsigned prec
, bool uns
) const
728 return this->zext (prec
);
730 return this->sext (prec
);
733 /* The same as double_int::ext with UNS = true. */
736 double_int::zext (unsigned prec
) const
738 const double_int
&cst
= *this;
739 double_int mask
= double_int::mask (prec
);
742 r
.low
= cst
.low
& mask
.low
;
743 r
.high
= cst
.high
& mask
.high
;
748 /* The same as double_int::ext with UNS = false. */
751 double_int::sext (unsigned prec
) const
753 const double_int
&cst
= *this;
754 double_int mask
= double_int::mask (prec
);
756 unsigned HOST_WIDE_INT snum
;
758 if (prec
<= HOST_BITS_PER_WIDE_INT
)
762 prec
-= HOST_BITS_PER_WIDE_INT
;
763 snum
= (unsigned HOST_WIDE_INT
) cst
.high
;
765 if (((snum
>> (prec
- 1)) & 1) == 1)
767 r
.low
= cst
.low
| ~mask
.low
;
768 r
.high
= cst
.high
| ~mask
.high
;
772 r
.low
= cst
.low
& mask
.low
;
773 r
.high
= cst
.high
& mask
.high
;
779 /* Returns true if CST fits in signed HOST_WIDE_INT. */
782 double_int::fits_shwi () const
784 const double_int
&cst
= *this;
786 return (HOST_WIDE_INT
) cst
.low
>= 0;
787 else if (cst
.high
== -1)
788 return (HOST_WIDE_INT
) cst
.low
< 0;
793 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
794 unsigned HOST_WIDE_INT if UNS is true. */
797 double_int::fits_hwi (bool uns
) const
800 return this->fits_uhwi ();
802 return this->fits_shwi ();
808 double_int::operator * (double_int b
) const
810 const double_int
&a
= *this;
812 mul_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
816 /* Multiplies *this with B and returns a reference to *this. */
819 double_int::operator *= (double_int b
)
821 mul_double (low
, high
, b
.low
, b
.high
, &low
, &high
);
825 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
826 *OVERFLOW is set to nonzero. */
829 double_int::mul_with_sign (double_int b
, bool unsigned_p
, bool *overflow
) const
831 const double_int
&a
= *this;
833 *overflow
= mul_double_wide_with_sign (a
.low
, a
.high
, b
.low
, b
.high
,
835 &tem
.low
, &tem
.high
, unsigned_p
);
840 double_int::wide_mul_with_sign (double_int b
, bool unsigned_p
,
841 double_int
*higher
, bool *overflow
) const
845 *overflow
= mul_double_wide_with_sign (low
, high
, b
.low
, b
.high
,
846 &lower
.low
, &lower
.high
,
847 &higher
->low
, &higher
->high
,
855 double_int::operator + (double_int b
) const
857 const double_int
&a
= *this;
859 add_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
863 /* Adds B to *this and returns a reference to *this. */
866 double_int::operator += (double_int b
)
868 add_double (low
, high
, b
.low
, b
.high
, &low
, &high
);
873 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
874 *OVERFLOW is set to nonzero. */
877 double_int::add_with_sign (double_int b
, bool unsigned_p
, bool *overflow
) const
879 const double_int
&a
= *this;
881 *overflow
= add_double_with_sign (a
.low
, a
.high
, b
.low
, b
.high
,
882 &ret
.low
, &ret
.high
, unsigned_p
);
889 double_int::operator - (double_int b
) const
891 const double_int
&a
= *this;
893 neg_double (b
.low
, b
.high
, &b
.low
, &b
.high
);
894 add_double (a
.low
, a
.high
, b
.low
, b
.high
, &ret
.low
, &ret
.high
);
898 /* Subtracts B from *this and returns a reference to *this. */
901 double_int::operator -= (double_int b
)
903 neg_double (b
.low
, b
.high
, &b
.low
, &b
.high
);
904 add_double (low
, high
, b
.low
, b
.high
, &low
, &high
);
909 /* Returns A - B. If the operation overflows via inconsistent sign bits,
910 *OVERFLOW is set to nonzero. */
913 double_int::sub_with_overflow (double_int b
, bool *overflow
) const
916 neg_double (b
.low
, b
.high
, &ret
.low
, &ret
.high
);
917 add_double (low
, high
, ret
.low
, ret
.high
, &ret
.low
, &ret
.high
);
918 *overflow
= OVERFLOW_SUM_SIGN (ret
.high
, b
.high
, high
);
925 double_int::operator - () const
927 const double_int
&a
= *this;
929 neg_double (a
.low
, a
.high
, &ret
.low
, &ret
.high
);
934 double_int::neg_with_overflow (bool *overflow
) const
937 *overflow
= neg_double (low
, high
, &ret
.low
, &ret
.high
);
941 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
942 specified by CODE). CODE is enum tree_code in fact, but double_int.h
943 must be included before tree.h. The remainder after the division is
947 double_int::divmod_with_overflow (double_int b
, bool uns
, unsigned code
,
948 double_int
*mod
, bool *overflow
) const
950 const double_int
&a
= *this;
953 *overflow
= div_and_round_double (code
, uns
, a
.low
, a
.high
,
954 b
.low
, b
.high
, &ret
.low
, &ret
.high
,
955 &mod
->low
, &mod
->high
);
960 double_int::divmod (double_int b
, bool uns
, unsigned code
,
961 double_int
*mod
) const
963 const double_int
&a
= *this;
966 div_and_round_double (code
, uns
, a
.low
, a
.high
,
967 b
.low
, b
.high
, &ret
.low
, &ret
.high
,
968 &mod
->low
, &mod
->high
);
972 /* The same as double_int::divmod with UNS = false. */
975 double_int::sdivmod (double_int b
, unsigned code
, double_int
*mod
) const
977 return this->divmod (b
, false, code
, mod
);
980 /* The same as double_int::divmod with UNS = true. */
983 double_int::udivmod (double_int b
, unsigned code
, double_int
*mod
) const
985 return this->divmod (b
, true, code
, mod
);
988 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
989 specified by CODE). CODE is enum tree_code in fact, but double_int.h
990 must be included before tree.h. */
993 double_int::div (double_int b
, bool uns
, unsigned code
) const
997 return this->divmod (b
, uns
, code
, &mod
);
1000 /* The same as double_int::div with UNS = false. */
1003 double_int::sdiv (double_int b
, unsigned code
) const
1005 return this->div (b
, false, code
);
1008 /* The same as double_int::div with UNS = true. */
1011 double_int::udiv (double_int b
, unsigned code
) const
1013 return this->div (b
, true, code
);
1016 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
1017 specified by CODE). CODE is enum tree_code in fact, but double_int.h
1018 must be included before tree.h. */
1021 double_int::mod (double_int b
, bool uns
, unsigned code
) const
1025 this->divmod (b
, uns
, code
, &mod
);
1029 /* The same as double_int::mod with UNS = false. */
1032 double_int::smod (double_int b
, unsigned code
) const
1034 return this->mod (b
, false, code
);
1037 /* The same as double_int::mod with UNS = true. */
1040 double_int::umod (double_int b
, unsigned code
) const
1042 return this->mod (b
, true, code
);
1045 /* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
1046 the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
1050 double_int::multiple_of (double_int factor
,
1051 bool unsigned_p
, double_int
*multiple
) const
1053 double_int remainder
;
1054 double_int quotient
= this->divmod (factor
, unsigned_p
,
1055 TRUNC_DIV_EXPR
, &remainder
);
1056 if (remainder
.is_zero ())
1058 *multiple
= quotient
;
1065 /* Set BITPOS bit in A. */
1067 double_int::set_bit (unsigned bitpos
) const
1069 double_int a
= *this;
1070 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
1071 a
.low
|= (unsigned HOST_WIDE_INT
) 1 << bitpos
;
1073 a
.high
|= (HOST_WIDE_INT
) 1 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
1078 /* Count trailing zeros in A. */
1080 double_int::trailing_zeros () const
1082 const double_int
&a
= *this;
1083 unsigned HOST_WIDE_INT w
= a
.low
? a
.low
: (unsigned HOST_WIDE_INT
) a
.high
;
1084 unsigned bits
= a
.low
? 0 : HOST_BITS_PER_WIDE_INT
;
1086 return HOST_BITS_PER_DOUBLE_INT
;
1087 bits
+= ctz_hwi (w
);
1091 /* Shift A left by COUNT places. */
1094 double_int::lshift (HOST_WIDE_INT count
) const
1098 gcc_checking_assert (count
>= 0);
1100 if (count
>= HOST_BITS_PER_DOUBLE_INT
)
1102 /* Shifting by the host word size is undefined according to the
1103 ANSI standard, so we must handle this as a special case. */
1107 else if (count
>= HOST_BITS_PER_WIDE_INT
)
1109 ret
.high
= low
<< (count
- HOST_BITS_PER_WIDE_INT
);
1114 ret
.high
= (((unsigned HOST_WIDE_INT
) high
<< count
)
1115 | (low
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
1116 ret
.low
= low
<< count
;
1122 /* Shift A right by COUNT places. */
1125 double_int::rshift (HOST_WIDE_INT count
) const
1129 gcc_checking_assert (count
>= 0);
1131 if (count
>= HOST_BITS_PER_DOUBLE_INT
)
1133 /* Shifting by the host word size is undefined according to the
1134 ANSI standard, so we must handle this as a special case. */
1138 else if (count
>= HOST_BITS_PER_WIDE_INT
)
1142 = (unsigned HOST_WIDE_INT
) (high
>> (count
- HOST_BITS_PER_WIDE_INT
));
1146 ret
.high
= high
>> count
;
1147 ret
.low
= ((low
>> count
)
1148 | ((unsigned HOST_WIDE_INT
) high
1149 << (HOST_BITS_PER_WIDE_INT
- count
- 1) << 1));
1155 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
1156 right if COUNT is negative. ARITH true specifies arithmetic shifting;
1157 otherwise use logical shift. */
1160 double_int::lshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const
1164 lshift_double (low
, high
, count
, prec
, &ret
.low
, &ret
.high
);
1166 rshift_double (low
, high
, absu_hwi (count
), prec
, &ret
.low
, &ret
.high
, arith
);
1170 /* Shift A right by COUNT places keeping only PREC bits of result. Shift
1171 left if COUNT is negative. ARITH true specifies arithmetic shifting;
1172 otherwise use logical shift. */
1175 double_int::rshift (HOST_WIDE_INT count
, unsigned int prec
, bool arith
) const
1179 rshift_double (low
, high
, count
, prec
, &ret
.low
, &ret
.high
, arith
);
1181 lshift_double (low
, high
, absu_hwi (count
), prec
, &ret
.low
, &ret
.high
);
1185 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
1186 Shift right if COUNT is negative. */
1189 double_int::alshift (HOST_WIDE_INT count
, unsigned int prec
) const
1193 lshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
);
1195 rshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
, true);
1199 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
1200 Shift left if COUNT is negative. */
1203 double_int::arshift (HOST_WIDE_INT count
, unsigned int prec
) const
1207 rshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
, true);
1209 lshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
);
1213 /* Logical shift A left by COUNT places keeping only PREC bits of result.
1214 Shift right if COUNT is negative. */
1217 double_int::llshift (HOST_WIDE_INT count
, unsigned int prec
) const
1221 lshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
);
1223 rshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
, false);
1227 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1228 Shift left if COUNT is negative. */
1231 double_int::lrshift (HOST_WIDE_INT count
, unsigned int prec
) const
1235 rshift_double (low
, high
, count
, prec
, &r
.low
, &r
.high
, false);
1237 lshift_double (low
, high
, absu_hwi (count
), prec
, &r
.low
, &r
.high
);
1241 /* Rotate A left by COUNT places keeping only PREC bits of result.
1242 Rotate right if COUNT is negative. */
1245 double_int::lrotate (HOST_WIDE_INT count
, unsigned int prec
) const
1253 t1
= this->llshift (count
, prec
);
1254 t2
= this->lrshift (prec
- count
, prec
);
1259 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1260 Rotate right if COUNT is negative. */
1263 double_int::rrotate (HOST_WIDE_INT count
, unsigned int prec
) const
1271 t1
= this->lrshift (count
, prec
);
1272 t2
= this->llshift (prec
- count
, prec
);
1277 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1278 comparison is given by UNS. */
1281 double_int::cmp (double_int b
, bool uns
) const
1284 return this->ucmp (b
);
1286 return this->scmp (b
);
1289 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
1293 double_int::ucmp (double_int b
) const
1295 const double_int
&a
= *this;
1296 if ((unsigned HOST_WIDE_INT
) a
.high
< (unsigned HOST_WIDE_INT
) b
.high
)
1298 if ((unsigned HOST_WIDE_INT
) a
.high
> (unsigned HOST_WIDE_INT
) b
.high
)
1308 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1312 double_int::scmp (double_int b
) const
1314 const double_int
&a
= *this;
1315 if (a
.high
< b
.high
)
1317 if (a
.high
> b
.high
)
1327 /* Compares two unsigned values A and B for less-than. */
1330 double_int::ult (double_int b
) const
1332 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1334 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1341 /* Compares two unsigned values A and B for less-than or equal-to. */
1344 double_int::ule (double_int b
) const
1346 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1348 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1355 /* Compares two unsigned values A and B for greater-than. */
1358 double_int::ugt (double_int b
) const
1360 if ((unsigned HOST_WIDE_INT
) high
> (unsigned HOST_WIDE_INT
) b
.high
)
1362 if ((unsigned HOST_WIDE_INT
) high
< (unsigned HOST_WIDE_INT
) b
.high
)
1369 /* Compares two signed values A and B for less-than. */
1372 double_int::slt (double_int b
) const
1383 /* Compares two signed values A and B for less-than or equal-to. */
1386 double_int::sle (double_int b
) const
1397 /* Compares two signed values A and B for greater-than. */
1400 double_int::sgt (double_int b
) const
1412 /* Compares two values A and B. Returns max value. Signedness of the
1413 comparison is given by UNS. */
1416 double_int::max (double_int b
, bool uns
)
1418 return (this->cmp (b
, uns
) == 1) ? *this : b
;
1421 /* Compares two signed values A and B. Returns max value. */
1424 double_int::smax (double_int b
)
1426 return (this->scmp (b
) == 1) ? *this : b
;
1429 /* Compares two unsigned values A and B. Returns max value. */
1432 double_int::umax (double_int b
)
1434 return (this->ucmp (b
) == 1) ? *this : b
;
1437 /* Compares two values A and B. Returns mix value. Signedness of the
1438 comparison is given by UNS. */
1441 double_int::min (double_int b
, bool uns
)
1443 return (this->cmp (b
, uns
) == -1) ? *this : b
;
1446 /* Compares two signed values A and B. Returns min value. */
1449 double_int::smin (double_int b
)
1451 return (this->scmp (b
) == -1) ? *this : b
;
1454 /* Compares two unsigned values A and B. Returns min value. */
1457 double_int::umin (double_int b
)
1459 return (this->ucmp (b
) == -1) ? *this : b
;
1462 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1465 double_int_split_digit (double_int
*cst
, unsigned base
)
1467 unsigned HOST_WIDE_INT resl
, reml
;
1468 HOST_WIDE_INT resh
, remh
;
1470 div_and_round_double (FLOOR_DIV_EXPR
, true, cst
->low
, cst
->high
, base
, 0,
1471 &resl
, &resh
, &reml
, &remh
);
1478 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1479 otherwise it is signed. */
1482 dump_double_int (FILE *file
, double_int cst
, bool uns
)
1484 unsigned digits
[100], n
;
1489 fprintf (file
, "0");
1493 if (!uns
&& cst
.is_negative ())
1495 fprintf (file
, "-");
1499 for (n
= 0; !cst
.is_zero (); n
++)
1500 digits
[n
] = double_int_split_digit (&cst
, 10);
1501 for (i
= n
- 1; i
>= 0; i
--)
1502 fprintf (file
, "%u", digits
[i
]);
1506 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1510 mpz_set_double_int (mpz_t result
, double_int val
, bool uns
)
1512 bool negate
= false;
1513 unsigned HOST_WIDE_INT vp
[2];
1515 if (!uns
&& val
.is_negative ())
1522 vp
[1] = (unsigned HOST_WIDE_INT
) val
.high
;
1523 mpz_import (result
, 2, -1, sizeof (HOST_WIDE_INT
), 0, 0, vp
);
1526 mpz_neg (result
, result
);
1529 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1530 values of VAL will be wrapped; otherwise, they will be set to the
1531 appropriate minimum or maximum TYPE bound. */
1534 mpz_get_double_int (const_tree type
, mpz_t val
, bool wrap
)
1536 unsigned HOST_WIDE_INT
*vp
;
1546 get_type_static_bounds (type
, min
, max
);
1548 if (mpz_cmp (val
, min
) < 0)
1550 else if (mpz_cmp (val
, max
) > 0)
1557 /* Determine the number of unsigned HOST_WIDE_INT that are required
1558 for representing the value. The code to calculate count is
1559 extracted from the GMP manual, section "Integer Import and Export":
1560 http://gmplib.org/manual/Integer-Import-and-Export.html */
1561 numb
= 8 * sizeof (HOST_WIDE_INT
);
1562 count
= (mpz_sizeinbase (val
, 2) + numb
-1) / numb
;
1565 vp
= (unsigned HOST_WIDE_INT
*) alloca (count
* sizeof (HOST_WIDE_INT
));
1569 mpz_export (vp
, &count
, -1, sizeof (HOST_WIDE_INT
), 0, 0, val
);
1571 gcc_assert (wrap
|| count
<= 2);
1574 res
.high
= (HOST_WIDE_INT
) vp
[1];
1576 res
= res
.ext (TYPE_PRECISION (type
), TYPE_UNSIGNED (type
));
1577 if (mpz_sgn (val
) < 0)