diff --git a/AK/Checked.h b/AK/Checked.h index d612807186..7026c49302 100644 --- a/AK/Checked.h +++ b/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 operator/(Checked const& a, Checked const& b) return c; } +template +constexpr Checked operator%(Checked const& a, Checked const& b) +{ + Checked c { a }; + c.mod(b.value()); + return c; +} + template constexpr bool operator<(Checked const& a, T b) {