Skip to main content

jeff/reader/optype/
int.rs

1//! Integer operations
2
3use crate::jeff_capnp;
4
5use super::ConstArray;
6
7/// An operation over integers.
8#[derive(Clone, Copy, Debug)]
9#[non_exhaustive]
10pub enum IntOp {
11    /// Create a constant 1 bit integer.
12    Const1(bool),
13    /// Create a constant 8 bit integer.
14    Const8(u8),
15    /// Create a constant 16 bit integer.
16    Const16(u16),
17    /// Create a constant 32 bit integer.
18    Const32(u32),
19    /// Create a constant 64 bit integer.
20    Const64(u64),
21    /// Add two integers.
22    Add,
23    /// Subtract two integers.
24    Sub,
25    /// Multiply two integers.
26    Mul,
27    /// Divide two signed integers.
28    DivS,
29    /// Divide two unsigned integers.
30    DivU,
31    /// Take the power of an integer.
32    Pow,
33    /// Logical bitwise AND.
34    And,
35    /// Logical bitwise OR.
36    Or,
37    /// Logical bitwise XOR.
38    Xor,
39    /// Logical bitwise NOT.
40    Not,
41    /// Minimum of two signed integers.
42    MinS,
43    /// Minimum of two unsigned integers.
44    MinU,
45    /// Maximum of two signed integers.
46    MaxS,
47    /// Maximum of two unsigned integers.
48    MaxU,
49    /// Test two integers for equality.
50    Eq,
51    /// Check if one signed integer is strictly less than another.
52    LtS,
53    /// Check if one signed integer is less than or equal to another.
54    LteS,
55    /// Check if one unsigned integer is strictly less than another.
56    LtU,
57    /// Check if one unsigned integer is less than or equal to another.
58    LteU,
59    /// Take the absolute value of a signed integer.
60    Abs,
61    /// Remainder of a division of two signed integers.
62    RemS,
63    /// Remainder of a division of two unsigned integers.
64    RemU,
65    /// Logical shift left.
66    Shl,
67    /// Logical shift right.
68    Shr,
69}
70
71/// An operation over integer arrays.
72#[derive(Clone, Copy, Debug)]
73#[non_exhaustive]
74pub enum IntArrayOp<'a> {
75    /// Create a constant 1 bit integer array.
76    ConstArray1(ConstArray<'a, bool>),
77    /// Create a constant 8 bit integer array.
78    ConstArray8(ConstArray<'a, u8>),
79    /// Create a constant 16 bit integer array.
80    ConstArray16(ConstArray<'a, u16>),
81    /// Create a constant 32 bit integer array.
82    ConstArray32(ConstArray<'a, u32>),
83    /// Create a constant 64 bit integer array.
84    ConstArray64(ConstArray<'a, u64>),
85    /// Create a zeroed integer array of a given bitwidth with dynamic length.
86    Zero {
87        /// The number of bits in each integer in the array.
88        bits: u8,
89    },
90    /// Get the value of an integer array at a given index.
91    GetIndex,
92    /// Set the value of an integer array at a given index.
93    SetIndex,
94    /// Get the length of an integer array.
95    Length,
96    /// Creates an integer array from a variable number of input values.
97    Create,
98}
99
100impl IntOp {
101    /// Create a new integer operation from a capnp reader.
102    pub(crate) fn read_capnp(int_op: jeff_capnp::int_op::Reader<'_>) -> Self {
103        match int_op.which().expect("Integer operation should be present") {
104            jeff_capnp::int_op::Which::Const1(val) => Self::Const1(val),
105            jeff_capnp::int_op::Which::Const8(val) => Self::Const8(val),
106            jeff_capnp::int_op::Which::Const16(val) => Self::Const16(val),
107            jeff_capnp::int_op::Which::Const32(val) => Self::Const32(val),
108            jeff_capnp::int_op::Which::Const64(val) => Self::Const64(val),
109            jeff_capnp::int_op::Which::Add(()) => Self::Add,
110            jeff_capnp::int_op::Which::Sub(()) => Self::Sub,
111            jeff_capnp::int_op::Which::Mul(()) => Self::Mul,
112            jeff_capnp::int_op::Which::DivS(()) => Self::DivS,
113            jeff_capnp::int_op::Which::DivU(()) => Self::DivU,
114            jeff_capnp::int_op::Which::Pow(()) => Self::Pow,
115            jeff_capnp::int_op::Which::And(()) => Self::And,
116            jeff_capnp::int_op::Which::Or(()) => Self::Or,
117            jeff_capnp::int_op::Which::Xor(()) => Self::Xor,
118            jeff_capnp::int_op::Which::Not(()) => Self::Not,
119            jeff_capnp::int_op::Which::MinS(()) => Self::MinS,
120            jeff_capnp::int_op::Which::MinU(()) => Self::MinU,
121            jeff_capnp::int_op::Which::MaxS(()) => Self::MaxS,
122            jeff_capnp::int_op::Which::MaxU(()) => Self::MaxU,
123            jeff_capnp::int_op::Which::Eq(()) => Self::Eq,
124            jeff_capnp::int_op::Which::LtS(()) => Self::LtS,
125            jeff_capnp::int_op::Which::LteS(()) => Self::LteS,
126            jeff_capnp::int_op::Which::LtU(()) => Self::LtU,
127            jeff_capnp::int_op::Which::LteU(()) => Self::LteU,
128            jeff_capnp::int_op::Which::Abs(()) => Self::Abs,
129            jeff_capnp::int_op::Which::RemS(()) => Self::RemS,
130            jeff_capnp::int_op::Which::RemU(()) => Self::RemU,
131            jeff_capnp::int_op::Which::Shl(()) => Self::Shl,
132            jeff_capnp::int_op::Which::Shr(()) => Self::Shr,
133        }
134    }
135}
136
137impl<'a> IntArrayOp<'a> {
138    /// Create a new integer array operation from a capnp reader.
139    pub(crate) fn read_capnp(int_array_op: jeff_capnp::int_array_op::Reader<'a>) -> Self {
140        match int_array_op
141            .which()
142            .expect("Integer array operation should be present")
143        {
144            jeff_capnp::int_array_op::Which::Const1(val) => Self::ConstArray1(
145                ConstArray::read_capnp(val.expect("Const1 should be present")),
146            ),
147            jeff_capnp::int_array_op::Which::Const8(val) => Self::ConstArray8(
148                ConstArray::read_capnp(val.expect("Const8 should be present")),
149            ),
150            jeff_capnp::int_array_op::Which::Const16(val) => Self::ConstArray16(
151                ConstArray::read_capnp(val.expect("Const16 should be present")),
152            ),
153            jeff_capnp::int_array_op::Which::Const32(val) => Self::ConstArray32(
154                ConstArray::read_capnp(val.expect("Const32 should be present")),
155            ),
156            jeff_capnp::int_array_op::Which::Const64(val) => Self::ConstArray64(
157                ConstArray::read_capnp(val.expect("Const64 should be present")),
158            ),
159            jeff_capnp::int_array_op::Which::Zero(val) => Self::Zero { bits: val },
160            jeff_capnp::int_array_op::Which::GetIndex(()) => Self::GetIndex,
161            jeff_capnp::int_array_op::Which::SetIndex(()) => Self::SetIndex,
162            jeff_capnp::int_array_op::Which::Length(()) => Self::Length,
163            jeff_capnp::int_array_op::Which::Create(()) => Self::Create,
164        }
165    }
166}