1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Conditional mask: returns value when condition is non-zero, else 0.
use crate::ir::{Expr, Program};
use crate::ops::primitive;
use crate::ops::{AlgebraicLaw, OpSpec, U32_OUTPUTS, U32_U32_INPUTS};
pub const LAWS: &[AlgebraicLaw] = &[AlgebraicLaw::Bounded {
lo: 0,
hi: u32::MAX,
}];
/// Select (conditional mask) operation.
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectOp;
impl SelectOp {
/// Declarative operation specification.
///
/// Laws are declared as explicit `AlgebraicLaw` values on `SPEC`.
pub const SPEC: OpSpec = OpSpec::composition_inlinable(
"primitive.compare.select",
U32_U32_INPUTS,
U32_OUTPUTS,
LAWS,
Self::program,
);
/// Build the canonical IR program.
///
/// # Examples
///
/// ```
/// use vyre::ir::Expr;
/// use vyre::ops::primitive::select_op::SelectOp;
///
/// let _expr = Expr::select(Expr::ne(Expr::u32(1), Expr::u32(0)), Expr::u32(7), Expr::u32(0));
/// let program = SelectOp::program();
/// assert!(!program.entry().is_empty());
/// ```
#[must_use]
pub fn program() -> Program {
primitive::binary_u32_program(|value, condition| {
Expr::select(Expr::ne(condition, Expr::u32(0)), value, Expr::u32(0))
})
}
}