double-int.h (double_int_sub): Declare.
[gcc.git] / gcc / double-int.c
1 /* Operations with long integers.
2 Copyright (C) 2006, 2007, 2009, 2010 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"
24 #include "tree.h"
25
26 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
27 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
28 and SUM1. Then this yields nonzero if overflow occurred during the
29 addition.
30
31 Overflow occurs if A and B have the same sign, but A and SUM differ in
32 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
33 sign. */
34 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
35
36 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
37 We do that by representing the two-word integer in 4 words, with only
38 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
39 number. The value of the word is LOWPART + HIGHPART * BASE. */
40
41 #define LOWPART(x) \
42 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
43 #define HIGHPART(x) \
44 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
45 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
46
47 /* Unpack a two-word integer into 4 words.
48 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
49 WORDS points to the array of HOST_WIDE_INTs. */
50
51 static void
52 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
53 {
54 words[0] = LOWPART (low);
55 words[1] = HIGHPART (low);
56 words[2] = LOWPART (hi);
57 words[3] = HIGHPART (hi);
58 }
59
60 /* Pack an array of 4 words into a two-word integer.
61 WORDS points to the array of words.
62 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
63
64 static void
65 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
66 HOST_WIDE_INT *hi)
67 {
68 *low = words[0] + words[1] * BASE;
69 *hi = words[2] + words[3] * BASE;
70 }
71
72 /* Force the double-word integer L1, H1 to be within the range of the
73 integer type TYPE. Stores the properly truncated and sign-extended
74 double-word integer in *LV, *HV. Returns true if the operation
75 overflows, that is, argument and result are different. */
76
77 int
78 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
79 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
80 {
81 unsigned HOST_WIDE_INT low0 = l1;
82 HOST_WIDE_INT high0 = h1;
83 unsigned int prec = TYPE_PRECISION (type);
84 int sign_extended_type;
85
86 /* Size types *are* sign extended. */
87 sign_extended_type = (!TYPE_UNSIGNED (type)
88 || (TREE_CODE (type) == INTEGER_TYPE
89 && TYPE_IS_SIZETYPE (type)));
90
91 /* First clear all bits that are beyond the type's precision. */
92 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
93 ;
94 else if (prec > HOST_BITS_PER_WIDE_INT)
95 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
96 else
97 {
98 h1 = 0;
99 if (prec < HOST_BITS_PER_WIDE_INT)
100 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
101 }
102
103 /* Then do sign extension if necessary. */
104 if (!sign_extended_type)
105 /* No sign extension */;
106 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
107 /* Correct width already. */;
108 else if (prec > HOST_BITS_PER_WIDE_INT)
109 {
110 /* Sign extend top half? */
111 if (h1 & ((unsigned HOST_WIDE_INT)1
112 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
113 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
114 }
115 else if (prec == HOST_BITS_PER_WIDE_INT)
116 {
117 if ((HOST_WIDE_INT)l1 < 0)
118 h1 = -1;
119 }
120 else
121 {
122 /* Sign extend bottom half? */
123 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
124 {
125 h1 = -1;
126 l1 |= (HOST_WIDE_INT)(-1) << prec;
127 }
128 }
129
130 *lv = l1;
131 *hv = h1;
132
133 /* If the value didn't fit, signal overflow. */
134 return l1 != low0 || h1 != high0;
135 }
136
137 /* Add two doubleword integers with doubleword result.
138 Return nonzero if the operation overflows according to UNSIGNED_P.
139 Each argument is given as two `HOST_WIDE_INT' pieces.
140 One argument is L1 and H1; the other, L2 and H2.
141 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
142
143 int
144 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
145 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
146 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
147 bool unsigned_p)
148 {
149 unsigned HOST_WIDE_INT l;
150 HOST_WIDE_INT h;
151
152 l = l1 + l2;
153 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
154 + (unsigned HOST_WIDE_INT) h2
155 + (l < l1));
156
157 *lv = l;
158 *hv = h;
159
160 if (unsigned_p)
161 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
162 || (h == h1
163 && l < l1));
164 else
165 return OVERFLOW_SUM_SIGN (h1, h2, h);
166 }
167
168 /* Negate a doubleword integer with doubleword result.
169 Return nonzero if the operation overflows, assuming it's signed.
170 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
171 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
172
173 int
174 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
175 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
176 {
177 if (l1 == 0)
178 {
179 *lv = 0;
180 *hv = - h1;
181 return (*hv & h1) < 0;
182 }
183 else
184 {
185 *lv = -l1;
186 *hv = ~h1;
187 return 0;
188 }
189 }
190
191 /* Multiply two doubleword integers with doubleword result.
192 Return nonzero if the operation overflows according to UNSIGNED_P.
193 Each argument is given as two `HOST_WIDE_INT' pieces.
194 One argument is L1 and H1; the other, L2 and H2.
195 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
196
197 int
198 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
199 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
200 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
201 bool unsigned_p)
202 {
203 HOST_WIDE_INT arg1[4];
204 HOST_WIDE_INT arg2[4];
205 HOST_WIDE_INT prod[4 * 2];
206 unsigned HOST_WIDE_INT carry;
207 int i, j, k;
208 unsigned HOST_WIDE_INT toplow, neglow;
209 HOST_WIDE_INT tophigh, neghigh;
210
211 encode (arg1, l1, h1);
212 encode (arg2, l2, h2);
213
214 memset (prod, 0, sizeof prod);
215
216 for (i = 0; i < 4; i++)
217 {
218 carry = 0;
219 for (j = 0; j < 4; j++)
220 {
221 k = i + j;
222 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
223 carry += arg1[i] * arg2[j];
224 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
225 carry += prod[k];
226 prod[k] = LOWPART (carry);
227 carry = HIGHPART (carry);
228 }
229 prod[i + 4] = carry;
230 }
231
232 decode (prod, lv, hv);
233 decode (prod + 4, &toplow, &tophigh);
234
235 /* Unsigned overflow is immediate. */
236 if (unsigned_p)
237 return (toplow | tophigh) != 0;
238
239 /* Check for signed overflow by calculating the signed representation of the
240 top half of the result; it should agree with the low half's sign bit. */
241 if (h1 < 0)
242 {
243 neg_double (l2, h2, &neglow, &neghigh);
244 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
245 }
246 if (h2 < 0)
247 {
248 neg_double (l1, h1, &neglow, &neghigh);
249 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
250 }
251 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
252 }
253
254 /* Shift the doubleword integer in L1, H1 left by COUNT places
255 keeping only PREC bits of result.
256 Shift right if COUNT is negative.
257 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
258 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
259
260 void
261 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
262 HOST_WIDE_INT count, unsigned int prec,
263 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, bool arith)
264 {
265 unsigned HOST_WIDE_INT signmask;
266
267 if (count < 0)
268 {
269 rshift_double (l1, h1, -count, prec, lv, hv, arith);
270 return;
271 }
272
273 if (SHIFT_COUNT_TRUNCATED)
274 count %= prec;
275
276 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
277 {
278 /* Shifting by the host word size is undefined according to the
279 ANSI standard, so we must handle this as a special case. */
280 *hv = 0;
281 *lv = 0;
282 }
283 else if (count >= HOST_BITS_PER_WIDE_INT)
284 {
285 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
286 *lv = 0;
287 }
288 else
289 {
290 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
291 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
292 *lv = l1 << count;
293 }
294
295 /* Sign extend all bits that are beyond the precision. */
296
297 signmask = -((prec > HOST_BITS_PER_WIDE_INT
298 ? ((unsigned HOST_WIDE_INT) *hv
299 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
300 : (*lv >> (prec - 1))) & 1);
301
302 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
303 ;
304 else if (prec >= HOST_BITS_PER_WIDE_INT)
305 {
306 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
307 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
308 }
309 else
310 {
311 *hv = signmask;
312 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
313 *lv |= signmask << prec;
314 }
315 }
316
317 /* Shift the doubleword integer in L1, H1 right by COUNT places
318 keeping only PREC bits of result. Shift left if COUNT is negative.
319 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
320 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
321
322 void
323 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
324 HOST_WIDE_INT count, unsigned int prec,
325 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
326 bool arith)
327 {
328 unsigned HOST_WIDE_INT signmask;
329
330 if (count < 0)
331 {
332 lshift_double (l1, h1, -count, prec, lv, hv, arith);
333 return;
334 }
335
336 signmask = (arith
337 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
338 : 0);
339
340 if (SHIFT_COUNT_TRUNCATED)
341 count %= prec;
342
343 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
344 {
345 /* Shifting by the host word size is undefined according to the
346 ANSI standard, so we must handle this as a special case. */
347 *hv = 0;
348 *lv = 0;
349 }
350 else if (count >= HOST_BITS_PER_WIDE_INT)
351 {
352 *hv = 0;
353 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
354 }
355 else
356 {
357 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
358 *lv = ((l1 >> count)
359 | ((unsigned HOST_WIDE_INT) h1
360 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
361 }
362
363 /* Zero / sign extend all bits that are beyond the precision. */
364
365 if (count >= (HOST_WIDE_INT)prec)
366 {
367 *hv = signmask;
368 *lv = signmask;
369 }
370 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
371 ;
372 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
373 {
374 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
375 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
376 }
377 else
378 {
379 *hv = signmask;
380 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
381 *lv |= signmask << (prec - count);
382 }
383 }
384
385 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
386 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
387 CODE is a tree code for a kind of division, one of
388 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
389 or EXACT_DIV_EXPR
390 It controls how the quotient is rounded to an integer.
391 Return nonzero if the operation overflows.
392 UNS nonzero says do unsigned division. */
393
394 int
395 div_and_round_double (unsigned code, int uns,
396 /* num == numerator == dividend */
397 unsigned HOST_WIDE_INT lnum_orig,
398 HOST_WIDE_INT hnum_orig,
399 /* den == denominator == divisor */
400 unsigned HOST_WIDE_INT lden_orig,
401 HOST_WIDE_INT hden_orig,
402 unsigned HOST_WIDE_INT *lquo,
403 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
404 HOST_WIDE_INT *hrem)
405 {
406 int quo_neg = 0;
407 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
408 HOST_WIDE_INT den[4], quo[4];
409 int i, j;
410 unsigned HOST_WIDE_INT work;
411 unsigned HOST_WIDE_INT carry = 0;
412 unsigned HOST_WIDE_INT lnum = lnum_orig;
413 HOST_WIDE_INT hnum = hnum_orig;
414 unsigned HOST_WIDE_INT lden = lden_orig;
415 HOST_WIDE_INT hden = hden_orig;
416 int overflow = 0;
417
418 if (hden == 0 && lden == 0)
419 overflow = 1, lden = 1;
420
421 /* Calculate quotient sign and convert operands to unsigned. */
422 if (!uns)
423 {
424 if (hnum < 0)
425 {
426 quo_neg = ~ quo_neg;
427 /* (minimum integer) / (-1) is the only overflow case. */
428 if (neg_double (lnum, hnum, &lnum, &hnum)
429 && ((HOST_WIDE_INT) lden & hden) == -1)
430 overflow = 1;
431 }
432 if (hden < 0)
433 {
434 quo_neg = ~ quo_neg;
435 neg_double (lden, hden, &lden, &hden);
436 }
437 }
438
439 if (hnum == 0 && hden == 0)
440 { /* single precision */
441 *hquo = *hrem = 0;
442 /* This unsigned division rounds toward zero. */
443 *lquo = lnum / lden;
444 goto finish_up;
445 }
446
447 if (hnum == 0)
448 { /* trivial case: dividend < divisor */
449 /* hden != 0 already checked. */
450 *hquo = *lquo = 0;
451 *hrem = hnum;
452 *lrem = lnum;
453 goto finish_up;
454 }
455
456 memset (quo, 0, sizeof quo);
457
458 memset (num, 0, sizeof num); /* to zero 9th element */
459 memset (den, 0, sizeof den);
460
461 encode (num, lnum, hnum);
462 encode (den, lden, hden);
463
464 /* Special code for when the divisor < BASE. */
465 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
466 {
467 /* hnum != 0 already checked. */
468 for (i = 4 - 1; i >= 0; i--)
469 {
470 work = num[i] + carry * BASE;
471 quo[i] = work / lden;
472 carry = work % lden;
473 }
474 }
475 else
476 {
477 /* Full double precision division,
478 with thanks to Don Knuth's "Seminumerical Algorithms". */
479 int num_hi_sig, den_hi_sig;
480 unsigned HOST_WIDE_INT quo_est, scale;
481
482 /* Find the highest nonzero divisor digit. */
483 for (i = 4 - 1;; i--)
484 if (den[i] != 0)
485 {
486 den_hi_sig = i;
487 break;
488 }
489
490 /* Insure that the first digit of the divisor is at least BASE/2.
491 This is required by the quotient digit estimation algorithm. */
492
493 scale = BASE / (den[den_hi_sig] + 1);
494 if (scale > 1)
495 { /* scale divisor and dividend */
496 carry = 0;
497 for (i = 0; i <= 4 - 1; i++)
498 {
499 work = (num[i] * scale) + carry;
500 num[i] = LOWPART (work);
501 carry = HIGHPART (work);
502 }
503
504 num[4] = carry;
505 carry = 0;
506 for (i = 0; i <= 4 - 1; i++)
507 {
508 work = (den[i] * scale) + carry;
509 den[i] = LOWPART (work);
510 carry = HIGHPART (work);
511 if (den[i] != 0) den_hi_sig = i;
512 }
513 }
514
515 num_hi_sig = 4;
516
517 /* Main loop */
518 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
519 {
520 /* Guess the next quotient digit, quo_est, by dividing the first
521 two remaining dividend digits by the high order quotient digit.
522 quo_est is never low and is at most 2 high. */
523 unsigned HOST_WIDE_INT tmp;
524
525 num_hi_sig = i + den_hi_sig + 1;
526 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
527 if (num[num_hi_sig] != den[den_hi_sig])
528 quo_est = work / den[den_hi_sig];
529 else
530 quo_est = BASE - 1;
531
532 /* Refine quo_est so it's usually correct, and at most one high. */
533 tmp = work - quo_est * den[den_hi_sig];
534 if (tmp < BASE
535 && (den[den_hi_sig - 1] * quo_est
536 > (tmp * BASE + num[num_hi_sig - 2])))
537 quo_est--;
538
539 /* Try QUO_EST as the quotient digit, by multiplying the
540 divisor by QUO_EST and subtracting from the remaining dividend.
541 Keep in mind that QUO_EST is the I - 1st digit. */
542
543 carry = 0;
544 for (j = 0; j <= den_hi_sig; j++)
545 {
546 work = quo_est * den[j] + carry;
547 carry = HIGHPART (work);
548 work = num[i + j] - LOWPART (work);
549 num[i + j] = LOWPART (work);
550 carry += HIGHPART (work) != 0;
551 }
552
553 /* If quo_est was high by one, then num[i] went negative and
554 we need to correct things. */
555 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
556 {
557 quo_est--;
558 carry = 0; /* add divisor back in */
559 for (j = 0; j <= den_hi_sig; j++)
560 {
561 work = num[i + j] + den[j] + carry;
562 carry = HIGHPART (work);
563 num[i + j] = LOWPART (work);
564 }
565
566 num [num_hi_sig] += carry;
567 }
568
569 /* Store the quotient digit. */
570 quo[i] = quo_est;
571 }
572 }
573
574 decode (quo, lquo, hquo);
575
576 finish_up:
577 /* If result is negative, make it so. */
578 if (quo_neg)
579 neg_double (*lquo, *hquo, lquo, hquo);
580
581 /* Compute trial remainder: rem = num - (quo * den) */
582 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
583 neg_double (*lrem, *hrem, lrem, hrem);
584 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
585
586 switch (code)
587 {
588 case TRUNC_DIV_EXPR:
589 case TRUNC_MOD_EXPR: /* round toward zero */
590 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
591 return overflow;
592
593 case FLOOR_DIV_EXPR:
594 case FLOOR_MOD_EXPR: /* round toward negative infinity */
595 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
596 {
597 /* quo = quo - 1; */
598 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
599 lquo, hquo);
600 }
601 else
602 return overflow;
603 break;
604
605 case CEIL_DIV_EXPR:
606 case CEIL_MOD_EXPR: /* round toward positive infinity */
607 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
608 {
609 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
610 lquo, hquo);
611 }
612 else
613 return overflow;
614 break;
615
616 case ROUND_DIV_EXPR:
617 case ROUND_MOD_EXPR: /* round to closest integer */
618 {
619 unsigned HOST_WIDE_INT labs_rem = *lrem;
620 HOST_WIDE_INT habs_rem = *hrem;
621 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
622 HOST_WIDE_INT habs_den = hden, htwice;
623
624 /* Get absolute values. */
625 if (*hrem < 0)
626 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
627 if (hden < 0)
628 neg_double (lden, hden, &labs_den, &habs_den);
629
630 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
631 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
632 labs_rem, habs_rem, &ltwice, &htwice);
633
634 if (((unsigned HOST_WIDE_INT) habs_den
635 < (unsigned HOST_WIDE_INT) htwice)
636 || (((unsigned HOST_WIDE_INT) habs_den
637 == (unsigned HOST_WIDE_INT) htwice)
638 && (labs_den <= ltwice)))
639 {
640 if (*hquo < 0)
641 /* quo = quo - 1; */
642 add_double (*lquo, *hquo,
643 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
644 else
645 /* quo = quo + 1; */
646 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
647 lquo, hquo);
648 }
649 else
650 return overflow;
651 }
652 break;
653
654 default:
655 gcc_unreachable ();
656 }
657
658 /* Compute true remainder: rem = num - (quo * den) */
659 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
660 neg_double (*lrem, *hrem, lrem, hrem);
661 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
662 return overflow;
663 }
664
665
666 /* Returns mask for PREC bits. */
667
668 double_int
669 double_int_mask (unsigned prec)
670 {
671 unsigned HOST_WIDE_INT m;
672 double_int mask;
673
674 if (prec > HOST_BITS_PER_WIDE_INT)
675 {
676 prec -= HOST_BITS_PER_WIDE_INT;
677 m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
678 mask.high = (HOST_WIDE_INT) m;
679 mask.low = ALL_ONES;
680 }
681 else
682 {
683 mask.high = 0;
684 mask.low = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
685 }
686
687 return mask;
688 }
689
690 /* Clears the bits of CST over the precision PREC. If UNS is false, the bits
691 outside of the precision are set to the sign bit (i.e., the PREC-th one),
692 otherwise they are set to zero.
693
694 This corresponds to returning the value represented by PREC lowermost bits
695 of CST, with the given signedness. */
696
697 double_int
698 double_int_ext (double_int cst, unsigned prec, bool uns)
699 {
700 if (uns)
701 return double_int_zext (cst, prec);
702 else
703 return double_int_sext (cst, prec);
704 }
705
706 /* The same as double_int_ext with UNS = true. */
707
708 double_int
709 double_int_zext (double_int cst, unsigned prec)
710 {
711 double_int mask = double_int_mask (prec);
712 double_int r;
713
714 r.low = cst.low & mask.low;
715 r.high = cst.high & mask.high;
716
717 return r;
718 }
719
720 /* The same as double_int_ext with UNS = false. */
721
722 double_int
723 double_int_sext (double_int cst, unsigned prec)
724 {
725 double_int mask = double_int_mask (prec);
726 double_int r;
727 unsigned HOST_WIDE_INT snum;
728
729 if (prec <= HOST_BITS_PER_WIDE_INT)
730 snum = cst.low;
731 else
732 {
733 prec -= HOST_BITS_PER_WIDE_INT;
734 snum = (unsigned HOST_WIDE_INT) cst.high;
735 }
736 if (((snum >> (prec - 1)) & 1) == 1)
737 {
738 r.low = cst.low | ~mask.low;
739 r.high = cst.high | ~mask.high;
740 }
741 else
742 {
743 r.low = cst.low & mask.low;
744 r.high = cst.high & mask.high;
745 }
746
747 return r;
748 }
749
750 /* Returns true if CST fits in signed HOST_WIDE_INT. */
751
752 bool
753 double_int_fits_in_shwi_p (double_int cst)
754 {
755 if (cst.high == 0)
756 return (HOST_WIDE_INT) cst.low >= 0;
757 else if (cst.high == -1)
758 return (HOST_WIDE_INT) cst.low < 0;
759 else
760 return false;
761 }
762
763 /* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
764 unsigned HOST_WIDE_INT if UNS is true. */
765
766 bool
767 double_int_fits_in_hwi_p (double_int cst, bool uns)
768 {
769 if (uns)
770 return double_int_fits_in_uhwi_p (cst);
771 else
772 return double_int_fits_in_shwi_p (cst);
773 }
774
775 /* Returns A * B. */
776
777 double_int
778 double_int_mul (double_int a, double_int b)
779 {
780 double_int ret;
781 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
782 return ret;
783 }
784
785 /* Returns A + B. */
786
787 double_int
788 double_int_add (double_int a, double_int b)
789 {
790 double_int ret;
791 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
792 return ret;
793 }
794
795 /* Returns A - B. */
796
797 double_int
798 double_int_sub (double_int a, double_int b)
799 {
800 double_int ret;
801 neg_double (b.low, b.high, &b.low, &b.high);
802 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
803 return ret;
804 }
805
806 /* Returns -A. */
807
808 double_int
809 double_int_neg (double_int a)
810 {
811 double_int ret;
812 neg_double (a.low, a.high, &ret.low, &ret.high);
813 return ret;
814 }
815
816 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
817 specified by CODE). CODE is enum tree_code in fact, but double_int.h
818 must be included before tree.h. The remainder after the division is
819 stored to MOD. */
820
821 double_int
822 double_int_divmod (double_int a, double_int b, bool uns, unsigned code,
823 double_int *mod)
824 {
825 double_int ret;
826
827 div_and_round_double (code, uns, a.low, a.high,
828 b.low, b.high, &ret.low, &ret.high,
829 &mod->low, &mod->high);
830 return ret;
831 }
832
833 /* The same as double_int_divmod with UNS = false. */
834
835 double_int
836 double_int_sdivmod (double_int a, double_int b, unsigned code, double_int *mod)
837 {
838 return double_int_divmod (a, b, false, code, mod);
839 }
840
841 /* The same as double_int_divmod with UNS = true. */
842
843 double_int
844 double_int_udivmod (double_int a, double_int b, unsigned code, double_int *mod)
845 {
846 return double_int_divmod (a, b, true, code, mod);
847 }
848
849 /* Returns A / B (computed as unsigned depending on UNS, and rounded as
850 specified by CODE). CODE is enum tree_code in fact, but double_int.h
851 must be included before tree.h. */
852
853 double_int
854 double_int_div (double_int a, double_int b, bool uns, unsigned code)
855 {
856 double_int mod;
857
858 return double_int_divmod (a, b, uns, code, &mod);
859 }
860
861 /* The same as double_int_div with UNS = false. */
862
863 double_int
864 double_int_sdiv (double_int a, double_int b, unsigned code)
865 {
866 return double_int_div (a, b, false, code);
867 }
868
869 /* The same as double_int_div with UNS = true. */
870
871 double_int
872 double_int_udiv (double_int a, double_int b, unsigned code)
873 {
874 return double_int_div (a, b, true, code);
875 }
876
877 /* Returns A % B (computed as unsigned depending on UNS, and rounded as
878 specified by CODE). CODE is enum tree_code in fact, but double_int.h
879 must be included before tree.h. */
880
881 double_int
882 double_int_mod (double_int a, double_int b, bool uns, unsigned code)
883 {
884 double_int mod;
885
886 double_int_divmod (a, b, uns, code, &mod);
887 return mod;
888 }
889
890 /* The same as double_int_mod with UNS = false. */
891
892 double_int
893 double_int_smod (double_int a, double_int b, unsigned code)
894 {
895 return double_int_mod (a, b, false, code);
896 }
897
898 /* The same as double_int_mod with UNS = true. */
899
900 double_int
901 double_int_umod (double_int a, double_int b, unsigned code)
902 {
903 return double_int_mod (a, b, true, code);
904 }
905
906 /* Set BITPOS bit in A. */
907 double_int
908 double_int_setbit (double_int a, unsigned bitpos)
909 {
910 if (bitpos < HOST_BITS_PER_WIDE_INT)
911 a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
912 else
913 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
914
915 return a;
916 }
917
918 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
919 right if COUNT is negative. ARITH true specifies arithmetic shifting;
920 otherwise use logical shift. */
921
922 double_int
923 double_int_lshift (double_int a, HOST_WIDE_INT count, unsigned int prec, bool arith)
924 {
925 double_int ret;
926 lshift_double (a.low, a.high, count, prec, &ret.low, &ret.high, arith);
927 return ret;
928 }
929
930 /* Shift A rigth by COUNT places keeping only PREC bits of result. Shift
931 left if COUNT is negative. ARITH true specifies arithmetic shifting;
932 otherwise use logical shift. */
933
934 double_int
935 double_int_rshift (double_int a, HOST_WIDE_INT count, unsigned int prec, bool arith)
936 {
937 double_int ret;
938 rshift_double (a.low, a.high, count, prec, &ret.low, &ret.high, arith);
939 return ret;
940 }
941
942 /* Rotate A left by COUNT places keeping only PREC bits of result.
943 Rotate right if COUNT is negative. */
944
945 double_int
946 double_int_lrotate (double_int a, HOST_WIDE_INT count, unsigned int prec)
947 {
948 double_int t1, t2;
949
950 count %= prec;
951 if (count < 0)
952 count += prec;
953
954 t1 = double_int_lshift (a, count, prec, false);
955 t2 = double_int_rshift (a, prec - count, prec, false);
956
957 return double_int_ior (t1, t2);
958 }
959
960 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
961 Rotate right if COUNT is negative. */
962
963 double_int
964 double_int_rrotate (double_int a, HOST_WIDE_INT count, unsigned int prec)
965 {
966 double_int t1, t2;
967
968 count %= prec;
969 if (count < 0)
970 count += prec;
971
972 t1 = double_int_rshift (a, count, prec, false);
973 t2 = double_int_lshift (a, prec - count, prec, false);
974
975 return double_int_ior (t1, t2);
976 }
977
978 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
979 comparison is given by UNS. */
980
981 int
982 double_int_cmp (double_int a, double_int b, bool uns)
983 {
984 if (uns)
985 return double_int_ucmp (a, b);
986 else
987 return double_int_scmp (a, b);
988 }
989
990 /* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
991 and 1 if A > B. */
992
993 int
994 double_int_ucmp (double_int a, double_int b)
995 {
996 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
997 return -1;
998 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
999 return 1;
1000 if (a.low < b.low)
1001 return -1;
1002 if (a.low > b.low)
1003 return 1;
1004
1005 return 0;
1006 }
1007
1008 /* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1009 and 1 if A > B. */
1010
1011 int
1012 double_int_scmp (double_int a, double_int b)
1013 {
1014 if (a.high < b.high)
1015 return -1;
1016 if (a.high > b.high)
1017 return 1;
1018 if (a.low < b.low)
1019 return -1;
1020 if (a.low > b.low)
1021 return 1;
1022
1023 return 0;
1024 }
1025
1026 /* Compares two values A and B. Returns max value. Signedness of the
1027 comparison is given by UNS. */
1028
1029 double_int
1030 double_int_max (double_int a, double_int b, bool uns)
1031 {
1032 return (double_int_cmp (a, b, uns) == 1) ? a : b;
1033 }
1034
1035 /* Compares two signed values A and B. Returns max value. */
1036
1037 double_int double_int_smax (double_int a, double_int b)
1038 {
1039 return (double_int_scmp (a, b) == 1) ? a : b;
1040 }
1041
1042 /* Compares two unsigned values A and B. Returns max value. */
1043
1044 double_int double_int_umax (double_int a, double_int b)
1045 {
1046 return (double_int_ucmp (a, b) == 1) ? a : b;
1047 }
1048
1049 /* Compares two values A and B. Returns mix value. Signedness of the
1050 comparison is given by UNS. */
1051
1052 double_int double_int_min (double_int a, double_int b, bool uns)
1053 {
1054 return (double_int_cmp (a, b, uns) == -1) ? a : b;
1055 }
1056
1057 /* Compares two signed values A and B. Returns min value. */
1058
1059 double_int double_int_smin (double_int a, double_int b)
1060 {
1061 return (double_int_scmp (a, b) == -1) ? a : b;
1062 }
1063
1064 /* Compares two unsigned values A and B. Returns min value. */
1065
1066 double_int double_int_umin (double_int a, double_int b)
1067 {
1068 return (double_int_ucmp (a, b) == -1) ? a : b;
1069 }
1070
1071 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1072
1073 static unsigned
1074 double_int_split_digit (double_int *cst, unsigned base)
1075 {
1076 unsigned HOST_WIDE_INT resl, reml;
1077 HOST_WIDE_INT resh, remh;
1078
1079 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
1080 &resl, &resh, &reml, &remh);
1081 cst->high = resh;
1082 cst->low = resl;
1083
1084 return reml;
1085 }
1086
1087 /* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1088 otherwise it is signed. */
1089
1090 void
1091 dump_double_int (FILE *file, double_int cst, bool uns)
1092 {
1093 unsigned digits[100], n;
1094 int i;
1095
1096 if (double_int_zero_p (cst))
1097 {
1098 fprintf (file, "0");
1099 return;
1100 }
1101
1102 if (!uns && double_int_negative_p (cst))
1103 {
1104 fprintf (file, "-");
1105 cst = double_int_neg (cst);
1106 }
1107
1108 for (n = 0; !double_int_zero_p (cst); n++)
1109 digits[n] = double_int_split_digit (&cst, 10);
1110 for (i = n - 1; i >= 0; i--)
1111 fprintf (file, "%u", digits[i]);
1112 }
1113
1114
1115 /* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1116 otherwise. */
1117
1118 void
1119 mpz_set_double_int (mpz_t result, double_int val, bool uns)
1120 {
1121 bool negate = false;
1122 unsigned HOST_WIDE_INT vp[2];
1123
1124 if (!uns && double_int_negative_p (val))
1125 {
1126 negate = true;
1127 val = double_int_neg (val);
1128 }
1129
1130 vp[0] = val.low;
1131 vp[1] = (unsigned HOST_WIDE_INT) val.high;
1132 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
1133
1134 if (negate)
1135 mpz_neg (result, result);
1136 }
1137
1138 /* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1139 values of VAL will be wrapped; otherwise, they will be set to the
1140 appropriate minimum or maximum TYPE bound. */
1141
1142 double_int
1143 mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
1144 {
1145 unsigned HOST_WIDE_INT *vp;
1146 size_t count, numb;
1147 double_int res;
1148
1149 if (!wrap)
1150 {
1151 mpz_t min, max;
1152
1153 mpz_init (min);
1154 mpz_init (max);
1155 get_type_static_bounds (type, min, max);
1156
1157 if (mpz_cmp (val, min) < 0)
1158 mpz_set (val, min);
1159 else if (mpz_cmp (val, max) > 0)
1160 mpz_set (val, max);
1161
1162 mpz_clear (min);
1163 mpz_clear (max);
1164 }
1165
1166 /* Determine the number of unsigned HOST_WIDE_INT that are required
1167 for representing the value. The code to calculate count is
1168 extracted from the GMP manual, section "Integer Import and Export":
1169 http://gmplib.org/manual/Integer-Import-and-Export.html */
1170 numb = 8*sizeof(HOST_WIDE_INT);
1171 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
1172 if (count < 2)
1173 count = 2;
1174 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof(HOST_WIDE_INT));
1175
1176 vp[0] = 0;
1177 vp[1] = 0;
1178 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
1179
1180 gcc_assert (wrap || count <= 2);
1181
1182 res.low = vp[0];
1183 res.high = (HOST_WIDE_INT) vp[1];
1184
1185 res = double_int_ext (res, TYPE_PRECISION (type), TYPE_UNSIGNED (type));
1186 if (mpz_sgn (val) < 0)
1187 res = double_int_neg (res);
1188
1189 return res;
1190 }