Skip to main content

Module simd_distance

Module simd_distance 

Source
Expand description

SIMD-accelerated distance computations for nearest-neighbor search.

§Strategy

Points are stored in Structure-of-Arrays (SoA) layout — three separate f32 slices for X, Y, and Z — so that SIMD lanes map directly onto multiple points at once.

AoS (cache-unfriendly for distance):   [(x0,y0,z0), (x1,y1,z1), …]
SoA (SIMD-friendly):                   xs=[x0,x1,…]  ys=[y0,y1,…]  zs=[z0,z1,…]

Given a query q = (qx, qy, qz) the squared distance to point i is:

d²ᵢ = (xᵢ−qx)² + (yᵢ−qy)² + (zᵢ−qz)²

SIMD processes 4 (SSE2) or 8 (AVX2) distances per instruction cycle.

§Dispatch

has AVX2?  → avx2_distances_squared  (8-wide f32)
else SSE2? → sse2_distances_squared  (4-wide f32, always present on x86-64)
else       → scalar_distances_squared (portable fallback)

The dispatch is resolved at runtime using is_x86_feature_detected!, so the same binary runs correctly on all hardware while using the widest SIMD available.

Structs§

SimdBruteForceSearch
Brute-force nearest-neighbor search with SIMD-accelerated distance computation.
SoaPoints
Point cloud stored in Structure-of-Arrays format for SIMD-friendly access.

Functions§

batch_distances_squared
Compute squared Euclidean distances from query to every point in pts.
scalar_distances_squared
Portable, vectorisation-friendly fallback (also the reference implementation).