1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2018 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-precision integer that can hold
57 any address offset, measured in either bits or bytes, with at
58 least one extra sign bit. At the moment the maximum address
59 size GCC supports is 64 bits. With 8-bit bytes and an extra
60 sign bit, offset_int therefore needs to have at least 68 bits
61 of precision. We round this up to 128 bits for efficiency.
62 Values of type T are converted to this precision by sign- or
63 zero-extending them based on the signedness of T.
65 The extra sign bit means that offset_int is effectively a signed
66 128-bit integer, i.e. it behaves like int128_t.
68 Since the values are logically signed, there is no need to
69 distinguish between signed and unsigned operations. Sign-sensitive
70 comparison operators <, <=, > and >= are therefore supported.
71 Shift operators << and >> are also supported, with >> being
72 an _arithmetic_ right shift.
74 [ Note that, even though offset_int is effectively int128_t,
75 it can still be useful to use unsigned comparisons like
76 wi::leu_p (a, b) as a more efficient short-hand for
79 3) widest_int. This representation is an approximation of
80 infinite precision math. However, it is not really infinite
81 precision math as in the GMP library. It is really finite
82 precision math where the precision is 4 times the size of the
83 largest integer that the target port can represent.
85 Like offset_int, widest_int is wider than all the values that
86 it needs to represent, so the integers are logically signed.
87 Sign-sensitive comparison operators <, <=, > and >= are supported,
90 There are several places in the GCC where this should/must be used:
92 * Code that does induction variable optimizations. This code
93 works with induction variables of many different types at the
94 same time. Because of this, it ends up doing many different
95 calculations where the operands are not compatible types. The
96 widest_int makes this easy, because it provides a field where
97 nothing is lost when converting from any variable,
99 * There are a small number of passes that currently use the
100 widest_int that should use the default. These should be
103 There are surprising features of offset_int and widest_int
104 that the users should be careful about:
106 1) Shifts and rotations are just weird. You have to specify a
107 precision in which the shift or rotate is to happen in. The bits
108 above this precision are zeroed. While this is what you
109 want, it is clearly non obvious.
111 2) Larger precision math sometimes does not produce the same
112 answer as would be expected for doing the math at the proper
113 precision. In particular, a multiply followed by a divide will
114 produce a different answer if the first product is larger than
115 what can be represented in the input precision.
117 The offset_int and the widest_int flavors are more expensive
118 than the default wide int, so in addition to the caveats with these
119 two, the default is the prefered representation.
121 All three flavors of wide_int are represented as a vector of
122 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
123 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
124 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
125 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
128 The default wide_int contains three fields: the vector (VAL),
129 the precision and a length (LEN). The length is the number of HWIs
130 needed to represent the value. widest_int and offset_int have a
131 constant precision that cannot be changed, so they only store the
134 Since most integers used in a compiler are small values, it is
135 generally profitable to use a representation of the value that is
136 as small as possible. LEN is used to indicate the number of
137 elements of the vector that are in use. The numbers are stored as
138 sign extended numbers as a means of compression. Leading
139 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
140 as long as they can be reconstructed from the top bit that is being
143 The precision and length of a wide_int are always greater than 0.
144 Any bits in a wide_int above the precision are sign-extended from the
145 most significant bit. For example, a 4-bit value 0x8 is represented as
146 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
147 constants to be represented with undefined bits above the precision.
148 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
149 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
150 and in wider precisions.
152 There are constructors to create the various forms of wide_int from
153 trees, rtl and constants. For trees the options are:
156 wi::to_wide (t) // Treat T as a wide_int
157 wi::to_offset (t) // Treat T as an offset_int
158 wi::to_widest (t) // Treat T as a widest_int
160 All three are light-weight accessors that should have no overhead
161 in release builds. If it is useful for readability reasons to
162 store the result in a temporary variable, the preferred method is:
164 wi::tree_to_wide_ref twide = wi::to_wide (t);
165 wi::tree_to_offset_ref toffset = wi::to_offset (t);
166 wi::tree_to_widest_ref twidest = wi::to_widest (t);
168 To make an rtx into a wide_int, you have to pair it with a mode.
169 The canonical way to do this is with rtx_mode_t as in:
172 wide_int x = rtx_mode_t (r, mode);
174 Similarly, a wide_int can only be constructed from a host value if
175 the target precision is given explicitly, such as in:
177 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
178 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
180 However, offset_int and widest_int have an inherent precision and so
181 can be initialized directly from a host value:
183 offset_int x = (int) c; // sign-extend C
184 widest_int x = (unsigned int) c; // zero-extend C
186 It is also possible to do arithmetic directly on rtx_mode_ts and
187 constants. For example:
189 wi::add (r1, r2); // add equal-sized rtx_mode_ts r1 and r2
190 wi::add (r1, 1); // add 1 to rtx_mode_t r1
191 wi::lshift (1, 100); // 1 << 100 as a widest_int
193 Many binary operations place restrictions on the combinations of inputs,
194 using the following rules:
196 - {rtx, wide_int} op {rtx, wide_int} -> wide_int
197 The inputs must be the same precision. The result is a wide_int
198 of the same precision
200 - {rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
201 (un)signed HOST_WIDE_INT op {rtx, wide_int} -> wide_int
202 The HOST_WIDE_INT is extended or truncated to the precision of
203 the other input. The result is a wide_int of the same precision
206 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
207 The inputs are extended to widest_int precision and produce a
210 - offset_int op offset_int -> offset_int
211 offset_int op (un)signed HOST_WIDE_INT -> offset_int
212 (un)signed HOST_WIDE_INT op offset_int -> offset_int
214 - widest_int op widest_int -> widest_int
215 widest_int op (un)signed HOST_WIDE_INT -> widest_int
216 (un)signed HOST_WIDE_INT op widest_int -> widest_int
218 Other combinations like:
220 - widest_int op offset_int and
221 - wide_int op offset_int
223 are not allowed. The inputs should instead be extended or truncated
226 The inputs to comparison functions like wi::eq_p and wi::lts_p
227 follow the same compatibility rules, although their return types
228 are different. Unary functions on X produce the same result as
229 a binary operation X + X. Shift functions X op Y also produce
230 the same result as X + X; the precision of the shift amount Y
231 can be arbitrarily different from X. */
233 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
234 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
235 can accomodate at least 1 more bit so that unsigned numbers of that
236 mode can be represented as a signed value. Note that it is still
237 possible to create fixed_wide_ints that have precisions greater than
238 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
239 double-width multiplication result, for example. */
240 #define WIDE_INT_MAX_ELTS \
241 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
243 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
245 /* This is the max size of any pointer on any machine. It does not
246 seem to be as easy to sniff this out of the machine description as
247 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
248 multiple address sizes and may have different address sizes for
249 different address spaces. However, currently the largest pointer
250 on any platform is 64 bits. When that changes, then it is likely
251 that a target hook should be defined so that targets can make this
252 value larger for those targets. */
253 #define ADDR_MAX_BITSIZE 64
255 /* This is the internal precision used when doing any address
256 arithmetic. The '4' is really 3 + 1. Three of the bits are for
257 the number of extra bits needed to do bit addresses and the other bit
258 is to allow everything to be signed without loosing any precision.
259 Then everything is rounded up to the next HWI for efficiency. */
260 #define ADDR_MAX_PRECISION \
261 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
262 & ~(HOST_BITS_PER_WIDE_INT - 1))
264 /* The number of HWIs needed to store an offset_int. */
265 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
267 /* The type of result produced by a binary operation on types T1 and T2.
268 Defined purely for brevity. */
269 #define WI_BINARY_RESULT(T1, T2) \
270 typename wi::binary_traits <T1, T2>::result_type
272 /* Likewise for binary operators, which excludes the case in which neither
273 T1 nor T2 is a wide-int-based type. */
274 #define WI_BINARY_OPERATOR_RESULT(T1, T2) \
275 typename wi::binary_traits <T1, T2>::operator_result
277 /* The type of result produced by T1 << T2. Leads to substitution failure
278 if the operation isn't supported. Defined purely for brevity. */
279 #define WI_SIGNED_SHIFT_RESULT(T1, T2) \
280 typename wi::binary_traits <T1, T2>::signed_shift_result_type
282 /* The type of result produced by a sign-agnostic binary predicate on
283 types T1 and T2. This is bool if wide-int operations make sense for
284 T1 and T2 and leads to substitution failure otherwise. */
285 #define WI_BINARY_PREDICATE_RESULT(T1, T2) \
286 typename wi::binary_traits <T1, T2>::predicate_result
288 /* The type of result produced by a signed binary predicate on types T1 and T2.
289 This is bool if signed comparisons make sense for T1 and T2 and leads to
290 substitution failure otherwise. */
291 #define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2) \
292 typename wi::binary_traits <T1, T2>::signed_predicate_result
294 /* The type of result produced by a unary operation on type T. */
295 #define WI_UNARY_RESULT(T) \
296 typename wi::binary_traits <T, T>::result_type
298 /* Define a variable RESULT to hold the result of a binary operation on
299 X and Y, which have types T1 and T2 respectively. Define VAL to
300 point to the blocks of RESULT. Once the user of the macro has
301 filled in VAL, it should call RESULT.set_len to set the number
302 of initialized blocks. */
303 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
304 WI_BINARY_RESULT (T1, T2) RESULT = \
305 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
306 HOST_WIDE_INT *VAL = RESULT.write_val ()
308 /* Similar for the result of a unary operation on X, which has type T. */
309 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
310 WI_UNARY_RESULT (T) RESULT = \
311 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
312 HOST_WIDE_INT *VAL = RESULT.write_val ()
314 template <typename T
> class generic_wide_int
;
315 template <int N
> class fixed_wide_int_storage
;
316 class wide_int_storage
;
318 /* An N-bit integer. Until we can use typedef templates, use this instead. */
319 #define FIXED_WIDE_INT(N) \
320 generic_wide_int < fixed_wide_int_storage <N> >
322 typedef generic_wide_int
<wide_int_storage
> wide_int
;
323 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION
) offset_int
;
324 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
) widest_int
;
326 /* wi::storage_ref can be a reference to a primitive type,
327 so this is the conservatively-correct setting. */
328 template <bool SE
, bool HDP
= true>
329 struct wide_int_ref_storage
;
331 typedef generic_wide_int
<wide_int_ref_storage
<false> > wide_int_ref
;
333 /* This can be used instead of wide_int_ref if the referenced value is
334 known to have type T. It carries across properties of T's representation,
335 such as whether excess upper bits in a HWI are defined, and can therefore
336 help avoid redundant work.
338 The macro could be replaced with a template typedef, once we're able
340 #define WIDE_INT_REF_FOR(T) \
342 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
343 wi::int_traits <T>::host_dependent_precision> >
347 /* Classifies an integer based on its precision. */
348 enum precision_type
{
349 /* The integer has both a precision and defined signedness. This allows
350 the integer to be converted to any width, since we know whether to fill
351 any extra bits with zeros or signs. */
354 /* The integer has a variable precision but no defined signedness. */
357 /* The integer has a constant precision (known at GCC compile time)
362 /* This class, which has no default implementation, is expected to
363 provide the following members:
365 static const enum precision_type precision_type;
366 Classifies the type of T.
368 static const unsigned int precision;
369 Only defined if precision_type == CONST_PRECISION. Specifies the
370 precision of all integers of type T.
372 static const bool host_dependent_precision;
373 True if the precision of T depends (or can depend) on the host.
375 static unsigned int get_precision (const T &x)
376 Return the number of bits in X.
378 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
379 unsigned int precision, const T &x)
380 Decompose X as a PRECISION-bit integer, returning the associated
381 wi::storage_ref. SCRATCH is available as scratch space if needed.
382 The routine should assert that PRECISION is acceptable. */
383 template <typename T
> struct int_traits
;
385 /* This class provides a single type, result_type, which specifies the
386 type of integer produced by a binary operation whose inputs have
387 types T1 and T2. The definition should be symmetric. */
388 template <typename T1
, typename T2
,
389 enum precision_type P1
= int_traits
<T1
>::precision_type
,
390 enum precision_type P2
= int_traits
<T2
>::precision_type
>
391 struct binary_traits
;
393 /* Specify the result type for each supported combination of binary
394 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
395 mixed, in order to give stronger type checking. When both inputs
396 are CONST_PRECISION, they must have the same precision. */
397 template <typename T1
, typename T2
>
398 struct binary_traits
<T1
, T2
, FLEXIBLE_PRECISION
, FLEXIBLE_PRECISION
>
400 typedef widest_int result_type
;
401 /* Don't define operators for this combination. */
404 template <typename T1
, typename T2
>
405 struct binary_traits
<T1
, T2
, FLEXIBLE_PRECISION
, VAR_PRECISION
>
407 typedef wide_int result_type
;
408 typedef result_type operator_result
;
409 typedef bool predicate_result
;
412 template <typename T1
, typename T2
>
413 struct binary_traits
<T1
, T2
, FLEXIBLE_PRECISION
, CONST_PRECISION
>
415 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
416 so as not to confuse gengtype. */
417 typedef generic_wide_int
< fixed_wide_int_storage
418 <int_traits
<T2
>::precision
> > result_type
;
419 typedef result_type operator_result
;
420 typedef bool predicate_result
;
421 typedef result_type signed_shift_result_type
;
422 typedef bool signed_predicate_result
;
425 template <typename T1
, typename T2
>
426 struct binary_traits
<T1
, T2
, VAR_PRECISION
, FLEXIBLE_PRECISION
>
428 typedef wide_int result_type
;
429 typedef result_type operator_result
;
430 typedef bool predicate_result
;
433 template <typename T1
, typename T2
>
434 struct binary_traits
<T1
, T2
, CONST_PRECISION
, FLEXIBLE_PRECISION
>
436 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
437 so as not to confuse gengtype. */
438 typedef generic_wide_int
< fixed_wide_int_storage
439 <int_traits
<T1
>::precision
> > result_type
;
440 typedef result_type operator_result
;
441 typedef bool predicate_result
;
442 typedef result_type signed_shift_result_type
;
443 typedef bool signed_predicate_result
;
446 template <typename T1
, typename T2
>
447 struct binary_traits
<T1
, T2
, CONST_PRECISION
, CONST_PRECISION
>
449 STATIC_ASSERT (int_traits
<T1
>::precision
== int_traits
<T2
>::precision
);
450 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
451 so as not to confuse gengtype. */
452 typedef generic_wide_int
< fixed_wide_int_storage
453 <int_traits
<T1
>::precision
> > result_type
;
454 typedef result_type operator_result
;
455 typedef bool predicate_result
;
456 typedef result_type signed_shift_result_type
;
457 typedef bool signed_predicate_result
;
460 template <typename T1
, typename T2
>
461 struct binary_traits
<T1
, T2
, VAR_PRECISION
, VAR_PRECISION
>
463 typedef wide_int result_type
;
464 typedef result_type operator_result
;
465 typedef bool predicate_result
;
469 /* Public functions for querying and operating on integers. */
472 template <typename T
>
473 unsigned int get_precision (const T
&);
475 template <typename T1
, typename T2
>
476 unsigned int get_binary_precision (const T1
&, const T2
&);
478 template <typename T1
, typename T2
>
479 void copy (T1
&, const T2
&);
481 #define UNARY_PREDICATE \
482 template <typename T> bool
483 #define UNARY_FUNCTION \
484 template <typename T> WI_UNARY_RESULT (T)
485 #define BINARY_PREDICATE \
486 template <typename T1, typename T2> bool
487 #define BINARY_FUNCTION \
488 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
489 #define SHIFT_FUNCTION \
490 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
492 UNARY_PREDICATE
fits_shwi_p (const T
&);
493 UNARY_PREDICATE
fits_uhwi_p (const T
&);
494 UNARY_PREDICATE
neg_p (const T
&, signop
= SIGNED
);
496 template <typename T
>
497 HOST_WIDE_INT
sign_mask (const T
&);
499 BINARY_PREDICATE
eq_p (const T1
&, const T2
&);
500 BINARY_PREDICATE
ne_p (const T1
&, const T2
&);
501 BINARY_PREDICATE
lt_p (const T1
&, const T2
&, signop
);
502 BINARY_PREDICATE
lts_p (const T1
&, const T2
&);
503 BINARY_PREDICATE
ltu_p (const T1
&, const T2
&);
504 BINARY_PREDICATE
le_p (const T1
&, const T2
&, signop
);
505 BINARY_PREDICATE
les_p (const T1
&, const T2
&);
506 BINARY_PREDICATE
leu_p (const T1
&, const T2
&);
507 BINARY_PREDICATE
gt_p (const T1
&, const T2
&, signop
);
508 BINARY_PREDICATE
gts_p (const T1
&, const T2
&);
509 BINARY_PREDICATE
gtu_p (const T1
&, const T2
&);
510 BINARY_PREDICATE
ge_p (const T1
&, const T2
&, signop
);
511 BINARY_PREDICATE
ges_p (const T1
&, const T2
&);
512 BINARY_PREDICATE
geu_p (const T1
&, const T2
&);
514 template <typename T1
, typename T2
>
515 int cmp (const T1
&, const T2
&, signop
);
517 template <typename T1
, typename T2
>
518 int cmps (const T1
&, const T2
&);
520 template <typename T1
, typename T2
>
521 int cmpu (const T1
&, const T2
&);
523 UNARY_FUNCTION
bit_not (const T
&);
524 UNARY_FUNCTION
neg (const T
&);
525 UNARY_FUNCTION
neg (const T
&, bool *);
526 UNARY_FUNCTION
abs (const T
&);
527 UNARY_FUNCTION
ext (const T
&, unsigned int, signop
);
528 UNARY_FUNCTION
sext (const T
&, unsigned int);
529 UNARY_FUNCTION
zext (const T
&, unsigned int);
530 UNARY_FUNCTION
set_bit (const T
&, unsigned int);
532 BINARY_FUNCTION
min (const T1
&, const T2
&, signop
);
533 BINARY_FUNCTION
smin (const T1
&, const T2
&);
534 BINARY_FUNCTION
umin (const T1
&, const T2
&);
535 BINARY_FUNCTION
max (const T1
&, const T2
&, signop
);
536 BINARY_FUNCTION
smax (const T1
&, const T2
&);
537 BINARY_FUNCTION
umax (const T1
&, const T2
&);
539 BINARY_FUNCTION
bit_and (const T1
&, const T2
&);
540 BINARY_FUNCTION
bit_and_not (const T1
&, const T2
&);
541 BINARY_FUNCTION
bit_or (const T1
&, const T2
&);
542 BINARY_FUNCTION
bit_or_not (const T1
&, const T2
&);
543 BINARY_FUNCTION
bit_xor (const T1
&, const T2
&);
544 BINARY_FUNCTION
add (const T1
&, const T2
&);
545 BINARY_FUNCTION
add (const T1
&, const T2
&, signop
, bool *);
546 BINARY_FUNCTION
sub (const T1
&, const T2
&);
547 BINARY_FUNCTION
sub (const T1
&, const T2
&, signop
, bool *);
548 BINARY_FUNCTION
mul (const T1
&, const T2
&);
549 BINARY_FUNCTION
mul (const T1
&, const T2
&, signop
, bool *);
550 BINARY_FUNCTION
smul (const T1
&, const T2
&, bool *);
551 BINARY_FUNCTION
umul (const T1
&, const T2
&, bool *);
552 BINARY_FUNCTION
mul_high (const T1
&, const T2
&, signop
);
553 BINARY_FUNCTION
div_trunc (const T1
&, const T2
&, signop
, bool * = 0);
554 BINARY_FUNCTION
sdiv_trunc (const T1
&, const T2
&);
555 BINARY_FUNCTION
udiv_trunc (const T1
&, const T2
&);
556 BINARY_FUNCTION
div_floor (const T1
&, const T2
&, signop
, bool * = 0);
557 BINARY_FUNCTION
udiv_floor (const T1
&, const T2
&);
558 BINARY_FUNCTION
sdiv_floor (const T1
&, const T2
&);
559 BINARY_FUNCTION
div_ceil (const T1
&, const T2
&, signop
, bool * = 0);
560 BINARY_FUNCTION
div_round (const T1
&, const T2
&, signop
, bool * = 0);
561 BINARY_FUNCTION
divmod_trunc (const T1
&, const T2
&, signop
,
562 WI_BINARY_RESULT (T1
, T2
) *);
563 BINARY_FUNCTION
gcd (const T1
&, const T2
&, signop
= UNSIGNED
);
564 BINARY_FUNCTION
mod_trunc (const T1
&, const T2
&, signop
, bool * = 0);
565 BINARY_FUNCTION
smod_trunc (const T1
&, const T2
&);
566 BINARY_FUNCTION
umod_trunc (const T1
&, const T2
&);
567 BINARY_FUNCTION
mod_floor (const T1
&, const T2
&, signop
, bool * = 0);
568 BINARY_FUNCTION
umod_floor (const T1
&, const T2
&);
569 BINARY_FUNCTION
mod_ceil (const T1
&, const T2
&, signop
, bool * = 0);
570 BINARY_FUNCTION
mod_round (const T1
&, const T2
&, signop
, bool * = 0);
572 template <typename T1
, typename T2
>
573 bool multiple_of_p (const T1
&, const T2
&, signop
);
575 template <typename T1
, typename T2
>
576 bool multiple_of_p (const T1
&, const T2
&, signop
,
577 WI_BINARY_RESULT (T1
, T2
) *);
579 SHIFT_FUNCTION
lshift (const T1
&, const T2
&);
580 SHIFT_FUNCTION
lrshift (const T1
&, const T2
&);
581 SHIFT_FUNCTION
arshift (const T1
&, const T2
&);
582 SHIFT_FUNCTION
rshift (const T1
&, const T2
&, signop sgn
);
583 SHIFT_FUNCTION
lrotate (const T1
&, const T2
&, unsigned int = 0);
584 SHIFT_FUNCTION
rrotate (const T1
&, const T2
&, unsigned int = 0);
586 #undef SHIFT_FUNCTION
587 #undef BINARY_PREDICATE
588 #undef BINARY_FUNCTION
589 #undef UNARY_PREDICATE
590 #undef UNARY_FUNCTION
592 bool only_sign_bit_p (const wide_int_ref
&, unsigned int);
593 bool only_sign_bit_p (const wide_int_ref
&);
594 int clz (const wide_int_ref
&);
595 int clrsb (const wide_int_ref
&);
596 int ctz (const wide_int_ref
&);
597 int exact_log2 (const wide_int_ref
&);
598 int floor_log2 (const wide_int_ref
&);
599 int ffs (const wide_int_ref
&);
600 int popcount (const wide_int_ref
&);
601 int parity (const wide_int_ref
&);
603 template <typename T
>
604 unsigned HOST_WIDE_INT
extract_uhwi (const T
&, unsigned int, unsigned int);
606 template <typename T
>
607 unsigned int min_precision (const T
&, signop
);
612 /* Contains the components of a decomposed integer for easy, direct
617 storage_ref (const HOST_WIDE_INT
*, unsigned int, unsigned int);
619 const HOST_WIDE_INT
*val
;
621 unsigned int precision
;
623 /* Provide enough trappings for this class to act as storage for
625 unsigned int get_len () const;
626 unsigned int get_precision () const;
627 const HOST_WIDE_INT
*get_val () const;
631 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT
*val_in
,
633 unsigned int precision_in
)
634 : val (val_in
), len (len_in
), precision (precision_in
)
639 wi::storage_ref::get_len () const
645 wi::storage_ref::get_precision () const
650 inline const HOST_WIDE_INT
*
651 wi::storage_ref::get_val () const
656 /* This class defines an integer type using the storage provided by the
657 template argument. The storage class must provide the following
660 unsigned int get_precision () const
661 Return the number of bits in the integer.
663 HOST_WIDE_INT *get_val () const
664 Return a pointer to the array of blocks that encodes the integer.
666 unsigned int get_len () const
667 Return the number of blocks in get_val (). If this is smaller
668 than the number of blocks implied by get_precision (), the
669 remaining blocks are sign extensions of block get_len () - 1.
671 Although not required by generic_wide_int itself, writable storage
672 classes can also provide the following functions:
674 HOST_WIDE_INT *write_val ()
675 Get a modifiable version of get_val ()
677 unsigned int set_len (unsigned int len)
678 Set the value returned by get_len () to LEN. */
679 template <typename storage
>
680 class GTY(()) generic_wide_int
: public storage
685 template <typename T
>
686 generic_wide_int (const T
&);
688 template <typename T
>
689 generic_wide_int (const T
&, unsigned int);
692 HOST_WIDE_INT
to_shwi (unsigned int) const;
693 HOST_WIDE_INT
to_shwi () const;
694 unsigned HOST_WIDE_INT
to_uhwi (unsigned int) const;
695 unsigned HOST_WIDE_INT
to_uhwi () const;
696 HOST_WIDE_INT
to_short_addr () const;
698 /* Public accessors for the interior of a wide int. */
699 HOST_WIDE_INT
sign_mask () const;
700 HOST_WIDE_INT
elt (unsigned int) const;
701 unsigned HOST_WIDE_INT
ulow () const;
702 unsigned HOST_WIDE_INT
uhigh () const;
703 HOST_WIDE_INT
slow () const;
704 HOST_WIDE_INT
shigh () const;
706 template <typename T
>
707 generic_wide_int
&operator = (const T
&);
709 #define ASSIGNMENT_OPERATOR(OP, F) \
710 template <typename T> \
711 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
713 /* Restrict these to cases where the shift operator is defined. */
714 #define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
715 template <typename T> \
716 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
718 #define INCDEC_OPERATOR(OP, DELTA) \
719 generic_wide_int &OP () { *this += DELTA; return *this; }
721 ASSIGNMENT_OPERATOR (operator &=, bit_and
)
722 ASSIGNMENT_OPERATOR (operator |=, bit_or
)
723 ASSIGNMENT_OPERATOR (operator ^=, bit_xor
)
724 ASSIGNMENT_OPERATOR (operator +=, add
)
725 ASSIGNMENT_OPERATOR (operator -=, sub
)
726 ASSIGNMENT_OPERATOR (operator *=, mul
)
727 ASSIGNMENT_OPERATOR (operator <<=, lshift
)
728 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
729 INCDEC_OPERATOR (operator ++, 1)
730 INCDEC_OPERATOR (operator --, -1)
732 #undef SHIFT_ASSIGNMENT_OPERATOR
733 #undef ASSIGNMENT_OPERATOR
734 #undef INCDEC_OPERATOR
736 /* Debugging functions. */
739 static const bool is_sign_extended
740 = wi::int_traits
<generic_wide_int
<storage
> >::is_sign_extended
;
743 template <typename storage
>
744 inline generic_wide_int
<storage
>::generic_wide_int () {}
746 template <typename storage
>
747 template <typename T
>
748 inline generic_wide_int
<storage
>::generic_wide_int (const T
&x
)
753 template <typename storage
>
754 template <typename T
>
755 inline generic_wide_int
<storage
>::generic_wide_int (const T
&x
,
756 unsigned int precision
)
757 : storage (x
, precision
)
761 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
762 If THIS does not fit in PRECISION, the information is lost. */
763 template <typename storage
>
765 generic_wide_int
<storage
>::to_shwi (unsigned int precision
) const
767 if (precision
< HOST_BITS_PER_WIDE_INT
)
768 return sext_hwi (this->get_val ()[0], precision
);
770 return this->get_val ()[0];
773 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
774 template <typename storage
>
776 generic_wide_int
<storage
>::to_shwi () const
778 if (is_sign_extended
)
779 return this->get_val ()[0];
781 return to_shwi (this->get_precision ());
784 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
785 PRECISION. If THIS does not fit in PRECISION, the information
787 template <typename storage
>
788 inline unsigned HOST_WIDE_INT
789 generic_wide_int
<storage
>::to_uhwi (unsigned int precision
) const
791 if (precision
< HOST_BITS_PER_WIDE_INT
)
792 return zext_hwi (this->get_val ()[0], precision
);
794 return this->get_val ()[0];
797 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
798 template <typename storage
>
799 inline unsigned HOST_WIDE_INT
800 generic_wide_int
<storage
>::to_uhwi () const
802 return to_uhwi (this->get_precision ());
805 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
806 represent addresses to using offset_int to represent addresses.
807 We use to_short_addr at the interface from new code to old,
809 template <typename storage
>
811 generic_wide_int
<storage
>::to_short_addr () const
813 return this->get_val ()[0];
816 /* Return the implicit value of blocks above get_len (). */
817 template <typename storage
>
819 generic_wide_int
<storage
>::sign_mask () const
821 unsigned int len
= this->get_len ();
822 unsigned HOST_WIDE_INT high
= this->get_val ()[len
- 1];
823 if (!is_sign_extended
)
825 unsigned int precision
= this->get_precision ();
826 int excess
= len
* HOST_BITS_PER_WIDE_INT
- precision
;
830 return (HOST_WIDE_INT
) (high
) < 0 ? -1 : 0;
833 /* Return the signed value of the least-significant explicitly-encoded
835 template <typename storage
>
837 generic_wide_int
<storage
>::slow () const
839 return this->get_val ()[0];
842 /* Return the signed value of the most-significant explicitly-encoded
844 template <typename storage
>
846 generic_wide_int
<storage
>::shigh () const
848 return this->get_val ()[this->get_len () - 1];
851 /* Return the unsigned value of the least-significant
852 explicitly-encoded block. */
853 template <typename storage
>
854 inline unsigned HOST_WIDE_INT
855 generic_wide_int
<storage
>::ulow () const
857 return this->get_val ()[0];
860 /* Return the unsigned value of the most-significant
861 explicitly-encoded block. */
862 template <typename storage
>
863 inline unsigned HOST_WIDE_INT
864 generic_wide_int
<storage
>::uhigh () const
866 return this->get_val ()[this->get_len () - 1];
869 /* Return block I, which might be implicitly or explicit encoded. */
870 template <typename storage
>
872 generic_wide_int
<storage
>::elt (unsigned int i
) const
874 if (i
>= this->get_len ())
877 return this->get_val ()[i
];
880 template <typename storage
>
881 template <typename T
>
882 inline generic_wide_int
<storage
> &
883 generic_wide_int
<storage
>::operator = (const T
&x
)
885 storage::operator = (x
);
889 /* Dump the contents of the integer to stderr, for debugging. */
890 template <typename storage
>
892 generic_wide_int
<storage
>::dump () const
894 unsigned int len
= this->get_len ();
895 const HOST_WIDE_INT
*val
= this->get_val ();
896 unsigned int precision
= this->get_precision ();
897 fprintf (stderr
, "[");
898 if (len
* HOST_BITS_PER_WIDE_INT
< precision
)
899 fprintf (stderr
, "...,");
900 for (unsigned int i
= 0; i
< len
- 1; ++i
)
901 fprintf (stderr
, HOST_WIDE_INT_PRINT_HEX
",", val
[len
- 1 - i
]);
902 fprintf (stderr
, HOST_WIDE_INT_PRINT_HEX
"], precision = %d\n",
908 template <typename storage
>
909 struct int_traits
< generic_wide_int
<storage
> >
910 : public wi::int_traits
<storage
>
912 static unsigned int get_precision (const generic_wide_int
<storage
> &);
913 static wi::storage_ref
decompose (HOST_WIDE_INT
*, unsigned int,
914 const generic_wide_int
<storage
> &);
918 template <typename storage
>
920 wi::int_traits
< generic_wide_int
<storage
> >::
921 get_precision (const generic_wide_int
<storage
> &x
)
923 return x
.get_precision ();
926 template <typename storage
>
927 inline wi::storage_ref
928 wi::int_traits
< generic_wide_int
<storage
> >::
929 decompose (HOST_WIDE_INT
*, unsigned int precision
,
930 const generic_wide_int
<storage
> &x
)
932 gcc_checking_assert (precision
== x
.get_precision ());
933 return wi::storage_ref (x
.get_val (), x
.get_len (), precision
);
936 /* Provide the storage for a wide_int_ref. This acts like a read-only
937 wide_int, with the optimization that VAL is normally a pointer to
938 another integer's storage, so that no array copy is needed. */
939 template <bool SE
, bool HDP
>
940 struct wide_int_ref_storage
: public wi::storage_ref
943 /* Scratch space that can be used when decomposing the original integer.
944 It must live as long as this object. */
945 HOST_WIDE_INT scratch
[2];
948 wide_int_ref_storage () {}
950 wide_int_ref_storage (const wi::storage_ref
&);
952 template <typename T
>
953 wide_int_ref_storage (const T
&);
955 template <typename T
>
956 wide_int_ref_storage (const T
&, unsigned int);
959 /* Create a reference from an existing reference. */
960 template <bool SE
, bool HDP
>
961 inline wide_int_ref_storage
<SE
, HDP
>::
962 wide_int_ref_storage (const wi::storage_ref
&x
)
966 /* Create a reference to integer X in its natural precision. Note
967 that the natural precision is host-dependent for primitive
969 template <bool SE
, bool HDP
>
970 template <typename T
>
971 inline wide_int_ref_storage
<SE
, HDP
>::wide_int_ref_storage (const T
&x
)
972 : storage_ref (wi::int_traits
<T
>::decompose (scratch
,
973 wi::get_precision (x
), x
))
977 /* Create a reference to integer X in precision PRECISION. */
978 template <bool SE
, bool HDP
>
979 template <typename T
>
980 inline wide_int_ref_storage
<SE
, HDP
>::
981 wide_int_ref_storage (const T
&x
, unsigned int precision
)
982 : storage_ref (wi::int_traits
<T
>::decompose (scratch
, precision
, x
))
988 template <bool SE
, bool HDP
>
989 struct int_traits
<wide_int_ref_storage
<SE
, HDP
> >
991 static const enum precision_type precision_type
= VAR_PRECISION
;
992 static const bool host_dependent_precision
= HDP
;
993 static const bool is_sign_extended
= SE
;
999 unsigned int force_to_size (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1000 unsigned int, unsigned int, unsigned int,
1002 unsigned int from_array (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1003 unsigned int, unsigned int, bool = true);
1006 /* The storage used by wide_int. */
1007 class GTY(()) wide_int_storage
1010 HOST_WIDE_INT val
[WIDE_INT_MAX_ELTS
];
1012 unsigned int precision
;
1015 wide_int_storage ();
1016 template <typename T
>
1017 wide_int_storage (const T
&);
1019 /* The standard generic_wide_int storage methods. */
1020 unsigned int get_precision () const;
1021 const HOST_WIDE_INT
*get_val () const;
1022 unsigned int get_len () const;
1023 HOST_WIDE_INT
*write_val ();
1024 void set_len (unsigned int, bool = false);
1026 template <typename T
>
1027 wide_int_storage
&operator = (const T
&);
1029 static wide_int
from (const wide_int_ref
&, unsigned int, signop
);
1030 static wide_int
from_array (const HOST_WIDE_INT
*, unsigned int,
1031 unsigned int, bool = true);
1032 static wide_int
create (unsigned int);
1034 /* FIXME: target-dependent, so should disappear. */
1035 wide_int
bswap () const;
1041 struct int_traits
<wide_int_storage
>
1043 static const enum precision_type precision_type
= VAR_PRECISION
;
1044 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1045 static const bool host_dependent_precision
= false;
1046 static const bool is_sign_extended
= true;
1047 template <typename T1
, typename T2
>
1048 static wide_int
get_binary_result (const T1
&, const T2
&);
1052 inline wide_int_storage::wide_int_storage () {}
1054 /* Initialize the storage from integer X, in its natural precision.
1055 Note that we do not allow integers with host-dependent precision
1056 to become wide_ints; wide_ints must always be logically independent
1058 template <typename T
>
1059 inline wide_int_storage::wide_int_storage (const T
&x
)
1061 { STATIC_ASSERT (!wi::int_traits
<T
>::host_dependent_precision
); }
1062 { STATIC_ASSERT (wi::int_traits
<T
>::precision_type
!= wi::CONST_PRECISION
); }
1063 WIDE_INT_REF_FOR (T
) xi (x
);
1064 precision
= xi
.precision
;
1065 wi::copy (*this, xi
);
1068 template <typename T
>
1069 inline wide_int_storage
&
1070 wide_int_storage::operator = (const T
&x
)
1072 { STATIC_ASSERT (!wi::int_traits
<T
>::host_dependent_precision
); }
1073 { STATIC_ASSERT (wi::int_traits
<T
>::precision_type
!= wi::CONST_PRECISION
); }
1074 WIDE_INT_REF_FOR (T
) xi (x
);
1075 precision
= xi
.precision
;
1076 wi::copy (*this, xi
);
1081 wide_int_storage::get_precision () const
1086 inline const HOST_WIDE_INT
*
1087 wide_int_storage::get_val () const
1093 wide_int_storage::get_len () const
1098 inline HOST_WIDE_INT
*
1099 wide_int_storage::write_val ()
1105 wide_int_storage::set_len (unsigned int l
, bool is_sign_extended
)
1108 if (!is_sign_extended
&& len
* HOST_BITS_PER_WIDE_INT
> precision
)
1109 val
[len
- 1] = sext_hwi (val
[len
- 1],
1110 precision
% HOST_BITS_PER_WIDE_INT
);
1113 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1116 wide_int_storage::from (const wide_int_ref
&x
, unsigned int precision
,
1119 wide_int result
= wide_int::create (precision
);
1120 result
.set_len (wi::force_to_size (result
.write_val (), x
.val
, x
.len
,
1121 x
.precision
, precision
, sgn
));
1125 /* Create a wide_int from the explicit block encoding given by VAL and
1126 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1127 true if the encoding may have redundant trailing blocks. */
1129 wide_int_storage::from_array (const HOST_WIDE_INT
*val
, unsigned int len
,
1130 unsigned int precision
, bool need_canon_p
)
1132 wide_int result
= wide_int::create (precision
);
1133 result
.set_len (wi::from_array (result
.write_val (), val
, len
, precision
,
1138 /* Return an uninitialized wide_int with precision PRECISION. */
1140 wide_int_storage::create (unsigned int precision
)
1143 x
.precision
= precision
;
1147 template <typename T1
, typename T2
>
1149 wi::int_traits
<wide_int_storage
>::get_binary_result (const T1
&x
, const T2
&y
)
1151 /* This shouldn't be used for two flexible-precision inputs. */
1152 STATIC_ASSERT (wi::int_traits
<T1
>::precision_type
!= FLEXIBLE_PRECISION
1153 || wi::int_traits
<T2
>::precision_type
!= FLEXIBLE_PRECISION
);
1154 if (wi::int_traits
<T1
>::precision_type
== FLEXIBLE_PRECISION
)
1155 return wide_int::create (wi::get_precision (y
));
1157 return wide_int::create (wi::get_precision (x
));
1160 /* The storage used by FIXED_WIDE_INT (N). */
1162 class GTY(()) fixed_wide_int_storage
1165 HOST_WIDE_INT val
[(N
+ HOST_BITS_PER_WIDE_INT
+ 1) / HOST_BITS_PER_WIDE_INT
];
1169 fixed_wide_int_storage ();
1170 template <typename T
>
1171 fixed_wide_int_storage (const T
&);
1173 /* The standard generic_wide_int storage methods. */
1174 unsigned int get_precision () const;
1175 const HOST_WIDE_INT
*get_val () const;
1176 unsigned int get_len () const;
1177 HOST_WIDE_INT
*write_val ();
1178 void set_len (unsigned int, bool = false);
1180 static FIXED_WIDE_INT (N
) from (const wide_int_ref
&, signop
);
1181 static FIXED_WIDE_INT (N
) from_array (const HOST_WIDE_INT
*, unsigned int,
1188 struct int_traits
< fixed_wide_int_storage
<N
> >
1190 static const enum precision_type precision_type
= CONST_PRECISION
;
1191 static const bool host_dependent_precision
= false;
1192 static const bool is_sign_extended
= true;
1193 static const unsigned int precision
= N
;
1194 template <typename T1
, typename T2
>
1195 static FIXED_WIDE_INT (N
) get_binary_result (const T1
&, const T2
&);
1200 inline fixed_wide_int_storage
<N
>::fixed_wide_int_storage () {}
1202 /* Initialize the storage from integer X, in precision N. */
1204 template <typename T
>
1205 inline fixed_wide_int_storage
<N
>::fixed_wide_int_storage (const T
&x
)
1207 /* Check for type compatibility. We don't want to initialize a
1208 fixed-width integer from something like a wide_int. */
1209 WI_BINARY_RESULT (T
, FIXED_WIDE_INT (N
)) *assertion ATTRIBUTE_UNUSED
;
1210 wi::copy (*this, WIDE_INT_REF_FOR (T
) (x
, N
));
1215 fixed_wide_int_storage
<N
>::get_precision () const
1221 inline const HOST_WIDE_INT
*
1222 fixed_wide_int_storage
<N
>::get_val () const
1229 fixed_wide_int_storage
<N
>::get_len () const
1235 inline HOST_WIDE_INT
*
1236 fixed_wide_int_storage
<N
>::write_val ()
1243 fixed_wide_int_storage
<N
>::set_len (unsigned int l
, bool)
1246 /* There are no excess bits in val[len - 1]. */
1247 STATIC_ASSERT (N
% HOST_BITS_PER_WIDE_INT
== 0);
1250 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1252 inline FIXED_WIDE_INT (N
)
1253 fixed_wide_int_storage
<N
>::from (const wide_int_ref
&x
, signop sgn
)
1255 FIXED_WIDE_INT (N
) result
;
1256 result
.set_len (wi::force_to_size (result
.write_val (), x
.val
, x
.len
,
1257 x
.precision
, N
, sgn
));
1261 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1262 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1265 inline FIXED_WIDE_INT (N
)
1266 fixed_wide_int_storage
<N
>::from_array (const HOST_WIDE_INT
*val
,
1270 FIXED_WIDE_INT (N
) result
;
1271 result
.set_len (wi::from_array (result
.write_val (), val
, len
,
1277 template <typename T1
, typename T2
>
1278 inline FIXED_WIDE_INT (N
)
1279 wi::int_traits
< fixed_wide_int_storage
<N
> >::
1280 get_binary_result (const T1
&, const T2
&)
1282 return FIXED_WIDE_INT (N
) ();
1285 /* A reference to one element of a trailing_wide_ints structure. */
1286 class trailing_wide_int_storage
1289 /* The precision of the integer, which is a fixed property of the
1290 parent trailing_wide_ints. */
1291 unsigned int m_precision
;
1293 /* A pointer to the length field. */
1294 unsigned char *m_len
;
1296 /* A pointer to the HWI array. There are enough elements to hold all
1297 values of precision M_PRECISION. */
1298 HOST_WIDE_INT
*m_val
;
1301 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT
*);
1303 /* The standard generic_wide_int storage methods. */
1304 unsigned int get_len () const;
1305 unsigned int get_precision () const;
1306 const HOST_WIDE_INT
*get_val () const;
1307 HOST_WIDE_INT
*write_val ();
1308 void set_len (unsigned int, bool = false);
1310 template <typename T
>
1311 trailing_wide_int_storage
&operator = (const T
&);
1314 typedef generic_wide_int
<trailing_wide_int_storage
> trailing_wide_int
;
1316 /* trailing_wide_int behaves like a wide_int. */
1320 struct int_traits
<trailing_wide_int_storage
>
1321 : public int_traits
<wide_int_storage
> {};
1324 /* An array of N wide_int-like objects that can be put at the end of
1325 a variable-sized structure. Use extra_size to calculate how many
1326 bytes beyond the sizeof need to be allocated. Use set_precision
1327 to initialize the structure. */
1329 class GTY((user
)) trailing_wide_ints
1332 /* The shared precision of each number. */
1333 unsigned short m_precision
;
1335 /* The shared maximum length of each number. */
1336 unsigned char m_max_len
;
1338 /* The current length of each number. */
1339 unsigned char m_len
[N
];
1341 /* The variable-length part of the structure, which always contains
1342 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1343 HOST_WIDE_INT m_val
[1];
1346 typedef WIDE_INT_REF_FOR (trailing_wide_int_storage
) const_reference
;
1348 void set_precision (unsigned int);
1349 unsigned int get_precision () const { return m_precision
; }
1350 trailing_wide_int
operator [] (unsigned int);
1351 const_reference
operator [] (unsigned int) const;
1352 static size_t extra_size (unsigned int);
1353 size_t extra_size () const { return extra_size (m_precision
); }
1356 inline trailing_wide_int_storage::
1357 trailing_wide_int_storage (unsigned int precision
, unsigned char *len
,
1359 : m_precision (precision
), m_len (len
), m_val (val
)
1364 trailing_wide_int_storage::get_len () const
1370 trailing_wide_int_storage::get_precision () const
1375 inline const HOST_WIDE_INT
*
1376 trailing_wide_int_storage::get_val () const
1381 inline HOST_WIDE_INT
*
1382 trailing_wide_int_storage::write_val ()
1388 trailing_wide_int_storage::set_len (unsigned int len
, bool is_sign_extended
)
1391 if (!is_sign_extended
&& len
* HOST_BITS_PER_WIDE_INT
> m_precision
)
1392 m_val
[len
- 1] = sext_hwi (m_val
[len
- 1],
1393 m_precision
% HOST_BITS_PER_WIDE_INT
);
1396 template <typename T
>
1397 inline trailing_wide_int_storage
&
1398 trailing_wide_int_storage::operator = (const T
&x
)
1400 WIDE_INT_REF_FOR (T
) xi (x
, m_precision
);
1401 wi::copy (*this, xi
);
1405 /* Initialize the structure and record that all elements have precision
1409 trailing_wide_ints
<N
>::set_precision (unsigned int precision
)
1411 m_precision
= precision
;
1412 m_max_len
= ((precision
+ HOST_BITS_PER_WIDE_INT
- 1)
1413 / HOST_BITS_PER_WIDE_INT
);
1416 /* Return a reference to element INDEX. */
1418 inline trailing_wide_int
1419 trailing_wide_ints
<N
>::operator [] (unsigned int index
)
1421 return trailing_wide_int_storage (m_precision
, &m_len
[index
],
1422 &m_val
[index
* m_max_len
]);
1426 inline typename trailing_wide_ints
<N
>::const_reference
1427 trailing_wide_ints
<N
>::operator [] (unsigned int index
) const
1429 return wi::storage_ref (&m_val
[index
* m_max_len
],
1430 m_len
[index
], m_precision
);
1433 /* Return how many extra bytes need to be added to the end of the structure
1434 in order to handle N wide_ints of precision PRECISION. */
1437 trailing_wide_ints
<N
>::extra_size (unsigned int precision
)
1439 unsigned int max_len
= ((precision
+ HOST_BITS_PER_WIDE_INT
- 1)
1440 / HOST_BITS_PER_WIDE_INT
);
1441 return (N
* max_len
- 1) * sizeof (HOST_WIDE_INT
);
1444 /* This macro is used in structures that end with a trailing_wide_ints field
1445 called FIELD. It declares get_NAME() and set_NAME() methods to access
1446 element I of FIELD. */
1447 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1448 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1449 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1453 /* Implementation of int_traits for primitive integer types like "int". */
1454 template <typename T
, bool signed_p
>
1455 struct primitive_int_traits
1457 static const enum precision_type precision_type
= FLEXIBLE_PRECISION
;
1458 static const bool host_dependent_precision
= true;
1459 static const bool is_sign_extended
= true;
1460 static unsigned int get_precision (T
);
1461 static wi::storage_ref
decompose (HOST_WIDE_INT
*, unsigned int, T
);
1465 template <typename T
, bool signed_p
>
1467 wi::primitive_int_traits
<T
, signed_p
>::get_precision (T
)
1469 return sizeof (T
) * CHAR_BIT
;
1472 template <typename T
, bool signed_p
>
1473 inline wi::storage_ref
1474 wi::primitive_int_traits
<T
, signed_p
>::decompose (HOST_WIDE_INT
*scratch
,
1475 unsigned int precision
, T x
)
1478 if (signed_p
|| scratch
[0] >= 0 || precision
<= HOST_BITS_PER_WIDE_INT
)
1479 return wi::storage_ref (scratch
, 1, precision
);
1481 return wi::storage_ref (scratch
, 2, precision
);
1484 /* Allow primitive C types to be used in wi:: routines. */
1488 struct int_traits
<unsigned char>
1489 : public primitive_int_traits
<unsigned char, false> {};
1492 struct int_traits
<unsigned short>
1493 : public primitive_int_traits
<unsigned short, false> {};
1496 struct int_traits
<int>
1497 : public primitive_int_traits
<int, true> {};
1500 struct int_traits
<unsigned int>
1501 : public primitive_int_traits
<unsigned int, false> {};
1504 struct int_traits
<long>
1505 : public primitive_int_traits
<long, true> {};
1508 struct int_traits
<unsigned long>
1509 : public primitive_int_traits
<unsigned long, false> {};
1511 #if defined HAVE_LONG_LONG
1513 struct int_traits
<long long>
1514 : public primitive_int_traits
<long long, true> {};
1517 struct int_traits
<unsigned long long>
1518 : public primitive_int_traits
<unsigned long long, false> {};
1524 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1525 and precision PRECISION. */
1526 struct hwi_with_prec
1529 hwi_with_prec (HOST_WIDE_INT
, unsigned int, signop
);
1531 unsigned int precision
;
1535 hwi_with_prec
shwi (HOST_WIDE_INT
, unsigned int);
1536 hwi_with_prec
uhwi (unsigned HOST_WIDE_INT
, unsigned int);
1538 hwi_with_prec
minus_one (unsigned int);
1539 hwi_with_prec
zero (unsigned int);
1540 hwi_with_prec
one (unsigned int);
1541 hwi_with_prec
two (unsigned int);
1544 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v
, unsigned int p
,
1546 : precision (p
), sgn (s
)
1548 if (precision
< HOST_BITS_PER_WIDE_INT
)
1549 val
= sext_hwi (v
, precision
);
1554 /* Return a signed integer that has value VAL and precision PRECISION. */
1555 inline wi::hwi_with_prec
1556 wi::shwi (HOST_WIDE_INT val
, unsigned int precision
)
1558 return hwi_with_prec (val
, precision
, SIGNED
);
1561 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1562 inline wi::hwi_with_prec
1563 wi::uhwi (unsigned HOST_WIDE_INT val
, unsigned int precision
)
1565 return hwi_with_prec (val
, precision
, UNSIGNED
);
1568 /* Return a wide int of -1 with precision PRECISION. */
1569 inline wi::hwi_with_prec
1570 wi::minus_one (unsigned int precision
)
1572 return wi::shwi (-1, precision
);
1575 /* Return a wide int of 0 with precision PRECISION. */
1576 inline wi::hwi_with_prec
1577 wi::zero (unsigned int precision
)
1579 return wi::shwi (0, precision
);
1582 /* Return a wide int of 1 with precision PRECISION. */
1583 inline wi::hwi_with_prec
1584 wi::one (unsigned int precision
)
1586 return wi::shwi (1, precision
);
1589 /* Return a wide int of 2 with precision PRECISION. */
1590 inline wi::hwi_with_prec
1591 wi::two (unsigned int precision
)
1593 return wi::shwi (2, precision
);
1598 /* ints_for<T>::zero (X) returns a zero that, when asssigned to a T,
1599 gives that T the same precision as X. */
1600 template<typename T
, precision_type
= int_traits
<T
>::precision_type
>
1603 static int zero (const T
&) { return 0; }
1606 template<typename T
>
1607 struct ints_for
<T
, VAR_PRECISION
>
1609 static hwi_with_prec
zero (const T
&);
1613 template<typename T
>
1614 inline wi::hwi_with_prec
1615 wi::ints_for
<T
, wi::VAR_PRECISION
>::zero (const T
&x
)
1617 return wi::zero (wi::get_precision (x
));
1623 struct int_traits
<wi::hwi_with_prec
>
1625 static const enum precision_type precision_type
= VAR_PRECISION
;
1626 /* hwi_with_prec has an explicitly-given precision, rather than the
1627 precision of HOST_WIDE_INT. */
1628 static const bool host_dependent_precision
= false;
1629 static const bool is_sign_extended
= true;
1630 static unsigned int get_precision (const wi::hwi_with_prec
&);
1631 static wi::storage_ref
decompose (HOST_WIDE_INT
*, unsigned int,
1632 const wi::hwi_with_prec
&);
1637 wi::int_traits
<wi::hwi_with_prec
>::get_precision (const wi::hwi_with_prec
&x
)
1642 inline wi::storage_ref
1643 wi::int_traits
<wi::hwi_with_prec
>::
1644 decompose (HOST_WIDE_INT
*scratch
, unsigned int precision
,
1645 const wi::hwi_with_prec
&x
)
1647 gcc_checking_assert (precision
== x
.precision
);
1649 if (x
.sgn
== SIGNED
|| x
.val
>= 0 || precision
<= HOST_BITS_PER_WIDE_INT
)
1650 return wi::storage_ref (scratch
, 1, precision
);
1652 return wi::storage_ref (scratch
, 2, precision
);
1655 /* Private functions for handling large cases out of line. They take
1656 individual length and array parameters because that is cheaper for
1657 the inline caller than constructing an object on the stack and
1658 passing a reference to it. (Although many callers use wide_int_refs,
1659 we generally want those to be removed by SRA.) */
1662 bool eq_p_large (const HOST_WIDE_INT
*, unsigned int,
1663 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1664 bool lts_p_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1665 const HOST_WIDE_INT
*, unsigned int);
1666 bool ltu_p_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1667 const HOST_WIDE_INT
*, unsigned int);
1668 int cmps_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1669 const HOST_WIDE_INT
*, unsigned int);
1670 int cmpu_large (const HOST_WIDE_INT
*, unsigned int, unsigned int,
1671 const HOST_WIDE_INT
*, unsigned int);
1672 unsigned int sext_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1674 unsigned int, unsigned int);
1675 unsigned int zext_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1677 unsigned int, unsigned int);
1678 unsigned int set_bit_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1679 unsigned int, unsigned int, unsigned int);
1680 unsigned int lshift_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1681 unsigned int, unsigned int, unsigned int);
1682 unsigned int lrshift_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1683 unsigned int, unsigned int, unsigned int,
1685 unsigned int arshift_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1686 unsigned int, unsigned int, unsigned int,
1688 unsigned int and_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1689 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1690 unsigned int and_not_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1691 unsigned int, const HOST_WIDE_INT
*,
1692 unsigned int, unsigned int);
1693 unsigned int or_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1694 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1695 unsigned int or_not_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1696 unsigned int, const HOST_WIDE_INT
*,
1697 unsigned int, unsigned int);
1698 unsigned int xor_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1699 const HOST_WIDE_INT
*, unsigned int, unsigned int);
1700 unsigned int add_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1701 const HOST_WIDE_INT
*, unsigned int, unsigned int,
1703 unsigned int sub_large (HOST_WIDE_INT
*, const HOST_WIDE_INT
*, unsigned int,
1704 const HOST_WIDE_INT
*, unsigned int, unsigned int,
1706 unsigned int mul_internal (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1707 unsigned int, const HOST_WIDE_INT
*,
1708 unsigned int, unsigned int, signop
, bool *,
1710 unsigned int divmod_internal (HOST_WIDE_INT
*, unsigned int *,
1711 HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
1712 unsigned int, unsigned int,
1713 const HOST_WIDE_INT
*,
1714 unsigned int, unsigned int,
1718 /* Return the number of bits that integer X can hold. */
1719 template <typename T
>
1721 wi::get_precision (const T
&x
)
1723 return wi::int_traits
<T
>::get_precision (x
);
1726 /* Return the number of bits that the result of a binary operation can
1727 hold when the input operands are X and Y. */
1728 template <typename T1
, typename T2
>
1730 wi::get_binary_precision (const T1
&x
, const T2
&y
)
1732 return get_precision (wi::int_traits
<WI_BINARY_RESULT (T1
, T2
)>::
1733 get_binary_result (x
, y
));
1736 /* Copy the contents of Y to X, but keeping X's current precision. */
1737 template <typename T1
, typename T2
>
1739 wi::copy (T1
&x
, const T2
&y
)
1741 HOST_WIDE_INT
*xval
= x
.write_val ();
1742 const HOST_WIDE_INT
*yval
= y
.get_val ();
1743 unsigned int len
= y
.get_len ();
1748 x
.set_len (len
, y
.is_sign_extended
);
1751 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1752 template <typename T
>
1754 wi::fits_shwi_p (const T
&x
)
1756 WIDE_INT_REF_FOR (T
) xi (x
);
1760 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1762 template <typename T
>
1764 wi::fits_uhwi_p (const T
&x
)
1766 WIDE_INT_REF_FOR (T
) xi (x
);
1767 if (xi
.precision
<= HOST_BITS_PER_WIDE_INT
)
1770 return xi
.slow () >= 0;
1771 return xi
.len
== 2 && xi
.uhigh () == 0;
1774 /* Return true if X is negative based on the interpretation of SGN.
1775 For UNSIGNED, this is always false. */
1776 template <typename T
>
1778 wi::neg_p (const T
&x
, signop sgn
)
1780 WIDE_INT_REF_FOR (T
) xi (x
);
1781 if (sgn
== UNSIGNED
)
1783 return xi
.sign_mask () < 0;
1786 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1787 template <typename T
>
1788 inline HOST_WIDE_INT
1789 wi::sign_mask (const T
&x
)
1791 WIDE_INT_REF_FOR (T
) xi (x
);
1792 return xi
.sign_mask ();
1795 /* Return true if X == Y. X and Y must be binary-compatible. */
1796 template <typename T1
, typename T2
>
1798 wi::eq_p (const T1
&x
, const T2
&y
)
1800 unsigned int precision
= get_binary_precision (x
, y
);
1801 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1802 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1803 if (xi
.is_sign_extended
&& yi
.is_sign_extended
)
1805 /* This case reduces to array equality. */
1806 if (xi
.len
!= yi
.len
)
1810 if (xi
.val
[i
] != yi
.val
[i
])
1812 while (++i
!= xi
.len
);
1815 if (__builtin_expect (yi
.len
== 1, true))
1817 /* XI is only equal to YI if it too has a single HWI. */
1820 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1821 with 0 are simple. */
1822 if (STATIC_CONSTANT_P (yi
.val
[0] == 0))
1823 return xi
.val
[0] == 0;
1824 /* Otherwise flush out any excess bits first. */
1825 unsigned HOST_WIDE_INT diff
= xi
.val
[0] ^ yi
.val
[0];
1826 int excess
= HOST_BITS_PER_WIDE_INT
- precision
;
1831 return eq_p_large (xi
.val
, xi
.len
, yi
.val
, yi
.len
, precision
);
1834 /* Return true if X != Y. X and Y must be binary-compatible. */
1835 template <typename T1
, typename T2
>
1837 wi::ne_p (const T1
&x
, const T2
&y
)
1839 return !eq_p (x
, y
);
1842 /* Return true if X < Y when both are treated as signed values. */
1843 template <typename T1
, typename T2
>
1845 wi::lts_p (const T1
&x
, const T2
&y
)
1847 unsigned int precision
= get_binary_precision (x
, y
);
1848 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1849 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1850 /* We optimize x < y, where y is 64 or fewer bits. */
1851 if (wi::fits_shwi_p (yi
))
1853 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1854 if (STATIC_CONSTANT_P (yi
.val
[0] == 0))
1856 /* If x fits directly into a shwi, we can compare directly. */
1857 if (wi::fits_shwi_p (xi
))
1858 return xi
.to_shwi () < yi
.to_shwi ();
1859 /* If x doesn't fit and is negative, then it must be more
1860 negative than any value in y, and hence smaller than y. */
1863 /* If x is positive, then it must be larger than any value in y,
1864 and hence greater than y. */
1867 /* Optimize the opposite case, if it can be detected at compile time. */
1868 if (STATIC_CONSTANT_P (xi
.len
== 1))
1869 /* If YI is negative it is lower than the least HWI.
1870 If YI is positive it is greater than the greatest HWI. */
1872 return lts_p_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
1875 /* Return true if X < Y when both are treated as unsigned values. */
1876 template <typename T1
, typename T2
>
1878 wi::ltu_p (const T1
&x
, const T2
&y
)
1880 unsigned int precision
= get_binary_precision (x
, y
);
1881 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
1882 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
1883 /* Optimize comparisons with constants. */
1884 if (STATIC_CONSTANT_P (yi
.len
== 1 && yi
.val
[0] >= 0))
1885 return xi
.len
== 1 && xi
.to_uhwi () < (unsigned HOST_WIDE_INT
) yi
.val
[0];
1886 if (STATIC_CONSTANT_P (xi
.len
== 1 && xi
.val
[0] >= 0))
1887 return yi
.len
!= 1 || yi
.to_uhwi () > (unsigned HOST_WIDE_INT
) xi
.val
[0];
1888 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1889 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1890 values does not change the result. */
1891 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
1893 unsigned HOST_WIDE_INT xl
= xi
.to_uhwi ();
1894 unsigned HOST_WIDE_INT yl
= yi
.to_uhwi ();
1897 return ltu_p_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
1900 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1901 template <typename T1
, typename T2
>
1903 wi::lt_p (const T1
&x
, const T2
&y
, signop sgn
)
1906 return lts_p (x
, y
);
1908 return ltu_p (x
, y
);
1911 /* Return true if X <= Y when both are treated as signed values. */
1912 template <typename T1
, typename T2
>
1914 wi::les_p (const T1
&x
, const T2
&y
)
1916 return !lts_p (y
, x
);
1919 /* Return true if X <= Y when both are treated as unsigned values. */
1920 template <typename T1
, typename T2
>
1922 wi::leu_p (const T1
&x
, const T2
&y
)
1924 return !ltu_p (y
, x
);
1927 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1928 template <typename T1
, typename T2
>
1930 wi::le_p (const T1
&x
, const T2
&y
, signop sgn
)
1933 return les_p (x
, y
);
1935 return leu_p (x
, y
);
1938 /* Return true if X > Y when both are treated as signed values. */
1939 template <typename T1
, typename T2
>
1941 wi::gts_p (const T1
&x
, const T2
&y
)
1943 return lts_p (y
, x
);
1946 /* Return true if X > Y when both are treated as unsigned values. */
1947 template <typename T1
, typename T2
>
1949 wi::gtu_p (const T1
&x
, const T2
&y
)
1951 return ltu_p (y
, x
);
1954 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1955 template <typename T1
, typename T2
>
1957 wi::gt_p (const T1
&x
, const T2
&y
, signop sgn
)
1960 return gts_p (x
, y
);
1962 return gtu_p (x
, y
);
1965 /* Return true if X >= Y when both are treated as signed values. */
1966 template <typename T1
, typename T2
>
1968 wi::ges_p (const T1
&x
, const T2
&y
)
1970 return !lts_p (x
, y
);
1973 /* Return true if X >= Y when both are treated as unsigned values. */
1974 template <typename T1
, typename T2
>
1976 wi::geu_p (const T1
&x
, const T2
&y
)
1978 return !ltu_p (x
, y
);
1981 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1982 template <typename T1
, typename T2
>
1984 wi::ge_p (const T1
&x
, const T2
&y
, signop sgn
)
1987 return ges_p (x
, y
);
1989 return geu_p (x
, y
);
1992 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1993 as signed values. */
1994 template <typename T1
, typename T2
>
1996 wi::cmps (const T1
&x
, const T2
&y
)
1998 unsigned int precision
= get_binary_precision (x
, y
);
1999 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2000 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2001 if (wi::fits_shwi_p (yi
))
2003 /* Special case for comparisons with 0. */
2004 if (STATIC_CONSTANT_P (yi
.val
[0] == 0))
2005 return neg_p (xi
) ? -1 : !(xi
.len
== 1 && xi
.val
[0] == 0);
2006 /* If x fits into a signed HWI, we can compare directly. */
2007 if (wi::fits_shwi_p (xi
))
2009 HOST_WIDE_INT xl
= xi
.to_shwi ();
2010 HOST_WIDE_INT yl
= yi
.to_shwi ();
2011 return xl
< yl
? -1 : xl
> yl
;
2013 /* If x doesn't fit and is negative, then it must be more
2014 negative than any signed HWI, and hence smaller than y. */
2017 /* If x is positive, then it must be larger than any signed HWI,
2018 and hence greater than y. */
2021 /* Optimize the opposite case, if it can be detected at compile time. */
2022 if (STATIC_CONSTANT_P (xi
.len
== 1))
2023 /* If YI is negative it is lower than the least HWI.
2024 If YI is positive it is greater than the greatest HWI. */
2025 return neg_p (yi
) ? 1 : -1;
2026 return cmps_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
2029 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2030 as unsigned values. */
2031 template <typename T1
, typename T2
>
2033 wi::cmpu (const T1
&x
, const T2
&y
)
2035 unsigned int precision
= get_binary_precision (x
, y
);
2036 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2037 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2038 /* Optimize comparisons with constants. */
2039 if (STATIC_CONSTANT_P (yi
.len
== 1 && yi
.val
[0] >= 0))
2041 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2044 /* Otherwise compare directly. */
2045 unsigned HOST_WIDE_INT xl
= xi
.to_uhwi ();
2046 unsigned HOST_WIDE_INT yl
= yi
.val
[0];
2047 return xl
< yl
? -1 : xl
> yl
;
2049 if (STATIC_CONSTANT_P (xi
.len
== 1 && xi
.val
[0] >= 0))
2051 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2054 /* Otherwise compare directly. */
2055 unsigned HOST_WIDE_INT xl
= xi
.val
[0];
2056 unsigned HOST_WIDE_INT yl
= yi
.to_uhwi ();
2057 return xl
< yl
? -1 : xl
> yl
;
2059 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2060 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2061 values does not change the result. */
2062 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2064 unsigned HOST_WIDE_INT xl
= xi
.to_uhwi ();
2065 unsigned HOST_WIDE_INT yl
= yi
.to_uhwi ();
2066 return xl
< yl
? -1 : xl
> yl
;
2068 return cmpu_large (xi
.val
, xi
.len
, precision
, yi
.val
, yi
.len
);
2071 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2072 X and Y indicated by SGN. */
2073 template <typename T1
, typename T2
>
2075 wi::cmp (const T1
&x
, const T2
&y
, signop sgn
)
2084 template <typename T
>
2085 inline WI_UNARY_RESULT (T
)
2086 wi::bit_not (const T
&x
)
2088 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
2089 WIDE_INT_REF_FOR (T
) xi (x
, get_precision (result
));
2090 for (unsigned int i
= 0; i
< xi
.len
; ++i
)
2091 val
[i
] = ~xi
.val
[i
];
2092 result
.set_len (xi
.len
);
2097 template <typename T
>
2098 inline WI_UNARY_RESULT (T
)
2099 wi::neg (const T
&x
)
2104 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2105 template <typename T
>
2106 inline WI_UNARY_RESULT (T
)
2107 wi::neg (const T
&x
, bool *overflow
)
2109 *overflow
= only_sign_bit_p (x
);
2113 /* Return the absolute value of x. */
2114 template <typename T
>
2115 inline WI_UNARY_RESULT (T
)
2116 wi::abs (const T
&x
)
2118 return neg_p (x
) ? neg (x
) : WI_UNARY_RESULT (T
) (x
);
2121 /* Return the result of sign-extending the low OFFSET bits of X. */
2122 template <typename T
>
2123 inline WI_UNARY_RESULT (T
)
2124 wi::sext (const T
&x
, unsigned int offset
)
2126 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
2127 unsigned int precision
= get_precision (result
);
2128 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
2130 if (offset
<= HOST_BITS_PER_WIDE_INT
)
2132 val
[0] = sext_hwi (xi
.ulow (), offset
);
2133 result
.set_len (1, true);
2136 result
.set_len (sext_large (val
, xi
.val
, xi
.len
, precision
, offset
));
2140 /* Return the result of zero-extending the low OFFSET bits of X. */
2141 template <typename T
>
2142 inline WI_UNARY_RESULT (T
)
2143 wi::zext (const T
&x
, unsigned int offset
)
2145 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
2146 unsigned int precision
= get_precision (result
);
2147 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
2149 /* This is not just an optimization, it is actually required to
2150 maintain canonization. */
2151 if (offset
>= precision
)
2153 wi::copy (result
, xi
);
2157 /* In these cases we know that at least the top bit will be clear,
2158 so no sign extension is necessary. */
2159 if (offset
< HOST_BITS_PER_WIDE_INT
)
2161 val
[0] = zext_hwi (xi
.ulow (), offset
);
2162 result
.set_len (1, true);
2165 result
.set_len (zext_large (val
, xi
.val
, xi
.len
, precision
, offset
), true);
2169 /* Return the result of extending the low OFFSET bits of X according to
2171 template <typename T
>
2172 inline WI_UNARY_RESULT (T
)
2173 wi::ext (const T
&x
, unsigned int offset
, signop sgn
)
2175 return sgn
== SIGNED
? sext (x
, offset
) : zext (x
, offset
);
2178 /* Return an integer that represents X | (1 << bit). */
2179 template <typename T
>
2180 inline WI_UNARY_RESULT (T
)
2181 wi::set_bit (const T
&x
, unsigned int bit
)
2183 WI_UNARY_RESULT_VAR (result
, val
, T
, x
);
2184 unsigned int precision
= get_precision (result
);
2185 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
2186 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2188 val
[0] = xi
.ulow () | (HOST_WIDE_INT_1U
<< bit
);
2192 result
.set_len (set_bit_large (val
, xi
.val
, xi
.len
, precision
, bit
));
2196 /* Return the mininum of X and Y, treating them both as having
2198 template <typename T1
, typename T2
>
2199 inline WI_BINARY_RESULT (T1
, T2
)
2200 wi::min (const T1
&x
, const T2
&y
, signop sgn
)
2202 WI_BINARY_RESULT_VAR (result
, val ATTRIBUTE_UNUSED
, T1
, x
, T2
, y
);
2203 unsigned int precision
= get_precision (result
);
2204 if (wi::le_p (x
, y
, sgn
))
2205 wi::copy (result
, WIDE_INT_REF_FOR (T1
) (x
, precision
));
2207 wi::copy (result
, WIDE_INT_REF_FOR (T2
) (y
, precision
));
2211 /* Return the minimum of X and Y, treating both as signed values. */
2212 template <typename T1
, typename T2
>
2213 inline WI_BINARY_RESULT (T1
, T2
)
2214 wi::smin (const T1
&x
, const T2
&y
)
2216 return wi::min (x
, y
, SIGNED
);
2219 /* Return the minimum of X and Y, treating both as unsigned values. */
2220 template <typename T1
, typename T2
>
2221 inline WI_BINARY_RESULT (T1
, T2
)
2222 wi::umin (const T1
&x
, const T2
&y
)
2224 return wi::min (x
, y
, UNSIGNED
);
2227 /* Return the maxinum of X and Y, treating them both as having
2229 template <typename T1
, typename T2
>
2230 inline WI_BINARY_RESULT (T1
, T2
)
2231 wi::max (const T1
&x
, const T2
&y
, signop sgn
)
2233 WI_BINARY_RESULT_VAR (result
, val ATTRIBUTE_UNUSED
, T1
, x
, T2
, y
);
2234 unsigned int precision
= get_precision (result
);
2235 if (wi::ge_p (x
, y
, sgn
))
2236 wi::copy (result
, WIDE_INT_REF_FOR (T1
) (x
, precision
));
2238 wi::copy (result
, WIDE_INT_REF_FOR (T2
) (y
, precision
));
2242 /* Return the maximum of X and Y, treating both as signed values. */
2243 template <typename T1
, typename T2
>
2244 inline WI_BINARY_RESULT (T1
, T2
)
2245 wi::smax (const T1
&x
, const T2
&y
)
2247 return wi::max (x
, y
, SIGNED
);
2250 /* Return the maximum of X and Y, treating both as unsigned values. */
2251 template <typename T1
, typename T2
>
2252 inline WI_BINARY_RESULT (T1
, T2
)
2253 wi::umax (const T1
&x
, const T2
&y
)
2255 return wi::max (x
, y
, UNSIGNED
);
2259 template <typename T1
, typename T2
>
2260 inline WI_BINARY_RESULT (T1
, T2
)
2261 wi::bit_and (const T1
&x
, const T2
&y
)
2263 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2264 unsigned int precision
= get_precision (result
);
2265 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2266 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2267 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2268 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2270 val
[0] = xi
.ulow () & yi
.ulow ();
2271 result
.set_len (1, is_sign_extended
);
2274 result
.set_len (and_large (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2275 precision
), is_sign_extended
);
2279 /* Return X & ~Y. */
2280 template <typename T1
, typename T2
>
2281 inline WI_BINARY_RESULT (T1
, T2
)
2282 wi::bit_and_not (const T1
&x
, const T2
&y
)
2284 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2285 unsigned int precision
= get_precision (result
);
2286 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2287 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2288 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2289 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2291 val
[0] = xi
.ulow () & ~yi
.ulow ();
2292 result
.set_len (1, is_sign_extended
);
2295 result
.set_len (and_not_large (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2296 precision
), is_sign_extended
);
2301 template <typename T1
, typename T2
>
2302 inline WI_BINARY_RESULT (T1
, T2
)
2303 wi::bit_or (const T1
&x
, const T2
&y
)
2305 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2306 unsigned int precision
= get_precision (result
);
2307 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2308 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2309 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2310 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2312 val
[0] = xi
.ulow () | yi
.ulow ();
2313 result
.set_len (1, is_sign_extended
);
2316 result
.set_len (or_large (val
, xi
.val
, xi
.len
,
2317 yi
.val
, yi
.len
, precision
), is_sign_extended
);
2321 /* Return X | ~Y. */
2322 template <typename T1
, typename T2
>
2323 inline WI_BINARY_RESULT (T1
, T2
)
2324 wi::bit_or_not (const T1
&x
, const T2
&y
)
2326 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2327 unsigned int precision
= get_precision (result
);
2328 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2329 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2330 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2331 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2333 val
[0] = xi
.ulow () | ~yi
.ulow ();
2334 result
.set_len (1, is_sign_extended
);
2337 result
.set_len (or_not_large (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2338 precision
), is_sign_extended
);
2343 template <typename T1
, typename T2
>
2344 inline WI_BINARY_RESULT (T1
, T2
)
2345 wi::bit_xor (const T1
&x
, const T2
&y
)
2347 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2348 unsigned int precision
= get_precision (result
);
2349 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2350 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2351 bool is_sign_extended
= xi
.is_sign_extended
&& yi
.is_sign_extended
;
2352 if (__builtin_expect (xi
.len
+ yi
.len
== 2, true))
2354 val
[0] = xi
.ulow () ^ yi
.ulow ();
2355 result
.set_len (1, is_sign_extended
);
2358 result
.set_len (xor_large (val
, xi
.val
, xi
.len
,
2359 yi
.val
, yi
.len
, precision
), is_sign_extended
);
2364 template <typename T1
, typename T2
>
2365 inline WI_BINARY_RESULT (T1
, T2
)
2366 wi::add (const T1
&x
, const T2
&y
)
2368 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2369 unsigned int precision
= get_precision (result
);
2370 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2371 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2372 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2374 val
[0] = xi
.ulow () + yi
.ulow ();
2377 /* If the precision is known at compile time to be greater than
2378 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2379 knowing that (a) all bits in those HWIs are significant and
2380 (b) the result has room for at least two HWIs. This provides
2381 a fast path for things like offset_int and widest_int.
2383 The STATIC_CONSTANT_P test prevents this path from being
2384 used for wide_ints. wide_ints with precisions greater than
2385 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2386 point handling them inline. */
2387 else if (STATIC_CONSTANT_P (precision
> HOST_BITS_PER_WIDE_INT
)
2388 && __builtin_expect (xi
.len
+ yi
.len
== 2, true))
2390 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2391 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2392 unsigned HOST_WIDE_INT resultl
= xl
+ yl
;
2394 val
[1] = (HOST_WIDE_INT
) resultl
< 0 ? 0 : -1;
2395 result
.set_len (1 + (((resultl
^ xl
) & (resultl
^ yl
))
2396 >> (HOST_BITS_PER_WIDE_INT
- 1)));
2399 result
.set_len (add_large (val
, xi
.val
, xi
.len
,
2400 yi
.val
, yi
.len
, precision
,
2405 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2406 and indicate in *OVERFLOW whether the operation overflowed. */
2407 template <typename T1
, typename T2
>
2408 inline WI_BINARY_RESULT (T1
, T2
)
2409 wi::add (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2411 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2412 unsigned int precision
= get_precision (result
);
2413 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2414 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2415 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2417 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2418 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2419 unsigned HOST_WIDE_INT resultl
= xl
+ yl
;
2421 *overflow
= (((resultl
^ xl
) & (resultl
^ yl
))
2422 >> (precision
- 1)) & 1;
2424 *overflow
= ((resultl
<< (HOST_BITS_PER_WIDE_INT
- precision
))
2425 < (xl
<< (HOST_BITS_PER_WIDE_INT
- precision
)));
2430 result
.set_len (add_large (val
, xi
.val
, xi
.len
,
2431 yi
.val
, yi
.len
, precision
,
2437 template <typename T1
, typename T2
>
2438 inline WI_BINARY_RESULT (T1
, T2
)
2439 wi::sub (const T1
&x
, const T2
&y
)
2441 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2442 unsigned int precision
= get_precision (result
);
2443 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2444 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2445 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2447 val
[0] = xi
.ulow () - yi
.ulow ();
2450 /* If the precision is known at compile time to be greater than
2451 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2452 knowing that (a) all bits in those HWIs are significant and
2453 (b) the result has room for at least two HWIs. This provides
2454 a fast path for things like offset_int and widest_int.
2456 The STATIC_CONSTANT_P test prevents this path from being
2457 used for wide_ints. wide_ints with precisions greater than
2458 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2459 point handling them inline. */
2460 else if (STATIC_CONSTANT_P (precision
> HOST_BITS_PER_WIDE_INT
)
2461 && __builtin_expect (xi
.len
+ yi
.len
== 2, true))
2463 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2464 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2465 unsigned HOST_WIDE_INT resultl
= xl
- yl
;
2467 val
[1] = (HOST_WIDE_INT
) resultl
< 0 ? 0 : -1;
2468 result
.set_len (1 + (((resultl
^ xl
) & (xl
^ yl
))
2469 >> (HOST_BITS_PER_WIDE_INT
- 1)));
2472 result
.set_len (sub_large (val
, xi
.val
, xi
.len
,
2473 yi
.val
, yi
.len
, precision
,
2478 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2479 and indicate in *OVERFLOW whether the operation overflowed. */
2480 template <typename T1
, typename T2
>
2481 inline WI_BINARY_RESULT (T1
, T2
)
2482 wi::sub (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2484 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2485 unsigned int precision
= get_precision (result
);
2486 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2487 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2488 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2490 unsigned HOST_WIDE_INT xl
= xi
.ulow ();
2491 unsigned HOST_WIDE_INT yl
= yi
.ulow ();
2492 unsigned HOST_WIDE_INT resultl
= xl
- yl
;
2494 *overflow
= (((xl
^ yl
) & (resultl
^ xl
)) >> (precision
- 1)) & 1;
2496 *overflow
= ((resultl
<< (HOST_BITS_PER_WIDE_INT
- precision
))
2497 > (xl
<< (HOST_BITS_PER_WIDE_INT
- precision
)));
2502 result
.set_len (sub_large (val
, xi
.val
, xi
.len
,
2503 yi
.val
, yi
.len
, precision
,
2509 template <typename T1
, typename T2
>
2510 inline WI_BINARY_RESULT (T1
, T2
)
2511 wi::mul (const T1
&x
, const T2
&y
)
2513 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2514 unsigned int precision
= get_precision (result
);
2515 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2516 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2517 if (precision
<= HOST_BITS_PER_WIDE_INT
)
2519 val
[0] = xi
.ulow () * yi
.ulow ();
2523 result
.set_len (mul_internal (val
, xi
.val
, xi
.len
, yi
.val
, yi
.len
,
2524 precision
, UNSIGNED
, 0, false));
2528 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2529 and indicate in *OVERFLOW whether the operation overflowed. */
2530 template <typename T1
, typename T2
>
2531 inline WI_BINARY_RESULT (T1
, T2
)
2532 wi::mul (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2534 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2535 unsigned int precision
= get_precision (result
);
2536 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2537 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2538 result
.set_len (mul_internal (val
, xi
.val
, xi
.len
,
2539 yi
.val
, yi
.len
, precision
,
2540 sgn
, overflow
, false));
2544 /* Return X * Y, treating both X and Y as signed values. Indicate in
2545 *OVERFLOW whether the operation overflowed. */
2546 template <typename T1
, typename T2
>
2547 inline WI_BINARY_RESULT (T1
, T2
)
2548 wi::smul (const T1
&x
, const T2
&y
, bool *overflow
)
2550 return mul (x
, y
, SIGNED
, overflow
);
2553 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2554 *OVERFLOW whether the operation overflowed. */
2555 template <typename T1
, typename T2
>
2556 inline WI_BINARY_RESULT (T1
, T2
)
2557 wi::umul (const T1
&x
, const T2
&y
, bool *overflow
)
2559 return mul (x
, y
, UNSIGNED
, overflow
);
2562 /* Perform a widening multiplication of X and Y, extending the values
2563 according to SGN, and return the high part of the result. */
2564 template <typename T1
, typename T2
>
2565 inline WI_BINARY_RESULT (T1
, T2
)
2566 wi::mul_high (const T1
&x
, const T2
&y
, signop sgn
)
2568 WI_BINARY_RESULT_VAR (result
, val
, T1
, x
, T2
, y
);
2569 unsigned int precision
= get_precision (result
);
2570 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2571 WIDE_INT_REF_FOR (T2
) yi (y
, precision
);
2572 result
.set_len (mul_internal (val
, xi
.val
, xi
.len
,
2573 yi
.val
, yi
.len
, precision
,
2578 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2579 signedness given by SGN. Indicate in *OVERFLOW if the result
2581 template <typename T1
, typename T2
>
2582 inline WI_BINARY_RESULT (T1
, T2
)
2583 wi::div_trunc (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2585 WI_BINARY_RESULT_VAR (quotient
, quotient_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 quotient
.set_len (divmod_internal (quotient_val
, 0, 0, xi
.val
, xi
.len
,
2592 yi
.val
, yi
.len
, yi
.precision
,
2597 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2598 template <typename T1
, typename T2
>
2599 inline WI_BINARY_RESULT (T1
, T2
)
2600 wi::sdiv_trunc (const T1
&x
, const T2
&y
)
2602 return div_trunc (x
, y
, SIGNED
);
2605 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2606 template <typename T1
, typename T2
>
2607 inline WI_BINARY_RESULT (T1
, T2
)
2608 wi::udiv_trunc (const T1
&x
, const T2
&y
)
2610 return div_trunc (x
, y
, UNSIGNED
);
2613 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2614 signedness given by SGN. Indicate in *OVERFLOW if the result
2616 template <typename T1
, typename T2
>
2617 inline WI_BINARY_RESULT (T1
, T2
)
2618 wi::div_floor (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2620 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2621 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2622 unsigned int precision
= get_precision (quotient
);
2623 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2624 WIDE_INT_REF_FOR (T2
) yi (y
);
2626 unsigned int remainder_len
;
2627 quotient
.set_len (divmod_internal (quotient_val
,
2628 &remainder_len
, remainder_val
,
2629 xi
.val
, xi
.len
, precision
,
2630 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2632 remainder
.set_len (remainder_len
);
2633 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
) && remainder
!= 0)
2634 return quotient
- 1;
2638 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2639 template <typename T1
, typename T2
>
2640 inline WI_BINARY_RESULT (T1
, T2
)
2641 wi::sdiv_floor (const T1
&x
, const T2
&y
)
2643 return div_floor (x
, y
, SIGNED
);
2646 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2647 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2648 template <typename T1
, typename T2
>
2649 inline WI_BINARY_RESULT (T1
, T2
)
2650 wi::udiv_floor (const T1
&x
, const T2
&y
)
2652 return div_floor (x
, y
, UNSIGNED
);
2655 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2656 signedness given by SGN. Indicate in *OVERFLOW if the result
2658 template <typename T1
, typename T2
>
2659 inline WI_BINARY_RESULT (T1
, T2
)
2660 wi::div_ceil (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2662 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2663 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2664 unsigned int precision
= get_precision (quotient
);
2665 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2666 WIDE_INT_REF_FOR (T2
) yi (y
);
2668 unsigned int remainder_len
;
2669 quotient
.set_len (divmod_internal (quotient_val
,
2670 &remainder_len
, remainder_val
,
2671 xi
.val
, xi
.len
, precision
,
2672 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2674 remainder
.set_len (remainder_len
);
2675 if (wi::neg_p (x
, sgn
) == wi::neg_p (y
, sgn
) && remainder
!= 0)
2676 return quotient
+ 1;
2680 /* Return X / Y, rouding towards nearest with ties away from zero.
2681 Treat X and Y as having the signedness given by SGN. Indicate
2682 in *OVERFLOW if the result overflows. */
2683 template <typename T1
, typename T2
>
2684 inline WI_BINARY_RESULT (T1
, T2
)
2685 wi::div_round (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2687 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2688 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2689 unsigned int precision
= get_precision (quotient
);
2690 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2691 WIDE_INT_REF_FOR (T2
) yi (y
);
2693 unsigned int remainder_len
;
2694 quotient
.set_len (divmod_internal (quotient_val
,
2695 &remainder_len
, remainder_val
,
2696 xi
.val
, xi
.len
, precision
,
2697 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2699 remainder
.set_len (remainder_len
);
2705 WI_BINARY_RESULT (T1
, T2
) abs_remainder
= wi::abs (remainder
);
2706 if (wi::geu_p (abs_remainder
, wi::sub (wi::abs (y
), abs_remainder
)))
2708 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
))
2709 return quotient
- 1;
2711 return quotient
+ 1;
2716 if (wi::geu_p (remainder
, wi::sub (y
, remainder
)))
2717 return quotient
+ 1;
2723 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2724 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2725 template <typename T1
, typename T2
>
2726 inline WI_BINARY_RESULT (T1
, T2
)
2727 wi::divmod_trunc (const T1
&x
, const T2
&y
, signop sgn
,
2728 WI_BINARY_RESULT (T1
, T2
) *remainder_ptr
)
2730 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2731 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2732 unsigned int precision
= get_precision (quotient
);
2733 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2734 WIDE_INT_REF_FOR (T2
) yi (y
);
2736 unsigned int remainder_len
;
2737 quotient
.set_len (divmod_internal (quotient_val
,
2738 &remainder_len
, remainder_val
,
2739 xi
.val
, xi
.len
, precision
,
2740 yi
.val
, yi
.len
, yi
.precision
, sgn
, 0));
2741 remainder
.set_len (remainder_len
);
2743 *remainder_ptr
= remainder
;
2747 /* Compute the greatest common divisor of two numbers A and B using
2748 Euclid's algorithm. */
2749 template <typename T1
, typename T2
>
2750 inline WI_BINARY_RESULT (T1
, T2
)
2751 wi::gcd (const T1
&a
, const T2
&b
, signop sgn
)
2758 while (gt_p (x
, 0, sgn
))
2760 z
= mod_trunc (y
, x
, sgn
);
2768 /* Compute X / Y, rouding towards 0, and return the remainder.
2769 Treat X and Y as having the signedness given by SGN. Indicate
2770 in *OVERFLOW if the division overflows. */
2771 template <typename T1
, typename T2
>
2772 inline WI_BINARY_RESULT (T1
, T2
)
2773 wi::mod_trunc (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2775 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2776 unsigned int precision
= get_precision (remainder
);
2777 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2778 WIDE_INT_REF_FOR (T2
) yi (y
);
2780 unsigned int remainder_len
;
2781 divmod_internal (0, &remainder_len
, remainder_val
,
2782 xi
.val
, xi
.len
, precision
,
2783 yi
.val
, yi
.len
, yi
.precision
, sgn
, overflow
);
2784 remainder
.set_len (remainder_len
);
2789 /* Compute X / Y, rouding towards 0, and return the remainder.
2790 Treat X and Y as signed values. */
2791 template <typename T1
, typename T2
>
2792 inline WI_BINARY_RESULT (T1
, T2
)
2793 wi::smod_trunc (const T1
&x
, const T2
&y
)
2795 return mod_trunc (x
, y
, SIGNED
);
2798 /* Compute X / Y, rouding towards 0, and return the remainder.
2799 Treat X and Y as unsigned values. */
2800 template <typename T1
, typename T2
>
2801 inline WI_BINARY_RESULT (T1
, T2
)
2802 wi::umod_trunc (const T1
&x
, const T2
&y
)
2804 return mod_trunc (x
, y
, UNSIGNED
);
2807 /* Compute X / Y, rouding towards -inf, and return the remainder.
2808 Treat X and Y as having the signedness given by SGN. Indicate
2809 in *OVERFLOW if the division overflows. */
2810 template <typename T1
, typename T2
>
2811 inline WI_BINARY_RESULT (T1
, T2
)
2812 wi::mod_floor (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2814 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2815 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2816 unsigned int precision
= get_precision (quotient
);
2817 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2818 WIDE_INT_REF_FOR (T2
) yi (y
);
2820 unsigned int remainder_len
;
2821 quotient
.set_len (divmod_internal (quotient_val
,
2822 &remainder_len
, remainder_val
,
2823 xi
.val
, xi
.len
, precision
,
2824 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2826 remainder
.set_len (remainder_len
);
2828 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
) && remainder
!= 0)
2829 return remainder
+ y
;
2833 /* Compute X / Y, rouding towards -inf, and return the remainder.
2834 Treat X and Y as unsigned values. */
2835 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2836 template <typename T1
, typename T2
>
2837 inline WI_BINARY_RESULT (T1
, T2
)
2838 wi::umod_floor (const T1
&x
, const T2
&y
)
2840 return mod_floor (x
, y
, UNSIGNED
);
2843 /* Compute X / Y, rouding towards +inf, and return the remainder.
2844 Treat X and Y as having the signedness given by SGN. Indicate
2845 in *OVERFLOW if the division overflows. */
2846 template <typename T1
, typename T2
>
2847 inline WI_BINARY_RESULT (T1
, T2
)
2848 wi::mod_ceil (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2850 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2851 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2852 unsigned int precision
= get_precision (quotient
);
2853 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2854 WIDE_INT_REF_FOR (T2
) yi (y
);
2856 unsigned int remainder_len
;
2857 quotient
.set_len (divmod_internal (quotient_val
,
2858 &remainder_len
, remainder_val
,
2859 xi
.val
, xi
.len
, precision
,
2860 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2862 remainder
.set_len (remainder_len
);
2864 if (wi::neg_p (x
, sgn
) == wi::neg_p (y
, sgn
) && remainder
!= 0)
2865 return remainder
- y
;
2869 /* Compute X / Y, rouding towards nearest with ties away from zero,
2870 and return the remainder. Treat X and Y as having the signedness
2871 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2872 template <typename T1
, typename T2
>
2873 inline WI_BINARY_RESULT (T1
, T2
)
2874 wi::mod_round (const T1
&x
, const T2
&y
, signop sgn
, bool *overflow
)
2876 WI_BINARY_RESULT_VAR (quotient
, quotient_val
, T1
, x
, T2
, y
);
2877 WI_BINARY_RESULT_VAR (remainder
, remainder_val
, T1
, x
, T2
, y
);
2878 unsigned int precision
= get_precision (quotient
);
2879 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2880 WIDE_INT_REF_FOR (T2
) yi (y
);
2882 unsigned int remainder_len
;
2883 quotient
.set_len (divmod_internal (quotient_val
,
2884 &remainder_len
, remainder_val
,
2885 xi
.val
, xi
.len
, precision
,
2886 yi
.val
, yi
.len
, yi
.precision
, sgn
,
2888 remainder
.set_len (remainder_len
);
2894 WI_BINARY_RESULT (T1
, T2
) abs_remainder
= wi::abs (remainder
);
2895 if (wi::geu_p (abs_remainder
, wi::sub (wi::abs (y
), abs_remainder
)))
2897 if (wi::neg_p (x
, sgn
) != wi::neg_p (y
, sgn
))
2898 return remainder
+ y
;
2900 return remainder
- y
;
2905 if (wi::geu_p (remainder
, wi::sub (y
, remainder
)))
2906 return remainder
- y
;
2912 /* Return true if X is a multiple of Y. Treat X and Y as having the
2913 signedness given by SGN. */
2914 template <typename T1
, typename T2
>
2916 wi::multiple_of_p (const T1
&x
, const T2
&y
, signop sgn
)
2918 return wi::mod_trunc (x
, y
, sgn
) == 0;
2921 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2922 Treat X and Y as having the signedness given by SGN. */
2923 template <typename T1
, typename T2
>
2925 wi::multiple_of_p (const T1
&x
, const T2
&y
, signop sgn
,
2926 WI_BINARY_RESULT (T1
, T2
) *res
)
2928 WI_BINARY_RESULT (T1
, T2
) remainder
;
2929 WI_BINARY_RESULT (T1
, T2
) quotient
2930 = divmod_trunc (x
, y
, sgn
, &remainder
);
2939 /* Return X << Y. Return 0 if Y is greater than or equal to
2940 the precision of X. */
2941 template <typename T1
, typename T2
>
2942 inline WI_UNARY_RESULT (T1
)
2943 wi::lshift (const T1
&x
, const T2
&y
)
2945 WI_UNARY_RESULT_VAR (result
, val
, T1
, x
);
2946 unsigned int precision
= get_precision (result
);
2947 WIDE_INT_REF_FOR (T1
) xi (x
, precision
);
2948 WIDE_INT_REF_FOR (T2
) yi (y
);
2949 /* Handle the simple cases quickly. */
2950 if (geu_p (yi
, precision
))
2957 unsigned int shift
= yi
.to_uhwi ();
2958 /* For fixed-precision integers like offset_int and widest_int,
2959 handle the case where the shift value is constant and the
2960 result is a single nonnegative HWI (meaning that we don't
2961 need to worry about val[1]). This is particularly common
2962 for converting a byte count to a bit count.
2964 For variable-precision integers like wide_int, handle HWI
2965 and sub-HWI integers inline. */
2966 if (STATIC_CONSTANT_P (xi
.precision
> HOST_BITS_PER_WIDE_INT
)
2967 ? (STATIC_CONSTANT_P (shift
< HOST_BITS_PER_WIDE_INT
- 1)
2969 && xi
.val
[0] <= (HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
)
2970 HOST_WIDE_INT_MAX
>> shift
))
2971 : precision
<= HOST_BITS_PER_WIDE_INT
)
2973 val
[0] = xi
.ulow () << shift
;
2977 result
.set_len (lshift_large (val
, xi
.val
, xi
.len
,
2983 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2984 or equal to the precision of X. */
2985 template <typename T1
, typename T2
>
2986 inline WI_UNARY_RESULT (T1
)
2987 wi::lrshift (const T1
&x
, const T2
&y
)
2989 WI_UNARY_RESULT_VAR (result
, val
, T1
, x
);
2990 /* Do things in the precision of the input rather than the output,
2991 since the result can be no larger than that. */
2992 WIDE_INT_REF_FOR (T1
) xi (x
);
2993 WIDE_INT_REF_FOR (T2
) yi (y
);
2994 /* Handle the simple cases quickly. */
2995 if (geu_p (yi
, xi
.precision
))
3002 unsigned int shift
= yi
.to_uhwi ();
3003 /* For fixed-precision integers like offset_int and widest_int,
3004 handle the case where the shift value is constant and the
3005 shifted value is a single nonnegative HWI (meaning that all
3006 bits above the HWI are zero). This is particularly common
3007 for converting a bit count to a byte count.
3009 For variable-precision integers like wide_int, handle HWI
3010 and sub-HWI integers inline. */
3011 if (STATIC_CONSTANT_P (xi
.precision
> HOST_BITS_PER_WIDE_INT
)
3012 ? (shift
< HOST_BITS_PER_WIDE_INT
3015 : xi
.precision
<= HOST_BITS_PER_WIDE_INT
)
3017 val
[0] = xi
.to_uhwi () >> shift
;
3021 result
.set_len (lrshift_large (val
, xi
.val
, xi
.len
, xi
.precision
,
3022 get_precision (result
), shift
));
3027 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
3028 Y is greater than or equal to the precision of X. */
3029 template <typename T1
, typename T2
>
3030 inline WI_UNARY_RESULT (T1
)
3031 wi::arshift (const T1
&x
, const T2
&y
)
3033 WI_UNARY_RESULT_VAR (result
, val
, T1
, x
);
3034 /* Do things in the precision of the input rather than the output,
3035 since the result can be no larger than that. */
3036 WIDE_INT_REF_FOR (T1
) xi (x
);
3037 WIDE_INT_REF_FOR (T2
) yi (y
);
3038 /* Handle the simple cases quickly. */
3039 if (geu_p (yi
, xi
.precision
))
3041 val
[0] = sign_mask (x
);
3046 unsigned int shift
= yi
.to_uhwi ();
3047 if (xi
.precision
<= HOST_BITS_PER_WIDE_INT
)
3049 val
[0] = sext_hwi (xi
.ulow () >> shift
, xi
.precision
- shift
);
3050 result
.set_len (1, true);
3053 result
.set_len (arshift_large (val
, xi
.val
, xi
.len
, xi
.precision
,
3054 get_precision (result
), shift
));
3059 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3060 logical shift otherwise. */
3061 template <typename T1
, typename T2
>
3062 inline WI_UNARY_RESULT (T1
)
3063 wi::rshift (const T1
&x
, const T2
&y
, signop sgn
)
3065 if (sgn
== UNSIGNED
)
3066 return lrshift (x
, y
);
3068 return arshift (x
, y
);
3071 /* Return the result of rotating the low WIDTH bits of X left by Y
3072 bits and zero-extending the result. Use a full-width rotate if
3074 template <typename T1
, typename T2
>
3075 WI_UNARY_RESULT (T1
)
3076 wi::lrotate (const T1
&x
, const T2
&y
, unsigned int width
)
3078 unsigned int precision
= get_binary_precision (x
, x
);
3081 WI_UNARY_RESULT (T2
) ymod
= umod_trunc (y
, width
);
3082 WI_UNARY_RESULT (T1
) left
= wi::lshift (x
, ymod
);
3083 WI_UNARY_RESULT (T1
) right
= wi::lrshift (x
, wi::sub (width
, ymod
));
3084 if (width
!= precision
)
3085 return wi::zext (left
, width
) | wi::zext (right
, width
);
3086 return left
| right
;
3089 /* Return the result of rotating the low WIDTH bits of X right by Y
3090 bits and zero-extending the result. Use a full-width rotate if
3092 template <typename T1
, typename T2
>
3093 WI_UNARY_RESULT (T1
)
3094 wi::rrotate (const T1
&x
, const T2
&y
, unsigned int width
)
3096 unsigned int precision
= get_binary_precision (x
, x
);
3099 WI_UNARY_RESULT (T2
) ymod
= umod_trunc (y
, width
);
3100 WI_UNARY_RESULT (T1
) right
= wi::lrshift (x
, ymod
);
3101 WI_UNARY_RESULT (T1
) left
= wi::lshift (x
, wi::sub (width
, ymod
));
3102 if (width
!= precision
)
3103 return wi::zext (left
, width
) | wi::zext (right
, width
);
3104 return left
| right
;
3107 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3110 wi::parity (const wide_int_ref
&x
)
3112 return popcount (x
) & 1;
3115 /* Extract WIDTH bits from X, starting at BITPOS. */
3116 template <typename T
>
3117 inline unsigned HOST_WIDE_INT
3118 wi::extract_uhwi (const T
&x
, unsigned int bitpos
, unsigned int width
)
3120 unsigned precision
= get_precision (x
);
3121 if (precision
< bitpos
+ width
)
3122 precision
= bitpos
+ width
;
3123 WIDE_INT_REF_FOR (T
) xi (x
, precision
);
3125 /* Handle this rare case after the above, so that we assert about
3126 bogus BITPOS values. */
3130 unsigned int start
= bitpos
/ HOST_BITS_PER_WIDE_INT
;
3131 unsigned int shift
= bitpos
% HOST_BITS_PER_WIDE_INT
;
3132 unsigned HOST_WIDE_INT res
= xi
.elt (start
);
3134 if (shift
+ width
> HOST_BITS_PER_WIDE_INT
)
3136 unsigned HOST_WIDE_INT upper
= xi
.elt (start
+ 1);
3137 res
|= upper
<< (-shift
% HOST_BITS_PER_WIDE_INT
);
3139 return zext_hwi (res
, width
);
3142 /* Return the minimum precision needed to store X with sign SGN. */
3143 template <typename T
>
3145 wi::min_precision (const T
&x
, signop sgn
)
3148 return get_precision (x
) - clrsb (x
);
3150 return get_precision (x
) - clz (x
);
3153 #define SIGNED_BINARY_PREDICATE(OP, F) \
3154 template <typename T1, typename T2> \
3155 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3156 OP (const T1 &x, const T2 &y) \
3158 return wi::F (x, y); \
3161 SIGNED_BINARY_PREDICATE (operator <, lts_p
)
3162 SIGNED_BINARY_PREDICATE (operator <=, les_p
)
3163 SIGNED_BINARY_PREDICATE (operator >, gts_p
)
3164 SIGNED_BINARY_PREDICATE (operator >=, ges_p
)
3166 #undef SIGNED_BINARY_PREDICATE
3168 #define UNARY_OPERATOR(OP, F) \
3169 template<typename T> \
3170 WI_UNARY_RESULT (generic_wide_int<T>) \
3171 OP (const generic_wide_int<T> &x) \
3176 #define BINARY_PREDICATE(OP, F) \
3177 template<typename T1, typename T2> \
3178 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3179 OP (const T1 &x, const T2 &y) \
3181 return wi::F (x, y); \
3184 #define BINARY_OPERATOR(OP, F) \
3185 template<typename T1, typename T2> \
3186 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3187 OP (const T1 &x, const T2 &y) \
3189 return wi::F (x, y); \
3192 #define SHIFT_OPERATOR(OP, F) \
3193 template<typename T1, typename T2> \
3194 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3195 OP (const T1 &x, const T2 &y) \
3197 return wi::F (x, y); \
3200 UNARY_OPERATOR (operator ~, bit_not
)
3201 UNARY_OPERATOR (operator -, neg
)
3202 BINARY_PREDICATE (operator ==, eq_p
)
3203 BINARY_PREDICATE (operator !=, ne_p
)
3204 BINARY_OPERATOR (operator &, bit_and
)
3205 BINARY_OPERATOR (operator |, bit_or
)
3206 BINARY_OPERATOR (operator ^, bit_xor
)
3207 BINARY_OPERATOR (operator +, add
)
3208 BINARY_OPERATOR (operator -, sub
)
3209 BINARY_OPERATOR (operator *, mul
)
3210 SHIFT_OPERATOR (operator <<, lshift
)
3212 #undef UNARY_OPERATOR
3213 #undef BINARY_PREDICATE
3214 #undef BINARY_OPERATOR
3215 #undef SHIFT_OPERATOR
3217 template <typename T1
, typename T2
>
3218 inline WI_SIGNED_SHIFT_RESULT (T1
, T2
)
3219 operator >> (const T1
&x
, const T2
&y
)
3221 return wi::arshift (x
, y
);
3224 template <typename T1
, typename T2
>
3225 inline WI_SIGNED_SHIFT_RESULT (T1
, T2
)
3226 operator / (const T1
&x
, const T2
&y
)
3228 return wi::sdiv_trunc (x
, y
);
3231 template <typename T1
, typename T2
>
3232 inline WI_SIGNED_SHIFT_RESULT (T1
, T2
)
3233 operator % (const T1
&x
, const T2
&y
)
3235 return wi::smod_trunc (x
, y
);
3238 template<typename T
>
3240 gt_ggc_mx (generic_wide_int
<T
> *)
3244 template<typename T
>
3246 gt_pch_nx (generic_wide_int
<T
> *)
3250 template<typename T
>
3252 gt_pch_nx (generic_wide_int
<T
> *, void (*) (void *, void *), void *)
3258 gt_ggc_mx (trailing_wide_ints
<N
> *)
3264 gt_pch_nx (trailing_wide_ints
<N
> *)
3270 gt_pch_nx (trailing_wide_ints
<N
> *, void (*) (void *, void *), void *)
3276 /* Used for overloaded functions in which the only other acceptable
3277 scalar type is a pointer. It stops a plain 0 from being treated
3278 as a null pointer. */
3279 struct never_used1
{};
3280 struct never_used2
{};
3282 wide_int
min_value (unsigned int, signop
);
3283 wide_int
min_value (never_used1
*);
3284 wide_int
min_value (never_used2
*);
3285 wide_int
max_value (unsigned int, signop
);
3286 wide_int
max_value (never_used1
*);
3287 wide_int
max_value (never_used2
*);
3289 /* FIXME: this is target dependent, so should be elsewhere.
3290 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3291 wide_int
from_buffer (const unsigned char *, unsigned int);
3293 #ifndef GENERATOR_FILE
3294 void to_mpz (const wide_int_ref
&, mpz_t
, signop
);
3297 wide_int
mask (unsigned int, bool, unsigned int);
3298 wide_int
shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3299 wide_int
set_bit_in_zero (unsigned int, unsigned int);
3300 wide_int
insert (const wide_int
&x
, const wide_int
&y
, unsigned int,
3303 template <typename T
>
3304 T
mask (unsigned int, bool);
3306 template <typename T
>
3307 T
shifted_mask (unsigned int, unsigned int, bool);
3309 template <typename T
>
3310 T
set_bit_in_zero (unsigned int);
3312 unsigned int mask (HOST_WIDE_INT
*, unsigned int, bool, unsigned int);
3313 unsigned int shifted_mask (HOST_WIDE_INT
*, unsigned int, unsigned int,
3314 bool, unsigned int);
3315 unsigned int from_array (HOST_WIDE_INT
*, const HOST_WIDE_INT
*,
3316 unsigned int, unsigned int, bool);
3319 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3320 and the other bits are clear, or the inverse if NEGATE_P. */
3322 wi::mask (unsigned int width
, bool negate_p
, unsigned int precision
)
3324 wide_int result
= wide_int::create (precision
);
3325 result
.set_len (mask (result
.write_val (), width
, negate_p
, precision
));
3329 /* Return a PRECISION-bit integer in which the low START bits are clear,
3330 the next WIDTH bits are set, and the other bits are clear,
3331 or the inverse if NEGATE_P. */
3333 wi::shifted_mask (unsigned int start
, unsigned int width
, bool negate_p
,
3334 unsigned int precision
)
3336 wide_int result
= wide_int::create (precision
);
3337 result
.set_len (shifted_mask (result
.write_val (), start
, width
, negate_p
,
3342 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3343 others are clear. */
3345 wi::set_bit_in_zero (unsigned int bit
, unsigned int precision
)
3347 return shifted_mask (bit
, 1, false, precision
);
3350 /* Return an integer of type T in which the low WIDTH bits are set
3351 and the other bits are clear, or the inverse if NEGATE_P. */
3352 template <typename T
>
3354 wi::mask (unsigned int width
, bool negate_p
)
3356 STATIC_ASSERT (wi::int_traits
<T
>::precision
);
3358 result
.set_len (mask (result
.write_val (), width
, negate_p
,
3359 wi::int_traits
<T
>::precision
));
3363 /* Return an integer of type T in which the low START bits are clear,
3364 the next WIDTH bits are set, and the other bits are clear, or the
3365 inverse if NEGATE_P. */
3366 template <typename T
>
3368 wi::shifted_mask (unsigned int start
, unsigned int width
, bool negate_p
)
3370 STATIC_ASSERT (wi::int_traits
<T
>::precision
);
3372 result
.set_len (shifted_mask (result
.write_val (), start
, width
,
3374 wi::int_traits
<T
>::precision
));
3378 /* Return an integer of type T in which bit BIT is set and all the
3379 others are clear. */
3380 template <typename T
>
3382 wi::set_bit_in_zero (unsigned int bit
)
3384 return shifted_mask
<T
> (bit
, 1, false);
3387 #endif /* WIDE_INT_H */