mod descr;
mod kind;
pub use descr::{parse_descr, struct_from_fields, to_descr, Endian};
pub use kind::{FloatKind, IntKind};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DType {
Bool,
I8,
I16,
I32,
I64,
U8,
U16,
U32,
U64,
F8E4M3,
F8E5M2,
F16,
BF16,
F32,
F64,
F128,
X87,
C64,
C128,
C256,
Str(usize),
Bytes(usize),
Object,
Struct(Vec<Field>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Field {
pub name: String,
pub dtype: DType,
pub shape: Vec<usize>,
}
impl DType {
pub fn size(&self) -> Option<usize> {
Some(match self {
DType::Bool | DType::I8 | DType::U8 | DType::F8E4M3 | DType::F8E5M2 => 1,
DType::I16 | DType::U16 | DType::F16 | DType::BF16 => 2,
DType::I32 | DType::U32 | DType::F32 => 4,
DType::I64 | DType::U64 | DType::F64 | DType::C64 => 8,
DType::F128 | DType::C128 => 16,
DType::X87 => 16,
DType::C256 => 32,
DType::Str(n) => 4 * n,
DType::Bytes(n) => *n,
DType::Object => return None,
DType::Struct(fields) => {
let mut total = 0usize;
for f in fields {
let n: usize = f.shape.iter().product::<usize>().max(1);
total += f.dtype.size()? * n;
}
total
}
})
}
pub fn is_integer(&self) -> bool {
matches!(
self,
DType::I8
| DType::I16
| DType::I32
| DType::I64
| DType::U8
| DType::U16
| DType::U32
| DType::U64
)
}
pub fn is_float(&self) -> bool {
matches!(
self,
DType::F8E4M3
| DType::F8E5M2
| DType::F16
| DType::BF16
| DType::F32
| DType::F64
| DType::F128
| DType::X87
)
}
pub fn is_complex(&self) -> bool {
matches!(self, DType::C64 | DType::C128 | DType::C256)
}
pub fn is_numeric(&self) -> bool {
self.is_integer() || self.is_float() || self.is_complex() || *self == DType::Bool
}
}