APSInt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/ADT/APSInt.h - Arbitrary Precision Signed Int -----*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file implements the APSInt class, which is a simple class that
  16. /// represents an arbitrary sized integer that knows its signedness.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_APSINT_H
  20. #define LLVM_ADT_APSINT_H
  21. #include "llvm/ADT/APInt.h"
  22. namespace llvm {
  23. /// An arbitrary precision integer that knows its signedness.
  24. class LLVM_NODISCARD APSInt : public APInt {
  25. bool IsUnsigned;
  26. public:
  27. /// Default constructor that creates an uninitialized APInt.
  28. explicit APSInt() : IsUnsigned(false) {}
  29. /// Create an APSInt with the specified width, default to unsigned.
  30. explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
  31. : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
  32. explicit APSInt(APInt I, bool isUnsigned = true)
  33. : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
  34. /// Construct an APSInt from a string representation.
  35. ///
  36. /// This constructor interprets the string \p Str using the radix of 10.
  37. /// The interpretation stops at the end of the string. The bit width of the
  38. /// constructed APSInt is determined automatically.
  39. ///
  40. /// \param Str the string to be interpreted.
  41. explicit APSInt(StringRef Str);
  42. /// Determine sign of this APSInt.
  43. ///
  44. /// \returns true if this APSInt is negative, false otherwise
  45. bool isNegative() const { return isSigned() && APInt::isNegative(); }
  46. /// Determine if this APSInt Value is non-negative (>= 0)
  47. ///
  48. /// \returns true if this APSInt is non-negative, false otherwise
  49. bool isNonNegative() const { return !isNegative(); }
  50. /// Determine if this APSInt Value is positive.
  51. ///
  52. /// This tests if the value of this APSInt is positive (> 0). Note
  53. /// that 0 is not a positive value.
  54. ///
  55. /// \returns true if this APSInt is positive.
  56. bool isStrictlyPositive() const { return isNonNegative() && !isZero(); }
  57. APSInt &operator=(APInt RHS) {
  58. // Retain our current sign.
  59. APInt::operator=(std::move(RHS));
  60. return *this;
  61. }
  62. APSInt &operator=(uint64_t RHS) {
  63. // Retain our current sign.
  64. APInt::operator=(RHS);
  65. return *this;
  66. }
  67. // Query sign information.
  68. bool isSigned() const { return !IsUnsigned; }
  69. bool isUnsigned() const { return IsUnsigned; }
  70. void setIsUnsigned(bool Val) { IsUnsigned = Val; }
  71. void setIsSigned(bool Val) { IsUnsigned = !Val; }
  72. /// Append this APSInt to the specified SmallString.
  73. void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
  74. APInt::toString(Str, Radix, isSigned());
  75. }
  76. using APInt::toString;
  77. /// Get the correctly-extended \c int64_t value.
  78. int64_t getExtValue() const {
  79. assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
  80. return isSigned() ? getSExtValue() : getZExtValue();
  81. }
  82. APSInt trunc(uint32_t width) const {
  83. return APSInt(APInt::trunc(width), IsUnsigned);
  84. }
  85. APSInt extend(uint32_t width) const {
  86. if (IsUnsigned)
  87. return APSInt(zext(width), IsUnsigned);
  88. else
  89. return APSInt(sext(width), IsUnsigned);
  90. }
  91. APSInt extOrTrunc(uint32_t width) const {
  92. if (IsUnsigned)
  93. return APSInt(zextOrTrunc(width), IsUnsigned);
  94. else
  95. return APSInt(sextOrTrunc(width), IsUnsigned);
  96. }
  97. const APSInt &operator%=(const APSInt &RHS) {
  98. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  99. if (IsUnsigned)
  100. *this = urem(RHS);
  101. else
  102. *this = srem(RHS);
  103. return *this;
  104. }
  105. const APSInt &operator/=(const APSInt &RHS) {
  106. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  107. if (IsUnsigned)
  108. *this = udiv(RHS);
  109. else
  110. *this = sdiv(RHS);
  111. return *this;
  112. }
  113. APSInt operator%(const APSInt &RHS) const {
  114. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  115. return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
  116. }
  117. APSInt operator/(const APSInt &RHS) const {
  118. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  119. return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
  120. }
  121. APSInt operator>>(unsigned Amt) const {
  122. return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
  123. }
  124. APSInt& operator>>=(unsigned Amt) {
  125. if (IsUnsigned)
  126. lshrInPlace(Amt);
  127. else
  128. ashrInPlace(Amt);
  129. return *this;
  130. }
  131. inline bool operator<(const APSInt& RHS) const {
  132. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  133. return IsUnsigned ? ult(RHS) : slt(RHS);
  134. }
  135. inline bool operator>(const APSInt& RHS) const {
  136. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  137. return IsUnsigned ? ugt(RHS) : sgt(RHS);
  138. }
  139. inline bool operator<=(const APSInt& RHS) const {
  140. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  141. return IsUnsigned ? ule(RHS) : sle(RHS);
  142. }
  143. inline bool operator>=(const APSInt& RHS) const {
  144. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  145. return IsUnsigned ? uge(RHS) : sge(RHS);
  146. }
  147. inline bool operator==(const APSInt& RHS) const {
  148. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  149. return eq(RHS);
  150. }
  151. inline bool operator!=(const APSInt& RHS) const {
  152. return !((*this) == RHS);
  153. }
  154. bool operator==(int64_t RHS) const {
  155. return compareValues(*this, get(RHS)) == 0;
  156. }
  157. bool operator!=(int64_t RHS) const {
  158. return compareValues(*this, get(RHS)) != 0;
  159. }
  160. bool operator<=(int64_t RHS) const {
  161. return compareValues(*this, get(RHS)) <= 0;
  162. }
  163. bool operator>=(int64_t RHS) const {
  164. return compareValues(*this, get(RHS)) >= 0;
  165. }
  166. bool operator<(int64_t RHS) const {
  167. return compareValues(*this, get(RHS)) < 0;
  168. }
  169. bool operator>(int64_t RHS) const {
  170. return compareValues(*this, get(RHS)) > 0;
  171. }
  172. // The remaining operators just wrap the logic of APInt, but retain the
  173. // signedness information.
  174. APSInt operator<<(unsigned Bits) const {
  175. return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
  176. }
  177. APSInt& operator<<=(unsigned Amt) {
  178. static_cast<APInt&>(*this) <<= Amt;
  179. return *this;
  180. }
  181. APSInt& operator++() {
  182. ++(static_cast<APInt&>(*this));
  183. return *this;
  184. }
  185. APSInt& operator--() {
  186. --(static_cast<APInt&>(*this));
  187. return *this;
  188. }
  189. APSInt operator++(int) {
  190. return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
  191. }
  192. APSInt operator--(int) {
  193. return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
  194. }
  195. APSInt operator-() const {
  196. return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
  197. }
  198. APSInt& operator+=(const APSInt& RHS) {
  199. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  200. static_cast<APInt&>(*this) += RHS;
  201. return *this;
  202. }
  203. APSInt& operator-=(const APSInt& RHS) {
  204. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  205. static_cast<APInt&>(*this) -= RHS;
  206. return *this;
  207. }
  208. APSInt& operator*=(const APSInt& RHS) {
  209. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  210. static_cast<APInt&>(*this) *= RHS;
  211. return *this;
  212. }
  213. APSInt& operator&=(const APSInt& RHS) {
  214. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  215. static_cast<APInt&>(*this) &= RHS;
  216. return *this;
  217. }
  218. APSInt& operator|=(const APSInt& RHS) {
  219. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  220. static_cast<APInt&>(*this) |= RHS;
  221. return *this;
  222. }
  223. APSInt& operator^=(const APSInt& RHS) {
  224. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  225. static_cast<APInt&>(*this) ^= RHS;
  226. return *this;
  227. }
  228. APSInt operator&(const APSInt& RHS) const {
  229. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  230. return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
  231. }
  232. APSInt operator|(const APSInt& RHS) const {
  233. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  234. return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
  235. }
  236. APSInt operator^(const APSInt &RHS) const {
  237. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  238. return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
  239. }
  240. APSInt operator*(const APSInt& RHS) const {
  241. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  242. return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
  243. }
  244. APSInt operator+(const APSInt& RHS) const {
  245. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  246. return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
  247. }
  248. APSInt operator-(const APSInt& RHS) const {
  249. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  250. return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
  251. }
  252. APSInt operator~() const {
  253. return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
  254. }
  255. /// Return the APSInt representing the maximum integer value with the given
  256. /// bit width and signedness.
  257. static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
  258. return APSInt(Unsigned ? APInt::getMaxValue(numBits)
  259. : APInt::getSignedMaxValue(numBits), Unsigned);
  260. }
  261. /// Return the APSInt representing the minimum integer value with the given
  262. /// bit width and signedness.
  263. static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
  264. return APSInt(Unsigned ? APInt::getMinValue(numBits)
  265. : APInt::getSignedMinValue(numBits), Unsigned);
  266. }
  267. /// Determine if two APSInts have the same value, zero- or
  268. /// sign-extending as needed.
  269. static bool isSameValue(const APSInt &I1, const APSInt &I2) {
  270. return !compareValues(I1, I2);
  271. }
  272. /// Compare underlying values of two numbers.
  273. static int compareValues(const APSInt &I1, const APSInt &I2) {
  274. if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
  275. return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
  276. // Check for a bit-width mismatch.
  277. if (I1.getBitWidth() > I2.getBitWidth())
  278. return compareValues(I1, I2.extend(I1.getBitWidth()));
  279. if (I2.getBitWidth() > I1.getBitWidth())
  280. return compareValues(I1.extend(I2.getBitWidth()), I2);
  281. // We have a signedness mismatch. Check for negative values and do an
  282. // unsigned compare if both are positive.
  283. if (I1.isSigned()) {
  284. assert(!I2.isSigned() && "Expected signed mismatch");
  285. if (I1.isNegative())
  286. return -1;
  287. } else {
  288. assert(I2.isSigned() && "Expected signed mismatch");
  289. if (I2.isNegative())
  290. return 1;
  291. }
  292. return I1.compare(I2);
  293. }
  294. static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
  295. static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
  296. /// Used to insert APSInt objects, or objects that contain APSInt objects,
  297. /// into FoldingSets.
  298. void Profile(FoldingSetNodeID& ID) const;
  299. };
  300. inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
  301. inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
  302. inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
  303. inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
  304. inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
  305. inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
  306. inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
  307. I.print(OS, I.isSigned());
  308. return OS;
  309. }
  310. /// Provide DenseMapInfo for APSInt, using the DenseMapInfo for APInt.
  311. template <> struct DenseMapInfo<APSInt, void> {
  312. static inline APSInt getEmptyKey() {
  313. return APSInt(DenseMapInfo<APInt, void>::getEmptyKey());
  314. }
  315. static inline APSInt getTombstoneKey() {
  316. return APSInt(DenseMapInfo<APInt, void>::getTombstoneKey());
  317. }
  318. static unsigned getHashValue(const APSInt &Key) {
  319. return DenseMapInfo<APInt, void>::getHashValue(Key);
  320. }
  321. static bool isEqual(const APSInt &LHS, const APSInt &RHS) {
  322. return LHS.getBitWidth() == RHS.getBitWidth() &&
  323. LHS.isUnsigned() == RHS.isUnsigned() && LHS == RHS;
  324. }
  325. };
  326. } // end namespace llvm
  327. #endif
  328. #ifdef __GNUC__
  329. #pragma GCC diagnostic pop
  330. #endif