Tedd.MortonEncoding 1.1.0

dotnet add package Tedd.MortonEncoding --version 1.1.0
                    
NuGet\Install-Package Tedd.MortonEncoding -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tedd.MortonEncoding" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tedd.MortonEncoding" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Tedd.MortonEncoding" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tedd.MortonEncoding --version 1.1.0
                    
#r "nuget: Tedd.MortonEncoding, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tedd.MortonEncoding@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tedd.MortonEncoding&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Tedd.MortonEncoding&version=1.1.0
                    
Install as a Cake Tool

Tedd.MortonEncoding

Allocation-free Morton encoding and decoding (Z-order curves) for .NET.

NuGet · Benchmark methodology

Install

dotnet add package Tedd.MortonEncoding --version 1.1.0

Capacity

API Dimensions Coordinate capacity Morton code
Encode / Decode 2D 16 bits per axis uint
Encode / Decode 3D 10 bits per axis uint
Encode64 / Decode64 2D 32 bits per axis ulong
Encode64 / Decode64 3D 21 bits per axis ulong (bit 63 unused)

Encoding places X in bit positions 0, 2, 4, ... for 2D, or 0, 3, 6, ... for 3D. Y and Z occupy the subsequent lanes.

The hot-path encoders deliberately mask unsupported high coordinate bits rather than validating or throwing. For example, 32-bit 3D encoding uses x & 0x3FF; 64-bit 3D encoding uses x & 0x1FFFFF.

Usage

using Tedd;

uint x = 10;
uint y = 20;

uint code2D = MortonEncoding.Encode(x, y);
MortonEncoding.Decode(code2D, out uint decodedX, out uint decodedY);

uint x32 = 4_000_000_000;
uint y32 = 3_000_000_000;

ulong code2D64 = MortonEncoding.Encode64(x32, y32);
MortonEncoding.Decode64(code2D64, out uint decodedX32, out uint decodedY32);

uint x21 = 1_000_000;
uint y21 = 1_500_000;
uint z21 = 2_000_000;

ulong code3D64 = MortonEncoding.Encode64(x21, y21, z21);
MortonEncoding.Decode64(code3D64, out uint decodedX21, out uint decodedY21, out uint decodedZ21);

The equivalent 3D 32-bit overloads are Encode(uint, uint, uint) and Decode(uint, out uint, out uint, out uint).

Execution paths

On netcoreapp3.0, net8.0, and net10.0, the dispatcher uses x86/x64 BMI2 PDEP and PEXT instructions when the processor supports them. Other processors and the net461 / netstandard assets use shift-and-mask software implementations.

EncodeFallback, DecodeFallback, Encode64Fallback, and Decode64Fallback expose the deterministic software path for environments that require it and for comparative benchmarks. All methods are thread-safe and allocate no managed memory.

Contiguous-field helpers

SplitXY and SplitXYZ split a scalar containing adjacent, equally sized fields. They do not decode an interleaved Morton code.

MortonEncoding.SplitXY(0b_1010_0011u, 4, out uint high, out uint low);
// high == 0b1010; low == 0b0011

Valid practical widths are 0-16 bits for SplitXY and 0-10 bits for SplitXYZ.

Target frameworks

The package provides assets for .NET Framework 4.6.1, .NET Standard 1.0 and 2.0, .NET Core 3.0, .NET 8, and .NET 10. The lowest compatible assets retain broad support; the concrete .NET Core and modern .NET assets enable runtime intrinsics.

Build, test, and benchmark

dotnet restore src/Tedd.MortonEncoding.sln
dotnet test src/Tedd.MortonEncoding.sln --configuration Release
dotnet run --project src/Tedd.MortonEncoding.Benchmarks -c Release -f net10.0 -- --filter '*'

The benchmark project retains the published 1.0.1 implementation as an archive and compares it with the current dispatch and software paths. See BENCHMARKS.md for hypotheses, controls, and interpretation.

Credits

The shift-and-mask constants derive from the bit-interleaving techniques described by Jeroen Baert in Morton encoding/decoding through bit interleaving. Julien Bilalte identified the BMI2 PDEP / PEXT approach.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.0 is compatible.  netstandard1.1 was computed.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wp8 was computed.  wp81 was computed.  wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 51 9/4/2026
1.0.1 1,412 1/11/2020
1.0.0 770 1/9/2020

Adds 64-bit 2D and 3D APIs, restores target-framework compatibility, enables BMI2 on .NET 10, fixes 3D fallback masking, and expands tests and benchmarks.