use std::sync::OnceLock;
pub(crate) type EntropyOfHistFn = fn(&[u64], u64) -> f32;
fn select_entropy_of_hist_fn() -> EntropyOfHistFn {
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
if std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("fma") {
return |hist, total| unsafe { crate::avx::entropy_of_hist_avx2(hist, total) };
}
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
{
|hist, total| unsafe { crate::neon::entropy_of_hist_neon(hist, total) }
}
#[cfg(not(all(target_arch = "aarch64", feature = "neon")))]
{
entropy_of_hist_scalar
}
}
pub(crate) fn selected_entropy_of_hist_fn() -> EntropyOfHistFn {
static ENTROPY_OF_HIST: OnceLock<EntropyOfHistFn> = OnceLock::new();
*ENTROPY_OF_HIST.get_or_init(select_entropy_of_hist_fn)
}
#[cfg(any(test, not(all(target_arch = "aarch64", feature = "neon"))))]
pub(super) fn entropy_of_hist_scalar(hist: &[u64], total: u64) -> f32 {
use crate::adaptive_quant::dirty_log2f;
if total == 0 {
return 0.0;
}
let t = total as f32;
let mut bits = 0.0;
for &c in hist.iter() {
if c != 0 {
let p = c as f32 / t;
bits -= c as f32 * dirty_log2f(p);
}
}
bits
}