Skip to main content

cubecl_core/frontend/container/vector/
ops.rs

1use core::{
2    marker::PhantomData,
3    ops::{Not, Rem},
4};
5use cubecl_ir::{Bitwise, ConstantValue, ElemType, Instruction, Type, UIntKind, UnaryOperands};
6use cubecl_macros::{cube, intrinsic};
7use num_traits::{NumCast, One, ToPrimitive, Zero};
8
9use crate::{
10    self as cubecl,
11    prelude::{
12        ArcTan2, InverseSqrt, IsInf, IsNan, Powf, Powi, SaturatingAdd, SaturatingSub, Trunc,
13    },
14};
15use crate::{prelude::*, unexpanded};
16
17use super::Vector;
18type VectorExpand<E, N> = NativeExpand<Vector<E, N>>;
19
20impl<P, N: Size> core::ops::Add<Self> for Vector<P, N>
21where
22    P: Scalar,
23    P: core::ops::Add<P, Output = P>,
24{
25    type Output = Self;
26
27    fn add(self, rhs: Self) -> Self::Output {
28        Self::new(self.val + rhs.val)
29    }
30}
31
32impl<P, N: Size> core::ops::Sub<Self> for Vector<P, N>
33where
34    P: Scalar,
35    P: core::ops::Sub<P, Output = P>,
36{
37    type Output = Self;
38
39    fn sub(self, rhs: Self) -> Self::Output {
40        Self::new(self.val - rhs.val)
41    }
42}
43
44impl<P, N: Size> core::ops::Mul<Self> for Vector<P, N>
45where
46    P: Scalar,
47    P: core::ops::Mul<P, Output = P>,
48{
49    type Output = Self;
50
51    fn mul(self, rhs: Self) -> Self::Output {
52        Self::new(self.val * rhs.val)
53    }
54}
55
56impl<P, N: Size> core::ops::Div<Self> for Vector<P, N>
57where
58    P: Scalar,
59    P: core::ops::Div<P, Output = P>,
60{
61    type Output = Self;
62
63    fn div(self, rhs: Self) -> Self::Output {
64        Self::new(self.val / rhs.val)
65    }
66}
67
68impl<P, N: Size> core::ops::AddAssign<Self> for Vector<P, N>
69where
70    P: Scalar,
71    P: core::ops::AddAssign,
72{
73    fn add_assign(&mut self, rhs: Self) {
74        self.val += rhs.val;
75    }
76}
77
78impl<P, N: Size> core::ops::SubAssign<Self> for Vector<P, N>
79where
80    P: Scalar,
81    P: core::ops::SubAssign,
82{
83    fn sub_assign(&mut self, rhs: Self) {
84        self.val -= rhs.val;
85    }
86}
87
88impl<P, N: Size> core::ops::DivAssign<Self> for Vector<P, N>
89where
90    P: Scalar,
91    P: core::ops::DivAssign,
92{
93    fn div_assign(&mut self, rhs: Self) {
94        self.val /= rhs.val;
95    }
96}
97
98impl<P, N: Size> core::ops::MulAssign<Self> for Vector<P, N>
99where
100    P: Scalar,
101    P: core::ops::MulAssign,
102{
103    fn mul_assign(&mut self, rhs: Self) {
104        self.val *= rhs.val;
105    }
106}
107
108impl<P, N: Size> core::cmp::PartialEq for Vector<P, N>
109where
110    P: Scalar,
111    P: core::cmp::PartialEq,
112{
113    fn eq(&self, other: &Self) -> bool {
114        self.val.eq(&other.val)
115    }
116}
117
118impl<P, N: Size> core::cmp::PartialOrd for Vector<P, N>
119where
120    P: Scalar,
121    P: core::cmp::PartialOrd,
122{
123    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
124        self.val.partial_cmp(&other.val)
125    }
126}
127
128impl<P, N: Size> core::ops::BitAnd<Self> for Vector<P, N>
129where
130    P: Scalar,
131    P: core::ops::BitAnd<P, Output = P>,
132{
133    type Output = Self;
134
135    fn bitand(self, rhs: Self) -> Self::Output {
136        Self::new(self.val & rhs.val)
137    }
138}
139
140impl<P, N: Size> core::ops::BitOr<Self> for Vector<P, N>
141where
142    P: Scalar,
143    P: core::ops::BitOr<P, Output = P>,
144{
145    type Output = Self;
146
147    fn bitor(self, rhs: Self) -> Self::Output {
148        Self::new(self.val | rhs.val)
149    }
150}
151
152impl<P, N: Size> core::ops::BitXor<Self> for Vector<P, N>
153where
154    P: Scalar,
155    P: core::ops::BitXor<P, Output = P>,
156{
157    type Output = Self;
158
159    fn bitxor(self, rhs: Self) -> Self::Output {
160        Self::new(self.val ^ rhs.val)
161    }
162}
163
164impl<P, N: Size> core::ops::Shl<Self> for Vector<P, N>
165where
166    P: Scalar,
167    P: core::ops::Shl<P, Output = P>,
168{
169    type Output = Self;
170
171    fn shl(self, rhs: Self) -> Self::Output {
172        Self::new(self.val << rhs.val)
173    }
174}
175
176impl<P, N: Size> core::ops::Shr<Self> for Vector<P, N>
177where
178    P: Scalar,
179    P: core::ops::Shr<P, Output = P>,
180{
181    type Output = Self;
182
183    fn shr(self, rhs: Self) -> Self::Output {
184        Self::new(self.val >> rhs.val)
185    }
186}
187
188impl<P, N: Size> core::ops::BitAndAssign<Self> for Vector<P, N>
189where
190    P: Scalar,
191    P: core::ops::BitAndAssign,
192{
193    fn bitand_assign(&mut self, rhs: Self) {
194        self.val &= rhs.val;
195    }
196}
197
198impl<P, N: Size> core::ops::BitOrAssign<Self> for Vector<P, N>
199where
200    P: Scalar,
201    P: core::ops::BitOrAssign,
202{
203    fn bitor_assign(&mut self, rhs: Self) {
204        self.val |= rhs.val;
205    }
206}
207
208impl<P, N: Size> core::ops::BitXorAssign<Self> for Vector<P, N>
209where
210    P: Scalar,
211    P: core::ops::BitXorAssign,
212{
213    fn bitxor_assign(&mut self, rhs: Self) {
214        self.val ^= rhs.val;
215    }
216}
217
218impl<P, N: Size> core::ops::ShlAssign<Self> for Vector<P, N>
219where
220    P: Scalar,
221    P: core::ops::ShlAssign,
222{
223    fn shl_assign(&mut self, rhs: Self) {
224        self.val <<= rhs.val;
225    }
226}
227
228impl<P, N: Size> core::ops::ShrAssign<Self> for Vector<P, N>
229where
230    P: Scalar,
231    P: core::ops::ShrAssign,
232{
233    fn shr_assign(&mut self, rhs: Self) {
234        self.val >>= rhs.val;
235    }
236}
237
238impl<P: Scalar + Abs, N: Size> Abs for Vector<P, N> {}
239impl<P: Scalar + Log, N: Size> Log for Vector<P, N> {}
240impl<P: Scalar + Log1p, N: Size> Log1p for Vector<P, N> {}
241impl<P: Scalar + Expm1, N: Size> Expm1 for Vector<P, N> {}
242impl<P: Scalar + Erf, N: Size> Erf for Vector<P, N> {}
243impl<P: Scalar + Exp, N: Size> Exp for Vector<P, N> {}
244impl<P: Scalar + Powf, N: Size> Powf for Vector<P, N> {}
245impl<P: Scalar + Powi<I>, I: Scalar, N: Size> Powi<Vector<I, N>> for Vector<P, N> {}
246impl<P: Scalar + Sqrt, N: Size> Sqrt for Vector<P, N> {}
247impl<P: Scalar + InverseSqrt, N: Size> InverseSqrt for Vector<P, N> {}
248impl<P: Scalar + Cos, N: Size> Cos for Vector<P, N> {}
249impl<P: Scalar + Sin, N: Size> Sin for Vector<P, N> {}
250impl<P: Scalar + Tan, N: Size> Tan for Vector<P, N> {}
251impl<P: Scalar + Tanh, N: Size> Tanh for Vector<P, N> {}
252impl<P: Scalar + Sinh, N: Size> Sinh for Vector<P, N> {}
253impl<P: Scalar + Cosh, N: Size> Cosh for Vector<P, N> {}
254impl<P: Scalar + ArcSin, N: Size> ArcSin for Vector<P, N> {}
255impl<P: Scalar + ArcCos, N: Size> ArcCos for Vector<P, N> {}
256impl<P: Scalar + ArcTan, N: Size> ArcTan for Vector<P, N> {}
257impl<P: Scalar + ArcSinh, N: Size> ArcSinh for Vector<P, N> {}
258impl<P: Scalar + ArcCosh, N: Size> ArcCosh for Vector<P, N> {}
259impl<P: Scalar + ArcTanh, N: Size> ArcTanh for Vector<P, N> {}
260impl<P: Scalar + ArcTan2, N: Size> ArcTan2 for Vector<P, N> {}
261impl<P: Scalar + Recip, N: Size> Recip for Vector<P, N> {}
262impl<P: Scalar + ModFloor, N: Size> ModFloor for Vector<P, N> {}
263impl<P: Scalar + Round, N: Size> Round for Vector<P, N> {}
264impl<P: Scalar + Floor, N: Size> Floor for Vector<P, N> {}
265impl<P: Scalar + Ceil, N: Size> Ceil for Vector<P, N> {}
266impl<P: Scalar + Trunc, N: Size> Trunc for Vector<P, N> {}
267impl<P: Scalar + ReverseBits, N: Size> ReverseBits for Vector<P, N> {}
268impl<P: Scalar + CubeNot, N: Size> CubeNot for Vector<P, N> {}
269impl<P: Scalar + SaturatingAdd, N: Size> SaturatingAdd for Vector<P, N> {}
270impl<P: Scalar + SaturatingSub, N: Size> SaturatingSub for Vector<P, N> {}
271impl<P: Scalar + IsNan, N: Size> IsNan for Vector<P, N> {}
272impl<P: Scalar + IsInf, N: Size> IsInf for Vector<P, N> {}
273impl<P: Scalar + Normalize, N: Size> Normalize for Vector<P, N> {}
274impl<P: Scalar + Magnitude, N: Size> Magnitude for Vector<P, N> {}
275impl<P: Scalar + VectorSum, N: Size> VectorSum for Vector<P, N> {}
276impl<P: Scalar + Degrees, N: Size> Degrees for Vector<P, N> {}
277impl<P: Scalar + Radians, N: Size> Radians for Vector<P, N> {}
278
279impl<P: Scalar + Rem<Output = P>, N: Size> Rem for Vector<P, N> {
280    type Output = Self;
281
282    fn rem(self, rhs: Self) -> Self::Output {
283        Vector::new(self.val.rem(rhs.val))
284    }
285}
286
287impl<P: Scalar + Ord, N: Size> Ord for Vector<P, N> {
288    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
289        self.val.cmp(&other.val)
290    }
291}
292
293#[cube]
294impl<P: CountOnes + Scalar, N: Size> Vector<P, N> {
295    pub fn count_ones(self) -> Vector<u32, N> {
296        intrinsic!(|scope| {
297            let out_item = Type::scalar(ElemType::UInt(UIntKind::U32))
298                .with_vector_size(self.expand.ty.vector_size());
299            let out = scope.create_value(out_item);
300            scope.register(Instruction::new(
301                Bitwise::CountOnes(UnaryOperands { input: self.expand }),
302                out,
303            ));
304            out.into()
305        })
306    }
307}
308
309impl<P: LeadingZeros + Scalar, N: Size> LeadingZeros for Vector<P, N> {}
310impl<P: FindFirstSet + Scalar, N: Size> FindFirstSet for Vector<P, N> {}
311impl<P: TrailingZeros + Scalar, N: Size> TrailingZeros for Vector<P, N> {}
312
313impl<P: Scalar + NumCast, N: Size> NumCast for Vector<P, N> {
314    fn from<T: num_traits::ToPrimitive>(n: T) -> Option<Self> {
315        let val: P = NumCast::from(n)?;
316        Some(Self {
317            val,
318            _size: PhantomData,
319        })
320    }
321}
322impl<P: Scalar + NumCast, N: Size> ToPrimitive for Vector<P, N> {
323    fn to_i64(&self) -> Option<i64> {
324        self.val.to_i64()
325    }
326
327    fn to_u64(&self) -> Option<u64> {
328        self.val.to_u64()
329    }
330}
331
332impl<P: Not<Output = P> + Scalar, N: Size> Not for Vector<P, N> {
333    type Output = Self;
334
335    fn not(self) -> Self::Output {
336        Vector::new(self.val.not())
337    }
338}
339
340#[allow(clippy::from_over_into)]
341impl<P: Scalar + Into<NativeExpand<P>>, N: Size> Into<NativeExpand<Self>> for Vector<P, N> {
342    fn into(self) -> NativeExpand<Self> {
343        let elem: NativeExpand<P> = self.val.into();
344        elem.expand.into()
345    }
346}
347
348impl<T: Scalar + Default, N: Size> Default for Vector<T, N> {
349    fn default() -> Self {
350        Self::new(T::default())
351    }
352}
353
354impl<T: Scalar + IntoRuntime, N: Size> IntoRuntime for Vector<T, N> {
355    fn __expand_runtime_method(self, scope: &Scope) -> Self::ExpandType {
356        let val = self.val.__expand_runtime_method(scope);
357        Self::__expand_new(scope, val)
358    }
359}
360impl<T: Scalar + IntoExpand, N: Size> IntoExpand for Vector<T, N> {
361    type Expand = VectorExpand<T, N>;
362
363    fn into_expand(self, scope: &Scope) -> Self::Expand {
364        self.__expand_runtime_method(scope)
365    }
366}
367
368impl<T: Scalar + Into<ConstantValue>, N: Size> From<Vector<T, N>> for ConstantValue {
369    fn from(value: Vector<T, N>) -> Self {
370        value.val.into()
371    }
372}
373
374impl<T: Scalar + Zero, N: Size> Zero for Vector<T, N> {
375    fn zero() -> Self {
376        Self::new(T::zero())
377    }
378
379    fn is_zero(&self) -> bool {
380        self.val.is_zero()
381    }
382}
383
384impl<T: Scalar + One, N: Size> One for Vector<T, N> {
385    fn one() -> Self {
386        Self::new(T::one())
387    }
388}
389
390macro_rules! operation_literal {
391    ($lit:ty) => {
392        impl<P, N: Size> core::ops::Add<$lit> for Vector<P, N>
393        where
394            P: Scalar,
395        {
396            type Output = Self;
397
398            fn add(self, _rhs: $lit) -> Self::Output {
399                unexpanded!();
400            }
401        }
402
403        impl<P, N: Size> core::ops::Sub<$lit> for Vector<P, N>
404        where
405            P: Scalar,
406        {
407            type Output = Self;
408
409            fn sub(self, _rhs: $lit) -> Self::Output {
410                unexpanded!();
411            }
412        }
413
414        impl<P, N: Size> core::ops::Mul<$lit> for Vector<P, N>
415        where
416            P: Scalar,
417        {
418            type Output = Self;
419
420            fn mul(self, _rhs: $lit) -> Self::Output {
421                unexpanded!();
422            }
423        }
424
425        impl<P, N: Size> core::ops::Div<$lit> for Vector<P, N>
426        where
427            P: Scalar,
428        {
429            type Output = Self;
430
431            fn div(self, _rhs: $lit) -> Self::Output {
432                unexpanded!();
433            }
434        }
435    };
436}
437
438operation_literal!(f32);
439operation_literal!(f64);
440operation_literal!(usize);
441operation_literal!(i32);
442operation_literal!(i64);