2020-09-04 13:33:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* Divide and multiply
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008
|
|
|
|
* Evan Teran
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and its
|
|
|
|
* documentation for any purpose and without fee is hereby granted, provided
|
|
|
|
* that the above copyright notice appears in all copies and that both the
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the same name not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. We make no representations about the
|
|
|
|
* suitability this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
2020-09-14 11:56:43 +00:00
|
|
|
#include <initializer_list>
|
2020-09-04 13:33:02 +00:00
|
|
|
|
2020-09-14 11:56:43 +00:00
|
|
|
namespace wide
|
2020-09-04 13:33:02 +00:00
|
|
|
{
|
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
class integer;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
struct common_type<wide::integer<Bits, Signed>, wide::integer<Bits2, Signed2>>;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, typename Arithmetic>
|
2020-09-14 11:56:43 +00:00
|
|
|
struct common_type<wide::integer<Bits, Signed>, Arithmetic>;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Arithmetic, size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
struct common_type<Arithmetic, wide::integer<Bits, Signed>>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace wide
|
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed>
|
2021-05-08 00:06:30 +00:00
|
|
|
class integer
|
2020-09-04 13:33:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-09-18 09:51:44 +00:00
|
|
|
using base_type = uint64_t;
|
|
|
|
using signed_base_type = int64_t;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
// ctors
|
2021-01-27 00:54:57 +00:00
|
|
|
constexpr integer() noexcept = default;
|
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
template <typename T>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer(T rhs) noexcept;
|
2021-01-27 00:54:57 +00:00
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
template <typename T>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer(std::initializer_list<T> il) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
// assignment
|
|
|
|
template <size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator=(const integer<Bits2, Signed2> & rhs) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Arithmetic>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator=(Arithmetic rhs) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Arithmetic>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator*=(const Arithmetic & rhs);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Arithmetic>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator/=(const Arithmetic & rhs);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Arithmetic>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator+=(const Arithmetic & rhs) noexcept(std::is_same_v<Signed, unsigned>);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Arithmetic>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator-=(const Arithmetic & rhs) noexcept(std::is_same_v<Signed, unsigned>);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Integral>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator%=(const Integral & rhs);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Integral>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator&=(const Integral & rhs) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Integral>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator|=(const Integral & rhs) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename Integral>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator^=(const Integral & rhs) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator<<=(int n) noexcept;
|
|
|
|
constexpr integer<Bits, Signed> & operator>>=(int n) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> & operator++() noexcept(std::is_same_v<Signed, unsigned>);
|
|
|
|
constexpr integer<Bits, Signed> operator++(int) noexcept(std::is_same_v<Signed, unsigned>);
|
|
|
|
constexpr integer<Bits, Signed> & operator--() noexcept(std::is_same_v<Signed, unsigned>);
|
|
|
|
constexpr integer<Bits, Signed> operator--(int) noexcept(std::is_same_v<Signed, unsigned>);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
// observers
|
|
|
|
|
|
|
|
constexpr explicit operator bool() const noexcept;
|
|
|
|
|
|
|
|
template <class T>
|
2021-05-06 21:04:47 +00:00
|
|
|
using _integral_not_wide_integer_class = typename std::enable_if<std::is_arithmetic<T>::value, T>::type;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
2021-05-06 21:04:47 +00:00
|
|
|
template <class T, class = _integral_not_wide_integer_class<T>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr operator T() const noexcept;
|
|
|
|
|
|
|
|
constexpr operator long double() const noexcept;
|
|
|
|
constexpr operator double() const noexcept;
|
|
|
|
constexpr operator float() const noexcept;
|
|
|
|
|
|
|
|
struct _impl;
|
|
|
|
|
2021-01-27 00:54:57 +00:00
|
|
|
base_type items[_impl::item_count];
|
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
private:
|
|
|
|
template <size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
friend class integer;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
2020-09-14 11:56:43 +00:00
|
|
|
friend class std::numeric_limits<integer<Bits, signed>>;
|
|
|
|
friend class std::numeric_limits<integer<Bits, unsigned>>;
|
2020-09-04 13:33:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static constexpr bool ArithmeticConcept() noexcept;
|
2021-05-06 21:04:47 +00:00
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
template <class T1, class T2>
|
2021-05-06 21:04:47 +00:00
|
|
|
using _only_arithmetic = typename std::enable_if<ArithmeticConcept<T1>() && ArithmeticConcept<T2>()>::type;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static constexpr bool IntegralConcept() noexcept;
|
2021-05-06 21:04:47 +00:00
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
template <class T, class T2>
|
2021-05-06 21:04:47 +00:00
|
|
|
using _only_integer = typename std::enable_if<IntegralConcept<T>() && IntegralConcept<T2>()>::type;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
// Unary operators
|
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator~(const integer<Bits, Signed> & lhs) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator-(const integer<Bits, Signed> & lhs) noexcept(std::is_same_v<Signed, unsigned>);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator+(const integer<Bits, Signed> & lhs) noexcept(std::is_same_v<Signed, unsigned>);
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
// Binary operators
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator*(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Arithmetic, Arithmetic2> constexpr operator*(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator/(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Arithmetic, Arithmetic2> constexpr operator/(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator+(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Arithmetic, Arithmetic2> constexpr operator+(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator-(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Arithmetic, Arithmetic2> constexpr operator-(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator%(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Integral, typename Integral2, class = _only_integer<Integral, Integral2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Integral, Integral2> constexpr operator%(const Integral & rhs, const Integral2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator&(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Integral, typename Integral2, class = _only_integer<Integral, Integral2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Integral, Integral2> constexpr operator&(const Integral & rhs, const Integral2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator|(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Integral, typename Integral2, class = _only_integer<Integral, Integral2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Integral, Integral2> constexpr operator|(const Integral & rhs, const Integral2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
std::common_type_t<integer<Bits, Signed>, integer<Bits2, Signed2>> constexpr
|
|
|
|
operator^(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Integral, typename Integral2, class = _only_integer<Integral, Integral2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
std::common_type_t<Integral, Integral2> constexpr operator^(const Integral & rhs, const Integral2 & lhs);
|
|
|
|
|
|
|
|
// TODO: Integral
|
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator<<(const integer<Bits, Signed> & lhs, int n) noexcept;
|
2021-05-06 21:04:47 +00:00
|
|
|
|
2020-09-04 13:33:02 +00:00
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator>>(const integer<Bits, Signed> & lhs, int n) noexcept;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, typename Int, typename = std::enable_if_t<!std::is_same_v<Int, int>>>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator<<(const integer<Bits, Signed> & lhs, Int n) noexcept
|
2020-09-04 13:33:02 +00:00
|
|
|
{
|
|
|
|
return lhs << int(n);
|
|
|
|
}
|
|
|
|
template <size_t Bits, typename Signed, typename Int, typename = std::enable_if_t<!std::is_same_v<Int, int>>>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr integer<Bits, Signed> operator>>(const integer<Bits, Signed> & lhs, Int n) noexcept
|
2020-09-04 13:33:02 +00:00
|
|
|
{
|
|
|
|
return lhs >> int(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr bool operator<(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr bool operator<(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr bool operator>(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr bool operator>(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr bool operator<=(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr bool operator<=(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr bool operator>=(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr bool operator>=(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr bool operator==(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr bool operator==(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
|
|
|
template <size_t Bits, typename Signed, size_t Bits2, typename Signed2>
|
2020-09-14 11:56:43 +00:00
|
|
|
constexpr bool operator!=(const integer<Bits, Signed> & lhs, const integer<Bits2, Signed2> & rhs);
|
2021-05-06 21:04:47 +00:00
|
|
|
template <typename Arithmetic, typename Arithmetic2, class = _only_arithmetic<Arithmetic, Arithmetic2>>
|
2020-09-04 13:33:02 +00:00
|
|
|
constexpr bool operator!=(const Arithmetic & rhs, const Arithmetic2 & lhs);
|
|
|
|
|
2020-09-14 11:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
template <size_t Bits, typename Signed>
|
2020-09-14 11:56:43 +00:00
|
|
|
struct hash<wide::integer<Bits, Signed>>;
|
2020-09-04 13:33:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "wide_integer_impl.h"
|