MORTIF
MORTon Indexer (Z-order) Fortran environment
a pure Fortran 2003+ library to encode and decode multidimensional integer indexes into Morton’s Z-order.

🔢 2D encoding
morton2D maps two 32-bit indexes → one 64-bit Morton code |
📐 3D encoding
morton3D maps three 32-bit indexes → one 64-bit code (up to 21 bits/axis) |
🔄 Lossless decoding
demorton2D/demorton3D are the exact inverse — bit-perfect round-trip |
⚡ Elemental interface All four procedures work on scalars and conformable arrays in one call |
🧩 Single module Entire library is a single mortif.f90 — easy to vendor |
📏 Configurable precision Optional b parameter restricts to 2/4/8/16/32 significant bits per axis |
🏎️ Zero overhead No range checks, no allocations, no system calls |
📦 Multiple build systems FoBiS, CMake, GNU Make |
For full documentation (guide, API reference, examples, etc…) see the MORTIF website.
Authors
Contributions are welcome — see the Contributing page.
Copyrights
This project is distributed under a multi-licensing system:
Anyone interested in using, developing, or contributing to this project is welcome — pick the license that best fits your needs.
Quick start
use, intrinsic :: iso_fortran_env, only : int32, int64
use mortif
implicit none
integer(int32) :: i, j, k
integer(int64) :: code
code = morton3D(i=0_int32, j=1_int32, k=0_int32)
print '(A,I0)', "Morton code of {0,1,0}: ", code ! 2
call demorton3D(code=code, i=i, j=j, k=k)
print '(A,3(I0,1X))', "Decoded: ", i, j, k ! 0 1 0
Elemental use on arrays:
use, intrinsic :: iso_fortran_env, only : int32, int64
use mortif
implicit none
integer(int32) :: ix(4) = [0, 1, 2, 3]
integer(int32) :: iy(4) = [0, 0, 1, 1]
integer(int64) :: codes(4)
codes = morton2D(i=ix, j=iy) ! [0, 1, 4, 5]
See src/tests/ for correctness tests including extrema and 4096-point 16×16×16 grid validation.
Install
One-line install
curl -fsSL https://github.com/szaghi/MORTIF/releases/latest/download/install.sh | bash
It installs the latest release into ~/.local with the build system you choose among those MORTIF ships
(--build fobis|fpm: a menu asks when you do not say), the compiler you name (--fc, --gnu, --intel), one install
per compiler (lib/mortif/<compiler>-<version>/, include/mortif/<compiler>-<version>/), and makes it usable through
pkg-config (pkg-config --cflags --libs mortif, with PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig). The release
carries PENF. Pass options after bash -s --, e.g. ... | bash -s -- --build fpm --prefix /opt/mortif; --help
lists them all, --uninstall removes the install. The installer is FoBiS’s
(One-line installer).
FoBiS
Standalone — clone, fetch the dependency, and build:
git clone https://github.com/szaghi/MORTIF && cd MORTIF
fobis fetch # fetch PENF
fobis build --mode static-gnu # static/libmortif.a, modules in static/mod
As a project dependency — declare MORTIF in your fobos and run fetch:
[dependencies]
deps_dir = src/third_party
MORTIF = https://github.com/szaghi/MORTIF
fobis fetch # fetch MORTIF and PENF
fobis fetch --update # update them
fpm
[dependencies]
MORTIF = { git = "https://github.com/szaghi/MORTIF" }
评论