mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-21 18:15:58 +00:00
AK: Add Checked<T>::multiplication_would_overflow()
This allows you to comfortably test if multiply 2 or 3 values would cause arithmetic overflow.
This commit is contained in:
parent
1b610ac2b6
commit
63b8c6913c
19
AK/Checked.h
19
AK/Checked.h
|
@ -234,6 +234,25 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
template<typename U, typename V, typename X>
|
||||
static bool multiplication_would_overflow(U u, V v)
|
||||
{
|
||||
Checked checked;
|
||||
checked = u;
|
||||
checked *= v;
|
||||
return checked.has_overflow();
|
||||
}
|
||||
|
||||
template<typename U, typename V, typename X>
|
||||
static bool multiplication_would_overflow(U u, V v, X x)
|
||||
{
|
||||
Checked checked;
|
||||
checked = u;
|
||||
checked *= v;
|
||||
checked *= x;
|
||||
return checked.has_overflow();
|
||||
}
|
||||
|
||||
private:
|
||||
T m_value;
|
||||
bool m_overflow { false };
|
||||
|
|
Loading…
Reference in a new issue