cubecl_core/frontend/element/float/
relaxed.rs

1use cubecl_common::flex32;
2use cubecl_ir::{Elem, ExpandElement, FloatKind, Scope};
3
4use crate::prelude::{Numeric, into_runtime_expand_element};
5
6use super::{
7    CubePrimitive, CubeType, ExpandElementIntoMut, ExpandElementTyped, Float, IntoRuntime,
8    KernelBuilder, KernelLauncher, LaunchArgExpand, Runtime, ScalarArgSettings,
9    into_mut_expand_element,
10};
11
12impl CubeType for flex32 {
13    type ExpandType = ExpandElementTyped<flex32>;
14}
15
16impl CubePrimitive for flex32 {
17    /// Return the element type to use on GPU
18    fn as_elem_native() -> Option<Elem> {
19        Some(Elem::Float(FloatKind::Flex32))
20    }
21}
22
23impl IntoRuntime for flex32 {
24    fn __expand_runtime_method(self, scope: &mut Scope) -> ExpandElementTyped<Self> {
25        let elem: ExpandElementTyped<Self> = self.into();
26        into_runtime_expand_element(scope, elem).into()
27    }
28}
29
30impl Numeric for flex32 {
31    fn min_value() -> Self {
32        <Self as num_traits::Float>::min_value()
33    }
34    fn max_value() -> Self {
35        <Self as num_traits::Float>::max_value()
36    }
37}
38
39impl ExpandElementIntoMut for flex32 {
40    fn elem_into_mut(scope: &mut Scope, elem: ExpandElement) -> ExpandElement {
41        into_mut_expand_element(scope, elem)
42    }
43}
44
45impl Float for flex32 {
46    const DIGITS: u32 = 32;
47
48    const EPSILON: Self = flex32::from_f32(half::f16::EPSILON.to_f32_const());
49
50    const INFINITY: Self = flex32::from_f32(f32::INFINITY);
51
52    const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS;
53
54    /// Maximum possible [`flex32`](crate::frontend::flex32) power of 10 exponent
55    const MAX_10_EXP: i32 = f32::MAX_10_EXP;
56    /// Maximum possible [`flex32`](crate::frontend::flex32) power of 2 exponent
57    const MAX_EXP: i32 = f32::MAX_EXP;
58
59    /// Minimum possible normal [`flex32`](crate::frontend::flex32) power of 10 exponent
60    const MIN_10_EXP: i32 = f32::MIN_10_EXP;
61    /// One greater than the minimum possible normal [`flex32`](crate::frontend::flex32) power of 2 exponent
62    const MIN_EXP: i32 = f32::MIN_EXP;
63
64    const MIN_POSITIVE: Self = flex32::from_f32(f32::MIN_POSITIVE);
65
66    const NAN: Self = flex32::from_f32(f32::NAN);
67
68    const NEG_INFINITY: Self = flex32::from_f32(f32::NEG_INFINITY);
69
70    const RADIX: u32 = 2;
71
72    fn new(val: f32) -> Self {
73        flex32::from_f32(val)
74    }
75}
76
77impl LaunchArgExpand for flex32 {
78    type CompilationArg = ();
79
80    fn expand(_: &Self::CompilationArg, builder: &mut KernelBuilder) -> ExpandElementTyped<Self> {
81        builder.scalar(flex32::as_elem(&builder.scope)).into()
82    }
83}
84
85impl ScalarArgSettings for flex32 {
86    fn register<R: Runtime>(&self, settings: &mut KernelLauncher<R>) {
87        settings.register_f32(self.to_f32());
88    }
89}