diff --git a/tensorflow/compiler/xla/util.cc b/tensorflow/compiler/xla/util.cc index 6e7deda13f0..f39bd269ef4 100644 --- a/tensorflow/compiler/xla/util.cc +++ b/tensorflow/compiler/xla/util.cc @@ -367,15 +367,15 @@ string SanitizeFileName(string file_name) { // precision, Numerische Mathematik, vol. 18, pp. 224–242, 1971. std::pair SplitF64ToF32(double x) { const float x_f32 = static_cast(x); - // Early return if x is an infinity or NaN. - if (!std::isfinite(x)) { - return std::make_pair(x_f32, 0.0f); - } - // Only values within the range of F32 are supported, unless it is infinity. - // Small values with large negative exponents would be rounded to zero. + // Early return if x is an infinity or NaN. if (!std::isfinite(x_f32)) { - LOG(WARNING) << "Out of range F64 constant detected: " << x; + // Only values within the range of F32 are supported, unless it is infinity. + // Small values with large negative exponents would be rounded to zero. + if (std::isfinite(x)) { + LOG(WARNING) << "Out of range F64 constant detected: " << x; + } + return std::make_pair(x_f32, 0.0f); } // The high float is simply the double rounded to the nearest float. Because diff --git a/tensorflow/compiler/xla/util_test.cc b/tensorflow/compiler/xla/util_test.cc index 5477dfba18d..6f60e241d92 100644 --- a/tensorflow/compiler/xla/util_test.cc +++ b/tensorflow/compiler/xla/util_test.cc @@ -126,5 +126,13 @@ TEST(UtilTest, RoundTripFpToString) { "-nan"); } +TEST(UtilTest, SplitF64ToF32) { + // Overflowing the F32 exponent in SplitF64ToF32 should result in a pair of + // [∞,0]. + EXPECT_EQ(SplitF64ToF32(std::numeric_limits::max()).first, + std::numeric_limits::infinity()); + EXPECT_EQ(SplitF64ToF32(std::numeric_limits::max()).second, 0.0f); +} + } // namespace } // namespace xla