inline CPLSafeInt<int> operator+( const CPLSafeInt<int>& A, const CPLSafeInt<int>& B ) { #ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE int res; if( __builtin_sadd_overflow(A.v(), B.v(), &res) ) throw CPLSafeIntOverflow(); return CPLSM(res); #elif defined(_MSC_VER) msl::utilities::SafeInt<int, CPLMSVCSafeIntException> A2(A.v()); msl::utilities::SafeInt<int, CPLMSVCSafeIntException> B2(B.v()); return CPLSM(static_cast<int>(A2 + B2)); #elif defined(CPL_HAS_GINT64) const int a = A.v(); const int b = B.v(); const GInt64 res = static_cast<GInt64>(a) + b; if( res < std::numeric_limits<int>::min() || res > std::numeric_limits<int>::max() ) { throw CPLSafeIntOverflow(); } return CPLSM(static_cast<int>(res)); #else return SafeAddSigned(A,B); #endif }
/* https://codereview.stackexchange.com/questions/37177/simpler-method-to-detect-int-overflow */ int safe_add(int32_t a, int32_t b, int * error) { int32_t result; int overflow = __builtin_sadd_overflow(a, b, &result); if (overflow){ *error = overflow; } return result; }
constexpr Result<int> sadd(int lhs, int rhs) { int sum{}; return {__builtin_sadd_overflow(lhs, rhs, &sum), sum}; }
int foo (int x, int y) { int r; return __builtin_sadd_overflow (x, y, &r); }