cubecl_core/frontend/container/line/
ops.rs1use num_traits::{NumCast, ToPrimitive};
2
3use crate::{
4 frontend::{
5 Abs, Ceil, Clamp, Cos, CubeIndex, CubeIndexMut, CubePrimitive, Erf, Exp,
6 ExpandElementTyped, Floor, Log, Log1p, Max, Min, Powf, Recip, Remainder, Round, Sin, Sqrt,
7 Tanh,
8 },
9 prelude::{CountOnes, ReverseBits},
10 unexpanded,
11};
12
13use super::Line;
14
15impl<P> core::ops::Add<Self> for Line<P>
16where
17 P: CubePrimitive,
18 P: core::ops::Add<P, Output = P>,
19{
20 type Output = Self;
21
22 fn add(self, rhs: Self) -> Self::Output {
23 Self::new(self.val + rhs.val)
24 }
25}
26
27impl<P> core::ops::Sub<Self> for Line<P>
28where
29 P: CubePrimitive,
30 P: core::ops::Sub<P, Output = P>,
31{
32 type Output = Self;
33
34 fn sub(self, rhs: Self) -> Self::Output {
35 Self::new(self.val - rhs.val)
36 }
37}
38
39impl<P> core::ops::Mul<Self> for Line<P>
40where
41 P: CubePrimitive,
42 P: core::ops::Mul<P, Output = P>,
43{
44 type Output = Self;
45
46 fn mul(self, rhs: Self) -> Self::Output {
47 Self::new(self.val * rhs.val)
48 }
49}
50
51impl<P> core::ops::Div<Self> for Line<P>
52where
53 P: CubePrimitive,
54 P: core::ops::Div<P, Output = P>,
55{
56 type Output = Self;
57
58 fn div(self, rhs: Self) -> Self::Output {
59 Self::new(self.val / rhs.val)
60 }
61}
62
63impl<P> core::ops::AddAssign<Self> for Line<P>
64where
65 P: CubePrimitive,
66 P: core::ops::AddAssign,
67{
68 fn add_assign(&mut self, rhs: Self) {
69 self.val += rhs.val;
70 }
71}
72
73impl<P> core::ops::SubAssign<Self> for Line<P>
74where
75 P: CubePrimitive,
76 P: core::ops::SubAssign,
77{
78 fn sub_assign(&mut self, rhs: Self) {
79 self.val -= rhs.val;
80 }
81}
82
83impl<P> core::ops::DivAssign<Self> for Line<P>
84where
85 P: CubePrimitive,
86 P: core::ops::DivAssign,
87{
88 fn div_assign(&mut self, rhs: Self) {
89 self.val /= rhs.val;
90 }
91}
92
93impl<P> core::ops::MulAssign<Self> for Line<P>
94where
95 P: CubePrimitive,
96 P: core::ops::MulAssign,
97{
98 fn mul_assign(&mut self, rhs: Self) {
99 self.val *= rhs.val;
100 }
101}
102
103impl<P> core::cmp::PartialEq for Line<P>
104where
105 P: CubePrimitive,
106 P: core::cmp::PartialEq,
107{
108 fn eq(&self, other: &Self) -> bool {
109 self.val.eq(&other.val)
110 }
111}
112
113impl<P> core::cmp::PartialOrd for Line<P>
114where
115 P: CubePrimitive,
116 P: core::cmp::PartialOrd,
117{
118 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
119 self.val.partial_cmp(&other.val)
120 }
121}
122
123impl<P> core::ops::BitAnd<Self> for Line<P>
124where
125 P: CubePrimitive,
126 P: core::ops::BitAnd<P, Output = P>,
127{
128 type Output = Self;
129
130 fn bitand(self, rhs: Self) -> Self::Output {
131 Self::new(self.val & rhs.val)
132 }
133}
134
135impl<P> core::ops::BitOr<Self> for Line<P>
136where
137 P: CubePrimitive,
138 P: core::ops::BitOr<P, Output = P>,
139{
140 type Output = Self;
141
142 fn bitor(self, rhs: Self) -> Self::Output {
143 Self::new(self.val | rhs.val)
144 }
145}
146
147impl<P> core::ops::BitXor<Self> for Line<P>
148where
149 P: CubePrimitive,
150 P: core::ops::BitXor<P, Output = P>,
151{
152 type Output = Self;
153
154 fn bitxor(self, rhs: Self) -> Self::Output {
155 Self::new(self.val ^ rhs.val)
156 }
157}
158
159impl<P> core::ops::Not for Line<P>
160where
161 P: CubePrimitive,
162 P: core::ops::Not<Output = P>,
163{
164 type Output = Self;
165
166 fn not(self) -> Self::Output {
167 Self::new(!self.val)
168 }
169}
170
171impl<P> core::ops::Shl<Self> for Line<P>
172where
173 P: CubePrimitive,
174 P: core::ops::Shl<P, Output = P>,
175{
176 type Output = Self;
177
178 fn shl(self, rhs: Self) -> Self::Output {
179 Self::new(self.val << rhs.val)
180 }
181}
182
183impl<P> core::ops::Shr<Self> for Line<P>
184where
185 P: CubePrimitive,
186 P: core::ops::Shr<P, Output = P>,
187{
188 type Output = Self;
189
190 fn shr(self, rhs: Self) -> Self::Output {
191 Self::new(self.val >> rhs.val)
192 }
193}
194
195impl<P> core::ops::BitAndAssign<Self> for Line<P>
196where
197 P: CubePrimitive,
198 P: core::ops::BitAndAssign,
199{
200 fn bitand_assign(&mut self, rhs: Self) {
201 self.val &= rhs.val;
202 }
203}
204
205impl<P> core::ops::BitOrAssign<Self> for Line<P>
206where
207 P: CubePrimitive,
208 P: core::ops::BitOrAssign,
209{
210 fn bitor_assign(&mut self, rhs: Self) {
211 self.val |= rhs.val;
212 }
213}
214
215impl<P> core::ops::BitXorAssign<Self> for Line<P>
216where
217 P: CubePrimitive,
218 P: core::ops::BitXorAssign,
219{
220 fn bitxor_assign(&mut self, rhs: Self) {
221 self.val ^= rhs.val;
222 }
223}
224
225impl<P> core::ops::ShlAssign<Self> for Line<P>
226where
227 P: CubePrimitive,
228 P: core::ops::ShlAssign,
229{
230 fn shl_assign(&mut self, rhs: Self) {
231 self.val <<= rhs.val;
232 }
233}
234
235impl<P> core::ops::ShrAssign<Self> for Line<P>
236where
237 P: CubePrimitive,
238 P: core::ops::ShrAssign,
239{
240 fn shr_assign(&mut self, rhs: Self) {
241 self.val >>= rhs.val;
242 }
243}
244
245impl<P: CubePrimitive + Abs> Abs for Line<P> {}
246impl<P: CubePrimitive + Max> Max for Line<P> {}
247impl<P: CubePrimitive + Min> Min for Line<P> {}
248impl<P: CubePrimitive + Clamp> Clamp for Line<P> {}
249impl<P: CubePrimitive + Log> Log for Line<P> {}
250impl<P: CubePrimitive + Log1p> Log1p for Line<P> {}
251impl<P: CubePrimitive + Erf> Erf for Line<P> {}
252impl<P: CubePrimitive + Exp> Exp for Line<P> {}
253impl<P: CubePrimitive + Powf> Powf for Line<P> {}
254impl<P: CubePrimitive + Sqrt> Sqrt for Line<P> {}
255impl<P: CubePrimitive + Cos> Cos for Line<P> {}
256impl<P: CubePrimitive + Sin> Sin for Line<P> {}
257impl<P: CubePrimitive + Tanh> Tanh for Line<P> {}
258impl<P: CubePrimitive + Recip> Recip for Line<P> {}
259impl<P: CubePrimitive + Remainder> Remainder for Line<P> {}
260impl<P: CubePrimitive + Round> Round for Line<P> {}
261impl<P: CubePrimitive + Floor> Floor for Line<P> {}
262impl<P: CubePrimitive + Ceil> Ceil for Line<P> {}
263impl<P: CubePrimitive + CountOnes> CountOnes for Line<P> {}
264impl<P: CubePrimitive + ReverseBits> ReverseBits for Line<P> {}
265
266impl<P: CubePrimitive + NumCast> NumCast for Line<P> {
267 fn from<T: num_traits::ToPrimitive>(n: T) -> Option<Self> {
268 let val: P = NumCast::from(n)?;
269 Some(Self { val })
270 }
271}
272impl<P: CubePrimitive + NumCast> ToPrimitive for Line<P> {
273 fn to_i64(&self) -> Option<i64> {
274 self.val.to_i64()
275 }
276
277 fn to_u64(&self) -> Option<u64> {
278 self.val.to_u64()
279 }
280}
281
282impl<P> CubeIndex<u32> for Line<P>
283where
284 P: CubePrimitive + CubeIndex<u32, Output = P>,
285{
286 type Output = P;
287
288 fn cube_idx(&self, _i: u32) -> &Self::Output {
289 unexpanded!()
290 }
291}
292
293impl<P> CubeIndexMut<u32> for Line<P>
294where
295 P: CubePrimitive + CubeIndexMut<u32, Output = P>,
296{
297 fn cube_idx_mut(&mut self, _i: u32) -> &mut Self::Output {
298 unexpanded!()
299 }
300}
301
302impl<P> CubeIndex<ExpandElementTyped<u32>> for Line<P>
303where
304 P: CubePrimitive + CubeIndex<ExpandElementTyped<u32>, Output = P>,
305{
306 type Output = P;
307
308 fn cube_idx(&self, _i: ExpandElementTyped<u32>) -> &Self::Output {
309 unexpanded!()
310 }
311}
312
313impl<P> CubeIndexMut<ExpandElementTyped<u32>> for Line<P>
314where
315 P: CubePrimitive + CubeIndexMut<ExpandElementTyped<u32>, Output = P>,
316{
317 fn cube_idx_mut(&mut self, _i: ExpandElementTyped<u32>) -> &mut Self::Output {
318 unexpanded!()
319 }
320}
321
322#[allow(clippy::from_over_into)]
323impl<P: CubePrimitive + Into<ExpandElementTyped<P>>> Into<ExpandElementTyped<Self>> for Line<P> {
324 fn into(self) -> ExpandElementTyped<Self> {
325 let elem: ExpandElementTyped<P> = self.val.into();
326 elem.expand.into()
327 }
328}
329
330macro_rules! operation_literal {
331 ($lit:ty) => {
332 impl<P> core::ops::Add<$lit> for Line<P>
333 where
334 P: CubePrimitive,
335 {
336 type Output = Self;
337
338 fn add(self, _rhs: $lit) -> Self::Output {
339 unexpanded!();
340 }
341 }
342
343 impl<P> core::ops::Sub<$lit> for Line<P>
344 where
345 P: CubePrimitive,
346 {
347 type Output = Self;
348
349 fn sub(self, _rhs: $lit) -> Self::Output {
350 unexpanded!();
351 }
352 }
353
354 impl<P> core::ops::Mul<$lit> for Line<P>
355 where
356 P: CubePrimitive,
357 {
358 type Output = Self;
359
360 fn mul(self, _rhs: $lit) -> Self::Output {
361 unexpanded!();
362 }
363 }
364
365 impl<P> core::ops::Div<$lit> for Line<P>
366 where
367 P: CubePrimitive,
368 {
369 type Output = Self;
370
371 fn div(self, _rhs: $lit) -> Self::Output {
372 unexpanded!();
373 }
374 }
375 };
376}
377
378operation_literal!(f32);
379operation_literal!(f64);
380operation_literal!(usize);
381operation_literal!(i32);
382operation_literal!(i64);