cubecl_ir/
arithmetic.rs

1use core::fmt::Display;
2
3use crate::TypeHash;
4
5use crate::{BinaryOperator, OperationArgs, OperationReflect, UnaryOperator, Variable};
6
7/// Arithmetic operations
8#[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    Tan(UnaryOperator),
29    Tanh(UnaryOperator),
30    Sinh(UnaryOperator),
31    Cosh(UnaryOperator),
32    ArcCos(UnaryOperator),
33    ArcSin(UnaryOperator),
34    ArcTan(UnaryOperator),
35    ArcSinh(UnaryOperator),
36    ArcCosh(UnaryOperator),
37    ArcTanh(UnaryOperator),
38    Degrees(UnaryOperator),
39    Radians(UnaryOperator),
40    ArcTan2(BinaryOperator),
41    Powf(BinaryOperator),
42    Powi(BinaryOperator),
43    Hypot(BinaryOperator),
44    Rhypot(BinaryOperator),
45    Sqrt(UnaryOperator),
46    InverseSqrt(UnaryOperator),
47    Round(UnaryOperator),
48    Floor(UnaryOperator),
49    Ceil(UnaryOperator),
50    Trunc(UnaryOperator),
51    Erf(UnaryOperator),
52    Recip(UnaryOperator),
53    Clamp(ClampOperator),
54    Modulo(BinaryOperator),
55    Neg(UnaryOperator),
56    #[operation(commutative)]
57    Max(BinaryOperator),
58    #[operation(commutative)]
59    Min(BinaryOperator),
60    Remainder(BinaryOperator),
61    Magnitude(UnaryOperator),
62    Normalize(UnaryOperator),
63    #[operation(commutative)]
64    Dot(BinaryOperator),
65    #[operation(commutative)]
66    MulHi(BinaryOperator),
67}
68
69impl Display for Arithmetic {
70    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71        match self {
72            Arithmetic::Add(op) => write!(f, "{} + {}", op.lhs, op.rhs),
73            Arithmetic::SaturatingAdd(op) => write!(f, "saturating_add({}, {})", op.lhs, op.rhs),
74            Arithmetic::Fma(op) => write!(f, "{} * {} + {}", op.a, op.b, op.c),
75            Arithmetic::Sub(op) => write!(f, "{} - {}", op.lhs, op.rhs),
76            Arithmetic::SaturatingSub(op) => write!(f, "saturating_sub({}, {})", op.lhs, op.rhs),
77            Arithmetic::Mul(op) => write!(f, "{} * {}", op.lhs, op.rhs),
78            Arithmetic::Div(op) => write!(f, "{} / {}", op.lhs, op.rhs),
79            Arithmetic::Abs(op) => write!(f, "{}.abs()", op.input),
80            Arithmetic::Exp(op) => write!(f, "{}.exp()", op.input),
81            Arithmetic::Log(op) => write!(f, "{}.log()", op.input),
82            Arithmetic::Log1p(op) => write!(f, "{}.log_1p()", op.input),
83            Arithmetic::Cos(op) => write!(f, "{}.cos()", op.input),
84            Arithmetic::Sin(op) => write!(f, "{}.sin()", op.input),
85            Arithmetic::Tan(op) => write!(f, "{}.tan()", op.input),
86            Arithmetic::Tanh(op) => write!(f, "{}.tanh()", op.input),
87            Arithmetic::Sinh(op) => write!(f, "{}.sinh()", op.input),
88            Arithmetic::Cosh(op) => write!(f, "{}.cosh()", op.input),
89            Arithmetic::ArcCos(op) => write!(f, "{}.acos()", op.input),
90            Arithmetic::ArcSin(op) => write!(f, "{}.asin()", op.input),
91            Arithmetic::ArcTan(op) => write!(f, "{}.atan()", op.input),
92            Arithmetic::ArcSinh(op) => write!(f, "{}.asinh()", op.input),
93            Arithmetic::ArcCosh(op) => write!(f, "{}.acosh()", op.input),
94            Arithmetic::ArcTanh(op) => write!(f, "{}.atanh()", op.input),
95            Arithmetic::Degrees(op) => write!(f, "{}.degrees()", op.input),
96            Arithmetic::Radians(op) => write!(f, "{}.radians()", op.input),
97            Arithmetic::ArcTan2(op) => write!(f, "{}.atan2({})", op.lhs, op.rhs),
98            Arithmetic::Powf(op) => write!(f, "{}.pow({})", op.lhs, op.rhs),
99            Arithmetic::Powi(op) => write!(f, "{}.powi({})", op.lhs, op.rhs),
100            Arithmetic::Hypot(op) => write!(f, "{}.hypot({})", op.lhs, op.rhs),
101            Arithmetic::Rhypot(op) => write!(f, "{}.rhypot({})", op.lhs, op.rhs),
102            Arithmetic::Sqrt(op) => write!(f, "{}.sqrt()", op.input),
103            Arithmetic::InverseSqrt(op) => write!(f, "{}.inverse_sqrt()", op.input),
104            Arithmetic::Round(op) => write!(f, "{}.round()", op.input),
105            Arithmetic::Floor(op) => write!(f, "{}.floor()", op.input),
106            Arithmetic::Ceil(op) => write!(f, "{}.ceil()", op.input),
107            Arithmetic::Trunc(op) => write!(f, "{}.trunc()", op.input),
108            Arithmetic::Erf(op) => write!(f, "{}.erf()", op.input),
109            Arithmetic::Recip(op) => write!(f, "{}.recip()", op.input),
110            Arithmetic::Clamp(op) => {
111                write!(f, "{}.clamp({}, {})", op.input, op.min_value, op.max_value)
112            }
113            Arithmetic::Modulo(op) => write!(f, "{} % {}", op.lhs, op.rhs),
114            Arithmetic::Neg(op) => write!(f, "-{}", op.input),
115            Arithmetic::Max(op) => write!(f, "{}.max({})", op.lhs, op.rhs),
116            Arithmetic::Min(op) => write!(f, "{}.min({})", op.lhs, op.rhs),
117            Arithmetic::Remainder(op) => write!(f, "{} rem {}", op.lhs, op.rhs),
118            Arithmetic::Magnitude(op) => write!(f, "{}.length()", op.input),
119            Arithmetic::Normalize(op) => write!(f, "{}.normalize()", op.input),
120            Arithmetic::Dot(op) => write!(f, "{}.dot({})", op.lhs, op.rhs),
121            Arithmetic::MulHi(op) => write!(f, "mul_hi({}, {})", op.lhs, op.rhs),
122        }
123    }
124}
125
126#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
127#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationArgs)]
128#[allow(missing_docs)]
129pub struct ClampOperator {
130    pub input: Variable,
131    pub min_value: Variable,
132    pub max_value: Variable,
133}
134
135#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
136#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationArgs)]
137#[allow(missing_docs)]
138pub struct ReadGlobalOperator {
139    pub variable: Variable,
140}
141
142#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
143#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash)]
144#[allow(missing_docs)]
145pub struct ReadGlobalWithLayoutOperator {
146    pub variable: Variable,
147    pub tensor_read_pos: usize,
148    pub tensor_layout_pos: usize,
149}
150
151#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
152#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationArgs)]
153#[allow(missing_docs)]
154pub struct FmaOperator {
155    pub a: Variable,
156    pub b: Variable,
157    pub c: Variable,
158}