xocomil 0.3.0

A lightweight, zero-allocation HTTP/1.1 request parser and response writer
Documentation
//! Shared SIMD primitive macro definitions for SSE2 and wasm SIMD.
//!
//! The `define_simd_primitives!()` macro stamps out architecture-specific
//! helper macros (`simd_splat!`, `simd_load!`, `simd_mask!`, etc.) that
//! abstract over intrinsic differences. Algorithm macros in `bytes.rs`,
//! `scan.rs`, and `validate.rs` consume these primitives.

/// Define SIMD primitive macros and `use` imports for the current target.
///
/// On x86-64: wraps `_mm_*` intrinsics (SSE2).
/// On wasm SIMD (wasm32 + simd128): wraps `u8x16_*` / `v128_*` intrinsics.
/// SSSE3 (pshufb) TCHAR validation lives in [`crate::tchar`].
///
/// Produces: `simd_splat!`, `simd_load!`, `simd_mask!` (cmpeq+bitmask),
/// `simd_cmpeq!`, `simd_bitmask!`, `simd_or!`, `simd_max_epu8!`,
/// `simd_andnot!`.
macro_rules! define_simd_primitives {
    () => {
        #[cfg(target_arch = "x86_64")]
        #[allow(unused_imports)]
        use std::arch::x86_64::{
            __m128i, _mm_andnot_si128, _mm_cmpeq_epi8, _mm_loadu_si128, _mm_max_epu8,
            _mm_movemask_epi8, _mm_or_si128, _mm_set1_epi8,
        };

        #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
        #[allow(unused_imports)]
        use std::arch::wasm32::{
            u8x16_bitmask, u8x16_eq, u8x16_max, u8x16_splat, v128_andnot, v128_load, v128_or,
        };

        #[allow(unused_macros)]
        macro_rules! simd_splat {
            ($val:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_set1_epi8($val as i8)
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    u8x16_splat($val)
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_load {
            ($ptr:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_loadu_si128(($ptr).cast::<__m128i>())
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    v128_load(($ptr).cast())
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_mask {
            ($chunk:expr, $vec:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_movemask_epi8(_mm_cmpeq_epi8($chunk, $vec)) as u32
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    u8x16_bitmask(u8x16_eq($chunk, $vec)) as u32
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_cmpeq {
            ($a:expr, $b:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_cmpeq_epi8($a, $b)
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    u8x16_eq($a, $b)
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_bitmask {
            ($v:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_movemask_epi8($v) as u32
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    u8x16_bitmask($v) as u32
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_or {
            ($a:expr, $b:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_or_si128($a, $b)
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    v128_or($a, $b)
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_max_epu8 {
            ($a:expr, $b:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_max_epu8($a, $b)
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    u8x16_max($a, $b)
                }
            }};
        }

        #[allow(unused_macros)]
        macro_rules! simd_andnot {
            ($negate:expr, $other:expr) => {{
                #[cfg(target_arch = "x86_64")]
                {
                    _mm_andnot_si128($negate, $other)
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    v128_andnot($other, $negate)
                }
            }};
        }
    };
}

pub(crate) use define_simd_primitives;