[llvm] 24ecfda - [APFloat] Fix uninitialized variable in IEEEFloat constructors
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 9 03:43:30 PDT 2020
Author: Simon Pilgrim
Date: 2020-09-09T11:38:30+01:00
New Revision: 24ecfdac7b7d195795b6cb0e373cba8bfa7911f4
URL: https://github.com/llvm/llvm-project/commit/24ecfdac7b7d195795b6cb0e373cba8bfa7911f4
DIFF: https://github.com/llvm/llvm-project/commit/24ecfdac7b7d195795b6cb0e373cba8bfa7911f4.diff
LOG: [APFloat] Fix uninitialized variable in IEEEFloat constructors
Some constructors of IEEEFloat do not initialize member variable exponent.
Fix it by initializing exponent with the following values:
For NaNs, the `exponent` is `maxExponent+1`.
For Infinities, the `exponent` is `maxExponent+1`.
For Zeroes, the `exponent` is `maxExponent-1`.
Patch by: @nullptr.cpp (Yang Fan)
Differential Revision: https://reviews.llvm.org/D86997
Added:
Modified:
llvm/include/llvm/ADT/APFloat.h
llvm/lib/Support/APFloat.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 876e52c150a0..1f9ac22621a6 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -249,7 +249,7 @@ class IEEEFloat final : public APFloatBase {
/// \name Constructors
/// @{
- IEEEFloat(const fltSemantics &); // Default construct to 0.0
+ IEEEFloat(const fltSemantics &); // Default construct to +0.0
IEEEFloat(const fltSemantics &, integerPart);
IEEEFloat(const fltSemantics &, uninitializedTag);
IEEEFloat(const fltSemantics &, const APInt &);
@@ -539,6 +539,9 @@ class IEEEFloat final : public APFloatBase {
roundingMode) const;
opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
roundingMode);
+ ExponentType exponentNaN() const;
+ ExponentType exponentInf() const;
+ ExponentType exponentZero() const;
/// @}
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 569cac790af9..7a4c8bd3639d 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -755,6 +755,7 @@ void IEEEFloat::copySignificand(const IEEEFloat &rhs) {
void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) {
category = fcNaN;
sign = Negative;
+ exponent = exponentNaN();
integerPart *significand = significandParts();
unsigned numParts = partCount();
@@ -925,8 +926,7 @@ IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) {
IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) {
initialize(&ourSemantics);
- category = fcZero;
- sign = false;
+ makeZero(false);
}
// Delegate to the previous constructor, because later copy constructor may
@@ -3379,15 +3379,13 @@ void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) {
sign = static_cast<unsigned int>(i2>>15);
if (myexponent == 0 && mysignificand == 0) {
- // exponent, significand meaningless
- category = fcZero;
+ makeZero(sign);
} else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
- // exponent, significand meaningless
- category = fcInfinity;
+ makeInf(sign);
} else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
(myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
- // exponent meaningless
category = fcNaN;
+ exponent = exponentNaN();
significandParts()[0] = mysignificand;
significandParts()[1] = 0;
} else {
@@ -3438,16 +3436,14 @@ void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) {
sign = static_cast<unsigned int>(i2>>63);
if (myexponent==0 &&
(mysignificand==0 && mysignificand2==0)) {
- // exponent, significand meaningless
- category = fcZero;
+ makeZero(sign);
} else if (myexponent==0x7fff &&
(mysignificand==0 && mysignificand2==0)) {
- // exponent, significand meaningless
- category = fcInfinity;
+ makeInf(sign);
} else if (myexponent==0x7fff &&
(mysignificand!=0 || mysignificand2 !=0)) {
- // exponent meaningless
category = fcNaN;
+ exponent = exponentNaN();
significandParts()[0] = mysignificand;
significandParts()[1] = mysignificand2;
} else {
@@ -3473,14 +3469,12 @@ void IEEEFloat::initFromDoubleAPInt(const APInt &api) {
sign = static_cast<unsigned int>(i>>63);
if (myexponent==0 && mysignificand==0) {
- // exponent, significand meaningless
- category = fcZero;
+ makeZero(sign);
} else if (myexponent==0x7ff && mysignificand==0) {
- // exponent, significand meaningless
- category = fcInfinity;
+ makeInf(sign);
} else if (myexponent==0x7ff && mysignificand!=0) {
- // exponent meaningless
category = fcNaN;
+ exponent = exponentNaN();
*significandParts() = mysignificand;
} else {
category = fcNormal;
@@ -3504,14 +3498,12 @@ void IEEEFloat::initFromFloatAPInt(const APInt &api) {
sign = i >> 31;
if (myexponent==0 && mysignificand==0) {
- // exponent, significand meaningless
- category = fcZero;
+ makeZero(sign);
} else if (myexponent==0xff && mysignificand==0) {
- // exponent, significand meaningless
- category = fcInfinity;
+ makeInf(sign);
} else if (myexponent==0xff && mysignificand!=0) {
- // sign, exponent, significand meaningless
category = fcNaN;
+ exponent = exponentNaN();
*significandParts() = mysignificand;
} else {
category = fcNormal;
@@ -3535,14 +3527,12 @@ void IEEEFloat::initFromBFloatAPInt(const APInt &api) {
sign = i >> 15;
if (myexponent == 0 && mysignificand == 0) {
- // exponent, significand meaningless
- category = fcZero;
+ makeZero(sign);
} else if (myexponent == 0xff && mysignificand == 0) {
- // exponent, significand meaningless
- category = fcInfinity;
+ makeInf(sign);
} else if (myexponent == 0xff && mysignificand != 0) {
- // sign, exponent, significand meaningless
category = fcNaN;
+ exponent = exponentNaN();
*significandParts() = mysignificand;
} else {
category = fcNormal;
@@ -3566,14 +3556,12 @@ void IEEEFloat::initFromHalfAPInt(const APInt &api) {
sign = i >> 15;
if (myexponent==0 && mysignificand==0) {
- // exponent, significand meaningless
- category = fcZero;
+ makeZero(sign);
} else if (myexponent==0x1f && mysignificand==0) {
- // exponent, significand meaningless
- category = fcInfinity;
+ makeInf(sign);
} else if (myexponent==0x1f && mysignificand!=0) {
- // sign, exponent, significand meaningless
category = fcNaN;
+ exponent = exponentNaN();
*significandParts() = mysignificand;
} else {
category = fcNormal;
@@ -4131,17 +4119,29 @@ IEEEFloat::opStatus IEEEFloat::next(bool nextDown) {
return result;
}
+APFloatBase::ExponentType IEEEFloat::exponentNaN() const {
+ return semantics->maxExponent + 1;
+}
+
+APFloatBase::ExponentType IEEEFloat::exponentInf() const {
+ return semantics->maxExponent + 1;
+}
+
+APFloatBase::ExponentType IEEEFloat::exponentZero() const {
+ return semantics->minExponent - 1;
+}
+
void IEEEFloat::makeInf(bool Negative) {
category = fcInfinity;
sign = Negative;
- exponent = semantics->maxExponent + 1;
+ exponent = exponentInf();
APInt::tcSet(significandParts(), 0, partCount());
}
void IEEEFloat::makeZero(bool Negative) {
category = fcZero;
sign = Negative;
- exponent = semantics->minExponent-1;
+ exponent = exponentZero();
APInt::tcSet(significandParts(), 0, partCount());
}
More information about the llvm-commits
mailing list