128-bit unsigned integerCombining two 32-bit integers into one 64-bit integerArray-like container for uints shorter than 8 bits (Rev 1)Lossy packing 32 bit integer to 16 bitMostly portable 128 by 64 bit divisionPacking and unpacking two 32-bit integers into an unsigned 64-bit integerConcatenate three 16-bit integers in one 64-bit integer with C++Portable safe unsigned integer arithmeticUnsigned integer division ARM Cortex-M0+ AssemblyShifting an 128-bit integer consisting of four 32-bit integersC++ big unsigned integer class

What does kpsewhich stand for?

128-bit unsigned integer

Gravitational Force Between Numbers

Which European Languages are not Indo-European?

What was Stree?

I know that there is a preselected candidate for a position to be filled at my department. What should I do?

What happened to boiled-off gases from the storage tanks at Launch Complex 39?

Is it possible to remotely hack the GPS system and disable GPS service worldwide?

What Armor Optimization applies to a Mithral full plate?

Is the Unsullied name meant to be ironic? How did it come to be?

Why do Russians almost not use verbs of possession akin to "have"?

How to zoom and pan very quick in Illustrator?

Why are Stein manifolds/spaces the analog of affine varieties/schemes in algebraic geometry?

Is the field of q-series 'dead'?

Why didn't Thanos use the Time Stone to stop the Avengers' plan?

Construct a word ladder

Best material to absorb as much light as possible

Is this statement about cut time correct?

Condition on all eigenvalues of a matrix

Can I install a back bike rack without attachment to the rear part of the frame?

Is it legal to have an abortion in another state or abroad?

Have 1.5% of all nuclear reactors ever built melted down?

Python program to find Armstrong numbers in a certain range

Using Arduino sensors with an STM32



128-bit unsigned integer


Combining two 32-bit integers into one 64-bit integerArray-like container for uints shorter than 8 bits (Rev 1)Lossy packing 32 bit integer to 16 bitMostly portable 128 by 64 bit divisionPacking and unpacking two 32-bit integers into an unsigned 64-bit integerConcatenate three 16-bit integers in one 64-bit integer with C++Portable safe unsigned integer arithmeticUnsigned integer division ARM Cortex-M0+ AssemblyShifting an 128-bit integer consisting of four 32-bit integersC++ big unsigned integer class






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7












$begingroup$


This implements 128-bit unsigned integer using C++14.
It works on MSVC and 32-bit architectures being complementary to the unsigned __int128 type provided by GCC and clang on 64-bit architectures.



// intx: extended precision integer library.
// Copyright 2019 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.

#pragma once

#include <algorithm>
#include <climits>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace intx

template <unsigned N>
struct uint;

/// The 128-bit unsigned integer.
///
/// This type is defined as a specialization of uint<> to easier integration with full intx package,
/// however, uint128 may be used independently.
template <>
struct uint<128>

uint64_t lo = 0;
uint64_t hi = 0;

constexpr uint() noexcept = default;

constexpr uint(uint64_t high, uint64_t low) noexcept : lolow, hihigh

template <typename T,
typename = typename std::enable_if_t<std::is_convertible<T, uint64_t>::value>>
constexpr uint(T x) noexcept : lo(static_cast<uint64_t>(x)) // NOLINT


#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
constexpr uint(unsigned __int128 x) noexcept // NOLINT
: louint64_t(x), hiuint64_t(x >> 64)


constexpr explicit operator unsigned __int128() const noexcept
lo;

#pragma GCC diagnostic pop
#endif

constexpr explicit operator bool() const noexcept lo;

/// Explicit converting operator for all builtin integral types.
template <typename Int, typename = typename std::enable_if<std::is_integral<Int>::value>::type>
constexpr explicit operator Int() const noexcept

return static_cast<Int>(lo);

;

using uint128 = uint<128>;


/// Linear arithmetic operators.
/// @

constexpr uint128 operator+(uint128 x, uint128 y) noexcept

const auto lo = x.lo + y.lo;
const auto carry = x.lo > lo;
const auto hi = x.hi + y.hi + carry;
return hi, lo;


constexpr uint128 operator+(uint128 x) noexcept

return x;


constexpr uint128 operator-(uint128 x, uint128 y) noexcept

const auto lo = x.lo - y.lo;
const auto borrow = x.lo < lo;
const auto hi = x.hi - y.hi - borrow;
return hi, lo;


constexpr uint128 operator-(uint128 x) noexcept

// Implementing as subtraction is better than ~x + 1.
// Clang9: Perfect.
// GCC8: Does something weird.
return 0 - x;


inline uint128& operator++(uint128& x) noexcept

return x = x + 1;


inline uint128& operator--(uint128& x) noexcept

return x = x - 1;


inline uint128 operator++(uint128& x, int) noexcept

auto ret = x;
++x;
return ret;


inline uint128 operator--(uint128& x, int) noexcept

auto ret = x;
--x;
return ret;


/// Optimized addition.
///
/// This keeps the multiprecision addition until CodeGen so the pattern is not
/// broken during other optimizations.
constexpr uint128 fast_add(uint128 x, uint128 y) noexcept

#ifdef __SIZEOF_INT128__xxx
return (unsigned __int128)x + (unsigned __int128)y;
#else
// Fallback to regular addition.
return x + y;
#endif


/// @


/// Comparison operators.
///
/// In all implementations bitwise operators are used instead of logical ones
/// to avoid branching.
///
/// @

constexpr bool operator==(uint128 x, uint128 y) noexcept

// Clang7: generates perfect xor based code,
// much better than __int128 where it uses vector instructions.
// GCC8: generates a bit worse cmp based code
// although it generates the xor based one for __int128.
return (x.lo == y.lo) & (x.hi == y.hi);


constexpr bool operator!=(uint128 x, uint128 y) noexcept
(x.hi != y.hi);


constexpr bool operator<(uint128 x, uint128 y) noexcept

// OPT: This should be implemented by checking the borrow of x - y,
// but compilers (GCC8, Clang7)
// have problem with properly optimizing subtraction.
return (x.hi < y.hi)

constexpr bool operator<=(uint128 x, uint128 y) noexcept
((x.hi == y.hi) & (x.lo <= y.lo));


constexpr bool operator>(uint128 x, uint128 y) noexcept

return !(x <= y);


constexpr bool operator>=(uint128 x, uint128 y) noexcept

return !(x < y);


/// @


/// Bitwise operators.
/// @(uint128 x, uint128 y) noexcept

// Clang7: perfect.
// GCC8: stupidly uses a vector instruction in all bitwise operators.
return y.lo;


constexpr uint128 operator&(uint128 x, uint128 y) noexcept

return x.hi & y.hi, x.lo & y.lo;


constexpr uint128 operator^(uint128 x, uint128 y) noexcept

return x.hi ^ y.hi, x.lo ^ y.lo;


constexpr uint128 operator<<(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 right shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.lo >> 1) >> (63 - shift)), x.lo << shift :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint128x.lo << (shift - 64), 0 : 0;


constexpr uint128 operator<<(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x << unsigned(shift);
return 0;


constexpr uint128 operator>>(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 left shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.hi << 1) << (63 - shift)) :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint1280, x.hi >> (shift - 64) : 0;


constexpr uint128 operator>>(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x >> unsigned(shift);
return 0;



/// @


/// Multiplication
/// @

/// Portable full unsigned multiplication 64 x 64 -> 128.
constexpr uint128 constexpr_umul(uint64_t x, uint64_t y) noexcept

uint64_t xl = x & 0xffffffff;
uint64_t xh = x >> 32;
uint64_t yl = y & 0xffffffff;
uint64_t yh = y >> 32;

uint64_t t0 = xl * yl;
uint64_t t1 = xh * yl;
uint64_t t2 = xl * yh;
uint64_t t3 = xh * yh;

uint64_t u1 = t1 + (t0 >> 32);
uint64_t u2 = t2 + (u1 & 0xffffffff);

uint64_t lo = (u2 << 32)

/// Full unsigned multiplication 64 x 64 -> 128.
inline uint128 umul(uint64_t x, uint64_t y) noexcept

#if defined(__SIZEOF_INT128__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
const auto p = static_cast<unsigned __int128>(x) * y;
return uint64_t(p >> 64), uint64_t(p);
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
unsigned __int64 hi;
const auto lo = _umul128(x, y, &hi);
return hi, lo;
#else
return constexpr_umul(x, y);
#endif


inline uint128 operator*(uint128 x, uint128 y) noexcept

auto p = umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


constexpr uint128 constexpr_mul(uint128 x, uint128 y) noexcept

auto p = constexpr_umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


/// @


/// Assignment operators.
/// @

constexpr uint128& operator+=(uint128& x, uint128 y) noexcept

return x = x + y;


constexpr uint128& operator-=(uint128& x, uint128 y) noexcept

return x = x - y;


inline uint128& operator*=(uint128& x, uint128 y) noexcept

return x = x * y;


constexpr uint128& operator


inline unsigned clz(uint32_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse(&most_significant_bit, x);
return 31 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clz(x));
#endif


inline unsigned clz(uint64_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse64(&most_significant_bit, x);
return 63 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clzll(x));
#endif


inline unsigned clz(uint128 x) noexcept

// In this order `h == 0` we get less instructions than in case of `h != 0`.
return x.hi == 0 ? clz(x.lo)


inline uint64_t bswap(uint64_t x) noexcept

#ifdef _MSC_VER
return _byteswap_uint64(x);
#else
return __builtin_bswap64(x);
#endif


inline uint128 bswap(uint128 x) noexcept

return bswap(x.lo), bswap(x.hi);



/// Division.
/// @

template <typename T>
struct div_result

T quot;
T rem;
;

namespace internal

constexpr uint16_t reciprocal_table_item(uint8_t d9) noexcept
d9));


#define REPEAT4(x)
reciprocal_table_item((x) + 0), reciprocal_table_item((x) + 1),
reciprocal_table_item((x) + 2), reciprocal_table_item((x) + 3)

#define REPEAT32(x)
REPEAT4((x) + 4 * 0), REPEAT4((x) + 4 * 1), REPEAT4((x) + 4 * 2), REPEAT4((x) + 4 * 3),
REPEAT4((x) + 4 * 4), REPEAT4((x) + 4 * 5), REPEAT4((x) + 4 * 6), REPEAT4((x) + 4 * 7)

#define REPEAT256()
REPEAT32(32 * 0), REPEAT32(32 * 1), REPEAT32(32 * 2), REPEAT32(32 * 3), REPEAT32(32 * 4),
REPEAT32(32 * 5), REPEAT32(32 * 6), REPEAT32(32 * 7)

/// Reciprocal lookup table.
constexpr uint16_t reciprocal_table[] = REPEAT256();

#undef REPEAT4
#undef REPEAT32
#undef REPEAT256
// namespace internal

/// Computes the reciprocal (2^128 - 1) / d - 2^64 for normalized d.
///
/// Based on Algorithm 2 from "Improved division by invariant integers".
inline uint64_t reciprocal_2by1(uint64_t d) noexcept

auto d9 = uint8_t(d >> 55);
auto v0 = uint64_tinternal::reciprocal_table[d9];

auto d40 = (d >> 24) + 1;
auto v1 = (v0 << 11) - (v0 * v0 * d40 >> 40) - 1;

auto v2 = (v1 << 13) + (v1 * (0x1000000000000000 - v1 * d40) >> 47);

auto d0 = d % 2;
auto d63 = d / 2 + d0; // ceil(d/2)
auto nd0 = uint64_t(-int64_t(d0));
auto e = ((v2 / 2) & nd0) - v2 * d63;
auto mh = umul(v2, e).hi;
auto v3 = (v2 << 31) + (mh >> 1);

// OPT: The compiler tries a bit too much with 128 + 64 addition and ends up using subtraction.
// Compare with __int128.
auto mf = umul(v3, d);
auto m = fast_add(mf, d);
auto v3a = m.hi + d;

auto v4 = v3 - v3a;

return v4;


inline uint64_t reciprocal_3by2(uint128 d) noexcept

auto v = reciprocal_2by1(d.hi);
auto p = d.hi * v;
p += d.lo;
if (p < d.lo)

--v;
if (p >= d.hi)

--v;
p -= d.hi;

p -= d.hi;


auto t = umul(v, d.lo);

p += t.hi;
if (p < t.hi)

--v;
if (uint128p, t.lo >= d)
--v;

return v;


inline div_result<uint64_t> udivrem_2by1(uint128 u, uint64_t d, uint64_t v) noexcept

auto q = umul(v, u.hi);
q = fast_add(q, u);

++q.hi;

auto r = u.lo - q.hi * d;

if (r > q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem_3by2(
uint64_t u2, uint64_t u1, uint64_t u0, uint128 d, uint64_t v) noexcept

auto q = umul(v, u2);
q = fast_add(q, u2, u1);

auto r1 = u1 - q.hi * d.hi;

auto t = umul(d.lo, q.hi);

auto r = uint128r1, u0 - t - d;
r1 = r.hi;

++q.hi;

if (r1 >= q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem(uint128 x, uint128 y) noexcept
(x.hi << lsh);
auto xn_lo = x.lo << lsh;

auto v = reciprocal_3by2(yn_hi, yn_lo);
auto res = udivrem_3by2(xn_ex, xn_hi, xn_lo, yn_hi, yn_lo, v);

return res.quot, res.rem >> lsh;


inline div_result<uint128> sdivrem(uint128 x, uint128 y) noexcept

constexpr auto sign_mask = uint1281 << 127;
const auto x_is_neg = (x & sign_mask) != 0;
const auto y_is_neg = (y & sign_mask) != 0;

const auto x_abs = x_is_neg ? -x : x;
const auto y_abs = y_is_neg ? -y : y;

const auto q_is_neg = x_is_neg ^ y_is_neg;

const auto res = udivrem(x_abs, y_abs);

return q_is_neg ? -res.quot : res.quot, x_is_neg ? -res.rem : res.rem;


inline uint128 operator/(uint128 x, uint128 y) noexcept

return udivrem(x, y).quot;


inline uint128 operator%(uint128 x, uint128 y) noexcept

return udivrem(x, y).rem;


inline uint128& operator/=(uint128& x, uint128 y) noexcept

return x = x / y;


inline uint128& operator%=(uint128& x, uint128 y) noexcept

return x = x % y;


/// @

// namespace intx


namespace std

template <unsigned N>
struct numeric_limits<intx::uint<N>>

using type = intx::uint<N>;

static constexpr bool is_specialized = true;
static constexpr bool is_integer = true;
static constexpr bool is_signed = false;
static constexpr bool is_exact = true;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr float_round_style round_style = round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = true;
static constexpr int digits = CHAR_BIT * sizeof(type);
static constexpr int digits10 = int(0.3010299956639812 * digits);
static constexpr int max_digits10 = 0;
static constexpr int radix = 2;
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool traps = std::numeric_limits<unsigned>::traps;
static constexpr bool tinyness_before = false;

static constexpr type min() noexcept return 0;
static constexpr type lowest() noexcept return min();
static constexpr type max() noexcept return ~type0;
static constexpr type epsilon() noexcept return 0;
static constexpr type round_error() noexcept return 0;
static constexpr type infinity() noexcept return 0;
static constexpr type quiet_NaN() noexcept return 0;
static constexpr type signaling_NaN() noexcept return 0;
static constexpr type denorm_min() noexcept return 0;
;
// namespace std

namespace intx

template <typename Int>
constexpr Int from_string(const char* s)

using namespace std::literals;

auto x = Int;
int num_digits = 0;

if (s[0] == '0' && s[1] == 'x')

s += 2;
while (auto d = *s++)

if (++num_digits > intsizeof(x) * 2)
throw std::overflow_error"Integer overflow";

x <<= 4;
if (d >= '0' && d <= '9')
d -= '0';
else if (d >= 'a' && d <= 'f')
d -= 'a' - 10;
else if (d >= 'A' && d <= 'F')
d -= 'A' - 10;
else
throw std::invalid_argument"Invalid literal character: "s + d;
x
return x;


while (auto d = *s++)

if (num_digits++ > std::numeric_limits<Int>::digits10)
throw std::overflow_error"Integer overflow";

x = constexpr_mul(x, Int10);
if (d >= '0' && d <= '9')
d -= '0';
else
throw std::invalid_argument"Invalid literal character: "s + d;
x += d;
if (x < d)
throw std::overflow_error"Integer overflow";

return x;


template <typename Int>
constexpr Int from_string(const std::string& s)

return from_string<Int>(s.c_str());


constexpr 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)


template <unsigned N>
inline std::string hex(uint<N> x)

return to_string(x, 16);

// namespace intx
```









share|improve this question







New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $begingroup$
    When you write "complementary to the unsigned __int128 type" do you mean that it's a drop-in replacement? (so one could write #define u128 unsigned __int128 or #define u128 intx::uint<128> depending on whether there's a native 128-bit integer)
    $endgroup$
    – Toby Speight
    7 hours ago


















7












$begingroup$


This implements 128-bit unsigned integer using C++14.
It works on MSVC and 32-bit architectures being complementary to the unsigned __int128 type provided by GCC and clang on 64-bit architectures.



// intx: extended precision integer library.
// Copyright 2019 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.

#pragma once

#include <algorithm>
#include <climits>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace intx

template <unsigned N>
struct uint;

/// The 128-bit unsigned integer.
///
/// This type is defined as a specialization of uint<> to easier integration with full intx package,
/// however, uint128 may be used independently.
template <>
struct uint<128>

uint64_t lo = 0;
uint64_t hi = 0;

constexpr uint() noexcept = default;

constexpr uint(uint64_t high, uint64_t low) noexcept : lolow, hihigh

template <typename T,
typename = typename std::enable_if_t<std::is_convertible<T, uint64_t>::value>>
constexpr uint(T x) noexcept : lo(static_cast<uint64_t>(x)) // NOLINT


#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
constexpr uint(unsigned __int128 x) noexcept // NOLINT
: louint64_t(x), hiuint64_t(x >> 64)


constexpr explicit operator unsigned __int128() const noexcept
lo;

#pragma GCC diagnostic pop
#endif

constexpr explicit operator bool() const noexcept lo;

/// Explicit converting operator for all builtin integral types.
template <typename Int, typename = typename std::enable_if<std::is_integral<Int>::value>::type>
constexpr explicit operator Int() const noexcept

return static_cast<Int>(lo);

;

using uint128 = uint<128>;


/// Linear arithmetic operators.
/// @

constexpr uint128 operator+(uint128 x, uint128 y) noexcept

const auto lo = x.lo + y.lo;
const auto carry = x.lo > lo;
const auto hi = x.hi + y.hi + carry;
return hi, lo;


constexpr uint128 operator+(uint128 x) noexcept

return x;


constexpr uint128 operator-(uint128 x, uint128 y) noexcept

const auto lo = x.lo - y.lo;
const auto borrow = x.lo < lo;
const auto hi = x.hi - y.hi - borrow;
return hi, lo;


constexpr uint128 operator-(uint128 x) noexcept

// Implementing as subtraction is better than ~x + 1.
// Clang9: Perfect.
// GCC8: Does something weird.
return 0 - x;


inline uint128& operator++(uint128& x) noexcept

return x = x + 1;


inline uint128& operator--(uint128& x) noexcept

return x = x - 1;


inline uint128 operator++(uint128& x, int) noexcept

auto ret = x;
++x;
return ret;


inline uint128 operator--(uint128& x, int) noexcept

auto ret = x;
--x;
return ret;


/// Optimized addition.
///
/// This keeps the multiprecision addition until CodeGen so the pattern is not
/// broken during other optimizations.
constexpr uint128 fast_add(uint128 x, uint128 y) noexcept

#ifdef __SIZEOF_INT128__xxx
return (unsigned __int128)x + (unsigned __int128)y;
#else
// Fallback to regular addition.
return x + y;
#endif


/// @


/// Comparison operators.
///
/// In all implementations bitwise operators are used instead of logical ones
/// to avoid branching.
///
/// @

constexpr bool operator==(uint128 x, uint128 y) noexcept

// Clang7: generates perfect xor based code,
// much better than __int128 where it uses vector instructions.
// GCC8: generates a bit worse cmp based code
// although it generates the xor based one for __int128.
return (x.lo == y.lo) & (x.hi == y.hi);


constexpr bool operator!=(uint128 x, uint128 y) noexcept
(x.hi != y.hi);


constexpr bool operator<(uint128 x, uint128 y) noexcept

// OPT: This should be implemented by checking the borrow of x - y,
// but compilers (GCC8, Clang7)
// have problem with properly optimizing subtraction.
return (x.hi < y.hi)

constexpr bool operator<=(uint128 x, uint128 y) noexcept
((x.hi == y.hi) & (x.lo <= y.lo));


constexpr bool operator>(uint128 x, uint128 y) noexcept

return !(x <= y);


constexpr bool operator>=(uint128 x, uint128 y) noexcept

return !(x < y);


/// @


/// Bitwise operators.
/// @(uint128 x, uint128 y) noexcept

// Clang7: perfect.
// GCC8: stupidly uses a vector instruction in all bitwise operators.
return y.lo;


constexpr uint128 operator&(uint128 x, uint128 y) noexcept

return x.hi & y.hi, x.lo & y.lo;


constexpr uint128 operator^(uint128 x, uint128 y) noexcept

return x.hi ^ y.hi, x.lo ^ y.lo;


constexpr uint128 operator<<(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 right shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.lo >> 1) >> (63 - shift)), x.lo << shift :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint128x.lo << (shift - 64), 0 : 0;


constexpr uint128 operator<<(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x << unsigned(shift);
return 0;


constexpr uint128 operator>>(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 left shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.hi << 1) << (63 - shift)) :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint1280, x.hi >> (shift - 64) : 0;


constexpr uint128 operator>>(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x >> unsigned(shift);
return 0;



/// @


/// Multiplication
/// @

/// Portable full unsigned multiplication 64 x 64 -> 128.
constexpr uint128 constexpr_umul(uint64_t x, uint64_t y) noexcept

uint64_t xl = x & 0xffffffff;
uint64_t xh = x >> 32;
uint64_t yl = y & 0xffffffff;
uint64_t yh = y >> 32;

uint64_t t0 = xl * yl;
uint64_t t1 = xh * yl;
uint64_t t2 = xl * yh;
uint64_t t3 = xh * yh;

uint64_t u1 = t1 + (t0 >> 32);
uint64_t u2 = t2 + (u1 & 0xffffffff);

uint64_t lo = (u2 << 32)

/// Full unsigned multiplication 64 x 64 -> 128.
inline uint128 umul(uint64_t x, uint64_t y) noexcept

#if defined(__SIZEOF_INT128__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
const auto p = static_cast<unsigned __int128>(x) * y;
return uint64_t(p >> 64), uint64_t(p);
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
unsigned __int64 hi;
const auto lo = _umul128(x, y, &hi);
return hi, lo;
#else
return constexpr_umul(x, y);
#endif


inline uint128 operator*(uint128 x, uint128 y) noexcept

auto p = umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


constexpr uint128 constexpr_mul(uint128 x, uint128 y) noexcept

auto p = constexpr_umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


/// @


/// Assignment operators.
/// @

constexpr uint128& operator+=(uint128& x, uint128 y) noexcept

return x = x + y;


constexpr uint128& operator-=(uint128& x, uint128 y) noexcept

return x = x - y;


inline uint128& operator*=(uint128& x, uint128 y) noexcept

return x = x * y;


constexpr uint128& operator


inline unsigned clz(uint32_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse(&most_significant_bit, x);
return 31 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clz(x));
#endif


inline unsigned clz(uint64_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse64(&most_significant_bit, x);
return 63 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clzll(x));
#endif


inline unsigned clz(uint128 x) noexcept

// In this order `h == 0` we get less instructions than in case of `h != 0`.
return x.hi == 0 ? clz(x.lo)


inline uint64_t bswap(uint64_t x) noexcept

#ifdef _MSC_VER
return _byteswap_uint64(x);
#else
return __builtin_bswap64(x);
#endif


inline uint128 bswap(uint128 x) noexcept

return bswap(x.lo), bswap(x.hi);



/// Division.
/// @

template <typename T>
struct div_result

T quot;
T rem;
;

namespace internal

constexpr uint16_t reciprocal_table_item(uint8_t d9) noexcept
d9));


#define REPEAT4(x)
reciprocal_table_item((x) + 0), reciprocal_table_item((x) + 1),
reciprocal_table_item((x) + 2), reciprocal_table_item((x) + 3)

#define REPEAT32(x)
REPEAT4((x) + 4 * 0), REPEAT4((x) + 4 * 1), REPEAT4((x) + 4 * 2), REPEAT4((x) + 4 * 3),
REPEAT4((x) + 4 * 4), REPEAT4((x) + 4 * 5), REPEAT4((x) + 4 * 6), REPEAT4((x) + 4 * 7)

#define REPEAT256()
REPEAT32(32 * 0), REPEAT32(32 * 1), REPEAT32(32 * 2), REPEAT32(32 * 3), REPEAT32(32 * 4),
REPEAT32(32 * 5), REPEAT32(32 * 6), REPEAT32(32 * 7)

/// Reciprocal lookup table.
constexpr uint16_t reciprocal_table[] = REPEAT256();

#undef REPEAT4
#undef REPEAT32
#undef REPEAT256
// namespace internal

/// Computes the reciprocal (2^128 - 1) / d - 2^64 for normalized d.
///
/// Based on Algorithm 2 from "Improved division by invariant integers".
inline uint64_t reciprocal_2by1(uint64_t d) noexcept

auto d9 = uint8_t(d >> 55);
auto v0 = uint64_tinternal::reciprocal_table[d9];

auto d40 = (d >> 24) + 1;
auto v1 = (v0 << 11) - (v0 * v0 * d40 >> 40) - 1;

auto v2 = (v1 << 13) + (v1 * (0x1000000000000000 - v1 * d40) >> 47);

auto d0 = d % 2;
auto d63 = d / 2 + d0; // ceil(d/2)
auto nd0 = uint64_t(-int64_t(d0));
auto e = ((v2 / 2) & nd0) - v2 * d63;
auto mh = umul(v2, e).hi;
auto v3 = (v2 << 31) + (mh >> 1);

// OPT: The compiler tries a bit too much with 128 + 64 addition and ends up using subtraction.
// Compare with __int128.
auto mf = umul(v3, d);
auto m = fast_add(mf, d);
auto v3a = m.hi + d;

auto v4 = v3 - v3a;

return v4;


inline uint64_t reciprocal_3by2(uint128 d) noexcept

auto v = reciprocal_2by1(d.hi);
auto p = d.hi * v;
p += d.lo;
if (p < d.lo)

--v;
if (p >= d.hi)

--v;
p -= d.hi;

p -= d.hi;


auto t = umul(v, d.lo);

p += t.hi;
if (p < t.hi)

--v;
if (uint128p, t.lo >= d)
--v;

return v;


inline div_result<uint64_t> udivrem_2by1(uint128 u, uint64_t d, uint64_t v) noexcept

auto q = umul(v, u.hi);
q = fast_add(q, u);

++q.hi;

auto r = u.lo - q.hi * d;

if (r > q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem_3by2(
uint64_t u2, uint64_t u1, uint64_t u0, uint128 d, uint64_t v) noexcept

auto q = umul(v, u2);
q = fast_add(q, u2, u1);

auto r1 = u1 - q.hi * d.hi;

auto t = umul(d.lo, q.hi);

auto r = uint128r1, u0 - t - d;
r1 = r.hi;

++q.hi;

if (r1 >= q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem(uint128 x, uint128 y) noexcept
(x.hi << lsh);
auto xn_lo = x.lo << lsh;

auto v = reciprocal_3by2(yn_hi, yn_lo);
auto res = udivrem_3by2(xn_ex, xn_hi, xn_lo, yn_hi, yn_lo, v);

return res.quot, res.rem >> lsh;


inline div_result<uint128> sdivrem(uint128 x, uint128 y) noexcept

constexpr auto sign_mask = uint1281 << 127;
const auto x_is_neg = (x & sign_mask) != 0;
const auto y_is_neg = (y & sign_mask) != 0;

const auto x_abs = x_is_neg ? -x : x;
const auto y_abs = y_is_neg ? -y : y;

const auto q_is_neg = x_is_neg ^ y_is_neg;

const auto res = udivrem(x_abs, y_abs);

return q_is_neg ? -res.quot : res.quot, x_is_neg ? -res.rem : res.rem;


inline uint128 operator/(uint128 x, uint128 y) noexcept

return udivrem(x, y).quot;


inline uint128 operator%(uint128 x, uint128 y) noexcept

return udivrem(x, y).rem;


inline uint128& operator/=(uint128& x, uint128 y) noexcept

return x = x / y;


inline uint128& operator%=(uint128& x, uint128 y) noexcept

return x = x % y;


/// @

// namespace intx


namespace std

template <unsigned N>
struct numeric_limits<intx::uint<N>>

using type = intx::uint<N>;

static constexpr bool is_specialized = true;
static constexpr bool is_integer = true;
static constexpr bool is_signed = false;
static constexpr bool is_exact = true;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr float_round_style round_style = round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = true;
static constexpr int digits = CHAR_BIT * sizeof(type);
static constexpr int digits10 = int(0.3010299956639812 * digits);
static constexpr int max_digits10 = 0;
static constexpr int radix = 2;
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool traps = std::numeric_limits<unsigned>::traps;
static constexpr bool tinyness_before = false;

static constexpr type min() noexcept return 0;
static constexpr type lowest() noexcept return min();
static constexpr type max() noexcept return ~type0;
static constexpr type epsilon() noexcept return 0;
static constexpr type round_error() noexcept return 0;
static constexpr type infinity() noexcept return 0;
static constexpr type quiet_NaN() noexcept return 0;
static constexpr type signaling_NaN() noexcept return 0;
static constexpr type denorm_min() noexcept return 0;
;
// namespace std

namespace intx

template <typename Int>
constexpr Int from_string(const char* s)

using namespace std::literals;

auto x = Int;
int num_digits = 0;

if (s[0] == '0' && s[1] == 'x')

s += 2;
while (auto d = *s++)

if (++num_digits > intsizeof(x) * 2)
throw std::overflow_error"Integer overflow";

x <<= 4;
if (d >= '0' && d <= '9')
d -= '0';
else if (d >= 'a' && d <= 'f')
d -= 'a' - 10;
else if (d >= 'A' && d <= 'F')
d -= 'A' - 10;
else
throw std::invalid_argument"Invalid literal character: "s + d;
x
return x;


while (auto d = *s++)

if (num_digits++ > std::numeric_limits<Int>::digits10)
throw std::overflow_error"Integer overflow";

x = constexpr_mul(x, Int10);
if (d >= '0' && d <= '9')
d -= '0';
else
throw std::invalid_argument"Invalid literal character: "s + d;
x += d;
if (x < d)
throw std::overflow_error"Integer overflow";

return x;


template <typename Int>
constexpr Int from_string(const std::string& s)

return from_string<Int>(s.c_str());


constexpr 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)


template <unsigned N>
inline std::string hex(uint<N> x)

return to_string(x, 16);

// namespace intx
```









share|improve this question







New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $begingroup$
    When you write "complementary to the unsigned __int128 type" do you mean that it's a drop-in replacement? (so one could write #define u128 unsigned __int128 or #define u128 intx::uint<128> depending on whether there's a native 128-bit integer)
    $endgroup$
    – Toby Speight
    7 hours ago














7












7








7





$begingroup$


This implements 128-bit unsigned integer using C++14.
It works on MSVC and 32-bit architectures being complementary to the unsigned __int128 type provided by GCC and clang on 64-bit architectures.



// intx: extended precision integer library.
// Copyright 2019 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.

#pragma once

#include <algorithm>
#include <climits>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace intx

template <unsigned N>
struct uint;

/// The 128-bit unsigned integer.
///
/// This type is defined as a specialization of uint<> to easier integration with full intx package,
/// however, uint128 may be used independently.
template <>
struct uint<128>

uint64_t lo = 0;
uint64_t hi = 0;

constexpr uint() noexcept = default;

constexpr uint(uint64_t high, uint64_t low) noexcept : lolow, hihigh

template <typename T,
typename = typename std::enable_if_t<std::is_convertible<T, uint64_t>::value>>
constexpr uint(T x) noexcept : lo(static_cast<uint64_t>(x)) // NOLINT


#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
constexpr uint(unsigned __int128 x) noexcept // NOLINT
: louint64_t(x), hiuint64_t(x >> 64)


constexpr explicit operator unsigned __int128() const noexcept
lo;

#pragma GCC diagnostic pop
#endif

constexpr explicit operator bool() const noexcept lo;

/// Explicit converting operator for all builtin integral types.
template <typename Int, typename = typename std::enable_if<std::is_integral<Int>::value>::type>
constexpr explicit operator Int() const noexcept

return static_cast<Int>(lo);

;

using uint128 = uint<128>;


/// Linear arithmetic operators.
/// @

constexpr uint128 operator+(uint128 x, uint128 y) noexcept

const auto lo = x.lo + y.lo;
const auto carry = x.lo > lo;
const auto hi = x.hi + y.hi + carry;
return hi, lo;


constexpr uint128 operator+(uint128 x) noexcept

return x;


constexpr uint128 operator-(uint128 x, uint128 y) noexcept

const auto lo = x.lo - y.lo;
const auto borrow = x.lo < lo;
const auto hi = x.hi - y.hi - borrow;
return hi, lo;


constexpr uint128 operator-(uint128 x) noexcept

// Implementing as subtraction is better than ~x + 1.
// Clang9: Perfect.
// GCC8: Does something weird.
return 0 - x;


inline uint128& operator++(uint128& x) noexcept

return x = x + 1;


inline uint128& operator--(uint128& x) noexcept

return x = x - 1;


inline uint128 operator++(uint128& x, int) noexcept

auto ret = x;
++x;
return ret;


inline uint128 operator--(uint128& x, int) noexcept

auto ret = x;
--x;
return ret;


/// Optimized addition.
///
/// This keeps the multiprecision addition until CodeGen so the pattern is not
/// broken during other optimizations.
constexpr uint128 fast_add(uint128 x, uint128 y) noexcept

#ifdef __SIZEOF_INT128__xxx
return (unsigned __int128)x + (unsigned __int128)y;
#else
// Fallback to regular addition.
return x + y;
#endif


/// @


/// Comparison operators.
///
/// In all implementations bitwise operators are used instead of logical ones
/// to avoid branching.
///
/// @

constexpr bool operator==(uint128 x, uint128 y) noexcept

// Clang7: generates perfect xor based code,
// much better than __int128 where it uses vector instructions.
// GCC8: generates a bit worse cmp based code
// although it generates the xor based one for __int128.
return (x.lo == y.lo) & (x.hi == y.hi);


constexpr bool operator!=(uint128 x, uint128 y) noexcept
(x.hi != y.hi);


constexpr bool operator<(uint128 x, uint128 y) noexcept

// OPT: This should be implemented by checking the borrow of x - y,
// but compilers (GCC8, Clang7)
// have problem with properly optimizing subtraction.
return (x.hi < y.hi)

constexpr bool operator<=(uint128 x, uint128 y) noexcept
((x.hi == y.hi) & (x.lo <= y.lo));


constexpr bool operator>(uint128 x, uint128 y) noexcept

return !(x <= y);


constexpr bool operator>=(uint128 x, uint128 y) noexcept

return !(x < y);


/// @


/// Bitwise operators.
/// @(uint128 x, uint128 y) noexcept

// Clang7: perfect.
// GCC8: stupidly uses a vector instruction in all bitwise operators.
return y.lo;


constexpr uint128 operator&(uint128 x, uint128 y) noexcept

return x.hi & y.hi, x.lo & y.lo;


constexpr uint128 operator^(uint128 x, uint128 y) noexcept

return x.hi ^ y.hi, x.lo ^ y.lo;


constexpr uint128 operator<<(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 right shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.lo >> 1) >> (63 - shift)), x.lo << shift :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint128x.lo << (shift - 64), 0 : 0;


constexpr uint128 operator<<(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x << unsigned(shift);
return 0;


constexpr uint128 operator>>(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 left shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.hi << 1) << (63 - shift)) :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint1280, x.hi >> (shift - 64) : 0;


constexpr uint128 operator>>(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x >> unsigned(shift);
return 0;



/// @


/// Multiplication
/// @

/// Portable full unsigned multiplication 64 x 64 -> 128.
constexpr uint128 constexpr_umul(uint64_t x, uint64_t y) noexcept

uint64_t xl = x & 0xffffffff;
uint64_t xh = x >> 32;
uint64_t yl = y & 0xffffffff;
uint64_t yh = y >> 32;

uint64_t t0 = xl * yl;
uint64_t t1 = xh * yl;
uint64_t t2 = xl * yh;
uint64_t t3 = xh * yh;

uint64_t u1 = t1 + (t0 >> 32);
uint64_t u2 = t2 + (u1 & 0xffffffff);

uint64_t lo = (u2 << 32)

/// Full unsigned multiplication 64 x 64 -> 128.
inline uint128 umul(uint64_t x, uint64_t y) noexcept

#if defined(__SIZEOF_INT128__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
const auto p = static_cast<unsigned __int128>(x) * y;
return uint64_t(p >> 64), uint64_t(p);
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
unsigned __int64 hi;
const auto lo = _umul128(x, y, &hi);
return hi, lo;
#else
return constexpr_umul(x, y);
#endif


inline uint128 operator*(uint128 x, uint128 y) noexcept

auto p = umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


constexpr uint128 constexpr_mul(uint128 x, uint128 y) noexcept

auto p = constexpr_umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


/// @


/// Assignment operators.
/// @

constexpr uint128& operator+=(uint128& x, uint128 y) noexcept

return x = x + y;


constexpr uint128& operator-=(uint128& x, uint128 y) noexcept

return x = x - y;


inline uint128& operator*=(uint128& x, uint128 y) noexcept

return x = x * y;


constexpr uint128& operator


inline unsigned clz(uint32_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse(&most_significant_bit, x);
return 31 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clz(x));
#endif


inline unsigned clz(uint64_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse64(&most_significant_bit, x);
return 63 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clzll(x));
#endif


inline unsigned clz(uint128 x) noexcept

// In this order `h == 0` we get less instructions than in case of `h != 0`.
return x.hi == 0 ? clz(x.lo)


inline uint64_t bswap(uint64_t x) noexcept

#ifdef _MSC_VER
return _byteswap_uint64(x);
#else
return __builtin_bswap64(x);
#endif


inline uint128 bswap(uint128 x) noexcept

return bswap(x.lo), bswap(x.hi);



/// Division.
/// @

template <typename T>
struct div_result

T quot;
T rem;
;

namespace internal

constexpr uint16_t reciprocal_table_item(uint8_t d9) noexcept
d9));


#define REPEAT4(x)
reciprocal_table_item((x) + 0), reciprocal_table_item((x) + 1),
reciprocal_table_item((x) + 2), reciprocal_table_item((x) + 3)

#define REPEAT32(x)
REPEAT4((x) + 4 * 0), REPEAT4((x) + 4 * 1), REPEAT4((x) + 4 * 2), REPEAT4((x) + 4 * 3),
REPEAT4((x) + 4 * 4), REPEAT4((x) + 4 * 5), REPEAT4((x) + 4 * 6), REPEAT4((x) + 4 * 7)

#define REPEAT256()
REPEAT32(32 * 0), REPEAT32(32 * 1), REPEAT32(32 * 2), REPEAT32(32 * 3), REPEAT32(32 * 4),
REPEAT32(32 * 5), REPEAT32(32 * 6), REPEAT32(32 * 7)

/// Reciprocal lookup table.
constexpr uint16_t reciprocal_table[] = REPEAT256();

#undef REPEAT4
#undef REPEAT32
#undef REPEAT256
// namespace internal

/// Computes the reciprocal (2^128 - 1) / d - 2^64 for normalized d.
///
/// Based on Algorithm 2 from "Improved division by invariant integers".
inline uint64_t reciprocal_2by1(uint64_t d) noexcept

auto d9 = uint8_t(d >> 55);
auto v0 = uint64_tinternal::reciprocal_table[d9];

auto d40 = (d >> 24) + 1;
auto v1 = (v0 << 11) - (v0 * v0 * d40 >> 40) - 1;

auto v2 = (v1 << 13) + (v1 * (0x1000000000000000 - v1 * d40) >> 47);

auto d0 = d % 2;
auto d63 = d / 2 + d0; // ceil(d/2)
auto nd0 = uint64_t(-int64_t(d0));
auto e = ((v2 / 2) & nd0) - v2 * d63;
auto mh = umul(v2, e).hi;
auto v3 = (v2 << 31) + (mh >> 1);

// OPT: The compiler tries a bit too much with 128 + 64 addition and ends up using subtraction.
// Compare with __int128.
auto mf = umul(v3, d);
auto m = fast_add(mf, d);
auto v3a = m.hi + d;

auto v4 = v3 - v3a;

return v4;


inline uint64_t reciprocal_3by2(uint128 d) noexcept

auto v = reciprocal_2by1(d.hi);
auto p = d.hi * v;
p += d.lo;
if (p < d.lo)

--v;
if (p >= d.hi)

--v;
p -= d.hi;

p -= d.hi;


auto t = umul(v, d.lo);

p += t.hi;
if (p < t.hi)

--v;
if (uint128p, t.lo >= d)
--v;

return v;


inline div_result<uint64_t> udivrem_2by1(uint128 u, uint64_t d, uint64_t v) noexcept

auto q = umul(v, u.hi);
q = fast_add(q, u);

++q.hi;

auto r = u.lo - q.hi * d;

if (r > q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem_3by2(
uint64_t u2, uint64_t u1, uint64_t u0, uint128 d, uint64_t v) noexcept

auto q = umul(v, u2);
q = fast_add(q, u2, u1);

auto r1 = u1 - q.hi * d.hi;

auto t = umul(d.lo, q.hi);

auto r = uint128r1, u0 - t - d;
r1 = r.hi;

++q.hi;

if (r1 >= q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem(uint128 x, uint128 y) noexcept
(x.hi << lsh);
auto xn_lo = x.lo << lsh;

auto v = reciprocal_3by2(yn_hi, yn_lo);
auto res = udivrem_3by2(xn_ex, xn_hi, xn_lo, yn_hi, yn_lo, v);

return res.quot, res.rem >> lsh;


inline div_result<uint128> sdivrem(uint128 x, uint128 y) noexcept

constexpr auto sign_mask = uint1281 << 127;
const auto x_is_neg = (x & sign_mask) != 0;
const auto y_is_neg = (y & sign_mask) != 0;

const auto x_abs = x_is_neg ? -x : x;
const auto y_abs = y_is_neg ? -y : y;

const auto q_is_neg = x_is_neg ^ y_is_neg;

const auto res = udivrem(x_abs, y_abs);

return q_is_neg ? -res.quot : res.quot, x_is_neg ? -res.rem : res.rem;


inline uint128 operator/(uint128 x, uint128 y) noexcept

return udivrem(x, y).quot;


inline uint128 operator%(uint128 x, uint128 y) noexcept

return udivrem(x, y).rem;


inline uint128& operator/=(uint128& x, uint128 y) noexcept

return x = x / y;


inline uint128& operator%=(uint128& x, uint128 y) noexcept

return x = x % y;


/// @

// namespace intx


namespace std

template <unsigned N>
struct numeric_limits<intx::uint<N>>

using type = intx::uint<N>;

static constexpr bool is_specialized = true;
static constexpr bool is_integer = true;
static constexpr bool is_signed = false;
static constexpr bool is_exact = true;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr float_round_style round_style = round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = true;
static constexpr int digits = CHAR_BIT * sizeof(type);
static constexpr int digits10 = int(0.3010299956639812 * digits);
static constexpr int max_digits10 = 0;
static constexpr int radix = 2;
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool traps = std::numeric_limits<unsigned>::traps;
static constexpr bool tinyness_before = false;

static constexpr type min() noexcept return 0;
static constexpr type lowest() noexcept return min();
static constexpr type max() noexcept return ~type0;
static constexpr type epsilon() noexcept return 0;
static constexpr type round_error() noexcept return 0;
static constexpr type infinity() noexcept return 0;
static constexpr type quiet_NaN() noexcept return 0;
static constexpr type signaling_NaN() noexcept return 0;
static constexpr type denorm_min() noexcept return 0;
;
// namespace std

namespace intx

template <typename Int>
constexpr Int from_string(const char* s)

using namespace std::literals;

auto x = Int;
int num_digits = 0;

if (s[0] == '0' && s[1] == 'x')

s += 2;
while (auto d = *s++)

if (++num_digits > intsizeof(x) * 2)
throw std::overflow_error"Integer overflow";

x <<= 4;
if (d >= '0' && d <= '9')
d -= '0';
else if (d >= 'a' && d <= 'f')
d -= 'a' - 10;
else if (d >= 'A' && d <= 'F')
d -= 'A' - 10;
else
throw std::invalid_argument"Invalid literal character: "s + d;
x
return x;


while (auto d = *s++)

if (num_digits++ > std::numeric_limits<Int>::digits10)
throw std::overflow_error"Integer overflow";

x = constexpr_mul(x, Int10);
if (d >= '0' && d <= '9')
d -= '0';
else
throw std::invalid_argument"Invalid literal character: "s + d;
x += d;
if (x < d)
throw std::overflow_error"Integer overflow";

return x;


template <typename Int>
constexpr Int from_string(const std::string& s)

return from_string<Int>(s.c_str());


constexpr 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)


template <unsigned N>
inline std::string hex(uint<N> x)

return to_string(x, 16);

// namespace intx
```









share|improve this question







New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




This implements 128-bit unsigned integer using C++14.
It works on MSVC and 32-bit architectures being complementary to the unsigned __int128 type provided by GCC and clang on 64-bit architectures.



// intx: extended precision integer library.
// Copyright 2019 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.

#pragma once

#include <algorithm>
#include <climits>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace intx

template <unsigned N>
struct uint;

/// The 128-bit unsigned integer.
///
/// This type is defined as a specialization of uint<> to easier integration with full intx package,
/// however, uint128 may be used independently.
template <>
struct uint<128>

uint64_t lo = 0;
uint64_t hi = 0;

constexpr uint() noexcept = default;

constexpr uint(uint64_t high, uint64_t low) noexcept : lolow, hihigh

template <typename T,
typename = typename std::enable_if_t<std::is_convertible<T, uint64_t>::value>>
constexpr uint(T x) noexcept : lo(static_cast<uint64_t>(x)) // NOLINT


#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
constexpr uint(unsigned __int128 x) noexcept // NOLINT
: louint64_t(x), hiuint64_t(x >> 64)


constexpr explicit operator unsigned __int128() const noexcept
lo;

#pragma GCC diagnostic pop
#endif

constexpr explicit operator bool() const noexcept lo;

/// Explicit converting operator for all builtin integral types.
template <typename Int, typename = typename std::enable_if<std::is_integral<Int>::value>::type>
constexpr explicit operator Int() const noexcept

return static_cast<Int>(lo);

;

using uint128 = uint<128>;


/// Linear arithmetic operators.
/// @

constexpr uint128 operator+(uint128 x, uint128 y) noexcept

const auto lo = x.lo + y.lo;
const auto carry = x.lo > lo;
const auto hi = x.hi + y.hi + carry;
return hi, lo;


constexpr uint128 operator+(uint128 x) noexcept

return x;


constexpr uint128 operator-(uint128 x, uint128 y) noexcept

const auto lo = x.lo - y.lo;
const auto borrow = x.lo < lo;
const auto hi = x.hi - y.hi - borrow;
return hi, lo;


constexpr uint128 operator-(uint128 x) noexcept

// Implementing as subtraction is better than ~x + 1.
// Clang9: Perfect.
// GCC8: Does something weird.
return 0 - x;


inline uint128& operator++(uint128& x) noexcept

return x = x + 1;


inline uint128& operator--(uint128& x) noexcept

return x = x - 1;


inline uint128 operator++(uint128& x, int) noexcept

auto ret = x;
++x;
return ret;


inline uint128 operator--(uint128& x, int) noexcept

auto ret = x;
--x;
return ret;


/// Optimized addition.
///
/// This keeps the multiprecision addition until CodeGen so the pattern is not
/// broken during other optimizations.
constexpr uint128 fast_add(uint128 x, uint128 y) noexcept

#ifdef __SIZEOF_INT128__xxx
return (unsigned __int128)x + (unsigned __int128)y;
#else
// Fallback to regular addition.
return x + y;
#endif


/// @


/// Comparison operators.
///
/// In all implementations bitwise operators are used instead of logical ones
/// to avoid branching.
///
/// @

constexpr bool operator==(uint128 x, uint128 y) noexcept

// Clang7: generates perfect xor based code,
// much better than __int128 where it uses vector instructions.
// GCC8: generates a bit worse cmp based code
// although it generates the xor based one for __int128.
return (x.lo == y.lo) & (x.hi == y.hi);


constexpr bool operator!=(uint128 x, uint128 y) noexcept
(x.hi != y.hi);


constexpr bool operator<(uint128 x, uint128 y) noexcept

// OPT: This should be implemented by checking the borrow of x - y,
// but compilers (GCC8, Clang7)
// have problem with properly optimizing subtraction.
return (x.hi < y.hi)

constexpr bool operator<=(uint128 x, uint128 y) noexcept
((x.hi == y.hi) & (x.lo <= y.lo));


constexpr bool operator>(uint128 x, uint128 y) noexcept

return !(x <= y);


constexpr bool operator>=(uint128 x, uint128 y) noexcept

return !(x < y);


/// @


/// Bitwise operators.
/// @(uint128 x, uint128 y) noexcept

// Clang7: perfect.
// GCC8: stupidly uses a vector instruction in all bitwise operators.
return y.lo;


constexpr uint128 operator&(uint128 x, uint128 y) noexcept

return x.hi & y.hi, x.lo & y.lo;


constexpr uint128 operator^(uint128 x, uint128 y) noexcept

return x.hi ^ y.hi, x.lo ^ y.lo;


constexpr uint128 operator<<(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 right shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.lo >> 1) >> (63 - shift)), x.lo << shift :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint128x.lo << (shift - 64), 0 : 0;


constexpr uint128 operator<<(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x << unsigned(shift);
return 0;


constexpr uint128 operator>>(uint128 x, unsigned shift) noexcept

return (shift < 64) ?
// Find the part moved from lo to hi.
// For shift == 0 left shift by (64 - shift) is invalid so
// split it into 2 shifts by 1 and (63 - shift).
uint128 ((x.hi << 1) << (63 - shift)) :

// Guarantee "defined" behavior for shifts larger than 128.
(shift < 128) ? uint1280, x.hi >> (shift - 64) : 0;


constexpr uint128 operator>>(uint128 x, uint128 shift) noexcept

if (shift < 128)
return x >> unsigned(shift);
return 0;



/// @


/// Multiplication
/// @

/// Portable full unsigned multiplication 64 x 64 -> 128.
constexpr uint128 constexpr_umul(uint64_t x, uint64_t y) noexcept

uint64_t xl = x & 0xffffffff;
uint64_t xh = x >> 32;
uint64_t yl = y & 0xffffffff;
uint64_t yh = y >> 32;

uint64_t t0 = xl * yl;
uint64_t t1 = xh * yl;
uint64_t t2 = xl * yh;
uint64_t t3 = xh * yh;

uint64_t u1 = t1 + (t0 >> 32);
uint64_t u2 = t2 + (u1 & 0xffffffff);

uint64_t lo = (u2 << 32)

/// Full unsigned multiplication 64 x 64 -> 128.
inline uint128 umul(uint64_t x, uint64_t y) noexcept

#if defined(__SIZEOF_INT128__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
const auto p = static_cast<unsigned __int128>(x) * y;
return uint64_t(p >> 64), uint64_t(p);
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
unsigned __int64 hi;
const auto lo = _umul128(x, y, &hi);
return hi, lo;
#else
return constexpr_umul(x, y);
#endif


inline uint128 operator*(uint128 x, uint128 y) noexcept

auto p = umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


constexpr uint128 constexpr_mul(uint128 x, uint128 y) noexcept

auto p = constexpr_umul(x.lo, y.lo);
p.hi += (x.lo * y.hi) + (x.hi * y.lo);
return p.hi, p.lo;


/// @


/// Assignment operators.
/// @

constexpr uint128& operator+=(uint128& x, uint128 y) noexcept

return x = x + y;


constexpr uint128& operator-=(uint128& x, uint128 y) noexcept

return x = x - y;


inline uint128& operator*=(uint128& x, uint128 y) noexcept

return x = x * y;


constexpr uint128& operator


inline unsigned clz(uint32_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse(&most_significant_bit, x);
return 31 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clz(x));
#endif


inline unsigned clz(uint64_t x) noexcept

#ifdef _MSC_VER
unsigned long most_significant_bit;
_BitScanReverse64(&most_significant_bit, x);
return 63 ^ (unsigned)most_significant_bit;
#else
return unsigned(__builtin_clzll(x));
#endif


inline unsigned clz(uint128 x) noexcept

// In this order `h == 0` we get less instructions than in case of `h != 0`.
return x.hi == 0 ? clz(x.lo)


inline uint64_t bswap(uint64_t x) noexcept

#ifdef _MSC_VER
return _byteswap_uint64(x);
#else
return __builtin_bswap64(x);
#endif


inline uint128 bswap(uint128 x) noexcept

return bswap(x.lo), bswap(x.hi);



/// Division.
/// @

template <typename T>
struct div_result

T quot;
T rem;
;

namespace internal

constexpr uint16_t reciprocal_table_item(uint8_t d9) noexcept
d9));


#define REPEAT4(x)
reciprocal_table_item((x) + 0), reciprocal_table_item((x) + 1),
reciprocal_table_item((x) + 2), reciprocal_table_item((x) + 3)

#define REPEAT32(x)
REPEAT4((x) + 4 * 0), REPEAT4((x) + 4 * 1), REPEAT4((x) + 4 * 2), REPEAT4((x) + 4 * 3),
REPEAT4((x) + 4 * 4), REPEAT4((x) + 4 * 5), REPEAT4((x) + 4 * 6), REPEAT4((x) + 4 * 7)

#define REPEAT256()
REPEAT32(32 * 0), REPEAT32(32 * 1), REPEAT32(32 * 2), REPEAT32(32 * 3), REPEAT32(32 * 4),
REPEAT32(32 * 5), REPEAT32(32 * 6), REPEAT32(32 * 7)

/// Reciprocal lookup table.
constexpr uint16_t reciprocal_table[] = REPEAT256();

#undef REPEAT4
#undef REPEAT32
#undef REPEAT256
// namespace internal

/// Computes the reciprocal (2^128 - 1) / d - 2^64 for normalized d.
///
/// Based on Algorithm 2 from "Improved division by invariant integers".
inline uint64_t reciprocal_2by1(uint64_t d) noexcept

auto d9 = uint8_t(d >> 55);
auto v0 = uint64_tinternal::reciprocal_table[d9];

auto d40 = (d >> 24) + 1;
auto v1 = (v0 << 11) - (v0 * v0 * d40 >> 40) - 1;

auto v2 = (v1 << 13) + (v1 * (0x1000000000000000 - v1 * d40) >> 47);

auto d0 = d % 2;
auto d63 = d / 2 + d0; // ceil(d/2)
auto nd0 = uint64_t(-int64_t(d0));
auto e = ((v2 / 2) & nd0) - v2 * d63;
auto mh = umul(v2, e).hi;
auto v3 = (v2 << 31) + (mh >> 1);

// OPT: The compiler tries a bit too much with 128 + 64 addition and ends up using subtraction.
// Compare with __int128.
auto mf = umul(v3, d);
auto m = fast_add(mf, d);
auto v3a = m.hi + d;

auto v4 = v3 - v3a;

return v4;


inline uint64_t reciprocal_3by2(uint128 d) noexcept

auto v = reciprocal_2by1(d.hi);
auto p = d.hi * v;
p += d.lo;
if (p < d.lo)

--v;
if (p >= d.hi)

--v;
p -= d.hi;

p -= d.hi;


auto t = umul(v, d.lo);

p += t.hi;
if (p < t.hi)

--v;
if (uint128p, t.lo >= d)
--v;

return v;


inline div_result<uint64_t> udivrem_2by1(uint128 u, uint64_t d, uint64_t v) noexcept

auto q = umul(v, u.hi);
q = fast_add(q, u);

++q.hi;

auto r = u.lo - q.hi * d;

if (r > q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem_3by2(
uint64_t u2, uint64_t u1, uint64_t u0, uint128 d, uint64_t v) noexcept

auto q = umul(v, u2);
q = fast_add(q, u2, u1);

auto r1 = u1 - q.hi * d.hi;

auto t = umul(d.lo, q.hi);

auto r = uint128r1, u0 - t - d;
r1 = r.hi;

++q.hi;

if (r1 >= q.lo)

--q.hi;
r += d;


if (r >= d)

++q.hi;
r -= d;


return q.hi, r;


inline div_result<uint128> udivrem(uint128 x, uint128 y) noexcept
(x.hi << lsh);
auto xn_lo = x.lo << lsh;

auto v = reciprocal_3by2(yn_hi, yn_lo);
auto res = udivrem_3by2(xn_ex, xn_hi, xn_lo, yn_hi, yn_lo, v);

return res.quot, res.rem >> lsh;


inline div_result<uint128> sdivrem(uint128 x, uint128 y) noexcept

constexpr auto sign_mask = uint1281 << 127;
const auto x_is_neg = (x & sign_mask) != 0;
const auto y_is_neg = (y & sign_mask) != 0;

const auto x_abs = x_is_neg ? -x : x;
const auto y_abs = y_is_neg ? -y : y;

const auto q_is_neg = x_is_neg ^ y_is_neg;

const auto res = udivrem(x_abs, y_abs);

return q_is_neg ? -res.quot : res.quot, x_is_neg ? -res.rem : res.rem;


inline uint128 operator/(uint128 x, uint128 y) noexcept

return udivrem(x, y).quot;


inline uint128 operator%(uint128 x, uint128 y) noexcept

return udivrem(x, y).rem;


inline uint128& operator/=(uint128& x, uint128 y) noexcept

return x = x / y;


inline uint128& operator%=(uint128& x, uint128 y) noexcept

return x = x % y;


/// @

// namespace intx


namespace std

template <unsigned N>
struct numeric_limits<intx::uint<N>>

using type = intx::uint<N>;

static constexpr bool is_specialized = true;
static constexpr bool is_integer = true;
static constexpr bool is_signed = false;
static constexpr bool is_exact = true;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr float_round_style round_style = round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = true;
static constexpr int digits = CHAR_BIT * sizeof(type);
static constexpr int digits10 = int(0.3010299956639812 * digits);
static constexpr int max_digits10 = 0;
static constexpr int radix = 2;
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool traps = std::numeric_limits<unsigned>::traps;
static constexpr bool tinyness_before = false;

static constexpr type min() noexcept return 0;
static constexpr type lowest() noexcept return min();
static constexpr type max() noexcept return ~type0;
static constexpr type epsilon() noexcept return 0;
static constexpr type round_error() noexcept return 0;
static constexpr type infinity() noexcept return 0;
static constexpr type quiet_NaN() noexcept return 0;
static constexpr type signaling_NaN() noexcept return 0;
static constexpr type denorm_min() noexcept return 0;
;
// namespace std

namespace intx

template <typename Int>
constexpr Int from_string(const char* s)

using namespace std::literals;

auto x = Int;
int num_digits = 0;

if (s[0] == '0' && s[1] == 'x')

s += 2;
while (auto d = *s++)

if (++num_digits > intsizeof(x) * 2)
throw std::overflow_error"Integer overflow";

x <<= 4;
if (d >= '0' && d <= '9')
d -= '0';
else if (d >= 'a' && d <= 'f')
d -= 'a' - 10;
else if (d >= 'A' && d <= 'F')
d -= 'A' - 10;
else
throw std::invalid_argument"Invalid literal character: "s + d;
x
return x;


while (auto d = *s++)

if (num_digits++ > std::numeric_limits<Int>::digits10)
throw std::overflow_error"Integer overflow";

x = constexpr_mul(x, Int10);
if (d >= '0' && d <= '9')
d -= '0';
else
throw std::invalid_argument"Invalid literal character: "s + d;
x += d;
if (x < d)
throw std::overflow_error"Integer overflow";

return x;


template <typename Int>
constexpr Int from_string(const std::string& s)

return from_string<Int>(s.c_str());


constexpr 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)


template <unsigned N>
inline std::string hex(uint<N> x)

return to_string(x, 16);

// namespace intx
```






c++ c++14 integer






share|improve this question







New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question







New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question






New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









Paweł BylicaPaweł Bylica

1362




1362




New contributor



Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Paweł Bylica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • $begingroup$
    When you write "complementary to the unsigned __int128 type" do you mean that it's a drop-in replacement? (so one could write #define u128 unsigned __int128 or #define u128 intx::uint<128> depending on whether there's a native 128-bit integer)
    $endgroup$
    – Toby Speight
    7 hours ago

















  • $begingroup$
    When you write "complementary to the unsigned __int128 type" do you mean that it's a drop-in replacement? (so one could write #define u128 unsigned __int128 or #define u128 intx::uint<128> depending on whether there's a native 128-bit integer)
    $endgroup$
    – Toby Speight
    7 hours ago
















$begingroup$
When you write "complementary to the unsigned __int128 type" do you mean that it's a drop-in replacement? (so one could write #define u128 unsigned __int128 or #define u128 intx::uint<128> depending on whether there's a native 128-bit integer)
$endgroup$
– Toby Speight
7 hours ago





$begingroup$
When you write "complementary to the unsigned __int128 type" do you mean that it's a drop-in replacement? (so one could write #define u128 unsigned __int128 or #define u128 intx::uint<128> depending on whether there's a native 128-bit integer)
$endgroup$
– Toby Speight
7 hours ago











1 Answer
1






active

oldest

votes


















9












$begingroup$

std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use one of std::uint_fast64_t or std::uint_least64_t instead, for better portability.



I'd expect constructors from other intx::uint<> instantiations (obviously, the narrowing conversions should be explicit).



The ++ and -- operators could be implemented much more efficiently by using the members rather than converting and adding/subtracting. Unary - might also be better implemented element-wise.



It's good to see that you've included a specialization of std::numeric_limits for this type. A minor nitpick: I think that digits10 needs to round up rather than down. It's unfortunate that we're not allowed to specialize std::is_integral too.



A few problems in from_string():



  • Style (minor): writing sizeof(x) instead of simply sizeof x makes it look like x is a type name.

  • Actually, there's a more serious error in that line, in assuming that char is 8 bits (2 hex digits), rather than using CHAR_BIT - I'd write sizeof x * CHAR_BIT / 4 there for full portability.

  • Alternatively, allow redundant leading zeros by simply checking clz(x) >= 4 before shifting.

  • Also, why are octal inputs to from_string() treated as decimal? That's surprising to me.

  • It might be useful to have (private) multiply/divide routines for the small multiplicands in from_string() and to_string().

  • Perhaps we could skip over any leading whitespace and/or +, like the standard conversion functions (std::stoi(), std::from_chars(), etc) do?

These string conversions look like they could easily be adapted to become streaming operators << and >>.



Finally, it's a great shame that you didn't include the unit tests in the review; that would have greatly aided reviewers (especially when making suggestions for improvement).






share|improve this answer











$endgroup$













    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Paweł Bylica is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220809%2f128-bit-unsigned-integer%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9












    $begingroup$

    std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use one of std::uint_fast64_t or std::uint_least64_t instead, for better portability.



    I'd expect constructors from other intx::uint<> instantiations (obviously, the narrowing conversions should be explicit).



    The ++ and -- operators could be implemented much more efficiently by using the members rather than converting and adding/subtracting. Unary - might also be better implemented element-wise.



    It's good to see that you've included a specialization of std::numeric_limits for this type. A minor nitpick: I think that digits10 needs to round up rather than down. It's unfortunate that we're not allowed to specialize std::is_integral too.



    A few problems in from_string():



    • Style (minor): writing sizeof(x) instead of simply sizeof x makes it look like x is a type name.

    • Actually, there's a more serious error in that line, in assuming that char is 8 bits (2 hex digits), rather than using CHAR_BIT - I'd write sizeof x * CHAR_BIT / 4 there for full portability.

    • Alternatively, allow redundant leading zeros by simply checking clz(x) >= 4 before shifting.

    • Also, why are octal inputs to from_string() treated as decimal? That's surprising to me.

    • It might be useful to have (private) multiply/divide routines for the small multiplicands in from_string() and to_string().

    • Perhaps we could skip over any leading whitespace and/or +, like the standard conversion functions (std::stoi(), std::from_chars(), etc) do?

    These string conversions look like they could easily be adapted to become streaming operators << and >>.



    Finally, it's a great shame that you didn't include the unit tests in the review; that would have greatly aided reviewers (especially when making suggestions for improvement).






    share|improve this answer











    $endgroup$

















      9












      $begingroup$

      std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use one of std::uint_fast64_t or std::uint_least64_t instead, for better portability.



      I'd expect constructors from other intx::uint<> instantiations (obviously, the narrowing conversions should be explicit).



      The ++ and -- operators could be implemented much more efficiently by using the members rather than converting and adding/subtracting. Unary - might also be better implemented element-wise.



      It's good to see that you've included a specialization of std::numeric_limits for this type. A minor nitpick: I think that digits10 needs to round up rather than down. It's unfortunate that we're not allowed to specialize std::is_integral too.



      A few problems in from_string():



      • Style (minor): writing sizeof(x) instead of simply sizeof x makes it look like x is a type name.

      • Actually, there's a more serious error in that line, in assuming that char is 8 bits (2 hex digits), rather than using CHAR_BIT - I'd write sizeof x * CHAR_BIT / 4 there for full portability.

      • Alternatively, allow redundant leading zeros by simply checking clz(x) >= 4 before shifting.

      • Also, why are octal inputs to from_string() treated as decimal? That's surprising to me.

      • It might be useful to have (private) multiply/divide routines for the small multiplicands in from_string() and to_string().

      • Perhaps we could skip over any leading whitespace and/or +, like the standard conversion functions (std::stoi(), std::from_chars(), etc) do?

      These string conversions look like they could easily be adapted to become streaming operators << and >>.



      Finally, it's a great shame that you didn't include the unit tests in the review; that would have greatly aided reviewers (especially when making suggestions for improvement).






      share|improve this answer











      $endgroup$















        9












        9








        9





        $begingroup$

        std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use one of std::uint_fast64_t or std::uint_least64_t instead, for better portability.



        I'd expect constructors from other intx::uint<> instantiations (obviously, the narrowing conversions should be explicit).



        The ++ and -- operators could be implemented much more efficiently by using the members rather than converting and adding/subtracting. Unary - might also be better implemented element-wise.



        It's good to see that you've included a specialization of std::numeric_limits for this type. A minor nitpick: I think that digits10 needs to round up rather than down. It's unfortunate that we're not allowed to specialize std::is_integral too.



        A few problems in from_string():



        • Style (minor): writing sizeof(x) instead of simply sizeof x makes it look like x is a type name.

        • Actually, there's a more serious error in that line, in assuming that char is 8 bits (2 hex digits), rather than using CHAR_BIT - I'd write sizeof x * CHAR_BIT / 4 there for full portability.

        • Alternatively, allow redundant leading zeros by simply checking clz(x) >= 4 before shifting.

        • Also, why are octal inputs to from_string() treated as decimal? That's surprising to me.

        • It might be useful to have (private) multiply/divide routines for the small multiplicands in from_string() and to_string().

        • Perhaps we could skip over any leading whitespace and/or +, like the standard conversion functions (std::stoi(), std::from_chars(), etc) do?

        These string conversions look like they could easily be adapted to become streaming operators << and >>.



        Finally, it's a great shame that you didn't include the unit tests in the review; that would have greatly aided reviewers (especially when making suggestions for improvement).






        share|improve this answer











        $endgroup$



        std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use one of std::uint_fast64_t or std::uint_least64_t instead, for better portability.



        I'd expect constructors from other intx::uint<> instantiations (obviously, the narrowing conversions should be explicit).



        The ++ and -- operators could be implemented much more efficiently by using the members rather than converting and adding/subtracting. Unary - might also be better implemented element-wise.



        It's good to see that you've included a specialization of std::numeric_limits for this type. A minor nitpick: I think that digits10 needs to round up rather than down. It's unfortunate that we're not allowed to specialize std::is_integral too.



        A few problems in from_string():



        • Style (minor): writing sizeof(x) instead of simply sizeof x makes it look like x is a type name.

        • Actually, there's a more serious error in that line, in assuming that char is 8 bits (2 hex digits), rather than using CHAR_BIT - I'd write sizeof x * CHAR_BIT / 4 there for full portability.

        • Alternatively, allow redundant leading zeros by simply checking clz(x) >= 4 before shifting.

        • Also, why are octal inputs to from_string() treated as decimal? That's surprising to me.

        • It might be useful to have (private) multiply/divide routines for the small multiplicands in from_string() and to_string().

        • Perhaps we could skip over any leading whitespace and/or +, like the standard conversion functions (std::stoi(), std::from_chars(), etc) do?

        These string conversions look like they could easily be adapted to become streaming operators << and >>.



        Finally, it's a great shame that you didn't include the unit tests in the review; that would have greatly aided reviewers (especially when making suggestions for improvement).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 5 hours ago

























        answered 7 hours ago









        Toby SpeightToby Speight

        28.2k742120




        28.2k742120




















            Paweł Bylica is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Paweł Bylica is a new contributor. Be nice, and check out our Code of Conduct.












            Paweł Bylica is a new contributor. Be nice, and check out our Code of Conduct.











            Paweł Bylica is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220809%2f128-bit-unsigned-integer%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Log på Navigationsmenu

            Creating second map without labels using QGIS?How to lock map labels for inset map in Print Composer?How to Force the Showing of Labels of a Vector File in QGISQGIS Valmiera, Labels only show for part of polygonsRemoving duplicate point labels in QGISLabeling every feature using QGIS?Show labels for point features outside map canvasAbbreviate Road Labels in QGIS only when requiredExporting map from composer in QGIS - text labels have moved in output?How to make sure labels in qgis turn up in layout map?Writing label expression with ArcMap and If then Statement?

            Nuuk Indholdsfortegnelse Etyomologi | Historie | Geografi | Transport og infrastruktur | Politik og administration | Uddannelsesinstitutioner | Kultur | Venskabsbyer | Noter | Eksterne henvisninger | Se også | Navigationsmenuwww.sermersooq.gl64°10′N 51°45′V / 64.167°N 51.750°V / 64.167; -51.75064°10′N 51°45′V / 64.167°N 51.750°V / 64.167; -51.750DMI - KlimanormalerSalmonsen, s. 850Grønlands Naturinstitut undersøger rensdyr i Akia og Maniitsoq foråret 2008Grønlands NaturinstitutNy vej til Qinngorput indviet i dagAntallet af biler i Nuuk må begrænsesNy taxacentral mødt med demonstrationKøreplan. Rute 1, 2 og 3SnescootersporNuukNord er for storSkoler i Kommuneqarfik SermersooqAtuarfik Samuel KleinschmidtKangillinguit AtuarfiatNuussuup AtuarfiaNuuk Internationale FriskoleIlinniarfissuaq, Grønlands SeminariumLedelseÅrsberetning for 2008Kunst og arkitekturÅrsberetning for 2008Julie om naturenNuuk KunstmuseumSilamiutGrønlands Nationalmuseum og ArkivStatistisk ÅrbogGrønlands LandsbibliotekStore koncerter på stribeVandhund nummer 1.000.000Kommuneqarfik Sermersooq – MalikForsidenVenskabsbyerLyngby-Taarbæk i GrønlandArctic Business NetworkWinter Cities 2008 i NuukDagligt opdaterede satellitbilleder fra NuukområdetKommuneqarfik Sermersooqs hjemmesideTurist i NuukGrønlands Statistiks databankGrønlands Hjemmestyres valgresultaterrrWorldCat124325457671310-5