| 15 | /// falls back to scalar implementations on other architectures. |
| 16 | #[inline] |
| 17 | pub fn distance(a: &[f32], b: &[f32], metric: DistanceMetric) -> f32 { |
| 18 | assert_eq!( |
| 19 | a.len(), |
| 20 | b.len(), |
| 21 | "distance: length mismatch (a.len()={}, b.len()={})", |
| 22 | a.len(), |
| 23 | b.len() |
| 24 | ); |
| 25 | let rt = simd::runtime(); |
| 26 | match metric { |
| 27 | DistanceMetric::L2 => (rt.l2_squared)(a, b), |
| 28 | DistanceMetric::Cosine => (rt.cosine_distance)(a, b), |
| 29 | DistanceMetric::InnerProduct => (rt.neg_inner_product)(a, b), |
| 30 | DistanceMetric::Manhattan => manhattan(a, b), |
| 31 | DistanceMetric::Chebyshev => chebyshev(a, b), |
| 32 | DistanceMetric::Hamming => hamming_f32(a, b), |
| 33 | DistanceMetric::Jaccard => jaccard(a, b), |
| 34 | DistanceMetric::Pearson => pearson(a, b), |
| 35 | // Unknown future metric — fall back to L2. |
| 36 | _ => (rt.l2_squared)(a, b), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Batch distance: compute distances from `query` to each candidate. |
| 41 | /// |