Skip to content

Commit a54d88f

Browse files
authored
[APFloat] Fix APFloat::getOne (#112308)
`APFloat::APFloat(const fltSemantics &Semantics, integerPart I)` interprets 'I' as a unsigned integer. Fix the bug found in #112113 (comment).
1 parent b3a8400 commit a54d88f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,10 @@ class APFloat : public APFloatBase {
10461046
///
10471047
/// \param Negative True iff the number should be negative.
10481048
static APFloat getOne(const fltSemantics &Sem, bool Negative = false) {
1049-
return APFloat(Sem, Negative ? -1 : 1);
1049+
APFloat Val(Sem, 1U);
1050+
if (Negative)
1051+
Val.changeSign();
1052+
return Val;
10501053
}
10511054

10521055
/// Factory for Positive and Negative Infinity.

llvm/unittests/ADT/APFloatTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ TEST(APFloatTest, Zero) {
892892
EXPECT_EQ(fcNegZero, APFloat(-0.0).classify());
893893
}
894894

895+
TEST(APFloatTest, getOne) {
896+
EXPECT_EQ(APFloat::getOne(APFloat::IEEEsingle(), false).convertToFloat(),
897+
1.0f);
898+
EXPECT_EQ(APFloat::getOne(APFloat::IEEEsingle(), true).convertToFloat(),
899+
-1.0f);
900+
}
901+
895902
TEST(APFloatTest, DecimalStringsWithoutNullTerminators) {
896903
// Make sure that we can parse strings without null terminators.
897904
// rdar://14323230.

0 commit comments

Comments
 (0)