pub enum ConstValue {
Int(i64),
UInt(u64),
Float(f64),
Bool(bool),
}Expand description
Constant value that can be stored in a UOp.
Variants§
Implementations§
Source§impl ConstValue
impl ConstValue
pub const fn dtype(&self) -> DType
pub const fn zero(dtype: ScalarDType) -> Self
pub const fn one(dtype: ScalarDType) -> Self
pub const fn neg_one(dtype: ScalarDType) -> Option<Self>
Sourcepub const fn min(dtype: ScalarDType) -> Self
pub const fn min(dtype: ScalarDType) -> Self
Minimum representable value for a scalar dtype (matches Tinygrad’s dtypes.min).
Sourcepub const fn max(dtype: ScalarDType) -> Self
pub const fn max(dtype: ScalarDType) -> Self
Maximum representable value for a scalar dtype (matches Tinygrad’s dtypes.max).
Sourcepub fn cast(&self, dtype: &DType) -> Option<Self>
pub fn cast(&self, dtype: &DType) -> Option<Self>
Cast this constant value to the target dtype.
Returns None if:
- The target dtype is not a scalar type
- The target dtype is not representable as a ConstValue (e.g., Void, Index, special float formats)
§Safety and Semantics
This method performs constant folding for cast operations and allows ALL casts (including lossy ones like float->int) since the user explicitly wrote the cast operation.
Uses Rust’s as operator for conversions, which follows C semantics:
- Truncation for narrowing conversions (e.g., i64 -> i32)
- Wrap-around for unsigned overflow
- Truncation toward zero for float-to-int conversions
For multi-stage conversions (e.g., casting through intermediate types), the value is cast to the target width and then extended back to the storage type. Example: i64 -> i8 -> i64 ensures proper sign extension.
Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if this constant is zero (additive identity).
Works for all numeric types: Int, UInt, Float, Bool.
Sourcepub const fn is_one(&self) -> bool
pub const fn is_one(&self) -> bool
Returns true if this constant is one (multiplicative identity).
Works for all numeric types: Int, UInt, Float, Bool.
Sourcepub const fn is_neg_one(&self) -> bool
pub const fn is_neg_one(&self) -> bool
Returns true if this constant is negative one.
Used for patterns like x // -1 → -x.
Sourcepub const fn try_int(&self) -> Option<i64>
pub const fn try_int(&self) -> Option<i64>
Try to extract an integer value (i64 or u64 as i64).
Used for constant pattern matching with specific integer values.
Sourcepub const fn try_float(&self) -> Option<f64>
pub const fn try_float(&self) -> Option<f64>
Try to extract a float value (f64).
Used for constant pattern matching with specific float values.
Sourcepub fn truncate(self, dtype: ScalarDType) -> Self
pub fn truncate(self, dtype: ScalarDType) -> Self
Truncate value to fit within dtype boundaries (two’s complement wrapping).
This is equivalent to Tinygrad’s ctypes-based truncation. Used for constant folding to ensure results respect the target dtype’s bit width.
Trait Implementations§
Source§impl Clone for ConstValue
impl Clone for ConstValue
Source§fn clone(&self) -> ConstValue
fn clone(&self) -> ConstValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ConstValue
impl Debug for ConstValue
Source§impl<'de> Deserialize<'de> for ConstValue
impl<'de> Deserialize<'de> for ConstValue
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<bool> for ConstValue
impl From<bool> for ConstValue
Source§impl From<f32> for ConstValue
impl From<f32> for ConstValue
Source§impl From<f64> for ConstValue
impl From<f64> for ConstValue
Source§impl From<i16> for ConstValue
impl From<i16> for ConstValue
Source§impl From<i32> for ConstValue
impl From<i32> for ConstValue
Source§impl From<i64> for ConstValue
impl From<i64> for ConstValue
Source§impl From<i8> for ConstValue
impl From<i8> for ConstValue
Source§impl From<u16> for ConstValue
impl From<u16> for ConstValue
Source§impl From<u32> for ConstValue
impl From<u32> for ConstValue
Source§impl From<u64> for ConstValue
impl From<u64> for ConstValue
Source§impl From<u8> for ConstValue
impl From<u8> for ConstValue
Source§impl Hash for ConstValue
Manual Hash impl because f64 doesn’t implement Hash.
Uses to_bits() for floats, which means NaN values with identical bit patterns hash equally.
impl Hash for ConstValue
Manual Hash impl because f64 doesn’t implement Hash. Uses to_bits() for floats, which means NaN values with identical bit patterns hash equally.