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