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: Self, to: Self) -> bool
pub fn can_safe_cast(from: Self, to: Self) -> bool
Check if casting from from to to is safe (preserves value).
Sourcepub fn least_upper_dtype(dtypes: &[Self]) -> Option<Self>
pub fn least_upper_dtype(dtypes: &[Self]) -> Option<Self>
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<T>+Ptr<T>→Ptr<T>(same Ptr types)Ptr<T>+Scalar(T)→Scalar(T)(Ptr will be auto-loaded in codegen)Ptr<T>+Scalar(U)→ promoted Scalar (if T and U are compatible)
Source§impl DType
impl DType
Sourcepub fn ptr(self, size: Option<usize>, addrspace: AddrSpace) -> Self
pub fn ptr(self, size: Option<usize>, addrspace: AddrSpace) -> Self
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 svod_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) -> Self
pub fn with_base(&self, new_base: ScalarDType) -> Self
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<Self>
pub fn with_ptr_base(&self, new_base: DType) -> Option<Self>
For Ptr types: replace the base dtype while preserving addrspace, size, and vcount. Returns None if not a Ptr.