re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / wide-int.h
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #ifndef WIDE_INT_H
21 #define WIDE_INT_H
22
23 /* wide-int.[cc|h] implements a class that efficiently performs
24 mathematical operations on finite precision integers. wide_ints
25 are designed to be transient - they are not for long term storage
26 of values. There is tight integration between wide_ints and the
27 other longer storage GCC representations (rtl and tree).
28
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
31
32 1) wide_int (the default). This flavor does the math in the
33 precision of its input arguments. It is assumed (and checked)
34 that the precisions of the operands and results are consistent.
35 This is the most efficient flavor. It is not possible to examine
36 bits above the precision that has been specified. Because of
37 this, the default flavor has semantics that are simple to
38 understand and in general model the underlying hardware that the
39 compiler is targetted for.
40
41 This flavor must be used at the RTL level of gcc because there
42 is, in general, not enough information in the RTL representation
43 to extend a value beyond the precision specified in the mode.
44
45 This flavor should also be used at the TREE and GIMPLE levels of
46 the compiler except for the circumstances described in the
47 descriptions of the other two flavors.
48
49 The default wide_int representation does not contain any
50 information inherent about signedness of the represented value,
51 so it can be used to represent both signed and unsigned numbers.
52 For operations where the results depend on signedness (full width
53 multiply, division, shifts, comparisons, and operations that need
54 overflow detected), the signedness must be specified separately.
55
56 2) offset_int. This is a fixed size representation that is
57 guaranteed to be large enough to compute any bit or byte sized
58 address calculation on the target. Currently the value is 64 + 4
59 bits rounded up to the next number even multiple of
60 HOST_BITS_PER_WIDE_INT (but this can be changed when the first
61 port needs more than 64 bits for the size of a pointer).
62
63 This flavor can be used for all address math on the target. In
64 this representation, the values are sign or zero extended based
65 on their input types to the internal precision. All math is done
66 in this precision and then the values are truncated to fit in the
67 result type. Unlike most gimple or rtl intermediate code, it is
68 not useful to perform the address arithmetic at the same
69 precision in which the operands are represented because there has
70 been no effort by the front ends to convert most addressing
71 arithmetic to canonical types.
72
73 3) widest_int. This representation is an approximation of
74 infinite precision math. However, it is not really infinite
75 precision math as in the GMP library. It is really finite
76 precision math where the precision is 4 times the size of the
77 largest integer that the target port can represent.
78
79 widest_int is supposed to be wider than any number that it needs to
80 store, meaning that there is always at least one leading sign bit.
81 All widest_int values are therefore signed.
82
83 There are several places in the GCC where this should/must be used:
84
85 * Code that does induction variable optimizations. This code
86 works with induction variables of many different types at the
87 same time. Because of this, it ends up doing many different
88 calculations where the operands are not compatible types. The
89 widest_int makes this easy, because it provides a field where
90 nothing is lost when converting from any variable,
91
92 * There are a small number of passes that currently use the
93 widest_int that should use the default. These should be
94 changed.
95
96 There are surprising features of offset_int and widest_int
97 that the users should be careful about:
98
99 1) Shifts and rotations are just weird. You have to specify a
100 precision in which the shift or rotate is to happen in. The bits
101 above this precision are zeroed. While this is what you
102 want, it is clearly non obvious.
103
104 2) Larger precision math sometimes does not produce the same
105 answer as would be expected for doing the math at the proper
106 precision. In particular, a multiply followed by a divide will
107 produce a different answer if the first product is larger than
108 what can be represented in the input precision.
109
110 The offset_int and the widest_int flavors are more expensive
111 than the default wide int, so in addition to the caveats with these
112 two, the default is the prefered representation.
113
114 All three flavors of wide_int are represented as a vector of
115 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
116 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
117 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
118 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
119 in element 0.
120
121 The default wide_int contains three fields: the vector (VAL),
122 the precision and a length (LEN). The length is the number of HWIs
123 needed to represent the value. widest_int and offset_int have a
124 constant precision that cannot be changed, so they only store the
125 VAL and LEN fields.
126
127 Since most integers used in a compiler are small values, it is
128 generally profitable to use a representation of the value that is
129 as small as possible. LEN is used to indicate the number of
130 elements of the vector that are in use. The numbers are stored as
131 sign extended numbers as a means of compression. Leading
132 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
133 as long as they can be reconstructed from the top bit that is being
134 represented.
135
136 The precision and length of a wide_int are always greater than 0.
137 Any bits in a wide_int above the precision are sign-extended from the
138 most significant bit. For example, a 4-bit value 0x8 is represented as
139 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
140 constants to be represented with undefined bits above the precision.
141 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
142 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
143 and in wider precisions.
144
145 There are constructors to create the various forms of wide_int from
146 trees, rtl and constants. For trees you can simply say:
147
148 tree t = ...;
149 wide_int x = t;
150
151 However, a little more syntax is required for rtl constants since
152 they do not have an explicit precision. To make an rtl into a
153 wide_int, you have to pair it with a mode. The canonical way to do
154 this is with std::make_pair as in:
155
156 rtx r = ...
157 wide_int x = std::make_pair (r, mode);
158
159 Similarly, a wide_int can only be constructed from a host value if
160 the target precision is given explicitly, such as in:
161
162 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
163 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
164
165 However, offset_int and widest_int have an inherent precision and so
166 can be initialized directly from a host value:
167
168 offset_int x = (int) c; // sign-extend C
169 widest_int x = (unsigned int) c; // zero-extend C
170
171 It is also possible to do arithmetic directly on trees, rtxes and
172 constants. For example:
173
174 wi::add (t1, t2); // add equal-sized INTEGER_CSTs t1 and t2
175 wi::add (t1, 1); // add 1 to INTEGER_CST t1
176 wi::add (r1, r2); // add equal-sized rtx constants r1 and r2
177 wi::lshift (1, 100); // 1 << 100 as a widest_int
178
179 Many binary operations place restrictions on the combinations of inputs,
180 using the following rules:
181
182 - {tree, rtx, wide_int} op {tree, rtx, wide_int} -> wide_int
183 The inputs must be the same precision. The result is a wide_int
184 of the same precision
185
186 - {tree, rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
187 (un)signed HOST_WIDE_INT op {tree, rtx, wide_int} -> wide_int
188 The HOST_WIDE_INT is extended or truncated to the precision of
189 the other input. The result is a wide_int of the same precision
190 as that input.
191
192 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
193 The inputs are extended to widest_int precision and produce a
194 widest_int result.
195
196 - offset_int op offset_int -> offset_int
197 offset_int op (un)signed HOST_WIDE_INT -> offset_int
198 (un)signed HOST_WIDE_INT op offset_int -> offset_int
199
200 - widest_int op widest_int -> widest_int
201 widest_int op (un)signed HOST_WIDE_INT -> widest_int
202 (un)signed HOST_WIDE_INT op widest_int -> widest_int
203
204 Other combinations like:
205
206 - widest_int op offset_int and
207 - wide_int op offset_int
208
209 are not allowed. The inputs should instead be extended or truncated
210 so that they match.
211
212 The inputs to comparison functions like wi::eq_p and wi::lts_p
213 follow the same compatibility rules, although their return types
214 are different. Unary functions on X produce the same result as
215 a binary operation X + X. Shift functions X op Y also produce
216 the same result as X + X; the precision of the shift amount Y
217 can be arbitrarily different from X. */
218
219 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
220 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
221 can accomodate at least 1 more bit so that unsigned numbers of that
222 mode can be represented as a signed value. Note that it is still
223 possible to create fixed_wide_ints that have precisions greater than
224 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
225 double-width multiplication result, for example. */
226 #define WIDE_INT_MAX_ELTS \
227 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
228
229 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
230
231 /* This is the max size of any pointer on any machine. It does not
232 seem to be as easy to sniff this out of the machine description as
233 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
234 multiple address sizes and may have different address sizes for
235 different address spaces. However, currently the largest pointer
236 on any platform is 64 bits. When that changes, then it is likely
237 that a target hook should be defined so that targets can make this
238 value larger for those targets. */
239 #define ADDR_MAX_BITSIZE 64
240
241 /* This is the internal precision used when doing any address
242 arithmetic. The '4' is really 3 + 1. Three of the bits are for
243 the number of extra bits needed to do bit addresses and the other bit
244 is to allow everything to be signed without loosing any precision.
245 Then everything is rounded up to the next HWI for efficiency. */
246 #define ADDR_MAX_PRECISION \
247 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
248 & ~(HOST_BITS_PER_WIDE_INT - 1))
249
250 /* The number of HWIs needed to store an offset_int. */
251 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
252
253 /* The type of result produced by a binary operation on types T1 and T2.
254 Defined purely for brevity. */
255 #define WI_BINARY_RESULT(T1, T2) \
256 typename wi::binary_traits <T1, T2>::result_type
257
258 /* The type of result produced by a unary operation on type T. */
259 #define WI_UNARY_RESULT(T) \
260 typename wi::unary_traits <T>::result_type
261
262 /* Define a variable RESULT to hold the result of a binary operation on
263 X and Y, which have types T1 and T2 respectively. Define VAL to
264 point to the blocks of RESULT. Once the user of the macro has
265 filled in VAL, it should call RESULT.set_len to set the number
266 of initialized blocks. */
267 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
268 WI_BINARY_RESULT (T1, T2) RESULT = \
269 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
270 HOST_WIDE_INT *VAL = RESULT.write_val ()
271
272 /* Similar for the result of a unary operation on X, which has type T. */
273 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
274 WI_UNARY_RESULT (T) RESULT = \
275 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
276 HOST_WIDE_INT *VAL = RESULT.write_val ()
277
278 template <typename T> class generic_wide_int;
279 template <int N> struct fixed_wide_int_storage;
280 class wide_int_storage;
281
282 /* An N-bit integer. Until we can use typedef templates, use this instead. */
283 #define FIXED_WIDE_INT(N) \
284 generic_wide_int < fixed_wide_int_storage <N> >
285
286 typedef generic_wide_int <wide_int_storage> wide_int;
287 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
288 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
289
290 template <bool SE>
291 struct wide_int_ref_storage;
292
293 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
294
295 /* This can be used instead of wide_int_ref if the referenced value is
296 known to have type T. It carries across properties of T's representation,
297 such as whether excess upper bits in a HWI are defined, and can therefore
298 help avoid redundant work.
299
300 The macro could be replaced with a template typedef, once we're able
301 to use those. */
302 #define WIDE_INT_REF_FOR(T) \
303 generic_wide_int \
304 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
305
306 namespace wi
307 {
308 /* Classifies an integer based on its precision. */
309 enum precision_type {
310 /* The integer has both a precision and defined signedness. This allows
311 the integer to be converted to any width, since we know whether to fill
312 any extra bits with zeros or signs. */
313 FLEXIBLE_PRECISION,
314
315 /* The integer has a variable precision but no defined signedness. */
316 VAR_PRECISION,
317
318 /* The integer has a constant precision (known at GCC compile time)
319 but no defined signedness. */
320 CONST_PRECISION
321 };
322
323 /* This class, which has no default implementation, is expected to
324 provide the following members:
325
326 static const enum precision_type precision_type;
327 Classifies the type of T.
328
329 static const unsigned int precision;
330 Only defined if precision_type == CONST_PRECISION. Specifies the
331 precision of all integers of type T.
332
333 static const bool host_dependent_precision;
334 True if the precision of T depends (or can depend) on the host.
335
336 static unsigned int get_precision (const T &x)
337 Return the number of bits in X.
338
339 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
340 unsigned int precision, const T &x)
341 Decompose X as a PRECISION-bit integer, returning the associated
342 wi::storage_ref. SCRATCH is available as scratch space if needed.
343 The routine should assert that PRECISION is acceptable. */
344 template <typename T> struct int_traits;
345
346 /* This class provides a single type, result_type, which specifies the
347 type of integer produced by a binary operation whose inputs have
348 types T1 and T2. The definition should be symmetric. */
349 template <typename T1, typename T2,
350 enum precision_type P1 = int_traits <T1>::precision_type,
351 enum precision_type P2 = int_traits <T2>::precision_type>
352 struct binary_traits;
353
354 /* The result of a unary operation on T is the same as the result of
355 a binary operation on two values of type T. */
356 template <typename T>
357 struct unary_traits : public binary_traits <T, T> {};
358
359 /* Specify the result type for each supported combination of binary
360 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
361 mixed, in order to give stronger type checking. When both inputs
362 are CONST_PRECISION, they must have the same precision. */
363 template <>
364 template <typename T1, typename T2>
365 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
366 {
367 typedef widest_int result_type;
368 };
369
370 template <>
371 template <typename T1, typename T2>
372 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
373 {
374 typedef wide_int result_type;
375 };
376
377 template <>
378 template <typename T1, typename T2>
379 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
380 {
381 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
382 so as not to confuse gengtype. */
383 typedef generic_wide_int < fixed_wide_int_storage
384 <int_traits <T2>::precision> > result_type;
385 };
386
387 template <>
388 template <typename T1, typename T2>
389 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
390 {
391 typedef wide_int result_type;
392 };
393
394 template <>
395 template <typename T1, typename T2>
396 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
397 {
398 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
399 so as not to confuse gengtype. */
400 typedef generic_wide_int < fixed_wide_int_storage
401 <int_traits <T1>::precision> > result_type;
402 };
403
404 template <>
405 template <typename T1, typename T2>
406 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
407 {
408 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
409 so as not to confuse gengtype. */
410 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
411 typedef generic_wide_int < fixed_wide_int_storage
412 <int_traits <T1>::precision> > result_type;
413 };
414
415 template <>
416 template <typename T1, typename T2>
417 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
418 {
419 typedef wide_int result_type;
420 };
421 }
422
423 /* Public functions for querying and operating on integers. */
424 namespace wi
425 {
426 template <typename T>
427 unsigned int get_precision (const T &);
428
429 template <typename T1, typename T2>
430 unsigned int get_binary_precision (const T1 &, const T2 &);
431
432 template <typename T1, typename T2>
433 void copy (T1 &, const T2 &);
434
435 #define UNARY_PREDICATE \
436 template <typename T> bool
437 #define UNARY_FUNCTION \
438 template <typename T> WI_UNARY_RESULT (T)
439 #define BINARY_PREDICATE \
440 template <typename T1, typename T2> bool
441 #define BINARY_FUNCTION \
442 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
443 #define SHIFT_FUNCTION \
444 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
445
446 UNARY_PREDICATE fits_shwi_p (const T &);
447 UNARY_PREDICATE fits_uhwi_p (const T &);
448 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
449
450 template <typename T>
451 HOST_WIDE_INT sign_mask (const T &);
452
453 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
454 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
455 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
456 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
457 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
458 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
459 BINARY_PREDICATE les_p (const T1 &, const T2 &);
460 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
461 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
462 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
463 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
464 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
465 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
466 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
467
468 template <typename T1, typename T2>
469 int cmp (const T1 &, const T2 &, signop);
470
471 template <typename T1, typename T2>
472 int cmps (const T1 &, const T2 &);
473
474 template <typename T1, typename T2>
475 int cmpu (const T1 &, const T2 &);
476
477 UNARY_FUNCTION bit_not (const T &);
478 UNARY_FUNCTION neg (const T &);
479 UNARY_FUNCTION neg (const T &, bool *);
480 UNARY_FUNCTION abs (const T &);
481 UNARY_FUNCTION ext (const T &, unsigned int, signop);
482 UNARY_FUNCTION sext (const T &, unsigned int);
483 UNARY_FUNCTION zext (const T &, unsigned int);
484 UNARY_FUNCTION set_bit (const T &, unsigned int);
485
486 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
487 BINARY_FUNCTION smin (const T1 &, const T2 &);
488 BINARY_FUNCTION umin (const T1 &, const T2 &);
489 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
490 BINARY_FUNCTION smax (const T1 &, const T2 &);
491 BINARY_FUNCTION umax (const T1 &, const T2 &);
492
493 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
494 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
495 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
496 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
497 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
498 BINARY_FUNCTION add (const T1 &, const T2 &);
499 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
500 BINARY_FUNCTION sub (const T1 &, const T2 &);
501 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
502 BINARY_FUNCTION mul (const T1 &, const T2 &);
503 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
504 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
505 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
506 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
507 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
508 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
509 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
510 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
511 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
512 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
513 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
514 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
515 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
516 WI_BINARY_RESULT (T1, T2) *);
517 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
518 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
519 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
520 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
521 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
522 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
523 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
524
525 template <typename T1, typename T2>
526 bool multiple_of_p (const T1 &, const T2 &, signop);
527
528 template <typename T1, typename T2>
529 bool multiple_of_p (const T1 &, const T2 &, signop,
530 WI_BINARY_RESULT (T1, T2) *);
531
532 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
533 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
534 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
535 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
536 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
537 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
538
539 #undef SHIFT_FUNCTION
540 #undef BINARY_PREDICATE
541 #undef BINARY_FUNCTION
542 #undef UNARY_PREDICATE
543 #undef UNARY_FUNCTION
544
545 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
546 bool only_sign_bit_p (const wide_int_ref &);
547 int clz (const wide_int_ref &);
548 int clrsb (const wide_int_ref &);
549 int ctz (const wide_int_ref &);
550 int exact_log2 (const wide_int_ref &);
551 int floor_log2 (const wide_int_ref &);
552 int ffs (const wide_int_ref &);
553 int popcount (const wide_int_ref &);
554 int parity (const wide_int_ref &);
555
556 template <typename T>
557 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
558
559 template <typename T>
560 unsigned int min_precision (const T &, signop);
561 }
562
563 namespace wi
564 {
565 /* Contains the components of a decomposed integer for easy, direct
566 access. */
567 struct storage_ref
568 {
569 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
570
571 const HOST_WIDE_INT *val;
572 unsigned int len;
573 unsigned int precision;
574
575 /* Provide enough trappings for this class to act as storage for
576 generic_wide_int. */
577 unsigned int get_len () const;
578 unsigned int get_precision () const;
579 const HOST_WIDE_INT *get_val () const;
580 };
581 }
582
583 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
584 unsigned int len_in,
585 unsigned int precision_in)
586 : val (val_in), len (len_in), precision (precision_in)
587 {
588 }
589
590 inline unsigned int
591 wi::storage_ref::get_len () const
592 {
593 return len;
594 }
595
596 inline unsigned int
597 wi::storage_ref::get_precision () const
598 {
599 return precision;
600 }
601
602 inline const HOST_WIDE_INT *
603 wi::storage_ref::get_val () const
604 {
605 return val;
606 }
607
608 /* This class defines an integer type using the storage provided by the
609 template argument. The storage class must provide the following
610 functions:
611
612 unsigned int get_precision () const
613 Return the number of bits in the integer.
614
615 HOST_WIDE_INT *get_val () const
616 Return a pointer to the array of blocks that encodes the integer.
617
618 unsigned int get_len () const
619 Return the number of blocks in get_val (). If this is smaller
620 than the number of blocks implied by get_precision (), the
621 remaining blocks are sign extensions of block get_len () - 1.
622
623 Although not required by generic_wide_int itself, writable storage
624 classes can also provide the following functions:
625
626 HOST_WIDE_INT *write_val ()
627 Get a modifiable version of get_val ()
628
629 unsigned int set_len (unsigned int len)
630 Set the value returned by get_len () to LEN. */
631 template <typename storage>
632 class GTY(()) generic_wide_int : public storage
633 {
634 public:
635 generic_wide_int ();
636
637 template <typename T>
638 generic_wide_int (const T &);
639
640 template <typename T>
641 generic_wide_int (const T &, unsigned int);
642
643 /* Conversions. */
644 HOST_WIDE_INT to_shwi (unsigned int) const;
645 HOST_WIDE_INT to_shwi () const;
646 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
647 unsigned HOST_WIDE_INT to_uhwi () const;
648 HOST_WIDE_INT to_short_addr () const;
649
650 /* Public accessors for the interior of a wide int. */
651 HOST_WIDE_INT sign_mask () const;
652 HOST_WIDE_INT elt (unsigned int) const;
653 unsigned HOST_WIDE_INT ulow () const;
654 unsigned HOST_WIDE_INT uhigh () const;
655 HOST_WIDE_INT slow () const;
656 HOST_WIDE_INT shigh () const;
657
658 template <typename T>
659 generic_wide_int &operator = (const T &);
660
661 #define BINARY_PREDICATE(OP, F) \
662 template <typename T> \
663 bool OP (const T &c) const { return wi::F (*this, c); }
664
665 #define UNARY_OPERATOR(OP, F) \
666 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
667
668 #define BINARY_OPERATOR(OP, F) \
669 template <typename T> \
670 WI_BINARY_RESULT (generic_wide_int, T) \
671 OP (const T &c) const { return wi::F (*this, c); }
672
673 #define ASSIGNMENT_OPERATOR(OP, F) \
674 template <typename T> \
675 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
676
677 #define INCDEC_OPERATOR(OP, DELTA) \
678 generic_wide_int &OP () { *this += DELTA; return *this; }
679
680 UNARY_OPERATOR (operator ~, bit_not)
681 UNARY_OPERATOR (operator -, neg)
682 BINARY_PREDICATE (operator ==, eq_p)
683 BINARY_PREDICATE (operator !=, ne_p)
684 BINARY_OPERATOR (operator &, bit_and)
685 BINARY_OPERATOR (and_not, bit_and_not)
686 BINARY_OPERATOR (operator |, bit_or)
687 BINARY_OPERATOR (or_not, bit_or_not)
688 BINARY_OPERATOR (operator ^, bit_xor)
689 BINARY_OPERATOR (operator +, add)
690 BINARY_OPERATOR (operator -, sub)
691 BINARY_OPERATOR (operator *, mul)
692 ASSIGNMENT_OPERATOR (operator &=, bit_and)
693 ASSIGNMENT_OPERATOR (operator |=, bit_or)
694 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
695 ASSIGNMENT_OPERATOR (operator +=, add)
696 ASSIGNMENT_OPERATOR (operator -=, sub)
697 ASSIGNMENT_OPERATOR (operator *=, mul)
698 INCDEC_OPERATOR (operator ++, 1)
699 INCDEC_OPERATOR (operator --, -1)
700
701 #undef BINARY_PREDICATE
702 #undef UNARY_OPERATOR
703 #undef BINARY_OPERATOR
704 #undef ASSIGNMENT_OPERATOR
705 #undef INCDEC_OPERATOR
706
707 /* Debugging functions. */
708 void dump () const;
709
710 static const bool is_sign_extended
711 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
712 };
713
714 template <typename storage>
715 inline generic_wide_int <storage>::generic_wide_int () {}
716
717 template <typename storage>
718 template <typename T>
719 inline generic_wide_int <storage>::generic_wide_int (const T &x)
720 : storage (x)
721 {
722 }
723
724 template <typename storage>
725 template <typename T>
726 inline generic_wide_int <storage>::generic_wide_int (const T &x,
727 unsigned int precision)
728 : storage (x, precision)
729 {
730 }
731
732 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
733 If THIS does not fit in PRECISION, the information is lost. */
734 template <typename storage>
735 inline HOST_WIDE_INT
736 generic_wide_int <storage>::to_shwi (unsigned int precision) const
737 {
738 if (precision < HOST_BITS_PER_WIDE_INT)
739 return sext_hwi (this->get_val ()[0], precision);
740 else
741 return this->get_val ()[0];
742 }
743
744 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
745 template <typename storage>
746 inline HOST_WIDE_INT
747 generic_wide_int <storage>::to_shwi () const
748 {
749 if (is_sign_extended)
750 return this->get_val ()[0];
751 else
752 return to_shwi (this->get_precision ());
753 }
754
755 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
756 PRECISION. If THIS does not fit in PRECISION, the information
757 is lost. */
758 template <typename storage>
759 inline unsigned HOST_WIDE_INT
760 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
761 {
762 if (precision < HOST_BITS_PER_WIDE_INT)
763 return zext_hwi (this->get_val ()[0], precision);
764 else
765 return this->get_val ()[0];
766 }
767
768 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
769 template <typename storage>
770 inline unsigned HOST_WIDE_INT
771 generic_wide_int <storage>::to_uhwi () const
772 {
773 return to_uhwi (this->get_precision ());
774 }
775
776 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
777 represent addresses to using offset_int to represent addresses.
778 We use to_short_addr at the interface from new code to old,
779 unconverted code. */
780 template <typename storage>
781 inline HOST_WIDE_INT
782 generic_wide_int <storage>::to_short_addr () const
783 {
784 return this->get_val ()[0];
785 }
786
787 /* Return the implicit value of blocks above get_len (). */
788 template <typename storage>
789 inline HOST_WIDE_INT
790 generic_wide_int <storage>::sign_mask () const
791 {
792 unsigned int len = this->get_len ();
793 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
794 if (!is_sign_extended)
795 {
796 unsigned int precision = this->get_precision ();
797 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
798 if (excess > 0)
799 high <<= excess;
800 }
801 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
802 }
803
804 /* Return the signed value of the least-significant explicitly-encoded
805 block. */
806 template <typename storage>
807 inline HOST_WIDE_INT
808 generic_wide_int <storage>::slow () const
809 {
810 return this->get_val ()[0];
811 }
812
813 /* Return the signed value of the most-significant explicitly-encoded
814 block. */
815 template <typename storage>
816 inline HOST_WIDE_INT
817 generic_wide_int <storage>::shigh () const
818 {
819 return this->get_val ()[this->get_len () - 1];
820 }
821
822 /* Return the unsigned value of the least-significant
823 explicitly-encoded block. */
824 template <typename storage>
825 inline unsigned HOST_WIDE_INT
826 generic_wide_int <storage>::ulow () const
827 {
828 return this->get_val ()[0];
829 }
830
831 /* Return the unsigned value of the most-significant
832 explicitly-encoded block. */
833 template <typename storage>
834 inline unsigned HOST_WIDE_INT
835 generic_wide_int <storage>::uhigh () const
836 {
837 return this->get_val ()[this->get_len () - 1];
838 }
839
840 /* Return block I, which might be implicitly or explicit encoded. */
841 template <typename storage>
842 inline HOST_WIDE_INT
843 generic_wide_int <storage>::elt (unsigned int i) const
844 {
845 if (i >= this->get_len ())
846 return sign_mask ();
847 else
848 return this->get_val ()[i];
849 }
850
851 template <typename storage>
852 template <typename T>
853 generic_wide_int <storage> &
854 generic_wide_int <storage>::operator = (const T &x)
855 {
856 storage::operator = (x);
857 return *this;
858 }
859
860 /* Dump the contents of the integer to stderr, for debugging. */
861 template <typename storage>
862 void
863 generic_wide_int <storage>::dump () const
864 {
865 unsigned int len = this->get_len ();
866 const HOST_WIDE_INT *val = this->get_val ();
867 unsigned int precision = this->get_precision ();
868 fprintf (stderr, "[");
869 if (len * HOST_BITS_PER_WIDE_INT < precision)
870 fprintf (stderr, "...,");
871 for (unsigned int i = 0; i < len - 1; ++i)
872 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
873 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
874 val[0], precision);
875 }
876
877 namespace wi
878 {
879 template <>
880 template <typename storage>
881 struct int_traits < generic_wide_int <storage> >
882 : public wi::int_traits <storage>
883 {
884 static unsigned int get_precision (const generic_wide_int <storage> &);
885 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
886 const generic_wide_int <storage> &);
887 };
888 }
889
890 template <typename storage>
891 inline unsigned int
892 wi::int_traits < generic_wide_int <storage> >::
893 get_precision (const generic_wide_int <storage> &x)
894 {
895 return x.get_precision ();
896 }
897
898 template <typename storage>
899 inline wi::storage_ref
900 wi::int_traits < generic_wide_int <storage> >::
901 decompose (HOST_WIDE_INT *, unsigned int precision,
902 const generic_wide_int <storage> &x)
903 {
904 gcc_checking_assert (precision == x.get_precision ());
905 return wi::storage_ref (x.get_val (), x.get_len (), precision);
906 }
907
908 /* Provide the storage for a wide_int_ref. This acts like a read-only
909 wide_int, with the optimization that VAL is normally a pointer to
910 another integer's storage, so that no array copy is needed. */
911 template <bool SE>
912 struct wide_int_ref_storage : public wi::storage_ref
913 {
914 private:
915 /* Scratch space that can be used when decomposing the original integer.
916 It must live as long as this object. */
917 HOST_WIDE_INT scratch[2];
918
919 public:
920 wide_int_ref_storage (const wi::storage_ref &);
921
922 template <typename T>
923 wide_int_ref_storage (const T &);
924
925 template <typename T>
926 wide_int_ref_storage (const T &, unsigned int);
927 };
928
929 /* Create a reference from an existing reference. */
930 template <bool SE>
931 inline wide_int_ref_storage <SE>::
932 wide_int_ref_storage (const wi::storage_ref &x)
933 : storage_ref (x)
934 {}
935
936 /* Create a reference to integer X in its natural precision. Note
937 that the natural precision is host-dependent for primitive
938 types. */
939 template <bool SE>
940 template <typename T>
941 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
942 : storage_ref (wi::int_traits <T>::decompose (scratch,
943 wi::get_precision (x), x))
944 {
945 }
946
947 /* Create a reference to integer X in precision PRECISION. */
948 template <bool SE>
949 template <typename T>
950 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
951 unsigned int precision)
952 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
953 {
954 }
955
956 namespace wi
957 {
958 template <>
959 template <bool SE>
960 struct int_traits <wide_int_ref_storage <SE> >
961 {
962 static const enum precision_type precision_type = VAR_PRECISION;
963 /* wi::storage_ref can be a reference to a primitive type,
964 so this is the conservatively-correct setting. */
965 static const bool host_dependent_precision = true;
966 static const bool is_sign_extended = SE;
967 };
968 }
969
970 namespace wi
971 {
972 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
973 unsigned int, unsigned int, unsigned int,
974 signop sgn);
975 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
976 unsigned int, unsigned int, bool = true);
977 }
978
979 /* The storage used by wide_int. */
980 class GTY(()) wide_int_storage
981 {
982 private:
983 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
984 unsigned int len;
985 unsigned int precision;
986
987 public:
988 wide_int_storage ();
989 template <typename T>
990 wide_int_storage (const T &);
991
992 /* The standard generic_wide_int storage methods. */
993 unsigned int get_precision () const;
994 const HOST_WIDE_INT *get_val () const;
995 unsigned int get_len () const;
996 HOST_WIDE_INT *write_val ();
997 void set_len (unsigned int, bool = false);
998
999 static wide_int from (const wide_int_ref &, unsigned int, signop);
1000 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1001 unsigned int, bool = true);
1002 static wide_int create (unsigned int);
1003
1004 /* FIXME: target-dependent, so should disappear. */
1005 wide_int bswap () const;
1006 };
1007
1008 namespace wi
1009 {
1010 template <>
1011 struct int_traits <wide_int_storage>
1012 {
1013 static const enum precision_type precision_type = VAR_PRECISION;
1014 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1015 static const bool host_dependent_precision = false;
1016 static const bool is_sign_extended = true;
1017 template <typename T1, typename T2>
1018 static wide_int get_binary_result (const T1 &, const T2 &);
1019 };
1020 }
1021
1022 inline wide_int_storage::wide_int_storage () {}
1023
1024 /* Initialize the storage from integer X, in its natural precision.
1025 Note that we do not allow integers with host-dependent precision
1026 to become wide_ints; wide_ints must always be logically independent
1027 of the host. */
1028 template <typename T>
1029 inline wide_int_storage::wide_int_storage (const T &x)
1030 {
1031 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1032 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1033 WIDE_INT_REF_FOR (T) xi (x);
1034 precision = xi.precision;
1035 wi::copy (*this, xi);
1036 }
1037
1038 inline unsigned int
1039 wide_int_storage::get_precision () const
1040 {
1041 return precision;
1042 }
1043
1044 inline const HOST_WIDE_INT *
1045 wide_int_storage::get_val () const
1046 {
1047 return val;
1048 }
1049
1050 inline unsigned int
1051 wide_int_storage::get_len () const
1052 {
1053 return len;
1054 }
1055
1056 inline HOST_WIDE_INT *
1057 wide_int_storage::write_val ()
1058 {
1059 return val;
1060 }
1061
1062 inline void
1063 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1064 {
1065 len = l;
1066 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1067 val[len - 1] = sext_hwi (val[len - 1],
1068 precision % HOST_BITS_PER_WIDE_INT);
1069 }
1070
1071 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1072 number. */
1073 inline wide_int
1074 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1075 signop sgn)
1076 {
1077 wide_int result = wide_int::create (precision);
1078 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1079 x.precision, precision, sgn));
1080 return result;
1081 }
1082
1083 /* Create a wide_int from the explicit block encoding given by VAL and
1084 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1085 true if the encoding may have redundant trailing blocks. */
1086 inline wide_int
1087 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1088 unsigned int precision, bool need_canon_p)
1089 {
1090 wide_int result = wide_int::create (precision);
1091 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1092 need_canon_p));
1093 return result;
1094 }
1095
1096 /* Return an uninitialized wide_int with precision PRECISION. */
1097 inline wide_int
1098 wide_int_storage::create (unsigned int precision)
1099 {
1100 wide_int x;
1101 x.precision = precision;
1102 return x;
1103 }
1104
1105 template <typename T1, typename T2>
1106 inline wide_int
1107 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1108 {
1109 /* This shouldn't be used for two flexible-precision inputs. */
1110 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1111 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1112 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1113 return wide_int::create (wi::get_precision (y));
1114 else
1115 return wide_int::create (wi::get_precision (x));
1116 }
1117
1118 /* The storage used by FIXED_WIDE_INT (N). */
1119 template <int N>
1120 class GTY(()) fixed_wide_int_storage
1121 {
1122 private:
1123 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1124 unsigned int len;
1125
1126 public:
1127 fixed_wide_int_storage ();
1128 template <typename T>
1129 fixed_wide_int_storage (const T &);
1130
1131 /* The standard generic_wide_int storage methods. */
1132 unsigned int get_precision () const;
1133 const HOST_WIDE_INT *get_val () const;
1134 unsigned int get_len () const;
1135 HOST_WIDE_INT *write_val ();
1136 void set_len (unsigned int, bool = false);
1137
1138 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1139 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1140 bool = true);
1141 };
1142
1143 namespace wi
1144 {
1145 template <>
1146 template <int N>
1147 struct int_traits < fixed_wide_int_storage <N> >
1148 {
1149 static const enum precision_type precision_type = CONST_PRECISION;
1150 static const bool host_dependent_precision = false;
1151 static const bool is_sign_extended = true;
1152 static const unsigned int precision = N;
1153 template <typename T1, typename T2>
1154 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1155 };
1156 }
1157
1158 template <int N>
1159 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1160
1161 /* Initialize the storage from integer X, in precision N. */
1162 template <int N>
1163 template <typename T>
1164 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1165 {
1166 /* Check for type compatibility. We don't want to initialize a
1167 fixed-width integer from something like a wide_int. */
1168 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1169 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1170 }
1171
1172 template <int N>
1173 inline unsigned int
1174 fixed_wide_int_storage <N>::get_precision () const
1175 {
1176 return N;
1177 }
1178
1179 template <int N>
1180 inline const HOST_WIDE_INT *
1181 fixed_wide_int_storage <N>::get_val () const
1182 {
1183 return val;
1184 }
1185
1186 template <int N>
1187 inline unsigned int
1188 fixed_wide_int_storage <N>::get_len () const
1189 {
1190 return len;
1191 }
1192
1193 template <int N>
1194 inline HOST_WIDE_INT *
1195 fixed_wide_int_storage <N>::write_val ()
1196 {
1197 return val;
1198 }
1199
1200 template <int N>
1201 inline void
1202 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1203 {
1204 len = l;
1205 /* There are no excess bits in val[len - 1]. */
1206 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1207 }
1208
1209 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1210 template <int N>
1211 inline FIXED_WIDE_INT (N)
1212 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1213 {
1214 FIXED_WIDE_INT (N) result;
1215 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1216 x.precision, N, sgn));
1217 return result;
1218 }
1219
1220 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1221 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1222 trailing blocks. */
1223 template <int N>
1224 inline FIXED_WIDE_INT (N)
1225 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1226 unsigned int len,
1227 bool need_canon_p)
1228 {
1229 FIXED_WIDE_INT (N) result;
1230 result.set_len (wi::from_array (result.write_val (), val, len,
1231 N, need_canon_p));
1232 return result;
1233 }
1234
1235 template <int N>
1236 template <typename T1, typename T2>
1237 inline FIXED_WIDE_INT (N)
1238 wi::int_traits < fixed_wide_int_storage <N> >::
1239 get_binary_result (const T1 &, const T2 &)
1240 {
1241 return FIXED_WIDE_INT (N) ();
1242 }
1243
1244 /* A reference to one element of a trailing_wide_ints structure. */
1245 class trailing_wide_int_storage
1246 {
1247 private:
1248 /* The precision of the integer, which is a fixed property of the
1249 parent trailing_wide_ints. */
1250 unsigned int m_precision;
1251
1252 /* A pointer to the length field. */
1253 unsigned char *m_len;
1254
1255 /* A pointer to the HWI array. There are enough elements to hold all
1256 values of precision M_PRECISION. */
1257 HOST_WIDE_INT *m_val;
1258
1259 public:
1260 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1261
1262 /* The standard generic_wide_int storage methods. */
1263 unsigned int get_len () const;
1264 unsigned int get_precision () const;
1265 const HOST_WIDE_INT *get_val () const;
1266 HOST_WIDE_INT *write_val ();
1267 void set_len (unsigned int, bool = false);
1268
1269 template <typename T>
1270 trailing_wide_int_storage &operator = (const T &);
1271 };
1272
1273 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1274
1275 /* trailing_wide_int behaves like a wide_int. */
1276 namespace wi
1277 {
1278 template <>
1279 struct int_traits <trailing_wide_int_storage>
1280 : public int_traits <wide_int_storage> {};
1281 }
1282
1283 /* An array of N wide_int-like objects that can be put at the end of
1284 a variable-sized structure. Use extra_size to calculate how many
1285 bytes beyond the sizeof need to be allocated. Use set_precision
1286 to initialize the structure. */
1287 template <int N>
1288 class GTY(()) trailing_wide_ints
1289 {
1290 private:
1291 /* The shared precision of each number. */
1292 unsigned short m_precision;
1293
1294 /* The shared maximum length of each number. */
1295 unsigned char m_max_len;
1296
1297 /* The current length of each number. */
1298 unsigned char m_len[N];
1299
1300 /* The variable-length part of the structure, which always contains
1301 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1302 HOST_WIDE_INT m_val[1];
1303
1304 public:
1305 void set_precision (unsigned int);
1306 trailing_wide_int operator [] (unsigned int);
1307 static size_t extra_size (unsigned int);
1308 };
1309
1310 inline trailing_wide_int_storage::
1311 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1312 HOST_WIDE_INT *val)
1313 : m_precision (precision), m_len (len), m_val (val)
1314 {
1315 }
1316
1317 inline unsigned int
1318 trailing_wide_int_storage::get_len () const
1319 {
1320 return *m_len;
1321 }
1322
1323 inline unsigned int
1324 trailing_wide_int_storage::get_precision () const
1325 {
1326 return m_precision;
1327 }
1328
1329 inline const HOST_WIDE_INT *
1330 trailing_wide_int_storage::get_val () const
1331 {
1332 return m_val;
1333 }
1334
1335 inline HOST_WIDE_INT *
1336 trailing_wide_int_storage::write_val ()
1337 {
1338 return m_val;
1339 }
1340
1341 inline void
1342 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1343 {
1344 *m_len = len;
1345 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1346 m_val[len - 1] = sext_hwi (m_val[len - 1],
1347 m_precision % HOST_BITS_PER_WIDE_INT);
1348 }
1349
1350 template <typename T>
1351 inline trailing_wide_int_storage &
1352 trailing_wide_int_storage::operator = (const T &x)
1353 {
1354 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1355 wi::copy (*this, xi);
1356 return *this;
1357 }
1358
1359 /* Initialize the structure and record that all elements have precision
1360 PRECISION. */
1361 template <int N>
1362 inline void
1363 trailing_wide_ints <N>::set_precision (unsigned int precision)
1364 {
1365 m_precision = precision;
1366 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1367 / HOST_BITS_PER_WIDE_INT);
1368 }
1369
1370 /* Return a reference to element INDEX. */
1371 template <int N>
1372 inline trailing_wide_int
1373 trailing_wide_ints <N>::operator [] (unsigned int index)
1374 {
1375 return trailing_wide_int_storage (m_precision, &m_len[index],
1376 &m_val[index * m_max_len]);
1377 }
1378
1379 /* Return how many extra bytes need to be added to the end of the structure
1380 in order to handle N wide_ints of precision PRECISION. */
1381 template <int N>
1382 inline size_t
1383 trailing_wide_ints <N>::extra_size (unsigned int precision)
1384 {
1385 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1386 / HOST_BITS_PER_WIDE_INT);
1387 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1388 }
1389
1390 /* This macro is used in structures that end with a trailing_wide_ints field
1391 called FIELD. It declares get_NAME() and set_NAME() methods to access
1392 element I of FIELD. */
1393 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1394 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1395 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1396
1397 namespace wi
1398 {
1399 /* Implementation of int_traits for primitive integer types like "int". */
1400 template <typename T, bool signed_p>
1401 struct primitive_int_traits
1402 {
1403 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1404 static const bool host_dependent_precision = true;
1405 static const bool is_sign_extended = true;
1406 static unsigned int get_precision (T);
1407 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1408 };
1409 }
1410
1411 template <typename T, bool signed_p>
1412 inline unsigned int
1413 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1414 {
1415 return sizeof (T) * CHAR_BIT;
1416 }
1417
1418 template <typename T, bool signed_p>
1419 inline wi::storage_ref
1420 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1421 unsigned int precision, T x)
1422 {
1423 scratch[0] = x;
1424 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1425 return wi::storage_ref (scratch, 1, precision);
1426 scratch[1] = 0;
1427 return wi::storage_ref (scratch, 2, precision);
1428 }
1429
1430 /* Allow primitive C types to be used in wi:: routines. */
1431 namespace wi
1432 {
1433 template <>
1434 struct int_traits <int>
1435 : public primitive_int_traits <int, true> {};
1436
1437 template <>
1438 struct int_traits <unsigned int>
1439 : public primitive_int_traits <unsigned int, false> {};
1440
1441 template <>
1442 struct int_traits <long>
1443 : public primitive_int_traits <long, true> {};
1444
1445 template <>
1446 struct int_traits <unsigned long>
1447 : public primitive_int_traits <unsigned long, false> {};
1448
1449 #if defined HAVE_LONG_LONG
1450 template <>
1451 struct int_traits <long long>
1452 : public primitive_int_traits <long long, true> {};
1453
1454 template <>
1455 struct int_traits <unsigned long long>
1456 : public primitive_int_traits <unsigned long long, false> {};
1457 #endif
1458 }
1459
1460 namespace wi
1461 {
1462 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1463 and precision PRECISION. */
1464 struct hwi_with_prec
1465 {
1466 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1467 HOST_WIDE_INT val;
1468 unsigned int precision;
1469 signop sgn;
1470 };
1471
1472 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1473 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1474
1475 hwi_with_prec minus_one (unsigned int);
1476 hwi_with_prec zero (unsigned int);
1477 hwi_with_prec one (unsigned int);
1478 hwi_with_prec two (unsigned int);
1479 }
1480
1481 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1482 signop s)
1483 : val (v), precision (p), sgn (s)
1484 {
1485 }
1486
1487 /* Return a signed integer that has value VAL and precision PRECISION. */
1488 inline wi::hwi_with_prec
1489 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1490 {
1491 return hwi_with_prec (val, precision, SIGNED);
1492 }
1493
1494 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1495 inline wi::hwi_with_prec
1496 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1497 {
1498 return hwi_with_prec (val, precision, UNSIGNED);
1499 }
1500
1501 /* Return a wide int of -1 with precision PRECISION. */
1502 inline wi::hwi_with_prec
1503 wi::minus_one (unsigned int precision)
1504 {
1505 return wi::shwi (-1, precision);
1506 }
1507
1508 /* Return a wide int of 0 with precision PRECISION. */
1509 inline wi::hwi_with_prec
1510 wi::zero (unsigned int precision)
1511 {
1512 return wi::shwi (0, precision);
1513 }
1514
1515 /* Return a wide int of 1 with precision PRECISION. */
1516 inline wi::hwi_with_prec
1517 wi::one (unsigned int precision)
1518 {
1519 return wi::shwi (1, precision);
1520 }
1521
1522 /* Return a wide int of 2 with precision PRECISION. */
1523 inline wi::hwi_with_prec
1524 wi::two (unsigned int precision)
1525 {
1526 return wi::shwi (2, precision);
1527 }
1528
1529 namespace wi
1530 {
1531 template <>
1532 struct int_traits <wi::hwi_with_prec>
1533 {
1534 static const enum precision_type precision_type = VAR_PRECISION;
1535 /* hwi_with_prec has an explicitly-given precision, rather than the
1536 precision of HOST_WIDE_INT. */
1537 static const bool host_dependent_precision = false;
1538 static const bool is_sign_extended = true;
1539 static unsigned int get_precision (const wi::hwi_with_prec &);
1540 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1541 const wi::hwi_with_prec &);
1542 };
1543 }
1544
1545 inline unsigned int
1546 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1547 {
1548 return x.precision;
1549 }
1550
1551 inline wi::storage_ref
1552 wi::int_traits <wi::hwi_with_prec>::
1553 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1554 const wi::hwi_with_prec &x)
1555 {
1556 gcc_checking_assert (precision == x.precision);
1557 scratch[0] = x.val;
1558 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1559 return wi::storage_ref (scratch, 1, precision);
1560 scratch[1] = 0;
1561 return wi::storage_ref (scratch, 2, precision);
1562 }
1563
1564 /* Private functions for handling large cases out of line. They take
1565 individual length and array parameters because that is cheaper for
1566 the inline caller than constructing an object on the stack and
1567 passing a reference to it. (Although many callers use wide_int_refs,
1568 we generally want those to be removed by SRA.) */
1569 namespace wi
1570 {
1571 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1572 const HOST_WIDE_INT *, unsigned int, unsigned int);
1573 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1574 const HOST_WIDE_INT *, unsigned int);
1575 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1576 const HOST_WIDE_INT *, unsigned int);
1577 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1578 const HOST_WIDE_INT *, unsigned int);
1579 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1580 const HOST_WIDE_INT *, unsigned int);
1581 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1582 unsigned int,
1583 unsigned int, unsigned int);
1584 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1585 unsigned int,
1586 unsigned int, unsigned int);
1587 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1588 unsigned int, unsigned int, unsigned int);
1589 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1590 unsigned int, unsigned int, unsigned int);
1591 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1592 unsigned int, unsigned int, unsigned int,
1593 unsigned int);
1594 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1595 unsigned int, unsigned int, unsigned int,
1596 unsigned int);
1597 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1598 const HOST_WIDE_INT *, unsigned int, unsigned int);
1599 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1600 unsigned int, const HOST_WIDE_INT *,
1601 unsigned int, unsigned int);
1602 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1603 const HOST_WIDE_INT *, unsigned int, unsigned int);
1604 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1605 unsigned int, const HOST_WIDE_INT *,
1606 unsigned int, unsigned int);
1607 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1608 const HOST_WIDE_INT *, unsigned int, unsigned int);
1609 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1610 const HOST_WIDE_INT *, unsigned int, unsigned int,
1611 signop, bool *);
1612 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1613 const HOST_WIDE_INT *, unsigned int, unsigned int,
1614 signop, bool *);
1615 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1616 unsigned int, const HOST_WIDE_INT *,
1617 unsigned int, unsigned int, signop, bool *,
1618 bool);
1619 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1620 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1621 unsigned int, unsigned int,
1622 const HOST_WIDE_INT *,
1623 unsigned int, unsigned int,
1624 signop, bool *);
1625 }
1626
1627 /* Return the number of bits that integer X can hold. */
1628 template <typename T>
1629 inline unsigned int
1630 wi::get_precision (const T &x)
1631 {
1632 return wi::int_traits <T>::get_precision (x);
1633 }
1634
1635 /* Return the number of bits that the result of a binary operation can
1636 hold when the input operands are X and Y. */
1637 template <typename T1, typename T2>
1638 inline unsigned int
1639 wi::get_binary_precision (const T1 &x, const T2 &y)
1640 {
1641 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1642 get_binary_result (x, y));
1643 }
1644
1645 /* Copy the contents of Y to X, but keeping X's current precision. */
1646 template <typename T1, typename T2>
1647 inline void
1648 wi::copy (T1 &x, const T2 &y)
1649 {
1650 HOST_WIDE_INT *xval = x.write_val ();
1651 const HOST_WIDE_INT *yval = y.get_val ();
1652 unsigned int len = y.get_len ();
1653 unsigned int i = 0;
1654 do
1655 xval[i] = yval[i];
1656 while (++i < len);
1657 x.set_len (len, y.is_sign_extended);
1658 }
1659
1660 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1661 template <typename T>
1662 inline bool
1663 wi::fits_shwi_p (const T &x)
1664 {
1665 WIDE_INT_REF_FOR (T) xi (x);
1666 return xi.len == 1;
1667 }
1668
1669 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1670 precision. */
1671 template <typename T>
1672 inline bool
1673 wi::fits_uhwi_p (const T &x)
1674 {
1675 WIDE_INT_REF_FOR (T) xi (x);
1676 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1677 return true;
1678 if (xi.len == 1)
1679 return xi.slow () >= 0;
1680 return xi.len == 2 && xi.uhigh () == 0;
1681 }
1682
1683 /* Return true if X is negative based on the interpretation of SGN.
1684 For UNSIGNED, this is always false. */
1685 template <typename T>
1686 inline bool
1687 wi::neg_p (const T &x, signop sgn)
1688 {
1689 WIDE_INT_REF_FOR (T) xi (x);
1690 if (sgn == UNSIGNED)
1691 return false;
1692 return xi.sign_mask () < 0;
1693 }
1694
1695 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1696 template <typename T>
1697 inline HOST_WIDE_INT
1698 wi::sign_mask (const T &x)
1699 {
1700 WIDE_INT_REF_FOR (T) xi (x);
1701 return xi.sign_mask ();
1702 }
1703
1704 /* Return true if X == Y. X and Y must be binary-compatible. */
1705 template <typename T1, typename T2>
1706 inline bool
1707 wi::eq_p (const T1 &x, const T2 &y)
1708 {
1709 unsigned int precision = get_binary_precision (x, y);
1710 WIDE_INT_REF_FOR (T1) xi (x, precision);
1711 WIDE_INT_REF_FOR (T2) yi (y, precision);
1712 if (xi.is_sign_extended && yi.is_sign_extended)
1713 {
1714 /* This case reduces to array equality. */
1715 if (xi.len != yi.len)
1716 return false;
1717 unsigned int i = 0;
1718 do
1719 if (xi.val[i] != yi.val[i])
1720 return false;
1721 while (++i != xi.len);
1722 return true;
1723 }
1724 if (__builtin_expect (yi.len == 1, true))
1725 {
1726 /* XI is only equal to YI if it too has a single HWI. */
1727 if (xi.len != 1)
1728 return false;
1729 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1730 with 0 are simple. */
1731 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1732 return xi.val[0] == 0;
1733 /* Otherwise flush out any excess bits first. */
1734 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1735 int excess = HOST_BITS_PER_WIDE_INT - precision;
1736 if (excess > 0)
1737 diff <<= excess;
1738 return diff == 0;
1739 }
1740 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1741 }
1742
1743 /* Return true if X != Y. X and Y must be binary-compatible. */
1744 template <typename T1, typename T2>
1745 inline bool
1746 wi::ne_p (const T1 &x, const T2 &y)
1747 {
1748 return !eq_p (x, y);
1749 }
1750
1751 /* Return true if X < Y when both are treated as signed values. */
1752 template <typename T1, typename T2>
1753 inline bool
1754 wi::lts_p (const T1 &x, const T2 &y)
1755 {
1756 unsigned int precision = get_binary_precision (x, y);
1757 WIDE_INT_REF_FOR (T1) xi (x, precision);
1758 WIDE_INT_REF_FOR (T2) yi (y, precision);
1759 /* We optimize x < y, where y is 64 or fewer bits. */
1760 if (wi::fits_shwi_p (yi))
1761 {
1762 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1763 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1764 return neg_p (xi);
1765 /* If x fits directly into a shwi, we can compare directly. */
1766 if (wi::fits_shwi_p (xi))
1767 return xi.to_shwi () < yi.to_shwi ();
1768 /* If x doesn't fit and is negative, then it must be more
1769 negative than any value in y, and hence smaller than y. */
1770 if (neg_p (xi))
1771 return true;
1772 /* If x is positive, then it must be larger than any value in y,
1773 and hence greater than y. */
1774 return false;
1775 }
1776 /* Optimize the opposite case, if it can be detected at compile time. */
1777 if (STATIC_CONSTANT_P (xi.len == 1))
1778 /* If YI is negative it is lower than the least HWI.
1779 If YI is positive it is greater than the greatest HWI. */
1780 return !neg_p (yi);
1781 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1782 }
1783
1784 /* Return true if X < Y when both are treated as unsigned values. */
1785 template <typename T1, typename T2>
1786 inline bool
1787 wi::ltu_p (const T1 &x, const T2 &y)
1788 {
1789 unsigned int precision = get_binary_precision (x, y);
1790 WIDE_INT_REF_FOR (T1) xi (x, precision);
1791 WIDE_INT_REF_FOR (T2) yi (y, precision);
1792 /* Optimize comparisons with constants. */
1793 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1794 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1795 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1796 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1797 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1798 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1799 values does not change the result. */
1800 if (__builtin_expect (xi.len + yi.len == 2, true))
1801 {
1802 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1803 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1804 return xl < yl;
1805 }
1806 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1807 }
1808
1809 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1810 template <typename T1, typename T2>
1811 inline bool
1812 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1813 {
1814 if (sgn == SIGNED)
1815 return lts_p (x, y);
1816 else
1817 return ltu_p (x, y);
1818 }
1819
1820 /* Return true if X <= Y when both are treated as signed values. */
1821 template <typename T1, typename T2>
1822 inline bool
1823 wi::les_p (const T1 &x, const T2 &y)
1824 {
1825 return !lts_p (y, x);
1826 }
1827
1828 /* Return true if X <= Y when both are treated as unsigned values. */
1829 template <typename T1, typename T2>
1830 inline bool
1831 wi::leu_p (const T1 &x, const T2 &y)
1832 {
1833 return !ltu_p (y, x);
1834 }
1835
1836 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1837 template <typename T1, typename T2>
1838 inline bool
1839 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1840 {
1841 if (sgn == SIGNED)
1842 return les_p (x, y);
1843 else
1844 return leu_p (x, y);
1845 }
1846
1847 /* Return true if X > Y when both are treated as signed values. */
1848 template <typename T1, typename T2>
1849 inline bool
1850 wi::gts_p (const T1 &x, const T2 &y)
1851 {
1852 return lts_p (y, x);
1853 }
1854
1855 /* Return true if X > Y when both are treated as unsigned values. */
1856 template <typename T1, typename T2>
1857 inline bool
1858 wi::gtu_p (const T1 &x, const T2 &y)
1859 {
1860 return ltu_p (y, x);
1861 }
1862
1863 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1864 template <typename T1, typename T2>
1865 inline bool
1866 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1867 {
1868 if (sgn == SIGNED)
1869 return gts_p (x, y);
1870 else
1871 return gtu_p (x, y);
1872 }
1873
1874 /* Return true if X >= Y when both are treated as signed values. */
1875 template <typename T1, typename T2>
1876 inline bool
1877 wi::ges_p (const T1 &x, const T2 &y)
1878 {
1879 return !lts_p (x, y);
1880 }
1881
1882 /* Return true if X >= Y when both are treated as unsigned values. */
1883 template <typename T1, typename T2>
1884 inline bool
1885 wi::geu_p (const T1 &x, const T2 &y)
1886 {
1887 return !ltu_p (x, y);
1888 }
1889
1890 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1891 template <typename T1, typename T2>
1892 inline bool
1893 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1894 {
1895 if (sgn == SIGNED)
1896 return ges_p (x, y);
1897 else
1898 return geu_p (x, y);
1899 }
1900
1901 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1902 as signed values. */
1903 template <typename T1, typename T2>
1904 inline int
1905 wi::cmps (const T1 &x, const T2 &y)
1906 {
1907 unsigned int precision = get_binary_precision (x, y);
1908 WIDE_INT_REF_FOR (T1) xi (x, precision);
1909 WIDE_INT_REF_FOR (T2) yi (y, precision);
1910 if (wi::fits_shwi_p (yi))
1911 {
1912 /* Special case for comparisons with 0. */
1913 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1914 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1915 /* If x fits into a signed HWI, we can compare directly. */
1916 if (wi::fits_shwi_p (xi))
1917 {
1918 HOST_WIDE_INT xl = xi.to_shwi ();
1919 HOST_WIDE_INT yl = yi.to_shwi ();
1920 return xl < yl ? -1 : xl > yl;
1921 }
1922 /* If x doesn't fit and is negative, then it must be more
1923 negative than any signed HWI, and hence smaller than y. */
1924 if (neg_p (xi))
1925 return -1;
1926 /* If x is positive, then it must be larger than any signed HWI,
1927 and hence greater than y. */
1928 return 1;
1929 }
1930 /* Optimize the opposite case, if it can be detected at compile time. */
1931 if (STATIC_CONSTANT_P (xi.len == 1))
1932 /* If YI is negative it is lower than the least HWI.
1933 If YI is positive it is greater than the greatest HWI. */
1934 return neg_p (yi) ? 1 : -1;
1935 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1936 }
1937
1938 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1939 as unsigned values. */
1940 template <typename T1, typename T2>
1941 inline int
1942 wi::cmpu (const T1 &x, const T2 &y)
1943 {
1944 unsigned int precision = get_binary_precision (x, y);
1945 WIDE_INT_REF_FOR (T1) xi (x, precision);
1946 WIDE_INT_REF_FOR (T2) yi (y, precision);
1947 /* Optimize comparisons with constants. */
1948 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1949 {
1950 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1951 if (xi.len != 1)
1952 return 1;
1953 /* Otherwise compare directly. */
1954 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1955 unsigned HOST_WIDE_INT yl = yi.val[0];
1956 return xl < yl ? -1 : xl > yl;
1957 }
1958 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1959 {
1960 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1961 if (yi.len != 1)
1962 return -1;
1963 /* Otherwise compare directly. */
1964 unsigned HOST_WIDE_INT xl = xi.val[0];
1965 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1966 return xl < yl ? -1 : xl > yl;
1967 }
1968 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1969 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1970 values does not change the result. */
1971 if (__builtin_expect (xi.len + yi.len == 2, true))
1972 {
1973 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1974 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1975 return xl < yl ? -1 : xl > yl;
1976 }
1977 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
1978 }
1979
1980 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1981 X and Y indicated by SGN. */
1982 template <typename T1, typename T2>
1983 inline int
1984 wi::cmp (const T1 &x, const T2 &y, signop sgn)
1985 {
1986 if (sgn == SIGNED)
1987 return cmps (x, y);
1988 else
1989 return cmpu (x, y);
1990 }
1991
1992 /* Return ~x. */
1993 template <typename T>
1994 inline WI_UNARY_RESULT (T)
1995 wi::bit_not (const T &x)
1996 {
1997 WI_UNARY_RESULT_VAR (result, val, T, x);
1998 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
1999 for (unsigned int i = 0; i < xi.len; ++i)
2000 val[i] = ~xi.val[i];
2001 result.set_len (xi.len);
2002 return result;
2003 }
2004
2005 /* Return -x. */
2006 template <typename T>
2007 inline WI_UNARY_RESULT (T)
2008 wi::neg (const T &x)
2009 {
2010 return sub (0, x);
2011 }
2012
2013 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2014 template <typename T>
2015 inline WI_UNARY_RESULT (T)
2016 wi::neg (const T &x, bool *overflow)
2017 {
2018 *overflow = only_sign_bit_p (x);
2019 return sub (0, x);
2020 }
2021
2022 /* Return the absolute value of x. */
2023 template <typename T>
2024 inline WI_UNARY_RESULT (T)
2025 wi::abs (const T &x)
2026 {
2027 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2028 }
2029
2030 /* Return the result of sign-extending the low OFFSET bits of X. */
2031 template <typename T>
2032 inline WI_UNARY_RESULT (T)
2033 wi::sext (const T &x, unsigned int offset)
2034 {
2035 WI_UNARY_RESULT_VAR (result, val, T, x);
2036 unsigned int precision = get_precision (result);
2037 WIDE_INT_REF_FOR (T) xi (x, precision);
2038
2039 if (offset <= HOST_BITS_PER_WIDE_INT)
2040 {
2041 val[0] = sext_hwi (xi.ulow (), offset);
2042 result.set_len (1, true);
2043 }
2044 else
2045 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2046 return result;
2047 }
2048
2049 /* Return the result of zero-extending the low OFFSET bits of X. */
2050 template <typename T>
2051 inline WI_UNARY_RESULT (T)
2052 wi::zext (const T &x, unsigned int offset)
2053 {
2054 WI_UNARY_RESULT_VAR (result, val, T, x);
2055 unsigned int precision = get_precision (result);
2056 WIDE_INT_REF_FOR (T) xi (x, precision);
2057
2058 /* This is not just an optimization, it is actually required to
2059 maintain canonization. */
2060 if (offset >= precision)
2061 {
2062 wi::copy (result, xi);
2063 return result;
2064 }
2065
2066 /* In these cases we know that at least the top bit will be clear,
2067 so no sign extension is necessary. */
2068 if (offset < HOST_BITS_PER_WIDE_INT)
2069 {
2070 val[0] = zext_hwi (xi.ulow (), offset);
2071 result.set_len (1, true);
2072 }
2073 else
2074 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2075 return result;
2076 }
2077
2078 /* Return the result of extending the low OFFSET bits of X according to
2079 signedness SGN. */
2080 template <typename T>
2081 inline WI_UNARY_RESULT (T)
2082 wi::ext (const T &x, unsigned int offset, signop sgn)
2083 {
2084 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2085 }
2086
2087 /* Return an integer that represents X | (1 << bit). */
2088 template <typename T>
2089 inline WI_UNARY_RESULT (T)
2090 wi::set_bit (const T &x, unsigned int bit)
2091 {
2092 WI_UNARY_RESULT_VAR (result, val, T, x);
2093 unsigned int precision = get_precision (result);
2094 WIDE_INT_REF_FOR (T) xi (x, precision);
2095 if (precision <= HOST_BITS_PER_WIDE_INT)
2096 {
2097 val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
2098 result.set_len (1);
2099 }
2100 else
2101 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2102 return result;
2103 }
2104
2105 /* Return the mininum of X and Y, treating them both as having
2106 signedness SGN. */
2107 template <typename T1, typename T2>
2108 inline WI_BINARY_RESULT (T1, T2)
2109 wi::min (const T1 &x, const T2 &y, signop sgn)
2110 {
2111 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2112 unsigned int precision = get_precision (result);
2113 if (wi::le_p (x, y, sgn))
2114 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2115 else
2116 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2117 return result;
2118 }
2119
2120 /* Return the minimum of X and Y, treating both as signed values. */
2121 template <typename T1, typename T2>
2122 inline WI_BINARY_RESULT (T1, T2)
2123 wi::smin (const T1 &x, const T2 &y)
2124 {
2125 return wi::min (x, y, SIGNED);
2126 }
2127
2128 /* Return the minimum of X and Y, treating both as unsigned values. */
2129 template <typename T1, typename T2>
2130 inline WI_BINARY_RESULT (T1, T2)
2131 wi::umin (const T1 &x, const T2 &y)
2132 {
2133 return wi::min (x, y, UNSIGNED);
2134 }
2135
2136 /* Return the maxinum of X and Y, treating them both as having
2137 signedness SGN. */
2138 template <typename T1, typename T2>
2139 inline WI_BINARY_RESULT (T1, T2)
2140 wi::max (const T1 &x, const T2 &y, signop sgn)
2141 {
2142 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2143 unsigned int precision = get_precision (result);
2144 if (wi::ge_p (x, y, sgn))
2145 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2146 else
2147 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2148 return result;
2149 }
2150
2151 /* Return the maximum of X and Y, treating both as signed values. */
2152 template <typename T1, typename T2>
2153 inline WI_BINARY_RESULT (T1, T2)
2154 wi::smax (const T1 &x, const T2 &y)
2155 {
2156 return wi::max (x, y, SIGNED);
2157 }
2158
2159 /* Return the maximum of X and Y, treating both as unsigned values. */
2160 template <typename T1, typename T2>
2161 inline WI_BINARY_RESULT (T1, T2)
2162 wi::umax (const T1 &x, const T2 &y)
2163 {
2164 return wi::max (x, y, UNSIGNED);
2165 }
2166
2167 /* Return X & Y. */
2168 template <typename T1, typename T2>
2169 inline WI_BINARY_RESULT (T1, T2)
2170 wi::bit_and (const T1 &x, const T2 &y)
2171 {
2172 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2173 unsigned int precision = get_precision (result);
2174 WIDE_INT_REF_FOR (T1) xi (x, precision);
2175 WIDE_INT_REF_FOR (T2) yi (y, precision);
2176 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2177 if (__builtin_expect (xi.len + yi.len == 2, true))
2178 {
2179 val[0] = xi.ulow () & yi.ulow ();
2180 result.set_len (1, is_sign_extended);
2181 }
2182 else
2183 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2184 precision), is_sign_extended);
2185 return result;
2186 }
2187
2188 /* Return X & ~Y. */
2189 template <typename T1, typename T2>
2190 inline WI_BINARY_RESULT (T1, T2)
2191 wi::bit_and_not (const T1 &x, const T2 &y)
2192 {
2193 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2194 unsigned int precision = get_precision (result);
2195 WIDE_INT_REF_FOR (T1) xi (x, precision);
2196 WIDE_INT_REF_FOR (T2) yi (y, precision);
2197 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2198 if (__builtin_expect (xi.len + yi.len == 2, true))
2199 {
2200 val[0] = xi.ulow () & ~yi.ulow ();
2201 result.set_len (1, is_sign_extended);
2202 }
2203 else
2204 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2205 precision), is_sign_extended);
2206 return result;
2207 }
2208
2209 /* Return X | Y. */
2210 template <typename T1, typename T2>
2211 inline WI_BINARY_RESULT (T1, T2)
2212 wi::bit_or (const T1 &x, const T2 &y)
2213 {
2214 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2215 unsigned int precision = get_precision (result);
2216 WIDE_INT_REF_FOR (T1) xi (x, precision);
2217 WIDE_INT_REF_FOR (T2) yi (y, precision);
2218 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2219 if (__builtin_expect (xi.len + yi.len == 2, true))
2220 {
2221 val[0] = xi.ulow () | yi.ulow ();
2222 result.set_len (1, is_sign_extended);
2223 }
2224 else
2225 result.set_len (or_large (val, xi.val, xi.len,
2226 yi.val, yi.len, precision), is_sign_extended);
2227 return result;
2228 }
2229
2230 /* Return X | ~Y. */
2231 template <typename T1, typename T2>
2232 inline WI_BINARY_RESULT (T1, T2)
2233 wi::bit_or_not (const T1 &x, const T2 &y)
2234 {
2235 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2236 unsigned int precision = get_precision (result);
2237 WIDE_INT_REF_FOR (T1) xi (x, precision);
2238 WIDE_INT_REF_FOR (T2) yi (y, precision);
2239 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2240 if (__builtin_expect (xi.len + yi.len == 2, true))
2241 {
2242 val[0] = xi.ulow () | ~yi.ulow ();
2243 result.set_len (1, is_sign_extended);
2244 }
2245 else
2246 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2247 precision), is_sign_extended);
2248 return result;
2249 }
2250
2251 /* Return X ^ Y. */
2252 template <typename T1, typename T2>
2253 inline WI_BINARY_RESULT (T1, T2)
2254 wi::bit_xor (const T1 &x, const T2 &y)
2255 {
2256 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2257 unsigned int precision = get_precision (result);
2258 WIDE_INT_REF_FOR (T1) xi (x, precision);
2259 WIDE_INT_REF_FOR (T2) yi (y, precision);
2260 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2261 if (__builtin_expect (xi.len + yi.len == 2, true))
2262 {
2263 val[0] = xi.ulow () ^ yi.ulow ();
2264 result.set_len (1, is_sign_extended);
2265 }
2266 else
2267 result.set_len (xor_large (val, xi.val, xi.len,
2268 yi.val, yi.len, precision), is_sign_extended);
2269 return result;
2270 }
2271
2272 /* Return X + Y. */
2273 template <typename T1, typename T2>
2274 inline WI_BINARY_RESULT (T1, T2)
2275 wi::add (const T1 &x, const T2 &y)
2276 {
2277 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2278 unsigned int precision = get_precision (result);
2279 WIDE_INT_REF_FOR (T1) xi (x, precision);
2280 WIDE_INT_REF_FOR (T2) yi (y, precision);
2281 if (precision <= HOST_BITS_PER_WIDE_INT)
2282 {
2283 val[0] = xi.ulow () + yi.ulow ();
2284 result.set_len (1);
2285 }
2286 /* If the precision is known at compile time to be greater than
2287 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2288 knowing that (a) all bits in those HWIs are significant and
2289 (b) the result has room for at least two HWIs. This provides
2290 a fast path for things like offset_int and widest_int.
2291
2292 The STATIC_CONSTANT_P test prevents this path from being
2293 used for wide_ints. wide_ints with precisions greater than
2294 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2295 point handling them inline. */
2296 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2297 && __builtin_expect (xi.len + yi.len == 2, true))
2298 {
2299 unsigned HOST_WIDE_INT xl = xi.ulow ();
2300 unsigned HOST_WIDE_INT yl = yi.ulow ();
2301 unsigned HOST_WIDE_INT resultl = xl + yl;
2302 val[0] = resultl;
2303 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2304 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2305 >> (HOST_BITS_PER_WIDE_INT - 1)));
2306 }
2307 else
2308 result.set_len (add_large (val, xi.val, xi.len,
2309 yi.val, yi.len, precision,
2310 UNSIGNED, 0));
2311 return result;
2312 }
2313
2314 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2315 and indicate in *OVERFLOW whether the operation overflowed. */
2316 template <typename T1, typename T2>
2317 inline WI_BINARY_RESULT (T1, T2)
2318 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2319 {
2320 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2321 unsigned int precision = get_precision (result);
2322 WIDE_INT_REF_FOR (T1) xi (x, precision);
2323 WIDE_INT_REF_FOR (T2) yi (y, precision);
2324 if (precision <= HOST_BITS_PER_WIDE_INT)
2325 {
2326 unsigned HOST_WIDE_INT xl = xi.ulow ();
2327 unsigned HOST_WIDE_INT yl = yi.ulow ();
2328 unsigned HOST_WIDE_INT resultl = xl + yl;
2329 if (sgn == SIGNED)
2330 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2331 >> (precision - 1)) & 1;
2332 else
2333 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2334 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2335 val[0] = resultl;
2336 result.set_len (1);
2337 }
2338 else
2339 result.set_len (add_large (val, xi.val, xi.len,
2340 yi.val, yi.len, precision,
2341 sgn, overflow));
2342 return result;
2343 }
2344
2345 /* Return X - Y. */
2346 template <typename T1, typename T2>
2347 inline WI_BINARY_RESULT (T1, T2)
2348 wi::sub (const T1 &x, const T2 &y)
2349 {
2350 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2351 unsigned int precision = get_precision (result);
2352 WIDE_INT_REF_FOR (T1) xi (x, precision);
2353 WIDE_INT_REF_FOR (T2) yi (y, precision);
2354 if (precision <= HOST_BITS_PER_WIDE_INT)
2355 {
2356 val[0] = xi.ulow () - yi.ulow ();
2357 result.set_len (1);
2358 }
2359 /* If the precision is known at compile time to be greater than
2360 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2361 knowing that (a) all bits in those HWIs are significant and
2362 (b) the result has room for at least two HWIs. This provides
2363 a fast path for things like offset_int and widest_int.
2364
2365 The STATIC_CONSTANT_P test prevents this path from being
2366 used for wide_ints. wide_ints with precisions greater than
2367 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2368 point handling them inline. */
2369 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2370 && __builtin_expect (xi.len + yi.len == 2, true))
2371 {
2372 unsigned HOST_WIDE_INT xl = xi.ulow ();
2373 unsigned HOST_WIDE_INT yl = yi.ulow ();
2374 unsigned HOST_WIDE_INT resultl = xl - yl;
2375 val[0] = resultl;
2376 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2377 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2378 >> (HOST_BITS_PER_WIDE_INT - 1)));
2379 }
2380 else
2381 result.set_len (sub_large (val, xi.val, xi.len,
2382 yi.val, yi.len, precision,
2383 UNSIGNED, 0));
2384 return result;
2385 }
2386
2387 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2388 and indicate in *OVERFLOW whether the operation overflowed. */
2389 template <typename T1, typename T2>
2390 inline WI_BINARY_RESULT (T1, T2)
2391 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2392 {
2393 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2394 unsigned int precision = get_precision (result);
2395 WIDE_INT_REF_FOR (T1) xi (x, precision);
2396 WIDE_INT_REF_FOR (T2) yi (y, precision);
2397 if (precision <= HOST_BITS_PER_WIDE_INT)
2398 {
2399 unsigned HOST_WIDE_INT xl = xi.ulow ();
2400 unsigned HOST_WIDE_INT yl = yi.ulow ();
2401 unsigned HOST_WIDE_INT resultl = xl - yl;
2402 if (sgn == SIGNED)
2403 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2404 else
2405 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2406 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2407 val[0] = resultl;
2408 result.set_len (1);
2409 }
2410 else
2411 result.set_len (sub_large (val, xi.val, xi.len,
2412 yi.val, yi.len, precision,
2413 sgn, overflow));
2414 return result;
2415 }
2416
2417 /* Return X * Y. */
2418 template <typename T1, typename T2>
2419 inline WI_BINARY_RESULT (T1, T2)
2420 wi::mul (const T1 &x, const T2 &y)
2421 {
2422 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2423 unsigned int precision = get_precision (result);
2424 WIDE_INT_REF_FOR (T1) xi (x, precision);
2425 WIDE_INT_REF_FOR (T2) yi (y, precision);
2426 if (precision <= HOST_BITS_PER_WIDE_INT)
2427 {
2428 val[0] = xi.ulow () * yi.ulow ();
2429 result.set_len (1);
2430 }
2431 else
2432 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2433 precision, UNSIGNED, 0, false));
2434 return result;
2435 }
2436
2437 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2438 and indicate in *OVERFLOW whether the operation overflowed. */
2439 template <typename T1, typename T2>
2440 inline WI_BINARY_RESULT (T1, T2)
2441 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2442 {
2443 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2444 unsigned int precision = get_precision (result);
2445 WIDE_INT_REF_FOR (T1) xi (x, precision);
2446 WIDE_INT_REF_FOR (T2) yi (y, precision);
2447 result.set_len (mul_internal (val, xi.val, xi.len,
2448 yi.val, yi.len, precision,
2449 sgn, overflow, false));
2450 return result;
2451 }
2452
2453 /* Return X * Y, treating both X and Y as signed values. Indicate in
2454 *OVERFLOW whether the operation overflowed. */
2455 template <typename T1, typename T2>
2456 inline WI_BINARY_RESULT (T1, T2)
2457 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2458 {
2459 return mul (x, y, SIGNED, overflow);
2460 }
2461
2462 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2463 *OVERFLOW whether the operation overflowed. */
2464 template <typename T1, typename T2>
2465 inline WI_BINARY_RESULT (T1, T2)
2466 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2467 {
2468 return mul (x, y, UNSIGNED, overflow);
2469 }
2470
2471 /* Perform a widening multiplication of X and Y, extending the values
2472 according to SGN, and return the high part of the result. */
2473 template <typename T1, typename T2>
2474 inline WI_BINARY_RESULT (T1, T2)
2475 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2476 {
2477 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2478 unsigned int precision = get_precision (result);
2479 WIDE_INT_REF_FOR (T1) xi (x, precision);
2480 WIDE_INT_REF_FOR (T2) yi (y, precision);
2481 result.set_len (mul_internal (val, xi.val, xi.len,
2482 yi.val, yi.len, precision,
2483 sgn, 0, true));
2484 return result;
2485 }
2486
2487 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2488 signedness given by SGN. Indicate in *OVERFLOW if the result
2489 overflows. */
2490 template <typename T1, typename T2>
2491 inline WI_BINARY_RESULT (T1, T2)
2492 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2493 {
2494 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2495 unsigned int precision = get_precision (quotient);
2496 WIDE_INT_REF_FOR (T1) xi (x, precision);
2497 WIDE_INT_REF_FOR (T2) yi (y);
2498
2499 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2500 precision,
2501 yi.val, yi.len, yi.precision,
2502 sgn, overflow));
2503 return quotient;
2504 }
2505
2506 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2507 template <typename T1, typename T2>
2508 inline WI_BINARY_RESULT (T1, T2)
2509 wi::sdiv_trunc (const T1 &x, const T2 &y)
2510 {
2511 return div_trunc (x, y, SIGNED);
2512 }
2513
2514 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2515 template <typename T1, typename T2>
2516 inline WI_BINARY_RESULT (T1, T2)
2517 wi::udiv_trunc (const T1 &x, const T2 &y)
2518 {
2519 return div_trunc (x, y, UNSIGNED);
2520 }
2521
2522 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2523 signedness given by SGN. Indicate in *OVERFLOW if the result
2524 overflows. */
2525 template <typename T1, typename T2>
2526 inline WI_BINARY_RESULT (T1, T2)
2527 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2528 {
2529 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2530 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2531 unsigned int precision = get_precision (quotient);
2532 WIDE_INT_REF_FOR (T1) xi (x, precision);
2533 WIDE_INT_REF_FOR (T2) yi (y);
2534
2535 unsigned int remainder_len;
2536 quotient.set_len (divmod_internal (quotient_val,
2537 &remainder_len, remainder_val,
2538 xi.val, xi.len, precision,
2539 yi.val, yi.len, yi.precision, sgn,
2540 overflow));
2541 remainder.set_len (remainder_len);
2542 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2543 return quotient - 1;
2544 return quotient;
2545 }
2546
2547 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2548 template <typename T1, typename T2>
2549 inline WI_BINARY_RESULT (T1, T2)
2550 wi::sdiv_floor (const T1 &x, const T2 &y)
2551 {
2552 return div_floor (x, y, SIGNED);
2553 }
2554
2555 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2556 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2557 template <typename T1, typename T2>
2558 inline WI_BINARY_RESULT (T1, T2)
2559 wi::udiv_floor (const T1 &x, const T2 &y)
2560 {
2561 return div_floor (x, y, UNSIGNED);
2562 }
2563
2564 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2565 signedness given by SGN. Indicate in *OVERFLOW if the result
2566 overflows. */
2567 template <typename T1, typename T2>
2568 inline WI_BINARY_RESULT (T1, T2)
2569 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2570 {
2571 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2572 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2573 unsigned int precision = get_precision (quotient);
2574 WIDE_INT_REF_FOR (T1) xi (x, precision);
2575 WIDE_INT_REF_FOR (T2) yi (y);
2576
2577 unsigned int remainder_len;
2578 quotient.set_len (divmod_internal (quotient_val,
2579 &remainder_len, remainder_val,
2580 xi.val, xi.len, precision,
2581 yi.val, yi.len, yi.precision, sgn,
2582 overflow));
2583 remainder.set_len (remainder_len);
2584 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2585 return quotient + 1;
2586 return quotient;
2587 }
2588
2589 /* Return X / Y, rouding towards nearest with ties away from zero.
2590 Treat X and Y as having the signedness given by SGN. Indicate
2591 in *OVERFLOW if the result overflows. */
2592 template <typename T1, typename T2>
2593 inline WI_BINARY_RESULT (T1, T2)
2594 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2595 {
2596 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2597 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2598 unsigned int precision = get_precision (quotient);
2599 WIDE_INT_REF_FOR (T1) xi (x, precision);
2600 WIDE_INT_REF_FOR (T2) yi (y);
2601
2602 unsigned int remainder_len;
2603 quotient.set_len (divmod_internal (quotient_val,
2604 &remainder_len, remainder_val,
2605 xi.val, xi.len, precision,
2606 yi.val, yi.len, yi.precision, sgn,
2607 overflow));
2608 remainder.set_len (remainder_len);
2609
2610 if (remainder != 0)
2611 {
2612 if (sgn == SIGNED)
2613 {
2614 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2615 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2616 {
2617 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2618 return quotient - 1;
2619 else
2620 return quotient + 1;
2621 }
2622 }
2623 else
2624 {
2625 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2626 return quotient + 1;
2627 }
2628 }
2629 return quotient;
2630 }
2631
2632 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2633 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2634 template <typename T1, typename T2>
2635 inline WI_BINARY_RESULT (T1, T2)
2636 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2637 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2638 {
2639 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2640 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2641 unsigned int precision = get_precision (quotient);
2642 WIDE_INT_REF_FOR (T1) xi (x, precision);
2643 WIDE_INT_REF_FOR (T2) yi (y);
2644
2645 unsigned int remainder_len;
2646 quotient.set_len (divmod_internal (quotient_val,
2647 &remainder_len, remainder_val,
2648 xi.val, xi.len, precision,
2649 yi.val, yi.len, yi.precision, sgn, 0));
2650 remainder.set_len (remainder_len);
2651
2652 *remainder_ptr = remainder;
2653 return quotient;
2654 }
2655
2656 /* Compute X / Y, rouding towards 0, and return the remainder.
2657 Treat X and Y as having the signedness given by SGN. Indicate
2658 in *OVERFLOW if the division overflows. */
2659 template <typename T1, typename T2>
2660 inline WI_BINARY_RESULT (T1, T2)
2661 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2662 {
2663 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2664 unsigned int precision = get_precision (remainder);
2665 WIDE_INT_REF_FOR (T1) xi (x, precision);
2666 WIDE_INT_REF_FOR (T2) yi (y);
2667
2668 unsigned int remainder_len;
2669 divmod_internal (0, &remainder_len, remainder_val,
2670 xi.val, xi.len, precision,
2671 yi.val, yi.len, yi.precision, sgn, overflow);
2672 remainder.set_len (remainder_len);
2673
2674 return remainder;
2675 }
2676
2677 /* Compute X / Y, rouding towards 0, and return the remainder.
2678 Treat X and Y as signed values. */
2679 template <typename T1, typename T2>
2680 inline WI_BINARY_RESULT (T1, T2)
2681 wi::smod_trunc (const T1 &x, const T2 &y)
2682 {
2683 return mod_trunc (x, y, SIGNED);
2684 }
2685
2686 /* Compute X / Y, rouding towards 0, and return the remainder.
2687 Treat X and Y as unsigned values. */
2688 template <typename T1, typename T2>
2689 inline WI_BINARY_RESULT (T1, T2)
2690 wi::umod_trunc (const T1 &x, const T2 &y)
2691 {
2692 return mod_trunc (x, y, UNSIGNED);
2693 }
2694
2695 /* Compute X / Y, rouding towards -inf, and return the remainder.
2696 Treat X and Y as having the signedness given by SGN. Indicate
2697 in *OVERFLOW if the division overflows. */
2698 template <typename T1, typename T2>
2699 inline WI_BINARY_RESULT (T1, T2)
2700 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2701 {
2702 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2703 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2704 unsigned int precision = get_precision (quotient);
2705 WIDE_INT_REF_FOR (T1) xi (x, precision);
2706 WIDE_INT_REF_FOR (T2) yi (y);
2707
2708 unsigned int remainder_len;
2709 quotient.set_len (divmod_internal (quotient_val,
2710 &remainder_len, remainder_val,
2711 xi.val, xi.len, precision,
2712 yi.val, yi.len, yi.precision, sgn,
2713 overflow));
2714 remainder.set_len (remainder_len);
2715
2716 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2717 return remainder + y;
2718 return remainder;
2719 }
2720
2721 /* Compute X / Y, rouding towards -inf, and return the remainder.
2722 Treat X and Y as unsigned values. */
2723 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2724 template <typename T1, typename T2>
2725 inline WI_BINARY_RESULT (T1, T2)
2726 wi::umod_floor (const T1 &x, const T2 &y)
2727 {
2728 return mod_floor (x, y, UNSIGNED);
2729 }
2730
2731 /* Compute X / Y, rouding towards +inf, and return the remainder.
2732 Treat X and Y as having the signedness given by SGN. Indicate
2733 in *OVERFLOW if the division overflows. */
2734 template <typename T1, typename T2>
2735 inline WI_BINARY_RESULT (T1, T2)
2736 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2737 {
2738 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2739 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2740 unsigned int precision = get_precision (quotient);
2741 WIDE_INT_REF_FOR (T1) xi (x, precision);
2742 WIDE_INT_REF_FOR (T2) yi (y);
2743
2744 unsigned int remainder_len;
2745 quotient.set_len (divmod_internal (quotient_val,
2746 &remainder_len, remainder_val,
2747 xi.val, xi.len, precision,
2748 yi.val, yi.len, yi.precision, sgn,
2749 overflow));
2750 remainder.set_len (remainder_len);
2751
2752 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2753 return remainder - y;
2754 return remainder;
2755 }
2756
2757 /* Compute X / Y, rouding towards nearest with ties away from zero,
2758 and return the remainder. Treat X and Y as having the signedness
2759 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2760 template <typename T1, typename T2>
2761 inline WI_BINARY_RESULT (T1, T2)
2762 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2763 {
2764 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2765 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2766 unsigned int precision = get_precision (quotient);
2767 WIDE_INT_REF_FOR (T1) xi (x, precision);
2768 WIDE_INT_REF_FOR (T2) yi (y);
2769
2770 unsigned int remainder_len;
2771 quotient.set_len (divmod_internal (quotient_val,
2772 &remainder_len, remainder_val,
2773 xi.val, xi.len, precision,
2774 yi.val, yi.len, yi.precision, sgn,
2775 overflow));
2776 remainder.set_len (remainder_len);
2777
2778 if (remainder != 0)
2779 {
2780 if (sgn == SIGNED)
2781 {
2782 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2783 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2784 {
2785 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2786 return remainder + y;
2787 else
2788 return remainder - y;
2789 }
2790 }
2791 else
2792 {
2793 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2794 return remainder - y;
2795 }
2796 }
2797 return remainder;
2798 }
2799
2800 /* Return true if X is a multiple of Y. Treat X and Y as having the
2801 signedness given by SGN. */
2802 template <typename T1, typename T2>
2803 inline bool
2804 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2805 {
2806 return wi::mod_trunc (x, y, sgn) == 0;
2807 }
2808
2809 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2810 Treat X and Y as having the signedness given by SGN. */
2811 template <typename T1, typename T2>
2812 inline bool
2813 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2814 WI_BINARY_RESULT (T1, T2) *res)
2815 {
2816 WI_BINARY_RESULT (T1, T2) remainder;
2817 WI_BINARY_RESULT (T1, T2) quotient
2818 = divmod_trunc (x, y, sgn, &remainder);
2819 if (remainder == 0)
2820 {
2821 *res = quotient;
2822 return true;
2823 }
2824 return false;
2825 }
2826
2827 /* Return X << Y. Return 0 if Y is greater than or equal to
2828 the precision of X. */
2829 template <typename T1, typename T2>
2830 inline WI_UNARY_RESULT (T1)
2831 wi::lshift (const T1 &x, const T2 &y)
2832 {
2833 WI_UNARY_RESULT_VAR (result, val, T1, x);
2834 unsigned int precision = get_precision (result);
2835 WIDE_INT_REF_FOR (T1) xi (x, precision);
2836 WIDE_INT_REF_FOR (T2) yi (y);
2837 /* Handle the simple cases quickly. */
2838 if (geu_p (yi, precision))
2839 {
2840 val[0] = 0;
2841 result.set_len (1);
2842 }
2843 else
2844 {
2845 unsigned int shift = yi.to_uhwi ();
2846 /* For fixed-precision integers like offset_int and widest_int,
2847 handle the case where the shift value is constant and the
2848 result is a single nonnegative HWI (meaning that we don't
2849 need to worry about val[1]). This is particularly common
2850 for converting a byte count to a bit count.
2851
2852 For variable-precision integers like wide_int, handle HWI
2853 and sub-HWI integers inline. */
2854 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2855 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2856 && xi.len == 1
2857 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2858 HOST_WIDE_INT_MAX >> shift))
2859 : precision <= HOST_BITS_PER_WIDE_INT)
2860 {
2861 val[0] = xi.ulow () << shift;
2862 result.set_len (1);
2863 }
2864 else
2865 result.set_len (lshift_large (val, xi.val, xi.len,
2866 precision, shift));
2867 }
2868 return result;
2869 }
2870
2871 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2872 or equal to the precision of X. */
2873 template <typename T1, typename T2>
2874 inline WI_UNARY_RESULT (T1)
2875 wi::lrshift (const T1 &x, const T2 &y)
2876 {
2877 WI_UNARY_RESULT_VAR (result, val, T1, x);
2878 /* Do things in the precision of the input rather than the output,
2879 since the result can be no larger than that. */
2880 WIDE_INT_REF_FOR (T1) xi (x);
2881 WIDE_INT_REF_FOR (T2) yi (y);
2882 /* Handle the simple cases quickly. */
2883 if (geu_p (yi, xi.precision))
2884 {
2885 val[0] = 0;
2886 result.set_len (1);
2887 }
2888 else
2889 {
2890 unsigned int shift = yi.to_uhwi ();
2891 /* For fixed-precision integers like offset_int and widest_int,
2892 handle the case where the shift value is constant and the
2893 shifted value is a single nonnegative HWI (meaning that all
2894 bits above the HWI are zero). This is particularly common
2895 for converting a bit count to a byte count.
2896
2897 For variable-precision integers like wide_int, handle HWI
2898 and sub-HWI integers inline. */
2899 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2900 ? xi.len == 1 && xi.val[0] >= 0
2901 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2902 {
2903 val[0] = xi.to_uhwi () >> shift;
2904 result.set_len (1);
2905 }
2906 else
2907 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2908 get_precision (result), shift));
2909 }
2910 return result;
2911 }
2912
2913 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2914 Y is greater than or equal to the precision of X. */
2915 template <typename T1, typename T2>
2916 inline WI_UNARY_RESULT (T1)
2917 wi::arshift (const T1 &x, const T2 &y)
2918 {
2919 WI_UNARY_RESULT_VAR (result, val, T1, x);
2920 /* Do things in the precision of the input rather than the output,
2921 since the result can be no larger than that. */
2922 WIDE_INT_REF_FOR (T1) xi (x);
2923 WIDE_INT_REF_FOR (T2) yi (y);
2924 /* Handle the simple cases quickly. */
2925 if (geu_p (yi, xi.precision))
2926 {
2927 val[0] = sign_mask (x);
2928 result.set_len (1);
2929 }
2930 else
2931 {
2932 unsigned int shift = yi.to_uhwi ();
2933 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2934 {
2935 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2936 result.set_len (1, true);
2937 }
2938 else
2939 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
2940 get_precision (result), shift));
2941 }
2942 return result;
2943 }
2944
2945 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2946 logical shift otherwise. */
2947 template <typename T1, typename T2>
2948 inline WI_UNARY_RESULT (T1)
2949 wi::rshift (const T1 &x, const T2 &y, signop sgn)
2950 {
2951 if (sgn == UNSIGNED)
2952 return lrshift (x, y);
2953 else
2954 return arshift (x, y);
2955 }
2956
2957 /* Return the result of rotating the low WIDTH bits of X left by Y
2958 bits and zero-extending the result. Use a full-width rotate if
2959 WIDTH is zero. */
2960 template <typename T1, typename T2>
2961 WI_UNARY_RESULT (T1)
2962 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
2963 {
2964 unsigned int precision = get_binary_precision (x, x);
2965 if (width == 0)
2966 width = precision;
2967 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2968 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
2969 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
2970 if (width != precision)
2971 return wi::zext (left, width) | wi::zext (right, width);
2972 return left | right;
2973 }
2974
2975 /* Return the result of rotating the low WIDTH bits of X right by Y
2976 bits and zero-extending the result. Use a full-width rotate if
2977 WIDTH is zero. */
2978 template <typename T1, typename T2>
2979 WI_UNARY_RESULT (T1)
2980 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
2981 {
2982 unsigned int precision = get_binary_precision (x, x);
2983 if (width == 0)
2984 width = precision;
2985 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2986 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
2987 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
2988 if (width != precision)
2989 return wi::zext (left, width) | wi::zext (right, width);
2990 return left | right;
2991 }
2992
2993 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
2994 is odd. */
2995 inline int
2996 wi::parity (const wide_int_ref &x)
2997 {
2998 return popcount (x) & 1;
2999 }
3000
3001 /* Extract WIDTH bits from X, starting at BITPOS. */
3002 template <typename T>
3003 inline unsigned HOST_WIDE_INT
3004 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3005 {
3006 unsigned precision = get_precision (x);
3007 if (precision < bitpos + width)
3008 precision = bitpos + width;
3009 WIDE_INT_REF_FOR (T) xi (x, precision);
3010
3011 /* Handle this rare case after the above, so that we assert about
3012 bogus BITPOS values. */
3013 if (width == 0)
3014 return 0;
3015
3016 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3017 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3018 unsigned HOST_WIDE_INT res = xi.elt (start);
3019 res >>= shift;
3020 if (shift + width > HOST_BITS_PER_WIDE_INT)
3021 {
3022 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3023 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3024 }
3025 return zext_hwi (res, width);
3026 }
3027
3028 /* Return the minimum precision needed to store X with sign SGN. */
3029 template <typename T>
3030 inline unsigned int
3031 wi::min_precision (const T &x, signop sgn)
3032 {
3033 if (sgn == SIGNED)
3034 return get_precision (x) - clrsb (x);
3035 else
3036 return get_precision (x) - clz (x);
3037 }
3038
3039 template<typename T>
3040 void
3041 gt_ggc_mx (generic_wide_int <T> *)
3042 {
3043 }
3044
3045 template<typename T>
3046 void
3047 gt_pch_nx (generic_wide_int <T> *)
3048 {
3049 }
3050
3051 template<typename T>
3052 void
3053 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3054 {
3055 }
3056
3057 template<int N>
3058 void
3059 gt_ggc_mx (trailing_wide_ints <N> *)
3060 {
3061 }
3062
3063 template<int N>
3064 void
3065 gt_pch_nx (trailing_wide_ints <N> *)
3066 {
3067 }
3068
3069 template<int N>
3070 void
3071 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3072 {
3073 }
3074
3075 namespace wi
3076 {
3077 /* Used for overloaded functions in which the only other acceptable
3078 scalar type is a pointer. It stops a plain 0 from being treated
3079 as a null pointer. */
3080 struct never_used1 {};
3081 struct never_used2 {};
3082
3083 wide_int min_value (unsigned int, signop);
3084 wide_int min_value (never_used1 *);
3085 wide_int min_value (never_used2 *);
3086 wide_int max_value (unsigned int, signop);
3087 wide_int max_value (never_used1 *);
3088 wide_int max_value (never_used2 *);
3089
3090 /* FIXME: this is target dependent, so should be elsewhere.
3091 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3092 wide_int from_buffer (const unsigned char *, unsigned int);
3093
3094 #ifndef GENERATOR_FILE
3095 void to_mpz (const wide_int_ref &, mpz_t, signop);
3096 #endif
3097
3098 wide_int mask (unsigned int, bool, unsigned int);
3099 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3100 wide_int set_bit_in_zero (unsigned int, unsigned int);
3101 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3102 unsigned int);
3103
3104 template <typename T>
3105 T mask (unsigned int, bool);
3106
3107 template <typename T>
3108 T shifted_mask (unsigned int, unsigned int, bool);
3109
3110 template <typename T>
3111 T set_bit_in_zero (unsigned int);
3112
3113 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3114 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3115 bool, unsigned int);
3116 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3117 unsigned int, unsigned int, bool);
3118 }
3119
3120 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3121 and the other bits are clear, or the inverse if NEGATE_P. */
3122 inline wide_int
3123 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3124 {
3125 wide_int result = wide_int::create (precision);
3126 result.set_len (mask (result.write_val (), width, negate_p, precision));
3127 return result;
3128 }
3129
3130 /* Return a PRECISION-bit integer in which the low START bits are clear,
3131 the next WIDTH bits are set, and the other bits are clear,
3132 or the inverse if NEGATE_P. */
3133 inline wide_int
3134 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3135 unsigned int precision)
3136 {
3137 wide_int result = wide_int::create (precision);
3138 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3139 precision));
3140 return result;
3141 }
3142
3143 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3144 others are clear. */
3145 inline wide_int
3146 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3147 {
3148 return shifted_mask (bit, 1, false, precision);
3149 }
3150
3151 /* Return an integer of type T in which the low WIDTH bits are set
3152 and the other bits are clear, or the inverse if NEGATE_P. */
3153 template <typename T>
3154 inline T
3155 wi::mask (unsigned int width, bool negate_p)
3156 {
3157 STATIC_ASSERT (wi::int_traits<T>::precision);
3158 T result;
3159 result.set_len (mask (result.write_val (), width, negate_p,
3160 wi::int_traits <T>::precision));
3161 return result;
3162 }
3163
3164 /* Return an integer of type T in which the low START bits are clear,
3165 the next WIDTH bits are set, and the other bits are clear, or the
3166 inverse if NEGATE_P. */
3167 template <typename T>
3168 inline T
3169 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3170 {
3171 STATIC_ASSERT (wi::int_traits<T>::precision);
3172 T result;
3173 result.set_len (shifted_mask (result.write_val (), start, width,
3174 negate_p,
3175 wi::int_traits <T>::precision));
3176 return result;
3177 }
3178
3179 /* Return an integer of type T in which bit BIT is set and all the
3180 others are clear. */
3181 template <typename T>
3182 inline T
3183 wi::set_bit_in_zero (unsigned int bit)
3184 {
3185 return shifted_mask <T> (bit, 1, false);
3186 }
3187
3188 #endif /* WIDE_INT_H */