mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 08:56:40 +00:00
AK: Add support for modulo to Checked<T>
This is used by the Jakt runtime.
This commit is contained in:
parent
cc0b970d81
commit
c3b4b0e88b
29
AK/Checked.h
29
AK/Checked.h
|
@ -204,6 +204,14 @@ public:
|
|||
m_value /= other;
|
||||
}
|
||||
|
||||
constexpr void mod(T other)
|
||||
{
|
||||
auto initial = m_value;
|
||||
div(other);
|
||||
m_value *= other;
|
||||
m_value = initial - m_value;
|
||||
}
|
||||
|
||||
constexpr void saturating_sub(T other)
|
||||
{
|
||||
sub(other);
|
||||
|
@ -278,6 +286,19 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
constexpr Checked& operator%=(Checked const& other)
|
||||
{
|
||||
m_overflow |= other.m_overflow;
|
||||
mod(other.value());
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Checked& operator%=(T other)
|
||||
{
|
||||
mod(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Checked& operator++()
|
||||
{
|
||||
add(1);
|
||||
|
@ -377,6 +398,14 @@ constexpr Checked<T> operator/(Checked<T> const& a, Checked<T> const& b)
|
|||
return c;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr Checked<T> operator%(Checked<T> const& a, Checked<T> const& b)
|
||||
{
|
||||
Checked<T> c { a };
|
||||
c.mod(b.value());
|
||||
return c;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator<(Checked<T> const& a, T b)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue