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