20#ifndef MORTON_MORTON_HPP
21#define MORTON_MORTON_HPP
36#if (defined(__x86_64__) || defined(_M_X64)) && (defined(__GNUC__) || defined(__clang__)) && \
37 !defined(__CUDACC__) && !defined(__HIPCC__)
48#ifndef MORTON_ENABLE_BMI2
49#define MORTON_ENABLE_BMI2 1
53#if defined(__BMI2__) && MORTON_ENABLE_BMI2
54#define MORTON_BMI2_INTRINSICS 1
56#define MORTON_BMI2_INTRINSICS 0
66#ifndef MORTON_ENABLE_RUNTIME_DISPATCH
67#define MORTON_ENABLE_RUNTIME_DISPATCH 0
69#if MORTON_X86 && MORTON_ENABLE_RUNTIME_DISPATCH && !MORTON_BMI2_INTRINSICS
70#define MORTON_X86_RUNTIME_DISPATCH 1
72#define MORTON_X86_RUNTIME_DISPATCH 0
75#if MORTON_X86 || MORTON_BMI2_INTRINSICS
86#ifndef MORTON_MAX_BITS
87#define MORTON_MAX_BITS 256
105template <
unsigned NBits>
107#if defined(MORTON_HAS_INT128)
113 (NBits <= 8), std::uint8_t,
115 (NBits <= 16), std::uint16_t,
116 std::conditional_t<(NBits <= 32), std::uint32_t,
118 std::conditional_t<(NBits <= 64), std::uint64_t, uint128_t>
125template <
unsigned NBits>
131#if defined(__cpp_if_consteval)
137#elif defined(__GNUC__) || defined(__clang__)
138 return __builtin_is_constant_evaluated();
147 for (
unsigned i = 0; i < n; ++i)
154template <
unsigned Dim,
unsigned Bits,
typename Code>
157 for (
unsigned i = 0; i < Bits; ++i)
158 m |= (Code(1) << (i * Dim + d));
164template <
unsigned Dim,
unsigned Bits,
typename Code,
typename Coord>
167 for (
unsigned i = 0; i < Bits; ++i)
168 r |= Code(Code(x >> i) & Code(1)) << (i * Dim + d);
173template <
unsigned Dim,
unsigned Bits,
typename Code,
typename Coord>
176 for (
unsigned i = 0; i < Bits; ++i)
177 r |= Coord(Coord(c >> (i * Dim + d)) & Coord(1)) << i;
185inline bool cpu_has_bmi2() {
186 static const bool v = __builtin_cpu_supports(
"bmi2") != 0;
189inline bool cpu_has_avx2() {
190 static const bool v = __builtin_cpu_supports(
"avx2") != 0;
193inline bool cpu_has_avx512f() {
194 static const bool v = __builtin_cpu_supports(
"avx512f") != 0;
199#if MORTON_X86_RUNTIME_DISPATCH
203__attribute__((target(
"bmi2"))) inline std::uint64_t pdep_u64_hw(std::uint64_t v, std::uint64_t m) {
204 return _pdep_u64(v, m);
206__attribute__((target(
"bmi2"))) inline std::uint64_t pext_u64_hw(std::uint64_t v, std::uint64_t m) {
207 return _pext_u64(v, m);
216template <
unsigned Dim,
unsigned Bits>
218 static_assert(Dim >= 1,
"Dim must be >= 1");
219 static_assert(Bits >= 1,
"Bits must be >= 1");
221 "Dim * Bits exceeds MORTON_MAX_BITS (raise it if you need wider codes)");
232 static constexpr unsigned code_type_bits =
sizeof(
code_type) * CHAR_BIT;
233 static constexpr unsigned coord_type_bits =
sizeof(
coord_type) * CHAR_BIT;
246 static constexpr unsigned octants = unsigned(1) << Dim;
260 template <
typename... Cs,
typename = std::enable_if_t<
sizeof...(Cs) == Dim>>
264 for (
unsigned d = 0; d < Dim; ++d)
272 for (
unsigned d = 0; d < Dim; ++d)
286 std::array<coord_type, Dim> out{};
287 for (
unsigned d = 0; d < Dim; ++d)
294 code_ = (code_ & keep_mask(d)) |
deposit(value, d);
307 code_ = (s & M) | (code_ & M_complement(d));
314 code_ = (s & M) | (code_ & M_complement(d));
321 code_ = (s & M) | (code_ & M_complement(d));
328 code_ = (s & M) | (code_ & M_complement(d));
380 std::array<Morton, 2 * Dim> out{};
381 for (
unsigned d = 0; d < Dim; ++d) {
397 for (
unsigned d = 0; d < Dim; ++d) {
398 off[d] = int(t % 3) - 1;
406 for (
unsigned d = 0; d < Dim; ++d) {
434 return unsigned((code_ >> (level * Dim)) & (
octants - 1));
440 unsigned shift = (level - 1) * Dim;
469 return a.code_ == b.code_;
472 return a.code_ != b.code_;
475 return a.code_ < b.code_;
478 return a.code_ <= b.code_;
481 return a.code_ > b.code_;
484 return a.code_ >= b.code_;
493#if MORTON_BMI2_INTRINSICS && !defined(__CUDA_ARCH__)
496#elif MORTON_X86_RUNTIME_DISPATCH
507#if MORTON_BMI2_INTRINSICS && !defined(__CUDA_ARCH__)
510#elif MORTON_X86_RUNTIME_DISPATCH
520#if defined(__CUDA_ARCH__)
525 return axis_masks_[d];
533 MORTON_HD static constexpr code_type keep_mask(
unsigned d) {
return M_complement(d); }
536 MORTON_HD static constexpr std::array<code_type, Dim> make_masks() {
537 std::array<code_type, Dim> a{};
538 for (
unsigned d = 0; d < Dim; ++d)
542 static constexpr std::array<code_type, Dim> axis_masks_ = make_masks();
552#if defined(MORTON_HAS_INT128)
A Morton (Z-order) code interleaving Dim unsigned coordinates of Bits bits each.
Definition morton.hpp:217
friend MORTON_HD constexpr bool operator>=(Morton a, Morton b) noexcept
Definition morton.hpp:483
MORTON_HD constexpr Morton & operator++() noexcept
Definition morton.hpp:447
static MORTON_HD constexpr coord_type extract(code_type c, unsigned d)
Definition morton.hpp:505
friend MORTON_HD constexpr bool operator<=(Morton a, Morton b) noexcept
Definition morton.hpp:477
MORTON_HD constexpr Morton operator--(int) noexcept
Definition morton.hpp:460
MORTON_HD constexpr void inc(unsigned d)
Increment axis d by one. O(1).
Definition morton.hpp:318
MORTON_HD constexpr void sub_sat(unsigned d, coord_type k)
Subtract k from axis d, clamping at 0 instead of wrapping.
Definition morton.hpp:351
static constexpr code_type field_mask
Definition morton.hpp:237
MORTON_HD constexpr void dec(unsigned d)
Decrement axis d by one. O(1).
Definition morton.hpp:325
detail::uint_for_t< Bits > coord_type
Definition morton.hpp:229
static constexpr unsigned bits_per_axis
Definition morton.hpp:225
static MORTON_HD constexpr Morton encode(Cs... coords)
Encode Dim coordinates into a Morton code.
Definition morton.hpp:261
friend MORTON_HD constexpr bool operator!=(Morton a, Morton b) noexcept
Definition morton.hpp:471
MORTON_HD constexpr code_type code() const noexcept
Definition morton.hpp:279
MORTON_HD constexpr std::array< coord_type, Dim > decode() const
Decode all coordinates.
Definition morton.hpp:285
MORTON_HD constexpr void set(unsigned d, coord_type value)
Replace one axis' coordinate (other axes untouched). Wraps mod 2^Bits.
Definition morton.hpp:293
MORTON_HD constexpr Morton & operator--() noexcept
Definition morton.hpp:456
MORTON_HD constexpr Morton() noexcept
Definition morton.hpp:250
MORTON_HD constexpr void sub(unsigned d, coord_type k)
Subtract k from axis d (wraps). O(1), branchless.
Definition morton.hpp:311
friend MORTON_HD constexpr bool operator==(Morton a, Morton b) noexcept
Definition morton.hpp:468
detail::uint_for_t< Dim *Bits > code_type
Definition morton.hpp:228
static MORTON_HD constexpr code_type axis_mask(unsigned d)
Mask of bits belonging to axis d.
Definition morton.hpp:519
MORTON_HD constexpr void add(unsigned d, coord_type k)
Add k to axis d (wraps). O(1), branchless.
Definition morton.hpp:304
static MORTON_HD constexpr Morton from_code(code_type raw) noexcept
Wrap a raw, already-interleaved code value.
Definition morton.hpp:253
MORTON_HD constexpr std::array< Morton, 2 *Dim > face_neighbors() const
The 2*Dim von Neumann (face) neighbours, ordered {axis0-, axis0+, axis1-, axis1+, ....
Definition morton.hpp:379
MORTON_HD constexpr Morton operator++(int) noexcept
Definition morton.hpp:451
MORTON_HD constexpr coord_type get(unsigned d) const
Decode coordinate of a single axis.
Definition morton.hpp:282
MORTON_HD constexpr Morton neighbor(unsigned d, int dir) const
Morton code of the neighbour one step along ± axis d (wraps).
Definition morton.hpp:332
static MORTON_HD constexpr Morton encode(const std::array< coord_type, Dim > &c)
Encode from an array of coordinates.
Definition morton.hpp:270
MORTON_HD constexpr void add_sat(unsigned d, coord_type k)
Add k to axis d, clamping at coord_max instead of wrapping.
Definition morton.hpp:344
MORTON_HD constexpr bool try_add(unsigned d, coord_type k)
Add k to axis d only if it does not overflow past coord_max.
Definition morton.hpp:358
MORTON_HD constexpr bool try_sub(unsigned d, coord_type k)
Subtract k from axis d only if it does not underflow past 0.
Definition morton.hpp:367
MORTON_HD constexpr std::array< Morton, detail::pow3(Dim) - 1 > all_neighbors() const
The 3^Dim - 1 Moore neighbours (all cells differing by -1/0/+1 on each axis, excluding self),...
Definition morton.hpp:390
MORTON_HD constexpr Morton child(unsigned level, unsigned octant) const
The octant-th child origin of a cell that is an ancestor at level (i.e.
Definition morton.hpp:439
static MORTON_HD constexpr code_type deposit(coord_type x, unsigned d)
Definition morton.hpp:489
friend MORTON_HD constexpr bool operator>(Morton a, Morton b) noexcept
Definition morton.hpp:480
MORTON_HD constexpr unsigned child_index(unsigned level) const
Index (0 .
Definition morton.hpp:433
static constexpr coord_type coord_max
Definition morton.hpp:242
static constexpr unsigned octants
Definition morton.hpp:246
static constexpr unsigned code_bits
Definition morton.hpp:226
friend MORTON_HD constexpr bool operator<(Morton a, Morton b) noexcept
Definition morton.hpp:474
MORTON_HD constexpr Morton ancestor(unsigned level) const
Ancestor cell origin at the given level (clears the low level*Dim bits).
Definition morton.hpp:424
static constexpr unsigned dimensions
Definition morton.hpp:224
The MORTON_HD host/device function marker, shared by every core header.
#define MORTON_HD
Definition hd.hpp:27
#define MORTON_MAX_BITS
Definition morton.hpp:87
MORTON_HD constexpr Coord compact_sw(Code c, unsigned d)
Definition morton.hpp:174
MORTON_HD constexpr bool is_consteval() noexcept
Definition morton.hpp:130
MORTON_HD constexpr Code axis_mask(unsigned d)
Definition morton.hpp:155
typename uint_for< NBits >::type uint_for_t
Definition morton.hpp:126
MORTON_HD constexpr Code spread_sw(Coord x, unsigned d)
Definition morton.hpp:165
MORTON_HD constexpr unsigned pow3(unsigned n)
Definition morton.hpp:145
Morton< 3, 21 > Morton3D21
Definition morton.hpp:550
Morton< 3, 16 > Morton3D16
Definition morton.hpp:551
Morton< 2, 32 > Morton2D32
Definition morton.hpp:548
Morton< 3, 32 > Morton3D32
Definition morton.hpp:553
Morton< 2, 16 > Morton2D16
Definition morton.hpp:549
Morton< 2, 64 > Morton2D64
Definition morton.hpp:554
Definition morton.hpp:106
std::conditional_t<(NBits<=builtin_max), builtin, wide_uint<(NBits+63)/64 > > type
Definition morton.hpp:123
std::conditional_t<(NBits<=8), std::uint8_t, std::conditional_t<(NBits<=16), std::uint16_t, std::conditional_t<(NBits<=32), std::uint32_t, std::conditional_t<(NBits<=64), std::uint64_t, uint128_t > > > > builtin
Definition morton.hpp:112
static constexpr unsigned builtin_max
Definition morton.hpp:108
Fixed-width unsigned integer of.
Definition wide_uint.hpp:46
Minimal fixed-width unsigned integer backing Morton codes wider than 128 bits.
#define MORTON_HAS_INT128
Definition wide_uint.hpp:25