-
Notifications
You must be signed in to change notification settings - Fork 1
/
APInt.h
1722 lines (1466 loc) · 60.5 KB
/
APInt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a class to represent arbitrary precision integral
// constant values and operations on them.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_APINT_H
#define LLVM_APINT_H
#include "llvm/Support/MathExtras.h"
#include <cassert>
#include <climits>
#include <cstring>
#include <string>
namespace llvm {
class Serializer;
class Deserializer;
class FoldingSetNodeID;
class raw_ostream;
class StringRef;
template<typename T>
class SmallVectorImpl;
// An unsigned host type used as a single part of a multi-part
// bignum.
typedef uint64_t integerPart;
const unsigned int host_char_bit = 8;
const unsigned int integerPartWidth = host_char_bit *
static_cast<unsigned int>(sizeof(integerPart));
//===----------------------------------------------------------------------===//
// APInt Class
//===----------------------------------------------------------------------===//
/// APInt - This class represents arbitrary precision constant integral values.
/// It is a functional replacement for common case unsigned integer type like
/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
/// than 64-bits of precision. APInt provides a variety of arithmetic operators
/// and methods to manipulate integer values of any bit-width. It supports both
/// the typical integer arithmetic and comparison operations as well as bitwise
/// manipulation.
///
/// The class has several invariants worth noting:
/// * All bit, byte, and word positions are zero-based.
/// * Once the bit width is set, it doesn't change except by the Truncate,
/// SignExtend, or ZeroExtend operations.
/// * All binary operators must be on APInt instances of the same bit width.
/// Attempting to use these operators on instances with different bit
/// widths will yield an assertion.
/// * The value is stored canonically as an unsigned value. For operations
/// where it makes a difference, there are both signed and unsigned variants
/// of the operation. For example, sdiv and udiv. However, because the bit
/// widths must be the same, operations such as Mul and Add produce the same
/// results regardless of whether the values are interpreted as signed or
/// not.
/// * In general, the class tries to follow the style of computation that LLVM
/// uses in its IR. This simplifies its use for LLVM.
///
/// @brief Class for arbitrary precision integers.
class APInt {
unsigned BitWidth; ///< The number of bits in this APInt.
/// This union is used to store the integer value. When the
/// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
union {
uint64_t VAL; ///< Used to store the <= 64 bits integer value.
uint64_t *pVal; ///< Used to store the >64 bits integer value.
};
/// This enum is used to hold the constants we needed for APInt.
enum {
/// Bits in a word
APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) *
CHAR_BIT,
/// Byte size of a word
APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
};
/// This constructor is used only internally for speed of construction of
/// temporaries. It is unsafe for general use so it is not public.
/// @brief Fast internal constructor
APInt(uint64_t* val, unsigned bits) : BitWidth(bits), pVal(val) { }
/// @returns true if the number of bits <= 64, false otherwise.
/// @brief Determine if this APInt just has one word to store value.
bool isSingleWord() const {
return BitWidth <= APINT_BITS_PER_WORD;
}
/// @returns the word position for the specified bit position.
/// @brief Determine which word a bit is in.
static unsigned whichWord(unsigned bitPosition) {
return bitPosition / APINT_BITS_PER_WORD;
}
/// @returns the bit position in a word for the specified bit position
/// in the APInt.
/// @brief Determine which bit in a word a bit is in.
static unsigned whichBit(unsigned bitPosition) {
return bitPosition % APINT_BITS_PER_WORD;
}
/// This method generates and returns a uint64_t (word) mask for a single
/// bit at a specific bit position. This is used to mask the bit in the
/// corresponding word.
/// @returns a uint64_t with only bit at "whichBit(bitPosition)" set
/// @brief Get a single bit mask.
static uint64_t maskBit(unsigned bitPosition) {
return 1ULL << whichBit(bitPosition);
}
/// This method is used internally to clear the to "N" bits in the high order
/// word that are not used by the APInt. This is needed after the most
/// significant word is assigned a value to ensure that those bits are
/// zero'd out.
/// @brief Clear unused high order bits
APInt& clearUnusedBits() {
// Compute how many bits are used in the final word
unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
if (wordBits == 0)
// If all bits are used, we want to leave the value alone. This also
// avoids the undefined behavior of >> when the shift is the same size as
// the word size (64).
return *this;
// Mask out the high bits.
uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
if (isSingleWord())
VAL &= mask;
else
pVal[getNumWords() - 1] &= mask;
return *this;
}
/// @returns the corresponding word for the specified bit position.
/// @brief Get the word corresponding to a bit position
uint64_t getWord(unsigned bitPosition) const {
return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
}
/// Converts a string into a number. The string must be non-empty
/// and well-formed as a number of the given base. The bit-width
/// must be sufficient to hold the result.
///
/// This is used by the constructors that take string arguments.
///
/// StringRef::getAsInteger is superficially similar but (1) does
/// not assume that the string is well-formed and (2) grows the
/// result to hold the input.
///
/// @param radix 2, 8, 10, or 16
/// @brief Convert a char array into an APInt
void fromString(unsigned numBits, StringRef str, uint8_t radix);
/// This is used by the toString method to divide by the radix. It simply
/// provides a more convenient form of divide for internal use since KnuthDiv
/// has specific constraints on its inputs. If those constraints are not met
/// then it provides a simpler form of divide.
/// @brief An internal division function for dividing APInts.
static void divide(const APInt LHS, unsigned lhsWords,
const APInt &RHS, unsigned rhsWords,
APInt *Quotient, APInt *Remainder);
/// out-of-line slow case for inline constructor
void initSlowCase(unsigned numBits, uint64_t val, bool isSigned);
/// out-of-line slow case for inline copy constructor
void initSlowCase(const APInt& that);
/// out-of-line slow case for shl
APInt shlSlowCase(unsigned shiftAmt) const;
/// out-of-line slow case for operator&
APInt AndSlowCase(const APInt& RHS) const;
/// out-of-line slow case for operator|
APInt OrSlowCase(const APInt& RHS) const;
/// out-of-line slow case for operator^
APInt XorSlowCase(const APInt& RHS) const;
/// out-of-line slow case for operator=
APInt& AssignSlowCase(const APInt& RHS);
/// out-of-line slow case for operator==
bool EqualSlowCase(const APInt& RHS) const;
/// out-of-line slow case for operator==
bool EqualSlowCase(uint64_t Val) const;
/// out-of-line slow case for countLeadingZeros
unsigned countLeadingZerosSlowCase() const;
/// out-of-line slow case for countTrailingOnes
unsigned countTrailingOnesSlowCase() const;
/// out-of-line slow case for countPopulation
unsigned countPopulationSlowCase() const;
public:
/// @name Constructors
/// @{
/// If isSigned is true then val is treated as if it were a signed value
/// (i.e. as an int64_t) and the appropriate sign extension to the bit width
/// will be done. Otherwise, no sign extension occurs (high order bits beyond
/// the range of val are zero filled).
/// @param numBits the bit width of the constructed APInt
/// @param val the initial value of the APInt
/// @param isSigned how to treat signedness of val
/// @brief Create a new APInt of numBits width, initialized as val.
APInt(unsigned numBits, uint64_t val, bool isSigned = false)
: BitWidth(numBits), VAL(0) {
assert(BitWidth && "bitwidth too small");
if (isSingleWord())
VAL = val;
else
initSlowCase(numBits, val, isSigned);
clearUnusedBits();
}
/// Note that numWords can be smaller or larger than the corresponding bit
/// width but any extraneous bits will be dropped.
/// @param numBits the bit width of the constructed APInt
/// @param numWords the number of words in bigVal
/// @param bigVal a sequence of words to form the initial value of the APInt
/// @brief Construct an APInt of numBits width, initialized as bigVal[].
APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
/// This constructor interprets the string \arg str in the given radix. The
/// interpretation stops when the first character that is not suitable for the
/// radix is encountered, or the end of the string. Acceptable radix values
/// are 2, 8, 10 and 16. It is an error for the value implied by the string to
/// require more bits than numBits.
///
/// @param numBits the bit width of the constructed APInt
/// @param str the string to be interpreted
/// @param radix the radix to use for the conversion
/// @brief Construct an APInt from a string representation.
APInt(unsigned numBits, StringRef str, uint8_t radix);
/// Simply makes *this a copy of that.
/// @brief Copy Constructor.
APInt(const APInt& that)
: BitWidth(that.BitWidth), VAL(0) {
assert(BitWidth && "bitwidth too small");
if (isSingleWord())
VAL = that.VAL;
else
initSlowCase(that);
}
/// @brief Destructor.
~APInt() {
if (!isSingleWord())
delete [] pVal;
}
/// Default constructor that creates an uninitialized APInt. This is useful
/// for object deserialization (pair this with the static method Read).
explicit APInt() : BitWidth(1) {}
/// Profile - Used to insert APInt objects, or objects that contain APInt
/// objects, into FoldingSets.
void Profile(FoldingSetNodeID& id) const;
/// @}
/// @name Value Tests
/// @{
/// This tests the high bit of this APInt to determine if it is set.
/// @returns true if this APInt is negative, false otherwise
/// @brief Determine sign of this APInt.
bool isNegative() const {
return (*this)[BitWidth - 1];
}
/// This tests the high bit of the APInt to determine if it is unset.
/// @brief Determine if this APInt Value is non-negative (>= 0)
bool isNonNegative() const {
return !isNegative();
}
/// This tests if the value of this APInt is positive (> 0). Note
/// that 0 is not a positive value.
/// @returns true if this APInt is positive.
/// @brief Determine if this APInt Value is positive.
bool isStrictlyPositive() const {
return isNonNegative() && !!*this;
}
/// This checks to see if the value has all bits of the APInt are set or not.
/// @brief Determine if all bits are set
bool isAllOnesValue() const {
return countPopulation() == BitWidth;
}
/// This checks to see if the value of this APInt is the maximum unsigned
/// value for the APInt's bit width.
/// @brief Determine if this is the largest unsigned value.
bool isMaxValue() const {
return countPopulation() == BitWidth;
}
/// This checks to see if the value of this APInt is the maximum signed
/// value for the APInt's bit width.
/// @brief Determine if this is the largest signed value.
bool isMaxSignedValue() const {
return BitWidth == 1 ? VAL == 0 :
!isNegative() && countPopulation() == BitWidth - 1;
}
/// This checks to see if the value of this APInt is the minimum unsigned
/// value for the APInt's bit width.
/// @brief Determine if this is the smallest unsigned value.
bool isMinValue() const {
return !*this;
}
/// This checks to see if the value of this APInt is the minimum signed
/// value for the APInt's bit width.
/// @brief Determine if this is the smallest signed value.
bool isMinSignedValue() const {
return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
}
/// @brief Check if this APInt has an N-bits unsigned integer value.
bool isIntN(unsigned N) const {
assert(N && "N == 0 ???");
if (N >= getBitWidth())
return true;
if (isSingleWord())
return isUIntN(N, VAL);
return APInt(N, getNumWords(), pVal).zext(getBitWidth()) == (*this);
}
/// @brief Check if this APInt has an N-bits signed integer value.
bool isSignedIntN(unsigned N) const {
assert(N && "N == 0 ???");
return getMinSignedBits() <= N;
}
/// @returns true if the argument APInt value is a power of two > 0.
bool isPowerOf2() const {
if (isSingleWord())
return isPowerOf2_64(VAL);
return countPopulationSlowCase() == 1;
}
/// isSignBit - Return true if this is the value returned by getSignBit.
bool isSignBit() const { return isMinSignedValue(); }
/// This converts the APInt to a boolean value as a test against zero.
/// @brief Boolean conversion function.
bool getBoolValue() const {
return !!*this;
}
/// getLimitedValue - If this value is smaller than the specified limit,
/// return it, otherwise return the limit value. This causes the value
/// to saturate to the limit.
uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
return (getActiveBits() > 64 || getZExtValue() > Limit) ?
Limit : getZExtValue();
}
/// @}
/// @name Value Generators
/// @{
/// @brief Gets maximum unsigned value of APInt for specific bit width.
static APInt getMaxValue(unsigned numBits) {
return getAllOnesValue(numBits);
}
/// @brief Gets maximum signed value of APInt for a specific bit width.
static APInt getSignedMaxValue(unsigned numBits) {
APInt API = getAllOnesValue(numBits);
API.clearBit(numBits - 1);
return API;
}
/// @brief Gets minimum unsigned value of APInt for a specific bit width.
static APInt getMinValue(unsigned numBits) {
return APInt(numBits, 0);
}
/// @brief Gets minimum signed value of APInt for a specific bit width.
static APInt getSignedMinValue(unsigned numBits) {
APInt API(numBits, 0);
API.setBit(numBits - 1);
return API;
}
/// getSignBit - This is just a wrapper function of getSignedMinValue(), and
/// it helps code readability when we want to get a SignBit.
/// @brief Get the SignBit for a specific bit width.
static APInt getSignBit(unsigned BitWidth) {
return getSignedMinValue(BitWidth);
}
/// @returns the all-ones value for an APInt of the specified bit-width.
/// @brief Get the all-ones value.
static APInt getAllOnesValue(unsigned numBits) {
return APInt(numBits, -1ULL, true);
}
/// @returns the '0' value for an APInt of the specified bit-width.
/// @brief Get the '0' value.
static APInt getNullValue(unsigned numBits) {
return APInt(numBits, 0);
}
/// Get an APInt with the same BitWidth as this APInt, just zero mask
/// the low bits and right shift to the least significant bit.
/// @returns the high "numBits" bits of this APInt.
APInt getHiBits(unsigned numBits) const;
/// Get an APInt with the same BitWidth as this APInt, just zero mask
/// the high bits.
/// @returns the low "numBits" bits of this APInt.
APInt getLoBits(unsigned numBits) const;
/// getOneBitSet - Return an APInt with exactly one bit set in the result.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
APInt Res(numBits, 0);
Res.setBit(BitNo);
return Res;
}
/// Constructs an APInt value that has a contiguous range of bits set. The
/// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
/// bits will be zero. For example, with parameters(32, 0, 16) you would get
/// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
/// example, with parameters (32, 28, 4), you would get 0xF000000F.
/// @param numBits the intended bit width of the result
/// @param loBit the index of the lowest bit set.
/// @param hiBit the index of the highest bit set.
/// @returns An APInt value with the requested bits set.
/// @brief Get a value with a block of bits set.
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
assert(hiBit <= numBits && "hiBit out of range");
assert(loBit < numBits && "loBit out of range");
if (hiBit < loBit)
return getLowBitsSet(numBits, hiBit) |
getHighBitsSet(numBits, numBits-loBit);
return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
}
/// Constructs an APInt value that has the top hiBitsSet bits set.
/// @param numBits the bitwidth of the result
/// @param hiBitsSet the number of high-order bits set in the result.
/// @brief Get a value with high bits set
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
assert(hiBitsSet <= numBits && "Too many bits to set!");
// Handle a degenerate case, to avoid shifting by word size
if (hiBitsSet == 0)
return APInt(numBits, 0);
unsigned shiftAmt = numBits - hiBitsSet;
// For small values, return quickly
if (numBits <= APINT_BITS_PER_WORD)
return APInt(numBits, ~0ULL << shiftAmt);
return getAllOnesValue(numBits).shl(shiftAmt);
}
/// Constructs an APInt value that has the bottom loBitsSet bits set.
/// @param numBits the bitwidth of the result
/// @param loBitsSet the number of low-order bits set in the result.
/// @brief Get a value with low bits set
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
assert(loBitsSet <= numBits && "Too many bits to set!");
// Handle a degenerate case, to avoid shifting by word size
if (loBitsSet == 0)
return APInt(numBits, 0);
if (loBitsSet == APINT_BITS_PER_WORD)
return APInt(numBits, -1ULL);
// For small values, return quickly.
if (numBits < APINT_BITS_PER_WORD)
return APInt(numBits, (1ULL << loBitsSet) - 1);
return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
}
/// The hash value is computed as the sum of the words and the bit width.
/// @returns A hash value computed from the sum of the APInt words.
/// @brief Get a hash value based on this APInt
uint64_t getHashValue() const;
/// This function returns a pointer to the internal storage of the APInt.
/// This is useful for writing out the APInt in binary form without any
/// conversions.
const uint64_t* getRawData() const {
if (isSingleWord())
return &VAL;
return &pVal[0];
}
/// @}
/// @name Unary Operators
/// @{
/// @returns a new APInt value representing *this incremented by one
/// @brief Postfix increment operator.
const APInt operator++(int) {
APInt API(*this);
++(*this);
return API;
}
/// @returns *this incremented by one
/// @brief Prefix increment operator.
APInt& operator++();
/// @returns a new APInt representing *this decremented by one.
/// @brief Postfix decrement operator.
const APInt operator--(int) {
APInt API(*this);
--(*this);
return API;
}
/// @returns *this decremented by one.
/// @brief Prefix decrement operator.
APInt& operator--();
/// Performs a bitwise complement operation on this APInt.
/// @returns an APInt that is the bitwise complement of *this
/// @brief Unary bitwise complement operator.
APInt operator~() const {
APInt Result(*this);
Result.flipAllBits();
return Result;
}
/// Negates *this using two's complement logic.
/// @returns An APInt value representing the negation of *this.
/// @brief Unary negation operator
APInt operator-() const {
return APInt(BitWidth, 0) - (*this);
}
/// Performs logical negation operation on this APInt.
/// @returns true if *this is zero, false otherwise.
/// @brief Logical negation operator.
bool operator!() const;
/// @}
/// @name Assignment Operators
/// @{
/// @returns *this after assignment of RHS.
/// @brief Copy assignment operator.
APInt& operator=(const APInt& RHS) {
// If the bitwidths are the same, we can avoid mucking with memory
if (isSingleWord() && RHS.isSingleWord()) {
VAL = RHS.VAL;
BitWidth = RHS.BitWidth;
return clearUnusedBits();
}
return AssignSlowCase(RHS);
}
/// The RHS value is assigned to *this. If the significant bits in RHS exceed
/// the bit width, the excess bits are truncated. If the bit width is larger
/// than 64, the value is zero filled in the unspecified high order bits.
/// @returns *this after assignment of RHS value.
/// @brief Assignment operator.
APInt& operator=(uint64_t RHS);
/// Performs a bitwise AND operation on this APInt and RHS. The result is
/// assigned to *this.
/// @returns *this after ANDing with RHS.
/// @brief Bitwise AND assignment operator.
APInt& operator&=(const APInt& RHS);
/// Performs a bitwise OR operation on this APInt and RHS. The result is
/// assigned *this;
/// @returns *this after ORing with RHS.
/// @brief Bitwise OR assignment operator.
APInt& operator|=(const APInt& RHS);
/// Performs a bitwise OR operation on this APInt and RHS. RHS is
/// logically zero-extended or truncated to match the bit-width of
/// the LHS.
///
/// @brief Bitwise OR assignment operator.
APInt& operator|=(uint64_t RHS) {
if (isSingleWord()) {
VAL |= RHS;
clearUnusedBits();
} else {
pVal[0] |= RHS;
}
return *this;
}
/// Performs a bitwise XOR operation on this APInt and RHS. The result is
/// assigned to *this.
/// @returns *this after XORing with RHS.
/// @brief Bitwise XOR assignment operator.
APInt& operator^=(const APInt& RHS);
/// Multiplies this APInt by RHS and assigns the result to *this.
/// @returns *this
/// @brief Multiplication assignment operator.
APInt& operator*=(const APInt& RHS);
/// Adds RHS to *this and assigns the result to *this.
/// @returns *this
/// @brief Addition assignment operator.
APInt& operator+=(const APInt& RHS);
/// Subtracts RHS from *this and assigns the result to *this.
/// @returns *this
/// @brief Subtraction assignment operator.
APInt& operator-=(const APInt& RHS);
/// Shifts *this left by shiftAmt and assigns the result to *this.
/// @returns *this after shifting left by shiftAmt
/// @brief Left-shift assignment function.
APInt& operator<<=(unsigned shiftAmt) {
*this = shl(shiftAmt);
return *this;
}
/// @}
/// @name Binary Operators
/// @{
/// Performs a bitwise AND operation on *this and RHS.
/// @returns An APInt value representing the bitwise AND of *this and RHS.
/// @brief Bitwise AND operator.
APInt operator&(const APInt& RHS) const {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
if (isSingleWord())
return APInt(getBitWidth(), VAL & RHS.VAL);
return AndSlowCase(RHS);
}
APInt And(const APInt& RHS) const {
return this->operator&(RHS);
}
/// Performs a bitwise OR operation on *this and RHS.
/// @returns An APInt value representing the bitwise OR of *this and RHS.
/// @brief Bitwise OR operator.
APInt operator|(const APInt& RHS) const {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
if (isSingleWord())
return APInt(getBitWidth(), VAL | RHS.VAL);
return OrSlowCase(RHS);
}
APInt Or(const APInt& RHS) const {
return this->operator|(RHS);
}
/// Performs a bitwise XOR operation on *this and RHS.
/// @returns An APInt value representing the bitwise XOR of *this and RHS.
/// @brief Bitwise XOR operator.
APInt operator^(const APInt& RHS) const {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
if (isSingleWord())
return APInt(BitWidth, VAL ^ RHS.VAL);
return XorSlowCase(RHS);
}
APInt Xor(const APInt& RHS) const {
return this->operator^(RHS);
}
/// Multiplies this APInt by RHS and returns the result.
/// @brief Multiplication operator.
APInt operator*(const APInt& RHS) const;
/// Adds RHS to this APInt and returns the result.
/// @brief Addition operator.
APInt operator+(const APInt& RHS) const;
APInt operator+(uint64_t RHS) const {
return (*this) + APInt(BitWidth, RHS);
}
/// Subtracts RHS from this APInt and returns the result.
/// @brief Subtraction operator.
APInt operator-(const APInt& RHS) const;
APInt operator-(uint64_t RHS) const {
return (*this) - APInt(BitWidth, RHS);
}
APInt operator<<(unsigned Bits) const {
return shl(Bits);
}
APInt operator<<(const APInt &Bits) const {
return shl(Bits);
}
/// Arithmetic right-shift this APInt by shiftAmt.
/// @brief Arithmetic right-shift function.
APInt ashr(unsigned shiftAmt) const;
/// Logical right-shift this APInt by shiftAmt.
/// @brief Logical right-shift function.
APInt lshr(unsigned shiftAmt) const;
/// Left-shift this APInt by shiftAmt.
/// @brief Left-shift function.
APInt shl(unsigned shiftAmt) const {
assert(shiftAmt <= BitWidth && "Invalid shift amount");
if (isSingleWord()) {
if (shiftAmt == BitWidth)
return APInt(BitWidth, 0); // avoid undefined shift results
return APInt(BitWidth, VAL << shiftAmt);
}
return shlSlowCase(shiftAmt);
}
/// @brief Rotate left by rotateAmt.
APInt rotl(unsigned rotateAmt) const;
/// @brief Rotate right by rotateAmt.
APInt rotr(unsigned rotateAmt) const;
/// Arithmetic right-shift this APInt by shiftAmt.
/// @brief Arithmetic right-shift function.
APInt ashr(const APInt &shiftAmt) const;
/// Logical right-shift this APInt by shiftAmt.
/// @brief Logical right-shift function.
APInt lshr(const APInt &shiftAmt) const;
/// Left-shift this APInt by shiftAmt.
/// @brief Left-shift function.
APInt shl(const APInt &shiftAmt) const;
/// @brief Rotate left by rotateAmt.
APInt rotl(const APInt &rotateAmt) const;
/// @brief Rotate right by rotateAmt.
APInt rotr(const APInt &rotateAmt) const;
/// Perform an unsigned divide operation on this APInt by RHS. Both this and
/// RHS are treated as unsigned quantities for purposes of this division.
/// @returns a new APInt value containing the division result
/// @brief Unsigned division operation.
APInt udiv(const APInt &RHS) const;
/// Signed divide this APInt by APInt RHS.
/// @brief Signed division function for APInt.
APInt sdiv(const APInt &RHS) const {
if (isNegative())
if (RHS.isNegative())
return (-(*this)).udiv(-RHS);
else
return -((-(*this)).udiv(RHS));
else if (RHS.isNegative())
return -(this->udiv(-RHS));
return this->udiv(RHS);
}
/// Perform an unsigned remainder operation on this APInt with RHS being the
/// divisor. Both this and RHS are treated as unsigned quantities for purposes
/// of this operation. Note that this is a true remainder operation and not
/// a modulo operation because the sign follows the sign of the dividend
/// which is *this.
/// @returns a new APInt value containing the remainder result
/// @brief Unsigned remainder operation.
APInt urem(const APInt &RHS) const;
/// Signed remainder operation on APInt.
/// @brief Function for signed remainder operation.
APInt srem(const APInt &RHS) const {
if (isNegative())
if (RHS.isNegative())
return -((-(*this)).urem(-RHS));
else
return -((-(*this)).urem(RHS));
else if (RHS.isNegative())
return this->urem(-RHS);
return this->urem(RHS);
}
/// Sometimes it is convenient to divide two APInt values and obtain both the
/// quotient and remainder. This function does both operations in the same
/// computation making it a little more efficient. The pair of input arguments
/// may overlap with the pair of output arguments. It is safe to call
/// udivrem(X, Y, X, Y), for example.
/// @brief Dual division/remainder interface.
static void udivrem(const APInt &LHS, const APInt &RHS,
APInt &Quotient, APInt &Remainder);
static void sdivrem(const APInt &LHS, const APInt &RHS,
APInt &Quotient, APInt &Remainder) {
if (LHS.isNegative()) {
if (RHS.isNegative())
APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
else
APInt::udivrem(-LHS, RHS, Quotient, Remainder);
Quotient = -Quotient;
Remainder = -Remainder;
} else if (RHS.isNegative()) {
APInt::udivrem(LHS, -RHS, Quotient, Remainder);
Quotient = -Quotient;
} else {
APInt::udivrem(LHS, RHS, Quotient, Remainder);
}
}
// Operations that return overflow indicators.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
APInt usub_ov(const APInt &RHS, bool &Overflow) const;
APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
APInt smul_ov(const APInt &RHS, bool &Overflow) const;
APInt umul_ov(const APInt &RHS, bool &Overflow) const;
APInt sshl_ov(unsigned Amt, bool &Overflow) const;
/// @returns the bit value at bitPosition
/// @brief Array-indexing support.
bool operator[](unsigned bitPosition) const;
/// @}
/// @name Comparison Operators
/// @{
/// Compares this APInt with RHS for the validity of the equality
/// relationship.
/// @brief Equality operator.
bool operator==(const APInt& RHS) const {
assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
if (isSingleWord())
return VAL == RHS.VAL;
return EqualSlowCase(RHS);
}
/// Compares this APInt with a uint64_t for the validity of the equality
/// relationship.
/// @returns true if *this == Val
/// @brief Equality operator.
bool operator==(uint64_t Val) const {
if (isSingleWord())
return VAL == Val;
return EqualSlowCase(Val);
}
/// Compares this APInt with RHS for the validity of the equality
/// relationship.
/// @returns true if *this == Val
/// @brief Equality comparison.
bool eq(const APInt &RHS) const {
return (*this) == RHS;
}
/// Compares this APInt with RHS for the validity of the inequality
/// relationship.
/// @returns true if *this != Val
/// @brief Inequality operator.
bool operator!=(const APInt& RHS) const {
return !((*this) == RHS);
}
/// Compares this APInt with a uint64_t for the validity of the inequality
/// relationship.
/// @returns true if *this != Val
/// @brief Inequality operator.
bool operator!=(uint64_t Val) const {
return !((*this) == Val);
}
/// Compares this APInt with RHS for the validity of the inequality
/// relationship.
/// @returns true if *this != Val
/// @brief Inequality comparison
bool ne(const APInt &RHS) const {
return !((*this) == RHS);
}
/// Regards both *this and RHS as unsigned quantities and compares them for
/// the validity of the less-than relationship.
/// @returns true if *this < RHS when both are considered unsigned.
/// @brief Unsigned less than comparison
bool ult(const APInt &RHS) const;
/// Regards both *this as an unsigned quantity and compares it with RHS for
/// the validity of the less-than relationship.
/// @returns true if *this < RHS when considered unsigned.
/// @brief Unsigned less than comparison
bool ult(uint64_t RHS) const {
return ult(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as signed quantities and compares them for
/// validity of the less-than relationship.
/// @returns true if *this < RHS when both are considered signed.
/// @brief Signed less than comparison
bool slt(const APInt& RHS) const;
/// Regards both *this as a signed quantity and compares it with RHS for
/// the validity of the less-than relationship.
/// @returns true if *this < RHS when considered signed.
/// @brief Signed less than comparison
bool slt(uint64_t RHS) const {
return slt(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as unsigned quantities and compares them for
/// validity of the less-or-equal relationship.
/// @returns true if *this <= RHS when both are considered unsigned.
/// @brief Unsigned less or equal comparison
bool ule(const APInt& RHS) const {
return ult(RHS) || eq(RHS);
}
/// Regards both *this as an unsigned quantity and compares it with RHS for
/// the validity of the less-or-equal relationship.
/// @returns true if *this <= RHS when considered unsigned.
/// @brief Unsigned less or equal comparison
bool ule(uint64_t RHS) const {
return ule(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as signed quantities and compares them for
/// validity of the less-or-equal relationship.
/// @returns true if *this <= RHS when both are considered signed.
/// @brief Signed less or equal comparison
bool sle(const APInt& RHS) const {
return slt(RHS) || eq(RHS);
}
/// Regards both *this as a signed quantity and compares it with RHS for
/// the validity of the less-or-equal relationship.
/// @returns true if *this <= RHS when considered signed.
/// @brief Signed less or equal comparison
bool sle(uint64_t RHS) const {
return sle(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as unsigned quantities and compares them for
/// the validity of the greater-than relationship.
/// @returns true if *this > RHS when both are considered unsigned.
/// @brief Unsigned greather than comparison
bool ugt(const APInt& RHS) const {
return !ult(RHS) && !eq(RHS);
}
/// Regards both *this as an unsigned quantity and compares it with RHS for
/// the validity of the greater-than relationship.
/// @returns true if *this > RHS when considered unsigned.
/// @brief Unsigned greater than comparison
bool ugt(uint64_t RHS) const {
return ugt(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as signed quantities and compares them for
/// the validity of the greater-than relationship.
/// @returns true if *this > RHS when both are considered signed.
/// @brief Signed greather than comparison
bool sgt(const APInt& RHS) const {
return !slt(RHS) && !eq(RHS);
}
/// Regards both *this as a signed quantity and compares it with RHS for
/// the validity of the greater-than relationship.
/// @returns true if *this > RHS when considered signed.
/// @brief Signed greater than comparison
bool sgt(uint64_t RHS) const {
return sgt(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as unsigned quantities and compares them for
/// validity of the greater-or-equal relationship.
/// @returns true if *this >= RHS when both are considered unsigned.
/// @brief Unsigned greater or equal comparison
bool uge(const APInt& RHS) const {
return !ult(RHS);
}
/// Regards both *this as an unsigned quantity and compares it with RHS for
/// the validity of the greater-or-equal relationship.
/// @returns true if *this >= RHS when considered unsigned.
/// @brief Unsigned greater or equal comparison
bool uge(uint64_t RHS) const {
return uge(APInt(getBitWidth(), RHS));
}
/// Regards both *this and RHS as signed quantities and compares them for
/// validity of the greater-or-equal relationship.
/// @returns true if *this >= RHS when both are considered signed.
/// @brief Signed greather or equal comparison
bool sge(const APInt& RHS) const {
return !slt(RHS);
}
/// Regards both *this as a signed quantity and compares it with RHS for
/// the validity of the greater-or-equal relationship.