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)
|
|
|
|
{
|
|
|
|
return static_cast<MakeUnsigned<Source>>(value) <= NumericLimits<Destination>::max();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked(const Checked&) = 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
|
|
|
{
|
|
|
|
return *this = Checked(value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked& operator=(const Checked& 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:49:03 +03:00
|
|
|
constexpr Checked& operator+=(const Checked& other)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:49:03 +03:00
|
|
|
constexpr Checked& operator-=(const Checked& other)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:49:03 +03:00
|
|
|
constexpr Checked& operator*=(const Checked& other)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:49:03 +03:00
|
|
|
constexpr Checked& operator/=(const Checked& other)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-05-01 18:30:47 +03:00
|
|
|
#ifdef __clang__
|
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked += v;
|
|
|
|
return checked.has_overflow();
|
|
|
|
#else
|
2020-04-30 11:50:26 +03:00
|
|
|
return __builtin_add_overflow_p(u, v, (T)0);
|
2020-05-01 18:30:47 +03:00
|
|
|
#endif
|
2020-04-30 11:50:26 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-05-01 18:30:47 +03:00
|
|
|
#ifdef __clang__
|
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked *= v;
|
|
|
|
return checked.has_overflow();
|
|
|
|
#else
|
2020-04-30 11:50:26 +03:00
|
|
|
return __builtin_mul_overflow_p(u, v, (T)0);
|
2020-05-01 18:30:47 +03:00
|
|
|
#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>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked<T> operator+(const Checked<T>& a, const Checked<T>& 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>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked<T> operator-(const Checked<T>& a, const Checked<T>& 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>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr Checked<T> operator*(const Checked<T>& a, const Checked<T>& 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>
|
2020-10-20 19:08:13 +03:00
|
|
|
constexpr Checked<T> operator/(const Checked<T>& a, const Checked<T>& 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
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator<(const Checked<T>& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator>(const Checked<T>& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator>=(const Checked<T>& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() >= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator<=(const Checked<T>& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator==(const Checked<T>& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator!=(const Checked<T>& a, T b)
|
2020-04-15 17:53:09 +03:00
|
|
|
{
|
|
|
|
return a.value() != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 19:30:30 +03:00
|
|
|
constexpr bool operator<(T a, const Checked<T>& 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 bool operator>(T a, const Checked<T>& 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 bool operator>=(T a, const Checked<T>& 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 bool operator<=(T a, const Checked<T>& 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 bool operator==(T a, const Checked<T>& 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 bool operator!=(T a, const Checked<T>& 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Checked;
|
|
|
|
using AK::make_checked;
|