use super::kernels::L;
use super::metric::Metric;
pub trait Elem: Copy + Send + Sync {
const PAD: Self;
fn as_f32<'a>(blk: &'a [[Self; L]], buf: &'a mut Vec<[f32; L]>) -> &'a [[f32; L]];
fn lane(x: Self) -> f32;
const DIRECT: bool;
fn integer_query(_q: &[f32]) -> Option<Vec<u8>> {
None
}
const ROW_MAJOR: bool = false;
fn int_dist<M: Metric>(_q: &[u8], _x: &[Self]) -> u32 {
unreachable!("integer distances are only for byte lists")
}
}
impl Elem for f32 {
const PAD: f32 = f32::INFINITY;
const DIRECT: bool = true;
#[inline(always)]
fn as_f32<'a>(blk: &'a [[f32; L]], _buf: &'a mut Vec<[f32; L]>) -> &'a [[f32; L]] {
blk
}
#[inline(always)]
fn lane(x: f32) -> f32 {
x
}
}
impl Elem for u8 {
const PAD: u8 = 0;
const DIRECT: bool = true;
#[inline]
fn as_f32<'a>(blk: &'a [[u8; L]], buf: &'a mut Vec<[f32; L]>) -> &'a [[f32; L]] {
buf.resize(blk.len(), [0.0; L]);
for (o, &h) in buf.as_flattened_mut().iter_mut().zip(blk.as_flattened()) {
*o = f32::from(h);
}
buf
}
#[inline(always)]
fn lane(x: u8) -> f32 {
f32::from(x)
}
fn integer_query(q: &[f32]) -> Option<Vec<u8>> {
if q.len() > 258
|| !q
.iter()
.all(|&v| (0.0..=255.0).contains(&v) && v.fract() == 0.0)
{
return None;
}
Some(q.iter().map(|&v| v as u8).collect())
}
const ROW_MAJOR: bool = true;
#[inline]
fn int_dist<M: Metric>(q: &[u8], x: &[u8]) -> u32 {
M::dist_int(q, x)
}
}
const HALF_SCALE: f32 = f32::from_bits((127 + 112) << 23);
#[inline(always)]
pub fn widen_half(h: u16) -> f32 {
let h = u32::from(h);
f32::from_bits(((h & 0x8000) << 16) | ((h & 0x7fff) << 13)) * HALF_SCALE
}
impl Elem for u16 {
const PAD: u16 = 0;
const DIRECT: bool = false;
#[inline]
fn as_f32<'a>(blk: &'a [[u16; L]], buf: &'a mut Vec<[f32; L]>) -> &'a [[f32; L]] {
buf.resize(blk.len(), [0.0; L]);
for (o, &h) in buf.as_flattened_mut().iter_mut().zip(blk.as_flattened()) {
*o = widen_half(h);
}
buf
}
#[inline(always)]
fn lane(x: u16) -> f32 {
widen_half(x)
}
}