cxxopts icon indicating copy to clipboard operation
cxxopts copied to clipboard

Operations out of range

Open susilehtola opened this issue 6 years ago • 4 comments

Compiling an application against cxxopts with Intel 18 compiler yields the following warnings

cxxopts.hpp(471): warning #61: integer operation result is out of range
              if (u > static_cast<U>(-std::numeric_limits<T>::min()))
                                     ^
          detected during:
            instantiation of "void cxxopts::values::detail::SignedCheck<T, true>::operator()(bool, U, const std::string &) [with T=int32_t={int}, U=unsigned int]" at line 498
            instantiation of "void cxxopts::values::detail::check_signed_range<T,U>(bool, U, const std::string &) [with T=int32_t={int}, U=unsigned int]" at line 577
            instantiation of "void cxxopts::values::integer_parser(const std::string &, T &) [with T=int32_t={int}]" at line 640

cxxopts.hpp(471): warning #61: integer operation result is out of range
              if (u > static_cast<U>(-std::numeric_limits<T>::min()))
                                     ^
          detected during:
            instantiation of "void cxxopts::values::detail::SignedCheck<T, true>::operator()(bool, U, const std::string &) [with T=int64_t={long}, U=unsigned long]" at line 498
            instantiation of "void cxxopts::values::detail::check_signed_range<T,U>(bool, U, const std::string &) [with T=int64_t={long}, U=unsigned long]" at line 577
            instantiation of "void cxxopts::values::integer_parser(const std::string &, T &) [with T=int64_t={long}]" at line 654

susilehtola avatar Apr 05 '18 08:04 susilehtola

I'm not sure what to do about this one, especially given that the other compilers don't complain. I'll have a look in the standard to see what it says about this.

Whilst -INT_MIN is out of range of int (it is INT_MAX+1), it is then cast to unsigned int, which is in range. This can be calculated at compile time, so I would expect this to be turned into a constant that is within the range of the respective types.

jarro2783 avatar Apr 05 '18 21:04 jarro2783

I am also seeing this with the gcc 5.4.0 compiler:

../common/cxxopts.hpp(471): warning: integer operation result is out of range
          detected during:
            instantiation of "void cxxopts::values::detail::SignedCheck<T, true>::operator()(__nv_bool, U, const std::__cxx11::string &) [with T=int32_t, U=uint32_t]"
(498): here
            instantiation of "void cxxopts::values::detail::check_signed_range<T,U>(__nv_bool, U, const std::__cxx11::string &) [with T=int32_t, U=uint32_t]"
(577): here
            instantiation of "void cxxopts::values::integer_parser(const std::__cxx11::string &, T &) [with T=int32_t]"
(640): here

../common/cxxopts.hpp(471): warning: integer operation result is out of range
          detected during:
            instantiation of "void cxxopts::values::detail::SignedCheck<T, true>::operator()(__nv_bool, U, const std::__cxx11::string &) [with T=int64_t, U=uint_fast32_t]"
(498): here
            instantiation of "void cxxopts::values::detail::check_signed_range<T,U>(__nv_bool, U, const std::__cxx11::string &) [with T=int64_t, U=uint_fast32_t]"
(577): here
            instantiation of "void cxxopts::values::integer_parser(const std::__cxx11::string &, T &) [with T=int64_t]"
(654): here
gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609

Here's the code being compiled:

    cxxopts::Options options(argv[0], "Agent based SEIR model on the GPU with CUDA.");
    options.add_options()
        ("c,count",      "Number of agents [2^10=1048576].", cxxopts::value<int>()->default_value("1048576"))
        ("t,time",       "Number of time steps [90].",       cxxopts::value<int>()->default_value("90"))
        ("p,prevalence", "Initial prevalence [1%%].",        cxxopts::value<float>()->default_value("0.01"));

    auto args = options.parse(argc, argv);
    agent_count        = uint32_t(args["count"].as<int>());
    time_steps         = uint32_t(args["time"].as<int>());
    initial_prevalence = args["prevalence"].as<float>();

clorton avatar Jul 27 '18 18:07 clorton

Not sure if this is still relevant, how about:

if (u > static_cast<U>(-(std::numeric_limits<T>::min() + 1)) + 1)

rzolau avatar Apr 26 '19 02:04 rzolau

I receive this error with Intel icpc 2017 compiler, with T=int32_t and T=int64_t.

AerysL avatar May 16 '19 18:05 AerysL