// cpu prefetch intrinsics for x86_64
// use for optimizing memory access patterns in latency-sensitive code

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
use std::arch::x86_64::{_MM_HINT_T0, _mm_prefetch};

/// prefetches memory for read access into L1 cache.
///
/// use this for data that will be read soon and accessed multiple times.
///
/// # safety
///
/// the pointer should be derived from a valid allocation or object. On x86_64
/// the prefetch instruction is only a cache hint and generally does not fault,
/// but callers should still avoid fabricating invalid provenance.
#[inline(always)]
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
pub unsafe fn prefetch_read<T>(ptr: *const T) {
    unsafe { _mm_prefetch(ptr as *const i8, _MM_HINT_T0) }
}

/// no-op prefetch fallback for unsupported targets.
///
/// # safety
///
/// this function does not dereference the pointer.
#[inline(always)]
#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
pub unsafe fn prefetch_read<T>(_ptr: *const T) {
    // no-op on unsupported platforms
}
