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    /// Select between two integers based on a condition mask.
70    Select,
71    /// Sign-extend an integer to a wider integer type.
72    ExtS,
73    /// Zero-extend an integer to a wider integer type.
74    ExtU,
75    /// Truncate an integer to a narrower integer type.
76    Trunc,
77    /// Convert a signed integer to a float.
78    ToFloatS,
79    /// Convert an unsigned integer to a float.
80    ToFloatU,
81}
82
83/// An operation over integer arrays.
84#[derive(Clone, Copy, Debug)]
85#[non_exhaustive]
86pub enum IntArrayOp<'a> {
87    /// Create a constant 1 bit integer array.
88    ConstArray1(ConstArray<'a, bool>),
89    /// Create a constant 8 bit integer array.
90    ConstArray8(ConstArray<'a, u8>),
91    /// Create a constant 16 bit integer array.
92    ConstArray16(ConstArray<'a, u16>),
93    /// Create a constant 32 bit integer array.
94    ConstArray32(ConstArray<'a, u32>),
95    /// Create a constant 64 bit integer array.
96    ConstArray64(ConstArray<'a, u64>),
97    /// Create a zeroed integer array of a given bitwidth with dynamic length.
98    Zero {
99        /// The number of bits in each integer in the array.
100        bits: u8,
101    },
102    /// Get the value of an integer array at a given index.
103    GetIndex,
104    /// Set the value of an integer array at a given index.
105    SetIndex,
106    /// Get the length of an integer array.
107    Length,
108    /// Creates an integer array from a variable number of input values.
109    Create,
110}
111
112impl IntOp {
113    /// Create a new integer operation from a capnp reader.
114    pub(crate) fn read_capnp(int_op: jeff_capnp::int_op::Reader<'_>) -> Self {
115        match int_op.which().expect("Integer operation should be present") {
116            jeff_capnp::int_op::Which::Const1(val) => Self::Const1(val),
117            jeff_capnp::int_op::Which::Const8(val) => Self::Const8(val),
118            jeff_capnp::int_op::Which::Const16(val) => Self::Const16(val),
119            jeff_capnp::int_op::Which::Const32(val) => Self::Const32(val),
120            jeff_capnp::int_op::Which::Const64(val) => Self::Const64(val),
121            jeff_capnp::int_op::Which::Add(()) => Self::Add,
122            jeff_capnp::int_op::Which::Sub(()) => Self::Sub,
123            jeff_capnp::int_op::Which::Mul(()) => Self::Mul,
124            jeff_capnp::int_op::Which::DivS(()) => Self::DivS,
125            jeff_capnp::int_op::Which::DivU(()) => Self::DivU,
126            jeff_capnp::int_op::Which::Pow(()) => Self::Pow,
127            jeff_capnp::int_op::Which::And(()) => Self::And,
128            jeff_capnp::int_op::Which::Or(()) => Self::Or,
129            jeff_capnp::int_op::Which::Xor(()) => Self::Xor,
130            jeff_capnp::int_op::Which::Not(()) => Self::Not,
131            jeff_capnp::int_op::Which::MinS(()) => Self::MinS,
132            jeff_capnp::int_op::Which::MinU(()) => Self::MinU,
133            jeff_capnp::int_op::Which::MaxS(()) => Self::MaxS,
134            jeff_capnp::int_op::Which::MaxU(()) => Self::MaxU,
135            jeff_capnp::int_op::Which::Eq(()) => Self::Eq,
136            jeff_capnp::int_op::Which::LtS(()) => Self::LtS,
137            jeff_capnp::int_op::Which::LteS(()) => Self::LteS,
138            jeff_capnp::int_op::Which::LtU(()) => Self::LtU,
139            jeff_capnp::int_op::Which::LteU(()) => Self::LteU,
140            jeff_capnp::int_op::Which::Abs(()) => Self::Abs,
141            jeff_capnp::int_op::Which::RemS(()) => Self::RemS,
142            jeff_capnp::int_op::Which::RemU(()) => Self::RemU,
143            jeff_capnp::int_op::Which::Shl(()) => Self::Shl,
144            jeff_capnp::int_op::Which::Shr(()) => Self::Shr,
145            jeff_capnp::int_op::Which::Select(()) => Self::Select,
146            jeff_capnp::int_op::Which::ExtS(()) => Self::ExtS,
147            jeff_capnp::int_op::Which::ExtU(()) => Self::ExtU,
148            jeff_capnp::int_op::Which::Trunc(()) => Self::Trunc,
149            jeff_capnp::int_op::Which::ToFloatS(()) => Self::ToFloatS,
150            jeff_capnp::int_op::Which::ToFloatU(()) => Self::ToFloatU,
151        }
152    }
153}
154
155impl<'a> IntArrayOp<'a> {
156    /// Create a new integer array operation from a capnp reader.
157    pub(crate) fn read_capnp(int_array_op: jeff_capnp::int_array_op::Reader<'a>) -> Self {
158        match int_array_op
159            .which()
160            .expect("Integer array operation should be present")
161        {
162            jeff_capnp::int_array_op::Which::Const1(val) => Self::ConstArray1(
163                ConstArray::read_capnp(val.expect("Const1 should be present")),
164            ),
165            jeff_capnp::int_array_op::Which::Const8(val) => Self::ConstArray8(
166                ConstArray::read_capnp(val.expect("Const8 should be present")),
167            ),
168            jeff_capnp::int_array_op::Which::Const16(val) => Self::ConstArray16(
169                ConstArray::read_capnp(val.expect("Const16 should be present")),
170            ),
171            jeff_capnp::int_array_op::Which::Const32(val) => Self::ConstArray32(
172                ConstArray::read_capnp(val.expect("Const32 should be present")),
173            ),
174            jeff_capnp::int_array_op::Which::Const64(val) => Self::ConstArray64(
175                ConstArray::read_capnp(val.expect("Const64 should be present")),
176            ),
177            jeff_capnp::int_array_op::Which::Zero(val) => Self::Zero { bits: val },
178            jeff_capnp::int_array_op::Which::GetIndex(()) => Self::GetIndex,
179            jeff_capnp::int_array_op::Which::SetIndex(()) => Self::SetIndex,
180            jeff_capnp::int_array_op::Which::Length(()) => Self::Length,
181            jeff_capnp::int_array_op::Which::Create(()) => Self::Create,
182        }
183    }
184}