1193323Sed//===-- llvm/ADT/APSInt.cpp - Arbitrary Precision Signed Int ---*- C++ -*--===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9193323Sed// This file implements the APSInt class, which is a simple class that 10193323Sed// represents an arbitrary sized integer that knows its signedness. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#include "llvm/ADT/APSInt.h" 15193323Sed#include "llvm/ADT/FoldingSet.h" 16309124Sdim#include "llvm/ADT/StringRef.h" 17193323Sed 18193323Sedusing namespace llvm; 19193323Sed 20288943SdimAPSInt::APSInt(StringRef Str) { 21288943Sdim assert(!Str.empty() && "Invalid string length"); 22288943Sdim 23288943Sdim // (Over-)estimate the required number of bits. 24288943Sdim unsigned NumBits = ((Str.size() * 64) / 19) + 2; 25353358Sdim APInt Tmp(NumBits, Str, /*radix=*/10); 26288943Sdim if (Str[0] == '-') { 27288943Sdim unsigned MinBits = Tmp.getMinSignedBits(); 28288943Sdim if (MinBits > 0 && MinBits < NumBits) 29288943Sdim Tmp = Tmp.trunc(MinBits); 30353358Sdim *this = APSInt(Tmp, /*isUnsigned=*/false); 31288943Sdim return; 32288943Sdim } 33288943Sdim unsigned ActiveBits = Tmp.getActiveBits(); 34288943Sdim if (ActiveBits > 0 && ActiveBits < NumBits) 35288943Sdim Tmp = Tmp.trunc(ActiveBits); 36353358Sdim *this = APSInt(Tmp, /*isUnsigned=*/true); 37288943Sdim} 38288943Sdim 39193323Sedvoid APSInt::Profile(FoldingSetNodeID& ID) const { 40193323Sed ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0)); 41193323Sed APInt::Profile(ID); 42193323Sed} 43