* da.po, es.po: Update.
[official-gcc.git] / gcc / wide-int.h
blob2115b614a9f84eb23964f9a1c0a9fc0775955d2c
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef WIDE_INT_H
21 #define WIDE_INT_H
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).
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
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.
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.
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.
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.
56 2) offset_int. This is a fixed-precision integer that can hold
57 any address offset, measured in either bits or bytes, with at
58 least one extra sign bit. At the moment the maximum address
59 size GCC supports is 64 bits. With 8-bit bytes and an extra
60 sign bit, offset_int therefore needs to have at least 68 bits
61 of precision. We round this up to 128 bits for efficiency.
62 Values of type T are converted to this precision by sign- or
63 zero-extending them based on the signedness of T.
65 The extra sign bit means that offset_int is effectively a signed
66 128-bit integer, i.e. it behaves like int128_t.
68 Since the values are logically signed, there is no need to
69 distinguish between signed and unsigned operations. Sign-sensitive
70 comparison operators <, <=, > and >= are therefore supported.
71 Shift operators << and >> are also supported, with >> being
72 an _arithmetic_ right shift.
74 [ Note that, even though offset_int is effectively int128_t,
75 it can still be useful to use unsigned comparisons like
76 wi::leu_p (a, b) as a more efficient short-hand for
77 "a >= 0 && a <= b". ]
79 3) widest_int. This representation is an approximation of
80 infinite precision math. However, it is not really infinite
81 precision math as in the GMP library. It is really finite
82 precision math where the precision is 4 times the size of the
83 largest integer that the target port can represent.
85 Like offset_int, widest_int is wider than all the values that
86 it needs to represent, so the integers are logically signed.
87 Sign-sensitive comparison operators <, <=, > and >= are supported,
88 as are << and >>.
90 There are several places in the GCC where this should/must be used:
92 * Code that does induction variable optimizations. This code
93 works with induction variables of many different types at the
94 same time. Because of this, it ends up doing many different
95 calculations where the operands are not compatible types. The
96 widest_int makes this easy, because it provides a field where
97 nothing is lost when converting from any variable,
99 * There are a small number of passes that currently use the
100 widest_int that should use the default. These should be
101 changed.
103 There are surprising features of offset_int and widest_int
104 that the users should be careful about:
106 1) Shifts and rotations are just weird. You have to specify a
107 precision in which the shift or rotate is to happen in. The bits
108 above this precision are zeroed. While this is what you
109 want, it is clearly non obvious.
111 2) Larger precision math sometimes does not produce the same
112 answer as would be expected for doing the math at the proper
113 precision. In particular, a multiply followed by a divide will
114 produce a different answer if the first product is larger than
115 what can be represented in the input precision.
117 The offset_int and the widest_int flavors are more expensive
118 than the default wide int, so in addition to the caveats with these
119 two, the default is the prefered representation.
121 All three flavors of wide_int are represented as a vector of
122 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
123 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
124 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
125 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
126 in element 0.
128 The default wide_int contains three fields: the vector (VAL),
129 the precision and a length (LEN). The length is the number of HWIs
130 needed to represent the value. widest_int and offset_int have a
131 constant precision that cannot be changed, so they only store the
132 VAL and LEN fields.
134 Since most integers used in a compiler are small values, it is
135 generally profitable to use a representation of the value that is
136 as small as possible. LEN is used to indicate the number of
137 elements of the vector that are in use. The numbers are stored as
138 sign extended numbers as a means of compression. Leading
139 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
140 as long as they can be reconstructed from the top bit that is being
141 represented.
143 The precision and length of a wide_int are always greater than 0.
144 Any bits in a wide_int above the precision are sign-extended from the
145 most significant bit. For example, a 4-bit value 0x8 is represented as
146 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
147 constants to be represented with undefined bits above the precision.
148 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
149 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
150 and in wider precisions.
152 There are constructors to create the various forms of wide_int from
153 trees, rtl and constants. For trees you can simply say:
155 tree t = ...;
156 wide_int x = t;
158 However, a little more syntax is required for rtl constants since
159 they do not have an explicit precision. To make an rtl into a
160 wide_int, you have to pair it with a mode. The canonical way to do
161 this is with rtx_mode_t as in:
163 rtx r = ...
164 wide_int x = rtx_mode_t (r, mode);
166 Similarly, a wide_int can only be constructed from a host value if
167 the target precision is given explicitly, such as in:
169 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
170 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
172 However, offset_int and widest_int have an inherent precision and so
173 can be initialized directly from a host value:
175 offset_int x = (int) c; // sign-extend C
176 widest_int x = (unsigned int) c; // zero-extend C
178 It is also possible to do arithmetic directly on trees, rtxes and
179 constants. For example:
181 wi::add (t1, t2); // add equal-sized INTEGER_CSTs t1 and t2
182 wi::add (t1, 1); // add 1 to INTEGER_CST t1
183 wi::add (r1, r2); // add equal-sized rtx constants r1 and r2
184 wi::lshift (1, 100); // 1 << 100 as a widest_int
186 Many binary operations place restrictions on the combinations of inputs,
187 using the following rules:
189 - {tree, rtx, wide_int} op {tree, rtx, wide_int} -> wide_int
190 The inputs must be the same precision. The result is a wide_int
191 of the same precision
193 - {tree, rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
194 (un)signed HOST_WIDE_INT op {tree, rtx, wide_int} -> wide_int
195 The HOST_WIDE_INT is extended or truncated to the precision of
196 the other input. The result is a wide_int of the same precision
197 as that input.
199 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
200 The inputs are extended to widest_int precision and produce a
201 widest_int result.
203 - offset_int op offset_int -> offset_int
204 offset_int op (un)signed HOST_WIDE_INT -> offset_int
205 (un)signed HOST_WIDE_INT op offset_int -> offset_int
207 - widest_int op widest_int -> widest_int
208 widest_int op (un)signed HOST_WIDE_INT -> widest_int
209 (un)signed HOST_WIDE_INT op widest_int -> widest_int
211 Other combinations like:
213 - widest_int op offset_int and
214 - wide_int op offset_int
216 are not allowed. The inputs should instead be extended or truncated
217 so that they match.
219 The inputs to comparison functions like wi::eq_p and wi::lts_p
220 follow the same compatibility rules, although their return types
221 are different. Unary functions on X produce the same result as
222 a binary operation X + X. Shift functions X op Y also produce
223 the same result as X + X; the precision of the shift amount Y
224 can be arbitrarily different from X. */
226 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
227 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
228 can accomodate at least 1 more bit so that unsigned numbers of that
229 mode can be represented as a signed value. Note that it is still
230 possible to create fixed_wide_ints that have precisions greater than
231 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
232 double-width multiplication result, for example. */
233 #define WIDE_INT_MAX_ELTS \
234 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
236 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
238 /* This is the max size of any pointer on any machine. It does not
239 seem to be as easy to sniff this out of the machine description as
240 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
241 multiple address sizes and may have different address sizes for
242 different address spaces. However, currently the largest pointer
243 on any platform is 64 bits. When that changes, then it is likely
244 that a target hook should be defined so that targets can make this
245 value larger for those targets. */
246 #define ADDR_MAX_BITSIZE 64
248 /* This is the internal precision used when doing any address
249 arithmetic. The '4' is really 3 + 1. Three of the bits are for
250 the number of extra bits needed to do bit addresses and the other bit
251 is to allow everything to be signed without loosing any precision.
252 Then everything is rounded up to the next HWI for efficiency. */
253 #define ADDR_MAX_PRECISION \
254 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
255 & ~(HOST_BITS_PER_WIDE_INT - 1))
257 /* The number of HWIs needed to store an offset_int. */
258 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
260 /* The type of result produced by a binary operation on types T1 and T2.
261 Defined purely for brevity. */
262 #define WI_BINARY_RESULT(T1, T2) \
263 typename wi::binary_traits <T1, T2>::result_type
265 /* The type of result produced by T1 << T2. Leads to substitution failure
266 if the operation isn't supported. Defined purely for brevity. */
267 #define WI_SIGNED_SHIFT_RESULT(T1, T2) \
268 typename wi::binary_traits <T1, T2>::signed_shift_result_type
270 /* The type of result produced by a signed binary predicate on types T1 and T2.
271 This is bool if signed comparisons make sense for T1 and T2 and leads to
272 substitution failure otherwise. */
273 #define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2) \
274 typename wi::binary_traits <T1, T2>::signed_predicate_result
276 /* The type of result produced by a unary operation on type T. */
277 #define WI_UNARY_RESULT(T) \
278 typename wi::unary_traits <T>::result_type
280 /* Define a variable RESULT to hold the result of a binary operation on
281 X and Y, which have types T1 and T2 respectively. Define VAL to
282 point to the blocks of RESULT. Once the user of the macro has
283 filled in VAL, it should call RESULT.set_len to set the number
284 of initialized blocks. */
285 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
286 WI_BINARY_RESULT (T1, T2) RESULT = \
287 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
288 HOST_WIDE_INT *VAL = RESULT.write_val ()
290 /* Similar for the result of a unary operation on X, which has type T. */
291 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
292 WI_UNARY_RESULT (T) RESULT = \
293 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
294 HOST_WIDE_INT *VAL = RESULT.write_val ()
296 template <typename T> class generic_wide_int;
297 template <int N> class fixed_wide_int_storage;
298 class wide_int_storage;
300 /* An N-bit integer. Until we can use typedef templates, use this instead. */
301 #define FIXED_WIDE_INT(N) \
302 generic_wide_int < fixed_wide_int_storage <N> >
304 typedef generic_wide_int <wide_int_storage> wide_int;
305 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
306 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
308 template <bool SE>
309 struct wide_int_ref_storage;
311 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
313 /* This can be used instead of wide_int_ref if the referenced value is
314 known to have type T. It carries across properties of T's representation,
315 such as whether excess upper bits in a HWI are defined, and can therefore
316 help avoid redundant work.
318 The macro could be replaced with a template typedef, once we're able
319 to use those. */
320 #define WIDE_INT_REF_FOR(T) \
321 generic_wide_int \
322 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
324 namespace wi
326 /* Classifies an integer based on its precision. */
327 enum precision_type {
328 /* The integer has both a precision and defined signedness. This allows
329 the integer to be converted to any width, since we know whether to fill
330 any extra bits with zeros or signs. */
331 FLEXIBLE_PRECISION,
333 /* The integer has a variable precision but no defined signedness. */
334 VAR_PRECISION,
336 /* The integer has a constant precision (known at GCC compile time)
337 and is signed. */
338 CONST_PRECISION
341 /* This class, which has no default implementation, is expected to
342 provide the following members:
344 static const enum precision_type precision_type;
345 Classifies the type of T.
347 static const unsigned int precision;
348 Only defined if precision_type == CONST_PRECISION. Specifies the
349 precision of all integers of type T.
351 static const bool host_dependent_precision;
352 True if the precision of T depends (or can depend) on the host.
354 static unsigned int get_precision (const T &x)
355 Return the number of bits in X.
357 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
358 unsigned int precision, const T &x)
359 Decompose X as a PRECISION-bit integer, returning the associated
360 wi::storage_ref. SCRATCH is available as scratch space if needed.
361 The routine should assert that PRECISION is acceptable. */
362 template <typename T> struct int_traits;
364 /* This class provides a single type, result_type, which specifies the
365 type of integer produced by a binary operation whose inputs have
366 types T1 and T2. The definition should be symmetric. */
367 template <typename T1, typename T2,
368 enum precision_type P1 = int_traits <T1>::precision_type,
369 enum precision_type P2 = int_traits <T2>::precision_type>
370 struct binary_traits;
372 /* The result of a unary operation on T is the same as the result of
373 a binary operation on two values of type T. */
374 template <typename T>
375 struct unary_traits : public binary_traits <T, T> {};
377 /* Specify the result type for each supported combination of binary
378 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
379 mixed, in order to give stronger type checking. When both inputs
380 are CONST_PRECISION, they must have the same precision. */
381 template <typename T1, typename T2>
382 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
384 typedef widest_int result_type;
387 template <typename T1, typename T2>
388 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
390 typedef wide_int result_type;
393 template <typename T1, typename T2>
394 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
396 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
397 so as not to confuse gengtype. */
398 typedef generic_wide_int < fixed_wide_int_storage
399 <int_traits <T2>::precision> > result_type;
400 typedef bool signed_predicate_result;
403 template <typename T1, typename T2>
404 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
406 typedef wide_int result_type;
409 template <typename T1, typename T2>
410 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
412 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
413 so as not to confuse gengtype. */
414 typedef generic_wide_int < fixed_wide_int_storage
415 <int_traits <T1>::precision> > result_type;
416 typedef result_type signed_shift_result_type;
417 typedef bool signed_predicate_result;
420 template <typename T1, typename T2>
421 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
423 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
424 so as not to confuse gengtype. */
425 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
426 typedef generic_wide_int < fixed_wide_int_storage
427 <int_traits <T1>::precision> > result_type;
428 typedef result_type signed_shift_result_type;
429 typedef bool signed_predicate_result;
432 template <typename T1, typename T2>
433 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
435 typedef wide_int result_type;
439 /* Public functions for querying and operating on integers. */
440 namespace wi
442 template <typename T>
443 unsigned int get_precision (const T &);
445 template <typename T1, typename T2>
446 unsigned int get_binary_precision (const T1 &, const T2 &);
448 template <typename T1, typename T2>
449 void copy (T1 &, const T2 &);
451 #define UNARY_PREDICATE \
452 template <typename T> bool
453 #define UNARY_FUNCTION \
454 template <typename T> WI_UNARY_RESULT (T)
455 #define BINARY_PREDICATE \
456 template <typename T1, typename T2> bool
457 #define BINARY_FUNCTION \
458 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
459 #define SHIFT_FUNCTION \
460 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
462 UNARY_PREDICATE fits_shwi_p (const T &);
463 UNARY_PREDICATE fits_uhwi_p (const T &);
464 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
466 template <typename T>
467 HOST_WIDE_INT sign_mask (const T &);
469 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
470 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
471 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
472 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
473 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
474 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
475 BINARY_PREDICATE les_p (const T1 &, const T2 &);
476 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
477 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
478 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
479 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
480 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
481 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
482 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
484 template <typename T1, typename T2>
485 int cmp (const T1 &, const T2 &, signop);
487 template <typename T1, typename T2>
488 int cmps (const T1 &, const T2 &);
490 template <typename T1, typename T2>
491 int cmpu (const T1 &, const T2 &);
493 UNARY_FUNCTION bit_not (const T &);
494 UNARY_FUNCTION neg (const T &);
495 UNARY_FUNCTION neg (const T &, bool *);
496 UNARY_FUNCTION abs (const T &);
497 UNARY_FUNCTION ext (const T &, unsigned int, signop);
498 UNARY_FUNCTION sext (const T &, unsigned int);
499 UNARY_FUNCTION zext (const T &, unsigned int);
500 UNARY_FUNCTION set_bit (const T &, unsigned int);
502 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
503 BINARY_FUNCTION smin (const T1 &, const T2 &);
504 BINARY_FUNCTION umin (const T1 &, const T2 &);
505 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
506 BINARY_FUNCTION smax (const T1 &, const T2 &);
507 BINARY_FUNCTION umax (const T1 &, const T2 &);
509 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
510 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
511 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
512 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
513 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
514 BINARY_FUNCTION add (const T1 &, const T2 &);
515 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
516 BINARY_FUNCTION sub (const T1 &, const T2 &);
517 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
518 BINARY_FUNCTION mul (const T1 &, const T2 &);
519 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
520 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
521 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
522 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
523 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
524 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
525 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
526 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
527 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
528 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
529 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
530 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
531 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
532 WI_BINARY_RESULT (T1, T2) *);
533 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
534 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
535 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
536 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
537 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
538 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
539 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
540 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
542 template <typename T1, typename T2>
543 bool multiple_of_p (const T1 &, const T2 &, signop);
545 template <typename T1, typename T2>
546 bool multiple_of_p (const T1 &, const T2 &, signop,
547 WI_BINARY_RESULT (T1, T2) *);
549 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
550 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
551 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
552 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
553 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
554 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
556 #undef SHIFT_FUNCTION
557 #undef BINARY_PREDICATE
558 #undef BINARY_FUNCTION
559 #undef UNARY_PREDICATE
560 #undef UNARY_FUNCTION
562 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
563 bool only_sign_bit_p (const wide_int_ref &);
564 int clz (const wide_int_ref &);
565 int clrsb (const wide_int_ref &);
566 int ctz (const wide_int_ref &);
567 int exact_log2 (const wide_int_ref &);
568 int floor_log2 (const wide_int_ref &);
569 int ffs (const wide_int_ref &);
570 int popcount (const wide_int_ref &);
571 int parity (const wide_int_ref &);
573 template <typename T>
574 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
576 template <typename T>
577 unsigned int min_precision (const T &, signop);
580 namespace wi
582 /* Contains the components of a decomposed integer for easy, direct
583 access. */
584 struct storage_ref
586 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
588 const HOST_WIDE_INT *val;
589 unsigned int len;
590 unsigned int precision;
592 /* Provide enough trappings for this class to act as storage for
593 generic_wide_int. */
594 unsigned int get_len () const;
595 unsigned int get_precision () const;
596 const HOST_WIDE_INT *get_val () const;
600 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
601 unsigned int len_in,
602 unsigned int precision_in)
603 : val (val_in), len (len_in), precision (precision_in)
607 inline unsigned int
608 wi::storage_ref::get_len () const
610 return len;
613 inline unsigned int
614 wi::storage_ref::get_precision () const
616 return precision;
619 inline const HOST_WIDE_INT *
620 wi::storage_ref::get_val () const
622 return val;
625 /* This class defines an integer type using the storage provided by the
626 template argument. The storage class must provide the following
627 functions:
629 unsigned int get_precision () const
630 Return the number of bits in the integer.
632 HOST_WIDE_INT *get_val () const
633 Return a pointer to the array of blocks that encodes the integer.
635 unsigned int get_len () const
636 Return the number of blocks in get_val (). If this is smaller
637 than the number of blocks implied by get_precision (), the
638 remaining blocks are sign extensions of block get_len () - 1.
640 Although not required by generic_wide_int itself, writable storage
641 classes can also provide the following functions:
643 HOST_WIDE_INT *write_val ()
644 Get a modifiable version of get_val ()
646 unsigned int set_len (unsigned int len)
647 Set the value returned by get_len () to LEN. */
648 template <typename storage>
649 class GTY(()) generic_wide_int : public storage
651 public:
652 generic_wide_int ();
654 template <typename T>
655 generic_wide_int (const T &);
657 template <typename T>
658 generic_wide_int (const T &, unsigned int);
660 /* Conversions. */
661 HOST_WIDE_INT to_shwi (unsigned int) const;
662 HOST_WIDE_INT to_shwi () const;
663 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
664 unsigned HOST_WIDE_INT to_uhwi () const;
665 HOST_WIDE_INT to_short_addr () const;
667 /* Public accessors for the interior of a wide int. */
668 HOST_WIDE_INT sign_mask () const;
669 HOST_WIDE_INT elt (unsigned int) const;
670 unsigned HOST_WIDE_INT ulow () const;
671 unsigned HOST_WIDE_INT uhigh () const;
672 HOST_WIDE_INT slow () const;
673 HOST_WIDE_INT shigh () const;
675 template <typename T>
676 generic_wide_int &operator = (const T &);
678 #define BINARY_PREDICATE(OP, F) \
679 template <typename T> \
680 bool OP (const T &c) const { return wi::F (*this, c); }
682 #define UNARY_OPERATOR(OP, F) \
683 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
685 #define BINARY_OPERATOR(OP, F) \
686 template <typename T> \
687 WI_BINARY_RESULT (generic_wide_int, T) \
688 OP (const T &c) const { return wi::F (*this, c); }
690 #define ASSIGNMENT_OPERATOR(OP, F) \
691 template <typename T> \
692 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
694 /* Restrict these to cases where the shift operator is defined. */
695 #define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
696 template <typename T> \
697 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
699 #define INCDEC_OPERATOR(OP, DELTA) \
700 generic_wide_int &OP () { *this += DELTA; return *this; }
702 UNARY_OPERATOR (operator ~, bit_not)
703 UNARY_OPERATOR (operator -, neg)
704 BINARY_PREDICATE (operator ==, eq_p)
705 BINARY_PREDICATE (operator !=, ne_p)
706 BINARY_OPERATOR (operator &, bit_and)
707 BINARY_OPERATOR (and_not, bit_and_not)
708 BINARY_OPERATOR (operator |, bit_or)
709 BINARY_OPERATOR (or_not, bit_or_not)
710 BINARY_OPERATOR (operator ^, bit_xor)
711 BINARY_OPERATOR (operator +, add)
712 BINARY_OPERATOR (operator -, sub)
713 BINARY_OPERATOR (operator *, mul)
714 ASSIGNMENT_OPERATOR (operator &=, bit_and)
715 ASSIGNMENT_OPERATOR (operator |=, bit_or)
716 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
717 ASSIGNMENT_OPERATOR (operator +=, add)
718 ASSIGNMENT_OPERATOR (operator -=, sub)
719 ASSIGNMENT_OPERATOR (operator *=, mul)
720 SHIFT_ASSIGNMENT_OPERATOR (operator <<=, <<)
721 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
722 INCDEC_OPERATOR (operator ++, 1)
723 INCDEC_OPERATOR (operator --, -1)
725 #undef BINARY_PREDICATE
726 #undef UNARY_OPERATOR
727 #undef BINARY_OPERATOR
728 #undef SHIFT_ASSIGNMENT_OPERATOR
729 #undef ASSIGNMENT_OPERATOR
730 #undef INCDEC_OPERATOR
732 /* Debugging functions. */
733 void dump () const;
735 static const bool is_sign_extended
736 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
739 template <typename storage>
740 inline generic_wide_int <storage>::generic_wide_int () {}
742 template <typename storage>
743 template <typename T>
744 inline generic_wide_int <storage>::generic_wide_int (const T &x)
745 : storage (x)
749 template <typename storage>
750 template <typename T>
751 inline generic_wide_int <storage>::generic_wide_int (const T &x,
752 unsigned int precision)
753 : storage (x, precision)
757 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
758 If THIS does not fit in PRECISION, the information is lost. */
759 template <typename storage>
760 inline HOST_WIDE_INT
761 generic_wide_int <storage>::to_shwi (unsigned int precision) const
763 if (precision < HOST_BITS_PER_WIDE_INT)
764 return sext_hwi (this->get_val ()[0], precision);
765 else
766 return this->get_val ()[0];
769 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
770 template <typename storage>
771 inline HOST_WIDE_INT
772 generic_wide_int <storage>::to_shwi () const
774 if (is_sign_extended)
775 return this->get_val ()[0];
776 else
777 return to_shwi (this->get_precision ());
780 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
781 PRECISION. If THIS does not fit in PRECISION, the information
782 is lost. */
783 template <typename storage>
784 inline unsigned HOST_WIDE_INT
785 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
787 if (precision < HOST_BITS_PER_WIDE_INT)
788 return zext_hwi (this->get_val ()[0], precision);
789 else
790 return this->get_val ()[0];
793 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
794 template <typename storage>
795 inline unsigned HOST_WIDE_INT
796 generic_wide_int <storage>::to_uhwi () const
798 return to_uhwi (this->get_precision ());
801 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
802 represent addresses to using offset_int to represent addresses.
803 We use to_short_addr at the interface from new code to old,
804 unconverted code. */
805 template <typename storage>
806 inline HOST_WIDE_INT
807 generic_wide_int <storage>::to_short_addr () const
809 return this->get_val ()[0];
812 /* Return the implicit value of blocks above get_len (). */
813 template <typename storage>
814 inline HOST_WIDE_INT
815 generic_wide_int <storage>::sign_mask () const
817 unsigned int len = this->get_len ();
818 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
819 if (!is_sign_extended)
821 unsigned int precision = this->get_precision ();
822 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
823 if (excess > 0)
824 high <<= excess;
826 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
829 /* Return the signed value of the least-significant explicitly-encoded
830 block. */
831 template <typename storage>
832 inline HOST_WIDE_INT
833 generic_wide_int <storage>::slow () const
835 return this->get_val ()[0];
838 /* Return the signed value of the most-significant explicitly-encoded
839 block. */
840 template <typename storage>
841 inline HOST_WIDE_INT
842 generic_wide_int <storage>::shigh () const
844 return this->get_val ()[this->get_len () - 1];
847 /* Return the unsigned value of the least-significant
848 explicitly-encoded block. */
849 template <typename storage>
850 inline unsigned HOST_WIDE_INT
851 generic_wide_int <storage>::ulow () const
853 return this->get_val ()[0];
856 /* Return the unsigned value of the most-significant
857 explicitly-encoded block. */
858 template <typename storage>
859 inline unsigned HOST_WIDE_INT
860 generic_wide_int <storage>::uhigh () const
862 return this->get_val ()[this->get_len () - 1];
865 /* Return block I, which might be implicitly or explicit encoded. */
866 template <typename storage>
867 inline HOST_WIDE_INT
868 generic_wide_int <storage>::elt (unsigned int i) const
870 if (i >= this->get_len ())
871 return sign_mask ();
872 else
873 return this->get_val ()[i];
876 template <typename storage>
877 template <typename T>
878 inline generic_wide_int <storage> &
879 generic_wide_int <storage>::operator = (const T &x)
881 storage::operator = (x);
882 return *this;
885 /* Dump the contents of the integer to stderr, for debugging. */
886 template <typename storage>
887 void
888 generic_wide_int <storage>::dump () const
890 unsigned int len = this->get_len ();
891 const HOST_WIDE_INT *val = this->get_val ();
892 unsigned int precision = this->get_precision ();
893 fprintf (stderr, "[");
894 if (len * HOST_BITS_PER_WIDE_INT < precision)
895 fprintf (stderr, "...,");
896 for (unsigned int i = 0; i < len - 1; ++i)
897 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
898 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
899 val[0], precision);
902 namespace wi
904 template <typename storage>
905 struct int_traits < generic_wide_int <storage> >
906 : public wi::int_traits <storage>
908 static unsigned int get_precision (const generic_wide_int <storage> &);
909 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
910 const generic_wide_int <storage> &);
914 template <typename storage>
915 inline unsigned int
916 wi::int_traits < generic_wide_int <storage> >::
917 get_precision (const generic_wide_int <storage> &x)
919 return x.get_precision ();
922 template <typename storage>
923 inline wi::storage_ref
924 wi::int_traits < generic_wide_int <storage> >::
925 decompose (HOST_WIDE_INT *, unsigned int precision,
926 const generic_wide_int <storage> &x)
928 gcc_checking_assert (precision == x.get_precision ());
929 return wi::storage_ref (x.get_val (), x.get_len (), precision);
932 /* Provide the storage for a wide_int_ref. This acts like a read-only
933 wide_int, with the optimization that VAL is normally a pointer to
934 another integer's storage, so that no array copy is needed. */
935 template <bool SE>
936 struct wide_int_ref_storage : public wi::storage_ref
938 private:
939 /* Scratch space that can be used when decomposing the original integer.
940 It must live as long as this object. */
941 HOST_WIDE_INT scratch[2];
943 public:
944 wide_int_ref_storage (const wi::storage_ref &);
946 template <typename T>
947 wide_int_ref_storage (const T &);
949 template <typename T>
950 wide_int_ref_storage (const T &, unsigned int);
953 /* Create a reference from an existing reference. */
954 template <bool SE>
955 inline wide_int_ref_storage <SE>::
956 wide_int_ref_storage (const wi::storage_ref &x)
957 : storage_ref (x)
960 /* Create a reference to integer X in its natural precision. Note
961 that the natural precision is host-dependent for primitive
962 types. */
963 template <bool SE>
964 template <typename T>
965 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
966 : storage_ref (wi::int_traits <T>::decompose (scratch,
967 wi::get_precision (x), x))
971 /* Create a reference to integer X in precision PRECISION. */
972 template <bool SE>
973 template <typename T>
974 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
975 unsigned int precision)
976 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
980 namespace wi
982 template <bool SE>
983 struct int_traits <wide_int_ref_storage <SE> >
985 static const enum precision_type precision_type = VAR_PRECISION;
986 /* wi::storage_ref can be a reference to a primitive type,
987 so this is the conservatively-correct setting. */
988 static const bool host_dependent_precision = true;
989 static const bool is_sign_extended = SE;
993 namespace wi
995 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
996 unsigned int, unsigned int, unsigned int,
997 signop sgn);
998 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
999 unsigned int, unsigned int, bool = true);
1002 /* The storage used by wide_int. */
1003 class GTY(()) wide_int_storage
1005 private:
1006 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
1007 unsigned int len;
1008 unsigned int precision;
1010 public:
1011 wide_int_storage ();
1012 template <typename T>
1013 wide_int_storage (const T &);
1015 /* The standard generic_wide_int storage methods. */
1016 unsigned int get_precision () const;
1017 const HOST_WIDE_INT *get_val () const;
1018 unsigned int get_len () const;
1019 HOST_WIDE_INT *write_val ();
1020 void set_len (unsigned int, bool = false);
1022 template <typename T>
1023 wide_int_storage &operator = (const T &);
1025 static wide_int from (const wide_int_ref &, unsigned int, signop);
1026 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1027 unsigned int, bool = true);
1028 static wide_int create (unsigned int);
1030 /* FIXME: target-dependent, so should disappear. */
1031 wide_int bswap () const;
1034 namespace wi
1036 template <>
1037 struct int_traits <wide_int_storage>
1039 static const enum precision_type precision_type = VAR_PRECISION;
1040 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1041 static const bool host_dependent_precision = false;
1042 static const bool is_sign_extended = true;
1043 template <typename T1, typename T2>
1044 static wide_int get_binary_result (const T1 &, const T2 &);
1048 inline wide_int_storage::wide_int_storage () {}
1050 /* Initialize the storage from integer X, in its natural precision.
1051 Note that we do not allow integers with host-dependent precision
1052 to become wide_ints; wide_ints must always be logically independent
1053 of the host. */
1054 template <typename T>
1055 inline wide_int_storage::wide_int_storage (const T &x)
1057 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1058 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1059 WIDE_INT_REF_FOR (T) xi (x);
1060 precision = xi.precision;
1061 wi::copy (*this, xi);
1064 template <typename T>
1065 inline wide_int_storage&
1066 wide_int_storage::operator = (const T &x)
1068 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1069 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1070 WIDE_INT_REF_FOR (T) xi (x);
1071 precision = xi.precision;
1072 wi::copy (*this, xi);
1073 return *this;
1076 inline unsigned int
1077 wide_int_storage::get_precision () const
1079 return precision;
1082 inline const HOST_WIDE_INT *
1083 wide_int_storage::get_val () const
1085 return val;
1088 inline unsigned int
1089 wide_int_storage::get_len () const
1091 return len;
1094 inline HOST_WIDE_INT *
1095 wide_int_storage::write_val ()
1097 return val;
1100 inline void
1101 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1103 len = l;
1104 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1105 val[len - 1] = sext_hwi (val[len - 1],
1106 precision % HOST_BITS_PER_WIDE_INT);
1109 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1110 number. */
1111 inline wide_int
1112 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1113 signop sgn)
1115 wide_int result = wide_int::create (precision);
1116 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1117 x.precision, precision, sgn));
1118 return result;
1121 /* Create a wide_int from the explicit block encoding given by VAL and
1122 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1123 true if the encoding may have redundant trailing blocks. */
1124 inline wide_int
1125 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1126 unsigned int precision, bool need_canon_p)
1128 wide_int result = wide_int::create (precision);
1129 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1130 need_canon_p));
1131 return result;
1134 /* Return an uninitialized wide_int with precision PRECISION. */
1135 inline wide_int
1136 wide_int_storage::create (unsigned int precision)
1138 wide_int x;
1139 x.precision = precision;
1140 return x;
1143 template <typename T1, typename T2>
1144 inline wide_int
1145 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1147 /* This shouldn't be used for two flexible-precision inputs. */
1148 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1149 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1150 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1151 return wide_int::create (wi::get_precision (y));
1152 else
1153 return wide_int::create (wi::get_precision (x));
1156 /* The storage used by FIXED_WIDE_INT (N). */
1157 template <int N>
1158 class GTY(()) fixed_wide_int_storage
1160 private:
1161 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1162 unsigned int len;
1164 public:
1165 fixed_wide_int_storage ();
1166 template <typename T>
1167 fixed_wide_int_storage (const T &);
1169 /* The standard generic_wide_int storage methods. */
1170 unsigned int get_precision () const;
1171 const HOST_WIDE_INT *get_val () const;
1172 unsigned int get_len () const;
1173 HOST_WIDE_INT *write_val ();
1174 void set_len (unsigned int, bool = false);
1176 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1177 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1178 bool = true);
1181 namespace wi
1183 template <int N>
1184 struct int_traits < fixed_wide_int_storage <N> >
1186 static const enum precision_type precision_type = CONST_PRECISION;
1187 static const bool host_dependent_precision = false;
1188 static const bool is_sign_extended = true;
1189 static const unsigned int precision = N;
1190 template <typename T1, typename T2>
1191 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1195 template <int N>
1196 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1198 /* Initialize the storage from integer X, in precision N. */
1199 template <int N>
1200 template <typename T>
1201 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1203 /* Check for type compatibility. We don't want to initialize a
1204 fixed-width integer from something like a wide_int. */
1205 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1206 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1209 template <int N>
1210 inline unsigned int
1211 fixed_wide_int_storage <N>::get_precision () const
1213 return N;
1216 template <int N>
1217 inline const HOST_WIDE_INT *
1218 fixed_wide_int_storage <N>::get_val () const
1220 return val;
1223 template <int N>
1224 inline unsigned int
1225 fixed_wide_int_storage <N>::get_len () const
1227 return len;
1230 template <int N>
1231 inline HOST_WIDE_INT *
1232 fixed_wide_int_storage <N>::write_val ()
1234 return val;
1237 template <int N>
1238 inline void
1239 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1241 len = l;
1242 /* There are no excess bits in val[len - 1]. */
1243 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1246 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1247 template <int N>
1248 inline FIXED_WIDE_INT (N)
1249 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1251 FIXED_WIDE_INT (N) result;
1252 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1253 x.precision, N, sgn));
1254 return result;
1257 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1258 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1259 trailing blocks. */
1260 template <int N>
1261 inline FIXED_WIDE_INT (N)
1262 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1263 unsigned int len,
1264 bool need_canon_p)
1266 FIXED_WIDE_INT (N) result;
1267 result.set_len (wi::from_array (result.write_val (), val, len,
1268 N, need_canon_p));
1269 return result;
1272 template <int N>
1273 template <typename T1, typename T2>
1274 inline FIXED_WIDE_INT (N)
1275 wi::int_traits < fixed_wide_int_storage <N> >::
1276 get_binary_result (const T1 &, const T2 &)
1278 return FIXED_WIDE_INT (N) ();
1281 /* A reference to one element of a trailing_wide_ints structure. */
1282 class trailing_wide_int_storage
1284 private:
1285 /* The precision of the integer, which is a fixed property of the
1286 parent trailing_wide_ints. */
1287 unsigned int m_precision;
1289 /* A pointer to the length field. */
1290 unsigned char *m_len;
1292 /* A pointer to the HWI array. There are enough elements to hold all
1293 values of precision M_PRECISION. */
1294 HOST_WIDE_INT *m_val;
1296 public:
1297 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1299 /* The standard generic_wide_int storage methods. */
1300 unsigned int get_len () const;
1301 unsigned int get_precision () const;
1302 const HOST_WIDE_INT *get_val () const;
1303 HOST_WIDE_INT *write_val ();
1304 void set_len (unsigned int, bool = false);
1306 template <typename T>
1307 trailing_wide_int_storage &operator = (const T &);
1310 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1312 /* trailing_wide_int behaves like a wide_int. */
1313 namespace wi
1315 template <>
1316 struct int_traits <trailing_wide_int_storage>
1317 : public int_traits <wide_int_storage> {};
1320 /* An array of N wide_int-like objects that can be put at the end of
1321 a variable-sized structure. Use extra_size to calculate how many
1322 bytes beyond the sizeof need to be allocated. Use set_precision
1323 to initialize the structure. */
1324 template <int N>
1325 class GTY(()) trailing_wide_ints
1327 private:
1328 /* The shared precision of each number. */
1329 unsigned short m_precision;
1331 /* The shared maximum length of each number. */
1332 unsigned char m_max_len;
1334 /* The current length of each number. */
1335 unsigned char m_len[N];
1337 /* The variable-length part of the structure, which always contains
1338 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1339 HOST_WIDE_INT m_val[1];
1341 public:
1342 void set_precision (unsigned int);
1343 trailing_wide_int operator [] (unsigned int);
1344 static size_t extra_size (unsigned int);
1347 inline trailing_wide_int_storage::
1348 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1349 HOST_WIDE_INT *val)
1350 : m_precision (precision), m_len (len), m_val (val)
1354 inline unsigned int
1355 trailing_wide_int_storage::get_len () const
1357 return *m_len;
1360 inline unsigned int
1361 trailing_wide_int_storage::get_precision () const
1363 return m_precision;
1366 inline const HOST_WIDE_INT *
1367 trailing_wide_int_storage::get_val () const
1369 return m_val;
1372 inline HOST_WIDE_INT *
1373 trailing_wide_int_storage::write_val ()
1375 return m_val;
1378 inline void
1379 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1381 *m_len = len;
1382 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1383 m_val[len - 1] = sext_hwi (m_val[len - 1],
1384 m_precision % HOST_BITS_PER_WIDE_INT);
1387 template <typename T>
1388 inline trailing_wide_int_storage &
1389 trailing_wide_int_storage::operator = (const T &x)
1391 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1392 wi::copy (*this, xi);
1393 return *this;
1396 /* Initialize the structure and record that all elements have precision
1397 PRECISION. */
1398 template <int N>
1399 inline void
1400 trailing_wide_ints <N>::set_precision (unsigned int precision)
1402 m_precision = precision;
1403 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1404 / HOST_BITS_PER_WIDE_INT);
1407 /* Return a reference to element INDEX. */
1408 template <int N>
1409 inline trailing_wide_int
1410 trailing_wide_ints <N>::operator [] (unsigned int index)
1412 return trailing_wide_int_storage (m_precision, &m_len[index],
1413 &m_val[index * m_max_len]);
1416 /* Return how many extra bytes need to be added to the end of the structure
1417 in order to handle N wide_ints of precision PRECISION. */
1418 template <int N>
1419 inline size_t
1420 trailing_wide_ints <N>::extra_size (unsigned int precision)
1422 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1423 / HOST_BITS_PER_WIDE_INT);
1424 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1427 /* This macro is used in structures that end with a trailing_wide_ints field
1428 called FIELD. It declares get_NAME() and set_NAME() methods to access
1429 element I of FIELD. */
1430 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1431 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1432 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1434 namespace wi
1436 /* Implementation of int_traits for primitive integer types like "int". */
1437 template <typename T, bool signed_p>
1438 struct primitive_int_traits
1440 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1441 static const bool host_dependent_precision = true;
1442 static const bool is_sign_extended = true;
1443 static unsigned int get_precision (T);
1444 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1448 template <typename T, bool signed_p>
1449 inline unsigned int
1450 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1452 return sizeof (T) * CHAR_BIT;
1455 template <typename T, bool signed_p>
1456 inline wi::storage_ref
1457 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1458 unsigned int precision, T x)
1460 scratch[0] = x;
1461 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1462 return wi::storage_ref (scratch, 1, precision);
1463 scratch[1] = 0;
1464 return wi::storage_ref (scratch, 2, precision);
1467 /* Allow primitive C types to be used in wi:: routines. */
1468 namespace wi
1470 template <>
1471 struct int_traits <int>
1472 : public primitive_int_traits <int, true> {};
1474 template <>
1475 struct int_traits <unsigned int>
1476 : public primitive_int_traits <unsigned int, false> {};
1478 template <>
1479 struct int_traits <long>
1480 : public primitive_int_traits <long, true> {};
1482 template <>
1483 struct int_traits <unsigned long>
1484 : public primitive_int_traits <unsigned long, false> {};
1486 #if defined HAVE_LONG_LONG
1487 template <>
1488 struct int_traits <long long>
1489 : public primitive_int_traits <long long, true> {};
1491 template <>
1492 struct int_traits <unsigned long long>
1493 : public primitive_int_traits <unsigned long long, false> {};
1494 #endif
1497 namespace wi
1499 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1500 and precision PRECISION. */
1501 struct hwi_with_prec
1503 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1504 HOST_WIDE_INT val;
1505 unsigned int precision;
1506 signop sgn;
1509 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1510 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1512 hwi_with_prec minus_one (unsigned int);
1513 hwi_with_prec zero (unsigned int);
1514 hwi_with_prec one (unsigned int);
1515 hwi_with_prec two (unsigned int);
1518 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1519 signop s)
1520 : val (v), precision (p), sgn (s)
1524 /* Return a signed integer that has value VAL and precision PRECISION. */
1525 inline wi::hwi_with_prec
1526 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1528 return hwi_with_prec (val, precision, SIGNED);
1531 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1532 inline wi::hwi_with_prec
1533 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1535 return hwi_with_prec (val, precision, UNSIGNED);
1538 /* Return a wide int of -1 with precision PRECISION. */
1539 inline wi::hwi_with_prec
1540 wi::minus_one (unsigned int precision)
1542 return wi::shwi (-1, precision);
1545 /* Return a wide int of 0 with precision PRECISION. */
1546 inline wi::hwi_with_prec
1547 wi::zero (unsigned int precision)
1549 return wi::shwi (0, precision);
1552 /* Return a wide int of 1 with precision PRECISION. */
1553 inline wi::hwi_with_prec
1554 wi::one (unsigned int precision)
1556 return wi::shwi (1, precision);
1559 /* Return a wide int of 2 with precision PRECISION. */
1560 inline wi::hwi_with_prec
1561 wi::two (unsigned int precision)
1563 return wi::shwi (2, precision);
1566 namespace wi
1568 template <>
1569 struct int_traits <wi::hwi_with_prec>
1571 static const enum precision_type precision_type = VAR_PRECISION;
1572 /* hwi_with_prec has an explicitly-given precision, rather than the
1573 precision of HOST_WIDE_INT. */
1574 static const bool host_dependent_precision = false;
1575 static const bool is_sign_extended = true;
1576 static unsigned int get_precision (const wi::hwi_with_prec &);
1577 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1578 const wi::hwi_with_prec &);
1582 inline unsigned int
1583 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1585 return x.precision;
1588 inline wi::storage_ref
1589 wi::int_traits <wi::hwi_with_prec>::
1590 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1591 const wi::hwi_with_prec &x)
1593 gcc_checking_assert (precision == x.precision);
1594 scratch[0] = x.val;
1595 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1596 return wi::storage_ref (scratch, 1, precision);
1597 scratch[1] = 0;
1598 return wi::storage_ref (scratch, 2, precision);
1601 /* Private functions for handling large cases out of line. They take
1602 individual length and array parameters because that is cheaper for
1603 the inline caller than constructing an object on the stack and
1604 passing a reference to it. (Although many callers use wide_int_refs,
1605 we generally want those to be removed by SRA.) */
1606 namespace wi
1608 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1609 const HOST_WIDE_INT *, unsigned int, unsigned int);
1610 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1611 const HOST_WIDE_INT *, unsigned int);
1612 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1613 const HOST_WIDE_INT *, unsigned int);
1614 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1615 const HOST_WIDE_INT *, unsigned int);
1616 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1617 const HOST_WIDE_INT *, unsigned int);
1618 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1619 unsigned int,
1620 unsigned int, unsigned int);
1621 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1622 unsigned int,
1623 unsigned int, unsigned int);
1624 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1625 unsigned int, unsigned int, unsigned int);
1626 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1627 unsigned int, unsigned int, unsigned int);
1628 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1629 unsigned int, unsigned int, unsigned int,
1630 unsigned int);
1631 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1632 unsigned int, unsigned int, unsigned int,
1633 unsigned int);
1634 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1635 const HOST_WIDE_INT *, unsigned int, unsigned int);
1636 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1637 unsigned int, const HOST_WIDE_INT *,
1638 unsigned int, unsigned int);
1639 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1640 const HOST_WIDE_INT *, unsigned int, unsigned int);
1641 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1642 unsigned int, const HOST_WIDE_INT *,
1643 unsigned int, unsigned int);
1644 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1645 const HOST_WIDE_INT *, unsigned int, unsigned int);
1646 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1647 const HOST_WIDE_INT *, unsigned int, unsigned int,
1648 signop, bool *);
1649 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1650 const HOST_WIDE_INT *, unsigned int, unsigned int,
1651 signop, bool *);
1652 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1653 unsigned int, const HOST_WIDE_INT *,
1654 unsigned int, unsigned int, signop, bool *,
1655 bool);
1656 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1657 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1658 unsigned int, unsigned int,
1659 const HOST_WIDE_INT *,
1660 unsigned int, unsigned int,
1661 signop, bool *);
1664 /* Return the number of bits that integer X can hold. */
1665 template <typename T>
1666 inline unsigned int
1667 wi::get_precision (const T &x)
1669 return wi::int_traits <T>::get_precision (x);
1672 /* Return the number of bits that the result of a binary operation can
1673 hold when the input operands are X and Y. */
1674 template <typename T1, typename T2>
1675 inline unsigned int
1676 wi::get_binary_precision (const T1 &x, const T2 &y)
1678 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1679 get_binary_result (x, y));
1682 /* Copy the contents of Y to X, but keeping X's current precision. */
1683 template <typename T1, typename T2>
1684 inline void
1685 wi::copy (T1 &x, const T2 &y)
1687 HOST_WIDE_INT *xval = x.write_val ();
1688 const HOST_WIDE_INT *yval = y.get_val ();
1689 unsigned int len = y.get_len ();
1690 unsigned int i = 0;
1692 xval[i] = yval[i];
1693 while (++i < len);
1694 x.set_len (len, y.is_sign_extended);
1697 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1698 template <typename T>
1699 inline bool
1700 wi::fits_shwi_p (const T &x)
1702 WIDE_INT_REF_FOR (T) xi (x);
1703 return xi.len == 1;
1706 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1707 precision. */
1708 template <typename T>
1709 inline bool
1710 wi::fits_uhwi_p (const T &x)
1712 WIDE_INT_REF_FOR (T) xi (x);
1713 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1714 return true;
1715 if (xi.len == 1)
1716 return xi.slow () >= 0;
1717 return xi.len == 2 && xi.uhigh () == 0;
1720 /* Return true if X is negative based on the interpretation of SGN.
1721 For UNSIGNED, this is always false. */
1722 template <typename T>
1723 inline bool
1724 wi::neg_p (const T &x, signop sgn)
1726 WIDE_INT_REF_FOR (T) xi (x);
1727 if (sgn == UNSIGNED)
1728 return false;
1729 return xi.sign_mask () < 0;
1732 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1733 template <typename T>
1734 inline HOST_WIDE_INT
1735 wi::sign_mask (const T &x)
1737 WIDE_INT_REF_FOR (T) xi (x);
1738 return xi.sign_mask ();
1741 /* Return true if X == Y. X and Y must be binary-compatible. */
1742 template <typename T1, typename T2>
1743 inline bool
1744 wi::eq_p (const T1 &x, const T2 &y)
1746 unsigned int precision = get_binary_precision (x, y);
1747 WIDE_INT_REF_FOR (T1) xi (x, precision);
1748 WIDE_INT_REF_FOR (T2) yi (y, precision);
1749 if (xi.is_sign_extended && yi.is_sign_extended)
1751 /* This case reduces to array equality. */
1752 if (xi.len != yi.len)
1753 return false;
1754 unsigned int i = 0;
1756 if (xi.val[i] != yi.val[i])
1757 return false;
1758 while (++i != xi.len);
1759 return true;
1761 if (__builtin_expect (yi.len == 1, true))
1763 /* XI is only equal to YI if it too has a single HWI. */
1764 if (xi.len != 1)
1765 return false;
1766 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1767 with 0 are simple. */
1768 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1769 return xi.val[0] == 0;
1770 /* Otherwise flush out any excess bits first. */
1771 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1772 int excess = HOST_BITS_PER_WIDE_INT - precision;
1773 if (excess > 0)
1774 diff <<= excess;
1775 return diff == 0;
1777 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1780 /* Return true if X != Y. X and Y must be binary-compatible. */
1781 template <typename T1, typename T2>
1782 inline bool
1783 wi::ne_p (const T1 &x, const T2 &y)
1785 return !eq_p (x, y);
1788 /* Return true if X < Y when both are treated as signed values. */
1789 template <typename T1, typename T2>
1790 inline bool
1791 wi::lts_p (const T1 &x, const T2 &y)
1793 unsigned int precision = get_binary_precision (x, y);
1794 WIDE_INT_REF_FOR (T1) xi (x, precision);
1795 WIDE_INT_REF_FOR (T2) yi (y, precision);
1796 /* We optimize x < y, where y is 64 or fewer bits. */
1797 if (wi::fits_shwi_p (yi))
1799 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1800 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1801 return neg_p (xi);
1802 /* If x fits directly into a shwi, we can compare directly. */
1803 if (wi::fits_shwi_p (xi))
1804 return xi.to_shwi () < yi.to_shwi ();
1805 /* If x doesn't fit and is negative, then it must be more
1806 negative than any value in y, and hence smaller than y. */
1807 if (neg_p (xi))
1808 return true;
1809 /* If x is positive, then it must be larger than any value in y,
1810 and hence greater than y. */
1811 return false;
1813 /* Optimize the opposite case, if it can be detected at compile time. */
1814 if (STATIC_CONSTANT_P (xi.len == 1))
1815 /* If YI is negative it is lower than the least HWI.
1816 If YI is positive it is greater than the greatest HWI. */
1817 return !neg_p (yi);
1818 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1821 /* Return true if X < Y when both are treated as unsigned values. */
1822 template <typename T1, typename T2>
1823 inline bool
1824 wi::ltu_p (const T1 &x, const T2 &y)
1826 unsigned int precision = get_binary_precision (x, y);
1827 WIDE_INT_REF_FOR (T1) xi (x, precision);
1828 WIDE_INT_REF_FOR (T2) yi (y, precision);
1829 /* Optimize comparisons with constants. */
1830 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1831 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1832 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1833 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1834 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1835 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1836 values does not change the result. */
1837 if (__builtin_expect (xi.len + yi.len == 2, true))
1839 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1840 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1841 return xl < yl;
1843 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1846 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1847 template <typename T1, typename T2>
1848 inline bool
1849 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1851 if (sgn == SIGNED)
1852 return lts_p (x, y);
1853 else
1854 return ltu_p (x, y);
1857 /* Return true if X <= Y when both are treated as signed values. */
1858 template <typename T1, typename T2>
1859 inline bool
1860 wi::les_p (const T1 &x, const T2 &y)
1862 return !lts_p (y, x);
1865 /* Return true if X <= Y when both are treated as unsigned values. */
1866 template <typename T1, typename T2>
1867 inline bool
1868 wi::leu_p (const T1 &x, const T2 &y)
1870 return !ltu_p (y, x);
1873 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1874 template <typename T1, typename T2>
1875 inline bool
1876 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1878 if (sgn == SIGNED)
1879 return les_p (x, y);
1880 else
1881 return leu_p (x, y);
1884 /* Return true if X > Y when both are treated as signed values. */
1885 template <typename T1, typename T2>
1886 inline bool
1887 wi::gts_p (const T1 &x, const T2 &y)
1889 return lts_p (y, x);
1892 /* Return true if X > Y when both are treated as unsigned values. */
1893 template <typename T1, typename T2>
1894 inline bool
1895 wi::gtu_p (const T1 &x, const T2 &y)
1897 return ltu_p (y, x);
1900 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1901 template <typename T1, typename T2>
1902 inline bool
1903 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1905 if (sgn == SIGNED)
1906 return gts_p (x, y);
1907 else
1908 return gtu_p (x, y);
1911 /* Return true if X >= Y when both are treated as signed values. */
1912 template <typename T1, typename T2>
1913 inline bool
1914 wi::ges_p (const T1 &x, const T2 &y)
1916 return !lts_p (x, y);
1919 /* Return true if X >= Y when both are treated as unsigned values. */
1920 template <typename T1, typename T2>
1921 inline bool
1922 wi::geu_p (const T1 &x, const T2 &y)
1924 return !ltu_p (x, y);
1927 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1928 template <typename T1, typename T2>
1929 inline bool
1930 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1932 if (sgn == SIGNED)
1933 return ges_p (x, y);
1934 else
1935 return geu_p (x, y);
1938 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1939 as signed values. */
1940 template <typename T1, typename T2>
1941 inline int
1942 wi::cmps (const T1 &x, const T2 &y)
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 if (wi::fits_shwi_p (yi))
1949 /* Special case for comparisons with 0. */
1950 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1951 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1952 /* If x fits into a signed HWI, we can compare directly. */
1953 if (wi::fits_shwi_p (xi))
1955 HOST_WIDE_INT xl = xi.to_shwi ();
1956 HOST_WIDE_INT yl = yi.to_shwi ();
1957 return xl < yl ? -1 : xl > yl;
1959 /* If x doesn't fit and is negative, then it must be more
1960 negative than any signed HWI, and hence smaller than y. */
1961 if (neg_p (xi))
1962 return -1;
1963 /* If x is positive, then it must be larger than any signed HWI,
1964 and hence greater than y. */
1965 return 1;
1967 /* Optimize the opposite case, if it can be detected at compile time. */
1968 if (STATIC_CONSTANT_P (xi.len == 1))
1969 /* If YI is negative it is lower than the least HWI.
1970 If YI is positive it is greater than the greatest HWI. */
1971 return neg_p (yi) ? 1 : -1;
1972 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1975 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1976 as unsigned values. */
1977 template <typename T1, typename T2>
1978 inline int
1979 wi::cmpu (const T1 &x, const T2 &y)
1981 unsigned int precision = get_binary_precision (x, y);
1982 WIDE_INT_REF_FOR (T1) xi (x, precision);
1983 WIDE_INT_REF_FOR (T2) yi (y, precision);
1984 /* Optimize comparisons with constants. */
1985 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1987 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1988 if (xi.len != 1)
1989 return 1;
1990 /* Otherwise compare directly. */
1991 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1992 unsigned HOST_WIDE_INT yl = yi.val[0];
1993 return xl < yl ? -1 : xl > yl;
1995 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1997 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1998 if (yi.len != 1)
1999 return -1;
2000 /* Otherwise compare directly. */
2001 unsigned HOST_WIDE_INT xl = xi.val[0];
2002 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2003 return xl < yl ? -1 : xl > yl;
2005 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2006 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2007 values does not change the result. */
2008 if (__builtin_expect (xi.len + yi.len == 2, true))
2010 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2011 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2012 return xl < yl ? -1 : xl > yl;
2014 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2017 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2018 X and Y indicated by SGN. */
2019 template <typename T1, typename T2>
2020 inline int
2021 wi::cmp (const T1 &x, const T2 &y, signop sgn)
2023 if (sgn == SIGNED)
2024 return cmps (x, y);
2025 else
2026 return cmpu (x, y);
2029 /* Return ~x. */
2030 template <typename T>
2031 inline WI_UNARY_RESULT (T)
2032 wi::bit_not (const T &x)
2034 WI_UNARY_RESULT_VAR (result, val, T, x);
2035 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2036 for (unsigned int i = 0; i < xi.len; ++i)
2037 val[i] = ~xi.val[i];
2038 result.set_len (xi.len);
2039 return result;
2042 /* Return -x. */
2043 template <typename T>
2044 inline WI_UNARY_RESULT (T)
2045 wi::neg (const T &x)
2047 return sub (0, x);
2050 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2051 template <typename T>
2052 inline WI_UNARY_RESULT (T)
2053 wi::neg (const T &x, bool *overflow)
2055 *overflow = only_sign_bit_p (x);
2056 return sub (0, x);
2059 /* Return the absolute value of x. */
2060 template <typename T>
2061 inline WI_UNARY_RESULT (T)
2062 wi::abs (const T &x)
2064 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2067 /* Return the result of sign-extending the low OFFSET bits of X. */
2068 template <typename T>
2069 inline WI_UNARY_RESULT (T)
2070 wi::sext (const T &x, unsigned int offset)
2072 WI_UNARY_RESULT_VAR (result, val, T, x);
2073 unsigned int precision = get_precision (result);
2074 WIDE_INT_REF_FOR (T) xi (x, precision);
2076 if (offset <= HOST_BITS_PER_WIDE_INT)
2078 val[0] = sext_hwi (xi.ulow (), offset);
2079 result.set_len (1, true);
2081 else
2082 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2083 return result;
2086 /* Return the result of zero-extending the low OFFSET bits of X. */
2087 template <typename T>
2088 inline WI_UNARY_RESULT (T)
2089 wi::zext (const T &x, unsigned int offset)
2091 WI_UNARY_RESULT_VAR (result, val, T, x);
2092 unsigned int precision = get_precision (result);
2093 WIDE_INT_REF_FOR (T) xi (x, precision);
2095 /* This is not just an optimization, it is actually required to
2096 maintain canonization. */
2097 if (offset >= precision)
2099 wi::copy (result, xi);
2100 return result;
2103 /* In these cases we know that at least the top bit will be clear,
2104 so no sign extension is necessary. */
2105 if (offset < HOST_BITS_PER_WIDE_INT)
2107 val[0] = zext_hwi (xi.ulow (), offset);
2108 result.set_len (1, true);
2110 else
2111 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2112 return result;
2115 /* Return the result of extending the low OFFSET bits of X according to
2116 signedness SGN. */
2117 template <typename T>
2118 inline WI_UNARY_RESULT (T)
2119 wi::ext (const T &x, unsigned int offset, signop sgn)
2121 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2124 /* Return an integer that represents X | (1 << bit). */
2125 template <typename T>
2126 inline WI_UNARY_RESULT (T)
2127 wi::set_bit (const T &x, unsigned int bit)
2129 WI_UNARY_RESULT_VAR (result, val, T, x);
2130 unsigned int precision = get_precision (result);
2131 WIDE_INT_REF_FOR (T) xi (x, precision);
2132 if (precision <= HOST_BITS_PER_WIDE_INT)
2134 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2135 result.set_len (1);
2137 else
2138 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2139 return result;
2142 /* Return the mininum of X and Y, treating them both as having
2143 signedness SGN. */
2144 template <typename T1, typename T2>
2145 inline WI_BINARY_RESULT (T1, T2)
2146 wi::min (const T1 &x, const T2 &y, signop sgn)
2148 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2149 unsigned int precision = get_precision (result);
2150 if (wi::le_p (x, y, sgn))
2151 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2152 else
2153 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2154 return result;
2157 /* Return the minimum of X and Y, treating both as signed values. */
2158 template <typename T1, typename T2>
2159 inline WI_BINARY_RESULT (T1, T2)
2160 wi::smin (const T1 &x, const T2 &y)
2162 return wi::min (x, y, SIGNED);
2165 /* Return the minimum of X and Y, treating both as unsigned values. */
2166 template <typename T1, typename T2>
2167 inline WI_BINARY_RESULT (T1, T2)
2168 wi::umin (const T1 &x, const T2 &y)
2170 return wi::min (x, y, UNSIGNED);
2173 /* Return the maxinum of X and Y, treating them both as having
2174 signedness SGN. */
2175 template <typename T1, typename T2>
2176 inline WI_BINARY_RESULT (T1, T2)
2177 wi::max (const T1 &x, const T2 &y, signop sgn)
2179 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2180 unsigned int precision = get_precision (result);
2181 if (wi::ge_p (x, y, sgn))
2182 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2183 else
2184 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2185 return result;
2188 /* Return the maximum of X and Y, treating both as signed values. */
2189 template <typename T1, typename T2>
2190 inline WI_BINARY_RESULT (T1, T2)
2191 wi::smax (const T1 &x, const T2 &y)
2193 return wi::max (x, y, SIGNED);
2196 /* Return the maximum of X and Y, treating both as unsigned values. */
2197 template <typename T1, typename T2>
2198 inline WI_BINARY_RESULT (T1, T2)
2199 wi::umax (const T1 &x, const T2 &y)
2201 return wi::max (x, y, UNSIGNED);
2204 /* Return X & Y. */
2205 template <typename T1, typename T2>
2206 inline WI_BINARY_RESULT (T1, T2)
2207 wi::bit_and (const T1 &x, const T2 &y)
2209 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2210 unsigned int precision = get_precision (result);
2211 WIDE_INT_REF_FOR (T1) xi (x, precision);
2212 WIDE_INT_REF_FOR (T2) yi (y, precision);
2213 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2214 if (__builtin_expect (xi.len + yi.len == 2, true))
2216 val[0] = xi.ulow () & yi.ulow ();
2217 result.set_len (1, is_sign_extended);
2219 else
2220 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2221 precision), is_sign_extended);
2222 return result;
2225 /* Return X & ~Y. */
2226 template <typename T1, typename T2>
2227 inline WI_BINARY_RESULT (T1, T2)
2228 wi::bit_and_not (const T1 &x, const T2 &y)
2230 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2231 unsigned int precision = get_precision (result);
2232 WIDE_INT_REF_FOR (T1) xi (x, precision);
2233 WIDE_INT_REF_FOR (T2) yi (y, precision);
2234 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2235 if (__builtin_expect (xi.len + yi.len == 2, true))
2237 val[0] = xi.ulow () & ~yi.ulow ();
2238 result.set_len (1, is_sign_extended);
2240 else
2241 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2242 precision), is_sign_extended);
2243 return result;
2246 /* Return X | Y. */
2247 template <typename T1, typename T2>
2248 inline WI_BINARY_RESULT (T1, T2)
2249 wi::bit_or (const T1 &x, const T2 &y)
2251 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2252 unsigned int precision = get_precision (result);
2253 WIDE_INT_REF_FOR (T1) xi (x, precision);
2254 WIDE_INT_REF_FOR (T2) yi (y, precision);
2255 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2256 if (__builtin_expect (xi.len + yi.len == 2, true))
2258 val[0] = xi.ulow () | yi.ulow ();
2259 result.set_len (1, is_sign_extended);
2261 else
2262 result.set_len (or_large (val, xi.val, xi.len,
2263 yi.val, yi.len, precision), is_sign_extended);
2264 return result;
2267 /* Return X | ~Y. */
2268 template <typename T1, typename T2>
2269 inline WI_BINARY_RESULT (T1, T2)
2270 wi::bit_or_not (const T1 &x, const T2 &y)
2272 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2273 unsigned int precision = get_precision (result);
2274 WIDE_INT_REF_FOR (T1) xi (x, precision);
2275 WIDE_INT_REF_FOR (T2) yi (y, precision);
2276 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2277 if (__builtin_expect (xi.len + yi.len == 2, true))
2279 val[0] = xi.ulow () | ~yi.ulow ();
2280 result.set_len (1, is_sign_extended);
2282 else
2283 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2284 precision), is_sign_extended);
2285 return result;
2288 /* Return X ^ Y. */
2289 template <typename T1, typename T2>
2290 inline WI_BINARY_RESULT (T1, T2)
2291 wi::bit_xor (const T1 &x, const T2 &y)
2293 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2294 unsigned int precision = get_precision (result);
2295 WIDE_INT_REF_FOR (T1) xi (x, precision);
2296 WIDE_INT_REF_FOR (T2) yi (y, precision);
2297 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2298 if (__builtin_expect (xi.len + yi.len == 2, true))
2300 val[0] = xi.ulow () ^ yi.ulow ();
2301 result.set_len (1, is_sign_extended);
2303 else
2304 result.set_len (xor_large (val, xi.val, xi.len,
2305 yi.val, yi.len, precision), is_sign_extended);
2306 return result;
2309 /* Return X + Y. */
2310 template <typename T1, typename T2>
2311 inline WI_BINARY_RESULT (T1, T2)
2312 wi::add (const T1 &x, const T2 &y)
2314 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2315 unsigned int precision = get_precision (result);
2316 WIDE_INT_REF_FOR (T1) xi (x, precision);
2317 WIDE_INT_REF_FOR (T2) yi (y, precision);
2318 if (precision <= HOST_BITS_PER_WIDE_INT)
2320 val[0] = xi.ulow () + yi.ulow ();
2321 result.set_len (1);
2323 /* If the precision is known at compile time to be greater than
2324 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2325 knowing that (a) all bits in those HWIs are significant and
2326 (b) the result has room for at least two HWIs. This provides
2327 a fast path for things like offset_int and widest_int.
2329 The STATIC_CONSTANT_P test prevents this path from being
2330 used for wide_ints. wide_ints with precisions greater than
2331 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2332 point handling them inline. */
2333 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2334 && __builtin_expect (xi.len + yi.len == 2, true))
2336 unsigned HOST_WIDE_INT xl = xi.ulow ();
2337 unsigned HOST_WIDE_INT yl = yi.ulow ();
2338 unsigned HOST_WIDE_INT resultl = xl + yl;
2339 val[0] = resultl;
2340 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2341 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2342 >> (HOST_BITS_PER_WIDE_INT - 1)));
2344 else
2345 result.set_len (add_large (val, xi.val, xi.len,
2346 yi.val, yi.len, precision,
2347 UNSIGNED, 0));
2348 return result;
2351 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2352 and indicate in *OVERFLOW whether the operation overflowed. */
2353 template <typename T1, typename T2>
2354 inline WI_BINARY_RESULT (T1, T2)
2355 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2357 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2358 unsigned int precision = get_precision (result);
2359 WIDE_INT_REF_FOR (T1) xi (x, precision);
2360 WIDE_INT_REF_FOR (T2) yi (y, precision);
2361 if (precision <= HOST_BITS_PER_WIDE_INT)
2363 unsigned HOST_WIDE_INT xl = xi.ulow ();
2364 unsigned HOST_WIDE_INT yl = yi.ulow ();
2365 unsigned HOST_WIDE_INT resultl = xl + yl;
2366 if (sgn == SIGNED)
2367 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2368 >> (precision - 1)) & 1;
2369 else
2370 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2371 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2372 val[0] = resultl;
2373 result.set_len (1);
2375 else
2376 result.set_len (add_large (val, xi.val, xi.len,
2377 yi.val, yi.len, precision,
2378 sgn, overflow));
2379 return result;
2382 /* Return X - Y. */
2383 template <typename T1, typename T2>
2384 inline WI_BINARY_RESULT (T1, T2)
2385 wi::sub (const T1 &x, const T2 &y)
2387 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2388 unsigned int precision = get_precision (result);
2389 WIDE_INT_REF_FOR (T1) xi (x, precision);
2390 WIDE_INT_REF_FOR (T2) yi (y, precision);
2391 if (precision <= HOST_BITS_PER_WIDE_INT)
2393 val[0] = xi.ulow () - yi.ulow ();
2394 result.set_len (1);
2396 /* If the precision is known at compile time to be greater than
2397 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2398 knowing that (a) all bits in those HWIs are significant and
2399 (b) the result has room for at least two HWIs. This provides
2400 a fast path for things like offset_int and widest_int.
2402 The STATIC_CONSTANT_P test prevents this path from being
2403 used for wide_ints. wide_ints with precisions greater than
2404 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2405 point handling them inline. */
2406 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2407 && __builtin_expect (xi.len + yi.len == 2, true))
2409 unsigned HOST_WIDE_INT xl = xi.ulow ();
2410 unsigned HOST_WIDE_INT yl = yi.ulow ();
2411 unsigned HOST_WIDE_INT resultl = xl - yl;
2412 val[0] = resultl;
2413 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2414 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2415 >> (HOST_BITS_PER_WIDE_INT - 1)));
2417 else
2418 result.set_len (sub_large (val, xi.val, xi.len,
2419 yi.val, yi.len, precision,
2420 UNSIGNED, 0));
2421 return result;
2424 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2425 and indicate in *OVERFLOW whether the operation overflowed. */
2426 template <typename T1, typename T2>
2427 inline WI_BINARY_RESULT (T1, T2)
2428 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2430 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2431 unsigned int precision = get_precision (result);
2432 WIDE_INT_REF_FOR (T1) xi (x, precision);
2433 WIDE_INT_REF_FOR (T2) yi (y, precision);
2434 if (precision <= HOST_BITS_PER_WIDE_INT)
2436 unsigned HOST_WIDE_INT xl = xi.ulow ();
2437 unsigned HOST_WIDE_INT yl = yi.ulow ();
2438 unsigned HOST_WIDE_INT resultl = xl - yl;
2439 if (sgn == SIGNED)
2440 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2441 else
2442 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2443 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2444 val[0] = resultl;
2445 result.set_len (1);
2447 else
2448 result.set_len (sub_large (val, xi.val, xi.len,
2449 yi.val, yi.len, precision,
2450 sgn, overflow));
2451 return result;
2454 /* Return X * Y. */
2455 template <typename T1, typename T2>
2456 inline WI_BINARY_RESULT (T1, T2)
2457 wi::mul (const T1 &x, const T2 &y)
2459 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2460 unsigned int precision = get_precision (result);
2461 WIDE_INT_REF_FOR (T1) xi (x, precision);
2462 WIDE_INT_REF_FOR (T2) yi (y, precision);
2463 if (precision <= HOST_BITS_PER_WIDE_INT)
2465 val[0] = xi.ulow () * yi.ulow ();
2466 result.set_len (1);
2468 else
2469 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2470 precision, UNSIGNED, 0, false));
2471 return result;
2474 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2475 and indicate in *OVERFLOW whether the operation overflowed. */
2476 template <typename T1, typename T2>
2477 inline WI_BINARY_RESULT (T1, T2)
2478 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2480 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2481 unsigned int precision = get_precision (result);
2482 WIDE_INT_REF_FOR (T1) xi (x, precision);
2483 WIDE_INT_REF_FOR (T2) yi (y, precision);
2484 result.set_len (mul_internal (val, xi.val, xi.len,
2485 yi.val, yi.len, precision,
2486 sgn, overflow, false));
2487 return result;
2490 /* Return X * Y, treating both X and Y as signed values. Indicate in
2491 *OVERFLOW whether the operation overflowed. */
2492 template <typename T1, typename T2>
2493 inline WI_BINARY_RESULT (T1, T2)
2494 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2496 return mul (x, y, SIGNED, overflow);
2499 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2500 *OVERFLOW whether the operation overflowed. */
2501 template <typename T1, typename T2>
2502 inline WI_BINARY_RESULT (T1, T2)
2503 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2505 return mul (x, y, UNSIGNED, overflow);
2508 /* Perform a widening multiplication of X and Y, extending the values
2509 according to SGN, and return the high part of the result. */
2510 template <typename T1, typename T2>
2511 inline WI_BINARY_RESULT (T1, T2)
2512 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2514 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2515 unsigned int precision = get_precision (result);
2516 WIDE_INT_REF_FOR (T1) xi (x, precision);
2517 WIDE_INT_REF_FOR (T2) yi (y, precision);
2518 result.set_len (mul_internal (val, xi.val, xi.len,
2519 yi.val, yi.len, precision,
2520 sgn, 0, true));
2521 return result;
2524 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2525 signedness given by SGN. Indicate in *OVERFLOW if the result
2526 overflows. */
2527 template <typename T1, typename T2>
2528 inline WI_BINARY_RESULT (T1, T2)
2529 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2531 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2532 unsigned int precision = get_precision (quotient);
2533 WIDE_INT_REF_FOR (T1) xi (x, precision);
2534 WIDE_INT_REF_FOR (T2) yi (y);
2536 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2537 precision,
2538 yi.val, yi.len, yi.precision,
2539 sgn, overflow));
2540 return quotient;
2543 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2544 template <typename T1, typename T2>
2545 inline WI_BINARY_RESULT (T1, T2)
2546 wi::sdiv_trunc (const T1 &x, const T2 &y)
2548 return div_trunc (x, y, SIGNED);
2551 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2552 template <typename T1, typename T2>
2553 inline WI_BINARY_RESULT (T1, T2)
2554 wi::udiv_trunc (const T1 &x, const T2 &y)
2556 return div_trunc (x, y, UNSIGNED);
2559 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2560 signedness given by SGN. Indicate in *OVERFLOW if the result
2561 overflows. */
2562 template <typename T1, typename T2>
2563 inline WI_BINARY_RESULT (T1, T2)
2564 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2566 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2567 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2568 unsigned int precision = get_precision (quotient);
2569 WIDE_INT_REF_FOR (T1) xi (x, precision);
2570 WIDE_INT_REF_FOR (T2) yi (y);
2572 unsigned int remainder_len;
2573 quotient.set_len (divmod_internal (quotient_val,
2574 &remainder_len, remainder_val,
2575 xi.val, xi.len, precision,
2576 yi.val, yi.len, yi.precision, sgn,
2577 overflow));
2578 remainder.set_len (remainder_len);
2579 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2580 return quotient - 1;
2581 return quotient;
2584 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2585 template <typename T1, typename T2>
2586 inline WI_BINARY_RESULT (T1, T2)
2587 wi::sdiv_floor (const T1 &x, const T2 &y)
2589 return div_floor (x, y, SIGNED);
2592 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2593 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2594 template <typename T1, typename T2>
2595 inline WI_BINARY_RESULT (T1, T2)
2596 wi::udiv_floor (const T1 &x, const T2 &y)
2598 return div_floor (x, y, UNSIGNED);
2601 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2602 signedness given by SGN. Indicate in *OVERFLOW if the result
2603 overflows. */
2604 template <typename T1, typename T2>
2605 inline WI_BINARY_RESULT (T1, T2)
2606 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2608 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2609 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2610 unsigned int precision = get_precision (quotient);
2611 WIDE_INT_REF_FOR (T1) xi (x, precision);
2612 WIDE_INT_REF_FOR (T2) yi (y);
2614 unsigned int remainder_len;
2615 quotient.set_len (divmod_internal (quotient_val,
2616 &remainder_len, remainder_val,
2617 xi.val, xi.len, precision,
2618 yi.val, yi.len, yi.precision, sgn,
2619 overflow));
2620 remainder.set_len (remainder_len);
2621 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2622 return quotient + 1;
2623 return quotient;
2626 /* Return X / Y, rouding towards nearest with ties away from zero.
2627 Treat X and Y as having the signedness given by SGN. Indicate
2628 in *OVERFLOW if the result overflows. */
2629 template <typename T1, typename T2>
2630 inline WI_BINARY_RESULT (T1, T2)
2631 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2633 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2634 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2635 unsigned int precision = get_precision (quotient);
2636 WIDE_INT_REF_FOR (T1) xi (x, precision);
2637 WIDE_INT_REF_FOR (T2) yi (y);
2639 unsigned int remainder_len;
2640 quotient.set_len (divmod_internal (quotient_val,
2641 &remainder_len, remainder_val,
2642 xi.val, xi.len, precision,
2643 yi.val, yi.len, yi.precision, sgn,
2644 overflow));
2645 remainder.set_len (remainder_len);
2647 if (remainder != 0)
2649 if (sgn == SIGNED)
2651 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2652 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2654 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2655 return quotient - 1;
2656 else
2657 return quotient + 1;
2660 else
2662 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2663 return quotient + 1;
2666 return quotient;
2669 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2670 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2671 template <typename T1, typename T2>
2672 inline WI_BINARY_RESULT (T1, T2)
2673 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2674 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2676 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2677 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2678 unsigned int precision = get_precision (quotient);
2679 WIDE_INT_REF_FOR (T1) xi (x, precision);
2680 WIDE_INT_REF_FOR (T2) yi (y);
2682 unsigned int remainder_len;
2683 quotient.set_len (divmod_internal (quotient_val,
2684 &remainder_len, remainder_val,
2685 xi.val, xi.len, precision,
2686 yi.val, yi.len, yi.precision, sgn, 0));
2687 remainder.set_len (remainder_len);
2689 *remainder_ptr = remainder;
2690 return quotient;
2693 /* Compute the greatest common divisor of two numbers A and B using
2694 Euclid's algorithm. */
2695 template <typename T1, typename T2>
2696 inline WI_BINARY_RESULT (T1, T2)
2697 wi::gcd (const T1 &a, const T2 &b, signop sgn)
2699 T1 x, y, z;
2701 x = wi::abs (a);
2702 y = wi::abs (b);
2704 while (gt_p (x, 0, sgn))
2706 z = mod_trunc (y, x, sgn);
2707 y = x;
2708 x = z;
2711 return y;
2714 /* Compute X / Y, rouding towards 0, and return the remainder.
2715 Treat X and Y as having the signedness given by SGN. Indicate
2716 in *OVERFLOW if the division overflows. */
2717 template <typename T1, typename T2>
2718 inline WI_BINARY_RESULT (T1, T2)
2719 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2721 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2722 unsigned int precision = get_precision (remainder);
2723 WIDE_INT_REF_FOR (T1) xi (x, precision);
2724 WIDE_INT_REF_FOR (T2) yi (y);
2726 unsigned int remainder_len;
2727 divmod_internal (0, &remainder_len, remainder_val,
2728 xi.val, xi.len, precision,
2729 yi.val, yi.len, yi.precision, sgn, overflow);
2730 remainder.set_len (remainder_len);
2732 return remainder;
2735 /* Compute X / Y, rouding towards 0, and return the remainder.
2736 Treat X and Y as signed values. */
2737 template <typename T1, typename T2>
2738 inline WI_BINARY_RESULT (T1, T2)
2739 wi::smod_trunc (const T1 &x, const T2 &y)
2741 return mod_trunc (x, y, SIGNED);
2744 /* Compute X / Y, rouding towards 0, and return the remainder.
2745 Treat X and Y as unsigned values. */
2746 template <typename T1, typename T2>
2747 inline WI_BINARY_RESULT (T1, T2)
2748 wi::umod_trunc (const T1 &x, const T2 &y)
2750 return mod_trunc (x, y, UNSIGNED);
2753 /* Compute X / Y, rouding towards -inf, and return the remainder.
2754 Treat X and Y as having the signedness given by SGN. Indicate
2755 in *OVERFLOW if the division overflows. */
2756 template <typename T1, typename T2>
2757 inline WI_BINARY_RESULT (T1, T2)
2758 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2760 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2761 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2762 unsigned int precision = get_precision (quotient);
2763 WIDE_INT_REF_FOR (T1) xi (x, precision);
2764 WIDE_INT_REF_FOR (T2) yi (y);
2766 unsigned int remainder_len;
2767 quotient.set_len (divmod_internal (quotient_val,
2768 &remainder_len, remainder_val,
2769 xi.val, xi.len, precision,
2770 yi.val, yi.len, yi.precision, sgn,
2771 overflow));
2772 remainder.set_len (remainder_len);
2774 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2775 return remainder + y;
2776 return remainder;
2779 /* Compute X / Y, rouding towards -inf, and return the remainder.
2780 Treat X and Y as unsigned values. */
2781 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2782 template <typename T1, typename T2>
2783 inline WI_BINARY_RESULT (T1, T2)
2784 wi::umod_floor (const T1 &x, const T2 &y)
2786 return mod_floor (x, y, UNSIGNED);
2789 /* Compute X / Y, rouding towards +inf, and return the remainder.
2790 Treat X and Y as having the signedness given by SGN. Indicate
2791 in *OVERFLOW if the division overflows. */
2792 template <typename T1, typename T2>
2793 inline WI_BINARY_RESULT (T1, T2)
2794 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2796 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2797 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2798 unsigned int precision = get_precision (quotient);
2799 WIDE_INT_REF_FOR (T1) xi (x, precision);
2800 WIDE_INT_REF_FOR (T2) yi (y);
2802 unsigned int remainder_len;
2803 quotient.set_len (divmod_internal (quotient_val,
2804 &remainder_len, remainder_val,
2805 xi.val, xi.len, precision,
2806 yi.val, yi.len, yi.precision, sgn,
2807 overflow));
2808 remainder.set_len (remainder_len);
2810 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2811 return remainder - y;
2812 return remainder;
2815 /* Compute X / Y, rouding towards nearest with ties away from zero,
2816 and return the remainder. Treat X and Y as having the signedness
2817 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2818 template <typename T1, typename T2>
2819 inline WI_BINARY_RESULT (T1, T2)
2820 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2822 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2823 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2824 unsigned int precision = get_precision (quotient);
2825 WIDE_INT_REF_FOR (T1) xi (x, precision);
2826 WIDE_INT_REF_FOR (T2) yi (y);
2828 unsigned int remainder_len;
2829 quotient.set_len (divmod_internal (quotient_val,
2830 &remainder_len, remainder_val,
2831 xi.val, xi.len, precision,
2832 yi.val, yi.len, yi.precision, sgn,
2833 overflow));
2834 remainder.set_len (remainder_len);
2836 if (remainder != 0)
2838 if (sgn == SIGNED)
2840 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2841 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2843 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2844 return remainder + y;
2845 else
2846 return remainder - y;
2849 else
2851 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2852 return remainder - y;
2855 return remainder;
2858 /* Return true if X is a multiple of Y. Treat X and Y as having the
2859 signedness given by SGN. */
2860 template <typename T1, typename T2>
2861 inline bool
2862 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2864 return wi::mod_trunc (x, y, sgn) == 0;
2867 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2868 Treat X and Y as having the signedness given by SGN. */
2869 template <typename T1, typename T2>
2870 inline bool
2871 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2872 WI_BINARY_RESULT (T1, T2) *res)
2874 WI_BINARY_RESULT (T1, T2) remainder;
2875 WI_BINARY_RESULT (T1, T2) quotient
2876 = divmod_trunc (x, y, sgn, &remainder);
2877 if (remainder == 0)
2879 *res = quotient;
2880 return true;
2882 return false;
2885 /* Return X << Y. Return 0 if Y is greater than or equal to
2886 the precision of X. */
2887 template <typename T1, typename T2>
2888 inline WI_UNARY_RESULT (T1)
2889 wi::lshift (const T1 &x, const T2 &y)
2891 WI_UNARY_RESULT_VAR (result, val, T1, x);
2892 unsigned int precision = get_precision (result);
2893 WIDE_INT_REF_FOR (T1) xi (x, precision);
2894 WIDE_INT_REF_FOR (T2) yi (y);
2895 /* Handle the simple cases quickly. */
2896 if (geu_p (yi, precision))
2898 val[0] = 0;
2899 result.set_len (1);
2901 else
2903 unsigned int shift = yi.to_uhwi ();
2904 /* For fixed-precision integers like offset_int and widest_int,
2905 handle the case where the shift value is constant and the
2906 result is a single nonnegative HWI (meaning that we don't
2907 need to worry about val[1]). This is particularly common
2908 for converting a byte count to a bit count.
2910 For variable-precision integers like wide_int, handle HWI
2911 and sub-HWI integers inline. */
2912 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2913 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2914 && xi.len == 1
2915 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2916 HOST_WIDE_INT_MAX >> shift))
2917 : precision <= HOST_BITS_PER_WIDE_INT)
2919 val[0] = xi.ulow () << shift;
2920 result.set_len (1);
2922 else
2923 result.set_len (lshift_large (val, xi.val, xi.len,
2924 precision, shift));
2926 return result;
2929 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2930 or equal to the precision of X. */
2931 template <typename T1, typename T2>
2932 inline WI_UNARY_RESULT (T1)
2933 wi::lrshift (const T1 &x, const T2 &y)
2935 WI_UNARY_RESULT_VAR (result, val, T1, x);
2936 /* Do things in the precision of the input rather than the output,
2937 since the result can be no larger than that. */
2938 WIDE_INT_REF_FOR (T1) xi (x);
2939 WIDE_INT_REF_FOR (T2) yi (y);
2940 /* Handle the simple cases quickly. */
2941 if (geu_p (yi, xi.precision))
2943 val[0] = 0;
2944 result.set_len (1);
2946 else
2948 unsigned int shift = yi.to_uhwi ();
2949 /* For fixed-precision integers like offset_int and widest_int,
2950 handle the case where the shift value is constant and the
2951 shifted value is a single nonnegative HWI (meaning that all
2952 bits above the HWI are zero). This is particularly common
2953 for converting a bit count to a byte count.
2955 For variable-precision integers like wide_int, handle HWI
2956 and sub-HWI integers inline. */
2957 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2958 ? (shift < HOST_BITS_PER_WIDE_INT
2959 && xi.len == 1
2960 && xi.val[0] >= 0)
2961 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2963 val[0] = xi.to_uhwi () >> shift;
2964 result.set_len (1);
2966 else
2967 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2968 get_precision (result), shift));
2970 return result;
2973 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2974 Y is greater than or equal to the precision of X. */
2975 template <typename T1, typename T2>
2976 inline WI_UNARY_RESULT (T1)
2977 wi::arshift (const T1 &x, const T2 &y)
2979 WI_UNARY_RESULT_VAR (result, val, T1, x);
2980 /* Do things in the precision of the input rather than the output,
2981 since the result can be no larger than that. */
2982 WIDE_INT_REF_FOR (T1) xi (x);
2983 WIDE_INT_REF_FOR (T2) yi (y);
2984 /* Handle the simple cases quickly. */
2985 if (geu_p (yi, xi.precision))
2987 val[0] = sign_mask (x);
2988 result.set_len (1);
2990 else
2992 unsigned int shift = yi.to_uhwi ();
2993 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2995 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2996 result.set_len (1, true);
2998 else
2999 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3000 get_precision (result), shift));
3002 return result;
3005 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3006 logical shift otherwise. */
3007 template <typename T1, typename T2>
3008 inline WI_UNARY_RESULT (T1)
3009 wi::rshift (const T1 &x, const T2 &y, signop sgn)
3011 if (sgn == UNSIGNED)
3012 return lrshift (x, y);
3013 else
3014 return arshift (x, y);
3017 /* Return the result of rotating the low WIDTH bits of X left by Y
3018 bits and zero-extending the result. Use a full-width rotate if
3019 WIDTH is zero. */
3020 template <typename T1, typename T2>
3021 WI_UNARY_RESULT (T1)
3022 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3024 unsigned int precision = get_binary_precision (x, x);
3025 if (width == 0)
3026 width = precision;
3027 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3028 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3029 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
3030 if (width != precision)
3031 return wi::zext (left, width) | wi::zext (right, width);
3032 return left | right;
3035 /* Return the result of rotating the low WIDTH bits of X right by Y
3036 bits and zero-extending the result. Use a full-width rotate if
3037 WIDTH is zero. */
3038 template <typename T1, typename T2>
3039 WI_UNARY_RESULT (T1)
3040 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3042 unsigned int precision = get_binary_precision (x, x);
3043 if (width == 0)
3044 width = precision;
3045 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3046 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
3047 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3048 if (width != precision)
3049 return wi::zext (left, width) | wi::zext (right, width);
3050 return left | right;
3053 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3054 is odd. */
3055 inline int
3056 wi::parity (const wide_int_ref &x)
3058 return popcount (x) & 1;
3061 /* Extract WIDTH bits from X, starting at BITPOS. */
3062 template <typename T>
3063 inline unsigned HOST_WIDE_INT
3064 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3066 unsigned precision = get_precision (x);
3067 if (precision < bitpos + width)
3068 precision = bitpos + width;
3069 WIDE_INT_REF_FOR (T) xi (x, precision);
3071 /* Handle this rare case after the above, so that we assert about
3072 bogus BITPOS values. */
3073 if (width == 0)
3074 return 0;
3076 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3077 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3078 unsigned HOST_WIDE_INT res = xi.elt (start);
3079 res >>= shift;
3080 if (shift + width > HOST_BITS_PER_WIDE_INT)
3082 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3083 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3085 return zext_hwi (res, width);
3088 /* Return the minimum precision needed to store X with sign SGN. */
3089 template <typename T>
3090 inline unsigned int
3091 wi::min_precision (const T &x, signop sgn)
3093 if (sgn == SIGNED)
3094 return get_precision (x) - clrsb (x);
3095 else
3096 return get_precision (x) - clz (x);
3099 #define SIGNED_BINARY_PREDICATE(OP, F) \
3100 template <typename T1, typename T2> \
3101 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3102 OP (const T1 &x, const T2 &y) \
3104 return wi::F (x, y); \
3107 SIGNED_BINARY_PREDICATE (operator <, lts_p)
3108 SIGNED_BINARY_PREDICATE (operator <=, les_p)
3109 SIGNED_BINARY_PREDICATE (operator >, gts_p)
3110 SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3112 #undef SIGNED_BINARY_PREDICATE
3114 template <typename T1, typename T2>
3115 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3116 operator << (const T1 &x, const T2 &y)
3118 return wi::lshift (x, y);
3121 template <typename T1, typename T2>
3122 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3123 operator >> (const T1 &x, const T2 &y)
3125 return wi::arshift (x, y);
3128 template<typename T>
3129 void
3130 gt_ggc_mx (generic_wide_int <T> *)
3134 template<typename T>
3135 void
3136 gt_pch_nx (generic_wide_int <T> *)
3140 template<typename T>
3141 void
3142 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3146 template<int N>
3147 void
3148 gt_ggc_mx (trailing_wide_ints <N> *)
3152 template<int N>
3153 void
3154 gt_pch_nx (trailing_wide_ints <N> *)
3158 template<int N>
3159 void
3160 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3164 namespace wi
3166 /* Used for overloaded functions in which the only other acceptable
3167 scalar type is a pointer. It stops a plain 0 from being treated
3168 as a null pointer. */
3169 struct never_used1 {};
3170 struct never_used2 {};
3172 wide_int min_value (unsigned int, signop);
3173 wide_int min_value (never_used1 *);
3174 wide_int min_value (never_used2 *);
3175 wide_int max_value (unsigned int, signop);
3176 wide_int max_value (never_used1 *);
3177 wide_int max_value (never_used2 *);
3179 /* FIXME: this is target dependent, so should be elsewhere.
3180 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3181 wide_int from_buffer (const unsigned char *, unsigned int);
3183 #ifndef GENERATOR_FILE
3184 void to_mpz (const wide_int_ref &, mpz_t, signop);
3185 #endif
3187 wide_int mask (unsigned int, bool, unsigned int);
3188 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3189 wide_int set_bit_in_zero (unsigned int, unsigned int);
3190 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3191 unsigned int);
3193 template <typename T>
3194 T mask (unsigned int, bool);
3196 template <typename T>
3197 T shifted_mask (unsigned int, unsigned int, bool);
3199 template <typename T>
3200 T set_bit_in_zero (unsigned int);
3202 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3203 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3204 bool, unsigned int);
3205 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3206 unsigned int, unsigned int, bool);
3209 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3210 and the other bits are clear, or the inverse if NEGATE_P. */
3211 inline wide_int
3212 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3214 wide_int result = wide_int::create (precision);
3215 result.set_len (mask (result.write_val (), width, negate_p, precision));
3216 return result;
3219 /* Return a PRECISION-bit integer in which the low START bits are clear,
3220 the next WIDTH bits are set, and the other bits are clear,
3221 or the inverse if NEGATE_P. */
3222 inline wide_int
3223 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3224 unsigned int precision)
3226 wide_int result = wide_int::create (precision);
3227 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3228 precision));
3229 return result;
3232 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3233 others are clear. */
3234 inline wide_int
3235 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3237 return shifted_mask (bit, 1, false, precision);
3240 /* Return an integer of type T in which the low WIDTH bits are set
3241 and the other bits are clear, or the inverse if NEGATE_P. */
3242 template <typename T>
3243 inline T
3244 wi::mask (unsigned int width, bool negate_p)
3246 STATIC_ASSERT (wi::int_traits<T>::precision);
3247 T result;
3248 result.set_len (mask (result.write_val (), width, negate_p,
3249 wi::int_traits <T>::precision));
3250 return result;
3253 /* Return an integer of type T in which the low START bits are clear,
3254 the next WIDTH bits are set, and the other bits are clear, or the
3255 inverse if NEGATE_P. */
3256 template <typename T>
3257 inline T
3258 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3260 STATIC_ASSERT (wi::int_traits<T>::precision);
3261 T result;
3262 result.set_len (shifted_mask (result.write_val (), start, width,
3263 negate_p,
3264 wi::int_traits <T>::precision));
3265 return result;
3268 /* Return an integer of type T in which bit BIT is set and all the
3269 others are clear. */
3270 template <typename T>
3271 inline T
3272 wi::set_bit_in_zero (unsigned int bit)
3274 return shifted_mask <T> (bit, 1, false);
3277 #endif /* WIDE_INT_H */