PR libstdc++/83279 handle sendfile not copying entire file
[official-gcc.git] / gcc / wide-int.h
blobbbfde909aeb17654568dd7ec2d8152b52ad7eb09
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef WIDE_INT_H
21 #define WIDE_INT_H
23 /* wide-int.[cc|h] implements a class that efficiently performs
24 mathematical operations on finite precision integers. wide_ints
25 are designed to be transient - they are not for long term storage
26 of values. There is tight integration between wide_ints and the
27 other longer storage GCC representations (rtl and tree).
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
32 1) wide_int (the default). This flavor does the math in the
33 precision of its input arguments. It is assumed (and checked)
34 that the precisions of the operands and results are consistent.
35 This is the most efficient flavor. It is not possible to examine
36 bits above the precision that has been specified. Because of
37 this, the default flavor has semantics that are simple to
38 understand and in general model the underlying hardware that the
39 compiler is targetted for.
41 This flavor must be used at the RTL level of gcc because there
42 is, in general, not enough information in the RTL representation
43 to extend a value beyond the precision specified in the mode.
45 This flavor should also be used at the TREE and GIMPLE levels of
46 the compiler except for the circumstances described in the
47 descriptions of the other two flavors.
49 The default wide_int representation does not contain any
50 information inherent about signedness of the represented value,
51 so it can be used to represent both signed and unsigned numbers.
52 For operations where the results depend on signedness (full width
53 multiply, division, shifts, comparisons, and operations that need
54 overflow detected), the signedness must be specified separately.
56 2) offset_int. This is a fixed-precision integer that can hold
57 any address offset, measured in either bits or bytes, with at
58 least one extra sign bit. At the moment the maximum address
59 size GCC supports is 64 bits. With 8-bit bytes and an extra
60 sign bit, offset_int therefore needs to have at least 68 bits
61 of precision. We round this up to 128 bits for efficiency.
62 Values of type T are converted to this precision by sign- or
63 zero-extending them based on the signedness of T.
65 The extra sign bit means that offset_int is effectively a signed
66 128-bit integer, i.e. it behaves like int128_t.
68 Since the values are logically signed, there is no need to
69 distinguish between signed and unsigned operations. Sign-sensitive
70 comparison operators <, <=, > and >= are therefore supported.
71 Shift operators << and >> are also supported, with >> being
72 an _arithmetic_ right shift.
74 [ Note that, even though offset_int is effectively int128_t,
75 it can still be useful to use unsigned comparisons like
76 wi::leu_p (a, b) as a more efficient short-hand for
77 "a >= 0 && a <= b". ]
79 3) widest_int. This representation is an approximation of
80 infinite precision math. However, it is not really infinite
81 precision math as in the GMP library. It is really finite
82 precision math where the precision is 4 times the size of the
83 largest integer that the target port can represent.
85 Like offset_int, widest_int is wider than all the values that
86 it needs to represent, so the integers are logically signed.
87 Sign-sensitive comparison operators <, <=, > and >= are supported,
88 as are << and >>.
90 There are several places in the GCC where this should/must be used:
92 * Code that does induction variable optimizations. This code
93 works with induction variables of many different types at the
94 same time. Because of this, it ends up doing many different
95 calculations where the operands are not compatible types. The
96 widest_int makes this easy, because it provides a field where
97 nothing is lost when converting from any variable,
99 * There are a small number of passes that currently use the
100 widest_int that should use the default. These should be
101 changed.
103 There are surprising features of offset_int and widest_int
104 that the users should be careful about:
106 1) Shifts and rotations are just weird. You have to specify a
107 precision in which the shift or rotate is to happen in. The bits
108 above this precision are zeroed. While this is what you
109 want, it is clearly non obvious.
111 2) Larger precision math sometimes does not produce the same
112 answer as would be expected for doing the math at the proper
113 precision. In particular, a multiply followed by a divide will
114 produce a different answer if the first product is larger than
115 what can be represented in the input precision.
117 The offset_int and the widest_int flavors are more expensive
118 than the default wide int, so in addition to the caveats with these
119 two, the default is the prefered representation.
121 All three flavors of wide_int are represented as a vector of
122 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
123 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
124 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
125 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
126 in element 0.
128 The default wide_int contains three fields: the vector (VAL),
129 the precision and a length (LEN). The length is the number of HWIs
130 needed to represent the value. widest_int and offset_int have a
131 constant precision that cannot be changed, so they only store the
132 VAL and LEN fields.
134 Since most integers used in a compiler are small values, it is
135 generally profitable to use a representation of the value that is
136 as small as possible. LEN is used to indicate the number of
137 elements of the vector that are in use. The numbers are stored as
138 sign extended numbers as a means of compression. Leading
139 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
140 as long as they can be reconstructed from the top bit that is being
141 represented.
143 The precision and length of a wide_int are always greater than 0.
144 Any bits in a wide_int above the precision are sign-extended from the
145 most significant bit. For example, a 4-bit value 0x8 is represented as
146 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
147 constants to be represented with undefined bits above the precision.
148 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
149 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
150 and in wider precisions.
152 There are constructors to create the various forms of wide_int from
153 trees, rtl and constants. For trees the options are:
155 tree t = ...;
156 wi::to_wide (t) // Treat T as a wide_int
157 wi::to_offset (t) // Treat T as an offset_int
158 wi::to_widest (t) // Treat T as a widest_int
160 All three are light-weight accessors that should have no overhead
161 in release builds. If it is useful for readability reasons to
162 store the result in a temporary variable, the preferred method is:
164 wi::tree_to_wide_ref twide = wi::to_wide (t);
165 wi::tree_to_offset_ref toffset = wi::to_offset (t);
166 wi::tree_to_widest_ref twidest = wi::to_widest (t);
168 To make an rtx into a wide_int, you have to pair it with a mode.
169 The canonical way to do this is with rtx_mode_t as in:
171 rtx r = ...
172 wide_int x = rtx_mode_t (r, mode);
174 Similarly, a wide_int can only be constructed from a host value if
175 the target precision is given explicitly, such as in:
177 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
178 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
180 However, offset_int and widest_int have an inherent precision and so
181 can be initialized directly from a host value:
183 offset_int x = (int) c; // sign-extend C
184 widest_int x = (unsigned int) c; // zero-extend C
186 It is also possible to do arithmetic directly on rtx_mode_ts and
187 constants. For example:
189 wi::add (r1, r2); // add equal-sized rtx_mode_ts r1 and r2
190 wi::add (r1, 1); // add 1 to rtx_mode_t r1
191 wi::lshift (1, 100); // 1 << 100 as a widest_int
193 Many binary operations place restrictions on the combinations of inputs,
194 using the following rules:
196 - {rtx, wide_int} op {rtx, wide_int} -> wide_int
197 The inputs must be the same precision. The result is a wide_int
198 of the same precision
200 - {rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
201 (un)signed HOST_WIDE_INT op {rtx, wide_int} -> wide_int
202 The HOST_WIDE_INT is extended or truncated to the precision of
203 the other input. The result is a wide_int of the same precision
204 as that input.
206 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
207 The inputs are extended to widest_int precision and produce a
208 widest_int result.
210 - offset_int op offset_int -> offset_int
211 offset_int op (un)signed HOST_WIDE_INT -> offset_int
212 (un)signed HOST_WIDE_INT op offset_int -> offset_int
214 - widest_int op widest_int -> widest_int
215 widest_int op (un)signed HOST_WIDE_INT -> widest_int
216 (un)signed HOST_WIDE_INT op widest_int -> widest_int
218 Other combinations like:
220 - widest_int op offset_int and
221 - wide_int op offset_int
223 are not allowed. The inputs should instead be extended or truncated
224 so that they match.
226 The inputs to comparison functions like wi::eq_p and wi::lts_p
227 follow the same compatibility rules, although their return types
228 are different. Unary functions on X produce the same result as
229 a binary operation X + X. Shift functions X op Y also produce
230 the same result as X + X; the precision of the shift amount Y
231 can be arbitrarily different from X. */
233 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
234 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
235 can accomodate at least 1 more bit so that unsigned numbers of that
236 mode can be represented as a signed value. Note that it is still
237 possible to create fixed_wide_ints that have precisions greater than
238 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
239 double-width multiplication result, for example. */
240 #define WIDE_INT_MAX_ELTS \
241 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
243 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
245 /* This is the max size of any pointer on any machine. It does not
246 seem to be as easy to sniff this out of the machine description as
247 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
248 multiple address sizes and may have different address sizes for
249 different address spaces. However, currently the largest pointer
250 on any platform is 64 bits. When that changes, then it is likely
251 that a target hook should be defined so that targets can make this
252 value larger for those targets. */
253 #define ADDR_MAX_BITSIZE 64
255 /* This is the internal precision used when doing any address
256 arithmetic. The '4' is really 3 + 1. Three of the bits are for
257 the number of extra bits needed to do bit addresses and the other bit
258 is to allow everything to be signed without loosing any precision.
259 Then everything is rounded up to the next HWI for efficiency. */
260 #define ADDR_MAX_PRECISION \
261 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
262 & ~(HOST_BITS_PER_WIDE_INT - 1))
264 /* The number of HWIs needed to store an offset_int. */
265 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
267 /* The type of result produced by a binary operation on types T1 and T2.
268 Defined purely for brevity. */
269 #define WI_BINARY_RESULT(T1, T2) \
270 typename wi::binary_traits <T1, T2>::result_type
272 /* Likewise for binary operators, which excludes the case in which neither
273 T1 nor T2 is a wide-int-based type. */
274 #define WI_BINARY_OPERATOR_RESULT(T1, T2) \
275 typename wi::binary_traits <T1, T2>::operator_result
277 /* The type of result produced by T1 << T2. Leads to substitution failure
278 if the operation isn't supported. Defined purely for brevity. */
279 #define WI_SIGNED_SHIFT_RESULT(T1, T2) \
280 typename wi::binary_traits <T1, T2>::signed_shift_result_type
282 /* The type of result produced by a sign-agnostic binary predicate on
283 types T1 and T2. This is bool if wide-int operations make sense for
284 T1 and T2 and leads to substitution failure otherwise. */
285 #define WI_BINARY_PREDICATE_RESULT(T1, T2) \
286 typename wi::binary_traits <T1, T2>::predicate_result
288 /* The type of result produced by a signed binary predicate on types T1 and T2.
289 This is bool if signed comparisons make sense for T1 and T2 and leads to
290 substitution failure otherwise. */
291 #define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2) \
292 typename wi::binary_traits <T1, T2>::signed_predicate_result
294 /* The type of result produced by a unary operation on type T. */
295 #define WI_UNARY_RESULT(T) \
296 typename wi::binary_traits <T, T>::result_type
298 /* Define a variable RESULT to hold the result of a binary operation on
299 X and Y, which have types T1 and T2 respectively. Define VAL to
300 point to the blocks of RESULT. Once the user of the macro has
301 filled in VAL, it should call RESULT.set_len to set the number
302 of initialized blocks. */
303 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
304 WI_BINARY_RESULT (T1, T2) RESULT = \
305 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
306 HOST_WIDE_INT *VAL = RESULT.write_val ()
308 /* Similar for the result of a unary operation on X, which has type T. */
309 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
310 WI_UNARY_RESULT (T) RESULT = \
311 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
312 HOST_WIDE_INT *VAL = RESULT.write_val ()
314 template <typename T> class generic_wide_int;
315 template <int N> class fixed_wide_int_storage;
316 class wide_int_storage;
318 /* An N-bit integer. Until we can use typedef templates, use this instead. */
319 #define FIXED_WIDE_INT(N) \
320 generic_wide_int < fixed_wide_int_storage <N> >
322 typedef generic_wide_int <wide_int_storage> wide_int;
323 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
324 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
326 /* wi::storage_ref can be a reference to a primitive type,
327 so this is the conservatively-correct setting. */
328 template <bool SE, bool HDP = true>
329 struct wide_int_ref_storage;
331 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
333 /* This can be used instead of wide_int_ref if the referenced value is
334 known to have type T. It carries across properties of T's representation,
335 such as whether excess upper bits in a HWI are defined, and can therefore
336 help avoid redundant work.
338 The macro could be replaced with a template typedef, once we're able
339 to use those. */
340 #define WIDE_INT_REF_FOR(T) \
341 generic_wide_int \
342 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
343 wi::int_traits <T>::host_dependent_precision> >
345 namespace wi
347 /* Classifies an integer based on its precision. */
348 enum precision_type {
349 /* The integer has both a precision and defined signedness. This allows
350 the integer to be converted to any width, since we know whether to fill
351 any extra bits with zeros or signs. */
352 FLEXIBLE_PRECISION,
354 /* The integer has a variable precision but no defined signedness. */
355 VAR_PRECISION,
357 /* The integer has a constant precision (known at GCC compile time)
358 and is signed. */
359 CONST_PRECISION
362 /* This class, which has no default implementation, is expected to
363 provide the following members:
365 static const enum precision_type precision_type;
366 Classifies the type of T.
368 static const unsigned int precision;
369 Only defined if precision_type == CONST_PRECISION. Specifies the
370 precision of all integers of type T.
372 static const bool host_dependent_precision;
373 True if the precision of T depends (or can depend) on the host.
375 static unsigned int get_precision (const T &x)
376 Return the number of bits in X.
378 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
379 unsigned int precision, const T &x)
380 Decompose X as a PRECISION-bit integer, returning the associated
381 wi::storage_ref. SCRATCH is available as scratch space if needed.
382 The routine should assert that PRECISION is acceptable. */
383 template <typename T> struct int_traits;
385 /* This class provides a single type, result_type, which specifies the
386 type of integer produced by a binary operation whose inputs have
387 types T1 and T2. The definition should be symmetric. */
388 template <typename T1, typename T2,
389 enum precision_type P1 = int_traits <T1>::precision_type,
390 enum precision_type P2 = int_traits <T2>::precision_type>
391 struct binary_traits;
393 /* Specify the result type for each supported combination of binary
394 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
395 mixed, in order to give stronger type checking. When both inputs
396 are CONST_PRECISION, they must have the same precision. */
397 template <typename T1, typename T2>
398 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
400 typedef widest_int result_type;
401 /* Don't define operators for this combination. */
404 template <typename T1, typename T2>
405 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
407 typedef wide_int result_type;
408 typedef result_type operator_result;
409 typedef bool predicate_result;
412 template <typename T1, typename T2>
413 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
415 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
416 so as not to confuse gengtype. */
417 typedef generic_wide_int < fixed_wide_int_storage
418 <int_traits <T2>::precision> > result_type;
419 typedef result_type operator_result;
420 typedef bool predicate_result;
421 typedef result_type signed_shift_result_type;
422 typedef bool signed_predicate_result;
425 template <typename T1, typename T2>
426 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
428 typedef wide_int result_type;
429 typedef result_type operator_result;
430 typedef bool predicate_result;
433 template <typename T1, typename T2>
434 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
436 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
437 so as not to confuse gengtype. */
438 typedef generic_wide_int < fixed_wide_int_storage
439 <int_traits <T1>::precision> > result_type;
440 typedef result_type operator_result;
441 typedef bool predicate_result;
442 typedef result_type signed_shift_result_type;
443 typedef bool signed_predicate_result;
446 template <typename T1, typename T2>
447 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
449 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
450 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
451 so as not to confuse gengtype. */
452 typedef generic_wide_int < fixed_wide_int_storage
453 <int_traits <T1>::precision> > result_type;
454 typedef result_type operator_result;
455 typedef bool predicate_result;
456 typedef result_type signed_shift_result_type;
457 typedef bool signed_predicate_result;
460 template <typename T1, typename T2>
461 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
463 typedef wide_int result_type;
464 typedef result_type operator_result;
465 typedef bool predicate_result;
469 /* Public functions for querying and operating on integers. */
470 namespace wi
472 template <typename T>
473 unsigned int get_precision (const T &);
475 template <typename T1, typename T2>
476 unsigned int get_binary_precision (const T1 &, const T2 &);
478 template <typename T1, typename T2>
479 void copy (T1 &, const T2 &);
481 #define UNARY_PREDICATE \
482 template <typename T> bool
483 #define UNARY_FUNCTION \
484 template <typename T> WI_UNARY_RESULT (T)
485 #define BINARY_PREDICATE \
486 template <typename T1, typename T2> bool
487 #define BINARY_FUNCTION \
488 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
489 #define SHIFT_FUNCTION \
490 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
492 UNARY_PREDICATE fits_shwi_p (const T &);
493 UNARY_PREDICATE fits_uhwi_p (const T &);
494 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
496 template <typename T>
497 HOST_WIDE_INT sign_mask (const T &);
499 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
500 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
501 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
502 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
503 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
504 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
505 BINARY_PREDICATE les_p (const T1 &, const T2 &);
506 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
507 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
508 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
509 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
510 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
511 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
512 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
514 template <typename T1, typename T2>
515 int cmp (const T1 &, const T2 &, signop);
517 template <typename T1, typename T2>
518 int cmps (const T1 &, const T2 &);
520 template <typename T1, typename T2>
521 int cmpu (const T1 &, const T2 &);
523 UNARY_FUNCTION bit_not (const T &);
524 UNARY_FUNCTION neg (const T &);
525 UNARY_FUNCTION neg (const T &, bool *);
526 UNARY_FUNCTION abs (const T &);
527 UNARY_FUNCTION ext (const T &, unsigned int, signop);
528 UNARY_FUNCTION sext (const T &, unsigned int);
529 UNARY_FUNCTION zext (const T &, unsigned int);
530 UNARY_FUNCTION set_bit (const T &, unsigned int);
532 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
533 BINARY_FUNCTION smin (const T1 &, const T2 &);
534 BINARY_FUNCTION umin (const T1 &, const T2 &);
535 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
536 BINARY_FUNCTION smax (const T1 &, const T2 &);
537 BINARY_FUNCTION umax (const T1 &, const T2 &);
539 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
540 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
541 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
542 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
543 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
544 BINARY_FUNCTION add (const T1 &, const T2 &);
545 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
546 BINARY_FUNCTION sub (const T1 &, const T2 &);
547 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
548 BINARY_FUNCTION mul (const T1 &, const T2 &);
549 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
550 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
551 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
552 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
553 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
554 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
555 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
556 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
557 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
558 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
559 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
560 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
561 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
562 WI_BINARY_RESULT (T1, T2) *);
563 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
564 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
565 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
566 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
567 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
568 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
569 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
570 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
572 template <typename T1, typename T2>
573 bool multiple_of_p (const T1 &, const T2 &, signop);
575 template <typename T1, typename T2>
576 bool multiple_of_p (const T1 &, const T2 &, signop,
577 WI_BINARY_RESULT (T1, T2) *);
579 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
580 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
581 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
582 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
583 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
584 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
586 #undef SHIFT_FUNCTION
587 #undef BINARY_PREDICATE
588 #undef BINARY_FUNCTION
589 #undef UNARY_PREDICATE
590 #undef UNARY_FUNCTION
592 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
593 bool only_sign_bit_p (const wide_int_ref &);
594 int clz (const wide_int_ref &);
595 int clrsb (const wide_int_ref &);
596 int ctz (const wide_int_ref &);
597 int exact_log2 (const wide_int_ref &);
598 int floor_log2 (const wide_int_ref &);
599 int ffs (const wide_int_ref &);
600 int popcount (const wide_int_ref &);
601 int parity (const wide_int_ref &);
603 template <typename T>
604 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
606 template <typename T>
607 unsigned int min_precision (const T &, signop);
610 namespace wi
612 /* Contains the components of a decomposed integer for easy, direct
613 access. */
614 struct storage_ref
616 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
618 const HOST_WIDE_INT *val;
619 unsigned int len;
620 unsigned int precision;
622 /* Provide enough trappings for this class to act as storage for
623 generic_wide_int. */
624 unsigned int get_len () const;
625 unsigned int get_precision () const;
626 const HOST_WIDE_INT *get_val () const;
630 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
631 unsigned int len_in,
632 unsigned int precision_in)
633 : val (val_in), len (len_in), precision (precision_in)
637 inline unsigned int
638 wi::storage_ref::get_len () const
640 return len;
643 inline unsigned int
644 wi::storage_ref::get_precision () const
646 return precision;
649 inline const HOST_WIDE_INT *
650 wi::storage_ref::get_val () const
652 return val;
655 /* This class defines an integer type using the storage provided by the
656 template argument. The storage class must provide the following
657 functions:
659 unsigned int get_precision () const
660 Return the number of bits in the integer.
662 HOST_WIDE_INT *get_val () const
663 Return a pointer to the array of blocks that encodes the integer.
665 unsigned int get_len () const
666 Return the number of blocks in get_val (). If this is smaller
667 than the number of blocks implied by get_precision (), the
668 remaining blocks are sign extensions of block get_len () - 1.
670 Although not required by generic_wide_int itself, writable storage
671 classes can also provide the following functions:
673 HOST_WIDE_INT *write_val ()
674 Get a modifiable version of get_val ()
676 unsigned int set_len (unsigned int len)
677 Set the value returned by get_len () to LEN. */
678 template <typename storage>
679 class GTY(()) generic_wide_int : public storage
681 public:
682 generic_wide_int ();
684 template <typename T>
685 generic_wide_int (const T &);
687 template <typename T>
688 generic_wide_int (const T &, unsigned int);
690 /* Conversions. */
691 HOST_WIDE_INT to_shwi (unsigned int) const;
692 HOST_WIDE_INT to_shwi () const;
693 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
694 unsigned HOST_WIDE_INT to_uhwi () const;
695 HOST_WIDE_INT to_short_addr () const;
697 /* Public accessors for the interior of a wide int. */
698 HOST_WIDE_INT sign_mask () const;
699 HOST_WIDE_INT elt (unsigned int) const;
700 unsigned HOST_WIDE_INT ulow () const;
701 unsigned HOST_WIDE_INT uhigh () const;
702 HOST_WIDE_INT slow () const;
703 HOST_WIDE_INT shigh () const;
705 template <typename T>
706 generic_wide_int &operator = (const T &);
708 #define ASSIGNMENT_OPERATOR(OP, F) \
709 template <typename T> \
710 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
712 /* Restrict these to cases where the shift operator is defined. */
713 #define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
714 template <typename T> \
715 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
717 #define INCDEC_OPERATOR(OP, DELTA) \
718 generic_wide_int &OP () { *this += DELTA; return *this; }
720 ASSIGNMENT_OPERATOR (operator &=, bit_and)
721 ASSIGNMENT_OPERATOR (operator |=, bit_or)
722 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
723 ASSIGNMENT_OPERATOR (operator +=, add)
724 ASSIGNMENT_OPERATOR (operator -=, sub)
725 ASSIGNMENT_OPERATOR (operator *=, mul)
726 ASSIGNMENT_OPERATOR (operator <<=, lshift)
727 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
728 INCDEC_OPERATOR (operator ++, 1)
729 INCDEC_OPERATOR (operator --, -1)
731 #undef SHIFT_ASSIGNMENT_OPERATOR
732 #undef ASSIGNMENT_OPERATOR
733 #undef INCDEC_OPERATOR
735 /* Debugging functions. */
736 void dump () const;
738 static const bool is_sign_extended
739 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
742 template <typename storage>
743 inline generic_wide_int <storage>::generic_wide_int () {}
745 template <typename storage>
746 template <typename T>
747 inline generic_wide_int <storage>::generic_wide_int (const T &x)
748 : storage (x)
752 template <typename storage>
753 template <typename T>
754 inline generic_wide_int <storage>::generic_wide_int (const T &x,
755 unsigned int precision)
756 : storage (x, precision)
760 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
761 If THIS does not fit in PRECISION, the information is lost. */
762 template <typename storage>
763 inline HOST_WIDE_INT
764 generic_wide_int <storage>::to_shwi (unsigned int precision) const
766 if (precision < HOST_BITS_PER_WIDE_INT)
767 return sext_hwi (this->get_val ()[0], precision);
768 else
769 return this->get_val ()[0];
772 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
773 template <typename storage>
774 inline HOST_WIDE_INT
775 generic_wide_int <storage>::to_shwi () const
777 if (is_sign_extended)
778 return this->get_val ()[0];
779 else
780 return to_shwi (this->get_precision ());
783 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
784 PRECISION. If THIS does not fit in PRECISION, the information
785 is lost. */
786 template <typename storage>
787 inline unsigned HOST_WIDE_INT
788 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
790 if (precision < HOST_BITS_PER_WIDE_INT)
791 return zext_hwi (this->get_val ()[0], precision);
792 else
793 return this->get_val ()[0];
796 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
797 template <typename storage>
798 inline unsigned HOST_WIDE_INT
799 generic_wide_int <storage>::to_uhwi () const
801 return to_uhwi (this->get_precision ());
804 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
805 represent addresses to using offset_int to represent addresses.
806 We use to_short_addr at the interface from new code to old,
807 unconverted code. */
808 template <typename storage>
809 inline HOST_WIDE_INT
810 generic_wide_int <storage>::to_short_addr () const
812 return this->get_val ()[0];
815 /* Return the implicit value of blocks above get_len (). */
816 template <typename storage>
817 inline HOST_WIDE_INT
818 generic_wide_int <storage>::sign_mask () const
820 unsigned int len = this->get_len ();
821 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
822 if (!is_sign_extended)
824 unsigned int precision = this->get_precision ();
825 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
826 if (excess > 0)
827 high <<= excess;
829 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
832 /* Return the signed value of the least-significant explicitly-encoded
833 block. */
834 template <typename storage>
835 inline HOST_WIDE_INT
836 generic_wide_int <storage>::slow () const
838 return this->get_val ()[0];
841 /* Return the signed value of the most-significant explicitly-encoded
842 block. */
843 template <typename storage>
844 inline HOST_WIDE_INT
845 generic_wide_int <storage>::shigh () const
847 return this->get_val ()[this->get_len () - 1];
850 /* Return the unsigned value of the least-significant
851 explicitly-encoded block. */
852 template <typename storage>
853 inline unsigned HOST_WIDE_INT
854 generic_wide_int <storage>::ulow () const
856 return this->get_val ()[0];
859 /* Return the unsigned value of the most-significant
860 explicitly-encoded block. */
861 template <typename storage>
862 inline unsigned HOST_WIDE_INT
863 generic_wide_int <storage>::uhigh () const
865 return this->get_val ()[this->get_len () - 1];
868 /* Return block I, which might be implicitly or explicit encoded. */
869 template <typename storage>
870 inline HOST_WIDE_INT
871 generic_wide_int <storage>::elt (unsigned int i) const
873 if (i >= this->get_len ())
874 return sign_mask ();
875 else
876 return this->get_val ()[i];
879 template <typename storage>
880 template <typename T>
881 inline generic_wide_int <storage> &
882 generic_wide_int <storage>::operator = (const T &x)
884 storage::operator = (x);
885 return *this;
888 /* Dump the contents of the integer to stderr, for debugging. */
889 template <typename storage>
890 void
891 generic_wide_int <storage>::dump () const
893 unsigned int len = this->get_len ();
894 const HOST_WIDE_INT *val = this->get_val ();
895 unsigned int precision = this->get_precision ();
896 fprintf (stderr, "[");
897 if (len * HOST_BITS_PER_WIDE_INT < precision)
898 fprintf (stderr, "...,");
899 for (unsigned int i = 0; i < len - 1; ++i)
900 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
901 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
902 val[0], precision);
905 namespace wi
907 template <typename storage>
908 struct int_traits < generic_wide_int <storage> >
909 : public wi::int_traits <storage>
911 static unsigned int get_precision (const generic_wide_int <storage> &);
912 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
913 const generic_wide_int <storage> &);
917 template <typename storage>
918 inline unsigned int
919 wi::int_traits < generic_wide_int <storage> >::
920 get_precision (const generic_wide_int <storage> &x)
922 return x.get_precision ();
925 template <typename storage>
926 inline wi::storage_ref
927 wi::int_traits < generic_wide_int <storage> >::
928 decompose (HOST_WIDE_INT *, unsigned int precision,
929 const generic_wide_int <storage> &x)
931 gcc_checking_assert (precision == x.get_precision ());
932 return wi::storage_ref (x.get_val (), x.get_len (), precision);
935 /* Provide the storage for a wide_int_ref. This acts like a read-only
936 wide_int, with the optimization that VAL is normally a pointer to
937 another integer's storage, so that no array copy is needed. */
938 template <bool SE, bool HDP>
939 struct wide_int_ref_storage : public wi::storage_ref
941 private:
942 /* Scratch space that can be used when decomposing the original integer.
943 It must live as long as this object. */
944 HOST_WIDE_INT scratch[2];
946 public:
947 wide_int_ref_storage (const wi::storage_ref &);
949 template <typename T>
950 wide_int_ref_storage (const T &);
952 template <typename T>
953 wide_int_ref_storage (const T &, unsigned int);
956 /* Create a reference from an existing reference. */
957 template <bool SE, bool HDP>
958 inline wide_int_ref_storage <SE, HDP>::
959 wide_int_ref_storage (const wi::storage_ref &x)
960 : storage_ref (x)
963 /* Create a reference to integer X in its natural precision. Note
964 that the natural precision is host-dependent for primitive
965 types. */
966 template <bool SE, bool HDP>
967 template <typename T>
968 inline wide_int_ref_storage <SE, HDP>::wide_int_ref_storage (const T &x)
969 : storage_ref (wi::int_traits <T>::decompose (scratch,
970 wi::get_precision (x), x))
974 /* Create a reference to integer X in precision PRECISION. */
975 template <bool SE, bool HDP>
976 template <typename T>
977 inline wide_int_ref_storage <SE, HDP>::
978 wide_int_ref_storage (const T &x, unsigned int precision)
979 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
983 namespace wi
985 template <bool SE, bool HDP>
986 struct int_traits <wide_int_ref_storage <SE, HDP> >
988 static const enum precision_type precision_type = VAR_PRECISION;
989 static const bool host_dependent_precision = HDP;
990 static const bool is_sign_extended = SE;
994 namespace wi
996 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
997 unsigned int, unsigned int, unsigned int,
998 signop sgn);
999 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1000 unsigned int, unsigned int, bool = true);
1003 /* The storage used by wide_int. */
1004 class GTY(()) wide_int_storage
1006 private:
1007 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
1008 unsigned int len;
1009 unsigned int precision;
1011 public:
1012 wide_int_storage ();
1013 template <typename T>
1014 wide_int_storage (const T &);
1016 /* The standard generic_wide_int storage methods. */
1017 unsigned int get_precision () const;
1018 const HOST_WIDE_INT *get_val () const;
1019 unsigned int get_len () const;
1020 HOST_WIDE_INT *write_val ();
1021 void set_len (unsigned int, bool = false);
1023 template <typename T>
1024 wide_int_storage &operator = (const T &);
1026 static wide_int from (const wide_int_ref &, unsigned int, signop);
1027 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1028 unsigned int, bool = true);
1029 static wide_int create (unsigned int);
1031 /* FIXME: target-dependent, so should disappear. */
1032 wide_int bswap () const;
1035 namespace wi
1037 template <>
1038 struct int_traits <wide_int_storage>
1040 static const enum precision_type precision_type = VAR_PRECISION;
1041 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1042 static const bool host_dependent_precision = false;
1043 static const bool is_sign_extended = true;
1044 template <typename T1, typename T2>
1045 static wide_int get_binary_result (const T1 &, const T2 &);
1049 inline wide_int_storage::wide_int_storage () {}
1051 /* Initialize the storage from integer X, in its natural precision.
1052 Note that we do not allow integers with host-dependent precision
1053 to become wide_ints; wide_ints must always be logically independent
1054 of the host. */
1055 template <typename T>
1056 inline wide_int_storage::wide_int_storage (const T &x)
1058 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1059 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1060 WIDE_INT_REF_FOR (T) xi (x);
1061 precision = xi.precision;
1062 wi::copy (*this, xi);
1065 template <typename T>
1066 inline wide_int_storage&
1067 wide_int_storage::operator = (const T &x)
1069 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1070 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1071 WIDE_INT_REF_FOR (T) xi (x);
1072 precision = xi.precision;
1073 wi::copy (*this, xi);
1074 return *this;
1077 inline unsigned int
1078 wide_int_storage::get_precision () const
1080 return precision;
1083 inline const HOST_WIDE_INT *
1084 wide_int_storage::get_val () const
1086 return val;
1089 inline unsigned int
1090 wide_int_storage::get_len () const
1092 return len;
1095 inline HOST_WIDE_INT *
1096 wide_int_storage::write_val ()
1098 return val;
1101 inline void
1102 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1104 len = l;
1105 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1106 val[len - 1] = sext_hwi (val[len - 1],
1107 precision % HOST_BITS_PER_WIDE_INT);
1110 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1111 number. */
1112 inline wide_int
1113 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1114 signop sgn)
1116 wide_int result = wide_int::create (precision);
1117 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1118 x.precision, precision, sgn));
1119 return result;
1122 /* Create a wide_int from the explicit block encoding given by VAL and
1123 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1124 true if the encoding may have redundant trailing blocks. */
1125 inline wide_int
1126 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1127 unsigned int precision, bool need_canon_p)
1129 wide_int result = wide_int::create (precision);
1130 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1131 need_canon_p));
1132 return result;
1135 /* Return an uninitialized wide_int with precision PRECISION. */
1136 inline wide_int
1137 wide_int_storage::create (unsigned int precision)
1139 wide_int x;
1140 x.precision = precision;
1141 return x;
1144 template <typename T1, typename T2>
1145 inline wide_int
1146 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1148 /* This shouldn't be used for two flexible-precision inputs. */
1149 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1150 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1151 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1152 return wide_int::create (wi::get_precision (y));
1153 else
1154 return wide_int::create (wi::get_precision (x));
1157 /* The storage used by FIXED_WIDE_INT (N). */
1158 template <int N>
1159 class GTY(()) fixed_wide_int_storage
1161 private:
1162 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1163 unsigned int len;
1165 public:
1166 fixed_wide_int_storage ();
1167 template <typename T>
1168 fixed_wide_int_storage (const T &);
1170 /* The standard generic_wide_int storage methods. */
1171 unsigned int get_precision () const;
1172 const HOST_WIDE_INT *get_val () const;
1173 unsigned int get_len () const;
1174 HOST_WIDE_INT *write_val ();
1175 void set_len (unsigned int, bool = false);
1177 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1178 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1179 bool = true);
1182 namespace wi
1184 template <int N>
1185 struct int_traits < fixed_wide_int_storage <N> >
1187 static const enum precision_type precision_type = CONST_PRECISION;
1188 static const bool host_dependent_precision = false;
1189 static const bool is_sign_extended = true;
1190 static const unsigned int precision = N;
1191 template <typename T1, typename T2>
1192 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1196 template <int N>
1197 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1199 /* Initialize the storage from integer X, in precision N. */
1200 template <int N>
1201 template <typename T>
1202 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1204 /* Check for type compatibility. We don't want to initialize a
1205 fixed-width integer from something like a wide_int. */
1206 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1207 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1210 template <int N>
1211 inline unsigned int
1212 fixed_wide_int_storage <N>::get_precision () const
1214 return N;
1217 template <int N>
1218 inline const HOST_WIDE_INT *
1219 fixed_wide_int_storage <N>::get_val () const
1221 return val;
1224 template <int N>
1225 inline unsigned int
1226 fixed_wide_int_storage <N>::get_len () const
1228 return len;
1231 template <int N>
1232 inline HOST_WIDE_INT *
1233 fixed_wide_int_storage <N>::write_val ()
1235 return val;
1238 template <int N>
1239 inline void
1240 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1242 len = l;
1243 /* There are no excess bits in val[len - 1]. */
1244 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1247 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1248 template <int N>
1249 inline FIXED_WIDE_INT (N)
1250 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1252 FIXED_WIDE_INT (N) result;
1253 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1254 x.precision, N, sgn));
1255 return result;
1258 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1259 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1260 trailing blocks. */
1261 template <int N>
1262 inline FIXED_WIDE_INT (N)
1263 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1264 unsigned int len,
1265 bool need_canon_p)
1267 FIXED_WIDE_INT (N) result;
1268 result.set_len (wi::from_array (result.write_val (), val, len,
1269 N, need_canon_p));
1270 return result;
1273 template <int N>
1274 template <typename T1, typename T2>
1275 inline FIXED_WIDE_INT (N)
1276 wi::int_traits < fixed_wide_int_storage <N> >::
1277 get_binary_result (const T1 &, const T2 &)
1279 return FIXED_WIDE_INT (N) ();
1282 /* A reference to one element of a trailing_wide_ints structure. */
1283 class trailing_wide_int_storage
1285 private:
1286 /* The precision of the integer, which is a fixed property of the
1287 parent trailing_wide_ints. */
1288 unsigned int m_precision;
1290 /* A pointer to the length field. */
1291 unsigned char *m_len;
1293 /* A pointer to the HWI array. There are enough elements to hold all
1294 values of precision M_PRECISION. */
1295 HOST_WIDE_INT *m_val;
1297 public:
1298 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1300 /* The standard generic_wide_int storage methods. */
1301 unsigned int get_len () const;
1302 unsigned int get_precision () const;
1303 const HOST_WIDE_INT *get_val () const;
1304 HOST_WIDE_INT *write_val ();
1305 void set_len (unsigned int, bool = false);
1307 template <typename T>
1308 trailing_wide_int_storage &operator = (const T &);
1311 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1313 /* trailing_wide_int behaves like a wide_int. */
1314 namespace wi
1316 template <>
1317 struct int_traits <trailing_wide_int_storage>
1318 : public int_traits <wide_int_storage> {};
1321 /* An array of N wide_int-like objects that can be put at the end of
1322 a variable-sized structure. Use extra_size to calculate how many
1323 bytes beyond the sizeof need to be allocated. Use set_precision
1324 to initialize the structure. */
1325 template <int N>
1326 class GTY(()) trailing_wide_ints
1328 private:
1329 /* The shared precision of each number. */
1330 unsigned short m_precision;
1332 /* The shared maximum length of each number. */
1333 unsigned char m_max_len;
1335 /* The current length of each number. */
1336 unsigned char m_len[N];
1338 /* The variable-length part of the structure, which always contains
1339 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1340 HOST_WIDE_INT m_val[1];
1342 public:
1343 void set_precision (unsigned int);
1344 trailing_wide_int operator [] (unsigned int);
1345 static size_t extra_size (unsigned int);
1348 inline trailing_wide_int_storage::
1349 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1350 HOST_WIDE_INT *val)
1351 : m_precision (precision), m_len (len), m_val (val)
1355 inline unsigned int
1356 trailing_wide_int_storage::get_len () const
1358 return *m_len;
1361 inline unsigned int
1362 trailing_wide_int_storage::get_precision () const
1364 return m_precision;
1367 inline const HOST_WIDE_INT *
1368 trailing_wide_int_storage::get_val () const
1370 return m_val;
1373 inline HOST_WIDE_INT *
1374 trailing_wide_int_storage::write_val ()
1376 return m_val;
1379 inline void
1380 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1382 *m_len = len;
1383 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1384 m_val[len - 1] = sext_hwi (m_val[len - 1],
1385 m_precision % HOST_BITS_PER_WIDE_INT);
1388 template <typename T>
1389 inline trailing_wide_int_storage &
1390 trailing_wide_int_storage::operator = (const T &x)
1392 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1393 wi::copy (*this, xi);
1394 return *this;
1397 /* Initialize the structure and record that all elements have precision
1398 PRECISION. */
1399 template <int N>
1400 inline void
1401 trailing_wide_ints <N>::set_precision (unsigned int precision)
1403 m_precision = precision;
1404 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1405 / HOST_BITS_PER_WIDE_INT);
1408 /* Return a reference to element INDEX. */
1409 template <int N>
1410 inline trailing_wide_int
1411 trailing_wide_ints <N>::operator [] (unsigned int index)
1413 return trailing_wide_int_storage (m_precision, &m_len[index],
1414 &m_val[index * m_max_len]);
1417 /* Return how many extra bytes need to be added to the end of the structure
1418 in order to handle N wide_ints of precision PRECISION. */
1419 template <int N>
1420 inline size_t
1421 trailing_wide_ints <N>::extra_size (unsigned int precision)
1423 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1424 / HOST_BITS_PER_WIDE_INT);
1425 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1428 /* This macro is used in structures that end with a trailing_wide_ints field
1429 called FIELD. It declares get_NAME() and set_NAME() methods to access
1430 element I of FIELD. */
1431 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1432 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1433 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1435 namespace wi
1437 /* Implementation of int_traits for primitive integer types like "int". */
1438 template <typename T, bool signed_p>
1439 struct primitive_int_traits
1441 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1442 static const bool host_dependent_precision = true;
1443 static const bool is_sign_extended = true;
1444 static unsigned int get_precision (T);
1445 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1449 template <typename T, bool signed_p>
1450 inline unsigned int
1451 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1453 return sizeof (T) * CHAR_BIT;
1456 template <typename T, bool signed_p>
1457 inline wi::storage_ref
1458 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1459 unsigned int precision, T x)
1461 scratch[0] = x;
1462 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1463 return wi::storage_ref (scratch, 1, precision);
1464 scratch[1] = 0;
1465 return wi::storage_ref (scratch, 2, precision);
1468 /* Allow primitive C types to be used in wi:: routines. */
1469 namespace wi
1471 template <>
1472 struct int_traits <unsigned char>
1473 : public primitive_int_traits <unsigned char, false> {};
1475 template <>
1476 struct int_traits <unsigned short>
1477 : public primitive_int_traits <unsigned short, false> {};
1479 template <>
1480 struct int_traits <int>
1481 : public primitive_int_traits <int, true> {};
1483 template <>
1484 struct int_traits <unsigned int>
1485 : public primitive_int_traits <unsigned int, false> {};
1487 template <>
1488 struct int_traits <long>
1489 : public primitive_int_traits <long, true> {};
1491 template <>
1492 struct int_traits <unsigned long>
1493 : public primitive_int_traits <unsigned long, false> {};
1495 #if defined HAVE_LONG_LONG
1496 template <>
1497 struct int_traits <long long>
1498 : public primitive_int_traits <long long, true> {};
1500 template <>
1501 struct int_traits <unsigned long long>
1502 : public primitive_int_traits <unsigned long long, false> {};
1503 #endif
1506 namespace wi
1508 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1509 and precision PRECISION. */
1510 struct hwi_with_prec
1512 hwi_with_prec () {}
1513 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1514 HOST_WIDE_INT val;
1515 unsigned int precision;
1516 signop sgn;
1519 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1520 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1522 hwi_with_prec minus_one (unsigned int);
1523 hwi_with_prec zero (unsigned int);
1524 hwi_with_prec one (unsigned int);
1525 hwi_with_prec two (unsigned int);
1528 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1529 signop s)
1530 : precision (p), sgn (s)
1532 if (precision < HOST_BITS_PER_WIDE_INT)
1533 val = sext_hwi (v, precision);
1534 else
1535 val = v;
1538 /* Return a signed integer that has value VAL and precision PRECISION. */
1539 inline wi::hwi_with_prec
1540 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1542 return hwi_with_prec (val, precision, SIGNED);
1545 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1546 inline wi::hwi_with_prec
1547 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1549 return hwi_with_prec (val, precision, UNSIGNED);
1552 /* Return a wide int of -1 with precision PRECISION. */
1553 inline wi::hwi_with_prec
1554 wi::minus_one (unsigned int precision)
1556 return wi::shwi (-1, precision);
1559 /* Return a wide int of 0 with precision PRECISION. */
1560 inline wi::hwi_with_prec
1561 wi::zero (unsigned int precision)
1563 return wi::shwi (0, precision);
1566 /* Return a wide int of 1 with precision PRECISION. */
1567 inline wi::hwi_with_prec
1568 wi::one (unsigned int precision)
1570 return wi::shwi (1, precision);
1573 /* Return a wide int of 2 with precision PRECISION. */
1574 inline wi::hwi_with_prec
1575 wi::two (unsigned int precision)
1577 return wi::shwi (2, precision);
1580 namespace wi
1582 /* ints_for<T>::zero (X) returns a zero that, when asssigned to a T,
1583 gives that T the same precision as X. */
1584 template<typename T, precision_type = int_traits<T>::precision_type>
1585 struct ints_for
1587 static int zero (const T &) { return 0; }
1590 template<typename T>
1591 struct ints_for<T, VAR_PRECISION>
1593 static hwi_with_prec zero (const T &);
1597 template<typename T>
1598 inline wi::hwi_with_prec
1599 wi::ints_for<T, wi::VAR_PRECISION>::zero (const T &x)
1601 return wi::zero (wi::get_precision (x));
1604 namespace wi
1606 template <>
1607 struct int_traits <wi::hwi_with_prec>
1609 static const enum precision_type precision_type = VAR_PRECISION;
1610 /* hwi_with_prec has an explicitly-given precision, rather than the
1611 precision of HOST_WIDE_INT. */
1612 static const bool host_dependent_precision = false;
1613 static const bool is_sign_extended = true;
1614 static unsigned int get_precision (const wi::hwi_with_prec &);
1615 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1616 const wi::hwi_with_prec &);
1620 inline unsigned int
1621 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1623 return x.precision;
1626 inline wi::storage_ref
1627 wi::int_traits <wi::hwi_with_prec>::
1628 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1629 const wi::hwi_with_prec &x)
1631 gcc_checking_assert (precision == x.precision);
1632 scratch[0] = x.val;
1633 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1634 return wi::storage_ref (scratch, 1, precision);
1635 scratch[1] = 0;
1636 return wi::storage_ref (scratch, 2, precision);
1639 /* Private functions for handling large cases out of line. They take
1640 individual length and array parameters because that is cheaper for
1641 the inline caller than constructing an object on the stack and
1642 passing a reference to it. (Although many callers use wide_int_refs,
1643 we generally want those to be removed by SRA.) */
1644 namespace wi
1646 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1647 const HOST_WIDE_INT *, unsigned int, unsigned int);
1648 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1649 const HOST_WIDE_INT *, unsigned int);
1650 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1651 const HOST_WIDE_INT *, unsigned int);
1652 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1653 const HOST_WIDE_INT *, unsigned int);
1654 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1655 const HOST_WIDE_INT *, unsigned int);
1656 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1657 unsigned int,
1658 unsigned int, unsigned int);
1659 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1660 unsigned int,
1661 unsigned int, unsigned int);
1662 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1663 unsigned int, unsigned int, unsigned int);
1664 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1665 unsigned int, unsigned int, unsigned int);
1666 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1667 unsigned int, unsigned int, unsigned int,
1668 unsigned int);
1669 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1670 unsigned int, unsigned int, unsigned int,
1671 unsigned int);
1672 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1673 const HOST_WIDE_INT *, unsigned int, unsigned int);
1674 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1675 unsigned int, const HOST_WIDE_INT *,
1676 unsigned int, unsigned int);
1677 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1678 const HOST_WIDE_INT *, unsigned int, unsigned int);
1679 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1680 unsigned int, const HOST_WIDE_INT *,
1681 unsigned int, unsigned int);
1682 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1683 const HOST_WIDE_INT *, unsigned int, unsigned int);
1684 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1685 const HOST_WIDE_INT *, unsigned int, unsigned int,
1686 signop, bool *);
1687 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1688 const HOST_WIDE_INT *, unsigned int, unsigned int,
1689 signop, bool *);
1690 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1691 unsigned int, const HOST_WIDE_INT *,
1692 unsigned int, unsigned int, signop, bool *,
1693 bool);
1694 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1695 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1696 unsigned int, unsigned int,
1697 const HOST_WIDE_INT *,
1698 unsigned int, unsigned int,
1699 signop, bool *);
1702 /* Return the number of bits that integer X can hold. */
1703 template <typename T>
1704 inline unsigned int
1705 wi::get_precision (const T &x)
1707 return wi::int_traits <T>::get_precision (x);
1710 /* Return the number of bits that the result of a binary operation can
1711 hold when the input operands are X and Y. */
1712 template <typename T1, typename T2>
1713 inline unsigned int
1714 wi::get_binary_precision (const T1 &x, const T2 &y)
1716 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1717 get_binary_result (x, y));
1720 /* Copy the contents of Y to X, but keeping X's current precision. */
1721 template <typename T1, typename T2>
1722 inline void
1723 wi::copy (T1 &x, const T2 &y)
1725 HOST_WIDE_INT *xval = x.write_val ();
1726 const HOST_WIDE_INT *yval = y.get_val ();
1727 unsigned int len = y.get_len ();
1728 unsigned int i = 0;
1730 xval[i] = yval[i];
1731 while (++i < len);
1732 x.set_len (len, y.is_sign_extended);
1735 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1736 template <typename T>
1737 inline bool
1738 wi::fits_shwi_p (const T &x)
1740 WIDE_INT_REF_FOR (T) xi (x);
1741 return xi.len == 1;
1744 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1745 precision. */
1746 template <typename T>
1747 inline bool
1748 wi::fits_uhwi_p (const T &x)
1750 WIDE_INT_REF_FOR (T) xi (x);
1751 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1752 return true;
1753 if (xi.len == 1)
1754 return xi.slow () >= 0;
1755 return xi.len == 2 && xi.uhigh () == 0;
1758 /* Return true if X is negative based on the interpretation of SGN.
1759 For UNSIGNED, this is always false. */
1760 template <typename T>
1761 inline bool
1762 wi::neg_p (const T &x, signop sgn)
1764 WIDE_INT_REF_FOR (T) xi (x);
1765 if (sgn == UNSIGNED)
1766 return false;
1767 return xi.sign_mask () < 0;
1770 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1771 template <typename T>
1772 inline HOST_WIDE_INT
1773 wi::sign_mask (const T &x)
1775 WIDE_INT_REF_FOR (T) xi (x);
1776 return xi.sign_mask ();
1779 /* Return true if X == Y. X and Y must be binary-compatible. */
1780 template <typename T1, typename T2>
1781 inline bool
1782 wi::eq_p (const T1 &x, const T2 &y)
1784 unsigned int precision = get_binary_precision (x, y);
1785 WIDE_INT_REF_FOR (T1) xi (x, precision);
1786 WIDE_INT_REF_FOR (T2) yi (y, precision);
1787 if (xi.is_sign_extended && yi.is_sign_extended)
1789 /* This case reduces to array equality. */
1790 if (xi.len != yi.len)
1791 return false;
1792 unsigned int i = 0;
1794 if (xi.val[i] != yi.val[i])
1795 return false;
1796 while (++i != xi.len);
1797 return true;
1799 if (__builtin_expect (yi.len == 1, true))
1801 /* XI is only equal to YI if it too has a single HWI. */
1802 if (xi.len != 1)
1803 return false;
1804 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1805 with 0 are simple. */
1806 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1807 return xi.val[0] == 0;
1808 /* Otherwise flush out any excess bits first. */
1809 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1810 int excess = HOST_BITS_PER_WIDE_INT - precision;
1811 if (excess > 0)
1812 diff <<= excess;
1813 return diff == 0;
1815 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1818 /* Return true if X != Y. X and Y must be binary-compatible. */
1819 template <typename T1, typename T2>
1820 inline bool
1821 wi::ne_p (const T1 &x, const T2 &y)
1823 return !eq_p (x, y);
1826 /* Return true if X < Y when both are treated as signed values. */
1827 template <typename T1, typename T2>
1828 inline bool
1829 wi::lts_p (const T1 &x, const T2 &y)
1831 unsigned int precision = get_binary_precision (x, y);
1832 WIDE_INT_REF_FOR (T1) xi (x, precision);
1833 WIDE_INT_REF_FOR (T2) yi (y, precision);
1834 /* We optimize x < y, where y is 64 or fewer bits. */
1835 if (wi::fits_shwi_p (yi))
1837 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1838 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1839 return neg_p (xi);
1840 /* If x fits directly into a shwi, we can compare directly. */
1841 if (wi::fits_shwi_p (xi))
1842 return xi.to_shwi () < yi.to_shwi ();
1843 /* If x doesn't fit and is negative, then it must be more
1844 negative than any value in y, and hence smaller than y. */
1845 if (neg_p (xi))
1846 return true;
1847 /* If x is positive, then it must be larger than any value in y,
1848 and hence greater than y. */
1849 return false;
1851 /* Optimize the opposite case, if it can be detected at compile time. */
1852 if (STATIC_CONSTANT_P (xi.len == 1))
1853 /* If YI is negative it is lower than the least HWI.
1854 If YI is positive it is greater than the greatest HWI. */
1855 return !neg_p (yi);
1856 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1859 /* Return true if X < Y when both are treated as unsigned values. */
1860 template <typename T1, typename T2>
1861 inline bool
1862 wi::ltu_p (const T1 &x, const T2 &y)
1864 unsigned int precision = get_binary_precision (x, y);
1865 WIDE_INT_REF_FOR (T1) xi (x, precision);
1866 WIDE_INT_REF_FOR (T2) yi (y, precision);
1867 /* Optimize comparisons with constants. */
1868 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1869 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1870 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1871 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1872 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1873 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1874 values does not change the result. */
1875 if (__builtin_expect (xi.len + yi.len == 2, true))
1877 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1878 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1879 return xl < yl;
1881 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1884 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1885 template <typename T1, typename T2>
1886 inline bool
1887 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1889 if (sgn == SIGNED)
1890 return lts_p (x, y);
1891 else
1892 return ltu_p (x, y);
1895 /* Return true if X <= Y when both are treated as signed values. */
1896 template <typename T1, typename T2>
1897 inline bool
1898 wi::les_p (const T1 &x, const T2 &y)
1900 return !lts_p (y, x);
1903 /* Return true if X <= Y when both are treated as unsigned values. */
1904 template <typename T1, typename T2>
1905 inline bool
1906 wi::leu_p (const T1 &x, const T2 &y)
1908 return !ltu_p (y, x);
1911 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1912 template <typename T1, typename T2>
1913 inline bool
1914 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1916 if (sgn == SIGNED)
1917 return les_p (x, y);
1918 else
1919 return leu_p (x, y);
1922 /* Return true if X > Y when both are treated as signed values. */
1923 template <typename T1, typename T2>
1924 inline bool
1925 wi::gts_p (const T1 &x, const T2 &y)
1927 return lts_p (y, x);
1930 /* Return true if X > Y when both are treated as unsigned values. */
1931 template <typename T1, typename T2>
1932 inline bool
1933 wi::gtu_p (const T1 &x, const T2 &y)
1935 return ltu_p (y, x);
1938 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1939 template <typename T1, typename T2>
1940 inline bool
1941 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1943 if (sgn == SIGNED)
1944 return gts_p (x, y);
1945 else
1946 return gtu_p (x, y);
1949 /* Return true if X >= Y when both are treated as signed values. */
1950 template <typename T1, typename T2>
1951 inline bool
1952 wi::ges_p (const T1 &x, const T2 &y)
1954 return !lts_p (x, y);
1957 /* Return true if X >= Y when both are treated as unsigned values. */
1958 template <typename T1, typename T2>
1959 inline bool
1960 wi::geu_p (const T1 &x, const T2 &y)
1962 return !ltu_p (x, y);
1965 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1966 template <typename T1, typename T2>
1967 inline bool
1968 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1970 if (sgn == SIGNED)
1971 return ges_p (x, y);
1972 else
1973 return geu_p (x, y);
1976 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1977 as signed values. */
1978 template <typename T1, typename T2>
1979 inline int
1980 wi::cmps (const T1 &x, const T2 &y)
1982 unsigned int precision = get_binary_precision (x, y);
1983 WIDE_INT_REF_FOR (T1) xi (x, precision);
1984 WIDE_INT_REF_FOR (T2) yi (y, precision);
1985 if (wi::fits_shwi_p (yi))
1987 /* Special case for comparisons with 0. */
1988 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1989 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1990 /* If x fits into a signed HWI, we can compare directly. */
1991 if (wi::fits_shwi_p (xi))
1993 HOST_WIDE_INT xl = xi.to_shwi ();
1994 HOST_WIDE_INT yl = yi.to_shwi ();
1995 return xl < yl ? -1 : xl > yl;
1997 /* If x doesn't fit and is negative, then it must be more
1998 negative than any signed HWI, and hence smaller than y. */
1999 if (neg_p (xi))
2000 return -1;
2001 /* If x is positive, then it must be larger than any signed HWI,
2002 and hence greater than y. */
2003 return 1;
2005 /* Optimize the opposite case, if it can be detected at compile time. */
2006 if (STATIC_CONSTANT_P (xi.len == 1))
2007 /* If YI is negative it is lower than the least HWI.
2008 If YI is positive it is greater than the greatest HWI. */
2009 return neg_p (yi) ? 1 : -1;
2010 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
2013 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2014 as unsigned values. */
2015 template <typename T1, typename T2>
2016 inline int
2017 wi::cmpu (const T1 &x, const T2 &y)
2019 unsigned int precision = get_binary_precision (x, y);
2020 WIDE_INT_REF_FOR (T1) xi (x, precision);
2021 WIDE_INT_REF_FOR (T2) yi (y, precision);
2022 /* Optimize comparisons with constants. */
2023 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2025 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2026 if (xi.len != 1)
2027 return 1;
2028 /* Otherwise compare directly. */
2029 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2030 unsigned HOST_WIDE_INT yl = yi.val[0];
2031 return xl < yl ? -1 : xl > yl;
2033 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2035 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2036 if (yi.len != 1)
2037 return -1;
2038 /* Otherwise compare directly. */
2039 unsigned HOST_WIDE_INT xl = xi.val[0];
2040 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2041 return xl < yl ? -1 : xl > yl;
2043 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2044 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2045 values does not change the result. */
2046 if (__builtin_expect (xi.len + yi.len == 2, true))
2048 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2049 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2050 return xl < yl ? -1 : xl > yl;
2052 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2055 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2056 X and Y indicated by SGN. */
2057 template <typename T1, typename T2>
2058 inline int
2059 wi::cmp (const T1 &x, const T2 &y, signop sgn)
2061 if (sgn == SIGNED)
2062 return cmps (x, y);
2063 else
2064 return cmpu (x, y);
2067 /* Return ~x. */
2068 template <typename T>
2069 inline WI_UNARY_RESULT (T)
2070 wi::bit_not (const T &x)
2072 WI_UNARY_RESULT_VAR (result, val, T, x);
2073 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2074 for (unsigned int i = 0; i < xi.len; ++i)
2075 val[i] = ~xi.val[i];
2076 result.set_len (xi.len);
2077 return result;
2080 /* Return -x. */
2081 template <typename T>
2082 inline WI_UNARY_RESULT (T)
2083 wi::neg (const T &x)
2085 return sub (0, x);
2088 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2089 template <typename T>
2090 inline WI_UNARY_RESULT (T)
2091 wi::neg (const T &x, bool *overflow)
2093 *overflow = only_sign_bit_p (x);
2094 return sub (0, x);
2097 /* Return the absolute value of x. */
2098 template <typename T>
2099 inline WI_UNARY_RESULT (T)
2100 wi::abs (const T &x)
2102 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2105 /* Return the result of sign-extending the low OFFSET bits of X. */
2106 template <typename T>
2107 inline WI_UNARY_RESULT (T)
2108 wi::sext (const T &x, unsigned int offset)
2110 WI_UNARY_RESULT_VAR (result, val, T, x);
2111 unsigned int precision = get_precision (result);
2112 WIDE_INT_REF_FOR (T) xi (x, precision);
2114 if (offset <= HOST_BITS_PER_WIDE_INT)
2116 val[0] = sext_hwi (xi.ulow (), offset);
2117 result.set_len (1, true);
2119 else
2120 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2121 return result;
2124 /* Return the result of zero-extending the low OFFSET bits of X. */
2125 template <typename T>
2126 inline WI_UNARY_RESULT (T)
2127 wi::zext (const T &x, unsigned int offset)
2129 WI_UNARY_RESULT_VAR (result, val, T, x);
2130 unsigned int precision = get_precision (result);
2131 WIDE_INT_REF_FOR (T) xi (x, precision);
2133 /* This is not just an optimization, it is actually required to
2134 maintain canonization. */
2135 if (offset >= precision)
2137 wi::copy (result, xi);
2138 return result;
2141 /* In these cases we know that at least the top bit will be clear,
2142 so no sign extension is necessary. */
2143 if (offset < HOST_BITS_PER_WIDE_INT)
2145 val[0] = zext_hwi (xi.ulow (), offset);
2146 result.set_len (1, true);
2148 else
2149 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2150 return result;
2153 /* Return the result of extending the low OFFSET bits of X according to
2154 signedness SGN. */
2155 template <typename T>
2156 inline WI_UNARY_RESULT (T)
2157 wi::ext (const T &x, unsigned int offset, signop sgn)
2159 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2162 /* Return an integer that represents X | (1 << bit). */
2163 template <typename T>
2164 inline WI_UNARY_RESULT (T)
2165 wi::set_bit (const T &x, unsigned int bit)
2167 WI_UNARY_RESULT_VAR (result, val, T, x);
2168 unsigned int precision = get_precision (result);
2169 WIDE_INT_REF_FOR (T) xi (x, precision);
2170 if (precision <= HOST_BITS_PER_WIDE_INT)
2172 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2173 result.set_len (1);
2175 else
2176 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2177 return result;
2180 /* Return the mininum of X and Y, treating them both as having
2181 signedness SGN. */
2182 template <typename T1, typename T2>
2183 inline WI_BINARY_RESULT (T1, T2)
2184 wi::min (const T1 &x, const T2 &y, signop sgn)
2186 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2187 unsigned int precision = get_precision (result);
2188 if (wi::le_p (x, y, sgn))
2189 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2190 else
2191 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2192 return result;
2195 /* Return the minimum of X and Y, treating both as signed values. */
2196 template <typename T1, typename T2>
2197 inline WI_BINARY_RESULT (T1, T2)
2198 wi::smin (const T1 &x, const T2 &y)
2200 return wi::min (x, y, SIGNED);
2203 /* Return the minimum of X and Y, treating both as unsigned values. */
2204 template <typename T1, typename T2>
2205 inline WI_BINARY_RESULT (T1, T2)
2206 wi::umin (const T1 &x, const T2 &y)
2208 return wi::min (x, y, UNSIGNED);
2211 /* Return the maxinum of X and Y, treating them both as having
2212 signedness SGN. */
2213 template <typename T1, typename T2>
2214 inline WI_BINARY_RESULT (T1, T2)
2215 wi::max (const T1 &x, const T2 &y, signop sgn)
2217 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2218 unsigned int precision = get_precision (result);
2219 if (wi::ge_p (x, y, sgn))
2220 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2221 else
2222 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2223 return result;
2226 /* Return the maximum of X and Y, treating both as signed values. */
2227 template <typename T1, typename T2>
2228 inline WI_BINARY_RESULT (T1, T2)
2229 wi::smax (const T1 &x, const T2 &y)
2231 return wi::max (x, y, SIGNED);
2234 /* Return the maximum of X and Y, treating both as unsigned values. */
2235 template <typename T1, typename T2>
2236 inline WI_BINARY_RESULT (T1, T2)
2237 wi::umax (const T1 &x, const T2 &y)
2239 return wi::max (x, y, UNSIGNED);
2242 /* Return X & Y. */
2243 template <typename T1, typename T2>
2244 inline WI_BINARY_RESULT (T1, T2)
2245 wi::bit_and (const T1 &x, const T2 &y)
2247 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2248 unsigned int precision = get_precision (result);
2249 WIDE_INT_REF_FOR (T1) xi (x, precision);
2250 WIDE_INT_REF_FOR (T2) yi (y, precision);
2251 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2252 if (__builtin_expect (xi.len + yi.len == 2, true))
2254 val[0] = xi.ulow () & yi.ulow ();
2255 result.set_len (1, is_sign_extended);
2257 else
2258 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2259 precision), is_sign_extended);
2260 return result;
2263 /* Return X & ~Y. */
2264 template <typename T1, typename T2>
2265 inline WI_BINARY_RESULT (T1, T2)
2266 wi::bit_and_not (const T1 &x, const T2 &y)
2268 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2269 unsigned int precision = get_precision (result);
2270 WIDE_INT_REF_FOR (T1) xi (x, precision);
2271 WIDE_INT_REF_FOR (T2) yi (y, precision);
2272 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2273 if (__builtin_expect (xi.len + yi.len == 2, true))
2275 val[0] = xi.ulow () & ~yi.ulow ();
2276 result.set_len (1, is_sign_extended);
2278 else
2279 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2280 precision), is_sign_extended);
2281 return result;
2284 /* Return X | Y. */
2285 template <typename T1, typename T2>
2286 inline WI_BINARY_RESULT (T1, T2)
2287 wi::bit_or (const T1 &x, const T2 &y)
2289 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2290 unsigned int precision = get_precision (result);
2291 WIDE_INT_REF_FOR (T1) xi (x, precision);
2292 WIDE_INT_REF_FOR (T2) yi (y, precision);
2293 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2294 if (__builtin_expect (xi.len + yi.len == 2, true))
2296 val[0] = xi.ulow () | yi.ulow ();
2297 result.set_len (1, is_sign_extended);
2299 else
2300 result.set_len (or_large (val, xi.val, xi.len,
2301 yi.val, yi.len, precision), is_sign_extended);
2302 return result;
2305 /* Return X | ~Y. */
2306 template <typename T1, typename T2>
2307 inline WI_BINARY_RESULT (T1, T2)
2308 wi::bit_or_not (const T1 &x, const T2 &y)
2310 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2311 unsigned int precision = get_precision (result);
2312 WIDE_INT_REF_FOR (T1) xi (x, precision);
2313 WIDE_INT_REF_FOR (T2) yi (y, precision);
2314 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2315 if (__builtin_expect (xi.len + yi.len == 2, true))
2317 val[0] = xi.ulow () | ~yi.ulow ();
2318 result.set_len (1, is_sign_extended);
2320 else
2321 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2322 precision), is_sign_extended);
2323 return result;
2326 /* Return X ^ Y. */
2327 template <typename T1, typename T2>
2328 inline WI_BINARY_RESULT (T1, T2)
2329 wi::bit_xor (const T1 &x, const T2 &y)
2331 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2332 unsigned int precision = get_precision (result);
2333 WIDE_INT_REF_FOR (T1) xi (x, precision);
2334 WIDE_INT_REF_FOR (T2) yi (y, precision);
2335 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2336 if (__builtin_expect (xi.len + yi.len == 2, true))
2338 val[0] = xi.ulow () ^ yi.ulow ();
2339 result.set_len (1, is_sign_extended);
2341 else
2342 result.set_len (xor_large (val, xi.val, xi.len,
2343 yi.val, yi.len, precision), is_sign_extended);
2344 return result;
2347 /* Return X + Y. */
2348 template <typename T1, typename T2>
2349 inline WI_BINARY_RESULT (T1, T2)
2350 wi::add (const T1 &x, const T2 &y)
2352 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2353 unsigned int precision = get_precision (result);
2354 WIDE_INT_REF_FOR (T1) xi (x, precision);
2355 WIDE_INT_REF_FOR (T2) yi (y, precision);
2356 if (precision <= HOST_BITS_PER_WIDE_INT)
2358 val[0] = xi.ulow () + yi.ulow ();
2359 result.set_len (1);
2361 /* If the precision is known at compile time to be greater than
2362 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2363 knowing that (a) all bits in those HWIs are significant and
2364 (b) the result has room for at least two HWIs. This provides
2365 a fast path for things like offset_int and widest_int.
2367 The STATIC_CONSTANT_P test prevents this path from being
2368 used for wide_ints. wide_ints with precisions greater than
2369 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2370 point handling them inline. */
2371 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2372 && __builtin_expect (xi.len + yi.len == 2, true))
2374 unsigned HOST_WIDE_INT xl = xi.ulow ();
2375 unsigned HOST_WIDE_INT yl = yi.ulow ();
2376 unsigned HOST_WIDE_INT resultl = xl + yl;
2377 val[0] = resultl;
2378 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2379 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2380 >> (HOST_BITS_PER_WIDE_INT - 1)));
2382 else
2383 result.set_len (add_large (val, xi.val, xi.len,
2384 yi.val, yi.len, precision,
2385 UNSIGNED, 0));
2386 return result;
2389 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2390 and indicate in *OVERFLOW whether the operation overflowed. */
2391 template <typename T1, typename T2>
2392 inline WI_BINARY_RESULT (T1, T2)
2393 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2395 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2396 unsigned int precision = get_precision (result);
2397 WIDE_INT_REF_FOR (T1) xi (x, precision);
2398 WIDE_INT_REF_FOR (T2) yi (y, precision);
2399 if (precision <= HOST_BITS_PER_WIDE_INT)
2401 unsigned HOST_WIDE_INT xl = xi.ulow ();
2402 unsigned HOST_WIDE_INT yl = yi.ulow ();
2403 unsigned HOST_WIDE_INT resultl = xl + yl;
2404 if (sgn == SIGNED)
2405 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2406 >> (precision - 1)) & 1;
2407 else
2408 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2409 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2410 val[0] = resultl;
2411 result.set_len (1);
2413 else
2414 result.set_len (add_large (val, xi.val, xi.len,
2415 yi.val, yi.len, precision,
2416 sgn, overflow));
2417 return result;
2420 /* Return X - Y. */
2421 template <typename T1, typename T2>
2422 inline WI_BINARY_RESULT (T1, T2)
2423 wi::sub (const T1 &x, const T2 &y)
2425 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2426 unsigned int precision = get_precision (result);
2427 WIDE_INT_REF_FOR (T1) xi (x, precision);
2428 WIDE_INT_REF_FOR (T2) yi (y, precision);
2429 if (precision <= HOST_BITS_PER_WIDE_INT)
2431 val[0] = xi.ulow () - yi.ulow ();
2432 result.set_len (1);
2434 /* If the precision is known at compile time to be greater than
2435 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2436 knowing that (a) all bits in those HWIs are significant and
2437 (b) the result has room for at least two HWIs. This provides
2438 a fast path for things like offset_int and widest_int.
2440 The STATIC_CONSTANT_P test prevents this path from being
2441 used for wide_ints. wide_ints with precisions greater than
2442 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2443 point handling them inline. */
2444 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2445 && __builtin_expect (xi.len + yi.len == 2, true))
2447 unsigned HOST_WIDE_INT xl = xi.ulow ();
2448 unsigned HOST_WIDE_INT yl = yi.ulow ();
2449 unsigned HOST_WIDE_INT resultl = xl - yl;
2450 val[0] = resultl;
2451 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2452 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2453 >> (HOST_BITS_PER_WIDE_INT - 1)));
2455 else
2456 result.set_len (sub_large (val, xi.val, xi.len,
2457 yi.val, yi.len, precision,
2458 UNSIGNED, 0));
2459 return result;
2462 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2463 and indicate in *OVERFLOW whether the operation overflowed. */
2464 template <typename T1, typename T2>
2465 inline WI_BINARY_RESULT (T1, T2)
2466 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2468 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2469 unsigned int precision = get_precision (result);
2470 WIDE_INT_REF_FOR (T1) xi (x, precision);
2471 WIDE_INT_REF_FOR (T2) yi (y, precision);
2472 if (precision <= HOST_BITS_PER_WIDE_INT)
2474 unsigned HOST_WIDE_INT xl = xi.ulow ();
2475 unsigned HOST_WIDE_INT yl = yi.ulow ();
2476 unsigned HOST_WIDE_INT resultl = xl - yl;
2477 if (sgn == SIGNED)
2478 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2479 else
2480 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2481 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2482 val[0] = resultl;
2483 result.set_len (1);
2485 else
2486 result.set_len (sub_large (val, xi.val, xi.len,
2487 yi.val, yi.len, precision,
2488 sgn, overflow));
2489 return result;
2492 /* Return X * Y. */
2493 template <typename T1, typename T2>
2494 inline WI_BINARY_RESULT (T1, T2)
2495 wi::mul (const T1 &x, const T2 &y)
2497 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2498 unsigned int precision = get_precision (result);
2499 WIDE_INT_REF_FOR (T1) xi (x, precision);
2500 WIDE_INT_REF_FOR (T2) yi (y, precision);
2501 if (precision <= HOST_BITS_PER_WIDE_INT)
2503 val[0] = xi.ulow () * yi.ulow ();
2504 result.set_len (1);
2506 else
2507 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2508 precision, UNSIGNED, 0, false));
2509 return result;
2512 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2513 and indicate in *OVERFLOW whether the operation overflowed. */
2514 template <typename T1, typename T2>
2515 inline WI_BINARY_RESULT (T1, T2)
2516 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2518 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2519 unsigned int precision = get_precision (result);
2520 WIDE_INT_REF_FOR (T1) xi (x, precision);
2521 WIDE_INT_REF_FOR (T2) yi (y, precision);
2522 result.set_len (mul_internal (val, xi.val, xi.len,
2523 yi.val, yi.len, precision,
2524 sgn, overflow, false));
2525 return result;
2528 /* Return X * Y, treating both X and Y as signed values. Indicate in
2529 *OVERFLOW whether the operation overflowed. */
2530 template <typename T1, typename T2>
2531 inline WI_BINARY_RESULT (T1, T2)
2532 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2534 return mul (x, y, SIGNED, overflow);
2537 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2538 *OVERFLOW whether the operation overflowed. */
2539 template <typename T1, typename T2>
2540 inline WI_BINARY_RESULT (T1, T2)
2541 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2543 return mul (x, y, UNSIGNED, overflow);
2546 /* Perform a widening multiplication of X and Y, extending the values
2547 according to SGN, and return the high part of the result. */
2548 template <typename T1, typename T2>
2549 inline WI_BINARY_RESULT (T1, T2)
2550 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2552 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2553 unsigned int precision = get_precision (result);
2554 WIDE_INT_REF_FOR (T1) xi (x, precision);
2555 WIDE_INT_REF_FOR (T2) yi (y, precision);
2556 result.set_len (mul_internal (val, xi.val, xi.len,
2557 yi.val, yi.len, precision,
2558 sgn, 0, true));
2559 return result;
2562 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2563 signedness given by SGN. Indicate in *OVERFLOW if the result
2564 overflows. */
2565 template <typename T1, typename T2>
2566 inline WI_BINARY_RESULT (T1, T2)
2567 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2569 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2570 unsigned int precision = get_precision (quotient);
2571 WIDE_INT_REF_FOR (T1) xi (x, precision);
2572 WIDE_INT_REF_FOR (T2) yi (y);
2574 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2575 precision,
2576 yi.val, yi.len, yi.precision,
2577 sgn, overflow));
2578 return quotient;
2581 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2582 template <typename T1, typename T2>
2583 inline WI_BINARY_RESULT (T1, T2)
2584 wi::sdiv_trunc (const T1 &x, const T2 &y)
2586 return div_trunc (x, y, SIGNED);
2589 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2590 template <typename T1, typename T2>
2591 inline WI_BINARY_RESULT (T1, T2)
2592 wi::udiv_trunc (const T1 &x, const T2 &y)
2594 return div_trunc (x, y, UNSIGNED);
2597 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2598 signedness given by SGN. Indicate in *OVERFLOW if the result
2599 overflows. */
2600 template <typename T1, typename T2>
2601 inline WI_BINARY_RESULT (T1, T2)
2602 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2604 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2605 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2606 unsigned int precision = get_precision (quotient);
2607 WIDE_INT_REF_FOR (T1) xi (x, precision);
2608 WIDE_INT_REF_FOR (T2) yi (y);
2610 unsigned int remainder_len;
2611 quotient.set_len (divmod_internal (quotient_val,
2612 &remainder_len, remainder_val,
2613 xi.val, xi.len, precision,
2614 yi.val, yi.len, yi.precision, sgn,
2615 overflow));
2616 remainder.set_len (remainder_len);
2617 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2618 return quotient - 1;
2619 return quotient;
2622 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2623 template <typename T1, typename T2>
2624 inline WI_BINARY_RESULT (T1, T2)
2625 wi::sdiv_floor (const T1 &x, const T2 &y)
2627 return div_floor (x, y, SIGNED);
2630 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2631 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2632 template <typename T1, typename T2>
2633 inline WI_BINARY_RESULT (T1, T2)
2634 wi::udiv_floor (const T1 &x, const T2 &y)
2636 return div_floor (x, y, UNSIGNED);
2639 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2640 signedness given by SGN. Indicate in *OVERFLOW if the result
2641 overflows. */
2642 template <typename T1, typename T2>
2643 inline WI_BINARY_RESULT (T1, T2)
2644 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2646 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2647 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2648 unsigned int precision = get_precision (quotient);
2649 WIDE_INT_REF_FOR (T1) xi (x, precision);
2650 WIDE_INT_REF_FOR (T2) yi (y);
2652 unsigned int remainder_len;
2653 quotient.set_len (divmod_internal (quotient_val,
2654 &remainder_len, remainder_val,
2655 xi.val, xi.len, precision,
2656 yi.val, yi.len, yi.precision, sgn,
2657 overflow));
2658 remainder.set_len (remainder_len);
2659 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2660 return quotient + 1;
2661 return quotient;
2664 /* Return X / Y, rouding towards nearest with ties away from zero.
2665 Treat X and Y as having the signedness given by SGN. Indicate
2666 in *OVERFLOW if the result overflows. */
2667 template <typename T1, typename T2>
2668 inline WI_BINARY_RESULT (T1, T2)
2669 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2671 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2672 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2673 unsigned int precision = get_precision (quotient);
2674 WIDE_INT_REF_FOR (T1) xi (x, precision);
2675 WIDE_INT_REF_FOR (T2) yi (y);
2677 unsigned int remainder_len;
2678 quotient.set_len (divmod_internal (quotient_val,
2679 &remainder_len, remainder_val,
2680 xi.val, xi.len, precision,
2681 yi.val, yi.len, yi.precision, sgn,
2682 overflow));
2683 remainder.set_len (remainder_len);
2685 if (remainder != 0)
2687 if (sgn == SIGNED)
2689 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2690 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2692 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2693 return quotient - 1;
2694 else
2695 return quotient + 1;
2698 else
2700 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2701 return quotient + 1;
2704 return quotient;
2707 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2708 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2709 template <typename T1, typename T2>
2710 inline WI_BINARY_RESULT (T1, T2)
2711 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2712 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2714 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2715 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2716 unsigned int precision = get_precision (quotient);
2717 WIDE_INT_REF_FOR (T1) xi (x, precision);
2718 WIDE_INT_REF_FOR (T2) yi (y);
2720 unsigned int remainder_len;
2721 quotient.set_len (divmod_internal (quotient_val,
2722 &remainder_len, remainder_val,
2723 xi.val, xi.len, precision,
2724 yi.val, yi.len, yi.precision, sgn, 0));
2725 remainder.set_len (remainder_len);
2727 *remainder_ptr = remainder;
2728 return quotient;
2731 /* Compute the greatest common divisor of two numbers A and B using
2732 Euclid's algorithm. */
2733 template <typename T1, typename T2>
2734 inline WI_BINARY_RESULT (T1, T2)
2735 wi::gcd (const T1 &a, const T2 &b, signop sgn)
2737 T1 x, y, z;
2739 x = wi::abs (a);
2740 y = wi::abs (b);
2742 while (gt_p (x, 0, sgn))
2744 z = mod_trunc (y, x, sgn);
2745 y = x;
2746 x = z;
2749 return y;
2752 /* Compute X / Y, rouding towards 0, and return the remainder.
2753 Treat X and Y as having the signedness given by SGN. Indicate
2754 in *OVERFLOW if the division overflows. */
2755 template <typename T1, typename T2>
2756 inline WI_BINARY_RESULT (T1, T2)
2757 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2759 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2760 unsigned int precision = get_precision (remainder);
2761 WIDE_INT_REF_FOR (T1) xi (x, precision);
2762 WIDE_INT_REF_FOR (T2) yi (y);
2764 unsigned int remainder_len;
2765 divmod_internal (0, &remainder_len, remainder_val,
2766 xi.val, xi.len, precision,
2767 yi.val, yi.len, yi.precision, sgn, overflow);
2768 remainder.set_len (remainder_len);
2770 return remainder;
2773 /* Compute X / Y, rouding towards 0, and return the remainder.
2774 Treat X and Y as signed values. */
2775 template <typename T1, typename T2>
2776 inline WI_BINARY_RESULT (T1, T2)
2777 wi::smod_trunc (const T1 &x, const T2 &y)
2779 return mod_trunc (x, y, SIGNED);
2782 /* Compute X / Y, rouding towards 0, and return the remainder.
2783 Treat X and Y as unsigned values. */
2784 template <typename T1, typename T2>
2785 inline WI_BINARY_RESULT (T1, T2)
2786 wi::umod_trunc (const T1 &x, const T2 &y)
2788 return mod_trunc (x, y, UNSIGNED);
2791 /* Compute X / Y, rouding towards -inf, and return the remainder.
2792 Treat X and Y as having the signedness given by SGN. Indicate
2793 in *OVERFLOW if the division overflows. */
2794 template <typename T1, typename T2>
2795 inline WI_BINARY_RESULT (T1, T2)
2796 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2798 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2799 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2800 unsigned int precision = get_precision (quotient);
2801 WIDE_INT_REF_FOR (T1) xi (x, precision);
2802 WIDE_INT_REF_FOR (T2) yi (y);
2804 unsigned int remainder_len;
2805 quotient.set_len (divmod_internal (quotient_val,
2806 &remainder_len, remainder_val,
2807 xi.val, xi.len, precision,
2808 yi.val, yi.len, yi.precision, sgn,
2809 overflow));
2810 remainder.set_len (remainder_len);
2812 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2813 return remainder + y;
2814 return remainder;
2817 /* Compute X / Y, rouding towards -inf, and return the remainder.
2818 Treat X and Y as unsigned values. */
2819 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2820 template <typename T1, typename T2>
2821 inline WI_BINARY_RESULT (T1, T2)
2822 wi::umod_floor (const T1 &x, const T2 &y)
2824 return mod_floor (x, y, UNSIGNED);
2827 /* Compute X / Y, rouding towards +inf, and return the remainder.
2828 Treat X and Y as having the signedness given by SGN. Indicate
2829 in *OVERFLOW if the division overflows. */
2830 template <typename T1, typename T2>
2831 inline WI_BINARY_RESULT (T1, T2)
2832 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2834 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2835 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2836 unsigned int precision = get_precision (quotient);
2837 WIDE_INT_REF_FOR (T1) xi (x, precision);
2838 WIDE_INT_REF_FOR (T2) yi (y);
2840 unsigned int remainder_len;
2841 quotient.set_len (divmod_internal (quotient_val,
2842 &remainder_len, remainder_val,
2843 xi.val, xi.len, precision,
2844 yi.val, yi.len, yi.precision, sgn,
2845 overflow));
2846 remainder.set_len (remainder_len);
2848 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2849 return remainder - y;
2850 return remainder;
2853 /* Compute X / Y, rouding towards nearest with ties away from zero,
2854 and return the remainder. Treat X and Y as having the signedness
2855 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2856 template <typename T1, typename T2>
2857 inline WI_BINARY_RESULT (T1, T2)
2858 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2860 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2861 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2862 unsigned int precision = get_precision (quotient);
2863 WIDE_INT_REF_FOR (T1) xi (x, precision);
2864 WIDE_INT_REF_FOR (T2) yi (y);
2866 unsigned int remainder_len;
2867 quotient.set_len (divmod_internal (quotient_val,
2868 &remainder_len, remainder_val,
2869 xi.val, xi.len, precision,
2870 yi.val, yi.len, yi.precision, sgn,
2871 overflow));
2872 remainder.set_len (remainder_len);
2874 if (remainder != 0)
2876 if (sgn == SIGNED)
2878 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2879 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2881 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2882 return remainder + y;
2883 else
2884 return remainder - y;
2887 else
2889 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2890 return remainder - y;
2893 return remainder;
2896 /* Return true if X is a multiple of Y. Treat X and Y as having the
2897 signedness given by SGN. */
2898 template <typename T1, typename T2>
2899 inline bool
2900 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2902 return wi::mod_trunc (x, y, sgn) == 0;
2905 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2906 Treat X and Y as having the signedness given by SGN. */
2907 template <typename T1, typename T2>
2908 inline bool
2909 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2910 WI_BINARY_RESULT (T1, T2) *res)
2912 WI_BINARY_RESULT (T1, T2) remainder;
2913 WI_BINARY_RESULT (T1, T2) quotient
2914 = divmod_trunc (x, y, sgn, &remainder);
2915 if (remainder == 0)
2917 *res = quotient;
2918 return true;
2920 return false;
2923 /* Return X << Y. Return 0 if Y is greater than or equal to
2924 the precision of X. */
2925 template <typename T1, typename T2>
2926 inline WI_UNARY_RESULT (T1)
2927 wi::lshift (const T1 &x, const T2 &y)
2929 WI_UNARY_RESULT_VAR (result, val, T1, x);
2930 unsigned int precision = get_precision (result);
2931 WIDE_INT_REF_FOR (T1) xi (x, precision);
2932 WIDE_INT_REF_FOR (T2) yi (y);
2933 /* Handle the simple cases quickly. */
2934 if (geu_p (yi, precision))
2936 val[0] = 0;
2937 result.set_len (1);
2939 else
2941 unsigned int shift = yi.to_uhwi ();
2942 /* For fixed-precision integers like offset_int and widest_int,
2943 handle the case where the shift value is constant and the
2944 result is a single nonnegative HWI (meaning that we don't
2945 need to worry about val[1]). This is particularly common
2946 for converting a byte count to a bit count.
2948 For variable-precision integers like wide_int, handle HWI
2949 and sub-HWI integers inline. */
2950 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2951 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2952 && xi.len == 1
2953 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2954 HOST_WIDE_INT_MAX >> shift))
2955 : precision <= HOST_BITS_PER_WIDE_INT)
2957 val[0] = xi.ulow () << shift;
2958 result.set_len (1);
2960 else
2961 result.set_len (lshift_large (val, xi.val, xi.len,
2962 precision, shift));
2964 return result;
2967 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2968 or equal to the precision of X. */
2969 template <typename T1, typename T2>
2970 inline WI_UNARY_RESULT (T1)
2971 wi::lrshift (const T1 &x, const T2 &y)
2973 WI_UNARY_RESULT_VAR (result, val, T1, x);
2974 /* Do things in the precision of the input rather than the output,
2975 since the result can be no larger than that. */
2976 WIDE_INT_REF_FOR (T1) xi (x);
2977 WIDE_INT_REF_FOR (T2) yi (y);
2978 /* Handle the simple cases quickly. */
2979 if (geu_p (yi, xi.precision))
2981 val[0] = 0;
2982 result.set_len (1);
2984 else
2986 unsigned int shift = yi.to_uhwi ();
2987 /* For fixed-precision integers like offset_int and widest_int,
2988 handle the case where the shift value is constant and the
2989 shifted value is a single nonnegative HWI (meaning that all
2990 bits above the HWI are zero). This is particularly common
2991 for converting a bit count to a byte count.
2993 For variable-precision integers like wide_int, handle HWI
2994 and sub-HWI integers inline. */
2995 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2996 ? (shift < HOST_BITS_PER_WIDE_INT
2997 && xi.len == 1
2998 && xi.val[0] >= 0)
2999 : xi.precision <= HOST_BITS_PER_WIDE_INT)
3001 val[0] = xi.to_uhwi () >> shift;
3002 result.set_len (1);
3004 else
3005 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
3006 get_precision (result), shift));
3008 return result;
3011 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
3012 Y is greater than or equal to the precision of X. */
3013 template <typename T1, typename T2>
3014 inline WI_UNARY_RESULT (T1)
3015 wi::arshift (const T1 &x, const T2 &y)
3017 WI_UNARY_RESULT_VAR (result, val, T1, x);
3018 /* Do things in the precision of the input rather than the output,
3019 since the result can be no larger than that. */
3020 WIDE_INT_REF_FOR (T1) xi (x);
3021 WIDE_INT_REF_FOR (T2) yi (y);
3022 /* Handle the simple cases quickly. */
3023 if (geu_p (yi, xi.precision))
3025 val[0] = sign_mask (x);
3026 result.set_len (1);
3028 else
3030 unsigned int shift = yi.to_uhwi ();
3031 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
3033 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3034 result.set_len (1, true);
3036 else
3037 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3038 get_precision (result), shift));
3040 return result;
3043 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3044 logical shift otherwise. */
3045 template <typename T1, typename T2>
3046 inline WI_UNARY_RESULT (T1)
3047 wi::rshift (const T1 &x, const T2 &y, signop sgn)
3049 if (sgn == UNSIGNED)
3050 return lrshift (x, y);
3051 else
3052 return arshift (x, y);
3055 /* Return the result of rotating the low WIDTH bits of X left by Y
3056 bits and zero-extending the result. Use a full-width rotate if
3057 WIDTH is zero. */
3058 template <typename T1, typename T2>
3059 WI_UNARY_RESULT (T1)
3060 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3062 unsigned int precision = get_binary_precision (x, x);
3063 if (width == 0)
3064 width = precision;
3065 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3066 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3067 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
3068 if (width != precision)
3069 return wi::zext (left, width) | wi::zext (right, width);
3070 return left | right;
3073 /* Return the result of rotating the low WIDTH bits of X right by Y
3074 bits and zero-extending the result. Use a full-width rotate if
3075 WIDTH is zero. */
3076 template <typename T1, typename T2>
3077 WI_UNARY_RESULT (T1)
3078 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3080 unsigned int precision = get_binary_precision (x, x);
3081 if (width == 0)
3082 width = precision;
3083 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3084 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
3085 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3086 if (width != precision)
3087 return wi::zext (left, width) | wi::zext (right, width);
3088 return left | right;
3091 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3092 is odd. */
3093 inline int
3094 wi::parity (const wide_int_ref &x)
3096 return popcount (x) & 1;
3099 /* Extract WIDTH bits from X, starting at BITPOS. */
3100 template <typename T>
3101 inline unsigned HOST_WIDE_INT
3102 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3104 unsigned precision = get_precision (x);
3105 if (precision < bitpos + width)
3106 precision = bitpos + width;
3107 WIDE_INT_REF_FOR (T) xi (x, precision);
3109 /* Handle this rare case after the above, so that we assert about
3110 bogus BITPOS values. */
3111 if (width == 0)
3112 return 0;
3114 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3115 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3116 unsigned HOST_WIDE_INT res = xi.elt (start);
3117 res >>= shift;
3118 if (shift + width > HOST_BITS_PER_WIDE_INT)
3120 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3121 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3123 return zext_hwi (res, width);
3126 /* Return the minimum precision needed to store X with sign SGN. */
3127 template <typename T>
3128 inline unsigned int
3129 wi::min_precision (const T &x, signop sgn)
3131 if (sgn == SIGNED)
3132 return get_precision (x) - clrsb (x);
3133 else
3134 return get_precision (x) - clz (x);
3137 #define SIGNED_BINARY_PREDICATE(OP, F) \
3138 template <typename T1, typename T2> \
3139 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3140 OP (const T1 &x, const T2 &y) \
3142 return wi::F (x, y); \
3145 SIGNED_BINARY_PREDICATE (operator <, lts_p)
3146 SIGNED_BINARY_PREDICATE (operator <=, les_p)
3147 SIGNED_BINARY_PREDICATE (operator >, gts_p)
3148 SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3150 #undef SIGNED_BINARY_PREDICATE
3152 #define UNARY_OPERATOR(OP, F) \
3153 template<typename T> \
3154 WI_UNARY_RESULT (generic_wide_int<T>) \
3155 OP (const generic_wide_int<T> &x) \
3157 return wi::F (x); \
3160 #define BINARY_PREDICATE(OP, F) \
3161 template<typename T1, typename T2> \
3162 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3163 OP (const T1 &x, const T2 &y) \
3165 return wi::F (x, y); \
3168 #define BINARY_OPERATOR(OP, F) \
3169 template<typename T1, typename T2> \
3170 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3171 OP (const T1 &x, const T2 &y) \
3173 return wi::F (x, y); \
3176 #define SHIFT_OPERATOR(OP, F) \
3177 template<typename T1, typename T2> \
3178 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3179 OP (const T1 &x, const T2 &y) \
3181 return wi::F (x, y); \
3184 UNARY_OPERATOR (operator ~, bit_not)
3185 UNARY_OPERATOR (operator -, neg)
3186 BINARY_PREDICATE (operator ==, eq_p)
3187 BINARY_PREDICATE (operator !=, ne_p)
3188 BINARY_OPERATOR (operator &, bit_and)
3189 BINARY_OPERATOR (operator |, bit_or)
3190 BINARY_OPERATOR (operator ^, bit_xor)
3191 BINARY_OPERATOR (operator +, add)
3192 BINARY_OPERATOR (operator -, sub)
3193 BINARY_OPERATOR (operator *, mul)
3194 SHIFT_OPERATOR (operator <<, lshift)
3196 #undef UNARY_OPERATOR
3197 #undef BINARY_PREDICATE
3198 #undef BINARY_OPERATOR
3199 #undef SHIFT_OPERATOR
3201 template <typename T1, typename T2>
3202 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3203 operator >> (const T1 &x, const T2 &y)
3205 return wi::arshift (x, y);
3208 template <typename T1, typename T2>
3209 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3210 operator / (const T1 &x, const T2 &y)
3212 return wi::sdiv_trunc (x, y);
3215 template <typename T1, typename T2>
3216 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3217 operator % (const T1 &x, const T2 &y)
3219 return wi::smod_trunc (x, y);
3222 template<typename T>
3223 void
3224 gt_ggc_mx (generic_wide_int <T> *)
3228 template<typename T>
3229 void
3230 gt_pch_nx (generic_wide_int <T> *)
3234 template<typename T>
3235 void
3236 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3240 template<int N>
3241 void
3242 gt_ggc_mx (trailing_wide_ints <N> *)
3246 template<int N>
3247 void
3248 gt_pch_nx (trailing_wide_ints <N> *)
3252 template<int N>
3253 void
3254 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3258 namespace wi
3260 /* Used for overloaded functions in which the only other acceptable
3261 scalar type is a pointer. It stops a plain 0 from being treated
3262 as a null pointer. */
3263 struct never_used1 {};
3264 struct never_used2 {};
3266 wide_int min_value (unsigned int, signop);
3267 wide_int min_value (never_used1 *);
3268 wide_int min_value (never_used2 *);
3269 wide_int max_value (unsigned int, signop);
3270 wide_int max_value (never_used1 *);
3271 wide_int max_value (never_used2 *);
3273 /* FIXME: this is target dependent, so should be elsewhere.
3274 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3275 wide_int from_buffer (const unsigned char *, unsigned int);
3277 #ifndef GENERATOR_FILE
3278 void to_mpz (const wide_int_ref &, mpz_t, signop);
3279 #endif
3281 wide_int mask (unsigned int, bool, unsigned int);
3282 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3283 wide_int set_bit_in_zero (unsigned int, unsigned int);
3284 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3285 unsigned int);
3287 template <typename T>
3288 T mask (unsigned int, bool);
3290 template <typename T>
3291 T shifted_mask (unsigned int, unsigned int, bool);
3293 template <typename T>
3294 T set_bit_in_zero (unsigned int);
3296 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3297 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3298 bool, unsigned int);
3299 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3300 unsigned int, unsigned int, bool);
3303 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3304 and the other bits are clear, or the inverse if NEGATE_P. */
3305 inline wide_int
3306 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3308 wide_int result = wide_int::create (precision);
3309 result.set_len (mask (result.write_val (), width, negate_p, precision));
3310 return result;
3313 /* Return a PRECISION-bit integer in which the low START bits are clear,
3314 the next WIDTH bits are set, and the other bits are clear,
3315 or the inverse if NEGATE_P. */
3316 inline wide_int
3317 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3318 unsigned int precision)
3320 wide_int result = wide_int::create (precision);
3321 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3322 precision));
3323 return result;
3326 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3327 others are clear. */
3328 inline wide_int
3329 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3331 return shifted_mask (bit, 1, false, precision);
3334 /* Return an integer of type T in which the low WIDTH bits are set
3335 and the other bits are clear, or the inverse if NEGATE_P. */
3336 template <typename T>
3337 inline T
3338 wi::mask (unsigned int width, bool negate_p)
3340 STATIC_ASSERT (wi::int_traits<T>::precision);
3341 T result;
3342 result.set_len (mask (result.write_val (), width, negate_p,
3343 wi::int_traits <T>::precision));
3344 return result;
3347 /* Return an integer of type T in which the low START bits are clear,
3348 the next WIDTH bits are set, and the other bits are clear, or the
3349 inverse if NEGATE_P. */
3350 template <typename T>
3351 inline T
3352 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3354 STATIC_ASSERT (wi::int_traits<T>::precision);
3355 T result;
3356 result.set_len (shifted_mask (result.write_val (), start, width,
3357 negate_p,
3358 wi::int_traits <T>::precision));
3359 return result;
3362 /* Return an integer of type T in which bit BIT is set and all the
3363 others are clear. */
3364 template <typename T>
3365 inline T
3366 wi::set_bit_in_zero (unsigned int bit)
3368 return shifted_mask <T> (bit, 1, false);
3371 #endif /* WIDE_INT_H */