NAME

Sort::DJB - Fast sorting using Daniel J. Bernstein's djbsort bitonic sorting networks

SYNOPSIS

use Sort::DJB qw(:all);

# Sort signed 32-bit integers (ascending)
my $sorted = sort_int32([5, 3, 1, 4, 2]);
# $sorted = [1, 2, 3, 4, 5]

# Sort descending
my $desc = sort_int32down([5, 3, 1, 4, 2]);
# $desc = [5, 4, 3, 2, 1]

# All data types
sort_int32(\@data);       sort_int32down(\@data);
sort_uint32(\@data);      sort_uint32down(\@data);
sort_int64(\@data);       sort_int64down(\@data);
sort_uint64(\@data);      sort_uint64down(\@data);
sort_float32(\@data);     sort_float32down(\@data);
sort_float64(\@data);     sort_float64down(\@data);

# Metadata
print Sort::DJB::version(), "\n";          # "20260210"
print Sort::DJB::arch(), "\n";             # "portable" or "amd64"
print Sort::DJB::int32_implementation(), "\n";  # e.g. "portable4" or "avx2"

DESCRIPTION

Sort::DJB provides Perl bindings to Daniel J. Bernstein's djbsort library, which sorts arrays using bitonic sorting networks. Key properties:

  • Fast - SIMD-optimized (AVX2/SSE4.2/NEON) when using a system-installed djbsort; portable C fallback when built standalone.

  • Constant-time - Data-independent execution flow, suitable for cryptographic applications where timing side-channels must be avoided.

  • Type-safe - Dedicated functions for int32, uint32, int64, uint64, float32, and float64, with both ascending and descending variants.

Each sort function takes an array reference and returns a new sorted array reference. The input array is not modified.

FUNCTIONS

All functions are exportable. Use :all to import everything, or import by type group (:int32, :uint32, :int64, :uint64, :float32, :float64).

sort_int32(\@array), sort_int32down(\@array)

Sort signed 32-bit integers in ascending/descending order.

sort_uint32(\@array), sort_uint32down(\@array)

Sort unsigned 32-bit integers.

sort_int64(\@array), sort_int64down(\@array)

Sort signed 64-bit integers.

sort_uint64(\@array), sort_uint64down(\@array)

Sort unsigned 64-bit integers.

sort_float32(\@array), sort_float32down(\@array)

Sort 32-bit floats. Handles NaN correctly.

sort_float64(\@array), sort_float64down(\@array)

Sort 64-bit doubles. Handles NaN correctly.

version()

Returns the djbsort library version string (e.g., "20260210").

arch()

Returns the architecture string (e.g., "amd64", "portable").

int32_implementation(), int64_implementation()

Returns the active sorting implementation (e.g., "avx2", "portable4").

ALGORITHM

djbsort uses bitonic sorting networks with O(n log^2 n) comparisons. Despite the worse asymptotic complexity compared to quicksort's O(n log n), the branch-free, vectorizable nature of sorting networks makes djbsort significantly faster in practice, especially with SIMD instructions.

SEE ALSO

https://sorting.cr.yp.to/ - djbsort homepage

Sort::DJB::Pure - Pure Perl implementation of the same algorithm

AUTHOR

XS bindings for djbsort-20260210 by Daniel J. Bernstein.

LICENSE

GPLv2. See the LICENSE file included with this distribution for the full text of the GNU General Public License, version 2.