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")
}
fn widen_into(src: &[Self], dst: &mut [f32]) {
for (o, &v) in dst.iter_mut().zip(src) {
*o = Self::lane(v);
}
}
fn fused_rows_dist<M: Metric>(_q: &[f32], _xs: [&[Self]; 4]) -> Option<[f32; 4]> {
None
}
}
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
}
#[inline]
pub fn narrow_half(v: f32) -> u16 {
let b = v.to_bits();
let sign = ((b >> 16) & 0x8000) as u16;
let exp = ((b >> 23) & 0xff) as i32;
let man = b & 0x007f_ffff;
if exp == 0xff {
return sign | 0x7c00 | if man != 0 { 0x0200 } else { 0 }; }
let e = exp - 112; if e >= 31 {
return sign | 0x7c00;
}
if e <= 0 {
let shift = (14 - e) as u32;
if shift > 24 {
return sign;
}
let m = man | 0x0080_0000;
let mut h = m >> shift;
let rem = m & ((1 << shift) - 1);
let half = 1 << (shift - 1);
if rem > half || (rem == half && h & 1 == 1) {
h += 1;
}
return sign | h as u16;
}
let mut h = ((e as u32) << 10) | (man >> 13);
let rem = man & 0x1fff;
if rem > 0x1000 || (rem == 0x1000 && h & 1 == 1) {
h += 1; }
sign | h as u16
}
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)
}
fn widen_into(src: &[u16], dst: &mut [f32]) {
super::half::widen(src, dst);
}
fn fused_rows_dist<M: Metric>(q: &[f32], xs: [&[u16]; 4]) -> Option<[f32; 4]> {
super::half::rows_dist::<M>(q, xs)
}
}