]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/double-int.c
backport: As described in http://gcc.gnu.org/ml/gcc/2012-08/msg00015.html...
[thirdparty/gcc.git] / gcc / double-int.c
CommitLineData
f82783bd 1/* Operations with long integers.
0ea62d93 2 Copyright (C) 2006, 2007, 2009, 2010, 2012 Free Software Foundation, Inc.
b8698a0f 3
f82783bd 4This file is part of GCC.
b8698a0f 5
f82783bd
ZD
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
9dcd6f09 8Free Software Foundation; either version 3, or (at your option) any
f82783bd 9later version.
b8698a0f 10
f82783bd
ZD
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b8698a0f 15
f82783bd 16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
f82783bd
ZD
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
338ae1c1 23#include "tm.h" /* For SHIFT_COUNT_TRUNCATED. */
f82783bd
ZD
24#include "tree.h"
25
330db1e3
RG
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
51static void
52encode (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
64static void
65decode (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
330db1e3
RG
72/* Add two doubleword integers with doubleword result.
73 Return nonzero if the operation overflows according to UNSIGNED_P.
74 Each argument is given as two `HOST_WIDE_INT' pieces.
75 One argument is L1 and H1; the other, L2 and H2.
76 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
77
78int
79add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
80 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
81 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
82 bool unsigned_p)
83{
84 unsigned HOST_WIDE_INT l;
85 HOST_WIDE_INT h;
86
87 l = l1 + l2;
88 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
89 + (unsigned HOST_WIDE_INT) h2
90 + (l < l1));
91
92 *lv = l;
93 *hv = h;
94
95 if (unsigned_p)
96 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
97 || (h == h1
98 && l < l1));
99 else
100 return OVERFLOW_SUM_SIGN (h1, h2, h);
101}
102
103/* Negate a doubleword integer with doubleword result.
104 Return nonzero if the operation overflows, assuming it's signed.
105 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
106 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
107
108int
109neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
110 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
111{
112 if (l1 == 0)
113 {
114 *lv = 0;
115 *hv = - h1;
116 return (*hv & h1) < 0;
117 }
118 else
119 {
120 *lv = -l1;
121 *hv = ~h1;
122 return 0;
123 }
124}
125
126/* Multiply two doubleword integers with doubleword result.
127 Return nonzero if the operation overflows according to UNSIGNED_P.
128 Each argument is given as two `HOST_WIDE_INT' pieces.
129 One argument is L1 and H1; the other, L2 and H2.
130 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
131
132int
133mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
134 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
135 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
136 bool unsigned_p)
4e7c4b73
MG
137{
138 unsigned HOST_WIDE_INT toplow;
139 HOST_WIDE_INT tophigh;
140
141 return mul_double_wide_with_sign (l1, h1, l2, h2,
142 lv, hv, &toplow, &tophigh,
143 unsigned_p);
144}
145
146int
147mul_double_wide_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
148 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
149 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
150 unsigned HOST_WIDE_INT *lw, HOST_WIDE_INT *hw,
151 bool unsigned_p)
330db1e3
RG
152{
153 HOST_WIDE_INT arg1[4];
154 HOST_WIDE_INT arg2[4];
155 HOST_WIDE_INT prod[4 * 2];
156 unsigned HOST_WIDE_INT carry;
157 int i, j, k;
4e7c4b73
MG
158 unsigned HOST_WIDE_INT neglow;
159 HOST_WIDE_INT neghigh;
330db1e3
RG
160
161 encode (arg1, l1, h1);
162 encode (arg2, l2, h2);
163
164 memset (prod, 0, sizeof prod);
165
166 for (i = 0; i < 4; i++)
167 {
168 carry = 0;
169 for (j = 0; j < 4; j++)
170 {
171 k = i + j;
172 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
eb87c7c4 173 carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j];
330db1e3
RG
174 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
175 carry += prod[k];
176 prod[k] = LOWPART (carry);
177 carry = HIGHPART (carry);
178 }
179 prod[i + 4] = carry;
180 }
181
182 decode (prod, lv, hv);
4e7c4b73 183 decode (prod + 4, lw, hw);
330db1e3
RG
184
185 /* Unsigned overflow is immediate. */
186 if (unsigned_p)
4e7c4b73 187 return (*lw | *hw) != 0;
330db1e3
RG
188
189 /* Check for signed overflow by calculating the signed representation of the
190 top half of the result; it should agree with the low half's sign bit. */
191 if (h1 < 0)
192 {
193 neg_double (l2, h2, &neglow, &neghigh);
4e7c4b73 194 add_double (neglow, neghigh, *lw, *hw, lw, hw);
330db1e3
RG
195 }
196 if (h2 < 0)
197 {
198 neg_double (l1, h1, &neglow, &neghigh);
4e7c4b73 199 add_double (neglow, neghigh, *lw, *hw, lw, hw);
330db1e3 200 }
4e7c4b73 201 return (*hv < 0 ? ~(*lw & *hw) : *lw | *hw) != 0;
330db1e3
RG
202}
203
477fcae3
RG
204/* Shift the doubleword integer in L1, H1 right by COUNT places
205 keeping only PREC bits of result. ARITH nonzero specifies
206 arithmetic shifting; otherwise use logical shift.
330db1e3
RG
207 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
208
477fcae3
RG
209static void
210rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
211 unsigned HOST_WIDE_INT count, unsigned int prec,
212 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
213 bool arith)
330db1e3
RG
214{
215 unsigned HOST_WIDE_INT signmask;
216
477fcae3
RG
217 signmask = (arith
218 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
219 : 0);
330db1e3
RG
220
221 if (SHIFT_COUNT_TRUNCATED)
222 count %= prec;
223
49ab6098 224 if (count >= HOST_BITS_PER_DOUBLE_INT)
330db1e3
RG
225 {
226 /* Shifting by the host word size is undefined according to the
227 ANSI standard, so we must handle this as a special case. */
228 *hv = 0;
229 *lv = 0;
230 }
231 else if (count >= HOST_BITS_PER_WIDE_INT)
232 {
477fcae3
RG
233 *hv = 0;
234 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
330db1e3
RG
235 }
236 else
237 {
477fcae3
RG
238 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
239 *lv = ((l1 >> count)
240 | ((unsigned HOST_WIDE_INT) h1
241 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
330db1e3
RG
242 }
243
477fcae3 244 /* Zero / sign extend all bits that are beyond the precision. */
330db1e3 245
5cf01d62 246 if (count >= prec)
477fcae3
RG
247 {
248 *hv = signmask;
249 *lv = signmask;
250 }
49ab6098 251 else if ((prec - count) >= HOST_BITS_PER_DOUBLE_INT)
330db1e3 252 ;
477fcae3 253 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
330db1e3 254 {
477fcae3
RG
255 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
256 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
330db1e3
RG
257 }
258 else
259 {
260 *hv = signmask;
477fcae3
RG
261 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
262 *lv |= signmask << (prec - count);
330db1e3
RG
263 }
264}
265
477fcae3
RG
266/* Shift the doubleword integer in L1, H1 left by COUNT places
267 keeping only PREC bits of result.
268 Shift right if COUNT is negative.
330db1e3
RG
269 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
270 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
271
272void
477fcae3 273lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
330db1e3 274 HOST_WIDE_INT count, unsigned int prec,
477fcae3 275 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, bool arith)
330db1e3
RG
276{
277 unsigned HOST_WIDE_INT signmask;
278
279 if (count < 0)
280 {
477fcae3 281 rshift_double (l1, h1, absu_hwi (count), prec, lv, hv, arith);
330db1e3
RG
282 return;
283 }
284
330db1e3
RG
285 if (SHIFT_COUNT_TRUNCATED)
286 count %= prec;
287
49ab6098 288 if (count >= HOST_BITS_PER_DOUBLE_INT)
330db1e3
RG
289 {
290 /* Shifting by the host word size is undefined according to the
291 ANSI standard, so we must handle this as a special case. */
292 *hv = 0;
293 *lv = 0;
294 }
295 else if (count >= HOST_BITS_PER_WIDE_INT)
296 {
477fcae3
RG
297 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
298 *lv = 0;
330db1e3
RG
299 }
300 else
301 {
477fcae3
RG
302 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
303 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
304 *lv = l1 << count;
330db1e3
RG
305 }
306
477fcae3 307 /* Sign extend all bits that are beyond the precision. */
330db1e3 308
477fcae3
RG
309 signmask = -((prec > HOST_BITS_PER_WIDE_INT
310 ? ((unsigned HOST_WIDE_INT) *hv
311 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
312 : (*lv >> (prec - 1))) & 1);
313
49ab6098 314 if (prec >= HOST_BITS_PER_DOUBLE_INT)
330db1e3 315 ;
477fcae3 316 else if (prec >= HOST_BITS_PER_WIDE_INT)
330db1e3 317 {
477fcae3
RG
318 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
319 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
330db1e3
RG
320 }
321 else
322 {
323 *hv = signmask;
477fcae3
RG
324 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
325 *lv |= signmask << prec;
330db1e3
RG
326 }
327}
328
330db1e3
RG
329/* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
330 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
331 CODE is a tree code for a kind of division, one of
332 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
333 or EXACT_DIV_EXPR
334 It controls how the quotient is rounded to an integer.
335 Return nonzero if the operation overflows.
336 UNS nonzero says do unsigned division. */
337
338int
339div_and_round_double (unsigned code, int uns,
340 /* num == numerator == dividend */
341 unsigned HOST_WIDE_INT lnum_orig,
342 HOST_WIDE_INT hnum_orig,
343 /* den == denominator == divisor */
344 unsigned HOST_WIDE_INT lden_orig,
345 HOST_WIDE_INT hden_orig,
346 unsigned HOST_WIDE_INT *lquo,
347 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
348 HOST_WIDE_INT *hrem)
349{
350 int quo_neg = 0;
351 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
352 HOST_WIDE_INT den[4], quo[4];
353 int i, j;
354 unsigned HOST_WIDE_INT work;
355 unsigned HOST_WIDE_INT carry = 0;
356 unsigned HOST_WIDE_INT lnum = lnum_orig;
357 HOST_WIDE_INT hnum = hnum_orig;
358 unsigned HOST_WIDE_INT lden = lden_orig;
359 HOST_WIDE_INT hden = hden_orig;
360 int overflow = 0;
361
362 if (hden == 0 && lden == 0)
363 overflow = 1, lden = 1;
364
365 /* Calculate quotient sign and convert operands to unsigned. */
366 if (!uns)
367 {
368 if (hnum < 0)
369 {
370 quo_neg = ~ quo_neg;
371 /* (minimum integer) / (-1) is the only overflow case. */
372 if (neg_double (lnum, hnum, &lnum, &hnum)
373 && ((HOST_WIDE_INT) lden & hden) == -1)
374 overflow = 1;
375 }
376 if (hden < 0)
377 {
378 quo_neg = ~ quo_neg;
379 neg_double (lden, hden, &lden, &hden);
380 }
381 }
382
383 if (hnum == 0 && hden == 0)
384 { /* single precision */
385 *hquo = *hrem = 0;
386 /* This unsigned division rounds toward zero. */
387 *lquo = lnum / lden;
388 goto finish_up;
389 }
390
391 if (hnum == 0)
392 { /* trivial case: dividend < divisor */
393 /* hden != 0 already checked. */
394 *hquo = *lquo = 0;
395 *hrem = hnum;
396 *lrem = lnum;
397 goto finish_up;
398 }
399
400 memset (quo, 0, sizeof quo);
401
402 memset (num, 0, sizeof num); /* to zero 9th element */
403 memset (den, 0, sizeof den);
404
405 encode (num, lnum, hnum);
406 encode (den, lden, hden);
407
408 /* Special code for when the divisor < BASE. */
409 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
410 {
411 /* hnum != 0 already checked. */
412 for (i = 4 - 1; i >= 0; i--)
413 {
414 work = num[i] + carry * BASE;
415 quo[i] = work / lden;
416 carry = work % lden;
417 }
418 }
419 else
420 {
421 /* Full double precision division,
422 with thanks to Don Knuth's "Seminumerical Algorithms". */
423 int num_hi_sig, den_hi_sig;
424 unsigned HOST_WIDE_INT quo_est, scale;
425
426 /* Find the highest nonzero divisor digit. */
427 for (i = 4 - 1;; i--)
428 if (den[i] != 0)
429 {
430 den_hi_sig = i;
431 break;
432 }
433
434 /* Insure that the first digit of the divisor is at least BASE/2.
435 This is required by the quotient digit estimation algorithm. */
436
437 scale = BASE / (den[den_hi_sig] + 1);
438 if (scale > 1)
439 { /* scale divisor and dividend */
440 carry = 0;
441 for (i = 0; i <= 4 - 1; i++)
442 {
443 work = (num[i] * scale) + carry;
444 num[i] = LOWPART (work);
445 carry = HIGHPART (work);
446 }
447
448 num[4] = carry;
449 carry = 0;
450 for (i = 0; i <= 4 - 1; i++)
451 {
452 work = (den[i] * scale) + carry;
453 den[i] = LOWPART (work);
454 carry = HIGHPART (work);
455 if (den[i] != 0) den_hi_sig = i;
456 }
457 }
458
459 num_hi_sig = 4;
460
461 /* Main loop */
462 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
463 {
464 /* Guess the next quotient digit, quo_est, by dividing the first
465 two remaining dividend digits by the high order quotient digit.
466 quo_est is never low and is at most 2 high. */
467 unsigned HOST_WIDE_INT tmp;
468
469 num_hi_sig = i + den_hi_sig + 1;
470 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
471 if (num[num_hi_sig] != den[den_hi_sig])
472 quo_est = work / den[den_hi_sig];
473 else
474 quo_est = BASE - 1;
475
476 /* Refine quo_est so it's usually correct, and at most one high. */
477 tmp = work - quo_est * den[den_hi_sig];
478 if (tmp < BASE
479 && (den[den_hi_sig - 1] * quo_est
480 > (tmp * BASE + num[num_hi_sig - 2])))
481 quo_est--;
482
483 /* Try QUO_EST as the quotient digit, by multiplying the
484 divisor by QUO_EST and subtracting from the remaining dividend.
485 Keep in mind that QUO_EST is the I - 1st digit. */
486
487 carry = 0;
488 for (j = 0; j <= den_hi_sig; j++)
489 {
490 work = quo_est * den[j] + carry;
491 carry = HIGHPART (work);
492 work = num[i + j] - LOWPART (work);
493 num[i + j] = LOWPART (work);
494 carry += HIGHPART (work) != 0;
495 }
496
497 /* If quo_est was high by one, then num[i] went negative and
498 we need to correct things. */
499 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
500 {
501 quo_est--;
502 carry = 0; /* add divisor back in */
503 for (j = 0; j <= den_hi_sig; j++)
504 {
505 work = num[i + j] + den[j] + carry;
506 carry = HIGHPART (work);
507 num[i + j] = LOWPART (work);
508 }
509
510 num [num_hi_sig] += carry;
511 }
512
513 /* Store the quotient digit. */
514 quo[i] = quo_est;
515 }
516 }
517
518 decode (quo, lquo, hquo);
519
520 finish_up:
521 /* If result is negative, make it so. */
522 if (quo_neg)
523 neg_double (*lquo, *hquo, lquo, hquo);
524
525 /* Compute trial remainder: rem = num - (quo * den) */
526 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
527 neg_double (*lrem, *hrem, lrem, hrem);
528 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
529
530 switch (code)
531 {
532 case TRUNC_DIV_EXPR:
533 case TRUNC_MOD_EXPR: /* round toward zero */
534 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
535 return overflow;
536
537 case FLOOR_DIV_EXPR:
538 case FLOOR_MOD_EXPR: /* round toward negative infinity */
539 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
540 {
541 /* quo = quo - 1; */
542 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
543 lquo, hquo);
544 }
545 else
546 return overflow;
547 break;
548
549 case CEIL_DIV_EXPR:
550 case CEIL_MOD_EXPR: /* round toward positive infinity */
551 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
552 {
553 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
554 lquo, hquo);
555 }
556 else
557 return overflow;
558 break;
559
560 case ROUND_DIV_EXPR:
561 case ROUND_MOD_EXPR: /* round to closest integer */
562 {
563 unsigned HOST_WIDE_INT labs_rem = *lrem;
564 HOST_WIDE_INT habs_rem = *hrem;
565 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
566 HOST_WIDE_INT habs_den = hden, htwice;
567
568 /* Get absolute values. */
569 if (*hrem < 0)
570 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
571 if (hden < 0)
572 neg_double (lden, hden, &labs_den, &habs_den);
573
574 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
575 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
576 labs_rem, habs_rem, &ltwice, &htwice);
577
578 if (((unsigned HOST_WIDE_INT) habs_den
579 < (unsigned HOST_WIDE_INT) htwice)
580 || (((unsigned HOST_WIDE_INT) habs_den
581 == (unsigned HOST_WIDE_INT) htwice)
582 && (labs_den <= ltwice)))
583 {
584 if (*hquo < 0)
585 /* quo = quo - 1; */
586 add_double (*lquo, *hquo,
587 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
588 else
589 /* quo = quo + 1; */
590 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
591 lquo, hquo);
592 }
593 else
594 return overflow;
595 }
596 break;
597
598 default:
599 gcc_unreachable ();
600 }
601
602 /* Compute true remainder: rem = num - (quo * den) */
603 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
604 neg_double (*lrem, *hrem, lrem, hrem);
605 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
606 return overflow;
607}
608
609
f82783bd
ZD
610/* Returns mask for PREC bits. */
611
b3ce5b6e 612double_int
0823efed 613double_int::mask (unsigned prec)
f82783bd
ZD
614{
615 unsigned HOST_WIDE_INT m;
616 double_int mask;
617
618 if (prec > HOST_BITS_PER_WIDE_INT)
619 {
620 prec -= HOST_BITS_PER_WIDE_INT;
621 m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
622 mask.high = (HOST_WIDE_INT) m;
623 mask.low = ALL_ONES;
624 }
625 else
626 {
627 mask.high = 0;
eb87c7c4 628 mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0;
f82783bd
ZD
629 }
630
631 return mask;
632}
633
0ea62d93
JJ
634/* Returns a maximum value for signed or unsigned integer
635 of precision PREC. */
636
637double_int
0823efed 638double_int::max_value (unsigned int prec, bool uns)
0ea62d93 639{
0823efed 640 return double_int::mask (prec - (uns ? 0 : 1));
0ea62d93
JJ
641}
642
643/* Returns a minimum value for signed or unsigned integer
644 of precision PREC. */
645
646double_int
0823efed 647double_int::min_value (unsigned int prec, bool uns)
0ea62d93
JJ
648{
649 if (uns)
650 return double_int_zero;
0823efed 651 return double_int_one.lshift (prec - 1, prec, false);
0ea62d93
JJ
652}
653
f82783bd
ZD
654/* Clears the bits of CST over the precision PREC. If UNS is false, the bits
655 outside of the precision are set to the sign bit (i.e., the PREC-th one),
656 otherwise they are set to zero.
b8698a0f 657
f82783bd
ZD
658 This corresponds to returning the value represented by PREC lowermost bits
659 of CST, with the given signedness. */
660
661double_int
0823efed 662double_int::ext (unsigned prec, bool uns) const
f82783bd
ZD
663{
664 if (uns)
0823efed 665 return this->zext (prec);
f82783bd 666 else
0823efed 667 return this->sext (prec);
f82783bd
ZD
668}
669
0823efed 670/* The same as double_int::ext with UNS = true. */
f82783bd
ZD
671
672double_int
0823efed 673double_int::zext (unsigned prec) const
f82783bd 674{
0823efed
DN
675 const double_int &cst = *this;
676 double_int mask = double_int::mask (prec);
f82783bd
ZD
677 double_int r;
678
0ad1d5a1
ZD
679 r.low = cst.low & mask.low;
680 r.high = cst.high & mask.high;
f82783bd
ZD
681
682 return r;
683}
684
0823efed 685/* The same as double_int::ext with UNS = false. */
f82783bd
ZD
686
687double_int
0823efed 688double_int::sext (unsigned prec) const
f82783bd 689{
0823efed
DN
690 const double_int &cst = *this;
691 double_int mask = double_int::mask (prec);
f82783bd
ZD
692 double_int r;
693 unsigned HOST_WIDE_INT snum;
694
695 if (prec <= HOST_BITS_PER_WIDE_INT)
696 snum = cst.low;
697 else
698 {
699 prec -= HOST_BITS_PER_WIDE_INT;
700 snum = (unsigned HOST_WIDE_INT) cst.high;
701 }
702 if (((snum >> (prec - 1)) & 1) == 1)
703 {
0ad1d5a1
ZD
704 r.low = cst.low | ~mask.low;
705 r.high = cst.high | ~mask.high;
f82783bd
ZD
706 }
707 else
708 {
0ad1d5a1
ZD
709 r.low = cst.low & mask.low;
710 r.high = cst.high & mask.high;
b8698a0f 711 }
f82783bd
ZD
712
713 return r;
714}
715
f82783bd
ZD
716/* Returns true if CST fits in signed HOST_WIDE_INT. */
717
718bool
0823efed 719double_int::fits_signed () const
f82783bd 720{
0823efed 721 const double_int &cst = *this;
f82783bd
ZD
722 if (cst.high == 0)
723 return (HOST_WIDE_INT) cst.low >= 0;
724 else if (cst.high == -1)
725 return (HOST_WIDE_INT) cst.low < 0;
726 else
727 return false;
728}
729
730/* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
731 unsigned HOST_WIDE_INT if UNS is true. */
732
733bool
0823efed 734double_int::fits (bool uns) const
f82783bd
ZD
735{
736 if (uns)
0823efed 737 return this->fits_unsigned ();
f82783bd 738 else
0823efed 739 return this->fits_signed ();
f82783bd
ZD
740}
741
f82783bd
ZD
742/* Returns A * B. */
743
744double_int
0823efed 745double_int::operator * (double_int b) const
f82783bd 746{
0823efed 747 const double_int &a = *this;
f82783bd
ZD
748 double_int ret;
749 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
750 return ret;
751}
752
b3a50850
XDL
753/* Returns A * B. If the operation overflows according to UNSIGNED_P,
754 *OVERFLOW is set to nonzero. */
755
756double_int
0823efed 757double_int::mul_with_sign (double_int b, bool unsigned_p, int *overflow) const
b3a50850 758{
0823efed 759 const double_int &a = *this;
b3a50850
XDL
760 double_int ret;
761 *overflow = mul_double_with_sign (a.low, a.high, b.low, b.high,
762 &ret.low, &ret.high, unsigned_p);
763 return ret;
764}
765
f82783bd
ZD
766/* Returns A + B. */
767
768double_int
0823efed 769double_int::operator + (double_int b) const
f82783bd 770{
0823efed 771 const double_int &a = *this;
f82783bd
ZD
772 double_int ret;
773 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
774 return ret;
775}
776
bdc45386
RB
777/* Returns A - B. */
778
779double_int
0823efed 780double_int::operator - (double_int b) const
bdc45386 781{
0823efed 782 const double_int &a = *this;
bdc45386
RB
783 double_int ret;
784 neg_double (b.low, b.high, &b.low, &b.high);
785 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
786 return ret;
787}
788
f82783bd
ZD
789/* Returns -A. */
790
791double_int
0823efed 792double_int::operator - () const
f82783bd 793{
0823efed 794 const double_int &a = *this;
f82783bd
ZD
795 double_int ret;
796 neg_double (a.low, a.high, &ret.low, &ret.high);
797 return ret;
798}
799
800/* Returns A / B (computed as unsigned depending on UNS, and rounded as
801 specified by CODE). CODE is enum tree_code in fact, but double_int.h
f414f2f3
ZD
802 must be included before tree.h. The remainder after the division is
803 stored to MOD. */
f82783bd
ZD
804
805double_int
0823efed
DN
806double_int::divmod (double_int b, bool uns, unsigned code,
807 double_int *mod) const
f82783bd 808{
0823efed 809 const double_int &a = *this;
f82783bd
ZD
810 double_int ret;
811
330db1e3 812 div_and_round_double (code, uns, a.low, a.high,
bbbbb16a
ILT
813 b.low, b.high, &ret.low, &ret.high,
814 &mod->low, &mod->high);
f82783bd
ZD
815 return ret;
816}
817
0823efed 818/* The same as double_int::divmod with UNS = false. */
f414f2f3
ZD
819
820double_int
0823efed 821double_int::sdivmod (double_int b, unsigned code, double_int *mod) const
f414f2f3 822{
0823efed 823 return this->divmod (b, false, code, mod);
f414f2f3
ZD
824}
825
0823efed 826/* The same as double_int::divmod with UNS = true. */
f414f2f3
ZD
827
828double_int
0823efed 829double_int::udivmod (double_int b, unsigned code, double_int *mod) const
f414f2f3 830{
0823efed 831 return this->divmod (b, true, code, mod);
f414f2f3
ZD
832}
833
834/* Returns A / B (computed as unsigned depending on UNS, and rounded as
835 specified by CODE). CODE is enum tree_code in fact, but double_int.h
836 must be included before tree.h. */
837
838double_int
0823efed 839double_int::div (double_int b, bool uns, unsigned code) const
f414f2f3
ZD
840{
841 double_int mod;
842
0823efed 843 return this->divmod (b, uns, code, &mod);
f414f2f3
ZD
844}
845
0823efed 846/* The same as double_int::div with UNS = false. */
f82783bd
ZD
847
848double_int
0823efed 849double_int::sdiv (double_int b, unsigned code) const
f82783bd 850{
0823efed 851 return this->div (b, false, code);
f82783bd
ZD
852}
853
0823efed 854/* The same as double_int::div with UNS = true. */
f82783bd
ZD
855
856double_int
0823efed 857double_int::udiv (double_int b, unsigned code) const
f82783bd 858{
0823efed 859 return this->div (b, true, code);
f82783bd
ZD
860}
861
f414f2f3
ZD
862/* Returns A % B (computed as unsigned depending on UNS, and rounded as
863 specified by CODE). CODE is enum tree_code in fact, but double_int.h
864 must be included before tree.h. */
865
866double_int
0823efed 867double_int::mod (double_int b, bool uns, unsigned code) const
f414f2f3
ZD
868{
869 double_int mod;
870
0823efed 871 this->divmod (b, uns, code, &mod);
f414f2f3
ZD
872 return mod;
873}
874
0823efed 875/* The same as double_int::mod with UNS = false. */
f414f2f3
ZD
876
877double_int
0823efed 878double_int::smod (double_int b, unsigned code) const
f414f2f3 879{
0823efed 880 return this->mod (b, false, code);
f414f2f3
ZD
881}
882
0823efed 883/* The same as double_int::mod with UNS = true. */
f414f2f3
ZD
884
885double_int
0823efed 886double_int::umod (double_int b, unsigned code) const
f414f2f3 887{
0823efed 888 return this->mod (b, true, code);
f414f2f3
ZD
889}
890
7735d6c7
BS
891/* Return TRUE iff PRODUCT is an integral multiple of FACTOR, and return
892 the multiple in *MULTIPLE. Otherwise return FALSE and leave *MULTIPLE
893 unchanged. */
894
895bool
0823efed
DN
896double_int::multiple_of (double_int factor,
897 bool unsigned_p, double_int *multiple) const
7735d6c7
BS
898{
899 double_int remainder;
0823efed 900 double_int quotient = this->divmod (factor, unsigned_p,
7735d6c7 901 TRUNC_DIV_EXPR, &remainder);
0823efed 902 if (remainder.is_zero ())
7735d6c7
BS
903 {
904 *multiple = quotient;
905 return true;
906 }
907
908 return false;
909}
910
54fb1ae0
AS
911/* Set BITPOS bit in A. */
912double_int
0823efed 913double_int::set_bit (unsigned bitpos) const
54fb1ae0 914{
0823efed 915 double_int a = *this;
54fb1ae0
AS
916 if (bitpos < HOST_BITS_PER_WIDE_INT)
917 a.low |= (unsigned HOST_WIDE_INT) 1 << bitpos;
918 else
919 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
920
921 return a;
922}
923
552cbe64
RG
924/* Count trailing zeros in A. */
925int
0823efed 926double_int::trailing_zeros () const
552cbe64 927{
0823efed 928 const double_int &a = *this;
552cbe64
RG
929 unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
930 unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
931 if (!w)
932 return HOST_BITS_PER_DOUBLE_INT;
bd95721f 933 bits += ctz_hwi (w);
552cbe64
RG
934 return bits;
935}
936
2bd1333d
AS
937/* Shift A left by COUNT places keeping only PREC bits of result. Shift
938 right if COUNT is negative. ARITH true specifies arithmetic shifting;
939 otherwise use logical shift. */
940
941double_int
0823efed 942double_int::lshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
2bd1333d 943{
0823efed 944 const double_int &a = *this;
2bd1333d
AS
945 double_int ret;
946 lshift_double (a.low, a.high, count, prec, &ret.low, &ret.high, arith);
947 return ret;
948}
949
0823efed 950/* Shift A right by COUNT places keeping only PREC bits of result. Shift
2bd1333d
AS
951 left if COUNT is negative. ARITH true specifies arithmetic shifting;
952 otherwise use logical shift. */
953
954double_int
0823efed 955double_int::rshift (HOST_WIDE_INT count, unsigned int prec, bool arith) const
2bd1333d 956{
0823efed 957 const double_int &a = *this;
2bd1333d 958 double_int ret;
477fcae3 959 lshift_double (a.low, a.high, -count, prec, &ret.low, &ret.high, arith);
2bd1333d
AS
960 return ret;
961}
962
0823efed
DN
963/* Arithmetic shift A left by COUNT places keeping only PREC bits of result.
964 Shift right if COUNT is negative. */
965
966double_int
967double_int::alshift (HOST_WIDE_INT count, unsigned int prec) const
968{
969 double_int r;
970 lshift_double (low, high, count, prec, &r.low, &r.high, true);
971 return r;
972}
973
974/* Arithmetic shift A right by COUNT places keeping only PREC bits of result.
975 Shift left if COUNT is negative. */
976
977double_int
978double_int::arshift (HOST_WIDE_INT count, unsigned int prec) const
979{
980 double_int r;
981 lshift_double (low, high, -count, prec, &r.low, &r.high, true);
982 return r;
983}
984
985/* Logical shift A left by COUNT places keeping only PREC bits of result.
986 Shift right if COUNT is negative. */
987
988double_int
989double_int::llshift (HOST_WIDE_INT count, unsigned int prec) const
990{
991 double_int r;
992 lshift_double (low, high, count, prec, &r.low, &r.high, false);
993 return r;
994}
995
996/* Logical shift A right by COUNT places keeping only PREC bits of result.
997 Shift left if COUNT is negative. */
998
999double_int
1000double_int::lrshift (HOST_WIDE_INT count, unsigned int prec) const
1001{
1002 double_int r;
1003 lshift_double (low, high, -count, prec, &r.low, &r.high, false);
1004 return r;
1005}
1006
fd7de64c
AS
1007/* Rotate A left by COUNT places keeping only PREC bits of result.
1008 Rotate right if COUNT is negative. */
1009
1010double_int
0823efed 1011double_int::lrotate (HOST_WIDE_INT count, unsigned int prec) const
fd7de64c
AS
1012{
1013 double_int t1, t2;
1014
1015 count %= prec;
1016 if (count < 0)
1017 count += prec;
1018
0823efed
DN
1019 t1 = this->lshift (count, prec, false);
1020 t2 = this->rshift (prec - count, prec, false);
fd7de64c 1021
0823efed 1022 return t1 | t2;
fd7de64c
AS
1023}
1024
1025/* Rotate A rigth by COUNT places keeping only PREC bits of result.
1026 Rotate right if COUNT is negative. */
1027
1028double_int
0823efed 1029double_int::rrotate (HOST_WIDE_INT count, unsigned int prec) const
fd7de64c
AS
1030{
1031 double_int t1, t2;
1032
1033 count %= prec;
1034 if (count < 0)
1035 count += prec;
1036
0823efed
DN
1037 t1 = this->rshift (count, prec, false);
1038 t2 = this->lshift (prec - count, prec, false);
fd7de64c 1039
0823efed 1040 return t1 | t2;
fd7de64c
AS
1041}
1042
f82783bd
ZD
1043/* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1044 comparison is given by UNS. */
1045
1046int
0823efed 1047double_int::cmp (double_int b, bool uns) const
f82783bd
ZD
1048{
1049 if (uns)
0823efed 1050 return this->ucmp (b);
f82783bd 1051 else
0823efed 1052 return this->scmp (b);
f82783bd
ZD
1053}
1054
1055/* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
1056 and 1 if A > B. */
1057
1058int
0823efed 1059double_int::ucmp (double_int b) const
f82783bd 1060{
0823efed 1061 const double_int &a = *this;
f82783bd
ZD
1062 if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
1063 return -1;
1064 if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
1065 return 1;
1066 if (a.low < b.low)
1067 return -1;
1068 if (a.low > b.low)
1069 return 1;
1070
1071 return 0;
1072}
1073
1074/* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
1075 and 1 if A > B. */
1076
1077int
0823efed 1078double_int::scmp (double_int b) const
f82783bd 1079{
0823efed 1080 const double_int &a = *this;
f82783bd
ZD
1081 if (a.high < b.high)
1082 return -1;
1083 if (a.high > b.high)
1084 return 1;
1e1ba002 1085 if (a.low < b.low)
f82783bd 1086 return -1;
1e1ba002 1087 if (a.low > b.low)
f82783bd
ZD
1088 return 1;
1089
1090 return 0;
1091}
1092
0823efed
DN
1093/* Compares two unsigned values A and B for less-than. */
1094
1095bool
1096double_int::ult (double_int b) const
1097{
1098 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1099 return true;
1100 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1101 return false;
1102 if (low < b.low)
1103 return true;
1104 return false;
1105}
1106
1107/* Compares two unsigned values A and B for greater-than. */
1108
1109bool
1110double_int::ugt (double_int b) const
1111{
1112 if ((unsigned HOST_WIDE_INT) high > (unsigned HOST_WIDE_INT) b.high)
1113 return true;
1114 if ((unsigned HOST_WIDE_INT) high < (unsigned HOST_WIDE_INT) b.high)
1115 return false;
1116 if (low > b.low)
1117 return true;
1118 return false;
1119}
1120
1121/* Compares two signed values A and B for less-than. */
1122
1123bool
1124double_int::slt (double_int b) const
1125{
1126 if (high < b.high)
1127 return true;
1128 if (high > b.high)
1129 return false;
1130 if (low < b.low)
1131 return true;
1132 return false;
1133}
1134
1135/* Compares two signed values A and B for greater-than. */
1136
1137bool
1138double_int::sgt (double_int b) const
1139{
1140 if (high > b.high)
1141 return true;
1142 if (high < b.high)
1143 return false;
1144 if (low > b.low)
1145 return true;
1146 return false;
1147}
1148
1149
fd7de64c
AS
1150/* Compares two values A and B. Returns max value. Signedness of the
1151 comparison is given by UNS. */
1152
1153double_int
0823efed 1154double_int::max (double_int b, bool uns)
fd7de64c 1155{
0823efed 1156 return (this->cmp (b, uns) == 1) ? *this : b;
fd7de64c
AS
1157}
1158
1159/* Compares two signed values A and B. Returns max value. */
1160
0823efed
DN
1161double_int
1162double_int::smax (double_int b)
fd7de64c 1163{
0823efed 1164 return (this->scmp (b) == 1) ? *this : b;
fd7de64c
AS
1165}
1166
1167/* Compares two unsigned values A and B. Returns max value. */
1168
0823efed
DN
1169double_int
1170double_int::umax (double_int b)
fd7de64c 1171{
0823efed 1172 return (this->ucmp (b) == 1) ? *this : b;
fd7de64c
AS
1173}
1174
1175/* Compares two values A and B. Returns mix value. Signedness of the
1176 comparison is given by UNS. */
1177
0823efed
DN
1178double_int
1179double_int::min (double_int b, bool uns)
fd7de64c 1180{
0823efed 1181 return (this->cmp (b, uns) == -1) ? *this : b;
fd7de64c
AS
1182}
1183
1184/* Compares two signed values A and B. Returns min value. */
1185
0823efed
DN
1186double_int
1187double_int::smin (double_int b)
fd7de64c 1188{
0823efed 1189 return (this->scmp (b) == -1) ? *this : b;
fd7de64c
AS
1190}
1191
1192/* Compares two unsigned values A and B. Returns min value. */
1193
0823efed
DN
1194double_int
1195double_int::umin (double_int b)
fd7de64c 1196{
0823efed 1197 return (this->ucmp (b) == -1) ? *this : b;
fd7de64c
AS
1198}
1199
f82783bd
ZD
1200/* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1201
1202static unsigned
1203double_int_split_digit (double_int *cst, unsigned base)
1204{
1205 unsigned HOST_WIDE_INT resl, reml;
1206 HOST_WIDE_INT resh, remh;
1207
1208 div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
1209 &resl, &resh, &reml, &remh);
1210 cst->high = resh;
1211 cst->low = resl;
1212
1213 return reml;
1214}
1215
1216/* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
1217 otherwise it is signed. */
1218
1219void
1220dump_double_int (FILE *file, double_int cst, bool uns)
1221{
1222 unsigned digits[100], n;
1223 int i;
1224
0823efed 1225 if (cst.is_zero ())
f82783bd
ZD
1226 {
1227 fprintf (file, "0");
1228 return;
1229 }
1230
0823efed 1231 if (!uns && cst.is_negative ())
f82783bd
ZD
1232 {
1233 fprintf (file, "-");
0823efed 1234 cst = -cst;
f82783bd
ZD
1235 }
1236
0823efed 1237 for (n = 0; !cst.is_zero (); n++)
f82783bd
ZD
1238 digits[n] = double_int_split_digit (&cst, 10);
1239 for (i = n - 1; i >= 0; i--)
1240 fprintf (file, "%u", digits[i]);
1241}
e4fd22c6
BM
1242
1243
1244/* Sets RESULT to VAL, taken unsigned if UNS is true and as signed
1245 otherwise. */
1246
1247void
1248mpz_set_double_int (mpz_t result, double_int val, bool uns)
1249{
1250 bool negate = false;
1251 unsigned HOST_WIDE_INT vp[2];
1252
0823efed 1253 if (!uns && val.is_negative ())
e4fd22c6
BM
1254 {
1255 negate = true;
0823efed 1256 val = -val;
e4fd22c6
BM
1257 }
1258
1259 vp[0] = val.low;
1260 vp[1] = (unsigned HOST_WIDE_INT) val.high;
1261 mpz_import (result, 2, -1, sizeof (HOST_WIDE_INT), 0, 0, vp);
1262
1263 if (negate)
1264 mpz_neg (result, result);
1265}
1266
1267/* Returns VAL converted to TYPE. If WRAP is true, then out-of-range
1268 values of VAL will be wrapped; otherwise, they will be set to the
1269 appropriate minimum or maximum TYPE bound. */
1270
1271double_int
22ea9ec0 1272mpz_get_double_int (const_tree type, mpz_t val, bool wrap)
e4fd22c6
BM
1273{
1274 unsigned HOST_WIDE_INT *vp;
1275 size_t count, numb;
1276 double_int res;
1277
1278 if (!wrap)
b8698a0f 1279 {
e4fd22c6
BM
1280 mpz_t min, max;
1281
1282 mpz_init (min);
1283 mpz_init (max);
1284 get_type_static_bounds (type, min, max);
1285
1286 if (mpz_cmp (val, min) < 0)
1287 mpz_set (val, min);
1288 else if (mpz_cmp (val, max) > 0)
1289 mpz_set (val, max);
1290
1291 mpz_clear (min);
1292 mpz_clear (max);
1293 }
1294
1295 /* Determine the number of unsigned HOST_WIDE_INT that are required
1296 for representing the value. The code to calculate count is
1297 extracted from the GMP manual, section "Integer Import and Export":
1298 http://gmplib.org/manual/Integer-Import-and-Export.html */
1299 numb = 8*sizeof(HOST_WIDE_INT);
1300 count = (mpz_sizeinbase (val, 2) + numb-1) / numb;
1301 if (count < 2)
1302 count = 2;
1303 vp = (unsigned HOST_WIDE_INT *) alloca (count * sizeof(HOST_WIDE_INT));
1304
1305 vp[0] = 0;
1306 vp[1] = 0;
1307 mpz_export (vp, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, val);
1308
1309 gcc_assert (wrap || count <= 2);
1310
1311 res.low = vp[0];
1312 res.high = (HOST_WIDE_INT) vp[1];
1313
0823efed 1314 res = res.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
e4fd22c6 1315 if (mpz_sgn (val) < 0)
0823efed 1316 res = -res;
e4fd22c6
BM
1317
1318 return res;
1319}