12#ifndef MORTON_WIDE_UINT_HPP
13#define MORTON_WIDE_UINT_HPP
28template <std::
size_t W>
30 static_assert(W >= 2,
"use a built-in integer for <= 64 bits");
31 std::array<std::uint64_t, W>
w{};
36 template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T> && (sizeof(T) <= 8)>>
37 MORTON_HD constexpr w
ide_u
int(T v) : w{} {
38 w[0] = std::u
int64_t(static_cast<std::make_
unsigned_t<T>>(v));
41#if defined(__SIZEOF_INT128__)
43 MORTON_HD constexpr w
ide_u
int(
unsigned __
int128 v) : w{} {
44 w[0] = std::u
int64_t(v);
46 w[1] = std::uint64_t(v >> 64);
51 template <std::
size_t W2,
typename = std::enable_if_t<W2 != W>>
53 for (std::size_t i = 0; i < W && i < W2; ++i)
57 MORTON_HD explicit constexpr operator std::uint64_t()
const {
return w[0]; }
58#if defined(__SIZEOF_INT128__)
59 MORTON_HD explicit constexpr operator unsigned __int128()
const {
60 unsigned __int128 r = w[0];
62 r |= (
unsigned __int128)(w[1]) << 64;
66 MORTON_HD explicit constexpr operator bool()
const {
67 for (std::size_t i = 0; i < W; ++i)
76 for (std::size_t i = 0; i < W; ++i)
81 for (std::size_t i = 0; i < W; ++i)
86 for (std::size_t i = 0; i < W; ++i)
91 for (std::size_t i = 0; i < W; ++i)
102 const unsigned ws = s / 64, bs = s % 64;
103 for (
int i =
int(W) - 1; i >= 0; --i) {
104 const int src = i - int(ws);
107 v = w[std::size_t(src)] << bs;
108 if (bs && src - 1 >= 0)
109 v |= w[std::size_t(src - 1)] >> (64 - bs);
111 r.
w[std::size_t(i)] = v;
117 const unsigned ws = s / 64, bs = s % 64;
118 for (std::size_t i = 0; i < W; ++i) {
119 const std::size_t src = i + ws;
123 if (bs && src + 1 < W)
124 v |= w[src + 1] << (64 - bs);
136 std::uint64_t carry = 0;
137 for (std::size_t i = 0; i < W; ++i) {
138 std::uint64_t s1 = a.
w[i] + b.
w[i];
139 std::uint64_t c1 = (s1 < a.
w[i]) ? 1u : 0u;
140 std::uint64_t s2 = s1 + carry;
141 std::uint64_t c2 = (s2 < s1) ? 1u : 0u;
149 std::uint64_t borrow = 0;
150 for (std::size_t i = 0; i < W; ++i) {
151 std::uint64_t d1 = a.
w[i] - b.
w[i];
152 std::uint64_t b1 = (a.
w[i] < b.
w[i]) ? 1u : 0u;
153 std::uint64_t d2 = d1 - borrow;
154 std::uint64_t b2 = (d1 < borrow) ? 1u : 0u;
165 for (std::size_t i = 0; i < W; ++i)
166 if (a.
w[i] != b.
w[i])
174 for (std::size_t i = W; i-- > 0;) {
175 if (a.
w[i] != b.
w[i])
176 return a.
w[i] < b.
w[i];
The MORTON_HD host/device function marker, shared by every core header.
#define MORTON_HD
Definition hd.hpp:27
Fixed-width unsigned integer of.
Definition wide_uint.hpp:29
friend MORTON_HD constexpr bool operator>=(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:186
friend MORTON_HD constexpr wide_uint operator+(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:134
MORTON_HD constexpr wide_uint & operator<<=(unsigned s)
Definition wide_uint.hpp:130
MORTON_HD constexpr wide_uint & operator&=(const wide_uint &o)
Definition wide_uint.hpp:80
friend MORTON_HD constexpr bool operator<(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:173
std::array< std::uint64_t, W > w
Words, little-endian: w[0] is least significant.
Definition wide_uint.hpp:31
friend MORTON_HD constexpr wide_uint operator-(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:147
friend MORTON_HD constexpr wide_uint operator|(wide_uint a, const wide_uint &b)
Definition wide_uint.hpp:96
MORTON_HD constexpr wide_uint & operator++()
Definition wide_uint.hpp:160
friend MORTON_HD constexpr bool operator!=(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:170
friend MORTON_HD constexpr bool operator>(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:180
friend MORTON_HD constexpr bool operator<=(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:183
MORTON_HD constexpr wide_uint(const wide_uint< W2 > &o)
Definition wide_uint.hpp:52
friend MORTON_HD constexpr bool operator==(const wide_uint &a, const wide_uint &b)
Definition wide_uint.hpp:164
MORTON_HD constexpr wide_uint & operator|=(const wide_uint &o)
Definition wide_uint.hpp:85
MORTON_HD constexpr wide_uint operator<<(unsigned s) const
Definition wide_uint.hpp:100
MORTON_HD constexpr wide_uint operator~() const
Definition wide_uint.hpp:74
MORTON_HD constexpr wide_uint & operator>>=(unsigned s)
Definition wide_uint.hpp:131
MORTON_HD constexpr wide_uint & operator^=(const wide_uint &o)
Definition wide_uint.hpp:90
friend MORTON_HD constexpr wide_uint operator&(wide_uint a, const wide_uint &b)
Definition wide_uint.hpp:95
MORTON_HD constexpr wide_uint operator>>(unsigned s) const
Definition wide_uint.hpp:115
MORTON_HD constexpr wide_uint()=default
MORTON_HD constexpr wide_uint & operator--()
Definition wide_uint.hpp:161
friend MORTON_HD constexpr wide_uint operator^(wide_uint a, const wide_uint &b)
Definition wide_uint.hpp:97