[13/77] Make floatn_mode return an opt_scalar_float_mode
[official-gcc.git] / gcc / wide-int.h
blob0596f9841188fa4cb288722d63d0778bbd4bf9f3
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 : precision (p), sgn (s)
1522 if (precision < HOST_BITS_PER_WIDE_INT)
1523 val = sext_hwi (v, precision);
1524 else
1525 val = v;
1528 /* Return a signed integer that has value VAL and precision PRECISION. */
1529 inline wi::hwi_with_prec
1530 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1532 return hwi_with_prec (val, precision, SIGNED);
1535 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1536 inline wi::hwi_with_prec
1537 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1539 return hwi_with_prec (val, precision, UNSIGNED);
1542 /* Return a wide int of -1 with precision PRECISION. */
1543 inline wi::hwi_with_prec
1544 wi::minus_one (unsigned int precision)
1546 return wi::shwi (-1, precision);
1549 /* Return a wide int of 0 with precision PRECISION. */
1550 inline wi::hwi_with_prec
1551 wi::zero (unsigned int precision)
1553 return wi::shwi (0, precision);
1556 /* Return a wide int of 1 with precision PRECISION. */
1557 inline wi::hwi_with_prec
1558 wi::one (unsigned int precision)
1560 return wi::shwi (1, precision);
1563 /* Return a wide int of 2 with precision PRECISION. */
1564 inline wi::hwi_with_prec
1565 wi::two (unsigned int precision)
1567 return wi::shwi (2, precision);
1570 namespace wi
1572 template <>
1573 struct int_traits <wi::hwi_with_prec>
1575 static const enum precision_type precision_type = VAR_PRECISION;
1576 /* hwi_with_prec has an explicitly-given precision, rather than the
1577 precision of HOST_WIDE_INT. */
1578 static const bool host_dependent_precision = false;
1579 static const bool is_sign_extended = true;
1580 static unsigned int get_precision (const wi::hwi_with_prec &);
1581 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1582 const wi::hwi_with_prec &);
1586 inline unsigned int
1587 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1589 return x.precision;
1592 inline wi::storage_ref
1593 wi::int_traits <wi::hwi_with_prec>::
1594 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1595 const wi::hwi_with_prec &x)
1597 gcc_checking_assert (precision == x.precision);
1598 scratch[0] = x.val;
1599 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1600 return wi::storage_ref (scratch, 1, precision);
1601 scratch[1] = 0;
1602 return wi::storage_ref (scratch, 2, precision);
1605 /* Private functions for handling large cases out of line. They take
1606 individual length and array parameters because that is cheaper for
1607 the inline caller than constructing an object on the stack and
1608 passing a reference to it. (Although many callers use wide_int_refs,
1609 we generally want those to be removed by SRA.) */
1610 namespace wi
1612 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1613 const HOST_WIDE_INT *, unsigned int, unsigned int);
1614 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1615 const HOST_WIDE_INT *, unsigned int);
1616 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1617 const HOST_WIDE_INT *, unsigned int);
1618 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1619 const HOST_WIDE_INT *, unsigned int);
1620 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1621 const HOST_WIDE_INT *, unsigned int);
1622 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1623 unsigned int,
1624 unsigned int, unsigned int);
1625 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1626 unsigned int,
1627 unsigned int, unsigned int);
1628 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1629 unsigned int, unsigned int, unsigned int);
1630 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1631 unsigned int, unsigned int, unsigned int);
1632 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1633 unsigned int, unsigned int, unsigned int,
1634 unsigned int);
1635 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1636 unsigned int, unsigned int, unsigned int,
1637 unsigned int);
1638 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1639 const HOST_WIDE_INT *, unsigned int, unsigned int);
1640 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1641 unsigned int, const HOST_WIDE_INT *,
1642 unsigned int, unsigned int);
1643 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1644 const HOST_WIDE_INT *, unsigned int, unsigned int);
1645 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1646 unsigned int, const HOST_WIDE_INT *,
1647 unsigned int, unsigned int);
1648 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1649 const HOST_WIDE_INT *, unsigned int, unsigned int);
1650 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1651 const HOST_WIDE_INT *, unsigned int, unsigned int,
1652 signop, bool *);
1653 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1654 const HOST_WIDE_INT *, unsigned int, unsigned int,
1655 signop, bool *);
1656 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1657 unsigned int, const HOST_WIDE_INT *,
1658 unsigned int, unsigned int, signop, bool *,
1659 bool);
1660 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1661 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1662 unsigned int, unsigned int,
1663 const HOST_WIDE_INT *,
1664 unsigned int, unsigned int,
1665 signop, bool *);
1668 /* Return the number of bits that integer X can hold. */
1669 template <typename T>
1670 inline unsigned int
1671 wi::get_precision (const T &x)
1673 return wi::int_traits <T>::get_precision (x);
1676 /* Return the number of bits that the result of a binary operation can
1677 hold when the input operands are X and Y. */
1678 template <typename T1, typename T2>
1679 inline unsigned int
1680 wi::get_binary_precision (const T1 &x, const T2 &y)
1682 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1683 get_binary_result (x, y));
1686 /* Copy the contents of Y to X, but keeping X's current precision. */
1687 template <typename T1, typename T2>
1688 inline void
1689 wi::copy (T1 &x, const T2 &y)
1691 HOST_WIDE_INT *xval = x.write_val ();
1692 const HOST_WIDE_INT *yval = y.get_val ();
1693 unsigned int len = y.get_len ();
1694 unsigned int i = 0;
1696 xval[i] = yval[i];
1697 while (++i < len);
1698 x.set_len (len, y.is_sign_extended);
1701 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1702 template <typename T>
1703 inline bool
1704 wi::fits_shwi_p (const T &x)
1706 WIDE_INT_REF_FOR (T) xi (x);
1707 return xi.len == 1;
1710 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1711 precision. */
1712 template <typename T>
1713 inline bool
1714 wi::fits_uhwi_p (const T &x)
1716 WIDE_INT_REF_FOR (T) xi (x);
1717 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1718 return true;
1719 if (xi.len == 1)
1720 return xi.slow () >= 0;
1721 return xi.len == 2 && xi.uhigh () == 0;
1724 /* Return true if X is negative based on the interpretation of SGN.
1725 For UNSIGNED, this is always false. */
1726 template <typename T>
1727 inline bool
1728 wi::neg_p (const T &x, signop sgn)
1730 WIDE_INT_REF_FOR (T) xi (x);
1731 if (sgn == UNSIGNED)
1732 return false;
1733 return xi.sign_mask () < 0;
1736 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1737 template <typename T>
1738 inline HOST_WIDE_INT
1739 wi::sign_mask (const T &x)
1741 WIDE_INT_REF_FOR (T) xi (x);
1742 return xi.sign_mask ();
1745 /* Return true if X == Y. X and Y must be binary-compatible. */
1746 template <typename T1, typename T2>
1747 inline bool
1748 wi::eq_p (const T1 &x, const T2 &y)
1750 unsigned int precision = get_binary_precision (x, y);
1751 WIDE_INT_REF_FOR (T1) xi (x, precision);
1752 WIDE_INT_REF_FOR (T2) yi (y, precision);
1753 if (xi.is_sign_extended && yi.is_sign_extended)
1755 /* This case reduces to array equality. */
1756 if (xi.len != yi.len)
1757 return false;
1758 unsigned int i = 0;
1760 if (xi.val[i] != yi.val[i])
1761 return false;
1762 while (++i != xi.len);
1763 return true;
1765 if (__builtin_expect (yi.len == 1, true))
1767 /* XI is only equal to YI if it too has a single HWI. */
1768 if (xi.len != 1)
1769 return false;
1770 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1771 with 0 are simple. */
1772 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1773 return xi.val[0] == 0;
1774 /* Otherwise flush out any excess bits first. */
1775 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1776 int excess = HOST_BITS_PER_WIDE_INT - precision;
1777 if (excess > 0)
1778 diff <<= excess;
1779 return diff == 0;
1781 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1784 /* Return true if X != Y. X and Y must be binary-compatible. */
1785 template <typename T1, typename T2>
1786 inline bool
1787 wi::ne_p (const T1 &x, const T2 &y)
1789 return !eq_p (x, y);
1792 /* Return true if X < Y when both are treated as signed values. */
1793 template <typename T1, typename T2>
1794 inline bool
1795 wi::lts_p (const T1 &x, const T2 &y)
1797 unsigned int precision = get_binary_precision (x, y);
1798 WIDE_INT_REF_FOR (T1) xi (x, precision);
1799 WIDE_INT_REF_FOR (T2) yi (y, precision);
1800 /* We optimize x < y, where y is 64 or fewer bits. */
1801 if (wi::fits_shwi_p (yi))
1803 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1804 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1805 return neg_p (xi);
1806 /* If x fits directly into a shwi, we can compare directly. */
1807 if (wi::fits_shwi_p (xi))
1808 return xi.to_shwi () < yi.to_shwi ();
1809 /* If x doesn't fit and is negative, then it must be more
1810 negative than any value in y, and hence smaller than y. */
1811 if (neg_p (xi))
1812 return true;
1813 /* If x is positive, then it must be larger than any value in y,
1814 and hence greater than y. */
1815 return false;
1817 /* Optimize the opposite case, if it can be detected at compile time. */
1818 if (STATIC_CONSTANT_P (xi.len == 1))
1819 /* If YI is negative it is lower than the least HWI.
1820 If YI is positive it is greater than the greatest HWI. */
1821 return !neg_p (yi);
1822 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1825 /* Return true if X < Y when both are treated as unsigned values. */
1826 template <typename T1, typename T2>
1827 inline bool
1828 wi::ltu_p (const T1 &x, const T2 &y)
1830 unsigned int precision = get_binary_precision (x, y);
1831 WIDE_INT_REF_FOR (T1) xi (x, precision);
1832 WIDE_INT_REF_FOR (T2) yi (y, precision);
1833 /* Optimize comparisons with constants. */
1834 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1835 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1836 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1837 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1838 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1839 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1840 values does not change the result. */
1841 if (__builtin_expect (xi.len + yi.len == 2, true))
1843 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1844 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1845 return xl < yl;
1847 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1850 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1851 template <typename T1, typename T2>
1852 inline bool
1853 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1855 if (sgn == SIGNED)
1856 return lts_p (x, y);
1857 else
1858 return ltu_p (x, y);
1861 /* Return true if X <= Y when both are treated as signed values. */
1862 template <typename T1, typename T2>
1863 inline bool
1864 wi::les_p (const T1 &x, const T2 &y)
1866 return !lts_p (y, x);
1869 /* Return true if X <= Y when both are treated as unsigned values. */
1870 template <typename T1, typename T2>
1871 inline bool
1872 wi::leu_p (const T1 &x, const T2 &y)
1874 return !ltu_p (y, x);
1877 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1878 template <typename T1, typename T2>
1879 inline bool
1880 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1882 if (sgn == SIGNED)
1883 return les_p (x, y);
1884 else
1885 return leu_p (x, y);
1888 /* Return true if X > Y when both are treated as signed values. */
1889 template <typename T1, typename T2>
1890 inline bool
1891 wi::gts_p (const T1 &x, const T2 &y)
1893 return lts_p (y, x);
1896 /* Return true if X > Y when both are treated as unsigned values. */
1897 template <typename T1, typename T2>
1898 inline bool
1899 wi::gtu_p (const T1 &x, const T2 &y)
1901 return ltu_p (y, x);
1904 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1905 template <typename T1, typename T2>
1906 inline bool
1907 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1909 if (sgn == SIGNED)
1910 return gts_p (x, y);
1911 else
1912 return gtu_p (x, y);
1915 /* Return true if X >= Y when both are treated as signed values. */
1916 template <typename T1, typename T2>
1917 inline bool
1918 wi::ges_p (const T1 &x, const T2 &y)
1920 return !lts_p (x, y);
1923 /* Return true if X >= Y when both are treated as unsigned values. */
1924 template <typename T1, typename T2>
1925 inline bool
1926 wi::geu_p (const T1 &x, const T2 &y)
1928 return !ltu_p (x, y);
1931 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1932 template <typename T1, typename T2>
1933 inline bool
1934 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1936 if (sgn == SIGNED)
1937 return ges_p (x, y);
1938 else
1939 return geu_p (x, y);
1942 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1943 as signed values. */
1944 template <typename T1, typename T2>
1945 inline int
1946 wi::cmps (const T1 &x, const T2 &y)
1948 unsigned int precision = get_binary_precision (x, y);
1949 WIDE_INT_REF_FOR (T1) xi (x, precision);
1950 WIDE_INT_REF_FOR (T2) yi (y, precision);
1951 if (wi::fits_shwi_p (yi))
1953 /* Special case for comparisons with 0. */
1954 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1955 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1956 /* If x fits into a signed HWI, we can compare directly. */
1957 if (wi::fits_shwi_p (xi))
1959 HOST_WIDE_INT xl = xi.to_shwi ();
1960 HOST_WIDE_INT yl = yi.to_shwi ();
1961 return xl < yl ? -1 : xl > yl;
1963 /* If x doesn't fit and is negative, then it must be more
1964 negative than any signed HWI, and hence smaller than y. */
1965 if (neg_p (xi))
1966 return -1;
1967 /* If x is positive, then it must be larger than any signed HWI,
1968 and hence greater than y. */
1969 return 1;
1971 /* Optimize the opposite case, if it can be detected at compile time. */
1972 if (STATIC_CONSTANT_P (xi.len == 1))
1973 /* If YI is negative it is lower than the least HWI.
1974 If YI is positive it is greater than the greatest HWI. */
1975 return neg_p (yi) ? 1 : -1;
1976 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1979 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1980 as unsigned values. */
1981 template <typename T1, typename T2>
1982 inline int
1983 wi::cmpu (const T1 &x, const T2 &y)
1985 unsigned int precision = get_binary_precision (x, y);
1986 WIDE_INT_REF_FOR (T1) xi (x, precision);
1987 WIDE_INT_REF_FOR (T2) yi (y, precision);
1988 /* Optimize comparisons with constants. */
1989 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1991 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1992 if (xi.len != 1)
1993 return 1;
1994 /* Otherwise compare directly. */
1995 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1996 unsigned HOST_WIDE_INT yl = yi.val[0];
1997 return xl < yl ? -1 : xl > yl;
1999 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2001 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2002 if (yi.len != 1)
2003 return -1;
2004 /* Otherwise compare directly. */
2005 unsigned HOST_WIDE_INT xl = xi.val[0];
2006 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2007 return xl < yl ? -1 : xl > yl;
2009 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2010 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2011 values does not change the result. */
2012 if (__builtin_expect (xi.len + yi.len == 2, true))
2014 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2015 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2016 return xl < yl ? -1 : xl > yl;
2018 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2021 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2022 X and Y indicated by SGN. */
2023 template <typename T1, typename T2>
2024 inline int
2025 wi::cmp (const T1 &x, const T2 &y, signop sgn)
2027 if (sgn == SIGNED)
2028 return cmps (x, y);
2029 else
2030 return cmpu (x, y);
2033 /* Return ~x. */
2034 template <typename T>
2035 inline WI_UNARY_RESULT (T)
2036 wi::bit_not (const T &x)
2038 WI_UNARY_RESULT_VAR (result, val, T, x);
2039 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2040 for (unsigned int i = 0; i < xi.len; ++i)
2041 val[i] = ~xi.val[i];
2042 result.set_len (xi.len);
2043 return result;
2046 /* Return -x. */
2047 template <typename T>
2048 inline WI_UNARY_RESULT (T)
2049 wi::neg (const T &x)
2051 return sub (0, x);
2054 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2055 template <typename T>
2056 inline WI_UNARY_RESULT (T)
2057 wi::neg (const T &x, bool *overflow)
2059 *overflow = only_sign_bit_p (x);
2060 return sub (0, x);
2063 /* Return the absolute value of x. */
2064 template <typename T>
2065 inline WI_UNARY_RESULT (T)
2066 wi::abs (const T &x)
2068 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2071 /* Return the result of sign-extending the low OFFSET bits of X. */
2072 template <typename T>
2073 inline WI_UNARY_RESULT (T)
2074 wi::sext (const T &x, unsigned int offset)
2076 WI_UNARY_RESULT_VAR (result, val, T, x);
2077 unsigned int precision = get_precision (result);
2078 WIDE_INT_REF_FOR (T) xi (x, precision);
2080 if (offset <= HOST_BITS_PER_WIDE_INT)
2082 val[0] = sext_hwi (xi.ulow (), offset);
2083 result.set_len (1, true);
2085 else
2086 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2087 return result;
2090 /* Return the result of zero-extending the low OFFSET bits of X. */
2091 template <typename T>
2092 inline WI_UNARY_RESULT (T)
2093 wi::zext (const T &x, unsigned int offset)
2095 WI_UNARY_RESULT_VAR (result, val, T, x);
2096 unsigned int precision = get_precision (result);
2097 WIDE_INT_REF_FOR (T) xi (x, precision);
2099 /* This is not just an optimization, it is actually required to
2100 maintain canonization. */
2101 if (offset >= precision)
2103 wi::copy (result, xi);
2104 return result;
2107 /* In these cases we know that at least the top bit will be clear,
2108 so no sign extension is necessary. */
2109 if (offset < HOST_BITS_PER_WIDE_INT)
2111 val[0] = zext_hwi (xi.ulow (), offset);
2112 result.set_len (1, true);
2114 else
2115 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2116 return result;
2119 /* Return the result of extending the low OFFSET bits of X according to
2120 signedness SGN. */
2121 template <typename T>
2122 inline WI_UNARY_RESULT (T)
2123 wi::ext (const T &x, unsigned int offset, signop sgn)
2125 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2128 /* Return an integer that represents X | (1 << bit). */
2129 template <typename T>
2130 inline WI_UNARY_RESULT (T)
2131 wi::set_bit (const T &x, unsigned int bit)
2133 WI_UNARY_RESULT_VAR (result, val, T, x);
2134 unsigned int precision = get_precision (result);
2135 WIDE_INT_REF_FOR (T) xi (x, precision);
2136 if (precision <= HOST_BITS_PER_WIDE_INT)
2138 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2139 result.set_len (1);
2141 else
2142 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2143 return result;
2146 /* Return the mininum of X and Y, treating them both as having
2147 signedness SGN. */
2148 template <typename T1, typename T2>
2149 inline WI_BINARY_RESULT (T1, T2)
2150 wi::min (const T1 &x, const T2 &y, signop sgn)
2152 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2153 unsigned int precision = get_precision (result);
2154 if (wi::le_p (x, y, sgn))
2155 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2156 else
2157 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2158 return result;
2161 /* Return the minimum of X and Y, treating both as signed values. */
2162 template <typename T1, typename T2>
2163 inline WI_BINARY_RESULT (T1, T2)
2164 wi::smin (const T1 &x, const T2 &y)
2166 return wi::min (x, y, SIGNED);
2169 /* Return the minimum of X and Y, treating both as unsigned values. */
2170 template <typename T1, typename T2>
2171 inline WI_BINARY_RESULT (T1, T2)
2172 wi::umin (const T1 &x, const T2 &y)
2174 return wi::min (x, y, UNSIGNED);
2177 /* Return the maxinum of X and Y, treating them both as having
2178 signedness SGN. */
2179 template <typename T1, typename T2>
2180 inline WI_BINARY_RESULT (T1, T2)
2181 wi::max (const T1 &x, const T2 &y, signop sgn)
2183 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2184 unsigned int precision = get_precision (result);
2185 if (wi::ge_p (x, y, sgn))
2186 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2187 else
2188 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2189 return result;
2192 /* Return the maximum of X and Y, treating both as signed values. */
2193 template <typename T1, typename T2>
2194 inline WI_BINARY_RESULT (T1, T2)
2195 wi::smax (const T1 &x, const T2 &y)
2197 return wi::max (x, y, SIGNED);
2200 /* Return the maximum of X and Y, treating both as unsigned values. */
2201 template <typename T1, typename T2>
2202 inline WI_BINARY_RESULT (T1, T2)
2203 wi::umax (const T1 &x, const T2 &y)
2205 return wi::max (x, y, UNSIGNED);
2208 /* Return X & Y. */
2209 template <typename T1, typename T2>
2210 inline WI_BINARY_RESULT (T1, T2)
2211 wi::bit_and (const T1 &x, const T2 &y)
2213 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2214 unsigned int precision = get_precision (result);
2215 WIDE_INT_REF_FOR (T1) xi (x, precision);
2216 WIDE_INT_REF_FOR (T2) yi (y, precision);
2217 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2218 if (__builtin_expect (xi.len + yi.len == 2, true))
2220 val[0] = xi.ulow () & yi.ulow ();
2221 result.set_len (1, is_sign_extended);
2223 else
2224 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2225 precision), is_sign_extended);
2226 return result;
2229 /* Return X & ~Y. */
2230 template <typename T1, typename T2>
2231 inline WI_BINARY_RESULT (T1, T2)
2232 wi::bit_and_not (const T1 &x, const T2 &y)
2234 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2235 unsigned int precision = get_precision (result);
2236 WIDE_INT_REF_FOR (T1) xi (x, precision);
2237 WIDE_INT_REF_FOR (T2) yi (y, precision);
2238 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2239 if (__builtin_expect (xi.len + yi.len == 2, true))
2241 val[0] = xi.ulow () & ~yi.ulow ();
2242 result.set_len (1, is_sign_extended);
2244 else
2245 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2246 precision), is_sign_extended);
2247 return result;
2250 /* Return X | Y. */
2251 template <typename T1, typename T2>
2252 inline WI_BINARY_RESULT (T1, T2)
2253 wi::bit_or (const T1 &x, const T2 &y)
2255 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2256 unsigned int precision = get_precision (result);
2257 WIDE_INT_REF_FOR (T1) xi (x, precision);
2258 WIDE_INT_REF_FOR (T2) yi (y, precision);
2259 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2260 if (__builtin_expect (xi.len + yi.len == 2, true))
2262 val[0] = xi.ulow () | yi.ulow ();
2263 result.set_len (1, is_sign_extended);
2265 else
2266 result.set_len (or_large (val, xi.val, xi.len,
2267 yi.val, yi.len, precision), is_sign_extended);
2268 return result;
2271 /* Return X | ~Y. */
2272 template <typename T1, typename T2>
2273 inline WI_BINARY_RESULT (T1, T2)
2274 wi::bit_or_not (const T1 &x, const T2 &y)
2276 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2277 unsigned int precision = get_precision (result);
2278 WIDE_INT_REF_FOR (T1) xi (x, precision);
2279 WIDE_INT_REF_FOR (T2) yi (y, precision);
2280 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2281 if (__builtin_expect (xi.len + yi.len == 2, true))
2283 val[0] = xi.ulow () | ~yi.ulow ();
2284 result.set_len (1, is_sign_extended);
2286 else
2287 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2288 precision), is_sign_extended);
2289 return result;
2292 /* Return X ^ Y. */
2293 template <typename T1, typename T2>
2294 inline WI_BINARY_RESULT (T1, T2)
2295 wi::bit_xor (const T1 &x, const T2 &y)
2297 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2298 unsigned int precision = get_precision (result);
2299 WIDE_INT_REF_FOR (T1) xi (x, precision);
2300 WIDE_INT_REF_FOR (T2) yi (y, precision);
2301 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2302 if (__builtin_expect (xi.len + yi.len == 2, true))
2304 val[0] = xi.ulow () ^ yi.ulow ();
2305 result.set_len (1, is_sign_extended);
2307 else
2308 result.set_len (xor_large (val, xi.val, xi.len,
2309 yi.val, yi.len, precision), is_sign_extended);
2310 return result;
2313 /* Return X + Y. */
2314 template <typename T1, typename T2>
2315 inline WI_BINARY_RESULT (T1, T2)
2316 wi::add (const T1 &x, const T2 &y)
2318 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2319 unsigned int precision = get_precision (result);
2320 WIDE_INT_REF_FOR (T1) xi (x, precision);
2321 WIDE_INT_REF_FOR (T2) yi (y, precision);
2322 if (precision <= HOST_BITS_PER_WIDE_INT)
2324 val[0] = xi.ulow () + yi.ulow ();
2325 result.set_len (1);
2327 /* If the precision is known at compile time to be greater than
2328 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2329 knowing that (a) all bits in those HWIs are significant and
2330 (b) the result has room for at least two HWIs. This provides
2331 a fast path for things like offset_int and widest_int.
2333 The STATIC_CONSTANT_P test prevents this path from being
2334 used for wide_ints. wide_ints with precisions greater than
2335 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2336 point handling them inline. */
2337 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2338 && __builtin_expect (xi.len + yi.len == 2, true))
2340 unsigned HOST_WIDE_INT xl = xi.ulow ();
2341 unsigned HOST_WIDE_INT yl = yi.ulow ();
2342 unsigned HOST_WIDE_INT resultl = xl + yl;
2343 val[0] = resultl;
2344 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2345 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2346 >> (HOST_BITS_PER_WIDE_INT - 1)));
2348 else
2349 result.set_len (add_large (val, xi.val, xi.len,
2350 yi.val, yi.len, precision,
2351 UNSIGNED, 0));
2352 return result;
2355 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2356 and indicate in *OVERFLOW whether the operation overflowed. */
2357 template <typename T1, typename T2>
2358 inline WI_BINARY_RESULT (T1, T2)
2359 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2361 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2362 unsigned int precision = get_precision (result);
2363 WIDE_INT_REF_FOR (T1) xi (x, precision);
2364 WIDE_INT_REF_FOR (T2) yi (y, precision);
2365 if (precision <= HOST_BITS_PER_WIDE_INT)
2367 unsigned HOST_WIDE_INT xl = xi.ulow ();
2368 unsigned HOST_WIDE_INT yl = yi.ulow ();
2369 unsigned HOST_WIDE_INT resultl = xl + yl;
2370 if (sgn == SIGNED)
2371 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2372 >> (precision - 1)) & 1;
2373 else
2374 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2375 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2376 val[0] = resultl;
2377 result.set_len (1);
2379 else
2380 result.set_len (add_large (val, xi.val, xi.len,
2381 yi.val, yi.len, precision,
2382 sgn, overflow));
2383 return result;
2386 /* Return X - Y. */
2387 template <typename T1, typename T2>
2388 inline WI_BINARY_RESULT (T1, T2)
2389 wi::sub (const T1 &x, const T2 &y)
2391 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2392 unsigned int precision = get_precision (result);
2393 WIDE_INT_REF_FOR (T1) xi (x, precision);
2394 WIDE_INT_REF_FOR (T2) yi (y, precision);
2395 if (precision <= HOST_BITS_PER_WIDE_INT)
2397 val[0] = xi.ulow () - yi.ulow ();
2398 result.set_len (1);
2400 /* If the precision is known at compile time to be greater than
2401 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2402 knowing that (a) all bits in those HWIs are significant and
2403 (b) the result has room for at least two HWIs. This provides
2404 a fast path for things like offset_int and widest_int.
2406 The STATIC_CONSTANT_P test prevents this path from being
2407 used for wide_ints. wide_ints with precisions greater than
2408 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2409 point handling them inline. */
2410 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2411 && __builtin_expect (xi.len + yi.len == 2, true))
2413 unsigned HOST_WIDE_INT xl = xi.ulow ();
2414 unsigned HOST_WIDE_INT yl = yi.ulow ();
2415 unsigned HOST_WIDE_INT resultl = xl - yl;
2416 val[0] = resultl;
2417 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2418 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2419 >> (HOST_BITS_PER_WIDE_INT - 1)));
2421 else
2422 result.set_len (sub_large (val, xi.val, xi.len,
2423 yi.val, yi.len, precision,
2424 UNSIGNED, 0));
2425 return result;
2428 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2429 and indicate in *OVERFLOW whether the operation overflowed. */
2430 template <typename T1, typename T2>
2431 inline WI_BINARY_RESULT (T1, T2)
2432 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2434 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2435 unsigned int precision = get_precision (result);
2436 WIDE_INT_REF_FOR (T1) xi (x, precision);
2437 WIDE_INT_REF_FOR (T2) yi (y, precision);
2438 if (precision <= HOST_BITS_PER_WIDE_INT)
2440 unsigned HOST_WIDE_INT xl = xi.ulow ();
2441 unsigned HOST_WIDE_INT yl = yi.ulow ();
2442 unsigned HOST_WIDE_INT resultl = xl - yl;
2443 if (sgn == SIGNED)
2444 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2445 else
2446 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2447 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2448 val[0] = resultl;
2449 result.set_len (1);
2451 else
2452 result.set_len (sub_large (val, xi.val, xi.len,
2453 yi.val, yi.len, precision,
2454 sgn, overflow));
2455 return result;
2458 /* Return X * Y. */
2459 template <typename T1, typename T2>
2460 inline WI_BINARY_RESULT (T1, T2)
2461 wi::mul (const T1 &x, const T2 &y)
2463 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2464 unsigned int precision = get_precision (result);
2465 WIDE_INT_REF_FOR (T1) xi (x, precision);
2466 WIDE_INT_REF_FOR (T2) yi (y, precision);
2467 if (precision <= HOST_BITS_PER_WIDE_INT)
2469 val[0] = xi.ulow () * yi.ulow ();
2470 result.set_len (1);
2472 else
2473 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2474 precision, UNSIGNED, 0, false));
2475 return result;
2478 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2479 and indicate in *OVERFLOW whether the operation overflowed. */
2480 template <typename T1, typename T2>
2481 inline WI_BINARY_RESULT (T1, T2)
2482 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2484 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2485 unsigned int precision = get_precision (result);
2486 WIDE_INT_REF_FOR (T1) xi (x, precision);
2487 WIDE_INT_REF_FOR (T2) yi (y, precision);
2488 result.set_len (mul_internal (val, xi.val, xi.len,
2489 yi.val, yi.len, precision,
2490 sgn, overflow, false));
2491 return result;
2494 /* Return X * Y, treating both X and Y as signed values. Indicate in
2495 *OVERFLOW whether the operation overflowed. */
2496 template <typename T1, typename T2>
2497 inline WI_BINARY_RESULT (T1, T2)
2498 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2500 return mul (x, y, SIGNED, overflow);
2503 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2504 *OVERFLOW whether the operation overflowed. */
2505 template <typename T1, typename T2>
2506 inline WI_BINARY_RESULT (T1, T2)
2507 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2509 return mul (x, y, UNSIGNED, overflow);
2512 /* Perform a widening multiplication of X and Y, extending the values
2513 according to SGN, and return the high part of the result. */
2514 template <typename T1, typename T2>
2515 inline WI_BINARY_RESULT (T1, T2)
2516 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2518 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2519 unsigned int precision = get_precision (result);
2520 WIDE_INT_REF_FOR (T1) xi (x, precision);
2521 WIDE_INT_REF_FOR (T2) yi (y, precision);
2522 result.set_len (mul_internal (val, xi.val, xi.len,
2523 yi.val, yi.len, precision,
2524 sgn, 0, true));
2525 return result;
2528 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2529 signedness given by SGN. Indicate in *OVERFLOW if the result
2530 overflows. */
2531 template <typename T1, typename T2>
2532 inline WI_BINARY_RESULT (T1, T2)
2533 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2535 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2536 unsigned int precision = get_precision (quotient);
2537 WIDE_INT_REF_FOR (T1) xi (x, precision);
2538 WIDE_INT_REF_FOR (T2) yi (y);
2540 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2541 precision,
2542 yi.val, yi.len, yi.precision,
2543 sgn, overflow));
2544 return quotient;
2547 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2548 template <typename T1, typename T2>
2549 inline WI_BINARY_RESULT (T1, T2)
2550 wi::sdiv_trunc (const T1 &x, const T2 &y)
2552 return div_trunc (x, y, SIGNED);
2555 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2556 template <typename T1, typename T2>
2557 inline WI_BINARY_RESULT (T1, T2)
2558 wi::udiv_trunc (const T1 &x, const T2 &y)
2560 return div_trunc (x, y, UNSIGNED);
2563 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2564 signedness given by SGN. Indicate in *OVERFLOW if the result
2565 overflows. */
2566 template <typename T1, typename T2>
2567 inline WI_BINARY_RESULT (T1, T2)
2568 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2570 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2571 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2572 unsigned int precision = get_precision (quotient);
2573 WIDE_INT_REF_FOR (T1) xi (x, precision);
2574 WIDE_INT_REF_FOR (T2) yi (y);
2576 unsigned int remainder_len;
2577 quotient.set_len (divmod_internal (quotient_val,
2578 &remainder_len, remainder_val,
2579 xi.val, xi.len, precision,
2580 yi.val, yi.len, yi.precision, sgn,
2581 overflow));
2582 remainder.set_len (remainder_len);
2583 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2584 return quotient - 1;
2585 return quotient;
2588 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2589 template <typename T1, typename T2>
2590 inline WI_BINARY_RESULT (T1, T2)
2591 wi::sdiv_floor (const T1 &x, const T2 &y)
2593 return div_floor (x, y, SIGNED);
2596 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2597 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2598 template <typename T1, typename T2>
2599 inline WI_BINARY_RESULT (T1, T2)
2600 wi::udiv_floor (const T1 &x, const T2 &y)
2602 return div_floor (x, y, UNSIGNED);
2605 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2606 signedness given by SGN. Indicate in *OVERFLOW if the result
2607 overflows. */
2608 template <typename T1, typename T2>
2609 inline WI_BINARY_RESULT (T1, T2)
2610 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2612 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2613 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2614 unsigned int precision = get_precision (quotient);
2615 WIDE_INT_REF_FOR (T1) xi (x, precision);
2616 WIDE_INT_REF_FOR (T2) yi (y);
2618 unsigned int remainder_len;
2619 quotient.set_len (divmod_internal (quotient_val,
2620 &remainder_len, remainder_val,
2621 xi.val, xi.len, precision,
2622 yi.val, yi.len, yi.precision, sgn,
2623 overflow));
2624 remainder.set_len (remainder_len);
2625 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2626 return quotient + 1;
2627 return quotient;
2630 /* Return X / Y, rouding towards nearest with ties away from zero.
2631 Treat X and Y as having the signedness given by SGN. Indicate
2632 in *OVERFLOW if the result overflows. */
2633 template <typename T1, typename T2>
2634 inline WI_BINARY_RESULT (T1, T2)
2635 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2637 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2638 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2639 unsigned int precision = get_precision (quotient);
2640 WIDE_INT_REF_FOR (T1) xi (x, precision);
2641 WIDE_INT_REF_FOR (T2) yi (y);
2643 unsigned int remainder_len;
2644 quotient.set_len (divmod_internal (quotient_val,
2645 &remainder_len, remainder_val,
2646 xi.val, xi.len, precision,
2647 yi.val, yi.len, yi.precision, sgn,
2648 overflow));
2649 remainder.set_len (remainder_len);
2651 if (remainder != 0)
2653 if (sgn == SIGNED)
2655 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2656 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2658 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2659 return quotient - 1;
2660 else
2661 return quotient + 1;
2664 else
2666 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2667 return quotient + 1;
2670 return quotient;
2673 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2674 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2675 template <typename T1, typename T2>
2676 inline WI_BINARY_RESULT (T1, T2)
2677 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2678 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2680 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2681 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2682 unsigned int precision = get_precision (quotient);
2683 WIDE_INT_REF_FOR (T1) xi (x, precision);
2684 WIDE_INT_REF_FOR (T2) yi (y);
2686 unsigned int remainder_len;
2687 quotient.set_len (divmod_internal (quotient_val,
2688 &remainder_len, remainder_val,
2689 xi.val, xi.len, precision,
2690 yi.val, yi.len, yi.precision, sgn, 0));
2691 remainder.set_len (remainder_len);
2693 *remainder_ptr = remainder;
2694 return quotient;
2697 /* Compute the greatest common divisor of two numbers A and B using
2698 Euclid's algorithm. */
2699 template <typename T1, typename T2>
2700 inline WI_BINARY_RESULT (T1, T2)
2701 wi::gcd (const T1 &a, const T2 &b, signop sgn)
2703 T1 x, y, z;
2705 x = wi::abs (a);
2706 y = wi::abs (b);
2708 while (gt_p (x, 0, sgn))
2710 z = mod_trunc (y, x, sgn);
2711 y = x;
2712 x = z;
2715 return y;
2718 /* Compute X / Y, rouding towards 0, and return the remainder.
2719 Treat X and Y as having the signedness given by SGN. Indicate
2720 in *OVERFLOW if the division overflows. */
2721 template <typename T1, typename T2>
2722 inline WI_BINARY_RESULT (T1, T2)
2723 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2725 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2726 unsigned int precision = get_precision (remainder);
2727 WIDE_INT_REF_FOR (T1) xi (x, precision);
2728 WIDE_INT_REF_FOR (T2) yi (y);
2730 unsigned int remainder_len;
2731 divmod_internal (0, &remainder_len, remainder_val,
2732 xi.val, xi.len, precision,
2733 yi.val, yi.len, yi.precision, sgn, overflow);
2734 remainder.set_len (remainder_len);
2736 return remainder;
2739 /* Compute X / Y, rouding towards 0, and return the remainder.
2740 Treat X and Y as signed values. */
2741 template <typename T1, typename T2>
2742 inline WI_BINARY_RESULT (T1, T2)
2743 wi::smod_trunc (const T1 &x, const T2 &y)
2745 return mod_trunc (x, y, SIGNED);
2748 /* Compute X / Y, rouding towards 0, and return the remainder.
2749 Treat X and Y as unsigned values. */
2750 template <typename T1, typename T2>
2751 inline WI_BINARY_RESULT (T1, T2)
2752 wi::umod_trunc (const T1 &x, const T2 &y)
2754 return mod_trunc (x, y, UNSIGNED);
2757 /* Compute X / Y, rouding towards -inf, and return the remainder.
2758 Treat X and Y as having the signedness given by SGN. Indicate
2759 in *OVERFLOW if the division overflows. */
2760 template <typename T1, typename T2>
2761 inline WI_BINARY_RESULT (T1, T2)
2762 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2764 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2765 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2766 unsigned int precision = get_precision (quotient);
2767 WIDE_INT_REF_FOR (T1) xi (x, precision);
2768 WIDE_INT_REF_FOR (T2) yi (y);
2770 unsigned int remainder_len;
2771 quotient.set_len (divmod_internal (quotient_val,
2772 &remainder_len, remainder_val,
2773 xi.val, xi.len, precision,
2774 yi.val, yi.len, yi.precision, sgn,
2775 overflow));
2776 remainder.set_len (remainder_len);
2778 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2779 return remainder + y;
2780 return remainder;
2783 /* Compute X / Y, rouding towards -inf, and return the remainder.
2784 Treat X and Y as unsigned values. */
2785 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2786 template <typename T1, typename T2>
2787 inline WI_BINARY_RESULT (T1, T2)
2788 wi::umod_floor (const T1 &x, const T2 &y)
2790 return mod_floor (x, y, UNSIGNED);
2793 /* Compute X / Y, rouding towards +inf, and return the remainder.
2794 Treat X and Y as having the signedness given by SGN. Indicate
2795 in *OVERFLOW if the division overflows. */
2796 template <typename T1, typename T2>
2797 inline WI_BINARY_RESULT (T1, T2)
2798 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2800 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2801 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2802 unsigned int precision = get_precision (quotient);
2803 WIDE_INT_REF_FOR (T1) xi (x, precision);
2804 WIDE_INT_REF_FOR (T2) yi (y);
2806 unsigned int remainder_len;
2807 quotient.set_len (divmod_internal (quotient_val,
2808 &remainder_len, remainder_val,
2809 xi.val, xi.len, precision,
2810 yi.val, yi.len, yi.precision, sgn,
2811 overflow));
2812 remainder.set_len (remainder_len);
2814 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2815 return remainder - y;
2816 return remainder;
2819 /* Compute X / Y, rouding towards nearest with ties away from zero,
2820 and return the remainder. Treat X and Y as having the signedness
2821 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2822 template <typename T1, typename T2>
2823 inline WI_BINARY_RESULT (T1, T2)
2824 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2826 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2827 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2828 unsigned int precision = get_precision (quotient);
2829 WIDE_INT_REF_FOR (T1) xi (x, precision);
2830 WIDE_INT_REF_FOR (T2) yi (y);
2832 unsigned int remainder_len;
2833 quotient.set_len (divmod_internal (quotient_val,
2834 &remainder_len, remainder_val,
2835 xi.val, xi.len, precision,
2836 yi.val, yi.len, yi.precision, sgn,
2837 overflow));
2838 remainder.set_len (remainder_len);
2840 if (remainder != 0)
2842 if (sgn == SIGNED)
2844 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2845 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2847 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2848 return remainder + y;
2849 else
2850 return remainder - y;
2853 else
2855 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2856 return remainder - y;
2859 return remainder;
2862 /* Return true if X is a multiple of Y. Treat X and Y as having the
2863 signedness given by SGN. */
2864 template <typename T1, typename T2>
2865 inline bool
2866 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2868 return wi::mod_trunc (x, y, sgn) == 0;
2871 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2872 Treat X and Y as having the signedness given by SGN. */
2873 template <typename T1, typename T2>
2874 inline bool
2875 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2876 WI_BINARY_RESULT (T1, T2) *res)
2878 WI_BINARY_RESULT (T1, T2) remainder;
2879 WI_BINARY_RESULT (T1, T2) quotient
2880 = divmod_trunc (x, y, sgn, &remainder);
2881 if (remainder == 0)
2883 *res = quotient;
2884 return true;
2886 return false;
2889 /* Return X << Y. Return 0 if Y is greater than or equal to
2890 the precision of X. */
2891 template <typename T1, typename T2>
2892 inline WI_UNARY_RESULT (T1)
2893 wi::lshift (const T1 &x, const T2 &y)
2895 WI_UNARY_RESULT_VAR (result, val, T1, x);
2896 unsigned int precision = get_precision (result);
2897 WIDE_INT_REF_FOR (T1) xi (x, precision);
2898 WIDE_INT_REF_FOR (T2) yi (y);
2899 /* Handle the simple cases quickly. */
2900 if (geu_p (yi, precision))
2902 val[0] = 0;
2903 result.set_len (1);
2905 else
2907 unsigned int shift = yi.to_uhwi ();
2908 /* For fixed-precision integers like offset_int and widest_int,
2909 handle the case where the shift value is constant and the
2910 result is a single nonnegative HWI (meaning that we don't
2911 need to worry about val[1]). This is particularly common
2912 for converting a byte count to a bit count.
2914 For variable-precision integers like wide_int, handle HWI
2915 and sub-HWI integers inline. */
2916 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2917 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2918 && xi.len == 1
2919 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2920 HOST_WIDE_INT_MAX >> shift))
2921 : precision <= HOST_BITS_PER_WIDE_INT)
2923 val[0] = xi.ulow () << shift;
2924 result.set_len (1);
2926 else
2927 result.set_len (lshift_large (val, xi.val, xi.len,
2928 precision, shift));
2930 return result;
2933 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2934 or equal to the precision of X. */
2935 template <typename T1, typename T2>
2936 inline WI_UNARY_RESULT (T1)
2937 wi::lrshift (const T1 &x, const T2 &y)
2939 WI_UNARY_RESULT_VAR (result, val, T1, x);
2940 /* Do things in the precision of the input rather than the output,
2941 since the result can be no larger than that. */
2942 WIDE_INT_REF_FOR (T1) xi (x);
2943 WIDE_INT_REF_FOR (T2) yi (y);
2944 /* Handle the simple cases quickly. */
2945 if (geu_p (yi, xi.precision))
2947 val[0] = 0;
2948 result.set_len (1);
2950 else
2952 unsigned int shift = yi.to_uhwi ();
2953 /* For fixed-precision integers like offset_int and widest_int,
2954 handle the case where the shift value is constant and the
2955 shifted value is a single nonnegative HWI (meaning that all
2956 bits above the HWI are zero). This is particularly common
2957 for converting a bit count to a byte count.
2959 For variable-precision integers like wide_int, handle HWI
2960 and sub-HWI integers inline. */
2961 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2962 ? (shift < HOST_BITS_PER_WIDE_INT
2963 && xi.len == 1
2964 && xi.val[0] >= 0)
2965 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2967 val[0] = xi.to_uhwi () >> shift;
2968 result.set_len (1);
2970 else
2971 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2972 get_precision (result), shift));
2974 return result;
2977 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2978 Y is greater than or equal to the precision of X. */
2979 template <typename T1, typename T2>
2980 inline WI_UNARY_RESULT (T1)
2981 wi::arshift (const T1 &x, const T2 &y)
2983 WI_UNARY_RESULT_VAR (result, val, T1, x);
2984 /* Do things in the precision of the input rather than the output,
2985 since the result can be no larger than that. */
2986 WIDE_INT_REF_FOR (T1) xi (x);
2987 WIDE_INT_REF_FOR (T2) yi (y);
2988 /* Handle the simple cases quickly. */
2989 if (geu_p (yi, xi.precision))
2991 val[0] = sign_mask (x);
2992 result.set_len (1);
2994 else
2996 unsigned int shift = yi.to_uhwi ();
2997 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2999 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3000 result.set_len (1, true);
3002 else
3003 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3004 get_precision (result), shift));
3006 return result;
3009 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3010 logical shift otherwise. */
3011 template <typename T1, typename T2>
3012 inline WI_UNARY_RESULT (T1)
3013 wi::rshift (const T1 &x, const T2 &y, signop sgn)
3015 if (sgn == UNSIGNED)
3016 return lrshift (x, y);
3017 else
3018 return arshift (x, y);
3021 /* Return the result of rotating the low WIDTH bits of X left by Y
3022 bits and zero-extending the result. Use a full-width rotate if
3023 WIDTH is zero. */
3024 template <typename T1, typename T2>
3025 WI_UNARY_RESULT (T1)
3026 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3028 unsigned int precision = get_binary_precision (x, x);
3029 if (width == 0)
3030 width = precision;
3031 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3032 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3033 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
3034 if (width != precision)
3035 return wi::zext (left, width) | wi::zext (right, width);
3036 return left | right;
3039 /* Return the result of rotating the low WIDTH bits of X right by Y
3040 bits and zero-extending the result. Use a full-width rotate if
3041 WIDTH is zero. */
3042 template <typename T1, typename T2>
3043 WI_UNARY_RESULT (T1)
3044 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3046 unsigned int precision = get_binary_precision (x, x);
3047 if (width == 0)
3048 width = precision;
3049 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3050 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
3051 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3052 if (width != precision)
3053 return wi::zext (left, width) | wi::zext (right, width);
3054 return left | right;
3057 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3058 is odd. */
3059 inline int
3060 wi::parity (const wide_int_ref &x)
3062 return popcount (x) & 1;
3065 /* Extract WIDTH bits from X, starting at BITPOS. */
3066 template <typename T>
3067 inline unsigned HOST_WIDE_INT
3068 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3070 unsigned precision = get_precision (x);
3071 if (precision < bitpos + width)
3072 precision = bitpos + width;
3073 WIDE_INT_REF_FOR (T) xi (x, precision);
3075 /* Handle this rare case after the above, so that we assert about
3076 bogus BITPOS values. */
3077 if (width == 0)
3078 return 0;
3080 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3081 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3082 unsigned HOST_WIDE_INT res = xi.elt (start);
3083 res >>= shift;
3084 if (shift + width > HOST_BITS_PER_WIDE_INT)
3086 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3087 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3089 return zext_hwi (res, width);
3092 /* Return the minimum precision needed to store X with sign SGN. */
3093 template <typename T>
3094 inline unsigned int
3095 wi::min_precision (const T &x, signop sgn)
3097 if (sgn == SIGNED)
3098 return get_precision (x) - clrsb (x);
3099 else
3100 return get_precision (x) - clz (x);
3103 #define SIGNED_BINARY_PREDICATE(OP, F) \
3104 template <typename T1, typename T2> \
3105 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3106 OP (const T1 &x, const T2 &y) \
3108 return wi::F (x, y); \
3111 SIGNED_BINARY_PREDICATE (operator <, lts_p)
3112 SIGNED_BINARY_PREDICATE (operator <=, les_p)
3113 SIGNED_BINARY_PREDICATE (operator >, gts_p)
3114 SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3116 #undef SIGNED_BINARY_PREDICATE
3118 template <typename T1, typename T2>
3119 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3120 operator << (const T1 &x, const T2 &y)
3122 return wi::lshift (x, y);
3125 template <typename T1, typename T2>
3126 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3127 operator >> (const T1 &x, const T2 &y)
3129 return wi::arshift (x, y);
3132 template<typename T>
3133 void
3134 gt_ggc_mx (generic_wide_int <T> *)
3138 template<typename T>
3139 void
3140 gt_pch_nx (generic_wide_int <T> *)
3144 template<typename T>
3145 void
3146 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3150 template<int N>
3151 void
3152 gt_ggc_mx (trailing_wide_ints <N> *)
3156 template<int N>
3157 void
3158 gt_pch_nx (trailing_wide_ints <N> *)
3162 template<int N>
3163 void
3164 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3168 namespace wi
3170 /* Used for overloaded functions in which the only other acceptable
3171 scalar type is a pointer. It stops a plain 0 from being treated
3172 as a null pointer. */
3173 struct never_used1 {};
3174 struct never_used2 {};
3176 wide_int min_value (unsigned int, signop);
3177 wide_int min_value (never_used1 *);
3178 wide_int min_value (never_used2 *);
3179 wide_int max_value (unsigned int, signop);
3180 wide_int max_value (never_used1 *);
3181 wide_int max_value (never_used2 *);
3183 /* FIXME: this is target dependent, so should be elsewhere.
3184 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3185 wide_int from_buffer (const unsigned char *, unsigned int);
3187 #ifndef GENERATOR_FILE
3188 void to_mpz (const wide_int_ref &, mpz_t, signop);
3189 #endif
3191 wide_int mask (unsigned int, bool, unsigned int);
3192 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3193 wide_int set_bit_in_zero (unsigned int, unsigned int);
3194 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3195 unsigned int);
3197 template <typename T>
3198 T mask (unsigned int, bool);
3200 template <typename T>
3201 T shifted_mask (unsigned int, unsigned int, bool);
3203 template <typename T>
3204 T set_bit_in_zero (unsigned int);
3206 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3207 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3208 bool, unsigned int);
3209 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3210 unsigned int, unsigned int, bool);
3213 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3214 and the other bits are clear, or the inverse if NEGATE_P. */
3215 inline wide_int
3216 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3218 wide_int result = wide_int::create (precision);
3219 result.set_len (mask (result.write_val (), width, negate_p, precision));
3220 return result;
3223 /* Return a PRECISION-bit integer in which the low START bits are clear,
3224 the next WIDTH bits are set, and the other bits are clear,
3225 or the inverse if NEGATE_P. */
3226 inline wide_int
3227 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3228 unsigned int precision)
3230 wide_int result = wide_int::create (precision);
3231 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3232 precision));
3233 return result;
3236 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3237 others are clear. */
3238 inline wide_int
3239 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3241 return shifted_mask (bit, 1, false, precision);
3244 /* Return an integer of type T in which the low WIDTH bits are set
3245 and the other bits are clear, or the inverse if NEGATE_P. */
3246 template <typename T>
3247 inline T
3248 wi::mask (unsigned int width, bool negate_p)
3250 STATIC_ASSERT (wi::int_traits<T>::precision);
3251 T result;
3252 result.set_len (mask (result.write_val (), width, negate_p,
3253 wi::int_traits <T>::precision));
3254 return result;
3257 /* Return an integer of type T in which the low START bits are clear,
3258 the next WIDTH bits are set, and the other bits are clear, or the
3259 inverse if NEGATE_P. */
3260 template <typename T>
3261 inline T
3262 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3264 STATIC_ASSERT (wi::int_traits<T>::precision);
3265 T result;
3266 result.set_len (shifted_mask (result.write_val (), start, width,
3267 negate_p,
3268 wi::int_traits <T>::precision));
3269 return result;
3272 /* Return an integer of type T in which bit BIT is set and all the
3273 others are clear. */
3274 template <typename T>
3275 inline T
3276 wi::set_bit_in_zero (unsigned int bit)
3278 return shifted_mask <T> (bit, 1, false);
3281 #endif /* WIDE_INT_H */