MORTIF

March 2, 2026 ยท View on GitHub

MORTon Indexer (Z-order) Fortran environment

a pure Fortran 2003+ library to encode and decode multidimensional integer indexes into Morton's Z-order.

GitHub tag GitHub issues CI Coverage License

๐Ÿ”ข 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

Documentation

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

FoBiS

Standalone โ€” clone, fetch the dependency, and build:

git clone https://github.com/szaghi/MORTIF && cd MORTIF
FoBiS.py fetch                    # fetch PENF
FoBiS.py build -mode static-gnu   # build static library

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.py fetch           # fetch and build
FoBiS.py fetch --update  # re-fetch and rebuild

CMake

git clone https://github.com/szaghi/MORTIF --recursive && cd MORTIF
cmake -B build -DMORTIF_ENABLE_TESTS=ON && cmake --build build && ctest --test-dir build

As a CMake subdirectory:

add_subdirectory(MORTIF)
target_link_libraries(your_target MORTIF::MORTIF)