2008-01-19 05:31:12 +01:00
|
|
|
//===-- llvm/ADT/APSInt.cpp - Arbitrary Precision Signed Int ---*- C++ -*--===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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 05:31:12 +01: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 09:51:28 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2020-03-02 18:24:11 +01:00
|
|
|
#include <cassert>
|
2008-01-19 05:31:12 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-06-23 20:22:10 +02: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 06:46:31 +02:00
|
|
|
APInt Tmp(NumBits, Str, /*radix=*/10);
|
2015-06-23 20:22:10 +02:00
|
|
|
if (Str[0] == '-') {
|
|
|
|
unsigned MinBits = Tmp.getMinSignedBits();
|
2020-06-10 16:35:42 +02:00
|
|
|
if (MinBits < NumBits)
|
|
|
|
Tmp = Tmp.trunc(std::max<unsigned>(1, MinBits));
|
2019-07-16 06:46:31 +02:00
|
|
|
*this = APSInt(Tmp, /*isUnsigned=*/false);
|
2015-06-23 20:22:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned ActiveBits = Tmp.getActiveBits();
|
2020-06-10 16:35:42 +02:00
|
|
|
if (ActiveBits < NumBits)
|
|
|
|
Tmp = Tmp.trunc(std::max<unsigned>(1, ActiveBits));
|
2019-07-16 06:46:31 +02:00
|
|
|
*this = APSInt(Tmp, /*isUnsigned=*/true);
|
2015-06-23 20:22:10 +02:00
|
|
|
}
|
|
|
|
|
2008-01-19 05:31:12 +01:00
|
|
|
void APSInt::Profile(FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0));
|
|
|
|
APInt::Profile(ID);
|
|
|
|
}
|