comparison gcc/double-int.c @ 16:04ced10e8804

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