vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
use crate::ir::model::expr::Expr;
use crate::ir::model::types::{BinOp, UnOp};

impl Expr {
    /// `a + b`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::add(Expr::u32(1), Expr::u32(2));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn add(left: Self, right: Self) -> Self {
        Self::BinOp {
            op: BinOp::Add,
            left: Box::new(left),
            right: Box::new(right),
        }
    }

    /// `a - b`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::sub(Expr::u32(2), Expr::u32(1));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn sub(left: Self, right: Self) -> Self {
        Self::BinOp {
            op: BinOp::Sub,
            left: Box::new(left),
            right: Box::new(right),
        }
    }

    /// `a * b`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::mul(Expr::u32(2), Expr::u32(3));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn mul(left: Self, right: Self) -> Self {
        Self::BinOp {
            op: BinOp::Mul,
            left: Box::new(left),
            right: Box::new(right),
        }
    }

    /// `a / b` (division, zero divisor returns 0)
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::div(Expr::u32(10), Expr::u32(2));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn div(left: Self, right: Self) -> Self {
        Self::BinOp {
            op: BinOp::Div,
            left: Box::new(left),
            right: Box::new(right),
        }
    }

    /// `a % b` (remainder, zero divisor returns 0)
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::rem(Expr::u32(10), Expr::u32(3));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn rem(left: Self, right: Self) -> Self {
        Self::BinOp {
            op: BinOp::Mod,
            left: Box::new(left),
            right: Box::new(right),
        }
    }

    /// Twos complement negation.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::negate(Expr::i32(1));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn negate(operand: Self) -> Self {
        Self::UnOp {
            op: UnOp::Negate,
            operand: Box::new(operand),
        }
    }

    /// `abs_diff(a, b)` — unsigned absolute difference.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::abs_diff(Expr::u32(3), Expr::u32(5));
    /// ```
    #[must_use]
    #[inline(always)]
    pub fn abs_diff(left: Self, right: Self) -> Self {
        Self::BinOp {
            op: BinOp::AbsDiff,
            left: Box::new(left),
            right: Box::new(right),
        }
    }
}