From a1ade2b37db409b3c71147bf5bd27a40ded7ff11 Mon Sep 17 00:00:00 2001 From: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com> Date: Sun, 2 Oct 2022 23:25:44 +0200 Subject: Use parser for input parameters of type long (#2506) * Use parser for input parameters of type long * Revert "Use parser for input parameters of type long" This reverts commit 9573bb3f693f1247e77faa433fd96dc294e68361. * Use parser for inputs of type long * add safeCasttoLong function * Fix typo in comment --- Source/Utils/WarpXUtil.cpp | 55 ++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 21 deletions(-) (limited to 'Source/Utils/WarpXUtil.cpp') diff --git a/Source/Utils/WarpXUtil.cpp b/Source/Utils/WarpXUtil.cpp index afe8b7daa..eecc04b78 100644 --- a/Source/Utils/WarpXUtil.cpp +++ b/Source/Utils/WarpXUtil.cpp @@ -276,30 +276,43 @@ void Store_parserString(const amrex::ParmParse& pp, std::string query_string, f.clear(); } -int safeCastToInt(const amrex::Real x, const std::string& real_name) { - int result = 0; - bool error_detected = false; - std::string assert_msg; - // (2.0*(numeric_limits::max()/2+1)) converts numeric_limits::max()+1 to a real ensuring accuracy to all digits - // This accepts x = 2**31-1 but rejects 2**31. - using namespace amrex::literals; - constexpr amrex::Real max_range = (2.0_rt*static_cast(std::numeric_limits::max()/2+1)); - if (x < max_range) { - if (std::ceil(x) >= std::numeric_limits::min()) { - result = static_cast(x); +namespace WarpXUtilSafeCast { + template< typename int_type > + AMREX_FORCE_INLINE + int_type safeCastTo(const amrex::Real x, const std::string& real_name) { + int_type result = int_type(0); + bool error_detected = false; + std::string assert_msg; + // (2.0*(numeric_limits::max()/2+1)) converts numeric_limits::max()+1 to a real ensuring accuracy to all digits + // This accepts x = 2**31-1 but rejects 2**31. + using namespace amrex::literals; + constexpr amrex::Real max_range = (2.0_rt*static_cast(std::numeric_limits::max()/2+1)); + if (x < max_range) { + if (std::ceil(x) >= std::numeric_limits::min()) { + result = static_cast(x); + } else { + error_detected = true; + assert_msg = "Negative overflow detected when casting " + real_name + " = " + + std::to_string(x) + " to integer type"; + } + } else if (x > 0) { + error_detected = true; + assert_msg = "Overflow detected when casting " + real_name + " = " + std::to_string(x) + " to integer type"; } else { error_detected = true; - assert_msg = "Negative overflow detected when casting " + real_name + " = " + std::to_string(x) + " to int"; + assert_msg = "NaN detected when casting " + real_name + " to integer type"; } - } else if (x > 0) { - error_detected = true; - assert_msg = "Overflow detected when casting " + real_name + " = " + std::to_string(x) + " to int"; - } else { - error_detected = true; - assert_msg = "NaN detected when casting " + real_name + " to int"; - } - WARPX_ALWAYS_ASSERT_WITH_MESSAGE(!error_detected, assert_msg); - return result; + WARPX_ALWAYS_ASSERT_WITH_MESSAGE(!error_detected, assert_msg); + return result; + } +} + +int safeCastToInt(const amrex::Real x, const std::string& real_name) { + return WarpXUtilSafeCast::safeCastTo (x, real_name); +} + +long safeCastToLong(const amrex::Real x, const std::string& real_name) { + return WarpXUtilSafeCast::safeCastTo (x, real_name); } Parser makeParser (std::string const& parse_function, amrex::Vector const& varnames) -- cgit v1.2.3