[llvm] [APFloat] Extend fltSemantics with field for explicit integer bit (PR #204860)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 19 09:30:38 PDT 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- llvm/include/llvm/ADT/APFloat.h llvm/lib/Support/APFloat.cpp llvm/unittests/ADT/APFloatTest.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index a36046d8e..8595b170b 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -103,12 +103,18 @@ constexpr fltSemantics APFloatBase::semFloat6E2M3FN = {
2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat4E2M1FN = {
2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
-constexpr fltSemantics APFloatBase::semX87DoubleExtended = {16383, -16382, 64,
- 80,
- fltNonfiniteBehavior::IEEE754,
- fltNanEncoding::IEEE,
- true,
- true, true, true, true};
+constexpr fltSemantics APFloatBase::semX87DoubleExtended = {
+ 16383,
+ -16382,
+ 64,
+ 80,
+ fltNonfiniteBehavior::IEEE754,
+ fltNanEncoding::IEEE,
+ true,
+ true,
+ true,
+ true,
+ true};
constexpr fltSemantics APFloatBase::semBogus = {0, 0, 0, 0};
constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {-1, 0, 0, 128};
constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = {
@@ -3714,15 +3720,17 @@ template <const fltSemantics &S>
void IEEEFloat::initFromIEEEAPInt(const APInt &api) {
assert(api.getBitWidth() == S.sizeInBits);
- constexpr unsigned int trailing_significand_bits = S.precision - 1 + S.hasExplicitIntegerBit;
+ constexpr unsigned int trailing_significand_bits =
+ S.precision - 1 + S.hasExplicitIntegerBit;
constexpr integerPart integer_bit =
integerPart{1} << (trailing_significand_bits % integerPartWidth);
constexpr uint64_t significand_mask = integer_bit - 1;
constexpr unsigned int exponent_bits =
S.sizeInBits - (S.hasSignedRepr ? 1 : 0) - trailing_significand_bits;
static_assert(exponent_bits < 64);
- constexpr unsigned int stored_significand_parts =
- partCountForBits(trailing_significand_bits + 2); // TODO: maybe needs +/-1, was S.hasExplicitIntegerBit ? +3 : +2
+ constexpr unsigned int stored_significand_parts = partCountForBits(
+ trailing_significand_bits +
+ 2); // TODO: maybe needs +/-1, was S.hasExplicitIntegerBit ? +3 : +2
constexpr uint64_t exponent_mask = (uint64_t{1} << exponent_bits) - 1;
constexpr bool is_zero_exp_reserved = S.hasDenormals || S.hasZero;
constexpr int bias = -(S.minExponent - (is_zero_exp_reserved ? 1 : 0));
@@ -3767,13 +3775,13 @@ void IEEEFloat::initFromIEEEAPInt(const APInt &api) {
if constexpr (S.hasExplicitIntegerBit) {
// This is only used and tested for x87DoubleExtended
static_assert(S.precision == 64);
- constexpr integerPart significand_mask_no_int_bit = (uint64_t{1} << (trailing_significand_bits - 1)) - 1;
- const integerPart myintegerbit = mysignificand[0] >> (trailing_significand_bits - 1);
- is_inf = myexponent - bias == ::exponentNaN(S) &&
- myintegerbit == 1 &&
- (mysignificand[0] & significand_mask_no_int_bit) == 0;
- }
- else {
+ constexpr integerPart significand_mask_no_int_bit =
+ (uint64_t{1} << (trailing_significand_bits - 1)) - 1;
+ const integerPart myintegerbit =
+ mysignificand[0] >> (trailing_significand_bits - 1);
+ is_inf = myexponent - bias == ::exponentNaN(S) && myintegerbit == 1 &&
+ (mysignificand[0] & significand_mask_no_int_bit) == 0;
+ } else {
is_inf = myexponent - bias == ::exponentInf(S) && all_zero_significand;
}
@@ -3802,13 +3810,14 @@ void IEEEFloat::initFromIEEEAPInt(const APInt &api) {
// This is only used and tested for x87DoubleExtended
static_assert(S.precision == 64);
const integerPart myintegerbit = mysignificand[0] >> 63;
- constexpr integerPart significand_mask_no_int_bit = (uint64_t{1} << (trailing_significand_bits - 1)) - 1;
- if (myexponent - bias != ::exponentNaN(S) && myexponent != 0 && myintegerbit == 0) {
+ constexpr integerPart significand_mask_no_int_bit =
+ (uint64_t{1} << (trailing_significand_bits - 1)) - 1;
+ if (myexponent - bias != ::exponentNaN(S) && myexponent != 0 &&
+ myintegerbit == 0) {
+ is_nan = true;
+ } else if (myexponent - bias == ::exponentNaN(S) &&
+ (mysignificand[0] & significand_mask_no_int_bit) == 0) {
is_nan = true;
- }
- else if (myexponent - bias == ::exponentNaN(S) &&
- (mysignificand[0] & significand_mask_no_int_bit) == 0) {
- is_nan = true;
}
}
@@ -3832,7 +3841,7 @@ void IEEEFloat::initFromIEEEAPInt(const APInt &api) {
exponent = S.minExponent;
else {
if constexpr (!S.hasExplicitIntegerBit) {
- significandParts()[mysignificand.size()-1] |= integer_bit;
+ significandParts()[mysignificand.size() - 1] |= integer_bit;
}
}
}
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 08cdbbe42..e43a0a6b1 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -7383,18 +7383,23 @@ TEST(APFloatTest, x87Next) {
EXPECT_TRUE(ilogb(F) == -1);
}
-static APInt makeX87Bits(bool sign, int64_t exponent, int integerBit, int64_t significand) {
- return (APInt(80, (sign ? 1 : 0)) << 79) | (APInt(80, exponent) << 64) | (APInt(80, integerBit) << 63) | APInt(80, significand);
+static APInt makeX87Bits(bool sign, int64_t exponent, int integerBit,
+ int64_t significand) {
+ return (APInt(80, (sign ? 1 : 0)) << 79) | (APInt(80, exponent) << 64) |
+ (APInt(80, integerBit) << 63) | APInt(80, significand);
}
-static APFloat makeX87FromBits(bool sign, int64_t exponent, int integerBit, int64_t significand) {
- return APFloat(APFloatBase::x87DoubleExtended(), makeX87Bits(sign, exponent, integerBit, significand));
+static APFloat makeX87FromBits(bool sign, int64_t exponent, int integerBit,
+ int64_t significand) {
+ return APFloat(APFloatBase::x87DoubleExtended(),
+ makeX87Bits(sign, exponent, integerBit, significand));
}
static APFloat makeX87(float value) {
bool losesInfo = false;
APFloat apf(value);
- apf.convert(APFloatBase::x87DoubleExtended(), llvm::APFloat::rmNearestTiesToEven, &losesInfo);
+ apf.convert(APFloatBase::x87DoubleExtended(),
+ llvm::APFloat::rmNearestTiesToEven, &losesInfo);
return apf;
}
@@ -7403,17 +7408,26 @@ TEST(APFloatTest, x87Bits) {
EXPECT_TRUE(makeX87FromBits(false, 0, 0, 0).bitwiseIsEqual(makeX87(0.0)));
EXPECT_TRUE(makeX87FromBits(false, bias, 1, 0).bitwiseIsEqual(makeX87(1.0)));
- EXPECT_TRUE(makeX87FromBits(false, bias+1, 1, 0).bitwiseIsEqual(makeX87(2.0)));
- EXPECT_TRUE(makeX87FromBits(true, bias+1, 1, 0).bitwiseIsEqual(makeX87(-2.0)));
- EXPECT_TRUE(makeX87FromBits(false, bias+2, 1, 0).bitwiseIsEqual(makeX87(4.0)));
- EXPECT_TRUE(makeX87FromBits(true, bias+2, 1, 0).bitwiseIsEqual(makeX87(-4.0)));
- EXPECT_TRUE(makeX87FromBits(false, bias+8, 1, 0).bitwiseIsEqual(makeX87(256.0)));
- EXPECT_TRUE(makeX87FromBits(true, bias+8, 1, 0).bitwiseIsEqual(makeX87(-256.0)));
+ EXPECT_TRUE(
+ makeX87FromBits(false, bias + 1, 1, 0).bitwiseIsEqual(makeX87(2.0)));
+ EXPECT_TRUE(
+ makeX87FromBits(true, bias + 1, 1, 0).bitwiseIsEqual(makeX87(-2.0)));
+ EXPECT_TRUE(
+ makeX87FromBits(false, bias + 2, 1, 0).bitwiseIsEqual(makeX87(4.0)));
+ EXPECT_TRUE(
+ makeX87FromBits(true, bias + 2, 1, 0).bitwiseIsEqual(makeX87(-4.0)));
+ EXPECT_TRUE(
+ makeX87FromBits(false, bias + 8, 1, 0).bitwiseIsEqual(makeX87(256.0)));
+ EXPECT_TRUE(
+ makeX87FromBits(true, bias + 8, 1, 0).bitwiseIsEqual(makeX87(-256.0)));
- EXPECT_EQ(makeX87FromBits(false, (1u << 14u) - 1u, 1, 0).bitcastToAPInt(), APInt(80, 0x7fff) << 63);
- EXPECT_EQ(makeX87FromBits(false, (1u << 14u) - 1u, 1, (1ul << 63ul) - 1ul).bitcastToAPInt(), (APInt(80, 1) << 78) - 1);
+ EXPECT_EQ(makeX87FromBits(false, (1u << 14u) - 1u, 1, 0).bitcastToAPInt(),
+ APInt(80, 0x7fff) << 63);
+ EXPECT_EQ(makeX87FromBits(false, (1u << 14u) - 1u, 1, (1ul << 63ul) - 1ul)
+ .bitcastToAPInt(),
+ (APInt(80, 1) << 78) - 1);
- const fltSemantics& S = APFloat::x87DoubleExtended();
+ const fltSemantics &S = APFloat::x87DoubleExtended();
// Test valid infinity: exp=0x7FFF, int_bit=1, fraction=0
{
@@ -7457,7 +7471,8 @@ TEST(APFloatTest, x87Bits) {
// Test pseudodenormal: exp=0, integer int_bit=1
{
- APFloat pseudoDenormal(APFloat::x87DoubleExtended(), makeX87Bits(false, 0, 1, 1));
+ APFloat pseudoDenormal(APFloat::x87DoubleExtended(),
+ makeX87Bits(false, 0, 1, 1));
EXPECT_TRUE(pseudoDenormal.isFinite());
EXPECT_FALSE(pseudoDenormal.isDenormal());
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/204860
More information about the llvm-commits
mailing list