2008-01-19 12:31:12 +08:00
|
|
|
//===-- llvm/ADT/APSInt.cpp - Arbitrary Precision Signed Int ---*- C++ -*--===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-01-19 12:31:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the APSInt class, which is a simple class that
|
|
|
|
// represents an arbitrary sized integer that knows its signedness.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/APSInt.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2016-04-16 15:51:28 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2020-03-03 01:24:11 +08:00
|
|
|
#include <cassert>
|
2008-01-19 12:31:12 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-06-24 02:22:10 +08:00
|
|
|
APSInt::APSInt(StringRef Str) {
|
|
|
|
assert(!Str.empty() && "Invalid string length");
|
|
|
|
|
|
|
|
// (Over-)estimate the required number of bits.
|
|
|
|
unsigned NumBits = ((Str.size() * 64) / 19) + 2;
|
2019-07-16 12:46:31 +08:00
|
|
|
APInt Tmp(NumBits, Str, /*radix=*/10);
|
2015-06-24 02:22:10 +08:00
|
|
|
if (Str[0] == '-') {
|
|
|
|
unsigned MinBits = Tmp.getMinSignedBits();
|
2020-06-10 22:35:42 +08:00
|
|
|
if (MinBits < NumBits)
|
|
|
|
Tmp = Tmp.trunc(std::max<unsigned>(1, MinBits));
|
2019-07-16 12:46:31 +08:00
|
|
|
*this = APSInt(Tmp, /*isUnsigned=*/false);
|
2015-06-24 02:22:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned ActiveBits = Tmp.getActiveBits();
|
2020-06-10 22:35:42 +08:00
|
|
|
if (ActiveBits < NumBits)
|
|
|
|
Tmp = Tmp.trunc(std::max<unsigned>(1, ActiveBits));
|
2019-07-16 12:46:31 +08:00
|
|
|
*this = APSInt(Tmp, /*isUnsigned=*/true);
|
2015-06-24 02:22:10 +08:00
|
|
|
}
|
|
|
|
|
2008-01-19 12:31:12 +08:00
|
|
|
void APSInt::Profile(FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0));
|
|
|
|
APInt::Profile(ID);
|
|
|
|
}
|