mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 12:12:47 +01:00
3637aff1ea
Summary: When constructing an APSInt from a string, the constructor doesn't correctly truncate the bit width of the result if the passed in string was "0" (or any alternative way to express 0 like "-0" or "000"). Instead of 1 (which is the smallest allowed bit width) it returns an APSInt with a bit width of 5. The reason is that the constructor checks that it never truncates the result to the invalid bit width of 0, so when it calculates that storing a "0" doesn't require any bits it just keeps the original overestimated bit width (which happens to be 5). This patch just sets the bit width of the result to 1 if the required bit width is 0. Reviewers: arphaman, dexonsmith Reviewed By: dexonsmith Subscribers: hiraditya, dexonsmith, JDevlieghere, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D81329
44 lines
1.4 KiB
C++
44 lines
1.4 KiB
C++
//===-- llvm/ADT/APSInt.cpp - Arbitrary Precision Signed Int ---*- C++ -*--===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include <cassert>
|
|
|
|
using namespace llvm;
|
|
|
|
APSInt::APSInt(StringRef Str) {
|
|
assert(!Str.empty() && "Invalid string length");
|
|
|
|
// (Over-)estimate the required number of bits.
|
|
unsigned NumBits = ((Str.size() * 64) / 19) + 2;
|
|
APInt Tmp(NumBits, Str, /*radix=*/10);
|
|
if (Str[0] == '-') {
|
|
unsigned MinBits = Tmp.getMinSignedBits();
|
|
if (MinBits < NumBits)
|
|
Tmp = Tmp.trunc(std::max<unsigned>(1, MinBits));
|
|
*this = APSInt(Tmp, /*isUnsigned=*/false);
|
|
return;
|
|
}
|
|
unsigned ActiveBits = Tmp.getActiveBits();
|
|
if (ActiveBits < NumBits)
|
|
Tmp = Tmp.trunc(std::max<unsigned>(1, ActiveBits));
|
|
*this = APSInt(Tmp, /*isUnsigned=*/true);
|
|
}
|
|
|
|
void APSInt::Profile(FoldingSetNodeID& ID) const {
|
|
ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0));
|
|
APInt::Profile(ID);
|
|
}
|