xref: /aosp_15_r20/external/llvm/lib/Support/APSInt.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- llvm/ADT/APSInt.cpp - Arbitrary Precision Signed Int ---*- C++ -*--===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the APSInt class, which is a simple class that
11*9880d681SAndroid Build Coastguard Worker // represents an arbitrary sized integer that knows its signedness.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/APSInt.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/FoldingSet.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker using namespace llvm;
20*9880d681SAndroid Build Coastguard Worker 
APSInt(StringRef Str)21*9880d681SAndroid Build Coastguard Worker APSInt::APSInt(StringRef Str) {
22*9880d681SAndroid Build Coastguard Worker   assert(!Str.empty() && "Invalid string length");
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker   // (Over-)estimate the required number of bits.
25*9880d681SAndroid Build Coastguard Worker   unsigned NumBits = ((Str.size() * 64) / 19) + 2;
26*9880d681SAndroid Build Coastguard Worker   APInt Tmp(NumBits, Str, /*Radix=*/10);
27*9880d681SAndroid Build Coastguard Worker   if (Str[0] == '-') {
28*9880d681SAndroid Build Coastguard Worker     unsigned MinBits = Tmp.getMinSignedBits();
29*9880d681SAndroid Build Coastguard Worker     if (MinBits > 0 && MinBits < NumBits)
30*9880d681SAndroid Build Coastguard Worker       Tmp = Tmp.trunc(MinBits);
31*9880d681SAndroid Build Coastguard Worker     *this = APSInt(Tmp, /*IsUnsigned=*/false);
32*9880d681SAndroid Build Coastguard Worker     return;
33*9880d681SAndroid Build Coastguard Worker   }
34*9880d681SAndroid Build Coastguard Worker   unsigned ActiveBits = Tmp.getActiveBits();
35*9880d681SAndroid Build Coastguard Worker   if (ActiveBits > 0 && ActiveBits < NumBits)
36*9880d681SAndroid Build Coastguard Worker     Tmp = Tmp.trunc(ActiveBits);
37*9880d681SAndroid Build Coastguard Worker   *this = APSInt(Tmp, /*IsUnsigned=*/true);
38*9880d681SAndroid Build Coastguard Worker }
39*9880d681SAndroid Build Coastguard Worker 
Profile(FoldingSetNodeID & ID) const40*9880d681SAndroid Build Coastguard Worker void APSInt::Profile(FoldingSetNodeID& ID) const {
41*9880d681SAndroid Build Coastguard Worker   ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0));
42*9880d681SAndroid Build Coastguard Worker   APInt::Profile(ID);
43*9880d681SAndroid Build Coastguard Worker }
44