diff --git a/src/FixedPoints/SFixedFreeFunctions.h b/src/FixedPoints/SFixedFreeFunctions.h index e6df4bd..b116d65 100644 --- a/src/FixedPoints/SFixedFreeFunctions.h +++ b/src/FixedPoints/SFixedFreeFunctions.h @@ -20,7 +20,9 @@ FIXED_POINTS_BEGIN_NAMESPACE template< unsigned Integer, unsigned Fraction > constexpr SFixed multiply(const SFixed & left, const SFixed & right) -{ +{ + static_assert(((Integer + 1) * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the result type would be too large"); + using ResultType = SFixed; using InternalType = typename ResultType::InternalType; return ResultType::fromInternal(static_cast(static_cast(left.getInternal()) * static_cast(right.getInternal()))); @@ -169,6 +171,8 @@ constexpr SFixed operator -(const SFixed & template< unsigned Integer, unsigned Fraction > constexpr SFixed operator *(const SFixed & left, const SFixed & right) { + static_assert(((Integer + 1) * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the intermediary type would be too large"); + using InternalType = typename SFixed::InternalType; using PrecisionType = typename SFixed::InternalType; return SFixed::fromInternal(static_cast((static_cast(left.getInternal()) * static_cast(right.getInternal())) >> Fraction)); diff --git a/src/FixedPoints/UFixedFreeFunctions.h b/src/FixedPoints/UFixedFreeFunctions.h index 0d2fb19..0c9e452 100644 --- a/src/FixedPoints/UFixedFreeFunctions.h +++ b/src/FixedPoints/UFixedFreeFunctions.h @@ -21,6 +21,8 @@ FIXED_POINTS_BEGIN_NAMESPACE template< unsigned Integer, unsigned Fraction > constexpr UFixed multiply(const UFixed & left, const UFixed & right) { + static_assert((Integer * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the result type would be too large"); + using ResultType = UFixed; using InternalType = typename ResultType::InternalType; return ResultType::fromInternal(static_cast(static_cast(left.getInternal()) * static_cast(right.getInternal()))); @@ -169,6 +171,8 @@ constexpr UFixed operator -(const UFixed & template< unsigned Integer, unsigned Fraction > constexpr UFixed operator *(const UFixed & left, const UFixed & right) { + static_assert((Integer * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the intermediary type would be too large"); + using InternalType = typename UFixed::InternalType; using PrecisionType = typename UFixed::InternalType; return UFixed::fromInternal(static_cast((static_cast(left.getInternal()) * static_cast(right.getInternal())) >> Fraction));