Skip to main content

cubecl_core/frontend/element/float/
relaxed.rs

1use cubecl_common::flex32;
2use cubecl_ir::{ConstantValue, ElemType, FloatKind, Scope, Type};
3
4use crate::prelude::*;
5
6use super::{CubePrimitive, CubeType, Float, IntoRuntime, NativeAssign, NativeExpand};
7
8impl CubeType for flex32 {
9    type ExpandType = NativeExpand<flex32>;
10}
11
12impl CubeDebug for flex32 {}
13impl Scalar for flex32 {}
14impl CubePrimitive for flex32 {
15    type Scalar = Self;
16    type Size = Const<1>;
17    type WithScalar<S: Scalar> = S;
18
19    /// Return the element type to use on GPU
20    fn as_type_native() -> Option<Type> {
21        Some(ElemType::Float(FloatKind::Flex32).into())
22    }
23
24    fn from_const_value(value: ConstantValue) -> Self {
25        let ConstantValue::Float(value) = value else {
26            unreachable!()
27        };
28        flex32::from_f64(value)
29    }
30}
31
32impl IntoRuntime for flex32 {
33    fn __expand_runtime_method(self, _scope: &Scope) -> NativeExpand<Self> {
34        self.into()
35    }
36}
37impl IntoExpand for flex32 {
38    type Expand = NativeExpand<flex32>;
39    fn into_expand(self, _: &Scope) -> Self::Expand {
40        self.into()
41    }
42}
43
44impl Numeric for flex32 {
45    fn min_value() -> Self {
46        <Self as num_traits::Float>::min_value()
47    }
48    fn max_value() -> Self {
49        <Self as num_traits::Float>::max_value()
50    }
51}
52
53impl NativeAssign for flex32 {}
54
55impl Float for flex32 {
56    const DIGITS: u32 = 32;
57
58    const EPSILON: Self = flex32::from_f32(half::f16::EPSILON.to_f32_const());
59
60    const INFINITY: Self = flex32::from_f32(f32::INFINITY);
61
62    const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS;
63
64    /// Maximum possible [`flex32`] power of 10 exponent
65    const MAX_10_EXP: i32 = f32::MAX_10_EXP;
66    /// Maximum possible [`flex32`] power of 2 exponent
67    const MAX_EXP: i32 = f32::MAX_EXP;
68
69    /// Minimum possible normal [`flex32`] power of 10 exponent
70    const MIN_10_EXP: i32 = f32::MIN_10_EXP;
71    /// One greater than the minimum possible normal [`flex32`] power of 2 exponent
72    const MIN_EXP: i32 = f32::MIN_EXP;
73
74    const MIN_POSITIVE: Self = flex32::from_f32(f32::MIN_POSITIVE);
75
76    const NAN: Self = flex32::from_f32(f32::NAN);
77
78    const NEG_INFINITY: Self = flex32::from_f32(f32::NEG_INFINITY);
79
80    const RADIX: u32 = 2;
81
82    fn new(val: f32) -> Self {
83        flex32::from_f32(val)
84    }
85}