Skip to main content

cubecl_core/frontend/element/float/
relaxed.rs

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