Path: blob/main/contrib/llvm-project/llvm/lib/Support/APSInt.cpp
35234 views
//===-- llvm/ADT/APSInt.cpp - Arbitrary Precision Signed Int ---*- C++ -*--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the APSInt class, which is a simple class that9// represents an arbitrary sized integer that knows its signedness.10//11//===----------------------------------------------------------------------===//1213#include "llvm/ADT/APSInt.h"14#include "llvm/ADT/FoldingSet.h"15#include "llvm/ADT/StringRef.h"16#include <cassert>1718using namespace llvm;1920APSInt::APSInt(StringRef Str) {21assert(!Str.empty() && "Invalid string length");2223// (Over-)estimate the required number of bits.24unsigned NumBits = ((Str.size() * 64) / 19) + 2;25APInt Tmp(NumBits, Str, /*radix=*/10);26if (Str[0] == '-') {27unsigned MinBits = Tmp.getSignificantBits();28if (MinBits < NumBits)29Tmp = Tmp.trunc(std::max<unsigned>(1, MinBits));30*this = APSInt(Tmp, /*isUnsigned=*/false);31return;32}33unsigned ActiveBits = Tmp.getActiveBits();34if (ActiveBits < NumBits)35Tmp = Tmp.trunc(std::max<unsigned>(1, ActiveBits));36*this = APSInt(Tmp, /*isUnsigned=*/true);37}3839void APSInt::Profile(FoldingSetNodeID& ID) const {40ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0));41APInt::Profile(ID);42}434445