2021-05-19 21:04:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
2023-02-04 16:03:18 +00:00
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
2021-05-19 21:04:48 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
#include <AK/BigIntBase.h>
|
2021-12-19 21:46:55 +00:00
|
|
|
#include <AK/BuiltinWrappers.h>
|
2021-05-19 21:04:48 +00:00
|
|
|
#include <AK/Checked.h>
|
|
|
|
#include <AK/Concepts.h>
|
|
|
|
#include <AK/Format.h>
|
|
|
|
#include <AK/NumericLimits.h>
|
|
|
|
#include <AK/StdLibExtraDetails.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
namespace Detail {
|
|
|
|
// As noted near the declaration of StaticStorage, bit_size is more like a hint for a storage size.
|
|
|
|
// The effective bit size is `sizeof(StaticStorage<...>) * 8`. It is a programmer's responsibility
|
|
|
|
// to ensure that the hinted bit_size is always greater than the actual integer size.
|
|
|
|
// That said, do not use unaligned (bit_size % 64 != 0) `UFixedBigInt`s if you do not know what you
|
|
|
|
// are doing.
|
|
|
|
template<size_t bit_size, typename Storage = StaticStorage<false, bit_size>>
|
|
|
|
class UFixedBigInt;
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
// ===== Concepts =====
|
|
|
|
template<typename T>
|
|
|
|
constexpr inline size_t assumed_bit_size = 0;
|
|
|
|
template<>
|
|
|
|
constexpr inline size_t assumed_bit_size<IntegerWrapper> = bit_width<int>;
|
|
|
|
template<size_t bit_size>
|
|
|
|
constexpr inline size_t assumed_bit_size<UFixedBigInt<bit_size>> = bit_size;
|
|
|
|
template<BuiltInUFixedInt T>
|
|
|
|
constexpr inline size_t assumed_bit_size<T> = bit_width<T>;
|
2021-05-19 21:04:48 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2023-02-04 16:03:18 +00:00
|
|
|
concept ConvertibleToUFixedInt = (assumed_bit_size<T> != 0);
|
|
|
|
|
2021-05-19 21:04:48 +00:00
|
|
|
template<typename T>
|
2023-02-04 16:03:18 +00:00
|
|
|
concept UFixedInt = (ConvertibleToUFixedInt<T> && !IsSame<T, IntegerWrapper>);
|
2021-05-19 21:04:48 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2023-02-04 16:03:18 +00:00
|
|
|
concept NotBuiltInUFixedInt = (UFixedInt<T> && !BuiltInUFixedInt<T>);
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
// ===== UFixedBigInt itself =====
|
|
|
|
template<size_t bit_size>
|
|
|
|
constexpr auto& get_storage_of(UFixedBigInt<bit_size>& value) { return value.m_data; }
|
2022-03-04 07:48:44 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
template<size_t bit_size>
|
|
|
|
constexpr auto& get_storage_of(UFixedBigInt<bit_size> const& value) { return value.m_data; }
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
template<typename Operand1, typename Operand2, typename Result>
|
|
|
|
constexpr void mul_internal(Operand1 const& operand1, Operand2 const& operand2, Result& result)
|
|
|
|
{
|
|
|
|
StorageOperations::baseline_mul(operand1, operand2, result, g_null_allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t bit_size, typename Storage>
|
|
|
|
class UFixedBigInt {
|
|
|
|
constexpr static size_t static_size = Storage::static_size;
|
|
|
|
constexpr static size_t part_size = static_size / 2;
|
|
|
|
using UFixedBigIntPart = Conditional<part_size * word_size <= 64, u64, UFixedBigInt<part_size * word_size>>;
|
|
|
|
|
|
|
|
public:
|
2021-05-19 21:04:48 +00:00
|
|
|
constexpr UFixedBigInt() = default;
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
explicit constexpr UFixedBigInt(IntegerWrapper value) { StorageOperations::copy(value.m_data, m_data); }
|
|
|
|
consteval UFixedBigInt(int value)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::copy(IntegerWrapper(value).m_data, m_data);
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
template<UFixedInt T>
|
|
|
|
requires(sizeof(T) > sizeof(Storage)) explicit constexpr UFixedBigInt(T const& value)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::copy(get_storage_of(value), m_data);
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
template<UFixedInt T>
|
|
|
|
requires(sizeof(T) <= sizeof(Storage)) constexpr UFixedBigInt(T const& value)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::copy(get_storage_of(value), m_data);
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr UFixedBigInt(UFixedBigIntPart const& low, UFixedBigIntPart const& high)
|
|
|
|
requires(static_size % 2 == 0)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
decltype(auto) low_storage = get_storage_of(low);
|
|
|
|
decltype(auto) high_storage = get_storage_of(high);
|
|
|
|
for (size_t i = 0; i < part_size; ++i)
|
|
|
|
m_data[i] = low_storage[i];
|
|
|
|
for (size_t i = 0; i < part_size; ++i)
|
|
|
|
m_data[i + part_size] = high_storage[i];
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
template<UFixedInt T, size_t n>
|
|
|
|
requires((assumed_bit_size<T> * n) <= bit_size) constexpr UFixedBigInt(const T (&value)[n])
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
if (offset % word_size == 0) {
|
|
|
|
// Aligned initialization (i. e. u256 from two u128)
|
|
|
|
decltype(auto) storage = get_storage_of(value[i]);
|
|
|
|
for (size_t i = 0; i < storage.size(); ++i)
|
|
|
|
m_data[i + offset / word_size] = storage[i];
|
|
|
|
} else if (offset % word_size == 32 && IsSame<T, u32>) {
|
|
|
|
// u32 vector initialization on 64-bit platforms
|
|
|
|
m_data[offset / word_size] |= static_cast<DoubleWord>(value[i]) << 32;
|
|
|
|
} else {
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
offset += assumed_bit_size<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = (offset + word_size - 1) / word_size; i < m_data.size(); ++i)
|
|
|
|
m_data[i] = 0;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
// Casts & parts extraction
|
|
|
|
template<NotBuiltInUFixedInt T>
|
|
|
|
constexpr explicit operator T() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
T result;
|
|
|
|
StorageOperations::copy(m_data, result.m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
template<BuiltInUFixedInt T>
|
|
|
|
requires(sizeof(T) <= sizeof(NativeWord)) constexpr explicit operator T() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return m_data[0];
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
template<BuiltInUFixedInt T>
|
|
|
|
requires(sizeof(T) == sizeof(DoubleWord)) constexpr explicit operator T() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return (static_cast<DoubleWord>(m_data[1]) << word_size) + m_data[0];
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr UFixedBigIntPart low() const
|
|
|
|
requires(static_size % 2 == 0)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
if constexpr (part_size == 1) {
|
|
|
|
return m_data[0];
|
|
|
|
} else if constexpr (IsSame<UFixedBigIntPart, DoubleWord>) {
|
|
|
|
return m_data[0] + (static_cast<DoubleWord>(m_data[1]) << word_size);
|
|
|
|
} else {
|
|
|
|
UFixedBigInt<part_size * word_size> result;
|
|
|
|
StorageOperations::copy(m_data, result.m_data);
|
|
|
|
return result;
|
|
|
|
}
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr UFixedBigIntPart high() const
|
|
|
|
requires(static_size % 2 == 0)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
if constexpr (part_size == 1) {
|
|
|
|
return m_data[part_size];
|
|
|
|
} else if constexpr (IsSame<UFixedBigIntPart, DoubleWord>) {
|
|
|
|
return m_data[part_size] + (static_cast<DoubleWord>(m_data[part_size + 1]) << word_size);
|
|
|
|
} else {
|
|
|
|
UFixedBigInt<part_size * word_size> result;
|
|
|
|
StorageOperations::copy(m_data, result.m_data, part_size);
|
|
|
|
return result;
|
|
|
|
}
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
Bytes bytes()
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return Bytes(reinterpret_cast<u8*>(this), sizeof(Storage));
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
ReadonlyBytes bytes() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return ReadonlyBytes(reinterpret_cast<u8 const*>(this), sizeof(Storage));
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr UnsignedStorageSpan span()
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return { m_data.data(), static_size };
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr UnsignedStorageReadonlySpan span() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return { m_data.data(), static_size };
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
// Binary utils
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr size_t popcnt() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
size_t result = 0;
|
|
|
|
for (size_t i = 0; i < m_data.size(); ++i)
|
|
|
|
result += popcount(m_data[i]);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr size_t ctz() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
size_t result = 0;
|
|
|
|
for (size_t i = 0; i < m_data.size(); ++i) {
|
|
|
|
if (m_data[i]) {
|
|
|
|
result += count_trailing_zeroes(m_data[i]);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result += word_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr size_t clz() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
size_t result = 0;
|
|
|
|
for (size_t i = m_data.size(); i--;) {
|
|
|
|
if (m_data[i]) {
|
|
|
|
result += count_leading_zeroes(m_data[i]);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result += word_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result + bit_size - word_size * static_size;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
// Comparisons
|
|
|
|
constexpr bool operator!() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
bool result = true;
|
|
|
|
for (size_t i = 0; i < m_data.size(); ++i)
|
|
|
|
result &= !m_data[i];
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr explicit operator bool() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
bool result = false;
|
|
|
|
for (size_t i = 0; i < m_data.size(); ++i)
|
|
|
|
result |= m_data[i];
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr bool operator==(UFixedInt auto const& other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return StorageOperations::compare(m_data, get_storage_of(other), true) == 0;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr bool operator==(IntegerWrapper other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return StorageOperations::compare(m_data, get_storage_of(other), true) == 0;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr int operator<=>(UFixedInt auto const& other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return StorageOperations::compare(m_data, get_storage_of(other), false);
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr int operator<=>(IntegerWrapper other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return StorageOperations::compare(m_data, get_storage_of(other), false);
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
#define DEFINE_STANDARD_BINARY_OPERATOR(op, function) \
|
|
|
|
constexpr auto operator op(UFixedInt auto const& other) const \
|
|
|
|
{ \
|
|
|
|
auto func = [](auto&& a, auto&& b, auto&& c) { function(a, b, c); }; \
|
|
|
|
return do_standard_binary_operation(other, func); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
constexpr auto operator op(IntegerWrapper other) const \
|
|
|
|
{ \
|
|
|
|
auto func = [](auto&& a, auto&& b, auto&& c) { function(a, b, c); }; \
|
|
|
|
return do_standard_binary_operation(other, func); \
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
#define DEFINE_STANDARD_COMPOUND_ASSIGNMENT(op, function) \
|
|
|
|
constexpr auto& operator op(UFixedInt auto const& other) \
|
|
|
|
{ \
|
|
|
|
auto func = [](auto&& a, auto&& b, auto&& c) { function(a, b, c); }; \
|
|
|
|
do_standard_compound_assignment(other, func); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
constexpr auto& operator op(IntegerWrapper other) \
|
|
|
|
{ \
|
|
|
|
auto func = [](auto&& a, auto&& b, auto&& c) { function(a, b, c); }; \
|
|
|
|
do_standard_compound_assignment(other, func); \
|
|
|
|
return *this; \
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
// Binary operators
|
|
|
|
DEFINE_STANDARD_BINARY_OPERATOR(^, StorageOperations::compute_bitwise<StorageOperations::Bitwise::XOR>)
|
|
|
|
DEFINE_STANDARD_BINARY_OPERATOR(&, StorageOperations::compute_bitwise<StorageOperations::Bitwise::AND>)
|
|
|
|
DEFINE_STANDARD_BINARY_OPERATOR(|, StorageOperations::compute_bitwise<StorageOperations::Bitwise::OR>)
|
|
|
|
DEFINE_STANDARD_COMPOUND_ASSIGNMENT(^=, StorageOperations::compute_inplace_bitwise<StorageOperations::Bitwise::XOR>)
|
|
|
|
DEFINE_STANDARD_COMPOUND_ASSIGNMENT(&=, StorageOperations::compute_inplace_bitwise<StorageOperations::Bitwise::AND>)
|
|
|
|
DEFINE_STANDARD_COMPOUND_ASSIGNMENT(|=, StorageOperations::compute_inplace_bitwise<StorageOperations::Bitwise::OR>)
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto operator~() const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<bit_size> result;
|
|
|
|
StorageOperations::compute_bitwise<StorageOperations::Bitwise::INVERT>(m_data, m_data, result.m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr auto operator<<(size_t shift) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<bit_size> result;
|
|
|
|
StorageOperations::shift_left(m_data, shift, result.m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr auto& operator<<=(size_t shift)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::shift_left(m_data, shift, m_data);
|
2021-05-19 21:04:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
constexpr auto operator>>(size_t shift) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<bit_size> result;
|
|
|
|
StorageOperations::shift_right(m_data, shift, result.m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto& operator>>=(size_t shift)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::shift_right(m_data, shift, m_data);
|
2021-05-19 21:04:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
// Arithmetic
|
|
|
|
template<UFixedInt T>
|
|
|
|
constexpr auto addc(T const& other, bool& carry) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<max(bit_size, assumed_bit_size<T>)> result;
|
|
|
|
carry = StorageOperations::add<false>(m_data, get_storage_of(other), result.m_data, carry);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
template<UFixedInt T>
|
|
|
|
constexpr auto subc(T const& other, bool& borrow) const
|
2021-07-05 18:14:29 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<max(bit_size, assumed_bit_size<T>)> result;
|
|
|
|
borrow = StorageOperations::add<true>(m_data, get_storage_of(other), result.m_data, borrow);
|
|
|
|
return result;
|
2021-07-05 18:14:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
DEFINE_STANDARD_BINARY_OPERATOR(+, StorageOperations::add<false>)
|
|
|
|
DEFINE_STANDARD_BINARY_OPERATOR(-, StorageOperations::add<true>)
|
|
|
|
DEFINE_STANDARD_COMPOUND_ASSIGNMENT(+=, StorageOperations::add<false>)
|
|
|
|
DEFINE_STANDARD_COMPOUND_ASSIGNMENT(-=, StorageOperations::add<true>)
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto& operator++()
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::increment<false>(m_data);
|
|
|
|
return *this;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto& operator--()
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
StorageOperations::increment<true>(m_data);
|
|
|
|
return *this;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto operator++(int)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<bit_size> result = *this;
|
|
|
|
StorageOperations::increment<false>(m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto operator--(int)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<bit_size> result = *this;
|
|
|
|
StorageOperations::increment<true>(m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
DEFINE_STANDARD_BINARY_OPERATOR(*, mul_internal)
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr auto& operator*=(UFixedInt auto const& other) { return *this = *this * other; }
|
|
|
|
constexpr auto& operator*=(IntegerWrapper const& other) { return *this = *this * other; }
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
template<UFixedInt T>
|
|
|
|
constexpr auto wide_multiply(T const& other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<bit_size + assumed_bit_size<T>> result;
|
|
|
|
mul_internal(m_data, get_storage_of(other), result.m_data);
|
|
|
|
return result;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
// FIXME: Refactor out this
|
|
|
|
using R = UFixedBigInt<bit_size>;
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
static constexpr size_t my_size()
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
return sizeof(Storage);
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
// FIXME: Do something smarter (process at least one word per iteration).
|
2021-05-19 21:04:48 +00:00
|
|
|
// FIXME: no restraints on this
|
|
|
|
template<Unsigned U>
|
2023-02-04 16:03:18 +00:00
|
|
|
requires(sizeof(Storage) >= sizeof(U)) constexpr R div_mod(U const& divisor, U& remainder) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
|
|
|
// FIXME: Is there a better way to raise a division by 0?
|
|
|
|
// Maybe as a compiletime warning?
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdiv-by-zero"
|
|
|
|
if (!divisor) {
|
2022-04-01 17:58:27 +00:00
|
|
|
int volatile x = 1;
|
|
|
|
int volatile y = 0;
|
|
|
|
[[maybe_unused]] int volatile z = x / y;
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
// fastpaths
|
|
|
|
if (*this < divisor) {
|
|
|
|
remainder = static_cast<U>(*this);
|
|
|
|
return 0u;
|
|
|
|
}
|
|
|
|
if (*this == divisor) {
|
|
|
|
remainder = 0u;
|
|
|
|
return 1u;
|
|
|
|
}
|
|
|
|
if (divisor == 1u) {
|
|
|
|
remainder = 0u;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
remainder = 0u;
|
|
|
|
R quotient = 0u;
|
|
|
|
|
|
|
|
for (ssize_t i = sizeof(R) * 8 - clz() - 1; i >= 0; --i) {
|
|
|
|
remainder <<= 1u;
|
2023-02-04 16:03:18 +00:00
|
|
|
remainder |= static_cast<unsigned>(*this >> (size_t)i) & 1u;
|
2021-05-19 21:04:48 +00:00
|
|
|
if (remainder >= divisor) {
|
|
|
|
remainder -= divisor;
|
|
|
|
quotient |= R { 1u } << (size_t)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return quotient;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Unsigned U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr R operator/(U const& other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
|
|
|
U mod { 0u }; // unused
|
|
|
|
return div_mod(other, mod);
|
|
|
|
}
|
|
|
|
template<Unsigned U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr U operator%(U const& other) const
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
|
|
|
R res { 0u };
|
|
|
|
div_mod(other, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Unsigned U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr R& operator/=(U const& other)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
|
|
|
*this = *this / other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<Unsigned U>
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr R& operator%=(U const& other)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
|
|
|
*this = *this % other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:25:47 +00:00
|
|
|
// Note: If there ever be need for non side-channel proof sqrt/pow/pow_mod of UFixedBigInt, you
|
|
|
|
// can restore them from Git history.
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
#undef DEFINE_STANDARD_BINARY_OPERATOR
|
|
|
|
#undef DEFINE_STANDARD_COMPOUND_ASSIGNMENT
|
|
|
|
|
|
|
|
// These functions are intended to be used in LibCrypto for equality checks without branching.
|
2022-03-08 10:46:04 +00:00
|
|
|
constexpr bool is_zero_constant_time() const
|
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
NativeWord fold = 0;
|
|
|
|
for (size_t i = 0; i < m_data.size(); ++i)
|
|
|
|
taint_for_optimizer(fold |= m_data[i]);
|
|
|
|
return !fold;
|
2022-03-08 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
constexpr bool is_equal_to_constant_time(UFixedBigInt<bit_size> other) const
|
2022-03-08 10:46:04 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
NativeWord fold = 0;
|
|
|
|
for (size_t i = 0; i < m_data.size(); ++i)
|
|
|
|
taint_for_optimizer(fold |= m_data[i] ^ other.m_data[i]);
|
|
|
|
return !fold;
|
2022-03-08 10:46:04 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
template<ConvertibleToUFixedInt T, typename Function>
|
|
|
|
constexpr auto do_standard_binary_operation(T const& other, Function function) const
|
2022-03-08 10:46:04 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
UFixedBigInt<max(bit_size, assumed_bit_size<T>)> result;
|
|
|
|
function(m_data, get_storage_of(other), result.m_data);
|
|
|
|
return result;
|
2022-03-08 10:46:04 +00:00
|
|
|
}
|
2023-02-04 16:03:18 +00:00
|
|
|
|
|
|
|
template<ConvertibleToUFixedInt T, typename Function>
|
|
|
|
constexpr void do_standard_compound_assignment(T const& other, Function function)
|
2022-03-08 10:46:04 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
static_assert(bit_size >= assumed_bit_size<T>, "Requested operation requires integer size to be expanded.");
|
|
|
|
function(m_data, get_storage_of(other), m_data);
|
2022-03-08 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
template<size_t other_bit_size, typename OtherStorage>
|
|
|
|
friend class UFixedBigInt;
|
|
|
|
|
|
|
|
friend constexpr auto& get_storage_of<bit_size>(UFixedBigInt<bit_size>&);
|
|
|
|
friend constexpr auto& get_storage_of<bit_size>(UFixedBigInt<bit_size> const&);
|
|
|
|
|
|
|
|
Storage m_data;
|
2021-05-19 21:04:48 +00:00
|
|
|
};
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
// FIXME: There is a bug in LLVM (https://github.com/llvm/llvm-project/issues/59783) which doesn't
|
|
|
|
// allow to use the following comparisons.
|
|
|
|
bool operator==(BuiltInUFixedInt auto const& a, NotBuiltInUFixedInt auto const& b) { return b.operator==(a); }
|
|
|
|
int operator<=>(BuiltInUFixedInt auto const& a, NotBuiltInUFixedInt auto const& b) { return -b.operator<=>(a); }
|
|
|
|
bool operator==(IntegerWrapper const& a, NotBuiltInUFixedInt auto const& b) { return b.operator==(a); }
|
|
|
|
int operator<=>(IntegerWrapper const& a, NotBuiltInUFixedInt auto const& b) { return -b.operator<=>(a); }
|
2022-10-16 22:06:11 +00:00
|
|
|
}
|
2021-05-19 21:04:48 +00:00
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
using Detail::UFixedBigInt;
|
|
|
|
|
|
|
|
template<size_t bit_size>
|
|
|
|
constexpr inline bool IsUnsigned<UFixedBigInt<bit_size>> = true;
|
|
|
|
template<size_t bit_size>
|
|
|
|
constexpr inline bool IsSigned<UFixedBigInt<bit_size>> = false;
|
|
|
|
|
|
|
|
template<size_t bit_size>
|
|
|
|
struct NumericLimits<UFixedBigInt<bit_size>> {
|
|
|
|
using T = UFixedBigInt<bit_size>;
|
|
|
|
|
|
|
|
static constexpr T min() { return T {}; }
|
|
|
|
static constexpr T max() { return --T {}; }
|
|
|
|
static constexpr bool is_signed() { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// ===== Formatting =====
|
|
|
|
// FIXME: This does not work for size != 2 ** x
|
|
|
|
template<Detail::NotBuiltInUFixedInt T>
|
|
|
|
struct Formatter<T> : StandardFormatter {
|
2021-05-19 21:04:48 +00:00
|
|
|
Formatter() = default;
|
|
|
|
explicit Formatter(StandardFormatter formatter)
|
|
|
|
: StandardFormatter(formatter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-04 16:03:18 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, T const& value)
|
2021-05-19 21:04:48 +00:00
|
|
|
{
|
2023-02-04 16:03:18 +00:00
|
|
|
using U = decltype(value.low());
|
|
|
|
|
2021-05-19 21:04:48 +00:00
|
|
|
if (m_precision.has_value())
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
|
|
|
if (m_mode == Mode::Pointer) {
|
|
|
|
// these are way to big for a pointer
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
if (m_mode == Mode::Default)
|
|
|
|
m_mode = Mode::Hexadecimal;
|
|
|
|
|
|
|
|
if (!value.high()) {
|
2023-02-04 16:03:18 +00:00
|
|
|
Formatter<U> formatter { *this };
|
2021-05-19 21:04:48 +00:00
|
|
|
return formatter.format(builder, value.low());
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 base = 0;
|
|
|
|
if (m_mode == Mode::Binary) {
|
|
|
|
base = 2;
|
|
|
|
} else if (m_mode == Mode::BinaryUppercase) {
|
|
|
|
base = 2;
|
|
|
|
} else if (m_mode == Mode::Octal) {
|
|
|
|
TODO();
|
|
|
|
} else if (m_mode == Mode::Decimal) {
|
|
|
|
TODO();
|
|
|
|
} else if (m_mode == Mode::Hexadecimal) {
|
|
|
|
base = 16;
|
|
|
|
} else if (m_mode == Mode::HexadecimalUppercase) {
|
|
|
|
base = 16;
|
|
|
|
} else {
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
ssize_t width = m_width.value_or(0);
|
2023-02-04 16:03:18 +00:00
|
|
|
ssize_t lower_length = ceil_div(Detail::assumed_bit_size<U>, (ssize_t)base);
|
|
|
|
Formatter<U> formatter { *this };
|
2021-05-19 21:04:48 +00:00
|
|
|
formatter.m_width = max(width - lower_length, (ssize_t)0);
|
2021-11-16 00:15:21 +00:00
|
|
|
TRY(formatter.format(builder, value.high()));
|
|
|
|
TRY(builder.put_literal("'"sv));
|
2021-05-19 21:04:48 +00:00
|
|
|
formatter.m_zero_pad = true;
|
|
|
|
formatter.m_alternative_form = false;
|
|
|
|
formatter.m_width = lower_length;
|
2021-11-16 00:15:21 +00:00
|
|
|
TRY(formatter.format(builder, value.low()));
|
|
|
|
return {};
|
2021-05-19 21:04:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// these sizes should suffice for most usecases
|
2023-02-04 16:03:18 +00:00
|
|
|
using u128 = AK::UFixedBigInt<128>;
|
|
|
|
using u256 = AK::UFixedBigInt<256>;
|
|
|
|
using u512 = AK::UFixedBigInt<512>;
|
|
|
|
using u1024 = AK::UFixedBigInt<1024>;
|
|
|
|
using u2048 = AK::UFixedBigInt<2048>;
|
|
|
|
using u4096 = AK::UFixedBigInt<4096>;
|