]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/double-int.c
alloc-pool.c, [...]: Add missing whitespace before "(".
[thirdparty/gcc.git] / gcc / double-int.c
1 /* Operations with long integers.
2 Copyright (C) 2006-2013 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 SHIFT_COUNT_TRUNCATED. */
24 #include "tree.h"
25
26 static int add_double_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
27 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
28 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
29 bool);
30
31 #define add_double(l1,h1,l2,h2,lv,hv) \
32 add_double_with_sign (l1, h1, l2, h2, lv, hv, false)
33
34 static int neg_double (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
35 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
36
37 static int mul_double_wide_with_sign (unsigned HOST_WIDE_INT, HOST_WIDE_INT,
38 unsigned HOST_WIDE_INT, HOST_WIDE_INT,
39 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
40 unsigned HOST_WIDE_INT *, HOST_WIDE_INT *,
41 bool);
42
43 #define mul_double(l1,h1,l2,h2,lv,hv) \
44 mul_double_wide_with_sign (l1, h1, l2, h2, lv, hv, NULL, NULL, false)
45
46 static int div_and_round_double (unsigned, int, unsigned HOST_WIDE_INT,
47 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
48 HOST_WIDE_INT, unsigned HOST_WIDE_INT *,
49 HOST_WIDE_INT *, unsigned HOST_WIDE_INT *,
50 HOST_WIDE_INT *);
51
52 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
53 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
54 and SUM1. Then this yields nonzero if overflow occurred during the
55 addition.
56
57 Overflow occurs if A and B have the same sign, but A and SUM differ in
58 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
59 sign. */
60 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
61
62 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
63 We do that by representing the two-word integer in 4 words, with only
64 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
65 number. The value of the word is LOWPART + HIGHPART * BASE. */
66
67 #define LOWPART(x) \
68 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
69 #define HIGHPART(x) \
70 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
71 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
72
73 /* Unpack a two-word integer into 4 words.
74 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
75 WORDS points to the array of HOST_WIDE_INTs. */
76
77 static void
78 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
79 {
80 words[0] = LOWPART (low);
81 words[1] = HIGHPART (low);
82 words[2] = LOWPART (hi);
83 words[3] = HIGHPART (hi);
84 }
85
86 /* Pack an array of 4 words into a two-word integer.
87 WORDS points to the array of words.
88 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
89
90 static void
91 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
92 HOST_WIDE_INT *hi)
93 {
94 *low = words[0] + words[1] * BASE;
95 *hi = words[2] + words[3] * BASE;
96 }
97
98 /* Add two doubleword integers with doubleword result.
99 Return nonzero if the operation overflows according to UNSIGNED_P.
100 Each argument is given as two `HOST_WIDE_INT' pieces.
101 One argument is L1 and H1; the other, L2 and H2.
102 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
103
104 static int
105 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
106 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
107 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
108 bool unsigned_p)
109 {
110 unsigned HOST_WIDE_INT l;
111 HOST_WIDE_INT h;
112
113 l = l1 + l2;
114 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
115 + (unsigned HOST_WIDE_INT) h2
116 + (l < l1));
117
118 *lv = l;
119 *hv = h;
120
121 if (unsigned_p)
122 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
123 || (h == h1
124 && l < l1));
125 else
126 return OVERFLOW_SUM_SIGN (h1, h2, h);
127 }
128
129 /* Negate a doubleword integer with doubleword result.
130 Return nonzero if the operation overflows, assuming it's signed.
131 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
132 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
133
134 static int
135 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
136 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
137 {
138 if (l1 == 0)
139 {
140 *lv = 0;
141 *hv = - h1;
142 return (*hv & h1) < 0;
143 }
144 else
145 {
146 *lv = -l1;
147 *hv = ~h1;
148 return 0;
149 }
150 }
151
152 /* Multiply two doubleword integers with quadword result.
153 Return nonzero if the operation overflows according to UNSIGNED_P.
154 Each argument is given as two `HOST_WIDE_INT' pieces.
155 One argument is L1 and H1; the other, L2 and H2.
156 The value is stored as four `HOST_WIDE_INT' pieces in *LV and *HV,
157 *LW and *HW.
158 If lw is NULL then only the low part and no overflow is computed. */
159
160 static int
161 mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
162 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
163 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
164 unsigned HOST_WIDE_INT *lw, HOST_WIDE_INT *hw,
165 bool unsigned_p)
166 {
167 HOST_WIDE_INT arg1[4];
168 HOST_WIDE_INT arg2[4];
169 HOST_WIDE_INT prod[4 * 2];
170 unsigned HOST_WIDE_INT carry;
171 int i, j, k;
172 unsigned HOST_WIDE_INT neglow;
173 HOST_WIDE_INT neghigh;
174
175 encode (arg1, l1, h1);
176 encode (arg2, l2, h2);
177
178 memset (prod, 0, sizeof prod);
179
180 for (i = 0; i < 4; i++)
181 {
182 carry = 0;
183 for (j = 0; j < 4; j++)
184 {
185 k = i + j;
186 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
187 carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j];
188 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
189 carry += prod[k];
190 prod[k] = LOWPART (carry);
191 carry = HIGHPART (carry);
192 }
193 prod[i + 4] = carry;
194 }
195
196 decode (prod, lv, hv);
197
198 /* We are not interested in the wide part nor in overflow. */
199 if (lw == NULL)
200 return 0;
201
202 decode (prod + 4, lw, hw);
203
204 /* Unsigned overflow is immediate. */
205 if (unsigned_p)
206 return (*lw | *hw) != 0;
207
208 /* Check for signed overflow by calculating the signed representation of the
209 top half of the result; it should agree with the low half's sign bit. */
210 if (h1 < 0)
211 {
212 neg_double (l2, h2, &neglow, &neghigh);
213 add_double (neglow, neghigh, *lw, *hw, lw, hw);
214 }
215 if (h2 < 0)
216 {
217 neg_double (l1, h1, &neglow, &neghigh);
218 add_double (neglow, neghigh, *lw, *hw, lw, hw);
219 }
220 return (*hv < 0 ? ~(*lw & *hw) : *lw | *hw) != 0;
221 }
222
223 /* Shift the doubleword integer in L1, H1 right by COUNT places
224 keeping only PREC bits of result. ARITH nonzero specifies
225 arithmetic shifting; otherwise use logical shift.
226 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
227
228 static void
229 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
230 unsigned HOST_WIDE_INT count, unsigned int prec,
231 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
232 bool arith)
233 {
234 unsigned HOST_WIDE_INT signmask;
235
236 signmask = (arith
237 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
238 : 0);
239
240 if (SHIFT_COUNT_TRUNCATED)
241 count %= prec;
242
243 if (count >= HOST_BITS_PER_DOUBLE_INT)
244 {
245 /* Shifting by the host word size is undefined according to the
246 ANSI standard, so we must handle this as a special case. */
247 *hv = 0;
248 *lv = 0;
249 }
250 else if (count >= HOST_BITS_PER_WIDE_INT)
251 {
252 *hv = 0;
253 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
254 }
255 else
256 {
257 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
258 *lv = ((l1 >> count)
259 | ((unsigned HOST_WIDE_INT) h1
260 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
261 }
262
263 /* Zero / sign extend all bits that are beyond the precision. */
264
265 if (count >= prec)
266 {
267 *hv = signmask;
268 *lv = signmask;
269 }
270 else if ((prec - count) >= HOST_BITS_PER_DOUBLE_INT)
271 ;
272 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
273 {
274 *hv &= ~(HOST_WIDE_INT_M1U << (prec - count - HOST_BITS_PER_WIDE_INT));
275 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
276 }
277 else
278 {
279 *hv = signmask;
280 *lv &= ~(HOST_WIDE_INT_M1U << (prec - count));
281 *lv |= signmask << (prec - count);
282 }
283 }
284
285 /* Shift the doubleword integer in L1, H1 left by COUNT places
286 keeping only PREC bits of result.
287 Shift right if COUNT is negative.
288 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
289 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
290
291 static void
292 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
293 unsigned HOST_WIDE_INT count, unsigned int prec,
294 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
295 {
296 unsigned HOST_WIDE_INT signmask;
297
298 if (SHIFT_COUNT_TRUNCATED)
299 count %= prec;
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, ltwice;
579 HOST_WIDE_INT habs_den = hden, htwice;
580
581 /* Get absolute values. */
582 if (*hrem < 0)
583 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
584 if (hden < 0)
585 neg_double (lden, hden, &labs_den, &habs_den);
586
587 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
588 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
589 labs_rem, habs_rem, &ltwice, &htwice);
590
591 if (((unsigned HOST_WIDE_INT) habs_den
592 < (unsigned HOST_WIDE_INT) htwice)
593 || (((unsigned HOST_WIDE_INT) habs_den
594 == (unsigned HOST_WIDE_INT) htwice)
595 && (labs_den <= ltwice)))
596 {
597 if (*hquo < 0)
598 /* quo = quo - 1; */
599 add_double (*lquo, *hquo,
600 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
601 else
602 /* quo = quo + 1; */
603 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
604 lquo, hquo);
605 }
606 else
607 return overflow;
608 }
609 break;
610
611 default:
612 gcc_unreachable ();
613 }
614
615 /* Compute true remainder: rem = num - (quo * den) */
616 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
617 neg_double (*lrem, *hrem, lrem, hrem);
618 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
619 return overflow;
620 }
621
622
623 /* Construct from a buffer of length LEN. BUFFER will be read according
624 to byte endianess and word endianess. Only the lower LEN bytes
625 of the result are set; the remaining high bytes are cleared. */
626
627 double_int
628 double_int::from_buffer (const unsigned char *buffer, int len)
629 {
630 double_int result = double_int_zero;
631 int words = len / UNITS_PER_WORD;
632
633 gcc_assert (len * BITS_PER_UNIT <= HOST_BITS_PER_DOUBLE_INT);
634
635 for (int byte = 0; byte < len; byte++)
636 {
637 int offset;
638 int bitpos = byte * BITS_PER_UNIT;
639 unsigned HOST_WIDE_INT value;
640
641 if (len > UNITS_PER_WORD)
642 {
643 int word = byte / UNITS_PER_WORD;
644
645 if (WORDS_BIG_ENDIAN)
646 word = (words - 1) - word;
647
648 offset = word * UNITS_PER_WORD;
649
650 if (BYTES_BIG_ENDIAN)
651 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
652 else
653 offset += byte % UNITS_PER_WORD;
654 }
655 else
656 offset = BYTES_BIG_ENDIAN ? (len - 1) - byte : byte;
657
658 value = (unsigned HOST_WIDE_INT) buffer[offset];
659
660 if (bitpos < HOST_BITS_PER_WIDE_INT)
661 result.low |= value << bitpos;
662 else
663 result.high |= value << (bitpos - HOST_BITS_PER_WIDE_INT);
664 }
665
666 return result;
667 }
668
669
670 /* Returns mask for PREC bits. */
671
672 double_int
673 double_int::mask (unsigned prec)
674 {
675 unsigned HOST_WIDE_INT m;
676 double_int mask;
677
678 if (prec > HOST_BITS_PER_WIDE_INT)
679 {
680 prec -= HOST_BITS_PER_WIDE_INT;
681 m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
682 mask.high = (HOST_WIDE_INT) m;
683 mask.low = ALL_ONES;
684 }
685 else
686 {
687 mask.high = 0;
688 mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0;
689 }
690
691 return mask;
692 }
693
694 /* Returns a maximum value for signed or unsigned integer
695 of precision PREC. */
696
697 double_int
698 double_int::max_value (unsigned int prec, bool uns)
699 {
700 return double_int::mask (prec - (uns ? 0 : 1));
701 }
702
703 /* Returns a minimum value for signed or unsigned integer
704 of precision PREC. */
705
706 double_int
707 double_int::min_value (unsigned int prec, bool uns)
708 {
709 if (uns)
710 return double_int_zero;
711 return double_int_one.lshift (prec - 1, prec, false);
712 }
713
714 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
715 outside of the precision are set to the sign bit (i.e., the PREC-th one),
716 otherwise they are set to zero.
717
718 This corresponds to returning the value represented by PREC lowermost bits
719 of CST, with the given signedness. */
720
721 double_int
722 double_int::ext (unsigned prec, bool uns) const
723 {
724 if (uns)
725 return this->zext (prec);
726 else
727 return this->sext (prec);
728 }
729
730 /* The same as double_int::ext with UNS = true. */
731
732 double_int
733 double_int::zext (unsigned prec) const
734 {
735 const double_int &cst = *this;
736 double_int mask = double_int::mask (prec);
737 double_int r;
738
739 r.low = cst.low & mask.low;
740 r.high = cst.high & mask.high;
741
742 return r;
743 }
744
745 /* The same as double_int::ext with UNS = false. */
746
747 double_int
748 double_int::sext (unsigned prec) const
749 {
750 const double_int &cst = *this;
751 double_int mask = double_int::mask (prec);
752 double_int r;
753 unsigned HOST_WIDE_INT snum;
754
755 if (prec <= HOST_BITS_PER_WIDE_INT)
756 snum = cst.low;
757 else
758 {
759 prec -= HOST_BITS_PER_WIDE_INT;
760 snum = (unsigned HOST_WIDE_INT) cst.high;
761 }
762 if (((snum >> (prec - 1)) & 1) == 1)
763 {
764 r.low = cst.low | ~mask.low;
765 r.high = cst.high | ~mask.high;
766 }
767 else
768 {
769 r.low = cst.low & mask.low;
770 r.high = cst.high & mask.high;
771 }
772
773 return r;
774 }
775
776 /* Returns true if CST fits in signed HOST_WIDE_INT. */
777
778 bool
779 double_int::fits_shwi () const
780 {
781 const double_int &cst = *this;
782 if (cst.high == 0)
783 return (HOST_WIDE_INT) cst.low >= 0;
784 else if (cst.high == -1)
785 return (HOST_WIDE_INT) cst.low < 0;
786 else
787 return false;
788 }
789
790 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
791 unsigned HOST_WIDE_INT if UNS is true. */
792
793 bool
794 double_int::fits_hwi (bool uns) const
795 {
796 if (uns)
797 return this->fits_uhwi ();
798 else
799 return this->fits_shwi ();
800 }
801
802 /* Returns A * B. */
803
804 double_int
805 double_int::operator * (double_int b) const
806 {
807 const double_int &a = *this;
808 double_int ret;
809 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
810 return ret;
811 }
812
813 /* Multiplies *this with B and returns a reference to *this. */
814
815 double_int &
816 double_int::operator *= (double_int b)
817 {
818 mul_double (low, high, b.low, b.high, &low, &high);
819 return *this;
820 }
821
822 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
823 *OVERFLOW is set to nonzero. */
824
825 double_int
826 double_int::mul_with_sign (double_int b, bool unsigned_p, bool *overflow) const
827 {
828 const double_int &a = *this;
829 double_int ret, tem;
830 *overflow = mul_double_wide_with_sign (a.low, a.high, b.low, b.high,
831 &ret.low, &ret.high,
832 &tem.low, &tem.high, unsigned_p);
833 return ret;
834 }
835
836 double_int
837 double_int::wide_mul_with_sign (double_int b, bool unsigned_p,
838 double_int *higher, bool *overflow) const
839
840 {
841 double_int lower;
842 *overflow = mul_double_wide_with_sign (low, high, b.low, b.high,
843 &lower.low, &lower.high,
844 &higher->low, &higher->high,
845 unsigned_p);
846 return lower;
847 }
848
849 /* Returns A + B. */
850
851 double_int
852 double_int::operator + (double_int b) const
853 {
854 const double_int &a = *this;
855 double_int ret;
856 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
857 return ret;
858 }
859
860 /* Adds B to *this and returns a reference to *this. */
861
862 double_int &
863 double_int::operator += (double_int b)
864 {
865 add_double (low, high, b.low, b.high, &low, &high);
866 return *this;
867 }
868
869
870 /* Returns A + B. If the operation overflows according to UNSIGNED_P,
871 *OVERFLOW is set to nonzero. */
872
873 double_int
874 double_int::add_with_sign (double_int b, bool unsigned_p, bool *overflow) const
875 {
876 const double_int &a = *this;
877 double_int ret;
878 *overflow = add_double_with_sign (a.low, a.high, b.low, b.high,
879 &ret.low, &ret.high, unsigned_p);
880 return ret;
881 }
882
883 /* Returns A - B. */
884
885 double_int
886 double_int::operator - (double_int b) const
887 {
888 const double_int &a = *this;
889 double_int ret;
890 neg_double (b.low, b.high, &b.low, &b.high);
891 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
892 return ret;
893 }
894
895 /* Subtracts B from *this and returns a reference to *this. */
896
897 double_int &
898 double_int::operator -= (double_int b)
899 {
900 neg_double (b.low, b.high, &b.low, &b.high);
901 add_double (low, high, b.low, b.high, &low, &high);
902 return *this;
903 }
904
905
906 /* Returns A - B. If the operation overflows via inconsistent sign bits,
907 *OVERFLOW is set to nonzero. */
908
909 double_int
910 double_int::sub_with_overflow (double_int b, bool *overflow) const
911 {
912 double_int ret;
913 neg_double (b.low, b.high, &ret.low, &ret.high);
914 add_double (low, high, ret.low, ret.high, &ret.low, &ret.high);
915 *overflow = OVERFLOW_SUM_SIGN (ret.high, b.high, high);
916 return ret;
917 }
918
919 /* Returns -A. */
920
921 double_int
922 double_int::operator - () const
923 {
924 const double_int &a = *this;
925 double_int ret;
926 neg_double (a.low, a.high, &ret.low, &ret.high);
927 return ret;
928 }
929
930 double_int
931 double_int::neg_with_overflow (bool *overflow) const
932 {
933 double_int ret;
934 *overflow = neg_double (low, high, &ret.low, &ret.high);
935 return ret;
936 }
937
938 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
939 specified by CODE). CODE is enum tree_code in fact, but double_int.h
940 must be included before tree.h. The remainder after the division is
941 stored to MOD. */
942
943 double_int
944 double_int::divmod_with_overflow (double_int b, bool uns, unsigned code,
945 double_int *mod, bool *overflow) const
946 {
947 const double_int &a = *this;
948 double_int ret;
949
950 *overflow = div_and_round_double (code, uns, a.low, a.high,
951 b.low, b.high, &ret.low, &ret.high,
952 &mod->low, &mod->high);
953 return ret;
954 }
955
956 double_int
957 double_int::divmod (double_int b, bool uns, unsigned code,
958 double_int *mod) const
959 {
960 const double_int &a = *this;
961 double_int ret;
962
963 div_and_round_double (code, uns, a.low, a.high,
964 b.low, b.high, &ret.low, &ret.high,
965 &mod->low, &mod->high);
966 return ret;
967 }
968
969 /* The same as double_int::divmod with UNS = false. */
970
971 double_int
972 double_int::sdivmod (double_int b, unsigned code, double_int *mod) const
973 {
974 return this->divmod (b, false, code, mod);
975 }
976
977 /* The same as double_int::divmod with UNS = true. */
978
979 double_int
980 double_int::udivmod (double_int b, unsigned code, double_int *mod) const
981 {
982 return this->divmod (b, true, code, mod);
983 }
984
985 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
986 specified by CODE). CODE is enum tree_code in fact, but double_int.h
987 must be included before tree.h. */
988
989 double_int
990 double_int::div (double_int b, bool uns, unsigned code) const
991 {
992 double_int mod;
993
994 return this->divmod (b, uns, code, &mod);
995 }
996
997 /* The same as double_int::div with UNS = false. */
998
999 double_int
1000 double_int::sdiv (double_int b, unsigned code) const
1001 {
1002 return this->div (b, false, code);
1003 }
1004
1005 /* The same as double_int::div with UNS = true. */
1006
1007 double_int
1008 double_int::udiv (double_int b, unsigned code) const
1009 {
1010 return this->div (b, true, code);
1011 }
1012
1013 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
1014 specified by CODE). CODE is enum tree_code in fact, but double_int.h
1015 must be included before tree.h. */
1016
1017 double_int
1018 double_int::mod (double_int b, bool uns, unsigned code) const
1019 {
1020 double_int mod;
1021
1022 this->divmod (b, uns, code, &mod);
1023 return mod;
1024 }
1025
1026 /* The same as double_int::mod with UNS = false. */
1027
1028 double_int
1029 double_int::smod (double_int b, unsigned code) const
1030 {
1031 return this->mod (b, false, code);
1032 }
1033
1034 /* The same as double_int::mod with UNS = true. */
1035
1036 double_int
1037 double_int::umod (double_int b, unsigned code) const
1038 {
1039 return this->mod (b, true, code);
1040 }
1041
1042 /* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
1043 the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
1044 unchanged. */
1045
1046 bool
1047 double_int::multiple_of (double_int factor,
1048 bool unsigned_p, double_int *multiple) const
1049 {
1050 double_int remainder;
1051 double_int quotient = this->divmod (factor, unsigned_p,
1052 TRUNC_DIV_EXPR, &remainder);
1053 if (remainder.is_zero ())
1054 {
1055 *multiple = quotient;
1056 return true;
1057 }
1058
1059 return false;
1060 }
1061
1062 /* Set BITPOS bit in A. */
1063 double_int
1064 double_int::set_bit (unsigned bitpos) const
1065 {
1066 double_int a = *this;
1067 if (bitpos < HOST_BITS_PER_WIDE_INT)
1068 a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
1069 else
1070 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1071
1072 return a;
1073 }
1074
1075 /* Count trailing zeros in A. */
1076 int
1077 double_int::trailing_zeros () const
1078 {
1079 const double_int &a = *this;
1080 unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
1081 unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
1082 if (!w)
1083 return HOST_BITS_PER_DOUBLE_INT;
1084 bits += ctz_hwi (w);
1085 return bits;
1086 }
1087
1088 /* Shift A left by COUNT places. */
1089
1090 double_int
1091 double_int::lshift (HOST_WIDE_INT count) const
1092 {
1093 double_int ret;
1094
1095 gcc_checking_assert (count >= 0);
1096
1097 if (count >= HOST_BITS_PER_DOUBLE_INT)
1098 {
1099 /* Shifting by the host word size is undefined according to the
1100 ANSI standard, so we must handle this as a special case. */
1101 ret.high = 0;
1102 ret.low = 0;
1103 }
1104 else if (count >= HOST_BITS_PER_WIDE_INT)
1105 {
1106 ret.high = low << (count - HOST_BITS_PER_WIDE_INT);
1107 ret.low = 0;
1108 }
1109 else
1110 {
1111 ret.high = (((unsigned HOST_WIDE_INT) high << count)
1112 | (low >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
1113 ret.low = low << count;
1114 }
1115
1116 return ret;
1117 }
1118
1119 /* Shift A right by COUNT places. */
1120
1121 double_int
1122 double_int::rshift (HOST_WIDE_INT count) const
1123 {
1124 double_int ret;
1125
1126 gcc_checking_assert (count >= 0);
1127
1128 if (count >= HOST_BITS_PER_DOUBLE_INT)
1129 {
1130 /* Shifting by the host word size is undefined according to the
1131 ANSI standard, so we must handle this as a special case. */
1132 ret.high = 0;
1133 ret.low = 0;
1134 }
1135 else if (count >= HOST_BITS_PER_WIDE_INT)
1136 {
1137 ret.high = 0;
1138 ret.low
1139 = (unsigned HOST_WIDE_INT) (high >> (count - HOST_BITS_PER_WIDE_INT));
1140 }
1141 else
1142 {
1143 ret.high = high >> count;
1144 ret.low = ((low >> count)
1145 | ((unsigned HOST_WIDE_INT) high
1146 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
1147 }
1148
1149 return ret;
1150 }
1151
1152 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
1153 right if COUNT is negative. ARITH true specifies arithmetic shifting;
1154 otherwise use logical shift. */
1155
1156 double_int
1157 double_int::lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
1158 {
1159 double_int ret;
1160 if (count > 0)
1161 lshift_double (low, high, count, prec, &ret.low, &ret.high);
1162 else
1163 rshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high, arith);
1164 return ret;
1165 }
1166
1167 /* Shift A right by COUNT places keeping only PREC bits of result. Shift
1168 left if COUNT is negative. ARITH true specifies arithmetic shifting;
1169 otherwise use logical shift. */
1170
1171 double_int
1172 double_int::rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
1173 {
1174 double_int ret;
1175 if (count > 0)
1176 rshift_double (low, high, count, prec, &ret.low, &ret.high, arith);
1177 else
1178 lshift_double (low, high, absu_hwi (count), prec, &ret.low, &ret.high);
1179 return ret;
1180 }
1181
1182 /* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
1183 Shift right if COUNT is negative. */
1184
1185 double_int
1186 double_int::alshift (HOST_WIDE_INT count, unsigned int prec) const
1187 {
1188 double_int r;
1189 if (count > 0)
1190 lshift_double (low, high, count, prec, &r.low, &r.high);
1191 else
1192 rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, true);
1193 return r;
1194 }
1195
1196 /* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
1197 Shift left if COUNT is negative. */
1198
1199 double_int
1200 double_int::arshift (HOST_WIDE_INT count, unsigned int prec) const
1201 {
1202 double_int r;
1203 if (count > 0)
1204 rshift_double (low, high, count, prec, &r.low, &r.high, true);
1205 else
1206 lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
1207 return r;
1208 }
1209
1210 /* Logical shift A left by COUNT places keeping only PREC bits of result.
1211 Shift right if COUNT is negative. */
1212
1213 double_int
1214 double_int::llshift (HOST_WIDE_INT count, unsigned int prec) const
1215 {
1216 double_int r;
1217 if (count > 0)
1218 lshift_double (low, high, count, prec, &r.low, &r.high);
1219 else
1220 rshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high, false);
1221 return r;
1222 }
1223
1224 /* Logical shift A right by COUNT places keeping only PREC bits of result.
1225 Shift left if COUNT is negative. */
1226
1227 double_int
1228 double_int::lrshift (HOST_WIDE_INT count, unsigned int prec) const
1229 {
1230 double_int r;
1231 if (count > 0)
1232 rshift_double (low, high, count, prec, &r.low, &r.high, false);
1233 else
1234 lshift_double (low, high, absu_hwi (count), prec, &r.low, &r.high);
1235 return r;
1236 }
1237
1238 /* Rotate A left by COUNT places keeping only PREC bits of result.
1239 Rotate right if COUNT is negative. */
1240
1241 double_int
1242 double_int::lrotate (HOST_WIDE_INT count, unsigned int prec) const
1243 {
1244 double_int t1, t2;
1245
1246 count %= prec;
1247 if (count < 0)
1248 count += prec;
1249
1250 t1 = this->llshift (count, prec);
1251 t2 = this->lrshift (prec - count, prec);
1252
1253 return t1 | t2;
1254 }
1255
1256 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
1257 Rotate right if COUNT is negative. */
1258
1259 double_int
1260 double_int::rrotate (HOST_WIDE_INT count, unsigned int prec) const
1261 {
1262 double_int t1, t2;
1263
1264 count %= prec;
1265 if (count < 0)
1266 count += prec;
1267
1268 t1 = this->lrshift (count, prec);
1269 t2 = this->llshift (prec - count, prec);
1270
1271 return t1 | t2;
1272 }
1273
1274 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1275 comparison is given by UNS. */
1276
1277 int
1278 double_int::cmp (double_int b, bool uns) const
1279 {
1280 if (uns)
1281 return this->ucmp (b);
1282 else
1283 return this->scmp (b);
1284 }
1285
1286 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
1287 and 1 if A > B. */
1288
1289 int
1290 double_int::ucmp (double_int b) const
1291 {
1292 const double_int &a = *this;
1293 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
1294 return -1;
1295 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
1296 return 1;
1297 if (a.low < b.low)
1298 return -1;
1299 if (a.low > b.low)
1300 return 1;
1301
1302 return 0;
1303 }
1304
1305 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1306 and 1 if A > B. */
1307
1308 int
1309 double_int::scmp (double_int b) const
1310 {
1311 const double_int &a = *this;
1312 if (a.high < b.high)
1313 return -1;
1314 if (a.high > b.high)
1315 return 1;
1316 if (a.low < b.low)
1317 return -1;
1318 if (a.low > b.low)
1319 return 1;
1320
1321 return 0;
1322 }
1323
1324 /* Compares two unsigned values A and B for less-than. */
1325
1326 bool
1327 double_int::ult (double_int b) const
1328 {
1329 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1330 return true;
1331 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1332 return false;
1333 if (low < b.low)
1334 return true;
1335 return false;
1336 }
1337
1338 /* Compares two unsigned values A and B for less-than or equal-to. */
1339
1340 bool
1341 double_int::ule (double_int b) const
1342 {
1343 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1344 return true;
1345 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1346 return false;
1347 if (low <= b.low)
1348 return true;
1349 return false;
1350 }
1351
1352 /* Compares two unsigned values A and B for greater-than. */
1353
1354 bool
1355 double_int::ugt (double_int b) const
1356 {
1357 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1358 return true;
1359 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1360 return false;
1361 if (low > b.low)
1362 return true;
1363 return false;
1364 }
1365
1366 /* Compares two signed values A and B for less-than. */
1367
1368 bool
1369 double_int::slt (double_int b) const
1370 {
1371 if (high < b.high)
1372 return true;
1373 if (high > b.high)
1374 return false;
1375 if (low < b.low)
1376 return true;
1377 return false;
1378 }
1379
1380 /* Compares two signed values A and B for less-than or equal-to. */
1381
1382 bool
1383 double_int::sle (double_int b) const
1384 {
1385 if (high < b.high)
1386 return true;
1387 if (high > b.high)
1388 return false;
1389 if (low <= b.low)
1390 return true;
1391 return false;
1392 }
1393
1394 /* Compares two signed values A and B for greater-than. */
1395
1396 bool
1397 double_int::sgt (double_int b) const
1398 {
1399 if (high > b.high)
1400 return true;
1401 if (high < b.high)
1402 return false;
1403 if (low > b.low)
1404 return true;
1405 return false;
1406 }
1407
1408
1409 /* Compares two values A and B. Returns max value. Signedness of the
1410 comparison is given by UNS. */
1411
1412 double_int
1413 double_int::max (double_int b, bool uns)
1414 {
1415 return (this->cmp (b, uns) == 1) ? *this : b;
1416 }
1417
1418 /* Compares two signed values A and B. Returns max value. */
1419
1420 double_int
1421 double_int::smax (double_int b)
1422 {
1423 return (this->scmp (b) == 1) ? *this : b;
1424 }
1425
1426 /* Compares two unsigned values A and B. Returns max value. */
1427
1428 double_int
1429 double_int::umax (double_int b)
1430 {
1431 return (this->ucmp (b) == 1) ? *this : b;
1432 }
1433
1434 /* Compares two values A and B. Returns mix value. Signedness of the
1435 comparison is given by UNS. */
1436
1437 double_int
1438 double_int::min (double_int b, bool uns)
1439 {
1440 return (this->cmp (b, uns) == -1) ? *this : b;
1441 }
1442
1443 /* Compares two signed values A and B. Returns min value. */
1444
1445 double_int
1446 double_int::smin (double_int b)
1447 {
1448 return (this->scmp (b) == -1) ? *this : b;
1449 }
1450
1451 /* Compares two unsigned values A and B. Returns min value. */
1452
1453 double_int
1454 double_int::umin (double_int b)
1455 {
1456 return (this->ucmp (b) == -1) ? *this : b;
1457 }
1458
1459 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1460
1461 static unsigned
1462 double_int_split_digit (double_int *cst, unsigned base)
1463 {
1464 unsigned HOST_WIDE_INT resl, reml;
1465 HOST_WIDE_INT resh, remh;
1466
1467 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
1468 &resl, &resh, &reml, &remh);
1469 cst->high = resh;
1470 cst->low = resl;
1471
1472 return reml;
1473 }
1474
1475 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1476 otherwise it is signed. */
1477
1478 void
1479 dump_double_int (FILE *file, double_int cst, bool uns)
1480 {
1481 unsigned digits[100], n;
1482 int i;
1483
1484 if (cst.is_zero ())
1485 {
1486 fprintf (file, "0");
1487 return;
1488 }
1489
1490 if (!uns && cst.is_negative ())
1491 {
1492 fprintf (file, "-");
1493 cst = -cst;
1494 }
1495
1496 for (n = 0; !cst.is_zero (); n++)
1497 digits[n] = double_int_split_digit (&cst, 10);
1498 for (i = n - 1; i >= 0; i--)
1499 fprintf (file, "%u", digits[i]);
1500 }
1501
1502
1503 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1504 otherwise. */
1505
1506 void
1507 mpz_set_double_int (mpz_t result, double_int val, bool uns)
1508 {
1509 bool negate = false;
1510 unsigned HOST_WIDE_INT vp[2];
1511
1512 if (!uns && val.is_negative ())
1513 {
1514 negate = true;
1515 val = -val;
1516 }
1517
1518 vp[0] = val.low;
1519 vp[1] = (unsigned HOST_WIDE_INT) val.high;
1520 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
1521
1522 if (negate)
1523 mpz_neg (result, result);
1524 }
1525
1526 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1527 values of VAL will be wrapped; otherwise, they will be set to the
1528 appropriate minimum or maximum TYPE bound. */
1529
1530 double_int
1531 mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
1532 {
1533 unsigned HOST_WIDE_INT *vp;
1534 size_t count, numb;
1535 double_int res;
1536
1537 if (!wrap)
1538 {
1539 mpz_t min, max;
1540
1541 mpz_init (min);
1542 mpz_init (max);
1543 get_type_static_bounds (type, min, max);
1544
1545 if (mpz_cmp (val, min) < 0)
1546 mpz_set (val, min);
1547 else if (mpz_cmp (val, max) > 0)
1548 mpz_set (val, max);
1549
1550 mpz_clear (min);
1551 mpz_clear (max);
1552 }
1553
1554 /* Determine the number of unsigned HOST_WIDE_INT that are required
1555 for representing the value. The code to calculate count is
1556 extracted from the GMP manual, section "Integer Import and Export":
1557 http://gmplib.org/manual/Integer-Import-and-Export.html */
1558 numb = 8 * sizeof (HOST_WIDE_INT);
1559 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
1560 if (count < 2)
1561 count = 2;
1562 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof (HOST_WIDE_INT));
1563
1564 vp[0] = 0;
1565 vp[1] = 0;
1566 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
1567
1568 gcc_assert (wrap || count <= 2);
1569
1570 res.low = vp[0];
1571 res.high = (HOST_WIDE_INT) vp[1];
1572
1573 res = res.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
1574 if (mpz_sgn (val) < 0)
1575 res = -res;
1576
1577 return res;
1578 }