2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / wide-int.h
blobd8f7b46080b41f27b83701cf1135331a0006e473
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2015 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 size representation that is
57 guaranteed to be large enough to compute any bit or byte sized
58 address calculation on the target. Currently the value is 64 + 4
59 bits rounded up to the next number even multiple of
60 HOST_BITS_PER_WIDE_INT (but this can be changed when the first
61 port needs more than 64 bits for the size of a pointer).
63 This flavor can be used for all address math on the target. In
64 this representation, the values are sign or zero extended based
65 on their input types to the internal precision. All math is done
66 in this precision and then the values are truncated to fit in the
67 result type. Unlike most gimple or rtl intermediate code, it is
68 not useful to perform the address arithmetic at the same
69 precision in which the operands are represented because there has
70 been no effort by the front ends to convert most addressing
71 arithmetic to canonical types.
73 3) widest_int. This representation is an approximation of
74 infinite precision math. However, it is not really infinite
75 precision math as in the GMP library. It is really finite
76 precision math where the precision is 4 times the size of the
77 largest integer that the target port can represent.
79 widest_int is supposed to be wider than any number that it needs to
80 store, meaning that there is always at least one leading sign bit.
81 All widest_int values are therefore signed.
83 There are several places in the GCC where this should/must be used:
85 * Code that does induction variable optimizations. This code
86 works with induction variables of many different types at the
87 same time. Because of this, it ends up doing many different
88 calculations where the operands are not compatible types. The
89 widest_int makes this easy, because it provides a field where
90 nothing is lost when converting from any variable,
92 * There are a small number of passes that currently use the
93 widest_int that should use the default. These should be
94 changed.
96 There are surprising features of offset_int and widest_int
97 that the users should be careful about:
99 1) Shifts and rotations are just weird. You have to specify a
100 precision in which the shift or rotate is to happen in. The bits
101 above this precision are zeroed. While this is what you
102 want, it is clearly non obvious.
104 2) Larger precision math sometimes does not produce the same
105 answer as would be expected for doing the math at the proper
106 precision. In particular, a multiply followed by a divide will
107 produce a different answer if the first product is larger than
108 what can be represented in the input precision.
110 The offset_int and the widest_int flavors are more expensive
111 than the default wide int, so in addition to the caveats with these
112 two, the default is the prefered representation.
114 All three flavors of wide_int are represented as a vector of
115 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
116 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
117 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
118 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
119 in element 0.
121 The default wide_int contains three fields: the vector (VAL),
122 the precision and a length (LEN). The length is the number of HWIs
123 needed to represent the value. widest_int and offset_int have a
124 constant precision that cannot be changed, so they only store the
125 VAL and LEN fields.
127 Since most integers used in a compiler are small values, it is
128 generally profitable to use a representation of the value that is
129 as small as possible. LEN is used to indicate the number of
130 elements of the vector that are in use. The numbers are stored as
131 sign extended numbers as a means of compression. Leading
132 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
133 as long as they can be reconstructed from the top bit that is being
134 represented.
136 The precision and length of a wide_int are always greater than 0.
137 Any bits in a wide_int above the precision are sign-extended from the
138 most significant bit. For example, a 4-bit value 0x8 is represented as
139 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
140 constants to be represented with undefined bits above the precision.
141 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
142 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
143 and in wider precisions.
145 There are constructors to create the various forms of wide_int from
146 trees, rtl and constants. For trees you can simply say:
148 tree t = ...;
149 wide_int x = t;
151 However, a little more syntax is required for rtl constants since
152 they do not have an explicit precision. To make an rtl into a
153 wide_int, you have to pair it with a mode. The canonical way to do
154 this is with std::make_pair as in:
156 rtx r = ...
157 wide_int x = std::make_pair (r, mode);
159 Similarly, a wide_int can only be constructed from a host value if
160 the target precision is given explicitly, such as in:
162 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
163 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
165 However, offset_int and widest_int have an inherent precision and so
166 can be initialized directly from a host value:
168 offset_int x = (int) c; // sign-extend C
169 widest_int x = (unsigned int) c; // zero-extend C
171 It is also possible to do arithmetic directly on trees, rtxes and
172 constants. For example:
174 wi::add (t1, t2); // add equal-sized INTEGER_CSTs t1 and t2
175 wi::add (t1, 1); // add 1 to INTEGER_CST t1
176 wi::add (r1, r2); // add equal-sized rtx constants r1 and r2
177 wi::lshift (1, 100); // 1 << 100 as a widest_int
179 Many binary operations place restrictions on the combinations of inputs,
180 using the following rules:
182 - {tree, rtx, wide_int} op {tree, rtx, wide_int} -> wide_int
183 The inputs must be the same precision. The result is a wide_int
184 of the same precision
186 - {tree, rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
187 (un)signed HOST_WIDE_INT op {tree, rtx, wide_int} -> wide_int
188 The HOST_WIDE_INT is extended or truncated to the precision of
189 the other input. The result is a wide_int of the same precision
190 as that input.
192 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
193 The inputs are extended to widest_int precision and produce a
194 widest_int result.
196 - offset_int op offset_int -> offset_int
197 offset_int op (un)signed HOST_WIDE_INT -> offset_int
198 (un)signed HOST_WIDE_INT op offset_int -> offset_int
200 - widest_int op widest_int -> widest_int
201 widest_int op (un)signed HOST_WIDE_INT -> widest_int
202 (un)signed HOST_WIDE_INT op widest_int -> widest_int
204 Other combinations like:
206 - widest_int op offset_int and
207 - wide_int op offset_int
209 are not allowed. The inputs should instead be extended or truncated
210 so that they match.
212 The inputs to comparison functions like wi::eq_p and wi::lts_p
213 follow the same compatibility rules, although their return types
214 are different. Unary functions on X produce the same result as
215 a binary operation X + X. Shift functions X op Y also produce
216 the same result as X + X; the precision of the shift amount Y
217 can be arbitrarily different from X. */
219 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
220 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
221 can accomodate at least 1 more bit so that unsigned numbers of that
222 mode can be represented as a signed value. Note that it is still
223 possible to create fixed_wide_ints that have precisions greater than
224 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
225 double-width multiplication result, for example. */
226 #define WIDE_INT_MAX_ELTS \
227 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
229 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
231 /* This is the max size of any pointer on any machine. It does not
232 seem to be as easy to sniff this out of the machine description as
233 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
234 multiple address sizes and may have different address sizes for
235 different address spaces. However, currently the largest pointer
236 on any platform is 64 bits. When that changes, then it is likely
237 that a target hook should be defined so that targets can make this
238 value larger for those targets. */
239 #define ADDR_MAX_BITSIZE 64
241 /* This is the internal precision used when doing any address
242 arithmetic. The '4' is really 3 + 1. Three of the bits are for
243 the number of extra bits needed to do bit addresses and the other bit
244 is to allow everything to be signed without loosing any precision.
245 Then everything is rounded up to the next HWI for efficiency. */
246 #define ADDR_MAX_PRECISION \
247 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
248 & ~(HOST_BITS_PER_WIDE_INT - 1))
250 /* The number of HWIs needed to store an offset_int. */
251 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
253 /* The type of result produced by a binary operation on types T1 and T2.
254 Defined purely for brevity. */
255 #define WI_BINARY_RESULT(T1, T2) \
256 typename wi::binary_traits <T1, T2>::result_type
258 /* The type of result produced by a unary operation on type T. */
259 #define WI_UNARY_RESULT(T) \
260 typename wi::unary_traits <T>::result_type
262 /* Define a variable RESULT to hold the result of a binary operation on
263 X and Y, which have types T1 and T2 respectively. Define VAL to
264 point to the blocks of RESULT. Once the user of the macro has
265 filled in VAL, it should call RESULT.set_len to set the number
266 of initialized blocks. */
267 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
268 WI_BINARY_RESULT (T1, T2) RESULT = \
269 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
270 HOST_WIDE_INT *VAL = RESULT.write_val ()
272 /* Similar for the result of a unary operation on X, which has type T. */
273 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
274 WI_UNARY_RESULT (T) RESULT = \
275 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
276 HOST_WIDE_INT *VAL = RESULT.write_val ()
278 template <typename T> class generic_wide_int;
279 template <int N> struct fixed_wide_int_storage;
280 class wide_int_storage;
282 /* An N-bit integer. Until we can use typedef templates, use this instead. */
283 #define FIXED_WIDE_INT(N) \
284 generic_wide_int < fixed_wide_int_storage <N> >
286 typedef generic_wide_int <wide_int_storage> wide_int;
287 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
288 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
290 template <bool SE>
291 struct wide_int_ref_storage;
293 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
295 /* This can be used instead of wide_int_ref if the referenced value is
296 known to have type T. It carries across properties of T's representation,
297 such as whether excess upper bits in a HWI are defined, and can therefore
298 help avoid redundant work.
300 The macro could be replaced with a template typedef, once we're able
301 to use those. */
302 #define WIDE_INT_REF_FOR(T) \
303 generic_wide_int \
304 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
306 namespace wi
308 /* Classifies an integer based on its precision. */
309 enum precision_type {
310 /* The integer has both a precision and defined signedness. This allows
311 the integer to be converted to any width, since we know whether to fill
312 any extra bits with zeros or signs. */
313 FLEXIBLE_PRECISION,
315 /* The integer has a variable precision but no defined signedness. */
316 VAR_PRECISION,
318 /* The integer has a constant precision (known at GCC compile time)
319 but no defined signedness. */
320 CONST_PRECISION
323 /* This class, which has no default implementation, is expected to
324 provide the following members:
326 static const enum precision_type precision_type;
327 Classifies the type of T.
329 static const unsigned int precision;
330 Only defined if precision_type == CONST_PRECISION. Specifies the
331 precision of all integers of type T.
333 static const bool host_dependent_precision;
334 True if the precision of T depends (or can depend) on the host.
336 static unsigned int get_precision (const T &x)
337 Return the number of bits in X.
339 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
340 unsigned int precision, const T &x)
341 Decompose X as a PRECISION-bit integer, returning the associated
342 wi::storage_ref. SCRATCH is available as scratch space if needed.
343 The routine should assert that PRECISION is acceptable. */
344 template <typename T> struct int_traits;
346 /* This class provides a single type, result_type, which specifies the
347 type of integer produced by a binary operation whose inputs have
348 types T1 and T2. The definition should be symmetric. */
349 template <typename T1, typename T2,
350 enum precision_type P1 = int_traits <T1>::precision_type,
351 enum precision_type P2 = int_traits <T2>::precision_type>
352 struct binary_traits;
354 /* The result of a unary operation on T is the same as the result of
355 a binary operation on two values of type T. */
356 template <typename T>
357 struct unary_traits : public binary_traits <T, T> {};
359 /* Specify the result type for each supported combination of binary
360 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
361 mixed, in order to give stronger type checking. When both inputs
362 are CONST_PRECISION, they must have the same precision. */
363 template <>
364 template <typename T1, typename T2>
365 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
367 typedef widest_int result_type;
370 template <>
371 template <typename T1, typename T2>
372 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
374 typedef wide_int result_type;
377 template <>
378 template <typename T1, typename T2>
379 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
381 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
382 so as not to confuse gengtype. */
383 typedef generic_wide_int < fixed_wide_int_storage
384 <int_traits <T2>::precision> > result_type;
387 template <>
388 template <typename T1, typename T2>
389 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
391 typedef wide_int result_type;
394 template <>
395 template <typename T1, typename T2>
396 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
398 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
399 so as not to confuse gengtype. */
400 typedef generic_wide_int < fixed_wide_int_storage
401 <int_traits <T1>::precision> > result_type;
404 template <>
405 template <typename T1, typename T2>
406 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
408 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
409 so as not to confuse gengtype. */
410 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
411 typedef generic_wide_int < fixed_wide_int_storage
412 <int_traits <T1>::precision> > result_type;
415 template <>
416 template <typename T1, typename T2>
417 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
419 typedef wide_int result_type;
423 /* Public functions for querying and operating on integers. */
424 namespace wi
426 template <typename T>
427 unsigned int get_precision (const T &);
429 template <typename T1, typename T2>
430 unsigned int get_binary_precision (const T1 &, const T2 &);
432 template <typename T1, typename T2>
433 void copy (T1 &, const T2 &);
435 #define UNARY_PREDICATE \
436 template <typename T> bool
437 #define UNARY_FUNCTION \
438 template <typename T> WI_UNARY_RESULT (T)
439 #define BINARY_PREDICATE \
440 template <typename T1, typename T2> bool
441 #define BINARY_FUNCTION \
442 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
443 #define SHIFT_FUNCTION \
444 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
446 UNARY_PREDICATE fits_shwi_p (const T &);
447 UNARY_PREDICATE fits_uhwi_p (const T &);
448 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
450 template <typename T>
451 HOST_WIDE_INT sign_mask (const T &);
453 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
454 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
455 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
456 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
457 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
458 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
459 BINARY_PREDICATE les_p (const T1 &, const T2 &);
460 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
461 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
462 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
463 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
464 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
465 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
466 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
468 template <typename T1, typename T2>
469 int cmp (const T1 &, const T2 &, signop);
471 template <typename T1, typename T2>
472 int cmps (const T1 &, const T2 &);
474 template <typename T1, typename T2>
475 int cmpu (const T1 &, const T2 &);
477 UNARY_FUNCTION bit_not (const T &);
478 UNARY_FUNCTION neg (const T &);
479 UNARY_FUNCTION neg (const T &, bool *);
480 UNARY_FUNCTION abs (const T &);
481 UNARY_FUNCTION ext (const T &, unsigned int, signop);
482 UNARY_FUNCTION sext (const T &, unsigned int);
483 UNARY_FUNCTION zext (const T &, unsigned int);
484 UNARY_FUNCTION set_bit (const T &, unsigned int);
486 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
487 BINARY_FUNCTION smin (const T1 &, const T2 &);
488 BINARY_FUNCTION umin (const T1 &, const T2 &);
489 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
490 BINARY_FUNCTION smax (const T1 &, const T2 &);
491 BINARY_FUNCTION umax (const T1 &, const T2 &);
493 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
494 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
495 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
496 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
497 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
498 BINARY_FUNCTION add (const T1 &, const T2 &);
499 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
500 BINARY_FUNCTION sub (const T1 &, const T2 &);
501 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
502 BINARY_FUNCTION mul (const T1 &, const T2 &);
503 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
504 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
505 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
506 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
507 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
508 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
509 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
510 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
511 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
512 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
513 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
514 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
515 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
516 WI_BINARY_RESULT (T1, T2) *);
517 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
518 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
519 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
520 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
521 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
522 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
523 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
525 template <typename T1, typename T2>
526 bool multiple_of_p (const T1 &, const T2 &, signop);
528 template <typename T1, typename T2>
529 bool multiple_of_p (const T1 &, const T2 &, signop,
530 WI_BINARY_RESULT (T1, T2) *);
532 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
533 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
534 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
535 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
536 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
537 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
539 #undef SHIFT_FUNCTION
540 #undef BINARY_PREDICATE
541 #undef BINARY_FUNCTION
542 #undef UNARY_PREDICATE
543 #undef UNARY_FUNCTION
545 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
546 bool only_sign_bit_p (const wide_int_ref &);
547 int clz (const wide_int_ref &);
548 int clrsb (const wide_int_ref &);
549 int ctz (const wide_int_ref &);
550 int exact_log2 (const wide_int_ref &);
551 int floor_log2 (const wide_int_ref &);
552 int ffs (const wide_int_ref &);
553 int popcount (const wide_int_ref &);
554 int parity (const wide_int_ref &);
556 template <typename T>
557 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
559 template <typename T>
560 unsigned int min_precision (const T &, signop);
563 namespace wi
565 /* Contains the components of a decomposed integer for easy, direct
566 access. */
567 struct storage_ref
569 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
571 const HOST_WIDE_INT *val;
572 unsigned int len;
573 unsigned int precision;
575 /* Provide enough trappings for this class to act as storage for
576 generic_wide_int. */
577 unsigned int get_len () const;
578 unsigned int get_precision () const;
579 const HOST_WIDE_INT *get_val () const;
583 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
584 unsigned int len_in,
585 unsigned int precision_in)
586 : val (val_in), len (len_in), precision (precision_in)
590 inline unsigned int
591 wi::storage_ref::get_len () const
593 return len;
596 inline unsigned int
597 wi::storage_ref::get_precision () const
599 return precision;
602 inline const HOST_WIDE_INT *
603 wi::storage_ref::get_val () const
605 return val;
608 /* This class defines an integer type using the storage provided by the
609 template argument. The storage class must provide the following
610 functions:
612 unsigned int get_precision () const
613 Return the number of bits in the integer.
615 HOST_WIDE_INT *get_val () const
616 Return a pointer to the array of blocks that encodes the integer.
618 unsigned int get_len () const
619 Return the number of blocks in get_val (). If this is smaller
620 than the number of blocks implied by get_precision (), the
621 remaining blocks are sign extensions of block get_len () - 1.
623 Although not required by generic_wide_int itself, writable storage
624 classes can also provide the following functions:
626 HOST_WIDE_INT *write_val ()
627 Get a modifiable version of get_val ()
629 unsigned int set_len (unsigned int len)
630 Set the value returned by get_len () to LEN. */
631 template <typename storage>
632 class GTY(()) generic_wide_int : public storage
634 public:
635 generic_wide_int ();
637 template <typename T>
638 generic_wide_int (const T &);
640 template <typename T>
641 generic_wide_int (const T &, unsigned int);
643 /* Conversions. */
644 HOST_WIDE_INT to_shwi (unsigned int) const;
645 HOST_WIDE_INT to_shwi () const;
646 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
647 unsigned HOST_WIDE_INT to_uhwi () const;
648 HOST_WIDE_INT to_short_addr () const;
650 /* Public accessors for the interior of a wide int. */
651 HOST_WIDE_INT sign_mask () const;
652 HOST_WIDE_INT elt (unsigned int) const;
653 unsigned HOST_WIDE_INT ulow () const;
654 unsigned HOST_WIDE_INT uhigh () const;
655 HOST_WIDE_INT slow () const;
656 HOST_WIDE_INT shigh () const;
658 template <typename T>
659 generic_wide_int &operator = (const T &);
661 #define BINARY_PREDICATE(OP, F) \
662 template <typename T> \
663 bool OP (const T &c) const { return wi::F (*this, c); }
665 #define UNARY_OPERATOR(OP, F) \
666 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
668 #define BINARY_OPERATOR(OP, F) \
669 template <typename T> \
670 WI_BINARY_RESULT (generic_wide_int, T) \
671 OP (const T &c) const { return wi::F (*this, c); }
673 #define ASSIGNMENT_OPERATOR(OP, F) \
674 template <typename T> \
675 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
677 #define INCDEC_OPERATOR(OP, DELTA) \
678 generic_wide_int &OP () { *this += DELTA; return *this; }
680 UNARY_OPERATOR (operator ~, bit_not)
681 UNARY_OPERATOR (operator -, neg)
682 BINARY_PREDICATE (operator ==, eq_p)
683 BINARY_PREDICATE (operator !=, ne_p)
684 BINARY_OPERATOR (operator &, bit_and)
685 BINARY_OPERATOR (and_not, bit_and_not)
686 BINARY_OPERATOR (operator |, bit_or)
687 BINARY_OPERATOR (or_not, bit_or_not)
688 BINARY_OPERATOR (operator ^, bit_xor)
689 BINARY_OPERATOR (operator +, add)
690 BINARY_OPERATOR (operator -, sub)
691 BINARY_OPERATOR (operator *, mul)
692 ASSIGNMENT_OPERATOR (operator &=, bit_and)
693 ASSIGNMENT_OPERATOR (operator |=, bit_or)
694 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
695 ASSIGNMENT_OPERATOR (operator +=, add)
696 ASSIGNMENT_OPERATOR (operator -=, sub)
697 ASSIGNMENT_OPERATOR (operator *=, mul)
698 INCDEC_OPERATOR (operator ++, 1)
699 INCDEC_OPERATOR (operator --, -1)
701 #undef BINARY_PREDICATE
702 #undef UNARY_OPERATOR
703 #undef BINARY_OPERATOR
704 #undef ASSIGNMENT_OPERATOR
705 #undef INCDEC_OPERATOR
707 /* Debugging functions. */
708 void dump () const;
710 static const bool is_sign_extended
711 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
714 template <typename storage>
715 inline generic_wide_int <storage>::generic_wide_int () {}
717 template <typename storage>
718 template <typename T>
719 inline generic_wide_int <storage>::generic_wide_int (const T &x)
720 : storage (x)
724 template <typename storage>
725 template <typename T>
726 inline generic_wide_int <storage>::generic_wide_int (const T &x,
727 unsigned int precision)
728 : storage (x, precision)
732 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
733 If THIS does not fit in PRECISION, the information is lost. */
734 template <typename storage>
735 inline HOST_WIDE_INT
736 generic_wide_int <storage>::to_shwi (unsigned int precision) const
738 if (precision < HOST_BITS_PER_WIDE_INT)
739 return sext_hwi (this->get_val ()[0], precision);
740 else
741 return this->get_val ()[0];
744 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
745 template <typename storage>
746 inline HOST_WIDE_INT
747 generic_wide_int <storage>::to_shwi () const
749 if (is_sign_extended)
750 return this->get_val ()[0];
751 else
752 return to_shwi (this->get_precision ());
755 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
756 PRECISION. If THIS does not fit in PRECISION, the information
757 is lost. */
758 template <typename storage>
759 inline unsigned HOST_WIDE_INT
760 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
762 if (precision < HOST_BITS_PER_WIDE_INT)
763 return zext_hwi (this->get_val ()[0], precision);
764 else
765 return this->get_val ()[0];
768 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
769 template <typename storage>
770 inline unsigned HOST_WIDE_INT
771 generic_wide_int <storage>::to_uhwi () const
773 return to_uhwi (this->get_precision ());
776 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
777 represent addresses to using offset_int to represent addresses.
778 We use to_short_addr at the interface from new code to old,
779 unconverted code. */
780 template <typename storage>
781 inline HOST_WIDE_INT
782 generic_wide_int <storage>::to_short_addr () const
784 return this->get_val ()[0];
787 /* Return the implicit value of blocks above get_len (). */
788 template <typename storage>
789 inline HOST_WIDE_INT
790 generic_wide_int <storage>::sign_mask () const
792 unsigned int len = this->get_len ();
793 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
794 if (!is_sign_extended)
796 unsigned int precision = this->get_precision ();
797 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
798 if (excess > 0)
799 high <<= excess;
801 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
804 /* Return the signed value of the least-significant explicitly-encoded
805 block. */
806 template <typename storage>
807 inline HOST_WIDE_INT
808 generic_wide_int <storage>::slow () const
810 return this->get_val ()[0];
813 /* Return the signed value of the most-significant explicitly-encoded
814 block. */
815 template <typename storage>
816 inline HOST_WIDE_INT
817 generic_wide_int <storage>::shigh () const
819 return this->get_val ()[this->get_len () - 1];
822 /* Return the unsigned value of the least-significant
823 explicitly-encoded block. */
824 template <typename storage>
825 inline unsigned HOST_WIDE_INT
826 generic_wide_int <storage>::ulow () const
828 return this->get_val ()[0];
831 /* Return the unsigned value of the most-significant
832 explicitly-encoded block. */
833 template <typename storage>
834 inline unsigned HOST_WIDE_INT
835 generic_wide_int <storage>::uhigh () const
837 return this->get_val ()[this->get_len () - 1];
840 /* Return block I, which might be implicitly or explicit encoded. */
841 template <typename storage>
842 inline HOST_WIDE_INT
843 generic_wide_int <storage>::elt (unsigned int i) const
845 if (i >= this->get_len ())
846 return sign_mask ();
847 else
848 return this->get_val ()[i];
851 template <typename storage>
852 template <typename T>
853 generic_wide_int <storage> &
854 generic_wide_int <storage>::operator = (const T &x)
856 storage::operator = (x);
857 return *this;
860 /* Dump the contents of the integer to stderr, for debugging. */
861 template <typename storage>
862 void
863 generic_wide_int <storage>::dump () const
865 unsigned int len = this->get_len ();
866 const HOST_WIDE_INT *val = this->get_val ();
867 unsigned int precision = this->get_precision ();
868 fprintf (stderr, "[");
869 if (len * HOST_BITS_PER_WIDE_INT < precision)
870 fprintf (stderr, "...,");
871 for (unsigned int i = 0; i < len - 1; ++i)
872 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
873 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
874 val[0], precision);
877 namespace wi
879 template <>
880 template <typename storage>
881 struct int_traits < generic_wide_int <storage> >
882 : public wi::int_traits <storage>
884 static unsigned int get_precision (const generic_wide_int <storage> &);
885 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
886 const generic_wide_int <storage> &);
890 template <typename storage>
891 inline unsigned int
892 wi::int_traits < generic_wide_int <storage> >::
893 get_precision (const generic_wide_int <storage> &x)
895 return x.get_precision ();
898 template <typename storage>
899 inline wi::storage_ref
900 wi::int_traits < generic_wide_int <storage> >::
901 decompose (HOST_WIDE_INT *, unsigned int precision,
902 const generic_wide_int <storage> &x)
904 gcc_checking_assert (precision == x.get_precision ());
905 return wi::storage_ref (x.get_val (), x.get_len (), precision);
908 /* Provide the storage for a wide_int_ref. This acts like a read-only
909 wide_int, with the optimization that VAL is normally a pointer to
910 another integer's storage, so that no array copy is needed. */
911 template <bool SE>
912 struct wide_int_ref_storage : public wi::storage_ref
914 private:
915 /* Scratch space that can be used when decomposing the original integer.
916 It must live as long as this object. */
917 HOST_WIDE_INT scratch[2];
919 public:
920 wide_int_ref_storage (const wi::storage_ref &);
922 template <typename T>
923 wide_int_ref_storage (const T &);
925 template <typename T>
926 wide_int_ref_storage (const T &, unsigned int);
929 /* Create a reference from an existing reference. */
930 template <bool SE>
931 inline wide_int_ref_storage <SE>::
932 wide_int_ref_storage (const wi::storage_ref &x)
933 : storage_ref (x)
936 /* Create a reference to integer X in its natural precision. Note
937 that the natural precision is host-dependent for primitive
938 types. */
939 template <bool SE>
940 template <typename T>
941 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
942 : storage_ref (wi::int_traits <T>::decompose (scratch,
943 wi::get_precision (x), x))
947 /* Create a reference to integer X in precision PRECISION. */
948 template <bool SE>
949 template <typename T>
950 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
951 unsigned int precision)
952 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
956 namespace wi
958 template <>
959 template <bool SE>
960 struct int_traits <wide_int_ref_storage <SE> >
962 static const enum precision_type precision_type = VAR_PRECISION;
963 /* wi::storage_ref can be a reference to a primitive type,
964 so this is the conservatively-correct setting. */
965 static const bool host_dependent_precision = true;
966 static const bool is_sign_extended = SE;
970 namespace wi
972 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
973 unsigned int, unsigned int, unsigned int,
974 signop sgn);
975 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
976 unsigned int, unsigned int, bool = true);
979 /* The storage used by wide_int. */
980 class GTY(()) wide_int_storage
982 private:
983 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
984 unsigned int len;
985 unsigned int precision;
987 public:
988 wide_int_storage ();
989 template <typename T>
990 wide_int_storage (const T &);
992 /* The standard generic_wide_int storage methods. */
993 unsigned int get_precision () const;
994 const HOST_WIDE_INT *get_val () const;
995 unsigned int get_len () const;
996 HOST_WIDE_INT *write_val ();
997 void set_len (unsigned int, bool = false);
999 static wide_int from (const wide_int_ref &, unsigned int, signop);
1000 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1001 unsigned int, bool = true);
1002 static wide_int create (unsigned int);
1004 /* FIXME: target-dependent, so should disappear. */
1005 wide_int bswap () const;
1008 namespace wi
1010 template <>
1011 struct int_traits <wide_int_storage>
1013 static const enum precision_type precision_type = VAR_PRECISION;
1014 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1015 static const bool host_dependent_precision = false;
1016 static const bool is_sign_extended = true;
1017 template <typename T1, typename T2>
1018 static wide_int get_binary_result (const T1 &, const T2 &);
1022 inline wide_int_storage::wide_int_storage () {}
1024 /* Initialize the storage from integer X, in its natural precision.
1025 Note that we do not allow integers with host-dependent precision
1026 to become wide_ints; wide_ints must always be logically independent
1027 of the host. */
1028 template <typename T>
1029 inline wide_int_storage::wide_int_storage (const T &x)
1031 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1032 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1033 WIDE_INT_REF_FOR (T) xi (x);
1034 precision = xi.precision;
1035 wi::copy (*this, xi);
1038 inline unsigned int
1039 wide_int_storage::get_precision () const
1041 return precision;
1044 inline const HOST_WIDE_INT *
1045 wide_int_storage::get_val () const
1047 return val;
1050 inline unsigned int
1051 wide_int_storage::get_len () const
1053 return len;
1056 inline HOST_WIDE_INT *
1057 wide_int_storage::write_val ()
1059 return val;
1062 inline void
1063 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1065 len = l;
1066 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1067 val[len - 1] = sext_hwi (val[len - 1],
1068 precision % HOST_BITS_PER_WIDE_INT);
1071 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1072 number. */
1073 inline wide_int
1074 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1075 signop sgn)
1077 wide_int result = wide_int::create (precision);
1078 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1079 x.precision, precision, sgn));
1080 return result;
1083 /* Create a wide_int from the explicit block encoding given by VAL and
1084 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1085 true if the encoding may have redundant trailing blocks. */
1086 inline wide_int
1087 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1088 unsigned int precision, bool need_canon_p)
1090 wide_int result = wide_int::create (precision);
1091 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1092 need_canon_p));
1093 return result;
1096 /* Return an uninitialized wide_int with precision PRECISION. */
1097 inline wide_int
1098 wide_int_storage::create (unsigned int precision)
1100 wide_int x;
1101 x.precision = precision;
1102 return x;
1105 template <typename T1, typename T2>
1106 inline wide_int
1107 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1109 /* This shouldn't be used for two flexible-precision inputs. */
1110 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1111 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1112 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1113 return wide_int::create (wi::get_precision (y));
1114 else
1115 return wide_int::create (wi::get_precision (x));
1118 /* The storage used by FIXED_WIDE_INT (N). */
1119 template <int N>
1120 class GTY(()) fixed_wide_int_storage
1122 private:
1123 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1124 unsigned int len;
1126 public:
1127 fixed_wide_int_storage ();
1128 template <typename T>
1129 fixed_wide_int_storage (const T &);
1131 /* The standard generic_wide_int storage methods. */
1132 unsigned int get_precision () const;
1133 const HOST_WIDE_INT *get_val () const;
1134 unsigned int get_len () const;
1135 HOST_WIDE_INT *write_val ();
1136 void set_len (unsigned int, bool = false);
1138 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1139 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1140 bool = true);
1143 namespace wi
1145 template <>
1146 template <int N>
1147 struct int_traits < fixed_wide_int_storage <N> >
1149 static const enum precision_type precision_type = CONST_PRECISION;
1150 static const bool host_dependent_precision = false;
1151 static const bool is_sign_extended = true;
1152 static const unsigned int precision = N;
1153 template <typename T1, typename T2>
1154 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1158 template <int N>
1159 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1161 /* Initialize the storage from integer X, in precision N. */
1162 template <int N>
1163 template <typename T>
1164 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1166 /* Check for type compatibility. We don't want to initialize a
1167 fixed-width integer from something like a wide_int. */
1168 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1169 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1172 template <int N>
1173 inline unsigned int
1174 fixed_wide_int_storage <N>::get_precision () const
1176 return N;
1179 template <int N>
1180 inline const HOST_WIDE_INT *
1181 fixed_wide_int_storage <N>::get_val () const
1183 return val;
1186 template <int N>
1187 inline unsigned int
1188 fixed_wide_int_storage <N>::get_len () const
1190 return len;
1193 template <int N>
1194 inline HOST_WIDE_INT *
1195 fixed_wide_int_storage <N>::write_val ()
1197 return val;
1200 template <int N>
1201 inline void
1202 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1204 len = l;
1205 /* There are no excess bits in val[len - 1]. */
1206 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1209 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1210 template <int N>
1211 inline FIXED_WIDE_INT (N)
1212 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1214 FIXED_WIDE_INT (N) result;
1215 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1216 x.precision, N, sgn));
1217 return result;
1220 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1221 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1222 trailing blocks. */
1223 template <int N>
1224 inline FIXED_WIDE_INT (N)
1225 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1226 unsigned int len,
1227 bool need_canon_p)
1229 FIXED_WIDE_INT (N) result;
1230 result.set_len (wi::from_array (result.write_val (), val, len,
1231 N, need_canon_p));
1232 return result;
1235 template <int N>
1236 template <typename T1, typename T2>
1237 inline FIXED_WIDE_INT (N)
1238 wi::int_traits < fixed_wide_int_storage <N> >::
1239 get_binary_result (const T1 &, const T2 &)
1241 return FIXED_WIDE_INT (N) ();
1244 /* A reference to one element of a trailing_wide_ints structure. */
1245 class trailing_wide_int_storage
1247 private:
1248 /* The precision of the integer, which is a fixed property of the
1249 parent trailing_wide_ints. */
1250 unsigned int m_precision;
1252 /* A pointer to the length field. */
1253 unsigned char *m_len;
1255 /* A pointer to the HWI array. There are enough elements to hold all
1256 values of precision M_PRECISION. */
1257 HOST_WIDE_INT *m_val;
1259 public:
1260 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1262 /* The standard generic_wide_int storage methods. */
1263 unsigned int get_len () const;
1264 unsigned int get_precision () const;
1265 const HOST_WIDE_INT *get_val () const;
1266 HOST_WIDE_INT *write_val ();
1267 void set_len (unsigned int, bool = false);
1269 template <typename T>
1270 trailing_wide_int_storage &operator = (const T &);
1273 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1275 /* trailing_wide_int behaves like a wide_int. */
1276 namespace wi
1278 template <>
1279 struct int_traits <trailing_wide_int_storage>
1280 : public int_traits <wide_int_storage> {};
1283 /* An array of N wide_int-like objects that can be put at the end of
1284 a variable-sized structure. Use extra_size to calculate how many
1285 bytes beyond the sizeof need to be allocated. Use set_precision
1286 to initialize the structure. */
1287 template <int N>
1288 class GTY(()) trailing_wide_ints
1290 private:
1291 /* The shared precision of each number. */
1292 unsigned short m_precision;
1294 /* The shared maximum length of each number. */
1295 unsigned char m_max_len;
1297 /* The current length of each number. */
1298 unsigned char m_len[N];
1300 /* The variable-length part of the structure, which always contains
1301 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1302 HOST_WIDE_INT m_val[1];
1304 public:
1305 void set_precision (unsigned int);
1306 trailing_wide_int operator [] (unsigned int);
1307 static size_t extra_size (unsigned int);
1310 inline trailing_wide_int_storage::
1311 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1312 HOST_WIDE_INT *val)
1313 : m_precision (precision), m_len (len), m_val (val)
1317 inline unsigned int
1318 trailing_wide_int_storage::get_len () const
1320 return *m_len;
1323 inline unsigned int
1324 trailing_wide_int_storage::get_precision () const
1326 return m_precision;
1329 inline const HOST_WIDE_INT *
1330 trailing_wide_int_storage::get_val () const
1332 return m_val;
1335 inline HOST_WIDE_INT *
1336 trailing_wide_int_storage::write_val ()
1338 return m_val;
1341 inline void
1342 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1344 *m_len = len;
1345 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1346 m_val[len - 1] = sext_hwi (m_val[len - 1],
1347 m_precision % HOST_BITS_PER_WIDE_INT);
1350 template <typename T>
1351 inline trailing_wide_int_storage &
1352 trailing_wide_int_storage::operator = (const T &x)
1354 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1355 wi::copy (*this, xi);
1356 return *this;
1359 /* Initialize the structure and record that all elements have precision
1360 PRECISION. */
1361 template <int N>
1362 inline void
1363 trailing_wide_ints <N>::set_precision (unsigned int precision)
1365 m_precision = precision;
1366 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1367 / HOST_BITS_PER_WIDE_INT);
1370 /* Return a reference to element INDEX. */
1371 template <int N>
1372 inline trailing_wide_int
1373 trailing_wide_ints <N>::operator [] (unsigned int index)
1375 return trailing_wide_int_storage (m_precision, &m_len[index],
1376 &m_val[index * m_max_len]);
1379 /* Return how many extra bytes need to be added to the end of the structure
1380 in order to handle N wide_ints of precision PRECISION. */
1381 template <int N>
1382 inline size_t
1383 trailing_wide_ints <N>::extra_size (unsigned int precision)
1385 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1386 / HOST_BITS_PER_WIDE_INT);
1387 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1390 /* This macro is used in structures that end with a trailing_wide_ints field
1391 called FIELD. It declares get_NAME() and set_NAME() methods to access
1392 element I of FIELD. */
1393 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1394 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1395 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1397 namespace wi
1399 /* Implementation of int_traits for primitive integer types like "int". */
1400 template <typename T, bool signed_p>
1401 struct primitive_int_traits
1403 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1404 static const bool host_dependent_precision = true;
1405 static const bool is_sign_extended = true;
1406 static unsigned int get_precision (T);
1407 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1411 template <typename T, bool signed_p>
1412 inline unsigned int
1413 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1415 return sizeof (T) * CHAR_BIT;
1418 template <typename T, bool signed_p>
1419 inline wi::storage_ref
1420 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1421 unsigned int precision, T x)
1423 scratch[0] = x;
1424 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1425 return wi::storage_ref (scratch, 1, precision);
1426 scratch[1] = 0;
1427 return wi::storage_ref (scratch, 2, precision);
1430 /* Allow primitive C types to be used in wi:: routines. */
1431 namespace wi
1433 template <>
1434 struct int_traits <int>
1435 : public primitive_int_traits <int, true> {};
1437 template <>
1438 struct int_traits <unsigned int>
1439 : public primitive_int_traits <unsigned int, false> {};
1441 template <>
1442 struct int_traits <long>
1443 : public primitive_int_traits <long, true> {};
1445 template <>
1446 struct int_traits <unsigned long>
1447 : public primitive_int_traits <unsigned long, false> {};
1449 #if defined HAVE_LONG_LONG
1450 template <>
1451 struct int_traits <long long>
1452 : public primitive_int_traits <long long, true> {};
1454 template <>
1455 struct int_traits <unsigned long long>
1456 : public primitive_int_traits <unsigned long long, false> {};
1457 #endif
1460 namespace wi
1462 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1463 and precision PRECISION. */
1464 struct hwi_with_prec
1466 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1467 HOST_WIDE_INT val;
1468 unsigned int precision;
1469 signop sgn;
1472 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1473 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1475 hwi_with_prec minus_one (unsigned int);
1476 hwi_with_prec zero (unsigned int);
1477 hwi_with_prec one (unsigned int);
1478 hwi_with_prec two (unsigned int);
1481 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1482 signop s)
1483 : val (v), precision (p), sgn (s)
1487 /* Return a signed integer that has value VAL and precision PRECISION. */
1488 inline wi::hwi_with_prec
1489 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1491 return hwi_with_prec (val, precision, SIGNED);
1494 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1495 inline wi::hwi_with_prec
1496 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1498 return hwi_with_prec (val, precision, UNSIGNED);
1501 /* Return a wide int of -1 with precision PRECISION. */
1502 inline wi::hwi_with_prec
1503 wi::minus_one (unsigned int precision)
1505 return wi::shwi (-1, precision);
1508 /* Return a wide int of 0 with precision PRECISION. */
1509 inline wi::hwi_with_prec
1510 wi::zero (unsigned int precision)
1512 return wi::shwi (0, precision);
1515 /* Return a wide int of 1 with precision PRECISION. */
1516 inline wi::hwi_with_prec
1517 wi::one (unsigned int precision)
1519 return wi::shwi (1, precision);
1522 /* Return a wide int of 2 with precision PRECISION. */
1523 inline wi::hwi_with_prec
1524 wi::two (unsigned int precision)
1526 return wi::shwi (2, precision);
1529 namespace wi
1531 template <>
1532 struct int_traits <wi::hwi_with_prec>
1534 static const enum precision_type precision_type = VAR_PRECISION;
1535 /* hwi_with_prec has an explicitly-given precision, rather than the
1536 precision of HOST_WIDE_INT. */
1537 static const bool host_dependent_precision = false;
1538 static const bool is_sign_extended = true;
1539 static unsigned int get_precision (const wi::hwi_with_prec &);
1540 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1541 const wi::hwi_with_prec &);
1545 inline unsigned int
1546 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1548 return x.precision;
1551 inline wi::storage_ref
1552 wi::int_traits <wi::hwi_with_prec>::
1553 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1554 const wi::hwi_with_prec &x)
1556 gcc_checking_assert (precision == x.precision);
1557 scratch[0] = x.val;
1558 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1559 return wi::storage_ref (scratch, 1, precision);
1560 scratch[1] = 0;
1561 return wi::storage_ref (scratch, 2, precision);
1564 /* Private functions for handling large cases out of line. They take
1565 individual length and array parameters because that is cheaper for
1566 the inline caller than constructing an object on the stack and
1567 passing a reference to it. (Although many callers use wide_int_refs,
1568 we generally want those to be removed by SRA.) */
1569 namespace wi
1571 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1572 const HOST_WIDE_INT *, unsigned int, unsigned int);
1573 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1574 const HOST_WIDE_INT *, unsigned int);
1575 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1576 const HOST_WIDE_INT *, unsigned int);
1577 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1578 const HOST_WIDE_INT *, unsigned int);
1579 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1580 const HOST_WIDE_INT *, unsigned int);
1581 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1582 unsigned int,
1583 unsigned int, unsigned int);
1584 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1585 unsigned int,
1586 unsigned int, unsigned int);
1587 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1588 unsigned int, unsigned int, unsigned int);
1589 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1590 unsigned int, unsigned int, unsigned int);
1591 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1592 unsigned int, unsigned int, unsigned int,
1593 unsigned int);
1594 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1595 unsigned int, unsigned int, unsigned int,
1596 unsigned int);
1597 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1598 const HOST_WIDE_INT *, unsigned int, unsigned int);
1599 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1600 unsigned int, const HOST_WIDE_INT *,
1601 unsigned int, unsigned int);
1602 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1603 const HOST_WIDE_INT *, unsigned int, unsigned int);
1604 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1605 unsigned int, const HOST_WIDE_INT *,
1606 unsigned int, unsigned int);
1607 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1608 const HOST_WIDE_INT *, unsigned int, unsigned int);
1609 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1610 const HOST_WIDE_INT *, unsigned int, unsigned int,
1611 signop, bool *);
1612 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1613 const HOST_WIDE_INT *, unsigned int, unsigned int,
1614 signop, bool *);
1615 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1616 unsigned int, const HOST_WIDE_INT *,
1617 unsigned int, unsigned int, signop, bool *,
1618 bool);
1619 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1620 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1621 unsigned int, unsigned int,
1622 const HOST_WIDE_INT *,
1623 unsigned int, unsigned int,
1624 signop, bool *);
1627 /* Return the number of bits that integer X can hold. */
1628 template <typename T>
1629 inline unsigned int
1630 wi::get_precision (const T &x)
1632 return wi::int_traits <T>::get_precision (x);
1635 /* Return the number of bits that the result of a binary operation can
1636 hold when the input operands are X and Y. */
1637 template <typename T1, typename T2>
1638 inline unsigned int
1639 wi::get_binary_precision (const T1 &x, const T2 &y)
1641 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1642 get_binary_result (x, y));
1645 /* Copy the contents of Y to X, but keeping X's current precision. */
1646 template <typename T1, typename T2>
1647 inline void
1648 wi::copy (T1 &x, const T2 &y)
1650 HOST_WIDE_INT *xval = x.write_val ();
1651 const HOST_WIDE_INT *yval = y.get_val ();
1652 unsigned int len = y.get_len ();
1653 unsigned int i = 0;
1655 xval[i] = yval[i];
1656 while (++i < len);
1657 x.set_len (len, y.is_sign_extended);
1660 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1661 template <typename T>
1662 inline bool
1663 wi::fits_shwi_p (const T &x)
1665 WIDE_INT_REF_FOR (T) xi (x);
1666 return xi.len == 1;
1669 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1670 precision. */
1671 template <typename T>
1672 inline bool
1673 wi::fits_uhwi_p (const T &x)
1675 WIDE_INT_REF_FOR (T) xi (x);
1676 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1677 return true;
1678 if (xi.len == 1)
1679 return xi.slow () >= 0;
1680 return xi.len == 2 && xi.uhigh () == 0;
1683 /* Return true if X is negative based on the interpretation of SGN.
1684 For UNSIGNED, this is always false. */
1685 template <typename T>
1686 inline bool
1687 wi::neg_p (const T &x, signop sgn)
1689 WIDE_INT_REF_FOR (T) xi (x);
1690 if (sgn == UNSIGNED)
1691 return false;
1692 return xi.sign_mask () < 0;
1695 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1696 template <typename T>
1697 inline HOST_WIDE_INT
1698 wi::sign_mask (const T &x)
1700 WIDE_INT_REF_FOR (T) xi (x);
1701 return xi.sign_mask ();
1704 /* Return true if X == Y. X and Y must be binary-compatible. */
1705 template <typename T1, typename T2>
1706 inline bool
1707 wi::eq_p (const T1 &x, const T2 &y)
1709 unsigned int precision = get_binary_precision (x, y);
1710 WIDE_INT_REF_FOR (T1) xi (x, precision);
1711 WIDE_INT_REF_FOR (T2) yi (y, precision);
1712 if (xi.is_sign_extended && yi.is_sign_extended)
1714 /* This case reduces to array equality. */
1715 if (xi.len != yi.len)
1716 return false;
1717 unsigned int i = 0;
1719 if (xi.val[i] != yi.val[i])
1720 return false;
1721 while (++i != xi.len);
1722 return true;
1724 if (__builtin_expect (yi.len == 1, true))
1726 /* XI is only equal to YI if it too has a single HWI. */
1727 if (xi.len != 1)
1728 return false;
1729 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1730 with 0 are simple. */
1731 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1732 return xi.val[0] == 0;
1733 /* Otherwise flush out any excess bits first. */
1734 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1735 int excess = HOST_BITS_PER_WIDE_INT - precision;
1736 if (excess > 0)
1737 diff <<= excess;
1738 return diff == 0;
1740 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1743 /* Return true if X != Y. X and Y must be binary-compatible. */
1744 template <typename T1, typename T2>
1745 inline bool
1746 wi::ne_p (const T1 &x, const T2 &y)
1748 return !eq_p (x, y);
1751 /* Return true if X < Y when both are treated as signed values. */
1752 template <typename T1, typename T2>
1753 inline bool
1754 wi::lts_p (const T1 &x, const T2 &y)
1756 unsigned int precision = get_binary_precision (x, y);
1757 WIDE_INT_REF_FOR (T1) xi (x, precision);
1758 WIDE_INT_REF_FOR (T2) yi (y, precision);
1759 /* We optimize x < y, where y is 64 or fewer bits. */
1760 if (wi::fits_shwi_p (yi))
1762 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1763 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1764 return neg_p (xi);
1765 /* If x fits directly into a shwi, we can compare directly. */
1766 if (wi::fits_shwi_p (xi))
1767 return xi.to_shwi () < yi.to_shwi ();
1768 /* If x doesn't fit and is negative, then it must be more
1769 negative than any value in y, and hence smaller than y. */
1770 if (neg_p (xi))
1771 return true;
1772 /* If x is positive, then it must be larger than any value in y,
1773 and hence greater than y. */
1774 return false;
1776 /* Optimize the opposite case, if it can be detected at compile time. */
1777 if (STATIC_CONSTANT_P (xi.len == 1))
1778 /* If YI is negative it is lower than the least HWI.
1779 If YI is positive it is greater than the greatest HWI. */
1780 return !neg_p (yi);
1781 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1784 /* Return true if X < Y when both are treated as unsigned values. */
1785 template <typename T1, typename T2>
1786 inline bool
1787 wi::ltu_p (const T1 &x, const T2 &y)
1789 unsigned int precision = get_binary_precision (x, y);
1790 WIDE_INT_REF_FOR (T1) xi (x, precision);
1791 WIDE_INT_REF_FOR (T2) yi (y, precision);
1792 /* Optimize comparisons with constants. */
1793 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1794 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1795 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1796 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1797 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1798 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1799 values does not change the result. */
1800 if (__builtin_expect (xi.len + yi.len == 2, true))
1802 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1803 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1804 return xl < yl;
1806 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1809 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1810 template <typename T1, typename T2>
1811 inline bool
1812 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1814 if (sgn == SIGNED)
1815 return lts_p (x, y);
1816 else
1817 return ltu_p (x, y);
1820 /* Return true if X <= Y when both are treated as signed values. */
1821 template <typename T1, typename T2>
1822 inline bool
1823 wi::les_p (const T1 &x, const T2 &y)
1825 return !lts_p (y, x);
1828 /* Return true if X <= Y when both are treated as unsigned values. */
1829 template <typename T1, typename T2>
1830 inline bool
1831 wi::leu_p (const T1 &x, const T2 &y)
1833 return !ltu_p (y, x);
1836 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1837 template <typename T1, typename T2>
1838 inline bool
1839 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1841 if (sgn == SIGNED)
1842 return les_p (x, y);
1843 else
1844 return leu_p (x, y);
1847 /* Return true if X > Y when both are treated as signed values. */
1848 template <typename T1, typename T2>
1849 inline bool
1850 wi::gts_p (const T1 &x, const T2 &y)
1852 return lts_p (y, x);
1855 /* Return true if X > Y when both are treated as unsigned values. */
1856 template <typename T1, typename T2>
1857 inline bool
1858 wi::gtu_p (const T1 &x, const T2 &y)
1860 return ltu_p (y, x);
1863 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1864 template <typename T1, typename T2>
1865 inline bool
1866 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1868 if (sgn == SIGNED)
1869 return gts_p (x, y);
1870 else
1871 return gtu_p (x, y);
1874 /* Return true if X >= Y when both are treated as signed values. */
1875 template <typename T1, typename T2>
1876 inline bool
1877 wi::ges_p (const T1 &x, const T2 &y)
1879 return !lts_p (x, y);
1882 /* Return true if X >= Y when both are treated as unsigned values. */
1883 template <typename T1, typename T2>
1884 inline bool
1885 wi::geu_p (const T1 &x, const T2 &y)
1887 return !ltu_p (x, y);
1890 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1891 template <typename T1, typename T2>
1892 inline bool
1893 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1895 if (sgn == SIGNED)
1896 return ges_p (x, y);
1897 else
1898 return geu_p (x, y);
1901 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1902 as signed values. */
1903 template <typename T1, typename T2>
1904 inline int
1905 wi::cmps (const T1 &x, const T2 &y)
1907 unsigned int precision = get_binary_precision (x, y);
1908 WIDE_INT_REF_FOR (T1) xi (x, precision);
1909 WIDE_INT_REF_FOR (T2) yi (y, precision);
1910 if (wi::fits_shwi_p (yi))
1912 /* Special case for comparisons with 0. */
1913 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1914 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1915 /* If x fits into a signed HWI, we can compare directly. */
1916 if (wi::fits_shwi_p (xi))
1918 HOST_WIDE_INT xl = xi.to_shwi ();
1919 HOST_WIDE_INT yl = yi.to_shwi ();
1920 return xl < yl ? -1 : xl > yl;
1922 /* If x doesn't fit and is negative, then it must be more
1923 negative than any signed HWI, and hence smaller than y. */
1924 if (neg_p (xi))
1925 return -1;
1926 /* If x is positive, then it must be larger than any signed HWI,
1927 and hence greater than y. */
1928 return 1;
1930 /* Optimize the opposite case, if it can be detected at compile time. */
1931 if (STATIC_CONSTANT_P (xi.len == 1))
1932 /* If YI is negative it is lower than the least HWI.
1933 If YI is positive it is greater than the greatest HWI. */
1934 return neg_p (yi) ? 1 : -1;
1935 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1938 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1939 as unsigned values. */
1940 template <typename T1, typename T2>
1941 inline int
1942 wi::cmpu (const T1 &x, const T2 &y)
1944 unsigned int precision = get_binary_precision (x, y);
1945 WIDE_INT_REF_FOR (T1) xi (x, precision);
1946 WIDE_INT_REF_FOR (T2) yi (y, precision);
1947 /* Optimize comparisons with constants. */
1948 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1950 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1951 if (xi.len != 1)
1952 return 1;
1953 /* Otherwise compare directly. */
1954 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1955 unsigned HOST_WIDE_INT yl = yi.val[0];
1956 return xl < yl ? -1 : xl > yl;
1958 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1960 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1961 if (yi.len != 1)
1962 return -1;
1963 /* Otherwise compare directly. */
1964 unsigned HOST_WIDE_INT xl = xi.val[0];
1965 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1966 return xl < yl ? -1 : xl > yl;
1968 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1969 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1970 values does not change the result. */
1971 if (__builtin_expect (xi.len + yi.len == 2, true))
1973 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1974 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1975 return xl < yl ? -1 : xl > yl;
1977 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
1980 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1981 X and Y indicated by SGN. */
1982 template <typename T1, typename T2>
1983 inline int
1984 wi::cmp (const T1 &x, const T2 &y, signop sgn)
1986 if (sgn == SIGNED)
1987 return cmps (x, y);
1988 else
1989 return cmpu (x, y);
1992 /* Return ~x. */
1993 template <typename T>
1994 inline WI_UNARY_RESULT (T)
1995 wi::bit_not (const T &x)
1997 WI_UNARY_RESULT_VAR (result, val, T, x);
1998 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
1999 for (unsigned int i = 0; i < xi.len; ++i)
2000 val[i] = ~xi.val[i];
2001 result.set_len (xi.len);
2002 return result;
2005 /* Return -x. */
2006 template <typename T>
2007 inline WI_UNARY_RESULT (T)
2008 wi::neg (const T &x)
2010 return sub (0, x);
2013 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2014 template <typename T>
2015 inline WI_UNARY_RESULT (T)
2016 wi::neg (const T &x, bool *overflow)
2018 *overflow = only_sign_bit_p (x);
2019 return sub (0, x);
2022 /* Return the absolute value of x. */
2023 template <typename T>
2024 inline WI_UNARY_RESULT (T)
2025 wi::abs (const T &x)
2027 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2030 /* Return the result of sign-extending the low OFFSET bits of X. */
2031 template <typename T>
2032 inline WI_UNARY_RESULT (T)
2033 wi::sext (const T &x, unsigned int offset)
2035 WI_UNARY_RESULT_VAR (result, val, T, x);
2036 unsigned int precision = get_precision (result);
2037 WIDE_INT_REF_FOR (T) xi (x, precision);
2039 if (offset <= HOST_BITS_PER_WIDE_INT)
2041 val[0] = sext_hwi (xi.ulow (), offset);
2042 result.set_len (1, true);
2044 else
2045 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2046 return result;
2049 /* Return the result of zero-extending the low OFFSET bits of X. */
2050 template <typename T>
2051 inline WI_UNARY_RESULT (T)
2052 wi::zext (const T &x, unsigned int offset)
2054 WI_UNARY_RESULT_VAR (result, val, T, x);
2055 unsigned int precision = get_precision (result);
2056 WIDE_INT_REF_FOR (T) xi (x, precision);
2058 /* This is not just an optimization, it is actually required to
2059 maintain canonization. */
2060 if (offset >= precision)
2062 wi::copy (result, xi);
2063 return result;
2066 /* In these cases we know that at least the top bit will be clear,
2067 so no sign extension is necessary. */
2068 if (offset < HOST_BITS_PER_WIDE_INT)
2070 val[0] = zext_hwi (xi.ulow (), offset);
2071 result.set_len (1, true);
2073 else
2074 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2075 return result;
2078 /* Return the result of extending the low OFFSET bits of X according to
2079 signedness SGN. */
2080 template <typename T>
2081 inline WI_UNARY_RESULT (T)
2082 wi::ext (const T &x, unsigned int offset, signop sgn)
2084 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2087 /* Return an integer that represents X | (1 << bit). */
2088 template <typename T>
2089 inline WI_UNARY_RESULT (T)
2090 wi::set_bit (const T &x, unsigned int bit)
2092 WI_UNARY_RESULT_VAR (result, val, T, x);
2093 unsigned int precision = get_precision (result);
2094 WIDE_INT_REF_FOR (T) xi (x, precision);
2095 if (precision <= HOST_BITS_PER_WIDE_INT)
2097 val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
2098 result.set_len (1);
2100 else
2101 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2102 return result;
2105 /* Return the mininum of X and Y, treating them both as having
2106 signedness SGN. */
2107 template <typename T1, typename T2>
2108 inline WI_BINARY_RESULT (T1, T2)
2109 wi::min (const T1 &x, const T2 &y, signop sgn)
2111 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2112 unsigned int precision = get_precision (result);
2113 if (wi::le_p (x, y, sgn))
2114 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2115 else
2116 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2117 return result;
2120 /* Return the minimum of X and Y, treating both as signed values. */
2121 template <typename T1, typename T2>
2122 inline WI_BINARY_RESULT (T1, T2)
2123 wi::smin (const T1 &x, const T2 &y)
2125 return wi::min (x, y, SIGNED);
2128 /* Return the minimum of X and Y, treating both as unsigned values. */
2129 template <typename T1, typename T2>
2130 inline WI_BINARY_RESULT (T1, T2)
2131 wi::umin (const T1 &x, const T2 &y)
2133 return wi::min (x, y, UNSIGNED);
2136 /* Return the maxinum of X and Y, treating them both as having
2137 signedness SGN. */
2138 template <typename T1, typename T2>
2139 inline WI_BINARY_RESULT (T1, T2)
2140 wi::max (const T1 &x, const T2 &y, signop sgn)
2142 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2143 unsigned int precision = get_precision (result);
2144 if (wi::ge_p (x, y, sgn))
2145 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2146 else
2147 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2148 return result;
2151 /* Return the maximum of X and Y, treating both as signed values. */
2152 template <typename T1, typename T2>
2153 inline WI_BINARY_RESULT (T1, T2)
2154 wi::smax (const T1 &x, const T2 &y)
2156 return wi::max (x, y, SIGNED);
2159 /* Return the maximum of X and Y, treating both as unsigned values. */
2160 template <typename T1, typename T2>
2161 inline WI_BINARY_RESULT (T1, T2)
2162 wi::umax (const T1 &x, const T2 &y)
2164 return wi::max (x, y, UNSIGNED);
2167 /* Return X & Y. */
2168 template <typename T1, typename T2>
2169 inline WI_BINARY_RESULT (T1, T2)
2170 wi::bit_and (const T1 &x, const T2 &y)
2172 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2173 unsigned int precision = get_precision (result);
2174 WIDE_INT_REF_FOR (T1) xi (x, precision);
2175 WIDE_INT_REF_FOR (T2) yi (y, precision);
2176 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2177 if (__builtin_expect (xi.len + yi.len == 2, true))
2179 val[0] = xi.ulow () & yi.ulow ();
2180 result.set_len (1, is_sign_extended);
2182 else
2183 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2184 precision), is_sign_extended);
2185 return result;
2188 /* Return X & ~Y. */
2189 template <typename T1, typename T2>
2190 inline WI_BINARY_RESULT (T1, T2)
2191 wi::bit_and_not (const T1 &x, const T2 &y)
2193 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2194 unsigned int precision = get_precision (result);
2195 WIDE_INT_REF_FOR (T1) xi (x, precision);
2196 WIDE_INT_REF_FOR (T2) yi (y, precision);
2197 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2198 if (__builtin_expect (xi.len + yi.len == 2, true))
2200 val[0] = xi.ulow () & ~yi.ulow ();
2201 result.set_len (1, is_sign_extended);
2203 else
2204 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2205 precision), is_sign_extended);
2206 return result;
2209 /* Return X | Y. */
2210 template <typename T1, typename T2>
2211 inline WI_BINARY_RESULT (T1, T2)
2212 wi::bit_or (const T1 &x, const T2 &y)
2214 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2215 unsigned int precision = get_precision (result);
2216 WIDE_INT_REF_FOR (T1) xi (x, precision);
2217 WIDE_INT_REF_FOR (T2) yi (y, precision);
2218 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2219 if (__builtin_expect (xi.len + yi.len == 2, true))
2221 val[0] = xi.ulow () | yi.ulow ();
2222 result.set_len (1, is_sign_extended);
2224 else
2225 result.set_len (or_large (val, xi.val, xi.len,
2226 yi.val, yi.len, precision), is_sign_extended);
2227 return result;
2230 /* Return X | ~Y. */
2231 template <typename T1, typename T2>
2232 inline WI_BINARY_RESULT (T1, T2)
2233 wi::bit_or_not (const T1 &x, const T2 &y)
2235 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2236 unsigned int precision = get_precision (result);
2237 WIDE_INT_REF_FOR (T1) xi (x, precision);
2238 WIDE_INT_REF_FOR (T2) yi (y, precision);
2239 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2240 if (__builtin_expect (xi.len + yi.len == 2, true))
2242 val[0] = xi.ulow () | ~yi.ulow ();
2243 result.set_len (1, is_sign_extended);
2245 else
2246 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2247 precision), is_sign_extended);
2248 return result;
2251 /* Return X ^ Y. */
2252 template <typename T1, typename T2>
2253 inline WI_BINARY_RESULT (T1, T2)
2254 wi::bit_xor (const T1 &x, const T2 &y)
2256 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2257 unsigned int precision = get_precision (result);
2258 WIDE_INT_REF_FOR (T1) xi (x, precision);
2259 WIDE_INT_REF_FOR (T2) yi (y, precision);
2260 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2261 if (__builtin_expect (xi.len + yi.len == 2, true))
2263 val[0] = xi.ulow () ^ yi.ulow ();
2264 result.set_len (1, is_sign_extended);
2266 else
2267 result.set_len (xor_large (val, xi.val, xi.len,
2268 yi.val, yi.len, precision), is_sign_extended);
2269 return result;
2272 /* Return X + Y. */
2273 template <typename T1, typename T2>
2274 inline WI_BINARY_RESULT (T1, T2)
2275 wi::add (const T1 &x, const T2 &y)
2277 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2278 unsigned int precision = get_precision (result);
2279 WIDE_INT_REF_FOR (T1) xi (x, precision);
2280 WIDE_INT_REF_FOR (T2) yi (y, precision);
2281 if (precision <= HOST_BITS_PER_WIDE_INT)
2283 val[0] = xi.ulow () + yi.ulow ();
2284 result.set_len (1);
2286 /* If the precision is known at compile time to be greater than
2287 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2288 knowing that (a) all bits in those HWIs are significant and
2289 (b) the result has room for at least two HWIs. This provides
2290 a fast path for things like offset_int and widest_int.
2292 The STATIC_CONSTANT_P test prevents this path from being
2293 used for wide_ints. wide_ints with precisions greater than
2294 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2295 point handling them inline. */
2296 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2297 && __builtin_expect (xi.len + yi.len == 2, true))
2299 unsigned HOST_WIDE_INT xl = xi.ulow ();
2300 unsigned HOST_WIDE_INT yl = yi.ulow ();
2301 unsigned HOST_WIDE_INT resultl = xl + yl;
2302 val[0] = resultl;
2303 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2304 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2305 >> (HOST_BITS_PER_WIDE_INT - 1)));
2307 else
2308 result.set_len (add_large (val, xi.val, xi.len,
2309 yi.val, yi.len, precision,
2310 UNSIGNED, 0));
2311 return result;
2314 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2315 and indicate in *OVERFLOW whether the operation overflowed. */
2316 template <typename T1, typename T2>
2317 inline WI_BINARY_RESULT (T1, T2)
2318 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2320 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2321 unsigned int precision = get_precision (result);
2322 WIDE_INT_REF_FOR (T1) xi (x, precision);
2323 WIDE_INT_REF_FOR (T2) yi (y, precision);
2324 if (precision <= HOST_BITS_PER_WIDE_INT)
2326 unsigned HOST_WIDE_INT xl = xi.ulow ();
2327 unsigned HOST_WIDE_INT yl = yi.ulow ();
2328 unsigned HOST_WIDE_INT resultl = xl + yl;
2329 if (sgn == SIGNED)
2330 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2331 >> (precision - 1)) & 1;
2332 else
2333 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2334 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2335 val[0] = resultl;
2336 result.set_len (1);
2338 else
2339 result.set_len (add_large (val, xi.val, xi.len,
2340 yi.val, yi.len, precision,
2341 sgn, overflow));
2342 return result;
2345 /* Return X - Y. */
2346 template <typename T1, typename T2>
2347 inline WI_BINARY_RESULT (T1, T2)
2348 wi::sub (const T1 &x, const T2 &y)
2350 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2351 unsigned int precision = get_precision (result);
2352 WIDE_INT_REF_FOR (T1) xi (x, precision);
2353 WIDE_INT_REF_FOR (T2) yi (y, precision);
2354 if (precision <= HOST_BITS_PER_WIDE_INT)
2356 val[0] = xi.ulow () - yi.ulow ();
2357 result.set_len (1);
2359 /* If the precision is known at compile time to be greater than
2360 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2361 knowing that (a) all bits in those HWIs are significant and
2362 (b) the result has room for at least two HWIs. This provides
2363 a fast path for things like offset_int and widest_int.
2365 The STATIC_CONSTANT_P test prevents this path from being
2366 used for wide_ints. wide_ints with precisions greater than
2367 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2368 point handling them inline. */
2369 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2370 && __builtin_expect (xi.len + yi.len == 2, true))
2372 unsigned HOST_WIDE_INT xl = xi.ulow ();
2373 unsigned HOST_WIDE_INT yl = yi.ulow ();
2374 unsigned HOST_WIDE_INT resultl = xl - yl;
2375 val[0] = resultl;
2376 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2377 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2378 >> (HOST_BITS_PER_WIDE_INT - 1)));
2380 else
2381 result.set_len (sub_large (val, xi.val, xi.len,
2382 yi.val, yi.len, precision,
2383 UNSIGNED, 0));
2384 return result;
2387 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2388 and indicate in *OVERFLOW whether the operation overflowed. */
2389 template <typename T1, typename T2>
2390 inline WI_BINARY_RESULT (T1, T2)
2391 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2393 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2394 unsigned int precision = get_precision (result);
2395 WIDE_INT_REF_FOR (T1) xi (x, precision);
2396 WIDE_INT_REF_FOR (T2) yi (y, precision);
2397 if (precision <= HOST_BITS_PER_WIDE_INT)
2399 unsigned HOST_WIDE_INT xl = xi.ulow ();
2400 unsigned HOST_WIDE_INT yl = yi.ulow ();
2401 unsigned HOST_WIDE_INT resultl = xl - yl;
2402 if (sgn == SIGNED)
2403 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2404 else
2405 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2406 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2407 val[0] = resultl;
2408 result.set_len (1);
2410 else
2411 result.set_len (sub_large (val, xi.val, xi.len,
2412 yi.val, yi.len, precision,
2413 sgn, overflow));
2414 return result;
2417 /* Return X * Y. */
2418 template <typename T1, typename T2>
2419 inline WI_BINARY_RESULT (T1, T2)
2420 wi::mul (const T1 &x, const T2 &y)
2422 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2423 unsigned int precision = get_precision (result);
2424 WIDE_INT_REF_FOR (T1) xi (x, precision);
2425 WIDE_INT_REF_FOR (T2) yi (y, precision);
2426 if (precision <= HOST_BITS_PER_WIDE_INT)
2428 val[0] = xi.ulow () * yi.ulow ();
2429 result.set_len (1);
2431 else
2432 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2433 precision, UNSIGNED, 0, false));
2434 return result;
2437 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2438 and indicate in *OVERFLOW whether the operation overflowed. */
2439 template <typename T1, typename T2>
2440 inline WI_BINARY_RESULT (T1, T2)
2441 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2443 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2444 unsigned int precision = get_precision (result);
2445 WIDE_INT_REF_FOR (T1) xi (x, precision);
2446 WIDE_INT_REF_FOR (T2) yi (y, precision);
2447 result.set_len (mul_internal (val, xi.val, xi.len,
2448 yi.val, yi.len, precision,
2449 sgn, overflow, false));
2450 return result;
2453 /* Return X * Y, treating both X and Y as signed values. Indicate in
2454 *OVERFLOW whether the operation overflowed. */
2455 template <typename T1, typename T2>
2456 inline WI_BINARY_RESULT (T1, T2)
2457 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2459 return mul (x, y, SIGNED, overflow);
2462 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2463 *OVERFLOW whether the operation overflowed. */
2464 template <typename T1, typename T2>
2465 inline WI_BINARY_RESULT (T1, T2)
2466 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2468 return mul (x, y, UNSIGNED, overflow);
2471 /* Perform a widening multiplication of X and Y, extending the values
2472 according to SGN, and return the high part of the result. */
2473 template <typename T1, typename T2>
2474 inline WI_BINARY_RESULT (T1, T2)
2475 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2477 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2478 unsigned int precision = get_precision (result);
2479 WIDE_INT_REF_FOR (T1) xi (x, precision);
2480 WIDE_INT_REF_FOR (T2) yi (y, precision);
2481 result.set_len (mul_internal (val, xi.val, xi.len,
2482 yi.val, yi.len, precision,
2483 sgn, 0, true));
2484 return result;
2487 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2488 signedness given by SGN. Indicate in *OVERFLOW if the result
2489 overflows. */
2490 template <typename T1, typename T2>
2491 inline WI_BINARY_RESULT (T1, T2)
2492 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2494 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2495 unsigned int precision = get_precision (quotient);
2496 WIDE_INT_REF_FOR (T1) xi (x, precision);
2497 WIDE_INT_REF_FOR (T2) yi (y);
2499 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2500 precision,
2501 yi.val, yi.len, yi.precision,
2502 sgn, overflow));
2503 return quotient;
2506 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2507 template <typename T1, typename T2>
2508 inline WI_BINARY_RESULT (T1, T2)
2509 wi::sdiv_trunc (const T1 &x, const T2 &y)
2511 return div_trunc (x, y, SIGNED);
2514 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2515 template <typename T1, typename T2>
2516 inline WI_BINARY_RESULT (T1, T2)
2517 wi::udiv_trunc (const T1 &x, const T2 &y)
2519 return div_trunc (x, y, UNSIGNED);
2522 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2523 signedness given by SGN. Indicate in *OVERFLOW if the result
2524 overflows. */
2525 template <typename T1, typename T2>
2526 inline WI_BINARY_RESULT (T1, T2)
2527 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2529 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2530 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2531 unsigned int precision = get_precision (quotient);
2532 WIDE_INT_REF_FOR (T1) xi (x, precision);
2533 WIDE_INT_REF_FOR (T2) yi (y);
2535 unsigned int remainder_len;
2536 quotient.set_len (divmod_internal (quotient_val,
2537 &remainder_len, remainder_val,
2538 xi.val, xi.len, precision,
2539 yi.val, yi.len, yi.precision, sgn,
2540 overflow));
2541 remainder.set_len (remainder_len);
2542 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2543 return quotient - 1;
2544 return quotient;
2547 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2548 template <typename T1, typename T2>
2549 inline WI_BINARY_RESULT (T1, T2)
2550 wi::sdiv_floor (const T1 &x, const T2 &y)
2552 return div_floor (x, y, SIGNED);
2555 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2556 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2557 template <typename T1, typename T2>
2558 inline WI_BINARY_RESULT (T1, T2)
2559 wi::udiv_floor (const T1 &x, const T2 &y)
2561 return div_floor (x, y, UNSIGNED);
2564 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2565 signedness given by SGN. Indicate in *OVERFLOW if the result
2566 overflows. */
2567 template <typename T1, typename T2>
2568 inline WI_BINARY_RESULT (T1, T2)
2569 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2571 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2572 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2573 unsigned int precision = get_precision (quotient);
2574 WIDE_INT_REF_FOR (T1) xi (x, precision);
2575 WIDE_INT_REF_FOR (T2) yi (y);
2577 unsigned int remainder_len;
2578 quotient.set_len (divmod_internal (quotient_val,
2579 &remainder_len, remainder_val,
2580 xi.val, xi.len, precision,
2581 yi.val, yi.len, yi.precision, sgn,
2582 overflow));
2583 remainder.set_len (remainder_len);
2584 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2585 return quotient + 1;
2586 return quotient;
2589 /* Return X / Y, rouding towards nearest with ties away from zero.
2590 Treat X and Y as having the signedness given by SGN. Indicate
2591 in *OVERFLOW if the result overflows. */
2592 template <typename T1, typename T2>
2593 inline WI_BINARY_RESULT (T1, T2)
2594 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2596 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2597 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2598 unsigned int precision = get_precision (quotient);
2599 WIDE_INT_REF_FOR (T1) xi (x, precision);
2600 WIDE_INT_REF_FOR (T2) yi (y);
2602 unsigned int remainder_len;
2603 quotient.set_len (divmod_internal (quotient_val,
2604 &remainder_len, remainder_val,
2605 xi.val, xi.len, precision,
2606 yi.val, yi.len, yi.precision, sgn,
2607 overflow));
2608 remainder.set_len (remainder_len);
2610 if (remainder != 0)
2612 if (sgn == SIGNED)
2614 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2615 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2617 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2618 return quotient - 1;
2619 else
2620 return quotient + 1;
2623 else
2625 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2626 return quotient + 1;
2629 return quotient;
2632 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2633 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2634 template <typename T1, typename T2>
2635 inline WI_BINARY_RESULT (T1, T2)
2636 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2637 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2639 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2640 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2641 unsigned int precision = get_precision (quotient);
2642 WIDE_INT_REF_FOR (T1) xi (x, precision);
2643 WIDE_INT_REF_FOR (T2) yi (y);
2645 unsigned int remainder_len;
2646 quotient.set_len (divmod_internal (quotient_val,
2647 &remainder_len, remainder_val,
2648 xi.val, xi.len, precision,
2649 yi.val, yi.len, yi.precision, sgn, 0));
2650 remainder.set_len (remainder_len);
2652 *remainder_ptr = remainder;
2653 return quotient;
2656 /* Compute X / Y, rouding towards 0, and return the remainder.
2657 Treat X and Y as having the signedness given by SGN. Indicate
2658 in *OVERFLOW if the division overflows. */
2659 template <typename T1, typename T2>
2660 inline WI_BINARY_RESULT (T1, T2)
2661 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2663 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2664 unsigned int precision = get_precision (remainder);
2665 WIDE_INT_REF_FOR (T1) xi (x, precision);
2666 WIDE_INT_REF_FOR (T2) yi (y);
2668 unsigned int remainder_len;
2669 divmod_internal (0, &remainder_len, remainder_val,
2670 xi.val, xi.len, precision,
2671 yi.val, yi.len, yi.precision, sgn, overflow);
2672 remainder.set_len (remainder_len);
2674 return remainder;
2677 /* Compute X / Y, rouding towards 0, and return the remainder.
2678 Treat X and Y as signed values. */
2679 template <typename T1, typename T2>
2680 inline WI_BINARY_RESULT (T1, T2)
2681 wi::smod_trunc (const T1 &x, const T2 &y)
2683 return mod_trunc (x, y, SIGNED);
2686 /* Compute X / Y, rouding towards 0, and return the remainder.
2687 Treat X and Y as unsigned values. */
2688 template <typename T1, typename T2>
2689 inline WI_BINARY_RESULT (T1, T2)
2690 wi::umod_trunc (const T1 &x, const T2 &y)
2692 return mod_trunc (x, y, UNSIGNED);
2695 /* Compute X / Y, rouding towards -inf, and return the remainder.
2696 Treat X and Y as having the signedness given by SGN. Indicate
2697 in *OVERFLOW if the division overflows. */
2698 template <typename T1, typename T2>
2699 inline WI_BINARY_RESULT (T1, T2)
2700 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2702 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2703 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2704 unsigned int precision = get_precision (quotient);
2705 WIDE_INT_REF_FOR (T1) xi (x, precision);
2706 WIDE_INT_REF_FOR (T2) yi (y);
2708 unsigned int remainder_len;
2709 quotient.set_len (divmod_internal (quotient_val,
2710 &remainder_len, remainder_val,
2711 xi.val, xi.len, precision,
2712 yi.val, yi.len, yi.precision, sgn,
2713 overflow));
2714 remainder.set_len (remainder_len);
2716 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2717 return remainder + y;
2718 return remainder;
2721 /* Compute X / Y, rouding towards -inf, and return the remainder.
2722 Treat X and Y as unsigned values. */
2723 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2724 template <typename T1, typename T2>
2725 inline WI_BINARY_RESULT (T1, T2)
2726 wi::umod_floor (const T1 &x, const T2 &y)
2728 return mod_floor (x, y, UNSIGNED);
2731 /* Compute X / Y, rouding towards +inf, and return the remainder.
2732 Treat X and Y as having the signedness given by SGN. Indicate
2733 in *OVERFLOW if the division overflows. */
2734 template <typename T1, typename T2>
2735 inline WI_BINARY_RESULT (T1, T2)
2736 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2738 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2739 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2740 unsigned int precision = get_precision (quotient);
2741 WIDE_INT_REF_FOR (T1) xi (x, precision);
2742 WIDE_INT_REF_FOR (T2) yi (y);
2744 unsigned int remainder_len;
2745 quotient.set_len (divmod_internal (quotient_val,
2746 &remainder_len, remainder_val,
2747 xi.val, xi.len, precision,
2748 yi.val, yi.len, yi.precision, sgn,
2749 overflow));
2750 remainder.set_len (remainder_len);
2752 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2753 return remainder - y;
2754 return remainder;
2757 /* Compute X / Y, rouding towards nearest with ties away from zero,
2758 and return the remainder. Treat X and Y as having the signedness
2759 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2760 template <typename T1, typename T2>
2761 inline WI_BINARY_RESULT (T1, T2)
2762 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2764 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2765 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2766 unsigned int precision = get_precision (quotient);
2767 WIDE_INT_REF_FOR (T1) xi (x, precision);
2768 WIDE_INT_REF_FOR (T2) yi (y);
2770 unsigned int remainder_len;
2771 quotient.set_len (divmod_internal (quotient_val,
2772 &remainder_len, remainder_val,
2773 xi.val, xi.len, precision,
2774 yi.val, yi.len, yi.precision, sgn,
2775 overflow));
2776 remainder.set_len (remainder_len);
2778 if (remainder != 0)
2780 if (sgn == SIGNED)
2782 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2783 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2785 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2786 return remainder + y;
2787 else
2788 return remainder - y;
2791 else
2793 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2794 return remainder - y;
2797 return remainder;
2800 /* Return true if X is a multiple of Y. Treat X and Y as having the
2801 signedness given by SGN. */
2802 template <typename T1, typename T2>
2803 inline bool
2804 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2806 return wi::mod_trunc (x, y, sgn) == 0;
2809 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2810 Treat X and Y as having the signedness given by SGN. */
2811 template <typename T1, typename T2>
2812 inline bool
2813 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2814 WI_BINARY_RESULT (T1, T2) *res)
2816 WI_BINARY_RESULT (T1, T2) remainder;
2817 WI_BINARY_RESULT (T1, T2) quotient
2818 = divmod_trunc (x, y, sgn, &remainder);
2819 if (remainder == 0)
2821 *res = quotient;
2822 return true;
2824 return false;
2827 /* Return X << Y. Return 0 if Y is greater than or equal to
2828 the precision of X. */
2829 template <typename T1, typename T2>
2830 inline WI_UNARY_RESULT (T1)
2831 wi::lshift (const T1 &x, const T2 &y)
2833 WI_UNARY_RESULT_VAR (result, val, T1, x);
2834 unsigned int precision = get_precision (result);
2835 WIDE_INT_REF_FOR (T1) xi (x, precision);
2836 WIDE_INT_REF_FOR (T2) yi (y);
2837 /* Handle the simple cases quickly. */
2838 if (geu_p (yi, precision))
2840 val[0] = 0;
2841 result.set_len (1);
2843 else
2845 unsigned int shift = yi.to_uhwi ();
2846 /* For fixed-precision integers like offset_int and widest_int,
2847 handle the case where the shift value is constant and the
2848 result is a single nonnegative HWI (meaning that we don't
2849 need to worry about val[1]). This is particularly common
2850 for converting a byte count to a bit count.
2852 For variable-precision integers like wide_int, handle HWI
2853 and sub-HWI integers inline. */
2854 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2855 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2856 && xi.len == 1
2857 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2858 HOST_WIDE_INT_MAX >> shift))
2859 : precision <= HOST_BITS_PER_WIDE_INT)
2861 val[0] = xi.ulow () << shift;
2862 result.set_len (1);
2864 else
2865 result.set_len (lshift_large (val, xi.val, xi.len,
2866 precision, shift));
2868 return result;
2871 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2872 or equal to the precision of X. */
2873 template <typename T1, typename T2>
2874 inline WI_UNARY_RESULT (T1)
2875 wi::lrshift (const T1 &x, const T2 &y)
2877 WI_UNARY_RESULT_VAR (result, val, T1, x);
2878 /* Do things in the precision of the input rather than the output,
2879 since the result can be no larger than that. */
2880 WIDE_INT_REF_FOR (T1) xi (x);
2881 WIDE_INT_REF_FOR (T2) yi (y);
2882 /* Handle the simple cases quickly. */
2883 if (geu_p (yi, xi.precision))
2885 val[0] = 0;
2886 result.set_len (1);
2888 else
2890 unsigned int shift = yi.to_uhwi ();
2891 /* For fixed-precision integers like offset_int and widest_int,
2892 handle the case where the shift value is constant and the
2893 shifted value is a single nonnegative HWI (meaning that all
2894 bits above the HWI are zero). This is particularly common
2895 for converting a bit count to a byte count.
2897 For variable-precision integers like wide_int, handle HWI
2898 and sub-HWI integers inline. */
2899 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2900 ? xi.len == 1 && xi.val[0] >= 0
2901 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2903 val[0] = xi.to_uhwi () >> shift;
2904 result.set_len (1);
2906 else
2907 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2908 get_precision (result), shift));
2910 return result;
2913 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2914 Y is greater than or equal to the precision of X. */
2915 template <typename T1, typename T2>
2916 inline WI_UNARY_RESULT (T1)
2917 wi::arshift (const T1 &x, const T2 &y)
2919 WI_UNARY_RESULT_VAR (result, val, T1, x);
2920 /* Do things in the precision of the input rather than the output,
2921 since the result can be no larger than that. */
2922 WIDE_INT_REF_FOR (T1) xi (x);
2923 WIDE_INT_REF_FOR (T2) yi (y);
2924 /* Handle the simple cases quickly. */
2925 if (geu_p (yi, xi.precision))
2927 val[0] = sign_mask (x);
2928 result.set_len (1);
2930 else
2932 unsigned int shift = yi.to_uhwi ();
2933 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2935 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2936 result.set_len (1, true);
2938 else
2939 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
2940 get_precision (result), shift));
2942 return result;
2945 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2946 logical shift otherwise. */
2947 template <typename T1, typename T2>
2948 inline WI_UNARY_RESULT (T1)
2949 wi::rshift (const T1 &x, const T2 &y, signop sgn)
2951 if (sgn == UNSIGNED)
2952 return lrshift (x, y);
2953 else
2954 return arshift (x, y);
2957 /* Return the result of rotating the low WIDTH bits of X left by Y
2958 bits and zero-extending the result. Use a full-width rotate if
2959 WIDTH is zero. */
2960 template <typename T1, typename T2>
2961 WI_UNARY_RESULT (T1)
2962 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
2964 unsigned int precision = get_binary_precision (x, x);
2965 if (width == 0)
2966 width = precision;
2967 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2968 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
2969 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
2970 if (width != precision)
2971 return wi::zext (left, width) | wi::zext (right, width);
2972 return left | right;
2975 /* Return the result of rotating the low WIDTH bits of X right by Y
2976 bits and zero-extending the result. Use a full-width rotate if
2977 WIDTH is zero. */
2978 template <typename T1, typename T2>
2979 WI_UNARY_RESULT (T1)
2980 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
2982 unsigned int precision = get_binary_precision (x, x);
2983 if (width == 0)
2984 width = precision;
2985 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2986 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
2987 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
2988 if (width != precision)
2989 return wi::zext (left, width) | wi::zext (right, width);
2990 return left | right;
2993 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
2994 is odd. */
2995 inline int
2996 wi::parity (const wide_int_ref &x)
2998 return popcount (x) & 1;
3001 /* Extract WIDTH bits from X, starting at BITPOS. */
3002 template <typename T>
3003 inline unsigned HOST_WIDE_INT
3004 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3006 unsigned precision = get_precision (x);
3007 if (precision < bitpos + width)
3008 precision = bitpos + width;
3009 WIDE_INT_REF_FOR (T) xi (x, precision);
3011 /* Handle this rare case after the above, so that we assert about
3012 bogus BITPOS values. */
3013 if (width == 0)
3014 return 0;
3016 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3017 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3018 unsigned HOST_WIDE_INT res = xi.elt (start);
3019 res >>= shift;
3020 if (shift + width > HOST_BITS_PER_WIDE_INT)
3022 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3023 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3025 return zext_hwi (res, width);
3028 /* Return the minimum precision needed to store X with sign SGN. */
3029 template <typename T>
3030 inline unsigned int
3031 wi::min_precision (const T &x, signop sgn)
3033 if (sgn == SIGNED)
3034 return get_precision (x) - clrsb (x);
3035 else
3036 return get_precision (x) - clz (x);
3039 template<typename T>
3040 void
3041 gt_ggc_mx (generic_wide_int <T> *)
3045 template<typename T>
3046 void
3047 gt_pch_nx (generic_wide_int <T> *)
3051 template<typename T>
3052 void
3053 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3057 template<int N>
3058 void
3059 gt_ggc_mx (trailing_wide_ints <N> *)
3063 template<int N>
3064 void
3065 gt_pch_nx (trailing_wide_ints <N> *)
3069 template<int N>
3070 void
3071 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3075 namespace wi
3077 /* Used for overloaded functions in which the only other acceptable
3078 scalar type is a pointer. It stops a plain 0 from being treated
3079 as a null pointer. */
3080 struct never_used1 {};
3081 struct never_used2 {};
3083 wide_int min_value (unsigned int, signop);
3084 wide_int min_value (never_used1 *);
3085 wide_int min_value (never_used2 *);
3086 wide_int max_value (unsigned int, signop);
3087 wide_int max_value (never_used1 *);
3088 wide_int max_value (never_used2 *);
3090 /* FIXME: this is target dependent, so should be elsewhere.
3091 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3092 wide_int from_buffer (const unsigned char *, unsigned int);
3094 #ifndef GENERATOR_FILE
3095 void to_mpz (const wide_int_ref &, mpz_t, signop);
3096 #endif
3098 wide_int mask (unsigned int, bool, unsigned int);
3099 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3100 wide_int set_bit_in_zero (unsigned int, unsigned int);
3101 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3102 unsigned int);
3104 template <typename T>
3105 T mask (unsigned int, bool);
3107 template <typename T>
3108 T shifted_mask (unsigned int, unsigned int, bool);
3110 template <typename T>
3111 T set_bit_in_zero (unsigned int);
3113 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3114 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3115 bool, unsigned int);
3116 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3117 unsigned int, unsigned int, bool);
3120 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3121 and the other bits are clear, or the inverse if NEGATE_P. */
3122 inline wide_int
3123 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3125 wide_int result = wide_int::create (precision);
3126 result.set_len (mask (result.write_val (), width, negate_p, precision));
3127 return result;
3130 /* Return a PRECISION-bit integer in which the low START bits are clear,
3131 the next WIDTH bits are set, and the other bits are clear,
3132 or the inverse if NEGATE_P. */
3133 inline wide_int
3134 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3135 unsigned int precision)
3137 wide_int result = wide_int::create (precision);
3138 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3139 precision));
3140 return result;
3143 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3144 others are clear. */
3145 inline wide_int
3146 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3148 return shifted_mask (bit, 1, false, precision);
3151 /* Return an integer of type T in which the low WIDTH bits are set
3152 and the other bits are clear, or the inverse if NEGATE_P. */
3153 template <typename T>
3154 inline T
3155 wi::mask (unsigned int width, bool negate_p)
3157 STATIC_ASSERT (wi::int_traits<T>::precision);
3158 T result;
3159 result.set_len (mask (result.write_val (), width, negate_p,
3160 wi::int_traits <T>::precision));
3161 return result;
3164 /* Return an integer of type T in which the low START bits are clear,
3165 the next WIDTH bits are set, and the other bits are clear, or the
3166 inverse if NEGATE_P. */
3167 template <typename T>
3168 inline T
3169 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3171 STATIC_ASSERT (wi::int_traits<T>::precision);
3172 T result;
3173 result.set_len (shifted_mask (result.write_val (), start, width,
3174 negate_p,
3175 wi::int_traits <T>::precision));
3176 return result;
3179 /* Return an integer of type T in which bit BIT is set and all the
3180 others are clear. */
3181 template <typename T>
3182 inline T
3183 wi::set_bit_in_zero (unsigned int bit)
3185 return shifted_mask <T> (bit, 1, false);
3188 #endif /* WIDE_INT_H */