1use core::fmt::Display;
2
3use crate::TypeHash;
4
5use crate::{BinaryOperator, OperationArgs, OperationReflect, UnaryOperator, Variable};
6
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationReflect)]
10#[operation(opcode_name = ArithmeticOpCode, pure)]
11pub enum Arithmetic {
12 #[operation(commutative)]
13 Add(BinaryOperator),
14 #[operation(commutative)]
15 SaturatingAdd(BinaryOperator),
16 Fma(FmaOperator),
17 Sub(BinaryOperator),
18 SaturatingSub(BinaryOperator),
19 #[operation(commutative)]
20 Mul(BinaryOperator),
21 Div(BinaryOperator),
22 Abs(UnaryOperator),
23 Exp(UnaryOperator),
24 Log(UnaryOperator),
25 Log1p(UnaryOperator),
26 Cos(UnaryOperator),
27 Sin(UnaryOperator),
28 Tanh(UnaryOperator),
29 Powf(BinaryOperator),
30 Powi(BinaryOperator),
31 Sqrt(UnaryOperator),
32 InverseSqrt(UnaryOperator),
33 Round(UnaryOperator),
34 Floor(UnaryOperator),
35 Ceil(UnaryOperator),
36 Trunc(UnaryOperator),
37 Erf(UnaryOperator),
38 Recip(UnaryOperator),
39 Clamp(ClampOperator),
40 Modulo(BinaryOperator),
41 Neg(UnaryOperator),
42 #[operation(commutative)]
43 Max(BinaryOperator),
44 #[operation(commutative)]
45 Min(BinaryOperator),
46 Remainder(BinaryOperator),
47 Magnitude(UnaryOperator),
48 Normalize(UnaryOperator),
49 #[operation(commutative)]
50 Dot(BinaryOperator),
51 #[operation(commutative)]
52 MulHi(BinaryOperator),
53}
54
55impl Display for Arithmetic {
56 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57 match self {
58 Arithmetic::Add(op) => write!(f, "{} + {}", op.lhs, op.rhs),
59 Arithmetic::SaturatingAdd(op) => write!(f, "saturating_add({}, {})", op.lhs, op.rhs),
60 Arithmetic::Fma(op) => write!(f, "{} * {} + {}", op.a, op.b, op.c),
61 Arithmetic::Sub(op) => write!(f, "{} - {}", op.lhs, op.rhs),
62 Arithmetic::SaturatingSub(op) => write!(f, "saturating_sub({}, {})", op.lhs, op.rhs),
63 Arithmetic::Mul(op) => write!(f, "{} * {}", op.lhs, op.rhs),
64 Arithmetic::Div(op) => write!(f, "{} / {}", op.lhs, op.rhs),
65 Arithmetic::Abs(op) => write!(f, "{}.abs()", op.input),
66 Arithmetic::Exp(op) => write!(f, "{}.exp()", op.input),
67 Arithmetic::Log(op) => write!(f, "{}.log()", op.input),
68 Arithmetic::Log1p(op) => write!(f, "{}.log_1p()", op.input),
69 Arithmetic::Cos(op) => write!(f, "{}.cos()", op.input),
70 Arithmetic::Sin(op) => write!(f, "{}.sin()", op.input),
71 Arithmetic::Tanh(op) => write!(f, "{}.tanh()", op.input),
72 Arithmetic::Powf(op) => write!(f, "{}.powf({})", op.lhs, op.rhs),
73 Arithmetic::Powi(op) => write!(f, "{}.powi({})", op.lhs, op.rhs),
74 Arithmetic::Sqrt(op) => write!(f, "{}.sqrt()", op.input),
75 Arithmetic::InverseSqrt(op) => write!(f, "{}.inverse_sqrt()", op.input),
76 Arithmetic::Round(op) => write!(f, "{}.round()", op.input),
77 Arithmetic::Floor(op) => write!(f, "{}.floor()", op.input),
78 Arithmetic::Ceil(op) => write!(f, "{}.ceil()", op.input),
79 Arithmetic::Trunc(op) => write!(f, "{}.trunc()", op.input),
80 Arithmetic::Erf(op) => write!(f, "{}.erf()", op.input),
81 Arithmetic::Recip(op) => write!(f, "{}.recip()", op.input),
82 Arithmetic::Clamp(op) => {
83 write!(f, "{}.clamp({}, {})", op.input, op.min_value, op.max_value)
84 }
85 Arithmetic::Modulo(op) => write!(f, "{} % {}", op.lhs, op.rhs),
86 Arithmetic::Neg(op) => write!(f, "-{}", op.input),
87 Arithmetic::Max(op) => write!(f, "{}.max({})", op.lhs, op.rhs),
88 Arithmetic::Min(op) => write!(f, "{}.min({})", op.lhs, op.rhs),
89 Arithmetic::Remainder(op) => write!(f, "{} rem {}", op.lhs, op.rhs),
90 Arithmetic::Magnitude(op) => write!(f, "{}.length()", op.input),
91 Arithmetic::Normalize(op) => write!(f, "{}.normalize()", op.input),
92 Arithmetic::Dot(op) => write!(f, "{}.dot({})", op.lhs, op.rhs),
93 Arithmetic::MulHi(op) => write!(f, "mul_hi({}, {})", op.lhs, op.rhs),
94 }
95 }
96}
97
98#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationArgs)]
100#[allow(missing_docs)]
101pub struct ClampOperator {
102 pub input: Variable,
103 pub min_value: Variable,
104 pub max_value: Variable,
105}
106
107#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
108#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationArgs)]
109#[allow(missing_docs)]
110pub struct ReadGlobalOperator {
111 pub variable: Variable,
112}
113
114#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
115#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash)]
116#[allow(missing_docs)]
117pub struct ReadGlobalWithLayoutOperator {
118 pub variable: Variable,
119 pub tensor_read_pos: usize,
120 pub tensor_layout_pos: usize,
121}
122
123#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
124#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationArgs)]
125#[allow(missing_docs)]
126pub struct FmaOperator {
127 pub a: Variable,
128 pub b: Variable,
129 pub c: Variable,
130}