[flang-commits] [flang] a05bae6 - [flang] Correct off-by-one error in SET_EXPONENT
peter klausler via flang-commits
flang-commits at lists.llvm.org
Thu Aug 12 12:51:14 PDT 2021
Author: peter klausler
Date: 2021-08-12T12:50:16-07:00
New Revision: a05bae6163a35115c24c447d52fcf48b28d91393
URL: https://github.com/llvm/llvm-project/commit/a05bae6163a35115c24c447d52fcf48b28d91393
DIFF: https://github.com/llvm/llvm-project/commit/a05bae6163a35115c24c447d52fcf48b28d91393.diff
LOG: [flang] Correct off-by-one error in SET_EXPONENT
SET_EXPONENT is returning values that are too large by a factor
of two.
Differential Revision: https://reviews.llvm.org/D107986
Added:
Modified:
flang/runtime/numeric.cpp
flang/unittests/Runtime/Numeric.cpp
Removed:
################################################################################
diff --git a/flang/runtime/numeric.cpp b/flang/runtime/numeric.cpp
index a0fb562d82abe..abdf03d6cd719 100644
--- a/flang/runtime/numeric.cpp
+++ b/flang/runtime/numeric.cpp
@@ -109,7 +109,7 @@ template <typename T> inline T SetExponent(T x, std::int64_t p) {
} else if (x == 0) {
return 0; // 0 -> 0
} else {
- int expo{std::ilogb(x)};
+ int expo{std::ilogb(x) + 1};
auto ip{static_cast<int>(p - expo)};
if (ip != p - expo) {
ip = p < 0 ? std::numeric_limits<int>::min()
diff --git a/flang/unittests/Runtime/Numeric.cpp b/flang/unittests/Runtime/Numeric.cpp
index cfaa46b6e070c..f37ac27a610e0 100644
--- a/flang/unittests/Runtime/Numeric.cpp
+++ b/flang/unittests/Runtime/Numeric.cpp
@@ -142,10 +142,10 @@ TEST(Numeric, Scale) {
TEST(Numeric, SetExponent) {
EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{0}, 0), 0);
EXPECT_EQ(RTNAME(SetExponent8)(Real<8>{0}, 666), 0);
- EXPECT_EQ(RTNAME(SetExponent8)(Real<8>{3.0}, 0), 1.5);
- EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{1.0}, 0), 1.0);
- EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{1.0}, 1), 2.0);
- EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{1.0}, -1), 0.5);
+ EXPECT_EQ(RTNAME(SetExponent8)(Real<8>{3.0}, 0), 0.75);
+ EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{1.0}, 0), 0.5);
+ EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{1.0}, 1), 1.0);
+ EXPECT_EQ(RTNAME(SetExponent4)(Real<4>{1.0}, -1), 0.25);
EXPECT_TRUE(std::isnan(
RTNAME(SetExponent4)(std::numeric_limits<Real<4>>::infinity(), 1)));
EXPECT_TRUE(std::isnan(
More information about the flang-commits
mailing list