From a05bae6163a35115c24c447d52fcf48b28d91393 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Thu, 12 Aug 2021 11:10:53 -0700 Subject: [PATCH] [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 --- flang/runtime/numeric.cpp | 2 +- flang/unittests/Runtime/Numeric.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/flang/runtime/numeric.cpp b/flang/runtime/numeric.cpp index a0fb562d82ab..abdf03d6cd71 100644 --- a/flang/runtime/numeric.cpp +++ b/flang/runtime/numeric.cpp @@ -109,7 +109,7 @@ template 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(p - expo)}; if (ip != p - expo) { ip = p < 0 ? std::numeric_limits::min() diff --git a/flang/unittests/Runtime/Numeric.cpp b/flang/unittests/Runtime/Numeric.cpp index cfaa46b6e070..f37ac27a610e 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>::infinity(), 1))); EXPECT_TRUE(std::isnan(