AK: Add support for modulo to Checked<T>

This is used by the Jakt runtime.
This commit is contained in:
Ali Mohammad Pur 2022-12-10 15:28:43 +03:30 committed by Ali Mohammad Pur
parent cc0b970d81
commit c3b4b0e88b

View file

@ -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)
{