peclet-morton 1.0.2
Fast Morton (Z-order) codes with O(1) arithmetic
Loading...
Searching...
No Matches
morton.hpp
Go to the documentation of this file.
1
19
20#ifndef MORTON_MORTON_HPP
21#define MORTON_MORTON_HPP
22
23#include <array>
24#include <climits>
25#include <cstddef>
26#include <cstdint>
27#include <type_traits>
28
29#include "morton/wide_uint.hpp"
30
31// ---- x86 ISA feature plumbing ---------------------------------------------
32// MORTON_X86 : building for x86-64 with GCC/Clang for the host (not CUDA/HIP).
33// Gates the runtime CPU-feature queries and the AVX-512 batch kernels, which
34// use per-function `target` attributes so they need no global -mavx512f/-mbmi2
35// and never run unless the executing CPU actually supports the instructions.
36#if (defined(__x86_64__) || defined(_M_X64)) && (defined(__GNUC__) || defined(__clang__)) && \
37 !defined(__CUDACC__) && !defined(__HIPCC__)
38#define MORTON_X86 1
39#else
40#define MORTON_X86 0
41#endif
42
43// MORTON_ENABLE_BMI2 (default 1): 0 forbids the compile-time PDEP/PEXT intrinsics path even when
44// the compiler targets BMI2 (-mbmi2, -march=native, or a default target such as x86-64-v3), so
45// encode/decode use the software path. The CMake option MORTON_ENABLE_BMI2=OFF defines it to 0 on
46// the morton target; with 1 (or undefined) the intrinsics path is used whenever the compiler
47// provides BMI2, as before.
48#ifndef MORTON_ENABLE_BMI2
49#define MORTON_ENABLE_BMI2 1
50#endif
51// MORTON_BMI2_INTRINSICS (internal): compile the PDEP/PEXT intrinsics path. The single
52// selection condition for it; nothing else in morton decides on __BMI2__.
53#if defined(__BMI2__) && MORTON_ENABLE_BMI2
54#define MORTON_BMI2_INTRINSICS 1
55#else
56#define MORTON_BMI2_INTRINSICS 0
57#endif
58
59// MORTON_ENABLE_RUNTIME_DISPATCH (opt-in, default 0): build a single binary
60// *without* -mbmi2 that still uses PDEP/PEXT when the running CPU has BMI2 and
61// the software fallback otherwise -- removing the build-time portability/speed
62// trade-off (e.g. for distributable wheels). Left off by default so the plain
63// non-BMI2 build stays strictly software (and pdep/pext-free). It self-disables
64// when the compile-time intrinsic path is selected (MORTON_BMI2_INTRINSICS), so
65// with MORTON_ENABLE_BMI2=0 it also works on a compiler whose target has BMI2.
66#ifndef MORTON_ENABLE_RUNTIME_DISPATCH
67#define MORTON_ENABLE_RUNTIME_DISPATCH 0
68#endif
69#if MORTON_X86 && MORTON_ENABLE_RUNTIME_DISPATCH && !MORTON_BMI2_INTRINSICS
70#define MORTON_X86_RUNTIME_DISPATCH 1
71#else
72#define MORTON_X86_RUNTIME_DISPATCH 0
73#endif
74
75#if MORTON_X86 || MORTON_BMI2_INTRINSICS
76#include <immintrin.h>
77#endif
78
79// MORTON_HAS_INT128 and detail::uint128_t are defined by wide_uint.hpp (included above), which is
80// the lowest header that needs the built-in; the alias carries the `__extension__` marker that
81// keeps `-Wpedantic` consumers warning-free.
82
83// Maximum supported code width (Dim * Bits). Codes <= 64 use a built-in
84// integer, <= 128 use __uint128_t (where available), and wider codes use the
85// software wide_uint<W> backend.
86#ifndef MORTON_MAX_BITS
87#define MORTON_MAX_BITS 256
88#endif
89
90// Mark functions callable from device code. Expands to nothing for an ordinary
91// host C++ build, so the CPU library is completely unchanged.
92//
93// MORTON_HD (the host/device function marker) lives in its own header so that headers included
94// above — wide_uint.hpp in particular — can annotate their operators device-callable too. It is
95// already pulled in transitively via wide_uint.hpp; include it here as well for the
96// direct/standalone case. See morton/hd.hpp for the Kokkos-vs-raw-CUDA-vs-host resolution.
97#include "morton/hd.hpp"
98
99namespace morton {
100
101namespace detail {
102
103// Smallest unsigned type that holds at least NBits bits: a built-in where one
104// exists, otherwise a wide_uint of the right number of 64-bit words.
105template <unsigned NBits>
106struct uint_for {
107#if defined(MORTON_HAS_INT128)
108 static constexpr unsigned builtin_max = 128;
109#else
110 static constexpr unsigned builtin_max = 64;
111#endif
112 using builtin = std::conditional_t<
113 (NBits <= 8), std::uint8_t,
114 std::conditional_t<
115 (NBits <= 16), std::uint16_t,
116 std::conditional_t<(NBits <= 32), std::uint32_t,
117#if defined(MORTON_HAS_INT128)
118 std::conditional_t<(NBits <= 64), std::uint64_t, uint128_t>
119#else
120 std::uint64_t
121#endif
122 >>>;
123 using type = std::conditional_t<(NBits <= builtin_max), builtin, wide_uint<(NBits + 63) / 64>>;
124};
125template <unsigned NBits>
127
128// True during constant evaluation. Lets a constexpr function avoid the
129// (non-constexpr) BMI2 intrinsics when evaluated at compile time.
130MORTON_HD constexpr bool is_consteval() noexcept {
131#if defined(__cpp_if_consteval)
132 if consteval {
133 return true;
134 } else {
135 return false;
136 }
137#elif defined(__GNUC__) || defined(__clang__)
138 return __builtin_is_constant_evaluated();
139#else
140 return false;
141#endif
142}
143
144// 3^n, as a compile-time value (size of a Moore neighbourhood including self).
145MORTON_HD constexpr unsigned pow3(unsigned n) {
146 unsigned r = 1;
147 for (unsigned i = 0; i < n; ++i)
148 r *= 3;
149 return r;
150}
151
152// Build the mask selecting the bits that belong to axis `d` in a code with
153// `Dim` interleaved axes of `Bits` bits each: positions d, d+Dim, d+2*Dim, ...
154template <unsigned Dim, unsigned Bits, typename Code>
155MORTON_HD constexpr Code axis_mask(unsigned d) {
156 Code m = 0;
157 for (unsigned i = 0; i < Bits; ++i)
158 m |= (Code(1) << (i * Dim + d));
159 return m;
160}
161
162// Software deposit: spread the low `Bits` bits of x across positions
163// d, d+Dim, ... (the portable fallback for PDEP). Works for any code width.
164template <unsigned Dim, unsigned Bits, typename Code, typename Coord>
165MORTON_HD constexpr Code spread_sw(Coord x, unsigned d) {
166 Code r = 0;
167 for (unsigned i = 0; i < Bits; ++i)
168 r |= Code(Code(x >> i) & Code(1)) << (i * Dim + d);
169 return r;
170}
171
172// Software extract: inverse of spread_sw (the portable fallback for PEXT).
173template <unsigned Dim, unsigned Bits, typename Code, typename Coord>
174MORTON_HD constexpr Coord compact_sw(Code c, unsigned d) {
175 Coord r = 0;
176 for (unsigned i = 0; i < Bits; ++i)
177 r |= Coord(Coord(c >> (i * Dim + d)) & Coord(1)) << i;
178 return r;
179}
180
181#if MORTON_X86
182// Cached runtime CPU-feature queries. __builtin_cpu_supports returns a nonzero
183// bitfield when the feature is present, so compare against 0. The first call
184// triggers detection; results are memoised in function-local statics.
185inline bool cpu_has_bmi2() {
186 static const bool v = __builtin_cpu_supports("bmi2") != 0;
187 return v;
188}
189inline bool cpu_has_avx2() {
190 static const bool v = __builtin_cpu_supports("avx2") != 0;
191 return v;
192}
193inline bool cpu_has_avx512f() {
194 static const bool v = __builtin_cpu_supports("avx512f") != 0;
195 return v;
196}
197#endif
198
199#if MORTON_X86_RUNTIME_DISPATCH
200// Compiled with BMI2 codegen for just these two functions (via the `target`
201// attribute), so the rest of the binary stays portable; only ever called from
202// the !is_consteval() runtime path guarded by cpu_has_bmi2().
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);
205}
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);
208}
209#endif
210
211} // namespace detail
212
216template <unsigned Dim, unsigned Bits>
217class Morton {
218 static_assert(Dim >= 1, "Dim must be >= 1");
219 static_assert(Bits >= 1, "Bits must be >= 1");
220 static_assert(Dim * Bits <= MORTON_MAX_BITS,
221 "Dim * Bits exceeds MORTON_MAX_BITS (raise it if you need wider codes)");
222
223 public:
224 static constexpr unsigned dimensions = Dim;
225 static constexpr unsigned bits_per_axis = Bits;
226 static constexpr unsigned code_bits = Dim * Bits;
227
230
231 private:
232 static constexpr unsigned code_type_bits = sizeof(code_type) * CHAR_BIT;
233 static constexpr unsigned coord_type_bits = sizeof(coord_type) * CHAR_BIT;
234
235 public:
237 static constexpr code_type field_mask = (code_bits >= code_type_bits)
238 ? code_type(~code_type(0))
239 : code_type((code_type(1) << code_bits) - 1);
240
242 static constexpr coord_type coord_max = (Bits >= coord_type_bits)
244 : coord_type((coord_type(1) << Bits) - 1);
245
246 static constexpr unsigned octants = unsigned(1) << Dim; // children per node
247
248 // ---- construction -----------------------------------------------------
249
250 MORTON_HD constexpr Morton() noexcept : code_(0) {}
251
253 MORTON_HD static constexpr Morton from_code(code_type raw) noexcept {
254 Morton m;
255 m.code_ = raw & field_mask;
256 return m;
257 }
258
260 template <typename... Cs, typename = std::enable_if_t<sizeof...(Cs) == Dim>>
261 MORTON_HD static constexpr Morton encode(Cs... coords) {
262 coord_type tmp[Dim] = {static_cast<coord_type>(coords)...};
263 Morton m;
264 for (unsigned d = 0; d < Dim; ++d)
265 m.code_ |= deposit(tmp[d], d);
266 return m;
267 }
268
270 MORTON_HD static constexpr Morton encode(const std::array<coord_type, Dim>& c) {
271 Morton m;
272 for (unsigned d = 0; d < Dim; ++d)
273 m.code_ |= deposit(c[d], d);
274 return m;
275 }
276
277 // ---- access -----------------------------------------------------------
278
279 MORTON_HD constexpr code_type code() const noexcept { return code_; }
280
282 MORTON_HD constexpr coord_type get(unsigned d) const { return extract(code_, d); }
283
285 MORTON_HD constexpr std::array<coord_type, Dim> decode() const {
286 std::array<coord_type, Dim> out{};
287 for (unsigned d = 0; d < Dim; ++d)
288 out[d] = extract(code_, d);
289 return out;
290 }
291
293 MORTON_HD constexpr void set(unsigned d, coord_type value) {
294 code_ = (code_ & keep_mask(d)) | deposit(value, d);
295 }
296
297 // ---- the headline: arithmetic in Morton space -------------------------
298 //
299 // Each of these touches only the bits of one axis and leaves the other
300 // axes' interleaved bits exactly where they are -- no decode/encode.
301 // All wrap modulo 2^Bits on the affected axis.
302
304 MORTON_HD constexpr void add(unsigned d, coord_type k) {
305 const code_type M = axis_mask(d);
306 code_type s = (code_ | ~M) + deposit(k, d);
307 code_ = (s & M) | (code_ & M_complement(d));
308 }
309
311 MORTON_HD constexpr void sub(unsigned d, coord_type k) {
312 const code_type M = axis_mask(d);
313 code_type s = (code_ & M) - deposit(k, d);
314 code_ = (s & M) | (code_ & M_complement(d));
315 }
316
318 MORTON_HD constexpr void inc(unsigned d) {
319 const code_type M = axis_mask(d);
320 code_type s = (code_ | ~M) + lsb(d);
321 code_ = (s & M) | (code_ & M_complement(d));
322 }
323
325 MORTON_HD constexpr void dec(unsigned d) {
326 const code_type M = axis_mask(d);
327 code_type s = (code_ & M) - lsb(d);
328 code_ = (s & M) | (code_ & M_complement(d));
329 }
330
332 MORTON_HD constexpr Morton neighbor(unsigned d, int dir) const {
333 Morton m = *this;
334 if (dir >= 0)
335 m.inc(d);
336 else
337 m.dec(d);
338 return m;
339 }
340
341 // ---- saturating / checked arithmetic (do not wrap) --------------------
342
344 MORTON_HD constexpr void add_sat(unsigned d, coord_type k) {
345 coord_type cur = get(d);
346 coord_type room = coord_type(coord_max - cur);
347 set(d, (k > room) ? coord_max : coord_type(cur + k));
348 }
349
351 MORTON_HD constexpr void sub_sat(unsigned d, coord_type k) {
352 coord_type cur = get(d);
353 set(d, (k > cur) ? coord_type(0) : coord_type(cur - k));
354 }
355
358 MORTON_HD constexpr bool try_add(unsigned d, coord_type k) {
359 coord_type cur = get(d);
360 if (k > coord_type(coord_max - cur))
361 return false;
362 set(d, coord_type(cur + k));
363 return true;
364 }
365
367 MORTON_HD constexpr bool try_sub(unsigned d, coord_type k) {
368 coord_type cur = get(d);
369 if (k > cur)
370 return false;
371 set(d, coord_type(cur - k));
372 return true;
373 }
374
375 // ---- neighbour sets ----------------------------------------------------
376
379 MORTON_HD constexpr std::array<Morton, 2 * Dim> face_neighbors() const {
380 std::array<Morton, 2 * Dim> out{};
381 for (unsigned d = 0; d < Dim; ++d) {
382 out[2 * d] = neighbor(d, -1);
383 out[2 * d + 1] = neighbor(d, +1);
384 }
385 return out;
386 }
387
390 MORTON_HD constexpr std::array<Morton, detail::pow3(Dim) - 1> all_neighbors() const {
391 std::array<Morton, detail::pow3(Dim) - 1> out{};
392 unsigned n = 0;
393 for (unsigned i = 0; i < detail::pow3(Dim); ++i) {
394 int off[Dim];
395 unsigned t = i;
396 bool zero = true;
397 for (unsigned d = 0; d < Dim; ++d) {
398 off[d] = int(t % 3) - 1;
399 if (off[d] != 0)
400 zero = false;
401 t /= 3;
402 }
403 if (zero)
404 continue; // skip self
405 Morton m = *this;
406 for (unsigned d = 0; d < Dim; ++d) {
407 if (off[d] > 0)
408 m.inc(d);
409 else if (off[d] < 0)
410 m.dec(d);
411 }
412 out[n++] = m;
413 }
414 return out;
415 }
416
417 // ---- octree / hierarchy navigation ------------------------------------
418 //
419 // A cell at `level` covers a 2^level block per axis and has its low
420 // level*Dim code bits zero. These helpers express that hierarchy directly.
421
424 MORTON_HD constexpr Morton ancestor(unsigned level) const {
425 if (level * Dim >= code_bits)
426 return Morton{};
427 code_type lowmask = (code_type(1) << (level * Dim)) - 1;
428 return from_code(code_ & ~lowmask);
429 }
430
433 MORTON_HD constexpr unsigned child_index(unsigned level) const {
434 return unsigned((code_ >> (level * Dim)) & (octants - 1));
435 }
436
439 MORTON_HD constexpr Morton child(unsigned level, unsigned octant) const {
440 unsigned shift = (level - 1) * Dim;
441 code_type cleared = code_ & ~(code_type(octants - 1) << shift);
442 return from_code(cleared | (code_type(octant) << shift));
443 }
444
445 // ---- Z-order successor / predecessor ----------------------------------
446
447 MORTON_HD constexpr Morton& operator++() noexcept {
448 code_ = (code_ + 1) & field_mask;
449 return *this;
450 }
451 MORTON_HD constexpr Morton operator++(int) noexcept {
452 Morton t = *this;
453 ++*this;
454 return t;
455 }
456 MORTON_HD constexpr Morton& operator--() noexcept {
457 code_ = (code_ - 1) & field_mask;
458 return *this;
459 }
460 MORTON_HD constexpr Morton operator--(int) noexcept {
461 Morton t = *this;
462 --*this;
463 return t;
464 }
465
466 // ---- comparison (Z-order) ---------------------------------------------
467
468 friend MORTON_HD constexpr bool operator==(Morton a, Morton b) noexcept {
469 return a.code_ == b.code_;
470 }
471 friend MORTON_HD constexpr bool operator!=(Morton a, Morton b) noexcept {
472 return a.code_ != b.code_;
473 }
474 friend MORTON_HD constexpr bool operator<(Morton a, Morton b) noexcept {
475 return a.code_ < b.code_;
476 }
477 friend MORTON_HD constexpr bool operator<=(Morton a, Morton b) noexcept {
478 return a.code_ <= b.code_;
479 }
480 friend MORTON_HD constexpr bool operator>(Morton a, Morton b) noexcept {
481 return a.code_ > b.code_;
482 }
483 friend MORTON_HD constexpr bool operator>=(Morton a, Morton b) noexcept {
484 return a.code_ >= b.code_;
485 }
486
487 // ---- low-level deposit/extract ----------------------------------------
488
489 MORTON_HD static constexpr code_type deposit(coord_type x, unsigned d) {
490 if constexpr (code_bits <= 64) {
491 // PDEP is a host x86 instruction; never emit it in CUDA device code
492 // (where __CUDA_ARCH__ is defined), even if the host was -mbmi2.
493#if MORTON_BMI2_INTRINSICS && !defined(__CUDA_ARCH__)
495 return code_type(_pdep_u64(std::uint64_t(x), std::uint64_t(axis_mask(d))));
496#elif MORTON_X86_RUNTIME_DISPATCH
497 // Single-binary path: use PDEP when the running CPU has BMI2.
498 if (!detail::is_consteval() && detail::cpu_has_bmi2())
499 return code_type(detail::pdep_u64_hw(std::uint64_t(x), std::uint64_t(axis_mask(d))));
500#endif
501 }
503 }
504
505 MORTON_HD static constexpr coord_type extract(code_type c, unsigned d) {
506 if constexpr (code_bits <= 64) {
507#if MORTON_BMI2_INTRINSICS && !defined(__CUDA_ARCH__)
509 return coord_type(_pext_u64(std::uint64_t(c), std::uint64_t(axis_mask(d))));
510#elif MORTON_X86_RUNTIME_DISPATCH
511 if (!detail::is_consteval() && detail::cpu_has_bmi2())
512 return coord_type(detail::pext_u64_hw(std::uint64_t(c), std::uint64_t(axis_mask(d))));
513#endif
514 }
516 }
517
519 MORTON_HD static constexpr code_type axis_mask(unsigned d) {
520#if defined(__CUDA_ARCH__)
521 // On device, compute the mask (avoids referencing the host static
522 // array from device code); it is a short compile-time-sized loop.
524#else
525 return axis_masks_[d];
526#endif
527 }
528
529 private:
530 MORTON_HD static constexpr code_type M_complement(unsigned d) {
531 return field_mask & ~axis_mask(d);
532 }
533 MORTON_HD static constexpr code_type keep_mask(unsigned d) { return M_complement(d); }
534 MORTON_HD static constexpr code_type lsb(unsigned d) { return code_type(1) << d; }
535
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)
540 return a;
541 }
542 static constexpr std::array<code_type, Dim> axis_masks_ = make_masks();
543
544 code_type code_;
545};
546
547// Convenient aliases for the common cases.
548using Morton2D32 = Morton<2, 32>; // 2D, 32 bits/axis -> 64-bit code
549using Morton2D16 = Morton<2, 16>; // 2D, 16 bits/axis -> 32-bit code
550using Morton3D21 = Morton<3, 21>; // 3D, 21 bits/axis -> 63-bit code
551using Morton3D16 = Morton<3, 16>; // 3D, 16 bits/axis -> 48-bit code
552#if defined(MORTON_HAS_INT128)
553using Morton3D32 = Morton<3, 32>; // 3D, 32 bits/axis -> 96-bit code
554using Morton2D64 = Morton<2, 64>; // 2D, 64 bits/axis -> 128-bit code
555#endif
556
557} // namespace morton
558
559#endif // MORTON_MORTON_HPP
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
Definition batch.hpp:26
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