2020-04-15 17:53:09 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2019 Apple Inc. All rights reserved.
|
2021-01-30 15:49:03 +03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-04-15 17:53:09 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2021-07-04 20:03:09 +03:00
|
|
|
#include <AK/Concepts.h>
|
2020-04-15 17:53:09 +03:00
|
|
|
#include <AK/NumericLimits.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename Destination, typename Source, bool destination_is_wider = (sizeof(Destination) >= sizeof(Source)), bool destination_is_signed = NumericLimits<Destination>::is_signed(), bool source_is_signed = NumericLimits<Source>::is_signed()>
|
|
|
|
struct TypeBoundsChecker;
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, false, false, false> {
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
{
|
|
|
|
return value <= NumericLimits<Destination>::max();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, false, true, true> {
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
{
|
|
|
|
return value <= NumericLimits<Destination>::max()
|
|
|
|
&& NumericLimits<Destination>::min() <= value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, false, false, true> {
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
{
|
2021-07-22 20:48:48 +03:00
|
|
|
return value >= 0 && value <= NumericLimits<Destination>::max();
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, false, true, false> {
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
{
|
|
|
|
return value <= static_cast<Source>(NumericLimits<Destination>::max());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, true, false, false> {
|
|
|
|
static constexpr bool is_within_range(Source)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, true, true, true> {
|
|
|
|
static constexpr bool is_within_range(Source)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, true, false, true> {
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
{
|
|
|
|
return value >= 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
struct TypeBoundsChecker<Destination, Source, true, true, false> {
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
{
|
|
|
|
if (sizeof(Destination) > sizeof(Source))
|
|
|
|
return true;
|
|
|
|
return value <= static_cast<Source>(NumericLimits<Destination>::max());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
2021-04-11 11:21:37 +03:00
|
|
|
[[nodiscard]] constexpr bool is_within_range(Source value)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return TypeBoundsChecker<Destination, Source>::is_within_range(value);
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:03:09 +03:00
|
|
|
template<Integral T>
|
2020-04-15 17:53:09 +03:00
|
|
|
class Checked {
|
|
|
|
public:
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked() = default;
|
2020-04-15 17:53:09 +03:00
|
|
|
|
2021-07-04 20:03:09 +03:00
|
|
|
explicit constexpr Checked(T value)
|
2020-04-15 17:53:09 +03:00
|
|
|
: m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:03:09 +03:00
|
|
|
template<Integral U>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked(U value)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
m_overflow = !is_within_range<T>(value);
|
|
|
|
m_value = value;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked(Checked const&) = default;
|
2020-04-15 17:53:09 +03:00
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked(Checked&& other)
|
2020-04-15 17:53:09 +03:00
|
|
|
: m_value(exchange(other.m_value, 0))
|
|
|
|
, m_overflow(exchange(other.m_overflow, false))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator=(U value)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2021-10-31 23:52:26 +03:00
|
|
|
*this = Checked(value);
|
|
|
|
return *this;
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked& operator=(Checked const& other) = default;
|
2020-04-15 17:53:09 +03:00
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator=(Checked&& other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
m_value = exchange(other.m_value, 0);
|
|
|
|
m_overflow = exchange(other.m_overflow, false);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-04-11 11:21:37 +03:00
|
|
|
[[nodiscard]] constexpr bool has_overflow() const
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return m_overflow;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
ALWAYS_INLINE constexpr bool operator!() const
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_overflow);
|
2020-04-15 17:53:09 +03:00
|
|
|
return !m_value;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
ALWAYS_INLINE constexpr T value() const
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_overflow);
|
2020-04-15 17:53:09 +03:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2022-12-09 19:34:40 +03:00
|
|
|
ALWAYS_INLINE constexpr T value_unchecked() const
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr void add(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
m_overflow |= __builtin_add_overflow(m_value, other, &m_value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr void sub(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
m_overflow |= __builtin_sub_overflow(m_value, other, &m_value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr void mul(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
m_overflow |= __builtin_mul_overflow(m_value, other, &m_value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr void div(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2021-05-07 08:15:54 +03:00
|
|
|
if constexpr (IsSigned<T>) {
|
|
|
|
// Ensure that the resulting value won't be out of range, this can only happen when dividing by -1.
|
|
|
|
if (other == -1 && m_value == NumericLimits<T>::min()) {
|
|
|
|
m_overflow = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 20:18:36 +03:00
|
|
|
if (other == 0) {
|
|
|
|
m_overflow = true;
|
|
|
|
return;
|
|
|
|
}
|
2020-04-15 17:53:09 +03:00
|
|
|
m_value /= other;
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:58:43 +03:00
|
|
|
constexpr void mod(T other)
|
|
|
|
{
|
|
|
|
auto initial = m_value;
|
|
|
|
div(other);
|
|
|
|
m_value *= other;
|
|
|
|
m_value = initial - m_value;
|
|
|
|
}
|
|
|
|
|
2022-06-15 22:34:25 +03:00
|
|
|
constexpr void saturating_sub(T other)
|
|
|
|
{
|
|
|
|
sub(other);
|
|
|
|
// Depending on whether other was positive or negative, we have to saturate to min or max.
|
|
|
|
if (m_overflow && other <= 0)
|
|
|
|
m_value = NumericLimits<T>::max();
|
|
|
|
else if (m_overflow)
|
|
|
|
m_value = NumericLimits<T>::min();
|
|
|
|
m_overflow = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void saturating_add(T other)
|
|
|
|
{
|
|
|
|
add(other);
|
|
|
|
// Depending on whether other was positive or negative, we have to saturate to max or min.
|
|
|
|
if (m_overflow && other >= 0)
|
|
|
|
m_value = NumericLimits<T>::max();
|
|
|
|
else if (m_overflow)
|
|
|
|
m_value = NumericLimits<T>::min();
|
|
|
|
m_overflow = false;
|
|
|
|
}
|
|
|
|
|
2023-08-19 21:01:37 +03:00
|
|
|
constexpr void saturating_mul(T other)
|
|
|
|
{
|
|
|
|
// Figure out if the result is positive, negative or zero beforehand.
|
|
|
|
auto either_is_zero = this->m_value == 0 || other == 0;
|
|
|
|
auto result_is_positive = (this->m_value > 0) == (other > 0);
|
|
|
|
|
|
|
|
mul(other);
|
|
|
|
if (m_overflow) {
|
|
|
|
if (either_is_zero)
|
|
|
|
m_value = 0;
|
|
|
|
else if (result_is_positive)
|
|
|
|
m_value = NumericLimits<T>::max();
|
|
|
|
else
|
|
|
|
m_value = NumericLimits<T>::min();
|
|
|
|
}
|
|
|
|
m_overflow = false;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked& operator+=(Checked const& other)
|
2021-01-30 15:49:03 +03:00
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
add(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator+=(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
add(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked& operator-=(Checked const& other)
|
2021-01-30 15:49:03 +03:00
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
sub(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator-=(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
sub(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked& operator*=(Checked const& other)
|
2021-01-30 15:49:03 +03:00
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
mul(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator*=(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
mul(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked& operator/=(Checked const& other)
|
2021-01-30 15:49:03 +03:00
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
div(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator/=(T other)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
div(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:58:43 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator++()
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
add(1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked operator++(int)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2020-08-26 00:48:47 +03:00
|
|
|
Checked old { *this };
|
2020-04-15 17:53:09 +03:00
|
|
|
add(1);
|
2020-08-26 00:48:47 +03:00
|
|
|
return old;
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
|
2021-03-08 23:07:58 +03:00
|
|
|
constexpr Checked& operator--()
|
|
|
|
{
|
|
|
|
sub(1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Checked operator--(int)
|
|
|
|
{
|
|
|
|
Checked old { *this };
|
|
|
|
sub(1);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2020-04-30 11:50:26 +03:00
|
|
|
template<typename U, typename V>
|
2021-04-11 11:21:37 +03:00
|
|
|
[[nodiscard]] static constexpr bool addition_would_overflow(U u, V v)
|
2020-04-30 11:50:26 +03:00
|
|
|
{
|
2022-12-17 22:09:21 +03:00
|
|
|
#if __has_builtin(__builtin_add_overflow_p)
|
|
|
|
return __builtin_add_overflow_p(u, v, (T)0);
|
2023-12-21 15:11:14 +03:00
|
|
|
#elif __has_builtin(__builtin_add_overflow)
|
|
|
|
T result;
|
|
|
|
return __builtin_add_overflow(u, v, &result);
|
2022-12-17 22:09:21 +03:00
|
|
|
#else
|
2020-05-01 18:30:47 +03:00
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked += v;
|
|
|
|
return checked.has_overflow();
|
|
|
|
#endif
|
2020-04-30 11:50:26 +03:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:53:21 +03:00
|
|
|
template<typename U, typename V>
|
|
|
|
static constexpr T saturating_add(U a, V b)
|
|
|
|
{
|
|
|
|
Checked checked { a };
|
|
|
|
checked.saturating_add(b);
|
|
|
|
return checked.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename V>
|
|
|
|
static constexpr T saturating_sub(U a, V b)
|
|
|
|
{
|
|
|
|
Checked checked { a };
|
|
|
|
checked.saturating_sub(b);
|
|
|
|
return checked.value();
|
|
|
|
}
|
|
|
|
|
2023-08-19 21:01:37 +03:00
|
|
|
template<typename U, typename V>
|
|
|
|
static constexpr T saturating_mul(U a, V b)
|
|
|
|
{
|
|
|
|
Checked checked { a };
|
|
|
|
checked.saturating_mul(b);
|
|
|
|
return checked.value();
|
|
|
|
}
|
|
|
|
|
2020-05-19 21:30:51 +03:00
|
|
|
template<typename U, typename V>
|
2021-04-11 11:21:37 +03:00
|
|
|
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v)
|
2020-04-15 18:14:18 +03:00
|
|
|
{
|
2022-12-17 22:09:21 +03:00
|
|
|
#if __has_builtin(__builtin_mul_overflow_p)
|
|
|
|
return __builtin_mul_overflow_p(u, v, (T)0);
|
2023-12-21 15:11:14 +03:00
|
|
|
#elif __has_builtin(__builtin_mul_overflow)
|
|
|
|
T result;
|
|
|
|
return __builtin_mul_overflow(u, v, &result);
|
2022-12-17 22:09:21 +03:00
|
|
|
#else
|
2020-05-01 18:30:47 +03:00
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked *= v;
|
|
|
|
return checked.has_overflow();
|
|
|
|
#endif
|
2020-04-15 18:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename V, typename X>
|
2021-04-11 11:21:37 +03:00
|
|
|
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v, X x)
|
2020-04-15 18:14:18 +03:00
|
|
|
{
|
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked *= v;
|
|
|
|
checked *= x;
|
|
|
|
return checked.has_overflow();
|
|
|
|
}
|
|
|
|
|
2020-04-15 17:53:09 +03:00
|
|
|
private:
|
2020-10-19 16:11:00 +03:00
|
|
|
T m_value {};
|
2020-04-15 17:53:09 +03:00
|
|
|
bool m_overflow { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked<T> operator+(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2020-08-26 00:48:47 +03:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.add(b.value());
|
|
|
|
return c;
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked<T> operator-(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2020-08-26 00:48:47 +03:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.sub(b.value());
|
|
|
|
return c;
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked<T> operator*(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2020-08-26 00:48:47 +03:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.mul(b.value());
|
|
|
|
return c;
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr Checked<T> operator/(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
2020-08-26 00:48:47 +03:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.div(b.value());
|
|
|
|
return c;
|
2020-04-15 17:53:09 +03:00
|
|
|
}
|
|
|
|
|
2022-12-10 14:58:43 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-15 17:53:09 +03:00
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator<(Checked<T> const& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator>(Checked<T> const& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator>=(Checked<T> const& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() >= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator<=(Checked<T> const& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator==(Checked<T> const& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator!=(Checked<T> const& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator<(T a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a < b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator>(T a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a > b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator>=(T a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a >= b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator<=(T a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a <= b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator==(T a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a == b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator!=(T a, Checked<T> const& b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a != b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked<T> make_checked(T value)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return Checked<T>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-04-15 17:53:09 +03:00
|
|
|
using AK::Checked;
|
|
|
|
using AK::make_checked;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|