in native/src/seal/util/common.h [138:178]
SEAL_NODISCARD inline constexpr T mul_safe(T in1, T in2)
{
SEAL_IF_CONSTEXPR(std::is_unsigned<T>::value)
{
if (in1 && (in2 > (std::numeric_limits<T>::max)() / in1))
{
throw std::logic_error("unsigned overflow");
}
}
else
{
// Positive inputs
if ((in1 > 0) && (in2 > 0) && (in2 > (std::numeric_limits<T>::max)() / in1))
{
throw std::logic_error("signed overflow");
}
#if (SEAL_COMPILER == SEAL_COMPILER_MSVC) && !defined(SEAL_USE_IF_CONSTEXPR)
#pragma warning(push)
#pragma warning(disable : 4146)
#endif
// Negative inputs
else if ((in1 < 0) && (in2 < 0) && ((-in2) > (std::numeric_limits<T>::max)() / (-in1)))
{
throw std::logic_error("signed overflow");
}
// Negative in1; positive in2
else if ((in1 < 0) && (in2 > 0) && (in2 > (std::numeric_limits<T>::max)() / (-in1)))
{
throw std::logic_error("signed underflow");
}
#if (SEAL_COMPILER == SEAL_COMPILER_MSVC) && !defined(SEAL_USE_IF_CONSTEXPR)
#pragma warning(pop)
#endif
// Positive in1; negative in2
else if ((in1 > 0) && (in2 < 0) && (in2 < (std::numeric_limits<T>::min)() / in1))
{
throw std::logic_error("signed underflow");
}
}
return static_cast<T>(in1 * in2);
}