From 2d5cbf1a2adad4c50195047f1a2aa3cfb6d5c5db Mon Sep 17 00:00:00 2001 From: Artem Belevich Date: Thu, 19 Dec 2019 13:02:01 -0800 Subject: [PATCH] Provide integer variants of isnan/isfinite to work around a quirk in MSVC standard library. PiperOrigin-RevId: 286447750 Change-Id: I5af8054ac69782e351cff440d88824eade90f963 --- tensorflow/compiler/xla/service/hlo_parser.cc | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tensorflow/compiler/xla/service/hlo_parser.cc b/tensorflow/compiler/xla/service/hlo_parser.cc index ef58b37b469..46fa47eaf1a 100644 --- a/tensorflow/compiler/xla/service/hlo_parser.cc +++ b/tensorflow/compiler/xla/service/hlo_parser.cc @@ -2611,18 +2611,37 @@ struct MinMaxFiniteValue { static double min() { return -max(); } }; +// MSVC's standard C++ library does not define isnan/isfinite for integer types. +// To work around that we will need to provide our own. +template +std::enable_if_t::value, bool> IsFinite(T val) { + return std::isfinite(val); +} +template +std::enable_if_t::value, bool> IsNaN(T val) { + return std::isnan(val); +} +template +std::enable_if_t::value, bool> IsFinite(T val) { + return std::isfinite(static_cast(val)); +} +template +std::enable_if_t::value, bool> IsNaN(T val) { + return std::isnan(static_cast(val)); +} + template bool HloParserImpl::CheckParsedValueIsInRange(LocTy loc, ParsedElemT value) { if (std::is_floating_point::value) { auto value_as_native_t = static_cast(value); auto value_double_converted = static_cast(value_as_native_t); - if (!std::isfinite(value) || std::isfinite(value_double_converted)) { + if (!IsFinite(value) || IsFinite(value_double_converted)) { value = value_double_converted; } } PrimitiveType literal_ty = primitive_util::NativeToPrimitiveType(); - if (std::isnan(value) || + if (IsNaN(value) || (std::numeric_limits::has_infinity && (std::numeric_limits::infinity() == value || -std::numeric_limits::infinity() == value))) {