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