C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates
C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates
Just include the header include/libmorton/morton.h. This will always have stub functions that point to the most efficient way to encode/decode Morton codes. If you want to test out alternative (and possibly slower) methods, you can find them in include/libmorton/morton2D.h and include/libmorton/morton3D.h. All libmorton functionality is in the libmorton namespace to avoid conflicts.
// ENCODING 2D / 3D morton codes, of length 32 and 64 bits inline uint_fast32_t morton2D_32_encode(const uint_fast16_t x, const uint_fast16_t y); inline uint_fast64_t morton2D_64_encode(const uint_fast32_t x, const uint_fast32_t y); inline uint_fast32_t morton3D_32_encode(const uint_fast16_t x, const uint_fast16_t y, const uint_fast16_t z); inline uint_fast64_t morton3D_64_encode(const uint_fast32_t x, const uint_fast32_t y, const uint_fast32_t z); // DECODING 2D / 3D morton codes, of length 32 and 64 bits inline void morton2D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y); inline void morton2D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y); inline void morton3D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y, uint_fast16_t& z); inline void morton3D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y, uint_fast32_t& z);
No compilation / installation is required (just download the headers and include them), but I was informed libmorton is packaged for Microsoft's VCPKG system as well, if you want a more controlled environment to install C++ packages in.
In the standard case, libmorton only uses operations that are supported on pretty much any CPU you can throw it at. If you know you're compiling for a specific architecture, you might gain a speed boost in encoding/decoding operations by enabling implementations for a specific instruction set. Libmorton ships with support for:
__BMI2__ before including morton.h. This is definitely a faster method when compared to the standard case._mm512_bitshuffle_epi64_mask. Define __AVX512BITALG__ before including morton.h. For more info on performance, see this PR.When using MSVC, these options can be found under Project Properties -> Code Generation -> Enable Enhanced Instruction set.
When using GCC (version 9.0 or higher), you can use -march=haswell (or -march=znver2) for BMI2 support and -march=icelake-client for AVX512 support.
The test folder contains tools I use to test correctness and performance of the libmorton implementation. You can regard them as unit tests. This section is under heavy re-writing, but might contain some useful code for advanced usage.
You can build the test suite:
test\msvc*test\makefileIf you use libmorton in your published paper or work, please reference it, for example as follows:
@Misc{libmorton18, author = "Jeroen Baert", title = "Libmorton: C++ Morton Encoding/Decoding Library", howpublished = "\url{https://github.com/Forceflow/libmorton}", year = "2018"}
I'm always curious what libmorton ends up on. If you end up using it, send me an e-mail!
See Contributing.md
No open issues yet, or sync has not completed.