vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
Documentation
use super::value_enum::Value;

impl Value {
    /// Interpret the value using the IR truth convention.
    #[inline]
    pub fn truthy(&self) -> bool {
        match self {
            Self::Float(v) => v.to_bits() != 0,
            Self::Tensor(t) => !t.data.is_empty(),
            Self::Array(arr) => !arr.is_empty(),
            _ => self.try_as_u32().unwrap_or(1) != 0,
        }
    }

    /// Return this value as little-endian bytes for buffer initialization.
    #[inline]
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            Self::U32(v) => v.to_le_bytes().to_vec(),
            Self::I32(v) => v.to_le_bytes().to_vec(),
            Self::U64(v) => v.to_le_bytes().to_vec(),
            Self::Bool(v) => u32::from(*v).to_le_bytes().to_vec(),
            Self::Bytes(bytes) => bytes.clone(),
            Self::Float(v) => v.to_le_bytes().to_vec(),
            Self::Tensor(t) => t.data.clone(),
            Self::Array(arr) => arr.iter().flat_map(Self::to_bytes).collect(),
        }
    }

    /// Return this value encoded at the declared input width.
    ///
    /// A zero width denotes a variable-length byte payload and preserves the
    /// full value. Fixed-width callers get exact-width little-endian bytes:
    /// shorter payloads are zero-filled and wider payloads are truncated to the
    /// declared type width.
    #[inline]
    pub fn to_bytes_width(&self, declared_width: usize) -> Vec<u8> {
        let mut bytes = self.to_bytes();
        if declared_width == 0 {
            return bytes;
        }
        bytes.resize(declared_width, 0);
        bytes.truncate(declared_width);
        bytes
    }
}