1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* Operations with long integers.
Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
/* Returns mask for PREC bits. */
static inline double_int
double_int_mask (unsigned prec)
{
unsigned HOST_WIDE_INT m;
double_int mask;
if (prec > HOST_BITS_PER_WIDE_INT)
{
prec -= HOST_BITS_PER_WIDE_INT;
m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
mask.high = (HOST_WIDE_INT) m;
mask.low = ALL_ONES;
}
else
{
mask.high = 0;
mask.low = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;
}
return mask;
}
/* Clears the bits of CST over the precision PREC. If UNS is false, the bits
outside of the precision are set to the sign bit (i.e., the PREC-th one),
otherwise they are set to zero.
This corresponds to returning the value represented by PREC lowermost bits
of CST, with the given signedness. */
double_int
double_int_ext (double_int cst, unsigned prec, bool uns)
{
if (uns)
return double_int_zext (cst, prec);
else
return double_int_sext (cst, prec);
}
/* The same as double_int_ext with UNS = true. */
double_int
double_int_zext (double_int cst, unsigned prec)
{
double_int mask = double_int_mask (prec);
double_int r;
r.low = cst.low & mask.low;
r.high = cst.high & mask.high;
return r;
}
/* The same as double_int_ext with UNS = false. */
double_int
double_int_sext (double_int cst, unsigned prec)
{
double_int mask = double_int_mask (prec);
double_int r;
unsigned HOST_WIDE_INT snum;
if (prec <= HOST_BITS_PER_WIDE_INT)
snum = cst.low;
else
{
prec -= HOST_BITS_PER_WIDE_INT;
snum = (unsigned HOST_WIDE_INT) cst.high;
}
if (((snum >> (prec - 1)) & 1) == 1)
{
r.low = cst.low | ~mask.low;
r.high = cst.high | ~mask.high;
}
else
{
r.low = cst.low & mask.low;
r.high = cst.high & mask.high;
}
return r;
}
/* Constructs long integer from tree CST. The extra bits over the precision of
the number are filled with sign bit if CST is signed, and with zeros if it
is unsigned. */
double_int
tree_to_double_int (tree cst)
{
/* We do not need to call double_int_restrict here to ensure the semantics as
described, as this is the default one for trees. */
return TREE_INT_CST (cst);
}
/* Returns true if CST fits in unsigned HOST_WIDE_INT. */
bool
double_int_fits_in_uhwi_p (double_int cst)
{
return cst.high == 0;
}
/* Returns true if CST fits in signed HOST_WIDE_INT. */
bool
double_int_fits_in_shwi_p (double_int cst)
{
if (cst.high == 0)
return (HOST_WIDE_INT) cst.low >= 0;
else if (cst.high == -1)
return (HOST_WIDE_INT) cst.low < 0;
else
return false;
}
/* Returns true if CST fits in HOST_WIDE_INT if UNS is false, or in
unsigned HOST_WIDE_INT if UNS is true. */
bool
double_int_fits_in_hwi_p (double_int cst, bool uns)
{
if (uns)
return double_int_fits_in_uhwi_p (cst);
else
return double_int_fits_in_shwi_p (cst);
}
/* Returns value of CST as a signed number. CST must satisfy
double_int_fits_in_shwi_p. */
HOST_WIDE_INT
double_int_to_shwi (double_int cst)
{
return (HOST_WIDE_INT) cst.low;
}
/* Returns value of CST as an unsigned number. CST must satisfy
double_int_fits_in_uhwi_p. */
unsigned HOST_WIDE_INT
double_int_to_uhwi (double_int cst)
{
return cst.low;
}
/* Returns A * B. */
double_int
double_int_mul (double_int a, double_int b)
{
double_int ret;
mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
return ret;
}
/* Returns A + B. */
double_int
double_int_add (double_int a, double_int b)
{
double_int ret;
add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
return ret;
}
/* Returns -A. */
double_int
double_int_neg (double_int a)
{
double_int ret;
neg_double (a.low, a.high, &ret.low, &ret.high);
return ret;
}
/* Returns A / B (computed as unsigned depending on UNS, and rounded as
specified by CODE). CODE is enum tree_code in fact, but double_int.h
must be included before tree.h. */
double_int
double_int_div (double_int a, double_int b, bool uns, unsigned code)
{
unsigned HOST_WIDE_INT rem_lo;
HOST_WIDE_INT rem_hi;
double_int ret;
div_and_round_double (code, uns, a.low, a.high, b.low, b.high,
&ret.low, &ret.high, &rem_lo, &rem_hi);
return ret;
}
/* The same as double_int_div with UNS = false. */
double_int
double_int_sdiv (double_int a, double_int b, unsigned code)
{
return double_int_div (a, b, false, code);
}
/* The same as double_int_div with UNS = true. */
double_int
double_int_udiv (double_int a, double_int b, unsigned code)
{
return double_int_div (a, b, true, code);
}
/* Constructs tree in type TYPE from with value given by CST. */
tree
double_int_to_tree (tree type, double_int cst)
{
cst = double_int_ext (cst, TYPE_PRECISION (type), TYPE_UNSIGNED (type));
return build_int_cst_wide (type, cst.low, cst.high);
}
/* Returns true if CST is negative. Of course, CST is considered to
be signed. */
bool
double_int_negative_p (double_int cst)
{
return cst.high < 0;
}
/* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
comparison is given by UNS. */
int
double_int_cmp (double_int a, double_int b, bool uns)
{
if (uns)
return double_int_ucmp (a, b);
else
return double_int_scmp (a, b);
}
/* Compares two unsigned values A and B. Returns -1 if A < B, 0 if A == B,
and 1 if A > B. */
int
double_int_ucmp (double_int a, double_int b)
{
if ((unsigned HOST_WIDE_INT) a.high < (unsigned HOST_WIDE_INT) b.high)
return -1;
if ((unsigned HOST_WIDE_INT) a.high > (unsigned HOST_WIDE_INT) b.high)
return 1;
if (a.low < b.low)
return -1;
if (a.low > b.low)
return 1;
return 0;
}
/* Compares two signed values A and B. Returns -1 if A < B, 0 if A == B,
and 1 if A > B. */
int
double_int_scmp (double_int a, double_int b)
{
if (a.high < b.high)
return -1;
if (a.high > b.high)
return 1;
if ((HOST_WIDE_INT) a.low < (HOST_WIDE_INT) b.low)
return -1;
if ((HOST_WIDE_INT) a.low > (HOST_WIDE_INT) b.low)
return 1;
return 0;
}
/* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
static unsigned
double_int_split_digit (double_int *cst, unsigned base)
{
unsigned HOST_WIDE_INT resl, reml;
HOST_WIDE_INT resh, remh;
div_and_round_double (FLOOR_DIV_EXPR, true, cst->low, cst->high, base, 0,
&resl, &resh, &reml, &remh);
cst->high = resh;
cst->low = resl;
return reml;
}
/* Dumps CST to FILE. If UNS is true, CST is considered to be unsigned,
otherwise it is signed. */
void
dump_double_int (FILE *file, double_int cst, bool uns)
{
unsigned digits[100], n;
int i;
if (double_int_zero_p (cst))
{
fprintf (file, "0");
return;
}
if (!uns && double_int_negative_p (cst))
{
fprintf (file, "-");
cst = double_int_neg (cst);
}
for (n = 0; !double_int_zero_p (cst); n++)
digits[n] = double_int_split_digit (&cst, 10);
for (i = n - 1; i >= 0; i--)
fprintf (file, "%u", digits[i]);
}
|