pub enum DType {
Scalar(ScalarDType),
Vector {
scalar: ScalarDType,
count: usize,
},
Ptr {
base: Box<DType>,
addrspace: AddrSpace,
size: Option<usize>,
vcount: usize,
},
Image {
kind: ImageKind,
shape: Vec<usize>,
},
}Expand description
Data type including scalars, vectors, pointers, and images.
Variants§
Scalar(ScalarDType)
Scalar type (single value).
Vector
Vector type (SIMD).
Ptr
Pointer type.
vcount is the vector count of the pointer itself (1 = scalar pointer, >1 = vector of pointers).
This matches Tinygrad’s PtrDType.v field.
Image
Image type (for texture operations).
Implementations§
Source§impl DType
impl DType
Sourcepub fn can_safe_cast(from: DType, to: DType) -> bool
pub fn can_safe_cast(from: DType, to: DType) -> bool
Check if casting from from to to is safe (preserves value).
Sourcepub fn least_upper_dtype(dtypes: &[DType]) -> Option<DType>
pub fn least_upper_dtype(dtypes: &[DType]) -> Option<DType>
Find the least upper bound type for a set of dtypes.
Returns the smallest type that all input types can be safely cast to.
Type promotion rules:
- Scalar + Scalar → promoted Scalar
- Ptr
+ Ptr → Ptr (same Ptr types) - Ptr
+ Scalar(T) → Scalar(T) (Ptr will be auto-loaded in codegen) - Ptr
+ Scalar(U) → promoted Scalar (if T and U are compatible)
Source§impl DType
impl DType
Sourcepub fn ptr(self, size: Option<usize>, addrspace: AddrSpace) -> DType
pub fn ptr(self, size: Option<usize>, addrspace: AddrSpace) -> DType
Create a pointer type from this dtype.
pub fn scalar(&self) -> Option<ScalarDType>
Sourcepub fn base(&self) -> ScalarDType
pub fn base(&self) -> ScalarDType
Get the base scalar type (works for both scalars and vectors).
Sourcepub fn scalar_dtype(&self) -> DType
pub fn scalar_dtype(&self) -> DType
Get scalar DType (works on both Scalar and Vector).
Unlike base() which returns ScalarDType, this returns DType.
This enables chaining with .vec().
§Examples
use morok_dtype::DType;
let vec_dtype = DType::Float32.vec(4);
assert_eq!(vec_dtype.scalar_dtype(), DType::Float32);
// Enable chaining: dtype.scalar_dtype().vec(new_count)
let new_vec = vec_dtype.scalar_dtype().vec(8);
assert_eq!(new_vec, DType::Float32.vec(8));Sourcepub fn with_base(&self, new_base: ScalarDType) -> DType
pub fn with_base(&self, new_base: ScalarDType) -> DType
Create a new dtype with a different base scalar type, preserving vector count.
Useful for type conversions like bool→uint8 where the structure is preserved.
Sourcepub fn with_ptr_base(&self, new_base: DType) -> Option<DType>
pub fn with_ptr_base(&self, new_base: DType) -> Option<DType>
For Ptr types: replace the base dtype while preserving addrspace, size, and vcount. Returns None if not a Ptr.
pub fn bytes(&self) -> usize
pub fn is_bool(&self) -> bool
pub fn is_signed(&self) -> bool
pub fn is_unsigned(&self) -> bool
pub fn is_int(&self) -> bool
pub fn is_float(&self) -> bool
pub fn is_fp8(&self) -> bool
pub fn min_value(&self) -> f64
pub fn max_value(&self) -> f64
pub fn c_style(&self) -> String
Source§impl DType
impl DType
pub const fn bool_() -> DType
pub const fn int8() -> DType
pub const fn int16() -> DType
pub const fn int32() -> DType
pub const fn int64() -> DType
pub const fn uint8() -> DType
pub const fn uint16() -> DType
pub const fn uint32() -> DType
pub const fn uint64() -> DType
pub const fn float16() -> DType
pub const fn bfloat16() -> DType
pub const fn float32() -> DType
pub const fn float64() -> DType
pub const fn void_() -> DType
pub const fn index() -> DType
Source§impl DType
impl DType
pub const Bool: DType
pub const Int8: DType
pub const Int16: DType
pub const Int32: DType
pub const Int64: DType
pub const UInt8: DType
pub const UInt16: DType
pub const UInt32: DType
pub const UInt64: DType
pub const FP8E4M3: DType
pub const FP8E5M2: DType
pub const Float16: DType
pub const BFloat16: DType
pub const Float32: DType
pub const Float64: DType
pub const Void: DType
pub const Index: DType
Trait Implementations§
Source§impl<'de> Deserialize<'de> for DType
impl<'de> Deserialize<'de> for DType
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<DType, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<DType, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl From<ScalarDType> for DType
impl From<ScalarDType> for DType
Source§fn from(scalar: ScalarDType) -> DType
fn from(scalar: ScalarDType) -> DType
Source§impl Serialize for DType
impl Serialize for DType
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Eq for DType
impl StructuralPartialEq for DType
Auto Trait Implementations§
impl Freeze for DType
impl RefUnwindSafe for DType
impl Send for DType
impl Sync for DType
impl Unpin for DType
impl UnsafeUnpin for DType
impl UnwindSafe for DType
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.