Skip to content

Commit

Permalink
[APFloat] Fix APFloat::getOne (#112308)
Browse files Browse the repository at this point in the history
`APFloat::APFloat(const fltSemantics &Semantics, integerPart I)`
interprets 'I' as a unsigned integer.
Fix the bug found in
#112113 (comment).
  • Loading branch information
dtcxzyw authored Oct 15, 2024
1 parent b3a8400 commit a54d88f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,10 @@ class APFloat : public APFloatBase {
///
/// \param Negative True iff the number should be negative.
static APFloat getOne(const fltSemantics &Sem, bool Negative = false) {
return APFloat(Sem, Negative ? -1 : 1);
APFloat Val(Sem, 1U);
if (Negative)
Val.changeSign();
return Val;
}

/// Factory for Positive and Negative Infinity.
Expand Down
7 changes: 7 additions & 0 deletions llvm/unittests/ADT/APFloatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,13 @@ TEST(APFloatTest, Zero) {
EXPECT_EQ(fcNegZero, APFloat(-0.0).classify());
}

TEST(APFloatTest, getOne) {
EXPECT_EQ(APFloat::getOne(APFloat::IEEEsingle(), false).convertToFloat(),
1.0f);
EXPECT_EQ(APFloat::getOne(APFloat::IEEEsingle(), true).convertToFloat(),
-1.0f);
}

TEST(APFloatTest, DecimalStringsWithoutNullTerminators) {
// Make sure that we can parse strings without null terminators.
// rdar://14323230.
Expand Down

0 comments on commit a54d88f

Please sign in to comment.