diff options
Diffstat (limited to 'Source/Utils/WarpXUtil.cpp')
-rw-r--r-- | Source/Utils/WarpXUtil.cpp | 55 |
1 files changed, 34 insertions, 21 deletions
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<int>::max()/2+1)) converts numeric_limits<int>::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<amrex::Real>(std::numeric_limits<int>::max()/2+1)); - if (x < max_range) { - if (std::ceil(x) >= std::numeric_limits<int>::min()) { - result = static_cast<int>(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<int>::max()/2+1)) converts numeric_limits<int>::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<amrex::Real>(std::numeric_limits<int_type>::max()/2+1)); + if (x < max_range) { + if (std::ceil(x) >= std::numeric_limits<int_type>::min()) { + result = static_cast<int_type>(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<int> (x, real_name); +} + +long safeCastToLong(const amrex::Real x, const std::string& real_name) { + return WarpXUtilSafeCast::safeCastTo<long> (x, real_name); } Parser makeParser (std::string const& parse_function, amrex::Vector<std::string> const& varnames) |