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