vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
use crate::ir::{Expr, Program};
use crate::ops::primitive;
use crate::ops::{AlgebraicLaw, OpSpec, U32_OUTPUTS, U32_U32_INPUTS};

// Extract a bounded bit field into the low bits.
//
// Packed operand encodes offset (bits 0..4) and count (bits 5..9).
// Extracts `count` bits starting at `offset` from value.
// Count is clamped so extraction never crosses bit 31.
// A zero count returns 0.



pub const LAWS: &[AlgebraicLaw] = &[AlgebraicLaw::Bounded {
    lo: 0,
    hi: u32::MAX,
}];

/// Extract bits operation.
#[derive(Debug, Clone, Copy, Default)]
pub struct ExtractBits;

impl ExtractBits {
    /// Declarative operation specification.
    ///
    /// Laws are declared as explicit `AlgebraicLaw` values on `SPEC`.
    pub const SPEC: OpSpec = OpSpec::composition_inlinable(
        "primitive.bitwise.extract_bits",
        U32_U32_INPUTS,
        U32_OUTPUTS,
        LAWS,
        Self::program,
    );

    /// Build the canonical IR program.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// use vyre::ops::primitive::bitwise::extract_bits::ExtractBits;
    ///
    /// let _expr = Expr::bitand(Expr::shr(Expr::u32(0xff00), Expr::u32(8)), Expr::u32(0xff));
    /// let program = ExtractBits::program();
    /// assert!(!program.entry().is_empty());
    /// ```
    #[must_use]
    pub fn program() -> Program {
        primitive::binary_u32_program(|value, packed| {
            let offset = Expr::bitand(packed.clone(), Expr::u32(31));
            let raw_count = Expr::bitand(Expr::shr(packed, Expr::u32(5)), Expr::u32(31));
            // Clamp count so offset + count <= 32
            let max_count = Expr::sub(Expr::u32(32), offset.clone());
            let count = Expr::select(
                Expr::le(raw_count.clone(), max_count.clone()),
                raw_count,
                max_count,
            );
            // Build mask: (1 << count) - 1, but handle count == 32 specially
            let mask = Expr::select(
                Expr::eq(count.clone(), Expr::u32(32)),
                Expr::u32(0xFFFF_FFFF),
                Expr::sub(Expr::shl(Expr::u32(1), count.clone()), Expr::u32(1)),
            );
            // Zero count returns 0
            Expr::select(
                Expr::eq(count, Expr::u32(0)),
                Expr::u32(0),
                Expr::bitand(Expr::shr(value, offset), mask),
            )
        })
    }
}

// Backend-specific lowering tripwires for extract_bits.

// WGSL lowering marker for `primitive.bitwise.extract_bits`.
//
// Not a stub: this is a zero-overhead Category A marker. `ExtractBits::program`
// builds concrete IR through `core/src/ops/primitive/bitwise/extract_bits/kernel.rs`; `core/src/lower/wgsl/expr.rs` emits WGSL.
// `core/tests/conformance.rs::conformance_all_primitives` verifies
// lowered GPU bytes are bit-exact against the conform CPU reference.
//
// ```wgsl
// _vyre_store_out(idx, select(0u, ((value >> offset) & mask), (count != 0u)));
// ```