Esempio n. 1
0
inline CPLSafeInt<int> operator*( const CPLSafeInt<int>& A,
                                  const CPLSafeInt<int>& B )
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
    int res;
    if( __builtin_smul_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 SafeMulSigned(A,B);
#endif
}
Esempio n. 2
0
int safe_multiply(int32_t a, int32_t b, int * error){

    int32_t result;
  // New in GCC5 to check for overflow when multiplying.
  // Why did this take 30 years to get into a standard library?
  int overflow = __builtin_smul_overflow(a, b, &result);
  if (overflow){
    *error = 1;
  }
  return result;
}
constexpr Result<int> smul(int lhs, int rhs) {
  int sum{};
  return {__builtin_smul_overflow(lhs, rhs, &sum), sum};
}