Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / wide-int.h
blob498d14d188e3d7cf8b77c3ccd017309fd87fb8d0
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2023 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 max number of HWIs needed to store a wide_int of PRECISION. */
268 #define WIDE_INT_MAX_HWIS(PRECISION) \
269 ((PRECISION + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
271 /* The type of result produced by a binary operation on types T1 and T2.
272 Defined purely for brevity. */
273 #define WI_BINARY_RESULT(T1, T2) \
274 typename wi::binary_traits <T1, T2>::result_type
276 /* Likewise for binary operators, which excludes the case in which neither
277 T1 nor T2 is a wide-int-based type. */
278 #define WI_BINARY_OPERATOR_RESULT(T1, T2) \
279 typename wi::binary_traits <T1, T2>::operator_result
281 /* The type of result produced by T1 << T2. Leads to substitution failure
282 if the operation isn't supported. Defined purely for brevity. */
283 #define WI_SIGNED_SHIFT_RESULT(T1, T2) \
284 typename wi::binary_traits <T1, T2>::signed_shift_result_type
286 /* The type of result produced by a sign-agnostic binary predicate on
287 types T1 and T2. This is bool if wide-int operations make sense for
288 T1 and T2 and leads to substitution failure otherwise. */
289 #define WI_BINARY_PREDICATE_RESULT(T1, T2) \
290 typename wi::binary_traits <T1, T2>::predicate_result
292 /* The type of result produced by a signed binary predicate on types T1 and T2.
293 This is bool if signed comparisons make sense for T1 and T2 and leads to
294 substitution failure otherwise. */
295 #define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2) \
296 typename wi::binary_traits <T1, T2>::signed_predicate_result
298 /* The type of result produced by a unary operation on type T. */
299 #define WI_UNARY_RESULT(T) \
300 typename wi::binary_traits <T, T>::result_type
302 /* Define a variable RESULT to hold the result of a binary operation on
303 X and Y, which have types T1 and T2 respectively. Define VAL to
304 point to the blocks of RESULT. Once the user of the macro has
305 filled in VAL, it should call RESULT.set_len to set the number
306 of initialized blocks. */
307 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
308 WI_BINARY_RESULT (T1, T2) RESULT = \
309 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
310 HOST_WIDE_INT *VAL = RESULT.write_val ()
312 /* Similar for the result of a unary operation on X, which has type T. */
313 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
314 WI_UNARY_RESULT (T) RESULT = \
315 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
316 HOST_WIDE_INT *VAL = RESULT.write_val ()
318 template <typename T> class generic_wide_int;
319 template <int N> class fixed_wide_int_storage;
320 class wide_int_storage;
322 /* An N-bit integer. Until we can use typedef templates, use this instead. */
323 #define FIXED_WIDE_INT(N) \
324 generic_wide_int < fixed_wide_int_storage <N> >
326 typedef generic_wide_int <wide_int_storage> wide_int;
327 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
328 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
329 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
330 so as not to confuse gengtype. */
331 typedef generic_wide_int < fixed_wide_int_storage <WIDE_INT_MAX_PRECISION * 2> > widest2_int;
333 /* wi::storage_ref can be a reference to a primitive type,
334 so this is the conservatively-correct setting. */
335 template <bool SE, bool HDP = true>
336 class wide_int_ref_storage;
338 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
340 /* This can be used instead of wide_int_ref if the referenced value is
341 known to have type T. It carries across properties of T's representation,
342 such as whether excess upper bits in a HWI are defined, and can therefore
343 help avoid redundant work.
345 The macro could be replaced with a template typedef, once we're able
346 to use those. */
347 #define WIDE_INT_REF_FOR(T) \
348 generic_wide_int \
349 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
350 wi::int_traits <T>::host_dependent_precision> >
352 namespace wi
354 /* Operations that calculate overflow do so even for
355 TYPE_OVERFLOW_WRAPS types. For example, adding 1 to +MAX_INT in
356 an unsigned int is 0 and does not overflow in C/C++, but wi::add
357 will set the overflow argument in case it's needed for further
358 analysis.
360 For operations that require overflow, these are the different
361 types of overflow. */
362 enum overflow_type {
363 OVF_NONE = 0,
364 OVF_UNDERFLOW = -1,
365 OVF_OVERFLOW = 1,
366 /* There was an overflow, but we are unsure whether it was an
367 overflow or an underflow. */
368 OVF_UNKNOWN = 2
371 /* Classifies an integer based on its precision. */
372 enum precision_type {
373 /* The integer has both a precision and defined signedness. This allows
374 the integer to be converted to any width, since we know whether to fill
375 any extra bits with zeros or signs. */
376 FLEXIBLE_PRECISION,
378 /* The integer has a variable precision but no defined signedness. */
379 VAR_PRECISION,
381 /* The integer has a constant precision (known at GCC compile time)
382 and is signed. */
383 CONST_PRECISION
386 /* This class, which has no default implementation, is expected to
387 provide the following members:
389 static const enum precision_type precision_type;
390 Classifies the type of T.
392 static const unsigned int precision;
393 Only defined if precision_type == CONST_PRECISION. Specifies the
394 precision of all integers of type T.
396 static const bool host_dependent_precision;
397 True if the precision of T depends (or can depend) on the host.
399 static unsigned int get_precision (const T &x)
400 Return the number of bits in X.
402 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
403 unsigned int precision, const T &x)
404 Decompose X as a PRECISION-bit integer, returning the associated
405 wi::storage_ref. SCRATCH is available as scratch space if needed.
406 The routine should assert that PRECISION is acceptable. */
407 template <typename T> struct int_traits;
409 /* This class provides a single type, result_type, which specifies the
410 type of integer produced by a binary operation whose inputs have
411 types T1 and T2. The definition should be symmetric. */
412 template <typename T1, typename T2,
413 enum precision_type P1 = int_traits <T1>::precision_type,
414 enum precision_type P2 = int_traits <T2>::precision_type>
415 struct binary_traits;
417 /* Specify the result type for each supported combination of binary
418 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
419 mixed, in order to give stronger type checking. When both inputs
420 are CONST_PRECISION, they must have the same precision. */
421 template <typename T1, typename T2>
422 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
424 typedef widest_int result_type;
425 /* Don't define operators for this combination. */
428 template <typename T1, typename T2>
429 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
431 typedef wide_int result_type;
432 typedef result_type operator_result;
433 typedef bool predicate_result;
436 template <typename T1, typename T2>
437 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
439 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
440 so as not to confuse gengtype. */
441 typedef generic_wide_int < fixed_wide_int_storage
442 <int_traits <T2>::precision> > result_type;
443 typedef result_type operator_result;
444 typedef bool predicate_result;
445 typedef result_type signed_shift_result_type;
446 typedef bool signed_predicate_result;
449 template <typename T1, typename T2>
450 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
452 typedef wide_int result_type;
453 typedef result_type operator_result;
454 typedef bool predicate_result;
457 template <typename T1, typename T2>
458 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
460 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
461 so as not to confuse gengtype. */
462 typedef generic_wide_int < fixed_wide_int_storage
463 <int_traits <T1>::precision> > result_type;
464 typedef result_type operator_result;
465 typedef bool predicate_result;
466 typedef result_type signed_shift_result_type;
467 typedef bool signed_predicate_result;
470 template <typename T1, typename T2>
471 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
473 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
474 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
475 so as not to confuse gengtype. */
476 typedef generic_wide_int < fixed_wide_int_storage
477 <int_traits <T1>::precision> > result_type;
478 typedef result_type operator_result;
479 typedef bool predicate_result;
480 typedef result_type signed_shift_result_type;
481 typedef bool signed_predicate_result;
484 template <typename T1, typename T2>
485 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
487 typedef wide_int result_type;
488 typedef result_type operator_result;
489 typedef bool predicate_result;
493 /* Public functions for querying and operating on integers. */
494 namespace wi
496 template <typename T>
497 unsigned int get_precision (const T &);
499 template <typename T1, typename T2>
500 unsigned int get_binary_precision (const T1 &, const T2 &);
502 template <typename T1, typename T2>
503 void copy (T1 &, const T2 &);
505 #define UNARY_PREDICATE \
506 template <typename T> bool
507 #define UNARY_FUNCTION \
508 template <typename T> WI_UNARY_RESULT (T)
509 #define BINARY_PREDICATE \
510 template <typename T1, typename T2> bool
511 #define BINARY_FUNCTION \
512 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
513 #define SHIFT_FUNCTION \
514 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
516 UNARY_PREDICATE fits_shwi_p (const T &);
517 UNARY_PREDICATE fits_uhwi_p (const T &);
518 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
520 template <typename T>
521 HOST_WIDE_INT sign_mask (const T &);
523 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
524 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
525 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
526 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
527 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
528 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
529 BINARY_PREDICATE les_p (const T1 &, const T2 &);
530 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
531 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
532 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
533 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
534 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
535 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
536 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
538 template <typename T1, typename T2>
539 int cmp (const T1 &, const T2 &, signop);
541 template <typename T1, typename T2>
542 int cmps (const T1 &, const T2 &);
544 template <typename T1, typename T2>
545 int cmpu (const T1 &, const T2 &);
547 UNARY_FUNCTION bit_not (const T &);
548 UNARY_FUNCTION neg (const T &);
549 UNARY_FUNCTION neg (const T &, overflow_type *);
550 UNARY_FUNCTION abs (const T &);
551 UNARY_FUNCTION ext (const T &, unsigned int, signop);
552 UNARY_FUNCTION sext (const T &, unsigned int);
553 UNARY_FUNCTION zext (const T &, unsigned int);
554 UNARY_FUNCTION set_bit (const T &, unsigned int);
555 UNARY_FUNCTION bswap (const T &);
556 UNARY_FUNCTION bitreverse (const T &);
558 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
559 BINARY_FUNCTION smin (const T1 &, const T2 &);
560 BINARY_FUNCTION umin (const T1 &, const T2 &);
561 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
562 BINARY_FUNCTION smax (const T1 &, const T2 &);
563 BINARY_FUNCTION umax (const T1 &, const T2 &);
565 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
566 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
567 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
568 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
569 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
570 BINARY_FUNCTION add (const T1 &, const T2 &);
571 BINARY_FUNCTION add (const T1 &, const T2 &, signop, overflow_type *);
572 BINARY_FUNCTION sub (const T1 &, const T2 &);
573 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, overflow_type *);
574 BINARY_FUNCTION mul (const T1 &, const T2 &);
575 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, overflow_type *);
576 BINARY_FUNCTION smul (const T1 &, const T2 &, overflow_type *);
577 BINARY_FUNCTION umul (const T1 &, const T2 &, overflow_type *);
578 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
579 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop,
580 overflow_type * = 0);
581 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
582 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
583 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop,
584 overflow_type * = 0);
585 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
586 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
587 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop,
588 overflow_type * = 0);
589 BINARY_FUNCTION udiv_ceil (const T1 &, const T2 &);
590 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop,
591 overflow_type * = 0);
592 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
593 WI_BINARY_RESULT (T1, T2) *);
594 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
595 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop,
596 overflow_type * = 0);
597 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
598 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
599 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop,
600 overflow_type * = 0);
601 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
602 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop,
603 overflow_type * = 0);
604 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop,
605 overflow_type * = 0);
607 template <typename T1, typename T2>
608 bool multiple_of_p (const T1 &, const T2 &, signop);
610 template <typename T1, typename T2>
611 bool multiple_of_p (const T1 &, const T2 &, signop,
612 WI_BINARY_RESULT (T1, T2) *);
614 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
615 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
616 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
617 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
618 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
619 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
621 #undef SHIFT_FUNCTION
622 #undef BINARY_PREDICATE
623 #undef BINARY_FUNCTION
624 #undef UNARY_PREDICATE
625 #undef UNARY_FUNCTION
627 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
628 bool only_sign_bit_p (const wide_int_ref &);
629 int clz (const wide_int_ref &);
630 int clrsb (const wide_int_ref &);
631 int ctz (const wide_int_ref &);
632 int exact_log2 (const wide_int_ref &);
633 int floor_log2 (const wide_int_ref &);
634 int ffs (const wide_int_ref &);
635 int popcount (const wide_int_ref &);
636 int parity (const wide_int_ref &);
638 template <typename T>
639 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
641 template <typename T>
642 unsigned int min_precision (const T &, signop);
644 static inline void accumulate_overflow (overflow_type &, overflow_type);
647 namespace wi
649 /* Contains the components of a decomposed integer for easy, direct
650 access. */
651 class storage_ref
653 public:
654 storage_ref () {}
655 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
657 const HOST_WIDE_INT *val;
658 unsigned int len;
659 unsigned int precision;
661 /* Provide enough trappings for this class to act as storage for
662 generic_wide_int. */
663 unsigned int get_len () const;
664 unsigned int get_precision () const;
665 const HOST_WIDE_INT *get_val () const;
669 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
670 unsigned int len_in,
671 unsigned int precision_in)
672 : val (val_in), len (len_in), precision (precision_in)
676 inline unsigned int
677 wi::storage_ref::get_len () const
679 return len;
682 inline unsigned int
683 wi::storage_ref::get_precision () const
685 return precision;
688 inline const HOST_WIDE_INT *
689 wi::storage_ref::get_val () const
691 return val;
694 /* This class defines an integer type using the storage provided by the
695 template argument. The storage class must provide the following
696 functions:
698 unsigned int get_precision () const
699 Return the number of bits in the integer.
701 HOST_WIDE_INT *get_val () const
702 Return a pointer to the array of blocks that encodes the integer.
704 unsigned int get_len () const
705 Return the number of blocks in get_val (). If this is smaller
706 than the number of blocks implied by get_precision (), the
707 remaining blocks are sign extensions of block get_len () - 1.
709 Although not required by generic_wide_int itself, writable storage
710 classes can also provide the following functions:
712 HOST_WIDE_INT *write_val ()
713 Get a modifiable version of get_val ()
715 unsigned int set_len (unsigned int len)
716 Set the value returned by get_len () to LEN. */
717 template <typename storage>
718 class GTY(()) generic_wide_int : public storage
720 public:
721 generic_wide_int ();
723 template <typename T>
724 generic_wide_int (const T &);
726 template <typename T>
727 generic_wide_int (const T &, unsigned int);
729 /* Conversions. */
730 HOST_WIDE_INT to_shwi (unsigned int) const;
731 HOST_WIDE_INT to_shwi () const;
732 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
733 unsigned HOST_WIDE_INT to_uhwi () const;
734 HOST_WIDE_INT to_short_addr () const;
736 /* Public accessors for the interior of a wide int. */
737 HOST_WIDE_INT sign_mask () const;
738 HOST_WIDE_INT elt (unsigned int) const;
739 HOST_WIDE_INT sext_elt (unsigned int) const;
740 unsigned HOST_WIDE_INT ulow () const;
741 unsigned HOST_WIDE_INT uhigh () const;
742 HOST_WIDE_INT slow () const;
743 HOST_WIDE_INT shigh () const;
745 template <typename T>
746 generic_wide_int &operator = (const T &);
748 #define ASSIGNMENT_OPERATOR(OP, F) \
749 template <typename T> \
750 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
752 /* Restrict these to cases where the shift operator is defined. */
753 #define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
754 template <typename T> \
755 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
757 #define INCDEC_OPERATOR(OP, DELTA) \
758 generic_wide_int &OP () { *this += DELTA; return *this; }
760 ASSIGNMENT_OPERATOR (operator &=, bit_and)
761 ASSIGNMENT_OPERATOR (operator |=, bit_or)
762 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
763 ASSIGNMENT_OPERATOR (operator +=, add)
764 ASSIGNMENT_OPERATOR (operator -=, sub)
765 ASSIGNMENT_OPERATOR (operator *=, mul)
766 ASSIGNMENT_OPERATOR (operator <<=, lshift)
767 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
768 INCDEC_OPERATOR (operator ++, 1)
769 INCDEC_OPERATOR (operator --, -1)
771 #undef SHIFT_ASSIGNMENT_OPERATOR
772 #undef ASSIGNMENT_OPERATOR
773 #undef INCDEC_OPERATOR
775 /* Debugging functions. */
776 void dump () const;
778 static const bool is_sign_extended
779 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
782 template <typename storage>
783 inline generic_wide_int <storage>::generic_wide_int () {}
785 template <typename storage>
786 template <typename T>
787 inline generic_wide_int <storage>::generic_wide_int (const T &x)
788 : storage (x)
792 template <typename storage>
793 template <typename T>
794 inline generic_wide_int <storage>::generic_wide_int (const T &x,
795 unsigned int precision)
796 : storage (x, precision)
800 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
801 If THIS does not fit in PRECISION, the information is lost. */
802 template <typename storage>
803 inline HOST_WIDE_INT
804 generic_wide_int <storage>::to_shwi (unsigned int precision) const
806 if (precision < HOST_BITS_PER_WIDE_INT)
807 return sext_hwi (this->get_val ()[0], precision);
808 else
809 return this->get_val ()[0];
812 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
813 template <typename storage>
814 inline HOST_WIDE_INT
815 generic_wide_int <storage>::to_shwi () const
817 if (is_sign_extended)
818 return this->get_val ()[0];
819 else
820 return to_shwi (this->get_precision ());
823 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
824 PRECISION. If THIS does not fit in PRECISION, the information
825 is lost. */
826 template <typename storage>
827 inline unsigned HOST_WIDE_INT
828 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
830 if (precision < HOST_BITS_PER_WIDE_INT)
831 return zext_hwi (this->get_val ()[0], precision);
832 else
833 return this->get_val ()[0];
836 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
837 template <typename storage>
838 inline unsigned HOST_WIDE_INT
839 generic_wide_int <storage>::to_uhwi () const
841 return to_uhwi (this->get_precision ());
844 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
845 represent addresses to using offset_int to represent addresses.
846 We use to_short_addr at the interface from new code to old,
847 unconverted code. */
848 template <typename storage>
849 inline HOST_WIDE_INT
850 generic_wide_int <storage>::to_short_addr () const
852 return this->get_val ()[0];
855 /* Return the implicit value of blocks above get_len (). */
856 template <typename storage>
857 inline HOST_WIDE_INT
858 generic_wide_int <storage>::sign_mask () const
860 unsigned int len = this->get_len ();
861 gcc_assert (len > 0);
863 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
864 if (!is_sign_extended)
866 unsigned int precision = this->get_precision ();
867 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
868 if (excess > 0)
869 high <<= excess;
871 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
874 /* Return the signed value of the least-significant explicitly-encoded
875 block. */
876 template <typename storage>
877 inline HOST_WIDE_INT
878 generic_wide_int <storage>::slow () const
880 return this->get_val ()[0];
883 /* Return the signed value of the most-significant explicitly-encoded
884 block. */
885 template <typename storage>
886 inline HOST_WIDE_INT
887 generic_wide_int <storage>::shigh () const
889 return this->get_val ()[this->get_len () - 1];
892 /* Return the unsigned value of the least-significant
893 explicitly-encoded block. */
894 template <typename storage>
895 inline unsigned HOST_WIDE_INT
896 generic_wide_int <storage>::ulow () const
898 return this->get_val ()[0];
901 /* Return the unsigned value of the most-significant
902 explicitly-encoded block. */
903 template <typename storage>
904 inline unsigned HOST_WIDE_INT
905 generic_wide_int <storage>::uhigh () const
907 return this->get_val ()[this->get_len () - 1];
910 /* Return block I, which might be implicitly or explicit encoded. */
911 template <typename storage>
912 inline HOST_WIDE_INT
913 generic_wide_int <storage>::elt (unsigned int i) const
915 if (i >= this->get_len ())
916 return sign_mask ();
917 else
918 return this->get_val ()[i];
921 /* Like elt, but sign-extend beyond the upper bit, instead of returning
922 the raw encoding. */
923 template <typename storage>
924 inline HOST_WIDE_INT
925 generic_wide_int <storage>::sext_elt (unsigned int i) const
927 HOST_WIDE_INT elt_i = elt (i);
928 if (!is_sign_extended)
930 unsigned int precision = this->get_precision ();
931 unsigned int lsb = i * HOST_BITS_PER_WIDE_INT;
932 if (precision - lsb < HOST_BITS_PER_WIDE_INT)
933 elt_i = sext_hwi (elt_i, precision - lsb);
935 return elt_i;
938 template <typename storage>
939 template <typename T>
940 inline generic_wide_int <storage> &
941 generic_wide_int <storage>::operator = (const T &x)
943 storage::operator = (x);
944 return *this;
947 /* Dump the contents of the integer to stderr, for debugging. */
948 template <typename storage>
949 void
950 generic_wide_int <storage>::dump () const
952 unsigned int len = this->get_len ();
953 const HOST_WIDE_INT *val = this->get_val ();
954 unsigned int precision = this->get_precision ();
955 fprintf (stderr, "[");
956 if (len * HOST_BITS_PER_WIDE_INT < precision)
957 fprintf (stderr, "...,");
958 for (unsigned int i = 0; i < len - 1; ++i)
959 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
960 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
961 val[0], precision);
964 namespace wi
966 template <typename storage>
967 struct int_traits < generic_wide_int <storage> >
968 : public wi::int_traits <storage>
970 static unsigned int get_precision (const generic_wide_int <storage> &);
971 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
972 const generic_wide_int <storage> &);
976 template <typename storage>
977 inline unsigned int
978 wi::int_traits < generic_wide_int <storage> >::
979 get_precision (const generic_wide_int <storage> &x)
981 return x.get_precision ();
984 template <typename storage>
985 inline wi::storage_ref
986 wi::int_traits < generic_wide_int <storage> >::
987 decompose (HOST_WIDE_INT *, unsigned int precision,
988 const generic_wide_int <storage> &x)
990 gcc_checking_assert (precision == x.get_precision ());
991 return wi::storage_ref (x.get_val (), x.get_len (), precision);
994 /* Provide the storage for a wide_int_ref. This acts like a read-only
995 wide_int, with the optimization that VAL is normally a pointer to
996 another integer's storage, so that no array copy is needed. */
997 template <bool SE, bool HDP>
998 class wide_int_ref_storage : public wi::storage_ref
1000 private:
1001 /* Scratch space that can be used when decomposing the original integer.
1002 It must live as long as this object. */
1003 HOST_WIDE_INT scratch[2];
1005 public:
1006 wide_int_ref_storage () {}
1008 wide_int_ref_storage (const wi::storage_ref &);
1010 template <typename T>
1011 wide_int_ref_storage (const T &);
1013 template <typename T>
1014 wide_int_ref_storage (const T &, unsigned int);
1017 /* Create a reference from an existing reference. */
1018 template <bool SE, bool HDP>
1019 inline wide_int_ref_storage <SE, HDP>::
1020 wide_int_ref_storage (const wi::storage_ref &x)
1021 : storage_ref (x)
1024 /* Create a reference to integer X in its natural precision. Note
1025 that the natural precision is host-dependent for primitive
1026 types. */
1027 template <bool SE, bool HDP>
1028 template <typename T>
1029 inline wide_int_ref_storage <SE, HDP>::wide_int_ref_storage (const T &x)
1030 : storage_ref (wi::int_traits <T>::decompose (scratch,
1031 wi::get_precision (x), x))
1035 /* Create a reference to integer X in precision PRECISION. */
1036 template <bool SE, bool HDP>
1037 template <typename T>
1038 inline wide_int_ref_storage <SE, HDP>::
1039 wide_int_ref_storage (const T &x, unsigned int precision)
1040 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
1044 namespace wi
1046 template <bool SE, bool HDP>
1047 struct int_traits <wide_int_ref_storage <SE, HDP> >
1049 static const enum precision_type precision_type = VAR_PRECISION;
1050 static const bool host_dependent_precision = HDP;
1051 static const bool is_sign_extended = SE;
1055 namespace wi
1057 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1058 unsigned int, unsigned int, unsigned int,
1059 signop sgn);
1060 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1061 unsigned int, unsigned int, bool = true);
1064 /* The storage used by wide_int. */
1065 class GTY(()) wide_int_storage
1067 private:
1068 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
1069 unsigned int len;
1070 unsigned int precision;
1072 public:
1073 wide_int_storage ();
1074 template <typename T>
1075 wide_int_storage (const T &);
1077 /* The standard generic_wide_int storage methods. */
1078 unsigned int get_precision () const;
1079 const HOST_WIDE_INT *get_val () const;
1080 unsigned int get_len () const;
1081 HOST_WIDE_INT *write_val ();
1082 void set_len (unsigned int, bool = false);
1084 template <typename T>
1085 wide_int_storage &operator = (const T &);
1087 static wide_int from (const wide_int_ref &, unsigned int, signop);
1088 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1089 unsigned int, bool = true);
1090 static wide_int create (unsigned int);
1093 namespace wi
1095 template <>
1096 struct int_traits <wide_int_storage>
1098 static const enum precision_type precision_type = VAR_PRECISION;
1099 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1100 static const bool host_dependent_precision = false;
1101 static const bool is_sign_extended = true;
1102 template <typename T1, typename T2>
1103 static wide_int get_binary_result (const T1 &, const T2 &);
1107 inline wide_int_storage::wide_int_storage () {}
1109 /* Initialize the storage from integer X, in its natural precision.
1110 Note that we do not allow integers with host-dependent precision
1111 to become wide_ints; wide_ints must always be logically independent
1112 of the host. */
1113 template <typename T>
1114 inline wide_int_storage::wide_int_storage (const T &x)
1116 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1117 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1118 WIDE_INT_REF_FOR (T) xi (x);
1119 precision = xi.precision;
1120 wi::copy (*this, xi);
1123 template <typename T>
1124 inline wide_int_storage&
1125 wide_int_storage::operator = (const T &x)
1127 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1128 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1129 WIDE_INT_REF_FOR (T) xi (x);
1130 precision = xi.precision;
1131 wi::copy (*this, xi);
1132 return *this;
1135 inline unsigned int
1136 wide_int_storage::get_precision () const
1138 return precision;
1141 inline const HOST_WIDE_INT *
1142 wide_int_storage::get_val () const
1144 return val;
1147 inline unsigned int
1148 wide_int_storage::get_len () const
1150 return len;
1153 inline HOST_WIDE_INT *
1154 wide_int_storage::write_val ()
1156 return val;
1159 inline void
1160 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1162 len = l;
1163 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1164 val[len - 1] = sext_hwi (val[len - 1],
1165 precision % HOST_BITS_PER_WIDE_INT);
1168 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1169 number. */
1170 inline wide_int
1171 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1172 signop sgn)
1174 wide_int result = wide_int::create (precision);
1175 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1176 x.precision, precision, sgn));
1177 return result;
1180 /* Create a wide_int from the explicit block encoding given by VAL and
1181 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1182 true if the encoding may have redundant trailing blocks. */
1183 inline wide_int
1184 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1185 unsigned int precision, bool need_canon_p)
1187 wide_int result = wide_int::create (precision);
1188 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1189 need_canon_p));
1190 return result;
1193 /* Return an uninitialized wide_int with precision PRECISION. */
1194 inline wide_int
1195 wide_int_storage::create (unsigned int precision)
1197 wide_int x;
1198 x.precision = precision;
1199 return x;
1202 template <typename T1, typename T2>
1203 inline wide_int
1204 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1206 /* This shouldn't be used for two flexible-precision inputs. */
1207 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1208 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1209 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1210 return wide_int::create (wi::get_precision (y));
1211 else
1212 return wide_int::create (wi::get_precision (x));
1215 /* The storage used by FIXED_WIDE_INT (N). */
1216 template <int N>
1217 class GTY(()) fixed_wide_int_storage
1219 private:
1220 HOST_WIDE_INT val[WIDE_INT_MAX_HWIS (N)];
1221 unsigned int len;
1223 public:
1224 fixed_wide_int_storage ();
1225 template <typename T>
1226 fixed_wide_int_storage (const T &);
1228 /* The standard generic_wide_int storage methods. */
1229 unsigned int get_precision () const;
1230 const HOST_WIDE_INT *get_val () const;
1231 unsigned int get_len () const;
1232 HOST_WIDE_INT *write_val ();
1233 void set_len (unsigned int, bool = false);
1235 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1236 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1237 bool = true);
1240 namespace wi
1242 template <int N>
1243 struct int_traits < fixed_wide_int_storage <N> >
1245 static const enum precision_type precision_type = CONST_PRECISION;
1246 static const bool host_dependent_precision = false;
1247 static const bool is_sign_extended = true;
1248 static const unsigned int precision = N;
1249 template <typename T1, typename T2>
1250 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1254 template <int N>
1255 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1257 /* Initialize the storage from integer X, in precision N. */
1258 template <int N>
1259 template <typename T>
1260 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1262 /* Check for type compatibility. We don't want to initialize a
1263 fixed-width integer from something like a wide_int. */
1264 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1265 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1268 template <int N>
1269 inline unsigned int
1270 fixed_wide_int_storage <N>::get_precision () const
1272 return N;
1275 template <int N>
1276 inline const HOST_WIDE_INT *
1277 fixed_wide_int_storage <N>::get_val () const
1279 return val;
1282 template <int N>
1283 inline unsigned int
1284 fixed_wide_int_storage <N>::get_len () const
1286 return len;
1289 template <int N>
1290 inline HOST_WIDE_INT *
1291 fixed_wide_int_storage <N>::write_val ()
1293 return val;
1296 template <int N>
1297 inline void
1298 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1300 len = l;
1301 /* There are no excess bits in val[len - 1]. */
1302 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1305 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1306 template <int N>
1307 inline FIXED_WIDE_INT (N)
1308 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1310 FIXED_WIDE_INT (N) result;
1311 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1312 x.precision, N, sgn));
1313 return result;
1316 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1317 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1318 trailing blocks. */
1319 template <int N>
1320 inline FIXED_WIDE_INT (N)
1321 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1322 unsigned int len,
1323 bool need_canon_p)
1325 FIXED_WIDE_INT (N) result;
1326 result.set_len (wi::from_array (result.write_val (), val, len,
1327 N, need_canon_p));
1328 return result;
1331 template <int N>
1332 template <typename T1, typename T2>
1333 inline FIXED_WIDE_INT (N)
1334 wi::int_traits < fixed_wide_int_storage <N> >::
1335 get_binary_result (const T1 &, const T2 &)
1337 return FIXED_WIDE_INT (N) ();
1340 /* A reference to one element of a trailing_wide_ints structure. */
1341 class trailing_wide_int_storage
1343 private:
1344 /* The precision of the integer, which is a fixed property of the
1345 parent trailing_wide_ints. */
1346 unsigned int m_precision;
1348 /* A pointer to the length field. */
1349 unsigned char *m_len;
1351 /* A pointer to the HWI array. There are enough elements to hold all
1352 values of precision M_PRECISION. */
1353 HOST_WIDE_INT *m_val;
1355 public:
1356 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1358 /* The standard generic_wide_int storage methods. */
1359 unsigned int get_len () const;
1360 unsigned int get_precision () const;
1361 const HOST_WIDE_INT *get_val () const;
1362 HOST_WIDE_INT *write_val ();
1363 void set_len (unsigned int, bool = false);
1365 template <typename T>
1366 trailing_wide_int_storage &operator = (const T &);
1369 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1371 /* trailing_wide_int behaves like a wide_int. */
1372 namespace wi
1374 template <>
1375 struct int_traits <trailing_wide_int_storage>
1376 : public int_traits <wide_int_storage> {};
1379 /* A variable-length array of wide_int-like objects that can be put
1380 at the end of a variable-sized structure. The number of objects is
1381 at most N and can be set at runtime by using set_precision().
1383 Use extra_size to calculate how many bytes beyond the
1384 sizeof need to be allocated. Use set_precision to initialize the
1385 structure. */
1386 template <int N>
1387 struct GTY((user)) trailing_wide_ints
1389 private:
1390 /* The shared precision of each number. */
1391 unsigned short m_precision;
1393 /* The shared maximum length of each number. */
1394 unsigned char m_max_len;
1396 /* The number of elements. */
1397 unsigned char m_num_elements;
1399 /* The current length of each number.
1400 Avoid char array so the whole structure is not a typeless storage
1401 that will, in turn, turn off TBAA on gimple, trees and RTL. */
1402 struct {unsigned char len;} m_len[N];
1404 /* The variable-length part of the structure, which always contains
1405 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1406 HOST_WIDE_INT m_val[1];
1408 public:
1409 typedef WIDE_INT_REF_FOR (trailing_wide_int_storage) const_reference;
1411 void set_precision (unsigned int precision, unsigned int num_elements = N);
1412 unsigned int get_precision () const { return m_precision; }
1413 unsigned int num_elements () const { return m_num_elements; }
1414 trailing_wide_int operator [] (unsigned int);
1415 const_reference operator [] (unsigned int) const;
1416 static size_t extra_size (unsigned int precision,
1417 unsigned int num_elements = N);
1418 size_t extra_size () const { return extra_size (m_precision,
1419 m_num_elements); }
1422 inline trailing_wide_int_storage::
1423 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1424 HOST_WIDE_INT *val)
1425 : m_precision (precision), m_len (len), m_val (val)
1429 inline unsigned int
1430 trailing_wide_int_storage::get_len () const
1432 return *m_len;
1435 inline unsigned int
1436 trailing_wide_int_storage::get_precision () const
1438 return m_precision;
1441 inline const HOST_WIDE_INT *
1442 trailing_wide_int_storage::get_val () const
1444 return m_val;
1447 inline HOST_WIDE_INT *
1448 trailing_wide_int_storage::write_val ()
1450 return m_val;
1453 inline void
1454 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1456 *m_len = len;
1457 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1458 m_val[len - 1] = sext_hwi (m_val[len - 1],
1459 m_precision % HOST_BITS_PER_WIDE_INT);
1462 template <typename T>
1463 inline trailing_wide_int_storage &
1464 trailing_wide_int_storage::operator = (const T &x)
1466 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1467 wi::copy (*this, xi);
1468 return *this;
1471 /* Initialize the structure and record that all elements have precision
1472 PRECISION. NUM_ELEMENTS can be no more than N. */
1473 template <int N>
1474 inline void
1475 trailing_wide_ints <N>::set_precision (unsigned int precision,
1476 unsigned int num_elements)
1478 gcc_checking_assert (num_elements <= N);
1479 m_num_elements = num_elements;
1480 m_precision = precision;
1481 m_max_len = WIDE_INT_MAX_HWIS (precision);
1484 /* Return a reference to element INDEX. */
1485 template <int N>
1486 inline trailing_wide_int
1487 trailing_wide_ints <N>::operator [] (unsigned int index)
1489 return trailing_wide_int_storage (m_precision, &m_len[index].len,
1490 &m_val[index * m_max_len]);
1493 template <int N>
1494 inline typename trailing_wide_ints <N>::const_reference
1495 trailing_wide_ints <N>::operator [] (unsigned int index) const
1497 return wi::storage_ref (&m_val[index * m_max_len],
1498 m_len[index].len, m_precision);
1501 /* Return how many extra bytes need to be added to the end of the
1502 structure in order to handle NUM_ELEMENTS wide_ints of precision
1503 PRECISION. NUM_ELEMENTS is the number of elements, and defaults
1504 to N. */
1505 template <int N>
1506 inline size_t
1507 trailing_wide_ints <N>::extra_size (unsigned int precision,
1508 unsigned int num_elements)
1510 unsigned int max_len = WIDE_INT_MAX_HWIS (precision);
1511 gcc_checking_assert (num_elements <= N);
1512 return (num_elements * max_len - 1) * sizeof (HOST_WIDE_INT);
1515 /* This macro is used in structures that end with a trailing_wide_ints field
1516 called FIELD. It declares get_NAME() and set_NAME() methods to access
1517 element I of FIELD. */
1518 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1519 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1520 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1522 namespace wi
1524 /* Implementation of int_traits for primitive integer types like "int". */
1525 template <typename T, bool signed_p>
1526 struct primitive_int_traits
1528 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1529 static const bool host_dependent_precision = true;
1530 static const bool is_sign_extended = true;
1531 static unsigned int get_precision (T);
1532 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1536 template <typename T, bool signed_p>
1537 inline unsigned int
1538 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1540 return sizeof (T) * CHAR_BIT;
1543 template <typename T, bool signed_p>
1544 inline wi::storage_ref
1545 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1546 unsigned int precision, T x)
1548 scratch[0] = x;
1549 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1550 return wi::storage_ref (scratch, 1, precision);
1551 scratch[1] = 0;
1552 return wi::storage_ref (scratch, 2, precision);
1555 /* Allow primitive C types to be used in wi:: routines. */
1556 namespace wi
1558 template <>
1559 struct int_traits <unsigned char>
1560 : public primitive_int_traits <unsigned char, false> {};
1562 template <>
1563 struct int_traits <unsigned short>
1564 : public primitive_int_traits <unsigned short, false> {};
1566 template <>
1567 struct int_traits <int>
1568 : public primitive_int_traits <int, true> {};
1570 template <>
1571 struct int_traits <unsigned int>
1572 : public primitive_int_traits <unsigned int, false> {};
1574 template <>
1575 struct int_traits <long>
1576 : public primitive_int_traits <long, true> {};
1578 template <>
1579 struct int_traits <unsigned long>
1580 : public primitive_int_traits <unsigned long, false> {};
1582 #if defined HAVE_LONG_LONG
1583 template <>
1584 struct int_traits <long long>
1585 : public primitive_int_traits <long long, true> {};
1587 template <>
1588 struct int_traits <unsigned long long>
1589 : public primitive_int_traits <unsigned long long, false> {};
1590 #endif
1593 namespace wi
1595 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1596 and precision PRECISION. */
1597 class hwi_with_prec
1599 public:
1600 hwi_with_prec () {}
1601 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1602 HOST_WIDE_INT val;
1603 unsigned int precision;
1604 signop sgn;
1607 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1608 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1610 hwi_with_prec minus_one (unsigned int);
1611 hwi_with_prec zero (unsigned int);
1612 hwi_with_prec one (unsigned int);
1613 hwi_with_prec two (unsigned int);
1616 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1617 signop s)
1618 : precision (p), sgn (s)
1620 if (precision < HOST_BITS_PER_WIDE_INT)
1621 val = sext_hwi (v, precision);
1622 else
1623 val = v;
1626 /* Return a signed integer that has value VAL and precision PRECISION. */
1627 inline wi::hwi_with_prec
1628 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1630 return hwi_with_prec (val, precision, SIGNED);
1633 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1634 inline wi::hwi_with_prec
1635 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1637 return hwi_with_prec (val, precision, UNSIGNED);
1640 /* Return a wide int of -1 with precision PRECISION. */
1641 inline wi::hwi_with_prec
1642 wi::minus_one (unsigned int precision)
1644 return wi::shwi (-1, precision);
1647 /* Return a wide int of 0 with precision PRECISION. */
1648 inline wi::hwi_with_prec
1649 wi::zero (unsigned int precision)
1651 return wi::shwi (0, precision);
1654 /* Return a wide int of 1 with precision PRECISION. */
1655 inline wi::hwi_with_prec
1656 wi::one (unsigned int precision)
1658 return wi::shwi (1, precision);
1661 /* Return a wide int of 2 with precision PRECISION. */
1662 inline wi::hwi_with_prec
1663 wi::two (unsigned int precision)
1665 return wi::shwi (2, precision);
1668 namespace wi
1670 /* ints_for<T>::zero (X) returns a zero that, when asssigned to a T,
1671 gives that T the same precision as X. */
1672 template<typename T, precision_type = int_traits<T>::precision_type>
1673 struct ints_for
1675 static int zero (const T &) { return 0; }
1678 template<typename T>
1679 struct ints_for<T, VAR_PRECISION>
1681 static hwi_with_prec zero (const T &);
1685 template<typename T>
1686 inline wi::hwi_with_prec
1687 wi::ints_for<T, wi::VAR_PRECISION>::zero (const T &x)
1689 return wi::zero (wi::get_precision (x));
1692 namespace wi
1694 template <>
1695 struct int_traits <wi::hwi_with_prec>
1697 static const enum precision_type precision_type = VAR_PRECISION;
1698 /* hwi_with_prec has an explicitly-given precision, rather than the
1699 precision of HOST_WIDE_INT. */
1700 static const bool host_dependent_precision = false;
1701 static const bool is_sign_extended = true;
1702 static unsigned int get_precision (const wi::hwi_with_prec &);
1703 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1704 const wi::hwi_with_prec &);
1708 inline unsigned int
1709 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1711 return x.precision;
1714 inline wi::storage_ref
1715 wi::int_traits <wi::hwi_with_prec>::
1716 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1717 const wi::hwi_with_prec &x)
1719 gcc_checking_assert (precision == x.precision);
1720 scratch[0] = x.val;
1721 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1722 return wi::storage_ref (scratch, 1, precision);
1723 scratch[1] = 0;
1724 return wi::storage_ref (scratch, 2, precision);
1727 /* Private functions for handling large cases out of line. They take
1728 individual length and array parameters because that is cheaper for
1729 the inline caller than constructing an object on the stack and
1730 passing a reference to it. (Although many callers use wide_int_refs,
1731 we generally want those to be removed by SRA.) */
1732 namespace wi
1734 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1735 const HOST_WIDE_INT *, unsigned int, unsigned int);
1736 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1737 const HOST_WIDE_INT *, unsigned int);
1738 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1739 const HOST_WIDE_INT *, unsigned int);
1740 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1741 const HOST_WIDE_INT *, unsigned int);
1742 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1743 const HOST_WIDE_INT *, unsigned int);
1744 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1745 unsigned int, unsigned int, unsigned int);
1746 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1747 unsigned int, unsigned int, unsigned int);
1748 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1749 unsigned int, unsigned int, unsigned int);
1750 unsigned int bswap_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1751 unsigned int, unsigned int);
1752 unsigned int bitreverse_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1753 unsigned int, unsigned int);
1755 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1756 unsigned int, unsigned int, unsigned int);
1757 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1758 unsigned int, unsigned int, unsigned int,
1759 unsigned int);
1760 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1761 unsigned int, unsigned int, unsigned int,
1762 unsigned int);
1763 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1764 const HOST_WIDE_INT *, unsigned int, unsigned int);
1765 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1766 unsigned int, const HOST_WIDE_INT *,
1767 unsigned int, unsigned int);
1768 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1769 const HOST_WIDE_INT *, unsigned int, unsigned int);
1770 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1771 unsigned int, const HOST_WIDE_INT *,
1772 unsigned int, unsigned int);
1773 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1774 const HOST_WIDE_INT *, unsigned int, unsigned int);
1775 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1776 const HOST_WIDE_INT *, unsigned int, unsigned int,
1777 signop, overflow_type *);
1778 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1779 const HOST_WIDE_INT *, unsigned int, unsigned int,
1780 signop, overflow_type *);
1781 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1782 unsigned int, const HOST_WIDE_INT *,
1783 unsigned int, unsigned int, signop,
1784 overflow_type *, bool);
1785 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1786 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1787 unsigned int, unsigned int,
1788 const HOST_WIDE_INT *,
1789 unsigned int, unsigned int,
1790 signop, overflow_type *);
1793 /* Return the number of bits that integer X can hold. */
1794 template <typename T>
1795 inline unsigned int
1796 wi::get_precision (const T &x)
1798 return wi::int_traits <T>::get_precision (x);
1801 /* Return the number of bits that the result of a binary operation can
1802 hold when the input operands are X and Y. */
1803 template <typename T1, typename T2>
1804 inline unsigned int
1805 wi::get_binary_precision (const T1 &x, const T2 &y)
1807 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1808 get_binary_result (x, y));
1811 /* Copy the contents of Y to X, but keeping X's current precision. */
1812 template <typename T1, typename T2>
1813 inline void
1814 wi::copy (T1 &x, const T2 &y)
1816 HOST_WIDE_INT *xval = x.write_val ();
1817 const HOST_WIDE_INT *yval = y.get_val ();
1818 unsigned int len = y.get_len ();
1819 unsigned int i = 0;
1821 xval[i] = yval[i];
1822 while (++i < len);
1823 x.set_len (len, y.is_sign_extended);
1826 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1827 template <typename T>
1828 inline bool
1829 wi::fits_shwi_p (const T &x)
1831 WIDE_INT_REF_FOR (T) xi (x);
1832 return xi.len == 1;
1835 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1836 precision. */
1837 template <typename T>
1838 inline bool
1839 wi::fits_uhwi_p (const T &x)
1841 WIDE_INT_REF_FOR (T) xi (x);
1842 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1843 return true;
1844 if (xi.len == 1)
1845 return xi.slow () >= 0;
1846 return xi.len == 2 && xi.uhigh () == 0;
1849 /* Return true if X is negative based on the interpretation of SGN.
1850 For UNSIGNED, this is always false. */
1851 template <typename T>
1852 inline bool
1853 wi::neg_p (const T &x, signop sgn)
1855 WIDE_INT_REF_FOR (T) xi (x);
1856 if (sgn == UNSIGNED)
1857 return false;
1858 return xi.sign_mask () < 0;
1861 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1862 template <typename T>
1863 inline HOST_WIDE_INT
1864 wi::sign_mask (const T &x)
1866 WIDE_INT_REF_FOR (T) xi (x);
1867 return xi.sign_mask ();
1870 /* Return true if X == Y. X and Y must be binary-compatible. */
1871 template <typename T1, typename T2>
1872 inline bool
1873 wi::eq_p (const T1 &x, const T2 &y)
1875 unsigned int precision = get_binary_precision (x, y);
1876 WIDE_INT_REF_FOR (T1) xi (x, precision);
1877 WIDE_INT_REF_FOR (T2) yi (y, precision);
1878 if (xi.is_sign_extended && yi.is_sign_extended)
1880 /* This case reduces to array equality. */
1881 if (xi.len != yi.len)
1882 return false;
1883 unsigned int i = 0;
1885 if (xi.val[i] != yi.val[i])
1886 return false;
1887 while (++i != xi.len);
1888 return true;
1890 if (LIKELY (yi.len == 1))
1892 /* XI is only equal to YI if it too has a single HWI. */
1893 if (xi.len != 1)
1894 return false;
1895 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1896 with 0 are simple. */
1897 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1898 return xi.val[0] == 0;
1899 /* Otherwise flush out any excess bits first. */
1900 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1901 int excess = HOST_BITS_PER_WIDE_INT - precision;
1902 if (excess > 0)
1903 diff <<= excess;
1904 return diff == 0;
1906 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1909 /* Return true if X != Y. X and Y must be binary-compatible. */
1910 template <typename T1, typename T2>
1911 inline bool
1912 wi::ne_p (const T1 &x, const T2 &y)
1914 return !eq_p (x, y);
1917 /* Return true if X < Y when both are treated as signed values. */
1918 template <typename T1, typename T2>
1919 inline bool
1920 wi::lts_p (const T1 &x, const T2 &y)
1922 unsigned int precision = get_binary_precision (x, y);
1923 WIDE_INT_REF_FOR (T1) xi (x, precision);
1924 WIDE_INT_REF_FOR (T2) yi (y, precision);
1925 /* We optimize x < y, where y is 64 or fewer bits. */
1926 if (wi::fits_shwi_p (yi))
1928 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1929 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1930 return neg_p (xi);
1931 /* If x fits directly into a shwi, we can compare directly. */
1932 if (wi::fits_shwi_p (xi))
1933 return xi.to_shwi () < yi.to_shwi ();
1934 /* If x doesn't fit and is negative, then it must be more
1935 negative than any value in y, and hence smaller than y. */
1936 if (neg_p (xi))
1937 return true;
1938 /* If x is positive, then it must be larger than any value in y,
1939 and hence greater than y. */
1940 return false;
1942 /* Optimize the opposite case, if it can be detected at compile time. */
1943 if (STATIC_CONSTANT_P (xi.len == 1))
1944 /* If YI is negative it is lower than the least HWI.
1945 If YI is positive it is greater than the greatest HWI. */
1946 return !neg_p (yi);
1947 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1950 /* Return true if X < Y when both are treated as unsigned values. */
1951 template <typename T1, typename T2>
1952 inline bool
1953 wi::ltu_p (const T1 &x, const T2 &y)
1955 unsigned int precision = get_binary_precision (x, y);
1956 WIDE_INT_REF_FOR (T1) xi (x, precision);
1957 WIDE_INT_REF_FOR (T2) yi (y, precision);
1958 /* Optimize comparisons with constants. */
1959 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1960 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1961 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1962 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1963 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1964 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1965 values does not change the result. */
1966 if (LIKELY (xi.len + yi.len == 2))
1968 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1969 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1970 return xl < yl;
1972 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1975 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1976 template <typename T1, typename T2>
1977 inline bool
1978 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1980 if (sgn == SIGNED)
1981 return lts_p (x, y);
1982 else
1983 return ltu_p (x, y);
1986 /* Return true if X <= Y when both are treated as signed values. */
1987 template <typename T1, typename T2>
1988 inline bool
1989 wi::les_p (const T1 &x, const T2 &y)
1991 return !lts_p (y, x);
1994 /* Return true if X <= Y when both are treated as unsigned values. */
1995 template <typename T1, typename T2>
1996 inline bool
1997 wi::leu_p (const T1 &x, const T2 &y)
1999 return !ltu_p (y, x);
2002 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
2003 template <typename T1, typename T2>
2004 inline bool
2005 wi::le_p (const T1 &x, const T2 &y, signop sgn)
2007 if (sgn == SIGNED)
2008 return les_p (x, y);
2009 else
2010 return leu_p (x, y);
2013 /* Return true if X > Y when both are treated as signed values. */
2014 template <typename T1, typename T2>
2015 inline bool
2016 wi::gts_p (const T1 &x, const T2 &y)
2018 return lts_p (y, x);
2021 /* Return true if X > Y when both are treated as unsigned values. */
2022 template <typename T1, typename T2>
2023 inline bool
2024 wi::gtu_p (const T1 &x, const T2 &y)
2026 return ltu_p (y, x);
2029 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
2030 template <typename T1, typename T2>
2031 inline bool
2032 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
2034 if (sgn == SIGNED)
2035 return gts_p (x, y);
2036 else
2037 return gtu_p (x, y);
2040 /* Return true if X >= Y when both are treated as signed values. */
2041 template <typename T1, typename T2>
2042 inline bool
2043 wi::ges_p (const T1 &x, const T2 &y)
2045 return !lts_p (x, y);
2048 /* Return true if X >= Y when both are treated as unsigned values. */
2049 template <typename T1, typename T2>
2050 inline bool
2051 wi::geu_p (const T1 &x, const T2 &y)
2053 return !ltu_p (x, y);
2056 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
2057 template <typename T1, typename T2>
2058 inline bool
2059 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
2061 if (sgn == SIGNED)
2062 return ges_p (x, y);
2063 else
2064 return geu_p (x, y);
2067 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2068 as signed values. */
2069 template <typename T1, typename T2>
2070 inline int
2071 wi::cmps (const T1 &x, const T2 &y)
2073 unsigned int precision = get_binary_precision (x, y);
2074 WIDE_INT_REF_FOR (T1) xi (x, precision);
2075 WIDE_INT_REF_FOR (T2) yi (y, precision);
2076 if (wi::fits_shwi_p (yi))
2078 /* Special case for comparisons with 0. */
2079 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2080 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
2081 /* If x fits into a signed HWI, we can compare directly. */
2082 if (wi::fits_shwi_p (xi))
2084 HOST_WIDE_INT xl = xi.to_shwi ();
2085 HOST_WIDE_INT yl = yi.to_shwi ();
2086 return xl < yl ? -1 : xl > yl;
2088 /* If x doesn't fit and is negative, then it must be more
2089 negative than any signed HWI, and hence smaller than y. */
2090 if (neg_p (xi))
2091 return -1;
2092 /* If x is positive, then it must be larger than any signed HWI,
2093 and hence greater than y. */
2094 return 1;
2096 /* Optimize the opposite case, if it can be detected at compile time. */
2097 if (STATIC_CONSTANT_P (xi.len == 1))
2098 /* If YI is negative it is lower than the least HWI.
2099 If YI is positive it is greater than the greatest HWI. */
2100 return neg_p (yi) ? 1 : -1;
2101 return cmps_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. Treat both X and Y
2105 as unsigned values. */
2106 template <typename T1, typename T2>
2107 inline int
2108 wi::cmpu (const T1 &x, const T2 &y)
2110 unsigned int precision = get_binary_precision (x, y);
2111 WIDE_INT_REF_FOR (T1) xi (x, precision);
2112 WIDE_INT_REF_FOR (T2) yi (y, precision);
2113 /* Optimize comparisons with constants. */
2114 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2116 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2117 if (xi.len != 1)
2118 return 1;
2119 /* Otherwise compare directly. */
2120 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2121 unsigned HOST_WIDE_INT yl = yi.val[0];
2122 return xl < yl ? -1 : xl > yl;
2124 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2126 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2127 if (yi.len != 1)
2128 return -1;
2129 /* Otherwise compare directly. */
2130 unsigned HOST_WIDE_INT xl = xi.val[0];
2131 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2132 return xl < yl ? -1 : xl > yl;
2134 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2135 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2136 values does not change the result. */
2137 if (LIKELY (xi.len + yi.len == 2))
2139 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2140 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2141 return xl < yl ? -1 : xl > yl;
2143 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2146 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2147 X and Y indicated by SGN. */
2148 template <typename T1, typename T2>
2149 inline int
2150 wi::cmp (const T1 &x, const T2 &y, signop sgn)
2152 if (sgn == SIGNED)
2153 return cmps (x, y);
2154 else
2155 return cmpu (x, y);
2158 /* Return ~x. */
2159 template <typename T>
2160 inline WI_UNARY_RESULT (T)
2161 wi::bit_not (const T &x)
2163 WI_UNARY_RESULT_VAR (result, val, T, x);
2164 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2165 for (unsigned int i = 0; i < xi.len; ++i)
2166 val[i] = ~xi.val[i];
2167 result.set_len (xi.len);
2168 return result;
2171 /* Return -x. */
2172 template <typename T>
2173 inline WI_UNARY_RESULT (T)
2174 wi::neg (const T &x)
2176 return sub (0, x);
2179 /* Return -x. Indicate in *OVERFLOW if performing the negation would
2180 cause an overflow. */
2181 template <typename T>
2182 inline WI_UNARY_RESULT (T)
2183 wi::neg (const T &x, overflow_type *overflow)
2185 *overflow = only_sign_bit_p (x) ? OVF_OVERFLOW : OVF_NONE;
2186 return sub (0, x);
2189 /* Return the absolute value of x. */
2190 template <typename T>
2191 inline WI_UNARY_RESULT (T)
2192 wi::abs (const T &x)
2194 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2197 /* Return the result of sign-extending the low OFFSET bits of X. */
2198 template <typename T>
2199 inline WI_UNARY_RESULT (T)
2200 wi::sext (const T &x, unsigned int offset)
2202 WI_UNARY_RESULT_VAR (result, val, T, x);
2203 unsigned int precision = get_precision (result);
2204 WIDE_INT_REF_FOR (T) xi (x, precision);
2206 if (offset <= HOST_BITS_PER_WIDE_INT)
2208 val[0] = sext_hwi (xi.ulow (), offset);
2209 result.set_len (1, true);
2211 else
2212 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2213 return result;
2216 /* Return the result of zero-extending the low OFFSET bits of X. */
2217 template <typename T>
2218 inline WI_UNARY_RESULT (T)
2219 wi::zext (const T &x, unsigned int offset)
2221 WI_UNARY_RESULT_VAR (result, val, T, x);
2222 unsigned int precision = get_precision (result);
2223 WIDE_INT_REF_FOR (T) xi (x, precision);
2225 /* This is not just an optimization, it is actually required to
2226 maintain canonization. */
2227 if (offset >= precision)
2229 wi::copy (result, xi);
2230 return result;
2233 /* In these cases we know that at least the top bit will be clear,
2234 so no sign extension is necessary. */
2235 if (offset < HOST_BITS_PER_WIDE_INT)
2237 val[0] = zext_hwi (xi.ulow (), offset);
2238 result.set_len (1, true);
2240 else
2241 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2242 return result;
2245 /* Return the result of extending the low OFFSET bits of X according to
2246 signedness SGN. */
2247 template <typename T>
2248 inline WI_UNARY_RESULT (T)
2249 wi::ext (const T &x, unsigned int offset, signop sgn)
2251 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2254 /* Return an integer that represents X | (1 << bit). */
2255 template <typename T>
2256 inline WI_UNARY_RESULT (T)
2257 wi::set_bit (const T &x, unsigned int bit)
2259 WI_UNARY_RESULT_VAR (result, val, T, x);
2260 unsigned int precision = get_precision (result);
2261 WIDE_INT_REF_FOR (T) xi (x, precision);
2262 if (precision <= HOST_BITS_PER_WIDE_INT)
2264 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2265 result.set_len (1);
2267 else
2268 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2269 return result;
2272 /* Byte swap the integer X.
2273 ??? This always swaps 8-bit octets, regardless of BITS_PER_UNIT.
2274 This function requires X's precision to be a multiple of 16 bits,
2275 so care needs to be taken for targets where BITS_PER_UNIT != 8. */
2276 template <typename T>
2277 inline WI_UNARY_RESULT (T)
2278 wi::bswap (const T &x)
2280 WI_UNARY_RESULT_VAR (result, val, T, x);
2281 unsigned int precision = get_precision (result);
2282 WIDE_INT_REF_FOR (T) xi (x, precision);
2283 result.set_len (bswap_large (val, xi.val, xi.len, precision));
2284 return result;
2287 /* Bitreverse the integer X. */
2288 template <typename T>
2289 inline WI_UNARY_RESULT (T)
2290 wi::bitreverse (const T &x)
2292 WI_UNARY_RESULT_VAR (result, val, T, x);
2293 unsigned int precision = get_precision (result);
2294 WIDE_INT_REF_FOR (T) xi (x, precision);
2295 result.set_len (bitreverse_large (val, xi.val, xi.len, precision));
2296 return result;
2299 /* Return the mininum of X and Y, treating them both as having
2300 signedness SGN. */
2301 template <typename T1, typename T2>
2302 inline WI_BINARY_RESULT (T1, T2)
2303 wi::min (const T1 &x, const T2 &y, signop sgn)
2305 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2306 unsigned int precision = get_precision (result);
2307 if (wi::le_p (x, y, sgn))
2308 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2309 else
2310 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2311 return result;
2314 /* Return the minimum of X and Y, treating both as signed values. */
2315 template <typename T1, typename T2>
2316 inline WI_BINARY_RESULT (T1, T2)
2317 wi::smin (const T1 &x, const T2 &y)
2319 return wi::min (x, y, SIGNED);
2322 /* Return the minimum of X and Y, treating both as unsigned values. */
2323 template <typename T1, typename T2>
2324 inline WI_BINARY_RESULT (T1, T2)
2325 wi::umin (const T1 &x, const T2 &y)
2327 return wi::min (x, y, UNSIGNED);
2330 /* Return the maxinum of X and Y, treating them both as having
2331 signedness SGN. */
2332 template <typename T1, typename T2>
2333 inline WI_BINARY_RESULT (T1, T2)
2334 wi::max (const T1 &x, const T2 &y, signop sgn)
2336 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2337 unsigned int precision = get_precision (result);
2338 if (wi::ge_p (x, y, sgn))
2339 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2340 else
2341 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2342 return result;
2345 /* Return the maximum of X and Y, treating both as signed values. */
2346 template <typename T1, typename T2>
2347 inline WI_BINARY_RESULT (T1, T2)
2348 wi::smax (const T1 &x, const T2 &y)
2350 return wi::max (x, y, SIGNED);
2353 /* Return the maximum of X and Y, treating both as unsigned values. */
2354 template <typename T1, typename T2>
2355 inline WI_BINARY_RESULT (T1, T2)
2356 wi::umax (const T1 &x, const T2 &y)
2358 return wi::max (x, y, UNSIGNED);
2361 /* Return X & Y. */
2362 template <typename T1, typename T2>
2363 inline WI_BINARY_RESULT (T1, T2)
2364 wi::bit_and (const T1 &x, const T2 &y)
2366 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2367 unsigned int precision = get_precision (result);
2368 WIDE_INT_REF_FOR (T1) xi (x, precision);
2369 WIDE_INT_REF_FOR (T2) yi (y, precision);
2370 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2371 if (LIKELY (xi.len + yi.len == 2))
2373 val[0] = xi.ulow () & yi.ulow ();
2374 result.set_len (1, is_sign_extended);
2376 else
2377 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2378 precision), is_sign_extended);
2379 return result;
2382 /* Return X & ~Y. */
2383 template <typename T1, typename T2>
2384 inline WI_BINARY_RESULT (T1, T2)
2385 wi::bit_and_not (const T1 &x, const T2 &y)
2387 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2388 unsigned int precision = get_precision (result);
2389 WIDE_INT_REF_FOR (T1) xi (x, precision);
2390 WIDE_INT_REF_FOR (T2) yi (y, precision);
2391 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2392 if (LIKELY (xi.len + yi.len == 2))
2394 val[0] = xi.ulow () & ~yi.ulow ();
2395 result.set_len (1, is_sign_extended);
2397 else
2398 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2399 precision), is_sign_extended);
2400 return result;
2403 /* Return X | Y. */
2404 template <typename T1, typename T2>
2405 inline WI_BINARY_RESULT (T1, T2)
2406 wi::bit_or (const T1 &x, const T2 &y)
2408 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2409 unsigned int precision = get_precision (result);
2410 WIDE_INT_REF_FOR (T1) xi (x, precision);
2411 WIDE_INT_REF_FOR (T2) yi (y, precision);
2412 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2413 if (LIKELY (xi.len + yi.len == 2))
2415 val[0] = xi.ulow () | yi.ulow ();
2416 result.set_len (1, is_sign_extended);
2418 else
2419 result.set_len (or_large (val, xi.val, xi.len,
2420 yi.val, yi.len, precision), is_sign_extended);
2421 return result;
2424 /* Return X | ~Y. */
2425 template <typename T1, typename T2>
2426 inline WI_BINARY_RESULT (T1, T2)
2427 wi::bit_or_not (const T1 &x, const T2 &y)
2429 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2430 unsigned int precision = get_precision (result);
2431 WIDE_INT_REF_FOR (T1) xi (x, precision);
2432 WIDE_INT_REF_FOR (T2) yi (y, precision);
2433 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2434 if (LIKELY (xi.len + yi.len == 2))
2436 val[0] = xi.ulow () | ~yi.ulow ();
2437 result.set_len (1, is_sign_extended);
2439 else
2440 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2441 precision), is_sign_extended);
2442 return result;
2445 /* Return X ^ Y. */
2446 template <typename T1, typename T2>
2447 inline WI_BINARY_RESULT (T1, T2)
2448 wi::bit_xor (const T1 &x, const T2 &y)
2450 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2451 unsigned int precision = get_precision (result);
2452 WIDE_INT_REF_FOR (T1) xi (x, precision);
2453 WIDE_INT_REF_FOR (T2) yi (y, precision);
2454 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2455 if (LIKELY (xi.len + yi.len == 2))
2457 val[0] = xi.ulow () ^ yi.ulow ();
2458 result.set_len (1, is_sign_extended);
2460 else
2461 result.set_len (xor_large (val, xi.val, xi.len,
2462 yi.val, yi.len, precision), is_sign_extended);
2463 return result;
2466 /* Return X + Y. */
2467 template <typename T1, typename T2>
2468 inline WI_BINARY_RESULT (T1, T2)
2469 wi::add (const T1 &x, const T2 &y)
2471 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2472 unsigned int precision = get_precision (result);
2473 WIDE_INT_REF_FOR (T1) xi (x, precision);
2474 WIDE_INT_REF_FOR (T2) yi (y, precision);
2475 if (precision <= HOST_BITS_PER_WIDE_INT)
2477 val[0] = xi.ulow () + yi.ulow ();
2478 result.set_len (1);
2480 /* If the precision is known at compile time to be greater than
2481 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2482 knowing that (a) all bits in those HWIs are significant and
2483 (b) the result has room for at least two HWIs. This provides
2484 a fast path for things like offset_int and widest_int.
2486 The STATIC_CONSTANT_P test prevents this path from being
2487 used for wide_ints. wide_ints with precisions greater than
2488 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2489 point handling them inline. */
2490 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2491 && LIKELY (xi.len + yi.len == 2))
2493 unsigned HOST_WIDE_INT xl = xi.ulow ();
2494 unsigned HOST_WIDE_INT yl = yi.ulow ();
2495 unsigned HOST_WIDE_INT resultl = xl + yl;
2496 val[0] = resultl;
2497 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2498 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2499 >> (HOST_BITS_PER_WIDE_INT - 1)));
2501 else
2502 result.set_len (add_large (val, xi.val, xi.len,
2503 yi.val, yi.len, precision,
2504 UNSIGNED, 0));
2505 return result;
2508 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2509 and indicate in *OVERFLOW whether the operation overflowed. */
2510 template <typename T1, typename T2>
2511 inline WI_BINARY_RESULT (T1, T2)
2512 wi::add (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2514 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2515 unsigned int precision = get_precision (result);
2516 WIDE_INT_REF_FOR (T1) xi (x, precision);
2517 WIDE_INT_REF_FOR (T2) yi (y, precision);
2518 if (precision <= HOST_BITS_PER_WIDE_INT)
2520 unsigned HOST_WIDE_INT xl = xi.ulow ();
2521 unsigned HOST_WIDE_INT yl = yi.ulow ();
2522 unsigned HOST_WIDE_INT resultl = xl + yl;
2523 if (sgn == SIGNED)
2525 if ((((resultl ^ xl) & (resultl ^ yl))
2526 >> (precision - 1)) & 1)
2528 if (xl > resultl)
2529 *overflow = OVF_UNDERFLOW;
2530 else if (xl < resultl)
2531 *overflow = OVF_OVERFLOW;
2532 else
2533 *overflow = OVF_NONE;
2535 else
2536 *overflow = OVF_NONE;
2538 else
2539 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2540 < (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2541 ? OVF_OVERFLOW : OVF_NONE;
2542 val[0] = resultl;
2543 result.set_len (1);
2545 else
2546 result.set_len (add_large (val, xi.val, xi.len,
2547 yi.val, yi.len, precision,
2548 sgn, overflow));
2549 return result;
2552 /* Return X - Y. */
2553 template <typename T1, typename T2>
2554 inline WI_BINARY_RESULT (T1, T2)
2555 wi::sub (const T1 &x, const T2 &y)
2557 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2558 unsigned int precision = get_precision (result);
2559 WIDE_INT_REF_FOR (T1) xi (x, precision);
2560 WIDE_INT_REF_FOR (T2) yi (y, precision);
2561 if (precision <= HOST_BITS_PER_WIDE_INT)
2563 val[0] = xi.ulow () - yi.ulow ();
2564 result.set_len (1);
2566 /* If the precision is known at compile time to be greater than
2567 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2568 knowing that (a) all bits in those HWIs are significant and
2569 (b) the result has room for at least two HWIs. This provides
2570 a fast path for things like offset_int and widest_int.
2572 The STATIC_CONSTANT_P test prevents this path from being
2573 used for wide_ints. wide_ints with precisions greater than
2574 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2575 point handling them inline. */
2576 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2577 && LIKELY (xi.len + yi.len == 2))
2579 unsigned HOST_WIDE_INT xl = xi.ulow ();
2580 unsigned HOST_WIDE_INT yl = yi.ulow ();
2581 unsigned HOST_WIDE_INT resultl = xl - yl;
2582 val[0] = resultl;
2583 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2584 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2585 >> (HOST_BITS_PER_WIDE_INT - 1)));
2587 else
2588 result.set_len (sub_large (val, xi.val, xi.len,
2589 yi.val, yi.len, precision,
2590 UNSIGNED, 0));
2591 return result;
2594 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2595 and indicate in *OVERFLOW whether the operation overflowed. */
2596 template <typename T1, typename T2>
2597 inline WI_BINARY_RESULT (T1, T2)
2598 wi::sub (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2600 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2601 unsigned int precision = get_precision (result);
2602 WIDE_INT_REF_FOR (T1) xi (x, precision);
2603 WIDE_INT_REF_FOR (T2) yi (y, precision);
2604 if (precision <= HOST_BITS_PER_WIDE_INT)
2606 unsigned HOST_WIDE_INT xl = xi.ulow ();
2607 unsigned HOST_WIDE_INT yl = yi.ulow ();
2608 unsigned HOST_WIDE_INT resultl = xl - yl;
2609 if (sgn == SIGNED)
2611 if ((((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1)
2613 if (xl > yl)
2614 *overflow = OVF_UNDERFLOW;
2615 else if (xl < yl)
2616 *overflow = OVF_OVERFLOW;
2617 else
2618 *overflow = OVF_NONE;
2620 else
2621 *overflow = OVF_NONE;
2623 else
2624 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2625 > (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2626 ? OVF_UNDERFLOW : OVF_NONE;
2627 val[0] = resultl;
2628 result.set_len (1);
2630 else
2631 result.set_len (sub_large (val, xi.val, xi.len,
2632 yi.val, yi.len, precision,
2633 sgn, overflow));
2634 return result;
2637 /* Return X * Y. */
2638 template <typename T1, typename T2>
2639 inline WI_BINARY_RESULT (T1, T2)
2640 wi::mul (const T1 &x, const T2 &y)
2642 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2643 unsigned int precision = get_precision (result);
2644 WIDE_INT_REF_FOR (T1) xi (x, precision);
2645 WIDE_INT_REF_FOR (T2) yi (y, precision);
2646 if (precision <= HOST_BITS_PER_WIDE_INT)
2648 val[0] = xi.ulow () * yi.ulow ();
2649 result.set_len (1);
2651 else
2652 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2653 precision, UNSIGNED, 0, false));
2654 return result;
2657 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2658 and indicate in *OVERFLOW whether the operation overflowed. */
2659 template <typename T1, typename T2>
2660 inline WI_BINARY_RESULT (T1, T2)
2661 wi::mul (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2663 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2664 unsigned int precision = get_precision (result);
2665 WIDE_INT_REF_FOR (T1) xi (x, precision);
2666 WIDE_INT_REF_FOR (T2) yi (y, precision);
2667 result.set_len (mul_internal (val, xi.val, xi.len,
2668 yi.val, yi.len, precision,
2669 sgn, overflow, false));
2670 return result;
2673 /* Return X * Y, treating both X and Y as signed values. Indicate in
2674 *OVERFLOW whether the operation overflowed. */
2675 template <typename T1, typename T2>
2676 inline WI_BINARY_RESULT (T1, T2)
2677 wi::smul (const T1 &x, const T2 &y, overflow_type *overflow)
2679 return mul (x, y, SIGNED, overflow);
2682 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2683 *OVERFLOW if the result overflows. */
2684 template <typename T1, typename T2>
2685 inline WI_BINARY_RESULT (T1, T2)
2686 wi::umul (const T1 &x, const T2 &y, overflow_type *overflow)
2688 return mul (x, y, UNSIGNED, overflow);
2691 /* Perform a widening multiplication of X and Y, extending the values
2692 according to SGN, and return the high part of the result. */
2693 template <typename T1, typename T2>
2694 inline WI_BINARY_RESULT (T1, T2)
2695 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2697 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2698 unsigned int precision = get_precision (result);
2699 WIDE_INT_REF_FOR (T1) xi (x, precision);
2700 WIDE_INT_REF_FOR (T2) yi (y, precision);
2701 result.set_len (mul_internal (val, xi.val, xi.len,
2702 yi.val, yi.len, precision,
2703 sgn, 0, true));
2704 return result;
2707 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2708 signedness given by SGN. Indicate in *OVERFLOW if the result
2709 overflows. */
2710 template <typename T1, typename T2>
2711 inline WI_BINARY_RESULT (T1, T2)
2712 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2714 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2715 unsigned int precision = get_precision (quotient);
2716 WIDE_INT_REF_FOR (T1) xi (x, precision);
2717 WIDE_INT_REF_FOR (T2) yi (y);
2719 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2720 precision,
2721 yi.val, yi.len, yi.precision,
2722 sgn, overflow));
2723 return quotient;
2726 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2727 template <typename T1, typename T2>
2728 inline WI_BINARY_RESULT (T1, T2)
2729 wi::sdiv_trunc (const T1 &x, const T2 &y)
2731 return div_trunc (x, y, SIGNED);
2734 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2735 template <typename T1, typename T2>
2736 inline WI_BINARY_RESULT (T1, T2)
2737 wi::udiv_trunc (const T1 &x, const T2 &y)
2739 return div_trunc (x, y, UNSIGNED);
2742 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2743 signedness given by SGN. Indicate in *OVERFLOW if the result
2744 overflows. */
2745 template <typename T1, typename T2>
2746 inline WI_BINARY_RESULT (T1, T2)
2747 wi::div_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2749 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2750 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2751 unsigned int precision = get_precision (quotient);
2752 WIDE_INT_REF_FOR (T1) xi (x, precision);
2753 WIDE_INT_REF_FOR (T2) yi (y);
2755 unsigned int remainder_len;
2756 quotient.set_len (divmod_internal (quotient_val,
2757 &remainder_len, remainder_val,
2758 xi.val, xi.len, precision,
2759 yi.val, yi.len, yi.precision, sgn,
2760 overflow));
2761 remainder.set_len (remainder_len);
2762 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2763 return quotient - 1;
2764 return quotient;
2767 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2768 template <typename T1, typename T2>
2769 inline WI_BINARY_RESULT (T1, T2)
2770 wi::sdiv_floor (const T1 &x, const T2 &y)
2772 return div_floor (x, y, SIGNED);
2775 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2776 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2777 template <typename T1, typename T2>
2778 inline WI_BINARY_RESULT (T1, T2)
2779 wi::udiv_floor (const T1 &x, const T2 &y)
2781 return div_floor (x, y, UNSIGNED);
2784 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2785 signedness given by SGN. Indicate in *OVERFLOW if the result
2786 overflows. */
2787 template <typename T1, typename T2>
2788 inline WI_BINARY_RESULT (T1, T2)
2789 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2791 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2792 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2793 unsigned int precision = get_precision (quotient);
2794 WIDE_INT_REF_FOR (T1) xi (x, precision);
2795 WIDE_INT_REF_FOR (T2) yi (y);
2797 unsigned int remainder_len;
2798 quotient.set_len (divmod_internal (quotient_val,
2799 &remainder_len, remainder_val,
2800 xi.val, xi.len, precision,
2801 yi.val, yi.len, yi.precision, sgn,
2802 overflow));
2803 remainder.set_len (remainder_len);
2804 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2805 return quotient + 1;
2806 return quotient;
2809 /* Return X / Y, rouding towards +inf. Treat X and Y as unsigned values. */
2810 template <typename T1, typename T2>
2811 inline WI_BINARY_RESULT (T1, T2)
2812 wi::udiv_ceil (const T1 &x, const T2 &y)
2814 return div_ceil (x, y, UNSIGNED);
2817 /* Return X / Y, rouding towards nearest with ties away from zero.
2818 Treat X and Y as having the signedness given by SGN. Indicate
2819 in *OVERFLOW if the result overflows. */
2820 template <typename T1, typename T2>
2821 inline WI_BINARY_RESULT (T1, T2)
2822 wi::div_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2824 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2825 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2826 unsigned int precision = get_precision (quotient);
2827 WIDE_INT_REF_FOR (T1) xi (x, precision);
2828 WIDE_INT_REF_FOR (T2) yi (y);
2830 unsigned int remainder_len;
2831 quotient.set_len (divmod_internal (quotient_val,
2832 &remainder_len, remainder_val,
2833 xi.val, xi.len, precision,
2834 yi.val, yi.len, yi.precision, sgn,
2835 overflow));
2836 remainder.set_len (remainder_len);
2838 if (remainder != 0)
2840 if (sgn == SIGNED)
2842 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2843 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2845 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2846 return quotient - 1;
2847 else
2848 return quotient + 1;
2851 else
2853 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2854 return quotient + 1;
2857 return quotient;
2860 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2861 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2862 template <typename T1, typename T2>
2863 inline WI_BINARY_RESULT (T1, T2)
2864 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2865 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2867 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2868 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2869 unsigned int precision = get_precision (quotient);
2870 WIDE_INT_REF_FOR (T1) xi (x, precision);
2871 WIDE_INT_REF_FOR (T2) yi (y);
2873 unsigned int remainder_len;
2874 quotient.set_len (divmod_internal (quotient_val,
2875 &remainder_len, remainder_val,
2876 xi.val, xi.len, precision,
2877 yi.val, yi.len, yi.precision, sgn, 0));
2878 remainder.set_len (remainder_len);
2880 *remainder_ptr = remainder;
2881 return quotient;
2884 /* Compute the greatest common divisor of two numbers A and B using
2885 Euclid's algorithm. */
2886 template <typename T1, typename T2>
2887 inline WI_BINARY_RESULT (T1, T2)
2888 wi::gcd (const T1 &a, const T2 &b, signop sgn)
2890 T1 x, y, z;
2892 x = wi::abs (a);
2893 y = wi::abs (b);
2895 while (gt_p (x, 0, sgn))
2897 z = mod_trunc (y, x, sgn);
2898 y = x;
2899 x = z;
2902 return y;
2905 /* Compute X / Y, rouding towards 0, and return the remainder.
2906 Treat X and Y as having the signedness given by SGN. Indicate
2907 in *OVERFLOW if the division overflows. */
2908 template <typename T1, typename T2>
2909 inline WI_BINARY_RESULT (T1, T2)
2910 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2912 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2913 unsigned int precision = get_precision (remainder);
2914 WIDE_INT_REF_FOR (T1) xi (x, precision);
2915 WIDE_INT_REF_FOR (T2) yi (y);
2917 unsigned int remainder_len;
2918 divmod_internal (0, &remainder_len, remainder_val,
2919 xi.val, xi.len, precision,
2920 yi.val, yi.len, yi.precision, sgn, overflow);
2921 remainder.set_len (remainder_len);
2923 return remainder;
2926 /* Compute X / Y, rouding towards 0, and return the remainder.
2927 Treat X and Y as signed values. */
2928 template <typename T1, typename T2>
2929 inline WI_BINARY_RESULT (T1, T2)
2930 wi::smod_trunc (const T1 &x, const T2 &y)
2932 return mod_trunc (x, y, SIGNED);
2935 /* Compute X / Y, rouding towards 0, and return the remainder.
2936 Treat X and Y as unsigned values. */
2937 template <typename T1, typename T2>
2938 inline WI_BINARY_RESULT (T1, T2)
2939 wi::umod_trunc (const T1 &x, const T2 &y)
2941 return mod_trunc (x, y, UNSIGNED);
2944 /* Compute X / Y, rouding towards -inf, and return the remainder.
2945 Treat X and Y as having the signedness given by SGN. Indicate
2946 in *OVERFLOW if the division overflows. */
2947 template <typename T1, typename T2>
2948 inline WI_BINARY_RESULT (T1, T2)
2949 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2951 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2952 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2953 unsigned int precision = get_precision (quotient);
2954 WIDE_INT_REF_FOR (T1) xi (x, precision);
2955 WIDE_INT_REF_FOR (T2) yi (y);
2957 unsigned int remainder_len;
2958 quotient.set_len (divmod_internal (quotient_val,
2959 &remainder_len, remainder_val,
2960 xi.val, xi.len, precision,
2961 yi.val, yi.len, yi.precision, sgn,
2962 overflow));
2963 remainder.set_len (remainder_len);
2965 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2966 return remainder + y;
2967 return remainder;
2970 /* Compute X / Y, rouding towards -inf, and return the remainder.
2971 Treat X and Y as unsigned values. */
2972 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2973 template <typename T1, typename T2>
2974 inline WI_BINARY_RESULT (T1, T2)
2975 wi::umod_floor (const T1 &x, const T2 &y)
2977 return mod_floor (x, y, UNSIGNED);
2980 /* Compute X / Y, rouding towards +inf, and return the remainder.
2981 Treat X and Y as having the signedness given by SGN. Indicate
2982 in *OVERFLOW if the division overflows. */
2983 template <typename T1, typename T2>
2984 inline WI_BINARY_RESULT (T1, T2)
2985 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2987 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2988 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2989 unsigned int precision = get_precision (quotient);
2990 WIDE_INT_REF_FOR (T1) xi (x, precision);
2991 WIDE_INT_REF_FOR (T2) yi (y);
2993 unsigned int remainder_len;
2994 quotient.set_len (divmod_internal (quotient_val,
2995 &remainder_len, remainder_val,
2996 xi.val, xi.len, precision,
2997 yi.val, yi.len, yi.precision, sgn,
2998 overflow));
2999 remainder.set_len (remainder_len);
3001 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
3002 return remainder - y;
3003 return remainder;
3006 /* Compute X / Y, rouding towards nearest with ties away from zero,
3007 and return the remainder. Treat X and Y as having the signedness
3008 given by SGN. Indicate in *OVERFLOW if the division overflows. */
3009 template <typename T1, typename T2>
3010 inline WI_BINARY_RESULT (T1, T2)
3011 wi::mod_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3013 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3014 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3015 unsigned int precision = get_precision (quotient);
3016 WIDE_INT_REF_FOR (T1) xi (x, precision);
3017 WIDE_INT_REF_FOR (T2) yi (y);
3019 unsigned int remainder_len;
3020 quotient.set_len (divmod_internal (quotient_val,
3021 &remainder_len, remainder_val,
3022 xi.val, xi.len, precision,
3023 yi.val, yi.len, yi.precision, sgn,
3024 overflow));
3025 remainder.set_len (remainder_len);
3027 if (remainder != 0)
3029 if (sgn == SIGNED)
3031 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
3032 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
3034 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
3035 return remainder + y;
3036 else
3037 return remainder - y;
3040 else
3042 if (wi::geu_p (remainder, wi::sub (y, remainder)))
3043 return remainder - y;
3046 return remainder;
3049 /* Return true if X is a multiple of Y. Treat X and Y as having the
3050 signedness given by SGN. */
3051 template <typename T1, typename T2>
3052 inline bool
3053 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
3055 return wi::mod_trunc (x, y, sgn) == 0;
3058 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
3059 Treat X and Y as having the signedness given by SGN. */
3060 template <typename T1, typename T2>
3061 inline bool
3062 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
3063 WI_BINARY_RESULT (T1, T2) *res)
3065 WI_BINARY_RESULT (T1, T2) remainder;
3066 WI_BINARY_RESULT (T1, T2) quotient
3067 = divmod_trunc (x, y, sgn, &remainder);
3068 if (remainder == 0)
3070 *res = quotient;
3071 return true;
3073 return false;
3076 /* Return X << Y. Return 0 if Y is greater than or equal to
3077 the precision of X. */
3078 template <typename T1, typename T2>
3079 inline WI_UNARY_RESULT (T1)
3080 wi::lshift (const T1 &x, const T2 &y)
3082 WI_UNARY_RESULT_VAR (result, val, T1, x);
3083 unsigned int precision = get_precision (result);
3084 WIDE_INT_REF_FOR (T1) xi (x, precision);
3085 WIDE_INT_REF_FOR (T2) yi (y);
3086 /* Handle the simple cases quickly. */
3087 if (geu_p (yi, precision))
3089 val[0] = 0;
3090 result.set_len (1);
3092 else
3094 unsigned int shift = yi.to_uhwi ();
3095 /* For fixed-precision integers like offset_int and widest_int,
3096 handle the case where the shift value is constant and the
3097 result is a single nonnegative HWI (meaning that we don't
3098 need to worry about val[1]). This is particularly common
3099 for converting a byte count to a bit count.
3101 For variable-precision integers like wide_int, handle HWI
3102 and sub-HWI integers inline. */
3103 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3104 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
3105 && xi.len == 1
3106 && IN_RANGE (xi.val[0], 0, HOST_WIDE_INT_MAX >> shift))
3107 : precision <= HOST_BITS_PER_WIDE_INT)
3109 val[0] = xi.ulow () << shift;
3110 result.set_len (1);
3112 else
3113 result.set_len (lshift_large (val, xi.val, xi.len,
3114 precision, shift));
3116 return result;
3119 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
3120 or equal to the precision of X. */
3121 template <typename T1, typename T2>
3122 inline WI_UNARY_RESULT (T1)
3123 wi::lrshift (const T1 &x, const T2 &y)
3125 WI_UNARY_RESULT_VAR (result, val, T1, x);
3126 /* Do things in the precision of the input rather than the output,
3127 since the result can be no larger than that. */
3128 WIDE_INT_REF_FOR (T1) xi (x);
3129 WIDE_INT_REF_FOR (T2) yi (y);
3130 /* Handle the simple cases quickly. */
3131 if (geu_p (yi, xi.precision))
3133 val[0] = 0;
3134 result.set_len (1);
3136 else
3138 unsigned int shift = yi.to_uhwi ();
3139 /* For fixed-precision integers like offset_int and widest_int,
3140 handle the case where the shift value is constant and the
3141 shifted value is a single nonnegative HWI (meaning that all
3142 bits above the HWI are zero). This is particularly common
3143 for converting a bit count to a byte count.
3145 For variable-precision integers like wide_int, handle HWI
3146 and sub-HWI integers inline. */
3147 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3148 ? (shift < HOST_BITS_PER_WIDE_INT
3149 && xi.len == 1
3150 && xi.val[0] >= 0)
3151 : xi.precision <= HOST_BITS_PER_WIDE_INT)
3153 val[0] = xi.to_uhwi () >> shift;
3154 result.set_len (1);
3156 else
3157 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
3158 get_precision (result), shift));
3160 return result;
3163 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
3164 Y is greater than or equal to the precision of X. */
3165 template <typename T1, typename T2>
3166 inline WI_UNARY_RESULT (T1)
3167 wi::arshift (const T1 &x, const T2 &y)
3169 WI_UNARY_RESULT_VAR (result, val, T1, x);
3170 /* Do things in the precision of the input rather than the output,
3171 since the result can be no larger than that. */
3172 WIDE_INT_REF_FOR (T1) xi (x);
3173 WIDE_INT_REF_FOR (T2) yi (y);
3174 /* Handle the simple cases quickly. */
3175 if (geu_p (yi, xi.precision))
3177 val[0] = sign_mask (x);
3178 result.set_len (1);
3180 else
3182 unsigned int shift = yi.to_uhwi ();
3183 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
3185 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3186 result.set_len (1, true);
3188 else
3189 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3190 get_precision (result), shift));
3192 return result;
3195 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3196 logical shift otherwise. */
3197 template <typename T1, typename T2>
3198 inline WI_UNARY_RESULT (T1)
3199 wi::rshift (const T1 &x, const T2 &y, signop sgn)
3201 if (sgn == UNSIGNED)
3202 return lrshift (x, y);
3203 else
3204 return arshift (x, y);
3207 /* Return the result of rotating the low WIDTH bits of X left by Y
3208 bits and zero-extending the result. Use a full-width rotate if
3209 WIDTH is zero. */
3210 template <typename T1, typename T2>
3211 WI_UNARY_RESULT (T1)
3212 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3214 unsigned int precision = get_binary_precision (x, x);
3215 if (width == 0)
3216 width = precision;
3217 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3218 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3219 WI_UNARY_RESULT (T1) right
3220 = wi::lrshift (width != precision ? wi::zext (x, width) : x,
3221 wi::sub (width, ymod));
3222 if (width != precision)
3223 return wi::zext (left, width) | right;
3224 return left | right;
3227 /* Return the result of rotating the low WIDTH bits of X right by Y
3228 bits and zero-extending the result. Use a full-width rotate if
3229 WIDTH is zero. */
3230 template <typename T1, typename T2>
3231 WI_UNARY_RESULT (T1)
3232 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3234 unsigned int precision = get_binary_precision (x, x);
3235 if (width == 0)
3236 width = precision;
3237 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3238 WI_UNARY_RESULT (T1) right
3239 = wi::lrshift (width != precision ? wi::zext (x, width) : x, ymod);
3240 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3241 if (width != precision)
3242 return wi::zext (left, width) | right;
3243 return left | right;
3246 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3247 is odd. */
3248 inline int
3249 wi::parity (const wide_int_ref &x)
3251 return popcount (x) & 1;
3254 /* Extract WIDTH bits from X, starting at BITPOS. */
3255 template <typename T>
3256 inline unsigned HOST_WIDE_INT
3257 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3259 unsigned precision = get_precision (x);
3260 if (precision < bitpos + width)
3261 precision = bitpos + width;
3262 WIDE_INT_REF_FOR (T) xi (x, precision);
3264 /* Handle this rare case after the above, so that we assert about
3265 bogus BITPOS values. */
3266 if (width == 0)
3267 return 0;
3269 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3270 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3271 unsigned HOST_WIDE_INT res = xi.elt (start);
3272 res >>= shift;
3273 if (shift + width > HOST_BITS_PER_WIDE_INT)
3275 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3276 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3278 return zext_hwi (res, width);
3281 /* Return the minimum precision needed to store X with sign SGN. */
3282 template <typename T>
3283 inline unsigned int
3284 wi::min_precision (const T &x, signop sgn)
3286 if (sgn == SIGNED)
3287 return get_precision (x) - clrsb (x);
3288 else
3289 return get_precision (x) - clz (x);
3292 #define SIGNED_BINARY_PREDICATE(OP, F) \
3293 template <typename T1, typename T2> \
3294 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3295 OP (const T1 &x, const T2 &y) \
3297 return wi::F (x, y); \
3300 SIGNED_BINARY_PREDICATE (operator <, lts_p)
3301 SIGNED_BINARY_PREDICATE (operator <=, les_p)
3302 SIGNED_BINARY_PREDICATE (operator >, gts_p)
3303 SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3305 #undef SIGNED_BINARY_PREDICATE
3307 #define UNARY_OPERATOR(OP, F) \
3308 template<typename T> \
3309 WI_UNARY_RESULT (generic_wide_int<T>) \
3310 OP (const generic_wide_int<T> &x) \
3312 return wi::F (x); \
3315 #define BINARY_PREDICATE(OP, F) \
3316 template<typename T1, typename T2> \
3317 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3318 OP (const T1 &x, const T2 &y) \
3320 return wi::F (x, y); \
3323 #define BINARY_OPERATOR(OP, F) \
3324 template<typename T1, typename T2> \
3325 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3326 OP (const T1 &x, const T2 &y) \
3328 return wi::F (x, y); \
3331 #define SHIFT_OPERATOR(OP, F) \
3332 template<typename T1, typename T2> \
3333 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3334 OP (const T1 &x, const T2 &y) \
3336 return wi::F (x, y); \
3339 UNARY_OPERATOR (operator ~, bit_not)
3340 UNARY_OPERATOR (operator -, neg)
3341 BINARY_PREDICATE (operator ==, eq_p)
3342 BINARY_PREDICATE (operator !=, ne_p)
3343 BINARY_OPERATOR (operator &, bit_and)
3344 BINARY_OPERATOR (operator |, bit_or)
3345 BINARY_OPERATOR (operator ^, bit_xor)
3346 BINARY_OPERATOR (operator +, add)
3347 BINARY_OPERATOR (operator -, sub)
3348 BINARY_OPERATOR (operator *, mul)
3349 SHIFT_OPERATOR (operator <<, lshift)
3351 #undef UNARY_OPERATOR
3352 #undef BINARY_PREDICATE
3353 #undef BINARY_OPERATOR
3354 #undef SHIFT_OPERATOR
3356 template <typename T1, typename T2>
3357 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3358 operator >> (const T1 &x, const T2 &y)
3360 return wi::arshift (x, y);
3363 template <typename T1, typename T2>
3364 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3365 operator / (const T1 &x, const T2 &y)
3367 return wi::sdiv_trunc (x, y);
3370 template <typename T1, typename T2>
3371 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3372 operator % (const T1 &x, const T2 &y)
3374 return wi::smod_trunc (x, y);
3377 template<typename T>
3378 void
3379 gt_ggc_mx (generic_wide_int <T> *)
3383 template<typename T>
3384 void
3385 gt_pch_nx (generic_wide_int <T> *)
3389 template<typename T>
3390 void
3391 gt_pch_nx (generic_wide_int <T> *, gt_pointer_operator, void *)
3395 template<int N>
3396 void
3397 gt_ggc_mx (trailing_wide_ints <N> *)
3401 template<int N>
3402 void
3403 gt_pch_nx (trailing_wide_ints <N> *)
3407 template<int N>
3408 void
3409 gt_pch_nx (trailing_wide_ints <N> *, gt_pointer_operator, void *)
3413 namespace wi
3415 /* Used for overloaded functions in which the only other acceptable
3416 scalar type is a pointer. It stops a plain 0 from being treated
3417 as a null pointer. */
3418 struct never_used1 {};
3419 struct never_used2 {};
3421 wide_int min_value (unsigned int, signop);
3422 wide_int min_value (never_used1 *);
3423 wide_int min_value (never_used2 *);
3424 wide_int max_value (unsigned int, signop);
3425 wide_int max_value (never_used1 *);
3426 wide_int max_value (never_used2 *);
3428 /* FIXME: this is target dependent, so should be elsewhere.
3429 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3430 wide_int from_buffer (const unsigned char *, unsigned int);
3432 #ifndef GENERATOR_FILE
3433 void to_mpz (const wide_int_ref &, mpz_t, signop);
3434 #endif
3436 wide_int mask (unsigned int, bool, unsigned int);
3437 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3438 wide_int set_bit_in_zero (unsigned int, unsigned int);
3439 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3440 unsigned int);
3441 wide_int round_down_for_mask (const wide_int &, const wide_int &);
3442 wide_int round_up_for_mask (const wide_int &, const wide_int &);
3444 wide_int mod_inv (const wide_int &a, const wide_int &b);
3446 template <typename T>
3447 T mask (unsigned int, bool);
3449 template <typename T>
3450 T shifted_mask (unsigned int, unsigned int, bool);
3452 template <typename T>
3453 T set_bit_in_zero (unsigned int);
3455 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3456 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3457 bool, unsigned int);
3458 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3459 unsigned int, unsigned int, bool);
3462 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3463 and the other bits are clear, or the inverse if NEGATE_P. */
3464 inline wide_int
3465 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3467 wide_int result = wide_int::create (precision);
3468 result.set_len (mask (result.write_val (), width, negate_p, precision));
3469 return result;
3472 /* Return a PRECISION-bit integer in which the low START bits are clear,
3473 the next WIDTH bits are set, and the other bits are clear,
3474 or the inverse if NEGATE_P. */
3475 inline wide_int
3476 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3477 unsigned int precision)
3479 wide_int result = wide_int::create (precision);
3480 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3481 precision));
3482 return result;
3485 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3486 others are clear. */
3487 inline wide_int
3488 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3490 return shifted_mask (bit, 1, false, precision);
3493 /* Return an integer of type T in which the low WIDTH bits are set
3494 and the other bits are clear, or the inverse if NEGATE_P. */
3495 template <typename T>
3496 inline T
3497 wi::mask (unsigned int width, bool negate_p)
3499 STATIC_ASSERT (wi::int_traits<T>::precision);
3500 T result;
3501 result.set_len (mask (result.write_val (), width, negate_p,
3502 wi::int_traits <T>::precision));
3503 return result;
3506 /* Return an integer of type T in which the low START bits are clear,
3507 the next WIDTH bits are set, and the other bits are clear, or the
3508 inverse if NEGATE_P. */
3509 template <typename T>
3510 inline T
3511 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3513 STATIC_ASSERT (wi::int_traits<T>::precision);
3514 T result;
3515 result.set_len (shifted_mask (result.write_val (), start, width,
3516 negate_p,
3517 wi::int_traits <T>::precision));
3518 return result;
3521 /* Return an integer of type T in which bit BIT is set and all the
3522 others are clear. */
3523 template <typename T>
3524 inline T
3525 wi::set_bit_in_zero (unsigned int bit)
3527 return shifted_mask <T> (bit, 1, false);
3530 /* Accumulate a set of overflows into OVERFLOW. */
3532 inline void
3533 wi::accumulate_overflow (wi::overflow_type &overflow,
3534 wi::overflow_type suboverflow)
3536 if (!suboverflow)
3537 return;
3538 if (!overflow)
3539 overflow = suboverflow;
3540 else if (overflow != suboverflow)
3541 overflow = wi::OVF_UNKNOWN;
3544 #endif /* WIDE_INT_H */