use std::arch::x86_64::*;
use crate::entropy::ALPHABET_SIZE;
#[inline]
#[target_feature(enable = "avx2,fma")]
fn dirty_log2f_x8(d: __m256) -> __m256 {
let one = _mm256_set1_ps(1.0);
let mut ix = _mm256_castps_si256(d);
ix = _mm256_add_epi32(
ix,
_mm256_set1_epi32((0x3f80_0000u32 - 0x3f35_04f3u32) as i32),
);
let n = _mm256_sub_epi32(_mm256_srli_epi32::<23>(ix), _mm256_set1_epi32(0x7f));
ix = _mm256_add_epi32(
_mm256_and_si256(ix, _mm256_set1_epi32(0x007f_ffff)),
_mm256_set1_epi32(0x3f35_04f3),
);
let a = _mm256_castsi256_ps(ix);
let numerator = _mm256_sub_ps(a, one);
let denominator = _mm256_add_ps(a, one);
let reciprocal0 = _mm256_rcp_ps(denominator);
let reciprocal = _mm256_mul_ps(
reciprocal0,
_mm256_fnmadd_ps(denominator, reciprocal0, _mm256_set1_ps(2.0)),
);
let x = _mm256_mul_ps(numerator, reciprocal);
let x2 = _mm256_mul_ps(x, x);
let mut u = _mm256_set1_ps(0.412_198_57);
u = _mm256_fmadd_ps(u, x2, _mm256_set1_ps(0.577_078_04));
u = _mm256_fmadd_ps(u, x2, _mm256_set1_ps(0.961_796_7));
let base = _mm256_fmadd_ps(x, _mm256_set1_ps(2.885_390_1), _mm256_cvtepi32_ps(n));
_mm256_fmadd_ps(_mm256_mul_ps(x2, x), u, base)
}
#[inline]
#[target_feature(enable = "avx2")]
fn cvtepu32_ps(v: __m256i) -> __m256 {
let signed = _mm256_cvtepi32_ps(v);
let high = _mm256_cmpgt_epi32(_mm256_setzero_si256(), v);
_mm256_add_ps(
signed,
_mm256_and_ps(_mm256_castsi256_ps(high), _mm256_set1_ps(4_294_967_296.0)),
)
}
#[target_feature(enable = "avx2,fma")]
pub(crate) fn counts_bit_cost_avx2(counts: &[u32; ALPHABET_SIZE], total_count: u32) -> f32 {
debug_assert_ne!(total_count, 0);
let log_total = _mm256_set1_ps(crate::adaptive_quant::dirty_log2f(total_count as f32));
let one = _mm256_set1_ps(1.0);
let mut cost = _mm256_setzero_ps();
for counts8 in counts.as_chunks::<8>().0 {
let count_i = unsafe { _mm256_loadu_si256(counts8.as_ptr().cast()) };
let count = cvtepu32_ps(count_i);
let positive = _mm256_max_ps(count, one);
cost = _mm256_fmadd_ps(
count,
_mm256_sub_ps(log_total, dirty_log2f_x8(positive)),
cost,
);
}
super::ac_strategy::hsum256(cost).max(0.0)
}