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 from_const_value(value: ConstantValue) -> Self {
25        let ConstantValue::Float(value) = value else {
26            unreachable!()
27        };
28        flex32::from_f64(value)
29    }
30}
31impl ConstantTyped for flex32 {
32    fn __expand_as_type(scope: &Scope) -> TypeHandle {
33        FloatFlex32Type::get(scope.ctx()).into()
34    }
35}
36
37impl IntoRuntime for flex32 {
38    fn __expand_runtime_method(self, _scope: &Scope) -> NativeExpand<Self> {
39        self.into()
40    }
41}
42impl IntoExpand for flex32 {
43    type Expand = NativeExpand<flex32>;
44    fn into_expand(self, _: &Scope) -> Self::Expand {
45        self.into()
46    }
47}
48
49impl Numeric for flex32 {
50    fn min_value() -> Self {
51        <Self as num_traits::Float>::min_value()
52    }
53    fn max_value() -> Self {
54        <Self as num_traits::Float>::max_value()
55    }
56}
57
58impl NativeAssign for flex32 {}
59
60impl Float for flex32 {
61    const DIGITS: u32 = 32;
62
63    const EPSILON: Self = flex32::from_f32(half::f16::EPSILON.to_f32_const());
64
65    const INFINITY: Self = flex32::from_f32(f32::INFINITY);
66
67    const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS;
68
69    /// Maximum possible [`flex32`] power of 10 exponent
70    const MAX_10_EXP: i32 = f32::MAX_10_EXP;
71    /// Maximum possible [`flex32`] power of 2 exponent
72    const MAX_EXP: i32 = f32::MAX_EXP;
73
74    /// Minimum possible normal [`flex32`] power of 10 exponent
75    const MIN_10_EXP: i32 = f32::MIN_10_EXP;
76    /// One greater than the minimum possible normal [`flex32`] power of 2 exponent
77    const MIN_EXP: i32 = f32::MIN_EXP;
78
79    const MIN_POSITIVE: Self = flex32::from_f32(f32::MIN_POSITIVE);
80
81    const NAN: Self = flex32::from_f32(f32::NAN);
82
83    const NEG_INFINITY: Self = flex32::from_f32(f32::NEG_INFINITY);
84
85    const RADIX: u32 = 2;
86
87    fn new(val: f64) -> Self {
88        flex32::from_f64(val)
89    }
90}