2020-04-15 14:53:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2019 Apple Inc. All rights reserved.
|
2021-01-30 12:49:03 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-04-15 14:53:09 +00: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>
|
|
|
|
#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 08:21:37 +00:00
|
|
|
[[nodiscard]] constexpr bool is_within_range(Source value)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return TypeBoundsChecker<Destination, Source>::is_within_range(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Checked {
|
|
|
|
public:
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked() = default;
|
2020-04-15 14:53:09 +00:00
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked(T value)
|
2020-04-15 14:53:09 +00:00
|
|
|
: m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked(U value)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
m_overflow = !is_within_range<T>(value);
|
|
|
|
m_value = value;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked(const Checked&) = default;
|
2020-04-15 14:53:09 +00:00
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked(Checked&& other)
|
2020-04-15 14:53:09 +00:00
|
|
|
: m_value(exchange(other.m_value, 0))
|
|
|
|
, m_overflow(exchange(other.m_overflow, false))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator=(U value)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return *this = Checked(value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator=(const Checked& other) = default;
|
2020-04-15 14:53:09 +00:00
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator=(Checked&& other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
m_value = exchange(other.m_value, 0);
|
|
|
|
m_overflow = exchange(other.m_overflow, false);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-04-11 08:21:37 +00:00
|
|
|
[[nodiscard]] constexpr bool has_overflow() const
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return m_overflow;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
ALWAYS_INLINE constexpr bool operator!() const
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_overflow);
|
2020-04-15 14:53:09 +00:00
|
|
|
return !m_value;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
ALWAYS_INLINE constexpr T value() const
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_overflow);
|
2020-04-15 14:53:09 +00:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr void add(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
m_overflow |= __builtin_add_overflow(m_value, other, &m_value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr void sub(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
m_overflow |= __builtin_sub_overflow(m_value, other, &m_value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr void mul(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
m_overflow |= __builtin_mul_overflow(m_value, other, &m_value);
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr void div(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
m_value /= other;
|
|
|
|
}
|
|
|
|
|
2021-01-30 12:49:03 +00:00
|
|
|
constexpr Checked& operator+=(const Checked& other)
|
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
add(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator+=(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
add(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-30 12:49:03 +00:00
|
|
|
constexpr Checked& operator-=(const Checked& other)
|
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
sub(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator-=(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
sub(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-30 12:49:03 +00:00
|
|
|
constexpr Checked& operator*=(const Checked& other)
|
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
mul(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator*=(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
mul(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-30 12:49:03 +00:00
|
|
|
constexpr Checked& operator/=(const Checked& other)
|
|
|
|
{
|
|
|
|
m_overflow |= other.m_overflow;
|
|
|
|
div(other.value());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator/=(T other)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
div(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked& operator++()
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
add(1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked operator++(int)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2020-08-25 21:48:47 +00:00
|
|
|
Checked old { *this };
|
2020-04-15 14:53:09 +00:00
|
|
|
add(1);
|
2020-08-25 21:48:47 +00:00
|
|
|
return old;
|
2020-04-15 14:53:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 20:07:58 +00:00
|
|
|
constexpr Checked& operator--()
|
|
|
|
{
|
|
|
|
sub(1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Checked operator--(int)
|
|
|
|
{
|
|
|
|
Checked old { *this };
|
|
|
|
sub(1);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:50:26 +00:00
|
|
|
template<typename U, typename V>
|
2021-04-11 08:21:37 +00:00
|
|
|
[[nodiscard]] static constexpr bool addition_would_overflow(U u, V v)
|
2020-04-30 08:50:26 +00:00
|
|
|
{
|
2020-05-01 15:30:47 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked += v;
|
|
|
|
return checked.has_overflow();
|
|
|
|
#else
|
2020-04-30 08:50:26 +00:00
|
|
|
return __builtin_add_overflow_p(u, v, (T)0);
|
2020-05-01 15:30:47 +00:00
|
|
|
#endif
|
2020-04-30 08:50:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:30:51 +00:00
|
|
|
template<typename U, typename V>
|
2021-04-11 08:21:37 +00:00
|
|
|
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v)
|
2020-04-15 15:14:18 +00:00
|
|
|
{
|
2020-05-01 15:30:47 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked *= v;
|
|
|
|
return checked.has_overflow();
|
|
|
|
#else
|
2020-04-30 08:50:26 +00:00
|
|
|
return __builtin_mul_overflow_p(u, v, (T)0);
|
2020-05-01 15:30:47 +00:00
|
|
|
#endif
|
2020-04-15 15:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename V, typename X>
|
2021-04-11 08:21:37 +00:00
|
|
|
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v, X x)
|
2020-04-15 15:14:18 +00:00
|
|
|
{
|
|
|
|
Checked checked;
|
|
|
|
checked = u;
|
|
|
|
checked *= v;
|
|
|
|
checked *= x;
|
|
|
|
return checked.has_overflow();
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:53:09 +00:00
|
|
|
private:
|
2020-10-19 13:11:00 +00:00
|
|
|
T m_value {};
|
2020-04-15 14:53:09 +00:00
|
|
|
bool m_overflow { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked<T> operator+(const Checked<T>& a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2020-08-25 21:48:47 +00:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.add(b.value());
|
|
|
|
return c;
|
2020-04-15 14:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked<T> operator-(const Checked<T>& a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2020-08-25 21:48:47 +00:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.sub(b.value());
|
|
|
|
return c;
|
2020-04-15 14:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked<T> operator*(const Checked<T>& a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2020-08-25 21:48:47 +00:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.mul(b.value());
|
|
|
|
return c;
|
2020-04-15 14:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-20 16:08:13 +00:00
|
|
|
constexpr Checked<T> operator/(const Checked<T>& a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
2020-08-25 21:48:47 +00:00
|
|
|
Checked<T> c { a };
|
|
|
|
c.div(b.value());
|
|
|
|
return c;
|
2020-04-15 14:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator<(const Checked<T>& a, T b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a.value() < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator>(const Checked<T>& a, T b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a.value() > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator>=(const Checked<T>& a, T b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a.value() >= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator<=(const Checked<T>& a, T b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a.value() <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator==(const Checked<T>& a, T b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a.value() == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator!=(const Checked<T>& a, T b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a.value() != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator<(T a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a < b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator>(T a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a > b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator>=(T a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a >= b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator<=(T a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a <= b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator==(T a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a == b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr bool operator!=(T a, const Checked<T>& b)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return a != b.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr Checked<T> make_checked(T value)
|
2020-04-15 14:53:09 +00:00
|
|
|
{
|
|
|
|
return Checked<T>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Checked;
|
|
|
|
using AK::make_checked;
|