cubecl_core/frontend/element/
uint.rs

1use cubecl_ir::{ConstantScalarValue, ExpandElement, Scope, StorageType, UIntKind};
2
3use crate::frontend::{CubePrimitive, CubeType, Numeric};
4use crate::ir::ElemType;
5
6use super::{
7    ExpandElementIntoMut, ExpandElementTyped, Int, IntoMut, IntoRuntime, into_mut_expand_element,
8    into_runtime_expand_element,
9};
10
11macro_rules! declare_uint {
12    ($primitive:ident, $kind:ident) => {
13        impl CubeType for $primitive {
14            type ExpandType = ExpandElementTyped<Self>;
15        }
16
17        impl CubePrimitive for $primitive {
18            fn as_type_native() -> Option<StorageType> {
19                Some(ElemType::UInt(UIntKind::$kind).into())
20            }
21
22            fn from_const_value(value: ConstantScalarValue) -> Self {
23                let ConstantScalarValue::UInt(value, _) = value else {
24                    unreachable!()
25                };
26                value as $primitive
27            }
28        }
29
30        impl IntoRuntime for $primitive {
31            fn __expand_runtime_method(self, scope: &mut Scope) -> ExpandElementTyped<Self> {
32                let elem: ExpandElementTyped<Self> = self.into();
33                into_runtime_expand_element(scope, elem).into()
34            }
35        }
36
37        impl IntoMut for $primitive {
38            fn into_mut(self, _scope: &mut Scope) -> Self {
39                self
40            }
41        }
42
43        impl ExpandElementIntoMut for $primitive {
44            fn elem_into_mut(scope: &mut Scope, elem: ExpandElement) -> ExpandElement {
45                into_mut_expand_element(scope, elem)
46            }
47        }
48
49        impl Numeric for $primitive {
50            fn min_value() -> Self {
51                $primitive::MIN
52            }
53            fn max_value() -> Self {
54                $primitive::MAX
55            }
56        }
57
58        impl Int for $primitive {
59            const BITS: u32 = $primitive::BITS;
60
61            fn new(val: i64) -> Self {
62                val as $primitive
63            }
64        }
65    };
66}
67
68declare_uint!(u8, U8);
69declare_uint!(u16, U16);
70declare_uint!(u32, U32);
71declare_uint!(u64, U64);