Skip to content

[APFloat] add power #122889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,26 @@ inline APFloat abs(APFloat X) {
return X;
}

/// Returns X^N for N >= 0.
inline APFloat powi(const APFloat &X, int64_t N) {
APFloat Acc = APFloat::getOne(X.getSemantics());
if (N == 0) {
return Acc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should canonicalize the value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For N==0? Can you explain?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All paths through the function, but yes this one too. Signaling nans should be quieted

}
assert(N >= 0 && "negative exponents not supported.");
APFloat Base = X;
int64_t RemainingExponent = N;
while (RemainingExponent > 0) {
while (RemainingExponent % 2 == 0) {
Base = Base * Base;
RemainingExponent /= 2;
}
--RemainingExponent;
Acc = Acc * Base;
}
return Acc;
}

/// Returns the negated value of the argument.
inline APFloat neg(APFloat X) {
X.changeSign();
Expand Down
65 changes: 65 additions & 0 deletions llvm/unittests/ADT/APFloatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3793,6 +3793,71 @@ TEST(APFloatTest, abs) {
EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
}

TEST(APFloatTest, powi) {

// Simple cases.
APFloat One = APFloat::getOne(APFloat::IEEEsingle(), false);
APFloat Two = APFloat(APFloat::IEEEsingle(), "2.0");
APFloat NegTwo = APFloat(APFloat::IEEEsingle(), "-2.0");
APFloat Four = APFloat(APFloat::IEEEsingle(), "4.0");
APFloat Eight = APFloat(APFloat::IEEEsingle(), "8.0");
APFloat NegEight = APFloat(APFloat::IEEEsingle(), "-8.0");

EXPECT_TRUE(One.bitwiseIsEqual(powi(One, 0)));
EXPECT_TRUE(One.bitwiseIsEqual(powi(One, 3)));
EXPECT_TRUE(Four.bitwiseIsEqual(powi(Two, 2)));
EXPECT_TRUE(Eight.bitwiseIsEqual(powi(Two, 3)));

// See page 63 of IEEE 754-2019.
APFloat PosInf = APFloat::getInf(APFloat::IEEEsingle(), false);
APFloat NegInf = APFloat::getInf(APFloat::IEEEsingle(), true);
APFloat PosZero = APFloat::getZero(APFloat::IEEEsingle(), false);
APFloat NegZero = APFloat::getZero(APFloat::IEEEsingle(), true);
APFloat PosQNaN = APFloat::getNaN(APFloat::IEEEsingle(), false, 0x1337);
APFloat NegQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true, 0x1337);
APInt Payload(64, 0x1337);
APFloat PosSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false, &Payload);
APFloat NegSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), true, &Payload);

EXPECT_TRUE(One.bitwiseIsEqual(powi(PosInf, 0)));
EXPECT_TRUE(NegEight.bitwiseIsEqual(powi(NegTwo, 3)));
EXPECT_TRUE(PosZero.bitwiseIsEqual(powi(PosZero, 3)));
EXPECT_TRUE(NegZero.bitwiseIsEqual(powi(NegZero, 5)));
EXPECT_TRUE(PosZero.bitwiseIsEqual(powi(NegZero, 6)));
EXPECT_TRUE(NegInf.bitwiseIsEqual(powi(NegInf, 3)));
EXPECT_TRUE(PosInf.bitwiseIsEqual(powi(NegInf, 4)));
EXPECT_TRUE(PosInf.bitwiseIsEqual(powi(PosInf, 3)));

// Nans
EXPECT_TRUE(PosQNaN.bitwiseIsEqual(powi(PosQNaN, 1)));
EXPECT_TRUE(NegQNaN.bitwiseIsEqual(powi(NegQNaN, 1)));
// Check signaling NaN is quieted for n == 1.
EXPECT_TRUE(PosQNaN.bitwiseIsEqual(powi(PosSNaN, 1)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arsenm there are now tests for quieting SNaNs.
Now what should we test for correct rounding?

Copy link
Contributor Author

@ImanHosseini ImanHosseini Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gcc's powi builtin: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fpowi
: https://github.com/gcc-mirror/gcc/blob/26b2d9f27ca24f0705641a85f29d179fa0600869/libgcc/libgcc2.c#L2582
It is not named pown (to avoid confusion with guarantees that pown makes) but powi, and we aren't committing to any precision or rounding guarantee. Same as with gcc's powi.

EXPECT_TRUE(NegQNaN.bitwiseIsEqual(powi(NegSNaN, 1)));

APFloat LargestDenormalF64(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
APFloat NegLargestDenormalF64(APFloat::IEEEdouble(),
"-0x1.ffffffffffffep-1023");
APFloat Smallest = APFloat::getSmallest(APFloat::IEEEsingle(), false);
APFloat NegSmallest = APFloat::getSmallest(APFloat::IEEEsingle(), true);
APFloat Largest = APFloat::getLargest(APFloat::IEEEsingle(), false);
APFloat NegLargest = APFloat::getLargest(APFloat::IEEEsingle(), true);

EXPECT_TRUE(LargestDenormalF64.bitwiseIsEqual(powi(LargestDenormalF64, 1)));
EXPECT_TRUE(
NegLargestDenormalF64.bitwiseIsEqual(powi(NegLargestDenormalF64, 1)));
EXPECT_TRUE(Smallest.bitwiseIsEqual(powi(Smallest, 1)));
EXPECT_TRUE(NegSmallest.bitwiseIsEqual(powi(NegSmallest, 1)));
EXPECT_TRUE(Largest.bitwiseIsEqual(powi(Largest, 1)));
EXPECT_TRUE(NegLargest.bitwiseIsEqual(powi(NegLargest, 1)));

EXPECT_TRUE(PosZero.bitwiseIsEqual(powi(Smallest, 2)));
EXPECT_TRUE(NegZero.bitwiseIsEqual(powi(NegSmallest, 3)));
EXPECT_TRUE(powi(Largest, 2).isInfinity());
EXPECT_TRUE(powi(NegLargest, 2).isInfinity());
EXPECT_TRUE(powi(NegLargest, 3).isNegative());
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the zeroes, the infinities, nan (particularly that signaling nan is quieted with payload preserved in the no-op case). Also largest denormal / smallest normal, largest normal.

Copy link
Contributor Author

@ImanHosseini ImanHosseini Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything left out? I've added tests covering these.

TEST(APFloatTest, neg) {
APFloat One = APFloat(APFloat::IEEEsingle(), "1.0");
APFloat NegOne = APFloat(APFloat::IEEEsingle(), "-1.0");
Expand Down
Loading