From e88de8f822f28a6a171a5a4fff7c2352d9606e85 Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Tue, 19 Jan 2021 12:55:15 +0300 Subject: [PATCH] Fix signed/unsigned comparison warnings (#3900) They are treated as error, which leads to build failure. Tested on Ubuntu 20.04, gcc 9.3.0. --- .../src/plugin_api/precision_utils.h | 49 +++++++++++++++---- .../src/transformations/convert_precision.cpp | 2 +- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/inference-engine/src/plugin_api/precision_utils.h b/inference-engine/src/plugin_api/precision_utils.h index 696e1f70bde..68ffed3cd6c 100644 --- a/inference-engine/src/plugin_api/precision_utils.h +++ b/inference-engine/src/plugin_api/precision_utils.h @@ -134,6 +134,15 @@ f32tof16Arrays(ie_fp16* dst, const float* src, size_t nelem, float scale = 1.f, #pragma warning(disable : 4018) #endif +namespace details { + +// To overcame syntax parse error, when `>` comparison operator is threated as template closing bracket +constexpr inline bool Greater(size_t v1, size_t v2) { + return v1 > v2; +} + +} // namespace details + /** * @brief Converts one integral type to another saturating the result if the source value doesn't fit * into destination type range @@ -148,15 +157,29 @@ template ::value >::type* = nullptr> inline OutT saturate_cast(const InT& value) { - if (std::numeric_limits::max() > std::numeric_limits::max() && - std::numeric_limits::min() < std::numeric_limits::min()) { + using MaxT = typename std::conditional< + details::Greater(sizeof(OutT), sizeof(InT)), + typename std::make_unsigned::type, + typename std::make_unsigned::type + >::type; + using MinT = typename std::conditional< + details::Greater(sizeof(OutT), sizeof(InT)), + typename std::make_signed::type, + typename std::make_signed::type + >::type; + + static const MaxT OUT_MAX = static_cast(std::numeric_limits::max()); + static const MaxT IN_MAX = static_cast(std::numeric_limits::max()); + + static const MinT OUT_MIN = static_cast(std::numeric_limits::min()); + static const MinT IN_MIN = static_cast(std::numeric_limits::min()); + + if (OUT_MAX > IN_MAX && OUT_MIN < IN_MIN) { return static_cast(value); } - const InT max = std::numeric_limits::max() < std::numeric_limits::max() ? std::numeric_limits::max() : - std::numeric_limits::max(); - const InT min = std::numeric_limits::min() > std::numeric_limits::min() ? std::numeric_limits::min() : - std::numeric_limits::min(); + const InT max = static_cast(OUT_MAX < IN_MAX ? OUT_MAX : IN_MAX); + const InT min = static_cast(OUT_MIN > IN_MIN ? OUT_MIN : IN_MIN); return static_cast(std::min(std::max(value, min), max)); } @@ -175,12 +198,20 @@ template ::value >::type* = nullptr> inline OutT saturate_cast(const InT& value) { - if (std::numeric_limits::max() > std::numeric_limits::max()) { + using MaxT = typename std::conditional< + details::Greater(sizeof(OutT), sizeof(InT)), + typename std::make_unsigned::type, + typename std::make_unsigned::type + >::type; + + static const MaxT OUT_MAX = static_cast(std::numeric_limits::max()); + static const MaxT IN_MAX = static_cast(std::numeric_limits::max()); + + if (OUT_MAX > IN_MAX) { return static_cast(value); } - const InT max = std::numeric_limits::max() < std::numeric_limits::max() ? std::numeric_limits::max() : - std::numeric_limits::max(); + const InT max = static_cast(OUT_MAX < IN_MAX ? OUT_MAX : IN_MAX); return static_cast(std::min(value, max)); } diff --git a/inference-engine/src/transformations/src/transformations/convert_precision.cpp b/inference-engine/src/transformations/src/transformations/convert_precision.cpp index 37e603895f1..275b28e3137 100644 --- a/inference-engine/src/transformations/src/transformations/convert_precision.cpp +++ b/inference-engine/src/transformations/src/transformations/convert_precision.cpp @@ -327,7 +327,7 @@ inline dst_type convert_value(src_type val) { // and we don't need to compare and clamp the input to std::numeric_limits::lowest() template <> inline int32_t convert_value(uint64_t val) { - if (val > std::numeric_limits::max()) { + if (val > static_cast(std::numeric_limits::max())) { return std::numeric_limits::max(); } return static_cast(val);