Fix wi::lshift
[official-gcc.git] / gcc / wide-int.h
blob6c816cc260bca1fd1ca14eb89e3984202c3f59cc
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2019 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 the options are:
155 tree t = ...;
156 wi::to_wide (t) // Treat T as a wide_int
157 wi::to_offset (t) // Treat T as an offset_int
158 wi::to_widest (t) // Treat T as a widest_int
160 All three are light-weight accessors that should have no overhead
161 in release builds. If it is useful for readability reasons to
162 store the result in a temporary variable, the preferred method is:
164 wi::tree_to_wide_ref twide = wi::to_wide (t);
165 wi::tree_to_offset_ref toffset = wi::to_offset (t);
166 wi::tree_to_widest_ref twidest = wi::to_widest (t);
168 To make an rtx into a wide_int, you have to pair it with a mode.
169 The canonical way to do this is with rtx_mode_t as in:
171 rtx r = ...
172 wide_int x = rtx_mode_t (r, mode);
174 Similarly, a wide_int can only be constructed from a host value if
175 the target precision is given explicitly, such as in:
177 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
178 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
180 However, offset_int and widest_int have an inherent precision and so
181 can be initialized directly from a host value:
183 offset_int x = (int) c; // sign-extend C
184 widest_int x = (unsigned int) c; // zero-extend C
186 It is also possible to do arithmetic directly on rtx_mode_ts and
187 constants. For example:
189 wi::add (r1, r2); // add equal-sized rtx_mode_ts r1 and r2
190 wi::add (r1, 1); // add 1 to rtx_mode_t r1
191 wi::lshift (1, 100); // 1 << 100 as a widest_int
193 Many binary operations place restrictions on the combinations of inputs,
194 using the following rules:
196 - {rtx, wide_int} op {rtx, wide_int} -> wide_int
197 The inputs must be the same precision. The result is a wide_int
198 of the same precision
200 - {rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
201 (un)signed HOST_WIDE_INT op {rtx, wide_int} -> wide_int
202 The HOST_WIDE_INT is extended or truncated to the precision of
203 the other input. The result is a wide_int of the same precision
204 as that input.
206 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
207 The inputs are extended to widest_int precision and produce a
208 widest_int result.
210 - offset_int op offset_int -> offset_int
211 offset_int op (un)signed HOST_WIDE_INT -> offset_int
212 (un)signed HOST_WIDE_INT op offset_int -> offset_int
214 - widest_int op widest_int -> widest_int
215 widest_int op (un)signed HOST_WIDE_INT -> widest_int
216 (un)signed HOST_WIDE_INT op widest_int -> widest_int
218 Other combinations like:
220 - widest_int op offset_int and
221 - wide_int op offset_int
223 are not allowed. The inputs should instead be extended or truncated
224 so that they match.
226 The inputs to comparison functions like wi::eq_p and wi::lts_p
227 follow the same compatibility rules, although their return types
228 are different. Unary functions on X produce the same result as
229 a binary operation X + X. Shift functions X op Y also produce
230 the same result as X + X; the precision of the shift amount Y
231 can be arbitrarily different from X. */
233 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
234 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
235 can accomodate at least 1 more bit so that unsigned numbers of that
236 mode can be represented as a signed value. Note that it is still
237 possible to create fixed_wide_ints that have precisions greater than
238 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
239 double-width multiplication result, for example. */
240 #define WIDE_INT_MAX_ELTS \
241 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
243 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
245 /* This is the max size of any pointer on any machine. It does not
246 seem to be as easy to sniff this out of the machine description as
247 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
248 multiple address sizes and may have different address sizes for
249 different address spaces. However, currently the largest pointer
250 on any platform is 64 bits. When that changes, then it is likely
251 that a target hook should be defined so that targets can make this
252 value larger for those targets. */
253 #define ADDR_MAX_BITSIZE 64
255 /* This is the internal precision used when doing any address
256 arithmetic. The '4' is really 3 + 1. Three of the bits are for
257 the number of extra bits needed to do bit addresses and the other bit
258 is to allow everything to be signed without loosing any precision.
259 Then everything is rounded up to the next HWI for efficiency. */
260 #define ADDR_MAX_PRECISION \
261 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
262 & ~(HOST_BITS_PER_WIDE_INT - 1))
264 /* The number of HWIs needed to store an offset_int. */
265 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
267 /* The type of result produced by a binary operation on types T1 and T2.
268 Defined purely for brevity. */
269 #define WI_BINARY_RESULT(T1, T2) \
270 typename wi::binary_traits <T1, T2>::result_type
272 /* Likewise for binary operators, which excludes the case in which neither
273 T1 nor T2 is a wide-int-based type. */
274 #define WI_BINARY_OPERATOR_RESULT(T1, T2) \
275 typename wi::binary_traits <T1, T2>::operator_result
277 /* The type of result produced by T1 << T2. Leads to substitution failure
278 if the operation isn't supported. Defined purely for brevity. */
279 #define WI_SIGNED_SHIFT_RESULT(T1, T2) \
280 typename wi::binary_traits <T1, T2>::signed_shift_result_type
282 /* The type of result produced by a sign-agnostic binary predicate on
283 types T1 and T2. This is bool if wide-int operations make sense for
284 T1 and T2 and leads to substitution failure otherwise. */
285 #define WI_BINARY_PREDICATE_RESULT(T1, T2) \
286 typename wi::binary_traits <T1, T2>::predicate_result
288 /* The type of result produced by a signed binary predicate on types T1 and T2.
289 This is bool if signed comparisons make sense for T1 and T2 and leads to
290 substitution failure otherwise. */
291 #define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2) \
292 typename wi::binary_traits <T1, T2>::signed_predicate_result
294 /* The type of result produced by a unary operation on type T. */
295 #define WI_UNARY_RESULT(T) \
296 typename wi::binary_traits <T, T>::result_type
298 /* Define a variable RESULT to hold the result of a binary operation on
299 X and Y, which have types T1 and T2 respectively. Define VAL to
300 point to the blocks of RESULT. Once the user of the macro has
301 filled in VAL, it should call RESULT.set_len to set the number
302 of initialized blocks. */
303 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
304 WI_BINARY_RESULT (T1, T2) RESULT = \
305 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
306 HOST_WIDE_INT *VAL = RESULT.write_val ()
308 /* Similar for the result of a unary operation on X, which has type T. */
309 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
310 WI_UNARY_RESULT (T) RESULT = \
311 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
312 HOST_WIDE_INT *VAL = RESULT.write_val ()
314 template <typename T> class generic_wide_int;
315 template <int N> class fixed_wide_int_storage;
316 class wide_int_storage;
318 /* An N-bit integer. Until we can use typedef templates, use this instead. */
319 #define FIXED_WIDE_INT(N) \
320 generic_wide_int < fixed_wide_int_storage <N> >
322 typedef generic_wide_int <wide_int_storage> wide_int;
323 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
324 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
325 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
326 so as not to confuse gengtype. */
327 typedef generic_wide_int < fixed_wide_int_storage <WIDE_INT_MAX_PRECISION * 2> > widest2_int;
329 /* wi::storage_ref can be a reference to a primitive type,
330 so this is the conservatively-correct setting. */
331 template <bool SE, bool HDP = true>
332 class wide_int_ref_storage;
334 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
336 /* This can be used instead of wide_int_ref if the referenced value is
337 known to have type T. It carries across properties of T's representation,
338 such as whether excess upper bits in a HWI are defined, and can therefore
339 help avoid redundant work.
341 The macro could be replaced with a template typedef, once we're able
342 to use those. */
343 #define WIDE_INT_REF_FOR(T) \
344 generic_wide_int \
345 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
346 wi::int_traits <T>::host_dependent_precision> >
348 namespace wi
350 /* Operations that calculate overflow do so even for
351 TYPE_OVERFLOW_WRAPS types. For example, adding 1 to +MAX_INT in
352 an unsigned int is 0 and does not overflow in C/C++, but wi::add
353 will set the overflow argument in case it's needed for further
354 analysis.
356 For operations that require overflow, these are the different
357 types of overflow. */
358 enum overflow_type {
359 OVF_NONE = 0,
360 OVF_UNDERFLOW = -1,
361 OVF_OVERFLOW = 1,
362 /* There was an overflow, but we are unsure whether it was an
363 overflow or an underflow. */
364 OVF_UNKNOWN = 2
367 /* Classifies an integer based on its precision. */
368 enum precision_type {
369 /* The integer has both a precision and defined signedness. This allows
370 the integer to be converted to any width, since we know whether to fill
371 any extra bits with zeros or signs. */
372 FLEXIBLE_PRECISION,
374 /* The integer has a variable precision but no defined signedness. */
375 VAR_PRECISION,
377 /* The integer has a constant precision (known at GCC compile time)
378 and is signed. */
379 CONST_PRECISION
382 /* This class, which has no default implementation, is expected to
383 provide the following members:
385 static const enum precision_type precision_type;
386 Classifies the type of T.
388 static const unsigned int precision;
389 Only defined if precision_type == CONST_PRECISION. Specifies the
390 precision of all integers of type T.
392 static const bool host_dependent_precision;
393 True if the precision of T depends (or can depend) on the host.
395 static unsigned int get_precision (const T &x)
396 Return the number of bits in X.
398 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
399 unsigned int precision, const T &x)
400 Decompose X as a PRECISION-bit integer, returning the associated
401 wi::storage_ref. SCRATCH is available as scratch space if needed.
402 The routine should assert that PRECISION is acceptable. */
403 template <typename T> struct int_traits;
405 /* This class provides a single type, result_type, which specifies the
406 type of integer produced by a binary operation whose inputs have
407 types T1 and T2. The definition should be symmetric. */
408 template <typename T1, typename T2,
409 enum precision_type P1 = int_traits <T1>::precision_type,
410 enum precision_type P2 = int_traits <T2>::precision_type>
411 struct binary_traits;
413 /* Specify the result type for each supported combination of binary
414 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
415 mixed, in order to give stronger type checking. When both inputs
416 are CONST_PRECISION, they must have the same precision. */
417 template <typename T1, typename T2>
418 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
420 typedef widest_int result_type;
421 /* Don't define operators for this combination. */
424 template <typename T1, typename T2>
425 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
427 typedef wide_int result_type;
428 typedef result_type operator_result;
429 typedef bool predicate_result;
432 template <typename T1, typename T2>
433 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
435 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
436 so as not to confuse gengtype. */
437 typedef generic_wide_int < fixed_wide_int_storage
438 <int_traits <T2>::precision> > result_type;
439 typedef result_type operator_result;
440 typedef bool predicate_result;
441 typedef result_type signed_shift_result_type;
442 typedef bool signed_predicate_result;
445 template <typename T1, typename T2>
446 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
448 typedef wide_int result_type;
449 typedef result_type operator_result;
450 typedef bool predicate_result;
453 template <typename T1, typename T2>
454 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
456 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
457 so as not to confuse gengtype. */
458 typedef generic_wide_int < fixed_wide_int_storage
459 <int_traits <T1>::precision> > result_type;
460 typedef result_type operator_result;
461 typedef bool predicate_result;
462 typedef result_type signed_shift_result_type;
463 typedef bool signed_predicate_result;
466 template <typename T1, typename T2>
467 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
469 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
470 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
471 so as not to confuse gengtype. */
472 typedef generic_wide_int < fixed_wide_int_storage
473 <int_traits <T1>::precision> > result_type;
474 typedef result_type operator_result;
475 typedef bool predicate_result;
476 typedef result_type signed_shift_result_type;
477 typedef bool signed_predicate_result;
480 template <typename T1, typename T2>
481 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
483 typedef wide_int result_type;
484 typedef result_type operator_result;
485 typedef bool predicate_result;
489 /* Public functions for querying and operating on integers. */
490 namespace wi
492 template <typename T>
493 unsigned int get_precision (const T &);
495 template <typename T1, typename T2>
496 unsigned int get_binary_precision (const T1 &, const T2 &);
498 template <typename T1, typename T2>
499 void copy (T1 &, const T2 &);
501 #define UNARY_PREDICATE \
502 template <typename T> bool
503 #define UNARY_FUNCTION \
504 template <typename T> WI_UNARY_RESULT (T)
505 #define BINARY_PREDICATE \
506 template <typename T1, typename T2> bool
507 #define BINARY_FUNCTION \
508 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
509 #define SHIFT_FUNCTION \
510 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
512 UNARY_PREDICATE fits_shwi_p (const T &);
513 UNARY_PREDICATE fits_uhwi_p (const T &);
514 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
516 template <typename T>
517 HOST_WIDE_INT sign_mask (const T &);
519 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
520 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
521 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
522 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
523 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
524 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
525 BINARY_PREDICATE les_p (const T1 &, const T2 &);
526 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
527 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
528 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
529 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
530 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
531 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
532 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
534 template <typename T1, typename T2>
535 int cmp (const T1 &, const T2 &, signop);
537 template <typename T1, typename T2>
538 int cmps (const T1 &, const T2 &);
540 template <typename T1, typename T2>
541 int cmpu (const T1 &, const T2 &);
543 UNARY_FUNCTION bit_not (const T &);
544 UNARY_FUNCTION neg (const T &);
545 UNARY_FUNCTION neg (const T &, overflow_type *);
546 UNARY_FUNCTION abs (const T &);
547 UNARY_FUNCTION ext (const T &, unsigned int, signop);
548 UNARY_FUNCTION sext (const T &, unsigned int);
549 UNARY_FUNCTION zext (const T &, unsigned int);
550 UNARY_FUNCTION set_bit (const T &, unsigned int);
552 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
553 BINARY_FUNCTION smin (const T1 &, const T2 &);
554 BINARY_FUNCTION umin (const T1 &, const T2 &);
555 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
556 BINARY_FUNCTION smax (const T1 &, const T2 &);
557 BINARY_FUNCTION umax (const T1 &, const T2 &);
559 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
560 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
561 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
562 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
563 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
564 BINARY_FUNCTION add (const T1 &, const T2 &);
565 BINARY_FUNCTION add (const T1 &, const T2 &, signop, overflow_type *);
566 BINARY_FUNCTION sub (const T1 &, const T2 &);
567 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, overflow_type *);
568 BINARY_FUNCTION mul (const T1 &, const T2 &);
569 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, overflow_type *);
570 BINARY_FUNCTION smul (const T1 &, const T2 &, overflow_type *);
571 BINARY_FUNCTION umul (const T1 &, const T2 &, overflow_type *);
572 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
573 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop,
574 overflow_type * = 0);
575 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
576 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
577 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop,
578 overflow_type * = 0);
579 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
580 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
581 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop,
582 overflow_type * = 0);
583 BINARY_FUNCTION udiv_ceil (const T1 &, const T2 &);
584 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop,
585 overflow_type * = 0);
586 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
587 WI_BINARY_RESULT (T1, T2) *);
588 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
589 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop,
590 overflow_type * = 0);
591 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
592 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
593 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop,
594 overflow_type * = 0);
595 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
596 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop,
597 overflow_type * = 0);
598 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop,
599 overflow_type * = 0);
601 template <typename T1, typename T2>
602 bool multiple_of_p (const T1 &, const T2 &, signop);
604 template <typename T1, typename T2>
605 bool multiple_of_p (const T1 &, const T2 &, signop,
606 WI_BINARY_RESULT (T1, T2) *);
608 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
609 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
610 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
611 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
612 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
613 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
615 #undef SHIFT_FUNCTION
616 #undef BINARY_PREDICATE
617 #undef BINARY_FUNCTION
618 #undef UNARY_PREDICATE
619 #undef UNARY_FUNCTION
621 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
622 bool only_sign_bit_p (const wide_int_ref &);
623 int clz (const wide_int_ref &);
624 int clrsb (const wide_int_ref &);
625 int ctz (const wide_int_ref &);
626 int exact_log2 (const wide_int_ref &);
627 int floor_log2 (const wide_int_ref &);
628 int ffs (const wide_int_ref &);
629 int popcount (const wide_int_ref &);
630 int parity (const wide_int_ref &);
632 template <typename T>
633 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
635 template <typename T>
636 unsigned int min_precision (const T &, signop);
638 static inline void accumulate_overflow (overflow_type &, overflow_type);
641 namespace wi
643 /* Contains the components of a decomposed integer for easy, direct
644 access. */
645 class storage_ref
647 public:
648 storage_ref () {}
649 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
651 const HOST_WIDE_INT *val;
652 unsigned int len;
653 unsigned int precision;
655 /* Provide enough trappings for this class to act as storage for
656 generic_wide_int. */
657 unsigned int get_len () const;
658 unsigned int get_precision () const;
659 const HOST_WIDE_INT *get_val () const;
663 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
664 unsigned int len_in,
665 unsigned int precision_in)
666 : val (val_in), len (len_in), precision (precision_in)
670 inline unsigned int
671 wi::storage_ref::get_len () const
673 return len;
676 inline unsigned int
677 wi::storage_ref::get_precision () const
679 return precision;
682 inline const HOST_WIDE_INT *
683 wi::storage_ref::get_val () const
685 return val;
688 /* This class defines an integer type using the storage provided by the
689 template argument. The storage class must provide the following
690 functions:
692 unsigned int get_precision () const
693 Return the number of bits in the integer.
695 HOST_WIDE_INT *get_val () const
696 Return a pointer to the array of blocks that encodes the integer.
698 unsigned int get_len () const
699 Return the number of blocks in get_val (). If this is smaller
700 than the number of blocks implied by get_precision (), the
701 remaining blocks are sign extensions of block get_len () - 1.
703 Although not required by generic_wide_int itself, writable storage
704 classes can also provide the following functions:
706 HOST_WIDE_INT *write_val ()
707 Get a modifiable version of get_val ()
709 unsigned int set_len (unsigned int len)
710 Set the value returned by get_len () to LEN. */
711 template <typename storage>
712 class GTY(()) generic_wide_int : public storage
714 public:
715 generic_wide_int ();
717 template <typename T>
718 generic_wide_int (const T &);
720 template <typename T>
721 generic_wide_int (const T &, unsigned int);
723 /* Conversions. */
724 HOST_WIDE_INT to_shwi (unsigned int) const;
725 HOST_WIDE_INT to_shwi () const;
726 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
727 unsigned HOST_WIDE_INT to_uhwi () const;
728 HOST_WIDE_INT to_short_addr () const;
730 /* Public accessors for the interior of a wide int. */
731 HOST_WIDE_INT sign_mask () const;
732 HOST_WIDE_INT elt (unsigned int) const;
733 unsigned HOST_WIDE_INT ulow () const;
734 unsigned HOST_WIDE_INT uhigh () const;
735 HOST_WIDE_INT slow () const;
736 HOST_WIDE_INT shigh () const;
738 template <typename T>
739 generic_wide_int &operator = (const T &);
741 #define ASSIGNMENT_OPERATOR(OP, F) \
742 template <typename T> \
743 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
745 /* Restrict these to cases where the shift operator is defined. */
746 #define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
747 template <typename T> \
748 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
750 #define INCDEC_OPERATOR(OP, DELTA) \
751 generic_wide_int &OP () { *this += DELTA; return *this; }
753 ASSIGNMENT_OPERATOR (operator &=, bit_and)
754 ASSIGNMENT_OPERATOR (operator |=, bit_or)
755 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
756 ASSIGNMENT_OPERATOR (operator +=, add)
757 ASSIGNMENT_OPERATOR (operator -=, sub)
758 ASSIGNMENT_OPERATOR (operator *=, mul)
759 ASSIGNMENT_OPERATOR (operator <<=, lshift)
760 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
761 INCDEC_OPERATOR (operator ++, 1)
762 INCDEC_OPERATOR (operator --, -1)
764 #undef SHIFT_ASSIGNMENT_OPERATOR
765 #undef ASSIGNMENT_OPERATOR
766 #undef INCDEC_OPERATOR
768 /* Debugging functions. */
769 void dump () const;
771 static const bool is_sign_extended
772 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
775 template <typename storage>
776 inline generic_wide_int <storage>::generic_wide_int () {}
778 template <typename storage>
779 template <typename T>
780 inline generic_wide_int <storage>::generic_wide_int (const T &x)
781 : storage (x)
785 template <typename storage>
786 template <typename T>
787 inline generic_wide_int <storage>::generic_wide_int (const T &x,
788 unsigned int precision)
789 : storage (x, precision)
793 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
794 If THIS does not fit in PRECISION, the information is lost. */
795 template <typename storage>
796 inline HOST_WIDE_INT
797 generic_wide_int <storage>::to_shwi (unsigned int precision) const
799 if (precision < HOST_BITS_PER_WIDE_INT)
800 return sext_hwi (this->get_val ()[0], precision);
801 else
802 return this->get_val ()[0];
805 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
806 template <typename storage>
807 inline HOST_WIDE_INT
808 generic_wide_int <storage>::to_shwi () const
810 if (is_sign_extended)
811 return this->get_val ()[0];
812 else
813 return to_shwi (this->get_precision ());
816 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
817 PRECISION. If THIS does not fit in PRECISION, the information
818 is lost. */
819 template <typename storage>
820 inline unsigned HOST_WIDE_INT
821 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
823 if (precision < HOST_BITS_PER_WIDE_INT)
824 return zext_hwi (this->get_val ()[0], precision);
825 else
826 return this->get_val ()[0];
829 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
830 template <typename storage>
831 inline unsigned HOST_WIDE_INT
832 generic_wide_int <storage>::to_uhwi () const
834 return to_uhwi (this->get_precision ());
837 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
838 represent addresses to using offset_int to represent addresses.
839 We use to_short_addr at the interface from new code to old,
840 unconverted code. */
841 template <typename storage>
842 inline HOST_WIDE_INT
843 generic_wide_int <storage>::to_short_addr () const
845 return this->get_val ()[0];
848 /* Return the implicit value of blocks above get_len (). */
849 template <typename storage>
850 inline HOST_WIDE_INT
851 generic_wide_int <storage>::sign_mask () const
853 unsigned int len = this->get_len ();
854 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
855 if (!is_sign_extended)
857 unsigned int precision = this->get_precision ();
858 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
859 if (excess > 0)
860 high <<= excess;
862 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
865 /* Return the signed value of the least-significant explicitly-encoded
866 block. */
867 template <typename storage>
868 inline HOST_WIDE_INT
869 generic_wide_int <storage>::slow () const
871 return this->get_val ()[0];
874 /* Return the signed value of the most-significant explicitly-encoded
875 block. */
876 template <typename storage>
877 inline HOST_WIDE_INT
878 generic_wide_int <storage>::shigh () const
880 return this->get_val ()[this->get_len () - 1];
883 /* Return the unsigned value of the least-significant
884 explicitly-encoded block. */
885 template <typename storage>
886 inline unsigned HOST_WIDE_INT
887 generic_wide_int <storage>::ulow () const
889 return this->get_val ()[0];
892 /* Return the unsigned value of the most-significant
893 explicitly-encoded block. */
894 template <typename storage>
895 inline unsigned HOST_WIDE_INT
896 generic_wide_int <storage>::uhigh () const
898 return this->get_val ()[this->get_len () - 1];
901 /* Return block I, which might be implicitly or explicit encoded. */
902 template <typename storage>
903 inline HOST_WIDE_INT
904 generic_wide_int <storage>::elt (unsigned int i) const
906 if (i >= this->get_len ())
907 return sign_mask ();
908 else
909 return this->get_val ()[i];
912 template <typename storage>
913 template <typename T>
914 inline generic_wide_int <storage> &
915 generic_wide_int <storage>::operator = (const T &x)
917 storage::operator = (x);
918 return *this;
921 /* Dump the contents of the integer to stderr, for debugging. */
922 template <typename storage>
923 void
924 generic_wide_int <storage>::dump () const
926 unsigned int len = this->get_len ();
927 const HOST_WIDE_INT *val = this->get_val ();
928 unsigned int precision = this->get_precision ();
929 fprintf (stderr, "[");
930 if (len * HOST_BITS_PER_WIDE_INT < precision)
931 fprintf (stderr, "...,");
932 for (unsigned int i = 0; i < len - 1; ++i)
933 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
934 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
935 val[0], precision);
938 namespace wi
940 template <typename storage>
941 struct int_traits < generic_wide_int <storage> >
942 : public wi::int_traits <storage>
944 static unsigned int get_precision (const generic_wide_int <storage> &);
945 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
946 const generic_wide_int <storage> &);
950 template <typename storage>
951 inline unsigned int
952 wi::int_traits < generic_wide_int <storage> >::
953 get_precision (const generic_wide_int <storage> &x)
955 return x.get_precision ();
958 template <typename storage>
959 inline wi::storage_ref
960 wi::int_traits < generic_wide_int <storage> >::
961 decompose (HOST_WIDE_INT *, unsigned int precision,
962 const generic_wide_int <storage> &x)
964 gcc_checking_assert (precision == x.get_precision ());
965 return wi::storage_ref (x.get_val (), x.get_len (), precision);
968 /* Provide the storage for a wide_int_ref. This acts like a read-only
969 wide_int, with the optimization that VAL is normally a pointer to
970 another integer's storage, so that no array copy is needed. */
971 template <bool SE, bool HDP>
972 class wide_int_ref_storage : public wi::storage_ref
974 private:
975 /* Scratch space that can be used when decomposing the original integer.
976 It must live as long as this object. */
977 HOST_WIDE_INT scratch[2];
979 public:
980 wide_int_ref_storage () {}
982 wide_int_ref_storage (const wi::storage_ref &);
984 template <typename T>
985 wide_int_ref_storage (const T &);
987 template <typename T>
988 wide_int_ref_storage (const T &, unsigned int);
991 /* Create a reference from an existing reference. */
992 template <bool SE, bool HDP>
993 inline wide_int_ref_storage <SE, HDP>::
994 wide_int_ref_storage (const wi::storage_ref &x)
995 : storage_ref (x)
998 /* Create a reference to integer X in its natural precision. Note
999 that the natural precision is host-dependent for primitive
1000 types. */
1001 template <bool SE, bool HDP>
1002 template <typename T>
1003 inline wide_int_ref_storage <SE, HDP>::wide_int_ref_storage (const T &x)
1004 : storage_ref (wi::int_traits <T>::decompose (scratch,
1005 wi::get_precision (x), x))
1009 /* Create a reference to integer X in precision PRECISION. */
1010 template <bool SE, bool HDP>
1011 template <typename T>
1012 inline wide_int_ref_storage <SE, HDP>::
1013 wide_int_ref_storage (const T &x, unsigned int precision)
1014 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
1018 namespace wi
1020 template <bool SE, bool HDP>
1021 struct int_traits <wide_int_ref_storage <SE, HDP> >
1023 static const enum precision_type precision_type = VAR_PRECISION;
1024 static const bool host_dependent_precision = HDP;
1025 static const bool is_sign_extended = SE;
1029 namespace wi
1031 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1032 unsigned int, unsigned int, unsigned int,
1033 signop sgn);
1034 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1035 unsigned int, unsigned int, bool = true);
1038 /* The storage used by wide_int. */
1039 class GTY(()) wide_int_storage
1041 private:
1042 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
1043 unsigned int len;
1044 unsigned int precision;
1046 public:
1047 wide_int_storage ();
1048 template <typename T>
1049 wide_int_storage (const T &);
1051 /* The standard generic_wide_int storage methods. */
1052 unsigned int get_precision () const;
1053 const HOST_WIDE_INT *get_val () const;
1054 unsigned int get_len () const;
1055 HOST_WIDE_INT *write_val ();
1056 void set_len (unsigned int, bool = false);
1058 template <typename T>
1059 wide_int_storage &operator = (const T &);
1061 static wide_int from (const wide_int_ref &, unsigned int, signop);
1062 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1063 unsigned int, bool = true);
1064 static wide_int create (unsigned int);
1066 /* FIXME: target-dependent, so should disappear. */
1067 wide_int bswap () const;
1070 namespace wi
1072 template <>
1073 struct int_traits <wide_int_storage>
1075 static const enum precision_type precision_type = VAR_PRECISION;
1076 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1077 static const bool host_dependent_precision = false;
1078 static const bool is_sign_extended = true;
1079 template <typename T1, typename T2>
1080 static wide_int get_binary_result (const T1 &, const T2 &);
1084 inline wide_int_storage::wide_int_storage () {}
1086 /* Initialize the storage from integer X, in its natural precision.
1087 Note that we do not allow integers with host-dependent precision
1088 to become wide_ints; wide_ints must always be logically independent
1089 of the host. */
1090 template <typename T>
1091 inline wide_int_storage::wide_int_storage (const T &x)
1093 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1094 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1095 WIDE_INT_REF_FOR (T) xi (x);
1096 precision = xi.precision;
1097 wi::copy (*this, xi);
1100 template <typename T>
1101 inline wide_int_storage&
1102 wide_int_storage::operator = (const T &x)
1104 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1105 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1106 WIDE_INT_REF_FOR (T) xi (x);
1107 precision = xi.precision;
1108 wi::copy (*this, xi);
1109 return *this;
1112 inline unsigned int
1113 wide_int_storage::get_precision () const
1115 return precision;
1118 inline const HOST_WIDE_INT *
1119 wide_int_storage::get_val () const
1121 return val;
1124 inline unsigned int
1125 wide_int_storage::get_len () const
1127 return len;
1130 inline HOST_WIDE_INT *
1131 wide_int_storage::write_val ()
1133 return val;
1136 inline void
1137 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1139 len = l;
1140 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1141 val[len - 1] = sext_hwi (val[len - 1],
1142 precision % HOST_BITS_PER_WIDE_INT);
1145 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1146 number. */
1147 inline wide_int
1148 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1149 signop sgn)
1151 wide_int result = wide_int::create (precision);
1152 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1153 x.precision, precision, sgn));
1154 return result;
1157 /* Create a wide_int from the explicit block encoding given by VAL and
1158 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1159 true if the encoding may have redundant trailing blocks. */
1160 inline wide_int
1161 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1162 unsigned int precision, bool need_canon_p)
1164 wide_int result = wide_int::create (precision);
1165 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1166 need_canon_p));
1167 return result;
1170 /* Return an uninitialized wide_int with precision PRECISION. */
1171 inline wide_int
1172 wide_int_storage::create (unsigned int precision)
1174 wide_int x;
1175 x.precision = precision;
1176 return x;
1179 template <typename T1, typename T2>
1180 inline wide_int
1181 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1183 /* This shouldn't be used for two flexible-precision inputs. */
1184 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1185 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1186 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1187 return wide_int::create (wi::get_precision (y));
1188 else
1189 return wide_int::create (wi::get_precision (x));
1192 /* The storage used by FIXED_WIDE_INT (N). */
1193 template <int N>
1194 class GTY(()) fixed_wide_int_storage
1196 private:
1197 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1198 unsigned int len;
1200 public:
1201 fixed_wide_int_storage ();
1202 template <typename T>
1203 fixed_wide_int_storage (const T &);
1205 /* The standard generic_wide_int storage methods. */
1206 unsigned int get_precision () const;
1207 const HOST_WIDE_INT *get_val () const;
1208 unsigned int get_len () const;
1209 HOST_WIDE_INT *write_val ();
1210 void set_len (unsigned int, bool = false);
1212 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1213 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1214 bool = true);
1217 namespace wi
1219 template <int N>
1220 struct int_traits < fixed_wide_int_storage <N> >
1222 static const enum precision_type precision_type = CONST_PRECISION;
1223 static const bool host_dependent_precision = false;
1224 static const bool is_sign_extended = true;
1225 static const unsigned int precision = N;
1226 template <typename T1, typename T2>
1227 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1231 template <int N>
1232 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1234 /* Initialize the storage from integer X, in precision N. */
1235 template <int N>
1236 template <typename T>
1237 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1239 /* Check for type compatibility. We don't want to initialize a
1240 fixed-width integer from something like a wide_int. */
1241 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1242 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1245 template <int N>
1246 inline unsigned int
1247 fixed_wide_int_storage <N>::get_precision () const
1249 return N;
1252 template <int N>
1253 inline const HOST_WIDE_INT *
1254 fixed_wide_int_storage <N>::get_val () const
1256 return val;
1259 template <int N>
1260 inline unsigned int
1261 fixed_wide_int_storage <N>::get_len () const
1263 return len;
1266 template <int N>
1267 inline HOST_WIDE_INT *
1268 fixed_wide_int_storage <N>::write_val ()
1270 return val;
1273 template <int N>
1274 inline void
1275 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1277 len = l;
1278 /* There are no excess bits in val[len - 1]. */
1279 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1282 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1283 template <int N>
1284 inline FIXED_WIDE_INT (N)
1285 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1287 FIXED_WIDE_INT (N) result;
1288 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1289 x.precision, N, sgn));
1290 return result;
1293 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1294 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1295 trailing blocks. */
1296 template <int N>
1297 inline FIXED_WIDE_INT (N)
1298 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1299 unsigned int len,
1300 bool need_canon_p)
1302 FIXED_WIDE_INT (N) result;
1303 result.set_len (wi::from_array (result.write_val (), val, len,
1304 N, need_canon_p));
1305 return result;
1308 template <int N>
1309 template <typename T1, typename T2>
1310 inline FIXED_WIDE_INT (N)
1311 wi::int_traits < fixed_wide_int_storage <N> >::
1312 get_binary_result (const T1 &, const T2 &)
1314 return FIXED_WIDE_INT (N) ();
1317 /* A reference to one element of a trailing_wide_ints structure. */
1318 class trailing_wide_int_storage
1320 private:
1321 /* The precision of the integer, which is a fixed property of the
1322 parent trailing_wide_ints. */
1323 unsigned int m_precision;
1325 /* A pointer to the length field. */
1326 unsigned char *m_len;
1328 /* A pointer to the HWI array. There are enough elements to hold all
1329 values of precision M_PRECISION. */
1330 HOST_WIDE_INT *m_val;
1332 public:
1333 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1335 /* The standard generic_wide_int storage methods. */
1336 unsigned int get_len () const;
1337 unsigned int get_precision () const;
1338 const HOST_WIDE_INT *get_val () const;
1339 HOST_WIDE_INT *write_val ();
1340 void set_len (unsigned int, bool = false);
1342 template <typename T>
1343 trailing_wide_int_storage &operator = (const T &);
1346 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1348 /* trailing_wide_int behaves like a wide_int. */
1349 namespace wi
1351 template <>
1352 struct int_traits <trailing_wide_int_storage>
1353 : public int_traits <wide_int_storage> {};
1356 /* An array of N wide_int-like objects that can be put at the end of
1357 a variable-sized structure. Use extra_size to calculate how many
1358 bytes beyond the sizeof need to be allocated. Use set_precision
1359 to initialize the structure. */
1360 template <int N>
1361 struct GTY((user)) trailing_wide_ints
1363 private:
1364 /* The shared precision of each number. */
1365 unsigned short m_precision;
1367 /* The shared maximum length of each number. */
1368 unsigned char m_max_len;
1370 /* The current length of each number. */
1371 unsigned char m_len[N];
1373 /* The variable-length part of the structure, which always contains
1374 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1375 HOST_WIDE_INT m_val[1];
1377 public:
1378 typedef WIDE_INT_REF_FOR (trailing_wide_int_storage) const_reference;
1380 void set_precision (unsigned int);
1381 unsigned int get_precision () const { return m_precision; }
1382 trailing_wide_int operator [] (unsigned int);
1383 const_reference operator [] (unsigned int) const;
1384 static size_t extra_size (unsigned int);
1385 size_t extra_size () const { return extra_size (m_precision); }
1388 inline trailing_wide_int_storage::
1389 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1390 HOST_WIDE_INT *val)
1391 : m_precision (precision), m_len (len), m_val (val)
1395 inline unsigned int
1396 trailing_wide_int_storage::get_len () const
1398 return *m_len;
1401 inline unsigned int
1402 trailing_wide_int_storage::get_precision () const
1404 return m_precision;
1407 inline const HOST_WIDE_INT *
1408 trailing_wide_int_storage::get_val () const
1410 return m_val;
1413 inline HOST_WIDE_INT *
1414 trailing_wide_int_storage::write_val ()
1416 return m_val;
1419 inline void
1420 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1422 *m_len = len;
1423 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1424 m_val[len - 1] = sext_hwi (m_val[len - 1],
1425 m_precision % HOST_BITS_PER_WIDE_INT);
1428 template <typename T>
1429 inline trailing_wide_int_storage &
1430 trailing_wide_int_storage::operator = (const T &x)
1432 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1433 wi::copy (*this, xi);
1434 return *this;
1437 /* Initialize the structure and record that all elements have precision
1438 PRECISION. */
1439 template <int N>
1440 inline void
1441 trailing_wide_ints <N>::set_precision (unsigned int precision)
1443 m_precision = precision;
1444 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1445 / HOST_BITS_PER_WIDE_INT);
1448 /* Return a reference to element INDEX. */
1449 template <int N>
1450 inline trailing_wide_int
1451 trailing_wide_ints <N>::operator [] (unsigned int index)
1453 return trailing_wide_int_storage (m_precision, &m_len[index],
1454 &m_val[index * m_max_len]);
1457 template <int N>
1458 inline typename trailing_wide_ints <N>::const_reference
1459 trailing_wide_ints <N>::operator [] (unsigned int index) const
1461 return wi::storage_ref (&m_val[index * m_max_len],
1462 m_len[index], m_precision);
1465 /* Return how many extra bytes need to be added to the end of the structure
1466 in order to handle N wide_ints of precision PRECISION. */
1467 template <int N>
1468 inline size_t
1469 trailing_wide_ints <N>::extra_size (unsigned int precision)
1471 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1472 / HOST_BITS_PER_WIDE_INT);
1473 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1476 /* This macro is used in structures that end with a trailing_wide_ints field
1477 called FIELD. It declares get_NAME() and set_NAME() methods to access
1478 element I of FIELD. */
1479 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1480 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1481 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1483 namespace wi
1485 /* Implementation of int_traits for primitive integer types like "int". */
1486 template <typename T, bool signed_p>
1487 struct primitive_int_traits
1489 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1490 static const bool host_dependent_precision = true;
1491 static const bool is_sign_extended = true;
1492 static unsigned int get_precision (T);
1493 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1497 template <typename T, bool signed_p>
1498 inline unsigned int
1499 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1501 return sizeof (T) * CHAR_BIT;
1504 template <typename T, bool signed_p>
1505 inline wi::storage_ref
1506 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1507 unsigned int precision, T x)
1509 scratch[0] = x;
1510 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1511 return wi::storage_ref (scratch, 1, precision);
1512 scratch[1] = 0;
1513 return wi::storage_ref (scratch, 2, precision);
1516 /* Allow primitive C types to be used in wi:: routines. */
1517 namespace wi
1519 template <>
1520 struct int_traits <unsigned char>
1521 : public primitive_int_traits <unsigned char, false> {};
1523 template <>
1524 struct int_traits <unsigned short>
1525 : public primitive_int_traits <unsigned short, false> {};
1527 template <>
1528 struct int_traits <int>
1529 : public primitive_int_traits <int, true> {};
1531 template <>
1532 struct int_traits <unsigned int>
1533 : public primitive_int_traits <unsigned int, false> {};
1535 template <>
1536 struct int_traits <long>
1537 : public primitive_int_traits <long, true> {};
1539 template <>
1540 struct int_traits <unsigned long>
1541 : public primitive_int_traits <unsigned long, false> {};
1543 #if defined HAVE_LONG_LONG
1544 template <>
1545 struct int_traits <long long>
1546 : public primitive_int_traits <long long, true> {};
1548 template <>
1549 struct int_traits <unsigned long long>
1550 : public primitive_int_traits <unsigned long long, false> {};
1551 #endif
1554 namespace wi
1556 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1557 and precision PRECISION. */
1558 class hwi_with_prec
1560 public:
1561 hwi_with_prec () {}
1562 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1563 HOST_WIDE_INT val;
1564 unsigned int precision;
1565 signop sgn;
1568 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1569 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1571 hwi_with_prec minus_one (unsigned int);
1572 hwi_with_prec zero (unsigned int);
1573 hwi_with_prec one (unsigned int);
1574 hwi_with_prec two (unsigned int);
1577 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1578 signop s)
1579 : precision (p), sgn (s)
1581 if (precision < HOST_BITS_PER_WIDE_INT)
1582 val = sext_hwi (v, precision);
1583 else
1584 val = v;
1587 /* Return a signed integer that has value VAL and precision PRECISION. */
1588 inline wi::hwi_with_prec
1589 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1591 return hwi_with_prec (val, precision, SIGNED);
1594 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1595 inline wi::hwi_with_prec
1596 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1598 return hwi_with_prec (val, precision, UNSIGNED);
1601 /* Return a wide int of -1 with precision PRECISION. */
1602 inline wi::hwi_with_prec
1603 wi::minus_one (unsigned int precision)
1605 return wi::shwi (-1, precision);
1608 /* Return a wide int of 0 with precision PRECISION. */
1609 inline wi::hwi_with_prec
1610 wi::zero (unsigned int precision)
1612 return wi::shwi (0, precision);
1615 /* Return a wide int of 1 with precision PRECISION. */
1616 inline wi::hwi_with_prec
1617 wi::one (unsigned int precision)
1619 return wi::shwi (1, precision);
1622 /* Return a wide int of 2 with precision PRECISION. */
1623 inline wi::hwi_with_prec
1624 wi::two (unsigned int precision)
1626 return wi::shwi (2, precision);
1629 namespace wi
1631 /* ints_for<T>::zero (X) returns a zero that, when asssigned to a T,
1632 gives that T the same precision as X. */
1633 template<typename T, precision_type = int_traits<T>::precision_type>
1634 struct ints_for
1636 static int zero (const T &) { return 0; }
1639 template<typename T>
1640 struct ints_for<T, VAR_PRECISION>
1642 static hwi_with_prec zero (const T &);
1646 template<typename T>
1647 inline wi::hwi_with_prec
1648 wi::ints_for<T, wi::VAR_PRECISION>::zero (const T &x)
1650 return wi::zero (wi::get_precision (x));
1653 namespace wi
1655 template <>
1656 struct int_traits <wi::hwi_with_prec>
1658 static const enum precision_type precision_type = VAR_PRECISION;
1659 /* hwi_with_prec has an explicitly-given precision, rather than the
1660 precision of HOST_WIDE_INT. */
1661 static const bool host_dependent_precision = false;
1662 static const bool is_sign_extended = true;
1663 static unsigned int get_precision (const wi::hwi_with_prec &);
1664 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1665 const wi::hwi_with_prec &);
1669 inline unsigned int
1670 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1672 return x.precision;
1675 inline wi::storage_ref
1676 wi::int_traits <wi::hwi_with_prec>::
1677 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1678 const wi::hwi_with_prec &x)
1680 gcc_checking_assert (precision == x.precision);
1681 scratch[0] = x.val;
1682 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1683 return wi::storage_ref (scratch, 1, precision);
1684 scratch[1] = 0;
1685 return wi::storage_ref (scratch, 2, precision);
1688 /* Private functions for handling large cases out of line. They take
1689 individual length and array parameters because that is cheaper for
1690 the inline caller than constructing an object on the stack and
1691 passing a reference to it. (Although many callers use wide_int_refs,
1692 we generally want those to be removed by SRA.) */
1693 namespace wi
1695 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1696 const HOST_WIDE_INT *, unsigned int, unsigned int);
1697 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1698 const HOST_WIDE_INT *, unsigned int);
1699 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1700 const HOST_WIDE_INT *, unsigned int);
1701 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1702 const HOST_WIDE_INT *, unsigned int);
1703 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1704 const HOST_WIDE_INT *, unsigned int);
1705 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1706 unsigned int,
1707 unsigned int, unsigned int);
1708 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1709 unsigned int,
1710 unsigned int, unsigned int);
1711 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1712 unsigned int, unsigned int, unsigned int);
1713 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1714 unsigned int, unsigned int, unsigned int);
1715 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1716 unsigned int, unsigned int, unsigned int,
1717 unsigned int);
1718 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1719 unsigned int, unsigned int, unsigned int,
1720 unsigned int);
1721 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1722 const HOST_WIDE_INT *, unsigned int, unsigned int);
1723 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1724 unsigned int, const HOST_WIDE_INT *,
1725 unsigned int, unsigned int);
1726 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1727 const HOST_WIDE_INT *, unsigned int, unsigned int);
1728 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1729 unsigned int, const HOST_WIDE_INT *,
1730 unsigned int, unsigned int);
1731 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1732 const HOST_WIDE_INT *, unsigned int, unsigned int);
1733 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1734 const HOST_WIDE_INT *, unsigned int, unsigned int,
1735 signop, overflow_type *);
1736 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1737 const HOST_WIDE_INT *, unsigned int, unsigned int,
1738 signop, overflow_type *);
1739 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1740 unsigned int, const HOST_WIDE_INT *,
1741 unsigned int, unsigned int, signop,
1742 overflow_type *, bool);
1743 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1744 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1745 unsigned int, unsigned int,
1746 const HOST_WIDE_INT *,
1747 unsigned int, unsigned int,
1748 signop, overflow_type *);
1751 /* Return the number of bits that integer X can hold. */
1752 template <typename T>
1753 inline unsigned int
1754 wi::get_precision (const T &x)
1756 return wi::int_traits <T>::get_precision (x);
1759 /* Return the number of bits that the result of a binary operation can
1760 hold when the input operands are X and Y. */
1761 template <typename T1, typename T2>
1762 inline unsigned int
1763 wi::get_binary_precision (const T1 &x, const T2 &y)
1765 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1766 get_binary_result (x, y));
1769 /* Copy the contents of Y to X, but keeping X's current precision. */
1770 template <typename T1, typename T2>
1771 inline void
1772 wi::copy (T1 &x, const T2 &y)
1774 HOST_WIDE_INT *xval = x.write_val ();
1775 const HOST_WIDE_INT *yval = y.get_val ();
1776 unsigned int len = y.get_len ();
1777 unsigned int i = 0;
1779 xval[i] = yval[i];
1780 while (++i < len);
1781 x.set_len (len, y.is_sign_extended);
1784 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1785 template <typename T>
1786 inline bool
1787 wi::fits_shwi_p (const T &x)
1789 WIDE_INT_REF_FOR (T) xi (x);
1790 return xi.len == 1;
1793 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1794 precision. */
1795 template <typename T>
1796 inline bool
1797 wi::fits_uhwi_p (const T &x)
1799 WIDE_INT_REF_FOR (T) xi (x);
1800 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1801 return true;
1802 if (xi.len == 1)
1803 return xi.slow () >= 0;
1804 return xi.len == 2 && xi.uhigh () == 0;
1807 /* Return true if X is negative based on the interpretation of SGN.
1808 For UNSIGNED, this is always false. */
1809 template <typename T>
1810 inline bool
1811 wi::neg_p (const T &x, signop sgn)
1813 WIDE_INT_REF_FOR (T) xi (x);
1814 if (sgn == UNSIGNED)
1815 return false;
1816 return xi.sign_mask () < 0;
1819 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1820 template <typename T>
1821 inline HOST_WIDE_INT
1822 wi::sign_mask (const T &x)
1824 WIDE_INT_REF_FOR (T) xi (x);
1825 return xi.sign_mask ();
1828 /* Return true if X == Y. X and Y must be binary-compatible. */
1829 template <typename T1, typename T2>
1830 inline bool
1831 wi::eq_p (const T1 &x, const T2 &y)
1833 unsigned int precision = get_binary_precision (x, y);
1834 WIDE_INT_REF_FOR (T1) xi (x, precision);
1835 WIDE_INT_REF_FOR (T2) yi (y, precision);
1836 if (xi.is_sign_extended && yi.is_sign_extended)
1838 /* This case reduces to array equality. */
1839 if (xi.len != yi.len)
1840 return false;
1841 unsigned int i = 0;
1843 if (xi.val[i] != yi.val[i])
1844 return false;
1845 while (++i != xi.len);
1846 return true;
1848 if (__builtin_expect (yi.len == 1, true))
1850 /* XI is only equal to YI if it too has a single HWI. */
1851 if (xi.len != 1)
1852 return false;
1853 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1854 with 0 are simple. */
1855 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1856 return xi.val[0] == 0;
1857 /* Otherwise flush out any excess bits first. */
1858 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1859 int excess = HOST_BITS_PER_WIDE_INT - precision;
1860 if (excess > 0)
1861 diff <<= excess;
1862 return diff == 0;
1864 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1867 /* Return true if X != Y. X and Y must be binary-compatible. */
1868 template <typename T1, typename T2>
1869 inline bool
1870 wi::ne_p (const T1 &x, const T2 &y)
1872 return !eq_p (x, y);
1875 /* Return true if X < Y when both are treated as signed values. */
1876 template <typename T1, typename T2>
1877 inline bool
1878 wi::lts_p (const T1 &x, const T2 &y)
1880 unsigned int precision = get_binary_precision (x, y);
1881 WIDE_INT_REF_FOR (T1) xi (x, precision);
1882 WIDE_INT_REF_FOR (T2) yi (y, precision);
1883 /* We optimize x < y, where y is 64 or fewer bits. */
1884 if (wi::fits_shwi_p (yi))
1886 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1887 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1888 return neg_p (xi);
1889 /* If x fits directly into a shwi, we can compare directly. */
1890 if (wi::fits_shwi_p (xi))
1891 return xi.to_shwi () < yi.to_shwi ();
1892 /* If x doesn't fit and is negative, then it must be more
1893 negative than any value in y, and hence smaller than y. */
1894 if (neg_p (xi))
1895 return true;
1896 /* If x is positive, then it must be larger than any value in y,
1897 and hence greater than y. */
1898 return false;
1900 /* Optimize the opposite case, if it can be detected at compile time. */
1901 if (STATIC_CONSTANT_P (xi.len == 1))
1902 /* If YI is negative it is lower than the least HWI.
1903 If YI is positive it is greater than the greatest HWI. */
1904 return !neg_p (yi);
1905 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1908 /* Return true if X < Y when both are treated as unsigned values. */
1909 template <typename T1, typename T2>
1910 inline bool
1911 wi::ltu_p (const T1 &x, const T2 &y)
1913 unsigned int precision = get_binary_precision (x, y);
1914 WIDE_INT_REF_FOR (T1) xi (x, precision);
1915 WIDE_INT_REF_FOR (T2) yi (y, precision);
1916 /* Optimize comparisons with constants. */
1917 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1918 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1919 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1920 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1921 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1922 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1923 values does not change the result. */
1924 if (__builtin_expect (xi.len + yi.len == 2, true))
1926 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1927 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1928 return xl < yl;
1930 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1933 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1934 template <typename T1, typename T2>
1935 inline bool
1936 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1938 if (sgn == SIGNED)
1939 return lts_p (x, y);
1940 else
1941 return ltu_p (x, y);
1944 /* Return true if X <= Y when both are treated as signed values. */
1945 template <typename T1, typename T2>
1946 inline bool
1947 wi::les_p (const T1 &x, const T2 &y)
1949 return !lts_p (y, x);
1952 /* Return true if X <= Y when both are treated as unsigned values. */
1953 template <typename T1, typename T2>
1954 inline bool
1955 wi::leu_p (const T1 &x, const T2 &y)
1957 return !ltu_p (y, x);
1960 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1961 template <typename T1, typename T2>
1962 inline bool
1963 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1965 if (sgn == SIGNED)
1966 return les_p (x, y);
1967 else
1968 return leu_p (x, y);
1971 /* Return true if X > Y when both are treated as signed values. */
1972 template <typename T1, typename T2>
1973 inline bool
1974 wi::gts_p (const T1 &x, const T2 &y)
1976 return lts_p (y, x);
1979 /* Return true if X > Y when both are treated as unsigned values. */
1980 template <typename T1, typename T2>
1981 inline bool
1982 wi::gtu_p (const T1 &x, const T2 &y)
1984 return ltu_p (y, x);
1987 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1988 template <typename T1, typename T2>
1989 inline bool
1990 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1992 if (sgn == SIGNED)
1993 return gts_p (x, y);
1994 else
1995 return gtu_p (x, y);
1998 /* Return true if X >= Y when both are treated as signed values. */
1999 template <typename T1, typename T2>
2000 inline bool
2001 wi::ges_p (const T1 &x, const T2 &y)
2003 return !lts_p (x, y);
2006 /* Return true if X >= Y when both are treated as unsigned values. */
2007 template <typename T1, typename T2>
2008 inline bool
2009 wi::geu_p (const T1 &x, const T2 &y)
2011 return !ltu_p (x, y);
2014 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
2015 template <typename T1, typename T2>
2016 inline bool
2017 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
2019 if (sgn == SIGNED)
2020 return ges_p (x, y);
2021 else
2022 return geu_p (x, y);
2025 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2026 as signed values. */
2027 template <typename T1, typename T2>
2028 inline int
2029 wi::cmps (const T1 &x, const T2 &y)
2031 unsigned int precision = get_binary_precision (x, y);
2032 WIDE_INT_REF_FOR (T1) xi (x, precision);
2033 WIDE_INT_REF_FOR (T2) yi (y, precision);
2034 if (wi::fits_shwi_p (yi))
2036 /* Special case for comparisons with 0. */
2037 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2038 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
2039 /* If x fits into a signed HWI, we can compare directly. */
2040 if (wi::fits_shwi_p (xi))
2042 HOST_WIDE_INT xl = xi.to_shwi ();
2043 HOST_WIDE_INT yl = yi.to_shwi ();
2044 return xl < yl ? -1 : xl > yl;
2046 /* If x doesn't fit and is negative, then it must be more
2047 negative than any signed HWI, and hence smaller than y. */
2048 if (neg_p (xi))
2049 return -1;
2050 /* If x is positive, then it must be larger than any signed HWI,
2051 and hence greater than y. */
2052 return 1;
2054 /* Optimize the opposite case, if it can be detected at compile time. */
2055 if (STATIC_CONSTANT_P (xi.len == 1))
2056 /* If YI is negative it is lower than the least HWI.
2057 If YI is positive it is greater than the greatest HWI. */
2058 return neg_p (yi) ? 1 : -1;
2059 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
2062 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2063 as unsigned values. */
2064 template <typename T1, typename T2>
2065 inline int
2066 wi::cmpu (const T1 &x, const T2 &y)
2068 unsigned int precision = get_binary_precision (x, y);
2069 WIDE_INT_REF_FOR (T1) xi (x, precision);
2070 WIDE_INT_REF_FOR (T2) yi (y, precision);
2071 /* Optimize comparisons with constants. */
2072 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2074 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2075 if (xi.len != 1)
2076 return 1;
2077 /* Otherwise compare directly. */
2078 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2079 unsigned HOST_WIDE_INT yl = yi.val[0];
2080 return xl < yl ? -1 : xl > yl;
2082 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2084 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2085 if (yi.len != 1)
2086 return -1;
2087 /* Otherwise compare directly. */
2088 unsigned HOST_WIDE_INT xl = xi.val[0];
2089 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2090 return xl < yl ? -1 : xl > yl;
2092 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2093 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2094 values does not change the result. */
2095 if (__builtin_expect (xi.len + yi.len == 2, true))
2097 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2098 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2099 return xl < yl ? -1 : xl > yl;
2101 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2104 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2105 X and Y indicated by SGN. */
2106 template <typename T1, typename T2>
2107 inline int
2108 wi::cmp (const T1 &x, const T2 &y, signop sgn)
2110 if (sgn == SIGNED)
2111 return cmps (x, y);
2112 else
2113 return cmpu (x, y);
2116 /* Return ~x. */
2117 template <typename T>
2118 inline WI_UNARY_RESULT (T)
2119 wi::bit_not (const T &x)
2121 WI_UNARY_RESULT_VAR (result, val, T, x);
2122 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2123 for (unsigned int i = 0; i < xi.len; ++i)
2124 val[i] = ~xi.val[i];
2125 result.set_len (xi.len);
2126 return result;
2129 /* Return -x. */
2130 template <typename T>
2131 inline WI_UNARY_RESULT (T)
2132 wi::neg (const T &x)
2134 return sub (0, x);
2137 /* Return -x. Indicate in *OVERFLOW if performing the negation would
2138 cause an overflow. */
2139 template <typename T>
2140 inline WI_UNARY_RESULT (T)
2141 wi::neg (const T &x, overflow_type *overflow)
2143 *overflow = only_sign_bit_p (x) ? OVF_OVERFLOW : OVF_NONE;
2144 return sub (0, x);
2147 /* Return the absolute value of x. */
2148 template <typename T>
2149 inline WI_UNARY_RESULT (T)
2150 wi::abs (const T &x)
2152 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2155 /* Return the result of sign-extending the low OFFSET bits of X. */
2156 template <typename T>
2157 inline WI_UNARY_RESULT (T)
2158 wi::sext (const T &x, unsigned int offset)
2160 WI_UNARY_RESULT_VAR (result, val, T, x);
2161 unsigned int precision = get_precision (result);
2162 WIDE_INT_REF_FOR (T) xi (x, precision);
2164 if (offset <= HOST_BITS_PER_WIDE_INT)
2166 val[0] = sext_hwi (xi.ulow (), offset);
2167 result.set_len (1, true);
2169 else
2170 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2171 return result;
2174 /* Return the result of zero-extending the low OFFSET bits of X. */
2175 template <typename T>
2176 inline WI_UNARY_RESULT (T)
2177 wi::zext (const T &x, unsigned int offset)
2179 WI_UNARY_RESULT_VAR (result, val, T, x);
2180 unsigned int precision = get_precision (result);
2181 WIDE_INT_REF_FOR (T) xi (x, precision);
2183 /* This is not just an optimization, it is actually required to
2184 maintain canonization. */
2185 if (offset >= precision)
2187 wi::copy (result, xi);
2188 return result;
2191 /* In these cases we know that at least the top bit will be clear,
2192 so no sign extension is necessary. */
2193 if (offset < HOST_BITS_PER_WIDE_INT)
2195 val[0] = zext_hwi (xi.ulow (), offset);
2196 result.set_len (1, true);
2198 else
2199 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2200 return result;
2203 /* Return the result of extending the low OFFSET bits of X according to
2204 signedness SGN. */
2205 template <typename T>
2206 inline WI_UNARY_RESULT (T)
2207 wi::ext (const T &x, unsigned int offset, signop sgn)
2209 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2212 /* Return an integer that represents X | (1 << bit). */
2213 template <typename T>
2214 inline WI_UNARY_RESULT (T)
2215 wi::set_bit (const T &x, unsigned int bit)
2217 WI_UNARY_RESULT_VAR (result, val, T, x);
2218 unsigned int precision = get_precision (result);
2219 WIDE_INT_REF_FOR (T) xi (x, precision);
2220 if (precision <= HOST_BITS_PER_WIDE_INT)
2222 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2223 result.set_len (1);
2225 else
2226 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2227 return result;
2230 /* Return the mininum of X and Y, treating them both as having
2231 signedness SGN. */
2232 template <typename T1, typename T2>
2233 inline WI_BINARY_RESULT (T1, T2)
2234 wi::min (const T1 &x, const T2 &y, signop sgn)
2236 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2237 unsigned int precision = get_precision (result);
2238 if (wi::le_p (x, y, sgn))
2239 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2240 else
2241 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2242 return result;
2245 /* Return the minimum of X and Y, treating both as signed values. */
2246 template <typename T1, typename T2>
2247 inline WI_BINARY_RESULT (T1, T2)
2248 wi::smin (const T1 &x, const T2 &y)
2250 return wi::min (x, y, SIGNED);
2253 /* Return the minimum of X and Y, treating both as unsigned values. */
2254 template <typename T1, typename T2>
2255 inline WI_BINARY_RESULT (T1, T2)
2256 wi::umin (const T1 &x, const T2 &y)
2258 return wi::min (x, y, UNSIGNED);
2261 /* Return the maxinum of X and Y, treating them both as having
2262 signedness SGN. */
2263 template <typename T1, typename T2>
2264 inline WI_BINARY_RESULT (T1, T2)
2265 wi::max (const T1 &x, const T2 &y, signop sgn)
2267 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2268 unsigned int precision = get_precision (result);
2269 if (wi::ge_p (x, y, sgn))
2270 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2271 else
2272 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2273 return result;
2276 /* Return the maximum of X and Y, treating both as signed values. */
2277 template <typename T1, typename T2>
2278 inline WI_BINARY_RESULT (T1, T2)
2279 wi::smax (const T1 &x, const T2 &y)
2281 return wi::max (x, y, SIGNED);
2284 /* Return the maximum of X and Y, treating both as unsigned values. */
2285 template <typename T1, typename T2>
2286 inline WI_BINARY_RESULT (T1, T2)
2287 wi::umax (const T1 &x, const T2 &y)
2289 return wi::max (x, y, UNSIGNED);
2292 /* Return X & Y. */
2293 template <typename T1, typename T2>
2294 inline WI_BINARY_RESULT (T1, T2)
2295 wi::bit_and (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 (and_large (val, xi.val, xi.len, yi.val, yi.len,
2309 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::bit_and_not (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 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2323 if (__builtin_expect (xi.len + yi.len == 2, true))
2325 val[0] = xi.ulow () & ~yi.ulow ();
2326 result.set_len (1, is_sign_extended);
2328 else
2329 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2330 precision), is_sign_extended);
2331 return result;
2334 /* Return X | Y. */
2335 template <typename T1, typename T2>
2336 inline WI_BINARY_RESULT (T1, T2)
2337 wi::bit_or (const T1 &x, const T2 &y)
2339 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2340 unsigned int precision = get_precision (result);
2341 WIDE_INT_REF_FOR (T1) xi (x, precision);
2342 WIDE_INT_REF_FOR (T2) yi (y, precision);
2343 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2344 if (__builtin_expect (xi.len + yi.len == 2, true))
2346 val[0] = xi.ulow () | yi.ulow ();
2347 result.set_len (1, is_sign_extended);
2349 else
2350 result.set_len (or_large (val, xi.val, xi.len,
2351 yi.val, yi.len, precision), is_sign_extended);
2352 return result;
2355 /* Return X | ~Y. */
2356 template <typename T1, typename T2>
2357 inline WI_BINARY_RESULT (T1, T2)
2358 wi::bit_or_not (const T1 &x, const T2 &y)
2360 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2361 unsigned int precision = get_precision (result);
2362 WIDE_INT_REF_FOR (T1) xi (x, precision);
2363 WIDE_INT_REF_FOR (T2) yi (y, precision);
2364 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2365 if (__builtin_expect (xi.len + yi.len == 2, true))
2367 val[0] = xi.ulow () | ~yi.ulow ();
2368 result.set_len (1, is_sign_extended);
2370 else
2371 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2372 precision), is_sign_extended);
2373 return result;
2376 /* Return X ^ Y. */
2377 template <typename T1, typename T2>
2378 inline WI_BINARY_RESULT (T1, T2)
2379 wi::bit_xor (const T1 &x, const T2 &y)
2381 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2382 unsigned int precision = get_precision (result);
2383 WIDE_INT_REF_FOR (T1) xi (x, precision);
2384 WIDE_INT_REF_FOR (T2) yi (y, precision);
2385 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2386 if (__builtin_expect (xi.len + yi.len == 2, true))
2388 val[0] = xi.ulow () ^ yi.ulow ();
2389 result.set_len (1, is_sign_extended);
2391 else
2392 result.set_len (xor_large (val, xi.val, xi.len,
2393 yi.val, yi.len, precision), is_sign_extended);
2394 return result;
2397 /* Return X + Y. */
2398 template <typename T1, typename T2>
2399 inline WI_BINARY_RESULT (T1, T2)
2400 wi::add (const T1 &x, const T2 &y)
2402 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2403 unsigned int precision = get_precision (result);
2404 WIDE_INT_REF_FOR (T1) xi (x, precision);
2405 WIDE_INT_REF_FOR (T2) yi (y, precision);
2406 if (precision <= HOST_BITS_PER_WIDE_INT)
2408 val[0] = xi.ulow () + yi.ulow ();
2409 result.set_len (1);
2411 /* If the precision is known at compile time to be greater than
2412 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2413 knowing that (a) all bits in those HWIs are significant and
2414 (b) the result has room for at least two HWIs. This provides
2415 a fast path for things like offset_int and widest_int.
2417 The STATIC_CONSTANT_P test prevents this path from being
2418 used for wide_ints. wide_ints with precisions greater than
2419 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2420 point handling them inline. */
2421 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2422 && __builtin_expect (xi.len + yi.len == 2, true))
2424 unsigned HOST_WIDE_INT xl = xi.ulow ();
2425 unsigned HOST_WIDE_INT yl = yi.ulow ();
2426 unsigned HOST_WIDE_INT resultl = xl + yl;
2427 val[0] = resultl;
2428 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2429 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2430 >> (HOST_BITS_PER_WIDE_INT - 1)));
2432 else
2433 result.set_len (add_large (val, xi.val, xi.len,
2434 yi.val, yi.len, precision,
2435 UNSIGNED, 0));
2436 return result;
2439 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2440 and indicate in *OVERFLOW whether the operation overflowed. */
2441 template <typename T1, typename T2>
2442 inline WI_BINARY_RESULT (T1, T2)
2443 wi::add (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2445 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2446 unsigned int precision = get_precision (result);
2447 WIDE_INT_REF_FOR (T1) xi (x, precision);
2448 WIDE_INT_REF_FOR (T2) yi (y, precision);
2449 if (precision <= HOST_BITS_PER_WIDE_INT)
2451 unsigned HOST_WIDE_INT xl = xi.ulow ();
2452 unsigned HOST_WIDE_INT yl = yi.ulow ();
2453 unsigned HOST_WIDE_INT resultl = xl + yl;
2454 if (sgn == SIGNED)
2456 if ((((resultl ^ xl) & (resultl ^ yl))
2457 >> (precision - 1)) & 1)
2459 if (xl > resultl)
2460 *overflow = OVF_UNDERFLOW;
2461 else if (xl < resultl)
2462 *overflow = OVF_OVERFLOW;
2463 else
2464 *overflow = OVF_NONE;
2466 else
2467 *overflow = OVF_NONE;
2469 else
2470 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2471 < (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2472 ? OVF_OVERFLOW : OVF_NONE;
2473 val[0] = resultl;
2474 result.set_len (1);
2476 else
2477 result.set_len (add_large (val, xi.val, xi.len,
2478 yi.val, yi.len, precision,
2479 sgn, overflow));
2480 return result;
2483 /* Return X - Y. */
2484 template <typename T1, typename T2>
2485 inline WI_BINARY_RESULT (T1, T2)
2486 wi::sub (const T1 &x, const T2 &y)
2488 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2489 unsigned int precision = get_precision (result);
2490 WIDE_INT_REF_FOR (T1) xi (x, precision);
2491 WIDE_INT_REF_FOR (T2) yi (y, precision);
2492 if (precision <= HOST_BITS_PER_WIDE_INT)
2494 val[0] = xi.ulow () - yi.ulow ();
2495 result.set_len (1);
2497 /* If the precision is known at compile time to be greater than
2498 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2499 knowing that (a) all bits in those HWIs are significant and
2500 (b) the result has room for at least two HWIs. This provides
2501 a fast path for things like offset_int and widest_int.
2503 The STATIC_CONSTANT_P test prevents this path from being
2504 used for wide_ints. wide_ints with precisions greater than
2505 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2506 point handling them inline. */
2507 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2508 && __builtin_expect (xi.len + yi.len == 2, true))
2510 unsigned HOST_WIDE_INT xl = xi.ulow ();
2511 unsigned HOST_WIDE_INT yl = yi.ulow ();
2512 unsigned HOST_WIDE_INT resultl = xl - yl;
2513 val[0] = resultl;
2514 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2515 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2516 >> (HOST_BITS_PER_WIDE_INT - 1)));
2518 else
2519 result.set_len (sub_large (val, xi.val, xi.len,
2520 yi.val, yi.len, precision,
2521 UNSIGNED, 0));
2522 return result;
2525 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2526 and indicate in *OVERFLOW whether the operation overflowed. */
2527 template <typename T1, typename T2>
2528 inline WI_BINARY_RESULT (T1, T2)
2529 wi::sub (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2531 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2532 unsigned int precision = get_precision (result);
2533 WIDE_INT_REF_FOR (T1) xi (x, precision);
2534 WIDE_INT_REF_FOR (T2) yi (y, precision);
2535 if (precision <= HOST_BITS_PER_WIDE_INT)
2537 unsigned HOST_WIDE_INT xl = xi.ulow ();
2538 unsigned HOST_WIDE_INT yl = yi.ulow ();
2539 unsigned HOST_WIDE_INT resultl = xl - yl;
2540 if (sgn == SIGNED)
2542 if ((((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1)
2544 if (xl > yl)
2545 *overflow = OVF_UNDERFLOW;
2546 else if (xl < yl)
2547 *overflow = OVF_OVERFLOW;
2548 else
2549 *overflow = OVF_NONE;
2551 else
2552 *overflow = OVF_NONE;
2554 else
2555 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2556 > (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2557 ? OVF_UNDERFLOW : OVF_NONE;
2558 val[0] = resultl;
2559 result.set_len (1);
2561 else
2562 result.set_len (sub_large (val, xi.val, xi.len,
2563 yi.val, yi.len, precision,
2564 sgn, overflow));
2565 return result;
2568 /* Return X * Y. */
2569 template <typename T1, typename T2>
2570 inline WI_BINARY_RESULT (T1, T2)
2571 wi::mul (const T1 &x, const T2 &y)
2573 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2574 unsigned int precision = get_precision (result);
2575 WIDE_INT_REF_FOR (T1) xi (x, precision);
2576 WIDE_INT_REF_FOR (T2) yi (y, precision);
2577 if (precision <= HOST_BITS_PER_WIDE_INT)
2579 val[0] = xi.ulow () * yi.ulow ();
2580 result.set_len (1);
2582 else
2583 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2584 precision, UNSIGNED, 0, false));
2585 return result;
2588 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2589 and indicate in *OVERFLOW whether the operation overflowed. */
2590 template <typename T1, typename T2>
2591 inline WI_BINARY_RESULT (T1, T2)
2592 wi::mul (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2594 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2595 unsigned int precision = get_precision (result);
2596 WIDE_INT_REF_FOR (T1) xi (x, precision);
2597 WIDE_INT_REF_FOR (T2) yi (y, precision);
2598 result.set_len (mul_internal (val, xi.val, xi.len,
2599 yi.val, yi.len, precision,
2600 sgn, overflow, false));
2601 return result;
2604 /* Return X * Y, treating both X and Y as signed values. Indicate in
2605 *OVERFLOW whether the operation overflowed. */
2606 template <typename T1, typename T2>
2607 inline WI_BINARY_RESULT (T1, T2)
2608 wi::smul (const T1 &x, const T2 &y, overflow_type *overflow)
2610 return mul (x, y, SIGNED, overflow);
2613 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2614 *OVERFLOW if the result overflows. */
2615 template <typename T1, typename T2>
2616 inline WI_BINARY_RESULT (T1, T2)
2617 wi::umul (const T1 &x, const T2 &y, overflow_type *overflow)
2619 return mul (x, y, UNSIGNED, overflow);
2622 /* Perform a widening multiplication of X and Y, extending the values
2623 according to SGN, and return the high part of the result. */
2624 template <typename T1, typename T2>
2625 inline WI_BINARY_RESULT (T1, T2)
2626 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2628 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2629 unsigned int precision = get_precision (result);
2630 WIDE_INT_REF_FOR (T1) xi (x, precision);
2631 WIDE_INT_REF_FOR (T2) yi (y, precision);
2632 result.set_len (mul_internal (val, xi.val, xi.len,
2633 yi.val, yi.len, precision,
2634 sgn, 0, true));
2635 return result;
2638 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2639 signedness given by SGN. Indicate in *OVERFLOW if the result
2640 overflows. */
2641 template <typename T1, typename T2>
2642 inline WI_BINARY_RESULT (T1, T2)
2643 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2645 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2646 unsigned int precision = get_precision (quotient);
2647 WIDE_INT_REF_FOR (T1) xi (x, precision);
2648 WIDE_INT_REF_FOR (T2) yi (y);
2650 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2651 precision,
2652 yi.val, yi.len, yi.precision,
2653 sgn, overflow));
2654 return quotient;
2657 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2658 template <typename T1, typename T2>
2659 inline WI_BINARY_RESULT (T1, T2)
2660 wi::sdiv_trunc (const T1 &x, const T2 &y)
2662 return div_trunc (x, y, SIGNED);
2665 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2666 template <typename T1, typename T2>
2667 inline WI_BINARY_RESULT (T1, T2)
2668 wi::udiv_trunc (const T1 &x, const T2 &y)
2670 return div_trunc (x, y, UNSIGNED);
2673 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2674 signedness given by SGN. Indicate in *OVERFLOW if the result
2675 overflows. */
2676 template <typename T1, typename T2>
2677 inline WI_BINARY_RESULT (T1, T2)
2678 wi::div_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
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,
2691 overflow));
2692 remainder.set_len (remainder_len);
2693 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2694 return quotient - 1;
2695 return quotient;
2698 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2699 template <typename T1, typename T2>
2700 inline WI_BINARY_RESULT (T1, T2)
2701 wi::sdiv_floor (const T1 &x, const T2 &y)
2703 return div_floor (x, y, SIGNED);
2706 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2707 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2708 template <typename T1, typename T2>
2709 inline WI_BINARY_RESULT (T1, T2)
2710 wi::udiv_floor (const T1 &x, const T2 &y)
2712 return div_floor (x, y, UNSIGNED);
2715 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2716 signedness given by SGN. Indicate in *OVERFLOW if the result
2717 overflows. */
2718 template <typename T1, typename T2>
2719 inline WI_BINARY_RESULT (T1, T2)
2720 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2722 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2723 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2724 unsigned int precision = get_precision (quotient);
2725 WIDE_INT_REF_FOR (T1) xi (x, precision);
2726 WIDE_INT_REF_FOR (T2) yi (y);
2728 unsigned int remainder_len;
2729 quotient.set_len (divmod_internal (quotient_val,
2730 &remainder_len, remainder_val,
2731 xi.val, xi.len, precision,
2732 yi.val, yi.len, yi.precision, sgn,
2733 overflow));
2734 remainder.set_len (remainder_len);
2735 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2736 return quotient + 1;
2737 return quotient;
2740 /* Return X / Y, rouding towards +inf. Treat X and Y as unsigned values. */
2741 template <typename T1, typename T2>
2742 inline WI_BINARY_RESULT (T1, T2)
2743 wi::udiv_ceil (const T1 &x, const T2 &y)
2745 return div_ceil (x, y, UNSIGNED);
2748 /* Return X / Y, rouding towards nearest with ties away from zero.
2749 Treat X and Y as having the signedness given by SGN. Indicate
2750 in *OVERFLOW if the result overflows. */
2751 template <typename T1, typename T2>
2752 inline WI_BINARY_RESULT (T1, T2)
2753 wi::div_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2755 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2756 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2757 unsigned int precision = get_precision (quotient);
2758 WIDE_INT_REF_FOR (T1) xi (x, precision);
2759 WIDE_INT_REF_FOR (T2) yi (y);
2761 unsigned int remainder_len;
2762 quotient.set_len (divmod_internal (quotient_val,
2763 &remainder_len, remainder_val,
2764 xi.val, xi.len, precision,
2765 yi.val, yi.len, yi.precision, sgn,
2766 overflow));
2767 remainder.set_len (remainder_len);
2769 if (remainder != 0)
2771 if (sgn == SIGNED)
2773 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2774 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2776 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2777 return quotient - 1;
2778 else
2779 return quotient + 1;
2782 else
2784 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2785 return quotient + 1;
2788 return quotient;
2791 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2792 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2793 template <typename T1, typename T2>
2794 inline WI_BINARY_RESULT (T1, T2)
2795 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2796 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2798 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2799 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2800 unsigned int precision = get_precision (quotient);
2801 WIDE_INT_REF_FOR (T1) xi (x, precision);
2802 WIDE_INT_REF_FOR (T2) yi (y);
2804 unsigned int remainder_len;
2805 quotient.set_len (divmod_internal (quotient_val,
2806 &remainder_len, remainder_val,
2807 xi.val, xi.len, precision,
2808 yi.val, yi.len, yi.precision, sgn, 0));
2809 remainder.set_len (remainder_len);
2811 *remainder_ptr = remainder;
2812 return quotient;
2815 /* Compute the greatest common divisor of two numbers A and B using
2816 Euclid's algorithm. */
2817 template <typename T1, typename T2>
2818 inline WI_BINARY_RESULT (T1, T2)
2819 wi::gcd (const T1 &a, const T2 &b, signop sgn)
2821 T1 x, y, z;
2823 x = wi::abs (a);
2824 y = wi::abs (b);
2826 while (gt_p (x, 0, sgn))
2828 z = mod_trunc (y, x, sgn);
2829 y = x;
2830 x = z;
2833 return y;
2836 /* Compute X / Y, rouding towards 0, and return the remainder.
2837 Treat X and Y as having the signedness given by SGN. Indicate
2838 in *OVERFLOW if the division overflows. */
2839 template <typename T1, typename T2>
2840 inline WI_BINARY_RESULT (T1, T2)
2841 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2843 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2844 unsigned int precision = get_precision (remainder);
2845 WIDE_INT_REF_FOR (T1) xi (x, precision);
2846 WIDE_INT_REF_FOR (T2) yi (y);
2848 unsigned int remainder_len;
2849 divmod_internal (0, &remainder_len, remainder_val,
2850 xi.val, xi.len, precision,
2851 yi.val, yi.len, yi.precision, sgn, overflow);
2852 remainder.set_len (remainder_len);
2854 return remainder;
2857 /* Compute X / Y, rouding towards 0, and return the remainder.
2858 Treat X and Y as signed values. */
2859 template <typename T1, typename T2>
2860 inline WI_BINARY_RESULT (T1, T2)
2861 wi::smod_trunc (const T1 &x, const T2 &y)
2863 return mod_trunc (x, y, SIGNED);
2866 /* Compute X / Y, rouding towards 0, and return the remainder.
2867 Treat X and Y as unsigned values. */
2868 template <typename T1, typename T2>
2869 inline WI_BINARY_RESULT (T1, T2)
2870 wi::umod_trunc (const T1 &x, const T2 &y)
2872 return mod_trunc (x, y, UNSIGNED);
2875 /* Compute X / Y, rouding towards -inf, and return the remainder.
2876 Treat X and Y as having the signedness given by SGN. Indicate
2877 in *OVERFLOW if the division overflows. */
2878 template <typename T1, typename T2>
2879 inline WI_BINARY_RESULT (T1, T2)
2880 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2882 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2883 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2884 unsigned int precision = get_precision (quotient);
2885 WIDE_INT_REF_FOR (T1) xi (x, precision);
2886 WIDE_INT_REF_FOR (T2) yi (y);
2888 unsigned int remainder_len;
2889 quotient.set_len (divmod_internal (quotient_val,
2890 &remainder_len, remainder_val,
2891 xi.val, xi.len, precision,
2892 yi.val, yi.len, yi.precision, sgn,
2893 overflow));
2894 remainder.set_len (remainder_len);
2896 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2897 return remainder + y;
2898 return remainder;
2901 /* Compute X / Y, rouding towards -inf, and return the remainder.
2902 Treat X and Y as unsigned values. */
2903 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2904 template <typename T1, typename T2>
2905 inline WI_BINARY_RESULT (T1, T2)
2906 wi::umod_floor (const T1 &x, const T2 &y)
2908 return mod_floor (x, y, UNSIGNED);
2911 /* Compute X / Y, rouding towards +inf, and return the remainder.
2912 Treat X and Y as having the signedness given by SGN. Indicate
2913 in *OVERFLOW if the division overflows. */
2914 template <typename T1, typename T2>
2915 inline WI_BINARY_RESULT (T1, T2)
2916 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2918 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2919 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2920 unsigned int precision = get_precision (quotient);
2921 WIDE_INT_REF_FOR (T1) xi (x, precision);
2922 WIDE_INT_REF_FOR (T2) yi (y);
2924 unsigned int remainder_len;
2925 quotient.set_len (divmod_internal (quotient_val,
2926 &remainder_len, remainder_val,
2927 xi.val, xi.len, precision,
2928 yi.val, yi.len, yi.precision, sgn,
2929 overflow));
2930 remainder.set_len (remainder_len);
2932 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2933 return remainder - y;
2934 return remainder;
2937 /* Compute X / Y, rouding towards nearest with ties away from zero,
2938 and return the remainder. Treat X and Y as having the signedness
2939 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2940 template <typename T1, typename T2>
2941 inline WI_BINARY_RESULT (T1, T2)
2942 wi::mod_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2944 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2945 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2946 unsigned int precision = get_precision (quotient);
2947 WIDE_INT_REF_FOR (T1) xi (x, precision);
2948 WIDE_INT_REF_FOR (T2) yi (y);
2950 unsigned int remainder_len;
2951 quotient.set_len (divmod_internal (quotient_val,
2952 &remainder_len, remainder_val,
2953 xi.val, xi.len, precision,
2954 yi.val, yi.len, yi.precision, sgn,
2955 overflow));
2956 remainder.set_len (remainder_len);
2958 if (remainder != 0)
2960 if (sgn == SIGNED)
2962 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2963 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2965 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2966 return remainder + y;
2967 else
2968 return remainder - y;
2971 else
2973 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2974 return remainder - y;
2977 return remainder;
2980 /* Return true if X is a multiple of Y. Treat X and Y as having the
2981 signedness given by SGN. */
2982 template <typename T1, typename T2>
2983 inline bool
2984 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2986 return wi::mod_trunc (x, y, sgn) == 0;
2989 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2990 Treat X and Y as having the signedness given by SGN. */
2991 template <typename T1, typename T2>
2992 inline bool
2993 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2994 WI_BINARY_RESULT (T1, T2) *res)
2996 WI_BINARY_RESULT (T1, T2) remainder;
2997 WI_BINARY_RESULT (T1, T2) quotient
2998 = divmod_trunc (x, y, sgn, &remainder);
2999 if (remainder == 0)
3001 *res = quotient;
3002 return true;
3004 return false;
3007 /* Return X << Y. Return 0 if Y is greater than or equal to
3008 the precision of X. */
3009 template <typename T1, typename T2>
3010 inline WI_UNARY_RESULT (T1)
3011 wi::lshift (const T1 &x, const T2 &y)
3013 WI_UNARY_RESULT_VAR (result, val, T1, x);
3014 unsigned int precision = get_precision (result);
3015 WIDE_INT_REF_FOR (T1) xi (x, precision);
3016 WIDE_INT_REF_FOR (T2) yi (y);
3017 /* Handle the simple cases quickly. */
3018 if (geu_p (yi, precision))
3020 val[0] = 0;
3021 result.set_len (1);
3023 else
3025 unsigned int shift = yi.to_uhwi ();
3026 /* For fixed-precision integers like offset_int and widest_int,
3027 handle the case where the shift value is constant and the
3028 result is a single nonnegative HWI (meaning that we don't
3029 need to worry about val[1]). This is particularly common
3030 for converting a byte count to a bit count.
3032 For variable-precision integers like wide_int, handle HWI
3033 and sub-HWI integers inline. */
3034 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3035 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
3036 && xi.len == 1
3037 && IN_RANGE (xi.val[0], 0, HOST_WIDE_INT_MAX >> shift))
3038 : precision <= HOST_BITS_PER_WIDE_INT)
3040 val[0] = xi.ulow () << shift;
3041 result.set_len (1);
3043 else
3044 result.set_len (lshift_large (val, xi.val, xi.len,
3045 precision, shift));
3047 return result;
3050 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
3051 or equal to the precision of X. */
3052 template <typename T1, typename T2>
3053 inline WI_UNARY_RESULT (T1)
3054 wi::lrshift (const T1 &x, const T2 &y)
3056 WI_UNARY_RESULT_VAR (result, val, T1, x);
3057 /* Do things in the precision of the input rather than the output,
3058 since the result can be no larger than that. */
3059 WIDE_INT_REF_FOR (T1) xi (x);
3060 WIDE_INT_REF_FOR (T2) yi (y);
3061 /* Handle the simple cases quickly. */
3062 if (geu_p (yi, xi.precision))
3064 val[0] = 0;
3065 result.set_len (1);
3067 else
3069 unsigned int shift = yi.to_uhwi ();
3070 /* For fixed-precision integers like offset_int and widest_int,
3071 handle the case where the shift value is constant and the
3072 shifted value is a single nonnegative HWI (meaning that all
3073 bits above the HWI are zero). This is particularly common
3074 for converting a bit count to a byte count.
3076 For variable-precision integers like wide_int, handle HWI
3077 and sub-HWI integers inline. */
3078 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3079 ? (shift < HOST_BITS_PER_WIDE_INT
3080 && xi.len == 1
3081 && xi.val[0] >= 0)
3082 : xi.precision <= HOST_BITS_PER_WIDE_INT)
3084 val[0] = xi.to_uhwi () >> shift;
3085 result.set_len (1);
3087 else
3088 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
3089 get_precision (result), shift));
3091 return result;
3094 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
3095 Y is greater than or equal to the precision of X. */
3096 template <typename T1, typename T2>
3097 inline WI_UNARY_RESULT (T1)
3098 wi::arshift (const T1 &x, const T2 &y)
3100 WI_UNARY_RESULT_VAR (result, val, T1, x);
3101 /* Do things in the precision of the input rather than the output,
3102 since the result can be no larger than that. */
3103 WIDE_INT_REF_FOR (T1) xi (x);
3104 WIDE_INT_REF_FOR (T2) yi (y);
3105 /* Handle the simple cases quickly. */
3106 if (geu_p (yi, xi.precision))
3108 val[0] = sign_mask (x);
3109 result.set_len (1);
3111 else
3113 unsigned int shift = yi.to_uhwi ();
3114 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
3116 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3117 result.set_len (1, true);
3119 else
3120 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3121 get_precision (result), shift));
3123 return result;
3126 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3127 logical shift otherwise. */
3128 template <typename T1, typename T2>
3129 inline WI_UNARY_RESULT (T1)
3130 wi::rshift (const T1 &x, const T2 &y, signop sgn)
3132 if (sgn == UNSIGNED)
3133 return lrshift (x, y);
3134 else
3135 return arshift (x, y);
3138 /* Return the result of rotating the low WIDTH bits of X left by Y
3139 bits and zero-extending the result. Use a full-width rotate if
3140 WIDTH is zero. */
3141 template <typename T1, typename T2>
3142 WI_UNARY_RESULT (T1)
3143 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3145 unsigned int precision = get_binary_precision (x, x);
3146 if (width == 0)
3147 width = precision;
3148 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3149 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3150 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
3151 if (width != precision)
3152 return wi::zext (left, width) | wi::zext (right, width);
3153 return left | right;
3156 /* Return the result of rotating the low WIDTH bits of X right by Y
3157 bits and zero-extending the result. Use a full-width rotate if
3158 WIDTH is zero. */
3159 template <typename T1, typename T2>
3160 WI_UNARY_RESULT (T1)
3161 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3163 unsigned int precision = get_binary_precision (x, x);
3164 if (width == 0)
3165 width = precision;
3166 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3167 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
3168 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3169 if (width != precision)
3170 return wi::zext (left, width) | wi::zext (right, width);
3171 return left | right;
3174 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3175 is odd. */
3176 inline int
3177 wi::parity (const wide_int_ref &x)
3179 return popcount (x) & 1;
3182 /* Extract WIDTH bits from X, starting at BITPOS. */
3183 template <typename T>
3184 inline unsigned HOST_WIDE_INT
3185 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3187 unsigned precision = get_precision (x);
3188 if (precision < bitpos + width)
3189 precision = bitpos + width;
3190 WIDE_INT_REF_FOR (T) xi (x, precision);
3192 /* Handle this rare case after the above, so that we assert about
3193 bogus BITPOS values. */
3194 if (width == 0)
3195 return 0;
3197 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3198 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3199 unsigned HOST_WIDE_INT res = xi.elt (start);
3200 res >>= shift;
3201 if (shift + width > HOST_BITS_PER_WIDE_INT)
3203 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3204 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3206 return zext_hwi (res, width);
3209 /* Return the minimum precision needed to store X with sign SGN. */
3210 template <typename T>
3211 inline unsigned int
3212 wi::min_precision (const T &x, signop sgn)
3214 if (sgn == SIGNED)
3215 return get_precision (x) - clrsb (x);
3216 else
3217 return get_precision (x) - clz (x);
3220 #define SIGNED_BINARY_PREDICATE(OP, F) \
3221 template <typename T1, typename T2> \
3222 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3223 OP (const T1 &x, const T2 &y) \
3225 return wi::F (x, y); \
3228 SIGNED_BINARY_PREDICATE (operator <, lts_p)
3229 SIGNED_BINARY_PREDICATE (operator <=, les_p)
3230 SIGNED_BINARY_PREDICATE (operator >, gts_p)
3231 SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3233 #undef SIGNED_BINARY_PREDICATE
3235 #define UNARY_OPERATOR(OP, F) \
3236 template<typename T> \
3237 WI_UNARY_RESULT (generic_wide_int<T>) \
3238 OP (const generic_wide_int<T> &x) \
3240 return wi::F (x); \
3243 #define BINARY_PREDICATE(OP, F) \
3244 template<typename T1, typename T2> \
3245 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3246 OP (const T1 &x, const T2 &y) \
3248 return wi::F (x, y); \
3251 #define BINARY_OPERATOR(OP, F) \
3252 template<typename T1, typename T2> \
3253 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3254 OP (const T1 &x, const T2 &y) \
3256 return wi::F (x, y); \
3259 #define SHIFT_OPERATOR(OP, F) \
3260 template<typename T1, typename T2> \
3261 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3262 OP (const T1 &x, const T2 &y) \
3264 return wi::F (x, y); \
3267 UNARY_OPERATOR (operator ~, bit_not)
3268 UNARY_OPERATOR (operator -, neg)
3269 BINARY_PREDICATE (operator ==, eq_p)
3270 BINARY_PREDICATE (operator !=, ne_p)
3271 BINARY_OPERATOR (operator &, bit_and)
3272 BINARY_OPERATOR (operator |, bit_or)
3273 BINARY_OPERATOR (operator ^, bit_xor)
3274 BINARY_OPERATOR (operator +, add)
3275 BINARY_OPERATOR (operator -, sub)
3276 BINARY_OPERATOR (operator *, mul)
3277 SHIFT_OPERATOR (operator <<, lshift)
3279 #undef UNARY_OPERATOR
3280 #undef BINARY_PREDICATE
3281 #undef BINARY_OPERATOR
3282 #undef SHIFT_OPERATOR
3284 template <typename T1, typename T2>
3285 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3286 operator >> (const T1 &x, const T2 &y)
3288 return wi::arshift (x, y);
3291 template <typename T1, typename T2>
3292 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3293 operator / (const T1 &x, const T2 &y)
3295 return wi::sdiv_trunc (x, y);
3298 template <typename T1, typename T2>
3299 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3300 operator % (const T1 &x, const T2 &y)
3302 return wi::smod_trunc (x, y);
3305 template<typename T>
3306 void
3307 gt_ggc_mx (generic_wide_int <T> *)
3311 template<typename T>
3312 void
3313 gt_pch_nx (generic_wide_int <T> *)
3317 template<typename T>
3318 void
3319 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3323 template<int N>
3324 void
3325 gt_ggc_mx (trailing_wide_ints <N> *)
3329 template<int N>
3330 void
3331 gt_pch_nx (trailing_wide_ints <N> *)
3335 template<int N>
3336 void
3337 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3341 namespace wi
3343 /* Used for overloaded functions in which the only other acceptable
3344 scalar type is a pointer. It stops a plain 0 from being treated
3345 as a null pointer. */
3346 struct never_used1 {};
3347 struct never_used2 {};
3349 wide_int min_value (unsigned int, signop);
3350 wide_int min_value (never_used1 *);
3351 wide_int min_value (never_used2 *);
3352 wide_int max_value (unsigned int, signop);
3353 wide_int max_value (never_used1 *);
3354 wide_int max_value (never_used2 *);
3356 /* FIXME: this is target dependent, so should be elsewhere.
3357 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3358 wide_int from_buffer (const unsigned char *, unsigned int);
3360 #ifndef GENERATOR_FILE
3361 void to_mpz (const wide_int_ref &, mpz_t, signop);
3362 #endif
3364 wide_int mask (unsigned int, bool, unsigned int);
3365 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3366 wide_int set_bit_in_zero (unsigned int, unsigned int);
3367 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3368 unsigned int);
3369 wide_int round_down_for_mask (const wide_int &, const wide_int &);
3370 wide_int round_up_for_mask (const wide_int &, const wide_int &);
3372 template <typename T>
3373 T mask (unsigned int, bool);
3375 template <typename T>
3376 T shifted_mask (unsigned int, unsigned int, bool);
3378 template <typename T>
3379 T set_bit_in_zero (unsigned int);
3381 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3382 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3383 bool, unsigned int);
3384 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3385 unsigned int, unsigned int, bool);
3388 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3389 and the other bits are clear, or the inverse if NEGATE_P. */
3390 inline wide_int
3391 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3393 wide_int result = wide_int::create (precision);
3394 result.set_len (mask (result.write_val (), width, negate_p, precision));
3395 return result;
3398 /* Return a PRECISION-bit integer in which the low START bits are clear,
3399 the next WIDTH bits are set, and the other bits are clear,
3400 or the inverse if NEGATE_P. */
3401 inline wide_int
3402 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3403 unsigned int precision)
3405 wide_int result = wide_int::create (precision);
3406 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3407 precision));
3408 return result;
3411 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3412 others are clear. */
3413 inline wide_int
3414 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3416 return shifted_mask (bit, 1, false, precision);
3419 /* Return an integer of type T in which the low WIDTH bits are set
3420 and the other bits are clear, or the inverse if NEGATE_P. */
3421 template <typename T>
3422 inline T
3423 wi::mask (unsigned int width, bool negate_p)
3425 STATIC_ASSERT (wi::int_traits<T>::precision);
3426 T result;
3427 result.set_len (mask (result.write_val (), width, negate_p,
3428 wi::int_traits <T>::precision));
3429 return result;
3432 /* Return an integer of type T in which the low START bits are clear,
3433 the next WIDTH bits are set, and the other bits are clear, or the
3434 inverse if NEGATE_P. */
3435 template <typename T>
3436 inline T
3437 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3439 STATIC_ASSERT (wi::int_traits<T>::precision);
3440 T result;
3441 result.set_len (shifted_mask (result.write_val (), start, width,
3442 negate_p,
3443 wi::int_traits <T>::precision));
3444 return result;
3447 /* Return an integer of type T in which bit BIT is set and all the
3448 others are clear. */
3449 template <typename T>
3450 inline T
3451 wi::set_bit_in_zero (unsigned int bit)
3453 return shifted_mask <T> (bit, 1, false);
3456 /* Accumulate a set of overflows into OVERFLOW. */
3458 static inline void
3459 wi::accumulate_overflow (wi::overflow_type &overflow,
3460 wi::overflow_type suboverflow)
3462 if (!suboverflow)
3463 return;
3464 if (!overflow)
3465 overflow = suboverflow;
3466 else if (overflow != suboverflow)
3467 overflow = wi::OVF_UNKNOWN;
3470 #endif /* WIDE_INT_H */