use core::arch::x86_64::*;
#[target_feature(enable = "avx512f,avx512vl")]
pub unsafe fn f32_to_bf16_avx512(src: &[f32], dest: &mut [u16]) {
let n = core::cmp::min(src.len(), dest.len());
let mut i = 0;
while i + 16 <= n {
unsafe {
let v = _mm512_loadu_ps(src.as_ptr().add(i)); let v_i = _mm512_castps_si512(v); let v_shifted = _mm512_srli_epi32(v_i, 16); let packed = _mm512_cvtepi32_epi16(v_shifted); _mm256_storeu_si256(dest.as_mut_ptr().add(i) as *mut __m256i, packed); }
i += 16;
}
while i < n {
unsafe {
*dest.get_unchecked_mut(i) = ((*src.get_unchecked(i)).to_bits() >> 16) as u16;
}
i += 1;
}
}
#[inline(always)]
pub unsafe fn adaptive_prefetch_f32(ptr: *const f32, dilation: usize) {
if dilation <= 8 {
} else if dilation <= 64 {
unsafe {
_mm_prefetch::<_MM_HINT_T0>(ptr as *const i8);
}
} else {
unsafe {
_mm_prefetch::<_MM_HINT_T1>(ptr as *const i8);
}
}
}
#[inline(always)]
pub unsafe fn adaptive_prefetch_2stage_f32(
ptr_next: *const f32,
ptr_next_next: *const f32,
dilation: usize,
) {
if dilation >= 128 {
unsafe {
_mm_prefetch::<_MM_HINT_T0>(ptr_next as *const i8);
_mm_prefetch::<_MM_HINT_T1>(ptr_next_next as *const i8);
}
} else {
unsafe {
adaptive_prefetch_f32(ptr_next, dilation);
}
}
}
#[inline(always)]
pub fn prefetch_t0<T>(base: *const T, offset: usize) {
unsafe {
core::arch::x86_64::_mm_prefetch::<{ core::arch::x86_64::_MM_HINT_T0 }>(
base.wrapping_add(offset).cast(),
);
}
}
#[inline(always)]
pub fn prefetch_t1<T>(base: *const T, offset: usize) {
unsafe {
core::arch::x86_64::_mm_prefetch::<{ core::arch::x86_64::_MM_HINT_T1 }>(
base.wrapping_add(offset).cast(),
);
}
}
#[inline(always)]
pub fn prefetch_strategy_simple(
base_ptr: *const f32,
_step: usize,
_k: usize,
_k_limit: usize,
_dilation: usize,
) {
prefetch_t0(base_ptr, 16);
}
#[inline(always)]
pub fn prefetch_strategy_2stage(
base_ptr: *const f32,
step: usize,
k: usize,
k_limit: usize,
_dilation: usize,
) {
if k + 1 < k_limit {
prefetch_t0(base_ptr, step);
if k + 2 < k_limit {
prefetch_t1(base_ptr, 2 * step);
}
}
}
pub unsafe fn set_daz_ftz() {
unsafe {
let mut mxcsr: u32 = 0;
core::arch::asm!("stmxcsr [{0}]", in(reg) &mut mxcsr);
mxcsr |= 0x8040;
core::arch::asm!("ldmxcsr [{0}]", in(reg) &mxcsr);
}
}