Skip to main content

jeff/reader/optype/
float.rs

1//! Floating point operations
2
3use super::ConstArray;
4use crate::jeff_capnp;
5use crate::types::FloatPrecision;
6
7/// An operation over floating point numbers.
8#[derive(Clone, Copy, Debug)]
9#[non_exhaustive]
10pub enum FloatOp {
11    /// Create a constant 32 bit float.
12    Const32(f32),
13    /// Create a constant 64 bit float.
14    Const64(f64),
15    /// Add two floats.
16    Add,
17    /// Subtract two floats.
18    Sub,
19    /// Multiply two floats.
20    Mul,
21    /// Divide two floats.
22    Div,
23    /// Calculate one float raised to the power of another.
24    Pow,
25    /// Test two floats for equality.
26    Eq,
27    /// Check if one float is strictly less than another.
28    Lt,
29    /// Check if one float is less than or equal to another.
30    Lte,
31    /// Calculate the square root of a float.
32    Sqrt,
33    /// Calculate the absolute value of a float.
34    Abs,
35    /// Round a float up to the nearest integer.
36    Ceil,
37    /// Round a float down to the nearest integer.
38    Floor,
39    /// Check if a float is NaN.
40    IsNan,
41    /// Check if a float is infinite.
42    IsInf,
43    /// Calculate e raised to the power of a float.
44    Exp,
45    /// Calculate the natural logarithm of a float.
46    Log,
47    /// Calculate the sine of a float.
48    Sin,
49    /// Calculate the cosine of a float.
50    Cos,
51    /// Calculate the tangent of a float.
52    Tan,
53    /// Calculate the arcsine of a float.
54    Asin,
55    /// Calculate the arccosine of a float.
56    Acos,
57    /// Calculate the arctangent of a float.
58    Atan,
59    /// Calculate the 2-argument arctangent.
60    Atan2,
61    /// Calculate the hyperbolic sine of a float.
62    Sinh,
63    /// Calculate the hyperbolic cosine of a float.
64    Cosh,
65    /// Calculate the hyperbolic tangent of a float.
66    Tanh,
67    /// Calculate the inverse hyperbolic sine of a float.
68    Asinh,
69    /// Calculate the inverse hyperbolic cosine of a float.
70    Acosh,
71    /// Calculate the inverse hyperbolic tangent of a float.
72    Atanh,
73    /// Maximum of two floats.
74    Max,
75    /// Minimum of two floats.
76    Min,
77    /// Select between two floats based on a condition mask.
78    Select,
79    /// Extend floating-point precision to a wider float type.
80    Ext,
81    /// Truncate floating-point precision to a narrower float type.
82    Trunc,
83    /// Convert a float to a signed integer.
84    ToSInt,
85    /// Convert a float to an unsigned integer.
86    ToUInt,
87}
88
89/// An operation over floating point arrays.
90#[derive(Clone, Copy, Debug)]
91#[non_exhaustive]
92pub enum FloatArrayOp<'a> {
93    /// Create a constant 32 bit float array.
94    Const32(ConstArray<'a, f32>),
95    /// Create a constant 64 bit float array.
96    Const64(ConstArray<'a, f64>),
97    /// Create a zeroed float array of a given precision with dynamic length.
98    Zero {
99        /// The precision of the floats in the array.
100        precision: FloatPrecision,
101    },
102    /// Get the value of a float array at a given index.
103    GetIndex,
104    /// Set the value of a float array at a given index.
105    SetIndex,
106    /// Get the length of a float array.
107    Length,
108    /// Creates a float array from a variable number of input values.
109    Create,
110}
111
112impl FloatOp {
113    /// Create a new floating point operation from a capnp reader.
114    pub(crate) fn read_capnp(float_op: jeff_capnp::float_op::Reader<'_>) -> Self {
115        match float_op.which().expect("Float operation should be present") {
116            jeff_capnp::float_op::Which::Const32(val) => Self::Const32(val),
117            jeff_capnp::float_op::Which::Const64(val) => Self::Const64(val),
118            jeff_capnp::float_op::Which::Add(()) => Self::Add,
119            jeff_capnp::float_op::Which::Sub(()) => Self::Sub,
120            jeff_capnp::float_op::Which::Mul(()) => Self::Mul,
121            jeff_capnp::float_op::Which::Div(()) => Self::Div,
122            jeff_capnp::float_op::Which::Pow(()) => Self::Pow,
123            jeff_capnp::float_op::Which::Eq(()) => Self::Eq,
124            jeff_capnp::float_op::Which::Lt(()) => Self::Lt,
125            jeff_capnp::float_op::Which::Lte(()) => Self::Lte,
126            jeff_capnp::float_op::Which::Sqrt(()) => Self::Sqrt,
127            jeff_capnp::float_op::Which::Abs(()) => Self::Abs,
128            jeff_capnp::float_op::Which::Ceil(()) => Self::Ceil,
129            jeff_capnp::float_op::Which::Floor(()) => Self::Floor,
130            jeff_capnp::float_op::Which::IsNan(()) => Self::IsNan,
131            jeff_capnp::float_op::Which::IsInf(()) => Self::IsInf,
132            jeff_capnp::float_op::Which::Exp(()) => Self::Exp,
133            jeff_capnp::float_op::Which::Log(()) => Self::Log,
134            jeff_capnp::float_op::Which::Sin(()) => Self::Sin,
135            jeff_capnp::float_op::Which::Cos(()) => Self::Cos,
136            jeff_capnp::float_op::Which::Tan(()) => Self::Tan,
137            jeff_capnp::float_op::Which::Asin(()) => Self::Asin,
138            jeff_capnp::float_op::Which::Acos(()) => Self::Acos,
139            jeff_capnp::float_op::Which::Atan(()) => Self::Atan,
140            jeff_capnp::float_op::Which::Atan2(()) => Self::Atan2,
141            jeff_capnp::float_op::Which::Sinh(()) => Self::Sinh,
142            jeff_capnp::float_op::Which::Cosh(()) => Self::Cosh,
143            jeff_capnp::float_op::Which::Tanh(()) => Self::Tanh,
144            jeff_capnp::float_op::Which::Asinh(()) => Self::Asinh,
145            jeff_capnp::float_op::Which::Acosh(()) => Self::Acosh,
146            jeff_capnp::float_op::Which::Atanh(()) => Self::Atanh,
147            jeff_capnp::float_op::Which::Max(()) => Self::Max,
148            jeff_capnp::float_op::Which::Min(()) => Self::Min,
149            jeff_capnp::float_op::Which::Select(()) => Self::Select,
150            jeff_capnp::float_op::Which::Ext(()) => Self::Ext,
151            jeff_capnp::float_op::Which::Trunc(()) => Self::Trunc,
152            jeff_capnp::float_op::Which::ToSInt(()) => Self::ToSInt,
153            jeff_capnp::float_op::Which::ToUInt(()) => Self::ToUInt,
154        }
155    }
156}
157
158impl<'a> FloatArrayOp<'a> {
159    /// Create a new floating point array operation from a capnp reader.
160    pub(crate) fn read_capnp(float_array_op: jeff_capnp::float_array_op::Reader<'a>) -> Self {
161        match float_array_op
162            .which()
163            .expect("Float array operation should be present")
164        {
165            jeff_capnp::float_array_op::Which::Const32(val) => Self::Const32(
166                ConstArray::read_capnp(val.expect("Const32 should be present")),
167            ),
168            jeff_capnp::float_array_op::Which::Const64(val) => Self::Const64(
169                ConstArray::read_capnp(val.expect("Const64 should be present")),
170            ),
171            jeff_capnp::float_array_op::Which::Zero(precision) => Self::Zero {
172                precision: FloatPrecision::from_capnp(
173                    precision.expect("Precision should be present"),
174                ),
175            },
176            jeff_capnp::float_array_op::Which::GetIndex(()) => Self::GetIndex,
177            jeff_capnp::float_array_op::Which::SetIndex(()) => Self::SetIndex,
178            jeff_capnp::float_array_op::Which::Length(()) => Self::Length,
179            jeff_capnp::float_array_op::Which::Create(()) => Self::Create,
180        }
181    }
182}