2020-04-15 11:53:09 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011-2019 Apple Inc. All rights reserved.
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
|
2020-04-15 11: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 14:03:09 -03:00
|
|
|
#include <AK/Concepts.h>
|
2020-04-15 11:53:09 -03:00
|
|
|
#include <AK/NumericLimits.h>
|
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2025-09-13 20:41:10 -03:00
|
|
|
template<typename Destination, typename Source, bool destination_is_wider = (NumericLimits<Destination>::max() >= NumericLimits<Source>::max()), bool destination_is_signed = NumericLimits<Destination>::is_signed(), bool source_is_signed = NumericLimits<Source>::is_signed()>
|
2020-04-15 11:53:09 -03:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-24 08:31:25 -03:00
|
|
|
// Computes 2^N as a floating-point Source value. The result is exactly representable in any IEEE floating-point type
|
|
|
|
|
// for any N within that type's exponent range — since 2^N has mantissa 1.0.
|
|
|
|
|
template<FloatingPoint Source>
|
|
|
|
|
static constexpr Source two_to_the(size_t n)
|
|
|
|
|
{
|
|
|
|
|
Source result = 1;
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
|
result *= 2;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 11:53:09 -03:00
|
|
|
template<typename Destination, typename Source>
|
|
|
|
|
struct TypeBoundsChecker<Destination, Source, false, true, true> {
|
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
|
{
|
2026-05-24 08:31:25 -03:00
|
|
|
if constexpr (IsFloatingPoint<Source>) {
|
|
|
|
|
// The value must lie in [Destination::min, Destination::max] *and* must be exactly representable as
|
|
|
|
|
// Destination; that is, the float must be integer-valued — with no fractional part.
|
|
|
|
|
//
|
|
|
|
|
// First gate: the value must fit in the safe-cast window — so static_cast<Destination> below is defined.
|
|
|
|
|
// Comparing directly against NumericLimits<Destination>::max() doesn't work when max isn't exactly
|
|
|
|
|
// representable in Source (e.g., INT_MAX = 2^31-1 in float rounds up to 2^31). Use 2^digits as a strict
|
|
|
|
|
// upper bound instead: exactly representable in any IEEE float — covering the range [-2^digits, 2^digits)
|
|
|
|
|
// for two's-complement signed destinations.
|
|
|
|
|
auto const boundary = two_to_the<Source>(NumericLimits<Destination>::digits());
|
|
|
|
|
if (!(value >= -boundary && value < boundary))
|
|
|
|
|
return false;
|
|
|
|
|
// Second gate: reject fractional values (round-trip check). Values in (-boundary, boundary) with a
|
|
|
|
|
// fractional part would truncate to an in-range integer — but aren't themselves within Destination's range.
|
|
|
|
|
auto const truncated = static_cast<Destination>(value);
|
|
|
|
|
return static_cast<Source>(truncated) == value;
|
|
|
|
|
} else {
|
|
|
|
|
return value <= NumericLimits<Destination>::max()
|
|
|
|
|
&& NumericLimits<Destination>::min() <= value;
|
|
|
|
|
}
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
|
|
|
|
struct TypeBoundsChecker<Destination, Source, false, false, true> {
|
|
|
|
|
static constexpr bool is_within_range(Source value)
|
|
|
|
|
{
|
2026-05-24 08:31:25 -03:00
|
|
|
if constexpr (IsFloatingPoint<Source>) {
|
|
|
|
|
// See the signed case above. The first gate is [0, 2^digits). 2^digits is exactly representable in any IEEE
|
|
|
|
|
// float. The second gate is the round-trip check: rejecting fractional values whose magnitudes exceed
|
|
|
|
|
// Destination::max.
|
|
|
|
|
auto const upper_exclusive = two_to_the<Source>(NumericLimits<Destination>::digits());
|
|
|
|
|
if (!(value >= 0 && value < upper_exclusive))
|
|
|
|
|
return false;
|
|
|
|
|
auto const truncated = static_cast<Destination>(value);
|
|
|
|
|
return static_cast<Source>(truncated) == value;
|
|
|
|
|
} else {
|
|
|
|
|
return value >= 0 && value <= NumericLimits<Destination>::max();
|
|
|
|
|
}
|
2020-04-15 11: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)
|
|
|
|
|
{
|
2025-09-13 20:41:10 -03:00
|
|
|
if (NumericLimits<Destination>::max() > NumericLimits<Source>::max())
|
2020-04-15 11:53:09 -03:00
|
|
|
return true;
|
|
|
|
|
return value <= static_cast<Source>(NumericLimits<Destination>::max());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Destination, typename Source>
|
2021-04-11 05:21:37 -03:00
|
|
|
[[nodiscard]] constexpr bool is_within_range(Source value)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return TypeBoundsChecker<Destination, Source>::is_within_range(value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 14:03:09 -03:00
|
|
|
template<Integral T>
|
2020-04-15 11:53:09 -03:00
|
|
|
class Checked {
|
|
|
|
|
public:
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked() = default;
|
2020-04-15 11:53:09 -03:00
|
|
|
|
2021-07-04 14:03:09 -03:00
|
|
|
explicit constexpr Checked(T value)
|
2020-04-15 11:53:09 -03:00
|
|
|
: m_value(value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 14:03:09 -03:00
|
|
|
template<Integral U>
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked(U value)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
m_overflow = !is_within_range<T>(value);
|
|
|
|
|
m_value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked(Checked const&) = default;
|
2020-04-15 11:53:09 -03:00
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked(Checked&& other)
|
2020-04-15 11:53:09 -03:00
|
|
|
: m_value(exchange(other.m_value, 0))
|
|
|
|
|
, m_overflow(exchange(other.m_overflow, false))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename U>
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator=(U value)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2021-10-31 17:52:26 -03:00
|
|
|
*this = Checked(value);
|
|
|
|
|
return *this;
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked& operator=(Checked const& other) = default;
|
2020-04-15 11:53:09 -03:00
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator=(Checked&& other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
m_value = exchange(other.m_value, 0);
|
|
|
|
|
m_overflow = exchange(other.m_overflow, false);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 05:21:37 -03:00
|
|
|
[[nodiscard]] constexpr bool has_overflow() const
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return m_overflow;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
ALWAYS_INLINE constexpr bool operator!() const
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(!m_overflow);
|
2020-04-15 11:53:09 -03:00
|
|
|
return !m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
ALWAYS_INLINE constexpr T value() const
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(!m_overflow);
|
2020-04-15 11:53:09 -03:00
|
|
|
return m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 13:34:40 -03:00
|
|
|
ALWAYS_INLINE constexpr T value_unchecked() const
|
|
|
|
|
{
|
|
|
|
|
return m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr void add(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= __builtin_add_overflow(m_value, other, &m_value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr void sub(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= __builtin_sub_overflow(m_value, other, &m_value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr void mul(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= __builtin_mul_overflow(m_value, other, &m_value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr void div(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2021-05-07 02: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 14:18:36 -03:00
|
|
|
if (other == 0) {
|
|
|
|
|
m_overflow = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-15 11:53:09 -03:00
|
|
|
m_value /= other;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 08:58:43 -03:00
|
|
|
constexpr void mod(T other)
|
|
|
|
|
{
|
|
|
|
|
auto initial = m_value;
|
|
|
|
|
div(other);
|
|
|
|
|
m_value *= other;
|
|
|
|
|
m_value = initial - m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked& operator+=(Checked const& other)
|
2021-01-30 09:49:03 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= other.m_overflow;
|
2025-02-19 11:05:16 -03:00
|
|
|
add(other.value_unchecked());
|
2021-01-30 09:49:03 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator+=(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
add(other);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked& operator-=(Checked const& other)
|
2021-01-30 09:49:03 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= other.m_overflow;
|
2025-02-19 11:05:16 -03:00
|
|
|
sub(other.value_unchecked());
|
2021-01-30 09:49:03 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator-=(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
sub(other);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked& operator*=(Checked const& other)
|
2021-01-30 09:49:03 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= other.m_overflow;
|
2025-02-19 11:05:16 -03:00
|
|
|
mul(other.value_unchecked());
|
2021-01-30 09:49:03 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator*=(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
mul(other);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked& operator/=(Checked const& other)
|
2021-01-30 09:49:03 -03:00
|
|
|
{
|
|
|
|
|
m_overflow |= other.m_overflow;
|
2025-02-19 11:05:16 -03:00
|
|
|
div(other.value_unchecked());
|
2021-01-30 09:49:03 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator/=(T other)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
div(other);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 08:58:43 -03:00
|
|
|
constexpr Checked& operator%=(Checked const& other)
|
|
|
|
|
{
|
|
|
|
|
m_overflow |= other.m_overflow;
|
2025-02-19 11:05:16 -03:00
|
|
|
mod(other.value_unchecked());
|
2022-12-10 08:58:43 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr Checked& operator%=(T other)
|
|
|
|
|
{
|
|
|
|
|
mod(other);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked& operator++()
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
add(1);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked operator++(int)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2020-08-25 18:48:47 -03:00
|
|
|
Checked old { *this };
|
2020-04-15 11:53:09 -03:00
|
|
|
add(1);
|
2020-08-25 18:48:47 -03:00
|
|
|
return old;
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 17: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 05:50:26 -03:00
|
|
|
template<typename U, typename V>
|
2021-04-11 05:21:37 -03:00
|
|
|
[[nodiscard]] static constexpr bool addition_would_overflow(U u, V v)
|
2020-04-30 05:50:26 -03:00
|
|
|
{
|
2022-12-17 16:09:21 -03:00
|
|
|
#if __has_builtin(__builtin_add_overflow_p)
|
|
|
|
|
return __builtin_add_overflow_p(u, v, (T)0);
|
2025-10-21 16:44:50 -03:00
|
|
|
#else
|
2023-12-21 09:11:14 -03:00
|
|
|
T result;
|
|
|
|
|
return __builtin_add_overflow(u, v, &result);
|
2020-05-01 12:30:47 -03:00
|
|
|
#endif
|
2020-04-30 05:50:26 -03:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:16:55 -03:00
|
|
|
template<typename... Ts>
|
|
|
|
|
[[nodiscard]] static constexpr bool addition_would_overflow(Ts... values)
|
|
|
|
|
requires(sizeof...(Ts) > 2)
|
|
|
|
|
{
|
|
|
|
|
Checked<T> result;
|
|
|
|
|
((result += values), ...);
|
|
|
|
|
return result.has_overflow();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 05:58:11 -03:00
|
|
|
template<typename U, typename V>
|
|
|
|
|
[[nodiscard]] static constexpr bool subtraction_would_overflow(U u, V v)
|
|
|
|
|
{
|
|
|
|
|
#if __has_builtin(__builtin_sub_overflow_p)
|
|
|
|
|
return __builtin_sub_overflow_p(u, v, (T)0);
|
2025-10-21 16:44:50 -03:00
|
|
|
#else
|
2024-05-18 05:58:11 -03:00
|
|
|
T result;
|
|
|
|
|
return __builtin_sub_overflow(u, v, &result);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 15:30:51 -03:00
|
|
|
template<typename U, typename V>
|
2021-04-11 05:21:37 -03:00
|
|
|
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v)
|
2020-04-15 12:14:18 -03:00
|
|
|
{
|
2022-12-17 16:09:21 -03:00
|
|
|
#if __has_builtin(__builtin_mul_overflow_p)
|
|
|
|
|
return __builtin_mul_overflow_p(u, v, (T)0);
|
2025-10-21 16:44:50 -03:00
|
|
|
#else
|
2023-12-21 09:11:14 -03:00
|
|
|
T result;
|
|
|
|
|
return __builtin_mul_overflow(u, v, &result);
|
2020-05-01 12:30:47 -03:00
|
|
|
#endif
|
2020-04-15 12:14:18 -03:00
|
|
|
}
|
|
|
|
|
|
2020-04-15 11:53:09 -03:00
|
|
|
private:
|
2020-10-19 10:11:00 -03:00
|
|
|
T m_value {};
|
2020-04-15 11:53:09 -03:00
|
|
|
bool m_overflow { false };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked<T> operator+(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2020-08-25 18:48:47 -03:00
|
|
|
Checked<T> c { a };
|
2025-02-19 11:05:16 -03:00
|
|
|
c += b;
|
2020-08-25 18:48:47 -03:00
|
|
|
return c;
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked<T> operator-(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2020-08-25 18:48:47 -03:00
|
|
|
Checked<T> c { a };
|
2025-02-19 11:05:16 -03:00
|
|
|
c -= b;
|
2020-08-25 18:48:47 -03:00
|
|
|
return c;
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked<T> operator*(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2020-08-25 18:48:47 -03:00
|
|
|
Checked<T> c { a };
|
2025-02-19 11:05:16 -03:00
|
|
|
c *= b;
|
2020-08-25 18:48:47 -03:00
|
|
|
return c;
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr Checked<T> operator/(Checked<T> const& a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
2020-08-25 18:48:47 -03:00
|
|
|
Checked<T> c { a };
|
2025-02-19 11:05:16 -03:00
|
|
|
c /= b;
|
2020-08-25 18:48:47 -03:00
|
|
|
return c;
|
2020-04-15 11:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-10 08:58:43 -03:00
|
|
|
template<typename T>
|
|
|
|
|
constexpr Checked<T> operator%(Checked<T> const& a, Checked<T> const& b)
|
|
|
|
|
{
|
|
|
|
|
Checked<T> c { a };
|
2025-02-19 11:05:16 -03:00
|
|
|
c %= b;
|
2022-12-10 08:58:43 -03:00
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 11:53:09 -03:00
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator<(Checked<T> const& a, T b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a.value() < b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator>(Checked<T> const& a, T b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a.value() > b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator>=(Checked<T> const& a, T b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a.value() >= b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator<=(Checked<T> const& a, T b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a.value() <= b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator==(Checked<T> const& a, T b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a.value() == b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator!=(Checked<T> const& a, T b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a.value() != b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator<(T a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a < b.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator>(T a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a > b.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator>=(T a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a >= b.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator<=(T a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a <= b.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator==(T a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a == b.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 14:58:27 -03:00
|
|
|
constexpr bool operator!=(T a, Checked<T> const& b)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return a != b.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 13:30:30 -03:00
|
|
|
constexpr Checked<T> make_checked(T value)
|
2020-04-15 11:53:09 -03:00
|
|
|
{
|
|
|
|
|
return Checked<T>(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-04-15 11:53:09 -03:00
|
|
|
using AK::Checked;
|
|
|
|
|
using AK::make_checked;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|