1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2013 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
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
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/>. */
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
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 remain unchanged. While this is what you
102 want, it is clearly is 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
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
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
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:
151 However, a little more syntax is required for rtl constants since
152 they do 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:
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 X if necessary
163 wide_int y = wi::uhwi (c, prec); // zero-extend X 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
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
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
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. */
224 #include "insn-modes.h"
227 #define DEBUG_WIDE_INT
230 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
231 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
232 can accomodate at least 1 more bit so that unsigned numbers of that
233 mode can be represented. Note that it is still possible to create
234 fixed_wide_ints that have precisions greater than
235 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
236 double-width multiplication result, for example. */
237 #define WIDE_INT_MAX_ELTS \
238 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
240 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
242 /* This is the max size of any pointer on any machine. It does not
243 seem to be as easy to sniff this out of the machine description as
244 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
245 multiple address sizes and may have different address sizes for
246 different address spaces. However, currently the largest pointer
247 on any platform is 64 bits. When that changes, then it is likely
248 that a target hook should be defined so that targets can make this
249 value larger for those targets. */
250 #define ADDR_MAX_BITSIZE 64
252 /* This is the internal precision used when doing any address
253 arithmetic. The '4' is really 3 + 1. Three of the bits are for
254 the number of extra bits needed to do bit addresses and single bit is
255 allow everything to be signed without loosing any precision. Then
256 everything is rounded up to the next HWI for efficiency. */
257 #define ADDR_MAX_PRECISION \
258 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) & ~(HOST_BITS_PER_WIDE_INT - 1))
260 /* The number of HWIs needed to store an offset_int. */
261 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
263 /* The type of result produced by a binary operation on types T1 and T2.
264 Defined purely for brevity. */
265 #define WI_BINARY_RESULT(T1, T2) \
266 typename wi::binary_traits <T1, T2>::result_type
268 /* The type of result produced by a unary operation on type T. */
269 #define WI_UNARY_RESULT(T) \
270 typename wi::unary_traits <T>::result_type
272 /* Define a variable RESULT to hold the result of a binary operation on
273 X and Y, which have types T1 and T2 respectively. Define VAR to
274 point to the blocks of RESULT. Once the user of the macro has
275 filled in VAR, it should call RESULT.set_len to set the number
276 of initialized blocks. */
277 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
278 WI_BINARY_RESULT (T1, T2) RESULT = \
279 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
280 HOST_WIDE_INT *VAL = RESULT.write_val ()
282 /* Similar for the result of a unary operation on X, which has type T. */
283 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
284 WI_UNARY_RESULT (T) RESULT = \
285 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
286 HOST_WIDE_INT *VAL = RESULT.write_val ()
288 template <typename T
> struct generic_wide_int
;
289 template <int N
> struct fixed_wide_int_storage
;
290 struct wide_int_storage
;
292 /* An N-bit integer. Until we can use typedef templates, use this instead. */
293 #define FIXED_WIDE_INT(N) \
294 generic_wide_int < fixed_wide_int_storage <N> >
296 typedef generic_wide_int
<wide_int_storage
> wide_int
;
297 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION
) offset_int
;
298 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
) widest_int
;
301 struct wide_int_ref_storage
;
303 typedef generic_wide_int
<wide_int_ref_storage
<false> > wide_int_ref
;
305 /* This can be used instead of wide_int_ref if the referenced value is
306 known to have type T. It carries across properties of T's representation,
307 such as whether excess upper bits in a HWI are defined, and can therefore
308 help avoid redundant work.
310 The macro could be replaced with a template typedef, once we're able
312 #define WIDE_INT_REF_FOR(T) \
314 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
318 /* Classifies an integer based on its precision. */
319 enum precision_type
{
320 /* The integer has both a precision and defined signedness. This allows
321 the integer to be converted to any width, since we know whether to fill
322 any extra bits with zeros or signs. */
325 /* The integer has a variable precision but no defined signedness. */
328 /* The integer has a constant precision (known at GCC compile time)
329 but no defined signedness. */
333 /* This class, which has no default implementation, is expected to
334 provide the following members:
336 static const enum precision_type precision_type;
337 Classifies the type of T.
339 static const unsigned int precision;
340 Only defined if precision_type == CONST_PRECISION. Specifies the
341 precision of all integers of type T.
343 static const bool host_dependent_precision;
344 True if the precision of T depends (or can depend) on the host.
346 static unsigned int get_precision (const T &x)
347 Return the number of bits in X.
349 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
350 unsigned int precision, const T &x)
351 Decompose X as a PRECISION-bit integer, returning the associated
352 wi::storage_ref. SCRATCH is available as scratch space if needed.
353 The routine should assert that PRECISION is acceptable. */
354 template <typename T
> struct int_traits
;
356 /* This class provides a single type, result_type, which specifies the
357 type of integer produced by a binary operation whose inputs have
358 types T1 and T2. The definition should be symmetric. */
359 template <typename T1
, typename T2
,
360 enum precision_type P1
= int_traits
<T1
>::precision_type
,
361 enum precision_type P2
= int_traits
<T2
>::precision_type
>
362 struct binary_traits
;
364 /* The result of a unary operation on T is the same as the result of
365 a binary operation on two values of type T. */
366 template <typename T
>
367 struct unary_traits
: public binary_traits
<T
, T
> {};
369 /* Specify the result type for each supported combination of binary
370 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
371 mixed, in order to give stronger type checking. When both inputs
372 are CONST_PRECISION, they must have the same precision. */
374 template <typename T1
, typename T2
>
375 struct binary_traits
<T1
, T2
, FLEXIBLE_PRECISION
, FLEXIBLE_PRECISION
>
377 typedef widest_int result_type
;
381 template <typename T1
, typename T2
>
382 struct binary_traits
<T1
, T2
, FLEXIBLE_PRECISION
, VAR_PRECISION
>
384 typedef wide_int result_type
;
388 template <typename T1
, typename T2
>
389 struct binary_traits
<T1
, T2
, FLEXIBLE_PRECISION
, CONST_PRECISION
>
391 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
392 so as not to confuse gengtype. */
393 typedef generic_wide_int
< fixed_wide_int_storage
394 <int_traits
<T2
>::precision
> > result_type
;
398 template <typename T1
, typename T2
>
399 struct binary_traits
<T1
, T2
, VAR_PRECISION
, FLEXIBLE_PRECISION
>
401 typedef wide_int result_type
;
405 template <typename T1
, typename T2
>
406 struct binary_traits
<T1
, T2
, CONST_PRECISION
, FLEXIBLE_PRECISION
>
408 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
409 so as not to confuse gengtype. */
410 typedef generic_wide_int
< fixed_wide_int_storage
411 <int_traits
<T1
>::precision
> > result_type
;
415 template <typename T1
, typename T2
>
416 struct binary_traits
<T1
, T2
, CONST_PRECISION
, CONST_PRECISION
>
418 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
419 so as not to confuse gengtype. */
420 STATIC_ASSERT (int_traits
<T1
>::precision
== int_traits
<T2
>::precision
);
421 typedef generic_wide_int
< fixed_wide_int_storage
422 <int_traits
<T1
>::precision
> > result_type
;
426 template <typename T1
, typename T2
>
427 struct binary_traits
<T1
, T2
, VAR_PRECISION
, VAR_PRECISION
>
429 typedef wide_int result_type
;
433 /* Public functions for querying and operating on integers. */
436 template <typename T
>
437 unsigned int get_precision (const T
&);
439 template <typename T1
, typename T2
>
440 unsigned int get_binary_precision (const T1
&, const T2
&);
442 template <typename T1
, typename T2
>
443 void copy (T1
&, const T2
&);
445 #define UNARY_PREDICATE \
446 template <typename T> bool
447 #define UNARY_FUNCTION \
448 template <typename T> WI_UNARY_RESULT (T)
449 #define BINARY_PREDICATE \
450 template <typename T1, typename T2> bool
451 #define BINARY_FUNCTION \
452 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
453 #define SHIFT_FUNCTION \
454 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
456 UNARY_PREDICATE
fits_shwi_p (const T
&);
457 UNARY_PREDICATE
fits_uhwi_p (const T
&);
458 UNARY_PREDICATE
neg_p (const T
&, signop
= SIGNED
);
460 template <typename T
>
461 HOST_WIDE_INT
sign_mask (const T
&);
463 BINARY_PREDICATE
eq_p (const T1
&, const T2
&);
464 BINARY_PREDICATE
ne_p (const T1
&, const T2
&);
465 BINARY_PREDICATE
lt_p (const T1
&, const T2
&, signop
);
466 BINARY_PREDICATE
lts_p (const T1
&, const T2
&);
467 BINARY_PREDICATE
ltu_p (const T1
&, const T2
&);
468 BINARY_PREDICATE
le_p (const T1
&, const T2
&, signop
);
469 BINARY_PREDICATE
les_p (const T1
&, const T2
&);
470 BINARY_PREDICATE
leu_p (const T1
&, const T2
&);
471 BINARY_PREDICATE
gt_p (const T1
&, const T2
&, signop
);
472 BINARY_PREDICATE
gts_p (const T1
&, const T2
&);
473 BINARY_PREDICATE
gtu_p (const T1
&, const T2
&);
474 BINARY_PREDICATE
ge_p (const T1
&, const T2
&, signop
);
475 BINARY_PREDICATE
ges_p (const T1
&, const T2
&);
476 BINARY_PREDICATE
geu_p (const T1
&, const T2
&);
478 template <typename T1
, typename T2
>
479 int cmp (const T1
&, const T2
&, signop
);
481 template <typename T1
, typename T2
>
482 int cmps (const T1
&, const T2
&);
484 template <typename T1
, typename T2
>
485 int cmpu (const T1
&, const T2
&);
487 UNARY_FUNCTION
bit_not (const T
&);
488 UNARY_FUNCTION
neg (const T
&);
489 UNARY_FUNCTION
neg (const T
&, bool *);
490 UNARY_FUNCTION
abs (const T
&);
491 UNARY_FUNCTION
ext (const T
&, unsigned int, signop
);
492 UNARY_FUNCTION
sext (const T
&, unsigned int);
493 UNARY_FUNCTION
zext (const T
&, unsigned int);
494 UNARY_FUNCTION
set_bit (const T
&, unsigned int);
496 BINARY_FUNCTION
min (const T1
&, const T2
&, signop
);
497 BINARY_FUNCTION
smin (const T1
&, const T2
&);
498 BINARY_FUNCTION
umin (const T1
&, const T2
&);
499 BINARY_FUNCTION
max (const T1
&, const T2
&, signop
);
500 BINARY_FUNCTION
smax (const T1
&, const T2
&);
501 BINARY_FUNCTION
umax (const T1
&, const T2
&);
503 BINARY_FUNCTION
bit_and (const T1
&, const T2
&);
504 BINARY_FUNCTION
bit_and_not (const T1
&, const T2
&);
505 BINARY_FUNCTION
bit_or (const T1
&, const T2
&);
506 BINARY_FUNCTION
bit_or_not (const T1
&, const T2
&);
507 BINARY_FUNCTION
bit_xor (const T1
&, const T2
&);
508 BINARY_FUNCTION
add (const T1
&, const T2
&);
509 BINARY_FUNCTION
add (const T1
&, const T2
&, signop
, bool *);
510 BINARY_FUNCTION
sub (const T1
&, const T2
&);
511 BINARY_FUNCTION
sub (const T1
&, const T2
&, signop
, bool *);
512 BINARY_FUNCTION
mul (const T1
&, const T2
&);
513 BINARY_FUNCTION
mul (const T1
&, const T2
&, signop
, bool *);
514 BINARY_FUNCTION
smul (const T1
&, const T2
&, bool *);
515 BINARY_FUNCTION
umul (const T1
&, const T2
&, bool *);
516 BINARY_FUNCTION
mul_high (const T1
&, const T2
&, signop
);
517 BINARY_FUNCTION
div_trunc (const T1
&, const T2
&, signop
, bool * = 0);
518 BINARY_FUNCTION
sdiv_trunc (const T1
&, const T2
&);
519 BINARY_FUNCTION
udiv_trunc (const T1
&, const T2
&);
520 BINARY_FUNCTION
div_floor (const T1
&, const T2
&, signop
, bool * = 0);
521 BINARY_FUNCTION
udiv_floor (const T1
&, const T2
&);
522 BINARY_FUNCTION
sdiv_floor (const T1
&, const T2
&);
523 BINARY_FUNCTION
div_ceil (const T1
&, const T2
&, signop
, bool * = 0);
524 BINARY_FUNCTION
udiv_ceil (const T1
&, const T2
&);
525 BINARY_FUNCTION
div_round (const T1
&, const T2
&, signop
, bool * = 0);
526 BINARY_FUNCTION
divmod_trunc (const T1
&, const T2
&, signop
,
527 WI_BINARY_RESULT (T1
, T2
) *);
528 BINARY_FUNCTION
mod_trunc (const T1
&, const T2
&, signop
, bool * = 0);
529 BINARY_FUNCTION
smod_trunc (const T1
&, const T2
&);
530 BINARY_FUNCTION
umod_trunc (const T1
&, const T2
&);
531 BINARY_FUNCTION
mod_floor (const T1
&, const T2
&, signop
, bool * = 0);
532 BINARY_FUNCTION
umod_floor (const T1
&, const T2
&);
533 BINARY_FUNCTION
mod_ceil (const T1
&, const T2
&, signop
, bool * = 0);
534 BINARY_FUNCTION
mod_round (const T1
&, const T2
&, signop
, bool * = 0);
536 template <typename T1
, typename T2
>
537 bool multiple_of_p (const T1
&, const T2
&, signop
,
538 WI_BINARY_RESULT (T1
, T2
) *);
540 SHIFT_FUNCTION
lshift (const T1
&, const T2
&);
541 SHIFT_FUNCTION
lrshift (const T1
&, const T2
&);
542 SHIFT_FUNCTION
arshift (const T1
&, const T2
&);
543 SHIFT_FUNCTION
rshift (const T1
&, const T2
&, signop sgn
);
544 SHIFT_FUNCTION
lrotate (const T1
&, const T2
&, unsigned int = 0);
545 SHIFT_FUNCTION
rrotate (const T1
&, const T2
&, unsigned int = 0);
547 #undef SHIFT_FUNCTION
548 #undef BINARY_PREDICATE
549 #undef BINARY_FUNCTION
550 #undef UNARY_PREDICATE
551 #undef UNARY_FUNCTION
553 bool only_sign_bit_p (const wide_int_ref
&, unsigned int);
554 bool only_sign_bit_p (const wide_int_ref
&);
555 int clz (const wide_int_ref
&);
556 int clrsb (const wide_int_ref
&);
557 int ctz (const wide_int_ref
&);
558 int exact_log2 (const wide_int_ref
&);
559 int floor_log2 (const wide_int_ref
&);
560 int ffs (const wide_int_ref
&);
561 int popcount (const wide_int_ref
&);
562 int parity (const wide_int_ref
&);
564 template <typename T
>
565 unsigned HOST_WIDE_INT
extract_uhwi (const T
&, unsigned int, unsigned int);
567 template <typename T
>
568 unsigned int min_precision (const T
&, signop
);
573 /* Contains the components of a decomposed integer for easy, direct
577 storage_ref (const HOST_WIDE_INT
*, unsigned int, unsigned int);
579 const HOST_WIDE_INT
*val
;
581 unsigned int precision
;
583 /* Provide enough trappings for this class to act as storage for
585 unsigned int get_len () const;
586 unsigned int get_precision () const;
587 const HOST_WIDE_INT
*get_val () const;
591 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT
*val_in
,
593 unsigned int precision_in
)
594 : val (val_in
), len (len_in
), precision (precision_in
)
599 wi::storage_ref::get_len () const
605 wi::storage_ref::get_precision () const
610 inline const HOST_WIDE_INT
*
611 wi::storage_ref::get_val () const
616 /* This class defines an integer type using the storage provided by the
617 template argument. The storage class must provide the following
620 unsigned int get_precision () const
621 Return the number of bits in the integer.
623 HOST_WIDE_INT *get_val () const
624 Return a pointer to the array of blocks that encodes the integer.
626 unsigned int get_len () const
627 Return the number of blocks in get_val (). If this is smaller
628 than the number of blocks implied by get_precision (), the
629 remaining blocks are sign extensions of block get_len () - 1.
631 Although not required by generic_wide_int itself, writable storage
632 classes can also provide the following functions:
634 HOST_WIDE_INT *write_val ()
635 Get a modifiable version of get_val ()
637 unsigned int set_len (unsigned int len)
638 Set the value returned by get_len () to LEN. */
639 template <typename storage
>
640 class GTY(()) generic_wide_int
: public storage
645 template <typename T
>
646 generic_wide_int (const T
&);
648 template <typename T
>
649 generic_wide_int (const T
&, unsigned int);
652 HOST_WIDE_INT
to_shwi (unsigned int) const;
653 HOST_WIDE_INT
to_shwi () const;
654 unsigned HOST_WIDE_INT
to_uhwi (unsigned int) const;
655 unsigned HOST_WIDE_INT
to_uhwi () const;
656 HOST_WIDE_INT
to_short_addr () const;
658 /* Public accessors for the interior of a wide int. */
659 HOST_WIDE_INT
sign_mask () const;
660 HOST_WIDE_INT
elt (unsigned int) const;
661 unsigned HOST_WIDE_INT
ulow () const;
662 unsigned HOST_WIDE_INT
uhigh () const;
663 HOST_WIDE_INT
slow () const;
664 HOST_WIDE_INT
shigh () const;
666 template <typename T
>
667 generic_wide_int
&operator = (const T
&);
669 #define BINARY_PREDICATE(OP, F) \
670 template <typename T> \
671 bool OP (const T &c) const { return wi::F (*this, c); }
673 #define UNARY_OPERATOR(OP, F) \
674 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
676 #define BINARY_OPERATOR(OP, F) \
677 template <typename T> \
678 WI_BINARY_RESULT (generic_wide_int, T) \
679 OP (const T &c) const { return wi::F (*this, c); }
681 #define ASSIGNMENT_OPERATOR(OP, F) \
682 template <typename T> \
683 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
685 #define INCDEC_OPERATOR(OP, DELTA) \
686 generic_wide_int &OP () { *this += DELTA; return *this; }
688 UNARY_OPERATOR (operator ~, bit_not
)
689 UNARY_OPERATOR (operator -, neg
)
690 BINARY_PREDICATE (operator ==, eq_p
)
691 BINARY_PREDICATE (operator !=, ne_p
)
692 BINARY_OPERATOR (operator &, bit_and
)
693 BINARY_OPERATOR (and_not
, bit_and_not
)
694 BINARY_OPERATOR (operator |, bit_or
)
695 BINARY_OPERATOR (or_not
, bit_or_not
)
696 BINARY_OPERATOR (operator ^, bit_xor
)
697 BINARY_OPERATOR (operator +, add
)
698 BINARY_OPERATOR (operator -, sub
)
699 BINARY_OPERATOR (operator *, mul
)
700 ASSIGNMENT_OPERATOR (operator &=, bit_and
)
701 ASSIGNMENT_OPERATOR (operator |=, bit_or
)
702 ASSIGNMENT_OPERATOR (operator ^=, bit_xor
)
703 ASSIGNMENT_OPERATOR (operator +=, add
)
704 ASSIGNMENT_OPERATOR (operator -=, sub
)
705 ASSIGNMENT_OPERATOR (operator *=, mul
)
706 INCDEC_OPERATOR (operator ++, 1)
707 INCDEC_OPERATOR (operator --, -1)
709 #undef BINARY_PREDICATE
710 #undef UNARY_OPERATOR
711 #undef BINARY_OPERATOR
712 #undef ASSIGNMENT_OPERATOR
713 #undef INCDEC_OPERATOR
715 char *dump (char *) const;
717 static const bool is_sign_extended
718 = wi::int_traits
<generic_wide_int
<storage
> >::is_sign_extended
;
721 template <typename storage
>
722 inline generic_wide_int
<storage
>::generic_wide_int () {}
724 template <typename storage
>
725 template <typename T
>
726 inline generic_wide_int
<storage
>::generic_wide_int (const T
&x
)
731 template <typename storage
>
732 template <typename T
>
733 inline generic_wide_int
<storage
>::generic_wide_int (const T
&x
,
734 unsigned int precision
)
735 : storage (x
, precision
)
739 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
740 If THIS does not fit in PRECISION, the information is lost. */
741 template <typename storage
>
743 generic_wide_int
<storage
>::to_shwi (unsigned int precision
) const
745 if (precision
< HOST_BITS_PER_WIDE_INT
)
746 return sext_hwi (this->get_val ()[0], precision
);
748 return this->get_val ()[0];
751 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
752 template <typename storage
>
754 generic_wide_int
<storage
>::to_shwi () const
756 if (is_sign_extended
)
757 return this->get_val ()[0];
759 return to_shwi (this->get_precision ());
762 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
763 PRECISION. If THIS does not fit in PRECISION, the information
765 template <typename storage
>
766 inline unsigned HOST_WIDE_INT
767 generic_wide_int
<storage
>::to_uhwi (unsigned int precision
) const
769 if (precision
< HOST_BITS_PER_WIDE_INT
)
770 return zext_hwi (this->get_val ()[0], precision
);
772 return this->get_val ()[0];
775 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
776 template <typename storage
>
777 inline unsigned HOST_WIDE_INT
778 generic_wide_int
<storage
>::to_uhwi () const
780 return to_uhwi (this->get_precision ());
783 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
784 represent addresses to using offset_int to represent addresses.
785 We use to_short_addr at the interface from new code to old,
787 template <typename storage
>
789 generic_wide_int
<storage
>::to_short_addr () const
791 return this->get_val ()[0];
794 /* Return the implicit value of blocks above get_len (). */
795 template <typename storage
>
797 generic_wide_int
<storage
>::sign_mask () const
799 unsigned int len
= this->get_len ();
800 unsigned HOST_WIDE_INT high
= this->get_val ()[len
- 1];
801 if (!is_sign_extended
)
803 unsigned int precision
= this->get_precision ();
804 int excess
= len
* HOST_BITS_PER_WIDE_INT
- precision
;
808 return (HOST_WIDE_INT
) (high
) < 0 ? -1 : 0;
811 /* Return the signed value of the least-significant explicitly-encoded
813 template <typename storage
>
815 generic_wide_int
<storage
>::slow () const
817 return this->get_val ()[0];
820 /* Return the signed value of the most-significant explicitly-encoded
822 template <typename storage
>
824 generic_wide_int
<storage
>::shigh () const
826 return this->get_val ()[this->get_len () - 1];
829 /* Return the unsigned value of the least-significant
830 explicitly-encoded block. */
831 template <typename storage
>
832 inline unsigned HOST_WIDE_INT
833 generic_wide_int
<storage
>::ulow () const
835 return this->get_val ()[0];
838 /* Return the unsigned value of the most-significant
839 explicitly-encoded block. */
840 template <typename storage
>
841 inline unsigned HOST_WIDE_INT
842 generic_wide_int
<storage
>::uhigh () const
844 return this->get_val ()[this->get_len () - 1];
847 /* Return block I, which might be implicitly or explicit encoded. */
848 template <typename storage
>
850 generic_wide_int
<storage
>::elt (unsigned int i
) const
852 if (i
>= this->get_len ())
855 return this->get_val ()[i
];
858 template <typename storage
>
859 template <typename T
>
860 generic_wide_int
<storage
> &
861 generic_wide_int
<storage
>::operator = (const T
&x
)
863 storage::operator = (x
);
870 template <typename storage
>
871 struct int_traits
< generic_wide_int
<storage
> >
872 : public wi::int_traits
<storage
>
874 static unsigned int get_precision (const generic_wide_int
<storage
> &);
875 static wi::storage_ref
decompose (HOST_WIDE_INT
*, unsigned int,
876 const generic_wide_int
<storage
> &);
880 template <typename storage
>
882 wi::int_traits
< generic_wide_int
<storage
> >::
883 get_precision (const generic_wide_int
<storage
> &x
)
885 return x
.get_precision ();
888 template <typename storage
>
889 inline wi::storage_ref
890 wi::int_traits
< generic_wide_int
<storage
> >::
891 decompose (HOST_WIDE_INT
*, unsigned int precision
,
892 const generic_wide_int
<storage
> &x
)
894 gcc_checking_assert (precision
== x
.get_precision ());
895 return wi::storage_ref (x
.get_val (), x
.get_len (), precision
);
898 /* Provide the storage for a wide_int_ref. This acts like a read-only
899 wide_int, with the optimization that VAL is normally a pointer to
900 another integer's storage, so that no array copy is needed. */
902 struct wide_int_ref_storage
: public wi::storage_ref
905 /* Scratch space that can be used when decomposing the original integer.
906 It must live as long as this object. */
907 HOST_WIDE_INT scratch
[2];
910 wide_int_ref_storage (const wi::storage_ref
&);
912 template <typename T
>
913 wide_int_ref_storage (const T
&);
915 template <typename T
>
916 wide_int_ref_storage (const T
&, unsigned int);
919 /* Create a reference from an existing reference. */
921 inline wide_int_ref_storage
<SE
>::
922 wide_int_ref_storage (const wi::storage_ref
&x
)
926 /* Create a reference to integer X in its natural precision. Note
927 that the natural precision is host-dependent for primitive
930 template <typename T
>
931 inline wide_int_ref_storage
<SE
>::wide_int_ref_storage (const T
&x
)
932 : storage_ref (wi::int_traits
<T
>::decompose (scratch
,
933 wi::get_precision (x
), x
))
937 /* Create a reference to integer X in precision PRECISION. */
939 template <typename T
>
940 inline wide_int_ref_storage
<SE
>::wide_int_ref_storage (const T
&x
,
941 unsigned int precision
)
942 : storage_ref (wi::int_traits
<T
>::decompose (scratch
, precision
, x
))
950 struct int_traits
<wide_int_ref_storage
<SE
> >
952 static const enum precision_type precision_type
= VAR_PRECISION
;
953 /* wi::storage_ref can be a reference to a primitive type,
954 so this is the conservatively-correct setting. */
955 static const bool host_dependent_precision
= true;
956 static const bool is_sign_extended
= SE
;
962 unsigned int force_to_size (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
963 unsigned int, unsigned int, unsigned int,
965 unsigned int from_array (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
966 unsigned int, unsigned int, bool = true);
969 /* The storage used by wide_int. */
970 class GTY(()) wide_int_storage
973 HOST_WIDE_INT val
[WIDE_INT_MAX_ELTS
];
975 unsigned int precision
;
979 template <typename T
>
980 wide_int_storage (const T
&);
982 /* The standard generic_wide_int storage methods. */
983 unsigned int get_precision () const;
984 const HOST_WIDE_INT
*get_val () const;
985 unsigned int get_len () const;
986 HOST_WIDE_INT
*write_val ();
987 void set_len (unsigned int, bool = false);
989 static wide_int
from (const wide_int_ref
&, unsigned int, signop
);
990 static wide_int
from_array (const HOST_WIDE_INT
*, unsigned int,
991 unsigned int, bool = true);
992 static wide_int
create (unsigned int);
994 /* FIXME: target-dependent, so should disappear. */
995 wide_int
bswap () const;
1001 struct int_traits
<wide_int_storage
>
1003 static const enum precision_type precision_type
= VAR_PRECISION
;
1004 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1005 static const bool host_dependent_precision
= false;
1006 static const bool is_sign_extended
= true;
1007 template <typename T1
, typename T2
>
1008 static wide_int
get_binary_result (const T1
&, const T2
&);
1012 inline wide_int_storage::wide_int_storage () {}
1014 /* Initialize the storage from integer X, in its natural precision.
1015 Note that we do not allow integers with host-dependent precision
1016 to become wide_ints; wide_ints must always be logically independent
1018 template <typename T
>
1019 inline wide_int_storage::wide_int_storage (const T
&x
)
1021 STATIC_ASSERT (!wi::int_traits
<T
>::host_dependent_precision
);
1022 WIDE_INT_REF_FOR (T
) xi (x
);
1023 precision
= xi
.precision
;
1024 wi::copy (*this, xi
);
1028 wide_int_storage::get_precision () const
1033 inline const HOST_WIDE_INT
*
1034 wide_int_storage::get_val () const
1040 wide_int_storage::get_len () const
1045 inline HOST_WIDE_INT
*
1046 wide_int_storage::write_val ()
1052 wide_int_storage::set_len (unsigned int l
, bool is_sign_extended
)
1055 if (!is_sign_extended
&& len
* HOST_BITS_PER_WIDE_INT
> precision
)
1056 val
[len
- 1] = sext_hwi (val
[len
- 1],
1057 precision
% HOST_BITS_PER_WIDE_INT
);
1060 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1063 wide_int_storage::from (const wide_int_ref
&x
, unsigned int precision
,
1066 wide_int result
= wide_int::create (precision
);
1067 result
.set_len (wi::force_to_size (result
.write_val (), x
.val
, x
.len
,
1068 x
.precision
, precision
, sgn
));
1072 /* Create a wide_int from the explicit block encoding given by VAL and
1073 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1074 true if the encoding may have redundant trailing blocks. */
1076 wide_int_storage::from_array (const HOST_WIDE_INT
*val
, unsigned int len
,
1077 unsigned int precision
, bool need_canon_p
)
1079 wide_int result
= wide_int::create (precision
);
1080 result
.set_len (wi::from_array (result
.write_val (), val
, len
, precision
,
1085 /* Return an uninitialized wide_int with precision PRECISION. */
1087 wide_int_storage::create (unsigned int precision
)
1090 x
.precision
= precision
;
1094 template <typename T1
, typename T2
>
1096 wi::int_traits
<wide_int_storage
>::get_binary_result (const T1
&x
, const T2
&y
)
1098 /* This shouldn't be used for two flexible-precision inputs. */
1099 STATIC_ASSERT (wi::int_traits
<T1
>::precision_type
!= FLEXIBLE_PRECISION
1100 || wi::int_traits
<T2
>::precision_type
!= FLEXIBLE_PRECISION
);
1101 if (wi::int_traits
<T1
>::precision_type
== FLEXIBLE_PRECISION
)
1102 return wide_int::create (wi::get_precision (y
));
1104 return wide_int::create (wi::get_precision (x
));
1107 /* The storage used by FIXED_WIDE_INT (N). */
1109 class GTY(()) fixed_wide_int_storage
1112 HOST_WIDE_INT val
[(N
+ HOST_BITS_PER_WIDE_INT
+ 1) / HOST_BITS_PER_WIDE_INT
];
1116 fixed_wide_int_storage ();
1117 template <typename T
>
1118 fixed_wide_int_storage (const T
&);
1120 /* The standard generic_wide_int storage methods. */
1121 unsigned int get_precision () const;
1122 const HOST_WIDE_INT
*get_val () const;
1123 unsigned int get_len () const;
1124 HOST_WIDE_INT
*write_val ();
1125 void set_len (unsigned int, bool = false);
1127 static FIXED_WIDE_INT (N
) from (const wide_int_ref
&, signop
);
1128 static FIXED_WIDE_INT (N
) from_array (const HOST_WIDE_INT
*, unsigned int,
1136 struct int_traits
< fixed_wide_int_storage
<N
> >
1138 static const enum precision_type precision_type
= CONST_PRECISION
;
1139 static const bool host_dependent_precision
= false;
1140 static const bool is_sign_extended
= true;
1141 static const unsigned int precision
= N
;
1142 template <typename T1
, typename T2
>
1143 static FIXED_WIDE_INT (N
) get_binary_result (const T1
&, const T2
&);
1148 inline fixed_wide_int_storage
<N
>::fixed_wide_int_storage () {}
1150 /* Initialize the storage from integer X, in precision N. */
1152 template <typename T
>
1153 inline fixed_wide_int_storage
<N
>::fixed_wide_int_storage (const T
&x
)
1155 /* Check for type compatibility. We don't want to initialize a
1156 fixed-width integer from something like a wide_int. */
1157 WI_BINARY_RESULT (T
, FIXED_WIDE_INT (N
)) *assertion ATTRIBUTE_UNUSED
;
1158 wi::copy (*this, WIDE_INT_REF_FOR (T
) (x
, N
));
1163 fixed_wide_int_storage
<N
>::get_precision () const
1169 inline const HOST_WIDE_INT
*
1170 fixed_wide_int_storage
<N
>::get_val () const
1177 fixed_wide_int_storage
<N
>::get_len () const
1183 inline HOST_WIDE_INT
*
1184 fixed_wide_int_storage
<N
>::write_val ()
1191 fixed_wide_int_storage
<N
>::set_len (unsigned int l
, bool)
1194 /* There are no excess bits in val[len - 1]. */
1195 STATIC_ASSERT (N
% HOST_BITS_PER_WIDE_INT
== 0);
1198 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1200 inline FIXED_WIDE_INT (N
)
1201 fixed_wide_int_storage
<N
>::from (const wide_int_ref
&x
, signop sgn
)
1203 FIXED_WIDE_INT (N
) result
;
1204 result
.set_len (wi::force_to_size (result
.write_val (), x
.val
, x
.len
,
1205 x
.precision
, N
, sgn
));
1209 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1210 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1213 inline FIXED_WIDE_INT (N
)
1214 fixed_wide_int_storage
<N
>::from_array (const HOST_WIDE_INT
*val
,
1218 FIXED_WIDE_INT (N
) result
;
1219 result
.set_len (wi::from_array (result
.write_val (), val
, len
,
1225 template <typename T1
, typename T2
>
1226 inline FIXED_WIDE_INT (N
)
1227 wi::int_traits
< fixed_wide_int_storage
<N
> >::
1228 get_binary_result (const T1
&, const T2
&)
1230 return FIXED_WIDE_INT (N
) ();
1233 /* A reference to one element of a trailing_wide_ints structure. */
1234 class trailing_wide_int_storage
1237 /* The precision of the integer, which is a fixed property of the
1238 parent trailing_wide_ints. */
1239 unsigned int m_precision
;
1241 /* A pointer to the length field. */
1242 unsigned char *m_len
;
1244 /* A pointer to the HWI array. There are enough elements to hold all
1245 values of precision M_PRECISION. */
1246 HOST_WIDE_INT
*m_val
;
1249 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT
*);
1251 /* The standard generic_wide_int storage methods. */
1252 unsigned int get_len () const;
1253 unsigned int get_precision () const;
1254 const HOST_WIDE_INT
*get_val () const;
1255 HOST_WIDE_INT
*write_val ();
1256 void set_len (unsigned int, bool = false);
1258 template <typename T
>
1259 trailing_wide_int_storage
&operator = (const T
&);
1262 typedef generic_wide_int
<trailing_wide_int_storage
> trailing_wide_int
;
1264 /* trailing_wide_int behaves like a wide_int. */
1268 struct int_traits
<trailing_wide_int_storage
>
1269 : public int_traits
<wide_int_storage
> {};
1272 /* An array of N wide_int-like objects that can be put at the end of
1273 a variable-sized structure. Use extra_size to calculate how many
1274 bytes beyond the sizeof need to be allocated. Use set_precision
1275 to initialize the structure. */
1277 class GTY(()) trailing_wide_ints
1280 /* The shared precision of each number. */
1281 unsigned short m_precision
;
1283 /* The shared maximum length of each number. */
1284 unsigned char m_max_len
;
1286 /* The current length of each number. */
1287 unsigned char m_len
[N
];
1289 /* The variable-length part of the structure, which always contains
1290 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1291 HOST_WIDE_INT m_val
[1];
1294 void set_precision (unsigned int);
1295 trailing_wide_int
operator [] (unsigned int);
1296 static size_t extra_size (unsigned int);
1299 inline trailing_wide_int_storage::
1300 trailing_wide_int_storage (unsigned int precision
, unsigned char *len
,
1302 : m_precision (precision
), m_len (len
), m_val (val
)
1307 trailing_wide_int_storage::get_len () const
1313 trailing_wide_int_storage::get_precision () const
1318 inline const HOST_WIDE_INT
*
1319 trailing_wide_int_storage::get_val () const
1324 inline HOST_WIDE_INT
*
1325 trailing_wide_int_storage::write_val ()
1331 trailing_wide_int_storage::set_len (unsigned int len
, bool is_sign_extended
)
1334 if (!is_sign_extended
&& len
* HOST_BITS_PER_WIDE_INT
> m_precision
)
1335 m_val
[len
- 1] = sext_hwi (m_val
[len
- 1],
1336 m_precision
% HOST_BITS_PER_WIDE_INT
);
1339 template <typename T
>
1340 inline trailing_wide_int_storage
&
1341 trailing_wide_int_storage::operator = (const T
&x
)
1343 WIDE_INT_REF_FOR (T
) xi (x
, m_precision
);
1344 wi::copy (*this, xi
);
1348 /* Initialize the structure and record that all elements have precision
1352 trailing_wide_ints
<N
>::set_precision (unsigned int precision
)
1354 m_precision
= precision
;
1355 m_max_len
= ((precision
+ HOST_BITS_PER_WIDE_INT
- 1)
1356 / HOST_BITS_PER_WIDE_INT
);
1359 /* Return a reference to element INDEX. */
1361 inline trailing_wide_int
1362 trailing_wide_ints
<N
>::operator [] (unsigned int index
)
1364 return trailing_wide_int_storage (m_precision
, &m_len
[index
],
1365 &m_val
[index
* m_max_len
]);
1368 /* Return how many extra bytes need to be added to the end of the structure
1369 in order to handle N wide_ints of precision PRECISION. */
1372 trailing_wide_ints
<N
>::extra_size (unsigned int precision
)
1374 unsigned int max_len
= ((precision
+ HOST_BITS_PER_WIDE_INT
- 1)
1375 / HOST_BITS_PER_WIDE_INT
);
1376 return (N
* max_len
- 1) * sizeof (HOST_WIDE_INT
);
1379 /* This macro is used in structures that end with a trailing_wide_ints field
1380 called FIELD. It declares get_NAME() and set_NAME() methods to access
1381 element I of FIELD. */
1382 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1383 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1384 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1388 /* Implementation of int_traits for primitive integer types like "int". */
1389 template <typename T
, bool signed_p
>
1390 struct primitive_int_traits
1392 static const enum precision_type precision_type
= FLEXIBLE_PRECISION
;
1393 static const bool host_dependent_precision
= true;
1394 static const bool is_sign_extended
= true;
1395 static unsigned int get_precision (T
);
1396 static wi::storage_ref
decompose (HOST_WIDE_INT
*, unsigned int, T
);
1400 template <typename T
, bool signed_p
>
1402 wi::primitive_int_traits
<T
, signed_p
>::get_precision (T
)
1404 return sizeof (T
) * CHAR_BIT
;
1407 template <typename T
, bool signed_p
>
1408 inline wi::storage_ref
1409 wi::primitive_int_traits
<T
, signed_p
>::decompose (HOST_WIDE_INT
*scratch
,
1410 unsigned int precision
, T x
)
1413 if (signed_p
|| scratch
[0] >= 0 || precision
<= HOST_BITS_PER_WIDE_INT
)
1414 return wi::storage_ref (scratch
, 1, precision
);
1416 return wi::storage_ref (scratch
, 2, precision
);
1419 /* Allow primitive C types to be used in wi:: routines. */
1423 struct int_traits
<int>
1424 : public primitive_int_traits
<int, true> {};
1427 struct int_traits
<unsigned int>
1428 : public primitive_int_traits
<unsigned int, false> {};
1430 #if HOST_BITS_PER_INT != HOST_BITS_PER_WIDE_INT
1432 struct int_traits
<HOST_WIDE_INT
>
1433 : public primitive_int_traits
<HOST_WIDE_INT
, true> {};
1436 struct int_traits
<unsigned HOST_WIDE_INT
>
1437 : public primitive_int_traits
<unsigned HOST_WIDE_INT
, false> {};
1443 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1444 and precision PRECISION. */
1445 struct hwi_with_prec
1447 hwi_with_prec (HOST_WIDE_INT
, unsigned int, signop
);
1449 unsigned int precision
;
1453 hwi_with_prec
shwi (HOST_WIDE_INT
, unsigned int);
1454 hwi_with_prec
uhwi (unsigned HOST_WIDE_INT
, unsigned int);
1456 hwi_with_prec
minus_one (unsigned int);
1457 hwi_with_prec
zero (unsigned int);
1458 hwi_with_prec
one (unsigned int);
1459 hwi_with_prec
two (unsigned int);
1462 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v
, unsigned int p
,
1464 : val (v
), precision (p
), sgn (s
)
1468 /* Return a signed integer that has value VAL and precision PRECISION. */
1469 inline wi::hwi_with_prec
1470 wi::shwi (HOST_WIDE_INT val
, unsigned int precision
)
1472 return hwi_with_prec (val
, precision
, SIGNED
);
1475 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1476 inline wi::hwi_with_prec
1477 wi::uhwi (unsigned HOST_WIDE_INT val
, unsigned int precision
)
1479 return hwi_with_prec (val
, precision
, UNSIGNED
);
1482 /* Return a wide int of -1 with precision PRECISION. */
1483 inline wi::hwi_with_prec
1484 wi::minus_one (unsigned int precision
)
1486 return wi::shwi (-1, precision
);
1489 /* Return a wide int of 0 with precision PRECISION. */
1490 inline wi::hwi_with_prec
1491 wi::zero (unsigned int precision
)
1493 return wi::shwi (0, precision
);
1496 /* Return a wide int of 1 with precision PRECISION. */
1497 inline wi::hwi_with_prec
1498 wi::one (unsigned int precision
)
1500 return wi::shwi (1, precision
);
1503 /* Return a wide int of 2 with precision PRECISION. */
1504 inline wi::hwi_with_prec
1505 wi::two (unsigned int precision
)
1507 return wi::shwi (2, precision
);
1513 struct int_traits
<wi::hwi_with_prec
>
1515 static const enum precision_type precision_type
= VAR_PRECISION
;
1516 /* hwi_with_prec has an explicitly-given precision, rather than the
1517 precision of HOST_WIDE_INT. */
1518 static const bool host_dependent_precision
= false;
1519 static const bool is_sign_extended
= true;
1520 static unsigned int get_precision (const wi::hwi_with_prec
&);
1521 static wi::storage_ref
decompose (HOST_WIDE_INT
*, unsigned int,
1522 const wi::hwi_with_prec
&);
1527 wi::int_traits
<wi::hwi_with_prec
>::get_precision (const wi::hwi_with_prec
&x
)
1532 inline wi::storage_ref
1533 wi::int_traits
<wi::hwi_with_prec
>::
1534 decompose (HOST_WIDE_INT
*scratch
, unsigned int precision
,
1535 const wi::hwi_with_prec
&x
)
1537 gcc_checking_assert (precision
== x
.precision
);
1539 if (x
.sgn
== SIGNED
|| x
.val
>= 0 || precision
<= HOST_BITS_PER_WIDE_INT
)
1540 return wi::storage_ref (scratch
, 1, precision
);
1542 return wi::storage_ref (scratch
, 2, precision
);
1545 /* Private functions for handling large cases out of line. They take
1546 individual length and array parameters because that is cheaper for
1547 the inline caller than constructing an object on the stack and
1548 passing a reference to it. (Although many callers use wide_int_refs,
1549 we generally want those to be removed by SRA.) */
1552 bool eq_p_large (const HOST_WIDE_INT
*, unsigned int,
1553 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1554 bool lts_p_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1555 const HOST_WIDE_INT
*, unsigned int);
1556 bool ltu_p_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1557 const HOST_WIDE_INT
*, unsigned int);
1558 int cmps_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1559 const HOST_WIDE_INT
*, unsigned int);
1560 int cmpu_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1561 const HOST_WIDE_INT
*, unsigned int);
1562 unsigned int sext_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1564 unsigned int, unsigned int);
1565 unsigned int zext_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1567 unsigned int, unsigned int);
1568 unsigned int set_bit_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1569 unsigned int, unsigned int, unsigned int);
1570 unsigned int lshift_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1571 unsigned int, unsigned int, unsigned int);
1572 unsigned int lrshift_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1573 unsigned int, unsigned int, unsigned int,
1575 unsigned int arshift_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1576 unsigned int, unsigned int, unsigned int,
1578 unsigned int and_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1579 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1580 unsigned int and_not_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1581 unsigned int, const HOST_WIDE_INT
*,
1582 unsigned int, unsigned int);
1583 unsigned int or_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1584 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1585 unsigned int or_not_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1586 unsigned int, const HOST_WIDE_INT
*,
1587 unsigned int, unsigned int);
1588 unsigned int xor_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1589 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1590 unsigned int add_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1591 const HOST_WIDE_INT
*, unsigned int, unsigned int,
1593 unsigned int sub_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1594 const HOST_WIDE_INT
*, unsigned int, unsigned int,
1596 unsigned int mul_internal (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1597 unsigned int, const HOST_WIDE_INT
*,
1598 unsigned int, unsigned int, signop
, bool *,
1600 unsigned int divmod_internal (HOST_WIDE_INT
*, unsigned int *,
1601 HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1602 unsigned int, unsigned int,
1603 const HOST_WIDE_INT
*,
1604 unsigned int, unsigned int,
1608 /* Return the number of bits that integer X can hold. */
1609 template <typename T
>
1611 wi::get_precision (const T
&x
)
1613 return wi::int_traits
<T
>::get_precision (x
);
1616 /* Return the number of bits that the result of a binary operation can
1617 hold when the input operands are X and Y. */
1618 template <typename T1
, typename T2
>
1620 wi::get_binary_precision (const T1
&x
, const T2
&y
)
1622 return get_precision (wi::int_traits
<WI_BINARY_RESULT (T1
, T2
)>::
1623 get_binary_result (x
, y
));
1626 /* Copy the contents of Y to X, but keeping X's current precision. */
1627 template <typename T1
, typename T2
>
1629 wi::copy (T1
&x
, const T2
&y
)
1631 HOST_WIDE_INT
*xval
= x
.write_val ();
1632 const HOST_WIDE_INT
*yval
= y
.get_val ();
1633 unsigned int len
= y
.get_len ();
1638 x
.set_len (len
, y
.is_sign_extended
);
1641 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1642 template <typename T
>
1644 wi::fits_shwi_p (const T
&x
)
1646 WIDE_INT_REF_FOR (T
) xi (x
);
1650 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1652 template <typename T
>
1654 wi::fits_uhwi_p (const T
&x
)
1656 WIDE_INT_REF_FOR (T
) xi (x
);
1657 if (xi
.precision
<= HOST_BITS_PER_WIDE_INT
)
1660 return xi
.slow () >= 0;
1661 return xi
.len
== 2 && xi
.uhigh () == 0;
1664 /* Return true if X is negative based on the interpretation of SGN.
1665 For UNSIGNED, this is always false. */
1666 template <typename T
>
1668 wi::neg_p (const T
&x
, signop sgn
)
1670 WIDE_INT_REF_FOR (T
) xi (x
);
1671 if (sgn
== UNSIGNED
)
1673 return xi
.sign_mask () < 0;
1676 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1677 template <typename T
>
1678 inline HOST_WIDE_INT
1679 wi::sign_mask (const T
&x
)
1681 WIDE_INT_REF_FOR (T
) xi (x
);
1682 return xi
.sign_mask ();
1685 /* Return true if X == Y. X and Y must be binary-compatible. */
1686 template <typename T1
, typename T2
>
1688 wi::eq_p (const T1
&x
, const T2
&y
)
1690 unsigned int precision
= get_binary_precision (x
, y
);
1691 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1692 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1693 if (xi
.is_sign_extended
&& yi
.is_sign_extended
)
1695 /* This case reduces to array equality. */
1696 if (xi
.len
!= yi
.len
)
1700 if (xi
.val
[i
] != yi
.val
[i
])
1702 while (++i
!= xi
.len
);
1705 if (__builtin_expect (yi
.len
== 1, true))
1707 /* XI is only equal to YI if it too has a single HWI. */
1710 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1711 with 0 are simple. */
1712 if (STATIC_CONSTANT_P (yi
.val
[0] == 0))
1713 return xi
.val
[0] == 0;
1714 /* Otherwise flush out any excess bits first. */
1715 unsigned HOST_WIDE_INT diff
= xi
.val
[0] ^ yi
.val
[0];
1716 int excess
= HOST_BITS_PER_WIDE_INT
- precision
;
1721 return eq_p_large (xi
.val
, xi
.len
, yi
.val
, yi
.len
, precision
);
1724 /* Return true if X != Y. X and Y must be binary-compatible. */
1725 template <typename T1
, typename T2
>
1727 wi::ne_p (const T1
&x
, const T2
&y
)
1729 return !eq_p (x
, y
);
1732 /* Return true if X < Y when both are treated as signed values. */
1733 template <typename T1
, typename T2
>
1735 wi::lts_p (const T1
&x
, const T2
&y
)
1737 unsigned int precision
= get_binary_precision (x
, y
);
1738 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1739 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1740 /* We optimize x < y, where y is 64 or fewer bits. */
1741 if (wi::fits_shwi_p (yi
))
1743 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1744 if (STATIC_CONSTANT_P (yi
.val
[0] == 0))
1746 /* If x fits directly into a shwi, we can compare directly. */
1747 if (wi::fits_shwi_p (xi
))
1748 return xi
.to_shwi () < yi
.to_shwi ();
1749 /* If x doesn't fit and is negative, then it must be more
1750 negative than any value in y, and hence smaller than y. */
1753 /* If x is positive, then it must be larger than any value in y,
1754 and hence greater than y. */
1757 /* Optimize the opposite case, if it can be detected at compile time. */
1758 if (STATIC_CONSTANT_P (xi
.len
== 1))
1759 /* If YI is negative it is lower than the least HWI.
1760 If YI is positive it is greater than the greatest HWI. */
1762 return lts_p_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
1765 /* Return true if X < Y when both are treated as unsigned values. */
1766 template <typename T1
, typename T2
>
1768 wi::ltu_p (const T1
&x
, const T2
&y
)
1770 unsigned int precision
= get_binary_precision (x
, y
);
1771 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1772 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1773 /* Optimize comparisons with constants. */
1774 if (STATIC_CONSTANT_P (yi
.len
== 1 && yi
.val
[0] >= 0))
1775 return xi
.len
== 1 && xi
.to_uhwi () < (unsigned HOST_WIDE_INT
) yi
.val
[0];
1776 if (STATIC_CONSTANT_P (xi
.len
== 1 && xi
.val
[0] >= 0))
1777 return yi
.len
!= 1 || yi
.to_uhwi () > (unsigned HOST_WIDE_INT
) xi
.val
[0];
1778 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1779 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1780 values does not change the result. */
1781 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
1783 unsigned HOST_WIDE_INT xl
= xi
.to_uhwi ();
1784 unsigned HOST_WIDE_INT yl
= yi
.to_uhwi ();
1787 return ltu_p_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
1790 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1791 template <typename T1
, typename T2
>
1793 wi::lt_p (const T1
&x
, const T2
&y
, signop sgn
)
1796 return lts_p (x
, y
);
1798 return ltu_p (x
, y
);
1801 /* Return true if X <= Y when both are treated as signed values. */
1802 template <typename T1
, typename T2
>
1804 wi::les_p (const T1
&x
, const T2
&y
)
1806 return !lts_p (y
, x
);
1809 /* Return true if X <= Y when both are treated as unsigned values. */
1810 template <typename T1
, typename T2
>
1812 wi::leu_p (const T1
&x
, const T2
&y
)
1814 return !ltu_p (y
, x
);
1817 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1818 template <typename T1
, typename T2
>
1820 wi::le_p (const T1
&x
, const T2
&y
, signop sgn
)
1823 return les_p (x
, y
);
1825 return leu_p (x
, y
);
1828 /* Return true if X > Y when both are treated as signed values. */
1829 template <typename T1
, typename T2
>
1831 wi::gts_p (const T1
&x
, const T2
&y
)
1833 return lts_p (y
, x
);
1836 /* Return true if X > Y when both are treated as unsigned values. */
1837 template <typename T1
, typename T2
>
1839 wi::gtu_p (const T1
&x
, const T2
&y
)
1841 return ltu_p (y
, x
);
1844 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1845 template <typename T1
, typename T2
>
1847 wi::gt_p (const T1
&x
, const T2
&y
, signop sgn
)
1850 return gts_p (x
, y
);
1852 return gtu_p (x
, y
);
1855 /* Return true if X >= Y when both are treated as signed values. */
1856 template <typename T1
, typename T2
>
1858 wi::ges_p (const T1
&x
, const T2
&y
)
1860 return !lts_p (x
, y
);
1863 /* Return true if X >= Y when both are treated as unsigned values. */
1864 template <typename T1
, typename T2
>
1866 wi::geu_p (const T1
&x
, const T2
&y
)
1868 return !ltu_p (x
, y
);
1871 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1872 template <typename T1
, typename T2
>
1874 wi::ge_p (const T1
&x
, const T2
&y
, signop sgn
)
1877 return ges_p (x
, y
);
1879 return geu_p (x
, y
);
1882 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1883 as signed values. */
1884 template <typename T1
, typename T2
>
1886 wi::cmps (const T1
&x
, const T2
&y
)
1888 unsigned int precision
= get_binary_precision (x
, y
);
1889 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1890 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1891 if (wi::fits_shwi_p (yi
))
1893 /* Special case for comparisons with 0. */
1894 if (STATIC_CONSTANT_P (yi
.val
[0] == 0))
1895 return neg_p (xi
) ? -1 : !(xi
.len
== 1 && xi
.val
[0] == 0);
1896 /* If x fits into a signed HWI, we can compare directly. */
1897 if (wi::fits_shwi_p (xi
))
1899 HOST_WIDE_INT xl
= xi
.to_shwi ();
1900 HOST_WIDE_INT yl
= yi
.to_shwi ();
1901 return xl
< yl
? -1 : xl
> yl
;
1903 /* If x doesn't fit and is negative, then it must be more
1904 negative than any signed HWI, and hence smaller than y. */
1907 /* If x is positive, then it must be larger than any signed HWI,
1908 and hence greater than y. */
1911 /* Optimize the opposite case, if it can be detected at compile time. */
1912 if (STATIC_CONSTANT_P (xi
.len
== 1))
1913 /* If YI is negative it is lower than the least HWI.
1914 If YI is positive it is greater than the greatest HWI. */
1915 return neg_p (yi
) ? 1 : -1;
1916 return cmps_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
1919 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1920 as unsigned values. */
1921 template <typename T1
, typename T2
>
1923 wi::cmpu (const T1
&x
, const T2
&y
)
1925 unsigned int precision
= get_binary_precision (x
, y
);
1926 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1927 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1928 /* Optimize comparisons with constants. */
1929 if (STATIC_CONSTANT_P (yi
.len
== 1 && yi
.val
[0] >= 0))
1931 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1934 /* Otherwise compare directly. */
1935 unsigned HOST_WIDE_INT xl
= xi
.to_uhwi ();
1936 unsigned HOST_WIDE_INT yl
= yi
.val
[0];
1937 return xl
< yl
? -1 : xl
> yl
;
1939 if (STATIC_CONSTANT_P (xi
.len
== 1 && xi
.val
[0] >= 0))
1941 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1944 /* Otherwise compare directly. */
1945 unsigned HOST_WIDE_INT xl
= xi
.val
[0];
1946 unsigned HOST_WIDE_INT yl
= yi
.to_uhwi ();
1947 return xl
< yl
? -1 : xl
> yl
;
1949 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1950 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1951 values does not change the result. */
1952 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
1954 unsigned HOST_WIDE_INT xl
= xi
.to_uhwi ();
1955 unsigned HOST_WIDE_INT yl
= yi
.to_uhwi ();
1956 return xl
< yl
? -1 : xl
> yl
;
1958 return cmpu_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
1961 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1962 X and Y indicated by SGN. */
1963 template <typename T1
, typename T2
>
1965 wi::cmp (const T1
&x
, const T2
&y
, signop sgn
)
1974 template <typename T
>
1975 inline WI_UNARY_RESULT (T
)
1976 wi::bit_not (const T
&x
)
1978 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
1979 WIDE_INT_REF_FOR (T
) xi (x
, get_precision (result
));
1980 for (unsigned int i
= 0; i
< xi
.len
; ++i
)
1981 val
[i
] = ~xi
.val
[i
];
1982 result
.set_len (xi
.len
);
1987 template <typename T
>
1988 inline WI_UNARY_RESULT (T
)
1989 wi::neg (const T
&x
)
1994 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
1995 template <typename T
>
1996 inline WI_UNARY_RESULT (T
)
1997 wi::neg (const T
&x
, bool *overflow
)
1999 *overflow
= only_sign_bit_p (x
);
2003 /* Return the absolute value of x. */
2004 template <typename T
>
2005 inline WI_UNARY_RESULT (T
)
2006 wi::abs (const T
&x
)
2008 return neg_p (x
) ? neg (x
) : WI_UNARY_RESULT (T
) (x
);
2011 /* Return the result of sign-extending the low OFFSET bits of X. */
2012 template <typename T
>
2013 inline WI_UNARY_RESULT (T
)
2014 wi::sext (const T
&x
, unsigned int offset
)
2016 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
2017 unsigned int precision
= get_precision (result
);
2018 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
2020 if (offset
<= HOST_BITS_PER_WIDE_INT
)
2022 val
[0] = sext_hwi (xi
.ulow (), offset
);
2023 result
.set_len (1, true);
2026 result
.set_len (sext_large (val
, xi
.val
, xi
.len
, precision
, offset
));
2030 /* Return the result of zero-extending the low OFFSET bits of X. */
2031 template <typename T
>
2032 inline WI_UNARY_RESULT (T
)
2033 wi::zext (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 /* This is not just an optimization, it is actually required to
2040 maintain canonization. */
2041 if (offset
>= precision
)
2043 wi::copy (result
, xi
);
2047 /* In these cases we know that at least the top bit will be clear,
2048 so no sign extension is necessary. */
2049 if (offset
< HOST_BITS_PER_WIDE_INT
)
2051 val
[0] = zext_hwi (xi
.ulow (), offset
);
2052 result
.set_len (1, true);
2055 result
.set_len (zext_large (val
, xi
.val
, xi
.len
, precision
, offset
), true);
2059 /* Return the result of extending the low OFFSET bits of X according to
2061 template <typename T
>
2062 inline WI_UNARY_RESULT (T
)
2063 wi::ext (const T
&x
, unsigned int offset
, signop sgn
)
2065 return sgn
== SIGNED
? sext (x
, offset
) : zext (x
, offset
);
2068 /* Return an integer that represents X | (1 << bit). */
2069 template <typename T
>
2070 inline WI_UNARY_RESULT (T
)
2071 wi::set_bit (const T
&x
, unsigned int bit
)
2073 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
2074 unsigned int precision
= get_precision (result
);
2075 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
2076 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2078 val
[0] = xi
.ulow () | ((unsigned HOST_WIDE_INT
) 1 << bit
);
2082 result
.set_len (set_bit_large (val
, xi
.val
, xi
.len
, precision
, bit
));
2086 /* Return the mininum of X and Y, treating them both as having
2088 template <typename T1
, typename T2
>
2089 inline WI_BINARY_RESULT (T1
, T2
)
2090 wi::min (const T1
&x
, const T2
&y
, signop sgn
)
2092 WI_BINARY_RESULT_VAR (result
, val ATTRIBUTE_UNUSED
, T1
, x
, T2
, y
);
2093 unsigned int precision
= get_precision (result
);
2094 if (wi::le_p (x
, y
, sgn
))
2095 wi::copy (result
, WIDE_INT_REF_FOR (T1
) (x
, precision
));
2097 wi::copy (result
, WIDE_INT_REF_FOR (T2
) (y
, precision
));
2101 /* Return the minimum of X and Y, treating both as signed values. */
2102 template <typename T1
, typename T2
>
2103 inline WI_BINARY_RESULT (T1
, T2
)
2104 wi::smin (const T1
&x
, const T2
&y
)
2106 return min (x
, y
, SIGNED
);
2109 /* Return the minimum of X and Y, treating both as unsigned values. */
2110 template <typename T1
, typename T2
>
2111 inline WI_BINARY_RESULT (T1
, T2
)
2112 wi::umin (const T1
&x
, const T2
&y
)
2114 return min (x
, y
, UNSIGNED
);
2117 /* Return the maxinum of X and Y, treating them both as having
2119 template <typename T1
, typename T2
>
2120 inline WI_BINARY_RESULT (T1
, T2
)
2121 wi::max (const T1
&x
, const T2
&y
, signop sgn
)
2123 WI_BINARY_RESULT_VAR (result
, val ATTRIBUTE_UNUSED
, T1
, x
, T2
, y
);
2124 unsigned int precision
= get_precision (result
);
2125 if (wi::ge_p (x
, y
, sgn
))
2126 wi::copy (result
, WIDE_INT_REF_FOR (T1
) (x
, precision
));
2128 wi::copy (result
, WIDE_INT_REF_FOR (T2
) (y
, precision
));
2132 /* Return the maximum of X and Y, treating both as signed values. */
2133 template <typename T1
, typename T2
>
2134 inline WI_BINARY_RESULT (T1
, T2
)
2135 wi::smax (const T1
&x
, const T2
&y
)
2137 return max (x
, y
, SIGNED
);
2140 /* Return the maximum of X and Y, treating both as unsigned values. */
2141 template <typename T1
, typename T2
>
2142 inline WI_BINARY_RESULT (T1
, T2
)
2143 wi::umax (const T1
&x
, const T2
&y
)
2145 return max (x
, y
, UNSIGNED
);
2149 template <typename T1
, typename T2
>
2150 inline WI_BINARY_RESULT (T1
, T2
)
2151 wi::bit_and (const T1
&x
, const T2
&y
)
2153 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2154 unsigned int precision
= get_precision (result
);
2155 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2156 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2157 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2158 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2160 val
[0] = xi
.ulow () & yi
.ulow ();
2161 result
.set_len (1, is_sign_extended
);
2164 result
.set_len (and_large (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2165 precision
), is_sign_extended
);
2169 /* Return X & ~Y. */
2170 template <typename T1
, typename T2
>
2171 inline WI_BINARY_RESULT (T1
, T2
)
2172 wi::bit_and_not (const T1
&x
, const T2
&y
)
2174 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2175 unsigned int precision
= get_precision (result
);
2176 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2177 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2178 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2179 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2181 val
[0] = xi
.ulow () & ~yi
.ulow ();
2182 result
.set_len (1, is_sign_extended
);
2185 result
.set_len (and_not_large (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2186 precision
), is_sign_extended
);
2191 template <typename T1
, typename T2
>
2192 inline WI_BINARY_RESULT (T1
, T2
)
2193 wi::bit_or (const T1
&x
, const T2
&y
)
2195 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2196 unsigned int precision
= get_precision (result
);
2197 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2198 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2199 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2200 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2202 val
[0] = xi
.ulow () | yi
.ulow ();
2203 result
.set_len (1, is_sign_extended
);
2206 result
.set_len (or_large (val
, xi
.val
, xi
.len
,
2207 yi
.val
, yi
.len
, precision
), is_sign_extended
);
2211 /* Return X | ~Y. */
2212 template <typename T1
, typename T2
>
2213 inline WI_BINARY_RESULT (T1
, T2
)
2214 wi::bit_or_not (const T1
&x
, const T2
&y
)
2216 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2217 unsigned int precision
= get_precision (result
);
2218 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2219 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2220 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2221 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2223 val
[0] = xi
.ulow () | ~yi
.ulow ();
2224 result
.set_len (1, is_sign_extended
);
2227 result
.set_len (or_not_large (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2228 precision
), is_sign_extended
);
2233 template <typename T1
, typename T2
>
2234 inline WI_BINARY_RESULT (T1
, T2
)
2235 wi::bit_xor (const T1
&x
, const T2
&y
)
2237 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2238 unsigned int precision
= get_precision (result
);
2239 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2240 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2241 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2242 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2244 val
[0] = xi
.ulow () ^ yi
.ulow ();
2245 result
.set_len (1, is_sign_extended
);
2248 result
.set_len (xor_large (val
, xi
.val
, xi
.len
,
2249 yi
.val
, yi
.len
, precision
), is_sign_extended
);
2254 template <typename T1
, typename T2
>
2255 inline WI_BINARY_RESULT (T1
, T2
)
2256 wi::add (const T1
&x
, const T2
&y
)
2258 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2259 unsigned int precision
= get_precision (result
);
2260 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2261 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2262 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2264 val
[0] = xi
.ulow () + yi
.ulow ();
2267 /* If the precision is known at compile time to be greater than
2268 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2269 knowing that (a) all bits in those HWIs are significant and
2270 (b) the result has room for at least two HWIs. This provides
2271 a fast path for things like offset_int and widest_int.
2273 The STATIC_CONSTANT_P test prevents this path from being
2274 used for wide_ints. wide_ints with precisions greater than
2275 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2276 point handling them inline. */
2277 else if (STATIC_CONSTANT_P (precision
> HOST_BITS_PER_WIDE_INT
)
2278 && __builtin_expect (xi
.len
+ yi
.len
== 2, true))
2280 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2281 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2282 unsigned HOST_WIDE_INT resultl
= xl
+ yl
;
2284 val
[1] = (HOST_WIDE_INT
) resultl
< 0 ? 0 : -1;
2285 result
.set_len (1 + (((resultl
^ xl
) & (resultl
^ yl
))
2286 >> (HOST_BITS_PER_WIDE_INT
- 1)));
2289 result
.set_len (add_large (val
, xi
.val
, xi
.len
,
2290 yi
.val
, yi
.len
, precision
,
2295 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2296 and indicate in *OVERFLOW whether the operation overflowed. */
2297 template <typename T1
, typename T2
>
2298 inline WI_BINARY_RESULT (T1
, T2
)
2299 wi::add (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2301 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2302 unsigned int precision
= get_precision (result
);
2303 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2304 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2305 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2307 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2308 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2309 unsigned HOST_WIDE_INT resultl
= xl
+ yl
;
2311 *overflow
= (((resultl
^ xl
) & (resultl
^ yl
))
2312 >> (precision
- 1)) & 1;
2314 *overflow
= ((resultl
<< (HOST_BITS_PER_WIDE_INT
- precision
))
2315 < (xl
<< (HOST_BITS_PER_WIDE_INT
- precision
)));
2320 result
.set_len (add_large (val
, xi
.val
, xi
.len
,
2321 yi
.val
, yi
.len
, precision
,
2327 template <typename T1
, typename T2
>
2328 inline WI_BINARY_RESULT (T1
, T2
)
2329 wi::sub (const T1
&x
, const T2
&y
)
2331 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2332 unsigned int precision
= get_precision (result
);
2333 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2334 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2335 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2337 val
[0] = xi
.ulow () - yi
.ulow ();
2340 /* If the precision is known at compile time to be greater than
2341 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2342 knowing that (a) all bits in those HWIs are significant and
2343 (b) the result has room for at least two HWIs. This provides
2344 a fast path for things like offset_int and widest_int.
2346 The STATIC_CONSTANT_P test prevents this path from being
2347 used for wide_ints. wide_ints with precisions greater than
2348 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2349 point handling them inline. */
2350 else if (STATIC_CONSTANT_P (precision
> HOST_BITS_PER_WIDE_INT
)
2351 && __builtin_expect (xi
.len
+ yi
.len
== 2, true))
2353 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2354 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2355 unsigned HOST_WIDE_INT resultl
= xl
- yl
;
2357 val
[1] = (HOST_WIDE_INT
) resultl
< 0 ? 0 : -1;
2358 result
.set_len (1 + (((resultl
^ xl
) & (xl
^ yl
))
2359 >> (HOST_BITS_PER_WIDE_INT
- 1)));
2362 result
.set_len (sub_large (val
, xi
.val
, xi
.len
,
2363 yi
.val
, yi
.len
, precision
,
2368 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2369 and indicate in *OVERFLOW whether the operation overflowed. */
2370 template <typename T1
, typename T2
>
2371 inline WI_BINARY_RESULT (T1
, T2
)
2372 wi::sub (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2374 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2375 unsigned int precision
= get_precision (result
);
2376 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2377 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2378 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2380 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2381 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2382 unsigned HOST_WIDE_INT resultl
= xl
- yl
;
2384 *overflow
= (((xl
^ yl
) & (resultl
^ xl
)) >> (precision
- 1)) & 1;
2386 *overflow
= ((resultl
<< (HOST_BITS_PER_WIDE_INT
- precision
))
2387 > (xl
<< (HOST_BITS_PER_WIDE_INT
- precision
)));
2392 result
.set_len (sub_large (val
, xi
.val
, xi
.len
,
2393 yi
.val
, yi
.len
, precision
,
2399 template <typename T1
, typename T2
>
2400 inline WI_BINARY_RESULT (T1
, T2
)
2401 wi::mul (const T1
&x
, const T2
&y
)
2403 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2404 unsigned int precision
= get_precision (result
);
2405 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2406 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2407 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2409 val
[0] = xi
.ulow () * yi
.ulow ();
2413 result
.set_len (mul_internal (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2414 precision
, UNSIGNED
, 0, false));
2418 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2419 and indicate in *OVERFLOW whether the operation overflowed. */
2420 template <typename T1
, typename T2
>
2421 inline WI_BINARY_RESULT (T1
, T2
)
2422 wi::mul (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2424 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2425 unsigned int precision
= get_precision (result
);
2426 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2427 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2428 result
.set_len (mul_internal (val
, xi
.val
, xi
.len
,
2429 yi
.val
, yi
.len
, precision
,
2430 sgn
, overflow
, false));
2434 /* Return X * Y, treating both X and Y as signed values. Indicate in
2435 *OVERFLOW whether the operation overflowed. */
2436 template <typename T1
, typename T2
>
2437 inline WI_BINARY_RESULT (T1
, T2
)
2438 wi::smul (const T1
&x
, const T2
&y
, bool *overflow
)
2440 return mul (x
, y
, SIGNED
, overflow
);
2443 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2444 *OVERFLOW whether the operation overflowed. */
2445 template <typename T1
, typename T2
>
2446 inline WI_BINARY_RESULT (T1
, T2
)
2447 wi::umul (const T1
&x
, const T2
&y
, bool *overflow
)
2449 return mul (x
, y
, UNSIGNED
, overflow
);
2452 /* Perform a widening multiplication of X and Y, extending the values
2453 according to SGN, and return the high part of the result. */
2454 template <typename T1
, typename T2
>
2455 inline WI_BINARY_RESULT (T1
, T2
)
2456 wi::mul_high (const T1
&x
, const T2
&y
, signop sgn
)
2458 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2459 unsigned int precision
= get_precision (result
);
2460 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2461 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2462 result
.set_len (mul_internal (val
, xi
.val
, xi
.len
,
2463 yi
.val
, yi
.len
, precision
,
2468 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2469 signedness given by SGN. Indicate in *OVERFLOW if the result
2471 template <typename T1
, typename T2
>
2472 inline WI_BINARY_RESULT (T1
, T2
)
2473 wi::div_trunc (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2475 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2476 unsigned int precision
= get_precision (quotient
);
2477 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2478 WIDE_INT_REF_FOR (T2
) yi (y
);
2480 quotient
.set_len (divmod_internal (quotient_val
, 0, 0, xi
.val
, xi
.len
,
2482 yi
.val
, yi
.len
, yi
.precision
,
2487 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2488 template <typename T1
, typename T2
>
2489 inline WI_BINARY_RESULT (T1
, T2
)
2490 wi::sdiv_trunc (const T1
&x
, const T2
&y
)
2492 return div_trunc (x
, y
, SIGNED
);
2495 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2496 template <typename T1
, typename T2
>
2497 inline WI_BINARY_RESULT (T1
, T2
)
2498 wi::udiv_trunc (const T1
&x
, const T2
&y
)
2500 return div_trunc (x
, y
, UNSIGNED
);
2503 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2504 signedness given by SGN. Indicate in *OVERFLOW if the result
2506 template <typename T1
, typename T2
>
2507 inline WI_BINARY_RESULT (T1
, T2
)
2508 wi::div_floor (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2510 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2511 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2512 unsigned int precision
= get_precision (quotient
);
2513 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2514 WIDE_INT_REF_FOR (T2
) yi (y
);
2516 unsigned int remainder_len
;
2517 quotient
.set_len (divmod_internal (quotient_val
,
2518 &remainder_len
, remainder_val
,
2519 xi
.val
, xi
.len
, precision
,
2520 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2522 remainder
.set_len (remainder_len
);
2523 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
) && remainder
!= 0)
2524 return quotient
- 1;
2528 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2529 template <typename T1
, typename T2
>
2530 inline WI_BINARY_RESULT (T1
, T2
)
2531 wi::sdiv_floor (const T1
&x
, const T2
&y
)
2533 return div_floor (x
, y
, SIGNED
);
2536 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2537 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2538 template <typename T1
, typename T2
>
2539 inline WI_BINARY_RESULT (T1
, T2
)
2540 wi::udiv_floor (const T1
&x
, const T2
&y
)
2542 return div_floor (x
, y
, UNSIGNED
);
2545 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2546 signedness given by SGN. Indicate in *OVERFLOW if the result
2548 template <typename T1
, typename T2
>
2549 inline WI_BINARY_RESULT (T1
, T2
)
2550 wi::div_ceil (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2552 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2553 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2554 unsigned int precision
= get_precision (quotient
);
2555 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2556 WIDE_INT_REF_FOR (T2
) yi (y
);
2558 unsigned int remainder_len
;
2559 quotient
.set_len (divmod_internal (quotient_val
,
2560 &remainder_len
, remainder_val
,
2561 xi
.val
, xi
.len
, precision
,
2562 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2564 remainder
.set_len (remainder_len
);
2565 if (wi::neg_p (x
, sgn
) == wi::neg_p (y
, sgn
) && remainder
!= 0)
2566 return quotient
+ 1;
2570 template <typename T1
, typename T2
>
2571 inline WI_BINARY_RESULT (T1
, T2
)
2572 wi::udiv_ceil (const T1
&x
, const T2
&y
)
2574 return div_ceil (x
, y
, UNSIGNED
);
2577 /* Return X / Y, rouding towards nearest with ties away from zero.
2578 Treat X and Y as having the signedness given by SGN. Indicate
2579 in *OVERFLOW if the result overflows. */
2580 template <typename T1
, typename T2
>
2581 inline WI_BINARY_RESULT (T1
, T2
)
2582 wi::div_round (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2584 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2585 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2586 unsigned int precision
= get_precision (quotient
);
2587 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2588 WIDE_INT_REF_FOR (T2
) yi (y
);
2590 unsigned int remainder_len
;
2591 quotient
.set_len (divmod_internal (quotient_val
,
2592 &remainder_len
, remainder_val
,
2593 xi
.val
, xi
.len
, precision
,
2594 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2596 remainder
.set_len (remainder_len
);
2602 if (wi::ges_p (wi::abs (remainder
),
2603 wi::lrshift (wi::abs (y
), 1)))
2605 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
))
2606 return quotient
- 1;
2608 return quotient
+ 1;
2613 if (wi::geu_p (remainder
, wi::lrshift (y
, 1)))
2614 return quotient
+ 1;
2620 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2621 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2622 template <typename T1
, typename T2
>
2623 inline WI_BINARY_RESULT (T1
, T2
)
2624 wi::divmod_trunc (const T1
&x
, const T2
&y
, signop sgn
,
2625 WI_BINARY_RESULT (T1
, T2
) *remainder_ptr
)
2627 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2628 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2629 unsigned int precision
= get_precision (quotient
);
2630 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2631 WIDE_INT_REF_FOR (T2
) yi (y
);
2633 unsigned int remainder_len
;
2634 quotient
.set_len (divmod_internal (quotient_val
,
2635 &remainder_len
, remainder_val
,
2636 xi
.val
, xi
.len
, precision
,
2637 yi
.val
, yi
.len
, yi
.precision
, sgn
, 0));
2638 remainder
.set_len (remainder_len
);
2640 *remainder_ptr
= remainder
;
2644 /* Compute X / Y, rouding towards 0, and return the remainder.
2645 Treat X and Y as having the signedness given by SGN. Indicate
2646 in *OVERFLOW if the division overflows. */
2647 template <typename T1
, typename T2
>
2648 inline WI_BINARY_RESULT (T1
, T2
)
2649 wi::mod_trunc (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2651 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2652 unsigned int precision
= get_precision (remainder
);
2653 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2654 WIDE_INT_REF_FOR (T2
) yi (y
);
2656 unsigned int remainder_len
;
2657 divmod_internal (0, &remainder_len
, remainder_val
,
2658 xi
.val
, xi
.len
, precision
,
2659 yi
.val
, yi
.len
, yi
.precision
, sgn
, overflow
);
2660 remainder
.set_len (remainder_len
);
2665 /* Compute X / Y, rouding towards 0, and return the remainder.
2666 Treat X and Y as signed values. */
2667 template <typename T1
, typename T2
>
2668 inline WI_BINARY_RESULT (T1
, T2
)
2669 wi::smod_trunc (const T1
&x
, const T2
&y
)
2671 return mod_trunc (x
, y
, SIGNED
);
2674 /* Compute X / Y, rouding towards 0, and return the remainder.
2675 Treat X and Y as unsigned values. */
2676 template <typename T1
, typename T2
>
2677 inline WI_BINARY_RESULT (T1
, T2
)
2678 wi::umod_trunc (const T1
&x
, const T2
&y
)
2680 return mod_trunc (x
, y
, UNSIGNED
);
2683 /* Compute X / Y, rouding towards -inf, and return the remainder.
2684 Treat X and Y as having the signedness given by SGN. Indicate
2685 in *OVERFLOW if the division overflows. */
2686 template <typename T1
, typename T2
>
2687 inline WI_BINARY_RESULT (T1
, T2
)
2688 wi::mod_floor (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2690 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2691 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2692 unsigned int precision
= get_precision (quotient
);
2693 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2694 WIDE_INT_REF_FOR (T2
) yi (y
);
2696 unsigned int remainder_len
;
2697 quotient
.set_len (divmod_internal (quotient_val
,
2698 &remainder_len
, remainder_val
,
2699 xi
.val
, xi
.len
, precision
,
2700 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2702 remainder
.set_len (remainder_len
);
2704 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
) && remainder
!= 0)
2705 return remainder
+ y
;
2709 /* Compute X / Y, rouding towards -inf, and return the remainder.
2710 Treat X and Y as unsigned values. */
2711 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2712 template <typename T1
, typename T2
>
2713 inline WI_BINARY_RESULT (T1
, T2
)
2714 wi::umod_floor (const T1
&x
, const T2
&y
)
2716 return mod_floor (x
, y
, UNSIGNED
);
2719 /* Compute X / Y, rouding towards +inf, and return the remainder.
2720 Treat X and Y as having the signedness given by SGN. Indicate
2721 in *OVERFLOW if the division overflows. */
2722 template <typename T1
, typename T2
>
2723 inline WI_BINARY_RESULT (T1
, T2
)
2724 wi::mod_ceil (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2726 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2727 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2728 unsigned int precision
= get_precision (quotient
);
2729 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2730 WIDE_INT_REF_FOR (T2
) yi (y
);
2732 unsigned int remainder_len
;
2733 quotient
.set_len (divmod_internal (quotient_val
,
2734 &remainder_len
, remainder_val
,
2735 xi
.val
, xi
.len
, precision
,
2736 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2738 remainder
.set_len (remainder_len
);
2740 if (wi::neg_p (x
, sgn
) == wi::neg_p (y
, sgn
) && remainder
!= 0)
2741 return remainder
- y
;
2745 /* Compute X / Y, rouding towards nearest with ties away from zero,
2746 and return the remainder. Treat X and Y as having the signedness
2747 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2748 template <typename T1
, typename T2
>
2749 inline WI_BINARY_RESULT (T1
, T2
)
2750 wi::mod_round (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2752 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2753 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2754 unsigned int precision
= get_precision (quotient
);
2755 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2756 WIDE_INT_REF_FOR (T2
) yi (y
);
2758 unsigned int remainder_len
;
2759 quotient
.set_len (divmod_internal (quotient_val
,
2760 &remainder_len
, remainder_val
,
2761 xi
.val
, xi
.len
, precision
,
2762 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2764 remainder
.set_len (remainder_len
);
2770 if (wi::ges_p (wi::abs (remainder
),
2771 wi::lrshift (wi::abs (y
), 1)))
2773 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
))
2774 return remainder
+ y
;
2776 return remainder
- y
;
2781 if (wi::geu_p (remainder
, wi::lrshift (y
, 1)))
2782 return remainder
- y
;
2788 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2789 Treat X and Y as having the signedness given by SGN. */
2790 template <typename T1
, typename T2
>
2792 wi::multiple_of_p (const T1
&x
, const T2
&y
, signop sgn
,
2793 WI_BINARY_RESULT (T1
, T2
) *res
)
2795 WI_BINARY_RESULT (T1
, T2
) remainder
;
2796 WI_BINARY_RESULT (T1
, T2
) quotient
2797 = divmod_trunc (x
, y
, sgn
, &remainder
);
2806 /* Return X << Y. Return 0 if Y is greater than or equal to
2807 the precision of X. */
2808 template <typename T1
, typename T2
>
2809 inline WI_UNARY_RESULT (T1
)
2810 wi::lshift (const T1
&x
, const T2
&y
)
2812 WI_UNARY_RESULT_VAR (result
, val
, T1
, x
);
2813 unsigned int precision
= get_precision (result
);
2814 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2815 WIDE_INT_REF_FOR (T2
) yi (y
);
2816 /* Handle the simple cases quickly. */
2817 if (geu_p (yi
, precision
))
2824 unsigned int shift
= yi
.to_uhwi ();
2825 /* For fixed-precision integers like offset_int and widest_int,
2826 handle the case where the shift value is constant and the
2827 result is a single nonnegative HWI (meaning that we don't
2828 need to worry about val[1]). This is particularly common
2829 for converting a byte count to a bit count.
2831 For variable-precision integers like wide_int, handle HWI
2832 and sub-HWI integers inline. */
2833 if (STATIC_CONSTANT_P (xi
.precision
> HOST_BITS_PER_WIDE_INT
)
2834 ? (STATIC_CONSTANT_P (shift
< HOST_BITS_PER_WIDE_INT
- 1)
2836 && xi
.val
[0] <= (HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
)
2837 HOST_WIDE_INT_MAX
>> shift
))
2838 : precision
<= HOST_BITS_PER_WIDE_INT
)
2840 val
[0] = xi
.ulow () << shift
;
2844 result
.set_len (lshift_large (val
, xi
.val
, xi
.len
,
2850 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2851 or equal to the precision of X. */
2852 template <typename T1
, typename T2
>
2853 inline WI_UNARY_RESULT (T1
)
2854 wi::lrshift (const T1
&x
, const T2
&y
)
2856 WI_UNARY_RESULT_VAR (result
, val
, T1
, x
);
2857 /* Do things in the precision of the input rather than the output,
2858 since the result can be no larger than that. */
2859 WIDE_INT_REF_FOR (T1
) xi (x
);
2860 WIDE_INT_REF_FOR (T2
) yi (y
);
2861 /* Handle the simple cases quickly. */
2862 if (geu_p (yi
, xi
.precision
))
2869 unsigned int shift
= yi
.to_uhwi ();
2870 /* For fixed-precision integers like offset_int and widest_int,
2871 handle the case where the shift value is constant and the
2872 shifted value is a single nonnegative HWI (meaning that all
2873 bits above the HWI are zero). This is particularly common
2874 for converting a bit count to a byte count.
2876 For variable-precision integers like wide_int, handle HWI
2877 and sub-HWI integers inline. */
2878 if (STATIC_CONSTANT_P (xi
.precision
> HOST_BITS_PER_WIDE_INT
)
2879 ? xi
.len
== 1 && xi
.val
[0] >= 0
2880 : xi
.precision
<= HOST_BITS_PER_WIDE_INT
)
2882 val
[0] = xi
.to_uhwi () >> shift
;
2886 result
.set_len (lrshift_large (val
, xi
.val
, xi
.len
, xi
.precision
,
2887 get_precision (result
), shift
));
2892 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2893 Y is greater than or equal to the precision of X. */
2894 template <typename T1
, typename T2
>
2895 inline WI_UNARY_RESULT (T1
)
2896 wi::arshift (const T1
&x
, const T2
&y
)
2898 WI_UNARY_RESULT_VAR (result
, val
, T1
, x
);
2899 /* Do things in the precision of the input rather than the output,
2900 since the result can be no larger than that. */
2901 WIDE_INT_REF_FOR (T1
) xi (x
);
2902 WIDE_INT_REF_FOR (T2
) yi (y
);
2903 /* Handle the simple cases quickly. */
2904 if (geu_p (yi
, xi
.precision
))
2906 val
[0] = sign_mask (x
);
2911 unsigned int shift
= yi
.to_uhwi ();
2912 if (xi
.precision
<= HOST_BITS_PER_WIDE_INT
)
2914 val
[0] = sext_hwi (xi
.ulow () >> shift
, xi
.precision
- shift
);
2915 result
.set_len (1, true);
2918 result
.set_len (arshift_large (val
, xi
.val
, xi
.len
, xi
.precision
,
2919 get_precision (result
), shift
));
2924 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2925 logical shift otherwise. If BITSIZE is nonzero, only use the low
2926 BITSIZE bits of Y. */
2927 template <typename T1
, typename T2
>
2928 inline WI_UNARY_RESULT (T1
)
2929 wi::rshift (const T1
&x
, const T2
&y
, signop sgn
)
2931 if (sgn
== UNSIGNED
)
2932 return lrshift (x
, y
);
2934 return arshift (x
, y
);
2937 /* Return the result of rotating the low WIDTH bits of X left by Y
2938 bits and zero-extending the result. Use a full-width rotate if
2940 template <typename T1
, typename T2
>
2941 WI_UNARY_RESULT (T1
)
2942 wi::lrotate (const T1
&x
, const T2
&y
, unsigned int width
)
2944 unsigned int precision
= get_binary_precision (x
, x
);
2947 WI_UNARY_RESULT (T2
) ymod
= umod_trunc (y
, width
);
2948 WI_UNARY_RESULT (T1
) left
= wi::lshift (x
, ymod
);
2949 WI_UNARY_RESULT (T1
) right
= wi::lrshift (x
, wi::sub (width
, ymod
));
2950 if (width
!= precision
)
2951 return wi::zext (left
, width
) | wi::zext (right
, width
);
2952 return left
| right
;
2955 /* Return the result of rotating the low WIDTH bits of X right by Y
2956 bits and zero-extending the result. Use a full-width rotate if
2958 template <typename T1
, typename T2
>
2959 WI_UNARY_RESULT (T1
)
2960 wi::rrotate (const T1
&x
, const T2
&y
, unsigned int width
)
2962 unsigned int precision
= get_binary_precision (x
, x
);
2965 WI_UNARY_RESULT (T2
) ymod
= umod_trunc (y
, width
);
2966 WI_UNARY_RESULT (T1
) right
= wi::lrshift (x
, ymod
);
2967 WI_UNARY_RESULT (T1
) left
= wi::lshift (x
, wi::sub (width
, ymod
));
2968 if (width
!= precision
)
2969 return wi::zext (left
, width
) | wi::zext (right
, width
);
2970 return left
| right
;
2973 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
2976 wi::parity (const wide_int_ref
&x
)
2978 return popcount (x
) & 1;
2981 /* Extract WIDTH bits from X, starting at BITPOS. */
2982 template <typename T
>
2983 inline unsigned HOST_WIDE_INT
2984 wi::extract_uhwi (const T
&x
, unsigned int bitpos
,
2987 unsigned precision
= get_precision (x
);
2988 if (precision
< bitpos
+ width
)
2989 precision
= bitpos
+ width
;
2990 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
2992 /* Handle this rare case after the above, so that we assert about
2993 bogus BITPOS values. */
2997 unsigned int start
= bitpos
/ HOST_BITS_PER_WIDE_INT
;
2998 unsigned int shift
= bitpos
% HOST_BITS_PER_WIDE_INT
;
2999 unsigned HOST_WIDE_INT res
= xi
.elt (start
);
3001 if (shift
+ width
> HOST_BITS_PER_WIDE_INT
)
3003 unsigned HOST_WIDE_INT upper
= xi
.elt (start
+ 1);
3004 res
|= upper
<< (-shift
% HOST_BITS_PER_WIDE_INT
);
3006 return zext_hwi (res
, width
);
3009 /* Return the minimum precision needed to store X with sign SGN. */
3010 template <typename T
>
3012 wi::min_precision (const T
&x
, signop sgn
)
3015 return get_precision (x
) - clrsb (x
);
3017 return get_precision (x
) - clz (x
);
3020 template<typename T
>
3022 gt_ggc_mx (generic_wide_int
<T
> *)
3026 template<typename T
>
3028 gt_pch_nx (generic_wide_int
<T
> *)
3032 template<typename T
>
3034 gt_pch_nx (generic_wide_int
<T
> *, void (*) (void *, void *), void *)
3040 gt_ggc_mx (trailing_wide_ints
<N
> *)
3046 gt_pch_nx (trailing_wide_ints
<N
> *)
3052 gt_pch_nx (trailing_wide_ints
<N
> *, void (*) (void *, void *), void *)
3058 /* Used for overloaded functions in which the only other acceptable
3059 scalar type is a pointer. It stops a plain 0 from being treated
3060 as a null pointer. */
3061 struct never_used1
{};
3062 struct never_used2
{};
3064 wide_int
min_value (unsigned int, signop
);
3065 wide_int
min_value (never_used1
*);
3066 wide_int
min_value (never_used2
*);
3067 wide_int
max_value (unsigned int, signop
);
3068 wide_int
max_value (never_used1
*);
3069 wide_int
max_value (never_used2
*);
3071 /* FIXME: this is target dependent, so should be elsewhere.
3072 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3073 wide_int
from_buffer (const unsigned char *, unsigned int);
3075 #ifndef GENERATOR_FILE
3076 void to_mpz (wide_int
, mpz_t
, signop
);
3079 wide_int
mask (unsigned int, bool, unsigned int);
3080 wide_int
shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3081 wide_int
set_bit_in_zero (unsigned int, unsigned int);
3082 wide_int
insert (const wide_int
&x
, const wide_int
&y
, unsigned int,
3085 template <typename T
>
3086 T
mask (unsigned int, bool);
3088 template <typename T
>
3089 T
shifted_mask (unsigned int, unsigned int, bool);
3091 template <typename T
>
3092 T
set_bit_in_zero (unsigned int);
3094 unsigned int mask (HOST_WIDE_INT
*, unsigned int, bool, unsigned int);
3095 unsigned int shifted_mask (HOST_WIDE_INT
*, unsigned int, unsigned int,
3096 bool, unsigned int);
3097 unsigned int from_array (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
3098 unsigned int, unsigned int, bool);
3101 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3102 and the other bits are clear, or the inverse if NEGATE_P. */
3104 wi::mask (unsigned int width
, bool negate_p
, unsigned int precision
)
3106 wide_int result
= wide_int::create (precision
);
3107 result
.set_len (mask (result
.write_val (), width
, negate_p
, precision
));
3111 /* Return a PRECISION-bit integer in which the low START bits are clear,
3112 the next WIDTH bits are set, and the other bits are clear,
3113 or the inverse if NEGATE_P. */
3115 wi::shifted_mask (unsigned int start
, unsigned int width
, bool negate_p
,
3116 unsigned int precision
)
3118 wide_int result
= wide_int::create (precision
);
3119 result
.set_len (shifted_mask (result
.write_val (), start
, width
, negate_p
,
3124 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3125 others are clear. */
3127 wi::set_bit_in_zero (unsigned int bit
, unsigned int precision
)
3129 return shifted_mask (bit
, 1, false, precision
);
3132 /* Return an integer of type T in which the low WIDTH bits are set
3133 and the other bits are clear, or the inverse if NEGATE_P. */
3134 template <typename T
>
3136 wi::mask (unsigned int width
, bool negate_p
)
3138 STATIC_ASSERT (wi::int_traits
<T
>::precision
);
3140 result
.set_len (mask (result
.write_val (), width
, negate_p
,
3141 wi::int_traits
<T
>::precision
));
3145 /* Return an integer of type T in which the low START bits are clear,
3146 the next WIDTH bits are set, and the other bits are clear, or the
3147 inverse if NEGATE_P. */
3148 template <typename T
>
3150 wi::shifted_mask (unsigned int start
, unsigned int width
, bool negate_p
)
3152 STATIC_ASSERT (wi::int_traits
<T
>::precision
);
3154 result
.set_len (shifted_mask (result
.write_val (), start
, width
,
3156 wi::int_traits
<T
>::precision
));
3160 /* Return an integer of type T in which bit BIT is set and all the
3161 others are clear. */
3162 template <typename T
>
3164 wi::set_bit_in_zero (unsigned int bit
)
3166 return shifted_mask
<T
> (bit
, 1, false);
3169 #endif /* WIDE_INT_H */