Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type aliases and literals up to uint512 #317

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,11 +916,6 @@ inline constexpr Int from_string(const std::string& s)
return from_string<Int>(s.c_str());
}

consteval uint128 operator""_u128(const char* s)
{
return from_string<uint128>(s);
}

template <unsigned N>
inline std::string to_string(uint<N> x, int base = 10)
{
Expand Down Expand Up @@ -1104,6 +1099,7 @@ using uint192 = uint<192>;
using uint256 = uint<256>;
using uint320 = uint<320>;
using uint384 = uint<384>;
using uint448 = uint<448>;
using uint512 = uint<512>;

template <unsigned N>
Expand Down Expand Up @@ -1843,17 +1839,27 @@ inline constexpr uint256 mulmod(const uint256& x, const uint256& y, const uint25
return udivrem(umul(x, y), mod).rem;
}


consteval uint256 operator""_u256(const char* s)
{
return from_string<uint256>(s);
}

consteval uint512 operator""_u512(const char* s)
{
return from_string<uint512>(s);
}

/// Define type alias uintN = uint<N> and the matching literal ""_uN.
/// The literal operators are defined in the intx::literals namespace.
#define DEFINE_ALIAS_AND_LITERAL(N) \
using uint##N = uint<N>; \
namespace literals \
{ \
consteval uint##N operator""_u##N(const char* s) \
{ \
return from_string<uint##N>(s); \
} \
}
DEFINE_ALIAS_AND_LITERAL(128);
DEFINE_ALIAS_AND_LITERAL(192);
DEFINE_ALIAS_AND_LITERAL(256);
DEFINE_ALIAS_AND_LITERAL(320);
DEFINE_ALIAS_AND_LITERAL(384);
DEFINE_ALIAS_AND_LITERAL(448);
DEFINE_ALIAS_AND_LITERAL(512);
#undef DEFINE_ALIAS_AND_LITERAL

using namespace literals;

/// Convert native representation to/from little-endian byte order.
/// intx and built-in integral types are supported.
Expand Down
10 changes: 10 additions & 0 deletions test/unittests/test_intx_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

#include "test_suite.hpp"

using namespace intx::literals;

static_assert(1_u128);
static_assert(1_u192);
static_assert(1_u256);
static_assert(1_u320);
static_assert(1_u384);
static_assert(1_u448);
static_assert(1_u512);

using namespace intx;

static_assert(uint128{2} + uint128{2} == 4);
Expand Down