]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/wide-int.h
* doc/install.texi (Specific, alpha): Remove note to use
[thirdparty/gcc.git] / gcc / wide-int.h
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
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/>. */
19
20 #ifndef WIDE_INT_H
21 #define WIDE_INT_H
22
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).
28
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
31
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.
40
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.
44
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.
48
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.
55
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.
64
65 The extra sign bit means that offset_int is effectively a signed
66 128-bit integer, i.e. it behaves like int128_t.
67
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.
73
74 [ Note that, even though offset_int is effectively int128_t,
75 it can still be useful to use unsigned comparisons like
76 wi::leu_p (a, b) as a more efficient short-hand for
77 "a >= 0 && a <= b". ]
78
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.
84
85 Like offset_int, widest_int is wider than all the values that
86 it needs to represent, so the integers are logically signed.
87 Sign-sensitive comparison operators <, <=, > and >= are supported,
88 as are << and >>.
89
90 There are several places in the GCC where this should/must be used:
91
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,
98
99 * There are a small number of passes that currently use the
100 widest_int that should use the default. These should be
101 changed.
102
103 There are surprising features of offset_int and widest_int
104 that the users should be careful about:
105
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.
110
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.
116
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.
120
121 All three flavors of wide_int are represented as a vector of
122 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
123 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
124 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
125 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
126 in element 0.
127
128 The default wide_int contains three fields: the vector (VAL),
129 the precision and a length (LEN). The length is the number of HWIs
130 needed to represent the value. widest_int and offset_int have a
131 constant precision that cannot be changed, so they only store the
132 VAL and LEN fields.
133
134 Since most integers used in a compiler are small values, it is
135 generally profitable to use a representation of the value that is
136 as small as possible. LEN is used to indicate the number of
137 elements of the vector that are in use. The numbers are stored as
138 sign extended numbers as a means of compression. Leading
139 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
140 as long as they can be reconstructed from the top bit that is being
141 represented.
142
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.
151
152 There are constructors to create the various forms of wide_int from
153 trees, rtl and constants. For trees the options are:
154
155 tree t = ...;
156 wi::to_wide (t) // Treat T as a wide_int
157 wi::to_offset (t) // Treat T as an offset_int
158 wi::to_widest (t) // Treat T as a widest_int
159
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:
163
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);
167
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:
170
171 rtx r = ...
172 wide_int x = rtx_mode_t (r, mode);
173
174 Similarly, a wide_int can only be constructed from a host value if
175 the target precision is given explicitly, such as in:
176
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
179
180 However, offset_int and widest_int have an inherent precision and so
181 can be initialized directly from a host value:
182
183 offset_int x = (int) c; // sign-extend C
184 widest_int x = (unsigned int) c; // zero-extend C
185
186 It is also possible to do arithmetic directly on rtx_mode_ts and
187 constants. For example:
188
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
192
193 Many binary operations place restrictions on the combinations of inputs,
194 using the following rules:
195
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
199
200 - {rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
201 (un)signed HOST_WIDE_INT op {rtx, wide_int} -> wide_int
202 The HOST_WIDE_INT is extended or truncated to the precision of
203 the other input. The result is a wide_int of the same precision
204 as that input.
205
206 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
207 The inputs are extended to widest_int precision and produce a
208 widest_int result.
209
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
213
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
217
218 Other combinations like:
219
220 - widest_int op offset_int and
221 - wide_int op offset_int
222
223 are not allowed. The inputs should instead be extended or truncated
224 so that they match.
225
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. */
232
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)
242
243 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
244
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
254
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))
263
264 /* The number of HWIs needed to store an offset_int. */
265 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
266
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
271
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
276
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
281
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
287
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
293
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
297
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 ()
307
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 ()
313
314 template <typename T> class generic_wide_int;
315 template <int N> class fixed_wide_int_storage;
316 class wide_int_storage;
317
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> >
321
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;
325 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
326 so as not to confuse gengtype. */
327 typedef generic_wide_int < fixed_wide_int_storage <WIDE_INT_MAX_PRECISION * 2> > widest2_int;
328
329 /* wi::storage_ref can be a reference to a primitive type,
330 so this is the conservatively-correct setting. */
331 template <bool SE, bool HDP = true>
332 class wide_int_ref_storage;
333
334 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
335
336 /* This can be used instead of wide_int_ref if the referenced value is
337 known to have type T. It carries across properties of T's representation,
338 such as whether excess upper bits in a HWI are defined, and can therefore
339 help avoid redundant work.
340
341 The macro could be replaced with a template typedef, once we're able
342 to use those. */
343 #define WIDE_INT_REF_FOR(T) \
344 generic_wide_int \
345 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
346 wi::int_traits <T>::host_dependent_precision> >
347
348 namespace wi
349 {
350 /* Operations that calculate overflow do so even for
351 TYPE_OVERFLOW_WRAPS types. For example, adding 1 to +MAX_INT in
352 an unsigned int is 0 and does not overflow in C/C++, but wi::add
353 will set the overflow argument in case it's needed for further
354 analysis.
355
356 For operations that require overflow, these are the different
357 types of overflow. */
358 enum overflow_type {
359 OVF_NONE = 0,
360 OVF_UNDERFLOW = -1,
361 OVF_OVERFLOW = 1,
362 /* There was an overflow, but we are unsure whether it was an
363 overflow or an underflow. */
364 OVF_UNKNOWN = 2
365 };
366
367 /* Classifies an integer based on its precision. */
368 enum precision_type {
369 /* The integer has both a precision and defined signedness. This allows
370 the integer to be converted to any width, since we know whether to fill
371 any extra bits with zeros or signs. */
372 FLEXIBLE_PRECISION,
373
374 /* The integer has a variable precision but no defined signedness. */
375 VAR_PRECISION,
376
377 /* The integer has a constant precision (known at GCC compile time)
378 and is signed. */
379 CONST_PRECISION
380 };
381
382 /* This class, which has no default implementation, is expected to
383 provide the following members:
384
385 static const enum precision_type precision_type;
386 Classifies the type of T.
387
388 static const unsigned int precision;
389 Only defined if precision_type == CONST_PRECISION. Specifies the
390 precision of all integers of type T.
391
392 static const bool host_dependent_precision;
393 True if the precision of T depends (or can depend) on the host.
394
395 static unsigned int get_precision (const T &x)
396 Return the number of bits in X.
397
398 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
399 unsigned int precision, const T &x)
400 Decompose X as a PRECISION-bit integer, returning the associated
401 wi::storage_ref. SCRATCH is available as scratch space if needed.
402 The routine should assert that PRECISION is acceptable. */
403 template <typename T> struct int_traits;
404
405 /* This class provides a single type, result_type, which specifies the
406 type of integer produced by a binary operation whose inputs have
407 types T1 and T2. The definition should be symmetric. */
408 template <typename T1, typename T2,
409 enum precision_type P1 = int_traits <T1>::precision_type,
410 enum precision_type P2 = int_traits <T2>::precision_type>
411 struct binary_traits;
412
413 /* Specify the result type for each supported combination of binary
414 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
415 mixed, in order to give stronger type checking. When both inputs
416 are CONST_PRECISION, they must have the same precision. */
417 template <typename T1, typename T2>
418 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
419 {
420 typedef widest_int result_type;
421 /* Don't define operators for this combination. */
422 };
423
424 template <typename T1, typename T2>
425 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
426 {
427 typedef wide_int result_type;
428 typedef result_type operator_result;
429 typedef bool predicate_result;
430 };
431
432 template <typename T1, typename T2>
433 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
434 {
435 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
436 so as not to confuse gengtype. */
437 typedef generic_wide_int < fixed_wide_int_storage
438 <int_traits <T2>::precision> > result_type;
439 typedef result_type operator_result;
440 typedef bool predicate_result;
441 typedef result_type signed_shift_result_type;
442 typedef bool signed_predicate_result;
443 };
444
445 template <typename T1, typename T2>
446 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
447 {
448 typedef wide_int result_type;
449 typedef result_type operator_result;
450 typedef bool predicate_result;
451 };
452
453 template <typename T1, typename T2>
454 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
455 {
456 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
457 so as not to confuse gengtype. */
458 typedef generic_wide_int < fixed_wide_int_storage
459 <int_traits <T1>::precision> > result_type;
460 typedef result_type operator_result;
461 typedef bool predicate_result;
462 typedef result_type signed_shift_result_type;
463 typedef bool signed_predicate_result;
464 };
465
466 template <typename T1, typename T2>
467 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
468 {
469 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
470 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
471 so as not to confuse gengtype. */
472 typedef generic_wide_int < fixed_wide_int_storage
473 <int_traits <T1>::precision> > result_type;
474 typedef result_type operator_result;
475 typedef bool predicate_result;
476 typedef result_type signed_shift_result_type;
477 typedef bool signed_predicate_result;
478 };
479
480 template <typename T1, typename T2>
481 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
482 {
483 typedef wide_int result_type;
484 typedef result_type operator_result;
485 typedef bool predicate_result;
486 };
487 }
488
489 /* Public functions for querying and operating on integers. */
490 namespace wi
491 {
492 template <typename T>
493 unsigned int get_precision (const T &);
494
495 template <typename T1, typename T2>
496 unsigned int get_binary_precision (const T1 &, const T2 &);
497
498 template <typename T1, typename T2>
499 void copy (T1 &, const T2 &);
500
501 #define UNARY_PREDICATE \
502 template <typename T> bool
503 #define UNARY_FUNCTION \
504 template <typename T> WI_UNARY_RESULT (T)
505 #define BINARY_PREDICATE \
506 template <typename T1, typename T2> bool
507 #define BINARY_FUNCTION \
508 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
509 #define SHIFT_FUNCTION \
510 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
511
512 UNARY_PREDICATE fits_shwi_p (const T &);
513 UNARY_PREDICATE fits_uhwi_p (const T &);
514 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
515
516 template <typename T>
517 HOST_WIDE_INT sign_mask (const T &);
518
519 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
520 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
521 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
522 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
523 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
524 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
525 BINARY_PREDICATE les_p (const T1 &, const T2 &);
526 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
527 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
528 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
529 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
530 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
531 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
532 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
533
534 template <typename T1, typename T2>
535 int cmp (const T1 &, const T2 &, signop);
536
537 template <typename T1, typename T2>
538 int cmps (const T1 &, const T2 &);
539
540 template <typename T1, typename T2>
541 int cmpu (const T1 &, const T2 &);
542
543 UNARY_FUNCTION bit_not (const T &);
544 UNARY_FUNCTION neg (const T &);
545 UNARY_FUNCTION neg (const T &, overflow_type *);
546 UNARY_FUNCTION abs (const T &);
547 UNARY_FUNCTION ext (const T &, unsigned int, signop);
548 UNARY_FUNCTION sext (const T &, unsigned int);
549 UNARY_FUNCTION zext (const T &, unsigned int);
550 UNARY_FUNCTION set_bit (const T &, unsigned int);
551
552 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
553 BINARY_FUNCTION smin (const T1 &, const T2 &);
554 BINARY_FUNCTION umin (const T1 &, const T2 &);
555 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
556 BINARY_FUNCTION smax (const T1 &, const T2 &);
557 BINARY_FUNCTION umax (const T1 &, const T2 &);
558
559 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
560 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
561 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
562 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
563 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
564 BINARY_FUNCTION add (const T1 &, const T2 &);
565 BINARY_FUNCTION add (const T1 &, const T2 &, signop, overflow_type *);
566 BINARY_FUNCTION sub (const T1 &, const T2 &);
567 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, overflow_type *);
568 BINARY_FUNCTION mul (const T1 &, const T2 &);
569 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, overflow_type *);
570 BINARY_FUNCTION smul (const T1 &, const T2 &, overflow_type *);
571 BINARY_FUNCTION umul (const T1 &, const T2 &, overflow_type *);
572 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
573 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop,
574 overflow_type * = 0);
575 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
576 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
577 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop,
578 overflow_type * = 0);
579 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
580 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
581 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop,
582 overflow_type * = 0);
583 BINARY_FUNCTION udiv_ceil (const T1 &, const T2 &);
584 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop,
585 overflow_type * = 0);
586 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
587 WI_BINARY_RESULT (T1, T2) *);
588 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
589 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop,
590 overflow_type * = 0);
591 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
592 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
593 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop,
594 overflow_type * = 0);
595 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
596 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop,
597 overflow_type * = 0);
598 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop,
599 overflow_type * = 0);
600
601 template <typename T1, typename T2>
602 bool multiple_of_p (const T1 &, const T2 &, signop);
603
604 template <typename T1, typename T2>
605 bool multiple_of_p (const T1 &, const T2 &, signop,
606 WI_BINARY_RESULT (T1, T2) *);
607
608 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
609 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
610 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
611 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
612 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
613 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
614
615 #undef SHIFT_FUNCTION
616 #undef BINARY_PREDICATE
617 #undef BINARY_FUNCTION
618 #undef UNARY_PREDICATE
619 #undef UNARY_FUNCTION
620
621 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
622 bool only_sign_bit_p (const wide_int_ref &);
623 int clz (const wide_int_ref &);
624 int clrsb (const wide_int_ref &);
625 int ctz (const wide_int_ref &);
626 int exact_log2 (const wide_int_ref &);
627 int floor_log2 (const wide_int_ref &);
628 int ffs (const wide_int_ref &);
629 int popcount (const wide_int_ref &);
630 int parity (const wide_int_ref &);
631
632 template <typename T>
633 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
634
635 template <typename T>
636 unsigned int min_precision (const T &, signop);
637
638 static inline void accumulate_overflow (overflow_type &, overflow_type);
639 }
640
641 namespace wi
642 {
643 /* Contains the components of a decomposed integer for easy, direct
644 access. */
645 class storage_ref
646 {
647 public:
648 storage_ref () {}
649 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
650
651 const HOST_WIDE_INT *val;
652 unsigned int len;
653 unsigned int precision;
654
655 /* Provide enough trappings for this class to act as storage for
656 generic_wide_int. */
657 unsigned int get_len () const;
658 unsigned int get_precision () const;
659 const HOST_WIDE_INT *get_val () const;
660 };
661 }
662
663 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
664 unsigned int len_in,
665 unsigned int precision_in)
666 : val (val_in), len (len_in), precision (precision_in)
667 {
668 }
669
670 inline unsigned int
671 wi::storage_ref::get_len () const
672 {
673 return len;
674 }
675
676 inline unsigned int
677 wi::storage_ref::get_precision () const
678 {
679 return precision;
680 }
681
682 inline const HOST_WIDE_INT *
683 wi::storage_ref::get_val () const
684 {
685 return val;
686 }
687
688 /* This class defines an integer type using the storage provided by the
689 template argument. The storage class must provide the following
690 functions:
691
692 unsigned int get_precision () const
693 Return the number of bits in the integer.
694
695 HOST_WIDE_INT *get_val () const
696 Return a pointer to the array of blocks that encodes the integer.
697
698 unsigned int get_len () const
699 Return the number of blocks in get_val (). If this is smaller
700 than the number of blocks implied by get_precision (), the
701 remaining blocks are sign extensions of block get_len () - 1.
702
703 Although not required by generic_wide_int itself, writable storage
704 classes can also provide the following functions:
705
706 HOST_WIDE_INT *write_val ()
707 Get a modifiable version of get_val ()
708
709 unsigned int set_len (unsigned int len)
710 Set the value returned by get_len () to LEN. */
711 template <typename storage>
712 class GTY(()) generic_wide_int : public storage
713 {
714 public:
715 generic_wide_int ();
716
717 template <typename T>
718 generic_wide_int (const T &);
719
720 template <typename T>
721 generic_wide_int (const T &, unsigned int);
722
723 /* Conversions. */
724 HOST_WIDE_INT to_shwi (unsigned int) const;
725 HOST_WIDE_INT to_shwi () const;
726 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
727 unsigned HOST_WIDE_INT to_uhwi () const;
728 HOST_WIDE_INT to_short_addr () const;
729
730 /* Public accessors for the interior of a wide int. */
731 HOST_WIDE_INT sign_mask () const;
732 HOST_WIDE_INT elt (unsigned int) const;
733 HOST_WIDE_INT sext_elt (unsigned int) const;
734 unsigned HOST_WIDE_INT ulow () const;
735 unsigned HOST_WIDE_INT uhigh () const;
736 HOST_WIDE_INT slow () const;
737 HOST_WIDE_INT shigh () const;
738
739 template <typename T>
740 generic_wide_int &operator = (const T &);
741
742 #define ASSIGNMENT_OPERATOR(OP, F) \
743 template <typename T> \
744 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
745
746 /* Restrict these to cases where the shift operator is defined. */
747 #define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
748 template <typename T> \
749 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
750
751 #define INCDEC_OPERATOR(OP, DELTA) \
752 generic_wide_int &OP () { *this += DELTA; return *this; }
753
754 ASSIGNMENT_OPERATOR (operator &=, bit_and)
755 ASSIGNMENT_OPERATOR (operator |=, bit_or)
756 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
757 ASSIGNMENT_OPERATOR (operator +=, add)
758 ASSIGNMENT_OPERATOR (operator -=, sub)
759 ASSIGNMENT_OPERATOR (operator *=, mul)
760 ASSIGNMENT_OPERATOR (operator <<=, lshift)
761 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
762 INCDEC_OPERATOR (operator ++, 1)
763 INCDEC_OPERATOR (operator --, -1)
764
765 #undef SHIFT_ASSIGNMENT_OPERATOR
766 #undef ASSIGNMENT_OPERATOR
767 #undef INCDEC_OPERATOR
768
769 /* Debugging functions. */
770 void dump () const;
771
772 static const bool is_sign_extended
773 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
774 };
775
776 template <typename storage>
777 inline generic_wide_int <storage>::generic_wide_int () {}
778
779 template <typename storage>
780 template <typename T>
781 inline generic_wide_int <storage>::generic_wide_int (const T &x)
782 : storage (x)
783 {
784 }
785
786 template <typename storage>
787 template <typename T>
788 inline generic_wide_int <storage>::generic_wide_int (const T &x,
789 unsigned int precision)
790 : storage (x, precision)
791 {
792 }
793
794 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
795 If THIS does not fit in PRECISION, the information is lost. */
796 template <typename storage>
797 inline HOST_WIDE_INT
798 generic_wide_int <storage>::to_shwi (unsigned int precision) const
799 {
800 if (precision < HOST_BITS_PER_WIDE_INT)
801 return sext_hwi (this->get_val ()[0], precision);
802 else
803 return this->get_val ()[0];
804 }
805
806 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
807 template <typename storage>
808 inline HOST_WIDE_INT
809 generic_wide_int <storage>::to_shwi () const
810 {
811 if (is_sign_extended)
812 return this->get_val ()[0];
813 else
814 return to_shwi (this->get_precision ());
815 }
816
817 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
818 PRECISION. If THIS does not fit in PRECISION, the information
819 is lost. */
820 template <typename storage>
821 inline unsigned HOST_WIDE_INT
822 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
823 {
824 if (precision < HOST_BITS_PER_WIDE_INT)
825 return zext_hwi (this->get_val ()[0], precision);
826 else
827 return this->get_val ()[0];
828 }
829
830 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
831 template <typename storage>
832 inline unsigned HOST_WIDE_INT
833 generic_wide_int <storage>::to_uhwi () const
834 {
835 return to_uhwi (this->get_precision ());
836 }
837
838 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
839 represent addresses to using offset_int to represent addresses.
840 We use to_short_addr at the interface from new code to old,
841 unconverted code. */
842 template <typename storage>
843 inline HOST_WIDE_INT
844 generic_wide_int <storage>::to_short_addr () const
845 {
846 return this->get_val ()[0];
847 }
848
849 /* Return the implicit value of blocks above get_len (). */
850 template <typename storage>
851 inline HOST_WIDE_INT
852 generic_wide_int <storage>::sign_mask () const
853 {
854 unsigned int len = this->get_len ();
855 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
856 if (!is_sign_extended)
857 {
858 unsigned int precision = this->get_precision ();
859 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
860 if (excess > 0)
861 high <<= excess;
862 }
863 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
864 }
865
866 /* Return the signed value of the least-significant explicitly-encoded
867 block. */
868 template <typename storage>
869 inline HOST_WIDE_INT
870 generic_wide_int <storage>::slow () const
871 {
872 return this->get_val ()[0];
873 }
874
875 /* Return the signed value of the most-significant explicitly-encoded
876 block. */
877 template <typename storage>
878 inline HOST_WIDE_INT
879 generic_wide_int <storage>::shigh () const
880 {
881 return this->get_val ()[this->get_len () - 1];
882 }
883
884 /* Return the unsigned value of the least-significant
885 explicitly-encoded block. */
886 template <typename storage>
887 inline unsigned HOST_WIDE_INT
888 generic_wide_int <storage>::ulow () const
889 {
890 return this->get_val ()[0];
891 }
892
893 /* Return the unsigned value of the most-significant
894 explicitly-encoded block. */
895 template <typename storage>
896 inline unsigned HOST_WIDE_INT
897 generic_wide_int <storage>::uhigh () const
898 {
899 return this->get_val ()[this->get_len () - 1];
900 }
901
902 /* Return block I, which might be implicitly or explicit encoded. */
903 template <typename storage>
904 inline HOST_WIDE_INT
905 generic_wide_int <storage>::elt (unsigned int i) const
906 {
907 if (i >= this->get_len ())
908 return sign_mask ();
909 else
910 return this->get_val ()[i];
911 }
912
913 /* Like elt, but sign-extend beyond the upper bit, instead of returning
914 the raw encoding. */
915 template <typename storage>
916 inline HOST_WIDE_INT
917 generic_wide_int <storage>::sext_elt (unsigned int i) const
918 {
919 HOST_WIDE_INT elt_i = elt (i);
920 if (!is_sign_extended)
921 {
922 unsigned int precision = this->get_precision ();
923 unsigned int lsb = i * HOST_BITS_PER_WIDE_INT;
924 if (precision - lsb < HOST_BITS_PER_WIDE_INT)
925 elt_i = sext_hwi (elt_i, precision - lsb);
926 }
927 return elt_i;
928 }
929
930 template <typename storage>
931 template <typename T>
932 inline generic_wide_int <storage> &
933 generic_wide_int <storage>::operator = (const T &x)
934 {
935 storage::operator = (x);
936 return *this;
937 }
938
939 /* Dump the contents of the integer to stderr, for debugging. */
940 template <typename storage>
941 void
942 generic_wide_int <storage>::dump () const
943 {
944 unsigned int len = this->get_len ();
945 const HOST_WIDE_INT *val = this->get_val ();
946 unsigned int precision = this->get_precision ();
947 fprintf (stderr, "[");
948 if (len * HOST_BITS_PER_WIDE_INT < precision)
949 fprintf (stderr, "...,");
950 for (unsigned int i = 0; i < len - 1; ++i)
951 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
952 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
953 val[0], precision);
954 }
955
956 namespace wi
957 {
958 template <typename storage>
959 struct int_traits < generic_wide_int <storage> >
960 : public wi::int_traits <storage>
961 {
962 static unsigned int get_precision (const generic_wide_int <storage> &);
963 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
964 const generic_wide_int <storage> &);
965 };
966 }
967
968 template <typename storage>
969 inline unsigned int
970 wi::int_traits < generic_wide_int <storage> >::
971 get_precision (const generic_wide_int <storage> &x)
972 {
973 return x.get_precision ();
974 }
975
976 template <typename storage>
977 inline wi::storage_ref
978 wi::int_traits < generic_wide_int <storage> >::
979 decompose (HOST_WIDE_INT *, unsigned int precision,
980 const generic_wide_int <storage> &x)
981 {
982 gcc_checking_assert (precision == x.get_precision ());
983 return wi::storage_ref (x.get_val (), x.get_len (), precision);
984 }
985
986 /* Provide the storage for a wide_int_ref. This acts like a read-only
987 wide_int, with the optimization that VAL is normally a pointer to
988 another integer's storage, so that no array copy is needed. */
989 template <bool SE, bool HDP>
990 class wide_int_ref_storage : public wi::storage_ref
991 {
992 private:
993 /* Scratch space that can be used when decomposing the original integer.
994 It must live as long as this object. */
995 HOST_WIDE_INT scratch[2];
996
997 public:
998 wide_int_ref_storage () {}
999
1000 wide_int_ref_storage (const wi::storage_ref &);
1001
1002 template <typename T>
1003 wide_int_ref_storage (const T &);
1004
1005 template <typename T>
1006 wide_int_ref_storage (const T &, unsigned int);
1007 };
1008
1009 /* Create a reference from an existing reference. */
1010 template <bool SE, bool HDP>
1011 inline wide_int_ref_storage <SE, HDP>::
1012 wide_int_ref_storage (const wi::storage_ref &x)
1013 : storage_ref (x)
1014 {}
1015
1016 /* Create a reference to integer X in its natural precision. Note
1017 that the natural precision is host-dependent for primitive
1018 types. */
1019 template <bool SE, bool HDP>
1020 template <typename T>
1021 inline wide_int_ref_storage <SE, HDP>::wide_int_ref_storage (const T &x)
1022 : storage_ref (wi::int_traits <T>::decompose (scratch,
1023 wi::get_precision (x), x))
1024 {
1025 }
1026
1027 /* Create a reference to integer X in precision PRECISION. */
1028 template <bool SE, bool HDP>
1029 template <typename T>
1030 inline wide_int_ref_storage <SE, HDP>::
1031 wide_int_ref_storage (const T &x, unsigned int precision)
1032 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
1033 {
1034 }
1035
1036 namespace wi
1037 {
1038 template <bool SE, bool HDP>
1039 struct int_traits <wide_int_ref_storage <SE, HDP> >
1040 {
1041 static const enum precision_type precision_type = VAR_PRECISION;
1042 static const bool host_dependent_precision = HDP;
1043 static const bool is_sign_extended = SE;
1044 };
1045 }
1046
1047 namespace wi
1048 {
1049 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1050 unsigned int, unsigned int, unsigned int,
1051 signop sgn);
1052 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1053 unsigned int, unsigned int, bool = true);
1054 }
1055
1056 /* The storage used by wide_int. */
1057 class GTY(()) wide_int_storage
1058 {
1059 private:
1060 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
1061 unsigned int len;
1062 unsigned int precision;
1063
1064 public:
1065 wide_int_storage ();
1066 template <typename T>
1067 wide_int_storage (const T &);
1068
1069 /* The standard generic_wide_int storage methods. */
1070 unsigned int get_precision () const;
1071 const HOST_WIDE_INT *get_val () const;
1072 unsigned int get_len () const;
1073 HOST_WIDE_INT *write_val ();
1074 void set_len (unsigned int, bool = false);
1075
1076 template <typename T>
1077 wide_int_storage &operator = (const T &);
1078
1079 static wide_int from (const wide_int_ref &, unsigned int, signop);
1080 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1081 unsigned int, bool = true);
1082 static wide_int create (unsigned int);
1083
1084 /* FIXME: target-dependent, so should disappear. */
1085 wide_int bswap () const;
1086 };
1087
1088 namespace wi
1089 {
1090 template <>
1091 struct int_traits <wide_int_storage>
1092 {
1093 static const enum precision_type precision_type = VAR_PRECISION;
1094 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1095 static const bool host_dependent_precision = false;
1096 static const bool is_sign_extended = true;
1097 template <typename T1, typename T2>
1098 static wide_int get_binary_result (const T1 &, const T2 &);
1099 };
1100 }
1101
1102 inline wide_int_storage::wide_int_storage () {}
1103
1104 /* Initialize the storage from integer X, in its natural precision.
1105 Note that we do not allow integers with host-dependent precision
1106 to become wide_ints; wide_ints must always be logically independent
1107 of the host. */
1108 template <typename T>
1109 inline wide_int_storage::wide_int_storage (const T &x)
1110 {
1111 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1112 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1113 WIDE_INT_REF_FOR (T) xi (x);
1114 precision = xi.precision;
1115 wi::copy (*this, xi);
1116 }
1117
1118 template <typename T>
1119 inline wide_int_storage&
1120 wide_int_storage::operator = (const T &x)
1121 {
1122 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1123 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1124 WIDE_INT_REF_FOR (T) xi (x);
1125 precision = xi.precision;
1126 wi::copy (*this, xi);
1127 return *this;
1128 }
1129
1130 inline unsigned int
1131 wide_int_storage::get_precision () const
1132 {
1133 return precision;
1134 }
1135
1136 inline const HOST_WIDE_INT *
1137 wide_int_storage::get_val () const
1138 {
1139 return val;
1140 }
1141
1142 inline unsigned int
1143 wide_int_storage::get_len () const
1144 {
1145 return len;
1146 }
1147
1148 inline HOST_WIDE_INT *
1149 wide_int_storage::write_val ()
1150 {
1151 return val;
1152 }
1153
1154 inline void
1155 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1156 {
1157 len = l;
1158 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1159 val[len - 1] = sext_hwi (val[len - 1],
1160 precision % HOST_BITS_PER_WIDE_INT);
1161 }
1162
1163 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1164 number. */
1165 inline wide_int
1166 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1167 signop sgn)
1168 {
1169 wide_int result = wide_int::create (precision);
1170 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1171 x.precision, precision, sgn));
1172 return result;
1173 }
1174
1175 /* Create a wide_int from the explicit block encoding given by VAL and
1176 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1177 true if the encoding may have redundant trailing blocks. */
1178 inline wide_int
1179 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1180 unsigned int precision, bool need_canon_p)
1181 {
1182 wide_int result = wide_int::create (precision);
1183 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1184 need_canon_p));
1185 return result;
1186 }
1187
1188 /* Return an uninitialized wide_int with precision PRECISION. */
1189 inline wide_int
1190 wide_int_storage::create (unsigned int precision)
1191 {
1192 wide_int x;
1193 x.precision = precision;
1194 return x;
1195 }
1196
1197 template <typename T1, typename T2>
1198 inline wide_int
1199 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1200 {
1201 /* This shouldn't be used for two flexible-precision inputs. */
1202 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1203 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1204 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1205 return wide_int::create (wi::get_precision (y));
1206 else
1207 return wide_int::create (wi::get_precision (x));
1208 }
1209
1210 /* The storage used by FIXED_WIDE_INT (N). */
1211 template <int N>
1212 class GTY(()) fixed_wide_int_storage
1213 {
1214 private:
1215 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1216 unsigned int len;
1217
1218 public:
1219 fixed_wide_int_storage ();
1220 template <typename T>
1221 fixed_wide_int_storage (const T &);
1222
1223 /* The standard generic_wide_int storage methods. */
1224 unsigned int get_precision () const;
1225 const HOST_WIDE_INT *get_val () const;
1226 unsigned int get_len () const;
1227 HOST_WIDE_INT *write_val ();
1228 void set_len (unsigned int, bool = false);
1229
1230 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1231 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1232 bool = true);
1233 };
1234
1235 namespace wi
1236 {
1237 template <int N>
1238 struct int_traits < fixed_wide_int_storage <N> >
1239 {
1240 static const enum precision_type precision_type = CONST_PRECISION;
1241 static const bool host_dependent_precision = false;
1242 static const bool is_sign_extended = true;
1243 static const unsigned int precision = N;
1244 template <typename T1, typename T2>
1245 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1246 };
1247 }
1248
1249 template <int N>
1250 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1251
1252 /* Initialize the storage from integer X, in precision N. */
1253 template <int N>
1254 template <typename T>
1255 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1256 {
1257 /* Check for type compatibility. We don't want to initialize a
1258 fixed-width integer from something like a wide_int. */
1259 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1260 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1261 }
1262
1263 template <int N>
1264 inline unsigned int
1265 fixed_wide_int_storage <N>::get_precision () const
1266 {
1267 return N;
1268 }
1269
1270 template <int N>
1271 inline const HOST_WIDE_INT *
1272 fixed_wide_int_storage <N>::get_val () const
1273 {
1274 return val;
1275 }
1276
1277 template <int N>
1278 inline unsigned int
1279 fixed_wide_int_storage <N>::get_len () const
1280 {
1281 return len;
1282 }
1283
1284 template <int N>
1285 inline HOST_WIDE_INT *
1286 fixed_wide_int_storage <N>::write_val ()
1287 {
1288 return val;
1289 }
1290
1291 template <int N>
1292 inline void
1293 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1294 {
1295 len = l;
1296 /* There are no excess bits in val[len - 1]. */
1297 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1298 }
1299
1300 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1301 template <int N>
1302 inline FIXED_WIDE_INT (N)
1303 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1304 {
1305 FIXED_WIDE_INT (N) result;
1306 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1307 x.precision, N, sgn));
1308 return result;
1309 }
1310
1311 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1312 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1313 trailing blocks. */
1314 template <int N>
1315 inline FIXED_WIDE_INT (N)
1316 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1317 unsigned int len,
1318 bool need_canon_p)
1319 {
1320 FIXED_WIDE_INT (N) result;
1321 result.set_len (wi::from_array (result.write_val (), val, len,
1322 N, need_canon_p));
1323 return result;
1324 }
1325
1326 template <int N>
1327 template <typename T1, typename T2>
1328 inline FIXED_WIDE_INT (N)
1329 wi::int_traits < fixed_wide_int_storage <N> >::
1330 get_binary_result (const T1 &, const T2 &)
1331 {
1332 return FIXED_WIDE_INT (N) ();
1333 }
1334
1335 /* A reference to one element of a trailing_wide_ints structure. */
1336 class trailing_wide_int_storage
1337 {
1338 private:
1339 /* The precision of the integer, which is a fixed property of the
1340 parent trailing_wide_ints. */
1341 unsigned int m_precision;
1342
1343 /* A pointer to the length field. */
1344 unsigned char *m_len;
1345
1346 /* A pointer to the HWI array. There are enough elements to hold all
1347 values of precision M_PRECISION. */
1348 HOST_WIDE_INT *m_val;
1349
1350 public:
1351 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1352
1353 /* The standard generic_wide_int storage methods. */
1354 unsigned int get_len () const;
1355 unsigned int get_precision () const;
1356 const HOST_WIDE_INT *get_val () const;
1357 HOST_WIDE_INT *write_val ();
1358 void set_len (unsigned int, bool = false);
1359
1360 template <typename T>
1361 trailing_wide_int_storage &operator = (const T &);
1362 };
1363
1364 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1365
1366 /* trailing_wide_int behaves like a wide_int. */
1367 namespace wi
1368 {
1369 template <>
1370 struct int_traits <trailing_wide_int_storage>
1371 : public int_traits <wide_int_storage> {};
1372 }
1373
1374 /* An array of N wide_int-like objects that can be put at the end of
1375 a variable-sized structure. Use extra_size to calculate how many
1376 bytes beyond the sizeof need to be allocated. Use set_precision
1377 to initialize the structure. */
1378 template <int N>
1379 struct GTY((user)) trailing_wide_ints
1380 {
1381 private:
1382 /* The shared precision of each number. */
1383 unsigned short m_precision;
1384
1385 /* The shared maximum length of each number. */
1386 unsigned char m_max_len;
1387
1388 /* The current length of each number. */
1389 unsigned char m_len[N];
1390
1391 /* The variable-length part of the structure, which always contains
1392 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1393 HOST_WIDE_INT m_val[1];
1394
1395 public:
1396 typedef WIDE_INT_REF_FOR (trailing_wide_int_storage) const_reference;
1397
1398 void set_precision (unsigned int);
1399 unsigned int get_precision () const { return m_precision; }
1400 trailing_wide_int operator [] (unsigned int);
1401 const_reference operator [] (unsigned int) const;
1402 static size_t extra_size (unsigned int);
1403 size_t extra_size () const { return extra_size (m_precision); }
1404 };
1405
1406 inline trailing_wide_int_storage::
1407 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1408 HOST_WIDE_INT *val)
1409 : m_precision (precision), m_len (len), m_val (val)
1410 {
1411 }
1412
1413 inline unsigned int
1414 trailing_wide_int_storage::get_len () const
1415 {
1416 return *m_len;
1417 }
1418
1419 inline unsigned int
1420 trailing_wide_int_storage::get_precision () const
1421 {
1422 return m_precision;
1423 }
1424
1425 inline const HOST_WIDE_INT *
1426 trailing_wide_int_storage::get_val () const
1427 {
1428 return m_val;
1429 }
1430
1431 inline HOST_WIDE_INT *
1432 trailing_wide_int_storage::write_val ()
1433 {
1434 return m_val;
1435 }
1436
1437 inline void
1438 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1439 {
1440 *m_len = len;
1441 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1442 m_val[len - 1] = sext_hwi (m_val[len - 1],
1443 m_precision % HOST_BITS_PER_WIDE_INT);
1444 }
1445
1446 template <typename T>
1447 inline trailing_wide_int_storage &
1448 trailing_wide_int_storage::operator = (const T &x)
1449 {
1450 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1451 wi::copy (*this, xi);
1452 return *this;
1453 }
1454
1455 /* Initialize the structure and record that all elements have precision
1456 PRECISION. */
1457 template <int N>
1458 inline void
1459 trailing_wide_ints <N>::set_precision (unsigned int precision)
1460 {
1461 m_precision = precision;
1462 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1463 / HOST_BITS_PER_WIDE_INT);
1464 }
1465
1466 /* Return a reference to element INDEX. */
1467 template <int N>
1468 inline trailing_wide_int
1469 trailing_wide_ints <N>::operator [] (unsigned int index)
1470 {
1471 return trailing_wide_int_storage (m_precision, &m_len[index],
1472 &m_val[index * m_max_len]);
1473 }
1474
1475 template <int N>
1476 inline typename trailing_wide_ints <N>::const_reference
1477 trailing_wide_ints <N>::operator [] (unsigned int index) const
1478 {
1479 return wi::storage_ref (&m_val[index * m_max_len],
1480 m_len[index], m_precision);
1481 }
1482
1483 /* Return how many extra bytes need to be added to the end of the structure
1484 in order to handle N wide_ints of precision PRECISION. */
1485 template <int N>
1486 inline size_t
1487 trailing_wide_ints <N>::extra_size (unsigned int precision)
1488 {
1489 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1490 / HOST_BITS_PER_WIDE_INT);
1491 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1492 }
1493
1494 /* This macro is used in structures that end with a trailing_wide_ints field
1495 called FIELD. It declares get_NAME() and set_NAME() methods to access
1496 element I of FIELD. */
1497 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1498 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1499 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1500
1501 namespace wi
1502 {
1503 /* Implementation of int_traits for primitive integer types like "int". */
1504 template <typename T, bool signed_p>
1505 struct primitive_int_traits
1506 {
1507 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1508 static const bool host_dependent_precision = true;
1509 static const bool is_sign_extended = true;
1510 static unsigned int get_precision (T);
1511 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1512 };
1513 }
1514
1515 template <typename T, bool signed_p>
1516 inline unsigned int
1517 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1518 {
1519 return sizeof (T) * CHAR_BIT;
1520 }
1521
1522 template <typename T, bool signed_p>
1523 inline wi::storage_ref
1524 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1525 unsigned int precision, T x)
1526 {
1527 scratch[0] = x;
1528 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1529 return wi::storage_ref (scratch, 1, precision);
1530 scratch[1] = 0;
1531 return wi::storage_ref (scratch, 2, precision);
1532 }
1533
1534 /* Allow primitive C types to be used in wi:: routines. */
1535 namespace wi
1536 {
1537 template <>
1538 struct int_traits <unsigned char>
1539 : public primitive_int_traits <unsigned char, false> {};
1540
1541 template <>
1542 struct int_traits <unsigned short>
1543 : public primitive_int_traits <unsigned short, false> {};
1544
1545 template <>
1546 struct int_traits <int>
1547 : public primitive_int_traits <int, true> {};
1548
1549 template <>
1550 struct int_traits <unsigned int>
1551 : public primitive_int_traits <unsigned int, false> {};
1552
1553 template <>
1554 struct int_traits <long>
1555 : public primitive_int_traits <long, true> {};
1556
1557 template <>
1558 struct int_traits <unsigned long>
1559 : public primitive_int_traits <unsigned long, false> {};
1560
1561 #if defined HAVE_LONG_LONG
1562 template <>
1563 struct int_traits <long long>
1564 : public primitive_int_traits <long long, true> {};
1565
1566 template <>
1567 struct int_traits <unsigned long long>
1568 : public primitive_int_traits <unsigned long long, false> {};
1569 #endif
1570 }
1571
1572 namespace wi
1573 {
1574 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1575 and precision PRECISION. */
1576 class hwi_with_prec
1577 {
1578 public:
1579 hwi_with_prec () {}
1580 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1581 HOST_WIDE_INT val;
1582 unsigned int precision;
1583 signop sgn;
1584 };
1585
1586 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1587 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1588
1589 hwi_with_prec minus_one (unsigned int);
1590 hwi_with_prec zero (unsigned int);
1591 hwi_with_prec one (unsigned int);
1592 hwi_with_prec two (unsigned int);
1593 }
1594
1595 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1596 signop s)
1597 : precision (p), sgn (s)
1598 {
1599 if (precision < HOST_BITS_PER_WIDE_INT)
1600 val = sext_hwi (v, precision);
1601 else
1602 val = v;
1603 }
1604
1605 /* Return a signed integer that has value VAL and precision PRECISION. */
1606 inline wi::hwi_with_prec
1607 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1608 {
1609 return hwi_with_prec (val, precision, SIGNED);
1610 }
1611
1612 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1613 inline wi::hwi_with_prec
1614 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1615 {
1616 return hwi_with_prec (val, precision, UNSIGNED);
1617 }
1618
1619 /* Return a wide int of -1 with precision PRECISION. */
1620 inline wi::hwi_with_prec
1621 wi::minus_one (unsigned int precision)
1622 {
1623 return wi::shwi (-1, precision);
1624 }
1625
1626 /* Return a wide int of 0 with precision PRECISION. */
1627 inline wi::hwi_with_prec
1628 wi::zero (unsigned int precision)
1629 {
1630 return wi::shwi (0, precision);
1631 }
1632
1633 /* Return a wide int of 1 with precision PRECISION. */
1634 inline wi::hwi_with_prec
1635 wi::one (unsigned int precision)
1636 {
1637 return wi::shwi (1, precision);
1638 }
1639
1640 /* Return a wide int of 2 with precision PRECISION. */
1641 inline wi::hwi_with_prec
1642 wi::two (unsigned int precision)
1643 {
1644 return wi::shwi (2, precision);
1645 }
1646
1647 namespace wi
1648 {
1649 /* ints_for<T>::zero (X) returns a zero that, when asssigned to a T,
1650 gives that T the same precision as X. */
1651 template<typename T, precision_type = int_traits<T>::precision_type>
1652 struct ints_for
1653 {
1654 static int zero (const T &) { return 0; }
1655 };
1656
1657 template<typename T>
1658 struct ints_for<T, VAR_PRECISION>
1659 {
1660 static hwi_with_prec zero (const T &);
1661 };
1662 }
1663
1664 template<typename T>
1665 inline wi::hwi_with_prec
1666 wi::ints_for<T, wi::VAR_PRECISION>::zero (const T &x)
1667 {
1668 return wi::zero (wi::get_precision (x));
1669 }
1670
1671 namespace wi
1672 {
1673 template <>
1674 struct int_traits <wi::hwi_with_prec>
1675 {
1676 static const enum precision_type precision_type = VAR_PRECISION;
1677 /* hwi_with_prec has an explicitly-given precision, rather than the
1678 precision of HOST_WIDE_INT. */
1679 static const bool host_dependent_precision = false;
1680 static const bool is_sign_extended = true;
1681 static unsigned int get_precision (const wi::hwi_with_prec &);
1682 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1683 const wi::hwi_with_prec &);
1684 };
1685 }
1686
1687 inline unsigned int
1688 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1689 {
1690 return x.precision;
1691 }
1692
1693 inline wi::storage_ref
1694 wi::int_traits <wi::hwi_with_prec>::
1695 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1696 const wi::hwi_with_prec &x)
1697 {
1698 gcc_checking_assert (precision == x.precision);
1699 scratch[0] = x.val;
1700 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1701 return wi::storage_ref (scratch, 1, precision);
1702 scratch[1] = 0;
1703 return wi::storage_ref (scratch, 2, precision);
1704 }
1705
1706 /* Private functions for handling large cases out of line. They take
1707 individual length and array parameters because that is cheaper for
1708 the inline caller than constructing an object on the stack and
1709 passing a reference to it. (Although many callers use wide_int_refs,
1710 we generally want those to be removed by SRA.) */
1711 namespace wi
1712 {
1713 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1714 const HOST_WIDE_INT *, unsigned int, unsigned int);
1715 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1716 const HOST_WIDE_INT *, unsigned int);
1717 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1718 const HOST_WIDE_INT *, unsigned int);
1719 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1720 const HOST_WIDE_INT *, unsigned int);
1721 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1722 const HOST_WIDE_INT *, unsigned int);
1723 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1724 unsigned int,
1725 unsigned int, unsigned int);
1726 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1727 unsigned int,
1728 unsigned int, unsigned int);
1729 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1730 unsigned int, unsigned int, unsigned int);
1731 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1732 unsigned int, unsigned int, unsigned int);
1733 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1734 unsigned int, unsigned int, unsigned int,
1735 unsigned int);
1736 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1737 unsigned int, unsigned int, unsigned int,
1738 unsigned int);
1739 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1740 const HOST_WIDE_INT *, unsigned int, unsigned int);
1741 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1742 unsigned int, const HOST_WIDE_INT *,
1743 unsigned int, unsigned int);
1744 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1745 const HOST_WIDE_INT *, unsigned int, unsigned int);
1746 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1747 unsigned int, const HOST_WIDE_INT *,
1748 unsigned int, unsigned int);
1749 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1750 const HOST_WIDE_INT *, unsigned int, unsigned int);
1751 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1752 const HOST_WIDE_INT *, unsigned int, unsigned int,
1753 signop, overflow_type *);
1754 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1755 const HOST_WIDE_INT *, unsigned int, unsigned int,
1756 signop, overflow_type *);
1757 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1758 unsigned int, const HOST_WIDE_INT *,
1759 unsigned int, unsigned int, signop,
1760 overflow_type *, bool);
1761 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1762 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1763 unsigned int, unsigned int,
1764 const HOST_WIDE_INT *,
1765 unsigned int, unsigned int,
1766 signop, overflow_type *);
1767 }
1768
1769 /* Return the number of bits that integer X can hold. */
1770 template <typename T>
1771 inline unsigned int
1772 wi::get_precision (const T &x)
1773 {
1774 return wi::int_traits <T>::get_precision (x);
1775 }
1776
1777 /* Return the number of bits that the result of a binary operation can
1778 hold when the input operands are X and Y. */
1779 template <typename T1, typename T2>
1780 inline unsigned int
1781 wi::get_binary_precision (const T1 &x, const T2 &y)
1782 {
1783 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1784 get_binary_result (x, y));
1785 }
1786
1787 /* Copy the contents of Y to X, but keeping X's current precision. */
1788 template <typename T1, typename T2>
1789 inline void
1790 wi::copy (T1 &x, const T2 &y)
1791 {
1792 HOST_WIDE_INT *xval = x.write_val ();
1793 const HOST_WIDE_INT *yval = y.get_val ();
1794 unsigned int len = y.get_len ();
1795 unsigned int i = 0;
1796 do
1797 xval[i] = yval[i];
1798 while (++i < len);
1799 x.set_len (len, y.is_sign_extended);
1800 }
1801
1802 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1803 template <typename T>
1804 inline bool
1805 wi::fits_shwi_p (const T &x)
1806 {
1807 WIDE_INT_REF_FOR (T) xi (x);
1808 return xi.len == 1;
1809 }
1810
1811 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1812 precision. */
1813 template <typename T>
1814 inline bool
1815 wi::fits_uhwi_p (const T &x)
1816 {
1817 WIDE_INT_REF_FOR (T) xi (x);
1818 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1819 return true;
1820 if (xi.len == 1)
1821 return xi.slow () >= 0;
1822 return xi.len == 2 && xi.uhigh () == 0;
1823 }
1824
1825 /* Return true if X is negative based on the interpretation of SGN.
1826 For UNSIGNED, this is always false. */
1827 template <typename T>
1828 inline bool
1829 wi::neg_p (const T &x, signop sgn)
1830 {
1831 WIDE_INT_REF_FOR (T) xi (x);
1832 if (sgn == UNSIGNED)
1833 return false;
1834 return xi.sign_mask () < 0;
1835 }
1836
1837 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1838 template <typename T>
1839 inline HOST_WIDE_INT
1840 wi::sign_mask (const T &x)
1841 {
1842 WIDE_INT_REF_FOR (T) xi (x);
1843 return xi.sign_mask ();
1844 }
1845
1846 /* Return true if X == Y. X and Y must be binary-compatible. */
1847 template <typename T1, typename T2>
1848 inline bool
1849 wi::eq_p (const T1 &x, const T2 &y)
1850 {
1851 unsigned int precision = get_binary_precision (x, y);
1852 WIDE_INT_REF_FOR (T1) xi (x, precision);
1853 WIDE_INT_REF_FOR (T2) yi (y, precision);
1854 if (xi.is_sign_extended && yi.is_sign_extended)
1855 {
1856 /* This case reduces to array equality. */
1857 if (xi.len != yi.len)
1858 return false;
1859 unsigned int i = 0;
1860 do
1861 if (xi.val[i] != yi.val[i])
1862 return false;
1863 while (++i != xi.len);
1864 return true;
1865 }
1866 if (__builtin_expect (yi.len == 1, true))
1867 {
1868 /* XI is only equal to YI if it too has a single HWI. */
1869 if (xi.len != 1)
1870 return false;
1871 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1872 with 0 are simple. */
1873 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1874 return xi.val[0] == 0;
1875 /* Otherwise flush out any excess bits first. */
1876 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1877 int excess = HOST_BITS_PER_WIDE_INT - precision;
1878 if (excess > 0)
1879 diff <<= excess;
1880 return diff == 0;
1881 }
1882 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1883 }
1884
1885 /* Return true if X != Y. X and Y must be binary-compatible. */
1886 template <typename T1, typename T2>
1887 inline bool
1888 wi::ne_p (const T1 &x, const T2 &y)
1889 {
1890 return !eq_p (x, y);
1891 }
1892
1893 /* Return true if X < Y when both are treated as signed values. */
1894 template <typename T1, typename T2>
1895 inline bool
1896 wi::lts_p (const T1 &x, const T2 &y)
1897 {
1898 unsigned int precision = get_binary_precision (x, y);
1899 WIDE_INT_REF_FOR (T1) xi (x, precision);
1900 WIDE_INT_REF_FOR (T2) yi (y, precision);
1901 /* We optimize x < y, where y is 64 or fewer bits. */
1902 if (wi::fits_shwi_p (yi))
1903 {
1904 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1905 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1906 return neg_p (xi);
1907 /* If x fits directly into a shwi, we can compare directly. */
1908 if (wi::fits_shwi_p (xi))
1909 return xi.to_shwi () < yi.to_shwi ();
1910 /* If x doesn't fit and is negative, then it must be more
1911 negative than any value in y, and hence smaller than y. */
1912 if (neg_p (xi))
1913 return true;
1914 /* If x is positive, then it must be larger than any value in y,
1915 and hence greater than y. */
1916 return false;
1917 }
1918 /* Optimize the opposite case, if it can be detected at compile time. */
1919 if (STATIC_CONSTANT_P (xi.len == 1))
1920 /* If YI is negative it is lower than the least HWI.
1921 If YI is positive it is greater than the greatest HWI. */
1922 return !neg_p (yi);
1923 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1924 }
1925
1926 /* Return true if X < Y when both are treated as unsigned values. */
1927 template <typename T1, typename T2>
1928 inline bool
1929 wi::ltu_p (const T1 &x, const T2 &y)
1930 {
1931 unsigned int precision = get_binary_precision (x, y);
1932 WIDE_INT_REF_FOR (T1) xi (x, precision);
1933 WIDE_INT_REF_FOR (T2) yi (y, precision);
1934 /* Optimize comparisons with constants. */
1935 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1936 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1937 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1938 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1939 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1940 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1941 values does not change the result. */
1942 if (__builtin_expect (xi.len + yi.len == 2, true))
1943 {
1944 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1945 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1946 return xl < yl;
1947 }
1948 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1949 }
1950
1951 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1952 template <typename T1, typename T2>
1953 inline bool
1954 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1955 {
1956 if (sgn == SIGNED)
1957 return lts_p (x, y);
1958 else
1959 return ltu_p (x, y);
1960 }
1961
1962 /* Return true if X <= Y when both are treated as signed values. */
1963 template <typename T1, typename T2>
1964 inline bool
1965 wi::les_p (const T1 &x, const T2 &y)
1966 {
1967 return !lts_p (y, x);
1968 }
1969
1970 /* Return true if X <= Y when both are treated as unsigned values. */
1971 template <typename T1, typename T2>
1972 inline bool
1973 wi::leu_p (const T1 &x, const T2 &y)
1974 {
1975 return !ltu_p (y, x);
1976 }
1977
1978 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1979 template <typename T1, typename T2>
1980 inline bool
1981 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1982 {
1983 if (sgn == SIGNED)
1984 return les_p (x, y);
1985 else
1986 return leu_p (x, y);
1987 }
1988
1989 /* Return true if X > Y when both are treated as signed values. */
1990 template <typename T1, typename T2>
1991 inline bool
1992 wi::gts_p (const T1 &x, const T2 &y)
1993 {
1994 return lts_p (y, x);
1995 }
1996
1997 /* Return true if X > Y when both are treated as unsigned values. */
1998 template <typename T1, typename T2>
1999 inline bool
2000 wi::gtu_p (const T1 &x, const T2 &y)
2001 {
2002 return ltu_p (y, x);
2003 }
2004
2005 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
2006 template <typename T1, typename T2>
2007 inline bool
2008 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
2009 {
2010 if (sgn == SIGNED)
2011 return gts_p (x, y);
2012 else
2013 return gtu_p (x, y);
2014 }
2015
2016 /* Return true if X >= Y when both are treated as signed values. */
2017 template <typename T1, typename T2>
2018 inline bool
2019 wi::ges_p (const T1 &x, const T2 &y)
2020 {
2021 return !lts_p (x, y);
2022 }
2023
2024 /* Return true if X >= Y when both are treated as unsigned values. */
2025 template <typename T1, typename T2>
2026 inline bool
2027 wi::geu_p (const T1 &x, const T2 &y)
2028 {
2029 return !ltu_p (x, y);
2030 }
2031
2032 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
2033 template <typename T1, typename T2>
2034 inline bool
2035 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
2036 {
2037 if (sgn == SIGNED)
2038 return ges_p (x, y);
2039 else
2040 return geu_p (x, y);
2041 }
2042
2043 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2044 as signed values. */
2045 template <typename T1, typename T2>
2046 inline int
2047 wi::cmps (const T1 &x, const T2 &y)
2048 {
2049 unsigned int precision = get_binary_precision (x, y);
2050 WIDE_INT_REF_FOR (T1) xi (x, precision);
2051 WIDE_INT_REF_FOR (T2) yi (y, precision);
2052 if (wi::fits_shwi_p (yi))
2053 {
2054 /* Special case for comparisons with 0. */
2055 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2056 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
2057 /* If x fits into a signed HWI, we can compare directly. */
2058 if (wi::fits_shwi_p (xi))
2059 {
2060 HOST_WIDE_INT xl = xi.to_shwi ();
2061 HOST_WIDE_INT yl = yi.to_shwi ();
2062 return xl < yl ? -1 : xl > yl;
2063 }
2064 /* If x doesn't fit and is negative, then it must be more
2065 negative than any signed HWI, and hence smaller than y. */
2066 if (neg_p (xi))
2067 return -1;
2068 /* If x is positive, then it must be larger than any signed HWI,
2069 and hence greater than y. */
2070 return 1;
2071 }
2072 /* Optimize the opposite case, if it can be detected at compile time. */
2073 if (STATIC_CONSTANT_P (xi.len == 1))
2074 /* If YI is negative it is lower than the least HWI.
2075 If YI is positive it is greater than the greatest HWI. */
2076 return neg_p (yi) ? 1 : -1;
2077 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
2078 }
2079
2080 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2081 as unsigned values. */
2082 template <typename T1, typename T2>
2083 inline int
2084 wi::cmpu (const T1 &x, const T2 &y)
2085 {
2086 unsigned int precision = get_binary_precision (x, y);
2087 WIDE_INT_REF_FOR (T1) xi (x, precision);
2088 WIDE_INT_REF_FOR (T2) yi (y, precision);
2089 /* Optimize comparisons with constants. */
2090 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2091 {
2092 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2093 if (xi.len != 1)
2094 return 1;
2095 /* Otherwise compare directly. */
2096 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2097 unsigned HOST_WIDE_INT yl = yi.val[0];
2098 return xl < yl ? -1 : xl > yl;
2099 }
2100 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2101 {
2102 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2103 if (yi.len != 1)
2104 return -1;
2105 /* Otherwise compare directly. */
2106 unsigned HOST_WIDE_INT xl = xi.val[0];
2107 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2108 return xl < yl ? -1 : xl > yl;
2109 }
2110 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2111 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2112 values does not change the result. */
2113 if (__builtin_expect (xi.len + yi.len == 2, true))
2114 {
2115 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2116 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2117 return xl < yl ? -1 : xl > yl;
2118 }
2119 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2120 }
2121
2122 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2123 X and Y indicated by SGN. */
2124 template <typename T1, typename T2>
2125 inline int
2126 wi::cmp (const T1 &x, const T2 &y, signop sgn)
2127 {
2128 if (sgn == SIGNED)
2129 return cmps (x, y);
2130 else
2131 return cmpu (x, y);
2132 }
2133
2134 /* Return ~x. */
2135 template <typename T>
2136 inline WI_UNARY_RESULT (T)
2137 wi::bit_not (const T &x)
2138 {
2139 WI_UNARY_RESULT_VAR (result, val, T, x);
2140 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2141 for (unsigned int i = 0; i < xi.len; ++i)
2142 val[i] = ~xi.val[i];
2143 result.set_len (xi.len);
2144 return result;
2145 }
2146
2147 /* Return -x. */
2148 template <typename T>
2149 inline WI_UNARY_RESULT (T)
2150 wi::neg (const T &x)
2151 {
2152 return sub (0, x);
2153 }
2154
2155 /* Return -x. Indicate in *OVERFLOW if performing the negation would
2156 cause an overflow. */
2157 template <typename T>
2158 inline WI_UNARY_RESULT (T)
2159 wi::neg (const T &x, overflow_type *overflow)
2160 {
2161 *overflow = only_sign_bit_p (x) ? OVF_OVERFLOW : OVF_NONE;
2162 return sub (0, x);
2163 }
2164
2165 /* Return the absolute value of x. */
2166 template <typename T>
2167 inline WI_UNARY_RESULT (T)
2168 wi::abs (const T &x)
2169 {
2170 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2171 }
2172
2173 /* Return the result of sign-extending the low OFFSET bits of X. */
2174 template <typename T>
2175 inline WI_UNARY_RESULT (T)
2176 wi::sext (const T &x, unsigned int offset)
2177 {
2178 WI_UNARY_RESULT_VAR (result, val, T, x);
2179 unsigned int precision = get_precision (result);
2180 WIDE_INT_REF_FOR (T) xi (x, precision);
2181
2182 if (offset <= HOST_BITS_PER_WIDE_INT)
2183 {
2184 val[0] = sext_hwi (xi.ulow (), offset);
2185 result.set_len (1, true);
2186 }
2187 else
2188 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2189 return result;
2190 }
2191
2192 /* Return the result of zero-extending the low OFFSET bits of X. */
2193 template <typename T>
2194 inline WI_UNARY_RESULT (T)
2195 wi::zext (const T &x, unsigned int offset)
2196 {
2197 WI_UNARY_RESULT_VAR (result, val, T, x);
2198 unsigned int precision = get_precision (result);
2199 WIDE_INT_REF_FOR (T) xi (x, precision);
2200
2201 /* This is not just an optimization, it is actually required to
2202 maintain canonization. */
2203 if (offset >= precision)
2204 {
2205 wi::copy (result, xi);
2206 return result;
2207 }
2208
2209 /* In these cases we know that at least the top bit will be clear,
2210 so no sign extension is necessary. */
2211 if (offset < HOST_BITS_PER_WIDE_INT)
2212 {
2213 val[0] = zext_hwi (xi.ulow (), offset);
2214 result.set_len (1, true);
2215 }
2216 else
2217 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2218 return result;
2219 }
2220
2221 /* Return the result of extending the low OFFSET bits of X according to
2222 signedness SGN. */
2223 template <typename T>
2224 inline WI_UNARY_RESULT (T)
2225 wi::ext (const T &x, unsigned int offset, signop sgn)
2226 {
2227 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2228 }
2229
2230 /* Return an integer that represents X | (1 << bit). */
2231 template <typename T>
2232 inline WI_UNARY_RESULT (T)
2233 wi::set_bit (const T &x, unsigned int bit)
2234 {
2235 WI_UNARY_RESULT_VAR (result, val, T, x);
2236 unsigned int precision = get_precision (result);
2237 WIDE_INT_REF_FOR (T) xi (x, precision);
2238 if (precision <= HOST_BITS_PER_WIDE_INT)
2239 {
2240 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2241 result.set_len (1);
2242 }
2243 else
2244 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2245 return result;
2246 }
2247
2248 /* Return the mininum of X and Y, treating them both as having
2249 signedness SGN. */
2250 template <typename T1, typename T2>
2251 inline WI_BINARY_RESULT (T1, T2)
2252 wi::min (const T1 &x, const T2 &y, signop sgn)
2253 {
2254 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2255 unsigned int precision = get_precision (result);
2256 if (wi::le_p (x, y, sgn))
2257 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2258 else
2259 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2260 return result;
2261 }
2262
2263 /* Return the minimum of X and Y, treating both as signed values. */
2264 template <typename T1, typename T2>
2265 inline WI_BINARY_RESULT (T1, T2)
2266 wi::smin (const T1 &x, const T2 &y)
2267 {
2268 return wi::min (x, y, SIGNED);
2269 }
2270
2271 /* Return the minimum of X and Y, treating both as unsigned values. */
2272 template <typename T1, typename T2>
2273 inline WI_BINARY_RESULT (T1, T2)
2274 wi::umin (const T1 &x, const T2 &y)
2275 {
2276 return wi::min (x, y, UNSIGNED);
2277 }
2278
2279 /* Return the maxinum of X and Y, treating them both as having
2280 signedness SGN. */
2281 template <typename T1, typename T2>
2282 inline WI_BINARY_RESULT (T1, T2)
2283 wi::max (const T1 &x, const T2 &y, signop sgn)
2284 {
2285 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2286 unsigned int precision = get_precision (result);
2287 if (wi::ge_p (x, y, sgn))
2288 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2289 else
2290 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2291 return result;
2292 }
2293
2294 /* Return the maximum of X and Y, treating both as signed values. */
2295 template <typename T1, typename T2>
2296 inline WI_BINARY_RESULT (T1, T2)
2297 wi::smax (const T1 &x, const T2 &y)
2298 {
2299 return wi::max (x, y, SIGNED);
2300 }
2301
2302 /* Return the maximum of X and Y, treating both as unsigned values. */
2303 template <typename T1, typename T2>
2304 inline WI_BINARY_RESULT (T1, T2)
2305 wi::umax (const T1 &x, const T2 &y)
2306 {
2307 return wi::max (x, y, UNSIGNED);
2308 }
2309
2310 /* Return X & Y. */
2311 template <typename T1, typename T2>
2312 inline WI_BINARY_RESULT (T1, T2)
2313 wi::bit_and (const T1 &x, const T2 &y)
2314 {
2315 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2316 unsigned int precision = get_precision (result);
2317 WIDE_INT_REF_FOR (T1) xi (x, precision);
2318 WIDE_INT_REF_FOR (T2) yi (y, precision);
2319 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2320 if (__builtin_expect (xi.len + yi.len == 2, true))
2321 {
2322 val[0] = xi.ulow () & yi.ulow ();
2323 result.set_len (1, is_sign_extended);
2324 }
2325 else
2326 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2327 precision), is_sign_extended);
2328 return result;
2329 }
2330
2331 /* Return X & ~Y. */
2332 template <typename T1, typename T2>
2333 inline WI_BINARY_RESULT (T1, T2)
2334 wi::bit_and_not (const T1 &x, const T2 &y)
2335 {
2336 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2337 unsigned int precision = get_precision (result);
2338 WIDE_INT_REF_FOR (T1) xi (x, precision);
2339 WIDE_INT_REF_FOR (T2) yi (y, precision);
2340 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2341 if (__builtin_expect (xi.len + yi.len == 2, true))
2342 {
2343 val[0] = xi.ulow () & ~yi.ulow ();
2344 result.set_len (1, is_sign_extended);
2345 }
2346 else
2347 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2348 precision), is_sign_extended);
2349 return result;
2350 }
2351
2352 /* Return X | Y. */
2353 template <typename T1, typename T2>
2354 inline WI_BINARY_RESULT (T1, T2)
2355 wi::bit_or (const T1 &x, const T2 &y)
2356 {
2357 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2358 unsigned int precision = get_precision (result);
2359 WIDE_INT_REF_FOR (T1) xi (x, precision);
2360 WIDE_INT_REF_FOR (T2) yi (y, precision);
2361 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2362 if (__builtin_expect (xi.len + yi.len == 2, true))
2363 {
2364 val[0] = xi.ulow () | yi.ulow ();
2365 result.set_len (1, is_sign_extended);
2366 }
2367 else
2368 result.set_len (or_large (val, xi.val, xi.len,
2369 yi.val, yi.len, precision), is_sign_extended);
2370 return result;
2371 }
2372
2373 /* Return X | ~Y. */
2374 template <typename T1, typename T2>
2375 inline WI_BINARY_RESULT (T1, T2)
2376 wi::bit_or_not (const T1 &x, const T2 &y)
2377 {
2378 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2379 unsigned int precision = get_precision (result);
2380 WIDE_INT_REF_FOR (T1) xi (x, precision);
2381 WIDE_INT_REF_FOR (T2) yi (y, precision);
2382 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2383 if (__builtin_expect (xi.len + yi.len == 2, true))
2384 {
2385 val[0] = xi.ulow () | ~yi.ulow ();
2386 result.set_len (1, is_sign_extended);
2387 }
2388 else
2389 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2390 precision), is_sign_extended);
2391 return result;
2392 }
2393
2394 /* Return X ^ Y. */
2395 template <typename T1, typename T2>
2396 inline WI_BINARY_RESULT (T1, T2)
2397 wi::bit_xor (const T1 &x, const T2 &y)
2398 {
2399 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2400 unsigned int precision = get_precision (result);
2401 WIDE_INT_REF_FOR (T1) xi (x, precision);
2402 WIDE_INT_REF_FOR (T2) yi (y, precision);
2403 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2404 if (__builtin_expect (xi.len + yi.len == 2, true))
2405 {
2406 val[0] = xi.ulow () ^ yi.ulow ();
2407 result.set_len (1, is_sign_extended);
2408 }
2409 else
2410 result.set_len (xor_large (val, xi.val, xi.len,
2411 yi.val, yi.len, precision), is_sign_extended);
2412 return result;
2413 }
2414
2415 /* Return X + Y. */
2416 template <typename T1, typename T2>
2417 inline WI_BINARY_RESULT (T1, T2)
2418 wi::add (const T1 &x, const T2 &y)
2419 {
2420 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2421 unsigned int precision = get_precision (result);
2422 WIDE_INT_REF_FOR (T1) xi (x, precision);
2423 WIDE_INT_REF_FOR (T2) yi (y, precision);
2424 if (precision <= HOST_BITS_PER_WIDE_INT)
2425 {
2426 val[0] = xi.ulow () + yi.ulow ();
2427 result.set_len (1);
2428 }
2429 /* If the precision is known at compile time to be greater than
2430 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2431 knowing that (a) all bits in those HWIs are significant and
2432 (b) the result has room for at least two HWIs. This provides
2433 a fast path for things like offset_int and widest_int.
2434
2435 The STATIC_CONSTANT_P test prevents this path from being
2436 used for wide_ints. wide_ints with precisions greater than
2437 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2438 point handling them inline. */
2439 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2440 && __builtin_expect (xi.len + yi.len == 2, true))
2441 {
2442 unsigned HOST_WIDE_INT xl = xi.ulow ();
2443 unsigned HOST_WIDE_INT yl = yi.ulow ();
2444 unsigned HOST_WIDE_INT resultl = xl + yl;
2445 val[0] = resultl;
2446 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2447 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2448 >> (HOST_BITS_PER_WIDE_INT - 1)));
2449 }
2450 else
2451 result.set_len (add_large (val, xi.val, xi.len,
2452 yi.val, yi.len, precision,
2453 UNSIGNED, 0));
2454 return result;
2455 }
2456
2457 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2458 and indicate in *OVERFLOW whether the operation overflowed. */
2459 template <typename T1, typename T2>
2460 inline WI_BINARY_RESULT (T1, T2)
2461 wi::add (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2462 {
2463 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2464 unsigned int precision = get_precision (result);
2465 WIDE_INT_REF_FOR (T1) xi (x, precision);
2466 WIDE_INT_REF_FOR (T2) yi (y, precision);
2467 if (precision <= HOST_BITS_PER_WIDE_INT)
2468 {
2469 unsigned HOST_WIDE_INT xl = xi.ulow ();
2470 unsigned HOST_WIDE_INT yl = yi.ulow ();
2471 unsigned HOST_WIDE_INT resultl = xl + yl;
2472 if (sgn == SIGNED)
2473 {
2474 if ((((resultl ^ xl) & (resultl ^ yl))
2475 >> (precision - 1)) & 1)
2476 {
2477 if (xl > resultl)
2478 *overflow = OVF_UNDERFLOW;
2479 else if (xl < resultl)
2480 *overflow = OVF_OVERFLOW;
2481 else
2482 *overflow = OVF_NONE;
2483 }
2484 else
2485 *overflow = OVF_NONE;
2486 }
2487 else
2488 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2489 < (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2490 ? OVF_OVERFLOW : OVF_NONE;
2491 val[0] = resultl;
2492 result.set_len (1);
2493 }
2494 else
2495 result.set_len (add_large (val, xi.val, xi.len,
2496 yi.val, yi.len, precision,
2497 sgn, overflow));
2498 return result;
2499 }
2500
2501 /* Return X - Y. */
2502 template <typename T1, typename T2>
2503 inline WI_BINARY_RESULT (T1, T2)
2504 wi::sub (const T1 &x, const T2 &y)
2505 {
2506 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2507 unsigned int precision = get_precision (result);
2508 WIDE_INT_REF_FOR (T1) xi (x, precision);
2509 WIDE_INT_REF_FOR (T2) yi (y, precision);
2510 if (precision <= HOST_BITS_PER_WIDE_INT)
2511 {
2512 val[0] = xi.ulow () - yi.ulow ();
2513 result.set_len (1);
2514 }
2515 /* If the precision is known at compile time to be greater than
2516 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2517 knowing that (a) all bits in those HWIs are significant and
2518 (b) the result has room for at least two HWIs. This provides
2519 a fast path for things like offset_int and widest_int.
2520
2521 The STATIC_CONSTANT_P test prevents this path from being
2522 used for wide_ints. wide_ints with precisions greater than
2523 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2524 point handling them inline. */
2525 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2526 && __builtin_expect (xi.len + yi.len == 2, true))
2527 {
2528 unsigned HOST_WIDE_INT xl = xi.ulow ();
2529 unsigned HOST_WIDE_INT yl = yi.ulow ();
2530 unsigned HOST_WIDE_INT resultl = xl - yl;
2531 val[0] = resultl;
2532 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2533 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2534 >> (HOST_BITS_PER_WIDE_INT - 1)));
2535 }
2536 else
2537 result.set_len (sub_large (val, xi.val, xi.len,
2538 yi.val, yi.len, precision,
2539 UNSIGNED, 0));
2540 return result;
2541 }
2542
2543 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2544 and indicate in *OVERFLOW whether the operation overflowed. */
2545 template <typename T1, typename T2>
2546 inline WI_BINARY_RESULT (T1, T2)
2547 wi::sub (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2548 {
2549 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2550 unsigned int precision = get_precision (result);
2551 WIDE_INT_REF_FOR (T1) xi (x, precision);
2552 WIDE_INT_REF_FOR (T2) yi (y, precision);
2553 if (precision <= HOST_BITS_PER_WIDE_INT)
2554 {
2555 unsigned HOST_WIDE_INT xl = xi.ulow ();
2556 unsigned HOST_WIDE_INT yl = yi.ulow ();
2557 unsigned HOST_WIDE_INT resultl = xl - yl;
2558 if (sgn == SIGNED)
2559 {
2560 if ((((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1)
2561 {
2562 if (xl > yl)
2563 *overflow = OVF_UNDERFLOW;
2564 else if (xl < yl)
2565 *overflow = OVF_OVERFLOW;
2566 else
2567 *overflow = OVF_NONE;
2568 }
2569 else
2570 *overflow = OVF_NONE;
2571 }
2572 else
2573 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2574 > (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2575 ? OVF_UNDERFLOW : OVF_NONE;
2576 val[0] = resultl;
2577 result.set_len (1);
2578 }
2579 else
2580 result.set_len (sub_large (val, xi.val, xi.len,
2581 yi.val, yi.len, precision,
2582 sgn, overflow));
2583 return result;
2584 }
2585
2586 /* Return X * Y. */
2587 template <typename T1, typename T2>
2588 inline WI_BINARY_RESULT (T1, T2)
2589 wi::mul (const T1 &x, const T2 &y)
2590 {
2591 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2592 unsigned int precision = get_precision (result);
2593 WIDE_INT_REF_FOR (T1) xi (x, precision);
2594 WIDE_INT_REF_FOR (T2) yi (y, precision);
2595 if (precision <= HOST_BITS_PER_WIDE_INT)
2596 {
2597 val[0] = xi.ulow () * yi.ulow ();
2598 result.set_len (1);
2599 }
2600 else
2601 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2602 precision, UNSIGNED, 0, false));
2603 return result;
2604 }
2605
2606 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2607 and indicate in *OVERFLOW whether the operation overflowed. */
2608 template <typename T1, typename T2>
2609 inline WI_BINARY_RESULT (T1, T2)
2610 wi::mul (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2611 {
2612 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2613 unsigned int precision = get_precision (result);
2614 WIDE_INT_REF_FOR (T1) xi (x, precision);
2615 WIDE_INT_REF_FOR (T2) yi (y, precision);
2616 result.set_len (mul_internal (val, xi.val, xi.len,
2617 yi.val, yi.len, precision,
2618 sgn, overflow, false));
2619 return result;
2620 }
2621
2622 /* Return X * Y, treating both X and Y as signed values. Indicate in
2623 *OVERFLOW whether the operation overflowed. */
2624 template <typename T1, typename T2>
2625 inline WI_BINARY_RESULT (T1, T2)
2626 wi::smul (const T1 &x, const T2 &y, overflow_type *overflow)
2627 {
2628 return mul (x, y, SIGNED, overflow);
2629 }
2630
2631 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2632 *OVERFLOW if the result overflows. */
2633 template <typename T1, typename T2>
2634 inline WI_BINARY_RESULT (T1, T2)
2635 wi::umul (const T1 &x, const T2 &y, overflow_type *overflow)
2636 {
2637 return mul (x, y, UNSIGNED, overflow);
2638 }
2639
2640 /* Perform a widening multiplication of X and Y, extending the values
2641 according to SGN, and return the high part of the result. */
2642 template <typename T1, typename T2>
2643 inline WI_BINARY_RESULT (T1, T2)
2644 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2645 {
2646 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2647 unsigned int precision = get_precision (result);
2648 WIDE_INT_REF_FOR (T1) xi (x, precision);
2649 WIDE_INT_REF_FOR (T2) yi (y, precision);
2650 result.set_len (mul_internal (val, xi.val, xi.len,
2651 yi.val, yi.len, precision,
2652 sgn, 0, true));
2653 return result;
2654 }
2655
2656 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2657 signedness given by SGN. Indicate in *OVERFLOW if the result
2658 overflows. */
2659 template <typename T1, typename T2>
2660 inline WI_BINARY_RESULT (T1, T2)
2661 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2662 {
2663 WI_BINARY_RESULT_VAR (quotient, quotient_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);
2667
2668 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2669 precision,
2670 yi.val, yi.len, yi.precision,
2671 sgn, overflow));
2672 return quotient;
2673 }
2674
2675 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2676 template <typename T1, typename T2>
2677 inline WI_BINARY_RESULT (T1, T2)
2678 wi::sdiv_trunc (const T1 &x, const T2 &y)
2679 {
2680 return div_trunc (x, y, SIGNED);
2681 }
2682
2683 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2684 template <typename T1, typename T2>
2685 inline WI_BINARY_RESULT (T1, T2)
2686 wi::udiv_trunc (const T1 &x, const T2 &y)
2687 {
2688 return div_trunc (x, y, UNSIGNED);
2689 }
2690
2691 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2692 signedness given by SGN. Indicate in *OVERFLOW if the result
2693 overflows. */
2694 template <typename T1, typename T2>
2695 inline WI_BINARY_RESULT (T1, T2)
2696 wi::div_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2697 {
2698 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2699 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2700 unsigned int precision = get_precision (quotient);
2701 WIDE_INT_REF_FOR (T1) xi (x, precision);
2702 WIDE_INT_REF_FOR (T2) yi (y);
2703
2704 unsigned int remainder_len;
2705 quotient.set_len (divmod_internal (quotient_val,
2706 &remainder_len, remainder_val,
2707 xi.val, xi.len, precision,
2708 yi.val, yi.len, yi.precision, sgn,
2709 overflow));
2710 remainder.set_len (remainder_len);
2711 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2712 return quotient - 1;
2713 return quotient;
2714 }
2715
2716 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2717 template <typename T1, typename T2>
2718 inline WI_BINARY_RESULT (T1, T2)
2719 wi::sdiv_floor (const T1 &x, const T2 &y)
2720 {
2721 return div_floor (x, y, SIGNED);
2722 }
2723
2724 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2725 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2726 template <typename T1, typename T2>
2727 inline WI_BINARY_RESULT (T1, T2)
2728 wi::udiv_floor (const T1 &x, const T2 &y)
2729 {
2730 return div_floor (x, y, UNSIGNED);
2731 }
2732
2733 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2734 signedness given by SGN. Indicate in *OVERFLOW if the result
2735 overflows. */
2736 template <typename T1, typename T2>
2737 inline WI_BINARY_RESULT (T1, T2)
2738 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2739 {
2740 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2741 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2742 unsigned int precision = get_precision (quotient);
2743 WIDE_INT_REF_FOR (T1) xi (x, precision);
2744 WIDE_INT_REF_FOR (T2) yi (y);
2745
2746 unsigned int remainder_len;
2747 quotient.set_len (divmod_internal (quotient_val,
2748 &remainder_len, remainder_val,
2749 xi.val, xi.len, precision,
2750 yi.val, yi.len, yi.precision, sgn,
2751 overflow));
2752 remainder.set_len (remainder_len);
2753 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2754 return quotient + 1;
2755 return quotient;
2756 }
2757
2758 /* Return X / Y, rouding towards +inf. Treat X and Y as unsigned values. */
2759 template <typename T1, typename T2>
2760 inline WI_BINARY_RESULT (T1, T2)
2761 wi::udiv_ceil (const T1 &x, const T2 &y)
2762 {
2763 return div_ceil (x, y, UNSIGNED);
2764 }
2765
2766 /* Return X / Y, rouding towards nearest with ties away from zero.
2767 Treat X and Y as having the signedness given by SGN. Indicate
2768 in *OVERFLOW if the result overflows. */
2769 template <typename T1, typename T2>
2770 inline WI_BINARY_RESULT (T1, T2)
2771 wi::div_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2772 {
2773 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2774 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2775 unsigned int precision = get_precision (quotient);
2776 WIDE_INT_REF_FOR (T1) xi (x, precision);
2777 WIDE_INT_REF_FOR (T2) yi (y);
2778
2779 unsigned int remainder_len;
2780 quotient.set_len (divmod_internal (quotient_val,
2781 &remainder_len, remainder_val,
2782 xi.val, xi.len, precision,
2783 yi.val, yi.len, yi.precision, sgn,
2784 overflow));
2785 remainder.set_len (remainder_len);
2786
2787 if (remainder != 0)
2788 {
2789 if (sgn == SIGNED)
2790 {
2791 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2792 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2793 {
2794 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2795 return quotient - 1;
2796 else
2797 return quotient + 1;
2798 }
2799 }
2800 else
2801 {
2802 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2803 return quotient + 1;
2804 }
2805 }
2806 return quotient;
2807 }
2808
2809 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2810 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2811 template <typename T1, typename T2>
2812 inline WI_BINARY_RESULT (T1, T2)
2813 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2814 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2815 {
2816 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2817 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2818 unsigned int precision = get_precision (quotient);
2819 WIDE_INT_REF_FOR (T1) xi (x, precision);
2820 WIDE_INT_REF_FOR (T2) yi (y);
2821
2822 unsigned int remainder_len;
2823 quotient.set_len (divmod_internal (quotient_val,
2824 &remainder_len, remainder_val,
2825 xi.val, xi.len, precision,
2826 yi.val, yi.len, yi.precision, sgn, 0));
2827 remainder.set_len (remainder_len);
2828
2829 *remainder_ptr = remainder;
2830 return quotient;
2831 }
2832
2833 /* Compute the greatest common divisor of two numbers A and B using
2834 Euclid's algorithm. */
2835 template <typename T1, typename T2>
2836 inline WI_BINARY_RESULT (T1, T2)
2837 wi::gcd (const T1 &a, const T2 &b, signop sgn)
2838 {
2839 T1 x, y, z;
2840
2841 x = wi::abs (a);
2842 y = wi::abs (b);
2843
2844 while (gt_p (x, 0, sgn))
2845 {
2846 z = mod_trunc (y, x, sgn);
2847 y = x;
2848 x = z;
2849 }
2850
2851 return y;
2852 }
2853
2854 /* Compute X / Y, rouding towards 0, and return the remainder.
2855 Treat X and Y as having the signedness given by SGN. Indicate
2856 in *OVERFLOW if the division overflows. */
2857 template <typename T1, typename T2>
2858 inline WI_BINARY_RESULT (T1, T2)
2859 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2860 {
2861 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2862 unsigned int precision = get_precision (remainder);
2863 WIDE_INT_REF_FOR (T1) xi (x, precision);
2864 WIDE_INT_REF_FOR (T2) yi (y);
2865
2866 unsigned int remainder_len;
2867 divmod_internal (0, &remainder_len, remainder_val,
2868 xi.val, xi.len, precision,
2869 yi.val, yi.len, yi.precision, sgn, overflow);
2870 remainder.set_len (remainder_len);
2871
2872 return remainder;
2873 }
2874
2875 /* Compute X / Y, rouding towards 0, and return the remainder.
2876 Treat X and Y as signed values. */
2877 template <typename T1, typename T2>
2878 inline WI_BINARY_RESULT (T1, T2)
2879 wi::smod_trunc (const T1 &x, const T2 &y)
2880 {
2881 return mod_trunc (x, y, SIGNED);
2882 }
2883
2884 /* Compute X / Y, rouding towards 0, and return the remainder.
2885 Treat X and Y as unsigned values. */
2886 template <typename T1, typename T2>
2887 inline WI_BINARY_RESULT (T1, T2)
2888 wi::umod_trunc (const T1 &x, const T2 &y)
2889 {
2890 return mod_trunc (x, y, UNSIGNED);
2891 }
2892
2893 /* Compute X / Y, rouding towards -inf, and return the remainder.
2894 Treat X and Y as having the signedness given by SGN. Indicate
2895 in *OVERFLOW if the division overflows. */
2896 template <typename T1, typename T2>
2897 inline WI_BINARY_RESULT (T1, T2)
2898 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2899 {
2900 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2901 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2902 unsigned int precision = get_precision (quotient);
2903 WIDE_INT_REF_FOR (T1) xi (x, precision);
2904 WIDE_INT_REF_FOR (T2) yi (y);
2905
2906 unsigned int remainder_len;
2907 quotient.set_len (divmod_internal (quotient_val,
2908 &remainder_len, remainder_val,
2909 xi.val, xi.len, precision,
2910 yi.val, yi.len, yi.precision, sgn,
2911 overflow));
2912 remainder.set_len (remainder_len);
2913
2914 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2915 return remainder + y;
2916 return remainder;
2917 }
2918
2919 /* Compute X / Y, rouding towards -inf, and return the remainder.
2920 Treat X and Y as unsigned values. */
2921 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2922 template <typename T1, typename T2>
2923 inline WI_BINARY_RESULT (T1, T2)
2924 wi::umod_floor (const T1 &x, const T2 &y)
2925 {
2926 return mod_floor (x, y, UNSIGNED);
2927 }
2928
2929 /* Compute X / Y, rouding towards +inf, and return the remainder.
2930 Treat X and Y as having the signedness given by SGN. Indicate
2931 in *OVERFLOW if the division overflows. */
2932 template <typename T1, typename T2>
2933 inline WI_BINARY_RESULT (T1, T2)
2934 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2935 {
2936 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2937 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2938 unsigned int precision = get_precision (quotient);
2939 WIDE_INT_REF_FOR (T1) xi (x, precision);
2940 WIDE_INT_REF_FOR (T2) yi (y);
2941
2942 unsigned int remainder_len;
2943 quotient.set_len (divmod_internal (quotient_val,
2944 &remainder_len, remainder_val,
2945 xi.val, xi.len, precision,
2946 yi.val, yi.len, yi.precision, sgn,
2947 overflow));
2948 remainder.set_len (remainder_len);
2949
2950 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2951 return remainder - y;
2952 return remainder;
2953 }
2954
2955 /* Compute X / Y, rouding towards nearest with ties away from zero,
2956 and return the remainder. Treat X and Y as having the signedness
2957 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2958 template <typename T1, typename T2>
2959 inline WI_BINARY_RESULT (T1, T2)
2960 wi::mod_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2961 {
2962 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2963 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2964 unsigned int precision = get_precision (quotient);
2965 WIDE_INT_REF_FOR (T1) xi (x, precision);
2966 WIDE_INT_REF_FOR (T2) yi (y);
2967
2968 unsigned int remainder_len;
2969 quotient.set_len (divmod_internal (quotient_val,
2970 &remainder_len, remainder_val,
2971 xi.val, xi.len, precision,
2972 yi.val, yi.len, yi.precision, sgn,
2973 overflow));
2974 remainder.set_len (remainder_len);
2975
2976 if (remainder != 0)
2977 {
2978 if (sgn == SIGNED)
2979 {
2980 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2981 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2982 {
2983 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2984 return remainder + y;
2985 else
2986 return remainder - y;
2987 }
2988 }
2989 else
2990 {
2991 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2992 return remainder - y;
2993 }
2994 }
2995 return remainder;
2996 }
2997
2998 /* Return true if X is a multiple of Y. Treat X and Y as having the
2999 signedness given by SGN. */
3000 template <typename T1, typename T2>
3001 inline bool
3002 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
3003 {
3004 return wi::mod_trunc (x, y, sgn) == 0;
3005 }
3006
3007 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
3008 Treat X and Y as having the signedness given by SGN. */
3009 template <typename T1, typename T2>
3010 inline bool
3011 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
3012 WI_BINARY_RESULT (T1, T2) *res)
3013 {
3014 WI_BINARY_RESULT (T1, T2) remainder;
3015 WI_BINARY_RESULT (T1, T2) quotient
3016 = divmod_trunc (x, y, sgn, &remainder);
3017 if (remainder == 0)
3018 {
3019 *res = quotient;
3020 return true;
3021 }
3022 return false;
3023 }
3024
3025 /* Return X << Y. Return 0 if Y is greater than or equal to
3026 the precision of X. */
3027 template <typename T1, typename T2>
3028 inline WI_UNARY_RESULT (T1)
3029 wi::lshift (const T1 &x, const T2 &y)
3030 {
3031 WI_UNARY_RESULT_VAR (result, val, T1, x);
3032 unsigned int precision = get_precision (result);
3033 WIDE_INT_REF_FOR (T1) xi (x, precision);
3034 WIDE_INT_REF_FOR (T2) yi (y);
3035 /* Handle the simple cases quickly. */
3036 if (geu_p (yi, precision))
3037 {
3038 val[0] = 0;
3039 result.set_len (1);
3040 }
3041 else
3042 {
3043 unsigned int shift = yi.to_uhwi ();
3044 /* For fixed-precision integers like offset_int and widest_int,
3045 handle the case where the shift value is constant and the
3046 result is a single nonnegative HWI (meaning that we don't
3047 need to worry about val[1]). This is particularly common
3048 for converting a byte count to a bit count.
3049
3050 For variable-precision integers like wide_int, handle HWI
3051 and sub-HWI integers inline. */
3052 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3053 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
3054 && xi.len == 1
3055 && IN_RANGE (xi.val[0], 0, HOST_WIDE_INT_MAX >> shift))
3056 : precision <= HOST_BITS_PER_WIDE_INT)
3057 {
3058 val[0] = xi.ulow () << shift;
3059 result.set_len (1);
3060 }
3061 else
3062 result.set_len (lshift_large (val, xi.val, xi.len,
3063 precision, shift));
3064 }
3065 return result;
3066 }
3067
3068 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
3069 or equal to the precision of X. */
3070 template <typename T1, typename T2>
3071 inline WI_UNARY_RESULT (T1)
3072 wi::lrshift (const T1 &x, const T2 &y)
3073 {
3074 WI_UNARY_RESULT_VAR (result, val, T1, x);
3075 /* Do things in the precision of the input rather than the output,
3076 since the result can be no larger than that. */
3077 WIDE_INT_REF_FOR (T1) xi (x);
3078 WIDE_INT_REF_FOR (T2) yi (y);
3079 /* Handle the simple cases quickly. */
3080 if (geu_p (yi, xi.precision))
3081 {
3082 val[0] = 0;
3083 result.set_len (1);
3084 }
3085 else
3086 {
3087 unsigned int shift = yi.to_uhwi ();
3088 /* For fixed-precision integers like offset_int and widest_int,
3089 handle the case where the shift value is constant and the
3090 shifted value is a single nonnegative HWI (meaning that all
3091 bits above the HWI are zero). This is particularly common
3092 for converting a bit count to a byte count.
3093
3094 For variable-precision integers like wide_int, handle HWI
3095 and sub-HWI integers inline. */
3096 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3097 ? (shift < HOST_BITS_PER_WIDE_INT
3098 && xi.len == 1
3099 && xi.val[0] >= 0)
3100 : xi.precision <= HOST_BITS_PER_WIDE_INT)
3101 {
3102 val[0] = xi.to_uhwi () >> shift;
3103 result.set_len (1);
3104 }
3105 else
3106 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
3107 get_precision (result), shift));
3108 }
3109 return result;
3110 }
3111
3112 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
3113 Y is greater than or equal to the precision of X. */
3114 template <typename T1, typename T2>
3115 inline WI_UNARY_RESULT (T1)
3116 wi::arshift (const T1 &x, const T2 &y)
3117 {
3118 WI_UNARY_RESULT_VAR (result, val, T1, x);
3119 /* Do things in the precision of the input rather than the output,
3120 since the result can be no larger than that. */
3121 WIDE_INT_REF_FOR (T1) xi (x);
3122 WIDE_INT_REF_FOR (T2) yi (y);
3123 /* Handle the simple cases quickly. */
3124 if (geu_p (yi, xi.precision))
3125 {
3126 val[0] = sign_mask (x);
3127 result.set_len (1);
3128 }
3129 else
3130 {
3131 unsigned int shift = yi.to_uhwi ();
3132 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
3133 {
3134 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3135 result.set_len (1, true);
3136 }
3137 else
3138 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3139 get_precision (result), shift));
3140 }
3141 return result;
3142 }
3143
3144 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3145 logical shift otherwise. */
3146 template <typename T1, typename T2>
3147 inline WI_UNARY_RESULT (T1)
3148 wi::rshift (const T1 &x, const T2 &y, signop sgn)
3149 {
3150 if (sgn == UNSIGNED)
3151 return lrshift (x, y);
3152 else
3153 return arshift (x, y);
3154 }
3155
3156 /* Return the result of rotating the low WIDTH bits of X left by Y
3157 bits and zero-extending the result. Use a full-width rotate if
3158 WIDTH is zero. */
3159 template <typename T1, typename T2>
3160 WI_UNARY_RESULT (T1)
3161 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3162 {
3163 unsigned int precision = get_binary_precision (x, x);
3164 if (width == 0)
3165 width = precision;
3166 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3167 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3168 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
3169 if (width != precision)
3170 return wi::zext (left, width) | wi::zext (right, width);
3171 return left | right;
3172 }
3173
3174 /* Return the result of rotating the low WIDTH bits of X right by Y
3175 bits and zero-extending the result. Use a full-width rotate if
3176 WIDTH is zero. */
3177 template <typename T1, typename T2>
3178 WI_UNARY_RESULT (T1)
3179 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3180 {
3181 unsigned int precision = get_binary_precision (x, x);
3182 if (width == 0)
3183 width = precision;
3184 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3185 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
3186 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3187 if (width != precision)
3188 return wi::zext (left, width) | wi::zext (right, width);
3189 return left | right;
3190 }
3191
3192 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3193 is odd. */
3194 inline int
3195 wi::parity (const wide_int_ref &x)
3196 {
3197 return popcount (x) & 1;
3198 }
3199
3200 /* Extract WIDTH bits from X, starting at BITPOS. */
3201 template <typename T>
3202 inline unsigned HOST_WIDE_INT
3203 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3204 {
3205 unsigned precision = get_precision (x);
3206 if (precision < bitpos + width)
3207 precision = bitpos + width;
3208 WIDE_INT_REF_FOR (T) xi (x, precision);
3209
3210 /* Handle this rare case after the above, so that we assert about
3211 bogus BITPOS values. */
3212 if (width == 0)
3213 return 0;
3214
3215 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3216 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3217 unsigned HOST_WIDE_INT res = xi.elt (start);
3218 res >>= shift;
3219 if (shift + width > HOST_BITS_PER_WIDE_INT)
3220 {
3221 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3222 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3223 }
3224 return zext_hwi (res, width);
3225 }
3226
3227 /* Return the minimum precision needed to store X with sign SGN. */
3228 template <typename T>
3229 inline unsigned int
3230 wi::min_precision (const T &x, signop sgn)
3231 {
3232 if (sgn == SIGNED)
3233 return get_precision (x) - clrsb (x);
3234 else
3235 return get_precision (x) - clz (x);
3236 }
3237
3238 #define SIGNED_BINARY_PREDICATE(OP, F) \
3239 template <typename T1, typename T2> \
3240 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3241 OP (const T1 &x, const T2 &y) \
3242 { \
3243 return wi::F (x, y); \
3244 }
3245
3246 SIGNED_BINARY_PREDICATE (operator <, lts_p)
3247 SIGNED_BINARY_PREDICATE (operator <=, les_p)
3248 SIGNED_BINARY_PREDICATE (operator >, gts_p)
3249 SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3250
3251 #undef SIGNED_BINARY_PREDICATE
3252
3253 #define UNARY_OPERATOR(OP, F) \
3254 template<typename T> \
3255 WI_UNARY_RESULT (generic_wide_int<T>) \
3256 OP (const generic_wide_int<T> &x) \
3257 { \
3258 return wi::F (x); \
3259 }
3260
3261 #define BINARY_PREDICATE(OP, F) \
3262 template<typename T1, typename T2> \
3263 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3264 OP (const T1 &x, const T2 &y) \
3265 { \
3266 return wi::F (x, y); \
3267 }
3268
3269 #define BINARY_OPERATOR(OP, F) \
3270 template<typename T1, typename T2> \
3271 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3272 OP (const T1 &x, const T2 &y) \
3273 { \
3274 return wi::F (x, y); \
3275 }
3276
3277 #define SHIFT_OPERATOR(OP, F) \
3278 template<typename T1, typename T2> \
3279 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3280 OP (const T1 &x, const T2 &y) \
3281 { \
3282 return wi::F (x, y); \
3283 }
3284
3285 UNARY_OPERATOR (operator ~, bit_not)
3286 UNARY_OPERATOR (operator -, neg)
3287 BINARY_PREDICATE (operator ==, eq_p)
3288 BINARY_PREDICATE (operator !=, ne_p)
3289 BINARY_OPERATOR (operator &, bit_and)
3290 BINARY_OPERATOR (operator |, bit_or)
3291 BINARY_OPERATOR (operator ^, bit_xor)
3292 BINARY_OPERATOR (operator +, add)
3293 BINARY_OPERATOR (operator -, sub)
3294 BINARY_OPERATOR (operator *, mul)
3295 SHIFT_OPERATOR (operator <<, lshift)
3296
3297 #undef UNARY_OPERATOR
3298 #undef BINARY_PREDICATE
3299 #undef BINARY_OPERATOR
3300 #undef SHIFT_OPERATOR
3301
3302 template <typename T1, typename T2>
3303 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3304 operator >> (const T1 &x, const T2 &y)
3305 {
3306 return wi::arshift (x, y);
3307 }
3308
3309 template <typename T1, typename T2>
3310 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3311 operator / (const T1 &x, const T2 &y)
3312 {
3313 return wi::sdiv_trunc (x, y);
3314 }
3315
3316 template <typename T1, typename T2>
3317 inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3318 operator % (const T1 &x, const T2 &y)
3319 {
3320 return wi::smod_trunc (x, y);
3321 }
3322
3323 template<typename T>
3324 void
3325 gt_ggc_mx (generic_wide_int <T> *)
3326 {
3327 }
3328
3329 template<typename T>
3330 void
3331 gt_pch_nx (generic_wide_int <T> *)
3332 {
3333 }
3334
3335 template<typename T>
3336 void
3337 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3338 {
3339 }
3340
3341 template<int N>
3342 void
3343 gt_ggc_mx (trailing_wide_ints <N> *)
3344 {
3345 }
3346
3347 template<int N>
3348 void
3349 gt_pch_nx (trailing_wide_ints <N> *)
3350 {
3351 }
3352
3353 template<int N>
3354 void
3355 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3356 {
3357 }
3358
3359 namespace wi
3360 {
3361 /* Used for overloaded functions in which the only other acceptable
3362 scalar type is a pointer. It stops a plain 0 from being treated
3363 as a null pointer. */
3364 struct never_used1 {};
3365 struct never_used2 {};
3366
3367 wide_int min_value (unsigned int, signop);
3368 wide_int min_value (never_used1 *);
3369 wide_int min_value (never_used2 *);
3370 wide_int max_value (unsigned int, signop);
3371 wide_int max_value (never_used1 *);
3372 wide_int max_value (never_used2 *);
3373
3374 /* FIXME: this is target dependent, so should be elsewhere.
3375 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3376 wide_int from_buffer (const unsigned char *, unsigned int);
3377
3378 #ifndef GENERATOR_FILE
3379 void to_mpz (const wide_int_ref &, mpz_t, signop);
3380 #endif
3381
3382 wide_int mask (unsigned int, bool, unsigned int);
3383 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3384 wide_int set_bit_in_zero (unsigned int, unsigned int);
3385 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3386 unsigned int);
3387 wide_int round_down_for_mask (const wide_int &, const wide_int &);
3388 wide_int round_up_for_mask (const wide_int &, const wide_int &);
3389
3390 template <typename T>
3391 T mask (unsigned int, bool);
3392
3393 template <typename T>
3394 T shifted_mask (unsigned int, unsigned int, bool);
3395
3396 template <typename T>
3397 T set_bit_in_zero (unsigned int);
3398
3399 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3400 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3401 bool, unsigned int);
3402 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3403 unsigned int, unsigned int, bool);
3404 }
3405
3406 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3407 and the other bits are clear, or the inverse if NEGATE_P. */
3408 inline wide_int
3409 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3410 {
3411 wide_int result = wide_int::create (precision);
3412 result.set_len (mask (result.write_val (), width, negate_p, precision));
3413 return result;
3414 }
3415
3416 /* Return a PRECISION-bit integer in which the low START bits are clear,
3417 the next WIDTH bits are set, and the other bits are clear,
3418 or the inverse if NEGATE_P. */
3419 inline wide_int
3420 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3421 unsigned int precision)
3422 {
3423 wide_int result = wide_int::create (precision);
3424 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3425 precision));
3426 return result;
3427 }
3428
3429 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3430 others are clear. */
3431 inline wide_int
3432 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3433 {
3434 return shifted_mask (bit, 1, false, precision);
3435 }
3436
3437 /* Return an integer of type T in which the low WIDTH bits are set
3438 and the other bits are clear, or the inverse if NEGATE_P. */
3439 template <typename T>
3440 inline T
3441 wi::mask (unsigned int width, bool negate_p)
3442 {
3443 STATIC_ASSERT (wi::int_traits<T>::precision);
3444 T result;
3445 result.set_len (mask (result.write_val (), width, negate_p,
3446 wi::int_traits <T>::precision));
3447 return result;
3448 }
3449
3450 /* Return an integer of type T in which the low START bits are clear,
3451 the next WIDTH bits are set, and the other bits are clear, or the
3452 inverse if NEGATE_P. */
3453 template <typename T>
3454 inline T
3455 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3456 {
3457 STATIC_ASSERT (wi::int_traits<T>::precision);
3458 T result;
3459 result.set_len (shifted_mask (result.write_val (), start, width,
3460 negate_p,
3461 wi::int_traits <T>::precision));
3462 return result;
3463 }
3464
3465 /* Return an integer of type T in which bit BIT is set and all the
3466 others are clear. */
3467 template <typename T>
3468 inline T
3469 wi::set_bit_in_zero (unsigned int bit)
3470 {
3471 return shifted_mask <T> (bit, 1, false);
3472 }
3473
3474 /* Accumulate a set of overflows into OVERFLOW. */
3475
3476 static inline void
3477 wi::accumulate_overflow (wi::overflow_type &overflow,
3478 wi::overflow_type suboverflow)
3479 {
3480 if (!suboverflow)
3481 return;
3482 if (!overflow)
3483 overflow = suboverflow;
3484 else if (overflow != suboverflow)
3485 overflow = wi::OVF_UNKNOWN;
3486 }
3487
3488 #endif /* WIDE_INT_H */