coretypes.h: Include machmode.h...
[gcc.git] / gcc / double-int.c
1 /* Operations with long integers.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h" /* For BITS_PER_UNIT and *_BIG_ENDIAN. */
24 #include "hash-set.h"
25 #include "vec.h"
26 #include "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "inchash.h"
30 #include "tree.h"
31
32 static int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
33 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
34 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
35 bool);
36
37 #define add_double(l1,h1,l2,h2,lv,hv) \
38 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
39
40 static int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
41 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
42
43 static int mul_double_wide_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
44 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
45 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
46 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
47 bool);
48
49 #define mul_double(l1,h1,l2,h2,lv,hv) \
50 mul_double_wide_with_sign (l1, h1, l2, h2, lv, hv, NULL, NULL, false)
51
52 static int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
53 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
54 HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
55 HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
56 HOST_WIDE_INT *);
57
58 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
59 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
60 and SUM1. Then this yields nonzero if overflow occurred during the
61 addition.
62
63 Overflow occurs if A and B have the same sign, but A and SUM differ in
64 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
65 sign. */
66 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
67
68 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
69 We do that by representing the two-word integer in 4 words, with only
70 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
71 number. The value of the word is LOWPART + HIGHPART * BASE. */
72
73 #define LOWPART(x) \
74 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
75 #define HIGHPART(x) \
76 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
77 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
78
79 /* Unpack a two-word integer into 4 words.
80 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
81 WORDS points to the array of HOST_WIDE_INTs. */
82
83 static void
84 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
85 {
86 words[0] = LOWPART (low);
87 words[1] = HIGHPART (low);
88 words[2] = LOWPART (hi);
89 words[3] = HIGHPART (hi);
90 }
91
92 /* Pack an array of 4 words into a two-word integer.
93 WORDS points to the array of words.
94 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
95
96 static void
97 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
98 HOST_WIDE_INT *hi)
99 {
100 *low = words[0] + words[1] * BASE;
101 *hi = words[2] + words[3] * BASE;
102 }
103
104 /* Add two doubleword integers with doubleword result.
105 Return nonzero if the operation overflows according to UNSIGNED_P.
106 Each argument is given as two `HOST_WIDE_INT' pieces.
107 One argument is L1 and H1; the other, L2 and H2.
108 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
109
110 static int
111 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
112 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
113 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
114 bool unsigned_p)
115 {
116 unsigned HOST_WIDE_INT l;
117 HOST_WIDE_INT h;
118
119 l = l1 + l2;
120 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
121 + (unsigned HOST_WIDE_INT) h2
122 + (l < l1));
123
124 *lv = l;
125 *hv = h;
126
127 if (unsigned_p)
128 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
129 || (h == h1
130 && l < l1));
131 else
132 return OVERFLOW_SUM_SIGN (h1, h2, h);
133 }
134
135 /* Negate a doubleword integer with doubleword result.
136 Return nonzero if the operation overflows, assuming it's signed.
137 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
138 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
139
140 static int
141 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
142 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
143 {
144 if (l1 == 0)
145 {
146 *lv = 0;
147 *hv = - (unsigned HOST_WIDE_INT) h1;
148 return (*hv & h1) < 0;
149 }
150 else
151 {
152 *lv = -l1;
153 *hv = ~h1;
154 return 0;
155 }
156 }
157
158 /* Multiply two doubleword integers with quadword result.
159 Return nonzero if the operation overflows according to UNSIGNED_P.
160 Each argument is given as two `HOST_WIDE_INT' pieces.
161 One argument is L1 and H1; the other, L2 and H2.
162 The value is stored as four `HOST_WIDE_INT' pieces in *LV and *HV,
163 *LW and *HW.
164 If lw is NULL then only the low part and no overflow is computed. */
165
166 static int
167 mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
168 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
169 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
170 unsigned HOST_WIDE_INT *lw, HOST_WIDE_INT *hw,
171 bool unsigned_p)
172 {
173 HOST_WIDE_INT arg1[4];
174 HOST_WIDE_INT arg2[4];
175 HOST_WIDE_INT prod[4 * 2];
176 unsigned HOST_WIDE_INT carry;
177 int i, j, k;
178 unsigned HOST_WIDE_INT neglow;
179 HOST_WIDE_INT neghigh;
180
181 encode (arg1, l1, h1);
182 encode (arg2, l2, h2);
183
184 memset (prod, 0, sizeof prod);
185
186 for (i = 0; i < 4; i++)
187 {
188 carry = 0;
189 for (j = 0; j < 4; j++)
190 {
191 k = i + j;
192 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
193 carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j];
194 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
195 carry += prod[k];
196 prod[k] = LOWPART (carry);
197 carry = HIGHPART (carry);
198 }
199 prod[i + 4] = carry;
200 }
201
202 decode (prod, lv, hv);
203
204 /* We are not interested in the wide part nor in overflow. */
205 if (lw == NULL)
206 return 0;
207
208 decode (prod + 4, lw, hw);
209
210 /* Unsigned overflow is immediate. */
211 if (unsigned_p)
212 return (*lw | *hw) != 0;
213
214 /* Check for signed overflow by calculating the signed representation of the
215 top half of the result; it should agree with the low half's sign bit. */
216 if (h1 < 0)
217 {
218 neg_double (l2, h2, &neglow, &neghigh);
219 add_double (neglow, neghigh, *lw, *hw, lw, hw);
220 }
221 if (h2 < 0)
222 {
223 neg_double (l1, h1, &neglow, &neghigh);
224 add_double (neglow, neghigh, *lw, *hw, lw, hw);
225 }
226 return (*hv < 0 ? ~(*lw & *hw) : *lw | *hw) != 0;
227 }
228
229 /* Shift the doubleword integer in L1, H1 right by COUNT places
230 keeping only PREC bits of result. ARITH nonzero specifies
231 arithmetic shifting; otherwise use logical shift.
232 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
233
234 static void
235 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
236 unsigned HOST_WIDE_INT count, unsigned int prec,
237 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
238 bool arith)
239 {
240 unsigned HOST_WIDE_INT signmask;
241
242 signmask = (arith
243 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
244 : 0);
245
246 if (count >= HOST_BITS_PER_DOUBLE_INT)
247 {
248 /* Shifting by the host word size is undefined according to the
249 ANSI standard, so we must handle this as a special case. */
250 *hv = 0;
251 *lv = 0;
252 }
253 else if (count >= HOST_BITS_PER_WIDE_INT)
254 {
255 *hv = 0;
256 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
257 }
258 else
259 {
260 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
261 *lv = ((l1 >> count)
262 | ((unsigned HOST_WIDE_INT) h1
263 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
264 }
265
266 /* Zero / sign extend all bits that are beyond the precision. */
267
268 if (count >= prec)
269 {
270 *hv = signmask;
271 *lv = signmask;
272 }
273 else if ((prec - count) >= HOST_BITS_PER_DOUBLE_INT)
274 ;
275 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
276 {
277 *hv &= ~(HOST_WIDE_INT_M1U << (prec - count - HOST_BITS_PER_WIDE_INT));
278 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
279 }
280 else
281 {
282 *hv = signmask;
283 *lv &= ~(HOST_WIDE_INT_M1U << (prec - count));
284 *lv |= signmask << (prec - count);
285 }
286 }
287
288 /* Shift the doubleword integer in L1, H1 left by COUNT places
289 keeping only PREC bits of result.
290 Shift right if COUNT is negative.
291 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
292 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
293
294 static void
295 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
296 unsigned HOST_WIDE_INT count, unsigned int prec,
297 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
298 {
299 unsigned HOST_WIDE_INT signmask;
300
301 if (count >= HOST_BITS_PER_DOUBLE_INT)
302 {
303 /* Shifting by the host word size is undefined according to the
304 ANSI standard, so we must handle this as a special case. */
305 *hv = 0;
306 *lv = 0;
307 }
308 else if (count >= HOST_BITS_PER_WIDE_INT)
309 {
310 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
311 *lv = 0;
312 }
313 else
314 {
315 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
316 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
317 *lv = l1 << count;
318 }
319
320 /* Sign extend all bits that are beyond the precision. */
321
322 signmask = -((prec > HOST_BITS_PER_WIDE_INT
323 ? ((unsigned HOST_WIDE_INT) *hv
324 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
325 : (*lv >> (prec - 1))) & 1);
326
327 if (prec >= HOST_BITS_PER_DOUBLE_INT)
328 ;
329 else if (prec >= HOST_BITS_PER_WIDE_INT)
330 {
331 *hv &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
332 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
333 }
334 else
335 {
336 *hv = signmask;
337 *lv &= ~(HOST_WIDE_INT_M1U << prec);
338 *lv |= signmask << prec;
339 }
340 }
341
342 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
343 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
344 CODE is a tree code for a kind of division, one of
345 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
346 or EXACT_DIV_EXPR
347 It controls how the quotient is rounded to an integer.
348 Return nonzero if the operation overflows.
349 UNS nonzero says do unsigned division. */
350
351 static int
352 div_and_round_double (unsigned code, int uns,
353 /* num == numerator == dividend */
354 unsigned HOST_WIDE_INT lnum_orig,
355 HOST_WIDE_INT hnum_orig,
356 /* den == denominator == divisor */
357 unsigned HOST_WIDE_INT lden_orig,
358 HOST_WIDE_INT hden_orig,
359 unsigned HOST_WIDE_INT *lquo,
360 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
361 HOST_WIDE_INT *hrem)
362 {
363 int quo_neg = 0;
364 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
365 HOST_WIDE_INT den[4], quo[4];
366 int i, j;
367 unsigned HOST_WIDE_INT work;
368 unsigned HOST_WIDE_INT carry = 0;
369 unsigned HOST_WIDE_INT lnum = lnum_orig;
370 HOST_WIDE_INT hnum = hnum_orig;
371 unsigned HOST_WIDE_INT lden = lden_orig;
372 HOST_WIDE_INT hden = hden_orig;
373 int overflow = 0;
374
375 if (hden == 0 && lden == 0)
376 overflow = 1, lden = 1;
377
378 /* Calculate quotient sign and convert operands to unsigned. */
379 if (!uns)
380 {
381 if (hnum < 0)
382 {
383 quo_neg = ~ quo_neg;
384 /* (minimum integer) / (-1) is the only overflow case. */
385 if (neg_double (lnum, hnum, &lnum, &hnum)
386 && ((HOST_WIDE_INT) lden & hden) == -1)
387 overflow = 1;
388 }
389 if (hden < 0)
390 {
391 quo_neg = ~ quo_neg;
392 neg_double (lden, hden, &lden, &hden);
393 }
394 }
395
396 if (hnum == 0 && hden == 0)
397 { /* single precision */
398 *hquo = *hrem = 0;
399 /* This unsigned division rounds toward zero. */
400 *lquo = lnum / lden;
401 goto finish_up;
402 }
403
404 if (hnum == 0)
405 { /* trivial case: dividend < divisor */
406 /* hden != 0 already checked. */
407 *hquo = *lquo = 0;
408 *hrem = hnum;
409 *lrem = lnum;
410 goto finish_up;
411 }
412
413 memset (quo, 0, sizeof quo);
414
415 memset (num, 0, sizeof num); /* to zero 9th element */
416 memset (den, 0, sizeof den);
417
418 encode (num, lnum, hnum);
419 encode (den, lden, hden);
420
421 /* Special code for when the divisor < BASE. */
422 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
423 {
424 /* hnum != 0 already checked. */
425 for (i = 4 - 1; i >= 0; i--)
426 {
427 work = num[i] + carry * BASE;
428 quo[i] = work / lden;
429 carry = work % lden;
430 }
431 }
432 else
433 {
434 /* Full double precision division,
435 with thanks to Don Knuth's "Seminumerical Algorithms". */
436 int num_hi_sig, den_hi_sig;
437 unsigned HOST_WIDE_INT quo_est, scale;
438
439 /* Find the highest nonzero divisor digit. */
440 for (i = 4 - 1;; i--)
441 if (den[i] != 0)
442 {
443 den_hi_sig = i;
444 break;
445 }
446
447 /* Insure that the first digit of the divisor is at least BASE/2.
448 This is required by the quotient digit estimation algorithm. */
449
450 scale = BASE / (den[den_hi_sig] + 1);
451 if (scale > 1)
452 { /* scale divisor and dividend */
453 carry = 0;
454 for (i = 0; i <= 4 - 1; i++)
455 {
456 work = (num[i] * scale) + carry;
457 num[i] = LOWPART (work);
458 carry = HIGHPART (work);
459 }
460
461 num[4] = carry;
462 carry = 0;
463 for (i = 0; i <= 4 - 1; i++)
464 {
465 work = (den[i] * scale) + carry;
466 den[i] = LOWPART (work);
467 carry = HIGHPART (work);
468 if (den[i] != 0) den_hi_sig = i;
469 }
470 }
471
472 num_hi_sig = 4;
473
474 /* Main loop */
475 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
476 {
477 /* Guess the next quotient digit, quo_est, by dividing the first
478 two remaining dividend digits by the high order quotient digit.
479 quo_est is never low and is at most 2 high. */
480 unsigned HOST_WIDE_INT tmp;
481
482 num_hi_sig = i + den_hi_sig + 1;
483 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
484 if (num[num_hi_sig] != den[den_hi_sig])
485 quo_est = work / den[den_hi_sig];
486 else
487 quo_est = BASE - 1;
488
489 /* Refine quo_est so it's usually correct, and at most one high. */
490 tmp = work - quo_est * den[den_hi_sig];
491 if (tmp < BASE
492 && (den[den_hi_sig - 1] * quo_est
493 > (tmp * BASE + num[num_hi_sig - 2])))
494 quo_est--;
495
496 /* Try QUO_EST as the quotient digit, by multiplying the
497 divisor by QUO_EST and subtracting from the remaining dividend.
498 Keep in mind that QUO_EST is the I - 1st digit. */
499
500 carry = 0;
501 for (j = 0; j <= den_hi_sig; j++)
502 {
503 work = quo_est * den[j] + carry;
504 carry = HIGHPART (work);
505 work = num[i + j] - LOWPART (work);
506 num[i + j] = LOWPART (work);
507 carry += HIGHPART (work) != 0;
508 }
509
510 /* If quo_est was high by one, then num[i] went negative and
511 we need to correct things. */
512 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
513 {
514 quo_est--;
515 carry = 0; /* add divisor back in */
516 for (j = 0; j <= den_hi_sig; j++)
517 {
518 work = num[i + j] + den[j] + carry;
519 carry = HIGHPART (work);
520 num[i + j] = LOWPART (work);
521 }
522
523 num [num_hi_sig] += carry;
524 }
525
526 /* Store the quotient digit. */
527 quo[i] = quo_est;
528 }
529 }
530
531 decode (quo, lquo, hquo);
532
533 finish_up:
534 /* If result is negative, make it so. */
535 if (quo_neg)
536 neg_double (*lquo, *hquo, lquo, hquo);
537
538 /* Compute trial remainder: rem = num - (quo * den) */
539 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
540 neg_double (*lrem, *hrem, lrem, hrem);
541 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
542
543 switch (code)
544 {
545 case TRUNC_DIV_EXPR:
546 case TRUNC_MOD_EXPR: /* round toward zero */
547 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
548 return overflow;
549
550 case FLOOR_DIV_EXPR:
551 case FLOOR_MOD_EXPR: /* round toward negative infinity */
552 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
553 {
554 /* quo = quo - 1; */
555 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
556 lquo, hquo);
557 }
558 else
559 return overflow;
560 break;
561
562 case CEIL_DIV_EXPR:
563 case CEIL_MOD_EXPR: /* round toward positive infinity */
564 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
565 {
566 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
567 lquo, hquo);
568 }
569 else
570 return overflow;
571 break;
572
573 case ROUND_DIV_EXPR:
574 case ROUND_MOD_EXPR: /* round to closest integer */
575 {
576 unsigned HOST_WIDE_INT labs_rem = *lrem;
577 HOST_WIDE_INT habs_rem = *hrem;
578 unsigned HOST_WIDE_INT labs_den = lden, lnegabs_rem, ldiff;
579 HOST_WIDE_INT habs_den = hden, hnegabs_rem, hdiff;
580
581 /* Get absolute values. */
582 if (!uns && *hrem < 0)
583 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
584 if (!uns && hden < 0)
585 neg_double (lden, hden, &labs_den, &habs_den);
586
587 /* If abs(rem) >= abs(den) - abs(rem), adjust the quotient. */
588 neg_double (labs_rem, habs_rem, &lnegabs_rem, &hnegabs_rem);
589 add_double (labs_den, habs_den, lnegabs_rem, hnegabs_rem,
590 &ldiff, &hdiff);
591
592 if (((unsigned HOST_WIDE_INT) habs_rem
593 > (unsigned HOST_WIDE_INT) hdiff)
594 || (habs_rem == hdiff && labs_rem >= ldiff))
595 {
596 if (quo_neg)
597 /* quo = quo - 1; */
598 add_double (*lquo, *hquo,
599 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
600 else
601 /* quo = quo + 1; */
602 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
603 lquo, hquo);
604 }
605 else
606 return overflow;
607 }
608 break;
609
610 default:
611 gcc_unreachable ();
612 }
613
614 /* Compute true remainder: rem = num - (quo * den) */
615 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
616 neg_double (*lrem, *hrem, lrem, hrem);
617 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
618 return overflow;
619 }
620
621
622 /* Construct from a buffer of length LEN. BUFFER will be read according
623 to byte endianess and word endianess. Only the lower LEN bytes
624 of the result are set; the remaining high bytes are cleared. */
625
626 double_int
627 double_int::from_buffer (const unsigned char *buffer, int len)
628 {
629 double_int result = double_int_zero;
630 int words = len / UNITS_PER_WORD;
631
632 gcc_assert (len * BITS_PER_UNIT <= HOST_BITS_PER_DOUBLE_INT);
633
634 for (int byte = 0; byte < len; byte++)
635 {
636 int offset;
637 int bitpos = byte * BITS_PER_UNIT;
638 unsigned HOST_WIDE_INT value;
639
640 if (len > UNITS_PER_WORD)
641 {
642 int word = byte / UNITS_PER_WORD;
643
644 if (WORDS_BIG_ENDIAN)
645 word = (words - 1) - word;
646
647 offset = word * UNITS_PER_WORD;
648
649 if (BYTES_BIG_ENDIAN)
650 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
651 else
652 offset += byte % UNITS_PER_WORD;
653 }
654 else
655 offset = BYTES_BIG_ENDIAN ? (len - 1) - byte : byte;
656
657 value = (unsigned HOST_WIDE_INT) buffer[offset];
658
659 if (bitpos < HOST_BITS_PER_WIDE_INT)
660 result.low |= value << bitpos;
661 else
662 result.high |= value << (bitpos - HOST_BITS_PER_WIDE_INT);
663 }
664
665 return result;
666 }
667
668
669 /* Returns mask for PREC bits. */
670
671 double_int
672 double_int::mask (unsigned prec)
673 {
674 unsigned HOST_WIDE_INT m;
675 double_int mask;
676
677 if (prec > HOST_BITS_PER_WIDE_INT)
678 {
679 prec -= HOST_BITS_PER_WIDE_INT;
680 m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
681 mask.high = (HOST_WIDE_INT) m;
682 mask.low = ALL_ONES;
683 }
684 else
685 {
686 mask.high = 0;
687 mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0;
688 }
689
690 return mask;
691 }
692
693 /* Returns a maximum value for signed or unsigned integer
694 of precision PREC. */
695
696 double_int
697 double_int::max_value (unsigned int prec, bool uns)
698 {
699 return double_int::mask (prec - (uns ? 0 : 1));
700 }
701
702 /* Returns a minimum value for signed or unsigned integer
703 of precision PREC. */
704
705 double_int
706 double_int::min_value (unsigned int prec, bool uns)
707 {
708 if (uns)
709 return double_int_zero;
710 return double_int_one.lshift (prec - 1, prec, false);
711 }
712
713 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
714 outside of the precision are set to the sign bit (i.e., the PREC-th one),
715 otherwise they are set to zero.
716
717 This corresponds to returning the value represented by PREC lowermost bits
718 of CST, with the given signedness. */
719
720 double_int
721 double_int::ext (unsigned prec, bool uns) const
722 {
723 if (uns)
724 return this->zext (prec);
725 else
726 return this->sext (prec);
727 }
728
729 /* The same as double_int::ext with UNS = true. */
730
731 double_int
732 double_int::zext (unsigned prec) const
733 {
734 const double_int &cst = *this;
735 double_int mask = double_int::mask (prec);
736 double_int r;
737
738 r.low = cst.low & mask.low;
739 r.high = cst.high & mask.high;
740
741 return r;
742 }
743
744 /* The same as double_int::ext with UNS = false. */
745
746 double_int
747 double_int::sext (unsigned prec) const
748 {
749 const double_int &cst = *this;
750 double_int mask = double_int::mask (prec);
751 double_int r;
752 unsigned HOST_WIDE_INT snum;
753
754 if (prec <= HOST_BITS_PER_WIDE_INT)
755 snum = cst.low;
756 else
757 {
758 prec -= HOST_BITS_PER_WIDE_INT;
759 snum = (unsigned HOST_WIDE_INT) cst.high;
760 }
761 if (((snum >> (prec - 1)) & 1) == 1)
762 {
763 r.low = cst.low | ~mask.low;
764 r.high = cst.high | ~mask.high;
765 }
766 else
767 {
768 r.low = cst.low & mask.low;
769 r.high = cst.high & mask.high;
770 }
771
772 return r;
773 }
774
775 /* Returns true if CST fits in signed HOST_WIDE_INT. */
776
777 bool
778 double_int::fits_shwi () const
779 {
780 const double_int &cst = *this;
781 if (cst.high == 0)
782 return (HOST_WIDE_INT) cst.low >= 0;
783 else if (cst.high == -1)
784 return (HOST_WIDE_INT) cst.low < 0;
785 else
786 return false;
787 }
788
789 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
790 unsigned HOST_WIDE_INT if UNS is true. */
791
792 bool
793 double_int::fits_hwi (bool uns) const
794 {
795 if (uns)
796 return this->fits_uhwi ();
797 else
798 return this->fits_shwi ();
799 }
800
801 /* Returns A * B. */
802
803 double_int
804 double_int::operator * (double_int b) const
805 {
806 const double_int &a = *this;
807 double_int ret;
808 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
809 return ret;
810 }
811
812 /* Multiplies *this with B and returns a reference to *this. */
813
814 double_int &
815 double_int::operator *= (double_int b)
816 {
817 mul_double (low, high, b.low, b.high, &low, &high);
818 return *this;
819 }
820
821 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
822 *OVERFLOW is set to nonzero. */
823
824 double_int
825 double_int::mul_with_sign (double_int b, bool unsigned_p, bool *overflow) const
826 {
827 const double_int &a = *this;
828 double_int ret, tem;
829 *overflow = mul_double_wide_with_sign (a.low, a.high, b.low, b.high,
830 &ret.low, &ret.high,
831 &tem.low, &tem.high, unsigned_p);
832 return ret;
833 }
834
835 double_int
836 double_int::wide_mul_with_sign (double_int b, bool unsigned_p,
837 double_int *higher, bool *overflow) const
838
839 {
840 double_int lower;
841 *overflow = mul_double_wide_with_sign (low, high, b.low, b.high,
842 &lower.low, &lower.high,
843 &higher->low, &higher->high,
844 unsigned_p);
845 return lower;
846 }
847
848 /* Returns A + B. */
849
850 double_int
851 double_int::operator + (double_int b) const
852 {
853 const double_int &a = *this;
854 double_int ret;
855 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
856 return ret;
857 }
858
859 /* Adds B to *this and returns a reference to *this. */
860
861 double_int &
862 double_int::operator += (double_int b)
863 {
864 add_double (low, high, b.low, b.high, &low, &high);
865 return *this;
866 }
867
868
869 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
870 *OVERFLOW is set to nonzero. */
871
872 double_int
873 double_int::add_with_sign (double_int b, bool unsigned_p, bool *overflow) const
874 {
875 const double_int &a = *this;
876 double_int ret;
877 *overflow = add_double_with_sign (a.low, a.high, b.low, b.high,
878 &ret.low, &ret.high, unsigned_p);
879 return ret;
880 }
881
882 /* Returns A - B. */
883
884 double_int
885 double_int::operator - (double_int b) const
886 {
887 const double_int &a = *this;
888 double_int ret;
889 neg_double (b.low, b.high, &b.low, &b.high);
890 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
891 return ret;
892 }
893
894 /* Subtracts B from *this and returns a reference to *this. */
895
896 double_int &
897 double_int::operator -= (double_int b)
898 {
899 neg_double (b.low, b.high, &b.low, &b.high);
900 add_double (low, high, b.low, b.high, &low, &high);
901 return *this;
902 }
903
904
905 /* Returns A - B. If the operation overflows via inconsistent sign bits,
906 *OVERFLOW is set to nonzero. */
907
908 double_int
909 double_int::sub_with_overflow (double_int b, bool *overflow) const
910 {
911 double_int ret;
912 neg_double (b.low, b.high, &ret.low, &ret.high);
913 add_double (low, high, ret.low, ret.high, &ret.low, &ret.high);
914 *overflow = OVERFLOW_SUM_SIGN (ret.high, b.high, high);
915 return ret;
916 }
917
918 /* Returns -A. */
919
920 double_int
921 double_int::operator - () const
922 {
923 const double_int &a = *this;
924 double_int ret;
925 neg_double (a.low, a.high, &ret.low, &ret.high);
926 return ret;
927 }
928
929 double_int
930 double_int::neg_with_overflow (bool *overflow) const
931 {
932 double_int ret;
933 *overflow = neg_double (low, high, &ret.low, &ret.high);
934 return ret;
935 }
936
937 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
938 specified by CODE). CODE is enum tree_code in fact, but double_int.h
939 must be included before tree.h. The remainder after the division is
940 stored to MOD. */
941
942 double_int
943 double_int::divmod_with_overflow (double_int b, bool uns, unsigned code,
944 double_int *mod, bool *overflow) const
945 {
946 const double_int &a = *this;
947 double_int ret;
948
949 *overflow = div_and_round_double (code, uns, a.low, a.high,
950 b.low, b.high, &ret.low, &ret.high,
951 &mod->low, &mod->high);
952 return ret;
953 }
954
955 double_int
956 double_int::divmod (double_int b, bool uns, unsigned code,
957 double_int *mod) const
958 {
959 const double_int &a = *this;
960 double_int ret;
961
962 div_and_round_double (code, uns, a.low, a.high,
963 b.low, b.high, &ret.low, &ret.high,
964 &mod->low, &mod->high);
965 return ret;
966 }
967
968 /* The same as double_int::divmod with UNS = false. */
969
970 double_int
971 double_int::sdivmod (double_int b, unsigned code, double_int *mod) const
972 {
973 return this->divmod (b, false, code, mod);
974 }
975
976 /* The same as double_int::divmod with UNS = true. */
977
978 double_int
979 double_int::udivmod (double_int b, unsigned code, double_int *mod) const
980 {
981 return this->divmod (b, true, code, mod);
982 }
983
984 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
985 specified by CODE). CODE is enum tree_code in fact, but double_int.h
986 must be included before tree.h. */
987
988 double_int
989 double_int::div (double_int b, bool uns, unsigned code) const
990 {
991 double_int mod;
992
993 return this->divmod (b, uns, code, &mod);
994 }
995
996 /* The same as double_int::div with UNS = false. */
997
998 double_int
999 double_int::sdiv (double_int b, unsigned code) const
1000 {
1001 return this->div (b, false, code);
1002 }
1003
1004 /* The same as double_int::div with UNS = true. */
1005
1006 double_int
1007 double_int::udiv (double_int b, unsigned code) const
1008 {
1009 return this->div (b, true, code);
1010 }
1011
1012 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
1013 specified by CODE). CODE is enum tree_code in fact, but double_int.h
1014 must be included before tree.h. */
1015
1016 double_int
1017 double_int::mod (double_int b, bool uns, unsigned code) const
1018 {
1019 double_int mod;
1020
1021 this->divmod (b, uns, code, &mod);
1022 return mod;
1023 }
1024
1025 /* The same as double_int::mod with UNS = false. */
1026
1027 double_int
1028 double_int::smod (double_int b, unsigned code) const
1029 {
1030 return this->mod (b, false, code);
1031 }
1032
1033 /* The same as double_int::mod with UNS = true. */
1034
1035 double_int
1036 double_int::umod (double_int b, unsigned code) const
1037 {
1038 return this->mod (b, true, code);
1039 }
1040
1041 /* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
1042 the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
1043 unchanged. */
1044
1045 bool
1046 double_int::multiple_of (double_int factor,
1047 bool unsigned_p, double_int *multiple) const
1048 {
1049 double_int remainder;
1050 double_int quotient = this->divmod (factor, unsigned_p,
1051 TRUNC_DIV_EXPR, &remainder);
1052 if (remainder.is_zero ())
1053 {
1054 *multiple = quotient;
1055 return true;
1056 }
1057
1058 return false;
1059 }
1060
1061 /* Set BITPOS bit in A. */
1062 double_int
1063 double_int::set_bit (unsigned bitpos) const
1064 {
1065 double_int a = *this;
1066 if (bitpos < HOST_BITS_PER_WIDE_INT)
1067 a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
1068 else
1069 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1070
1071 return a;
1072 }
1073
1074 /* Count trailing zeros in A. */
1075 int
1076 double_int::trailing_zeros () const
1077 {
1078 const double_int &a = *this;
1079 unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
1080 unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
1081 if (!w)
1082 return HOST_BITS_PER_DOUBLE_INT;
1083 bits += ctz_hwi (w);
1084 return bits;
1085 }
1086
1087 /* Shift A left by COUNT places. */
1088
1089 double_int
1090 double_int::lshift (HOST_WIDE_INT count) const
1091 {
1092 double_int ret;
1093
1094 gcc_checking_assert (count >= 0);
1095
1096 if (count >= HOST_BITS_PER_DOUBLE_INT)
1097 {
1098 /* Shifting by the host word size is undefined according to the
1099 ANSI standard, so we must handle this as a special case. */
1100 ret.high = 0;
1101 ret.low = 0;
1102 }
1103 else if (count >= HOST_BITS_PER_WIDE_INT)
1104 {
1105 ret.high = low << (count - HOST_BITS_PER_WIDE_INT);
1106 ret.low = 0;
1107 }
1108 else
1109 {
1110 ret.high = (((unsigned HOST_WIDE_INT) high << count)
1111 | (low >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
1112 ret.low = low << count;
1113 }
1114
1115 return ret;
1116 }
1117
1118 /* Shift A right by COUNT places. */
1119
1120 double_int
1121 double_int::rshift (HOST_WIDE_INT count) const
1122 {
1123 double_int ret;
1124
1125 gcc_checking_assert (count >= 0);
1126
1127 if (count >= HOST_BITS_PER_DOUBLE_INT)
1128 {
1129 /* Shifting by the host word size is undefined according to the
1130 ANSI standard, so we must handle this as a special case. */
1131 ret.high = 0;
1132 ret.low = 0;
1133 }
1134 else if (count >= HOST_BITS_PER_WIDE_INT)
1135 {
1136 ret.high = 0;
1137 ret.low
1138 = (unsigned HOST_WIDE_INT) (high >> (count - HOST_BITS_PER_WIDE_INT));
1139 }
1140 else
1141 {
1142 ret.high = high >> count;
1143 ret.low = ((low >> count)
1144 | ((unsigned HOST_WIDE_INT) high
1145 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
1146 }
1147
1148 return ret;
1149 }
1150
1151 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
1152 right if COUNT is negative. ARITH true specifies arithmetic shifting;
1153 otherwise use logical shift. */
1154
1155 double_int
1156 double_int::lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
1157 {
1158 double_int ret;
1159 if (count > 0)
1160 lshift_double (low, high, count, prec, &ret.low, &ret.high);
1161 else
1162 rshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high, arith);
1163 return ret;
1164 }
1165
1166 /* Shift A right by COUNT places keeping only PREC bits of result. Shift
1167 left if COUNT is negative. ARITH true specifies arithmetic shifting;
1168 otherwise use logical shift. */
1169
1170 double_int
1171 double_int::rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
1172 {
1173 double_int ret;
1174 if (count > 0)
1175 rshift_double (low, high, count, prec, &ret.low, &ret.high, arith);
1176 else
1177 lshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high);
1178 return ret;
1179 }
1180
1181 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
1182 Shift right if COUNT is negative. */
1183
1184 double_int
1185 double_int::alshift (HOST_WIDE_INT count, unsigned int prec) const
1186 {
1187 double_int r;
1188 if (count > 0)
1189 lshift_double (low, high, count, prec, &r.low, &r.high);
1190 else
1191 rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, true);
1192 return r;
1193 }
1194
1195 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
1196 Shift left if COUNT is negative. */
1197
1198 double_int
1199 double_int::arshift (HOST_WIDE_INT count, unsigned int prec) const
1200 {
1201 double_int r;
1202 if (count > 0)
1203 rshift_double (low, high, count, prec, &r.low, &r.high, true);
1204 else
1205 lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
1206 return r;
1207 }
1208
1209 /* Logical shift A left by COUNT places keeping only PREC bits of result.
1210 Shift right if COUNT is negative. */
1211
1212 double_int
1213 double_int::llshift (HOST_WIDE_INT count, unsigned int prec) const
1214 {
1215 double_int r;
1216 if (count > 0)
1217 lshift_double (low, high, count, prec, &r.low, &r.high);
1218 else
1219 rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, false);
1220 return r;
1221 }
1222
1223 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1224 Shift left if COUNT is negative. */
1225
1226 double_int
1227 double_int::lrshift (HOST_WIDE_INT count, unsigned int prec) const
1228 {
1229 double_int r;
1230 if (count > 0)
1231 rshift_double (low, high, count, prec, &r.low, &r.high, false);
1232 else
1233 lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
1234 return r;
1235 }
1236
1237 /* Rotate A left by COUNT places keeping only PREC bits of result.
1238 Rotate right if COUNT is negative. */
1239
1240 double_int
1241 double_int::lrotate (HOST_WIDE_INT count, unsigned int prec) const
1242 {
1243 double_int t1, t2;
1244
1245 count %= prec;
1246 if (count < 0)
1247 count += prec;
1248
1249 t1 = this->llshift (count, prec);
1250 t2 = this->lrshift (prec - count, prec);
1251
1252 return t1 | t2;
1253 }
1254
1255 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1256 Rotate right if COUNT is negative. */
1257
1258 double_int
1259 double_int::rrotate (HOST_WIDE_INT count, unsigned int prec) const
1260 {
1261 double_int t1, t2;
1262
1263 count %= prec;
1264 if (count < 0)
1265 count += prec;
1266
1267 t1 = this->lrshift (count, prec);
1268 t2 = this->llshift (prec - count, prec);
1269
1270 return t1 | t2;
1271 }
1272
1273 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1274 comparison is given by UNS. */
1275
1276 int
1277 double_int::cmp (double_int b, bool uns) const
1278 {
1279 if (uns)
1280 return this->ucmp (b);
1281 else
1282 return this->scmp (b);
1283 }
1284
1285 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
1286 and 1 if A > B. */
1287
1288 int
1289 double_int::ucmp (double_int b) const
1290 {
1291 const double_int &a = *this;
1292 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
1293 return -1;
1294 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
1295 return 1;
1296 if (a.low < b.low)
1297 return -1;
1298 if (a.low > b.low)
1299 return 1;
1300
1301 return 0;
1302 }
1303
1304 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1305 and 1 if A > B. */
1306
1307 int
1308 double_int::scmp (double_int b) const
1309 {
1310 const double_int &a = *this;
1311 if (a.high < b.high)
1312 return -1;
1313 if (a.high > b.high)
1314 return 1;
1315 if (a.low < b.low)
1316 return -1;
1317 if (a.low > b.low)
1318 return 1;
1319
1320 return 0;
1321 }
1322
1323 /* Compares two unsigned values A and B for less-than. */
1324
1325 bool
1326 double_int::ult (double_int b) const
1327 {
1328 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1329 return true;
1330 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1331 return false;
1332 if (low < b.low)
1333 return true;
1334 return false;
1335 }
1336
1337 /* Compares two unsigned values A and B for less-than or equal-to. */
1338
1339 bool
1340 double_int::ule (double_int b) const
1341 {
1342 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1343 return true;
1344 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1345 return false;
1346 if (low <= b.low)
1347 return true;
1348 return false;
1349 }
1350
1351 /* Compares two unsigned values A and B for greater-than. */
1352
1353 bool
1354 double_int::ugt (double_int b) const
1355 {
1356 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1357 return true;
1358 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1359 return false;
1360 if (low > b.low)
1361 return true;
1362 return false;
1363 }
1364
1365 /* Compares two signed values A and B for less-than. */
1366
1367 bool
1368 double_int::slt (double_int b) const
1369 {
1370 if (high < b.high)
1371 return true;
1372 if (high > b.high)
1373 return false;
1374 if (low < b.low)
1375 return true;
1376 return false;
1377 }
1378
1379 /* Compares two signed values A and B for less-than or equal-to. */
1380
1381 bool
1382 double_int::sle (double_int b) const
1383 {
1384 if (high < b.high)
1385 return true;
1386 if (high > b.high)
1387 return false;
1388 if (low <= b.low)
1389 return true;
1390 return false;
1391 }
1392
1393 /* Compares two signed values A and B for greater-than. */
1394
1395 bool
1396 double_int::sgt (double_int b) const
1397 {
1398 if (high > b.high)
1399 return true;
1400 if (high < b.high)
1401 return false;
1402 if (low > b.low)
1403 return true;
1404 return false;
1405 }
1406
1407
1408 /* Compares two values A and B. Returns max value. Signedness of the
1409 comparison is given by UNS. */
1410
1411 double_int
1412 double_int::max (double_int b, bool uns)
1413 {
1414 return (this->cmp (b, uns) == 1) ? *this : b;
1415 }
1416
1417 /* Compares two signed values A and B. Returns max value. */
1418
1419 double_int
1420 double_int::smax (double_int b)
1421 {
1422 return (this->scmp (b) == 1) ? *this : b;
1423 }
1424
1425 /* Compares two unsigned values A and B. Returns max value. */
1426
1427 double_int
1428 double_int::umax (double_int b)
1429 {
1430 return (this->ucmp (b) == 1) ? *this : b;
1431 }
1432
1433 /* Compares two values A and B. Returns mix value. Signedness of the
1434 comparison is given by UNS. */
1435
1436 double_int
1437 double_int::min (double_int b, bool uns)
1438 {
1439 return (this->cmp (b, uns) == -1) ? *this : b;
1440 }
1441
1442 /* Compares two signed values A and B. Returns min value. */
1443
1444 double_int
1445 double_int::smin (double_int b)
1446 {
1447 return (this->scmp (b) == -1) ? *this : b;
1448 }
1449
1450 /* Compares two unsigned values A and B. Returns min value. */
1451
1452 double_int
1453 double_int::umin (double_int b)
1454 {
1455 return (this->ucmp (b) == -1) ? *this : b;
1456 }
1457
1458 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1459
1460 static unsigned
1461 double_int_split_digit (double_int *cst, unsigned base)
1462 {
1463 unsigned HOST_WIDE_INT resl, reml;
1464 HOST_WIDE_INT resh, remh;
1465
1466 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
1467 &resl, &resh, &reml, &remh);
1468 cst->high = resh;
1469 cst->low = resl;
1470
1471 return reml;
1472 }
1473
1474 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1475 otherwise it is signed. */
1476
1477 void
1478 dump_double_int (FILE *file, double_int cst, bool uns)
1479 {
1480 unsigned digits[100], n;
1481 int i;
1482
1483 if (cst.is_zero ())
1484 {
1485 fprintf (file, "0");
1486 return;
1487 }
1488
1489 if (!uns && cst.is_negative ())
1490 {
1491 fprintf (file, "-");
1492 cst = -cst;
1493 }
1494
1495 for (n = 0; !cst.is_zero (); n++)
1496 digits[n] = double_int_split_digit (&cst, 10);
1497 for (i = n - 1; i >= 0; i--)
1498 fprintf (file, "%u", digits[i]);
1499 }
1500
1501
1502 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1503 otherwise. */
1504
1505 void
1506 mpz_set_double_int (mpz_t result, double_int val, bool uns)
1507 {
1508 bool negate = false;
1509 unsigned HOST_WIDE_INT vp[2];
1510
1511 if (!uns && val.is_negative ())
1512 {
1513 negate = true;
1514 val = -val;
1515 }
1516
1517 vp[0] = val.low;
1518 vp[1] = (unsigned HOST_WIDE_INT) val.high;
1519 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
1520
1521 if (negate)
1522 mpz_neg (result, result);
1523 }
1524
1525 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1526 values of VAL will be wrapped; otherwise, they will be set to the
1527 appropriate minimum or maximum TYPE bound. */
1528
1529 double_int
1530 mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
1531 {
1532 unsigned HOST_WIDE_INT *vp;
1533 size_t count, numb;
1534 double_int res;
1535
1536 if (!wrap)
1537 {
1538 mpz_t min, max;
1539
1540 mpz_init (min);
1541 mpz_init (max);
1542 get_type_static_bounds (type, min, max);
1543
1544 if (mpz_cmp (val, min) < 0)
1545 mpz_set (val, min);
1546 else if (mpz_cmp (val, max) > 0)
1547 mpz_set (val, max);
1548
1549 mpz_clear (min);
1550 mpz_clear (max);
1551 }
1552
1553 /* Determine the number of unsigned HOST_WIDE_INT that are required
1554 for representing the value. The code to calculate count is
1555 extracted from the GMP manual, section "Integer Import and Export":
1556 http://gmplib.org/manual/Integer-Import-and-Export.html */
1557 numb = 8 * sizeof (HOST_WIDE_INT);
1558 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
1559 if (count < 2)
1560 count = 2;
1561 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof (HOST_WIDE_INT));
1562
1563 vp[0] = 0;
1564 vp[1] = 0;
1565 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
1566
1567 gcc_assert (wrap || count <= 2);
1568
1569 res.low = vp[0];
1570 res.high = (HOST_WIDE_INT) vp[1];
1571
1572 res = res.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
1573 if (mpz_sgn (val) < 0)
1574 res = -res;
1575
1576 return res;
1577 }